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.
5 #include "net/quic/quic_alarm.h"
7 #include "base/logging.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using testing::Return
;
12 using testing::Invoke
;
18 class MockDelegate
: public QuicAlarm::Delegate
{
20 MOCK_METHOD0(OnAlarm
, QuicTime());
23 class TestAlarm
: public QuicAlarm
{
25 explicit TestAlarm(QuicAlarm::Delegate
* delegate
) : QuicAlarm(delegate
) {}
27 bool scheduled() const { return scheduled_
; }
35 virtual void SetImpl() OVERRIDE
{
36 DCHECK(deadline().IsInitialized());
40 virtual void CancelImpl() OVERRIDE
{
41 DCHECK(!deadline().IsInitialized());
49 class QuicAlarmTest
: public ::testing::Test
{
52 : delegate_(new MockDelegate()),
54 deadline_(QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7))),
55 deadline2_(QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(14))),
56 new_deadline_(QuicTime::Zero()) {
60 alarm_
.Set(new_deadline_
);
63 MockDelegate
* delegate_
; // not owned
67 QuicTime new_deadline_
;
70 TEST_F(QuicAlarmTest
, IsSet
) {
71 EXPECT_FALSE(alarm_
.IsSet());
74 TEST_F(QuicAlarmTest
, Set
) {
75 QuicTime deadline
= QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7));
77 EXPECT_TRUE(alarm_
.IsSet());
78 EXPECT_TRUE(alarm_
.scheduled());
79 EXPECT_EQ(deadline
, alarm_
.deadline());
82 TEST_F(QuicAlarmTest
, Cancel
) {
83 QuicTime deadline
= QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7));
86 EXPECT_FALSE(alarm_
.IsSet());
87 EXPECT_FALSE(alarm_
.scheduled());
88 EXPECT_EQ(QuicTime::Zero(), alarm_
.deadline());
91 TEST_F(QuicAlarmTest
, Fire
) {
92 QuicTime deadline
= QuicTime::Zero().Add(QuicTime::Delta::FromSeconds(7));
94 EXPECT_CALL(*delegate_
, OnAlarm()).WillOnce(Return(QuicTime::Zero()));
96 EXPECT_FALSE(alarm_
.IsSet());
97 EXPECT_FALSE(alarm_
.scheduled());
98 EXPECT_EQ(QuicTime::Zero(), alarm_
.deadline());
101 TEST_F(QuicAlarmTest
, FireAndResetViaReturn
) {
102 alarm_
.Set(deadline_
);
103 EXPECT_CALL(*delegate_
, OnAlarm()).WillOnce(Return(deadline2_
));
105 EXPECT_TRUE(alarm_
.IsSet());
106 EXPECT_TRUE(alarm_
.scheduled());
107 EXPECT_EQ(deadline2_
, alarm_
.deadline());
110 TEST_F(QuicAlarmTest
, FireAndResetViaSet
) {
111 alarm_
.Set(deadline_
);
112 new_deadline_
= deadline2_
;
113 EXPECT_CALL(*delegate_
, OnAlarm()).WillOnce(DoAll(
114 Invoke(this, &QuicAlarmTest::ResetAlarm
),
115 Return(QuicTime::Zero())));
117 EXPECT_TRUE(alarm_
.IsSet());
118 EXPECT_TRUE(alarm_
.scheduled());
119 EXPECT_EQ(deadline2_
, alarm_
.deadline());