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

Side by Side Diff: test/BaseJsTest.h

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Remove a #include Utils.h from test. Created June 2, 2017, 7:38 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 #ifndef MOCKS_H 18 #ifndef MOCKS_H
19 #define MOCKS_H 19 #define MOCKS_H
20 20
21 #include <thread>
22
21 #include <AdblockPlus.h> 23 #include <AdblockPlus.h>
22 #include <gtest/gtest.h> 24 #include <gtest/gtest.h>
23 #include "../src/Thread.h" 25 #include "../src/Thread.h"
24 26
27 class Sync
sergei 2017/06/16 15:05:55 There is a pretty similar class in src/FilterEngin
hub 2017/06/16 21:52:54 That's where it comes from. The problem with Util
sergei 2017/07/03 09:25:53 Acknowledged.
28 {
29 public:
30 Sync()
31 : set(false)
32 {
33 }
34 void Wait(int sec = 20)
sergei 2017/06/16 15:05:55 For the class in utils I think it makes sense to u
hub 2017/06/16 21:52:55 At that point I'd that have Wait() to have a std::
35 {
36 std::unique_lock<std::mutex> lock(mutex);
37 while (!set)
38 cv.wait_for(lock, std::chrono::seconds(sec));
39 }
40 void Set(const std::string& error_ = std::string())
sergei 2017/06/16 15:05:55 unfortunately we are not using anything like m_ pr
hub 2017/06/16 21:52:55 This is actually for -Wshadow that sadly isn't ena
41 {
42 {
43 std::unique_lock<std::mutex> lock(mutex);
44 set = true;
45 error = error_;
46 }
47 cv.notify_all();
48 }
49 std::string GetError()
50 {
51 std::unique_lock<std::mutex> lock(mutex);
52 return error;
53 }
54 private:
55 std::mutex mutex;
56 std::condition_variable cv;
57 bool set;
58 std::string error;
59 };
60
25 // Strictly speaking in each test there should be a special implementation of 61 // Strictly speaking in each test there should be a special implementation of
26 // an interface, which is merely referenced by a wrapper and the latter should 62 // an interface, which is merely referenced by a wrapper and the latter should
27 // be injected into JsEngine or what ever. However the everthing a test often 63 // be injected into JsEngine or what ever. However the everthing a test often
28 // actually needs is the access to pending tasks. Therefore instantiation of 64 // actually needs is the access to pending tasks. Therefore instantiation of
29 // implemenation of an interface, creation of shared tasks and sharing them 65 // implemenation of an interface, creation of shared tasks and sharing them
30 // with tasks in test is located in this class. 66 // with tasks in test is located in this class.
31 // 67 //
32 // Task is passed as an additional template parameter instead of using traits 68 // Task is passed as an additional template parameter instead of using traits
33 // (CRTP does not work with types in derived class) merely to simplify the code 69 // (CRTP does not work with types in derived class) merely to simplify the code
34 // by minimization. 70 // by minimization.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 class ThrowingLogSystem : public AdblockPlus::LogSystem 131 class ThrowingLogSystem : public AdblockPlus::LogSystem
96 { 132 {
97 public: 133 public:
98 void operator()(LogLevel logLevel, const std::string& message, 134 void operator()(LogLevel logLevel, const std::string& message,
99 const std::string& source) 135 const std::string& source)
100 { 136 {
101 throw std::runtime_error("Unexpected error: " + message); 137 throw std::runtime_error("Unexpected error: " + message);
102 } 138 }
103 }; 139 };
104 140
105 class ThrowingFileSystem : public AdblockPlus::FileSystem 141 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem
106 { 142 {
107 public: 143 public:
108 std::shared_ptr<std::istream> Read(const std::string& path) const 144 std::shared_ptr<std::istream> Read(const std::string& path) const
109 { 145 {
110 throw std::runtime_error("Not implemented"); 146 throw std::runtime_error("Not implemented");
111 } 147 }
148 void Read(const std::string& path,
149 const ReadCallback& callback) const
150 {
151 throw std::runtime_error("Not implemented");
152 }
112 153
113 void Write(const std::string& path, std::istream& content) 154 void Write(const std::string& path, std::istream& content)
114 { 155 {
115 throw std::runtime_error("Not implemented"); 156 throw std::runtime_error("Not implemented");
116 } 157 }
158 void Write(const std::string& path, std::shared_ptr<std::istream> data,
159 const Callback& callback)
160 {
161 throw std::runtime_error("Not implemented");
162 }
117 163
118 void Move(const std::string& fromPath, const std::string& toPath) 164 void Move(const std::string& fromPath, const std::string& toPath)
119 { 165 {
120 throw std::runtime_error("Not implemented"); 166 throw std::runtime_error("Not implemented");
121 } 167 }
168 void Move(const std::string& fromPath, const std::string& toPath,
169 const Callback& callback)
170 {
171 throw std::runtime_error("Not implemented");
172 }
122 173
123 void Remove(const std::string& path) 174 void Remove(const std::string& path)
124 { 175 {
125 throw std::runtime_error("Not implemented"); 176 throw std::runtime_error("Not implemented");
126 } 177 }
178 void Remove(const std::string& path, const Callback& callback)
179 {
180 throw std::runtime_error("Not implemented");
181 }
127 182
128 StatResult Stat(const std::string& path) const 183 StatResult Stat(const std::string& path) const
129 { 184 {
130 throw std::runtime_error("Not implemented"); 185 throw std::runtime_error("Not implemented");
131 } 186 }
187 void Stat(const std::string& path,
188 const StatCallback& callback) const
189 {
190 throw std::runtime_error("Not implemented");
191 }
132 192
133 std::string Resolve(const std::string& path) const 193 std::string Resolve(const std::string& path) const
134 { 194 {
135 throw std::runtime_error("Not implemented"); 195 throw std::runtime_error("Not implemented");
136 } 196 }
137
138 }; 197 };
139 198
140 class ThrowingWebRequest : public AdblockPlus::IWebRequest 199 class ThrowingWebRequest : public AdblockPlus::IWebRequest
141 { 200 {
142 public: 201 public:
143 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override 202 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override
144 { 203 {
145 throw std::runtime_error("Unexpected GET: " + url); 204 throw std::runtime_error("Unexpected GET: " + url);
146 } 205 }
147 }; 206 };
148 207
149 class LazyFileSystem : public AdblockPlus::FileSystem 208 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System
150 { 209 {
151 public: 210 public:
152 std::shared_ptr<std::istream> Read(const std::string& path) const 211 std::shared_ptr<std::istream> Read(const std::string& path) const
153 { 212 {
154 std::string dummyData(""); 213 std::string dummyData("");
155 if (path == "patterns.ini") 214 if (path == "patterns.ini")
156 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; 215 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~";
157 else if (path == "prefs.json") 216 else if (path == "prefs.json")
158 dummyData = "{}"; 217 dummyData = "{}";
159 return std::shared_ptr<std::istream>(new std::istringstream(dummyData)); 218 return std::shared_ptr<std::istream>(new std::istringstream(dummyData));
160 } 219 }
161 220
221 void Read(const std::string& path, const ReadCallback& callback) const
222 {
223 std::thread([this, path, callback]
sergei 2017/06/16 15:05:55 I actually planned to gradually convert to a speci
hub 2017/06/16 21:52:55 Acknowledged.
224 {
225 auto result = Read(path);
226 std::stringstream stream;
227 stream << result->rdbuf();
228 std::string content = stream.str();
229 callback(std::move(content), "");
230 }).detach();
231 }
232
162 void Write(const std::string& path, std::istream& content) 233 void Write(const std::string& path, std::istream& content)
163 { 234 {
164 } 235 }
165 236
237 void Write(const std::string& path, std::shared_ptr<std::istream> data,
238 const Callback& callback)
239 {
240 std::thread([this, path, data, callback]
241 {
242 Write(path, *data);
243 callback("");
244 }).detach();
245 }
246
166 void Move(const std::string& fromPath, const std::string& toPath) 247 void Move(const std::string& fromPath, const std::string& toPath)
167 { 248 {
168 } 249 }
169 250
251 void Move(const std::string& fromPath, const std::string& toPath,
252 const Callback& callback)
253 {
254 std::thread([this, fromPath, toPath, callback]
255 {
256 Move(fromPath, toPath);
257 callback("");
258 }).detach();
259 }
260
170 void Remove(const std::string& path) 261 void Remove(const std::string& path)
171 { 262 {
172 } 263 }
173 264
265 void Remove(const std::string& path, const Callback& callback)
266 {
267 std::thread([this, path, callback]
268 {
269 Remove(path);
270 callback("");
271 }).detach();
272 }
273
174 StatResult Stat(const std::string& path) const 274 StatResult Stat(const std::string& path) const
175 { 275 {
176 StatResult result; 276 StatResult result;
177 if (path == "patterns.ini") 277 if (path == "patterns.ini")
178 { 278 {
179 result.exists = true; 279 result.exists = true;
180 result.isFile = true; 280 result.isFile = true;
181 } 281 }
182 return result; 282 return result;
183 } 283 }
184 284
285 void Stat(const std::string& path, const StatCallback& callback) const
286 {
287 std::thread([this, path, callback]
288 {
289 callback(Stat(path), "");
290 }).detach();
291 }
292
185 std::string Resolve(const std::string& path) const 293 std::string Resolve(const std::string& path) const
186 { 294 {
187 return path; 295 return path;
188 } 296 }
189 }; 297 };
190 298
191 class NoopWebRequest : public AdblockPlus::IWebRequest 299 class NoopWebRequest : public AdblockPlus::IWebRequest
192 { 300 {
193 public: 301 public:
194 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override 302 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 protected: 348 protected:
241 AdblockPlus::JsEnginePtr jsEngine; 349 AdblockPlus::JsEnginePtr jsEngine;
242 350
243 virtual void SetUp() 351 virtual void SetUp()
244 { 352 {
245 jsEngine = CreateJsEngine(); 353 jsEngine = CreateJsEngine();
246 } 354 }
247 }; 355 };
248 356
249 #endif 357 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld