1 // Copyright 2015 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 "extensions/browser/api/cast_channel/keep_alive_delegate.h"
7 #include "base/timer/mock_timer.h"
8 #include "extensions/browser/api/cast_channel/test_util.h"
9 #include "net/base/net_errors.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 namespace cast_channel
{
20 const int64 kTestPingTimeoutMillis
= 1000;
21 const int64 kTestLivenessTimeoutMillis
= 10000;
23 // Extends MockTimer with a mockable method ResetTriggered() which permits
24 // test code to set GMock expectations for Timer::Reset().
25 class MockTimerWithMonitoredReset
: public base::MockTimer
{
27 MockTimerWithMonitoredReset(bool retain_user_task
, bool is_repeating
)
28 : base::MockTimer(retain_user_task
, is_repeating
) {}
29 ~MockTimerWithMonitoredReset() override
{}
31 // Instrumentation point for determining how many times Reset() was called.
32 MOCK_METHOD0(ResetTriggered
, void(void));
34 // Passes through the Reset call to the base MockTimer and visits the mock
35 // ResetTriggered method.
36 void Reset() override
{
37 base::MockTimer::Reset();
42 class KeepAliveDelegateTest
: public testing::Test
{
44 KeepAliveDelegateTest() {}
45 ~KeepAliveDelegateTest() override
{}
48 void SetUp() override
{
49 inner_delegate_
= new MockCastTransportDelegate
;
50 keep_alive_
.reset(new KeepAliveDelegate(
51 &socket_
, make_scoped_ptr(inner_delegate_
),
52 base::TimeDelta::FromMilliseconds(kTestPingTimeoutMillis
),
53 base::TimeDelta::FromMilliseconds(kTestLivenessTimeoutMillis
)));
54 liveness_timer_
= new MockTimerWithMonitoredReset(true, false);
55 ping_timer_
= new MockTimerWithMonitoredReset(true, false);
56 keep_alive_
->SetTimersForTest(make_scoped_ptr(ping_timer_
),
57 make_scoped_ptr(liveness_timer_
));
60 MockCastSocket socket_
;
61 scoped_ptr
<KeepAliveDelegate
> keep_alive_
;
62 MockCastTransportDelegate
* inner_delegate_
;
63 MockTimerWithMonitoredReset
* liveness_timer_
;
64 MockTimerWithMonitoredReset
* ping_timer_
;
66 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegateTest
);
69 TEST_F(KeepAliveDelegateTest
, TestPing
) {
70 EXPECT_CALL(*socket_
.mock_transport(),
71 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
72 KeepAliveDelegate::kHeartbeatPingType
)),
73 _
)).WillOnce(RunCompletionCallback
<1>(net::OK
));
74 EXPECT_CALL(*inner_delegate_
, Start());
75 EXPECT_CALL(*ping_timer_
, ResetTriggered()).Times(2);
76 EXPECT_CALL(*liveness_timer_
, ResetTriggered()).Times(2);
80 keep_alive_
->OnMessage(KeepAliveDelegate::CreateKeepAliveMessage(
81 KeepAliveDelegate::kHeartbeatPongType
));
84 TEST_F(KeepAliveDelegateTest
, TestPingFailed
) {
85 EXPECT_CALL(*socket_
.mock_transport(),
86 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
87 KeepAliveDelegate::kHeartbeatPingType
)),
89 .WillOnce(RunCompletionCallback
<1>(net::ERR_CONNECTION_RESET
));
90 EXPECT_CALL(*inner_delegate_
, Start());
91 EXPECT_CALL(*inner_delegate_
, OnError(CHANNEL_ERROR_SOCKET_ERROR
, _
));
92 EXPECT_CALL(*ping_timer_
, ResetTriggered()).Times(1);
93 EXPECT_CALL(*liveness_timer_
, ResetTriggered()).Times(1);
99 TEST_F(KeepAliveDelegateTest
, TestPingAndLivenessTimeout
) {
100 EXPECT_CALL(*socket_
.mock_transport(),
101 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
102 KeepAliveDelegate::kHeartbeatPingType
)),
103 _
)).WillOnce(RunCompletionCallback
<1>(net::OK
));
104 EXPECT_CALL(*inner_delegate_
, OnError(CHANNEL_ERROR_PING_TIMEOUT
, _
));
105 EXPECT_CALL(*inner_delegate_
, Start());
106 EXPECT_CALL(*ping_timer_
, ResetTriggered()).Times(1);
107 EXPECT_CALL(*liveness_timer_
, ResetTriggered()).Times(1);
109 keep_alive_
->Start();
111 liveness_timer_
->Fire();
114 TEST_F(KeepAliveDelegateTest
, TestResetTimersAndPassthroughAllOtherTraffic
) {
115 CastMessage other_message
=
116 KeepAliveDelegate::CreateKeepAliveMessage("NEITHER_PING_NOR_PONG");
118 EXPECT_CALL(*inner_delegate_
, OnMessage(EqualsProto(other_message
)));
119 EXPECT_CALL(*inner_delegate_
, Start());
120 EXPECT_CALL(*ping_timer_
, ResetTriggered()).Times(2);
121 EXPECT_CALL(*liveness_timer_
, ResetTriggered()).Times(2);
123 keep_alive_
->Start();
124 keep_alive_
->OnMessage(other_message
);
128 } // namespace cast_channel
129 } // namespace core_api
130 } // namespace extensions