| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "media/video/capture/screen/screen_capturer_fake.h" | 9 #include "media/video/capture/screen/screen_capturer_fake.h" |
| 10 #include "remoting/base/auto_thread_task_runner.h" | 10 #include "remoting/base/auto_thread_task_runner.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 class ChromotingHostTest : public testing::Test { | 64 class ChromotingHostTest : public testing::Test { |
| 65 public: | 65 public: |
| 66 ChromotingHostTest() { | 66 ChromotingHostTest() { |
| 67 } | 67 } |
| 68 | 68 |
| 69 virtual void SetUp() OVERRIDE { | 69 virtual void SetUp() OVERRIDE { |
| 70 ui_task_runner_ = new AutoThreadTaskRunner( | 70 task_runner_ = new AutoThreadTaskRunner( |
| 71 message_loop_.message_loop_proxy(), | 71 message_loop_.message_loop_proxy(), |
| 72 base::Bind(&ChromotingHostTest::QuitMainMessageLoop, | 72 base::Bind(&ChromotingHostTest::QuitMainMessageLoop, |
| 73 base::Unretained(this))); | 73 base::Unretained(this))); |
| 74 | 74 |
| 75 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory()); | 75 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory()); |
| 76 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) | 76 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) |
| 77 .Times(AnyNumber()) | 77 .Times(AnyNumber()) |
| 78 .WillRepeatedly(Invoke(this, | 78 .WillRepeatedly(Invoke(this, |
| 79 &ChromotingHostTest::CreateDesktopEnvironment)); | 79 &ChromotingHostTest::CreateDesktopEnvironment)); |
| 80 EXPECT_CALL(*desktop_environment_factory_, SupportsAudioCapture()) | 80 EXPECT_CALL(*desktop_environment_factory_, SupportsAudioCapture()) |
| 81 .Times(AnyNumber()) | 81 .Times(AnyNumber()) |
| 82 .WillRepeatedly(Return(false)); | 82 .WillRepeatedly(Return(false)); |
| 83 | 83 |
| 84 session_manager_ = new protocol::MockSessionManager(); | 84 session_manager_ = new protocol::MockSessionManager(); |
| 85 | 85 |
| 86 host_ = new ChromotingHost( | 86 host_.reset(new ChromotingHost( |
| 87 &signal_strategy_, | 87 &signal_strategy_, |
| 88 desktop_environment_factory_.get(), | 88 desktop_environment_factory_.get(), |
| 89 scoped_ptr<protocol::SessionManager>(session_manager_), | 89 scoped_ptr<protocol::SessionManager>(session_manager_), |
| 90 ui_task_runner_, // Audio | 90 task_runner_, // Audio |
| 91 ui_task_runner_, // Input | 91 task_runner_, // Input |
| 92 ui_task_runner_, // Video capture | 92 task_runner_, // Video capture |
| 93 ui_task_runner_, // Video encode | 93 task_runner_, // Video encode |
| 94 ui_task_runner_, // Network | 94 task_runner_, // Network |
| 95 ui_task_runner_); // UI | 95 task_runner_)); // UI |
| 96 host_->AddStatusObserver(&host_status_observer_); | 96 host_->AddStatusObserver(&host_status_observer_); |
| 97 | 97 |
| 98 xmpp_login_ = "host@domain"; | 98 xmpp_login_ = "host@domain"; |
| 99 session1_ = new MockSession(); | 99 session1_ = new MockSession(); |
| 100 session2_ = new MockSession(); | 100 session2_ = new MockSession(); |
| 101 session_unowned1_.reset(new MockSession()); | 101 session_unowned1_.reset(new MockSession()); |
| 102 session_unowned2_.reset(new MockSession()); | 102 session_unowned2_.reset(new MockSession()); |
| 103 session_config1_ = SessionConfig::ForTest(); | 103 session_config1_ = SessionConfig::ForTest(); |
| 104 session_jid1_ = "user@domain/rest-of-jid"; | 104 session_jid1_ = "user@domain/rest-of-jid"; |
| 105 session_config2_ = SessionConfig::ForTest(); | 105 session_config2_ = SessionConfig::ForTest(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 175 |
| 176 // Helper method to pretend a client is connected to ChromotingHost. | 176 // Helper method to pretend a client is connected to ChromotingHost. |
| 177 void SimulateClientConnection(int connection_index, bool authenticate, | 177 void SimulateClientConnection(int connection_index, bool authenticate, |
| 178 bool reject) { | 178 bool reject) { |
| 179 scoped_ptr<protocol::ConnectionToClient> connection = | 179 scoped_ptr<protocol::ConnectionToClient> connection = |
| 180 ((connection_index == 0) ? owned_connection1_ : owned_connection2_). | 180 ((connection_index == 0) ? owned_connection1_ : owned_connection2_). |
| 181 PassAs<protocol::ConnectionToClient>(); | 181 PassAs<protocol::ConnectionToClient>(); |
| 182 protocol::ConnectionToClient* connection_ptr = connection.get(); | 182 protocol::ConnectionToClient* connection_ptr = connection.get(); |
| 183 scoped_ptr<ClientSession> client(new ClientSession( | 183 scoped_ptr<ClientSession> client(new ClientSession( |
| 184 host_.get(), | 184 host_.get(), |
| 185 ui_task_runner_, // Audio | 185 task_runner_, // Audio |
| 186 ui_task_runner_, // Input | 186 task_runner_, // Input |
| 187 ui_task_runner_, // Video capture | 187 task_runner_, // Video capture |
| 188 ui_task_runner_, // Video encode | 188 task_runner_, // Video encode |
| 189 ui_task_runner_, // Network | 189 task_runner_, // Network |
| 190 ui_task_runner_, // UI | 190 task_runner_, // UI |
| 191 connection.Pass(), | 191 connection.Pass(), |
| 192 desktop_environment_factory_.get(), | 192 desktop_environment_factory_.get(), |
| 193 base::TimeDelta())); | 193 base::TimeDelta())); |
| 194 | 194 |
| 195 ClientSession* client_raw = client.get(); | 195 connection_ptr->set_host_stub(client.get()); |
| 196 connection_ptr->set_host_stub(client_raw); | |
| 197 | |
| 198 ui_task_runner_->PostTask( | |
| 199 FROM_HERE, base::Bind(&ChromotingHostTest::AddClientToHost, | |
| 200 host_, base::Passed(&client))); | |
| 201 | 196 |
| 202 if (authenticate) { | 197 if (authenticate) { |
| 203 ui_task_runner_->PostTask( | 198 task_runner_->PostTask( |
| 204 FROM_HERE, base::Bind(&ClientSession::OnConnectionAuthenticated, | 199 FROM_HERE, |
| 205 base::Unretained(client_raw), connection_ptr)); | 200 base::Bind(&ClientSession::OnConnectionAuthenticated, |
| 201 base::Unretained(client.get()), connection_ptr)); |
| 206 if (!reject) { | 202 if (!reject) { |
| 207 ui_task_runner_->PostTask( | 203 task_runner_->PostTask( |
| 208 FROM_HERE, | 204 FROM_HERE, |
| 209 base::Bind(&ClientSession::OnConnectionChannelsConnected, | 205 base::Bind(&ClientSession::OnConnectionChannelsConnected, |
| 210 base::Unretained(client_raw), connection_ptr)); | 206 base::Unretained(client.get()), connection_ptr)); |
| 211 } | 207 } |
| 212 } else { | 208 } else { |
| 213 ui_task_runner_->PostTask( | 209 task_runner_->PostTask( |
| 214 FROM_HERE, base::Bind(&ClientSession::OnConnectionClosed, | 210 FROM_HERE, base::Bind(&ClientSession::OnConnectionClosed, |
| 215 base::Unretained(client_raw), connection_ptr, | 211 base::Unretained(client.get()), connection_ptr, |
| 216 protocol::AUTHENTICATION_FAILED)); | 212 protocol::AUTHENTICATION_FAILED)); |
| 217 } | 213 } |
| 218 | 214 |
| 219 get_client(connection_index) = client_raw; | 215 get_client(connection_index) = client.get(); |
| 216 |
| 217 // |host| is responsible for deleting |client| from now on. |
| 218 host_->clients_.push_back(client.release()); |
| 220 } | 219 } |
| 221 | 220 |
| 222 virtual void TearDown() OVERRIDE { | 221 virtual void TearDown() OVERRIDE { |
| 223 // Make sure that the host has been properly deleted. | 222 // Make sure that the host has been properly deleted. |
| 224 DCHECK(host_.get() == NULL); | 223 DCHECK(host_.get() == NULL); |
| 225 } | 224 } |
| 226 | 225 |
| 227 // Change the session route for |client1_|. | 226 // Change the session route for |client1_|. |
| 228 void ChangeSessionRoute(const std::string& channel_name, | 227 void ChangeSessionRoute(const std::string& channel_name, |
| 229 const protocol::TransportRoute& route) { | 228 const protocol::TransportRoute& route) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 void SetEventHandler(protocol::Session::EventHandler* event_handler) { | 284 void SetEventHandler(protocol::Session::EventHandler* event_handler) { |
| 286 session_event_handler_ = event_handler; | 285 session_event_handler_ = event_handler; |
| 287 } | 286 } |
| 288 | 287 |
| 289 void NotifyConnectionClosed() { | 288 void NotifyConnectionClosed() { |
| 290 if (session_event_handler_) { | 289 if (session_event_handler_) { |
| 291 session_event_handler_->OnSessionStateChange(protocol::Session::CLOSED); | 290 session_event_handler_->OnSessionStateChange(protocol::Session::CLOSED); |
| 292 } | 291 } |
| 293 } | 292 } |
| 294 | 293 |
| 295 static void AddClientToHost(scoped_refptr<ChromotingHost> host, | 294 void ShutdownHost() { |
| 296 scoped_ptr<ClientSession> client) { | 295 task_runner_->PostTask( |
| 297 // |host| is responsible for deleting |client| from now on. | 296 FROM_HERE, |
| 298 host->clients_.push_back(client.release()); | 297 base::Bind(&ChromotingHostTest::StopAndReleaseTaskRunner, |
| 298 base::Unretained(this))); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void ShutdownHost() { | 301 void StopAndReleaseTaskRunner() { |
| 302 ui_task_runner_->PostTask( | 302 host_.reset(); |
| 303 FROM_HERE, | 303 task_runner_ = NULL; |
| 304 base::Bind(&ChromotingHost::Shutdown, host_, | |
| 305 base::Bind(&ChromotingHostTest::ReleaseUiTaskRunner, | |
| 306 base::Unretained(this)))); | |
| 307 } | |
| 308 | |
| 309 void ReleaseUiTaskRunner() { | |
| 310 ui_task_runner_ = NULL; | |
| 311 host_ = NULL; | |
| 312 desktop_environment_factory_.reset(); | 304 desktop_environment_factory_.reset(); |
| 313 } | 305 } |
| 314 | 306 |
| 315 void QuitMainMessageLoop() { | 307 void QuitMainMessageLoop() { |
| 316 PostQuitTask(&message_loop_); | 308 PostQuitTask(&message_loop_); |
| 317 } | 309 } |
| 318 | 310 |
| 319 // Expect the host and session manager to start, and return the expectation | 311 // Expect the host and session manager to start, and return the expectation |
| 320 // that the session manager has started. | 312 // that the session manager has started. |
| 321 Expectation ExpectHostAndSessionManagerStart() { | 313 Expectation ExpectHostAndSessionManagerStart() { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 if (expect_host_status_change) { | 379 if (expect_host_status_change) { |
| 388 EXPECT_CALL(host_status_observer_, OnClientDisconnected(session_jid)) | 380 EXPECT_CALL(host_status_observer_, OnClientDisconnected(session_jid)) |
| 389 .After(after) | 381 .After(after) |
| 390 .WillOnce(action) | 382 .WillOnce(action) |
| 391 .RetiresOnSaturation(); | 383 .RetiresOnSaturation(); |
| 392 } | 384 } |
| 393 } | 385 } |
| 394 | 386 |
| 395 protected: | 387 protected: |
| 396 MessageLoop message_loop_; | 388 MessageLoop message_loop_; |
| 397 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; | 389 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
| 398 MockConnectionToClientEventHandler handler_; | 390 MockConnectionToClientEventHandler handler_; |
| 399 MockSignalStrategy signal_strategy_; | 391 MockSignalStrategy signal_strategy_; |
| 400 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_; | 392 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_; |
| 401 scoped_refptr<ChromotingHost> host_; | 393 scoped_ptr<ChromotingHost> host_; |
| 402 MockHostStatusObserver host_status_observer_; | 394 MockHostStatusObserver host_status_observer_; |
| 403 protocol::MockSessionManager* session_manager_; | 395 protocol::MockSessionManager* session_manager_; |
| 404 std::string xmpp_login_; | 396 std::string xmpp_login_; |
| 405 MockConnectionToClient* connection1_; | 397 MockConnectionToClient* connection1_; |
| 406 scoped_ptr<MockConnectionToClient> owned_connection1_; | 398 scoped_ptr<MockConnectionToClient> owned_connection1_; |
| 407 ClientSession* client1_; | 399 ClientSession* client1_; |
| 408 std::string session_jid1_; | 400 std::string session_jid1_; |
| 409 MockSession* session1_; // Owned by |connection_|. | 401 MockSession* session1_; // Owned by |connection_|. |
| 410 SessionConfig session_config1_; | 402 SessionConfig session_config1_; |
| 411 MockVideoStub video_stub1_; | 403 MockVideoStub video_stub1_; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 ExpectClientDisconnected(0, true, video_packet_sent, | 640 ExpectClientDisconnected(0, true, video_packet_sent, |
| 649 InvokeWithoutArgs(this, &ChromotingHostTest::ShutdownHost)); | 641 InvokeWithoutArgs(this, &ChromotingHostTest::ShutdownHost)); |
| 650 EXPECT_CALL(host_status_observer_, OnShutdown()); | 642 EXPECT_CALL(host_status_observer_, OnShutdown()); |
| 651 | 643 |
| 652 host_->Start(xmpp_login_); | 644 host_->Start(xmpp_login_); |
| 653 SimulateClientConnection(0, true, false); | 645 SimulateClientConnection(0, true, false); |
| 654 message_loop_.Run(); | 646 message_loop_.Run(); |
| 655 } | 647 } |
| 656 | 648 |
| 657 } // namespace remoting | 649 } // namespace remoting |
| OLD | NEW |