| 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-2017 eyeo GmbH | 3  * Copyright (C) 2006-2017 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 30 matching lines...) Expand all  Loading... | 
| 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 | 50 | 
| 51     NotificationPtr PeekNotification(const std::string& url = std::string()) | 51     std::unique_ptr<Notification> PeekNotification(const std::string& url = std:
     :string()) | 
| 52     { | 52     { | 
| 53       NotificationPtr retValue; | 53       std::unique_ptr<Notification> retValue; | 
| 54       filterEngine->SetShowNotificationCallback(std::bind( | 54       filterEngine->SetShowNotificationCallback( | 
| 55         &NotificationTest::NotificationAvailableCallback, | 55         [&retValue](Notification& notification) { | 
| 56         std::placeholders::_1, std::ref(retValue))); | 56           retValue.reset(new Notification(std::move(notification))); | 
|  | 57         }); | 
| 57       filterEngine->ShowNextNotification(url); | 58       filterEngine->ShowNextNotification(url); | 
| 58       filterEngine->RemoveShowNotificationCallback(); | 59       filterEngine->RemoveShowNotificationCallback(); | 
| 59       return retValue; | 60       return retValue; | 
| 60     } | 61     } | 
| 61 |  | 
| 62     static void NotificationAvailableCallback(const NotificationPtr& src, Notifi
     cationPtr& dst) |  | 
| 63     { |  | 
| 64       EXPECT_TRUE(src); |  | 
| 65       dst = src; |  | 
| 66     } |  | 
| 67   }; | 62   }; | 
| 68 | 63 | 
| 69   class MockWebRequest : public WebRequest | 64   class MockWebRequest : public WebRequest | 
| 70   { | 65   { | 
| 71   public: | 66   public: | 
| 72     std::string responseText; | 67     std::string responseText; | 
| 73     explicit MockWebRequest(const std::string& notification) | 68     explicit MockWebRequest(const std::string& notification) | 
| 74       : responseText(notification) | 69       : responseText(notification) | 
| 75     { | 70     { | 
| 76     } | 71     } | 
| (...skipping 30 matching lines...) Expand all  Loading... | 
| 107           "\"type\": \"information\"," | 102           "\"type\": \"information\"," | 
| 108           "\"message\": {" | 103           "\"message\": {" | 
| 109              "\"en-US\": \"message\"" | 104              "\"en-US\": \"message\"" | 
| 110           "}," | 105           "}," | 
| 111           "\"title\": \"Title\"" | 106           "\"title\": \"Title\"" | 
| 112         "}]" | 107         "}]" | 
| 113         "}"; | 108         "}"; | 
| 114       jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( | 109       jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( | 
| 115         new MockWebRequest(responseJsonText))); | 110         new MockWebRequest(responseJsonText))); | 
| 116       jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 111       jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 
| 117       filterEngine.reset(new FilterEngine(jsEngine)); | 112       filterEngine = FilterEngine::Create(jsEngine); | 
| 118       filterEngine->SetShowNotificationCallback( | 113       filterEngine->SetShowNotificationCallback( | 
| 119         std::bind(&NotificationMockWebRequestTest::OnNotification, | 114         [this](Notification& notification) { | 
| 120         this, std::placeholders::_1)); | 115           isNotificationCallbackCalled = true; | 
| 121     } | 116           EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notificatio
     n.GetType()); | 
| 122 | 117           EXPECT_EQ("Title", notification.GetTexts().title); | 
| 123     void OnNotification(const NotificationPtr& notification) | 118           EXPECT_EQ("message", notification.GetTexts().message); | 
| 124     { | 119           notification.MarkAsShown(); | 
| 125       isNotificationCallbackCalled = true; | 120         }); | 
| 126       ASSERT_TRUE(notification); |  | 
| 127       EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->G
     etType()); |  | 
| 128       EXPECT_EQ("Title", notification->GetTexts().title); |  | 
| 129       EXPECT_EQ("message", notification->GetTexts().message); |  | 
| 130       notification->MarkAsShown(); |  | 
| 131     } | 121     } | 
| 132   }; | 122   }; | 
| 133 #endif | 123 #endif | 
| 134 } | 124 } | 
| 135 | 125 | 
| 136 TEST_F(NotificationTest, NoNotifications) | 126 TEST_F(NotificationTest, NoNotifications) | 
| 137 { | 127 { | 
| 138   EXPECT_FALSE(PeekNotification()); | 128   EXPECT_FALSE(PeekNotification()); | 
| 139 } | 129 } | 
| 140 | 130 | 
| 141 #ifdef NotificationMockWebRequestTest_ENABLED | 131 #ifdef NotificationMockWebRequestTest_ENABLED | 
| 142 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 132 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 
| 143 { | 133 { | 
| 144   AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 134   AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 
| 145   EXPECT_TRUE(isNotificationCallbackCalled); | 135   EXPECT_TRUE(isNotificationCallbackCalled); | 
| 146 } | 136 } | 
| 147 #endif | 137 #endif | 
| 148 | 138 | 
| 149 TEST_F(NotificationTest, AddNotification) | 139 TEST_F(NotificationTest, AddNotification) | 
| 150 { | 140 { | 
| 151   AddNotification("{" | 141   AddNotification("{" | 
| 152       "type: 'critical'," | 142       "type: 'critical'," | 
| 153       "title: 'testTitle'," | 143       "title: 'testTitle'," | 
| 154       "message: 'testMessage'," | 144       "message: 'testMessage'," | 
| 155     "}"); | 145     "}"); | 
| 156   NotificationPtr notification = PeekNotification(); | 146   auto notification = PeekNotification(); | 
| 157   ASSERT_TRUE(notification); | 147   ASSERT_TRUE(notification); | 
| 158   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 148   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 
| 159   EXPECT_EQ("testTitle", notification->GetTexts().title); | 149   EXPECT_EQ("testTitle", notification->GetTexts().title); | 
| 160   EXPECT_EQ("testMessage", notification->GetTexts().message); | 150   EXPECT_EQ("testMessage", notification->GetTexts().message); | 
| 161 } | 151 } | 
| 162 | 152 | 
| 163 TEST_F(NotificationTest, FilterByUrl) | 153 TEST_F(NotificationTest, FilterByUrl) | 
| 164 { | 154 { | 
| 165   AddNotification("{ id: 'no-filter', type: 'critical' }"); | 155   AddNotification("{ id: 'no-filter', type: 'critical' }"); | 
| 166   AddNotification("{ id: 'www.com', type: 'information'," | 156   AddNotification("{ id: 'www.com', type: 'information'," | 
| 167     "urlFilters:['||www.com$document']" | 157     "urlFilters:['||www.com$document']" | 
| 168   "}"); | 158   "}"); | 
| 169   AddNotification("{ id: 'www.de', type: 'question'," | 159   AddNotification("{ id: 'www.de', type: 'question'," | 
| 170     "urlFilters:['||www.de$document']" | 160     "urlFilters:['||www.de$document']" | 
| 171   "}"); | 161   "}"); | 
| 172 | 162 | 
| 173   NotificationPtr notification = PeekNotification(); | 163   auto notification = PeekNotification(); | 
| 174   ASSERT_TRUE(notification); | 164   ASSERT_TRUE(notification); | 
| 175   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 165   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
     )); | 
| 176 | 166 | 
| 177   notification = PeekNotification("http://www.de"); | 167   notification = PeekNotification("http://www.de"); | 
| 178   ASSERT_TRUE(notification); | 168   ASSERT_TRUE(notification); | 
| 179   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
     )); | 169   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
     )); | 
| 180 | 170 | 
| 181   notification = PeekNotification("http://www.com"); | 171   notification = PeekNotification("http://www.com"); | 
| 182   ASSERT_TRUE(notification); | 172   ASSERT_TRUE(notification); | 
| 183   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
     pe()); | 173   EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
     pe()); | 
| 184 } | 174 } | 
| 185 | 175 | 
| 186 TEST_F(NotificationTest, MarkAsShown) | 176 TEST_F(NotificationTest, MarkAsShown) | 
| 187 { | 177 { | 
| 188   AddNotification("{ id: 'id', type: 'question' }"); | 178   AddNotification("{ id: 'id', type: 'question' }"); | 
| 189   EXPECT_TRUE(PeekNotification()); | 179   EXPECT_TRUE(PeekNotification()); | 
| 190   NotificationPtr notification = PeekNotification(); | 180   auto notification = PeekNotification(); | 
| 191   ASSERT_TRUE(notification); | 181   ASSERT_TRUE(notification); | 
| 192   notification->MarkAsShown(); | 182   notification->MarkAsShown(); | 
| 193   EXPECT_FALSE(PeekNotification()); | 183   EXPECT_FALSE(PeekNotification()); | 
| 194 } | 184 } | 
| 195 | 185 | 
| 196 TEST_F(NotificationTest, NoLinks) | 186 TEST_F(NotificationTest, NoLinks) | 
| 197 { | 187 { | 
| 198   AddNotification("{ id: 'id'}"); | 188   AddNotification("{ id: 'id'}"); | 
| 199   NotificationPtr notification = PeekNotification(); | 189   auto notification = PeekNotification(); | 
| 200   ASSERT_TRUE(notification); | 190   ASSERT_TRUE(notification); | 
| 201   EXPECT_EQ(0u, notification->GetLinks().size()); | 191   EXPECT_EQ(0u, notification->GetLinks().size()); | 
| 202 } | 192 } | 
| 203 | 193 | 
| 204 TEST_F(NotificationTest, Links) | 194 TEST_F(NotificationTest, Links) | 
| 205 { | 195 { | 
| 206   AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 196   AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 
| 207   NotificationPtr notification = PeekNotification(); | 197   auto notification = PeekNotification(); | 
| 208   ASSERT_TRUE(notification); | 198   ASSERT_TRUE(notification); | 
| 209   std::vector<std::string> notificationLinks = notification->GetLinks(); | 199   std::vector<std::string> notificationLinks = notification->GetLinks(); | 
| 210   ASSERT_EQ(2u, notificationLinks.size()); | 200   ASSERT_EQ(2u, notificationLinks.size()); | 
| 211   EXPECT_EQ("link1", notificationLinks[0]); | 201   EXPECT_EQ("link1", notificationLinks[0]); | 
| 212   EXPECT_EQ("link2", notificationLinks[1]); | 202   EXPECT_EQ("link2", notificationLinks[1]); | 
| 213 } | 203 } | 
| OLD | NEW | 
|---|