OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include "BaseJsTest.h" |
| 19 |
| 20 using namespace AdblockPlus; |
| 21 |
| 22 /* This define enables NotificationMockWebRequestTest but to run it |
| 23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js. |
| 24 #include <chrono> |
| 25 #include <thread> |
| 26 #define NotificationMockWebRequestTest_ENABLED |
| 27 //*/ |
| 28 |
| 29 namespace |
| 30 { |
| 31 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; |
| 32 |
| 33 class NotificationTest : public BaseJsTest |
| 34 { |
| 35 protected: |
| 36 FilterEnginePtr filterEngine; |
| 37 void SetUp() override |
| 38 { |
| 39 BaseJsTest::SetUp(); |
| 40 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); |
| 41 jsEngine->SetWebRequest(std::tr1::make_shared<LazyWebRequest>()); |
| 42 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 43 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
| 44 } |
| 45 }; |
| 46 |
| 47 class MockWebRequest : public WebRequest |
| 48 { |
| 49 public: |
| 50 std::string m_responseText; |
| 51 explicit MockWebRequest(const std::string& notification) |
| 52 : m_responseText(notification) |
| 53 { |
| 54 } |
| 55 ServerResponse GET(const std::string& url, |
| 56 const HeaderList& requestHeaders) const override |
| 57 { |
| 58 if (url.find("/notification.json") == std::string::npos) |
| 59 { |
| 60 return ServerResponse(); |
| 61 } |
| 62 ServerResponse serverResponse; |
| 63 serverResponse.status = NS_OK; |
| 64 serverResponse.responseStatus = 200; |
| 65 serverResponse.responseText = m_responseText; |
| 66 return serverResponse; |
| 67 } |
| 68 }; |
| 69 |
| 70 #ifdef NotificationMockWebRequestTest_ENABLED |
| 71 class NotificationMockWebRequestTest : public BaseJsTest |
| 72 { |
| 73 protected: |
| 74 FilterEnginePtr filterEngine; |
| 75 void SetUp() override |
| 76 { |
| 77 BaseJsTest::SetUp(); |
| 78 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); |
| 79 const char* responseJsonText = "{" |
| 80 "\"notifications\": [{" |
| 81 "\"id\": \"some id\"," |
| 82 "\"type\": \"information\"," |
| 83 "\"message\": {" |
| 84 "\"en-US\": \"Critical message\"," |
| 85 "\"de\": \"Achtung\"" |
| 86 "}," |
| 87 "\"title\": \"Title\"" |
| 88 "}]" |
| 89 "}"; |
| 90 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson
Text)); |
| 91 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 92 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
| 93 } |
| 94 }; |
| 95 #endif |
| 96 } |
| 97 |
| 98 TEST_F(NotificationTest, NotificationCtr) |
| 99 { |
| 100 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC
ATION_TYPE_CRITICAL, "testId"); |
| 101 ASSERT_NE(nullptr, notification); |
| 102 EXPECT_EQ("testId", notification->GetId()); |
| 103 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 104 } |
| 105 |
| 106 TEST_F(NotificationTest, MultilingualProperties) |
| 107 { |
| 108 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC
ATION_TYPE_CRITICAL, "testId"); |
| 109 ASSERT_NE(nullptr, notification); |
| 110 notification->SetTitle("French", "fr-FR"); |
| 111 { |
| 112 auto texts = notification->GetTexts(); |
| 113 EXPECT_EQ("", texts.title); |
| 114 EXPECT_EQ("", texts.message); |
| 115 } |
| 116 { |
| 117 auto texts = notification->GetTexts("de"); |
| 118 EXPECT_EQ("", texts.title); |
| 119 EXPECT_EQ("", texts.message); |
| 120 } |
| 121 { |
| 122 auto texts = notification->GetTexts("fr"); |
| 123 EXPECT_EQ("", texts.title); |
| 124 EXPECT_EQ("", texts.message); |
| 125 } |
| 126 { |
| 127 auto texts = notification->GetTexts("fr-FR"); |
| 128 EXPECT_EQ("French", texts.title); |
| 129 EXPECT_EQ("", texts.message); |
| 130 } |
| 131 notification->SetTitle("German", "de"); |
| 132 { |
| 133 auto texts = notification->GetTexts(); |
| 134 EXPECT_EQ("", texts.title); |
| 135 EXPECT_EQ("", texts.message); |
| 136 } |
| 137 { |
| 138 auto texts = notification->GetTexts("de"); |
| 139 EXPECT_EQ("German", texts.title); |
| 140 EXPECT_EQ("", texts.message); |
| 141 } |
| 142 { |
| 143 auto texts = notification->GetTexts("de-DE"); |
| 144 EXPECT_EQ("German", texts.title); |
| 145 EXPECT_EQ("", texts.message); |
| 146 } |
| 147 { |
| 148 auto texts = notification->GetTexts("fr-FR"); |
| 149 EXPECT_EQ("French", texts.title); |
| 150 EXPECT_EQ("", texts.message); |
| 151 } |
| 152 notification->SetTitle("English", "en"); |
| 153 { |
| 154 auto texts = notification->GetTexts(); |
| 155 EXPECT_EQ("", texts.title); |
| 156 EXPECT_EQ("", texts.message); |
| 157 } |
| 158 { |
| 159 auto texts = notification->GetTexts("en"); |
| 160 EXPECT_EQ("English", texts.title); |
| 161 EXPECT_EQ("", texts.message); |
| 162 } |
| 163 { |
| 164 auto texts = notification->GetTexts("en-US"); |
| 165 EXPECT_EQ("English", texts.title); |
| 166 EXPECT_EQ("", texts.message); |
| 167 } |
| 168 { |
| 169 auto texts = notification->GetTexts("de"); |
| 170 EXPECT_EQ("German", texts.title); |
| 171 EXPECT_EQ("", texts.message); |
| 172 } |
| 173 { |
| 174 auto texts = notification->GetTexts("fr-FR"); |
| 175 EXPECT_EQ("French", texts.title); |
| 176 EXPECT_EQ("", texts.message); |
| 177 } |
| 178 notification->SetTitle("Default", "en-US"); |
| 179 { |
| 180 auto texts = notification->GetTexts(); |
| 181 EXPECT_EQ("Default", texts.title); |
| 182 EXPECT_EQ("", texts.message); |
| 183 } |
| 184 { |
| 185 auto texts = notification->GetTexts("en"); |
| 186 EXPECT_EQ("English", texts.title); |
| 187 EXPECT_EQ("", texts.message); |
| 188 } |
| 189 { |
| 190 auto texts = notification->GetTexts("en-US"); |
| 191 EXPECT_EQ("Default", texts.title); |
| 192 EXPECT_EQ("", texts.message); |
| 193 } |
| 194 { |
| 195 auto texts = notification->GetTexts("xx"); |
| 196 EXPECT_EQ("Default", texts.title); |
| 197 EXPECT_EQ("", texts.message); |
| 198 } |
| 199 |
| 200 notification->SetTitle("testTitleNoLocale"); |
| 201 { |
| 202 auto texts = notification->GetTexts(); |
| 203 EXPECT_EQ("testTitleNoLocale", texts.title); |
| 204 EXPECT_EQ("", texts.message); |
| 205 } |
| 206 { |
| 207 auto texts = notification->GetTexts("de-DE"); |
| 208 EXPECT_EQ("testTitleNoLocale", texts.title); |
| 209 EXPECT_EQ("", texts.message); |
| 210 } |
| 211 |
| 212 notification->SetTitle("French", "fr-FR"); |
| 213 { |
| 214 auto texts = notification->GetTexts(); |
| 215 EXPECT_EQ("", texts.title); |
| 216 EXPECT_EQ("", texts.message); |
| 217 } |
| 218 { |
| 219 auto texts = notification->GetTexts("de"); |
| 220 EXPECT_EQ("", texts.title); |
| 221 EXPECT_EQ("", texts.message); |
| 222 } |
| 223 { |
| 224 auto texts = notification->GetTexts("fr"); |
| 225 EXPECT_EQ("", texts.title); |
| 226 EXPECT_EQ("", texts.message); |
| 227 } |
| 228 { |
| 229 auto texts = notification->GetTexts("fr-FR"); |
| 230 EXPECT_EQ("French", texts.title); |
| 231 EXPECT_EQ("", texts.message); |
| 232 } |
| 233 } |
| 234 |
| 235 TEST_F(NotificationTest, UrlFilters) |
| 236 { |
| 237 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC
ATION_TYPE_CRITICAL, "testId"); |
| 238 ASSERT_NE(nullptr, notification); |
| 239 EXPECT_EQ(0, notification->GetUrlFilters().size()); |
| 240 notification->AddUrlFilter("filterA"); |
| 241 { |
| 242 auto urlFilters = notification->GetUrlFilters(); |
| 243 ASSERT_EQ(1, urlFilters.size()); |
| 244 EXPECT_EQ("filterA", urlFilters[0]); |
| 245 } |
| 246 notification->AddUrlFilter("filterB"); |
| 247 { |
| 248 auto urlFilters = notification->GetUrlFilters(); |
| 249 ASSERT_EQ(2, urlFilters.size()); |
| 250 EXPECT_EQ("filterA", urlFilters[0]); |
| 251 EXPECT_EQ("filterB", urlFilters[1]); |
| 252 } |
| 253 notification->AddUrlFilter("filterC"); |
| 254 { |
| 255 auto urlFilters = notification->GetUrlFilters(); |
| 256 ASSERT_EQ(3, urlFilters.size()); |
| 257 EXPECT_EQ("filterA", urlFilters[0]); |
| 258 EXPECT_EQ("filterB", urlFilters[1]); |
| 259 EXPECT_EQ("filterC", urlFilters[2]); |
| 260 } |
| 261 } |
| 262 |
| 263 TEST_F(NotificationTest, NoNotifications) |
| 264 { |
| 265 auto notification = filterEngine->GetNextNotificationToShow(); |
| 266 EXPECT_EQ(nullptr, notification); |
| 267 } |
| 268 |
| 269 #ifdef NotificationMockWebRequestTest_ENABLED |
| 270 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 271 { |
| 272 std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack |
| 273 auto notification = filterEngine->GetNextNotificationToShow(); |
| 274 // try another one immediately to avoid queuing of the next notification by |
| 275 // the timer. |
| 276 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 277 ASSERT_NE(nullptr, notification.get()); |
| 278 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); |
| 279 EXPECT_EQ("some id", notification->GetId()); |
| 280 auto defaultTexts = notification->GetTexts("xx"); |
| 281 EXPECT_EQ("Title", defaultTexts.title); |
| 282 EXPECT_EQ("Critical message", defaultTexts.message); |
| 283 auto deTexts = notification->GetTexts("de"); |
| 284 EXPECT_EQ("Title", deTexts.title); |
| 285 EXPECT_EQ("Achtung", deTexts.message); |
| 286 } |
| 287 #endif |
| 288 |
| 289 TEST_F(NotificationTest, AddNotification) |
| 290 { |
| 291 { |
| 292 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "id"); |
| 293 ASSERT_NE(nullptr, notification); |
| 294 filterEngine->AddNotification(notification); |
| 295 } |
| 296 auto notification = filterEngine->GetNextNotificationToShow(); |
| 297 ASSERT_NE(nullptr, notification.get()); |
| 298 EXPECT_EQ("id", notification->GetId()); |
| 299 } |
| 300 |
| 301 TEST_F(NotificationTest, FilterByUrl1) |
| 302 { |
| 303 { |
| 304 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "no-filter"); |
| 305 ASSERT_NE(nullptr, notification); |
| 306 filterEngine->AddNotification(notification); |
| 307 } |
| 308 { |
| 309 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "com"); |
| 310 ASSERT_NE(nullptr, notification); |
| 311 notification->AddUrlFilter("http://www.com"); |
| 312 filterEngine->AddNotification(notification); |
| 313 } |
| 314 { |
| 315 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "de"); |
| 316 ASSERT_NE(nullptr, notification); |
| 317 notification->AddUrlFilter("http://www.de"); |
| 318 filterEngine->AddNotification(notification); |
| 319 } |
| 320 auto notification = filterEngine->GetNextNotificationToShow(); |
| 321 ASSERT_NE(nullptr, notification.get()); |
| 322 EXPECT_EQ("no-filter", notification->GetId()); |
| 323 notification = filterEngine->GetNextNotificationToShow(); |
| 324 EXPECT_EQ(nullptr, notification.get()); |
| 325 notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 326 ASSERT_NE(nullptr, notification.get()); |
| 327 EXPECT_EQ("de", notification->GetId()); |
| 328 } |
| 329 TEST_F(NotificationTest, FilterByUrl2) |
| 330 { |
| 331 { |
| 332 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "no-filter"); |
| 333 ASSERT_NE(nullptr, notification); |
| 334 filterEngine->AddNotification(notification); |
| 335 } |
| 336 { |
| 337 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "com"); |
| 338 ASSERT_NE(nullptr, notification); |
| 339 notification->AddUrlFilter("http://www.com"); |
| 340 filterEngine->AddNotification(notification); |
| 341 } |
| 342 { |
| 343 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "de"); |
| 344 ASSERT_NE(nullptr, notification); |
| 345 notification->AddUrlFilter("http://www.de"); |
| 346 filterEngine->AddNotification(notification); |
| 347 } |
| 348 auto notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 349 ASSERT_NE(nullptr, notification.get()); |
| 350 EXPECT_EQ("de", notification->GetId()); |
| 351 notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 352 EXPECT_EQ(nullptr, notification.get()); |
| 353 notification = filterEngine->GetNextNotificationToShow(); |
| 354 ASSERT_NE(nullptr, notification.get()); |
| 355 EXPECT_EQ("no-filter", notification->GetId()); |
| 356 } |
| 357 |
| 358 TEST_F(NotificationTest, AddRemoveNotification) |
| 359 { |
| 360 auto newInfoNotification = filterEngine->CreateNotification(NotificationType::
NOTIFICATION_TYPE_INFORMATION, "id"); |
| 361 ASSERT_NE(nullptr, newInfoNotification); |
| 362 filterEngine->AddNotification(newInfoNotification); |
| 363 filterEngine->RemoveNotification(newInfoNotification); |
| 364 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 365 } |
| 366 |
| 367 TEST_F(NotificationTest, MarkAsShown) |
| 368 { |
| 369 auto newInfoNotification = filterEngine->CreateNotification(NotificationType::
NOTIFICATION_TYPE_INFORMATION, "id"); |
| 370 ASSERT_NE(nullptr, newInfoNotification); |
| 371 filterEngine->AddNotification(newInfoNotification); |
| 372 filterEngine->MarkNotificationAsShown("id"); |
| 373 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 374 } |
OLD | NEW |