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