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 fun `new list notification has no element`() {
25 assertThat(listNotification.elements, emptyIterable())
29 fun `new list notification is empty`() {
30 assertThat(listNotification.isEmpty, equalTo(true))
34 fun `list notification retains set elements`() {
35 listNotification.setElements(listOf("a", "b", "c"))
36 assertThat(listNotification.elements, contains("a", "b", "c"))
40 fun `list notification deduplicates set elements`() {
41 listNotification.setElements(listOf("a", "b", "a"))
42 assertThat(listNotification.elements, contains("a", "b"))
46 fun `list notification retains added elements`() {
47 listNotification.add("a")
48 listNotification.add("b")
49 listNotification.add("c")
50 assertThat(listNotification.elements, contains("a", "b", "c"))
54 fun `list notification deduplicates elements`() {
55 listNotification.add("a")
56 listNotification.add("b")
57 listNotification.add("a")
58 assertThat(listNotification.elements, contains("a", "b"))
62 fun `list notification removes correct element`() {
63 listNotification.setElements(listOf("a", "b", "c"))
64 listNotification.remove("b")
65 assertThat(listNotification.elements, contains("a", "c"))
69 fun `removing the last element dismisses the notification`() {
70 val notificationDismissed = AtomicBoolean()
71 val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) }
72 listNotification.addNotificationListener(notificationListener)
73 listNotification.add("a")
74 listNotification.remove("a")
75 assertThat(notificationDismissed.get(), equalTo(true))
79 fun `dismissing the list notification removes all elements`() {
80 listNotification.setElements(listOf("a", "b", "c"))
81 listNotification.dismiss()
82 assertThat(listNotification.elements, emptyIterable())
86 fun `list notification with different elements is not equal`() {
87 val secondNotification = ListNotification<String>(ID, KEY, template)
88 listNotification.add("a")
89 secondNotification.add("b")
90 assertThat(listNotification, not(equalTo(secondNotification)))
94 fun `list notification with different key is not equal`() {
95 val secondNotification = ListNotification<String>(ID, OTHER_KEY, template)
96 assertThat(listNotification, not(equalTo(secondNotification)))
100 fun `copied notifications have the same hash code`() {
101 val secondNotification = ListNotification(listNotification)
102 listNotification.add("a")
103 secondNotification.add("a")
104 listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime)
105 assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode()))
109 fun `list notification is not equal to other objects`() {
110 assertThat(listNotification, not(equalTo(Any())))
115 private const val ID = "notification-id"
116 private const val KEY = "element-key"
117 private const val OTHER_KEY = "other-key"