Index: test/Notification.cpp |
diff --git a/test/Notification.cpp b/test/Notification.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8d91f9f937e00b26f246ffa9ae1640b508933361 |
--- /dev/null |
+++ b/test/Notification.cpp |
@@ -0,0 +1,374 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#include "BaseJsTest.h" |
+ |
+using namespace AdblockPlus; |
+ |
+/* This define enables NotificationMockWebRequestTest but to run it |
+// one need to set INITIAL_DELAY to about 2000 msec in notification.js. |
+#include <chrono> |
+#include <thread> |
+#define NotificationMockWebRequestTest_ENABLED |
+//*/ |
+ |
+namespace |
+{ |
+ typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; |
+ |
+ class NotificationTest : public BaseJsTest |
+ { |
+ protected: |
+ FilterEnginePtr filterEngine; |
+ void SetUp() override |
+ { |
+ BaseJsTest::SetUp(); |
+ jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); |
+ jsEngine->SetWebRequest(std::tr1::make_shared<LazyWebRequest>()); |
+ jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
+ filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
+ } |
+ }; |
+ |
+ class MockWebRequest : public WebRequest |
+ { |
+ public: |
+ std::string m_responseText; |
+ explicit MockWebRequest(const std::string& notification) |
+ : m_responseText(notification) |
+ { |
+ } |
+ ServerResponse GET(const std::string& url, |
+ const HeaderList& requestHeaders) const override |
+ { |
+ if (url.find("/notification.json") == std::string::npos) |
+ { |
+ return ServerResponse(); |
+ } |
+ ServerResponse serverResponse; |
+ serverResponse.status = NS_OK; |
+ serverResponse.responseStatus = 200; |
+ serverResponse.responseText = m_responseText; |
+ return serverResponse; |
+ } |
+ }; |
+ |
+#ifdef NotificationMockWebRequestTest_ENABLED |
+ class NotificationMockWebRequestTest : public BaseJsTest |
+ { |
+ protected: |
+ FilterEnginePtr filterEngine; |
+ void SetUp() override |
+ { |
+ BaseJsTest::SetUp(); |
+ jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); |
+ const char* responseJsonText = "{" |
+ "\"notifications\": [{" |
+ "\"id\": \"some id\"," |
+ "\"type\": \"information\"," |
+ "\"message\": {" |
+ "\"en-US\": \"Critical message\"," |
+ "\"de\": \"Achtung\"" |
+ "}," |
+ "\"title\": \"Title\"" |
+ "}]" |
+ "}"; |
+ jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJsonText)); |
+ jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
+ filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
+ } |
+ }; |
+#endif |
+} |
+ |
+TEST_F(NotificationTest, NotificationCtr) |
+{ |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_CRITICAL, "testId"); |
+ ASSERT_NE(nullptr, notification); |
+ EXPECT_EQ("testId", notification->GetId()); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType()); |
+} |
+ |
+TEST_F(NotificationTest, MultilingualProperties) |
+{ |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_CRITICAL, "testId"); |
+ ASSERT_NE(nullptr, notification); |
+ notification->SetTitle("French", "fr-FR"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de"); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr"); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr-FR"); |
+ EXPECT_EQ("French", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ notification->SetTitle("German", "de"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de"); |
+ EXPECT_EQ("German", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de-DE"); |
+ EXPECT_EQ("German", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr-FR"); |
+ EXPECT_EQ("French", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ notification->SetTitle("English", "en"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("en"); |
+ EXPECT_EQ("English", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("en-US"); |
+ EXPECT_EQ("English", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de"); |
+ EXPECT_EQ("German", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr-FR"); |
+ EXPECT_EQ("French", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ notification->SetTitle("Default", "en-US"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("Default", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("en"); |
+ EXPECT_EQ("English", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("en-US"); |
+ EXPECT_EQ("Default", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("xx"); |
+ EXPECT_EQ("Default", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ |
+ notification->SetTitle("testTitleNoLocale"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("testTitleNoLocale", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de-DE"); |
+ EXPECT_EQ("testTitleNoLocale", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ |
+ notification->SetTitle("French", "fr-FR"); |
+ { |
+ auto texts = notification->GetTexts(); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("de"); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr"); |
+ EXPECT_EQ("", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+ { |
+ auto texts = notification->GetTexts("fr-FR"); |
+ EXPECT_EQ("French", texts.title); |
+ EXPECT_EQ("", texts.message); |
+ } |
+} |
+ |
+TEST_F(NotificationTest, UrlFilters) |
+{ |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_CRITICAL, "testId"); |
+ ASSERT_NE(nullptr, notification); |
+ EXPECT_EQ(0, notification->GetUrlFilters().size()); |
+ notification->AddUrlFilter("filterA"); |
+ { |
+ auto urlFilters = notification->GetUrlFilters(); |
+ ASSERT_EQ(1, urlFilters.size()); |
+ EXPECT_EQ("filterA", urlFilters[0]); |
+ } |
+ notification->AddUrlFilter("filterB"); |
+ { |
+ auto urlFilters = notification->GetUrlFilters(); |
+ ASSERT_EQ(2, urlFilters.size()); |
+ EXPECT_EQ("filterA", urlFilters[0]); |
+ EXPECT_EQ("filterB", urlFilters[1]); |
+ } |
+ notification->AddUrlFilter("filterC"); |
+ { |
+ auto urlFilters = notification->GetUrlFilters(); |
+ ASSERT_EQ(3, urlFilters.size()); |
+ EXPECT_EQ("filterA", urlFilters[0]); |
+ EXPECT_EQ("filterB", urlFilters[1]); |
+ EXPECT_EQ("filterC", urlFilters[2]); |
+ } |
+} |
+ |
+TEST_F(NotificationTest, NoNotifications) |
+{ |
+ auto notification = filterEngine->GetNextNotificationToShow(); |
+ EXPECT_EQ(nullptr, notification); |
+} |
+ |
+#ifdef NotificationMockWebRequestTest_ENABLED |
+TEST_F(NotificationMockWebRequestTest, SingleNotification) |
+{ |
+ std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack |
+ auto notification = filterEngine->GetNextNotificationToShow(); |
+ // try another one immediately to avoid queuing of the next notification by |
+ // the timer. |
+ EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetType()); |
+ EXPECT_EQ("some id", notification->GetId()); |
+ auto defaultTexts = notification->GetTexts("xx"); |
+ EXPECT_EQ("Title", defaultTexts.title); |
+ EXPECT_EQ("Critical message", defaultTexts.message); |
+ auto deTexts = notification->GetTexts("de"); |
+ EXPECT_EQ("Title", deTexts.title); |
+ EXPECT_EQ("Achtung", deTexts.message); |
+} |
+#endif |
+ |
+TEST_F(NotificationTest, AddNotification) |
+{ |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "id"); |
+ ASSERT_NE(nullptr, notification); |
+ filterEngine->AddNotification(notification); |
+ } |
+ auto notification = filterEngine->GetNextNotificationToShow(); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ("id", notification->GetId()); |
+} |
+ |
+TEST_F(NotificationTest, FilterByUrl1) |
+{ |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "no-filter"); |
+ ASSERT_NE(nullptr, notification); |
+ filterEngine->AddNotification(notification); |
+ } |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "com"); |
+ ASSERT_NE(nullptr, notification); |
+ notification->AddUrlFilter("http://www.com"); |
+ filterEngine->AddNotification(notification); |
+ } |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "de"); |
+ ASSERT_NE(nullptr, notification); |
+ notification->AddUrlFilter("http://www.de"); |
+ filterEngine->AddNotification(notification); |
+ } |
+ auto notification = filterEngine->GetNextNotificationToShow(); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ("no-filter", notification->GetId()); |
+ notification = filterEngine->GetNextNotificationToShow(); |
+ EXPECT_EQ(nullptr, notification.get()); |
+ notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ("de", notification->GetId()); |
+} |
+TEST_F(NotificationTest, FilterByUrl2) |
+{ |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "no-filter"); |
+ ASSERT_NE(nullptr, notification); |
+ filterEngine->AddNotification(notification); |
+ } |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "com"); |
+ ASSERT_NE(nullptr, notification); |
+ notification->AddUrlFilter("http://www.com"); |
+ filterEngine->AddNotification(notification); |
+ } |
+ { |
+ auto notification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "de"); |
+ ASSERT_NE(nullptr, notification); |
+ notification->AddUrlFilter("http://www.de"); |
+ filterEngine->AddNotification(notification); |
+ } |
+ auto notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ("de", notification->GetId()); |
+ notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
+ EXPECT_EQ(nullptr, notification.get()); |
+ notification = filterEngine->GetNextNotificationToShow(); |
+ ASSERT_NE(nullptr, notification.get()); |
+ EXPECT_EQ("no-filter", notification->GetId()); |
+} |
+ |
+TEST_F(NotificationTest, AddRemoveNotification) |
+{ |
+ auto newInfoNotification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "id"); |
+ ASSERT_NE(nullptr, newInfoNotification); |
+ filterEngine->AddNotification(newInfoNotification); |
+ filterEngine->RemoveNotification(newInfoNotification); |
+ EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
+} |
+ |
+TEST_F(NotificationTest, MarkAsShown) |
+{ |
+ auto newInfoNotification = filterEngine->CreateNotification(NotificationType::NOTIFICATION_TYPE_INFORMATION, "id"); |
+ ASSERT_NE(nullptr, newInfoNotification); |
+ filterEngine->AddNotification(newInfoNotification); |
+ filterEngine->MarkNotificationAsShown("id"); |
+ EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
+} |