OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/task.h" | 5 #include "base/task.h" |
6 #include "remoting/host/capturer_fake.h" | 6 #include "remoting/host/capturer_fake.h" |
7 #include "remoting/host/chromoting_host.h" | 7 #include "remoting/host/chromoting_host.h" |
8 #include "remoting/host/chromoting_host_context.h" | 8 #include "remoting/host/chromoting_host_context.h" |
9 #include "remoting/host/desktop_environment_fake.h" | 9 #include "remoting/host/desktop_environment_fake.h" |
10 #include "remoting/host/host_mock_objects.h" | 10 #include "remoting/host/host_mock_objects.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 using testing::Return; | 34 using testing::Return; |
35 | 35 |
36 namespace remoting { | 36 namespace remoting { |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 void PostQuitTask(MessageLoop* message_loop) { | 40 void PostQuitTask(MessageLoop* message_loop) { |
41 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 41 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
42 } | 42 } |
43 | 43 |
44 void BeginSessionRequest(protocol::HostStub* host_stub) { | 44 void BeginSessionRequest(protocol::ConnectionToClient* connection, |
| 45 protocol::HostStub* host_stub) { |
45 LocalLoginCredentials* credentials = | 46 LocalLoginCredentials* credentials = |
46 new LocalLoginCredentials(); | 47 new LocalLoginCredentials(); |
47 credentials->set_type(protocol::PASSWORD); | 48 credentials->set_type(protocol::PASSWORD); |
48 credentials->set_username("hello"); | 49 credentials->set_username("hello"); |
49 | 50 |
50 const std::string password = "world!"; | 51 const std::string password = "world!"; |
51 credentials->set_credential(password.data(), password.length()); | 52 credentials->set_credential(password.data(), password.length()); |
52 | 53 |
53 host_stub->BeginSessionRequest( | 54 host_stub->BeginSessionRequest( |
| 55 connection, |
54 credentials, | 56 credentials, |
55 new DeleteTask<LocalLoginCredentials>(credentials)); | 57 new DeleteTask<LocalLoginCredentials>(credentials)); |
56 } | 58 } |
57 | 59 |
58 // Run the task and delete it afterwards. This action is used to deal with | 60 // Run the task and delete it afterwards. This action is used to deal with |
59 // done callbacks. | 61 // done callbacks. |
60 ACTION(RunDoneTask) { | 62 ACTION(RunDoneTask) { |
61 arg1->Run(); | 63 arg1->Run(); |
62 delete arg1; | 64 delete arg1; |
63 } | 65 } |
(...skipping 19 matching lines...) Expand all Loading... |
83 .WillByDefault(Return(&message_loop_)); | 85 .WillByDefault(Return(&message_loop_)); |
84 EXPECT_CALL(context_, main_message_loop()) | 86 EXPECT_CALL(context_, main_message_loop()) |
85 .Times(AnyNumber()); | 87 .Times(AnyNumber()); |
86 EXPECT_CALL(context_, encode_message_loop()) | 88 EXPECT_CALL(context_, encode_message_loop()) |
87 .Times(AnyNumber()); | 89 .Times(AnyNumber()); |
88 EXPECT_CALL(context_, network_message_loop()) | 90 EXPECT_CALL(context_, network_message_loop()) |
89 .Times(AnyNumber()); | 91 .Times(AnyNumber()); |
90 | 92 |
91 Capturer* capturer = new CapturerFake(context_.main_message_loop()); | 93 Capturer* capturer = new CapturerFake(context_.main_message_loop()); |
92 host_stub_ = new MockHostStub(); | 94 host_stub_ = new MockHostStub(); |
| 95 host_stub2_ = new MockHostStub(); |
93 input_stub_ = new MockInputStub(); | 96 input_stub_ = new MockInputStub(); |
| 97 input_stub2_ = new MockInputStub(); |
94 DesktopEnvironment* desktop = | 98 DesktopEnvironment* desktop = |
95 new DesktopEnvironmentFake(capturer, input_stub_); | 99 new DesktopEnvironmentFake(capturer, input_stub_); |
96 host_ = ChromotingHost::Create(&context_, config_, desktop); | 100 host_ = ChromotingHost::Create(&context_, config_, desktop); |
97 connection_ = new MockConnectionToClient( | 101 connection_ = new MockConnectionToClient( |
98 &message_loop_, &handler_, host_stub_, input_stub_); | 102 &message_loop_, &handler_, host_stub_, input_stub_); |
| 103 connection2_ = new MockConnectionToClient( |
| 104 &message_loop_, &handler_, host_stub2_, input_stub2_); |
99 session_ = new MockSession(); | 105 session_ = new MockSession(); |
| 106 session2_ = new MockSession(); |
100 session_config_.reset(SessionConfig::CreateDefault()); | 107 session_config_.reset(SessionConfig::CreateDefault()); |
| 108 session_config2_.reset(SessionConfig::CreateDefault()); |
101 | 109 |
102 ON_CALL(video_stub_, ProcessVideoPacket(_, _)) | 110 ON_CALL(video_stub_, ProcessVideoPacket(_, _)) |
103 .WillByDefault( | 111 .WillByDefault( |
104 DoAll(DeleteArg<0>(), DeleteArg<1>())); | 112 DoAll(DeleteArg<0>(), DeleteArg<1>())); |
| 113 ON_CALL(video_stub2_, ProcessVideoPacket(_, _)) |
| 114 .WillByDefault( |
| 115 DoAll(DeleteArg<0>(), DeleteArg<1>())); |
105 ON_CALL(*connection_.get(), video_stub()) | 116 ON_CALL(*connection_.get(), video_stub()) |
106 .WillByDefault(Return(&video_stub_)); | 117 .WillByDefault(Return(&video_stub_)); |
107 ON_CALL(*connection_.get(), client_stub()) | 118 ON_CALL(*connection_.get(), client_stub()) |
108 .WillByDefault(Return(&client_stub_)); | 119 .WillByDefault(Return(&client_stub_)); |
109 ON_CALL(*connection_.get(), session()) | 120 ON_CALL(*connection_.get(), session()) |
110 .WillByDefault(Return(session_)); | 121 .WillByDefault(Return(session_)); |
| 122 ON_CALL(*connection2_.get(), video_stub()) |
| 123 .WillByDefault(Return(&video_stub2_)); |
| 124 ON_CALL(*connection2_.get(), client_stub()) |
| 125 .WillByDefault(Return(&client_stub2_)); |
| 126 ON_CALL(*connection2_.get(), session()) |
| 127 .WillByDefault(Return(session2_)); |
111 ON_CALL(*session_.get(), config()) | 128 ON_CALL(*session_.get(), config()) |
112 .WillByDefault(Return(session_config_.get())); | 129 .WillByDefault(Return(session_config_.get())); |
| 130 ON_CALL(*session2_.get(), config()) |
| 131 .WillByDefault(Return(session_config2_.get())); |
113 EXPECT_CALL(*connection_.get(), video_stub()) | 132 EXPECT_CALL(*connection_.get(), video_stub()) |
114 .Times(AnyNumber()); | 133 .Times(AnyNumber()); |
115 EXPECT_CALL(*connection_.get(), client_stub()) | 134 EXPECT_CALL(*connection_.get(), client_stub()) |
116 .Times(AnyNumber()); | 135 .Times(AnyNumber()); |
117 EXPECT_CALL(*connection_.get(), session()) | 136 EXPECT_CALL(*connection_.get(), session()) |
118 .Times(AnyNumber()); | 137 .Times(AnyNumber()); |
| 138 EXPECT_CALL(*connection2_.get(), video_stub()) |
| 139 .Times(AnyNumber()); |
| 140 EXPECT_CALL(*connection2_.get(), client_stub()) |
| 141 .Times(AnyNumber()); |
| 142 EXPECT_CALL(*connection2_.get(), session()) |
| 143 .Times(AnyNumber()); |
119 EXPECT_CALL(*session_.get(), config()) | 144 EXPECT_CALL(*session_.get(), config()) |
120 .Times(AnyNumber()); | 145 .Times(AnyNumber()); |
| 146 EXPECT_CALL(*session2_.get(), config()) |
| 147 .Times(AnyNumber()); |
| 148 |
121 } | 149 } |
122 | 150 |
123 virtual void TearDown() { | 151 virtual void TearDown() { |
124 } | 152 } |
125 | 153 |
126 // Helper metjod to pretend a client is connected to ChromotingHost. | 154 // Helper method to pretend a client is connected to ChromotingHost. |
127 void SimulateClientConnection() { | 155 void SimulateClientConnection(int connection_index) { |
| 156 scoped_refptr<MockConnectionToClient> connection = |
| 157 (connection_index == 0) ? connection_ : connection2_; |
| 158 |
128 context_.network_message_loop()->PostTask( | 159 context_.network_message_loop()->PostTask( |
129 FROM_HERE, | 160 FROM_HERE, |
130 NewRunnableMethod(host_.get(), | 161 NewRunnableMethod(host_.get(), |
131 &ChromotingHost::set_connection, | 162 &ChromotingHost::add_connection, |
132 connection_)); | 163 connection)); |
133 context_.network_message_loop()->PostTask( | 164 context_.network_message_loop()->PostTask( |
134 FROM_HERE, | 165 FROM_HERE, |
135 NewRunnableMethod(host_.get(), | 166 NewRunnableMethod(host_.get(), |
136 &ChromotingHost::OnClientConnected, | 167 &ChromotingHost::OnClientConnected, |
137 connection_)); | 168 connection)); |
138 context_.network_message_loop()->PostTask( | 169 context_.network_message_loop()->PostTask( |
139 FROM_HERE, | 170 FROM_HERE, |
140 NewRunnableFunction(&BeginSessionRequest, host_->host_stub())); | 171 NewRunnableFunction(&BeginSessionRequest, |
| 172 connection, |
| 173 host_->host_stub())); |
141 } | 174 } |
142 | 175 |
143 // Helper method to remove a client connection from ChromotongHost. | 176 void SimulateSecondClientConnection() { |
| 177 SimulateClientConnection(1); |
| 178 } |
| 179 |
| 180 // Helper method to remove a client connection from ChromotingHost. |
144 void RemoveClientConnection() { | 181 void RemoveClientConnection() { |
145 context_.network_message_loop()->PostTask( | 182 context_.network_message_loop()->PostTask( |
146 FROM_HERE, | 183 FROM_HERE, |
147 NewRunnableMethod(host_.get(), | 184 NewRunnableMethod(host_.get(), |
148 &ChromotingHost::OnClientDisconnected, | 185 &ChromotingHost::OnClientDisconnected, |
149 connection_)); | 186 connection_)); |
150 } | 187 } |
151 | 188 |
152 protected: | 189 protected: |
153 MessageLoop message_loop_; | 190 MessageLoop message_loop_; |
154 MockConnectionToClientEventHandler handler_; | 191 MockConnectionToClientEventHandler handler_; |
155 scoped_refptr<ChromotingHost> host_; | 192 scoped_refptr<ChromotingHost> host_; |
156 scoped_refptr<InMemoryHostConfig> config_; | 193 scoped_refptr<InMemoryHostConfig> config_; |
157 MockChromotingHostContext context_; | 194 MockChromotingHostContext context_; |
158 scoped_refptr<MockConnectionToClient> connection_; | 195 scoped_refptr<MockConnectionToClient> connection_; |
159 scoped_refptr<MockSession> session_; | 196 scoped_refptr<MockSession> session_; |
160 scoped_ptr<SessionConfig> session_config_; | 197 scoped_ptr<SessionConfig> session_config_; |
161 MockVideoStub video_stub_; | 198 MockVideoStub video_stub_; |
162 MockClientStub client_stub_; | 199 MockClientStub client_stub_; |
163 MockHostStub* host_stub_; | 200 MockHostStub* host_stub_; |
164 MockInputStub* input_stub_; | 201 MockInputStub* input_stub_; |
| 202 scoped_refptr<MockConnectionToClient> connection2_; |
| 203 scoped_refptr<MockSession> session2_; |
| 204 scoped_ptr<SessionConfig> session_config2_; |
| 205 MockVideoStub video_stub2_; |
| 206 MockClientStub client_stub2_; |
| 207 MockHostStub* host_stub2_; |
| 208 MockInputStub* input_stub2_; |
165 }; | 209 }; |
166 | 210 |
167 TEST_F(ChromotingHostTest, StartAndShutdown) { | 211 TEST_F(ChromotingHostTest, StartAndShutdown) { |
168 host_->Start(NewRunnableFunction(&PostQuitTask, &message_loop_)); | 212 host_->Start(NewRunnableFunction(&PostQuitTask, &message_loop_)); |
169 | 213 |
170 message_loop_.PostTask(FROM_HERE, | 214 message_loop_.PostTask(FROM_HERE, |
171 NewRunnableMethod(host_.get(), | 215 NewRunnableMethod(host_.get(), |
172 &ChromotingHost::Shutdown)); | 216 &ChromotingHost::Shutdown)); |
173 message_loop_.Run(); | 217 message_loop_.Run(); |
174 } | 218 } |
(...skipping 10 matching lines...) Expand all Loading... |
185 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 229 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
186 .WillOnce(DoAll( | 230 .WillOnce(DoAll( |
187 InvokeWithoutArgs(host_.get(), &ChromotingHost::Shutdown), | 231 InvokeWithoutArgs(host_.get(), &ChromotingHost::Shutdown), |
188 RunDoneTask())) | 232 RunDoneTask())) |
189 .RetiresOnSaturation(); | 233 .RetiresOnSaturation(); |
190 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 234 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
191 .Times(AnyNumber()); | 235 .Times(AnyNumber()); |
192 EXPECT_CALL(*connection_.get(), Disconnect()) | 236 EXPECT_CALL(*connection_.get(), Disconnect()) |
193 .RetiresOnSaturation(); | 237 .RetiresOnSaturation(); |
194 | 238 |
195 SimulateClientConnection(); | 239 SimulateClientConnection(0); |
196 message_loop_.Run(); | 240 message_loop_.Run(); |
197 } | 241 } |
198 | 242 |
199 TEST_F(ChromotingHostTest, Reconnect) { | 243 TEST_F(ChromotingHostTest, Reconnect) { |
200 host_->Start(NewRunnableFunction(&PostQuitTask, &message_loop_)); | 244 host_->Start(NewRunnableFunction(&PostQuitTask, &message_loop_)); |
201 | 245 |
202 EXPECT_CALL(client_stub_, BeginSessionResponse(_, _)) | 246 EXPECT_CALL(client_stub_, BeginSessionResponse(_, _)) |
203 .Times(2) | 247 .Times(2) |
204 .WillRepeatedly(RunDoneTask()); | 248 .WillRepeatedly(RunDoneTask()); |
205 | 249 |
206 // When the video packet is received we first disconnect the mock | 250 // When the video packet is received we first disconnect the mock |
207 // connection. | 251 // connection. |
208 { | 252 { |
209 InSequence s; | 253 InSequence s; |
210 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 254 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
211 .WillOnce(DoAll( | 255 .WillOnce(DoAll( |
212 InvokeWithoutArgs(this, | 256 InvokeWithoutArgs(this, |
213 &ChromotingHostTest::RemoveClientConnection), | 257 &ChromotingHostTest::RemoveClientConnection), |
214 RunDoneTask())) | 258 RunDoneTask())) |
215 .RetiresOnSaturation(); | 259 .RetiresOnSaturation(); |
216 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 260 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
217 .Times(AnyNumber()); | 261 .Times(AnyNumber()); |
218 } | 262 } |
219 | 263 |
220 // If Disconnect() is called we can break the main message loop. | 264 // If Disconnect() is called we can break the main message loop. |
221 EXPECT_CALL(*connection_.get(), Disconnect()) | 265 EXPECT_CALL(*connection_.get(), Disconnect()) |
222 .WillOnce(QuitMainMessageLoop(&message_loop_)) | 266 .WillOnce(QuitMainMessageLoop(&message_loop_)) |
223 .RetiresOnSaturation(); | 267 .RetiresOnSaturation(); |
224 | 268 |
225 SimulateClientConnection(); | 269 SimulateClientConnection(0); |
226 message_loop_.Run(); | 270 message_loop_.Run(); |
227 | 271 |
228 // Connect the client again. | 272 // Connect the client again. |
229 { | 273 { |
230 InSequence s; | 274 InSequence s; |
231 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 275 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
232 .WillOnce(DoAll( | 276 .WillOnce(DoAll( |
233 InvokeWithoutArgs(host_.get(), &ChromotingHost::Shutdown), | 277 InvokeWithoutArgs(host_.get(), &ChromotingHost::Shutdown), |
234 RunDoneTask())) | 278 RunDoneTask())) |
235 .RetiresOnSaturation(); | 279 .RetiresOnSaturation(); |
236 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) | 280 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
237 .Times(AnyNumber()); | 281 .Times(AnyNumber()); |
238 } | 282 } |
239 EXPECT_CALL(*connection_.get(), Disconnect()) | 283 EXPECT_CALL(*connection_.get(), Disconnect()) |
240 .RetiresOnSaturation(); | 284 .RetiresOnSaturation(); |
241 | 285 |
242 SimulateClientConnection(); | 286 SimulateClientConnection(0); |
243 message_loop_.Run(); | 287 message_loop_.Run(); |
244 } | 288 } |
245 | 289 |
| 290 TEST_F(ChromotingHostTest, ConnectTwice) { |
| 291 host_->Start(NewRunnableFunction(&PostQuitTask, &message_loop_)); |
| 292 |
| 293 EXPECT_CALL(client_stub_, BeginSessionResponse(_, _)) |
| 294 .Times(1) |
| 295 .WillRepeatedly(RunDoneTask()); |
| 296 |
| 297 EXPECT_CALL(client_stub2_, BeginSessionResponse(_, _)) |
| 298 .Times(1) |
| 299 .WillRepeatedly(RunDoneTask()); |
| 300 |
| 301 // When a video packet is received we connect the second mock |
| 302 // connection. |
| 303 { |
| 304 InSequence s; |
| 305 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
| 306 .WillOnce(DoAll( |
| 307 InvokeWithoutArgs( |
| 308 this, &ChromotingHostTest::SimulateSecondClientConnection), |
| 309 RunDoneTask())) |
| 310 .RetiresOnSaturation(); |
| 311 EXPECT_CALL(video_stub_, ProcessVideoPacket(_, _)) |
| 312 .Times(AnyNumber()); |
| 313 EXPECT_CALL(video_stub2_, ProcessVideoPacket(_, _)) |
| 314 .WillOnce(DoAll( |
| 315 InvokeWithoutArgs(host_.get(), &ChromotingHost::Shutdown), |
| 316 RunDoneTask())) |
| 317 .RetiresOnSaturation(); |
| 318 EXPECT_CALL(video_stub2_, ProcessVideoPacket(_, _)) |
| 319 .Times(AnyNumber()); |
| 320 } |
| 321 |
| 322 EXPECT_CALL(*connection_.get(), Disconnect()) |
| 323 .RetiresOnSaturation(); |
| 324 EXPECT_CALL(*connection2_.get(), Disconnect()) |
| 325 .RetiresOnSaturation(); |
| 326 |
| 327 SimulateClientConnection(0); |
| 328 message_loop_.Run(); |
| 329 } |
| 330 |
246 } // namespace remoting | 331 } // namespace remoting |
OLD | NEW |