From e2c6a96cd34f89bc5f7a5652918101d62081ffd9 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 4 Jan 2026 16:54:35 +0000 Subject: [PATCH] test(android): cover notification tap intent --- apps/android/app/build.gradle.kts | 5 +++ .../clawdbot/android/NodeForegroundService.kt | 22 ++++++---- .../android/NodeForegroundServiceTest.kt | 43 +++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 apps/android/app/src/test/java/com/clawdbot/android/NodeForegroundServiceTest.kt diff --git a/apps/android/app/build.gradle.kts b/apps/android/app/build.gradle.kts index f244b2dd8..31d5f0eac 100644 --- a/apps/android/app/build.gradle.kts +++ b/apps/android/app/build.gradle.kts @@ -48,6 +48,10 @@ android { lint { disable += setOf("IconLauncherShape") } + + testOptions { + unitTests.isIncludeAndroidResources = true + } } kotlin { @@ -96,6 +100,7 @@ dependencies { testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2") testImplementation("io.kotest:kotest-runner-junit5-jvm:6.0.7") testImplementation("io.kotest:kotest-assertions-core-jvm:6.0.7") + testImplementation("org.robolectric:robolectric:4.16") testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.13.3") } diff --git a/apps/android/app/src/main/java/com/clawdbot/android/NodeForegroundService.kt b/apps/android/app/src/main/java/com/clawdbot/android/NodeForegroundService.kt index 8f5001199..cbcd35385 100644 --- a/apps/android/app/src/main/java/com/clawdbot/android/NodeForegroundService.kt +++ b/apps/android/app/src/main/java/com/clawdbot/android/NodeForegroundService.kt @@ -101,16 +101,22 @@ class NodeForegroundService : Service() { val launchIntent = Intent(this, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP } - val launchPending = PendingIntent.getActivity( - this, 1, launchIntent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - ) + val launchPending = + PendingIntent.getActivity( + this, + 1, + launchIntent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, + ) val stopIntent = Intent(this, NodeForegroundService::class.java).setAction(ACTION_STOP) - val stopPending = PendingIntent.getService( - this, 2, stopIntent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - ) + val stopPending = + PendingIntent.getService( + this, + 2, + stopIntent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, + ) return NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) diff --git a/apps/android/app/src/test/java/com/clawdbot/android/NodeForegroundServiceTest.kt b/apps/android/app/src/test/java/com/clawdbot/android/NodeForegroundServiceTest.kt new file mode 100644 index 000000000..cb1c8b898 --- /dev/null +++ b/apps/android/app/src/test/java/com/clawdbot/android/NodeForegroundServiceTest.kt @@ -0,0 +1,43 @@ +package com.clawdbot.android + +import android.app.Notification +import android.content.Intent +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class NodeForegroundServiceTest { + @Test + fun buildNotificationSetsLaunchIntent() { + val service = Robolectric.buildService(NodeForegroundService::class.java).get() + val notification = buildNotification(service) + + val pendingIntent = notification.contentIntent + assertNotNull(pendingIntent) + + val savedIntent = Shadows.shadowOf(pendingIntent).savedIntent + assertNotNull(savedIntent) + assertEquals(MainActivity::class.java.name, savedIntent.component?.className) + + val expectedFlags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP + assertEquals(expectedFlags, savedIntent.flags and expectedFlags) + } + + private fun buildNotification(service: NodeForegroundService): Notification { + val method = + NodeForegroundService::class.java.getDeclaredMethod( + "buildNotification", + String::class.java, + String::class.java, + ) + method.isAccessible = true + return method.invoke(service, "Title", "Text") as Notification + } +}