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

Side by Side Diff: test/FilterEngine.cpp

Issue 29433591: Issue 5180 - start to inject implementation of WebRequest into JsEngine::ctr (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created May 8, 2017, 11:59 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/BaseJsTest.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 26 matching lines...) Expand all
37 class VeryLazyFileSystem : public LazyFileSystem 37 class VeryLazyFileSystem : public LazyFileSystem
38 { 38 {
39 public: 39 public:
40 StatResult Stat(const std::string& path) const 40 StatResult Stat(const std::string& path) const
41 { 41 {
42 return StatResult(); 42 return StatResult();
43 } 43 }
44 }; 44 };
45 45
46 template<class FileSystem, class LogSystem> 46 template<class FileSystem, class LogSystem>
47 class FilterEngineTestGeneric : public BaseJsTest 47 class FilterEngineTestGeneric : public ::testing::Test
48 { 48 {
49 protected: 49 protected:
50 FilterEnginePtr filterEngine; 50 FilterEnginePtr filterEngine;
51 51
52 void SetUp() override 52 void SetUp() override
53 { 53 {
54 BaseJsTest::SetUp(); 54 JsEngineCreationParameters jsEngineParams;
55 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem)); 55 jsEngineParams.fileSystem.reset(new FileSystem());
56 jsEngine->SetWebRequest(std::make_shared<LazyWebRequest>()); 56 jsEngineParams.logSystem.reset(new LogSystem());
57 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); 57 jsEngineParams.timer.reset(new NoopTimer());
58 jsEngineParams.webRequest.reset(new NoopWebRequest());
59 auto jsEngine = CreateJsEngine(std::move(jsEngineParams));
58 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 60 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
59 } 61 }
60 void TearDown() override 62 void TearDown() override
61 { 63 {
62 // Workaround for issue 5198 64 // Workaround for issue 5198
63 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 65 std::this_thread::sleep_for(std::chrono::milliseconds(100));
64 } 66 }
65 }; 67 };
66 68
67 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 69 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
(...skipping 12 matching lines...) Expand all
80 { 82 {
81 return response; 83 return response;
82 } 84 }
83 }; 85 };
84 86
85 std::shared_ptr<MockWebRequest> mockWebRequest; 87 std::shared_ptr<MockWebRequest> mockWebRequest;
86 FilterEnginePtr filterEngine; 88 FilterEnginePtr filterEngine;
87 89
88 void SetUp() 90 void SetUp()
89 { 91 {
90 AdblockPlus::AppInfo appInfo; 92 JsEngineCreationParameters jsEngineParams;
91 appInfo.name = "test"; 93 jsEngineParams.appInfo.name = "test";
92 appInfo.version = "1.0.1"; 94 jsEngineParams.appInfo.version = "1.0.1";
93 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo); 95 jsEngineParams.timer = CreateDefaultTimer();
94 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); 96 jsEngineParams.fileSystem.reset(new LazyFileSystem());
97 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(std::move(jsEngineParam s));
95 jsEngine->SetWebRequest(mockWebRequest = std::make_shared<MockWebRequest>( )); 98 jsEngine->SetWebRequest(mockWebRequest = std::make_shared<MockWebRequest>( ));
96 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 99 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
97 } 100 }
98 }; 101 };
99 102
100 class FilterEngineWithFreshFolder : public ::testing::Test 103 class FilterEngineWithFreshFolder : public ::testing::Test
101 { 104 {
102 protected: 105 protected:
103 FileSystemPtr fileSystem; 106 FileSystemPtr fileSystem;
104 std::weak_ptr<JsEngine> weakJsEngine; 107 std::weak_ptr<JsEngine> weakJsEngine;
105 108
106 void SetUp() override 109 void SetUp() override
107 { 110 {
108 fileSystem.reset(new DefaultFileSystem()); 111 fileSystem.reset(new DefaultFileSystem());
109 // Since there is neither in memory FS nor functionality to work with 112 // Since there is neither in memory FS nor functionality to work with
110 // directories use the hack: manually clean the directory. 113 // directories use the hack: manually clean the directory.
111 removeFileIfExists("patterns.ini"); 114 removeFileIfExists("patterns.ini");
112 removeFileIfExists("prefs.json"); 115 removeFileIfExists("prefs.json");
113 } 116 }
114 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 117 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo())
115 { 118 {
116 auto jsEngine = JsEngine::New(appInfo); 119 JsEngineCreationParameters jsEngineParams;
120 jsEngineParams.appInfo = appInfo;
121 jsEngineParams.fileSystem = fileSystem;
122 jsEngineParams.logSystem.reset(new LazyLogSystem());
123 jsEngineParams.timer.reset(new NoopTimer());
124 jsEngineParams.webRequest.reset(new NoopWebRequest());
125 auto jsEngine = CreateJsEngine(std::move(jsEngineParams));
117 weakJsEngine = jsEngine; 126 weakJsEngine = jsEngine;
118 jsEngine->SetFileSystem(fileSystem);
119 jsEngine->SetWebRequest(std::make_shared<LazyWebRequest>());
120 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
121 return jsEngine; 127 return jsEngine;
122 } 128 }
123 void TearDown() override 129 void TearDown() override
124 { 130 {
125 removeFileIfExists("patterns.ini"); 131 removeFileIfExists("patterns.ini");
126 removeFileIfExists("prefs.json"); 132 removeFileIfExists("prefs.json");
127 fileSystem.reset(); 133 fileSystem.reset();
128 } 134 }
129 void removeFileIfExists(const std::string& path) 135 void removeFileIfExists(const std::string& path)
130 { 136 {
(...skipping 11 matching lines...) Expand all
142 { 148 {
143 return false; 149 return false;
144 } 150 }
145 }; 151 };
146 int i = 5; 152 int i = 5;
147 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove()) 153 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove())
148 std::this_thread::sleep_for(std::chrono::seconds(2)); 154 std::this_thread::sleep_for(std::chrono::seconds(2));
149 } 155 }
150 }; 156 };
151 157
152 class FilterEngineIsAllowedConnectionTest : public BaseJsTest 158 class FilterEngineIsAllowedConnectionTest : public ::testing::Test
153 { 159 {
154 class MockWebRequest : public LazyWebRequest 160 class MockWebRequest : public LazyWebRequest
155 { 161 {
156 public: 162 public:
157 std::map</*beginning of url*/std::string, AdblockPlus::ServerResponse> res ponses; 163 std::map</*beginning of url*/std::string, AdblockPlus::ServerResponse> res ponses;
158 164
159 AdblockPlus::ServerResponse GET(const std::string& url, 165 AdblockPlus::ServerResponse GET(const std::string& url,
160 const AdblockPlus::HeaderList& requestHeaders) const 166 const AdblockPlus::HeaderList& requestHeaders) const
161 { 167 {
162 for (const auto& response : responses) 168 for (const auto& response : responses)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 bool isConnectionAllowed; 209 bool isConnectionAllowed;
204 struct 210 struct
205 { 211 {
206 std::string url; 212 std::string url;
207 std::mutex mutex; 213 std::mutex mutex;
208 std::condition_variable cv; 214 std::condition_variable cv;
209 } downloadStatusChanged; 215 } downloadStatusChanged;
210 }; 216 };
211 std::shared_ptr<SharedData> data; 217 std::shared_ptr<SharedData> data;
212 FilterEnginePtr filterEngine; 218 FilterEnginePtr filterEngine;
219 JsEnginePtr jsEngine;
213 220
214 void SetUp() 221 void SetUp()
215 { 222 {
216 data = std::make_shared<SharedData>(); 223 data = std::make_shared<SharedData>();
217 BaseJsTest::SetUp(); 224 JsEngineCreationParameters jsEngineParams;
218 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); 225 jsEngineParams.logSystem.reset(new LazyLogSystem());
226 jsEngineParams.fileSystem.reset(new LazyFileSystem());
227 jsEngineParams.timer = CreateDefaultTimer();
228 jsEngine = CreateJsEngine(std::move(jsEngineParams));
219 jsEngine->SetWebRequest(webRequest = std::make_shared<MockWebRequest>()); 229 jsEngine->SetWebRequest(webRequest = std::make_shared<MockWebRequest>());
220 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
221 230
222 subscriptionUrlPrefix = "http://example"; 231 subscriptionUrlPrefix = "http://example";
223 ServerResponse exampleSubscriptionResponse; 232 ServerResponse exampleSubscriptionResponse;
224 exampleSubscriptionResponse.responseStatus = 200; 233 exampleSubscriptionResponse.responseStatus = 200;
225 exampleSubscriptionResponse.status = IWebRequest::NS_OK; 234 exampleSubscriptionResponse.status = IWebRequest::NS_OK;
226 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com"; 235 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com";
227 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse); 236 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse);
228 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false)); 237 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false));
229 data->isConnectionAllowed = true; 238 data->isConnectionAllowed = true;
230 auto closure = data; 239 auto closure = data;
(...skipping 900 matching lines...) Expand 10 before | Expand all | Expand 10 after
1131 filterEngine->SetAllowedConnectionType(&testConnection); 1140 filterEngine->SetAllowedConnectionType(&testConnection);
1132 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1141 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1133 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1142 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1134 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1143 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1135 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings(); 1144 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1136 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1145 ASSERT_EQ(1u, capturedConnectionTypes.size());
1137 EXPECT_TRUE(capturedConnectionTypes[0].first); 1146 EXPECT_TRUE(capturedConnectionTypes[0].first);
1138 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1147 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1139 } 1148 }
1140 } 1149 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.cpp ('k') | test/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld