• 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 <string>
6 
7 #include "base/run_loop.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/child/webkitplatformsupport_impl.h"
11 
12 namespace {
13 
14 // Derives WebKitPlatformSupportImpl for testing shared timers.
15 class TestWebKitPlatformSupport
16     : public webkit_glue::WebKitPlatformSupportImpl {
17  public:
TestWebKitPlatformSupport()18   TestWebKitPlatformSupport() : mock_monotonically_increasing_time_(0) {
19   }
20 
21   // WebKitPlatformSupportImpl implementation
GetLocalizedString(int)22   virtual base::string16 GetLocalizedString(int) OVERRIDE {
23     return base::string16();
24   }
25 
GetDataResource(int,ui::ScaleFactor)26   virtual base::StringPiece GetDataResource(int, ui::ScaleFactor) OVERRIDE {
27     return base::StringPiece();
28   }
29 
CreateResourceLoader(const webkit_glue::ResourceLoaderBridge::RequestInfo &)30   virtual webkit_glue::ResourceLoaderBridge* CreateResourceLoader(
31       const webkit_glue::ResourceLoaderBridge::RequestInfo&) OVERRIDE {
32     return NULL;
33   }
34 
CreateWebSocketStreamBridge(blink::WebSocketStreamHandle *,webkit_glue::WebSocketStreamHandleDelegate *)35   virtual webkit_glue::WebSocketStreamHandleBridge* CreateWebSocketStreamBridge(
36       blink::WebSocketStreamHandle*,
37       webkit_glue::WebSocketStreamHandleDelegate*) OVERRIDE {
38     return NULL;
39   }
40 
41   // Returns mock time when enabled.
monotonicallyIncreasingTime()42   virtual double monotonicallyIncreasingTime() OVERRIDE {
43     if (mock_monotonically_increasing_time_ > 0.0)
44       return mock_monotonically_increasing_time_;
45     return webkit_glue::WebKitPlatformSupportImpl::
46         monotonicallyIncreasingTime();
47   }
48 
OnStartSharedTimer(base::TimeDelta delay)49   virtual void OnStartSharedTimer(base::TimeDelta delay) OVERRIDE {
50     shared_timer_delay_ = delay;
51   }
52 
shared_timer_delay()53   base::TimeDelta shared_timer_delay() {
54     return shared_timer_delay_;
55   }
56 
set_mock_monotonically_increasing_time(double mock_time)57   void set_mock_monotonically_increasing_time(double mock_time) {
58     mock_monotonically_increasing_time_ = mock_time;
59   }
60 
61  private:
62   base::TimeDelta shared_timer_delay_;
63   double mock_monotonically_increasing_time_;
64 };
65 
TEST(WebkitGlueTest,SuspendResumeSharedTimer)66 TEST(WebkitGlueTest, SuspendResumeSharedTimer) {
67   base::MessageLoop message_loop;
68 
69   TestWebKitPlatformSupport platform_support;
70 
71   // Set a timer to fire as soon as possible.
72   platform_support.setSharedTimerFireInterval(0);
73   // Suspend timers immediately so the above timer wouldn't be fired.
74   platform_support.SuspendSharedTimer();
75   // The above timer would have posted a task which can be processed out of the
76   // message loop.
77   base::RunLoop().RunUntilIdle();
78   // Set a mock time after 1 second to simulate timers suspended for 1 second.
79   double new_time = base::Time::Now().ToDoubleT() + 1;
80   platform_support.set_mock_monotonically_increasing_time(new_time);
81   // Resume timers so that the timer set above will be set again to fire
82   // immediately.
83   platform_support.ResumeSharedTimer();
84   EXPECT_TRUE(base::TimeDelta() == platform_support.shared_timer_delay());
85 }
86 
87 }  // namespace
88