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.Matchers.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;
34 * Unit test for {@link ListNotificationFilterTest}.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class ListNotificationFilterTest {
40 private static final String LOCAL_ID = "local-id";
42 private final PostVisibilityFilter postVisibilityFilter = mock(PostVisibilityFilter.class);
43 private final ReplyVisibilityFilter replyVisibilityFilter = mock(ReplyVisibilityFilter.class);
44 private final ListNotificationFilter listNotificationFilter = new ListNotificationFilter(postVisibilityFilter, replyVisibilityFilter);
46 private final Sone localSone = mock(Sone.class);
47 private final SoneOptions soneOptions = mock(SoneOptions.class);
48 private final OwnIdentity localIdentity = mock(OwnIdentity.class);
49 private final List<ListNotification<Post>> newPostNotifications = Arrays.asList(createNewPostNotification());
50 private final List<ListNotification<PostReply>> newReplyNotifications = Arrays.asList(createNewReplyNotification());
51 private final List<ListNotification<Post>> mentionNotifications = Arrays.asList(createMentionNotification());
53 public ListNotificationFilterTest() {
54 when(localSone.getId()).thenReturn(LOCAL_ID);
55 when(localSone.isLocal()).thenReturn(true);
56 when(localSone.getIdentity()).thenReturn(localIdentity);
57 when(localIdentity.getId()).thenReturn(LOCAL_ID);
58 when(localSone.getOptions()).thenReturn(soneOptions);
62 public void filterIsOnlyCreatedOnce() {
63 Injector injector = Guice.createInjector();
64 ListNotificationFilter firstFilter = injector.getInstance(ListNotificationFilter.class);
65 ListNotificationFilter secondFilter = injector.getInstance(ListNotificationFilter.class);
66 assertThat(firstFilter, sameInstance(secondFilter));
70 public void newSoneNotificationsAreNotRemovedIfNotLoggedIn() {
71 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
72 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
73 assertThat(filteredNotifications, contains(notifications.get(0)));
76 private Notification createNewSoneNotification() {
77 ListNotification<Sone> newSoneNotification = mock(ListNotification.class);
78 when(newSoneNotification.getId()).thenReturn("new-sone-notification");
79 return newSoneNotification;
83 public void newSoneNotificationsAreRemovedIfLoggedInAndNewSonesShouldNotBeShown() {
84 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
85 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
86 assertThat(filteredNotifications, emptyIterable());
90 public void newSoneNotificationsAreNotRemovedIfLoggedInAndNewSonesShouldBeShown() {
91 List<Notification> notifications = Arrays.asList(createNewSoneNotification());
92 when(soneOptions.isShowNewSoneNotifications()).thenReturn(true);
93 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
94 assertThat(filteredNotifications, contains(notifications.get(0)));
97 private ListNotification<Post> createNewPostNotification() {
98 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
99 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
100 when(newSoneNotification.getId()).thenReturn("new-post-notification");
101 return newSoneNotification;
105 public void newPostNotificationIsNotShownIfOptionsSetAccordingly() {
106 List<ListNotification<Post>> notifications = Arrays.asList(createNewPostNotification());
107 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, localSone);
108 assertThat(filteredNotifications, hasSize(0));
111 private void activateNewPostNotifications() {
112 when(soneOptions.isShowNewPostNotifications()).thenReturn(true);
115 private boolean addPostToPostNotification(List<ListNotification<Post>> notifications) {
116 return notifications.get(0).getElements().add(mock(Post.class));
119 private void setPostVisibilityPredicate(Predicate<Post> value) {
120 when(postVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
124 public void newPostNotificationIsNotShownIfNoNewPostsAreVisible() {
125 activateNewPostNotifications();
126 addPostToPostNotification(newPostNotifications);
127 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
128 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
129 assertThat(filteredNotifications, hasSize(0));
133 public void newPostNotificationIsShownIfNewPostsAreVisible() {
134 activateNewPostNotifications();
135 addPostToPostNotification(newPostNotifications);
136 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
137 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
138 assertThat(filteredNotifications, contains((Notification) newPostNotifications.get(0)));
142 public void newPostNotificationIsNotShownIfNewPostsAreVisibleButLocalSoneIsNull() {
143 activateNewPostNotifications();
144 addPostToPostNotification(newPostNotifications);
145 setPostVisibilityPredicate(Predicates.<Post>alwaysTrue());
146 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, null);
147 assertThat(filteredNotifications, Matchers.<Notification>emptyIterable());
151 public void newPostNotificationContainsOnlyVisiblePosts() {
152 activateNewPostNotifications();
153 addPostToPostNotification(newPostNotifications);
154 addPostToPostNotification(newPostNotifications);
155 setPostVisibilityPredicate(new Predicate<Post>() {
157 public boolean apply(@Nullable Post post) {
158 return post.equals(newPostNotifications.get(0).getElements().get(1));
161 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newPostNotifications, localSone);
162 assertThat(filteredNotifications, hasSize(1));
163 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(newPostNotifications.get(0).getElements().get(1)));
166 private ListNotification<PostReply> createNewReplyNotification() {
167 ListNotification<PostReply> newReplyNotifications = mock(ListNotification.class);
168 when(newReplyNotifications.getElements()).thenReturn(new ArrayList<PostReply>());
169 when(newReplyNotifications.getId()).thenReturn("new-reply-notification");
170 return newReplyNotifications;
173 private void activateNewReplyNotifications() {
174 when(soneOptions.isShowNewReplyNotifications()).thenReturn(true);
177 private void addReplyToNewReplyNotification(List<ListNotification<PostReply>> notifications) {
178 notifications.get(0).getElements().add(mock(PostReply.class));
181 private void setReplyVisibilityPredicate(Predicate<PostReply> value) {
182 when(replyVisibilityFilter.isVisible(any(Sone.class))).thenReturn(value);
186 public void newReplyNotificationContainsOnlyVisibleReplies() {
187 activateNewReplyNotifications();
188 addReplyToNewReplyNotification(newReplyNotifications);
189 addReplyToNewReplyNotification(newReplyNotifications);
190 setReplyVisibilityPredicate(new Predicate<PostReply>() {
192 public boolean apply(@Nullable PostReply postReply) {
193 return postReply.equals(newReplyNotifications.get(0).getElements().get(1));
196 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
197 assertThat(filteredNotifications, hasSize(1));
198 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements().get(0), is(newReplyNotifications.get(0).getElements().get(1)));
202 public void newReplyNotificationIsNotModifiedIfAllRepliesAreVisible() {
203 activateNewReplyNotifications();
204 addReplyToNewReplyNotification(newReplyNotifications);
205 addReplyToNewReplyNotification(newReplyNotifications);
206 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
207 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
208 assertThat(filteredNotifications, hasSize(1));
209 assertThat(filteredNotifications.get(0), is((Notification) newReplyNotifications.get(0)));
210 assertThat(((ListNotification<PostReply>) filteredNotifications.get(0)).getElements(), hasSize(2));
214 public void newReplyNotificationIsNotShownIfNoRepliesAreVisible() {
215 activateNewReplyNotifications();
216 addReplyToNewReplyNotification(newReplyNotifications);
217 addReplyToNewReplyNotification(newReplyNotifications);
218 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysFalse());
219 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
220 assertThat(filteredNotifications, hasSize(0));
224 public void newReplyNotificationIsNotShownIfDeactivatedInOptions() {
225 addReplyToNewReplyNotification(newReplyNotifications);
226 addReplyToNewReplyNotification(newReplyNotifications);
227 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
228 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, localSone);
229 assertThat(filteredNotifications, hasSize(0));
233 public void newReplyNotificationIsNotShownIfCurrentSoneIsNull() {
234 addReplyToNewReplyNotification(newReplyNotifications);
235 addReplyToNewReplyNotification(newReplyNotifications);
236 setReplyVisibilityPredicate(Predicates.<PostReply>alwaysTrue());
237 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(newReplyNotifications, null);
238 assertThat(filteredNotifications, hasSize(0));
241 private ListNotification<Post> createMentionNotification() {
242 ListNotification<Post> newSoneNotification = mock(ListNotification.class);
243 when(newSoneNotification.getElements()).thenReturn(new ArrayList<Post>());
244 when(newSoneNotification.getId()).thenReturn("mention-notification");
245 return newSoneNotification;
249 public void mentionNotificationContainsOnlyVisiblePosts() {
250 addPostToPostNotification(mentionNotifications);
251 addPostToPostNotification(mentionNotifications);
252 setPostVisibilityPredicate(new Predicate<Post>() {
254 public boolean apply(@Nullable Post post) {
255 return post.equals(mentionNotifications.get(0).getElements().get(1));
258 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
259 assertThat(filteredNotifications, hasSize(1));
260 assertThat(((ListNotification<Post>) filteredNotifications.get(0)).getElements().get(0), is(mentionNotifications.get(0).getElements().get(1)));
264 public void mentionNotificationIsNotShownIfNoPostsAreVisible() {
265 addPostToPostNotification(mentionNotifications);
266 addPostToPostNotification(mentionNotifications);
267 setPostVisibilityPredicate(Predicates.<Post>alwaysFalse());
268 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(mentionNotifications, localSone);
269 assertThat(filteredNotifications, hasSize(0));
273 public void unfilterableNotificationIsNotFiltered() {
274 Notification notification = mock(Notification.class);
275 when(notification.getId()).thenReturn("random-notification");
276 List<Notification> notifications = Arrays.asList(notification);
277 List<Notification> filteredNotifications = listNotificationFilter.filterNotifications(notifications, null);
278 assertThat(filteredNotifications, contains(notification));