1 package net.pterodactylus.sone.notify
3 import com.google.inject.Guice
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.PostReply
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.createLocalSone
8 import net.pterodactylus.sone.test.createPost
9 import net.pterodactylus.sone.test.createPostReply
10 import net.pterodactylus.sone.test.verifySingletonInstance
11 import net.pterodactylus.util.notify.Notification
12 import net.pterodactylus.util.notify.TemplateNotification
13 import net.pterodactylus.util.template.Template
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.contains
16 import org.hamcrest.Matchers.emptyIterable
17 import org.hamcrest.Matchers.equalTo
18 import org.hamcrest.Matchers.hasSize
22 * Unit test for [ListNotificationFilterTest].
24 class ListNotificationFilterTest {
26 private val listNotificationFilter = ListNotificationFilter(showAllPosts, showAllReplies)
29 fun `filter is only created once`() {
30 val injector = Guice.createInjector()
31 injector.verifySingletonInstance<ListNotificationFilter>()
35 fun `new sone notifications are not removed if not logged in`() {
36 val notification = createNewSoneNotification()
37 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null)
38 assertThat(filteredNotifications, contains<Notification>(notification))
42 fun `new sone notifications are removed if logged in and new sones should not be shown`() {
43 val notification = createNewSoneNotification()
44 val localSone = createLocalSone()
45 localSone.options.isShowNewSoneNotifications = false
46 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone)
47 assertThat(filteredNotifications, emptyIterable())
51 fun `new sone notifications are not removed if logged in and new sones should be shown`() {
52 val notification = createNewSoneNotification()
53 val localSone = createLocalSone()
54 localSone.options.isShowNewSoneNotifications = true
55 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), localSone)
56 assertThat(filteredNotifications, contains<Notification>(notification))
60 fun `new post notification is not shown if options set accordingly`() {
61 val newPostNotification = createNewPostNotification()
62 newPostNotification.add(createPost())
63 val localSone = createLocalSone()
64 localSone.options.isShowNewPostNotifications = false
65 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
66 assertThat(filteredNotifications, emptyIterable())
70 fun `new post notification is not shown if no new posts are visible`() {
71 val localSone = createLocalSone()
72 localSone.options.isShowNewPostNotifications = true
73 val newPostNotification = createNewPostNotification()
74 newPostNotification.add(createPost())
75 val listNotificationFilter = ListNotificationFilter(showNoPosts, showAllReplies)
76 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
77 assertThat(filteredNotifications, emptyIterable())
81 fun `new post notification is shown if new posts are visible`() {
82 val localSone = createLocalSone()
83 localSone.options.isShowNewPostNotifications = true
84 val newPostNotification = createNewPostNotification()
85 newPostNotification.add(createPost())
86 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
87 assertThat(filteredNotifications, contains<Notification>(newPostNotification))
91 fun `new post notification is not shown if new posts are visible but local sone is null`() {
92 val localSone = createLocalSone()
93 localSone.options.isShowNewPostNotifications = true
94 val newPostNotification = createNewPostNotification()
95 newPostNotification.add(createPost())
96 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), null)
97 assertThat(filteredNotifications, emptyIterable())
101 fun `new post notification contains only visible posts`() {
102 val localSone = createLocalSone()
103 localSone.options.isShowNewPostNotifications = true
104 val newPostNotification = createNewPostNotification()
105 newPostNotification.add(createPost())
106 newPostNotification.add(createPost())
107 val listNotificationFilter = ListNotificationFilter(matchThisPost(newPostNotification.elements[1]), showAllReplies)
108 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newPostNotification), localSone)
109 assertThat((filteredNotifications[0] as ListNotification<Post>).elements, contains(newPostNotification.elements[1]))
113 fun `new reply notification contains only visible replies`() {
114 val localSone = createLocalSone()
115 localSone.options.isShowNewReplyNotifications = true
116 val newReplyNotification = createNewReplyNotification()
117 newReplyNotification.add(createPostReply())
118 newReplyNotification.add(createPostReply())
119 val listNotificationFilter = ListNotificationFilter(showAllPosts, matchThisReply(newReplyNotification.elements[1]))
120 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
121 assertThat(filteredNotifications, hasSize(1))
122 assertThat((filteredNotifications[0] as ListNotification<PostReply?>).elements[0], equalTo(newReplyNotification.elements[1]))
126 fun `new reply notification is not modified if all replies are visible`() {
127 val localSone = createLocalSone()
128 localSone.options.isShowNewReplyNotifications = true
129 val newReplyNotification = createNewReplyNotification()
130 newReplyNotification.add(createPostReply())
131 newReplyNotification.add(createPostReply())
132 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
133 assertThat(filteredNotifications, contains<Notification>(newReplyNotification))
137 fun `new reply notification is not shown if no replies are visible`() {
138 val localSone = createLocalSone()
139 localSone.options.isShowNewReplyNotifications = true
140 val newReplyNotification = createNewReplyNotification()
141 newReplyNotification.add(createPostReply())
142 newReplyNotification.add(createPostReply())
143 val listNotificationFilter = ListNotificationFilter(showAllPosts, showNoReplies)
144 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
145 assertThat(filteredNotifications, emptyIterable())
149 fun `new reply notification is not shown if deactivated in options`() {
150 val localSone = createLocalSone()
151 localSone.options.isShowNewReplyNotifications = false
152 val newReplyNotification = createNewReplyNotification()
153 newReplyNotification.add(createPostReply())
154 newReplyNotification.add(createPostReply())
155 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), localSone)
156 assertThat(filteredNotifications, emptyIterable())
160 fun `new reply notification is not shown if current sone is null`() {
161 val newReplyNotification = createNewReplyNotification()
162 newReplyNotification.add(createPostReply())
163 newReplyNotification.add(createPostReply())
164 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(newReplyNotification), null)
165 assertThat(filteredNotifications, emptyIterable())
169 fun `mention notification contains only visible posts`() {
170 val mentionNotification = createMentionNotification()
171 mentionNotification.add(createPost())
172 mentionNotification.add(createPost())
173 val listNotificationFilter = ListNotificationFilter(matchThisPost(mentionNotification.elements[1]), showAllReplies)
174 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null)
175 assertThat(filteredNotifications, hasSize(1))
176 assertThat((filteredNotifications[0] as ListNotification<Post?>).elements[0], equalTo(mentionNotification.elements[1]))
180 fun `mention notification is not shown if no posts are visible`() {
181 val mentionNotification = createMentionNotification()
182 mentionNotification.add(createPost())
183 mentionNotification.add(createPost())
184 val listNotificationFilter = ListNotificationFilter(showNoPosts, showAllReplies)
185 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(mentionNotification), null)
186 assertThat(filteredNotifications, emptyIterable())
191 fun `unfilterable notification is not filtered`() {
192 val notification: Notification = TemplateNotification("random-notification", Template())
193 val filteredNotifications = listNotificationFilter.filterNotifications(listOf(notification), null)
194 assertThat(filteredNotifications, contains(notification))
199 private fun createNewSoneNotification() =
200 ListNotification<Sone>("new-sone-notification", "", Template())
202 private fun createNewPostNotification() =
203 ListNotification<Post>("new-post-notification", "", Template())
205 private fun createNewReplyNotification() =
206 ListNotification<PostReply>("new-reply-notification", "", Template())
208 private fun createMentionNotification() =
209 ListNotification<Post>("mention-notification", "", Template())