🔀 Merge “release/v81” into “master”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / notify / ListNotificationTest.kt
1 package net.pterodactylus.sone.notify
2
3 import net.pterodactylus.util.notify.*
4 import net.pterodactylus.util.template.*
5 import org.hamcrest.MatcherAssert.*
6 import org.hamcrest.Matchers.*
7 import org.junit.*
8 import java.util.concurrent.atomic.*
9
10 /**
11  * Unit test for [ListNotification].
12  */
13 class ListNotificationTest {
14
15         private val template = Template()
16         private val listNotification = ListNotification<String>(ID, KEY, template)
17
18         @Test
19         fun `creating a list notification sets empty iterable on element key in template context`() {
20                 assertThat(template.initialContext.get(KEY) as Iterable<*>, emptyIterable())
21         }
22
23         @Test
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"))
29         }
30
31         @Test
32         fun `new list notification has no element`() {
33                 assertThat(listNotification.elements, emptyIterable())
34         }
35
36         @Test
37         fun `new list notification is empty`() {
38                 assertThat(listNotification.isEmpty, equalTo(true))
39         }
40
41         @Test
42         fun `list notification retains set elements`() {
43                 listNotification.setElements(listOf("a", "b", "c"))
44                 assertThat(listNotification.elements, contains("a", "b", "c"))
45         }
46
47         @Test
48         fun `list notification deduplicates set elements`() {
49                 listNotification.setElements(listOf("a", "b", "a"))
50                 assertThat(listNotification.elements, contains("a", "b"))
51         }
52
53         @Test
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"))
59         }
60
61         @Test
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"))
67         }
68
69         @Test
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"))
74         }
75
76         @Test
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))
84         }
85
86         @Test
87         fun `dismissing the list notification removes all elements`() {
88                 listNotification.setElements(listOf("a", "b", "c"))
89                 listNotification.dismiss()
90                 assertThat(listNotification.elements, emptyIterable())
91         }
92
93         @Test
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)))
99         }
100
101         @Test
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)))
105         }
106
107         @Test
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()))
114         }
115
116         @Test
117         fun `list notification is not equal to other objects`() {
118                 assertThat(listNotification, not(equalTo(Any())))
119         }
120
121 }
122
123 private const val ID = "notification-id"
124 private const val KEY = "element-key"
125 private const val OTHER_KEY = "other-key"