| Left: | ||
| Right: |
| 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](const Notification& notification) { |
| 56 std::placeholders::_1, std::ref(retValue))); | 56 NotificationAvailableCallback(notification, retValue); |
| 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 |
| 62 static void NotificationAvailableCallback(const NotificationPtr& src, Notifi cationPtr& dst) | 63 static void NotificationAvailableCallback(const Notification& src, std::uniq ue_ptr<Notification>& dst) |
|
sergei
2017/04/21 17:57:38
Could you please also remove this method and move
hub
2017/04/21 18:37:28
Done
| |
| 63 { | 64 { |
| 64 EXPECT_TRUE(src); | 65 EXPECT_FALSE(src.IsUndefined()); |
|
sergei
2017/04/21 17:57:38
Not sure whether we need that check.
hub
2017/04/21 18:37:28
Removed.
| |
| 65 dst = src; | 66 dst.reset(new Notification(src)); |
| 66 } | 67 } |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 class MockWebRequest : public WebRequest | 70 class MockWebRequest : public WebRequest |
| 70 { | 71 { |
| 71 public: | 72 public: |
| 72 std::string responseText; | 73 std::string responseText; |
| 73 explicit MockWebRequest(const std::string& notification) | 74 explicit MockWebRequest(const std::string& notification) |
| 74 : responseText(notification) | 75 : responseText(notification) |
| 75 { | 76 { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 "\"type\": \"information\"," | 108 "\"type\": \"information\"," |
| 108 "\"message\": {" | 109 "\"message\": {" |
| 109 "\"en-US\": \"message\"" | 110 "\"en-US\": \"message\"" |
| 110 "}," | 111 "}," |
| 111 "\"title\": \"Title\"" | 112 "\"title\": \"Title\"" |
| 112 "}]" | 113 "}]" |
| 113 "}"; | 114 "}"; |
| 114 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( | 115 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( |
| 115 new MockWebRequest(responseJsonText))); | 116 new MockWebRequest(responseJsonText))); |
| 116 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 117 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 117 filterEngine.reset(new FilterEngine(jsEngine)); | 118 filterEngine = FilterEngine::Create(jsEngine); |
| 118 filterEngine->SetShowNotificationCallback( | 119 filterEngine->SetShowNotificationCallback([this](Notification& notificatio n) { |
| 119 std::bind(&NotificationMockWebRequestTest::OnNotification, | 120 OnNotification(notification); |
| 120 this, std::placeholders::_1)); | 121 }); |
| 121 } | 122 } |
| 122 | 123 |
| 123 void OnNotification(const NotificationPtr& notification) | 124 void OnNotification(Notification& notification) |
| 124 { | 125 { |
| 125 isNotificationCallbackCalled = true; | 126 isNotificationCallbackCalled = true; |
| 126 ASSERT_TRUE(notification); | 127 ASSERT_FALSE(notification.IsUndefined()); |
| 127 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->G etType()); | 128 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification.Ge tType()); |
| 128 EXPECT_EQ("Title", notification->GetTexts().title); | 129 EXPECT_EQ("Title", notification.GetTexts().title); |
| 129 EXPECT_EQ("message", notification->GetTexts().message); | 130 EXPECT_EQ("message", notification.GetTexts().message); |
| 130 notification->MarkAsShown(); | 131 notification.MarkAsShown(); |
| 131 } | 132 } |
| 132 }; | 133 }; |
| 133 #endif | 134 #endif |
| 134 } | 135 } |
| 135 | 136 |
| 136 TEST_F(NotificationTest, NoNotifications) | 137 TEST_F(NotificationTest, NoNotifications) |
| 137 { | 138 { |
| 138 EXPECT_FALSE(PeekNotification()); | 139 EXPECT_FALSE(PeekNotification()); |
| 139 } | 140 } |
| 140 | 141 |
| 141 #ifdef NotificationMockWebRequestTest_ENABLED | 142 #ifdef NotificationMockWebRequestTest_ENABLED |
| 142 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 143 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 143 { | 144 { |
| 144 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 145 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
| 145 EXPECT_TRUE(isNotificationCallbackCalled); | 146 EXPECT_TRUE(isNotificationCallbackCalled); |
| 146 } | 147 } |
| 147 #endif | 148 #endif |
| 148 | 149 |
| 149 TEST_F(NotificationTest, AddNotification) | 150 TEST_F(NotificationTest, AddNotification) |
| 150 { | 151 { |
| 151 AddNotification("{" | 152 AddNotification("{" |
| 152 "type: 'critical'," | 153 "type: 'critical'," |
| 153 "title: 'testTitle'," | 154 "title: 'testTitle'," |
| 154 "message: 'testMessage'," | 155 "message: 'testMessage'," |
| 155 "}"); | 156 "}"); |
| 156 NotificationPtr notification = PeekNotification(); | 157 auto notification = PeekNotification(); |
| 157 ASSERT_TRUE(notification); | 158 EXPECT_TRUE(notification); |
|
sergei
2017/04/21 17:57:38
here it should stay ASSERT because if the pointer
hub
2017/04/21 18:37:28
Changing this and elsewhere it applies.
| |
| 158 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); | 159 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
| 159 EXPECT_EQ("testTitle", notification->GetTexts().title); | 160 EXPECT_EQ("testTitle", notification->GetTexts().title); |
| 160 EXPECT_EQ("testMessage", notification->GetTexts().message); | 161 EXPECT_EQ("testMessage", notification->GetTexts().message); |
| 161 } | 162 } |
| 162 | 163 |
| 163 TEST_F(NotificationTest, FilterByUrl) | 164 TEST_F(NotificationTest, FilterByUrl) |
| 164 { | 165 { |
| 165 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 166 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
| 166 AddNotification("{ id: 'www.com', type: 'information'," | 167 AddNotification("{ id: 'www.com', type: 'information'," |
| 167 "urlFilters:['||www.com$document']" | 168 "urlFilters:['||www.com$document']" |
| 168 "}"); | 169 "}"); |
| 169 AddNotification("{ id: 'www.de', type: 'question'," | 170 AddNotification("{ id: 'www.de', type: 'question'," |
| 170 "urlFilters:['||www.de$document']" | 171 "urlFilters:['||www.de$document']" |
| 171 "}"); | 172 "}"); |
| 172 | 173 |
| 173 NotificationPtr notification = PeekNotification(); | 174 auto notification = PeekNotification(); |
| 174 ASSERT_TRUE(notification); | 175 EXPECT_TRUE(notification); |
| 175 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); | 176 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
| 176 | 177 |
| 177 notification = PeekNotification("http://www.de"); | 178 notification = PeekNotification("http://www.de"); |
| 178 ASSERT_TRUE(notification); | 179 EXPECT_TRUE(notification); |
| 179 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); | 180 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); |
| 180 | 181 |
| 181 notification = PeekNotification("http://www.com"); | 182 notification = PeekNotification("http://www.com"); |
| 182 ASSERT_TRUE(notification); | 183 EXPECT_TRUE(notification); |
| 183 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); | 184 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); |
| 184 } | 185 } |
| 185 | 186 |
| 186 TEST_F(NotificationTest, MarkAsShown) | 187 TEST_F(NotificationTest, MarkAsShown) |
| 187 { | 188 { |
| 188 AddNotification("{ id: 'id', type: 'question' }"); | 189 AddNotification("{ id: 'id', type: 'question' }"); |
| 189 EXPECT_TRUE(PeekNotification()); | 190 EXPECT_TRUE(PeekNotification()); |
| 190 NotificationPtr notification = PeekNotification(); | 191 auto notification = PeekNotification(); |
| 191 ASSERT_TRUE(notification); | |
| 192 notification->MarkAsShown(); | 192 notification->MarkAsShown(); |
| 193 EXPECT_FALSE(PeekNotification()); | 193 EXPECT_FALSE(PeekNotification()); |
| 194 } | 194 } |
| 195 | 195 |
| 196 TEST_F(NotificationTest, NoLinks) | 196 TEST_F(NotificationTest, NoLinks) |
| 197 { | 197 { |
| 198 AddNotification("{ id: 'id'}"); | 198 AddNotification("{ id: 'id'}"); |
| 199 NotificationPtr notification = PeekNotification(); | 199 auto notification = PeekNotification(); |
| 200 ASSERT_TRUE(notification); | 200 EXPECT_TRUE(notification); |
| 201 EXPECT_EQ(0u, notification->GetLinks().size()); | 201 EXPECT_EQ(0u, notification->GetLinks().size()); |
| 202 } | 202 } |
| 203 | 203 |
| 204 TEST_F(NotificationTest, Links) | 204 TEST_F(NotificationTest, Links) |
| 205 { | 205 { |
| 206 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); | 206 AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
| 207 NotificationPtr notification = PeekNotification(); | 207 auto notification = PeekNotification(); |
| 208 ASSERT_TRUE(notification); | 208 EXPECT_TRUE(notification); |
| 209 std::vector<std::string> notificationLinks = notification->GetLinks(); | 209 std::vector<std::string> notificationLinks = notification->GetLinks(); |
| 210 ASSERT_EQ(2u, notificationLinks.size()); | 210 ASSERT_EQ(2u, notificationLinks.size()); |
| 211 EXPECT_EQ("link1", notificationLinks[0]); | 211 EXPECT_EQ("link1", notificationLinks[0]); |
| 212 EXPECT_EQ("link2", notificationLinks[1]); | 212 EXPECT_EQ("link2", notificationLinks[1]); |
| 213 } | 213 } |
| OLD | NEW |