X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationTest.kt;h=4c820359e5bda04326ad0dfddae736f77da4f8cd;hp=0000000000000000000000000000000000000000;hb=d50730f6a330439e0e7ef97ca9329dffe72d5640;hpb=97fe04482ebb8a08e43294acde041c2975cbd8ee diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt new file mode 100644 index 0000000..4c82035 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt @@ -0,0 +1,125 @@ +package net.pterodactylus.sone.notify + +import net.pterodactylus.util.notify.* +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import java.util.concurrent.atomic.* + +/** + * Unit test for [ListNotification]. + */ +class ListNotificationTest { + + private val template = Template() + private val listNotification = ListNotification(ID, KEY, template) + + @Test + fun `creating a list notification sets empty iterable on element key in template context`() { + assertThat(template.initialContext.get(KEY) as Iterable<*>, emptyIterable()) + } + + @Test + @Suppress("UNCHECKED_CAST") + fun `list in template context gets updated when elements are added`() { + listNotification.add("a") + listNotification.add("b") + assertThat(template.initialContext.get(KEY) as Iterable, contains("a", "b")) + } + + @Test + fun `new list notification has no element`() { + assertThat(listNotification.elements, emptyIterable()) + } + + @Test + fun `new list notification is empty`() { + assertThat(listNotification.isEmpty, equalTo(true)) + } + + @Test + fun `list notification retains set elements`() { + listNotification.setElements(listOf("a", "b", "c")) + assertThat(listNotification.elements, contains("a", "b", "c")) + } + + @Test + fun `list notification deduplicates set elements`() { + listNotification.setElements(listOf("a", "b", "a")) + assertThat(listNotification.elements, contains("a", "b")) + } + + @Test + fun `list notification retains added elements`() { + listNotification.add("a") + listNotification.add("b") + listNotification.add("c") + assertThat(listNotification.elements, contains("a", "b", "c")) + } + + @Test + fun `list notification deduplicates elements`() { + listNotification.add("a") + listNotification.add("b") + listNotification.add("a") + assertThat(listNotification.elements, contains("a", "b")) + } + + @Test + fun `list notification removes correct element`() { + listNotification.setElements(listOf("a", "b", "c")) + listNotification.remove("b") + assertThat(listNotification.elements, contains("a", "c")) + } + + @Test + fun `removing the last element dismisses the notification`() { + val notificationDismissed = AtomicBoolean() + val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) } + listNotification.addNotificationListener(notificationListener) + listNotification.add("a") + listNotification.remove("a") + assertThat(notificationDismissed.get(), equalTo(true)) + } + + @Test + fun `dismissing the list notification removes all elements`() { + listNotification.setElements(listOf("a", "b", "c")) + listNotification.dismiss() + assertThat(listNotification.elements, emptyIterable()) + } + + @Test + fun `list notification with different elements is not equal`() { + val secondNotification = ListNotification(ID, KEY, template) + listNotification.add("a") + secondNotification.add("b") + assertThat(listNotification, not(equalTo(secondNotification))) + } + + @Test + fun `list notification with different key is not equal`() { + val secondNotification = ListNotification(ID, OTHER_KEY, template) + assertThat(listNotification, not(equalTo(secondNotification))) + } + + @Test + fun `copied notifications have the same hash code`() { + val secondNotification = ListNotification(listNotification) + listNotification.add("a") + secondNotification.add("a") + listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime) + assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode())) + } + + @Test + fun `list notification is not equal to other objects`() { + assertThat(listNotification, not(equalTo(Any()))) + } + +} + +private const val ID = "notification-id" +private const val KEY = "element-key" +private const val OTHER_KEY = "other-key"