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

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

Issue 2266443002: Optimize posting of WTF::Closure and improve scheduler test mocks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cut back to just the WTF::Closure fix plus the scheduler test mock refactor. Created 4 years, 3 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 14 matching lines...) Expand all
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "platform/testing/TestingPlatformSupport.h" 31 #include "platform/testing/TestingPlatformSupport.h"
32 32
33 #include "base/command_line.h" 33 #include "base/command_line.h"
34 #include "base/memory/discardable_memory_allocator.h" 34 #include "base/memory/discardable_memory_allocator.h"
35 #include "base/memory/ptr_util.h"
35 #include "base/metrics/statistics_recorder.h" 36 #include "base/metrics/statistics_recorder.h"
36 #include "base/test/icu_test_util.h" 37 #include "base/test/icu_test_util.h"
37 #include "base/test/test_discardable_memory_allocator.h" 38 #include "base/test/test_discardable_memory_allocator.h"
38 #include "cc/blink/web_compositor_support_impl.h" 39 #include "cc/blink/web_compositor_support_impl.h"
40 #include "cc/test/ordered_simple_task_runner.h"
39 #include "platform/EventTracer.h" 41 #include "platform/EventTracer.h"
40 #include "platform/HTTPNames.h" 42 #include "platform/HTTPNames.h"
41 #include "platform/heap/Heap.h" 43 #include "platform/heap/Heap.h"
44 #include "platform/scheduler/base/test_time_source.h"
45 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h"
46 #include "platform/scheduler/renderer/renderer_scheduler_impl.h"
42 #include "wtf/CryptographicallyRandomNumber.h" 47 #include "wtf/CryptographicallyRandomNumber.h"
43 #include "wtf/CurrentTime.h" 48 #include "wtf/CurrentTime.h"
44 #include "wtf/PtrUtil.h" 49 #include "wtf/PtrUtil.h"
45 #include "wtf/WTF.h" 50 #include "wtf/WTF.h"
46 #include "wtf/allocator/Partitions.h" 51 #include "wtf/allocator/Partitions.h"
47 #include <memory> 52 #include <memory>
48 53
49 namespace blink { 54 namespace blink {
50 55
51 namespace { 56 namespace {
(...skipping 29 matching lines...) Expand all
81 Platform::setCurrentPlatformForTesting(m_oldPlatform); 86 Platform::setCurrentPlatformForTesting(m_oldPlatform);
82 } 87 }
83 88
84 WebString TestingPlatformSupport::defaultLocale() 89 WebString TestingPlatformSupport::defaultLocale()
85 { 90 {
86 return WebString::fromUTF8("en-US"); 91 return WebString::fromUTF8("en-US");
87 } 92 }
88 93
89 WebCompositorSupport* TestingPlatformSupport::compositorSupport() 94 WebCompositorSupport* TestingPlatformSupport::compositorSupport()
90 { 95 {
91 return m_config.compositorSupport; 96 if (m_config.compositorSupport)
97 return m_config.compositorSupport;
98
99 return m_oldPlatform ? m_oldPlatform->compositorSupport() : nullptr;
92 } 100 }
93 101
94 WebThread* TestingPlatformSupport::currentThread() 102 WebThread* TestingPlatformSupport::currentThread()
95 { 103 {
96 return m_oldPlatform ? m_oldPlatform->currentThread() : nullptr; 104 return m_oldPlatform ? m_oldPlatform->currentThread() : nullptr;
97 } 105 }
98 106
99 class TestingPlatformMockWebTaskRunner : public WebTaskRunner { 107 WebBlobRegistry* TestingPlatformSupport::blobRegistry()
100 WTF_MAKE_NONCOPYABLE(TestingPlatformMockWebTaskRunner);
101 public:
102 explicit TestingPlatformMockWebTaskRunner(Deque<std::unique_ptr<WebTaskRunne r::Task>>* tasks) : m_tasks(tasks) { }
103 ~TestingPlatformMockWebTaskRunner() override { }
104
105 void postTask(const WebTraceLocation&, Task* task) override
106 {
107 m_tasks->append(wrapUnique(task));
108 }
109
110 void postDelayedTask(const WebTraceLocation&, Task*, double delayMs) overrid e
111 {
112 NOTREACHED();
113 }
114
115 bool runsTasksOnCurrentThread() override
116 {
117 NOTREACHED();
118 return true;
119 }
120
121 std::unique_ptr<WebTaskRunner> clone() override
122 {
123 return WTF::wrapUnique(new TestingPlatformMockWebTaskRunner(m_tasks));
124 }
125
126 double virtualTimeSeconds() const override
127 {
128 NOTREACHED();
129 return 0.0;
130 }
131
132 double monotonicallyIncreasingVirtualTimeSeconds() const override
133 {
134 NOTREACHED();
135 return 0.0;
136 }
137
138 base::SingleThreadTaskRunner* taskRunner() override
139 {
140 NOTREACHED();
141 return nullptr;
142 }
143
144 private:
145 Deque<std::unique_ptr<WebTaskRunner::Task>>* m_tasks; // NOT OWNED
146 };
147
148 // TestingPlatformMockScheduler definition:
149
150 TestingPlatformMockScheduler::TestingPlatformMockScheduler()
151 : m_mockWebTaskRunner(wrapUnique(new TestingPlatformMockWebTaskRunner(&m_tas ks))) { }
152
153 TestingPlatformMockScheduler::~TestingPlatformMockScheduler() { }
154
155 WebTaskRunner* TestingPlatformMockScheduler::loadingTaskRunner()
156 { 108 {
157 return m_mockWebTaskRunner.get(); 109 return m_oldPlatform ? m_oldPlatform->blobRegistry() : nullptr;
158 } 110 }
159 111
160 WebTaskRunner* TestingPlatformMockScheduler::timerTaskRunner() 112 WebClipboard* TestingPlatformSupport::clipboard()
161 { 113 {
162 return m_mockWebTaskRunner.get(); 114 return m_oldPlatform ? m_oldPlatform->clipboard() : nullptr;
163 } 115 }
164 116
165 void TestingPlatformMockScheduler::runSingleTask() 117 WebFileUtilities* TestingPlatformSupport::fileUtilities()
166 { 118 {
167 if (m_tasks.isEmpty()) 119 return m_oldPlatform ? m_oldPlatform->fileUtilities() : nullptr;
168 return;
169 m_tasks.takeFirst()->run();
170 } 120 }
171 121
172 void TestingPlatformMockScheduler::runAllTasks() 122 WebIDBFactory* TestingPlatformSupport::idbFactory()
173 { 123 {
174 while (!m_tasks.isEmpty()) 124 return m_oldPlatform ? m_oldPlatform->idbFactory() : nullptr;
175 m_tasks.takeFirst()->run();
176 } 125 }
177 126
178 class TestingPlatformMockWebThread : public WebThread { 127 WebMimeRegistry* TestingPlatformSupport::mimeRegistry()
179 WTF_MAKE_NONCOPYABLE(TestingPlatformMockWebThread); 128 {
180 public: 129 return m_oldPlatform ? m_oldPlatform->mimeRegistry() : nullptr;
181 TestingPlatformMockWebThread() : m_mockWebScheduler(wrapUnique(new TestingPl atformMockScheduler)) { } 130 }
182 ~TestingPlatformMockWebThread() override { }
183 131
184 WebTaskRunner* getWebTaskRunner() override 132 WebURLLoaderMockFactory* TestingPlatformSupport::getURLLoaderMockFactory()
185 { 133 {
186 return m_mockWebScheduler->timerTaskRunner(); 134 return m_oldPlatform ? m_oldPlatform->getURLLoaderMockFactory() : nullptr;
187 } 135 }
188 136
189 bool isCurrentThread() const override 137 WebURLLoader* TestingPlatformSupport::createURLLoader()
190 { 138 {
191 NOTREACHED(); 139 return m_oldPlatform ? m_oldPlatform->createURLLoader() : nullptr;
192 return true; 140 }
193 }
194 141
195 WebScheduler* scheduler() const override 142 WebData TestingPlatformSupport::loadResource(const char* name)
196 { 143 {
197 return m_mockWebScheduler.get(); 144 return m_oldPlatform ? m_oldPlatform->loadResource(name) : WebData();
198 } 145 }
199 146
200 TestingPlatformMockScheduler* mockWebScheduler() 147 WebURLError TestingPlatformSupport::cancelledError(const WebURL& url) const
201 { 148 {
202 return m_mockWebScheduler.get(); 149 return m_oldPlatform ? m_oldPlatform->cancelledError(url) : WebURLError();
203 } 150 }
204
205 private:
206 std::unique_ptr<TestingPlatformMockScheduler> m_mockWebScheduler;
207 };
208 151
209 // TestingPlatformSupportWithMockScheduler definition: 152 // TestingPlatformSupportWithMockScheduler definition:
210 153
211 TestingPlatformSupportWithMockScheduler::TestingPlatformSupportWithMockScheduler () 154 TestingPlatformSupportWithMockScheduler::TestingPlatformSupportWithMockScheduler ()
212 : m_mockWebThread(wrapUnique(new TestingPlatformMockWebThread())) { } 155 : TestingPlatformSupportWithMockScheduler(TestingPlatformSupport::Config()) { }
213 156
214 TestingPlatformSupportWithMockScheduler::TestingPlatformSupportWithMockScheduler (const Config& config) 157 TestingPlatformSupportWithMockScheduler::TestingPlatformSupportWithMockScheduler (const Config& config)
215 : TestingPlatformSupport(config) 158 : TestingPlatformSupport(config)
216 , m_mockWebThread(wrapUnique(new TestingPlatformMockWebThread())) { } 159 , m_clock(new base::SimpleTestTickClock())
160 , m_mockTaskRunner(new cc::OrderedSimpleTaskRunner(m_clock.get(), true))
161 , m_scheduler(new scheduler::RendererSchedulerImpl(scheduler::SchedulerTqmDe legateForTest::Create(m_mockTaskRunner, base::WrapUnique(new scheduler::TestTime Source(m_clock.get())))))
162 , m_thread(m_scheduler->CreateMainThread())
163 {
164 // Set the work batch size to one so RunPendingTasks behaves as expected.
165 m_scheduler->GetSchedulerHelperForTesting()->SetWorkBatchSizeForTesting(1);
217 166
218 TestingPlatformSupportWithMockScheduler::~TestingPlatformSupportWithMockSchedule r() { } 167 WTF::setTimeFunctionsForTesting(getTestTime);
168 }
169
170 TestingPlatformSupportWithMockScheduler::~TestingPlatformSupportWithMockSchedule r()
171 {
172 WTF::setTimeFunctionsForTesting(nullptr);
173 m_scheduler->Shutdown();
174 }
219 175
220 WebThread* TestingPlatformSupportWithMockScheduler::currentThread() 176 WebThread* TestingPlatformSupportWithMockScheduler::currentThread()
221 { 177 {
222 return m_mockWebThread.get(); 178 if (m_thread->isCurrentThread())
179 return m_thread.get();
180 return TestingPlatformSupport::currentThread();
223 } 181 }
224 182
225 TestingPlatformMockScheduler* TestingPlatformSupportWithMockScheduler::mockWebSc heduler() 183 void TestingPlatformSupportWithMockScheduler::runSingleTask()
226 { 184 {
227 return m_mockWebThread->mockWebScheduler(); 185 m_mockTaskRunner->SetRunTaskLimit(1);
186 m_mockTaskRunner->RunPendingTasks();
187 m_mockTaskRunner->ClearRunTaskLimit();
188 }
189
190 void TestingPlatformSupportWithMockScheduler::runUntilIdle()
191 {
192 m_mockTaskRunner->RunUntilIdle();
193 }
194
195 void TestingPlatformSupportWithMockScheduler::runForPeriodSeconds(double seconds )
196 {
197 m_mockTaskRunner->RunForPeriod(base::TimeDelta::FromSecondsD(seconds));
198 }
199
200 void TestingPlatformSupportWithMockScheduler::advanceClockSeconds(double seconds )
201 {
202 m_clock->Advance(base::TimeDelta::FromSecondsD(seconds));
203 }
204
205 void TestingPlatformSupportWithMockScheduler::setAutoAdvanceNowToPendingTasks(bo ol autoAdvance)
206 {
207 m_mockTaskRunner->SetAutoAdvanceNowToPendingTasks(autoAdvance);
208 }
209
210 scheduler::RendererScheduler* TestingPlatformSupportWithMockScheduler::rendererS cheduler() const
211 {
212 return m_scheduler.get();
213 }
214
215 // static
216 double TestingPlatformSupportWithMockScheduler::getTestTime()
217 {
218 TestingPlatformSupportWithMockScheduler* platform = static_cast<TestingPlatf ormSupportWithMockScheduler*>(Platform::current());
219 return (platform->m_clock->NowTicks() - base::TimeTicks()).InSecondsF();
228 } 220 }
229 221
230 class ScopedUnittestsEnvironmentSetup::DummyPlatform final : public blink::Platf orm { 222 class ScopedUnittestsEnvironmentSetup::DummyPlatform final : public blink::Platf orm {
231 public: 223 public:
232 DummyPlatform() { } 224 DummyPlatform() { }
233 225
234 blink::WebThread* currentThread() override 226 blink::WebThread* currentThread() override
235 { 227 {
236 static DummyThread dummyThread; 228 static DummyThread dummyThread;
237 return &dummyThread; 229 return &dummyThread;
(...skipping 26 matching lines...) Expand all
264 ThreadState::current()->registerTraceDOMWrappers(nullptr, nullptr, nullptr); 256 ThreadState::current()->registerTraceDOMWrappers(nullptr, nullptr, nullptr);
265 EventTracer::initialize(); 257 EventTracer::initialize();
266 HTTPNames::init(); 258 HTTPNames::init();
267 } 259 }
268 260
269 ScopedUnittestsEnvironmentSetup::~ScopedUnittestsEnvironmentSetup() 261 ScopedUnittestsEnvironmentSetup::~ScopedUnittestsEnvironmentSetup()
270 { 262 {
271 } 263 }
272 264
273 } // namespace blink 265 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698