| 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "remoting/base/auto_thread_task_runner.h" | 6 #include "remoting/base/auto_thread_task_runner.h" |
| 7 #include "remoting/base/constants.h" | 7 #include "remoting/base/constants.h" |
| 8 #include "remoting/capturer/video_capturer_mock_objects.h" | 8 #include "remoting/capturer/video_capturer_mock_objects.h" |
| 9 #include "remoting/capturer/video_frame_capturer_fake.h" | 9 #include "remoting/capturer/video_frame_capturer_fake.h" |
| 10 #include "remoting/host/audio_capturer.h" | 10 #include "remoting/host/audio_capturer.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 } | 48 } |
| 49 | 49 |
| 50 ACTION_P2(LocalMouseMoved, client_session, event) { | 50 ACTION_P2(LocalMouseMoved, client_session, event) { |
| 51 client_session->LocalMouseMoved(SkIPoint::Make(event.x(), event.y())); | 51 client_session->LocalMouseMoved(SkIPoint::Make(event.x(), event.y())); |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 class ClientSessionTest : public testing::Test { | 56 class ClientSessionTest : public testing::Test { |
| 57 public: | 57 public: |
| 58 ClientSessionTest() | 58 ClientSessionTest() : client_jid_("user@domain/rest-of-jid") {} |
| 59 : client_jid_("user@domain/rest-of-jid"), | |
| 60 event_executor_(NULL) {} | |
| 61 | 59 |
| 62 virtual void SetUp() OVERRIDE; | 60 virtual void SetUp() OVERRIDE; |
| 63 virtual void TearDown() OVERRIDE; | 61 virtual void TearDown() OVERRIDE; |
| 64 | 62 |
| 65 // Disconnects the client session. | 63 // Disconnects the client session. |
| 66 void DisconnectClientSession(); | 64 void DisconnectClientSession(); |
| 67 | 65 |
| 68 // Asynchronously stops the client session. OnClientStopped() will be called | 66 // Stops and releases the ClientSession, allowing the MessageLoop to quit. |
| 69 // once the client session is fully stopped. | |
| 70 void StopClientSession(); | 67 void StopClientSession(); |
| 71 | 68 |
| 72 protected: | 69 protected: |
| 73 // Creates a DesktopEnvironment with a fake VideoFrameCapturer, to mock | 70 // Creates a DesktopEnvironment with a fake VideoFrameCapturer, to mock |
| 74 // DesktopEnvironmentFactory::Create(). | 71 // DesktopEnvironmentFactory::Create(). |
| 75 DesktopEnvironment* CreateDesktopEnvironment(); | 72 DesktopEnvironment* CreateDesktopEnvironment(); |
| 76 | 73 |
| 74 // Returns |event_executor_| created and initialized by SetUp(), to mock |
| 75 // DesktopEnvironment::CreateEventExecutor(). |
| 76 EventExecutor* CreateEventExecutor( |
| 77 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 78 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
| 79 |
| 80 // Creates a fake VideoFrameCapturer, to mock |
| 81 // DesktopEnvironment::CreateVideoCapturer(). |
| 82 VideoFrameCapturer* CreateVideoCapturer( |
| 83 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
| 84 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner); |
| 85 |
| 77 // Notifies the client session that the client connection has been | 86 // Notifies the client session that the client connection has been |
| 78 // authenticated and channels have been connected. This effectively enables | 87 // authenticated and channels have been connected. This effectively enables |
| 79 // the input pipe line and starts video capturing. | 88 // the input pipe line and starts video capturing. |
| 80 void ConnectClientSession(); | 89 void ConnectClientSession(); |
| 81 | 90 |
| 82 // Invoked when the last reference to the AutoThreadTaskRunner has been | 91 // Invoked when the last reference to the AutoThreadTaskRunner has been |
| 83 // released and quits the message loop to finish the test. | 92 // released and quits the message loop to finish the test. |
| 84 void QuitMainMessageLoop(); | 93 void QuitMainMessageLoop(); |
| 85 | 94 |
| 86 // Releases the ClientSession when it has been fully stopped, allowing | |
| 87 // the MessageLoop to quit. | |
| 88 void OnClientStopped(); | |
| 89 | |
| 90 // Message loop passed to |client_session_| to perform all functions on. | 95 // Message loop passed to |client_session_| to perform all functions on. |
| 91 MessageLoop message_loop_; | 96 MessageLoop message_loop_; |
| 92 | 97 |
| 93 // ClientSession instance under test. | 98 // ClientSession instance under test. |
| 94 scoped_refptr<ClientSession> client_session_; | 99 scoped_refptr<ClientSession> client_session_; |
| 95 | 100 |
| 96 // ClientSession::EventHandler mock for use in tests. | 101 // ClientSession::EventHandler mock for use in tests. |
| 97 MockClientSessionEventHandler session_event_handler_; | 102 MockClientSessionEventHandler session_event_handler_; |
| 98 | 103 |
| 99 // Storage for values to be returned by the protocol::Session mock. | 104 // Storage for values to be returned by the protocol::Session mock. |
| 100 SessionConfig session_config_; | 105 SessionConfig session_config_; |
| 101 const std::string client_jid_; | 106 const std::string client_jid_; |
| 102 | 107 |
| 103 // Stubs returned to |client_session_| components by |connection_|. | 108 // Stubs returned to |client_session_| components by |connection_|. |
| 104 MockClientStub client_stub_; | 109 MockClientStub client_stub_; |
| 105 MockVideoStub video_stub_; | 110 MockVideoStub video_stub_; |
| 106 | 111 |
| 107 // DesktopEnvironment owns |event_executor_|, but input injection tests need | 112 // DesktopEnvironment owns |event_executor_|, but input injection tests need |
| 108 // to express expectations on it. | 113 // to express expectations on it. |
| 109 MockEventExecutor* event_executor_; | 114 scoped_ptr<MockEventExecutor> event_executor_; |
| 110 | 115 |
| 111 // ClientSession owns |connection_| but tests need it to inject fake events. | 116 // ClientSession owns |connection_| but tests need it to inject fake events. |
| 112 MockConnectionToClient* connection_; | 117 MockConnectionToClient* connection_; |
| 113 | 118 |
| 114 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_; | 119 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_; |
| 115 }; | 120 }; |
| 116 | 121 |
| 117 void ClientSessionTest::SetUp() { | 122 void ClientSessionTest::SetUp() { |
| 118 // Arrange to run |message_loop_| until no components depend on it. | 123 // Arrange to run |message_loop_| until no components depend on it. |
| 119 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner( | 124 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner( |
| 120 message_loop_.message_loop_proxy(), | 125 message_loop_.message_loop_proxy(), |
| 121 base::Bind(&ClientSessionTest::QuitMainMessageLoop, | 126 base::Bind(&ClientSessionTest::QuitMainMessageLoop, |
| 122 base::Unretained(this))); | 127 base::Unretained(this))); |
| 123 | 128 |
| 124 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory()); | 129 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory()); |
| 125 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) | 130 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) |
| 126 .Times(AnyNumber()) | 131 .Times(AnyNumber()) |
| 127 .WillRepeatedly(Invoke(this, | 132 .WillRepeatedly(Invoke(this, |
| 128 &ClientSessionTest::CreateDesktopEnvironment)); | 133 &ClientSessionTest::CreateDesktopEnvironment)); |
| 134 EXPECT_CALL(*desktop_environment_factory_, SupportsAudioCapture()) |
| 135 .Times(AnyNumber()) |
| 136 .WillRepeatedly(Return(false)); |
| 137 |
| 138 event_executor_.reset(new MockEventExecutor()); |
| 129 | 139 |
| 130 session_config_ = SessionConfig::ForTest(); | 140 session_config_ = SessionConfig::ForTest(); |
| 131 | 141 |
| 132 // Mock protocol::Session APIs called directly by ClientSession. | 142 // Mock protocol::Session APIs called directly by ClientSession. |
| 133 protocol::MockSession* session = new MockSession(); | 143 protocol::MockSession* session = new MockSession(); |
| 134 EXPECT_CALL(*session, config()).WillRepeatedly(ReturnRef(session_config_)); | 144 EXPECT_CALL(*session, config()).WillRepeatedly(ReturnRef(session_config_)); |
| 135 EXPECT_CALL(*session, jid()).WillRepeatedly(ReturnRef(client_jid_)); | 145 EXPECT_CALL(*session, jid()).WillRepeatedly(ReturnRef(client_jid_)); |
| 136 EXPECT_CALL(*session, SetEventHandler(_)); | 146 EXPECT_CALL(*session, SetEventHandler(_)); |
| 137 | 147 |
| 138 // Mock protocol::ConnectionToClient APIs called directly by ClientSession. | 148 // Mock protocol::ConnectionToClient APIs called directly by ClientSession. |
| 139 // HostStub is not touched by ClientSession, so we can safely pass NULL. | 149 // HostStub is not touched by ClientSession, so we can safely pass NULL. |
| 140 scoped_ptr<MockConnectionToClient> connection( | 150 scoped_ptr<MockConnectionToClient> connection( |
| 141 new MockConnectionToClient(session, NULL)); | 151 new MockConnectionToClient(session, NULL)); |
| 142 EXPECT_CALL(*connection, session()).WillRepeatedly(Return(session)); | 152 EXPECT_CALL(*connection, session()).WillRepeatedly(Return(session)); |
| 143 EXPECT_CALL(*connection, client_stub()) | 153 EXPECT_CALL(*connection, client_stub()) |
| 144 .WillRepeatedly(Return(&client_stub_)); | 154 .WillRepeatedly(Return(&client_stub_)); |
| 145 EXPECT_CALL(*connection, video_stub()).WillRepeatedly(Return(&video_stub_)); | 155 EXPECT_CALL(*connection, video_stub()).WillRepeatedly(Return(&video_stub_)); |
| 146 EXPECT_CALL(*connection, Disconnect()); | 156 EXPECT_CALL(*connection, Disconnect()); |
| 147 connection_ = connection.get(); | 157 connection_ = connection.get(); |
| 148 | 158 |
| 149 client_session_ = new ClientSession( | 159 client_session_ = new ClientSession( |
| 150 &session_event_handler_, | 160 &session_event_handler_, |
| 151 ui_task_runner, // Audio thread. | 161 ui_task_runner, // Audio thread. |
| 162 ui_task_runner, // Input thread. |
| 152 ui_task_runner, // Capture thread. | 163 ui_task_runner, // Capture thread. |
| 153 ui_task_runner, // Encode thread. | 164 ui_task_runner, // Encode thread. |
| 154 ui_task_runner, // Network thread. | 165 ui_task_runner, // Network thread. |
| 166 ui_task_runner, // UI thread. |
| 155 connection.PassAs<protocol::ConnectionToClient>(), | 167 connection.PassAs<protocol::ConnectionToClient>(), |
| 156 desktop_environment_factory_.get(), | 168 desktop_environment_factory_.get(), |
| 157 base::TimeDelta()); | 169 base::TimeDelta()); |
| 158 } | 170 } |
| 159 | 171 |
| 160 void ClientSessionTest::TearDown() { | 172 void ClientSessionTest::TearDown() { |
| 161 // Verify that the client session has been stopped. | 173 // Verify that the client session has been stopped. |
| 162 EXPECT_TRUE(client_session_.get() == NULL); | 174 EXPECT_TRUE(client_session_.get() == NULL); |
| 163 } | 175 } |
| 164 | 176 |
| 165 void ClientSessionTest::DisconnectClientSession() { | 177 void ClientSessionTest::DisconnectClientSession() { |
| 166 client_session_->Disconnect(); | 178 client_session_->Disconnect(); |
| 167 // MockSession won't trigger OnConnectionClosed, so fake it. | 179 // MockSession won't trigger OnConnectionClosed, so fake it. |
| 168 client_session_->OnConnectionClosed(client_session_->connection(), | 180 client_session_->OnConnectionClosed(client_session_->connection(), |
| 169 protocol::OK); | 181 protocol::OK); |
| 170 } | 182 } |
| 171 | 183 |
| 172 void ClientSessionTest::StopClientSession() { | 184 void ClientSessionTest::StopClientSession() { |
| 173 // MockClientSessionEventHandler won't trigger Stop, so fake it. | 185 // MockClientSessionEventHandler won't trigger Stop, so fake it. |
| 174 client_session_->Stop(base::Bind( | 186 client_session_->Stop(); |
| 175 &ClientSessionTest::OnClientStopped, base::Unretained(this))); | 187 client_session_ = NULL; |
| 188 |
| 189 desktop_environment_factory_.reset(); |
| 176 } | 190 } |
| 177 | 191 |
| 178 DesktopEnvironment* ClientSessionTest::CreateDesktopEnvironment() { | 192 DesktopEnvironment* ClientSessionTest::CreateDesktopEnvironment() { |
| 179 scoped_ptr<VideoFrameCapturer> video_capturer(new VideoFrameCapturerFake()); | 193 MockDesktopEnvironment* desktop_environment = new MockDesktopEnvironment(); |
| 194 EXPECT_CALL(*desktop_environment, CreateAudioCapturerPtr(_)) |
| 195 .Times(0); |
| 196 EXPECT_CALL(*desktop_environment, CreateEventExecutorPtr(_, _)) |
| 197 .WillOnce(Invoke(this, &ClientSessionTest::CreateEventExecutor)); |
| 198 EXPECT_CALL(*desktop_environment, CreateVideoCapturerPtr(_, _)) |
| 199 .WillOnce(Invoke(this, &ClientSessionTest::CreateVideoCapturer)); |
| 180 | 200 |
| 181 EXPECT_TRUE(!event_executor_); | 201 return desktop_environment; |
| 182 event_executor_ = new MockEventExecutor(); | 202 } |
| 183 return new DesktopEnvironment(scoped_ptr<AudioCapturer>(NULL), | 203 |
| 184 scoped_ptr<EventExecutor>(event_executor_), | 204 EventExecutor* ClientSessionTest::CreateEventExecutor( |
| 185 video_capturer.Pass()); | 205 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 206 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
| 207 EXPECT_TRUE(event_executor_); |
| 208 return event_executor_.release(); |
| 209 } |
| 210 |
| 211 VideoFrameCapturer* ClientSessionTest::CreateVideoCapturer( |
| 212 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
| 213 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner) { |
| 214 return new VideoFrameCapturerFake(); |
| 186 } | 215 } |
| 187 | 216 |
| 188 void ClientSessionTest::ConnectClientSession() { | 217 void ClientSessionTest::ConnectClientSession() { |
| 189 client_session_->OnConnectionAuthenticated(client_session_->connection()); | 218 client_session_->OnConnectionAuthenticated(client_session_->connection()); |
| 190 client_session_->OnConnectionChannelsConnected(client_session_->connection()); | 219 client_session_->OnConnectionChannelsConnected(client_session_->connection()); |
| 191 } | 220 } |
| 192 | 221 |
| 193 void ClientSessionTest::QuitMainMessageLoop() { | 222 void ClientSessionTest::QuitMainMessageLoop() { |
| 194 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 223 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 195 } | 224 } |
| 196 | 225 |
| 197 void ClientSessionTest::OnClientStopped() { | |
| 198 client_session_ = NULL; | |
| 199 } | |
| 200 | |
| 201 MATCHER_P2(EqualsClipboardEvent, m, d, "") { | 226 MATCHER_P2(EqualsClipboardEvent, m, d, "") { |
| 202 return (strcmp(arg.mime_type().c_str(), m) == 0 && | 227 return (strcmp(arg.mime_type().c_str(), m) == 0 && |
| 203 memcmp(arg.data().data(), d, arg.data().size()) == 0); | 228 memcmp(arg.data().data(), d, arg.data().size()) == 0); |
| 204 } | 229 } |
| 205 | 230 |
| 206 TEST_F(ClientSessionTest, ClipboardStubFilter) { | 231 TEST_F(ClientSessionTest, ClipboardStubFilter) { |
| 207 protocol::ClipboardEvent clipboard_event1; | 232 protocol::ClipboardEvent clipboard_event1; |
| 208 clipboard_event1.set_mime_type(kMimeTypeTextUtf8); | 233 clipboard_event1.set_mime_type(kMimeTypeTextUtf8); |
| 209 clipboard_event1.set_data("a"); | 234 clipboard_event1.set_data("a"); |
| 210 | 235 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 EXPECT_CALL(*event_executor_, StartPtr(_)); | 434 EXPECT_CALL(*event_executor_, StartPtr(_)); |
| 410 Expectation connected = | 435 Expectation connected = |
| 411 EXPECT_CALL(session_event_handler_, OnSessionChannelsConnected(_)); | 436 EXPECT_CALL(session_event_handler_, OnSessionChannelsConnected(_)); |
| 412 EXPECT_CALL(session_event_handler_, OnSessionClosed(_)); | 437 EXPECT_CALL(session_event_handler_, OnSessionClosed(_)); |
| 413 | 438 |
| 414 int input_x[3] = { -999, 100, 999 }; | 439 int input_x[3] = { -999, 100, 999 }; |
| 415 int expected_x[3] = { 0, 100, VideoFrameCapturerFake::kWidth - 1 }; | 440 int expected_x[3] = { 0, 100, VideoFrameCapturerFake::kWidth - 1 }; |
| 416 int input_y[3] = { -999, 50, 999 }; | 441 int input_y[3] = { -999, 50, 999 }; |
| 417 int expected_y[3] = { 0, 50, VideoFrameCapturerFake::kHeight - 1 }; | 442 int expected_y[3] = { 0, 50, VideoFrameCapturerFake::kHeight - 1 }; |
| 418 | 443 |
| 419 // Inject the 1st event once a video packet has been received. | |
| 420 protocol::MouseEvent injected_event; | |
| 421 injected_event.set_x(input_x[0]); | |
| 422 injected_event.set_y(input_y[0]); | |
| 423 connected = | |
| 424 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | |
| 425 .After(connected) | |
| 426 .WillOnce(InjectMouseEvent(connection_, injected_event)); | |
| 427 | |
| 428 protocol::MouseEvent expected_event; | 444 protocol::MouseEvent expected_event; |
| 429 for (int j = 0; j < 3; j++) { | 445 for (int j = 0; j < 3; j++) { |
| 430 for (int i = 0; i < 3; i++) { | 446 for (int i = 0; i < 3; i++) { |
| 431 // Skip the first iteration since the 1st event has been injected already. | 447 protocol::MouseEvent injected_event; |
| 432 if (i > 0 || j > 0) { | 448 injected_event.set_x(input_x[i]); |
| 433 injected_event.set_x(input_x[i]); | 449 injected_event.set_y(input_y[j]); |
| 434 injected_event.set_y(input_y[j]); | 450 |
| 451 if (i == 0 && j == 0) { |
| 452 // Inject the 1st event once a video packet has been received. |
| 453 connected = |
| 454 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 455 .After(connected) |
| 456 .WillOnce(InjectMouseEvent(connection_, injected_event)); |
| 457 } else { |
| 458 // Every next event is injected once the previous event has been |
| 459 // received. |
| 435 connected = | 460 connected = |
| 436 EXPECT_CALL(*event_executor_, | 461 EXPECT_CALL(*event_executor_, |
| 437 InjectMouseEvent(EqualsMouseEvent(expected_event.x(), | 462 InjectMouseEvent(EqualsMouseEvent(expected_event.x(), |
| 438 expected_event.y()))) | 463 expected_event.y()))) |
| 439 .After(connected) | 464 .After(connected) |
| 440 .WillOnce(InjectMouseEvent(connection_, injected_event)); | 465 .WillOnce(InjectMouseEvent(connection_, injected_event)); |
| 441 } | 466 } |
| 442 | 467 |
| 443 expected_event.set_x(expected_x[i]); | 468 expected_event.set_x(expected_x[i]); |
| 444 expected_event.set_y(expected_y[j]); | 469 expected_event.set_y(expected_y[j]); |
| 445 } | 470 } |
| 446 } | 471 } |
| 447 | 472 |
| 448 // Shutdown the connection once the last event has been received. | 473 // Shutdown the connection once the last event has been received. |
| 449 EXPECT_CALL(*event_executor_, | 474 EXPECT_CALL(*event_executor_, |
| 450 InjectMouseEvent(EqualsMouseEvent(expected_event.x(), | 475 InjectMouseEvent(EqualsMouseEvent(expected_event.x(), |
| 451 expected_event.y()))) | 476 expected_event.y()))) |
| 452 .After(connected) | 477 .After(connected) |
| 453 .WillOnce(DoAll( | 478 .WillOnce(DoAll( |
| 454 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession), | 479 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession), |
| 455 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession))); | 480 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession))); |
| 456 | 481 |
| 457 ConnectClientSession(); | 482 ConnectClientSession(); |
| 458 message_loop_.Run(); | 483 message_loop_.Run(); |
| 459 } | 484 } |
| 460 | 485 |
| 461 } // namespace remoting | 486 } // namespace remoting |
| OLD | NEW |