Home | History | Annotate | Download | only in glue
      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 "webkit/glue/webkit_glue.h"
      6 
      7 #include <string>
      8 
      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"
     13 
     14 namespace {
     15 
     16 // Derives WebKitPlatformSupportImpl for testing shared timers.
     17 class TestWebKitPlatformSupport
     18     : public webkit_glue::WebKitPlatformSupportImpl {
     19  public:
     20   TestWebKitPlatformSupport() : mock_monotonically_increasing_time_(0) {
     21   }
     22 
     23   // WebKitPlatformSupportImpl implementation
     24   virtual base::string16 GetLocalizedString(int) OVERRIDE {
     25     return base::string16();
     26   }
     27 
     28   virtual base::StringPiece GetDataResource(int, ui::ScaleFactor) OVERRIDE {
     29     return base::StringPiece();
     30   }
     31 
     32   virtual webkit_glue::ResourceLoaderBridge* CreateResourceLoader(
     33       const webkit_glue::ResourceLoaderBridge::RequestInfo&) OVERRIDE {
     34     return NULL;
     35   }
     36 
     37   virtual webkit_glue::WebSocketStreamHandleBridge* CreateWebSocketBridge(
     38       WebKit::WebSocketStreamHandle*,
     39       webkit_glue::WebSocketStreamHandleDelegate*) OVERRIDE {
     40     return NULL;
     41   }
     42 
     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();
     49   }
     50 
     51   virtual void OnStartSharedTimer(base::TimeDelta delay) OVERRIDE {
     52     shared_timer_delay_ = delay;
     53   }
     54 
     55   base::TimeDelta shared_timer_delay() {
     56     return shared_timer_delay_;
     57   }
     58 
     59   void set_mock_monotonically_increasing_time(double mock_time) {
     60     mock_monotonically_increasing_time_ = mock_time;
     61   }
     62 
     63  private:
     64   base::TimeDelta shared_timer_delay_;
     65   double mock_monotonically_increasing_time_;
     66 };
     67 
     68 TEST(WebkitGlueTest, SuspendResumeSharedTimer) {
     69   base::MessageLoop message_loop;
     70   TestWebKitPlatformSupport platform_support;
     71 
     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
     77   // message loop.
     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
     83   // immediately.
     84   platform_support.ResumeSharedTimer();
     85   EXPECT_TRUE(base::TimeDelta() == platform_support.shared_timer_delay());
     86 }
     87 
     88 }  // namespace
     89