Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / test / test_chromoting_client_unittest.cc
blob9ae48e70a16ba2bc9e7ed528f99a5463c8346143
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/connection_setup_info.h"
10 #include "remoting/test/test_chromoting_client.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace {
14 const char kTestUserName[] = "test_user@faux_address.com";
15 const char kAccessToken[] = "faux_access_token";
18 namespace remoting {
19 namespace test {
21 using testing::_;
23 // Provides base functionality for the TestChromotingClient Tests below. This
24 // fixture also creates an IO MessageLoop for use by the TestChromotingClient.
25 // Overrides a subset of the RemoteConnectionObserver interface to track
26 // connection status changes for result verification.
27 class TestChromotingClientTest : public ::testing::Test,
28 public RemoteConnectionObserver {
29 public:
30 TestChromotingClientTest();
31 ~TestChromotingClientTest() override;
33 protected:
34 // testing::Test interface.
35 void SetUp() override;
36 void TearDown() override;
38 // Used for result verification.
39 bool is_connected_to_host_;
40 protocol::ConnectionToHost::State connection_state_;
41 protocol::ErrorCode error_code_;
43 // Used for simulating different conditions for the TestChromotingClient.
44 ConnectionSetupInfo connection_setup_info_;
45 FakeConnectionToHost* fake_connection_to_host_;
47 scoped_ptr<TestChromotingClient> test_chromoting_client_;
49 private:
50 // RemoteConnectionObserver interface.
51 void ConnectionStateChanged(protocol::ConnectionToHost::State state,
52 protocol::ErrorCode error_code) override;
53 void ConnectionReady(bool ready) override;
55 base::MessageLoopForIO message_loop_;
57 DISALLOW_COPY_AND_ASSIGN(TestChromotingClientTest);
60 TestChromotingClientTest::TestChromotingClientTest()
61 : is_connected_to_host_(false),
62 connection_state_(protocol::ConnectionToHost::INITIALIZING),
63 error_code_(protocol::OK),
64 fake_connection_to_host_(nullptr) {
67 TestChromotingClientTest::~TestChromotingClientTest() {
70 void TestChromotingClientTest::SetUp() {
71 test_chromoting_client_.reset(new TestChromotingClient());
72 test_chromoting_client_->AddRemoteConnectionObserver(this);
74 // Pass ownership of the FakeConnectionToHost to the chromoting instance but
75 // keep the ptr around so we can use it to simulate state changes. It will
76 // remain valid until |test_chromoting_client_| is destroyed.
77 fake_connection_to_host_ = new FakeConnectionToHost();
78 test_chromoting_client_->SetConnectionToHostForTests(
79 make_scoped_ptr(fake_connection_to_host_));
81 connection_setup_info_.access_token = kAccessToken;
82 connection_setup_info_.user_name = kTestUserName;
83 connection_setup_info_.auth_methods.push_back(
84 protocol::AuthenticationMethod::ThirdParty());
87 void TestChromotingClientTest::TearDown() {
88 test_chromoting_client_->RemoveRemoteConnectionObserver(this);
89 fake_connection_to_host_ = nullptr;
91 // The chromoting instance must be destroyed before the message loop.
92 test_chromoting_client_.reset();
94 // The LibjingleTransportFactory destroys the PortAllocator via a DeleteSoon
95 // operation. If we do not allow the message loop to run here, we run the
96 // risk of the DeleteSoon task being dropped and incurring a memory leak.
97 message_loop_.RunUntilIdle();
100 void TestChromotingClientTest::ConnectionStateChanged(
101 protocol::ConnectionToHost::State state,
102 protocol::ErrorCode error_code) {
103 connection_state_ = state;
104 error_code_ = error_code;
106 if (state != protocol::ConnectionToHost::State::CONNECTED ||
107 error_code != protocol::OK) {
108 is_connected_to_host_ = false;
112 void TestChromotingClientTest::ConnectionReady(bool ready) {
113 if (ready) {
114 is_connected_to_host_ = true;
118 TEST_F(TestChromotingClientTest, StartConnectionAndDisconnect) {
119 test_chromoting_client_->StartConnection(connection_setup_info_);
120 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
121 EXPECT_EQ(protocol::OK, error_code_);
122 EXPECT_FALSE(is_connected_to_host_);
124 // Simulate an AUTHENTICATED message being sent from the Jingle session.
125 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED,
126 protocol::OK);
127 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED,
128 connection_state_);
129 EXPECT_EQ(protocol::OK, error_code_);
130 EXPECT_FALSE(is_connected_to_host_);
132 // Simulate a CONNECTED message being sent from the Jingle session.
133 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED,
134 protocol::OK);
135 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
136 EXPECT_EQ(protocol::OK, error_code_);
137 EXPECT_FALSE(is_connected_to_host_);
139 fake_connection_to_host_->SignalConnectionReady(true);
140 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
141 EXPECT_EQ(protocol::OK, error_code_);
142 EXPECT_TRUE(is_connected_to_host_);
144 test_chromoting_client_->EndConnection();
145 EXPECT_EQ(protocol::ConnectionToHost::State::CLOSED, connection_state_);
146 EXPECT_EQ(protocol::OK, error_code_);
147 EXPECT_FALSE(is_connected_to_host_);
150 TEST_F(TestChromotingClientTest,
151 StartConnectionThenFailWithAuthenticationError) {
152 test_chromoting_client_->StartConnection(connection_setup_info_);
153 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
154 EXPECT_EQ(protocol::OK, error_code_);
155 EXPECT_FALSE(is_connected_to_host_);
157 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED,
158 protocol::AUTHENTICATION_FAILED);
159 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
160 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_);
161 EXPECT_FALSE(is_connected_to_host_);
163 // Close the connection via the TestChromotingClient and verify the error
164 // state is persisted.
165 test_chromoting_client_->EndConnection();
166 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
167 EXPECT_EQ(protocol::ErrorCode::AUTHENTICATION_FAILED, error_code_);
168 EXPECT_FALSE(is_connected_to_host_);
171 TEST_F(TestChromotingClientTest, StartConnectionThenFailWithUnknownError) {
172 test_chromoting_client_->StartConnection(connection_setup_info_);
173 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_);
174 EXPECT_EQ(protocol::OK, error_code_);
175 EXPECT_FALSE(is_connected_to_host_);
177 // Simulate an AUTHENTICATED message being sent from the Jingle session.
178 fake_connection_to_host_->SignalStateChange(protocol::Session::AUTHENTICATED,
179 protocol::OK);
180 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED,
181 connection_state_);
182 EXPECT_EQ(protocol::OK, error_code_);
183 EXPECT_FALSE(is_connected_to_host_);
185 // Simulate a CONNECTED message being sent from the Jingle session.
186 fake_connection_to_host_->SignalStateChange(protocol::Session::CONNECTED,
187 protocol::OK);
188 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
189 EXPECT_EQ(protocol::OK, error_code_);
190 EXPECT_FALSE(is_connected_to_host_);
192 fake_connection_to_host_->SignalConnectionReady(true);
193 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_);
194 EXPECT_EQ(protocol::OK, error_code_);
195 EXPECT_TRUE(is_connected_to_host_);
197 fake_connection_to_host_->SignalStateChange(protocol::Session::FAILED,
198 protocol::UNKNOWN_ERROR);
199 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
200 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_);
201 EXPECT_FALSE(is_connected_to_host_);
203 // Close the connection via the TestChromotingClient and verify the error
204 // state is persisted.
205 test_chromoting_client_->EndConnection();
206 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_);
207 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_);
208 EXPECT_FALSE(is_connected_to_host_);
211 } // namespace test
212 } // namespace remoting