| 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( |
| 130 new MockDesktopEnvironmentFactory(ui_task_runner_)); |
| 125 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) | 131 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) |
| 126 .Times(AnyNumber()) | 132 .Times(AnyNumber()) |
| 127 .WillRepeatedly(Invoke(this, | 133 .WillRepeatedly(Invoke(this, |
| 128 &ClientSessionTest::CreateDesktopEnvironment)); | 134 &ClientSessionTest::CreateDesktopEnvironment)); |
| 129 | 135 |
| 136 event_executor_.reset(new MockEventExecutor()); |
| 137 |
| 130 session_config_ = SessionConfig::ForTest(); | 138 session_config_ = SessionConfig::ForTest(); |
| 131 | 139 |
| 132 // Mock protocol::Session APIs called directly by ClientSession. | 140 // Mock protocol::Session APIs called directly by ClientSession. |
| 133 protocol::MockSession* session = new MockSession(); | 141 protocol::MockSession* session = new MockSession(); |
| 134 EXPECT_CALL(*session, config()).WillRepeatedly(ReturnRef(session_config_)); | 142 EXPECT_CALL(*session, config()).WillRepeatedly(ReturnRef(session_config_)); |
| 135 EXPECT_CALL(*session, jid()).WillRepeatedly(ReturnRef(client_jid_)); | 143 EXPECT_CALL(*session, jid()).WillRepeatedly(ReturnRef(client_jid_)); |
| 136 EXPECT_CALL(*session, SetEventHandler(_)); | 144 EXPECT_CALL(*session, SetEventHandler(_)); |
| 137 | 145 |
| 138 // Mock protocol::ConnectionToClient APIs called directly by ClientSession. | 146 // Mock protocol::ConnectionToClient APIs called directly by ClientSession. |
| 139 // HostStub is not touched by ClientSession, so we can safely pass NULL. | 147 // HostStub is not touched by ClientSession, so we can safely pass NULL. |
| 140 scoped_ptr<MockConnectionToClient> connection( | 148 scoped_ptr<MockConnectionToClient> connection( |
| 141 new MockConnectionToClient(session, NULL)); | 149 new MockConnectionToClient(session, NULL)); |
| 142 EXPECT_CALL(*connection, session()).WillRepeatedly(Return(session)); | 150 EXPECT_CALL(*connection, session()).WillRepeatedly(Return(session)); |
| 143 EXPECT_CALL(*connection, client_stub()) | 151 EXPECT_CALL(*connection, client_stub()) |
| 144 .WillRepeatedly(Return(&client_stub_)); | 152 .WillRepeatedly(Return(&client_stub_)); |
| 145 EXPECT_CALL(*connection, video_stub()).WillRepeatedly(Return(&video_stub_)); | 153 EXPECT_CALL(*connection, video_stub()).WillRepeatedly(Return(&video_stub_)); |
| 146 EXPECT_CALL(*connection, Disconnect()); | 154 EXPECT_CALL(*connection, Disconnect()); |
| 147 connection_ = connection.get(); | 155 connection_ = connection.get(); |
| 148 | 156 |
| 149 client_session_ = new ClientSession( | 157 client_session_ = new ClientSession( |
| 150 &session_event_handler_, | 158 &session_event_handler_, |
| 151 ui_task_runner, // Audio thread. | 159 ui_task_runner, // Audio thread. |
| 160 ui_task_runner, // Input thread. |
| 152 ui_task_runner, // Capture thread. | 161 ui_task_runner, // Capture thread. |
| 153 ui_task_runner, // Encode thread. | 162 ui_task_runner, // Encode thread. |
| 154 ui_task_runner, // Network thread. | 163 ui_task_runner, // Network thread. |
| 164 ui_task_runner, // UI thread. |
| 155 connection.PassAs<protocol::ConnectionToClient>(), | 165 connection.PassAs<protocol::ConnectionToClient>(), |
| 156 desktop_environment_factory_.get(), | 166 desktop_environment_factory_.get(), |
| 157 base::TimeDelta()); | 167 base::TimeDelta()); |
| 158 } | 168 } |
| 159 | 169 |
| 160 void ClientSessionTest::TearDown() { | 170 void ClientSessionTest::TearDown() { |
| 161 // Verify that the client session has been stopped. | 171 // Verify that the client session has been stopped. |
| 162 EXPECT_TRUE(client_session_.get() == NULL); | 172 EXPECT_TRUE(client_session_.get() == NULL); |
| 163 } | 173 } |
| 164 | 174 |
| 165 void ClientSessionTest::DisconnectClientSession() { | 175 void ClientSessionTest::DisconnectClientSession() { |
| 166 client_session_->Disconnect(); | 176 client_session_->Disconnect(); |
| 167 // MockSession won't trigger OnConnectionClosed, so fake it. | 177 // MockSession won't trigger OnConnectionClosed, so fake it. |
| 168 client_session_->OnConnectionClosed(client_session_->connection(), | 178 client_session_->OnConnectionClosed(client_session_->connection(), |
| 169 protocol::OK); | 179 protocol::OK); |
| 170 } | 180 } |
| 171 | 181 |
| 172 void ClientSessionTest::StopClientSession() { | 182 void ClientSessionTest::StopClientSession() { |
| 173 // MockClientSessionEventHandler won't trigger Stop, so fake it. | 183 // MockClientSessionEventHandler won't trigger Stop, so fake it. |
| 174 client_session_->Stop(base::Bind( | 184 client_session_->Stop(); |
| 175 &ClientSessionTest::OnClientStopped, base::Unretained(this))); | 185 client_session_ = NULL; |
| 186 |
| 187 desktop_environment_factory_.reset(); |
| 176 } | 188 } |
| 177 | 189 |
| 178 DesktopEnvironment* ClientSessionTest::CreateDesktopEnvironment() { | 190 DesktopEnvironment* ClientSessionTest::CreateDesktopEnvironment() { |
| 179 scoped_ptr<VideoFrameCapturer> video_capturer(new VideoFrameCapturerFake()); | 191 MockDesktopEnvironment* desktop_environment = new MockDesktopEnvironment(); |
| 192 EXPECT_CALL(*desktop_environment, CreateAudioCapturerPtr(_)) |
| 193 .Times(0); |
| 194 EXPECT_CALL(*desktop_environment, CreateEventExecutorPtr(_, _)) |
| 195 .WillOnce(Invoke(this, &ClientSessionTest::CreateEventExecutor)); |
| 196 EXPECT_CALL(*desktop_environment, CreateVideoCapturerPtr(_, _)) |
| 197 .WillOnce(Invoke(this, &ClientSessionTest::CreateVideoCapturer)); |
| 180 | 198 |
| 181 EXPECT_TRUE(!event_executor_); | 199 return desktop_environment; |
| 182 event_executor_ = new MockEventExecutor(); | 200 } |
| 183 return new DesktopEnvironment(scoped_ptr<AudioCapturer>(NULL), | 201 |
| 184 scoped_ptr<EventExecutor>(event_executor_), | 202 EventExecutor* ClientSessionTest::CreateEventExecutor( |
| 185 video_capturer.Pass()); | 203 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, |
| 204 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
| 205 EXPECT_TRUE(event_executor_); |
| 206 return event_executor_.release(); |
| 207 } |
| 208 |
| 209 VideoFrameCapturer* ClientSessionTest::CreateVideoCapturer( |
| 210 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
| 211 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner) { |
| 212 return new VideoFrameCapturerFake(); |
| 186 } | 213 } |
| 187 | 214 |
| 188 void ClientSessionTest::ConnectClientSession() { | 215 void ClientSessionTest::ConnectClientSession() { |
| 189 client_session_->OnConnectionAuthenticated(client_session_->connection()); | 216 client_session_->OnConnectionAuthenticated(client_session_->connection()); |
| 190 client_session_->OnConnectionChannelsConnected(client_session_->connection()); | 217 client_session_->OnConnectionChannelsConnected(client_session_->connection()); |
| 191 } | 218 } |
| 192 | 219 |
| 193 void ClientSessionTest::QuitMainMessageLoop() { | 220 void ClientSessionTest::QuitMainMessageLoop() { |
| 194 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 221 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 195 } | 222 } |
| 196 | 223 |
| 197 void ClientSessionTest::OnClientStopped() { | |
| 198 client_session_ = NULL; | |
| 199 } | |
| 200 | |
| 201 MATCHER_P2(EqualsClipboardEvent, m, d, "") { | 224 MATCHER_P2(EqualsClipboardEvent, m, d, "") { |
| 202 return (strcmp(arg.mime_type().c_str(), m) == 0 && | 225 return (strcmp(arg.mime_type().c_str(), m) == 0 && |
| 203 memcmp(arg.data().data(), d, arg.data().size()) == 0); | 226 memcmp(arg.data().data(), d, arg.data().size()) == 0); |
| 204 } | 227 } |
| 205 | 228 |
| 206 TEST_F(ClientSessionTest, ClipboardStubFilter) { | 229 TEST_F(ClientSessionTest, ClipboardStubFilter) { |
| 207 protocol::ClipboardEvent clipboard_event1; | 230 protocol::ClipboardEvent clipboard_event1; |
| 208 clipboard_event1.set_mime_type(kMimeTypeTextUtf8); | 231 clipboard_event1.set_mime_type(kMimeTypeTextUtf8); |
| 209 clipboard_event1.set_data("a"); | 232 clipboard_event1.set_data("a"); |
| 210 | 233 |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 .After(connected) | 475 .After(connected) |
| 453 .WillOnce(DoAll( | 476 .WillOnce(DoAll( |
| 454 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession), | 477 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession), |
| 455 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession))); | 478 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession))); |
| 456 | 479 |
| 457 ConnectClientSession(); | 480 ConnectClientSession(); |
| 458 message_loop_.Run(); | 481 message_loop_.Run(); |
| 459 } | 482 } |
| 460 | 483 |
| 461 } // namespace remoting | 484 } // namespace remoting |
| OLD | NEW |