Yokan / rebuild
Course 1 8624561

ProfileNotificationsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/notifications/ProfileNotificationsTest.kt

test / fixture83 lines
1package eu.friedcutlet.yokan.services.notifications23import eu.friedcutlet.yokan.domain.ProfileId4import eu.friedcutlet.yokan.app.profiles.ProfileSession5import kotlinx.coroutines.test.runTest6import kotlin.test.Test7import kotlin.test.assertEquals8import kotlin.test.assertFalse9import kotlin.test.assertNotEquals10import kotlin.test.assertNull11import kotlin.test.assertTrue1213class ProfileNotificationsTest {14    @Test fun defaultAdoptsLegacyAndroidAndIosNotificationIdentities() {15        val namespace = ProfileNotificationNamespace(ProfileId.Default)16        for (tag in listOf(null, "", "chapter-42", "periodic.sync")) {17            assertNull(namespace.androidTag(NotificationChannel.Downloads, tag))18            assertEquals(("yokan_downloads" + tag.orEmpty()).hashCode(), namespace.androidId("yokan_downloads", tag))19            assertEquals("yokan.downloads.${tag.orEmpty()}", namespace.iosIdentifier(NotificationChannel.Downloads, tag))20        }21    }2223    @Test fun identicalAndHashCollidingTagsCannotCrossExtraOwnersOrChannels() {24        assertEquals("Aa".hashCode(), "BB".hashCode())25        val android = mutableSetOf<Pair<String?, Int>>()26        val ios = mutableSetOf<String>()27        for (id in listOf(A, B)) for (channel in NotificationChannel.entries) for (tag in listOf("Aa", "BB", "chapter-42", "")) {28            val namespace = ProfileNotificationNamespace(id)29            assertTrue(android.add(namespace.androidTag(channel, tag) to namespace.androidId("channel", tag)))30            assertTrue(ios.add(namespace.iosIdentifier(channel, tag)))31        }32        assertNotEquals(ProfileNotificationNamespace(A).androidTag(NotificationChannel.Downloads, ""),33            ProfileNotificationNamespace(A).androidTag(NotificationChannel.Downloads, "0:"))34    }3536    @Test fun aDefaultTagCannotImpersonateAnExtraOwnersNotificationKey() {37        val extra = ProfileNotificationNamespace(A)38        val forgedTag = extra.androidTag(NotificationChannel.Downloads, "chapter-42")39        val default = ProfileNotificationNamespace(ProfileId.Default)40        assertNotEquals(default.androidTag(NotificationChannel.Downloads, forgedTag) to default.androidId("channel", forgedTag),41            extra.androidTag(NotificationChannel.Downloads, "chapter-42") to extra.androidId("channel", "chapter-42"))42    }4344    @Test fun sessionForcesItsOriginAndSuppressesStaleNotificationReferences() = runTest {45        val recording = RecordingNotifications()46        val owner = ProfileSession(A, databaseFactory = { error("Notifications must not open storage") }, notificationsFactory = {47            assertEquals(A, it); recording48        })49        val captured = owner.notifications50        captured.notify(NotificationPayload(NotificationChannel.Downloads, "Title", "Progress", "chapter-42", profileId = B))51        captured.requestPermission(); captured.cancel(NotificationChannel.Downloads, "chapter-42")52        assertEquals(A, recording.posted.single().profileId)53        assertEquals(1, recording.permissionRequests); assertEquals(1, recording.cancelled.size)54        owner.close()55        captured.notify(NotificationPayload(NotificationChannel.Downloads, "Stale", "Stale"))56        captured.cancel(NotificationChannel.Downloads, "chapter-42"); captured.requestPermission()57        assertFalse(captured.supported()); assertEquals(1, recording.posted.size)58        assertEquals(1, recording.permissionRequests); assertEquals(1, recording.cancelled.size)59    }6061    @Test fun retiringOneOwnerLeavesOtherOwnersNotificationEndpointUsable() = runTest {62        val a = RecordingNotifications(); val b = RecordingNotifications()63        val left = ProfileSession(A, notificationsFactory = { a }); val right = ProfileSession(B, notificationsFactory = { b })64        val old = left.notifications; val current = right.notifications65        left.close()66        old.notify(NotificationPayload(NotificationChannel.Sync, "Old", "Old"))67        current.notify(NotificationPayload(NotificationChannel.Sync, "B", "B"))68        assertEquals(0, a.posted.size); assertEquals(B, b.posted.single().profileId); assertTrue(current.supported())69        right.close()70    }7172    @Test fun inAppMessagesKeepDeviceEventsVisibleAndSuppressForeignProfileEvents() {73        val owned = InAppMessage("A", "Private", profileId = A)74        assertTrue(owned.visibleTo(A)); assertFalse(owned.visibleTo(B)); assertFalse(owned.visibleTo(ProfileId.Default))75        val device = InAppMessage("Device", "Permission")76        assertTrue(device.visibleTo(A)); assertTrue(device.visibleTo(B)); assertTrue(device.visibleTo(ProfileId.Default))77    }7879    private companion object {80        val A = ProfileId("p_" + "a".repeat(32))81        val B = ProfileId("p_" + "b".repeat(32))82    }83}84

Search chapters, reference and source filenames.