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

Side by Side Diff: test/FilterEngine.cpp

Issue 29428624: Issue 5180 - stop using of WebRequestPtr in tests (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created May 3, 2017, 2:07 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
« no previous file with comments | « test/BaseJsTest.h ('k') | test/JsEngine.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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 template<class FileSystem, class LogSystem> 46 template<class FileSystem, class LogSystem>
47 class FilterEngineTestGeneric : public BaseJsTest 47 class FilterEngineTestGeneric : public BaseJsTest
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 BaseJsTest::SetUp();
55 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem)); 55 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new FileSystem));
56 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest)); 56 jsEngine->SetWebRequest(std::make_shared<LazyWebRequest>());
57 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem)); 57 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LogSystem));
58 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 58 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
59 } 59 }
60 void TearDown() override 60 void TearDown() override
61 { 61 {
62 // Workaround for issue 5198 62 // Workaround for issue 5198
63 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 63 std::this_thread::sleep_for(std::chrono::milliseconds(100));
64 } 64 }
65 }; 65 };
66 66
67 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 67 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
68 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData; 68 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData;
69 69
70 class UpdaterTest : public ::testing::Test 70 class UpdaterTest : public ::testing::Test
71 { 71 {
72 protected: 72 protected:
73 class MockWebRequest : public AdblockPlus::WebRequest 73 class MockWebRequest : public AdblockPlus::WebRequest
74 { 74 {
75 public: 75 public:
76 AdblockPlus::ServerResponse response; 76 AdblockPlus::ServerResponse response;
77 77
78 AdblockPlus::ServerResponse GET(const std::string& url, 78 AdblockPlus::ServerResponse GET(const std::string& url,
79 const AdblockPlus::HeaderList& requestHeaders) const 79 const AdblockPlus::HeaderList& requestHeaders) const
80 { 80 {
81 return response; 81 return response;
82 } 82 }
83 }; 83 };
84 84
85 MockWebRequest* mockWebRequest; 85 std::shared_ptr<MockWebRequest> mockWebRequest;
86 FilterEnginePtr filterEngine; 86 FilterEnginePtr filterEngine;
87 87
88 void SetUp() 88 void SetUp()
89 { 89 {
90 AdblockPlus::AppInfo appInfo; 90 AdblockPlus::AppInfo appInfo;
91 appInfo.name = "test"; 91 appInfo.name = "test";
92 appInfo.version = "1.0.1"; 92 appInfo.version = "1.0.1";
93 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo); 93 AdblockPlus::JsEnginePtr jsEngine = CreateJsEngine(appInfo);
94 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem)); 94 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem));
95 mockWebRequest = new MockWebRequest; 95 jsEngine->SetWebRequest(mockWebRequest = std::make_shared<MockWebRequest>( ));
96 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(mockWebRequest));
97 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 96 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
98 } 97 }
99 }; 98 };
100 99
101 class FilterEngineWithFreshFolder : public ::testing::Test 100 class FilterEngineWithFreshFolder : public ::testing::Test
102 { 101 {
103 protected: 102 protected:
104 FileSystemPtr fileSystem; 103 FileSystemPtr fileSystem;
105 std::weak_ptr<JsEngine> weakJsEngine; 104 std::weak_ptr<JsEngine> weakJsEngine;
106 105
107 void SetUp() override 106 void SetUp() override
108 { 107 {
109 fileSystem.reset(new DefaultFileSystem()); 108 fileSystem.reset(new DefaultFileSystem());
110 // Since there is neither in memory FS nor functionality to work with 109 // Since there is neither in memory FS nor functionality to work with
111 // directories use the hack: manually clean the directory. 110 // directories use the hack: manually clean the directory.
112 removeFileIfExists("patterns.ini"); 111 removeFileIfExists("patterns.ini");
113 removeFileIfExists("prefs.json"); 112 removeFileIfExists("prefs.json");
114 } 113 }
115 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 114 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo())
116 { 115 {
117 auto jsEngine = JsEngine::New(appInfo); 116 auto jsEngine = JsEngine::New(appInfo);
118 weakJsEngine = jsEngine; 117 weakJsEngine = jsEngine;
119 jsEngine->SetFileSystem(fileSystem); 118 jsEngine->SetFileSystem(fileSystem);
120 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(new LazyWebRequest())); 119 jsEngine->SetWebRequest(std::make_shared<LazyWebRequest>());
121 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); 120 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
122 return jsEngine; 121 return jsEngine;
123 } 122 }
124 void TearDown() override 123 void TearDown() override
125 { 124 {
126 removeFileIfExists("patterns.ini"); 125 removeFileIfExists("patterns.ini");
127 removeFileIfExists("prefs.json"); 126 removeFileIfExists("prefs.json");
128 fileSystem.reset(); 127 fileSystem.reset();
129 } 128 }
130 void removeFileIfExists(const std::string& path) 129 void removeFileIfExists(const std::string& path)
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 void Clear() 185 void Clear()
187 { 186 {
188 std::lock_guard<std::mutex> lock(mutex); 187 std::lock_guard<std::mutex> lock(mutex);
189 strings.clear(); 188 strings.clear();
190 } 189 }
191 private: 190 private:
192 mutable std::mutex mutex; 191 mutable std::mutex mutex;
193 std::vector<std::pair<bool, std::string>> strings; 192 std::vector<std::pair<bool, std::string>> strings;
194 }; 193 };
195 protected: 194 protected:
196 MockWebRequest* webRequest; 195 std::shared_ptr<MockWebRequest> webRequest;
197 std::string subscriptionUrlPrefix; 196 std::string subscriptionUrlPrefix;
198 FilterEngine::CreationParameters createParams; 197 FilterEngine::CreationParameters createParams;
199 // HACK: it's a shared pointer to keep it available in 198 // HACK: it's a shared pointer to keep it available in
200 // isConnectionAllowedCallback after destroying of the test. 199 // isConnectionAllowedCallback after destroying of the test.
201 struct SharedData 200 struct SharedData
202 { 201 {
203 SyncStrings capturedConnectionTypes; 202 SyncStrings capturedConnectionTypes;
204 bool isConnectionAllowed; 203 bool isConnectionAllowed;
205 struct 204 struct
206 { 205 {
207 std::string url; 206 std::string url;
208 std::mutex mutex; 207 std::mutex mutex;
209 std::condition_variable cv; 208 std::condition_variable cv;
210 } downloadStatusChanged; 209 } downloadStatusChanged;
211 }; 210 };
212 std::shared_ptr<SharedData> data; 211 std::shared_ptr<SharedData> data;
213 FilterEnginePtr filterEngine; 212 FilterEnginePtr filterEngine;
214 213
215 void SetUp() 214 void SetUp()
216 { 215 {
217 data = std::make_shared<SharedData>(); 216 data = std::make_shared<SharedData>();
218 BaseJsTest::SetUp(); 217 BaseJsTest::SetUp();
219 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem())); 218 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem()));
220 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(webRequest = new MockWe bRequest())); 219 jsEngine->SetWebRequest(webRequest = std::make_shared<MockWebRequest>());
221 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem())); 220 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem()));
222 221
223 subscriptionUrlPrefix = "http://example"; 222 subscriptionUrlPrefix = "http://example";
224 ServerResponse exampleSubscriptionResponse; 223 ServerResponse exampleSubscriptionResponse;
225 exampleSubscriptionResponse.responseStatus = 200; 224 exampleSubscriptionResponse.responseStatus = 200;
226 exampleSubscriptionResponse.status = WebRequest::NS_OK; 225 exampleSubscriptionResponse.status = WebRequest::NS_OK;
227 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com"; 226 exampleSubscriptionResponse.responseText = "[Adblock Plus 2.0]\n||example. com";
228 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse); 227 webRequest->responses.emplace(subscriptionUrlPrefix, exampleSubscriptionRe sponse);
229 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false)); 228 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_selec t", jsEngine->NewValue(false));
230 data->isConnectionAllowed = true; 229 data->isConnectionAllowed = true;
(...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after
1132 filterEngine->SetAllowedConnectionType(&testConnection); 1131 filterEngine->SetAllowedConnectionType(&testConnection);
1133 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1132 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1134 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1133 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1135 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1134 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1136 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings(); 1135 auto capturedConnectionTypes = data->capturedConnectionTypes.GetStrings();
1137 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1136 ASSERT_EQ(1u, capturedConnectionTypes.size());
1138 EXPECT_TRUE(capturedConnectionTypes[0].first); 1137 EXPECT_TRUE(capturedConnectionTypes[0].first);
1139 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1138 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1140 } 1139 }
1141 } 1140 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.h ('k') | test/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld