LEFT | RIGHT |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.libadblockplus.tests; | 18 package org.adblockplus.libadblockplus.tests; |
19 | 19 |
| 20 import android.util.Log; |
20 import org.adblockplus.libadblockplus.FilterEngine; | 21 import org.adblockplus.libadblockplus.FilterEngine; |
21 import org.adblockplus.libadblockplus.HeaderEntry; | 22 import org.adblockplus.libadblockplus.HeaderEntry; |
22 import org.adblockplus.libadblockplus.LazyLogSystem; | 23 import org.adblockplus.libadblockplus.LazyLogSystem; |
23 import org.adblockplus.libadblockplus.LazyWebRequest; | 24 import org.adblockplus.libadblockplus.LazyWebRequest; |
24 import org.adblockplus.libadblockplus.Notification; | 25 import org.adblockplus.libadblockplus.Notification; |
25 import org.adblockplus.libadblockplus.ServerResponse; | 26 import org.adblockplus.libadblockplus.ServerResponse; |
26 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 27 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
27 import org.adblockplus.libadblockplus.WebRequest; | 28 import org.adblockplus.libadblockplus.WebRequest; |
28 | 29 |
29 import org.junit.Test; | 30 import org.junit.Test; |
30 | 31 |
31 import java.util.List; | 32 import java.util.List; |
32 | 33 |
33 public class NotificationTest extends BaseJsTest { | 34 public class NotificationTest extends BaseJsTest |
| 35 { |
34 | 36 |
35 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 } |
36 | 67 |
37 @Override | 68 @Override |
38 protected void setUp() throws Exception { | 69 public void showNotificationCallback(Notification notification) |
39 super.setUp(); | 70 { |
| 71 Log.d(TAG, this + " received [" + notification + "]"); |
| 72 retValue = notification; |
| 73 } |
| 74 } |
40 | 75 |
41 jsEngine.setLogSystem(new LazyLogSystem()); | 76 protected Notification peekNotification(String url) throws InterruptedExceptio
n |
42 jsEngine.setWebRequest(new LazyWebRequest()); | 77 { |
43 filterEngine = new FilterEngine(jsEngine); | 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 |
47 jsEngine.evaluate( | 99 public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
48 "(function()\n" + | 100 { |
49 "{\n" + | 101 if (url.indexOf("/notification.json") < 0) |
50 "require('notification').Notification.addNotification(" + notificati
on + ");\n" + | 102 { |
51 "})();"); | 103 return new ServerResponse(); |
| 104 } |
| 105 |
| 106 ServerResponse response = new ServerResponse(); |
| 107 response.setStatus(ServerResponse.NsStatus.OK); |
| 108 response.setResponseStatus(200); |
| 109 response.setResponse(responseText); |
| 110 return response; |
52 } | 111 } |
| 112 } |
53 | 113 |
54 private class LocalShowNotificationCallback extends ShowNotificationCallback
{ | 114 @Test |
| 115 public void testNoNotifications() throws InterruptedException |
| 116 { |
| 117 assertNull(peekNotification("")); |
| 118 } |
55 | 119 |
56 private Notification retValue; | 120 @Test |
| 121 public void testAddNotification() throws InterruptedException |
| 122 { |
| 123 addNotification( |
| 124 "{\n" + |
| 125 " type: 'critical',\n" + |
| 126 " title: 'testTitle',\n" + |
| 127 " message: 'testMessage',\n" + |
| 128 "}"); |
| 129 Notification notification = peekNotification(""); |
| 130 assertNotNull(notification); |
| 131 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
| 132 assertEquals("testTitle", notification.getTitle()); |
| 133 assertEquals("testMessage", notification.getMessageString()); |
| 134 } |
57 | 135 |
58 public Notification getRetValue() { | 136 @Test |
59 return retValue; | 137 public void testFilterByUrl() throws InterruptedException |
60 } | 138 { |
| 139 addNotification("{ id:'no-filter', type:'critical' }"); |
| 140 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.com$
document'] }"); |
| 141 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$docum
ent'] }"); |
61 | 142 |
62 @Override | 143 Notification notification = peekNotification(""); |
63 public void showNotificationCallback(Notification jsValue) { | 144 assertNotNull(notification); |
64 retValue = jsValue; | 145 assertEquals(Notification.Type.CRITICAL, notification.getType()); |
65 } | |
66 } | |
67 | 146 |
68 protected Notification peekNotification(String url) { | 147 notification = peekNotification("http://www.de"); |
69 LocalShowNotificationCallback callback = new LocalShowNotificationCallba
ck(); | 148 assertNotNull(notification); |
70 filterEngine.setShowNotificationCallback(callback); | 149 assertEquals(Notification.Type.QUESTION, notification.getType()); |
71 filterEngine.showNextNotification(url); | |
72 filterEngine.removeShowNotificationCallback(); | |
73 return callback.getRetValue(); | |
74 } | |
75 | 150 |
76 private class MockWebRequest extends WebRequest { | 151 notification = peekNotification("http://www.com"); |
| 152 assertNotNull(notification); |
| 153 assertEquals(Notification.Type.INFORMATION, notification.getType()); |
| 154 } |
77 | 155 |
78 public MockWebRequest(String notification) { | 156 @Test |
79 this.responseText = notification; | 157 public void testMarkAsShown() throws InterruptedException |
80 } | 158 { |
| 159 addNotification("{ type: 'question' }"); |
| 160 assertNotNull(peekNotification("")); |
81 | 161 |
82 private String responseText; | 162 Notification notification = peekNotification(""); |
| 163 assertNotNull(notification); |
83 | 164 |
84 @Override | 165 Thread.sleep(1000); |
85 public ServerResponse httpGET(String url, List<HeaderEntry> headers) { | 166 notification.markAsShown(); |
86 if (url.indexOf("/notification.json") < 0) | |
87 return new ServerResponse(); | |
88 | 167 |
89 ServerResponse response = new ServerResponse(); | 168 assertNull(peekNotification("")); |
90 response.setStatus(ServerResponse.NsStatus.OK); | 169 } |
91 response.setResponseStatus(200); | |
92 response.setResponse(responseText); | |
93 return response; | |
94 } | |
95 } | |
96 | |
97 @Test | |
98 public void testNoNotifications() { | |
99 assertNull(peekNotification("")); | |
100 } | |
101 | |
102 @Test | |
103 public void testAddNotification() { | |
104 addNotification( | |
105 "{\n" + | |
106 " type: 'critical',\n" + | |
107 " title: 'testTitle',\n" + | |
108 " message: 'testMessage',\n" + | |
109 "}"); | |
110 Notification notification = peekNotification(""); | |
111 assertNotNull(notification); | |
112 assertEquals(Notification.Type.CRITICAL, notification.getType()); | |
113 assertEquals("testTitle", notification.getTitle()); | |
114 assertEquals("testMessage", notification.getMessageString()); | |
115 } | |
116 | |
117 @Test | |
118 public void testFilterByUrl() { | |
119 addNotification("{ id:'no-filter', type:'critical' }"); | |
120 addNotification("{ id:'www.com', type:'information', urlFilters:['||www.
com$document'] }"); | |
121 addNotification("{ id:'www.de', type:'question', urlFilters:['||www.de$d
ocument'] }"); | |
122 | |
123 Notification notification = peekNotification(""); | |
124 assertNotNull(notification); | |
125 assertEquals(Notification.Type.CRITICAL, notification.getType()); | |
126 | |
127 notification = peekNotification("http://www.de"); | |
128 assertNotNull(notification); | |
129 assertEquals(Notification.Type.QUESTION, notification.getType()); | |
130 | |
131 notification = peekNotification("http://www.com"); | |
132 assertNotNull(notification); | |
133 assertEquals(Notification.Type.INFORMATION, notification.getType()); | |
134 } | |
135 | |
136 @Test | |
137 public void testMarkAsShown() throws InterruptedException { | |
138 addNotification("{ id: 'id', type: 'question' }"); | |
139 assertNotNull(peekNotification("")); | |
140 | |
141 Notification notification = peekNotification(""); | |
142 assertNotNull(notification); | |
143 notification.markAsShown(); | |
144 assertNull(peekNotification("")); | |
145 } | |
146 } | 170 } |
LEFT | RIGHT |