• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/run_loop.h"
6 #include "base/time/time.h"
7 #include "content/child/blink_platform_impl.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace content {
11 
12 // Derives BlinkPlatformImpl for testing shared timers.
13 class TestBlinkPlatformImpl : public BlinkPlatformImpl {
14  public:
TestBlinkPlatformImpl()15   TestBlinkPlatformImpl() : mock_monotonically_increasing_time_(0) {}
16 
17   // Returns mock time when enabled.
monotonicallyIncreasingTime()18   virtual double monotonicallyIncreasingTime() OVERRIDE {
19     if (mock_monotonically_increasing_time_ > 0.0)
20       return mock_monotonically_increasing_time_;
21     return BlinkPlatformImpl::monotonicallyIncreasingTime();
22   }
23 
OnStartSharedTimer(base::TimeDelta delay)24   virtual void OnStartSharedTimer(base::TimeDelta delay) OVERRIDE {
25     shared_timer_delay_ = delay;
26   }
27 
shared_timer_delay()28   base::TimeDelta shared_timer_delay() { return shared_timer_delay_; }
29 
set_mock_monotonically_increasing_time(double mock_time)30   void set_mock_monotonically_increasing_time(double mock_time) {
31     mock_monotonically_increasing_time_ = mock_time;
32   }
33 
34  private:
35   base::TimeDelta shared_timer_delay_;
36   double mock_monotonically_increasing_time_;
37 
38   DISALLOW_COPY_AND_ASSIGN(TestBlinkPlatformImpl);
39 };
40 
TEST(BlinkPlatformTest,SuspendResumeSharedTimer)41 TEST(BlinkPlatformTest, SuspendResumeSharedTimer) {
42   base::MessageLoop message_loop;
43 
44   TestBlinkPlatformImpl platform_impl;
45 
46   // Set a timer to fire as soon as possible.
47   platform_impl.setSharedTimerFireInterval(0);
48 
49   // Suspend timers immediately so the above timer wouldn't be fired.
50   platform_impl.SuspendSharedTimer();
51 
52   // The above timer would have posted a task which can be processed out of the
53   // message loop.
54   base::RunLoop().RunUntilIdle();
55 
56   // Set a mock time after 1 second to simulate timers suspended for 1 second.
57   double new_time = base::Time::Now().ToDoubleT() + 1;
58   platform_impl.set_mock_monotonically_increasing_time(new_time);
59 
60   // Resume timers so that the timer set above will be set again to fire
61   // immediately.
62   platform_impl.ResumeSharedTimer();
63 
64   EXPECT_TRUE(base::TimeDelta() == platform_impl.shared_timer_delay());
65 }
66 
67 }  // namespace content
68