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.
5 #include "webkit/glue/webkit_glue.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/child/webkitplatformsupport_impl.h"
16 // Derives WebKitPlatformSupportImpl for testing shared timers.
17 class TestWebKitPlatformSupport
18 : public webkit_glue::WebKitPlatformSupportImpl
{
20 TestWebKitPlatformSupport() : mock_monotonically_increasing_time_(0) {
23 // WebKitPlatformSupportImpl implementation
24 virtual base::string16
GetLocalizedString(int) OVERRIDE
{
25 return base::string16();
28 virtual base::StringPiece
GetDataResource(int, ui::ScaleFactor
) OVERRIDE
{
29 return base::StringPiece();
32 virtual webkit_glue::ResourceLoaderBridge
* CreateResourceLoader(
33 const webkit_glue::ResourceLoaderBridge::RequestInfo
&) OVERRIDE
{
37 virtual webkit_glue::WebSocketStreamHandleBridge
* CreateWebSocketBridge(
38 WebKit::WebSocketStreamHandle
*,
39 webkit_glue::WebSocketStreamHandleDelegate
*) OVERRIDE
{
43 // Returns mock time when enabled.
44 virtual double monotonicallyIncreasingTime() OVERRIDE
{
45 if (mock_monotonically_increasing_time_
> 0.0)
46 return mock_monotonically_increasing_time_
;
47 return webkit_glue::WebKitPlatformSupportImpl::
48 monotonicallyIncreasingTime();
51 virtual void OnStartSharedTimer(base::TimeDelta delay
) OVERRIDE
{
52 shared_timer_delay_
= delay
;
55 base::TimeDelta
shared_timer_delay() {
56 return shared_timer_delay_
;
59 void set_mock_monotonically_increasing_time(double mock_time
) {
60 mock_monotonically_increasing_time_
= mock_time
;
64 base::TimeDelta shared_timer_delay_
;
65 double mock_monotonically_increasing_time_
;
68 TEST(WebkitGlueTest
, SuspendResumeSharedTimer
) {
69 base::MessageLoop message_loop
;
70 TestWebKitPlatformSupport platform_support
;
72 // Set a timer to fire as soon as possible.
73 platform_support
.setSharedTimerFireInterval(0);
74 // Suspend timers immediately so the above timer wouldn't be fired.
75 platform_support
.SuspendSharedTimer();
76 // The above timer would have posted a task which can be processed out of the
78 message_loop
.RunUntilIdle();
79 // Set a mock time after 1 second to simulate timers suspended for 1 second.
80 double new_time
= base::Time::Now().ToDoubleT() + 1;
81 platform_support
.set_mock_monotonically_increasing_time(new_time
);
82 // Resume timers so that the timer set above will be set again to fire
84 platform_support
.ResumeSharedTimer();
85 EXPECT_TRUE(base::TimeDelta() == platform_support
.shared_timer_delay());