OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 19 matching lines...) Expand all Loading... | |
30 | 30 |
31 #ifndef TestingPlatformSupport_h | 31 #ifndef TestingPlatformSupport_h |
32 #define TestingPlatformSupport_h | 32 #define TestingPlatformSupport_h |
33 | 33 |
34 #include "platform/PlatformExport.h" | 34 #include "platform/PlatformExport.h" |
35 #include "platform/WebTaskRunner.h" | 35 #include "platform/WebTaskRunner.h" |
36 #include "public/platform/Platform.h" | 36 #include "public/platform/Platform.h" |
37 #include "public/platform/WebCompositorSupport.h" | 37 #include "public/platform/WebCompositorSupport.h" |
38 #include "public/platform/WebScheduler.h" | 38 #include "public/platform/WebScheduler.h" |
39 #include "public/platform/WebThread.h" | 39 #include "public/platform/WebThread.h" |
40 #include "wtf/Assertions.h" | |
40 #include "wtf/Vector.h" | 41 #include "wtf/Vector.h" |
41 #include <memory> | 42 #include <memory> |
42 | 43 |
43 namespace base { | 44 namespace base { |
44 class SimpleTestTickClock; | 45 class SimpleTestTickClock; |
45 class TestDiscardableMemoryAllocator; | 46 class TestDiscardableMemoryAllocator; |
46 } | 47 } |
47 | 48 |
48 namespace cc { | 49 namespace cc { |
49 class OrderedSimpleTaskRunner; | 50 class OrderedSimpleTaskRunner; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 InterventionReporter*, | 101 InterventionReporter*, |
101 WebViewScheduler::WebViewSchedulerSettings*) override { | 102 WebViewScheduler::WebViewSchedulerSettings*) override { |
102 return nullptr; | 103 return nullptr; |
103 } | 104 } |
104 void suspendTimerQueue() override {} | 105 void suspendTimerQueue() override {} |
105 void resumeTimerQueue() override {} | 106 void resumeTimerQueue() override {} |
106 void addPendingNavigation(WebScheduler::NavigatingFrameType) override {} | 107 void addPendingNavigation(WebScheduler::NavigatingFrameType) override {} |
107 void removePendingNavigation(WebScheduler::NavigatingFrameType) override {} | 108 void removePendingNavigation(WebScheduler::NavigatingFrameType) override {} |
108 }; | 109 }; |
109 | 110 |
111 // A base class to override Platform methods for testing. You can override | |
112 // compositorSupport() method via a TestingPlatformSupport::Config parameter. | |
113 // Other virtual Platform methods that are overriden in this class will call | |
114 // the original Platform methods that can be obtained by Platform::current() | |
115 // in the consturctor. | |
116 // Or you may to consider to implement your own inheritance of this class to | |
117 // override arbitorary virtual methods. | |
118 // You should use ScopedTestingPlatformSupport to hold any instance of this | |
119 // class and inheritances in order to setup and shutdown Platform instance | |
120 // correctly. See also comments on ScopedTestingPlatformSupport. | |
110 class TestingPlatformSupport : public Platform { | 121 class TestingPlatformSupport : public Platform { |
111 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); | 122 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); |
112 | 123 |
113 public: | 124 public: |
114 struct Config { | 125 struct Config { |
115 WebCompositorSupport* compositorSupport = nullptr; | 126 WebCompositorSupport* compositorSupport = nullptr; |
116 }; | 127 }; |
117 | 128 |
118 TestingPlatformSupport(); | 129 TestingPlatformSupport(); |
119 explicit TestingPlatformSupport(const Config&); | 130 explicit TestingPlatformSupport(const Config&); |
(...skipping 15 matching lines...) Expand all Loading... | |
135 InterfaceProvider* interfaceProvider() override; | 146 InterfaceProvider* interfaceProvider() override; |
136 | 147 |
137 protected: | 148 protected: |
138 class TestingInterfaceProvider; | 149 class TestingInterfaceProvider; |
139 | 150 |
140 const Config m_config; | 151 const Config m_config; |
141 Platform* const m_oldPlatform; | 152 Platform* const m_oldPlatform; |
142 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; | 153 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; |
143 }; | 154 }; |
144 | 155 |
156 // This class adds mocked scheduler support to TestingPlatformSupport. See also | |
157 // comments on TestingPlatformSupport to use this class correctly. | |
145 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { | 158 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { |
146 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); | 159 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); |
147 | 160 |
148 public: | 161 public: |
149 TestingPlatformSupportWithMockScheduler(); | 162 TestingPlatformSupportWithMockScheduler(); |
150 explicit TestingPlatformSupportWithMockScheduler(const Config&); | 163 explicit TestingPlatformSupportWithMockScheduler(const Config&); |
151 ~TestingPlatformSupportWithMockScheduler() override; | 164 ~TestingPlatformSupportWithMockScheduler() override; |
152 | 165 |
153 // Platform: | 166 // Platform: |
154 WebThread* currentThread() override; | 167 WebThread* currentThread() override; |
(...skipping 26 matching lines...) Expand all Loading... | |
181 | 194 |
182 protected: | 195 protected: |
183 static double getTestTime(); | 196 static double getTestTime(); |
184 | 197 |
185 std::unique_ptr<base::SimpleTestTickClock> m_clock; | 198 std::unique_ptr<base::SimpleTestTickClock> m_clock; |
186 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner; | 199 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner; |
187 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler; | 200 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler; |
188 std::unique_ptr<WebThread> m_thread; | 201 std::unique_ptr<WebThread> m_thread; |
189 }; | 202 }; |
190 | 203 |
204 // A template class to hold an instance of TestingPlatformSupport class and | |
205 // inheritances. This class also sets the Platform inheritance to be available | |
206 // through Platform::current(), and safely reset it to the previous instance | |
207 // on destructing the instance. This instance could be nested. | |
208 // | |
209 // Usage: | |
210 // | |
211 // #include "wtf/PtrUtil.h" | |
212 // | |
213 // TEST_F(SampleTest, sampleTest) { | |
214 // ScopedTestingPlatformSupport<MyTestingPlatformSupport> platform( | |
215 // WTF::makeUnique<MyTestingPlatformSupport>(...)); | |
216 // | |
217 // // Or can set an instance later. | |
218 // platform.reset(WTF::makeUnique<MyTestingPlatformSupport>(...)); | |
219 // ... | |
220 // // Can call methods of MyTestingPlatformSupport. | |
221 // EXPECT_TRUE(platform->myMethodIsCalled()); | |
222 // } | |
223 // | |
224 // Note: A valid Platform instance should be already set before setting a | |
225 // TestingPlatformSupport instance, and the original instance should outlive. | |
226 // These conditions should be always true in usual use cases, but if you are | |
227 // changing ScopedUnittestsEnvironmentSetup or something like that, you should | |
228 // keep these restrictions in mind. | |
229 template <class T> | |
230 class ScopedTestingPlatformSupport { | |
231 public: | |
232 ScopedTestingPlatformSupport() : ScopedTestingPlatformSupport(nullptr) {} | |
233 explicit ScopedTestingPlatformSupport( | |
234 std::unique_ptr<T> testingPlatformSupport) | |
235 : m_originalPlatform(nullptr) { | |
236 reset(std::move(testingPlatformSupport)); | |
237 } | |
238 void reset(std::unique_ptr<T> testingPlatformSupport) { | |
haraken
2017/01/11 13:03:38
This method is getting a bit too complicated.
May
Takashi Toyoshima
2017/01/12 08:56:13
Original intention of this complicated logic was t
| |
239 // Try obtaining a valid Platform instance to restore a valid instance | |
240 // always. | |
241 if (!m_originalPlatform) | |
242 m_originalPlatform = Platform::current(); | |
243 | |
244 // To set a TestingPlatformSupport instance, |m_originalPlatform| should be | |
245 // valid so that a valid instance could be always restored. | |
246 DCHECK(!testingPlatformSupport || m_originalPlatform); | |
247 | |
248 // The old instance should be deleted before setting new Platform instance | |
249 // so to check if the TestingPlatformSupport was registered correctly in | |
250 // the TestingPlatformSupport's destructor. | |
251 m_testingPlatformSupport = std::move(testingPlatformSupport); | |
252 | |
253 // To avoid calling setCurrentPlatformForTesting() with nullptr, do nothing | |
254 // if Platform::current() is still nullptr, and |testingPlatformSupport| is | |
255 // also nullptr. This may happen for stack-allocated instances. | |
256 if (!m_testingPlatformSupport && !m_originalPlatform) | |
257 return; | |
258 | |
259 Platform::setCurrentPlatformForTesting(m_testingPlatformSupport | |
260 ? m_testingPlatformSupport.get() | |
261 : m_originalPlatform); | |
262 } | |
263 ~ScopedTestingPlatformSupport() { reset(nullptr); } | |
264 const T* operator->() const { return m_testingPlatformSupport.get(); } | |
265 T* operator->() { return m_testingPlatformSupport.get(); } | |
266 | |
267 private: | |
268 Platform* m_originalPlatform; | |
269 std::unique_ptr<T> m_testingPlatformSupport; | |
270 }; | |
271 | |
191 class ScopedUnittestsEnvironmentSetup { | 272 class ScopedUnittestsEnvironmentSetup { |
192 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); | 273 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); |
193 | 274 |
194 public: | 275 public: |
195 ScopedUnittestsEnvironmentSetup(int argc, char** argv); | 276 ScopedUnittestsEnvironmentSetup(int argc, char** argv); |
196 ~ScopedUnittestsEnvironmentSetup(); | 277 ~ScopedUnittestsEnvironmentSetup(); |
197 | 278 |
198 private: | 279 private: |
199 class DummyPlatform; | 280 class DummyPlatform; |
200 std::unique_ptr<base::TestDiscardableMemoryAllocator> | 281 std::unique_ptr<base::TestDiscardableMemoryAllocator> |
201 m_discardableMemoryAllocator; | 282 m_discardableMemoryAllocator; |
202 std::unique_ptr<DummyPlatform> m_platform; | 283 std::unique_ptr<DummyPlatform> m_platform; |
203 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; | 284 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; |
204 TestingPlatformSupport::Config m_testingPlatformConfig; | 285 TestingPlatformSupport::Config m_testingPlatformConfig; |
205 std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport; | 286 ScopedTestingPlatformSupport<TestingPlatformSupport> m_testingPlatformSupport; |
Takashi Toyoshima
2017/01/12 08:56:13
Unfortunately, we can not use ScopedTestingPlatfor
| |
206 }; | 287 }; |
207 | 288 |
208 } // namespace blink | 289 } // namespace blink |
209 | 290 |
210 #endif // TestingPlatformSupport_h | 291 #endif // TestingPlatformSupport_h |
OLD | NEW |