1 package net.pterodactylus.sone.notify;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5 import static org.hamcrest.Matchers.emptyIterable;
6 import static org.hamcrest.Matchers.hasSize;
7 import static org.hamcrest.Matchers.is;
8 import static org.hamcrest.Matchers.sameInstance;
9 import static org.mockito.ArgumentMatchers.any;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
17 import javax.annotation.Nullable;
19 import net.pterodactylus.sone.data.Post;
20 import net.pterodactylus.sone.data.PostReply;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.sone.data.SoneOptions;
23 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
24 import net.pterodactylus.util.notify.Notification;
26 import com.google.common.base.Predicate;
27 import com.google.common.base.Predicates;
28 import com.google.inject.Guice;
29 import com.google.inject.Injector;
30 import org.hamcrest.Matchers;
31 import org.junit.Test;
32 import org.mockito.ArgumentMatchers;
35 * Unit test for {@link ListNotificationFilterTest}.
37 public class ListNotificationFilterTest {
39 private static final String LOCAL_ID = "local-id";
41 private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class);
42 private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class);
43 private final ListNotificationFilter listNotificationFilter = new ListNotificationFilter(postVisibilityFilter, replyVisibilityFilter);
45 private final Sone localSone = mock(Sone.class);
46 private final SoneOptions soneOptions = mock(SoneOptions.class);
47 private final OwnIdentity localIdentity = mock(OwnIdentity.class);
48 private final List<ListNotification<Post>> newPostNotifications = Arrays.asList(createNewPostNotification());
49 private final List<ListNotification<PostReply>> newReplyNotifications = Arrays.asList(createNewReplyNotification());
50 private final List<ListNotification<Post>> mentionNotifications = Arrays.asList(createMentionNotification());
52 public ListNotificationFilterTest() {
53 when(localSone.getId()).thenReturn(LOCAL_ID);
54 when(localSone.isLocal()).thenReturn(true);
55 when(localSone.getIdentity()).thenReturn(localIdentity);
56 when(localIdentity.getId()).thenReturn(LOCAL_ID);
57 when(localSone.getOptions()).thenReturn(soneOptions);
61 public void filterIsOnlyCreatedOnce() {
62 Injector injector = Guice.createInjector();
63 ListNotificationFilter firstFilter = injector.getInstance(ListNotificationFilter.class);
64 ListNotificationFilter secondFilter = injector.getInstance(ListNotificationFilter.class);
65 assertThat(firstFilter, sameInstance(secondFilter));
69 public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() {
70 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
71 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
72 assertThat(filteredNotifications, contains(notifications.get(0)));
75 private Notification createNewSoneNotification() {
76 ListNotification<Sone> newSoneNotification = mock(ListNotification.class);
77 when(newSoneNotification.getId()).thenReturn("new-sone-notification");
78 return newSoneNotification;
82 public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() {
83 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
84 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
85 assertThat(filteredNotifications, emptyIterable());
89 public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() {
90 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
91 when(soneOptions.isShowNewSoneNotifications()).thenReturn(true);
92 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
93 assertThat(filteredNotifications, contains(notifications.get(0)));
96 private ListNotification<Post> createNewPostNotification() {
97 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
98 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
99 when(newSoneNotification.getId()).thenReturn("new-post-notification");
100 return newSoneNotification;
104 public void newPostNotificationIsNotShownIfOptionsSetAccordingly() {
105 List<ListNotification<Post>> notifications = Arrays.asList(createNewPostNotification());
106 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
107 assertThat(filteredNotifications, hasSize(0));
110 private void activateNewPostNotifications() {
111 when(soneOptions.isShowNewPostNotifications()).thenReturn(true);
114 private boolean addPostToPostNotification(List<ListNotification<Post>> notifications) {
115 return notifications.get(0).getElements().add(mock(Post.class));
118 private void setPostVisibilityPredicate(Predicate<Post> value) {
119 when(postVisibilityFilter.isVisible(ArgumentMatchers.<Sone>any())).thenReturn(value);
123 public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() {
124 activateNewPostNotifications();
125 addPostToPostNotification(newPostNotifications);
126 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
127 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
128 assertThat(filteredNotifications, hasSize(0));
132 public void newPostNotificationIsShownIfNewPostsAreVisible() {
133 activateNewPostNotifications();
134 addPostToPostNotification(newPostNotifications);
135 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
136 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
137 assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0)));
141 public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() {
142 activateNewPostNotifications();
143 addPostToPostNotification(newPostNotifications);
144 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
145 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null);
146 assertThat(filteredNotifications, Matchers.<Notification>emptyIterable());
150 public void newPostNotificationContainsOnlyVisiblePosts() {
151 activateNewPostNotifications();
152 addPostToPostNotification(newPostNotifications);
153 addPostToPostNotification(newPostNotifications);
154 setPostVisibilityPredicate(new Predicate<Post>() {
156 public boolean apply(@Nullable Post post) {
157 return post.equals(newPostNotifications.get(0).getElements().get(1));
160 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
161 assertThat(filteredNotifications, hasSize(1));
162 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1)));
165 private ListNotification<PostReply> createNewReplyNotification() {
166 ListNotification<PostReply> newReplyNotifications = mock(ListNotification.class);
167 when(newReplyNotifications.getElements()).thenReturn(new ArrayList<PostReply>());
168 when(newReplyNotifications.getId()).thenReturn("new-reply-notification");
169 return newReplyNotifications;
172 private void activateNewReplyNotifications() {
173 when(soneOptions.isShowNewReplyNotifications()).thenReturn(true);
176 private void addReplyToNewReplyNotification(List<ListNotification<PostReply>> notifications) {
177 notifications.get(0).getElements().add(mock(PostReply.class));
180 private void setReplyVisibilityPredicate(Predicate<PostReply> value) {
181 when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
185 public void newReplyNotificationContainsOnlyVisibleReplies() {
186 activateNewReplyNotifications();
187 addReplyToNewReplyNotification(newReplyNotifications);
188 addReplyToNewReplyNotification(newReplyNotifications);
189 setReplyVisibilityPredicate(new Predicate<PostReply>() {
191 public boolean apply(@Nullable PostReply postReply) {
192 return postReply.equals(newReplyNotifications.get(0).getElements().get(1));
195 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
196 assertThat(filteredNotifications, hasSize(1));
197 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1)));
201 public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() {
202 activateNewReplyNotifications();
203 addReplyToNewReplyNotification(newReplyNotifications);
204 addReplyToNewReplyNotification(newReplyNotifications);
205 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
206 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
207 assertThat(filteredNotifications, hasSize(1));
208 assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0)));
209 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements(), hasSize(2));
213 public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() {
214 activateNewReplyNotifications();
215 addReplyToNewReplyNotification(newReplyNotifications);
216 addReplyToNewReplyNotification(newReplyNotifications);
217 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysFalse());
218 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
219 assertThat(filteredNotifications, hasSize(0));
223 public void newReplyNotificationIsNotShownIfDeactivatedInOptions() {
224 addReplyToNewReplyNotification(newReplyNotifications);
225 addReplyToNewReplyNotification(newReplyNotifications);
226 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
227 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
228 assertThat(filteredNotifications, hasSize(0));
232 public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() {
233 addReplyToNewReplyNotification(newReplyNotifications);
234 addReplyToNewReplyNotification(newReplyNotifications);
235 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
236 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null);
237 assertThat(filteredNotifications, hasSize(0));
240 private ListNotification<Post> createMentionNotification() {
241 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
242 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
243 when(newSoneNotification.getId()).thenReturn("mention-notification");
244 return newSoneNotification;
248 public void mentionNotificationContainsOnlyVisiblePosts() {
249 addPostToPostNotification(mentionNotifications);
250 addPostToPostNotification(mentionNotifications);
251 setPostVisibilityPredicate(new Predicate<Post>() {
253 public boolean apply(@Nullable Post post) {
254 return post.equals(mentionNotifications.get(0).getElements().get(1));
257 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
258 assertThat(filteredNotifications, hasSize(1));
259 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1)));
263 public void mentionNotificationIsNotShownIfNoPostsAreVisible() {
264 addPostToPostNotification(mentionNotifications);
265 addPostToPostNotification(mentionNotifications);
266 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
267 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
268 assertThat(filteredNotifications, hasSize(0));
272 public void unfilterableNotificationIsNotFiltered() {
273 Notification notification = mock(Notification.class);
274 when(notification.getId()).thenReturn("random-notification");
275 List<Notification> notifications = Arrays.asList(notification);
276 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
277 assertThat(filteredNotifications, contains(notification));