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 |
| 47 class MockWebRequest : public WebRequest |
| 48 { |
| 49 public: |
| 50 std::string m_responseText; |
| 51 explicit MockWebRequest(const std::string& notification) |
| 52 : m_responseText(notification) |
| 53 { |
| 54 } |
| 55 ServerResponse GET(const std::string& url, |
| 56 const HeaderList& requestHeaders) const override |
| 57 { |
| 58 if (url.find("/notification.json") == std::string::npos) |
| 59 { |
| 60 return ServerResponse(); |
| 61 } |
| 62 ServerResponse serverResponse; |
| 63 serverResponse.status = NS_OK; |
| 64 serverResponse.responseStatus = 200; |
| 65 serverResponse.responseText = m_responseText; |
| 66 return serverResponse; |
| 67 } |
| 68 }; |
| 69 |
| 70 #ifdef NotificationMockWebRequestTest_ENABLED |
| 71 class NotificationMockWebRequestTest : public BaseJsTest |
| 72 { |
| 73 protected: |
| 74 FilterEnginePtr filterEngine; |
| 75 void SetUp() override |
| 76 { |
| 77 BaseJsTest::SetUp(); |
| 78 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); |
| 79 const char* responseJsonText = "{" |
| 80 "\"notifications\": [{" |
| 81 "\"id\": \"some id\"," |
| 82 "\"type\": \"information\"," |
| 83 "\"message\": {" |
| 84 "\"en-US\": \"Critical message\"," |
| 85 "\"de\": \"Achtung\"" |
| 86 "}," |
| 87 "\"title\": \"Title\"" |
| 88 "}]" |
| 89 "}"; |
| 90 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson
Text)); |
| 91 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 92 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
| 93 } |
| 94 }; |
| 95 #endif |
| 96 } |
| 97 |
| 98 TEST_F(NotificationTest, NotificationCtr) |
| 99 { |
| 100 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC
ATION_TYPE_CRITICAL, "testId"); |
| 101 ASSERT_NE(nullptr, notification); |
| 102 EXPECT_EQ("testId", notification->GetId()); |
| 103 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 104 } |
| 105 |
| 106 TEST_F(NotificationTest, UrlFilters) |
| 107 { |
| 108 auto notification = filterEngine->CreateNotification(NotificationType::NOTIFIC
ATION_TYPE_CRITICAL, "testId"); |
| 109 ASSERT_NE(nullptr, notification); |
| 110 EXPECT_EQ(0, notification->GetUrlFilters().size()); |
| 111 notification->AddUrlFilter("filterA"); |
| 112 { |
| 113 auto urlFilters = notification->GetUrlFilters(); |
| 114 ASSERT_EQ(1, urlFilters.size()); |
| 115 EXPECT_EQ("filterA", urlFilters[0]); |
| 116 } |
| 117 notification->AddUrlFilter("filterB"); |
| 118 { |
| 119 auto urlFilters = notification->GetUrlFilters(); |
| 120 ASSERT_EQ(2, urlFilters.size()); |
| 121 EXPECT_EQ("filterA", urlFilters[0]); |
| 122 EXPECT_EQ("filterB", urlFilters[1]); |
| 123 } |
| 124 notification->AddUrlFilter("filterC"); |
| 125 { |
| 126 auto urlFilters = notification->GetUrlFilters(); |
| 127 ASSERT_EQ(3, urlFilters.size()); |
| 128 EXPECT_EQ("filterA", urlFilters[0]); |
| 129 EXPECT_EQ("filterB", urlFilters[1]); |
| 130 EXPECT_EQ("filterC", urlFilters[2]); |
| 131 } |
| 132 } |
| 133 |
| 134 TEST_F(NotificationTest, NoNotifications) |
| 135 { |
| 136 auto notification = filterEngine->GetNextNotificationToShow(); |
| 137 EXPECT_EQ(nullptr, notification); |
| 138 } |
| 139 |
| 140 #ifdef NotificationMockWebRequestTest_ENABLED |
| 141 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 142 { |
| 143 std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack |
| 144 auto notification = filterEngine->GetNextNotificationToShow(); |
| 145 // try another one immediately to avoid queuing of the next notification by |
| 146 // the timer. |
| 147 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 148 ASSERT_NE(nullptr, notification.get()); |
| 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); |
| 150 } |
| 151 #endif |
| 152 |
| 153 TEST_F(NotificationTest, AddNotification) |
| 154 { |
| 155 { |
| 156 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "id"); |
| 157 ASSERT_NE(nullptr, notification); |
| 158 filterEngine->AddNotification(notification); |
| 159 } |
| 160 auto notification = filterEngine->GetNextNotificationToShow(); |
| 161 ASSERT_NE(nullptr, notification.get()); |
| 162 EXPECT_EQ("id", notification->GetId()); |
| 163 } |
| 164 |
| 165 TEST_F(NotificationTest, FilterByUrl1) |
| 166 { |
| 167 { |
| 168 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "no-filter"); |
| 169 ASSERT_NE(nullptr, notification); |
| 170 filterEngine->AddNotification(notification); |
| 171 } |
| 172 { |
| 173 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "com"); |
| 174 ASSERT_NE(nullptr, notification); |
| 175 notification->AddUrlFilter("http://www.com"); |
| 176 filterEngine->AddNotification(notification); |
| 177 } |
| 178 { |
| 179 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "de"); |
| 180 ASSERT_NE(nullptr, notification); |
| 181 notification->AddUrlFilter("http://www.de"); |
| 182 filterEngine->AddNotification(notification); |
| 183 } |
| 184 auto notification = filterEngine->GetNextNotificationToShow(); |
| 185 ASSERT_NE(nullptr, notification.get()); |
| 186 EXPECT_EQ("no-filter", notification->GetId()); |
| 187 notification = filterEngine->GetNextNotificationToShow(); |
| 188 EXPECT_EQ(nullptr, notification.get()); |
| 189 notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 190 ASSERT_NE(nullptr, notification.get()); |
| 191 EXPECT_EQ("de", notification->GetId()); |
| 192 } |
| 193 TEST_F(NotificationTest, FilterByUrl2) |
| 194 { |
| 195 { |
| 196 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "no-filter"); |
| 197 ASSERT_NE(nullptr, notification); |
| 198 filterEngine->AddNotification(notification); |
| 199 } |
| 200 { |
| 201 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "com"); |
| 202 ASSERT_NE(nullptr, notification); |
| 203 notification->AddUrlFilter("http://www.com"); |
| 204 filterEngine->AddNotification(notification); |
| 205 } |
| 206 { |
| 207 auto notification = filterEngine->CreateNotification(NotificationType::NOTIF
ICATION_TYPE_INFORMATION, "de"); |
| 208 ASSERT_NE(nullptr, notification); |
| 209 notification->AddUrlFilter("http://www.de"); |
| 210 filterEngine->AddNotification(notification); |
| 211 } |
| 212 auto notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 213 ASSERT_NE(nullptr, notification.get()); |
| 214 EXPECT_EQ("de", notification->GetId()); |
| 215 notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 216 EXPECT_EQ(nullptr, notification.get()); |
| 217 notification = filterEngine->GetNextNotificationToShow(); |
| 218 ASSERT_NE(nullptr, notification.get()); |
| 219 EXPECT_EQ("no-filter", notification->GetId()); |
| 220 } |
| 221 |
| 222 TEST_F(NotificationTest, AddRemoveNotification) |
| 223 { |
| 224 auto newInfoNotification = filterEngine->CreateNotification(NotificationType::
NOTIFICATION_TYPE_INFORMATION, "id"); |
| 225 ASSERT_NE(nullptr, newInfoNotification); |
| 226 filterEngine->AddNotification(newInfoNotification); |
| 227 filterEngine->RemoveNotification(newInfoNotification); |
| 228 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 229 } |
| 230 |
| 231 TEST_F(NotificationTest, MarkAsShown) |
| 232 { |
| 233 auto newInfoNotification = filterEngine->CreateNotification(NotificationType::
NOTIFICATION_TYPE_INFORMATION, "id"); |
| 234 ASSERT_NE(nullptr, newInfoNotification); |
| 235 filterEngine->AddNotification(newInfoNotification); |
| 236 newInfoNotification->MarkAsShown(); |
| 237 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); |
| 238 } |
OLD | NEW |