| Index: test/Notification.cpp |
| diff --git a/test/Notification.cpp b/test/Notification.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1bc4ae683629667946c11698b90db231f005b433 |
| --- /dev/null |
| +++ b/test/Notification.cpp |
| @@ -0,0 +1,172 @@ |
| +/* |
| + * 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); |
| + } |
| + |
| + void AddNotificaiton(const std::string& notification) |
| + { |
| + jsEngine->Evaluate("(function()" |
| + "{" |
| + "require('notification').Notification.addNotification(" + notification + ");" |
| + "})();"); |
| + } |
| + }; |
| + |
| + 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\": \"message\"" |
| + "}," |
| + "\"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, 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("Title", notification->GetTitle()); |
| + EXPECT_EQ("message", notification->GetMessageString()); |
| +} |
| +#endif |
| + |
| +TEST_F(NotificationTest, AddNotification) |
| +{ |
| + AddNotificaiton("{" |
| + "type: 'critical'," |
| + "title: 'testTitle'," |
| + "message: 'testMessage'," |
| + "}"); |
| + auto notification = filterEngine->GetNextNotificationToShow(); |
| + ASSERT_NE(nullptr, notification.get()); |
| + EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType()); |
| + EXPECT_EQ("testTitle", notification->GetTitle()); |
| + EXPECT_EQ("testMessage", notification->GetMessageString()); |
| +} |
| + |
| +TEST_F(NotificationTest, FilterByUrl1) |
| +{ |
| + AddNotificaiton("{ id: 'no-filter', type: 'critical' }"); |
| + AddNotificaiton("{ id: 'www.com', type: 'information'," |
| + "urlFilters:['http://www.com']" |
| + "}"); |
| + AddNotificaiton("{ id: 'www.de', type: 'question'," |
| + "urlFilters:['http://www.de']" |
| + "}"); |
| + |
| + auto notification = filterEngine->GetNextNotificationToShow(); |
| + ASSERT_NE(nullptr, notification.get()); |
| + EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType()); |
| + |
| + notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| + ASSERT_NE(nullptr, notification.get()); |
| + EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType()); |
| + |
| + notification = filterEngine->GetNextNotificationToShow("http://www.com"); |
| + ASSERT_NE(nullptr, notification.get()); |
| + EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetType()); |
| +} |
| + |
| +TEST_F(NotificationTest, MarkAsShown) |
| +{ |
| + AddNotificaiton("{ id: 'id', type: 'question' }"); |
| + auto notification = filterEngine->GetNextNotificationToShow(); |
| + EXPECT_NE(nullptr, notification); |
| + notification = filterEngine->GetNextNotificationToShow(); |
| + ASSERT_NE(nullptr, notification); |
| + notification->MarkAsShown(); |
| + EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| +} |