Roll src/third_party/skia f559de4:94e5010
[chromium-blink-merge.git] / remoting / test / test_chromoting_client_unittest.cc
blob9f232891905ce0420b1c4a28b8fa71146e2e324c
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 <string>
7 #include "base/message_loop/message_loop.h"
8 #include "remoting/protocol/fake_connection_to_host.h"
9 #include "remoting/test/test_chromoting_client.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace {
13 const char kTestUserName[] = "test_user@faux_address.com";
14 const char kAccessToken[] = "faux_access_token";
17 namespace remoting {
18 namespace test {
20 using testing::_;
22 // Provides base functionality for the TestChromotingClient Tests below. This
23 // fixture also creates an IO MessageLoop for use by the TestChromotingClient.
24 // Overrides a subset of the RemoteConnectionObserver interface to track
25 // connection status changes for result verification.
26 class TestChromotingClientTest : public ::testing::Test,
27 public RemoteConnectionObserver {
28 public:
29 TestChromotingClientTest();
30 ~TestChromotingClientTest() override;
32 protected:
33 // testing::Test interface.
34 void SetUp() override;
35 void TearDown() override;
37 // Used for result verification.
38 bool is_connected_to_host_;
39 protocol::ConnectionToHost::State connection_state_;
40 protocol::ErrorCode error_code_;
42 // Used for simulating different conditions for the TestChromotingClient.
43 RemoteHostInfo remote_host_info_;
44 FakeConnectionToHost* fake_connection_to_host_;
46 scoped_ptr<TestChromotingClient> test_chromoting_client_;
48 private:
49 // RemoteConnectionObserver interface.
50 void ConnectionStateChanged(protocol::ConnectionToHost::State state,
51 protocol::ErrorCode error_code) override;
52 void ConnectionReady(bool ready) override;
54 base::MessageLoopForIO message_loop_;
56 DISALLOW_COPY_AND_ASSIGN(TestChromotingClientTest);
59 TestChromotingClientTest::TestChromotingClientTest()
60 : is_connected_to_host_(false),
61 connection_state_(protocol::ConnectionToHost::INITIALIZING),
62 error_code_(protocol::OK),
63 fake_connection_to_host_(nullptr) {
66 TestChromotingClientTest::~TestChromotingClientTest() {
69 void TestChromotingClientTest::SetUp() {
70 test_chromoting_client_.reset(new TestChromotingClient());
71 test_chromoting_client_->AddRemoteConnectionObserver(this);
73 // Pass ownership of the FakeConnectionToHost to the chromoting instance but
74 // keep the ptr around so we can use it to simulate state changes. It will
75 // remain valid until |test_chromoting_client_| is destroyed.
76 fake_connection_to_host_ = new FakeConnectionToHost();
77 test_chromoting_client_->SetConnectionToHostForTests(
78 make_scoped_ptr(fake_connection_to_host_));
80 remote_host_info_.remote_host_status = kRemoteHostStatusReady;
83 void TestChromotingClientTest::TearDown() {
84 test_chromoting_client_->RemoveRemoteConnectionObserver(this);
85 fake_connection_to_host_ = nullptr;
87 // The chromoting instance must be destroyed before the message loop.
88 test_chromoting_client_.reset();
90 // The LibjingleTransportFactory destroys the PortAllocator via a DeleteSoon
91 // operation. If we do not allow the message loop to run here, we run the
92 // risk of the DeleteSoon task being dropped and incurring a memory leak.
93 message_loop_.RunUntilIdle();
96 void TestChromotingClientTest::ConnectionStateChanged(
97 protocol::ConnectionToHost::State state,
98 protocol::ErrorCode error_code) {
99 connection_state_ = state;
100 error_code_ = error_code;
102 if (state != protocol::ConnectionToHost::State::CONNECTED ||
103 error_code != protocol::OK) {
104 is_connected_to_host_ = false;
108 void TestChromotingClientTest::ConnectionReady(bool ready) {
109 if (ready) {
110 is_connected_to_host_ = true;
114 TEST_F(TestChromotingClientTest, StartConnectionAndDisconnect) {
115 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken,
116 remote_host_info_);
117 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
118 EXPECT_EQ(protocol::OK, error_code_);
119 EXPECT_FALSE(is_connected_to_host_);
121 // Simulate an AUTHENTICATED message being sent from the Jingle session.
122 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED,
123 protocol::OK);
124 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED,
125 connection_state_);
126 EXPECT_EQ(protocol::OK, error_code_);
127 EXPECT_FALSE(is_connected_to_host_);
129 // Simulate a CONNECTED message being sent from the Jingle session.
130 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED,
131 protocol::OK);
132 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
133 EXPECT_EQ(protocol::OK, error_code_);
134 EXPECT_FALSE(is_connected_to_host_);
136 fake_connection_to_host_->SignalConnectionReady(true);
137 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
138 EXPECT_EQ(protocol::OK, error_code_);
139 EXPECT_TRUE(is_connected_to_host_);
141 test_chromoting_client_->EndConnection();
142 EXPECT_EQ(protocol::ConnectionToHost::State::CLOSED, connection_state_);
143 EXPECT_EQ(protocol::OK, error_code_);
144 EXPECT_FALSE(is_connected_to_host_);
147 TEST_F(TestChromotingClientTest,
148 StartConnectionThenFailWithAuthenticationError) {
149 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken,
150 remote_host_info_);
151 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
152 EXPECT_EQ(protocol::OK, error_code_);
153 EXPECT_FALSE(is_connected_to_host_);
155 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED,
156 protocol::AUTHENTICATION_FAILED);
157 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
158 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_);
159 EXPECT_FALSE(is_connected_to_host_);
161 // Close the connection via the TestChromotingClient and verify the error
162 // state is persisted.
163 test_chromoting_client_->EndConnection();
164 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
165 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_);
166 EXPECT_FALSE(is_connected_to_host_);
169 TEST_F(TestChromotingClientTest, StartConnectionThenFailWithUnknownError) {
170 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken,
171 remote_host_info_);
172 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
173 EXPECT_EQ(protocol::OK, error_code_);
174 EXPECT_FALSE(is_connected_to_host_);
176 // Simulate an AUTHENTICATED message being sent from the Jingle session.
177 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED,
178 protocol::OK);
179 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED,
180 connection_state_);
181 EXPECT_EQ(protocol::OK, error_code_);
182 EXPECT_FALSE(is_connected_to_host_);
184 // Simulate a CONNECTED message being sent from the Jingle session.
185 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED,
186 protocol::OK);
187 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
188 EXPECT_EQ(protocol::OK, error_code_);
189 EXPECT_FALSE(is_connected_to_host_);
191 fake_connection_to_host_->SignalConnectionReady(true);
192 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
193 EXPECT_EQ(protocol::OK, error_code_);
194 EXPECT_TRUE(is_connected_to_host_);
196 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED,
197 protocol::UNKNOWN_ERROR);
198 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
199 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_);
200 EXPECT_FALSE(is_connected_to_host_);
202 // Close the connection via the TestChromotingClient and verify the error
203 // state is persisted.
204 test_chromoting_client_->EndConnection();
205 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
206 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_);
207 EXPECT_FALSE(is_connected_to_host_);
210 } // namespace test
211 } // namespace remoting