Home | History | Annotate | Download | only in domain_reliability
      1 // Copyright 2014 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 "components/domain_reliability/util.h"
      6 
      7 #include "base/bind.h"
      8 #include "components/domain_reliability/test_util.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace domain_reliability {
     12 namespace {
     13 
     14 using base::TimeDelta;
     15 using base::TimeTicks;
     16 
     17 class DomainReliabilityMockTimeTest : public testing::Test {
     18  protected:
     19   MockTime time_;
     20 };
     21 
     22 TEST_F(DomainReliabilityMockTimeTest, Create) {
     23 }
     24 
     25 TEST_F(DomainReliabilityMockTimeTest, NowAndAdvance) {
     26   const TimeDelta delta = TimeDelta::FromSeconds(1);
     27 
     28   TimeTicks initial = time_.NowTicks();
     29   time_.Advance(delta);
     30   TimeTicks final = time_.NowTicks();
     31   EXPECT_EQ(delta, final - initial);
     32 }
     33 
     34 TEST_F(DomainReliabilityMockTimeTest, AddTask) {
     35   const TimeDelta delta = TimeDelta::FromSeconds(1);
     36   TestCallback callback;
     37 
     38   time_.AddTask(2 * delta, callback.callback());
     39   time_.Advance(delta);
     40   EXPECT_FALSE(callback.called());
     41   time_.Advance(delta);
     42   EXPECT_TRUE(callback.called());
     43 }
     44 
     45 TEST_F(DomainReliabilityMockTimeTest, TimerCreate) {
     46   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
     47 }
     48 
     49 TEST_F(DomainReliabilityMockTimeTest, TimerIsRunning) {
     50   const TimeDelta delta = TimeDelta::FromSeconds(1);
     51   TestCallback callback;
     52 
     53   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
     54   EXPECT_FALSE(timer->IsRunning());
     55   timer->Start(FROM_HERE, delta, callback.callback());
     56   EXPECT_TRUE(timer->IsRunning());
     57   timer->Stop();
     58   EXPECT_FALSE(timer->IsRunning());
     59 }
     60 
     61 TEST_F(DomainReliabilityMockTimeTest, TimerGoesOff) {
     62   const TimeDelta delta = TimeDelta::FromSeconds(1);
     63   TestCallback callback;
     64 
     65   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
     66 
     67   timer->Start(FROM_HERE, 2 * delta, callback.callback());
     68   time_.Advance(delta);
     69   EXPECT_FALSE(callback.called());
     70   time_.Advance(delta);
     71   EXPECT_TRUE(callback.called());
     72 }
     73 
     74 TEST_F(DomainReliabilityMockTimeTest, TimerStopped) {
     75   const TimeDelta delta = TimeDelta::FromSeconds(1);
     76   TestCallback callback;
     77 
     78   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
     79 
     80   timer->Start(FROM_HERE, 2 * delta, callback.callback());
     81   time_.Advance(delta);
     82   timer->Stop();
     83   time_.Advance(delta);
     84   EXPECT_FALSE(callback.called());
     85 }
     86 
     87 TEST_F(DomainReliabilityMockTimeTest, TimerRestarted) {
     88   const TimeDelta delta = TimeDelta::FromSeconds(1);
     89   TestCallback callback;
     90 
     91   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
     92 
     93   timer->Start(FROM_HERE, 2 * delta, callback.callback());
     94   time_.Advance(delta);
     95   timer->Start(FROM_HERE, 2 * delta, callback.callback());
     96   time_.Advance(delta);
     97   EXPECT_FALSE(callback.called());
     98   time_.Advance(delta);
     99   EXPECT_TRUE(callback.called());
    100 }
    101 
    102 TEST_F(DomainReliabilityMockTimeTest, TimerReentrantStart) {
    103   const TimeDelta delta = TimeDelta::FromSeconds(1);
    104   scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
    105   TestCallback callback;
    106 
    107   timer->Start(
    108       FROM_HERE,
    109       delta,
    110       base::Bind(
    111           &MockTime::Timer::Start,
    112           base::Unretained(timer.get()),
    113           FROM_HERE,
    114           delta,
    115           callback.callback()));
    116   time_.Advance(delta);
    117   EXPECT_FALSE(callback.called());
    118   EXPECT_TRUE(timer->IsRunning());
    119   time_.Advance(delta);
    120   EXPECT_TRUE(callback.called());
    121   EXPECT_FALSE(timer->IsRunning());
    122 }
    123 
    124 }  // namespace
    125 }  // namespace domain_reliability
    126