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 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 10 matching lines...) Expand all Loading... |
70 serverResponse.responseText = responseText; | 87 serverResponse.responseText = responseText; |
71 return serverResponse; | 88 return serverResponse; |
72 } | 89 } |
73 }; | 90 }; |
74 | 91 |
75 #ifdef NotificationMockWebRequestTest_ENABLED | 92 #ifdef NotificationMockWebRequestTest_ENABLED |
76 class NotificationMockWebRequestTest : public BaseJsTest | 93 class NotificationMockWebRequestTest : public BaseJsTest |
77 { | 94 { |
78 protected: | 95 protected: |
79 FilterEnginePtr filterEngine; | 96 FilterEnginePtr filterEngine; |
| 97 bool isNotificationCallbackCalled; |
80 void SetUp() | 98 void SetUp() |
81 { | 99 { |
82 BaseJsTest::SetUp(); | 100 BaseJsTest::SetUp(); |
| 101 isNotificationCallbackCalled = false; |
83 jsEngine->SetFileSystem( | 102 jsEngine->SetFileSystem( |
84 std::tr1::shared_ptr<LazyFileSystem>(new LazyFileSystem())); | 103 std::tr1::shared_ptr<LazyFileSystem>(new LazyFileSystem())); |
85 const char* responseJsonText = "{" | 104 const char* responseJsonText = "{" |
86 "\"notifications\": [{" | 105 "\"notifications\": [{" |
87 "\"id\": \"some id\"," | 106 "\"id\": \"some id\"," |
88 "\"type\": \"information\"," | 107 "\"type\": \"information\"," |
89 "\"message\": {" | 108 "\"message\": {" |
90 "\"en-US\": \"message\"" | 109 "\"en-US\": \"message\"" |
91 "}," | 110 "}," |
92 "\"title\": \"Title\"" | 111 "\"title\": \"Title\"" |
93 "}]" | 112 "}]" |
94 "}"; | 113 "}"; |
95 jsEngine->SetWebRequest(std::tr1::shared_ptr<MockWebRequest>( | 114 jsEngine->SetWebRequest(std::tr1::shared_ptr<MockWebRequest>( |
96 new MockWebRequest(responseJsonText))); | 115 new MockWebRequest(responseJsonText))); |
97 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
98 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(); |
99 } | 131 } |
100 }; | 132 }; |
101 #endif | 133 #endif |
102 } | 134 } |
103 | 135 |
104 TEST_F(NotificationTest, NoNotifications) | 136 TEST_F(NotificationTest, NoNotifications) |
105 { | 137 { |
106 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 138 EXPECT_FALSE(PeekNotification()); |
107 EXPECT_EQ(NULL, notification.get()); | |
108 } | 139 } |
109 | 140 |
110 #ifdef NotificationMockWebRequestTest_ENABLED | 141 #ifdef NotificationMockWebRequestTest_ENABLED |
111 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 142 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
112 { | 143 { |
113 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
114 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 145 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 } | 146 } |
123 #endif | 147 #endif |
124 | 148 |
125 TEST_F(NotificationTest, AddNotification) | 149 TEST_F(NotificationTest, AddNotification) |
126 { | 150 { |
127 AddNotification("{" | 151 AddNotification("{" |
128 "type: 'critical'," | 152 "type: 'critical'," |
129 "title: 'testTitle'," | 153 "title: 'testTitle'," |
130 "message: 'testMessage'," | 154 "message: 'testMessage'," |
131 "}"); | 155 "}"); |
132 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 156 NotificationPtr notification = PeekNotification(); |
133 ASSERT_TRUE(notification); | 157 ASSERT_TRUE(notification); |
134 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
135 EXPECT_EQ("testTitle", notification->GetTitle()); | 159 EXPECT_EQ("testTitle", notification->GetTitle()); |
136 EXPECT_EQ("testMessage", notification->GetMessageString()); | 160 EXPECT_EQ("testMessage", notification->GetMessageString()); |
137 } | 161 } |
138 | 162 |
139 TEST_F(NotificationTest, FilterByUrl) | 163 TEST_F(NotificationTest, FilterByUrl) |
140 { | 164 { |
141 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 165 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
142 AddNotification("{ id: 'www.com', type: 'information'," | 166 AddNotification("{ id: 'www.com', type: 'information'," |
143 "urlFilters:['http://www.com']" | 167 "urlFilters:['||www.com$document']" |
144 "}"); | 168 "}"); |
145 AddNotification("{ id: 'www.de', type: 'question'," | 169 AddNotification("{ id: 'www.de', type: 'question'," |
146 "urlFilters:['http://www.de']" | 170 "urlFilters:['||www.de$document']" |
147 "}"); | 171 "}"); |
148 | 172 |
149 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 173 NotificationPtr notification = PeekNotification(); |
150 ASSERT_TRUE(notification); | 174 ASSERT_TRUE(notification); |
151 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
152 | 176 |
153 notification = filterEngine->GetNextNotificationToShow("http://www.de"); | 177 notification = PeekNotification("http://www.de"); |
154 ASSERT_TRUE(notification); | 178 ASSERT_TRUE(notification); |
155 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); | 179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); |
156 | 180 |
157 notification = filterEngine->GetNextNotificationToShow("http://www.com"); | 181 notification = PeekNotification("http://www.com"); |
158 ASSERT_TRUE(notification); | 182 ASSERT_TRUE(notification); |
159 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); | 183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); |
160 } | 184 } |
161 | 185 |
162 TEST_F(NotificationTest, MarkAsShown) | 186 TEST_F(NotificationTest, MarkAsShown) |
163 { | 187 { |
164 AddNotification("{ id: 'id', type: 'question' }"); | 188 AddNotification("{ id: 'id', type: 'question' }"); |
165 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 189 EXPECT_TRUE(PeekNotification()); |
166 EXPECT_TRUE(notification); | 190 NotificationPtr notification = PeekNotification(); |
167 notification = filterEngine->GetNextNotificationToShow(); | |
168 ASSERT_TRUE(notification); | 191 ASSERT_TRUE(notification); |
169 notification->MarkAsShown(); | 192 notification->MarkAsShown(); |
170 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); | 193 EXPECT_FALSE(PeekNotification()); |
171 } | 194 } |
172 | 195 |
173 TEST_F(NotificationTest, NoLinks) | 196 TEST_F(NotificationTest, NoLinks) |
174 { | 197 { |
175 AddNotification("{ id: 'id'}"); | 198 AddNotification("{ id: 'id'}"); |
176 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 199 NotificationPtr notification = PeekNotification(); |
177 ASSERT_TRUE(notification); | 200 ASSERT_TRUE(notification); |
178 std::vector<std::string> notificationLinks = notification->GetLinks(); | 201 EXPECT_EQ(0, notification->GetLinks().size()); |
179 EXPECT_EQ(0, notificationLinks.size()); | |
180 } | 202 } |
181 | 203 |
182 TEST_F(NotificationTest, Links) | 204 TEST_F(NotificationTest, Links) |
183 { | 205 { |
184 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 206 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
185 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 207 NotificationPtr notification = PeekNotification(); |
186 ASSERT_TRUE(notification); | 208 ASSERT_TRUE(notification); |
187 std::vector<std::string> notificationLinks = notification->GetLinks(); | 209 std::vector<std::string> notificationLinks = notification->GetLinks(); |
188 ASSERT_EQ(2, notificationLinks.size()); | 210 ASSERT_EQ(2, notificationLinks.size()); |
189 EXPECT_EQ("link1", notificationLinks[0]); | 211 EXPECT_EQ("link1", notificationLinks[0]); |
190 EXPECT_EQ("link2", notificationLinks[1]); | 212 EXPECT_EQ("link2", notificationLinks[1]); |
191 } | 213 } |
OLD | NEW |