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 <AdblockPlus.h> | 21 #include <AdblockPlus.h> |
22 #include <gtest/gtest.h> | 22 #include <gtest/gtest.h> |
23 #include "../src/Thread.h" | 23 #include "../src/Thread.h" |
24 | 24 |
| 25 // 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 |
| 27 // 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 |
| 29 // implemenation of an interface, creation of shared tasks and sharing them |
| 30 // with tasks in test is located in this class. |
| 31 // |
| 32 // 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 |
| 34 // by minimization. |
| 35 template<typename T, typename TTask, typename Interface> |
| 36 class DelayedMixin : public Interface |
| 37 { |
| 38 public: |
| 39 typedef TTask Task; |
| 40 typedef std::shared_ptr<std::list<Task>> SharedTasks; |
| 41 static std::unique_ptr<Interface> New(SharedTasks& tasks) |
| 42 { |
| 43 std::unique_ptr<T> result(new T()); |
| 44 tasks = result->tasks; |
| 45 return std::move(result); |
| 46 } |
| 47 protected: |
| 48 DelayedMixin() |
| 49 : tasks(std::make_shared<std::list<Task>>()) |
| 50 { |
| 51 } |
| 52 |
| 53 SharedTasks tasks; |
| 54 }; |
| 55 |
25 class ThrowingTimer : public AdblockPlus::ITimer | 56 class ThrowingTimer : public AdblockPlus::ITimer |
26 { | 57 { |
27 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& t
imerCallback) override | 58 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& t
imerCallback) override |
28 { | 59 { |
29 throw std::runtime_error("Unexpected timer: " + std::to_string(timeout.count
())); | 60 throw std::runtime_error("Unexpected timer: " + std::to_string(timeout.count
())); |
30 } | 61 } |
31 }; | 62 }; |
32 | 63 |
33 class NoopTimer : public AdblockPlus::ITimer | 64 class NoopTimer : public AdblockPlus::ITimer |
34 { | 65 { |
35 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& t
imerCallback) override | 66 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& t
imerCallback) override |
36 { | 67 { |
37 } | 68 } |
38 }; | 69 }; |
39 | 70 |
| 71 struct DelayedTimerTask |
| 72 { |
| 73 std::chrono::milliseconds timeout; |
| 74 AdblockPlus::ITimer::TimerCallback callback; |
| 75 }; |
| 76 |
| 77 class DelayedTimer : public DelayedMixin<DelayedTimer, DelayedTimerTask, Adblock
Plus::ITimer> |
| 78 { |
| 79 public: |
| 80 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& t
imerCallback) override |
| 81 { |
| 82 Task task = { timeout, timerCallback }; |
| 83 tasks->emplace_back(task); |
| 84 } |
| 85 |
| 86 // JS part often schedules download requests using Utils.runAsync which calls |
| 87 // setTimeout(callback, 0). So, we need to firstly process those timers |
| 88 // to actually schedule web requests and afterwards we may inspect pending |
| 89 // web requests. |
| 90 // non-immeditate timers are not touched |
| 91 static void ProcessImmediateTimers(DelayedTimer::SharedTasks& timerTasks); |
| 92 }; |
| 93 |
| 94 |
40 class ThrowingLogSystem : public AdblockPlus::LogSystem | 95 class ThrowingLogSystem : public AdblockPlus::LogSystem |
41 { | 96 { |
42 public: | 97 public: |
43 void operator()(LogLevel logLevel, const std::string& message, | 98 void operator()(LogLevel logLevel, const std::string& message, |
44 const std::string& source) | 99 const std::string& source) |
45 { | 100 { |
46 throw std::runtime_error("Unexpected error: " + message); | 101 throw std::runtime_error("Unexpected error: " + message); |
47 } | 102 } |
48 }; | 103 }; |
49 | 104 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 }; | 204 }; |
150 | 205 |
151 class NoopWebRequest : public AdblockPlus::IWebRequest | 206 class NoopWebRequest : public AdblockPlus::IWebRequest |
152 { | 207 { |
153 public: | 208 public: |
154 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders
, const GetCallback& callback) override | 209 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders
, const GetCallback& callback) override |
155 { | 210 { |
156 } | 211 } |
157 }; | 212 }; |
158 | 213 |
| 214 struct DelayedWebRequestTask |
| 215 { |
| 216 std::string url; |
| 217 AdblockPlus::HeaderList headers; |
| 218 AdblockPlus::IWebRequest::GetCallback getCallback; |
| 219 }; |
| 220 |
| 221 class DelayedWebRequest : public DelayedMixin<DelayedWebRequest, DelayedWebReque
stTask, AdblockPlus::IWebRequest> |
| 222 { |
| 223 public: |
| 224 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders
, const GetCallback& callback) override |
| 225 { |
| 226 Task task = { url, requestHeaders, callback }; |
| 227 tasks->emplace_back(task); |
| 228 } |
| 229 }; |
| 230 |
159 class LazyLogSystem : public AdblockPlus::LogSystem | 231 class LazyLogSystem : public AdblockPlus::LogSystem |
160 { | 232 { |
161 public: | 233 public: |
162 void operator()(LogLevel logLevel, const std::string& message, | 234 void operator()(LogLevel logLevel, const std::string& message, |
163 const std::string& source) | 235 const std::string& source) |
164 { | 236 { |
165 } | 237 } |
166 }; | 238 }; |
167 | 239 |
168 struct JsEngineCreationParameters | 240 struct JsEngineCreationParameters |
(...skipping 14 matching lines...) Expand all Loading... |
183 protected: | 255 protected: |
184 AdblockPlus::JsEnginePtr jsEngine; | 256 AdblockPlus::JsEnginePtr jsEngine; |
185 | 257 |
186 virtual void SetUp() | 258 virtual void SetUp() |
187 { | 259 { |
188 jsEngine = CreateJsEngine(); | 260 jsEngine = CreateJsEngine(); |
189 } | 261 } |
190 }; | 262 }; |
191 | 263 |
192 #endif | 264 #endif |
OLD | NEW |