X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationTest.kt;h=08d4829a9d4fc8f5be959ca7a934d0ea97167365;hb=e399e8596825ef7480f9803166f0b41b0cad3391;hp=0000000000000000000000000000000000000000;hpb=91e2eb453b5ad6345cb3587e7cc6326b6c3831f0;p=Sone.git 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..08d4829 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt @@ -0,0 +1,103 @@ +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 + 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 retains added elements`() { + listNotification.add("a") + listNotification.add("b") + listNotification.add("c") + assertThat(listNotification.elements, contains("a", "b", "c")) + } + + @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"