105 lines · 4.0 KB · KotlinRaw
package com.cottondb.android.platformimport android.Manifestimport android.app.NotificationManagerimport android.content.Intentimport android.content.pm.PackageManagerimport android.os.Buildimport android.os.SystemClockimport android.service.notification.StatusBarNotificationimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport org.junit.Afterimport org.junit.Assert.assertNotNullimport org.junit.Assume.assumeTrueimport org.junit.Testimport org.junit.runner.RunWith/** * Real foreground-service lifecycle test. It uses Android's visible notification and its Stop * PendingIntent as the public behaviour contract; no playback or service internals are inspected. */@RunWith(AndroidJUnit4::class)class AndroidAlarmServiceLifecycleTest { private val instrumentation = InstrumentationRegistry.getInstrumentation() private val context = instrumentation.targetContext private val notifications = context.getSystemService(NotificationManager::class.java) private var harness: PermissionHarnessActivity? = null @After fun stopServiceAndHarness() { context.stopService(Intent(context, AndroidAlarmService::class.java)) instrumentation.runOnMainSync { harness?.finish() } harness = null } @Test fun silentAlarmPublishesForegroundNotificationAndStopActionEndsIt() { assumeTrue("POST_NOTIFICATIONS is required to observe the foreground notification", notificationPermissionGranted()) foregroundHarness() context.startForegroundService( AndroidAlarmService.startIntent( context, AlarmAlert(id = FIXTURE_ID, label = "Instrumented alarm", sound = false), ), ) val visible = awaitNotification() val stop = visible.notification.actions ?.singleOrNull { action -> action.title?.toString() == "Stop" } ?.actionIntent assertNotNull("Alarm notification must expose a Stop action", stop) stop!!.send() awaitNotificationGone() } private fun foregroundHarness() { if (harness != null) return val activity = instrumentation.startActivitySync( Intent(context, PermissionHarnessActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), ) check(activity is PermissionHarnessActivity) { "Unexpected foreground harness: $activity" } harness = activity instrumentation.waitForIdleSync() } private fun notificationPermissionGranted(): Boolean { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED ) return false return notifications.areNotificationsEnabled() } private fun awaitNotification(): StatusBarNotification { val deadline = SystemClock.elapsedRealtime() + TIMEOUT_MS while (SystemClock.elapsedRealtime() < deadline) { currentAlarmNotification()?.let { return it } SystemClock.sleep(POLL_MS) } error("Alarm foreground notification did not appear.") } private fun awaitNotificationGone() { val deadline = SystemClock.elapsedRealtime() + TIMEOUT_MS while (SystemClock.elapsedRealtime() < deadline) { if (currentAlarmNotification() == null) return SystemClock.sleep(POLL_MS) } error("Alarm foreground notification did not stop.") } private fun currentAlarmNotification(): StatusBarNotification? = notifications.activeNotifications.firstOrNull { candidate -> candidate.packageName == context.packageName && candidate.id == AndroidAlarmService.NOTIFICATION_ID && candidate.notification.channelId == AndroidAlarmService.CHANNEL_ID } private companion object { const val FIXTURE_ID = "instrumented-alarm-service" const val POLL_MS = 25L const val TIMEOUT_MS = 5_000L }}