| 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     void AddNotificaiton(const std::string& notification) | 
|  | 47     { | 
|  | 48       jsEngine->Evaluate("(function()" | 
|  | 49       "{" | 
|  | 50         "require('notification').Notification.addNotification(" + notification +
      ");" | 
|  | 51       "})();"); | 
|  | 52     } | 
|  | 53   }; | 
|  | 54 | 
|  | 55   class MockWebRequest : public WebRequest | 
|  | 56   { | 
|  | 57   public: | 
|  | 58     std::string m_responseText; | 
|  | 59     explicit MockWebRequest(const std::string& notification) | 
|  | 60       : m_responseText(notification) | 
|  | 61     { | 
|  | 62     } | 
|  | 63     ServerResponse GET(const std::string& url, | 
|  | 64       const HeaderList& requestHeaders) const override | 
|  | 65     { | 
|  | 66       if (url.find("/notification.json") == std::string::npos) | 
|  | 67       { | 
|  | 68         return ServerResponse(); | 
|  | 69       } | 
|  | 70       ServerResponse serverResponse; | 
|  | 71       serverResponse.status = NS_OK; | 
|  | 72       serverResponse.responseStatus = 200; | 
|  | 73       serverResponse.responseText = m_responseText; | 
|  | 74       return serverResponse; | 
|  | 75     } | 
|  | 76   }; | 
|  | 77 | 
|  | 78 #ifdef NotificationMockWebRequestTest_ENABLED | 
|  | 79   class NotificationMockWebRequestTest : public BaseJsTest | 
|  | 80   { | 
|  | 81   protected: | 
|  | 82     FilterEnginePtr filterEngine; | 
|  | 83     void SetUp() override | 
|  | 84     { | 
|  | 85       BaseJsTest::SetUp(); | 
|  | 86       jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); | 
|  | 87       const char* responseJsonText = "{" | 
|  | 88         "\"notifications\": [{" | 
|  | 89           "\"id\": \"some id\"," | 
|  | 90           "\"type\": \"information\"," | 
|  | 91           "\"message\": {" | 
|  | 92              "\"en-US\": \"message\"" | 
|  | 93           "}," | 
|  | 94           "\"title\": \"Title\"" | 
|  | 95         "}]" | 
|  | 96         "}"; | 
|  | 97       jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson
     Text)); | 
|  | 98       jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 
|  | 99       filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); | 
|  | 100     } | 
|  | 101   }; | 
|  | 102 #endif | 
|  | 103 } | 
|  | 104 | 
|  | 105 TEST_F(NotificationTest, NoNotifications) | 
|  | 106 { | 
|  | 107   auto notification = filterEngine->GetNextNotificationToShow(); | 
|  | 108   EXPECT_EQ(nullptr, notification); | 
|  | 109 } | 
|  | 110 | 
|  | 111 #ifdef NotificationMockWebRequestTest_ENABLED | 
|  | 112 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 
|  | 113 { | 
|  | 114   std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack | 
|  | 115   auto notification = filterEngine->GetNextNotificationToShow(); | 
|  | 116   // try another one immediately to avoid queuing of the next notification by | 
|  | 117   // the timer. | 
|  | 118   EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); | 
|  | 119   ASSERT_NE(nullptr, notification.get()); | 
|  | 120   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
     pe()); | 
|  | 121   EXPECT_EQ("Title", notification->GetTitle()); | 
|  | 122   EXPECT_EQ("message", notification->GetMessageString()); | 
|  | 123 } | 
|  | 124 #endif | 
|  | 125 | 
|  | 126 TEST_F(NotificationTest, AddNotification) | 
|  | 127 { | 
|  | 128   AddNotificaiton("{" | 
|  | 129       "type: 'critical'," | 
|  | 130       "title: 'testTitle'," | 
|  | 131       "message: 'testMessage'," | 
|  | 132     "}"); | 
|  | 133   auto notification = filterEngine->GetNextNotificationToShow(); | 
|  | 134   ASSERT_NE(nullptr, notification.get()); | 
|  | 135   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 
|  | 136   EXPECT_EQ("testTitle", notification->GetTitle()); | 
|  | 137   EXPECT_EQ("testMessage", notification->GetMessageString()); | 
|  | 138 } | 
|  | 139 | 
|  | 140 TEST_F(NotificationTest, FilterByUrl1) | 
|  | 141 { | 
|  | 142   AddNotificaiton("{ id: 'no-filter', type: 'critical' }"); | 
|  | 143   AddNotificaiton("{ id: 'www.com', type: 'information'," | 
|  | 144     "urlFilters:['http://www.com']" | 
|  | 145   "}"); | 
|  | 146   AddNotificaiton("{ id: 'www.de', type: 'question'," | 
|  | 147     "urlFilters:['http://www.de']" | 
|  | 148   "}"); | 
|  | 149 | 
|  | 150   auto notification = filterEngine->GetNextNotificationToShow(); | 
|  | 151   ASSERT_NE(nullptr, notification.get()); | 
|  | 152   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 
|  | 153 | 
|  | 154   notification = filterEngine->GetNextNotificationToShow("http://www.de"); | 
|  | 155   ASSERT_NE(nullptr, notification.get()); | 
|  | 156   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
     )); | 
|  | 157 | 
|  | 158   notification = filterEngine->GetNextNotificationToShow("http://www.com"); | 
|  | 159   ASSERT_NE(nullptr, notification.get()); | 
|  | 160   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
     pe()); | 
|  | 161 } | 
|  | 162 | 
|  | 163 TEST_F(NotificationTest, MarkAsShown) | 
|  | 164 { | 
|  | 165   AddNotificaiton("{ id: 'id', type: 'question' }"); | 
|  | 166   auto notification = filterEngine->GetNextNotificationToShow(); | 
|  | 167   EXPECT_NE(nullptr, notification); | 
|  | 168   notification = filterEngine->GetNextNotificationToShow(); | 
|  | 169   ASSERT_NE(nullptr, notification); | 
|  | 170   notification->MarkAsShown(); | 
|  | 171   EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); | 
|  | 172 } | 
| OLD | NEW | 
|---|