LEFT | RIGHT |
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 Loading... |
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 IOBuffer Read(const std::string& path) const |
109 { | 111 { |
110 throw std::runtime_error("Not implemented"); | 112 throw std::runtime_error("Not implemented"); |
111 } | 113 } |
112 void Read(const std::string& path, | 114 void Read(const std::string& path, |
113 const ReadCallback& callback) const | 115 const ReadCallback& callback) const |
114 { | 116 { |
115 throw std::runtime_error("Not implemented"); | 117 throw std::runtime_error("Not implemented"); |
116 } | 118 } |
117 | 119 |
118 void Write(const std::string& path, std::istream& content) | 120 void Write(const std::string& path, const IOBuffer& content) |
119 { | 121 { |
120 throw std::runtime_error("Not implemented"); | 122 throw std::runtime_error("Not implemented"); |
121 } | 123 } |
122 void Write(const std::string& path, std::istream& data, | 124 void Write(const std::string& path, const IOBuffer& data, |
123 const Callback& callback) | 125 const Callback& callback) |
124 { | 126 { |
125 throw std::runtime_error("Not implemented"); | 127 throw std::runtime_error("Not implemented"); |
126 } | 128 } |
127 | 129 |
128 void Move(const std::string& fromPath, const std::string& toPath) | 130 void Move(const std::string& fromPath, const std::string& toPath) |
129 { | 131 { |
130 throw std::runtime_error("Not implemented"); | 132 throw std::runtime_error("Not implemented"); |
131 } | 133 } |
132 void Move(const std::string& fromPath, const std::string& toPath, | 134 void Move(const std::string& fromPath, const std::string& toPath, |
(...skipping 18 matching lines...) Expand all Loading... |
151 void Stat(const std::string& path, | 153 void Stat(const std::string& path, |
152 const StatCallback& callback) const | 154 const StatCallback& callback) const |
153 { | 155 { |
154 throw std::runtime_error("Not implemented"); | 156 throw std::runtime_error("Not implemented"); |
155 } | 157 } |
156 | 158 |
157 std::string Resolve(const std::string& path) const | 159 std::string Resolve(const std::string& path) const |
158 { | 160 { |
159 throw std::runtime_error("Not implemented"); | 161 throw std::runtime_error("Not implemented"); |
160 } | 162 } |
161 void Resolve(const std::string& path, | |
162 const ResolveCallback& callback) const | |
163 { | |
164 throw std::runtime_error("Not implemented"); | |
165 } | |
166 }; | 163 }; |
167 | 164 |
168 class ThrowingWebRequest : public AdblockPlus::IWebRequest | 165 class ThrowingWebRequest : public AdblockPlus::IWebRequest |
169 { | 166 { |
170 public: | 167 public: |
171 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 |
172 { | 169 { |
173 throw std::runtime_error("Unexpected GET: " + url); | 170 throw std::runtime_error("Unexpected GET: " + url); |
174 } | 171 } |
175 }; | 172 }; |
176 | 173 |
177 class LazyFileSystem : public AdblockPlus::FileSystem | 174 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File
System |
178 { | 175 { |
179 public: | 176 public: |
180 std::shared_ptr<std::istream> Read(const std::string& path) const | 177 IOBuffer Read(const std::string& path) const |
181 { | 178 { |
182 std::string dummyData(""); | 179 std::string dummyData(""); |
183 if (path == "patterns.ini") | 180 if (path == "patterns.ini") |
184 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; | 181 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; |
185 else if (path == "prefs.json") | 182 else if (path == "prefs.json") |
186 dummyData = "{}"; | 183 dummyData = "{}"; |
187 return std::shared_ptr<std::istream>(new std::istringstream(dummyData)); | 184 return IOBuffer(dummyData.cbegin(), dummyData.cend()); |
188 } | 185 } |
189 | 186 |
190 void Read(const std::string& path, const ReadCallback& callback) const | 187 void Read(const std::string& path, const ReadCallback& callback) const |
191 { | 188 { |
192 } | 189 std::thread([this, path, callback] |
193 | 190 { |
194 void Write(const std::string& path, std::istream& content) | 191 auto data = Read(path); |
195 { | 192 callback(std::move(data), ""); |
196 } | 193 }).detach(); |
197 | 194 } |
198 void Write(const std::string& path, std::istream& data, | 195 |
| 196 void Write(const std::string& path, const IOBuffer& content) |
| 197 { |
| 198 } |
| 199 |
| 200 void Write(const std::string& path, const IOBuffer& data, |
199 const Callback& callback) | 201 const Callback& callback) |
200 { | 202 { |
| 203 std::thread([this, path, data, callback] |
| 204 { |
| 205 Write(path, data); |
| 206 callback(""); |
| 207 }).detach(); |
201 } | 208 } |
202 | 209 |
203 void Move(const std::string& fromPath, const std::string& toPath) | 210 void Move(const std::string& fromPath, const std::string& toPath) |
204 { | 211 { |
205 } | 212 } |
206 | 213 |
207 void Move(const std::string& fromPath, const std::string& toPath, | 214 void Move(const std::string& fromPath, const std::string& toPath, |
208 const Callback& callback) | 215 const Callback& callback) |
209 { | 216 { |
| 217 std::thread([this, fromPath, toPath, callback] |
| 218 { |
| 219 Move(fromPath, toPath); |
| 220 callback(""); |
| 221 }).detach(); |
210 } | 222 } |
211 | 223 |
212 void Remove(const std::string& path) | 224 void Remove(const std::string& path) |
213 { | 225 { |
214 } | 226 } |
215 | 227 |
216 void Remove(const std::string& path, const Callback& callback) | 228 void Remove(const std::string& path, const Callback& callback) |
217 { | 229 { |
| 230 std::thread([this, path, callback] |
| 231 { |
| 232 Remove(path); |
| 233 callback(""); |
| 234 }).detach(); |
218 } | 235 } |
219 | 236 |
220 StatResult Stat(const std::string& path) const | 237 StatResult Stat(const std::string& path) const |
221 { | 238 { |
222 StatResult result; | 239 StatResult result; |
223 if (path == "patterns.ini") | 240 if (path == "patterns.ini") |
224 { | 241 { |
225 result.exists = true; | 242 result.exists = true; |
226 result.isFile = true; | 243 result.isFile = true; |
227 } | 244 } |
228 return result; | 245 return result; |
229 } | 246 } |
230 | 247 |
231 void Stat(const std::string& path, | 248 void Stat(const std::string& path, const StatCallback& callback) const |
232 const StatCallback& callback) const | 249 { |
233 { | 250 std::thread([this, path, callback] |
| 251 { |
| 252 callback(Stat(path), ""); |
| 253 }).detach(); |
234 } | 254 } |
235 | 255 |
236 std::string Resolve(const std::string& path) const | 256 std::string Resolve(const std::string& path) const |
237 { | 257 { |
238 return path; | 258 return path; |
239 } | |
240 | |
241 void Resolve(const std::string& path, | |
242 const ResolveCallback& callback) const | |
243 { | |
244 } | 259 } |
245 }; | 260 }; |
246 | 261 |
247 class NoopWebRequest : public AdblockPlus::IWebRequest | 262 class NoopWebRequest : public AdblockPlus::IWebRequest |
248 { | 263 { |
249 public: | 264 public: |
250 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 |
251 { | 266 { |
252 } | 267 } |
253 }; | 268 }; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 protected: | 311 protected: |
297 AdblockPlus::JsEnginePtr jsEngine; | 312 AdblockPlus::JsEnginePtr jsEngine; |
298 | 313 |
299 virtual void SetUp() | 314 virtual void SetUp() |
300 { | 315 { |
301 jsEngine = CreateJsEngine(); | 316 jsEngine = CreateJsEngine(); |
302 } | 317 } |
303 }; | 318 }; |
304 | 319 |
305 #endif | 320 #endif |
LEFT | RIGHT |