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: move MarkAsShown into Notification class and get rid of locale arg in Notification::GetTexts Created Jan. 22, 2015, 10:02 a.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");
Wladimir Palant 2015/01/22 10:47:20 Instead of defining this API for tests only, pleas
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, UrlFilters)
107 {
108 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC ATION_TYPE_CRITICAL, "testId");
109 ASSERT_NE(nullptr, notification);
110 EXPECT_EQ(0, notification->GetUrlFilters().size());
111 notification->AddUrlFilter("filterA");
112 {
113 auto urlFilters = notification->GetUrlFilters();
114 ASSERT_EQ(1, urlFilters.size());
115 EXPECT_EQ("filterA", urlFilters[0]);
116 }
117 notification->AddUrlFilter("filterB");
118 {
119 auto urlFilters = notification->GetUrlFilters();
120 ASSERT_EQ(2, urlFilters.size());
121 EXPECT_EQ("filterA", urlFilters[0]);
122 EXPECT_EQ("filterB", urlFilters[1]);
123 }
124 notification->AddUrlFilter("filterC");
125 {
126 auto urlFilters = notification->GetUrlFilters();
127 ASSERT_EQ(3, urlFilters.size());
128 EXPECT_EQ("filterA", urlFilters[0]);
129 EXPECT_EQ("filterB", urlFilters[1]);
130 EXPECT_EQ("filterC", urlFilters[2]);
131 }
132 }
133
134 TEST_F(NotificationTest, NoNotifications) 102 TEST_F(NotificationTest, NoNotifications)
135 { 103 {
136 auto notification = filterEngine->GetNextNotificationToShow(); 104 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
137 EXPECT_EQ(nullptr, notification); 105 EXPECT_EQ(NULL, notification.get());
138 } 106 }
139 107
140 #ifdef NotificationMockWebRequestTest_ENABLED 108 #ifdef NotificationMockWebRequestTest_ENABLED
141 TEST_F(NotificationMockWebRequestTest, SingleNotification) 109 TEST_F(NotificationMockWebRequestTest, SingleNotification)
142 { 110 {
143 std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack 111 AdblockPlus::Sleep(5000/*msec*/); // it's a hack
144 auto notification = filterEngine->GetNextNotificationToShow(); 112 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
145 // 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
146 // the timer. 114 // the timer.
147 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 115 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
148 ASSERT_NE(nullptr, notification.get()); 116 ASSERT_TRUE(notification);
149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 117 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
118 EXPECT_EQ("Title", notification->GetTitle());
119 EXPECT_EQ("message", notification->GetMessageString());
150 } 120 }
151 #endif 121 #endif
152 122
153 TEST_F(NotificationTest, AddNotification) 123 TEST_F(NotificationTest, AddNotification)
154 { 124 {
155 { 125 AddNotification("{"
156 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "id"); 126 "type: 'critical',"
157 ASSERT_NE(nullptr, notification); 127 "title: 'testTitle',"
158 filterEngine->AddNotification(notification); 128 "message: 'testMessage',"
159 } 129 "}");
160 auto notification = filterEngine->GetNextNotificationToShow(); 130 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
161 ASSERT_NE(nullptr, notification.get()); 131 ASSERT_TRUE(notification);
162 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());
163 } 135 }
164 136
165 TEST_F(NotificationTest, FilterByUrl1) 137 TEST_F(NotificationTest, FilterByUrl)
166 { 138 {
167 { 139 AddNotification("{ id: 'no-filter', type: 'critical' }");
168 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "no-filter"); 140 AddNotification("{ id: 'www.com', type: 'information',"
169 ASSERT_NE(nullptr, notification); 141 "urlFilters:['http://www.com']"
170 filterEngine->AddNotification(notification); 142 "}");
171 } 143 AddNotification("{ id: 'www.de', type: 'question',"
172 { 144 "urlFilters:['http://www.de']"
173 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "com"); 145 "}");
174 ASSERT_NE(nullptr, notification); 146
175 notification->AddUrlFilter("http://www.com"); 147 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
176 filterEngine->AddNotification(notification); 148 ASSERT_TRUE(notification);
177 } 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
178 { 150
179 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "de");
180 ASSERT_NE(nullptr, notification);
181 notification->AddUrlFilter("http://www.de");
182 filterEngine->AddNotification(notification);
183 }
184 auto notification = filterEngine->GetNextNotificationToShow();
185 ASSERT_NE(nullptr, notification.get());
186 EXPECT_EQ("no-filter", notification->GetId());
187 notification = filterEngine->GetNextNotificationToShow();
188 EXPECT_EQ(nullptr, notification.get());
189 notification = filterEngine->GetNextNotificationToShow("http://www.de"); 151 notification = filterEngine->GetNextNotificationToShow("http://www.de");
190 ASSERT_NE(nullptr, notification.get()); 152 ASSERT_TRUE(notification);
191 EXPECT_EQ("de", notification->GetId()); 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( ));
192 }
193 TEST_F(NotificationTest, FilterByUrl2)
194 {
195 {
196 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "no-filter");
197 ASSERT_NE(nullptr, notification);
198 filterEngine->AddNotification(notification);
199 }
200 {
201 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "com");
202 ASSERT_NE(nullptr, notification);
203 notification->AddUrlFilter("http://www.com");
204 filterEngine->AddNotification(notification);
205 }
206 {
207 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF ICATION_TYPE_INFORMATION, "de");
208 ASSERT_NE(nullptr, notification);
209 notification->AddUrlFilter("http://www.de");
210 filterEngine->AddNotification(notification);
211 }
212 auto notification = filterEngine->GetNextNotificationToShow("http://www.de");
213 ASSERT_NE(nullptr, notification.get());
214 EXPECT_EQ("de", notification->GetId());
215 notification = filterEngine->GetNextNotificationToShow("http://www.de");
216 EXPECT_EQ(nullptr, notification.get());
217 notification = filterEngine->GetNextNotificationToShow();
218 ASSERT_NE(nullptr, notification.get());
219 EXPECT_EQ("no-filter", notification->GetId());
220 }
221 154
222 TEST_F(NotificationTest, AddRemoveNotification) 155 notification = filterEngine->GetNextNotificationToShow("http://www.com");
223 { 156 ASSERT_TRUE(notification);
224 auto newInfoNotification = filterEngine->CreateNotification(NotificationType:: NOTIFICATION_TYPE_INFORMATION, "id"); 157 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
225 ASSERT_NE(nullptr, newInfoNotification);
226 filterEngine->AddNotification(newInfoNotification);
227 filterEngine->RemoveNotification(newInfoNotification);
228 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get());
229 } 158 }
230 159
231 TEST_F(NotificationTest, MarkAsShown) 160 TEST_F(NotificationTest, MarkAsShown)
232 { 161 {
233 auto newInfoNotification = filterEngine->CreateNotification(NotificationType:: NOTIFICATION_TYPE_INFORMATION, "id"); 162 AddNotification("{ id: 'id', type: 'question' }");
234 ASSERT_NE(nullptr, newInfoNotification); 163 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
235 filterEngine->AddNotification(newInfoNotification); 164 EXPECT_TRUE(notification);
236 newInfoNotification->MarkAsShown(); 165 notification = filterEngine->GetNextNotificationToShow();
237 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 166 ASSERT_TRUE(notification);
167 notification->MarkAsShown();
168 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
238 } 169 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld