Left: | ||
Right: |
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-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 Loading... | |
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 Notification PeekNotification(const std::string& url = std::string()) | 51 std::unique_ptr<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 Notification retValue(filterEngine->GetJsEngine()->Evaluate("undefined")); | 53 std::unique_ptr<Notification> retValue; |
54 filterEngine->SetShowNotificationCallback(std::bind( | 54 filterEngine->SetShowNotificationCallback( |
55 &NotificationTest::NotificationAvailableCallback, | 55 [&retValue](Notification& notification) { |
56 std::placeholders::_1, std::ref(retValue))); | 56 retValue.reset(new Notification(std::move(notification))); |
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 }); | |
57 filterEngine->ShowNextNotification(url); | 58 filterEngine->ShowNextNotification(url); |
58 filterEngine->RemoveShowNotificationCallback(); | 59 filterEngine->RemoveShowNotificationCallback(); |
59 return retValue; | 60 return retValue; |
60 } | |
61 | |
62 static void NotificationAvailableCallback(const Notification& src, Notificat ion& dst) | |
63 { | |
64 EXPECT_FALSE(src.IsUndefined()); | |
65 dst = src; | |
66 } | 61 } |
67 }; | 62 }; |
68 | 63 |
69 class MockWebRequest : public WebRequest | 64 class MockWebRequest : public WebRequest |
70 { | 65 { |
71 public: | 66 public: |
72 std::string responseText; | 67 std::string responseText; |
73 explicit MockWebRequest(const std::string& notification) | 68 explicit MockWebRequest(const std::string& notification) |
74 : responseText(notification) | 69 : responseText(notification) |
75 { | 70 { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 "\"type\": \"information\"," | 102 "\"type\": \"information\"," |
108 "\"message\": {" | 103 "\"message\": {" |
109 "\"en-US\": \"message\"" | 104 "\"en-US\": \"message\"" |
110 "}," | 105 "}," |
111 "\"title\": \"Title\"" | 106 "\"title\": \"Title\"" |
112 "}]" | 107 "}]" |
113 "}"; | 108 "}"; |
114 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( | 109 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( |
115 new MockWebRequest(responseJsonText))); | 110 new MockWebRequest(responseJsonText))); |
116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 111 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
117 filterEngine.reset(new FilterEngine(jsEngine)); | 112 filterEngine = FilterEngine::Create(jsEngine); |
118 filterEngine->SetShowNotificationCallback( | 113 filterEngine->SetShowNotificationCallback( |
119 std::bind(&NotificationMockWebRequestTest::OnNotification, | 114 [this](Notification& notification) { |
120 this, std::placeholders::_1)); | 115 isNotificationCallbackCalled = true; |
121 } | 116 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notificatio n.GetType()); |
122 | 117 EXPECT_EQ("Title", notification.GetTexts().title); |
123 void OnNotification(const Notification& notification) | 118 EXPECT_EQ("message", notification.GetTexts().message); |
124 { | 119 notification.MarkAsShown(); |
125 isNotificationCallbackCalled = true; | 120 }); |
126 ASSERT_TRUE(notification); | |
127 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->G etType()); | |
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); | |
130 notification->MarkAsShown(); | |
131 } | 121 } |
132 }; | 122 }; |
133 #endif | 123 #endif |
134 } | 124 } |
135 | 125 |
136 TEST_F(NotificationTest, NoNotifications) | 126 TEST_F(NotificationTest, NoNotifications) |
137 { | 127 { |
138 EXPECT_TRUE(PeekNotification().IsUndefined()); | 128 EXPECT_FALSE(PeekNotification()); |
139 } | 129 } |
140 | 130 |
141 #ifdef NotificationMockWebRequestTest_ENABLED | 131 #ifdef NotificationMockWebRequestTest_ENABLED |
142 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 132 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
143 { | 133 { |
144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 134 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
145 EXPECT_TRUE(isNotificationCallbackCalled); | 135 EXPECT_TRUE(isNotificationCallbackCalled); |
146 } | 136 } |
147 #endif | 137 #endif |
148 | 138 |
149 TEST_F(NotificationTest, AddNotification) | 139 TEST_F(NotificationTest, AddNotification) |
150 { | 140 { |
151 AddNotification("{" | 141 AddNotification("{" |
152 "type: 'critical'," | 142 "type: 'critical'," |
153 "title: 'testTitle'," | 143 "title: 'testTitle'," |
154 "message: 'testMessage'," | 144 "message: 'testMessage'," |
155 "}"); | 145 "}"); |
156 Notification notification = PeekNotification(); | 146 auto notification = PeekNotification(); |
157 EXPECT_FALSE(notification.IsUndefined()); | 147 ASSERT_TRUE(notification); |
158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification.GetType() ); | 148 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
159 EXPECT_EQ("testTitle", notification.GetTexts().title); | 149 EXPECT_EQ("testTitle", notification->GetTexts().title); |
160 EXPECT_EQ("testMessage", notification.GetTexts().message); | 150 EXPECT_EQ("testMessage", notification->GetTexts().message); |
161 } | 151 } |
162 | 152 |
163 TEST_F(NotificationTest, FilterByUrl) | 153 TEST_F(NotificationTest, FilterByUrl) |
164 { | 154 { |
165 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 155 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
166 AddNotification("{ id: 'www.com', type: 'information'," | 156 AddNotification("{ id: 'www.com', type: 'information'," |
167 "urlFilters:['||www.com$document']" | 157 "urlFilters:['||www.com$document']" |
168 "}"); | 158 "}"); |
169 AddNotification("{ id: 'www.de', type: 'question'," | 159 AddNotification("{ id: 'www.de', type: 'question'," |
170 "urlFilters:['||www.de$document']" | 160 "urlFilters:['||www.de$document']" |
171 "}"); | 161 "}"); |
172 | 162 |
173 Notification notification = PeekNotification(); | 163 auto notification = PeekNotification(); |
174 EXPECT_FALSE(notification.IsUndefined()); | 164 ASSERT_TRUE(notification); |
175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification.GetType() ); | 165 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
176 | 166 |
177 notification = PeekNotification("http://www.de"); | 167 notification = PeekNotification("http://www.de"); |
178 EXPECT_FALSE(notification.IsUndefined()); | 168 ASSERT_TRUE(notification); |
179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification.GetType() ); | 169 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); |
180 | 170 |
181 notification = PeekNotification("http://www.com"); | 171 notification = PeekNotification("http://www.com"); |
182 EXPECT_FALSE(notification.IsUndefined()); | 172 ASSERT_TRUE(notification); |
183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification.GetTyp e()); | 173 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); |
184 } | 174 } |
185 | 175 |
186 TEST_F(NotificationTest, MarkAsShown) | 176 TEST_F(NotificationTest, MarkAsShown) |
187 { | 177 { |
188 AddNotification("{ id: 'id', type: 'question' }"); | 178 AddNotification("{ id: 'id', type: 'question' }"); |
189 EXPECT_FALSE(PeekNotification().IsUndefined()); | 179 EXPECT_TRUE(PeekNotification()); |
190 Notification notification = PeekNotification(); | 180 auto notification = PeekNotification(); |
191 notification.MarkAsShown(); | 181 ASSERT_TRUE(notification); |
192 EXPECT_TRUE(PeekNotification().IsUndefined()); | 182 notification->MarkAsShown(); |
183 EXPECT_FALSE(PeekNotification()); | |
193 } | 184 } |
194 | 185 |
195 TEST_F(NotificationTest, NoLinks) | 186 TEST_F(NotificationTest, NoLinks) |
196 { | 187 { |
197 AddNotification("{ id: 'id'}"); | 188 AddNotification("{ id: 'id'}"); |
198 Notification notification = PeekNotification(); | 189 auto notification = PeekNotification(); |
199 EXPECT_FALSE(notification.IsUndefined()); | 190 ASSERT_TRUE(notification); |
200 EXPECT_EQ(0u, notification.GetLinks().size()); | 191 EXPECT_EQ(0u, notification->GetLinks().size()); |
201 } | 192 } |
202 | 193 |
203 TEST_F(NotificationTest, Links) | 194 TEST_F(NotificationTest, Links) |
204 { | 195 { |
205 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 196 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
206 Notification notification = PeekNotification(); | 197 auto notification = PeekNotification(); |
207 EXPECT_FALSE(notification.IsUndefined()); | 198 ASSERT_TRUE(notification); |
208 std::vector<std::string> notificationLinks = notification.GetLinks(); | 199 std::vector<std::string> notificationLinks = notification->GetLinks(); |
209 ASSERT_EQ(2u, notificationLinks.size()); | 200 ASSERT_EQ(2u, notificationLinks.size()); |
210 EXPECT_EQ("link1", notificationLinks[0]); | 201 EXPECT_EQ("link1", notificationLinks[0]); |
211 EXPECT_EQ("link2", notificationLinks[1]); | 202 EXPECT_EQ("link2", notificationLinks[1]); |
212 } | 203 } |
LEFT | RIGHT |