OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 13 matching lines...) Expand all Loading... |
24 import org.adblockplus.libadblockplus.LazyWebRequest; | 24 import org.adblockplus.libadblockplus.LazyWebRequest; |
25 import org.adblockplus.libadblockplus.Notification; | 25 import org.adblockplus.libadblockplus.Notification; |
26 import org.adblockplus.libadblockplus.ServerResponse; | 26 import org.adblockplus.libadblockplus.ServerResponse; |
27 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 27 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
28 import org.adblockplus.libadblockplus.WebRequest; | 28 import org.adblockplus.libadblockplus.WebRequest; |
29 | 29 |
30 import org.junit.Test; | 30 import org.junit.Test; |
31 | 31 |
32 import java.util.List; | 32 import java.util.List; |
33 | 33 |
34 public class NotificationTest extends BaseJsTest { | 34 public class NotificationTest extends BaseJsTest |
| 35 { |
35 | 36 |
36 protected FilterEngine filterEngine; | 37 protected FilterEngine filterEngine; |
| 38 |
| 39 @Override |
| 40 protected void setUp() throws Exception |
| 41 { |
| 42 super.setUp(); |
| 43 |
| 44 jsEngine.setWebRequest(new LazyWebRequest()); |
| 45 filterEngine = new FilterEngine(jsEngine); |
| 46 } |
| 47 |
| 48 protected void addNotification(String notification) |
| 49 { |
| 50 jsEngine.evaluate( |
| 51 "(function()\n" + |
| 52 "{\n" + |
| 53 "require('notification').Notification.addNotification(" + notification + "
);\n" + |
| 54 "})();"); |
| 55 } |
| 56 |
| 57 private static final String TAG = "notification"; |
| 58 |
| 59 private class LocalShowNotificationCallback extends ShowNotificationCallback |
| 60 { |
| 61 private Notification retValue; |
| 62 |
| 63 public Notification getRetValue() |
| 64 { |
| 65 return retValue; |
| 66 } |
37 | 67 |
38 @Override | 68 @Override |
39 protected void setUp() throws Exception { | 69 public void showNotificationCallback(Notification notification) |
40 super.setUp(); | 70 { |
| 71 Log.d(TAG, this + " received [" + notification + "]"); |
| 72 retValue = notification; |
| 73 } |
| 74 } |
41 | 75 |
42 jsEngine.setWebRequest(new LazyWebRequest()); | 76 protected Notification peekNotification(String url) throws InterruptedExceptio
n |
43 filterEngine = new FilterEngine(jsEngine); | 77 { |
| 78 Log.d(TAG, "Start peek"); |
| 79 |
| 80 LocalShowNotificationCallback callback = new LocalShowNotificationCallback()
; |
| 81 Log.d(TAG, "set callback " + callback); |
| 82 filterEngine.setShowNotificationCallback(callback); |
| 83 filterEngine.showNextNotification(url); |
| 84 filterEngine.removeShowNotificationCallback(); |
| 85 Log.d(TAG, "removed callback"); |
| 86 return callback.getRetValue(); |
| 87 } |
| 88 |
| 89 private class MockWebRequest extends WebRequest |
| 90 { |
| 91 private String responseText; |
| 92 |
| 93 public MockWebRequest(String responseText) |
| 94 { |
| 95 this.responseText = responseText; |
44 } | 96 } |
45 | 97 |
46 protected void addNotification(String notification) | 98 @Override |
| 99 public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
47 { | 100 { |
48 jsEngine.evaluate( | 101 if (url.indexOf("/notification.json") < 0) |
49 "(function()\n" + | 102 return new ServerResponse(); |
50 "{\n" + | 103 |
51 "require('notification').Notification.addNotification(" + notificati
on + ");\n" + | 104 ServerResponse response = new ServerResponse(); |
52 "})();"); | 105 response.setStatus(ServerResponse.NsStatus.OK); |
| 106 response.setResponseStatus(200); |
| 107 response.setResponse(responseText); |
| 108 return response; |
53 } | 109 } |
| 110 } |
54 | 111 |
55 private static final String TAG = "notification"; | 112 @Test |
| 113 public void testNoNotifications() throws InterruptedException |
| 114 { |
| 115 assertNull(peekNotification("")); |
| 116 } |
56 | 117 |
57 private class LocalShowNotificationCallback extends ShowNotificationCallback | 118 @Test |
58 { | 119 public void testAddNotification() throws InterruptedException |
59 private Notification retValue; | 120 { |
| 121 addNotification( |
| 122 "{\n" + |
| 123 " type: 'critical',\n" + |
| 124 " title: 'testTitle',\n" + |
| 125 " message: 'testMessage',\n" + |
| 126 "}"); |
| 127 Notification notification = peekNotification(""); |
| 128 assertNotNull(notification); |
| 129 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 130 assertEquals("testTitle", notification.getTitle()); |
| 131 assertEquals("testMessage", notification.getMessageString()); |
| 132 } |
60 | 133 |
61 public Notification getRetValue() | 134 @Test |
62 { | 135 public void testFilterByUrl() throws InterruptedException |
63 return retValue; | 136 { |
64 } | 137 addNotification("{ id:'no-filter', type:'critical' }"); |
| 138 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$
document'] }"); |
| 139 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$docum
ent'] }"); |
65 | 140 |
66 @Override | 141 Notification notification = peekNotification(""); |
67 public void showNotificationCallback(Notification notification) | 142 assertNotNull(notification); |
68 { | 143 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
69 Log.d(TAG, this + " received [" + notification + "]"); | |
70 retValue = notification; | |
71 } | |
72 } | |
73 | 144 |
74 protected Notification peekNotification(String url) throws InterruptedExcept
ion | 145 notification = peekNotification("http://www.de"); |
75 { | 146 assertNotNull(notification); |
76 Log.d(TAG, "Start peek"); | 147 assertEquals(Notification.Type.QUESTION, notification.getType()); |
77 | 148 |
78 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); | 149 notification = peekNotification("http://www.com"); |
79 Log.d(TAG, "set callback " + callback); | 150 assertNotNull(notification); |
80 filterEngine.setShowNotificationCallback(callback); | 151 assertEquals(Notification.Type.INFORMATION, notification.getType()); |
81 filterEngine.showNextNotification(url); | 152 } |
82 filterEngine.removeShowNotificationCallback(); | |
83 Log.d(TAG, "removed callback"); | |
84 return callback.getRetValue(); | |
85 } | |
86 | 153 |
87 private class MockWebRequest extends WebRequest | 154 @Test |
88 { | 155 public void testMarkAsShown() throws InterruptedException |
89 private String responseText; | 156 { |
| 157 addNotification("{ type: 'question' }"); |
| 158 assertNotNull(peekNotification("")); |
90 | 159 |
91 public MockWebRequest(String responseText) | 160 Notification notification = peekNotification(""); |
92 { | 161 assertNotNull(notification); |
93 this.responseText = responseText; | |
94 } | |
95 | 162 |
96 @Override | 163 Thread.sleep(1000); |
97 public ServerResponse httpGET(String url, List<HeaderEntry> headers) | 164 notification.markAsShown(); |
98 { | |
99 if (url.indexOf("/notification.json") < 0) | |
100 return new ServerResponse(); | |
101 | 165 |
102 ServerResponse response = new ServerResponse(); | 166 assertNull(peekNotification("")); |
103 response.setStatus(ServerResponse.NsStatus.OK); | 167 } |
104 response.setResponseStatus(200); | |
105 response.setResponse(responseText); | |
106 return response; | |
107 } | |
108 } | |
109 | |
110 @Test | |
111 public void testNoNotifications() throws InterruptedException | |
112 { | |
113 assertNull(peekNotification("")); | |
114 } | |
115 | |
116 @Test | |
117 public void testAddNotification() throws InterruptedException | |
118 { | |
119 addNotification( | |
120 "{\n" + | |
121 " type: 'critical',\n" + | |
122 " title: 'testTitle',\n" + | |
123 " message: 'testMessage',\n" + | |
124 "}"); | |
125 Notification notification = peekNotification(""); | |
126 assertNotNull(notification); | |
127 assertEquals(Notification.Type.CRITICAL, notification.getType()); | |
128 assertEquals("testTitle", notification.getTitle()); | |
129 assertEquals("testMessage", notification.getMessageString()); | |
130 } | |
131 | |
132 @Test | |
133 public void testFilterByUrl() throws InterruptedException | |
134 { | |
135 addNotification("{ id:'no-filter', type:'critical' }"); | |
136 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.
com$document'] }"); | |
137 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$d
ocument'] }"); | |
138 | |
139 Notification notification = peekNotification(""); | |
140 assertNotNull(notification); | |
141 assertEquals(Notification.Type.CRITICAL, notification.getType()); | |
142 | |
143 notification = peekNotification("http://www.de"); | |
144 assertNotNull(notification); | |
145 assertEquals(Notification.Type.QUESTION, notification.getType()); | |
146 | |
147 notification = peekNotification("http://www.com"); | |
148 assertNotNull(notification); | |
149 assertEquals(Notification.Type.INFORMATION, notification.getType()); | |
150 } | |
151 | |
152 @Test | |
153 public void testMarkAsShown() throws InterruptedException | |
154 { | |
155 addNotification("{ type: 'question' }"); | |
156 assertNotNull(peekNotification("")); | |
157 | |
158 Notification notification = peekNotification(""); | |
159 assertNotNull(notification); | |
160 | |
161 Thread.sleep(1000); | |
162 notification.markAsShown(); | |
163 | |
164 assertNull(peekNotification("")); | |
165 } | |
166 } | 168 } |
OLD | NEW |