|
| 1 | +package net.thunderbird.feature.notification.impl |
| 2 | + |
| 3 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 4 | +import androidx.compose.ui.unit.dp |
| 5 | +import assertk.assertThat |
| 6 | +import assertk.assertions.containsAtLeast |
| 7 | +import assertk.assertions.hasSize |
| 8 | +import assertk.assertions.isEqualTo |
| 9 | +import assertk.assertions.isNotNull |
| 10 | +import assertk.assertions.isNull |
| 11 | +import kotlin.concurrent.thread |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlinx.coroutines.runBlocking |
| 14 | +import kotlinx.coroutines.test.runTest |
| 15 | +import net.thunderbird.feature.notification.api.NotificationId |
| 16 | +import net.thunderbird.feature.notification.api.NotificationSeverity |
| 17 | +import net.thunderbird.feature.notification.api.content.AppNotification |
| 18 | +import net.thunderbird.feature.notification.api.ui.icon.NotificationIcon |
| 19 | + |
| 20 | +@Suppress("MaxLineLength") |
| 21 | +class DefaultNotificationRegistryTest { |
| 22 | + @Test |
| 23 | + fun `register should return NotificationId given notification`() = runTest { |
| 24 | + // Arrange |
| 25 | + val notification = FakeNotification() |
| 26 | + val registry = DefaultNotificationRegistry() |
| 27 | + |
| 28 | + // Act |
| 29 | + val notificationId = registry.register(notification) |
| 30 | + |
| 31 | + // Assert |
| 32 | + assertThat(registry[notificationId]) |
| 33 | + .isNotNull() |
| 34 | + .isEqualTo(notification) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `register should return same NotificationId when registering the same notification multiple times`() = runTest { |
| 39 | + // Arrange |
| 40 | + val notification = FakeNotification() |
| 41 | + val registry = DefaultNotificationRegistry() |
| 42 | + |
| 43 | + // Act |
| 44 | + val notificationId1 = registry.register(notification) |
| 45 | + val notificationId2 = registry.register(notification) |
| 46 | + |
| 47 | + // Assert |
| 48 | + assertThat(notificationId1) |
| 49 | + .isEqualTo(notificationId2) |
| 50 | + assertThat(registry[notificationId1]) |
| 51 | + .isNotNull() |
| 52 | + .isEqualTo(notification) |
| 53 | + assertThat(registry[notificationId2]) |
| 54 | + .isNotNull() |
| 55 | + .isEqualTo(notification) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `register should not register duplicated notifications when running concurrently`() = runTest { |
| 60 | + // Arrange |
| 61 | + val notificationSize = 100 |
| 62 | + val registerTries = 50 |
| 63 | + val notifications = List(size = notificationSize) { index -> |
| 64 | + FakeNotification( |
| 65 | + title = "fake notification $index", |
| 66 | + ) |
| 67 | + } |
| 68 | + val expectedNotificationIds = List(size = notificationSize) { index -> |
| 69 | + NotificationId(value = index + 1) |
| 70 | + } |
| 71 | + val registry = DefaultNotificationRegistry() |
| 72 | + |
| 73 | + // Act |
| 74 | + List(size = registerTries) { |
| 75 | + thread(start = true) { |
| 76 | + notifications.forEach { notification -> |
| 77 | + runBlocking { |
| 78 | + registry.register(notification) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }.forEach { |
| 83 | + it.join() |
| 84 | + } |
| 85 | + |
| 86 | + // Assert |
| 87 | + val registrar = registry.registrar |
| 88 | + assertThat(registrar).hasSize(notificationSize) |
| 89 | + assertThat(registrar) |
| 90 | + .containsAtLeast(elements = expectedNotificationIds.zip(notifications).toTypedArray()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `operator get Notification should return NotificationId when notification is in the registrar`() = runTest { |
| 95 | + // Arrange |
| 96 | + val notification = FakeNotification() |
| 97 | + val registry = DefaultNotificationRegistry() |
| 98 | + registry.register(notification) |
| 99 | + |
| 100 | + // Act |
| 101 | + val notificationId = registry[notification] |
| 102 | + |
| 103 | + // Assert |
| 104 | + assertThat(notificationId).isNotNull() |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `operator get Notification should return null when notification is NOT in the registrar`() = runTest { |
| 109 | + // Arrange |
| 110 | + val notification = FakeNotification() |
| 111 | + val notRegisteredNotification = FakeNotification(title = "that is not registered!!") |
| 112 | + val registry = DefaultNotificationRegistry() |
| 113 | + registry.register(notification) |
| 114 | + |
| 115 | + // Act |
| 116 | + val notificationId = registry[notRegisteredNotification] |
| 117 | + |
| 118 | + // Assert |
| 119 | + assertThat(notificationId).isNull() |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `operator get NotificationId should return Notification when notification is in the registrar`() = runTest { |
| 124 | + // Arrange |
| 125 | + val notification = FakeNotification() |
| 126 | + val registry = DefaultNotificationRegistry() |
| 127 | + val notificationId = registry.register(notification) |
| 128 | + |
| 129 | + // Act |
| 130 | + val registrarNotification = registry[notificationId] |
| 131 | + |
| 132 | + // Assert |
| 133 | + assertThat(registrarNotification).isNotNull() |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `operator get NotificationId should return null when notification is NOT in the registrar`() = runTest { |
| 138 | + // Arrange |
| 139 | + val registry = DefaultNotificationRegistry() |
| 140 | + val notification = FakeNotification() |
| 141 | + registry.register(notification) |
| 142 | + |
| 143 | + // Act |
| 144 | + val notificationId = registry[NotificationId(value = Int.MAX_VALUE)] |
| 145 | + |
| 146 | + // Assert |
| 147 | + assertThat(notificationId).isNull() |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + fun `unregister should remove notification from registrar when given a notification object and Notification is in registrar`() = |
| 152 | + runTest { |
| 153 | + // Arrange |
| 154 | + val registry = DefaultNotificationRegistry() |
| 155 | + val notification = FakeNotification() |
| 156 | + registry.register(notification) |
| 157 | + |
| 158 | + // Act |
| 159 | + registry.unregister(notification) |
| 160 | + |
| 161 | + // Assert |
| 162 | + assertThat(registry[notification]).isNull() |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + fun `unregister should remove notification from registrar when given a notification id and Notification is in registrar`() = |
| 167 | + runTest { |
| 168 | + // Arrange |
| 169 | + val registry = DefaultNotificationRegistry() |
| 170 | + val notification = FakeNotification() |
| 171 | + val notificationId = registry.register(notification) |
| 172 | + |
| 173 | + // Act |
| 174 | + registry.unregister(notificationId) |
| 175 | + |
| 176 | + // Assert |
| 177 | + assertThat(registry[notification]).isNull() |
| 178 | + } |
| 179 | + |
| 180 | + data class FakeNotification( |
| 181 | + override val title: String = "fake title", |
| 182 | + override val contentText: String? = "fake content", |
| 183 | + override val severity: NotificationSeverity = NotificationSeverity.Information, |
| 184 | + override val icon: NotificationIcon = NotificationIcon( |
| 185 | + inAppNotificationIcon = ImageVector.Builder( |
| 186 | + defaultWidth = 0.dp, |
| 187 | + defaultHeight = 0.dp, |
| 188 | + viewportWidth = 0f, |
| 189 | + viewportHeight = 0f, |
| 190 | + ).build(), |
| 191 | + ), |
| 192 | + ) : AppNotification() |
| 193 | +} |
0 commit comments