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

Side by Side Diff: test/Notification.cpp

Issue 29419629: Issue 5164 - Remove NotificationPtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created April 21, 2017, 2:08 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/Notification.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 30 matching lines...) Expand all
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 50
51 NotificationPtr PeekNotification(const std::string& url = std::string()) 51 Notification PeekNotification(const std::string& url = std::string())
sergei 2017/04/21 15:30:33 I think it would be better to use std::unique_ptr
hub 2017/04/21 16:29:31 and it would solve the other problem with the cons
52 { 52 {
53 NotificationPtr retValue; 53 Notification retValue(filterEngine->GetJsEngine()->Evaluate("undefined"));
54 filterEngine->SetShowNotificationCallback(std::bind( 54 filterEngine->SetShowNotificationCallback(std::bind(
55 &NotificationTest::NotificationAvailableCallback, 55 &NotificationTest::NotificationAvailableCallback,
56 std::placeholders::_1, std::ref(retValue))); 56 std::placeholders::_1, std::ref(retValue)));
sergei 2017/04/21 15:30:33 JIC, now we may use lambda functions
hub 2017/04/21 16:29:31 ok will change that while I'm at it.
57 filterEngine->ShowNextNotification(url); 57 filterEngine->ShowNextNotification(url);
58 filterEngine->RemoveShowNotificationCallback(); 58 filterEngine->RemoveShowNotificationCallback();
59 return retValue; 59 return retValue;
60 } 60 }
61 61
62 static void NotificationAvailableCallback(const NotificationPtr& src, Notifi cationPtr& dst) 62 static void NotificationAvailableCallback(const Notification& src, Notificat ion& dst)
63 { 63 {
64 EXPECT_TRUE(src); 64 EXPECT_FALSE(src.IsUndefined());
65 dst = src; 65 dst = src;
66 } 66 }
67 }; 67 };
68 68
69 class MockWebRequest : public WebRequest 69 class MockWebRequest : public WebRequest
70 { 70 {
71 public: 71 public:
72 std::string responseText; 72 std::string responseText;
73 explicit MockWebRequest(const std::string& notification) 73 explicit MockWebRequest(const std::string& notification)
74 : responseText(notification) 74 : responseText(notification)
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 "}"; 113 "}";
114 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( 114 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>(
115 new MockWebRequest(responseJsonText))); 115 new MockWebRequest(responseJsonText)));
116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
117 filterEngine.reset(new FilterEngine(jsEngine)); 117 filterEngine.reset(new FilterEngine(jsEngine));
118 filterEngine->SetShowNotificationCallback( 118 filterEngine->SetShowNotificationCallback(
119 std::bind(&NotificationMockWebRequestTest::OnNotification, 119 std::bind(&NotificationMockWebRequestTest::OnNotification,
120 this, std::placeholders::_1)); 120 this, std::placeholders::_1));
121 } 121 }
122 122
123 void OnNotification(const NotificationPtr& notification) 123 void OnNotification(const Notification& notification)
124 { 124 {
125 isNotificationCallbackCalled = true; 125 isNotificationCallbackCalled = true;
126 ASSERT_TRUE(notification); 126 ASSERT_TRUE(notification);
127 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->G etType()); 127 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->G etType());
128 EXPECT_EQ("Title", notification->GetTexts().title); 128 EXPECT_EQ("Title", notification->GetTexts().title);
sergei 2017/04/21 15:30:33 This is not compilable. In order to run this test
hub 2017/04/21 16:29:32 I realize this code doesn't get built. Will fix th
129 EXPECT_EQ("message", notification->GetTexts().message); 129 EXPECT_EQ("message", notification->GetTexts().message);
130 notification->MarkAsShown(); 130 notification->MarkAsShown();
131 } 131 }
132 }; 132 };
133 #endif 133 #endif
134 } 134 }
135 135
136 TEST_F(NotificationTest, NoNotifications) 136 TEST_F(NotificationTest, NoNotifications)
137 { 137 {
138 EXPECT_FALSE(PeekNotification()); 138 EXPECT_TRUE(PeekNotification().IsUndefined());
139 } 139 }
140 140
141 #ifdef NotificationMockWebRequestTest_ENABLED 141 #ifdef NotificationMockWebRequestTest_ENABLED
142 TEST_F(NotificationMockWebRequestTest, SingleNotification) 142 TEST_F(NotificationMockWebRequestTest, SingleNotification)
143 { 143 {
144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack 144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack
145 EXPECT_TRUE(isNotificationCallbackCalled); 145 EXPECT_TRUE(isNotificationCallbackCalled);
146 } 146 }
147 #endif 147 #endif
148 148
149 TEST_F(NotificationTest, AddNotification) 149 TEST_F(NotificationTest, AddNotification)
150 { 150 {
151 AddNotification("{" 151 AddNotification("{"
152 "type: 'critical'," 152 "type: 'critical',"
153 "title: 'testTitle'," 153 "title: 'testTitle',"
154 "message: 'testMessage'," 154 "message: 'testMessage',"
155 "}"); 155 "}");
156 NotificationPtr notification = PeekNotification(); 156 Notification notification = PeekNotification();
157 ASSERT_TRUE(notification); 157 EXPECT_FALSE(notification.IsUndefined());
158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification.GetType() );
159 EXPECT_EQ("testTitle", notification->GetTexts().title); 159 EXPECT_EQ("testTitle", notification.GetTexts().title);
160 EXPECT_EQ("testMessage", notification->GetTexts().message); 160 EXPECT_EQ("testMessage", notification.GetTexts().message);
161 } 161 }
162 162
163 TEST_F(NotificationTest, FilterByUrl) 163 TEST_F(NotificationTest, FilterByUrl)
164 { 164 {
165 AddNotification("{ id: 'no-filter', type: 'critical' }"); 165 AddNotification("{ id: 'no-filter', type: 'critical' }");
166 AddNotification("{ id: 'www.com', type: 'information'," 166 AddNotification("{ id: 'www.com', type: 'information',"
167 "urlFilters:['||www.com$document']" 167 "urlFilters:['||www.com$document']"
168 "}"); 168 "}");
169 AddNotification("{ id: 'www.de', type: 'question'," 169 AddNotification("{ id: 'www.de', type: 'question',"
170 "urlFilters:['||www.de$document']" 170 "urlFilters:['||www.de$document']"
171 "}"); 171 "}");
172 172
173 NotificationPtr notification = PeekNotification(); 173 Notification notification = PeekNotification();
174 ASSERT_TRUE(notification); 174 EXPECT_FALSE(notification.IsUndefined());
175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification.GetType() );
176 176
177 notification = PeekNotification("http://www.de"); 177 notification = PeekNotification("http://www.de");
178 ASSERT_TRUE(notification); 178 EXPECT_FALSE(notification.IsUndefined());
179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); 179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification.GetType() );
180 180
181 notification = PeekNotification("http://www.com"); 181 notification = PeekNotification("http://www.com");
182 ASSERT_TRUE(notification); 182 EXPECT_FALSE(notification.IsUndefined());
183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification.GetTyp e());
184 } 184 }
185 185
186 TEST_F(NotificationTest, MarkAsShown) 186 TEST_F(NotificationTest, MarkAsShown)
187 { 187 {
188 AddNotification("{ id: 'id', type: 'question' }"); 188 AddNotification("{ id: 'id', type: 'question' }");
189 EXPECT_TRUE(PeekNotification()); 189 EXPECT_FALSE(PeekNotification().IsUndefined());
190 NotificationPtr notification = PeekNotification(); 190 Notification notification = PeekNotification();
191 ASSERT_TRUE(notification); 191 notification.MarkAsShown();
192 notification->MarkAsShown(); 192 EXPECT_TRUE(PeekNotification().IsUndefined());
193 EXPECT_FALSE(PeekNotification());
194 } 193 }
195 194
196 TEST_F(NotificationTest, NoLinks) 195 TEST_F(NotificationTest, NoLinks)
197 { 196 {
198 AddNotification("{ id: 'id'}"); 197 AddNotification("{ id: 'id'}");
199 NotificationPtr notification = PeekNotification(); 198 Notification notification = PeekNotification();
200 ASSERT_TRUE(notification); 199 EXPECT_FALSE(notification.IsUndefined());
201 EXPECT_EQ(0u, notification->GetLinks().size()); 200 EXPECT_EQ(0u, notification.GetLinks().size());
202 } 201 }
203 202
204 TEST_F(NotificationTest, Links) 203 TEST_F(NotificationTest, Links)
205 { 204 {
206 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); 205 AddNotification("{ id: 'id', links: ['link1', 'link2'] }");
207 NotificationPtr notification = PeekNotification(); 206 Notification notification = PeekNotification();
208 ASSERT_TRUE(notification); 207 EXPECT_FALSE(notification.IsUndefined());
209 std::vector<std::string> notificationLinks = notification->GetLinks(); 208 std::vector<std::string> notificationLinks = notification.GetLinks();
210 ASSERT_EQ(2u, notificationLinks.size()); 209 ASSERT_EQ(2u, notificationLinks.size());
211 EXPECT_EQ("link1", notificationLinks[0]); 210 EXPECT_EQ("link1", notificationLinks[0]);
212 EXPECT_EQ("link2", notificationLinks[1]); 211 EXPECT_EQ("link2", notificationLinks[1]);
213 } 212 }
OLDNEW
« include/AdblockPlus/Notification.h ('K') | « src/FilterEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld