OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <string> | |
6 | |
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" | |
11 | |
12 namespace { | |
13 const char kTestUserName[] = "test_user@faux_address.com"; | |
14 const char kAccessToken[] = "faux_access_token"; | |
15 } | |
16 | |
17 namespace remoting { | |
18 namespace test { | |
19 | |
20 using testing::_; | |
21 | |
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, | |
Sergey Ulanov
2015/03/11 00:09:52
I'm not sure how useful it is to unittest test cod
joedow
2015/03/11 19:06:39
Acknowledged.
| |
27 public RemoteConnectionObserver { | |
28 public: | |
29 TestChromotingClientTest(); | |
30 ~TestChromotingClientTest() override; | |
31 | |
32 protected: | |
33 // testing::Test interface. | |
34 void SetUp() override; | |
35 void TearDown() override; | |
36 | |
37 // Used for result verification. | |
38 bool is_connected_to_host_; | |
39 protocol::ConnectionToHost::State connection_state_; | |
40 protocol::ErrorCode error_code_; | |
41 | |
42 // Used for simulating different conditions for the TestChromotingClient. | |
43 RemoteHostInfo remote_host_info_; | |
44 FakeConnectionToHost* fake_connection_to_host_; | |
45 | |
46 scoped_ptr<TestChromotingClient> test_chromoting_client_; | |
47 | |
48 private: | |
49 // RemoteConnectionObserver interface. | |
50 void ConnectionStateChanged(protocol::ConnectionToHost::State state, | |
51 protocol::ErrorCode error_code) override; | |
52 void ConnectionReady(bool ready) override; | |
53 | |
54 base::MessageLoopForIO message_loop_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(TestChromotingClientTest); | |
57 }; | |
58 | |
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) { | |
64 } | |
65 | |
66 TestChromotingClientTest::~TestChromotingClientTest() { | |
67 } | |
68 | |
69 void TestChromotingClientTest::SetUp() { | |
70 test_chromoting_client_.reset(new TestChromotingClient()); | |
71 test_chromoting_client_->AddRemoteConnectionObserver(this); | |
72 | |
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_)); | |
79 | |
80 remote_host_info_.remote_host_status = kRemoteHostStatusReady; | |
81 } | |
82 | |
83 void TestChromotingClientTest::TearDown() { | |
84 test_chromoting_client_->RemoveRemoteConnectionObserver(this); | |
85 fake_connection_to_host_ = nullptr; | |
86 | |
87 // The chromoting instance must be destroyed before the message loop. | |
88 test_chromoting_client_.reset(); | |
89 | |
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(); | |
94 } | |
95 | |
96 void TestChromotingClientTest::ConnectionStateChanged( | |
97 protocol::ConnectionToHost::State state, | |
98 protocol::ErrorCode error_code) { | |
99 connection_state_ = state; | |
100 error_code_ = error_code; | |
101 | |
102 if (state != protocol::ConnectionToHost::State::CONNECTED || | |
103 error_code != protocol::ErrorCode::OK) { | |
104 is_connected_to_host_ = false; | |
105 } | |
106 } | |
107 | |
108 void TestChromotingClientTest::ConnectionReady(bool ready) { | |
109 if (ready) { | |
110 is_connected_to_host_ = true; | |
111 } | |
112 } | |
113 | |
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::ErrorCode::OK, error_code_); | |
119 EXPECT_FALSE(is_connected_to_host_); | |
120 | |
121 // Simulate an AUTHENTICATED message being sent from the Jingle session. | |
122 fake_connection_to_host_->OnSessionStateChange( | |
123 protocol::Session::AUTHENTICATED); | |
124 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED, | |
125 connection_state_); | |
126 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
127 EXPECT_FALSE(is_connected_to_host_); | |
128 | |
129 // Simulate a CONNECTED message being sent from the Jingle session. | |
130 fake_connection_to_host_->OnSessionStateChange(protocol::Session::CONNECTED); | |
131 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); | |
132 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
133 EXPECT_FALSE(is_connected_to_host_); | |
134 | |
135 // TestChromotingClient will consider its connection to the host | |
136 // complete once the video channel is active. | |
137 fake_connection_to_host_->OnVideoChannelStatus(true); | |
138 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); | |
139 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
140 EXPECT_TRUE(is_connected_to_host_); | |
141 | |
142 test_chromoting_client_->EndConnection(); | |
143 EXPECT_EQ(protocol::ConnectionToHost::State::CLOSED, connection_state_); | |
144 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
145 EXPECT_FALSE(is_connected_to_host_); | |
146 } | |
147 | |
148 TEST_F(TestChromotingClientTest, | |
149 StartConnectionThenFailWithAuthenticationError) { | |
150 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken, | |
151 remote_host_info_); | |
152 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_); | |
153 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
154 EXPECT_FALSE(is_connected_to_host_); | |
155 | |
156 fake_connection_to_host_->set_session_state_change_error_code( | |
157 protocol::AUTHENTICATION_FAILED); | |
158 fake_connection_to_host_->OnSessionStateChange(protocol::Session::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_); | |
162 | |
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_); | |
169 } | |
170 | |
171 TEST_F(TestChromotingClientTest, StartConnectionThenFailWithUnknownError) { | |
172 test_chromoting_client_->StartConnection(kTestUserName, kAccessToken, | |
173 remote_host_info_); | |
174 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTING, connection_state_); | |
175 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
176 EXPECT_FALSE(is_connected_to_host_); | |
177 | |
178 // Simulate an AUTHENTICATED message being sent from the Jingle session. | |
179 fake_connection_to_host_->OnSessionStateChange( | |
180 protocol::Session::AUTHENTICATED); | |
181 EXPECT_EQ(protocol::ConnectionToHost::State::AUTHENTICATED, | |
182 connection_state_); | |
183 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
184 EXPECT_FALSE(is_connected_to_host_); | |
185 | |
186 // Simulate a CONNECTED message being sent from the Jingle session. | |
187 fake_connection_to_host_->OnSessionStateChange(protocol::Session::CONNECTED); | |
188 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); | |
189 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
190 EXPECT_FALSE(is_connected_to_host_); | |
191 | |
192 // TestChromotingClient will consider its connection to the host complete | |
193 // once the video channel is active. | |
194 fake_connection_to_host_->OnVideoChannelStatus(true); | |
195 EXPECT_EQ(protocol::ConnectionToHost::State::CONNECTED, connection_state_); | |
196 EXPECT_EQ(protocol::ErrorCode::OK, error_code_); | |
197 EXPECT_TRUE(is_connected_to_host_); | |
198 | |
199 fake_connection_to_host_->set_session_state_change_error_code( | |
200 protocol::UNKNOWN_ERROR); | |
201 fake_connection_to_host_->OnSessionStateChange(protocol::Session::FAILED); | |
202 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); | |
203 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_); | |
204 EXPECT_FALSE(is_connected_to_host_); | |
205 | |
206 // Close the connection via the TestChromotingClient and verify the error | |
207 // state is persisted. | |
208 test_chromoting_client_->EndConnection(); | |
209 EXPECT_EQ(protocol::ConnectionToHost::State::FAILED, connection_state_); | |
210 EXPECT_EQ(protocol::ErrorCode::UNKNOWN_ERROR, error_code_); | |
211 EXPECT_FALSE(is_connected_to_host_); | |
212 } | |
213 | |
214 } // namespace test | |
215 } // namespace remoting | |
OLD | NEW |