|
| 1 | +package xyz.block.buzz.mobile |
| 2 | + |
| 3 | +import android.Manifest |
| 4 | +import android.os.Build |
| 5 | +import android.os.SystemClock |
| 6 | +import android.service.notification.StatusBarNotification |
| 7 | +import androidx.core.app.NotificationCompat |
| 8 | +import androidx.core.app.NotificationManagerCompat |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import androidx.test.platform.app.InstrumentationRegistry |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Before |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.runner.RunWith |
| 15 | +import kotlin.test.assertEquals |
| 16 | +import kotlin.test.assertFalse |
| 17 | +import kotlin.test.assertTrue |
| 18 | +import kotlin.test.fail |
| 19 | + |
| 20 | +/** |
| 21 | + * The generic notification against the real notification manager: a card |
| 22 | + * kept for a lost message survives later wakes, and one the user cleared |
| 23 | + * stays cleared. |
| 24 | + */ |
| 25 | +@RunWith(AndroidJUnit4::class) |
| 26 | +class WakeNotifierGenericTest { |
| 27 | + private val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 28 | + private val manager = NotificationManagerCompat.from(context) |
| 29 | + private val notifier = WakeNotifier(context) |
| 30 | + |
| 31 | + @Before fun setUp() { |
| 32 | + if (Build.VERSION.SDK_INT >= 33) { |
| 33 | + InstrumentationRegistry.getInstrumentation().uiAutomation |
| 34 | + .grantRuntimePermission(context.packageName, Manifest.permission.POST_NOTIFICATIONS) |
| 35 | + } |
| 36 | + notifier.createChannels() |
| 37 | + manager.cancelAll() |
| 38 | + } |
| 39 | + |
| 40 | + @After fun tearDown() = manager.cancelAll() |
| 41 | + |
| 42 | + @Test fun lostGenericJourney() { |
| 43 | + // A clean wake clears an unmarked generic, as before. |
| 44 | + notifier.postGeneric() |
| 45 | + awaitGeneric(Generic.SHOWING) |
| 46 | + notifier.cancelGeneric() |
| 47 | + awaitGeneric(Generic.NONE) |
| 48 | + |
| 49 | + // The 2-second card is still up when the result reports a lost |
| 50 | + // message: it is kept, and later clean wakes leave it. |
| 51 | + notifier.postGeneric() |
| 52 | + val timerCard = awaitGeneric(Generic.SHOWING) |
| 53 | + paceUpdate() |
| 54 | + assertTrue(notifier.keepGeneric(shownThisWake = true, silent = true)) |
| 55 | + awaitRepost(timerCard, Generic.LOST) |
| 56 | + notifier.cancelGeneric() |
| 57 | + drain() |
| 58 | + assertEquals(Generic.LOST, generic()) |
| 59 | + |
| 60 | + // A later wake's 2-second card re-posts it and keeps the mark. |
| 61 | + val kept = activeGeneric()!! |
| 62 | + paceUpdate() |
| 63 | + notifier.postGeneric() |
| 64 | + awaitRepost(kept, Generic.LOST) |
| 65 | + |
| 66 | + // The user clears it. |
| 67 | + clearGeneric() |
| 68 | + awaitGeneric(Generic.NONE) |
| 69 | + |
| 70 | + // The user clears the 2-second card before the result reports a lost |
| 71 | + // message: it is not posted again. |
| 72 | + notifier.postGeneric() |
| 73 | + awaitGeneric(Generic.SHOWING) |
| 74 | + clearGeneric() |
| 75 | + awaitGeneric(Generic.NONE) |
| 76 | + assertFalse(notifier.keepGeneric(shownThisWake = true, silent = true)) |
| 77 | + drain() |
| 78 | + assertEquals(Generic.NONE, generic()) |
| 79 | + |
| 80 | + // Nothing posted this wake yet: the kept card is posted. |
| 81 | + assertTrue(notifier.keepGeneric(shownThisWake = false, silent = false)) |
| 82 | + awaitGeneric(Generic.LOST) |
| 83 | + } |
| 84 | + |
| 85 | + private enum class Generic { NONE, SHOWING, LOST } |
| 86 | + |
| 87 | + // As a swipe or tap does: the platform cancels the card, not the handler. |
| 88 | + private fun clearGeneric() = manager.cancel("generic", 2) |
| 89 | + |
| 90 | + // The notification service applies posts and cancels asynchronously, so |
| 91 | + // each step waits, within a bound, for the state it expects. |
| 92 | + private fun awaitGeneric(expected: Generic): StatusBarNotification? { |
| 93 | + await("generic $expected, was ${generic()}") { generic() == expected } |
| 94 | + return activeGeneric() |
| 95 | + } |
| 96 | + |
| 97 | + // Waits for a new post of the card, so the state read is the re-post's |
| 98 | + // and not the record it replaced. |
| 99 | + private fun awaitRepost(previous: StatusBarNotification?, expected: Generic) { |
| 100 | + await("a re-post of the generic") { |
| 101 | + activeGeneric()?.let { it.postTime != previous?.postTime } == true |
| 102 | + } |
| 103 | + assertEquals(expected, generic()) |
| 104 | + } |
| 105 | + |
| 106 | + // Android silently drops an update to a showing card while the app |
| 107 | + // posts faster than max_notification_enqueue_rate (5 a second), and the |
| 108 | + // estimate carries over between runs. 700 ms with no post keeps the |
| 109 | + // rate under that, so quick reruns don't shed the test's updates. |
| 110 | + private fun paceUpdate() = SystemClock.sleep(700) |
| 111 | + |
| 112 | + // Before asserting that nothing changed: a barrier card posted and |
| 113 | + // cancelled after the step is applied after it too. |
| 114 | + private fun drain() { |
| 115 | + manager.notify( |
| 116 | + "barrier", |
| 117 | + 3, |
| 118 | + NotificationCompat.Builder(context, WakeNotifier.OTHER_CHANNEL) |
| 119 | + .setSmallIcon(android.R.drawable.stat_notify_chat) |
| 120 | + .build(), |
| 121 | + ) |
| 122 | + await("the barrier card") { manager.activeNotifications.any { it.tag == "barrier" } } |
| 123 | + manager.cancel("barrier", 3) |
| 124 | + await("the barrier card to clear") { manager.activeNotifications.none { it.tag == "barrier" } } |
| 125 | + } |
| 126 | + |
| 127 | + private fun await(what: String, condition: () -> Boolean) { |
| 128 | + val deadline = SystemClock.uptimeMillis() + 5_000 |
| 129 | + while (!condition()) { |
| 130 | + if (SystemClock.uptimeMillis() > deadline) fail("timed out waiting for $what") |
| 131 | + Thread.sleep(20) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private fun activeGeneric(): StatusBarNotification? { |
| 136 | + val active = manager.activeNotifications.filter { it.tag == "generic" && it.id == 2 } |
| 137 | + assertTrue(active.size <= 1) |
| 138 | + return active.firstOrNull() |
| 139 | + } |
| 140 | + |
| 141 | + private fun generic(): Generic { |
| 142 | + val card = activeGeneric() ?: return Generic.NONE |
| 143 | + return if (card.notification.extras.getBoolean("xyz.block.buzz.mobile.lost")) Generic.LOST else Generic.SHOWING |
| 144 | + } |
| 145 | +} |
0 commit comments