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 //#define NotificationMockWebRequestTest_ENABLED |
| 25 |
| 26 namespace |
| 27 { |
| 28 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; |
| 29 |
| 30 class NotificationTest : public BaseJsTest |
| 31 { |
| 32 protected: |
| 33 FilterEnginePtr filterEngine; |
| 34 void SetUp() override |
| 35 { |
| 36 BaseJsTest::SetUp(); |
| 37 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); |
| 38 jsEngine->SetWebRequest(WebRequestPtr(new LazyWebRequest())); |
| 39 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 40 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
| 41 } |
| 42 |
| 43 void AddNotification(const std::string& notification) |
| 44 { |
| 45 jsEngine->Evaluate("(function()" |
| 46 "{" |
| 47 "require('notification').Notification.addNotification(" + notification +
");" |
| 48 "})();"); |
| 49 } |
| 50 }; |
| 51 |
| 52 class MockWebRequest : public WebRequest |
| 53 { |
| 54 public: |
| 55 std::string responseText; |
| 56 explicit MockWebRequest(const std::string& notification) |
| 57 : responseText(notification) |
| 58 { |
| 59 } |
| 60 ServerResponse GET(const std::string& url, |
| 61 const HeaderList& requestHeaders) const |
| 62 { |
| 63 if (url.find("/notification.json") == std::string::npos) |
| 64 { |
| 65 return ServerResponse(); |
| 66 } |
| 67 ServerResponse serverResponse; |
| 68 serverResponse.status = NS_OK; |
| 69 serverResponse.responseStatus = 200; |
| 70 serverResponse.responseText = responseText; |
| 71 return serverResponse; |
| 72 } |
| 73 }; |
| 74 |
| 75 #ifdef NotificationMockWebRequestTest_ENABLED |
| 76 class NotificationMockWebRequestTest : public BaseJsTest |
| 77 { |
| 78 protected: |
| 79 FilterEnginePtr filterEngine; |
| 80 void SetUp() override |
| 81 { |
| 82 BaseJsTest::SetUp(); |
| 83 jsEngine->SetFileSystem(std::tr1::make_shared<LazyFileSystem>()); |
| 84 const char* responseJsonText = "{" |
| 85 "\"notifications\": [{" |
| 86 "\"id\": \"some id\"," |
| 87 "\"type\": \"information\"," |
| 88 "\"message\": {" |
| 89 "\"en-US\": \"message\"" |
| 90 "}," |
| 91 "\"title\": \"Title\"" |
| 92 "}]" |
| 93 "}"; |
| 94 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson
Text)); |
| 95 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 96 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); |
| 97 } |
| 98 }; |
| 99 #endif |
| 100 } |
| 101 |
| 102 TEST_F(NotificationTest, NoNotifications) |
| 103 { |
| 104 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
| 105 EXPECT_EQ(NULL, notification.get()); |
| 106 } |
| 107 |
| 108 #ifdef NotificationMockWebRequestTest_ENABLED |
| 109 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 110 { |
| 111 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
| 112 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
| 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 } |
| 121 #endif |
| 122 |
| 123 TEST_F(NotificationTest, AddNotification) |
| 124 { |
| 125 AddNotification("{" |
| 126 "type: 'critical'," |
| 127 "title: 'testTitle'," |
| 128 "message: 'testMessage'," |
| 129 "}"); |
| 130 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
| 131 ASSERT_TRUE(notification); |
| 132 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 133 EXPECT_EQ("testTitle", notification->GetTitle()); |
| 134 EXPECT_EQ("testMessage", notification->GetMessageString()); |
| 135 } |
| 136 |
| 137 TEST_F(NotificationTest, FilterByUrl) |
| 138 { |
| 139 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
| 140 AddNotification("{ id: 'www.com', type: 'information'," |
| 141 "urlFilters:['http://www.com']" |
| 142 "}"); |
| 143 AddNotification("{ id: 'www.de', type: 'question'," |
| 144 "urlFilters:['http://www.de']" |
| 145 "}"); |
| 146 |
| 147 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
| 148 ASSERT_TRUE(notification); |
| 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType(
)); |
| 150 |
| 151 notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
| 152 ASSERT_TRUE(notification); |
| 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType(
)); |
| 154 |
| 155 notification = filterEngine->GetNextNotificationToShow("http://www.com"); |
| 156 ASSERT_TRUE(notification); |
| 157 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy
pe()); |
| 158 } |
| 159 |
| 160 TEST_F(NotificationTest, MarkAsShown) |
| 161 { |
| 162 AddNotification("{ id: 'id', type: 'question' }"); |
| 163 NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
| 164 EXPECT_TRUE(notification); |
| 165 notification = filterEngine->GetNextNotificationToShow(); |
| 166 ASSERT_TRUE(notification); |
| 167 notification->MarkAsShown(); |
| 168 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); |
| 169 } |
OLD | NEW |