Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: third_party/WebKit/Source/platform/testing/TestingPlatformSupport.h

Issue 2588403002: TestingPlatformSupport: register Platform instance correctly (Closed)
Patch Set: new plan Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
115 // compositorSupport() method via a TestingPlatformSupport::Config parameter.
116 // Other virtual Platform methods that are overriden in this class will call
117 // the original Platform methods that can be obtained by Platform::current()
118 // in the consturctor.
119 // Or you may to consider to implement your own inheritance of this class to
120 // override arbitorary virtual methods.
121 // You should use ScopedTestingPlatformSupport to hold any instance of this
122 // class and inheritances in order to setup and shutdown Platform instance
123 // correctly. See also comments on ScopedTestingPlatformSupport.
haraken 2017/01/12 11:21:01 Slightly simpler: // A base class to override Pla
Takashi Toyoshima 2017/01/12 12:13:01 Actually, existing TestingPlatformSupport and Test
110 class TestingPlatformSupport : public Platform { 124 class TestingPlatformSupport : public Platform {
111 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport); 125 WTF_MAKE_NONCOPYABLE(TestingPlatformSupport);
112 126
113 public: 127 public:
114 struct Config { 128 struct Config {
115 WebCompositorSupport* compositorSupport = nullptr; 129 WebCompositorSupport* compositorSupport = nullptr;
116 }; 130 };
117 131
118 TestingPlatformSupport(); 132 TestingPlatformSupport();
119 explicit TestingPlatformSupport(const Config&); 133 explicit TestingPlatformSupport(const Config&);
(...skipping 15 matching lines...) Expand all
135 InterfaceProvider* interfaceProvider() override; 149 InterfaceProvider* interfaceProvider() override;
136 150
137 protected: 151 protected:
138 class TestingInterfaceProvider; 152 class TestingInterfaceProvider;
139 153
140 const Config m_config; 154 const Config m_config;
141 Platform* const m_oldPlatform; 155 Platform* const m_oldPlatform;
142 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider; 156 std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider;
143 }; 157 };
144 158
159 // This class adds mocked scheduler support to TestingPlatformSupport. See also
160 // comments on TestingPlatformSupport to use this class correctly.
145 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport { 161 class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
146 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler); 162 WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler);
147 163
148 public: 164 public:
149 TestingPlatformSupportWithMockScheduler(); 165 TestingPlatformSupportWithMockScheduler();
150 explicit TestingPlatformSupportWithMockScheduler(const Config&); 166 explicit TestingPlatformSupportWithMockScheduler(const Config&);
151 ~TestingPlatformSupportWithMockScheduler() override; 167 ~TestingPlatformSupportWithMockScheduler() override;
152 168
153 // Platform: 169 // Platform:
154 WebThread* currentThread() override; 170 WebThread* currentThread() override;
(...skipping 26 matching lines...) Expand all
181 197
182 protected: 198 protected:
183 static double getTestTime(); 199 static double getTestTime();
184 200
185 std::unique_ptr<base::SimpleTestTickClock> m_clock; 201 std::unique_ptr<base::SimpleTestTickClock> m_clock;
186 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner; 202 scoped_refptr<cc::OrderedSimpleTaskRunner> m_mockTaskRunner;
187 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler; 203 std::unique_ptr<scheduler::RendererSchedulerImpl> m_scheduler;
188 std::unique_ptr<WebThread> m_thread; 204 std::unique_ptr<WebThread> m_thread;
189 }; 205 };
190 206
191 class ScopedUnittestsEnvironmentSetup { 207 // A template class to hold an instance of TestingPlatformSupport class and
208 // inheritances. This class also sets up the instance to be available through
209 // Platform::current(), and safely reset it to the previous Platform instance
210 // on its destruction. Instances could be nested.
haraken 2017/01/12 11:21:01 Slightly simpler: // ScopedTestingPlatformSupport
Takashi Toyoshima 2017/01/12 12:13:01 Done.
211 //
212 // Usage:
213 //
214 // #include "platform/testing/TestingPlatformSupport.h"
215 //
216 // TEST_F(SampleTest, sampleTest) {
217 // ScopedTestingPlatformSupport<MyTestingPlatformSupport> platform;
218 // ...
219 // // You can call methods of MyTestingPlatformSupport.
220 // EXPECT_TRUE(platform->myMethodIsCalled());
221 //
222 // // Another instance can be nested.
223 // {
224 // // Constructor's arguments can be passed like this.
225 // Arg arg;
226 // ScopedTestingPlatformSupport<MyAnotherTestingPlatformSupport, const Arg&>
227 // another_platform(args);
228 // ...
229 // }
230 //
231 // // Here the origianl MyTestingPlatformSupport should be restored.
haraken 2017/01/12 11:21:01 original
Takashi Toyoshima 2017/01/12 12:13:02 Done.
232 // }
233 template <class T, typename... Args>
234 class ScopedTestingPlatformSupport final {
haraken 2017/01/12 11:21:01 This looks much simpler!
Takashi Toyoshima 2017/01/12 12:13:01 Acknowledged.
235 STACK_ALLOCATED();
236
237 public:
238 explicit ScopedTestingPlatformSupport(Args&&... args) {
239 m_testingPlatformSupport = WTF::makeUnique<T>(std::forward<Args>(args)...);
240 m_originalPlatform = Platform::current();
241 DCHECK(m_originalPlatform);
242 Platform::setCurrentPlatformForTesting(m_testingPlatformSupport.get());
243 }
244 ~ScopedTestingPlatformSupport() {
245 DCHECK_EQ(m_testingPlatformSupport.get(), Platform::current());
246 m_testingPlatformSupport.reset();
247 Platform::setCurrentPlatformForTesting(m_originalPlatform);
248 }
249
250 const T* operator->() const { return m_testingPlatformSupport.get(); }
251 T* operator->() { return m_testingPlatformSupport.get(); }
252
253 private:
254 std::unique_ptr<T> m_testingPlatformSupport;
255 Platform* m_originalPlatform;
256 };
257
258 class ScopedUnittestsEnvironmentSetup final {
192 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup); 259 WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup);
193 260
194 public: 261 public:
195 ScopedUnittestsEnvironmentSetup(int argc, char** argv); 262 ScopedUnittestsEnvironmentSetup(int argc, char** argv);
196 ~ScopedUnittestsEnvironmentSetup(); 263 ~ScopedUnittestsEnvironmentSetup();
197 264
198 private: 265 private:
199 class DummyPlatform; 266 class DummyPlatform;
200 std::unique_ptr<base::TestDiscardableMemoryAllocator> 267 std::unique_ptr<base::TestDiscardableMemoryAllocator>
201 m_discardableMemoryAllocator; 268 m_discardableMemoryAllocator;
202 std::unique_ptr<DummyPlatform> m_platform; 269 std::unique_ptr<DummyPlatform> m_dummyPlatform;
203 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport; 270 std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport;
204 TestingPlatformSupport::Config m_testingPlatformConfig; 271 TestingPlatformSupport::Config m_testingPlatformConfig;
205 std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport; 272 std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport;
206 }; 273 };
207 274
208 } // namespace blink 275 } // namespace blink
209 276
210 #endif // TestingPlatformSupport_h 277 #endif // TestingPlatformSupport_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698