OLD | NEW |
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 Loading... |
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 struct PeekNotificationResult |
| 52 { |
| 53 bool isCallbackCalled; |
| 54 NotificationPtr notification; |
| 55 }; |
| 56 PeekNotificationResult PeekNotification(const std::string& url = std::string
()) |
| 57 { |
| 58 PeekNotificationResult retValue = {false}; |
| 59 filterEngine->SetNotificationAvailableCallback([&retValue](const Notificat
ionPtr& notification) |
| 60 { |
| 61 retValue.isCallbackCalled = true; |
| 62 retValue.notification = notification; |
| 63 }); |
| 64 filterEngine->ShowNextNotification(url); |
| 65 filterEngine->RemoveNotificationAvailableCallback(); |
| 66 return retValue; |
| 67 } |
50 }; | 68 }; |
51 | 69 |
52 class MockWebRequest : public WebRequest | 70 class MockWebRequest : public WebRequest |
53 { | 71 { |
54 public: | 72 public: |
55 std::string responseText; | 73 std::string responseText; |
56 explicit MockWebRequest(const std::string& notification) | 74 explicit MockWebRequest(const std::string& notification) |
57 : responseText(notification) | 75 : responseText(notification) |
58 { | 76 { |
59 } | 77 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 new MockWebRequest(responseJsonText))); | 114 new MockWebRequest(responseJsonText))); |
97 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 115 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
98 filterEngine.reset(new FilterEngine(jsEngine)); | 116 filterEngine.reset(new FilterEngine(jsEngine)); |
99 } | 117 } |
100 }; | 118 }; |
101 #endif | 119 #endif |
102 } | 120 } |
103 | 121 |
104 TEST_F(NotificationTest, NoNotifications) | 122 TEST_F(NotificationTest, NoNotifications) |
105 { | 123 { |
106 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 124 EXPECT_FALSE(PeekNotification().isCallbackCalled); |
107 EXPECT_EQ(NULL, notification.get()); | |
108 } | 125 } |
109 | 126 |
110 #ifdef NotificationMockWebRequestTest_ENABLED | 127 #ifdef NotificationMockWebRequestTest_ENABLED |
111 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 128 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
112 { | 129 { |
| 130 bool isNotificationCallbackCalled = false; |
| 131 filterEngine->SetNotificationAvailableCallback([&isNotificationCallbackCalled]
(const NotificationPtr& notification) |
| 132 { |
| 133 isNotificationCallbackCalled = true; |
| 134 ASSERT_TRUE(notification); |
| 135 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->Get
Type()); |
| 136 EXPECT_EQ("Title", notification->GetTitle()); |
| 137 EXPECT_EQ("message", notification->GetMessageString()); |
| 138 notification->MarkAsShown(); |
| 139 }); |
113 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 140 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
114 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 141 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 } | 142 } |
123 #endif | 143 #endif |
124 | 144 |
125 TEST_F(NotificationTest, AddNotification) | 145 TEST_F(NotificationTest, AddNotification) |
126 { | 146 { |
127 AddNotification("{" | 147 AddNotification("{" |
128 "type: 'critical'," | 148 "type: 'critical'," |
129 "title: 'testTitle'," | 149 "title: 'testTitle'," |
130 "message: 'testMessage'," | 150 "message: 'testMessage'," |
131 "}"); | 151 "}"); |
132 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 152 PeekNotificationResult peekNotificationResult = PeekNotification(); |
133 ASSERT_TRUE(notification); | 153 ASSERT_TRUE(peekNotificationResult.notification); |
134 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 154 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, peekNotificationResult
.notification->GetType()); |
135 EXPECT_EQ("testTitle", notification->GetTitle()); | 155 EXPECT_EQ("testTitle", peekNotificationResult.notification->GetTitle()); |
136 EXPECT_EQ("testMessage", notification->GetMessageString()); | 156 EXPECT_EQ("testMessage", peekNotificationResult.notification->GetMessageString
()); |
137 } | 157 } |
138 | 158 |
139 TEST_F(NotificationTest, FilterByUrl) | 159 TEST_F(NotificationTest, FilterByUrl) |
140 { | 160 { |
141 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 161 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
142 AddNotification("{ id: 'www.com', type: 'information'," | 162 AddNotification("{ id: 'www.com', type: 'information'," |
143 "urlFilters:['http://www.com']" | 163 "urlFilters:['||www.com$document']" |
144 "}"); | 164 "}"); |
145 AddNotification("{ id: 'www.de', type: 'question'," | 165 AddNotification("{ id: 'www.de', type: 'question'," |
146 "urlFilters:['http://www.de']" | 166 "urlFilters:['||www.de$document']" |
147 "}"); | 167 "}"); |
148 | 168 |
149 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 169 PeekNotificationResult peekNotificationResult = PeekNotification(); |
150 ASSERT_TRUE(notification); | 170 ASSERT_TRUE(peekNotificationResult.notification); |
151 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 171 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, peekNotificationResult
.notification->GetType()); |
152 | 172 |
153 notification = filterEngine->GetNextNotificationToShow("http://www.de"); | 173 peekNotificationResult = PeekNotification("http://www.de"); |
154 ASSERT_TRUE(notification); | 174 ASSERT_TRUE(peekNotificationResult.notification); |
155 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); | 175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, peekNotificationResult
.notification->GetType()); |
156 | 176 |
157 notification = filterEngine->GetNextNotificationToShow("http://www.com"); | 177 peekNotificationResult = PeekNotification("http://www.com"); |
158 ASSERT_TRUE(notification); | 178 ASSERT_TRUE(peekNotificationResult.notification); |
159 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); | 179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, peekNotificationRes
ult.notification->GetType()); |
160 } | 180 } |
161 | 181 |
162 TEST_F(NotificationTest, MarkAsShown) | 182 TEST_F(NotificationTest, MarkAsShown) |
163 { | 183 { |
164 AddNotification("{ id: 'id', type: 'question' }"); | 184 AddNotification("{ id: 'id', type: 'question' }"); |
165 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 185 EXPECT_TRUE(PeekNotification().notification); |
166 EXPECT_TRUE(notification); | 186 PeekNotificationResult peekNotificationResult = PeekNotification(); |
167 notification = filterEngine->GetNextNotificationToShow(); | 187 ASSERT_TRUE(peekNotificationResult.notification); |
168 ASSERT_TRUE(notification); | 188 peekNotificationResult.notification->MarkAsShown(); |
169 notification->MarkAsShown(); | 189 EXPECT_FALSE(PeekNotification().isCallbackCalled); |
170 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); | |
171 } | 190 } |
172 | 191 |
173 TEST_F(NotificationTest, NoLinks) | 192 TEST_F(NotificationTest, NoLinks) |
174 { | 193 { |
175 AddNotification("{ id: 'id'}"); | 194 AddNotification("{ id: 'id'}"); |
176 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 195 PeekNotificationResult peekNotificationResult = PeekNotification(); |
177 ASSERT_TRUE(notification); | 196 ASSERT_TRUE(peekNotificationResult.notification); |
178 std::vector<std::string> notificationLinks = notification->GetLinks(); | 197 EXPECT_EQ(0, peekNotificationResult.notification->GetLinks().size()); |
179 EXPECT_EQ(0, notificationLinks.size()); | |
180 } | 198 } |
181 | 199 |
182 TEST_F(NotificationTest, Links) | 200 TEST_F(NotificationTest, Links) |
183 { | 201 { |
184 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 202 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
185 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 203 PeekNotificationResult peekNotificationResult = PeekNotification(); |
186 ASSERT_TRUE(notification); | 204 ASSERT_TRUE(peekNotificationResult.notification); |
187 std::vector<std::string> notificationLinks = notification->GetLinks(); | 205 std::vector<std::string> notificationLinks = peekNotificationResult.notificati
on->GetLinks(); |
188 ASSERT_EQ(2, notificationLinks.size()); | 206 ASSERT_EQ(2, notificationLinks.size()); |
189 EXPECT_EQ("link1", notificationLinks[0]); | 207 EXPECT_EQ("link1", notificationLinks[0]); |
190 EXPECT_EQ("link2", notificationLinks[1]); | 208 EXPECT_EQ("link2", notificationLinks[1]); |
191 } | 209 } |
OLD | NEW |