| Left: | ||
| Right: |
| 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-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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 std::unique_ptr<Notification> PeekNotification(const std::string& url = std: :string()) | 51 std::unique_ptr<Notification> PeekNotification(const std::string& url = std: :string()) |
| 52 { | 52 { |
| 53 std::unique_ptr<Notification> retValue; | 53 std::unique_ptr<Notification> retValue; |
| 54 filterEngine->SetShowNotificationCallback( | 54 filterEngine->SetShowNotificationCallback( |
| 55 [&retValue](const Notification& notification) { | 55 [&retValue](Notification& notification) { |
| 56 NotificationAvailableCallback(notification, retValue); | 56 retValue.reset(new Notification(std::move(notification))); |
| 57 }); | 57 }); |
| 58 filterEngine->ShowNextNotification(url); | 58 filterEngine->ShowNextNotification(url); |
| 59 filterEngine->RemoveShowNotificationCallback(); | 59 filterEngine->RemoveShowNotificationCallback(); |
| 60 return retValue; | 60 return retValue; |
| 61 } | |
| 62 | |
| 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
| |
| 64 { | |
| 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.
| |
| 66 dst.reset(new Notification(src)); | |
| 67 } | 61 } |
| 68 }; | 62 }; |
| 69 | 63 |
| 70 class MockWebRequest : public WebRequest | 64 class MockWebRequest : public WebRequest |
| 71 { | 65 { |
| 72 public: | 66 public: |
| 73 std::string responseText; | 67 std::string responseText; |
| 74 explicit MockWebRequest(const std::string& notification) | 68 explicit MockWebRequest(const std::string& notification) |
| 75 : responseText(notification) | 69 : responseText(notification) |
| 76 { | 70 { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 "\"message\": {" | 103 "\"message\": {" |
| 110 "\"en-US\": \"message\"" | 104 "\"en-US\": \"message\"" |
| 111 "}," | 105 "}," |
| 112 "\"title\": \"Title\"" | 106 "\"title\": \"Title\"" |
| 113 "}]" | 107 "}]" |
| 114 "}"; | 108 "}"; |
| 115 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( | 109 jsEngine->SetWebRequest(std::shared_ptr<MockWebRequest>( |
| 116 new MockWebRequest(responseJsonText))); | 110 new MockWebRequest(responseJsonText))); |
| 117 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); | 111 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); |
| 118 filterEngine = FilterEngine::Create(jsEngine); | 112 filterEngine = FilterEngine::Create(jsEngine); |
| 119 filterEngine->SetShowNotificationCallback([this](Notification& notificatio n) { | 113 filterEngine->SetShowNotificationCallback( |
| 120 OnNotification(notification); | 114 [this](Notification& notification) { |
| 115 isNotificationCallbackCalled = true; | |
| 116 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notificatio n.GetType()); | |
| 117 EXPECT_EQ("Title", notification.GetTexts().title); | |
| 118 EXPECT_EQ("message", notification.GetTexts().message); | |
| 119 notification.MarkAsShown(); | |
| 121 }); | 120 }); |
| 122 } | |
| 123 | |
| 124 void OnNotification(Notification& notification) | |
| 125 { | |
| 126 isNotificationCallbackCalled = true; | |
| 127 ASSERT_FALSE(notification.IsUndefined()); | |
| 128 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification.Ge tType()); | |
| 129 EXPECT_EQ("Title", notification.GetTexts().title); | |
| 130 EXPECT_EQ("message", notification.GetTexts().message); | |
| 131 notification.MarkAsShown(); | |
| 132 } | 121 } |
| 133 }; | 122 }; |
| 134 #endif | 123 #endif |
| 135 } | 124 } |
| 136 | 125 |
| 137 TEST_F(NotificationTest, NoNotifications) | 126 TEST_F(NotificationTest, NoNotifications) |
| 138 { | 127 { |
| 139 EXPECT_FALSE(PeekNotification()); | 128 EXPECT_FALSE(PeekNotification()); |
| 140 } | 129 } |
| 141 | 130 |
| 142 #ifdef NotificationMockWebRequestTest_ENABLED | 131 #ifdef NotificationMockWebRequestTest_ENABLED |
| 143 TEST_F(NotificationMockWebRequestTest, SingleNotification) | 132 TEST_F(NotificationMockWebRequestTest, SingleNotification) |
| 144 { | 133 { |
| 145 AdblockPlus::Sleep(5000/*msec*/); // it's a hack | 134 AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
| 146 EXPECT_TRUE(isNotificationCallbackCalled); | 135 EXPECT_TRUE(isNotificationCallbackCalled); |
| 147 } | 136 } |
| 148 #endif | 137 #endif |
| 149 | 138 |
| 150 TEST_F(NotificationTest, AddNotification) | 139 TEST_F(NotificationTest, AddNotification) |
| 151 { | 140 { |
| 152 AddNotification("{" | 141 AddNotification("{" |
| 153 "type: 'critical'," | 142 "type: 'critical'," |
| 154 "title: 'testTitle'," | 143 "title: 'testTitle'," |
| 155 "message: 'testMessage'," | 144 "message: 'testMessage'," |
| 156 "}"); | 145 "}"); |
| 157 auto notification = PeekNotification(); | 146 auto notification = PeekNotification(); |
| 158 EXPECT_TRUE(notification); | 147 ASSERT_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.
| |
| 159 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); | 148 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
| 160 EXPECT_EQ("testTitle", notification->GetTexts().title); | 149 EXPECT_EQ("testTitle", notification->GetTexts().title); |
| 161 EXPECT_EQ("testMessage", notification->GetTexts().message); | 150 EXPECT_EQ("testMessage", notification->GetTexts().message); |
| 162 } | 151 } |
| 163 | 152 |
| 164 TEST_F(NotificationTest, FilterByUrl) | 153 TEST_F(NotificationTest, FilterByUrl) |
| 165 { | 154 { |
| 166 AddNotification("{ id: 'no-filter', type: 'critical' }"); | 155 AddNotification("{ id: 'no-filter', type: 'critical' }"); |
| 167 AddNotification("{ id: 'www.com', type: 'information'," | 156 AddNotification("{ id: 'www.com', type: 'information'," |
| 168 "urlFilters:['||www.com$document']" | 157 "urlFilters:['||www.com$document']" |
| 169 "}"); | 158 "}"); |
| 170 AddNotification("{ id: 'www.de', type: 'question'," | 159 AddNotification("{ id: 'www.de', type: 'question'," |
| 171 "urlFilters:['||www.de$document']" | 160 "urlFilters:['||www.de$document']" |
| 172 "}"); | 161 "}"); |
| 173 | 162 |
| 174 auto notification = PeekNotification(); | 163 auto notification = PeekNotification(); |
| 175 EXPECT_TRUE(notification); | 164 ASSERT_TRUE(notification); |
| 176 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); | 165 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); |
| 177 | 166 |
| 178 notification = PeekNotification("http://www.de"); | 167 notification = PeekNotification("http://www.de"); |
| 179 EXPECT_TRUE(notification); | 168 ASSERT_TRUE(notification); |
| 180 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); | 169 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); |
| 181 | 170 |
| 182 notification = PeekNotification("http://www.com"); | 171 notification = PeekNotification("http://www.com"); |
| 183 EXPECT_TRUE(notification); | 172 ASSERT_TRUE(notification); |
| 184 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); | 173 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); |
| 185 } | 174 } |
| 186 | 175 |
| 187 TEST_F(NotificationTest, MarkAsShown) | 176 TEST_F(NotificationTest, MarkAsShown) |
| 188 { | 177 { |
| 189 AddNotification("{ id: 'id', type: 'question' }"); | 178 AddNotification("{ id: 'id', type: 'question' }"); |
| 190 EXPECT_TRUE(PeekNotification()); | 179 EXPECT_TRUE(PeekNotification()); |
| 191 auto notification = PeekNotification(); | 180 auto notification = PeekNotification(); |
| 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 auto notification = PeekNotification(); | 189 auto notification = PeekNotification(); |
| 200 EXPECT_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 auto notification = PeekNotification(); | 197 auto notification = PeekNotification(); |
| 208 EXPECT_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 } |
| LEFT | RIGHT |