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: Updated patch after review. Created June 16, 2017, 9:52 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
25 // Strictly speaking in each test there should be a special implementation of 27 // 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 28 // 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 29 // 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 30 // actually needs is the access to pending tasks. Therefore instantiation of
29 // implemenation of an interface, creation of shared tasks and sharing them 31 // implemenation of an interface, creation of shared tasks and sharing them
30 // with tasks in test is located in this class. 32 // with tasks in test is located in this class.
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 class ThrowingLogSystem : public AdblockPlus::LogSystem 97 class ThrowingLogSystem : public AdblockPlus::LogSystem
96 { 98 {
97 public: 99 public:
98 void operator()(LogLevel logLevel, const std::string& message, 100 void operator()(LogLevel logLevel, const std::string& message,
99 const std::string& source) 101 const std::string& source)
100 { 102 {
101 throw std::runtime_error("Unexpected error: " + message); 103 throw std::runtime_error("Unexpected error: " + message);
102 } 104 }
103 }; 105 };
104 106
105 class ThrowingFileSystem : public AdblockPlus::FileSystem 107 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem
106 { 108 {
107 public: 109 public:
108 std::shared_ptr<std::istream> Read(const std::string& path) const 110 std::shared_ptr<std::istream> Read(const std::string& path) const
109 { 111 {
110 throw std::runtime_error("Not implemented"); 112 throw std::runtime_error("Not implemented");
111 } 113 }
114 void Read(const std::string& path,
115 const ReadCallback& callback) const
116 {
117 throw std::runtime_error("Not implemented");
118 }
112 119
113 void Write(const std::string& path, std::istream& content) 120 void Write(const std::string& path, std::istream& content)
114 { 121 {
115 throw std::runtime_error("Not implemented"); 122 throw std::runtime_error("Not implemented");
116 } 123 }
124 void Write(const std::string& path, const std::string& data,
125 const Callback& callback)
126 {
127 throw std::runtime_error("Not implemented");
128 }
117 129
118 void Move(const std::string& fromPath, const std::string& toPath) 130 void Move(const std::string& fromPath, const std::string& toPath)
119 { 131 {
120 throw std::runtime_error("Not implemented"); 132 throw std::runtime_error("Not implemented");
121 } 133 }
134 void Move(const std::string& fromPath, const std::string& toPath,
135 const Callback& callback)
136 {
137 throw std::runtime_error("Not implemented");
138 }
122 139
123 void Remove(const std::string& path) 140 void Remove(const std::string& path)
124 { 141 {
125 throw std::runtime_error("Not implemented"); 142 throw std::runtime_error("Not implemented");
126 } 143 }
144 void Remove(const std::string& path, const Callback& callback)
145 {
146 throw std::runtime_error("Not implemented");
147 }
127 148
128 StatResult Stat(const std::string& path) const 149 StatResult Stat(const std::string& path) const
129 { 150 {
130 throw std::runtime_error("Not implemented"); 151 throw std::runtime_error("Not implemented");
131 } 152 }
153 void Stat(const std::string& path,
154 const StatCallback& callback) const
155 {
156 throw std::runtime_error("Not implemented");
157 }
132 158
133 std::string Resolve(const std::string& path) const 159 std::string Resolve(const std::string& path) const
134 { 160 {
135 throw std::runtime_error("Not implemented"); 161 throw std::runtime_error("Not implemented");
136 } 162 }
137
138 }; 163 };
139 164
140 class ThrowingWebRequest : public AdblockPlus::IWebRequest 165 class ThrowingWebRequest : public AdblockPlus::IWebRequest
141 { 166 {
142 public: 167 public:
143 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override 168 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override
144 { 169 {
145 throw std::runtime_error("Unexpected GET: " + url); 170 throw std::runtime_error("Unexpected GET: " + url);
146 } 171 }
147 }; 172 };
148 173
149 class LazyFileSystem : public AdblockPlus::FileSystem 174 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System
150 { 175 {
151 public: 176 public:
152 std::shared_ptr<std::istream> Read(const std::string& path) const 177 std::shared_ptr<std::istream> Read(const std::string& path) const
153 { 178 {
154 std::string dummyData(""); 179 std::string dummyData("");
155 if (path == "patterns.ini") 180 if (path == "patterns.ini")
156 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; 181 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~";
157 else if (path == "prefs.json") 182 else if (path == "prefs.json")
158 dummyData = "{}"; 183 dummyData = "{}";
159 return std::shared_ptr<std::istream>(new std::istringstream(dummyData)); 184 return std::shared_ptr<std::istream>(new std::istringstream(dummyData));
160 } 185 }
161 186
187 void Read(const std::string& path, const ReadCallback& callback) const
188 {
189 std::thread([this, path, callback]
190 {
191 auto result = Read(path);
192 std::stringstream stream;
193 stream << result->rdbuf();
194 std::string content = stream.str();
195 callback(std::move(content), "");
196 }).detach();
197 }
198
162 void Write(const std::string& path, std::istream& content) 199 void Write(const std::string& path, std::istream& content)
163 { 200 {
164 } 201 }
165 202
203 void Write(const std::string& path, const std::string& data,
204 const Callback& callback)
205 {
206 std::thread([this, path, data, callback]
207 {
208 std::stringstream stream;
209 stream << data;
210 Write(path, stream);
211 callback("");
212 }).detach();
213 }
214
166 void Move(const std::string& fromPath, const std::string& toPath) 215 void Move(const std::string& fromPath, const std::string& toPath)
167 { 216 {
168 } 217 }
169 218
219 void Move(const std::string& fromPath, const std::string& toPath,
220 const Callback& callback)
221 {
222 std::thread([this, fromPath, toPath, callback]
223 {
224 Move(fromPath, toPath);
225 callback("");
226 }).detach();
227 }
228
170 void Remove(const std::string& path) 229 void Remove(const std::string& path)
171 { 230 {
172 } 231 }
173 232
233 void Remove(const std::string& path, const Callback& callback)
234 {
235 std::thread([this, path, callback]
236 {
237 Remove(path);
238 callback("");
239 }).detach();
240 }
241
174 StatResult Stat(const std::string& path) const 242 StatResult Stat(const std::string& path) const
175 { 243 {
176 StatResult result; 244 StatResult result;
177 if (path == "patterns.ini") 245 if (path == "patterns.ini")
178 { 246 {
179 result.exists = true; 247 result.exists = true;
180 result.isFile = true; 248 result.isFile = true;
181 } 249 }
182 return result; 250 return result;
183 } 251 }
184 252
253 void Stat(const std::string& path, const StatCallback& callback) const
254 {
255 std::thread([this, path, callback]
256 {
257 callback(Stat(path), "");
258 }).detach();
259 }
260
185 std::string Resolve(const std::string& path) const 261 std::string Resolve(const std::string& path) const
186 { 262 {
187 return path; 263 return path;
188 } 264 }
189 }; 265 };
190 266
191 class NoopWebRequest : public AdblockPlus::IWebRequest 267 class NoopWebRequest : public AdblockPlus::IWebRequest
192 { 268 {
193 public: 269 public:
194 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override 270 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: 316 protected:
241 AdblockPlus::JsEnginePtr jsEngine; 317 AdblockPlus::JsEnginePtr jsEngine;
242 318
243 virtual void SetUp() 319 virtual void SetUp()
244 { 320 {
245 jsEngine = CreateJsEngine(); 321 jsEngine = CreateJsEngine();
246 } 322 }
247 }; 323 };
248 324
249 #endif 325 #endif
OLDNEW
« src/Thread.h ('K') | « src/Thread.h ('k') | test/BaseJsTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld