Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_connection_helper_test.cc
blob2e677221a84d51d5a62fd33dda4fb5c7ff65ba85
1 // Copyright (c) 2012 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 "net/quic/quic_connection_helper.h"
7 #include "net/quic/test_tools/mock_clock.h"
8 #include "net/quic/test_tools/mock_random.h"
9 #include "net/quic/test_tools/test_task_runner.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace net {
13 namespace test {
14 namespace {
16 class TestDelegate : public QuicAlarm::Delegate {
17 public:
18 TestDelegate() : fired_(false) {}
20 virtual QuicTime OnAlarm() OVERRIDE {
21 fired_ = true;
22 return QuicTime::Zero();
25 bool fired() const { return fired_; }
26 void Clear() { fired_= false; }
28 private:
29 bool fired_;
32 class QuicConnectionHelperTest : public ::testing::Test {
33 protected:
34 QuicConnectionHelperTest()
35 : runner_(new TestTaskRunner(&clock_)),
36 helper_(runner_.get(), &clock_, &random_generator_) {
39 scoped_refptr<TestTaskRunner> runner_;
40 QuicConnectionHelper helper_;
41 MockClock clock_;
42 MockRandom random_generator_;
45 TEST_F(QuicConnectionHelperTest, GetClock) {
46 EXPECT_EQ(&clock_, helper_.GetClock());
49 TEST_F(QuicConnectionHelperTest, GetRandomGenerator) {
50 EXPECT_EQ(&random_generator_, helper_.GetRandomGenerator());
53 TEST_F(QuicConnectionHelperTest, CreateAlarm) {
54 TestDelegate* delegate = new TestDelegate();
55 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
57 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
58 alarm->Set(clock_.Now().Add(delta));
60 // Verify that the alarm task has been posted.
61 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
62 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
63 runner_->GetPostedTasks()[0].delay);
65 runner_->RunNextTask();
66 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
67 EXPECT_TRUE(delegate->fired());
70 TEST_F(QuicConnectionHelperTest, CreateAlarmAndCancel) {
71 TestDelegate* delegate = new TestDelegate();
72 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
74 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
75 alarm->Set(clock_.Now().Add(delta));
76 alarm->Cancel();
78 // The alarm task should still be posted.
79 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
80 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
81 runner_->GetPostedTasks()[0].delay);
83 runner_->RunNextTask();
84 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
85 EXPECT_FALSE(delegate->fired());
88 TEST_F(QuicConnectionHelperTest, CreateAlarmAndReset) {
89 TestDelegate* delegate = new TestDelegate();
90 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
92 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
93 alarm->Set(clock_.Now().Add(delta));
94 alarm->Cancel();
95 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
96 alarm->Set(clock_.Now().Add(new_delta));
98 // The alarm task should still be posted.
99 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
100 EXPECT_EQ(base::TimeDelta::FromMicroseconds(delta.ToMicroseconds()),
101 runner_->GetPostedTasks()[0].delay);
103 runner_->RunNextTask();
104 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
105 EXPECT_FALSE(delegate->fired());
107 // The alarm task should be posted again.
108 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
110 runner_->RunNextTask();
111 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now());
112 EXPECT_TRUE(delegate->fired());
115 TEST_F(QuicConnectionHelperTest, CreateAlarmAndResetEarlier) {
116 TestDelegate* delegate = new TestDelegate();
117 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
119 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(3);
120 alarm->Set(clock_.Now().Add(delta));
121 alarm->Cancel();
122 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(1);
123 alarm->Set(clock_.Now().Add(new_delta));
125 // Both alarm tasks will be posted.
126 ASSERT_EQ(2u, runner_->GetPostedTasks().size());
128 // The earlier task will execute and will fire the alarm->
129 runner_->RunNextTask();
130 EXPECT_EQ(QuicTime::Zero().Add(new_delta), clock_.Now());
131 EXPECT_TRUE(delegate->fired());
132 delegate->Clear();
134 // The latter task is still posted.
135 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
137 // When the latter task is executed, the weak ptr will be invalid and
138 // the alarm will not fire.
139 runner_->RunNextTask();
140 EXPECT_EQ(QuicTime::Zero().Add(delta), clock_.Now());
141 EXPECT_FALSE(delegate->fired());
144 } // namespace
145 } // namespace test
146 } // namespace net