08d4829a9d4fc8f5be959ca7a934d0ea97167365
[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         fun `new list notification has no element`() {
25                 assertThat(listNotification.elements, emptyIterable())
26         }
27
28         @Test
29         fun `new list notification is empty`() {
30                 assertThat(listNotification.isEmpty, equalTo(true))
31         }
32
33         @Test
34         fun `list notification retains set elements`() {
35                 listNotification.setElements(listOf("a", "b", "c"))
36                 assertThat(listNotification.elements, contains("a", "b", "c"))
37         }
38
39         @Test
40         fun `list notification retains added elements`() {
41                 listNotification.add("a")
42                 listNotification.add("b")
43                 listNotification.add("c")
44                 assertThat(listNotification.elements, contains("a", "b", "c"))
45         }
46
47         @Test
48         fun `list notification removes correct element`() {
49                 listNotification.setElements(listOf("a", "b", "c"))
50                 listNotification.remove("b")
51                 assertThat(listNotification.elements, contains("a", "c"))
52         }
53
54         @Test
55         fun `removing the last element dismisses the notification`() {
56                 val notificationDismissed = AtomicBoolean()
57                 val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) }
58                 listNotification.addNotificationListener(notificationListener)
59                 listNotification.add("a")
60                 listNotification.remove("a")
61                 assertThat(notificationDismissed.get(), equalTo(true))
62         }
63
64         @Test
65         fun `dismissing the list notification removes all elements`() {
66                 listNotification.setElements(listOf("a", "b", "c"))
67                 listNotification.dismiss()
68                 assertThat(listNotification.elements, emptyIterable())
69         }
70
71         @Test
72         fun `list notification with different elements is not equal`() {
73                 val secondNotification = ListNotification<String>(ID, KEY, template)
74                 listNotification.add("a")
75                 secondNotification.add("b")
76                 assertThat(listNotification, not(equalTo(secondNotification)))
77         }
78
79         @Test
80         fun `list notification with different key is not equal`() {
81                 val secondNotification = ListNotification<String>(ID, OTHER_KEY, template)
82                 assertThat(listNotification, not(equalTo(secondNotification)))
83         }
84
85         @Test
86         fun `copied notifications have the same hash code`() {
87                 val secondNotification = ListNotification(listNotification)
88                 listNotification.add("a")
89                 secondNotification.add("a")
90                 listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime)
91                 assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode()))
92         }
93
94         @Test
95         fun `list notification is not equal to other objects`() {
96                 assertThat(listNotification, not(equalTo(Any())))
97         }
98
99 }
100
101 private const val ID = "notification-id"
102 private const val KEY = "element-key"
103 private const val OTHER_KEY = "other-key"