Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / keep_alive_delegate_unittest.cc
blob69e7fac0762ada9d658b3e41ebe7e91c54070e33
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/test/simple_test_tick_clock.h"
8 #include "base/timer/mock_timer.h"
9 #include "extensions/browser/api/cast_channel/cast_test_util.h"
10 #include "net/base/net_errors.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::_;
16 namespace extensions {
17 namespace api {
18 namespace cast_channel {
19 namespace {
21 const int64 kTestPingTimeoutMillis = 1000;
22 const int64 kTestLivenessTimeoutMillis = 10000;
24 // Extends MockTimer with a mockable method ResetTriggered() which permits
25 // test code to set GMock expectations for Timer::Reset().
26 class MockTimerWithMonitoredReset : public base::MockTimer {
27 public:
28 MockTimerWithMonitoredReset(bool retain_user_task, bool is_repeating)
29 : base::MockTimer(retain_user_task, is_repeating) {}
30 ~MockTimerWithMonitoredReset() override {}
32 // Instrumentation point for determining how many times Reset() was called.
33 MOCK_METHOD0(ResetTriggered, void(void));
34 MOCK_METHOD0(Stop, void(void));
36 // Passes through the Reset call to the base MockTimer and visits the mock
37 // ResetTriggered method.
38 void Reset() override {
39 base::MockTimer::Reset();
40 ResetTriggered();
44 class KeepAliveDelegateTest : public testing::Test {
45 public:
46 KeepAliveDelegateTest() {}
47 ~KeepAliveDelegateTest() override {}
49 protected:
50 void SetUp() override {
51 inner_delegate_ = new MockCastTransportDelegate;
52 logger_ =
53 new Logger(scoped_ptr<base::TickClock>(new base::SimpleTestTickClock),
54 base::TimeTicks());
55 keep_alive_.reset(new KeepAliveDelegate(
56 &socket_, logger_, make_scoped_ptr(inner_delegate_),
57 base::TimeDelta::FromMilliseconds(kTestPingTimeoutMillis),
58 base::TimeDelta::FromMilliseconds(kTestLivenessTimeoutMillis)));
59 liveness_timer_ = new MockTimerWithMonitoredReset(true, false);
60 ping_timer_ = new MockTimerWithMonitoredReset(true, false);
61 EXPECT_CALL(*liveness_timer_, Stop()).Times(0);
62 EXPECT_CALL(*ping_timer_, Stop()).Times(0);
63 keep_alive_->SetTimersForTest(make_scoped_ptr(ping_timer_),
64 make_scoped_ptr(liveness_timer_));
67 MockCastSocket socket_;
68 scoped_ptr<KeepAliveDelegate> keep_alive_;
69 scoped_refptr<Logger> logger_;
70 MockCastTransportDelegate* inner_delegate_;
71 MockTimerWithMonitoredReset* liveness_timer_;
72 MockTimerWithMonitoredReset* ping_timer_;
74 private:
75 DISALLOW_COPY_AND_ASSIGN(KeepAliveDelegateTest);
78 TEST_F(KeepAliveDelegateTest, TestErrorHandledBeforeStarting) {
79 keep_alive_->OnError(CHANNEL_ERROR_CONNECT_ERROR);
82 TEST_F(KeepAliveDelegateTest, TestPing) {
83 EXPECT_CALL(*socket_.mock_transport(),
84 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
85 KeepAliveDelegate::kHeartbeatPingType)),
86 _)).WillOnce(RunCompletionCallback<1>(net::OK));
87 EXPECT_CALL(*inner_delegate_, Start());
88 EXPECT_CALL(*ping_timer_, ResetTriggered()).Times(2);
89 EXPECT_CALL(*liveness_timer_, ResetTriggered()).Times(2);
90 EXPECT_CALL(*ping_timer_, Stop());
92 keep_alive_->Start();
93 ping_timer_->Fire();
94 keep_alive_->OnMessage(KeepAliveDelegate::CreateKeepAliveMessage(
95 KeepAliveDelegate::kHeartbeatPongType));
98 TEST_F(KeepAliveDelegateTest, TestPingFailed) {
99 EXPECT_CALL(*socket_.mock_transport(),
100 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
101 KeepAliveDelegate::kHeartbeatPingType)),
103 .WillOnce(RunCompletionCallback<1>(net::ERR_CONNECTION_RESET));
104 EXPECT_CALL(*inner_delegate_, Start());
105 EXPECT_CALL(*inner_delegate_, OnError(CHANNEL_ERROR_SOCKET_ERROR));
106 EXPECT_CALL(*ping_timer_, ResetTriggered()).Times(1);
107 EXPECT_CALL(*liveness_timer_, ResetTriggered()).Times(1);
108 EXPECT_CALL(*liveness_timer_, Stop());
109 EXPECT_CALL(*ping_timer_, Stop()).Times(2);
111 keep_alive_->Start();
112 ping_timer_->Fire();
113 EXPECT_EQ(proto::PING_WRITE_ERROR,
114 logger_->GetLastErrors(socket_.id()).event_type);
115 EXPECT_EQ(net::ERR_CONNECTION_RESET,
116 logger_->GetLastErrors(socket_.id()).net_return_value);
119 TEST_F(KeepAliveDelegateTest, TestPingAndLivenessTimeout) {
120 EXPECT_CALL(*socket_.mock_transport(),
121 SendMessage(EqualsProto(KeepAliveDelegate::CreateKeepAliveMessage(
122 KeepAliveDelegate::kHeartbeatPingType)),
123 _)).WillOnce(RunCompletionCallback<1>(net::OK));
124 EXPECT_CALL(*inner_delegate_, OnError(CHANNEL_ERROR_PING_TIMEOUT));
125 EXPECT_CALL(*inner_delegate_, Start());
126 EXPECT_CALL(*ping_timer_, ResetTriggered()).Times(1);
127 EXPECT_CALL(*liveness_timer_, ResetTriggered()).Times(1);
128 EXPECT_CALL(*liveness_timer_, Stop()).Times(2);
129 EXPECT_CALL(*ping_timer_, Stop()).Times(2);
131 keep_alive_->Start();
132 ping_timer_->Fire();
133 liveness_timer_->Fire();
136 TEST_F(KeepAliveDelegateTest, TestResetTimersAndPassthroughAllOtherTraffic) {
137 CastMessage other_message =
138 KeepAliveDelegate::CreateKeepAliveMessage("NEITHER_PING_NOR_PONG");
140 EXPECT_CALL(*inner_delegate_, OnMessage(EqualsProto(other_message)));
141 EXPECT_CALL(*inner_delegate_, Start());
142 EXPECT_CALL(*ping_timer_, ResetTriggered()).Times(2);
143 EXPECT_CALL(*liveness_timer_, ResetTriggered()).Times(2);
145 keep_alive_->Start();
146 keep_alive_->OnMessage(other_message);
149 } // namespace
150 } // namespace cast_channel
151 } // namespace api
152 } // namespace extensions