OLD | NEW |
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 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::ostream& content) |
| 121 { |
| 122 throw std::runtime_error("Not implemented"); |
| 123 } |
| 124 void Write(const std::string& path, const std::vector<char>& 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 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 |
162 void Write(const std::string& path, std::istream& content) | 187 void Read(const std::string& path, const ReadCallback& callback) const |
163 { | 188 { |
| 189 std::thread([this, path, callback] |
| 190 { |
| 191 auto is = Read(path); |
| 192 is->seekg(0, std::ios_base::end); |
| 193 auto dataSize = is->tellg(); |
| 194 is->seekg(0, std::ios_base::beg); |
| 195 |
| 196 std::vector<char> data(dataSize); |
| 197 is->read(data.data(), data.size()); |
| 198 callback(std::move(data), ""); |
| 199 }).detach(); |
| 200 } |
| 201 |
| 202 void Write(const std::string& path, std::ostream& content) |
| 203 { |
| 204 } |
| 205 |
| 206 void Write(const std::string& path, const std::vector<char>& data, |
| 207 const Callback& callback) |
| 208 { |
| 209 std::thread([this, path, data, callback] |
| 210 { |
| 211 std::stringstream stream; |
| 212 stream.write(data.data(), data.size()); |
| 213 Write(path, stream); |
| 214 callback(""); |
| 215 }).detach(); |
164 } | 216 } |
165 | 217 |
166 void Move(const std::string& fromPath, const std::string& toPath) | 218 void Move(const std::string& fromPath, const std::string& toPath) |
167 { | 219 { |
168 } | 220 } |
169 | 221 |
| 222 void Move(const std::string& fromPath, const std::string& toPath, |
| 223 const Callback& callback) |
| 224 { |
| 225 std::thread([this, fromPath, toPath, callback] |
| 226 { |
| 227 Move(fromPath, toPath); |
| 228 callback(""); |
| 229 }).detach(); |
| 230 } |
| 231 |
170 void Remove(const std::string& path) | 232 void Remove(const std::string& path) |
171 { | 233 { |
172 } | 234 } |
173 | 235 |
| 236 void Remove(const std::string& path, const Callback& callback) |
| 237 { |
| 238 std::thread([this, path, callback] |
| 239 { |
| 240 Remove(path); |
| 241 callback(""); |
| 242 }).detach(); |
| 243 } |
| 244 |
174 StatResult Stat(const std::string& path) const | 245 StatResult Stat(const std::string& path) const |
175 { | 246 { |
176 StatResult result; | 247 StatResult result; |
177 if (path == "patterns.ini") | 248 if (path == "patterns.ini") |
178 { | 249 { |
179 result.exists = true; | 250 result.exists = true; |
180 result.isFile = true; | 251 result.isFile = true; |
181 } | 252 } |
182 return result; | 253 return result; |
183 } | 254 } |
184 | 255 |
| 256 void Stat(const std::string& path, const StatCallback& callback) const |
| 257 { |
| 258 std::thread([this, path, callback] |
| 259 { |
| 260 callback(Stat(path), ""); |
| 261 }).detach(); |
| 262 } |
| 263 |
185 std::string Resolve(const std::string& path) const | 264 std::string Resolve(const std::string& path) const |
186 { | 265 { |
187 return path; | 266 return path; |
188 } | 267 } |
189 }; | 268 }; |
190 | 269 |
191 class NoopWebRequest : public AdblockPlus::IWebRequest | 270 class NoopWebRequest : public AdblockPlus::IWebRequest |
192 { | 271 { |
193 public: | 272 public: |
194 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders
, const GetCallback& callback) override | 273 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 Loading... |
240 protected: | 319 protected: |
241 AdblockPlus::JsEnginePtr jsEngine; | 320 AdblockPlus::JsEnginePtr jsEngine; |
242 | 321 |
243 virtual void SetUp() | 322 virtual void SetUp() |
244 { | 323 { |
245 jsEngine = CreateJsEngine(); | 324 jsEngine = CreateJsEngine(); |
246 } | 325 } |
247 }; | 326 }; |
248 | 327 |
249 #endif | 328 #endif |
OLD | NEW |