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: Rebased on https://codereview.adblockplus.org/29481704 Created July 6, 2017, 10: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 #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 FileSystem::IOBuffer Read(const std::string& path) const 110 IOBuffer Read(const std::string& path) const
111 {
112 throw std::runtime_error("Not implemented");
113 }
114 void Read(const std::string& path,
115 const ReadCallback& callback) const
109 { 116 {
110 throw std::runtime_error("Not implemented"); 117 throw std::runtime_error("Not implemented");
111 } 118 }
112 119
113 void Write(const std::string& path, const FileSystem::IOBuffer& content) 120 void Write(const std::string& path, const IOBuffer& content)
121 {
122 throw std::runtime_error("Not implemented");
123 }
124 void Write(const std::string& path, const IOBuffer& data,
125 const Callback& callback)
114 { 126 {
115 throw std::runtime_error("Not implemented"); 127 throw std::runtime_error("Not implemented");
116 } 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 IOBuffer Read(const std::string& path) const 177 IOBuffer 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 IOBuffer(dummyData.cbegin(), dummyData.cend()); 184 return IOBuffer(dummyData.cbegin(), dummyData.cend());
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 data = Read(path);
192 callback(std::move(data), "");
193 }).detach();
194 }
195
162 void Write(const std::string& path, const IOBuffer& content) 196 void Write(const std::string& path, const IOBuffer& content)
163 { 197 {
164 } 198 }
165 199
200 void Write(const std::string& path, const IOBuffer& data,
201 const Callback& callback)
202 {
203 std::thread([this, path, data, callback]
204 {
205 Write(path, data);
206 callback("");
207 }).detach();
208 }
209
166 void Move(const std::string& fromPath, const std::string& toPath) 210 void Move(const std::string& fromPath, const std::string& toPath)
167 { 211 {
168 } 212 }
169 213
214 void Move(const std::string& fromPath, const std::string& toPath,
215 const Callback& callback)
216 {
217 std::thread([this, fromPath, toPath, callback]
218 {
219 Move(fromPath, toPath);
220 callback("");
221 }).detach();
222 }
223
170 void Remove(const std::string& path) 224 void Remove(const std::string& path)
171 { 225 {
172 } 226 }
173 227
228 void Remove(const std::string& path, const Callback& callback)
229 {
230 std::thread([this, path, callback]
231 {
232 Remove(path);
233 callback("");
234 }).detach();
235 }
236
174 StatResult Stat(const std::string& path) const 237 StatResult Stat(const std::string& path) const
175 { 238 {
176 StatResult result; 239 StatResult result;
177 if (path == "patterns.ini") 240 if (path == "patterns.ini")
178 { 241 {
179 result.exists = true; 242 result.exists = true;
180 result.isFile = true; 243 result.isFile = true;
181 } 244 }
182 return result; 245 return result;
183 } 246 }
184 247
248 void Stat(const std::string& path, const StatCallback& callback) const
249 {
250 std::thread([this, path, callback]
251 {
252 callback(Stat(path), "");
253 }).detach();
254 }
255
185 std::string Resolve(const std::string& path) const 256 std::string Resolve(const std::string& path) const
186 { 257 {
187 return path; 258 return path;
188 } 259 }
189 }; 260 };
190 261
191 class NoopWebRequest : public AdblockPlus::IWebRequest 262 class NoopWebRequest : public AdblockPlus::IWebRequest
192 { 263 {
193 public: 264 public:
194 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override 265 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: 311 protected:
241 AdblockPlus::JsEnginePtr jsEngine; 312 AdblockPlus::JsEnginePtr jsEngine;
242 313
243 virtual void SetUp() 314 virtual void SetUp()
244 { 315 {
245 jsEngine = CreateJsEngine(); 316 jsEngine = CreateJsEngine();
246 } 317 }
247 }; 318 };
248 319
249 #endif 320 #endif
OLDNEW
« src/DefaultFileSystem.cpp ('K') | « src/Utils.cpp ('k') | test/BaseJsTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld