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