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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test | 127 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test |
128 { | 128 { |
129 protected: | 129 protected: |
130 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; | 130 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; |
131 DelayedWebRequest::SharedTasks webRequestTasks; | 131 DelayedWebRequest::SharedTasks webRequestTasks; |
132 DelayedTimer::SharedTasks timerTasks; | 132 DelayedTimer::SharedTasks timerTasks; |
133 FilterEngine::CreationParameters createParams; | 133 FilterEngine::CreationParameters createParams; |
134 ConnectionTypes capturedConnectionTypes; | 134 ConnectionTypes capturedConnectionTypes; |
135 bool isConnectionAllowed; | 135 bool isConnectionAllowed; |
136 std::vector<std::function<void(bool)>> isSubscriptionDowloadAllowedCallbacks
; | 136 std::vector<std::function<void(bool)>> isSubscriptionDownloadAllowedCallback
s; |
137 FilterEnginePtr filterEngine; | 137 FilterEnginePtr filterEngine; |
138 JsEnginePtr jsEngine; | 138 JsEnginePtr jsEngine; |
139 | 139 |
140 void SetUp() | 140 void SetUp() |
141 { | 141 { |
142 isConnectionAllowed = true; | 142 isConnectionAllowed = true; |
143 | 143 |
144 JsEngineCreationParameters jsEngineParams; | 144 JsEngineCreationParameters jsEngineParams; |
145 jsEngineParams.logSystem.reset(new LazyLogSystem()); | 145 jsEngineParams.logSystem.reset(new LazyLogSystem()); |
146 jsEngineParams.fileSystem.reset(new LazyFileSystem()); | 146 jsEngineParams.fileSystem.reset(new LazyFileSystem()); |
147 jsEngineParams.timer = DelayedTimer::New(timerTasks); | 147 jsEngineParams.timer = DelayedTimer::New(timerTasks); |
148 jsEngineParams.webRequest = DelayedWebRequest::New(webRequestTasks); | 148 jsEngineParams.webRequest = DelayedWebRequest::New(webRequestTasks); |
149 jsEngine = CreateJsEngine(std::move(jsEngineParams)); | 149 jsEngine = CreateJsEngine(std::move(jsEngineParams)); |
150 | 150 |
151 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec
t", jsEngine->NewValue(false)); | 151 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec
t", jsEngine->NewValue(false)); |
152 | 152 |
153 createParams.isSubscriptionDowloadAllowedCallback = [this](const std::stri
ng* allowedConnectionType, | 153 createParams.isSubscriptionDownloadAllowedCallback = [this](const std::str
ing* allowedConnectionType, |
154 const std::function<void(bool)>& isSubscriptionDowloadAllowedCallback){ | 154 const std::function<void(bool)>& isSubscriptionDownloadAllowedCallback){ |
155 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon
nectionType ? *allowedConnectionType : std::string()); | 155 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon
nectionType ? *allowedConnectionType : std::string()); |
156 isSubscriptionDowloadAllowedCallbacks.emplace_back(isSubscriptionDowload
AllowedCallback); | 156 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo
adAllowedCallback); |
157 }; | 157 }; |
158 } | 158 } |
159 | 159 |
160 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp
endToUrl = std::string()) | 160 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp
endToUrl = std::string()) |
161 { | 161 { |
162 auto subscriptionUrl = "http://example" + apppendToUrl; | 162 auto subscriptionUrl = "http://example" + apppendToUrl; |
163 bool isSubscriptionDownloadStatusReceived = false; | 163 bool isSubscriptionDownloadStatusReceived = false; |
164 if (!filterEngine) | 164 if (!filterEngine) |
165 { | 165 { |
166 filterEngine = FilterEngine::Create(jsEngine, createParams); | 166 filterEngine = FilterEngine::Create(jsEngine, createParams); |
167 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece
ived, &subscriptionUrl](const std::string& action, JsValue&& item) | 167 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece
ived, &subscriptionUrl](const std::string& action, JsValue&& item) |
168 { | 168 { |
169 if (action == "subscription.downloadStatus" && item.GetProperty("url")
.AsString() == subscriptionUrl) | 169 if (action == "subscription.downloadStatus" && item.GetProperty("url")
.AsString() == subscriptionUrl) |
170 isSubscriptionDownloadStatusReceived = true; | 170 isSubscriptionDownloadStatusReceived = true; |
171 }); | 171 }); |
172 } | 172 } |
173 auto subscription = filterEngine->GetSubscription(subscriptionUrl); | 173 auto subscription = filterEngine->GetSubscription(subscriptionUrl); |
174 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs
criptionUrl; | 174 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs
criptionUrl; |
175 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr
iptionUrl; | 175 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr
iptionUrl; |
176 subscription.UpdateFilters(); | 176 subscription.UpdateFilters(); |
177 | 177 |
178 // Since currently the check is called from implemenation of web request | 178 // Since currently the check is called from implemenation of web request |
179 // they have to been firstly scheduled, namely before processing of | 179 // they have to been firstly scheduled, namely before processing of |
180 // 'is subscription download allowed' callbacks; | 180 // 'is subscription download allowed' callbacks; |
181 DelayedTimer::ProcessImmediateTimers(timerTasks); | 181 DelayedTimer::ProcessImmediateTimers(timerTasks); |
182 | 182 |
183 for (const auto& isSubscriptionDowloadAllowedCallback : isSubscriptionDowl
oadAllowedCallbacks) | 183 for (const auto& isSubscriptionDownloadAllowedCallback : isSubscriptionDow
nloadAllowedCallbacks) |
184 { | 184 { |
185 isSubscriptionDowloadAllowedCallback(isConnectionAllowed); | 185 isSubscriptionDownloadAllowedCallback(isConnectionAllowed); |
186 } | 186 } |
187 isSubscriptionDowloadAllowedCallbacks.clear(); | 187 isSubscriptionDownloadAllowedCallbacks.clear(); |
188 | 188 |
189 { | 189 { |
190 auto ii_webRequest = std::find_if(webRequestTasks->begin(), webRequestTa
sks->end(), [&subscriptionUrl](const DelayedWebRequest::Task& task)->bool | 190 auto ii_webRequest = std::find_if(webRequestTasks->begin(), webRequestTa
sks->end(), [&subscriptionUrl](const DelayedWebRequest::Task& task)->bool |
191 { | 191 { |
192 return Utils::BeginsWith(task.url, subscriptionUrl); | 192 return Utils::BeginsWith(task.url, subscriptionUrl); |
193 }); | 193 }); |
194 | 194 |
195 // if download is allowed then there should be a web request | 195 // if download is allowed then there should be a web request |
196 EXPECT_EQ(isConnectionAllowed, ii_webRequest != webRequestTasks->end()); | 196 EXPECT_EQ(isConnectionAllowed, ii_webRequest != webRequestTasks->end()); |
197 if (ii_webRequest != webRequestTasks->end()) | 197 if (ii_webRequest != webRequestTasks->end()) |
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 ASSERT_TRUE(aaSubscription); | 931 ASSERT_TRUE(aaSubscription); |
932 aaSubscription->RemoveFromList(); | 932 aaSubscription->RemoveFromList(); |
933 } | 933 } |
934 | 934 |
935 testSubscriptionState(parameter.expectedAAStatus, otherSubscriptionsNumber); | 935 testSubscriptionState(parameter.expectedAAStatus, otherSubscriptionsNumber); |
936 } | 936 } |
937 } | 937 } |
938 | 938 |
939 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, AbsentCallbackAllowsUpdati
ng) | 939 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, AbsentCallbackAllowsUpdati
ng) |
940 { | 940 { |
941 createParams.isSubscriptionDowloadAllowedCallback = FilterEngine::IsConnection
AllowedAsyncCallback(); | 941 createParams.isSubscriptionDownloadAllowedCallback = FilterEngine::IsConnectio
nAllowedAsyncCallback(); |
942 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); | 942 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); |
943 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); | 943 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); |
944 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 944 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
945 } | 945 } |
946 | 946 |
947 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, AllowingCallbackAllowsUpda
ting) | 947 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, AllowingCallbackAllowsUpda
ting) |
948 { | 948 { |
949 // no stored allowed_connection_type preference | 949 // no stored allowed_connection_type preference |
950 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); | 950 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); |
951 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); | 951 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin
g()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 std::string testConnection = "test connection"; | 1010 std::string testConnection = "test connection"; |
1011 filterEngine->SetAllowedConnectionType(&testConnection); | 1011 filterEngine->SetAllowedConnectionType(&testConnection); |
1012 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); | 1012 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); |
1013 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); | 1013 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr
ing()); |
1014 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1014 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); |
1015 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1015 ASSERT_EQ(1u, capturedConnectionTypes.size()); |
1016 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1016 EXPECT_TRUE(capturedConnectionTypes[0].first); |
1017 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); | 1017 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); |
1018 } | 1018 } |
1019 } | 1019 } |
OLD | NEW |