summaryrefslogtreecommitdiffstats
path: root/components/domain_reliability/util_unittest.cc
blob: 3f21fccaa50cc4bf01e1daa2424493845651f092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/domain_reliability/util.h"

#include "base/bind.h"
#include "components/domain_reliability/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace domain_reliability {
namespace {

using base::TimeDelta;
using base::TimeTicks;

class DomainReliabilityMockTimeTest : public testing::Test {
 protected:
  MockTime time_;
};

TEST_F(DomainReliabilityMockTimeTest, Create) {
}

TEST_F(DomainReliabilityMockTimeTest, NowAndAdvance) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);

  TimeTicks initial = time_.NowTicks();
  time_.Advance(delta);
  TimeTicks final = time_.NowTicks();
  EXPECT_EQ(delta, final - initial);
}

TEST_F(DomainReliabilityMockTimeTest, AddTask) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  TestCallback callback;

  time_.AddTask(2 * delta, callback.callback());
  time_.Advance(delta);
  EXPECT_FALSE(callback.called());
  time_.Advance(delta);
  EXPECT_TRUE(callback.called());
}

TEST_F(DomainReliabilityMockTimeTest, TimerCreate) {
  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
}

TEST_F(DomainReliabilityMockTimeTest, TimerIsRunning) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  TestCallback callback;

  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
  EXPECT_FALSE(timer->IsRunning());
  timer->Start(FROM_HERE, delta, callback.callback());
  EXPECT_TRUE(timer->IsRunning());
  timer->Stop();
  EXPECT_FALSE(timer->IsRunning());
}

TEST_F(DomainReliabilityMockTimeTest, TimerGoesOff) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  TestCallback callback;

  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());

  timer->Start(FROM_HERE, 2 * delta, callback.callback());
  time_.Advance(delta);
  EXPECT_FALSE(callback.called());
  time_.Advance(delta);
  EXPECT_TRUE(callback.called());
}

TEST_F(DomainReliabilityMockTimeTest, TimerStopped) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  TestCallback callback;

  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());

  timer->Start(FROM_HERE, 2 * delta, callback.callback());
  time_.Advance(delta);
  timer->Stop();
  time_.Advance(delta);
  EXPECT_FALSE(callback.called());
}

TEST_F(DomainReliabilityMockTimeTest, TimerRestarted) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  TestCallback callback;

  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());

  timer->Start(FROM_HERE, 2 * delta, callback.callback());
  time_.Advance(delta);
  timer->Start(FROM_HERE, 2 * delta, callback.callback());
  time_.Advance(delta);
  EXPECT_FALSE(callback.called());
  time_.Advance(delta);
  EXPECT_TRUE(callback.called());
}

TEST_F(DomainReliabilityMockTimeTest, TimerReentrantStart) {
  const TimeDelta delta = TimeDelta::FromSeconds(1);
  scoped_ptr<MockTime::Timer> timer(time_.CreateTimer());
  TestCallback callback;

  timer->Start(
      FROM_HERE,
      delta,
      base::Bind(
          &MockTime::Timer::Start,
          base::Unretained(timer.get()),
          FROM_HERE,
          delta,
          callback.callback()));
  time_.Advance(delta);
  EXPECT_FALSE(callback.called());
  EXPECT_TRUE(timer->IsRunning());
  time_.Advance(delta);
  EXPECT_TRUE(callback.called());
  EXPECT_FALSE(timer->IsRunning());
}

}  // namespace
}  // namespace domain_reliability