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.
5 #include "components/domain_reliability/util.h"
8 #include "components/domain_reliability/test_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace domain_reliability
{
14 using base::TimeDelta
;
15 using base::TimeTicks
;
17 class DomainReliabilityMockTimeTest
: public testing::Test
{
22 TEST_F(DomainReliabilityMockTimeTest
, Create
) {
25 TEST_F(DomainReliabilityMockTimeTest
, NowAndAdvance
) {
26 const TimeDelta delta
= TimeDelta::FromSeconds(1);
28 TimeTicks initial
= time_
.NowTicks();
30 TimeTicks final
= time_
.NowTicks();
31 EXPECT_EQ(delta
, final
- initial
);
34 TEST_F(DomainReliabilityMockTimeTest
, AddTask
) {
35 const TimeDelta delta
= TimeDelta::FromSeconds(1);
36 TestCallback callback
;
38 time_
.AddTask(2 * delta
, callback
.callback());
40 EXPECT_FALSE(callback
.called());
42 EXPECT_TRUE(callback
.called());
45 TEST_F(DomainReliabilityMockTimeTest
, TimerCreate
) {
46 scoped_ptr
<MockTime::Timer
> timer(time_
.CreateTimer());
49 TEST_F(DomainReliabilityMockTimeTest
, TimerIsRunning
) {
50 const TimeDelta delta
= TimeDelta::FromSeconds(1);
51 TestCallback callback
;
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());
58 EXPECT_FALSE(timer
->IsRunning());
61 TEST_F(DomainReliabilityMockTimeTest
, TimerGoesOff
) {
62 const TimeDelta delta
= TimeDelta::FromSeconds(1);
63 TestCallback callback
;
65 scoped_ptr
<MockTime::Timer
> timer(time_
.CreateTimer());
67 timer
->Start(FROM_HERE
, 2 * delta
, callback
.callback());
69 EXPECT_FALSE(callback
.called());
71 EXPECT_TRUE(callback
.called());
74 TEST_F(DomainReliabilityMockTimeTest
, TimerStopped
) {
75 const TimeDelta delta
= TimeDelta::FromSeconds(1);
76 TestCallback callback
;
78 scoped_ptr
<MockTime::Timer
> timer(time_
.CreateTimer());
80 timer
->Start(FROM_HERE
, 2 * delta
, callback
.callback());
84 EXPECT_FALSE(callback
.called());
87 TEST_F(DomainReliabilityMockTimeTest
, TimerRestarted
) {
88 const TimeDelta delta
= TimeDelta::FromSeconds(1);
89 TestCallback callback
;
91 scoped_ptr
<MockTime::Timer
> timer(time_
.CreateTimer());
93 timer
->Start(FROM_HERE
, 2 * delta
, callback
.callback());
95 timer
->Start(FROM_HERE
, 2 * delta
, callback
.callback());
97 EXPECT_FALSE(callback
.called());
99 EXPECT_TRUE(callback
.called());
102 TEST_F(DomainReliabilityMockTimeTest
, TimerReentrantStart
) {
103 const TimeDelta delta
= TimeDelta::FromSeconds(1);
104 scoped_ptr
<MockTime::Timer
> timer(time_
.CreateTimer());
105 TestCallback callback
;
111 &MockTime::Timer::Start
,
112 base::Unretained(timer
.get()),
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());
125 } // namespace domain_reliability