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 retains added elements`() {
41 listNotification.add("a")
42 listNotification.add("b")
43 listNotification.add("c")
44 assertThat(listNotification.elements, contains("a", "b", "c"))
48 fun `list notification deduplicates elements`() {
49 listNotification.add("a")
50 listNotification.add("b")
51 listNotification.add("a")
52 assertThat(listNotification.elements, contains("a", "b"))
56 fun `list notification removes correct element`() {
57 listNotification.setElements(listOf("a", "b", "c"))
58 listNotification.remove("b")
59 assertThat(listNotification.elements, contains("a", "c"))
63 fun `removing the last element dismisses the notification`() {
64 val notificationDismissed = AtomicBoolean()
65 val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) }
66 listNotification.addNotificationListener(notificationListener)
67 listNotification.add("a")
68 listNotification.remove("a")
69 assertThat(notificationDismissed.get(), equalTo(true))
73 fun `dismissing the list notification removes all elements`() {
74 listNotification.setElements(listOf("a", "b", "c"))
75 listNotification.dismiss()
76 assertThat(listNotification.elements, emptyIterable())
80 fun `list notification with different elements is not equal`() {
81 val secondNotification = ListNotification<String>(ID, KEY, template)
82 listNotification.add("a")
83 secondNotification.add("b")
84 assertThat(listNotification, not(equalTo(secondNotification)))
88 fun `list notification with different key is not equal`() {
89 val secondNotification = ListNotification<String>(ID, OTHER_KEY, template)
90 assertThat(listNotification, not(equalTo(secondNotification)))
94 fun `copied notifications have the same hash code`() {
95 val secondNotification = ListNotification(listNotification)
96 listNotification.add("a")
97 secondNotification.add("a")
98 listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime)
99 assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode()))
103 fun `list notification is not equal to other objects`() {
104 assertThat(listNotification, not(equalTo(Any())))
109 private const val ID = "notification-id"
110 private const val KEY = "element-key"
111 private const val OTHER_KEY = "other-key"