Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FilterEngine.cpp

Issue 29500602: Issue 5450 - introduce the Platform class (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 28, 2017, 12:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 #include <AdblockPlus/DefaultLogSystem.h>
19 #include <thread> 20 #include <thread>
20 #include <condition_variable> 21 #include <condition_variable>
21 22
22 using namespace AdblockPlus; 23 using namespace AdblockPlus;
23 24
24 namespace AdblockPlus 25 namespace AdblockPlus
25 { 26 {
26 namespace Utils 27 namespace Utils
27 { 28 {
28 inline bool BeginsWith(const std::string& str, const std::string& beginning) 29 inline bool BeginsWith(const std::string& str, const std::string& beginning)
(...skipping 14 matching lines...) Expand all
43 { 44 {
44 callback(StatResult(), ""); 45 callback(StatResult(), "");
45 }); 46 });
46 } 47 }
47 }; 48 };
48 49
49 template<class LazyFileSystemT, class LogSystem> 50 template<class LazyFileSystemT, class LogSystem>
50 class FilterEngineTestGeneric : public ::testing::Test 51 class FilterEngineTestGeneric : public ::testing::Test
51 { 52 {
52 protected: 53 protected:
54 std::unique_ptr<Platform> platform;
53 FilterEnginePtr filterEngine; 55 FilterEnginePtr filterEngine;
54 56
55 void SetUp() override 57 void SetUp() override
56 { 58 {
57 LazyFileSystemT* fileSystem; 59 LazyFileSystemT* fileSystem;
58 JsEngineCreationParameters jsEngineParams; 60 ThrowingPlatformCreationParameters platformParams;
59 jsEngineParams.fileSystem.reset(fileSystem = new LazyFileSystemT()); 61 platformParams.logSystem.reset(new LogSystem());
60 jsEngineParams.logSystem.reset(new LogSystem()); 62 platformParams.timer.reset(new NoopTimer());
61 jsEngineParams.timer.reset(new NoopTimer()); 63 platformParams.fileSystem.reset(fileSystem = new LazyFileSystemT());
62 jsEngineParams.webRequest.reset(new NoopWebRequest()); 64 platformParams.webRequest.reset(new NoopWebRequest());
63 auto jsEngine = CreateJsEngine(std::move(jsEngineParams)); 65 platform.reset(new Platform(std::move(platformParams)));
64 filterEngine = CreateFilterEngine(*fileSystem, jsEngine); 66 filterEngine = CreateFilterEngine(*fileSystem, platform->GetJsEngine());
65 } 67 }
66 }; 68 };
67 69
68 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 70 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
69 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData; 71 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData;
70 72
71 class FilterEngineWithFreshFolder : public ::testing::Test 73 class FilterEngineWithFreshFolder : public ::testing::Test
72 { 74 {
73 protected: 75 protected:
76 std::unique_ptr<Platform> platform;
74 FileSystemPtr fileSystem; 77 FileSystemPtr fileSystem;
75 std::list<SchedulerTask> fileSystemTasks; 78 std::list<SchedulerTask> fileSystemTasks;
76 std::weak_ptr<JsEngine> weakJsEngine; 79 std::weak_ptr<JsEngine> weakJsEngine;
77 80
78 void SetUp() override 81 void SetUp() override
79 { 82 {
80 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task) 83 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task)
81 { 84 {
82 fileSystemTasks.emplace_back(task); 85 fileSystemTasks.emplace_back(task);
83 }); 86 });
84 // Since there is neither in memory FS nor functionality to work with 87 // Since there is neither in memory FS nor functionality to work with
85 // directories use the hack: manually clean the directory. 88 // directories use the hack: manually clean the directory.
86 removeFileIfExists("patterns.ini"); 89 removeFileIfExists("patterns.ini");
87 removeFileIfExists("prefs.json"); 90 removeFileIfExists("prefs.json");
88 } 91 }
89 JsEnginePtr CreateJsEngine(const AppInfo& appInfo = AppInfo()) 92 JsEnginePtr CreateJsEngine(const AppInfo& appInfo = AppInfo())
90 { 93 {
91 JsEngineCreationParameters jsEngineParams; 94 ThrowingPlatformCreationParameters platformParams;
92 jsEngineParams.appInfo = appInfo; 95 platformParams.logSystem.reset(new LazyLogSystem());
93 jsEngineParams.fileSystem = fileSystem; 96 platformParams.timer.reset(new NoopTimer());
94 jsEngineParams.logSystem.reset(new LazyLogSystem()); 97 platformParams.fileSystem = fileSystem;
95 jsEngineParams.timer.reset(new NoopTimer()); 98 platformParams.webRequest.reset(new NoopWebRequest());
96 jsEngineParams.webRequest.reset(new NoopWebRequest()); 99 platform.reset(new Platform(std::move(platformParams)));
97 auto jsEngine = ::CreateJsEngine(std::move(jsEngineParams)); 100 platform->SetUpJsEngine(appInfo);
101 auto jsEngine = platform->GetJsEngine();
98 weakJsEngine = jsEngine; 102 weakJsEngine = jsEngine;
99 return jsEngine; 103 return jsEngine;
100 } 104 }
101 105
102 FilterEnginePtr CreateFilterEngine(const JsEnginePtr& jsEngine, 106 FilterEnginePtr CreateFilterEngine(const JsEnginePtr& jsEngine,
103 const FilterEngine::CreationParameters& creationParams = FilterEngine::Cre ationParameters()) 107 const FilterEngine::CreationParameters& creationParams = FilterEngine::Cre ationParameters())
104 { 108 {
105 FilterEnginePtr retValue; 109 FilterEnginePtr retValue;
106 FilterEngine::CreateAsync(jsEngine, [&retValue](const FilterEnginePtr& fil terEngine) 110 FilterEngine::CreateAsync(jsEngine, [&retValue](const FilterEnginePtr& fil terEngine)
107 { 111 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test 161 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test
158 { 162 {
159 protected: 163 protected:
160 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; 164 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes;
161 DelayedWebRequest::SharedTasks webRequestTasks; 165 DelayedWebRequest::SharedTasks webRequestTasks;
162 DelayedTimer::SharedTasks timerTasks; 166 DelayedTimer::SharedTasks timerTasks;
163 FilterEngine::CreationParameters createParams; 167 FilterEngine::CreationParameters createParams;
164 ConnectionTypes capturedConnectionTypes; 168 ConnectionTypes capturedConnectionTypes;
165 bool isConnectionAllowed; 169 bool isConnectionAllowed;
166 std::vector<std::function<void(bool)>> isSubscriptionDownloadAllowedCallback s; 170 std::vector<std::function<void(bool)>> isSubscriptionDownloadAllowedCallback s;
171 std::unique_ptr<Platform> platform;
167 FilterEnginePtr filterEngine; 172 FilterEnginePtr filterEngine;
168 JsEnginePtr jsEngine;
169 LazyFileSystem* fileSystem; 173 LazyFileSystem* fileSystem;
170 174
171 void SetUp() 175 void SetUp()
172 { 176 {
173 isConnectionAllowed = true; 177 isConnectionAllowed = true;
174 178
175 JsEngineCreationParameters jsEngineParams; 179 ThrowingPlatformCreationParameters platformParams;
176 jsEngineParams.logSystem.reset(new LazyLogSystem()); 180 platformParams.logSystem.reset(new LazyLogSystem());
177 jsEngineParams.fileSystem.reset(fileSystem = new LazyFileSystem()); 181 platformParams.timer = DelayedTimer::New(timerTasks);
178 jsEngineParams.timer = DelayedTimer::New(timerTasks); 182 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem());
179 jsEngineParams.webRequest = DelayedWebRequest::New(webRequestTasks); 183 platformParams.webRequest = DelayedWebRequest::New(webRequestTasks);
180 jsEngine = CreateJsEngine(std::move(jsEngineParams)); 184 platform.reset(new Platform(std::move(platformParams)));
181 185
182 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false)); 186 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", platform->GetJsEngine()->NewValue(false));
183 187
184 createParams.isSubscriptionDownloadAllowedCallback = [this](const std::str ing* allowedConnectionType, 188 createParams.isSubscriptionDownloadAllowedCallback = [this](const std::str ing* allowedConnectionType,
185 const std::function<void(bool)>& isSubscriptionDownloadAllowedCallback){ 189 const std::function<void(bool)>& isSubscriptionDownloadAllowedCallback){
186 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon nectionType ? *allowedConnectionType : std::string()); 190 capturedConnectionTypes.emplace_back(!!allowedConnectionType, allowedCon nectionType ? *allowedConnectionType : std::string());
187 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo adAllowedCallback); 191 isSubscriptionDownloadAllowedCallbacks.emplace_back(isSubscriptionDownlo adAllowedCallback);
188 }; 192 };
189 } 193 }
190 194
191 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string()) 195 Subscription EnsureExampleSubscriptionAndForceUpdate(const std::string& appp endToUrl = std::string())
192 { 196 {
193 auto subscriptionUrl = "http://example" + apppendToUrl; 197 auto subscriptionUrl = "http://example" + apppendToUrl;
194 bool isSubscriptionDownloadStatusReceived = false; 198 bool isSubscriptionDownloadStatusReceived = false;
195 if (!filterEngine) 199 if (!filterEngine)
196 { 200 {
197 filterEngine = CreateFilterEngine(*fileSystem, jsEngine, createParams); 201 filterEngine = CreateFilterEngine(*fileSystem, platform->GetJsEngine(), createParams);
198 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece ived, &subscriptionUrl](const std::string& action, JsValue&& item) 202 filterEngine->SetFilterChangeCallback([&isSubscriptionDownloadStatusRece ived, &subscriptionUrl](const std::string& action, JsValue&& item)
199 { 203 {
200 if (action == "subscription.downloadStatus" && item.GetProperty("url") .AsString() == subscriptionUrl) 204 if (action == "subscription.downloadStatus" && item.GetProperty("url") .AsString() == subscriptionUrl)
201 isSubscriptionDownloadStatusReceived = true; 205 isSubscriptionDownloadStatusReceived = true;
202 }); 206 });
203 } 207 }
204 auto subscription = filterEngine->GetSubscription(subscriptionUrl); 208 auto subscription = filterEngine->GetSubscription(subscriptionUrl);
205 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl; 209 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()) << subs criptionUrl;
206 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl; 210 EXPECT_TRUE(subscription.GetProperty("downloadStatus").IsNull()) << subscr iptionUrl;
207 subscription.UpdateFilters(); 211 subscription.UpdateFilters();
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 996 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
993 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt atus").AsString()); 997 EXPECT_EQ("synchronize_connection_error", subscription.GetProperty("downloadSt atus").AsString());
994 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size()); 998 EXPECT_EQ(0u, subscription.GetProperty("filters").AsList().size());
995 EXPECT_EQ(1u, capturedConnectionTypes.size()); 999 EXPECT_EQ(1u, capturedConnectionTypes.size());
996 } 1000 }
997 1001
998 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, PredefinedAllowedConnectio nTypeIsPassedToCallback) 1002 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, PredefinedAllowedConnectio nTypeIsPassedToCallback)
999 { 1003 {
1000 std::string predefinedAllowedConnectionType = "non-metered"; 1004 std::string predefinedAllowedConnectionType = "non-metered";
1001 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type ", 1005 createParams.preconfiguredPrefs.insert(std::make_pair("allowed_connection_type ",
1002 jsEngine->NewValue(predefinedAllowedConnectionType))); 1006 platform->GetJsEngine()->NewValue(predefinedAllowedConnectionType)));
1003 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1007 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1004 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g()); 1008 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStrin g());
1005 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1009 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1006 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1010 ASSERT_EQ(1u, capturedConnectionTypes.size());
1007 EXPECT_TRUE(capturedConnectionTypes[0].first); 1011 EXPECT_TRUE(capturedConnectionTypes[0].first);
1008 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second); 1012 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second);
1009 } 1013 }
1010 1014
1011 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, ConfiguredConnectionTypeIs PassedToCallback) 1015 TEST_F(FilterEngineIsSubscriptionDownloadAllowedTest, ConfiguredConnectionTypeIs PassedToCallback)
1012 { 1016 {
1013 // FilterEngine->RemoveSubscription is not usable here because subscriptions 1017 // FilterEngine->RemoveSubscription is not usable here because subscriptions
1014 // are cached internally by URL. So, different URLs are used in diffirent 1018 // are cached internally by URL. So, different URLs are used in diffirent
1015 // checks. 1019 // checks.
1016 { 1020 {
1017 std::string predefinedAllowedConnectionType = "non-metered"; 1021 std::string predefinedAllowedConnectionType = "non-metered";
1018 createParams.preconfiguredPrefs.insert(std::make_pair( 1022 createParams.preconfiguredPrefs.insert(std::make_pair(
1019 "allowed_connection_type", jsEngine->NewValue(predefinedAllowedConnectionT ype))); 1023 "allowed_connection_type", platform->GetJsEngine()->NewValue(predefinedAll owedConnectionType)));
1020 auto subscription = EnsureExampleSubscriptionAndForceUpdate(); 1024 auto subscription = EnsureExampleSubscriptionAndForceUpdate();
1021 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1025 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1022 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1026 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1023 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1027 ASSERT_EQ(1u, capturedConnectionTypes.size());
1024 EXPECT_TRUE(capturedConnectionTypes[0].first); 1028 EXPECT_TRUE(capturedConnectionTypes[0].first);
1025 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second ); 1029 EXPECT_EQ(predefinedAllowedConnectionType, capturedConnectionTypes[0].second );
1026 } 1030 }
1027 capturedConnectionTypes.clear(); 1031 capturedConnectionTypes.clear();
1028 { 1032 {
1029 // set no value 1033 // set no value
(...skipping 11 matching lines...) Expand all
1041 std::string testConnection = "test connection"; 1045 std::string testConnection = "test connection";
1042 filterEngine->SetAllowedConnectionType(&testConnection); 1046 filterEngine->SetAllowedConnectionType(&testConnection);
1043 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1047 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1044 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1048 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1045 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1049 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1046 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1050 ASSERT_EQ(1u, capturedConnectionTypes.size());
1047 EXPECT_TRUE(capturedConnectionTypes[0].first); 1051 EXPECT_TRUE(capturedConnectionTypes[0].first);
1048 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1052 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1049 } 1053 }
1050 } 1054 }
OLDNEW

Powered by Google App Engine
This is Rietveld