1 package net.pterodactylus.sone.notify
3 import net.pterodactylus.util.notify.*
4 import net.pterodactylus.util.template.*
5 import org.hamcrest.MatcherAssert.*
6 import org.hamcrest.Matchers.*
8 import java.util.concurrent.atomic.*
11 * Unit test for [ListNotification].
13 class ListNotificationTest {
15 private val template = Template()
16 private val listNotification = ListNotification<String>(ID, KEY, template)
19 fun `creating a list notification sets empty iterable on element key in template context`() {
20 assertThat(template.initialContext.get(KEY) as Iterable<*>, emptyIterable())
24 @Suppress("UNCHECKED_CAST")
25 fun `list in template context gets updated when elements are added`() {
26 listNotification.add("a")
27 listNotification.add("b")
28 assertThat(template.initialContext.get(KEY) as Iterable<String>, contains("a", "b"))
32 fun `new list notification has no element`() {
33 assertThat(listNotification.elements, emptyIterable())
37 fun `new list notification is empty`() {
38 assertThat(listNotification.isEmpty, equalTo(true))
42 fun `list notification retains set elements`() {
43 listNotification.setElements(listOf("a", "b", "c"))
44 assertThat(listNotification.elements, contains("a", "b", "c"))
48 fun `list notification deduplicates set elements`() {
49 listNotification.setElements(listOf("a", "b", "a"))
50 assertThat(listNotification.elements, contains("a", "b"))
54 fun `list notification retains added elements`() {
55 listNotification.add("a")
56 listNotification.add("b")
57 listNotification.add("c")
58 assertThat(listNotification.elements, contains("a", "b", "c"))
62 fun `list notification deduplicates elements`() {
63 listNotification.add("a")
64 listNotification.add("b")
65 listNotification.add("a")
66 assertThat(listNotification.elements, contains("a", "b"))
70 fun `list notification removes correct element`() {
71 listNotification.setElements(listOf("a", "b", "c"))
72 listNotification.remove("b")
73 assertThat(listNotification.elements, contains("a", "c"))
77 fun `removing the last element dismisses the notification`() {
78 val notificationDismissed = AtomicBoolean()
79 val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) }
80 listNotification.addNotificationListener(notificationListener)
81 listNotification.add("a")
82 listNotification.remove("a")
83 assertThat(notificationDismissed.get(), equalTo(true))
87 fun `dismissing the list notification removes all elements`() {
88 listNotification.setElements(listOf("a", "b", "c"))
89 listNotification.dismiss()
90 assertThat(listNotification.elements, emptyIterable())
94 fun `list notification with different elements is not equal`() {
95 val secondNotification = ListNotification<String>(ID, KEY, template)
96 listNotification.add("a")
97 secondNotification.add("b")
98 assertThat(listNotification, not(equalTo(secondNotification)))
102 fun `list notification with different key is not equal`() {
103 val secondNotification = ListNotification<String>(ID, OTHER_KEY, template)
104 assertThat(listNotification, not(equalTo(secondNotification)))
108 fun `copied notifications have the same hash code`() {
109 val secondNotification = ListNotification(listNotification)
110 listNotification.add("a")
111 secondNotification.add("a")
112 listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime)
113 assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode()))
117 fun `list notification is not equal to other objects`() {
118 assertThat(listNotification, not(equalTo(Any())))
123 private const val ID = "notification-id"
124 private const val KEY = "element-key"
125 private const val OTHER_KEY = "other-key"