| LEFT | RIGHT |
| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include "BaseJsTest.h" | 18 #include "BaseJsTest.h" |
| 19 | 19 |
| 20 using namespace AdblockPlus; | 20 using namespace AdblockPlus; |
| 21 | 21 |
| 22 // This define enables NotificationMockWebRequestTest but to run it | 22 // This define enables NotificationMockWebRequestTest but to run it |
| 23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js. | 23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js. |
| 24 //#define NotificationMockWebRequestTest_ENABLED | 24 //#define NotificationMockWebRequestTest_ENABLED |
| 25 | 25 |
| 26 namespace | 26 namespace |
| 27 { | 27 { |
| 28 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; | 28 typedef std::shared_ptr<FilterEngine> FilterEnginePtr; |
| 29 | 29 |
| 30 class NotificationTest : public BaseJsTest | 30 class NotificationTest : public BaseJsTest |
| 31 { | 31 { |
| 32 protected: | 32 protected: |
| 33 FilterEnginePtr filterEngine; | 33 FilterEnginePtr filterEngine; |
| 34 void SetUp() override | 34 void SetUp() |
| 35 { | 35 { |
| 36 BaseJsTest::SetUp(); | 36 BaseJsTest::SetUp(); |
| 37 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); | 37 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); |
| 38 jsEngine->SetWebRequest(WebRequestPtr(new LazyWebRequest())); | 38 jsEngine->SetWebRequest(WebRequestPtr(new LazyWebRequest())); |
| 39 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 39 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 40 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); | 40 filterEngine.reset(new FilterEngine(jsEngine)); |
| 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 } |
| 50 |
| 51 NotificationPtr PeekNotification(const std::string& url = std::string()) |
| 52 { |
| 53 NotificationPtr retValue; |
| 54 filterEngine->SetShowNotificationCallback(std::bind( |
| 55 &NotificationTest::NotificationAvailableCallback, |
| 56 std::placeholders::_1, std::ref(retValue))); |
| 57 filterEngine->ShowNextNotification(url); |
| 58 filterEngine->RemoveShowNotificationCallback(); |
| 59 return retValue; |
| 60 } |
| 61 |
| 62 static void NotificationAvailableCallback(const NotificationPtr& src, Notifi
cationPtr& dst) |
| 63 { |
| 64 EXPECT_TRUE(src); |
| 65 dst = src; |
| 49 } | 66 } |
| 50 }; | 67 }; |
| 51 | 68 |
| 52 class MockWebRequest : public WebRequest | 69 class MockWebRequest : public WebRequest |
| 53 { | 70 { |
| 54 public: | 71 public: |
| 55 std::string responseText; | 72 std::string responseText; |
| 56 explicit MockWebRequest(const std::string& notification) | 73 explicit MockWebRequest(const std::string& notification) |
| 57 : responseText(notification) | 74 : responseText(notification) |
| 58 { | 75 { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 70 serverResponse.responseText = responseText; | 87 serverResponse.responseText = responseText; |
| 71 return serverResponse; | 88 return serverResponse; |
| 72 } | 89 } |
| 73 }; | 90 }; |
| 74 | 91 |
| 75 #ifdef NotificationMockWebRequestTest_ENABLED | 92 #ifdef NotificationMockWebRequestTest_ENABLED |
| 76 class NotificationMockWebRequestTest : public BaseJsTest | 93 class NotificationMockWebRequestTest : public BaseJsTest |
| 77 { | 94 { |
| 78 protected: | 95 protected: |
| 79 FilterEnginePtr filterEngine; | 96 FilterEnginePtr filterEngine; |
| 80 void SetUp() override | 97 bool isNotificationCallbackCalled; |
| 98 void SetUp() |
| 81 { | 99 { |
| 82 BaseJsTest::SetUp(); | 100 BaseJsTest::SetUp(); |
| 83 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); | 101 isNotificationCallbackCalled = false; |
| 102 jsEngine->SetFileSystem( |
| 103 std::shared_ptr<LazyFileSystem>(new LazyFileSystem())); |
| 84 const char* responseJsonText = "{" | 104 const char* responseJsonText = "{" |
| 85 "\"notifications\": [{" | 105 "\"notifications\": [{" |
| 86 "\"id\": \"some id\"," | 106 "\"id\": \"some id\"," |
| 87 "\"type\": \"information\"," | 107 "\"type\": \"information\"," |
| 88 "\"message\": {" | 108 "\"message\": {" |
| 89 "\"en-US\": \"message\"" | 109 "\"en-US\": \"message\"" |
| 90 "}," | 110 "}," |
| 91 "\"title\": \"Title\"" | 111 "\"title\": \"Title\"" |
| 92 "}]" | 112 "}]" |
| 93 "}"; | 113 "}"; |
| 94 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson
Text)); | 114 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( |
| 115 new MockWebRequest(responseJsonText))); |
| 95 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 96 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); | 117 filterEngine.reset(new FilterEngine(jsEngine)); |
| 118 filterEngine->SetShowNotificationCallback( |
| 119 std::bind(&NotificationMockWebRequestTest::OnNotification, |
| 120 this, std::placeholders::_1)); |
| 121 } |
| 122 |
| 123 void OnNotification(const NotificationPtr& notification) |
| 124 { |
| 125 isNotificationCallbackCalled = true; |
| 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(); |
| 97 } | 131 } |
| 98 }; | 132 }; |
| 99 #endif | 133 #endif |
| 100 } | 134 } |
| 101 | 135 |
| 102 TEST_F(NotificationTest, NoNotifications) | 136 TEST_F(NotificationTest, NoNotifications) |
| 103 { | 137 { |
| 104 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 138 EXPECT_FALSE(PeekNotification()); |
| 105 EXPECT_EQ(NULL, notification.get()); | |
| 106 } | 139 } |
| 107 | 140 |
| 108 #ifdef NotificationMockWebRequestTest_ENABLED | 141 #ifdef NotificationMockWebRequestTest_ENABLED |
| 109 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 142 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 110 { | 143 { |
| 111 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
| 112 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 145 EXPECT_TRUE(isNotificationCallbackCalled); |
| 113 // try another one immediately to avoid queuing of the next notification by | |
| 114 // the timer. | |
| 115 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); | |
| 116 ASSERT_TRUE(notification); | |
| 117 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); | |
| 118 EXPECT_EQ("Title", notification->GetTitle()); | |
| 119 EXPECT_EQ("message", notification->GetMessageString()); | |
| 120 } | 146 } |
| 121 #endif | 147 #endif |
| 122 | 148 |
| 123 TEST_F(NotificationTest, AddNotification) | 149 TEST_F(NotificationTest, AddNotification) |
| 124 { | 150 { |
| 125 AddNotification("{" | 151 AddNotification("{" |
| 126 "type: 'critical'," | 152 "type: 'critical'," |
| 127 "title: 'testTitle'," | 153 "title: 'testTitle'," |
| 128 "message: 'testMessage'," | 154 "message: 'testMessage'," |
| 129 "}"); | 155 "}"); |
| 130 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 156 NotificationPtr notification = PeekNotification(); |
| 131 ASSERT_TRUE(notification); | 157 ASSERT_TRUE(notification); |
| 132 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 133 EXPECT_EQ("testTitle", notification->GetTexts().title); | 159 EXPECT_EQ("testTitle", notification->GetTexts().title); |
| 134 EXPECT_EQ("testMessage", notification->GetTexts().message); | 160 EXPECT_EQ("testMessage", notification->GetTexts().message); |
| 135 } | 161 } |
| 136 | 162 |
| 137 TEST_F(NotificationTest, FilterByUrl) | 163 TEST_F(NotificationTest, FilterByUrl) |
| 138 { | 164 { |
| 139 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 165 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
| 140 AddNotification("{ id: 'www.com', type: 'information'," | 166 AddNotification("{ id: 'www.com', type: 'information'," |
| 141 "urlFilters:['http://www.com']" | 167 "urlFilters:['||www.com$document']" |
| 142 "}"); | 168 "}"); |
| 143 AddNotification("{ id: 'www.de', type: 'question'," | 169 AddNotification("{ id: 'www.de', type: 'question'," |
| 144 "urlFilters:['http://www.de']" | 170 "urlFilters:['||www.de$document']" |
| 145 "}"); | 171 "}"); |
| 146 | 172 |
| 147 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 173 NotificationPtr notification = PeekNotification(); |
| 148 ASSERT_TRUE(notification); | 174 ASSERT_TRUE(notification); |
| 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); | 175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 150 | 176 |
| 151 notification = filterEngine->GetNextNotificationToShow("http://www.de"); | 177 notification = PeekNotification("http://www.de"); |
| 152 ASSERT_TRUE(notification); | 178 ASSERT_TRUE(notification); |
| 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); | 179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); |
| 154 | 180 |
| 155 notification = filterEngine->GetNextNotificationToShow("http://www.com"); | 181 notification = PeekNotification("http://www.com"); |
| 156 ASSERT_TRUE(notification); | 182 ASSERT_TRUE(notification); |
| 157 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); | 183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); |
| 158 } | 184 } |
| 159 | 185 |
| 160 TEST_F(NotificationTest, MarkAsShown) | 186 TEST_F(NotificationTest, MarkAsShown) |
| 161 { | 187 { |
| 162 AddNotification("{ id: 'id', type: 'question' }"); | 188 AddNotification("{ id: 'id', type: 'question' }"); |
| 163 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); | 189 EXPECT_TRUE(PeekNotification()); |
| 164 EXPECT_TRUE(notification); | 190 NotificationPtr notification = PeekNotification(); |
| 165 notification = filterEngine->GetNextNotificationToShow(); | |
| 166 ASSERT_TRUE(notification); | 191 ASSERT_TRUE(notification); |
| 167 notification->MarkAsShown(); | 192 notification->MarkAsShown(); |
| 168 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); | 193 EXPECT_FALSE(PeekNotification()); |
| 169 } | 194 } |
| 195 |
| 196 TEST_F(NotificationTest, NoLinks) |
| 197 { |
| 198 AddNotification("{ id: 'id'}"); |
| 199 NotificationPtr notification = PeekNotification(); |
| 200 ASSERT_TRUE(notification); |
| 201 EXPECT_EQ(0, notification->GetLinks().size()); |
| 202 } |
| 203 |
| 204 TEST_F(NotificationTest, Links) |
| 205 { |
| 206 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
| 207 NotificationPtr notification = PeekNotification(); |
| 208 ASSERT_TRUE(notification); |
| 209 std::vector<std::string> notificationLinks = notification->GetLinks(); |
| 210 ASSERT_EQ(2, notificationLinks.size()); |
| 211 EXPECT_EQ("link1", notificationLinks[0]); |
| 212 EXPECT_EQ("link2", notificationLinks[1]); |
| 213 } |
| LEFT | RIGHT |