Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/Notification.cpp

Issue 5797488346791936: Issue 1107 - Support notifications (Closed)
Left Patch Set: Created Jan. 19, 2015, 12:50 p.m.
Right Patch Set: fix comment Created Jan. 23, 2015, 3:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/Notification.cpp ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 #include "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 19
20 using namespace AdblockPlus; 20 using namespace AdblockPlus;
21 21
22 /* This define enables NotificationMockWebRequestTest but to run it 22 // This define enables NotificationMockWebRequestTest but to run it
23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js. 23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js.
24 #include <chrono> 24 //#define NotificationMockWebRequestTest_ENABLED
25 #include <thread>
26 #define NotificationMockWebRequestTest_ENABLED
27 //*/
28 25
29 namespace 26 namespace
30 { 27 {
31 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; 28 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr;
32 29
33 class NotificationTest : public BaseJsTest 30 class NotificationTest : public BaseJsTest
34 { 31 {
35 protected: 32 protected:
36 FilterEnginePtr filterEngine; 33 FilterEnginePtr filterEngine;
37 void SetUp() override 34 void SetUp() override
38 { 35 {
39 BaseJsTest::SetUp(); 36 BaseJsTest::SetUp();
40 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); 37 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem()));
41 jsEngine->SetWebRequest(std::tr1::make_shared<LazyWebRequest>()); 38 jsEngine->SetWebRequest(WebRequestPtr(new LazyWebRequest()));
42 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 39 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
43 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); 40 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine);
41 }
42
43 void AddNotification(const std::string& notification)
44 {
45 jsEngine->Evaluate("(function()"
46 "{"
47 "require('notification').Notification.addNotification(" + notification + ");"
48 "})();");
44 } 49 }
45 }; 50 };
46 51
47 class MockWebRequest : public WebRequest 52 class MockWebRequest : public WebRequest
48 { 53 {
49 public: 54 public:
50 std::string m_responseText; 55 std::string responseText;
51 explicit MockWebRequest(const std::string& notification) 56 explicit MockWebRequest(const std::string& notification)
52 : m_responseText(notification) 57 : responseText(notification)
53 { 58 {
54 } 59 }
55 ServerResponse GET(const std::string& url, 60 ServerResponse GET(const std::string& url,
56 const HeaderList& requestHeaders) const override 61 const HeaderList& requestHeaders) const
57 { 62 {
58 if (url.find("/notification.json") == std::string::npos) 63 if (url.find("/notification.json") == std::string::npos)
59 { 64 {
60 return ServerResponse(); 65 return ServerResponse();
61 } 66 }
62 ServerResponse serverResponse; 67 ServerResponse serverResponse;
63 serverResponse.status = NS_OK; 68 serverResponse.status = NS_OK;
64 serverResponse.responseStatus = 200; 69 serverResponse.responseStatus = 200;
65 serverResponse.responseText = m_responseText; 70 serverResponse.responseText = responseText;
66 return serverResponse; 71 return serverResponse;
67 } 72 }
68 }; 73 };
69 74
70 #ifdef NotificationMockWebRequestTest_ENABLED 75 #ifdef NotificationMockWebRequestTest_ENABLED
71 class NotificationMockWebRequestTest : public BaseJsTest 76 class NotificationMockWebRequestTest : public BaseJsTest
72 { 77 {
73 protected: 78 protected:
74 FilterEnginePtr filterEngine; 79 FilterEnginePtr filterEngine;
75 void SetUp() override 80 void SetUp() override
76 { 81 {
77 BaseJsTest::SetUp(); 82 BaseJsTest::SetUp();
78 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); 83 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>());
79 const char* responseJsonText = "{" 84 const char* responseJsonText = "{"
80 "\"notifications\": [{" 85 "\"notifications\": [{"
81 "\"id\": \"some id\"," 86 "\"id\": \"some id\","
82 "\"type\": \"information\"," 87 "\"type\": \"information\","
83 "\"message\": {" 88 "\"message\": {"
84 "\"en-US\": \"Critical message\"," 89 "\"en-US\": \"message\""
85 "\"de\": \"Achtung\""
86 "}," 90 "},"
87 "\"title\": \"Title\"" 91 "\"title\": \"Title\""
88 "}]" 92 "}]"
89 "}"; 93 "}";
90 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson Text)); 94 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson Text));
91 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 95 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
92 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); 96 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine);
93 } 97 }
94 }; 98 };
95 #endif 99 #endif
96 } 100 }
97 101
98 TEST_F(NotificationTest, NotificationCtr)
99 {
100 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC ATION_TYPE_CRITICAL, "testId");
101 ASSERT_NE(nullptr, notification);
102 EXPECT_EQ("testId", notification->GetId());
103 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
104 }
105
106 TEST_F(NotificationTest, MultilingualProperties)
107 {
108 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC ATION_TYPE_CRITICAL, "testId");
109 ASSERT_NE(nullptr, notification);
110 notification->SetTitle("French", "fr-FR");
111 {
112 auto texts = filterEngine->GetNotificationTexts(notification);
113 EXPECT_EQ("", texts.title);
114 EXPECT_EQ("", texts.message);
115 }
116 {
117 auto texts = filterEngine->GetNotificationTexts(notification, "de");
118 EXPECT_EQ("", texts.title);
119 EXPECT_EQ("", texts.message);
120 }
121 {
122 auto texts = filterEngine->GetNotificationTexts(notification, "fr");
123 EXPECT_EQ("", texts.title);
124 EXPECT_EQ("", texts.message);
125 }
126 {
127 auto texts = filterEngine->GetNotificationTexts(notification, "fr-FR");
128 EXPECT_EQ("French", texts.title);
129 EXPECT_EQ("", texts.message);
130 }
131 notification->SetTitle("German", "de");
132 {
133 auto texts = filterEngine->GetNotificationTexts(notification);
134 EXPECT_EQ("", texts.title);
135 EXPECT_EQ("", texts.message);
136 }
137 {
138 auto texts = filterEngine->GetNotificationTexts(notification, "de");
139 EXPECT_EQ("German", texts.title);
140 EXPECT_EQ("", texts.message);
141 }
142 {
143 auto texts = filterEngine->GetNotificationTexts(notification, "de-DE");
144 EXPECT_EQ("German", texts.title);
145 EXPECT_EQ("", texts.message);
146 }
147 {
148 auto texts = filterEngine->GetNotificationTexts(notification, "fr-FR");
149 EXPECT_EQ("French", texts.title);
150 EXPECT_EQ("", texts.message);
151 }
152 notification->SetTitle("English", "en");
153 {
154 auto texts = filterEngine->GetNotificationTexts(notification);
155 EXPECT_EQ("", texts.title);
156 EXPECT_EQ("", texts.message);
157 }
158 {
159 auto texts = filterEngine->GetNotificationTexts(notification, "en");
160 EXPECT_EQ("English", texts.title);
161 EXPECT_EQ("", texts.message);
162 }
163 {
164 auto texts = filterEngine->GetNotificationTexts(notification, "en-US");
165 EXPECT_EQ("English", texts.title);
166 EXPECT_EQ("", texts.message);
167 }
168 {
169 auto texts = filterEngine->GetNotificationTexts(notification, "de");
170 EXPECT_EQ("German", texts.title);
171 EXPECT_EQ("", texts.message);
172 }
173 {
174 auto texts = filterEngine->GetNotificationTexts(notification, "fr-FR");
175 EXPECT_EQ("French", texts.title);
176 EXPECT_EQ("", texts.message);
177 }
178 notification->SetTitle("Default", "en-US");
179 {
180 auto texts = filterEngine->GetNotificationTexts(notification);
181 EXPECT_EQ("Default", texts.title);
182 EXPECT_EQ("", texts.message);
183 }
184 {
185 auto texts = filterEngine->GetNotificationTexts(notification, "en");
186 EXPECT_EQ("English", texts.title);
187 EXPECT_EQ("", texts.message);
188 }
189 {
190 auto texts = filterEngine->GetNotificationTexts(notification, "en-US");
191 EXPECT_EQ("Default", texts.title);
192 EXPECT_EQ("", texts.message);
193 }
194 {
195 auto texts = filterEngine->GetNotificationTexts(notification, "xx");
196 EXPECT_EQ("Default", texts.title);
197 EXPECT_EQ("", texts.message);
198 }
199
200 notification->SetTitle("testTitleNoLocale");
201 {
202 auto texts = filterEngine->GetNotificationTexts(notification);
203 EXPECT_EQ("testTitleNoLocale", texts.title);
204 EXPECT_EQ("", texts.message);
205 }
206 {
207 auto texts = filterEngine->GetNotificationTexts(notification, "de-DE");
208 EXPECT_EQ("testTitleNoLocale", texts.title);
209 EXPECT_EQ("", texts.message);
210 }
211
212 notification->SetTitle("French", "fr-FR");
213 {
214 auto texts = filterEngine->GetNotificationTexts(notification);
215 EXPECT_EQ("", texts.title);
216 EXPECT_EQ("", texts.message);
217 }
218 {
219 auto texts = filterEngine->GetNotificationTexts(notification, "de");
220 EXPECT_EQ("", texts.title);
221 EXPECT_EQ("", texts.message);
222 }
223 {
224 auto texts = filterEngine->GetNotificationTexts(notification, "fr");
225 EXPECT_EQ("", texts.title);
226 EXPECT_EQ("", texts.message);
227 }
228 {
229 auto texts = filterEngine->GetNotificationTexts(notification, "fr-FR");
230 EXPECT_EQ("French", texts.title);
231 EXPECT_EQ("", texts.message);
232 }
233 }
234
235 TEST_F(NotificationTest, UrlFilters)
236 {
237 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC ATION_TYPE_CRITICAL, "testId");
238 ASSERT_NE(nullptr, notification);
239 EXPECT_EQ(0, notification->GetUrlFilters().size());
240 notification->AddUrlFilter("filterA");
241 {
242 auto urlFilters = notification->GetUrlFilters();
243 ASSERT_EQ(1, urlFilters.size());
244 EXPECT_EQ("filterA", urlFilters[0]);
245 }
246 notification->AddUrlFilter("filterB");
247 {
248 auto urlFilters = notification->GetUrlFilters();
249 ASSERT_EQ(2, urlFilters.size());
250 EXPECT_EQ("filterA", urlFilters[0]);
251 EXPECT_EQ("filterB", urlFilters[1]);
252 }
253 notification->AddUrlFilter("filterC");
254 {
255 auto urlFilters = notification->GetUrlFilters();
256 ASSERT_EQ(3, urlFilters.size());
257 EXPECT_EQ("filterA", urlFilters[0]);
258 EXPECT_EQ("filterB", urlFilters[1]);
259 EXPECT_EQ("filterC", urlFilters[2]);
260 }
261 }
262
263 TEST_F(NotificationTest, NoNotifications) 102 TEST_F(NotificationTest, NoNotifications)
264 { 103 {
265 auto notification = filterEngine->GetNextNotificationToShow(); 104 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
266 EXPECT_EQ(nullptr, notification); 105 EXPECT_EQ(NULL, notification.get());
267 } 106 }
268 107
269 #ifdef NotificationMockWebRequestTest_ENABLED 108 #ifdef NotificationMockWebRequestTest_ENABLED
270 TEST_F(NotificationMockWebRequestTest, SingleNotification) 109 TEST_F(NotificationMockWebRequestTest, SingleNotification)
271 { 110 {
272 std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack 111 AdblockPlus::Sleep(5000/*msec*/); // it's a hack
273 auto notification = filterEngine->GetNextNotificationToShow(); 112 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
274 // try another one immediately to avoid queuing of the next notification by 113 // try another one immediately to avoid queuing of the next notification by
275 // the timer. 114 // the timer.
276 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 115 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
277 ASSERT_NE(nullptr, notification.get()); 116 ASSERT_TRUE(notification);
278 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 117 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
279 EXPECT_EQ("some id", notification->GetId()); 118 EXPECT_EQ("Title", notification->GetTitle());
280 auto defaultTexts = filterEngine->GetNotificationTexts(notification, "xx"); 119 EXPECT_EQ("message", notification->GetMessageString());
281 EXPECT_EQ("Title", defaultTexts.title);
282 EXPECT_EQ("Critical message", defaultTexts.message);
283 auto deTexts = filterEngine->GetNotificationTexts(notification, "de");
284 EXPECT_EQ("Title", deTexts.title);
285 EXPECT_EQ("Achtung", deTexts.message);
286 } 120 }
287 #endif 121 #endif
288 122
289 TEST_F(NotificationTest, AddNotification) 123 TEST_F(NotificationTest, AddNotification)
290 { 124 {
291 { 125 AddNotification("{"
292 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "id"); 126 "type: 'critical',"
293 ASSERT_NE(nullptr, notification); 127 "title: 'testTitle',"
294 filterEngine->AddNotification(notification); 128 "message: 'testMessage',"
295 } 129 "}");
296 auto notification = filterEngine->GetNextNotificationToShow(); 130 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
297 ASSERT_NE(nullptr, notification.get()); 131 ASSERT_TRUE(notification);
298 EXPECT_EQ("id", notification->GetId()); 132 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
133 EXPECT_EQ("testTitle", notification->GetTitle());
134 EXPECT_EQ("testMessage", notification->GetMessageString());
299 } 135 }
300 136
301 TEST_F(NotificationTest, FilterByUrl1) 137 TEST_F(NotificationTest, FilterByUrl)
302 { 138 {
303 { 139 AddNotification("{ id: 'no-filter', type: 'critical' }");
304 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "no-filter"); 140 AddNotification("{ id: 'www.com', type: 'information',"
305 ASSERT_NE(nullptr, notification); 141 "urlFilters:['http://www.com']"
306 filterEngine->AddNotification(notification); 142 "}");
307 } 143 AddNotification("{ id: 'www.de', type: 'question',"
308 { 144 "urlFilters:['http://www.de']"
309 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "com"); 145 "}");
310 ASSERT_NE(nullptr, notification); 146
311 notification->AddUrlFilter("http://www.com"); 147 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
312 filterEngine->AddNotification(notification); 148 ASSERT_TRUE(notification);
313 } 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
314 { 150
315 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "de");
316 ASSERT_NE(nullptr, notification);
317 notification->AddUrlFilter("http://www.de");
318 filterEngine->AddNotification(notification);
319 }
320 auto notification = filterEngine->GetNextNotificationToShow();
321 ASSERT_NE(nullptr, notification.get());
322 EXPECT_EQ("no-filter", notification->GetId());
323 notification = filterEngine->GetNextNotificationToShow();
324 EXPECT_EQ(nullptr, notification.get());
325 notification = filterEngine->GetNextNotificationToShow("http://www.de"); 151 notification = filterEngine->GetNextNotificationToShow("http://www.de");
326 ASSERT_NE(nullptr, notification.get()); 152 ASSERT_TRUE(notification);
327 EXPECT_EQ("de", notification->GetId()); 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( ));
328 }
329 TEST_F(NotificationTest, FilterByUrl2)
330 {
331 {
332 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "no-filter");
333 ASSERT_NE(nullptr, notification);
334 filterEngine->AddNotification(notification);
335 }
336 {
337 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "com");
338 ASSERT_NE(nullptr, notification);
339 notification->AddUrlFilter("http://www.com");
340 filterEngine->AddNotification(notification);
341 }
342 {
343 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "de");
344 ASSERT_NE(nullptr, notification);
345 notification->AddUrlFilter("http://www.de");
346 filterEngine->AddNotification(notification);
347 }
348 auto notification = filterEngine->GetNextNotificationToShow("http://www.de");
349 ASSERT_NE(nullptr, notification.get());
350 EXPECT_EQ("de", notification->GetId());
351 notification = filterEngine->GetNextNotificationToShow("http://www.de");
352 EXPECT_EQ(nullptr, notification.get());
353 notification = filterEngine->GetNextNotificationToShow();
354 ASSERT_NE(nullptr, notification.get());
355 EXPECT_EQ("no-filter", notification->GetId());
356 }
357 154
358 TEST_F(NotificationTest, AddRemoveNotification) 155 notification = filterEngine->GetNextNotificationToShow("http://www.com");
359 { 156 ASSERT_TRUE(notification);
360 auto newInfoNotification = filterEngine->CreateNotification(NotificationType:: NOTIFICATION_TYPE_INFORMATION, "id"); 157 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
361 ASSERT_NE(nullptr, newInfoNotification);
362 filterEngine->AddNotification(newInfoNotification);
363 filterEngine->RemoveNotification(newInfoNotification);
364 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get());
365 } 158 }
366 159
367 TEST_F(NotificationTest, MarkAsShown) 160 TEST_F(NotificationTest, MarkAsShown)
368 { 161 {
369 auto newInfoNotification = filterEngine->CreateNotification(NotificationType:: NOTIFICATION_TYPE_INFORMATION, "id"); 162 AddNotification("{ id: 'id', type: 'question' }");
370 ASSERT_NE(nullptr, newInfoNotification); 163 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
371 filterEngine->AddNotification(newInfoNotification); 164 EXPECT_TRUE(notification);
372 filterEngine->MarkNotificationAsShown("id"); 165 notification = filterEngine->GetNextNotificationToShow();
373 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 166 ASSERT_TRUE(notification);
167 notification->MarkAsShown();
168 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
374 } 169 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld