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

Side by Side Diff: test/Notification.cpp

Issue 29317074: Issue 2693 - Update adblockplus dependency (Closed)
Patch Set: fix according to comments and update revision Created June 26, 2015, 2:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« include/AdblockPlus/FilterEngine.h ('K') | « src/FilterEngine.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 29 matching lines...) Expand all
40 filterEngine.reset(new FilterEngine(jsEngine)); 40 filterEngine.reset(new FilterEngine(jsEngine));
41 } 41 }
42 42
43 void AddNotification(const std::string& notification) 43 void AddNotification(const std::string& notification)
44 { 44 {
45 jsEngine->Evaluate("(function()" 45 jsEngine->Evaluate("(function()"
46 "{" 46 "{"
47 "require('notification').Notification.addNotification(" + notification + ");" 47 "require('notification').Notification.addNotification(" + notification + ");"
48 "})();"); 48 "})();");
49 } 49 }
50
51 NotificationPtr PeekNotification(const std::string& url = std::string())
52 {
53 NotificationPtr retValue;
54 filterEngine->SetShowNotificationCallback(std::tr1::bind(
55 &NotificationTest::NotificationAvailableCallback,
56 std::tr1::placeholders::_1, std::tr1::ref(retValue)));
57 filterEngine->ShowNextNotification(url);
58 filterEngine->RemoveShowNotificationCallback();
59 return retValue;
60 }
61
62 static void NotificationAvailableCallback(const NotificationPtr& src, Notifi cationPtr& dst)
63 {
64 EXPECT_TRUE(src);
65 dst = src;
66 }
50 }; 67 };
51 68
52 class MockWebRequest : public WebRequest 69 class MockWebRequest : public WebRequest
53 { 70 {
54 public: 71 public:
55 std::string responseText; 72 std::string responseText;
56 explicit MockWebRequest(const std::string& notification) 73 explicit MockWebRequest(const std::string& notification)
57 : responseText(notification) 74 : responseText(notification)
58 { 75 {
59 } 76 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 new MockWebRequest(responseJsonText))); 113 new MockWebRequest(responseJsonText)));
97 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 114 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
98 filterEngine.reset(new FilterEngine(jsEngine)); 115 filterEngine.reset(new FilterEngine(jsEngine));
99 } 116 }
100 }; 117 };
101 #endif 118 #endif
102 } 119 }
103 120
104 TEST_F(NotificationTest, NoNotifications) 121 TEST_F(NotificationTest, NoNotifications)
105 { 122 {
106 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 123 EXPECT_FALSE(PeekNotification());
107 EXPECT_EQ(NULL, notification.get());
108 } 124 }
109 125
110 #ifdef NotificationMockWebRequestTest_ENABLED 126 #ifdef NotificationMockWebRequestTest_ENABLED
111 TEST_F(NotificationMockWebRequestTest, SingleNotification) 127 TEST_F(NotificationMockWebRequestTest, SingleNotification)
112 { 128 {
129 bool isNotificationCallbackCalled = false;
130 filterEngine->SetNotificationAvailableCallback([&isNotificationCallbackCalled] (const NotificationPtr& notification)
Felix Dahlke 2015/06/30 19:45:32 Still C++03 here :)
sergei 2015/07/01 11:46:09 Done.
131 {
132 isNotificationCallbackCalled = true;
133 ASSERT_TRUE(notification);
134 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->Get Type());
135 EXPECT_EQ("Title", notification->GetTitle());
136 EXPECT_EQ("message", notification->GetMessageString());
137 notification->MarkAsShown();
138 });
113 AdblockPlus::Sleep(5000/*msec*/); // it's a hack 139 AdblockPlus::Sleep(5000/*msec*/); // it's a hack
114 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 140 EXPECT_TRUE(isNotificationCallbackCalled);
115 // try another one immediately to avoid queuing of the next notification by
116 // the timer.
117 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
118 ASSERT_TRUE(notification);
119 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
120 EXPECT_EQ("Title", notification->GetTitle());
121 EXPECT_EQ("message", notification->GetMessageString());
122 } 141 }
123 #endif 142 #endif
124 143
125 TEST_F(NotificationTest, AddNotification) 144 TEST_F(NotificationTest, AddNotification)
126 { 145 {
127 AddNotification("{" 146 AddNotification("{"
128 "type: 'critical'," 147 "type: 'critical',"
129 "title: 'testTitle'," 148 "title: 'testTitle',"
130 "message: 'testMessage'," 149 "message: 'testMessage',"
131 "}"); 150 "}");
132 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 151 NotificationPtr notification = PeekNotification();
133 ASSERT_TRUE(notification); 152 ASSERT_TRUE(notification);
134 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
135 EXPECT_EQ("testTitle", notification->GetTitle()); 154 EXPECT_EQ("testTitle", notification->GetTitle());
136 EXPECT_EQ("testMessage", notification->GetMessageString()); 155 EXPECT_EQ("testMessage", notification->GetMessageString());
137 } 156 }
138 157
139 TEST_F(NotificationTest, FilterByUrl) 158 TEST_F(NotificationTest, FilterByUrl)
140 { 159 {
141 AddNotification("{ id: 'no-filter', type: 'critical' }"); 160 AddNotification("{ id: 'no-filter', type: 'critical' }");
142 AddNotification("{ id: 'www.com', type: 'information'," 161 AddNotification("{ id: 'www.com', type: 'information',"
143 "urlFilters:['http://www.com']" 162 "urlFilters:['||www.com$document']"
144 "}"); 163 "}");
145 AddNotification("{ id: 'www.de', type: 'question'," 164 AddNotification("{ id: 'www.de', type: 'question',"
146 "urlFilters:['http://www.de']" 165 "urlFilters:['||www.de$document']"
147 "}"); 166 "}");
148 167
149 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 168 NotificationPtr notification = PeekNotification();
150 ASSERT_TRUE(notification); 169 ASSERT_TRUE(notification);
151 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 170 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
152 171
153 notification = filterEngine->GetNextNotificationToShow("http://www.de"); 172 notification = PeekNotification("http://www.de");
154 ASSERT_TRUE(notification); 173 ASSERT_TRUE(notification);
155 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); 174 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( ));
156 175
157 notification = filterEngine->GetNextNotificationToShow("http://www.com"); 176 notification = PeekNotification("http://www.com");
158 ASSERT_TRUE(notification); 177 ASSERT_TRUE(notification);
159 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 178 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
160 } 179 }
161 180
162 TEST_F(NotificationTest, MarkAsShown) 181 TEST_F(NotificationTest, MarkAsShown)
163 { 182 {
164 AddNotification("{ id: 'id', type: 'question' }"); 183 AddNotification("{ id: 'id', type: 'question' }");
165 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 184 EXPECT_TRUE(PeekNotification());
166 EXPECT_TRUE(notification); 185 NotificationPtr notification = PeekNotification();
167 notification = filterEngine->GetNextNotificationToShow();
168 ASSERT_TRUE(notification); 186 ASSERT_TRUE(notification);
169 notification->MarkAsShown(); 187 notification->MarkAsShown();
170 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); 188 EXPECT_FALSE(PeekNotification());
171 } 189 }
172 190
173 TEST_F(NotificationTest, NoLinks) 191 TEST_F(NotificationTest, NoLinks)
174 { 192 {
175 AddNotification("{ id: 'id'}"); 193 AddNotification("{ id: 'id'}");
176 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 194 NotificationPtr notification = PeekNotification();
177 ASSERT_TRUE(notification); 195 ASSERT_TRUE(notification);
178 std::vector<std::string> notificationLinks = notification->GetLinks(); 196 EXPECT_EQ(0, notification->GetLinks().size());
179 EXPECT_EQ(0, notificationLinks.size());
180 } 197 }
181 198
182 TEST_F(NotificationTest, Links) 199 TEST_F(NotificationTest, Links)
183 { 200 {
184 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); 201 AddNotification("{ id: 'id', links: ['link1', 'link2'] }");
185 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); 202 NotificationPtr notification = PeekNotification();
186 ASSERT_TRUE(notification); 203 ASSERT_TRUE(notification);
187 std::vector<std::string> notificationLinks = notification->GetLinks(); 204 std::vector<std::string> notificationLinks = notification->GetLinks();
188 ASSERT_EQ(2, notificationLinks.size()); 205 ASSERT_EQ(2, notificationLinks.size());
189 EXPECT_EQ("link1", notificationLinks[0]); 206 EXPECT_EQ("link1", notificationLinks[0]);
190 EXPECT_EQ("link2", notificationLinks[1]); 207 EXPECT_EQ("link2", notificationLinks[1]);
191 } 208 }
Felix Dahlke 2015/06/30 19:45:32 Nit: Was a newline at EOF added here, or removed?
sergei 2015/07/01 11:46:09 I guess it was removed, so I've added '\n' here.
OLDNEW
« include/AdblockPlus/FilterEngine.h ('K') | « src/FilterEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld