| Left: | ||
| Right: |
| 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> | 21 #include <thread> |
| 22 | 22 |
| 23 #include <AdblockPlus.h> | 23 #include <AdblockPlus.h> |
| 24 #include <gtest/gtest.h> | 24 #include <gtest/gtest.h> |
| 25 #include "../src/Thread.h" | 25 #include "../src/Thread.h" |
| 26 | 26 |
| 27 class Sync | |
|
sergei
2017/06/16 15:05:55
There is a pretty similar class in src/FilterEngin
hub
2017/06/16 21:52:54
That's where it comes from.
The problem with Util
sergei
2017/07/03 09:25:53
Acknowledged.
| |
| 28 { | |
| 29 public: | |
| 30 Sync() | |
| 31 : set(false) | |
| 32 { | |
| 33 } | |
| 34 void Wait(int sec = 20) | |
|
sergei
2017/06/16 15:05:55
For the class in utils I think it makes sense to u
hub
2017/06/16 21:52:55
At that point I'd that have Wait() to have a std::
| |
| 35 { | |
| 36 std::unique_lock<std::mutex> lock(mutex); | |
| 37 while (!set) | |
| 38 cv.wait_for(lock, std::chrono::seconds(sec)); | |
| 39 } | |
| 40 void Set(const std::string& error_ = std::string()) | |
|
sergei
2017/06/16 15:05:55
unfortunately we are not using anything like m_ pr
hub
2017/06/16 21:52:55
This is actually for -Wshadow that sadly isn't ena
| |
| 41 { | |
| 42 { | |
| 43 std::unique_lock<std::mutex> lock(mutex); | |
| 44 set = true; | |
| 45 error = error_; | |
| 46 } | |
| 47 cv.notify_all(); | |
| 48 } | |
| 49 std::string GetError() | |
| 50 { | |
| 51 std::unique_lock<std::mutex> lock(mutex); | |
| 52 return error; | |
| 53 } | |
| 54 private: | |
| 55 std::mutex mutex; | |
| 56 std::condition_variable cv; | |
| 57 bool set; | |
| 58 std::string error; | |
| 59 }; | |
| 60 | |
| 61 // 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 |
| 62 // 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 |
| 63 // 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 |
| 64 // actually needs is the access to pending tasks. Therefore instantiation of | 30 // actually needs is the access to pending tasks. Therefore instantiation of |
| 65 // implemenation of an interface, creation of shared tasks and sharing them | 31 // implemenation of an interface, creation of shared tasks and sharing them |
| 66 // with tasks in test is located in this class. | 32 // with tasks in test is located in this class. |
| 67 // | 33 // |
| 68 // Task is passed as an additional template parameter instead of using traits | 34 // Task is passed as an additional template parameter instead of using traits |
| 69 // (CRTP does not work with types in derived class) merely to simplify the code | 35 // (CRTP does not work with types in derived class) merely to simplify the code |
| 70 // by minimization. | 36 // by minimization. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 void operator()(LogLevel logLevel, const std::string& message, | 100 void operator()(LogLevel logLevel, const std::string& message, |
| 135 const std::string& source) | 101 const std::string& source) |
| 136 { | 102 { |
| 137 throw std::runtime_error("Unexpected error: " + message); | 103 throw std::runtime_error("Unexpected error: " + message); |
| 138 } | 104 } |
| 139 }; | 105 }; |
| 140 | 106 |
| 141 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem | 107 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem |
| 142 { | 108 { |
| 143 public: | 109 public: |
| 144 std::shared_ptr<std::istream> Read(const std::string& path) const | 110 IOBuffer Read(const std::string& path) const |
| 145 { | 111 { |
| 146 throw std::runtime_error("Not implemented"); | 112 throw std::runtime_error("Not implemented"); |
| 147 } | 113 } |
| 148 void Read(const std::string& path, | 114 void Read(const std::string& path, |
| 149 const ReadCallback& callback) const | 115 const ReadCallback& callback) const |
| 150 { | 116 { |
| 151 throw std::runtime_error("Not implemented"); | 117 throw std::runtime_error("Not implemented"); |
| 152 } | 118 } |
| 153 | 119 |
| 154 void Write(const std::string& path, std::istream& content) | 120 void Write(const std::string& path, const IOBuffer& content) |
| 155 { | 121 { |
| 156 throw std::runtime_error("Not implemented"); | 122 throw std::runtime_error("Not implemented"); |
| 157 } | 123 } |
| 158 void Write(const std::string& path, std::shared_ptr<std::istream> data, | 124 void Write(const std::string& path, const IOBuffer& data, |
| 159 const Callback& callback) | 125 const Callback& callback) |
| 160 { | 126 { |
| 161 throw std::runtime_error("Not implemented"); | 127 throw std::runtime_error("Not implemented"); |
| 162 } | 128 } |
| 163 | 129 |
| 164 void Move(const std::string& fromPath, const std::string& toPath) | 130 void Move(const std::string& fromPath, const std::string& toPath) |
| 165 { | 131 { |
| 166 throw std::runtime_error("Not implemented"); | 132 throw std::runtime_error("Not implemented"); |
| 167 } | 133 } |
| 168 void Move(const std::string& fromPath, const std::string& toPath, | 134 void Move(const std::string& fromPath, const std::string& toPath, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 public: | 167 public: |
| 202 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 |
| 203 { | 169 { |
| 204 throw std::runtime_error("Unexpected GET: " + url); | 170 throw std::runtime_error("Unexpected GET: " + url); |
| 205 } | 171 } |
| 206 }; | 172 }; |
| 207 | 173 |
| 208 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System | 174 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System |
| 209 { | 175 { |
| 210 public: | 176 public: |
| 211 std::shared_ptr<std::istream> Read(const std::string& path) const | 177 IOBuffer Read(const std::string& path) const |
| 212 { | 178 { |
| 213 std::string dummyData(""); | 179 std::string dummyData(""); |
| 214 if (path == "patterns.ini") | 180 if (path == "patterns.ini") |
| 215 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; | 181 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; |
| 216 else if (path == "prefs.json") | 182 else if (path == "prefs.json") |
| 217 dummyData = "{}"; | 183 dummyData = "{}"; |
| 218 return std::shared_ptr<std::istream>(new std::istringstream(dummyData)); | 184 return IOBuffer(dummyData.cbegin(), dummyData.cend()); |
| 219 } | 185 } |
| 220 | 186 |
| 221 void Read(const std::string& path, const ReadCallback& callback) const | 187 void Read(const std::string& path, const ReadCallback& callback) const |
| 222 { | 188 { |
| 223 std::thread([this, path, callback] | 189 std::thread([this, path, callback] |
|
sergei
2017/06/16 15:05:55
I actually planned to gradually convert to a speci
hub
2017/06/16 21:52:55
Acknowledged.
| |
| 224 { | 190 { |
| 225 auto result = Read(path); | 191 auto data = Read(path); |
| 226 std::stringstream stream; | 192 callback(std::move(data), ""); |
| 227 stream << result->rdbuf(); | 193 }).detach(); |
| 228 std::string content = stream.str(); | 194 } |
| 229 callback(std::move(content), ""); | 195 |
| 230 }).detach(); | 196 void Write(const std::string& path, const IOBuffer& content) |
| 231 } | 197 { |
| 232 | 198 } |
| 233 void Write(const std::string& path, std::istream& content) | 199 |
| 234 { | 200 void Write(const std::string& path, const IOBuffer& data, |
| 235 } | |
| 236 | |
| 237 void Write(const std::string& path, std::shared_ptr<std::istream> data, | |
| 238 const Callback& callback) | 201 const Callback& callback) |
| 239 { | 202 { |
| 240 std::thread([this, path, data, callback] | 203 std::thread([this, path, data, callback] |
| 241 { | 204 { |
| 242 Write(path, *data); | 205 Write(path, data); |
| 243 callback(""); | 206 callback(""); |
| 244 }).detach(); | 207 }).detach(); |
| 245 } | 208 } |
| 246 | 209 |
| 247 void Move(const std::string& fromPath, const std::string& toPath) | 210 void Move(const std::string& fromPath, const std::string& toPath) |
| 248 { | 211 { |
| 249 } | 212 } |
| 250 | 213 |
| 251 void Move(const std::string& fromPath, const std::string& toPath, | 214 void Move(const std::string& fromPath, const std::string& toPath, |
| 252 const Callback& callback) | 215 const Callback& callback) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 protected: | 311 protected: |
| 349 AdblockPlus::JsEnginePtr jsEngine; | 312 AdblockPlus::JsEnginePtr jsEngine; |
| 350 | 313 |
| 351 virtual void SetUp() | 314 virtual void SetUp() |
| 352 { | 315 { |
| 353 jsEngine = CreateJsEngine(); | 316 jsEngine = CreateJsEngine(); |
| 354 } | 317 } |
| 355 }; | 318 }; |
| 356 | 319 |
| 357 #endif | 320 #endif |
| LEFT | RIGHT |