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

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 1456873003: More regular Platform implementations in unit tests (reland.) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: have GN disable c4267 also Created 5 years 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 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "platform/Timer.h" 6 #include "platform/Timer.h"
7 7
8 #include "platform/testing/TestingPlatformSupport.h"
8 #include "public/platform/Platform.h" 9 #include "public/platform/Platform.h"
9 #include "public/platform/WebScheduler.h" 10 #include "public/platform/WebScheduler.h"
10 #include "public/platform/WebThread.h" 11 #include "public/platform/WebThread.h"
11 #include "public/platform/WebViewScheduler.h" 12 #include "public/platform/WebViewScheduler.h"
12 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include <queue> 15 #include <queue>
15 16
16 using testing::ElementsAre; 17 using testing::ElementsAre;
17 18
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 230
230 virtual void exitRunLoop() 231 virtual void exitRunLoop()
231 { 232 {
232 ASSERT_NOT_REACHED(); 233 ASSERT_NOT_REACHED();
233 } 234 }
234 235
235 private: 236 private:
236 OwnPtr<MockWebScheduler> m_webScheduler; 237 OwnPtr<MockWebScheduler> m_webScheduler;
237 }; 238 };
238 239
239 class TimerTestPlatform : public Platform { 240 class TimerTestPlatform : public TestingPlatformSupport {
240 public: 241 public:
241 TimerTestPlatform() 242 TimerTestPlatform()
242 : m_webThread(adoptPtr(new FakeWebThread())) { } 243 : m_webThread(adoptPtr(new FakeWebThread())) { }
243 ~TimerTestPlatform() override { } 244 ~TimerTestPlatform() override { }
244 245
245 WebThread* currentThread() override 246 WebThread* currentThread() override
246 { 247 {
247 return m_webThread.get(); 248 return m_webThread.get();
248 } 249 }
249 250
250 void cryptographicallyRandomValues(unsigned char*, size_t) override
251 {
252 ASSERT_NOT_REACHED();
253 }
254
255 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) o verride
256 {
257 static const unsigned char enabled[] = {0};
258 return enabled;
259 }
260
261 void runUntilIdle() 251 void runUntilIdle()
262 { 252 {
263 mockScheduler()->runUntilIdle(); 253 mockScheduler()->runUntilIdle();
264 } 254 }
265 255
266 void runPendingTasks() 256 void runPendingTasks()
267 { 257 {
268 mockScheduler()->runPendingTasks(); 258 mockScheduler()->runPendingTasks();
269 } 259 }
270 260
(...skipping 18 matching lines...) Expand all
289 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); 279 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
290 } 280 }
291 281
292 OwnPtr<FakeWebThread> m_webThread; 282 OwnPtr<FakeWebThread> m_webThread;
293 }; 283 };
294 284
295 class TimerTest : public testing::Test { 285 class TimerTest : public testing::Test {
296 public: 286 public:
297 void SetUp() override 287 void SetUp() override
298 { 288 {
299 m_platform = adoptPtr(new TimerTestPlatform());
300 m_oldPlatform = Platform::current();
301 Platform::initialize(m_platform.get());
302 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); 289 WTF::setMonotonicallyIncreasingTimeFunction(currentTime);
303 290
304 m_runTimes.clear(); 291 m_runTimes.clear();
305 gCurrentTimeSecs = 10.0; 292 gCurrentTimeSecs = 10.0;
306 m_startTime = gCurrentTimeSecs; 293 m_startTime = gCurrentTimeSecs;
307 } 294 }
308 295
309 void TearDown() override
310 {
311 Platform::initialize(m_oldPlatform);
312 }
313
314 void countingTask(Timer<TimerTest>*) 296 void countingTask(Timer<TimerTest>*)
315 { 297 {
316 m_runTimes.append(monotonicallyIncreasingTime()); 298 m_runTimes.append(monotonicallyIncreasingTime());
317 } 299 }
318 300
319 void recordNextFireTimeTask(Timer<TimerTest>* timer) 301 void recordNextFireTimeTask(Timer<TimerTest>* timer)
320 { 302 {
321 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn terval()); 303 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn terval());
322 } 304 }
323 305
324 void advanceTimeBy(double timeSecs) 306 void advanceTimeBy(double timeSecs)
325 { 307 {
326 gCurrentTimeSecs += timeSecs; 308 gCurrentTimeSecs += timeSecs;
327 } 309 }
328 310
329 void runUntilIdle() 311 void runUntilIdle()
330 { 312 {
331 m_platform->runUntilIdle(); 313 m_platform.runUntilIdle();
332 } 314 }
333 315
334 void runPendingTasks() 316 void runPendingTasks()
335 { 317 {
336 m_platform->runPendingTasks(); 318 m_platform.runPendingTasks();
337 } 319 }
338 320
339 void runUntilIdleOrDeadlinePassed(double deadline) 321 void runUntilIdleOrDeadlinePassed(double deadline)
340 { 322 {
341 m_platform->runUntilIdleOrDeadlinePassed(deadline); 323 m_platform.runUntilIdleOrDeadlinePassed(deadline);
342 } 324 }
343 325
344 bool hasOneTimerTask() const 326 bool hasOneTimerTask() const
345 { 327 {
346 return m_platform->hasOneTimerTask(); 328 return m_platform.hasOneTimerTask();
347 } 329 }
348 330
349 double nextTimerTaskDelaySecs() const 331 double nextTimerTaskDelaySecs() const
350 { 332 {
351 return m_platform->nextTimerTaskDelaySecs(); 333 return m_platform.nextTimerTaskDelaySecs();
352 } 334 }
353 335
354 protected: 336 protected:
355 double m_startTime; 337 double m_startTime;
356 WTF::Vector<double> m_runTimes; 338 WTF::Vector<double> m_runTimes;
357 WTF::Vector<double> m_nextFireTimes; 339 WTF::Vector<double> m_nextFireTimes;
358 340
359 private: 341 private:
360 OwnPtr<TimerTestPlatform> m_platform; 342 TimerTestPlatform m_platform;
361 Platform* m_oldPlatform;
362 }; 343 };
363 344
364 TEST_F(TimerTest, StartOneShot_Zero) 345 TEST_F(TimerTest, StartOneShot_Zero)
365 { 346 {
366 Timer<TimerTest> timer(this, &TimerTest::countingTask); 347 Timer<TimerTest> timer(this, &TimerTest::countingTask);
367 timer.startOneShot(0, BLINK_FROM_HERE); 348 timer.startOneShot(0, BLINK_FROM_HERE);
368 349
369 ASSERT(hasOneTimerTask()); 350 ASSERT(hasOneTimerTask());
370 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 351 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
371 352
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 m_startTime + 6.0, 809 m_startTime + 6.0,
829 m_startTime + 8.0, 810 m_startTime + 8.0,
830 m_startTime + 10.0, 811 m_startTime + 10.0,
831 m_startTime + 14.0, 812 m_startTime + 14.0,
832 m_startTime + 18.0, 813 m_startTime + 18.0,
833 m_startTime + 28.0)); 814 m_startTime + 28.0));
834 } 815 }
835 816
836 } // namespace 817 } // namespace
837 } // namespace blink 818 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/TestingPlatformSupport.cpp ('k') | third_party/WebKit/Source/platform/blink_platform.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698