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/Allocator.h" |
| 41 #include "wtf/Assertions.h" |
| 42 #include "wtf/PtrUtil.h" |
40 #include "wtf/Vector.h" | 43 #include "wtf/Vector.h" |
41 #include <memory> | 44 #include <memory> |
| 45 #include <utility> |
42 | 46 |
43 namespace base { | 47 namespace base { |
44 class SimpleTestTickClock; | 48 class SimpleTestTickClock; |
45 class TestDiscardableMemoryAllocator; | 49 class TestDiscardableMemoryAllocator; |
46 } | 50 } |
47 | 51 |
48 namespace cc { | 52 namespace cc { |
49 class OrderedSimpleTaskRunner; | 53 class OrderedSimpleTaskRunner; |
50 } | 54 } |
51 | 55 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 InterventionReporter*, | 104 InterventionReporter*, |
101 WebViewScheduler::WebViewSchedulerSettings*) override { | 105 WebViewScheduler::WebViewSchedulerSettings*) override { |
102 return nullptr; | 106 return nullptr; |
103 } | 107 } |
104 void suspendTimerQueue() override {} | 108 void suspendTimerQueue() override {} |
105 void resumeTimerQueue() override {} | 109 void resumeTimerQueue() override {} |
106 void addPendingNavigation(WebScheduler::NavigatingFrameType) override {} | 110 void addPendingNavigation(WebScheduler::NavigatingFrameType) override {} |
107 void removePendingNavigation(WebScheduler::NavigatingFrameType) override {} | 111 void removePendingNavigation(WebScheduler::NavigatingFrameType) override {} |
108 }; | 112 }; |
109 | 113 |
| 114 // A base class to override Platform methods for testing. You can override the |
| 115 // behavior by subclassing TestingPlatformSupport or using |
| 116 // ScopedTestingPlatformSupport (see below). |
110 class TestingPlatformSupport : public Platform { | 117 class TestingPlatformSupport : public Platform { |
111 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); | 118 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); |
112 | 119 |
113 public: | 120 public: |
114 struct Config { | 121 struct Config { |
115 WebCompositorSupport* compositorSupport = nullptr; | 122 WebCompositorSupport* compositorSupport = nullptr; |
116 }; | 123 }; |
117 | 124 |
118 TestingPlatformSupport(); | 125 TestingPlatformSupport(); |
119 explicit TestingPlatformSupport(const Config&); | 126 explicit TestingPlatformSupport(const Config&); |
(...skipping 15 matching lines...) Expand all Loading... |
135 InterfaceProvider* interfaceProvider() override; | 142 InterfaceProvider* interfaceProvider() override; |
136 | 143 |
137 protected: | 144 protected: |
138 class TestingInterfaceProvider; | 145 class TestingInterfaceProvider; |
139 | 146 |
140 const Config m_config; | 147 const Config m_config; |
141 Platform* const m_oldPlatform; | 148 Platform* const m_oldPlatform; |
142 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; | 149 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; |
143 }; | 150 }; |
144 | 151 |
| 152 // This class adds mocked scheduler support to TestingPlatformSupport. See also |
| 153 // ScopedTestingPlatformSupport to use this class correctly. |
145 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { | 154 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { |
146 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); | 155 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); |
147 | 156 |
148 public: | 157 public: |
149 TestingPlatformSupportWithMockScheduler(); | 158 TestingPlatformSupportWithMockScheduler(); |
150 explicit TestingPlatformSupportWithMockScheduler(const Config&); | 159 explicit TestingPlatformSupportWithMockScheduler(const Config&); |
151 ~TestingPlatformSupportWithMockScheduler() override; | 160 ~TestingPlatformSupportWithMockScheduler() override; |
152 | 161 |
153 // Platform: | 162 // Platform: |
154 WebThread* currentThread() override; | 163 WebThread* currentThread() override; |
(...skipping 26 matching lines...) Expand all Loading... |
181 | 190 |
182 protected: | 191 protected: |
183 static double getTestTime(); | 192 static double getTestTime(); |
184 | 193 |
185 std::unique_ptr<base::SimpleTestTickClock> m_clock; | 194 std::unique_ptr<base::SimpleTestTickClock> m_clock; |
186 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner; | 195 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner; |
187 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler; | 196 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler; |
188 std::unique_ptr<WebThread> m_thread; | 197 std::unique_ptr<WebThread> m_thread; |
189 }; | 198 }; |
190 | 199 |
191 class ScopedUnittestsEnvironmentSetup { | 200 // ScopedTestingPlatformSupport<MyTestingPlatformSupport> can be used to |
| 201 // override Platform::current() with MyTestingPlatformSupport, like this: |
| 202 // |
| 203 // #include "platform/testing/TestingPlatformSupport.h" |
| 204 // |
| 205 // TEST_F(SampleTest, sampleTest) { |
| 206 // ScopedTestingPlatformSupport<MyTestingPlatformSupport> platform; |
| 207 // ... |
| 208 // // You can call methods of MyTestingPlatformSupport. |
| 209 // EXPECT_TRUE(platform->myMethodIsCalled()); |
| 210 // |
| 211 // // Another instance can be nested. |
| 212 // { |
| 213 // // Constructor's arguments can be passed like this. |
| 214 // Arg arg; |
| 215 // ScopedTestingPlatformSupport<MyAnotherTestingPlatformSupport, const Arg&> |
| 216 // another_platform(args); |
| 217 // ... |
| 218 // } |
| 219 // |
| 220 // // Here the original MyTestingPlatformSupport should be restored. |
| 221 // } |
| 222 template <class T, typename... Args> |
| 223 class ScopedTestingPlatformSupport final { |
| 224 STACK_ALLOCATED(); |
| 225 |
| 226 public: |
| 227 explicit ScopedTestingPlatformSupport(Args&&... args) { |
| 228 m_testingPlatformSupport = WTF::makeUnique<T>(std::forward<Args>(args)...); |
| 229 m_originalPlatform = Platform::current(); |
| 230 DCHECK(m_originalPlatform); |
| 231 Platform::setCurrentPlatformForTesting(m_testingPlatformSupport.get()); |
| 232 } |
| 233 ~ScopedTestingPlatformSupport() { |
| 234 DCHECK_EQ(m_testingPlatformSupport.get(), Platform::current()); |
| 235 m_testingPlatformSupport.reset(); |
| 236 Platform::setCurrentPlatformForTesting(m_originalPlatform); |
| 237 } |
| 238 |
| 239 const T* operator->() const { return m_testingPlatformSupport.get(); } |
| 240 T* operator->() { return m_testingPlatformSupport.get(); } |
| 241 |
| 242 private: |
| 243 std::unique_ptr<T> m_testingPlatformSupport; |
| 244 Platform* m_originalPlatform; |
| 245 }; |
| 246 |
| 247 class ScopedUnittestsEnvironmentSetup final { |
192 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); | 248 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); |
193 | 249 |
194 public: | 250 public: |
195 ScopedUnittestsEnvironmentSetup(int argc, char** argv); | 251 ScopedUnittestsEnvironmentSetup(int argc, char** argv); |
196 ~ScopedUnittestsEnvironmentSetup(); | 252 ~ScopedUnittestsEnvironmentSetup(); |
197 | 253 |
198 private: | 254 private: |
199 class DummyPlatform; | 255 class DummyPlatform; |
200 std::unique_ptr<base::TestDiscardableMemoryAllocator> | 256 std::unique_ptr<base::TestDiscardableMemoryAllocator> |
201 m_discardableMemoryAllocator; | 257 m_discardableMemoryAllocator; |
202 std::unique_ptr<DummyPlatform> m_platform; | 258 std::unique_ptr<DummyPlatform> m_dummyPlatform; |
203 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; | 259 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; |
204 TestingPlatformSupport::Config m_testingPlatformConfig; | 260 TestingPlatformSupport::Config m_testingPlatformConfig; |
205 std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport; | 261 std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport; |
206 }; | 262 }; |
207 | 263 |
208 } // namespace blink | 264 } // namespace blink |
209 | 265 |
210 #endif // TestingPlatformSupport_h | 266 #endif // TestingPlatformSupport_h |
OLD | NEW |