Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: trunk/src/remoting/host/client_session_unittest.cc

Issue 308743006: Revert 273652 "Host extensions" (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « trunk/src/remoting/host/client_session.cc ('k') | trunk/src/remoting/host/host_extension.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm>
6 #include <string>
7 #include <vector>
8
9 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_util.h"
11 #include "base/test/test_simple_task_runner.h" 6 #include "base/test/test_simple_task_runner.h"
12 #include "remoting/base/auto_thread_task_runner.h" 7 #include "remoting/base/auto_thread_task_runner.h"
13 #include "remoting/base/constants.h" 8 #include "remoting/base/constants.h"
14 #include "remoting/host/audio_capturer.h" 9 #include "remoting/host/audio_capturer.h"
15 #include "remoting/host/client_session.h" 10 #include "remoting/host/client_session.h"
16 #include "remoting/host/desktop_environment.h" 11 #include "remoting/host/desktop_environment.h"
17 #include "remoting/host/host_extension.h"
18 #include "remoting/host/host_mock_objects.h" 12 #include "remoting/host/host_mock_objects.h"
19 #include "remoting/host/screen_capturer_fake.h" 13 #include "remoting/host/screen_capturer_fake.h"
20 #include "remoting/protocol/protocol_mock_objects.h" 14 #include "remoting/protocol/protocol_mock_objects.h"
21 #include "testing/gmock/include/gmock/gmock-matchers.h" 15 #include "testing/gmock/include/gmock/gmock-matchers.h"
22 #include "testing/gmock_mutant.h"
23 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 17 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
25 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" 18 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
26 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object s.h" 19 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object s.h"
27 20
28 namespace remoting { 21 namespace remoting {
29 22
30 using protocol::MockConnectionToClient; 23 using protocol::MockConnectionToClient;
31 using protocol::MockClientStub; 24 using protocol::MockClientStub;
32 using protocol::MockHostStub; 25 using protocol::MockHostStub;
33 using protocol::MockInputStub; 26 using protocol::MockInputStub;
34 using protocol::MockSession; 27 using protocol::MockSession;
35 using protocol::MockVideoStub; 28 using protocol::MockVideoStub;
36 using protocol::SessionConfig; 29 using protocol::SessionConfig;
37 30
38 using testing::_; 31 using testing::_;
39 using testing::AnyNumber; 32 using testing::AnyNumber;
40 using testing::AtMost; 33 using testing::AtMost;
41 using testing::CreateFunctor;
42 using testing::DeleteArg; 34 using testing::DeleteArg;
43 using testing::DoAll; 35 using testing::DoAll;
44 using testing::Expectation; 36 using testing::Expectation;
45 using testing::Invoke;
46 using testing::Return; 37 using testing::Return;
47 using testing::ReturnRef; 38 using testing::ReturnRef;
48 using testing::Sequence; 39 using testing::Sequence;
49 using testing::StrEq; 40 using testing::StrEq;
50 using testing::StrictMock; 41 using testing::StrictMock;
51 42
52 namespace { 43 namespace {
53 44
54 const char kDefaultTestCapability[] = "default";
55
56 ACTION_P2(InjectClipboardEvent, connection, event) { 45 ACTION_P2(InjectClipboardEvent, connection, event) {
57 connection->clipboard_stub()->InjectClipboardEvent(event); 46 connection->clipboard_stub()->InjectClipboardEvent(event);
58 } 47 }
59 48
60 ACTION_P2(InjectKeyEvent, connection, event) { 49 ACTION_P2(InjectKeyEvent, connection, event) {
61 connection->input_stub()->InjectKeyEvent(event); 50 connection->input_stub()->InjectKeyEvent(event);
62 } 51 }
63 52
64 ACTION_P2(InjectMouseEvent, connection, event) { 53 ACTION_P2(InjectMouseEvent, connection, event) {
65 connection->input_stub()->InjectMouseEvent(event); 54 connection->input_stub()->InjectMouseEvent(event);
66 } 55 }
67 56
68 ACTION_P2(LocalMouseMoved, client_session, event) { 57 ACTION_P2(LocalMouseMoved, client_session, event) {
69 client_session->OnLocalMouseMoved( 58 client_session->OnLocalMouseMoved(
70 webrtc::DesktopVector(event.x(), event.y())); 59 webrtc::DesktopVector(event.x(), event.y()));
71 } 60 }
72 61
73 ACTION_P2(SetGnubbyAuthHandlerForTesting, client_session, gnubby_auth_handler) { 62 ACTION_P2(SetGnubbyAuthHandlerForTesting, client_session, gnubby_auth_handler) {
74 client_session->SetGnubbyAuthHandlerForTesting(gnubby_auth_handler); 63 client_session->SetGnubbyAuthHandlerForTesting(gnubby_auth_handler);
75 } 64 }
76 65
77 ACTION_P2(DeliverClientMessage, client_session, message) { 66 ACTION_P2(DeliverClientMessage, client_session, message) {
78 client_session->DeliverClientMessage(message); 67 client_session->DeliverClientMessage(message);
79 } 68 }
80 69
81 ACTION_P2(AddHostCapabilities, client_session, capability) {
82 client_session->AddHostCapabilities(capability);
83 } 70 }
84 71
85 // Matches a |protocol::Capabilities| argument against a list of capabilities
86 // formatted as a space-separated string.
87 MATCHER_P(EqCapabilities, expected_capabilities, "") {
88 if (!arg.has_capabilities())
89 return false;
90
91 std::vector<std::string> words_args;
92 std::vector<std::string> words_expected;
93 Tokenize(arg.capabilities(), " ", &words_args);
94 Tokenize(expected_capabilities, " ", &words_expected);
95 std::sort(words_args.begin(), words_args.end());
96 std::sort(words_expected.begin(), words_expected.end());
97 return words_args == words_expected;
98 }
99
100 // |HostExtension| implementation that can handle an extension message type and
101 // provide capabilities.
102 class FakeExtension : public HostExtension {
103 public:
104 FakeExtension(const std::string& message_type,
105 const std::string& capabilities);
106 virtual ~FakeExtension();
107
108 virtual std::string GetCapabilities() OVERRIDE;
109 virtual scoped_ptr<HostExtensionSession> CreateExtensionSession(
110 ClientSession* client_session) OVERRIDE;
111
112 bool message_handled() {
113 return message_handled_;
114 }
115
116 private:
117 class FakeExtensionSession : public HostExtensionSession {
118 public:
119 FakeExtensionSession(FakeExtension* extension);
120 virtual ~FakeExtensionSession();
121
122 virtual bool OnExtensionMessage(
123 ClientSession* client_session,
124 const protocol::ExtensionMessage& message) OVERRIDE;
125
126 private:
127 FakeExtension* extension_;
128 };
129
130 std::string message_type_;
131 std::string capabilities_;
132 bool message_handled_;
133 };
134
135 typedef std::vector<HostExtension*> HostExtensionList;
136
137 void CreateExtensionSessions(const HostExtensionList& extensions,
138 ClientSession* client_session) {
139 for (HostExtensionList::const_iterator extension = extensions.begin();
140 extension != extensions.end(); ++extension) {
141 scoped_ptr<HostExtensionSession> extension_session =
142 (*extension)->CreateExtensionSession(client_session);
143 if (extension_session)
144 client_session->AddExtensionSession(extension_session.Pass());
145 }
146 }
147
148 } // namespace
149
150 class ClientSessionTest : public testing::Test { 72 class ClientSessionTest : public testing::Test {
151 public: 73 public:
152 ClientSessionTest() : client_jid_("user@domain/rest-of-jid") {} 74 ClientSessionTest() : client_jid_("user@domain/rest-of-jid") {}
153 75
154 virtual void SetUp() OVERRIDE; 76 virtual void SetUp() OVERRIDE;
155 virtual void TearDown() OVERRIDE; 77 virtual void TearDown() OVERRIDE;
156 78
157 // Disconnects the client session. 79 // Disconnects the client session.
158 void DisconnectClientSession(); 80 void DisconnectClientSession();
159 81
(...skipping 11 matching lines...) Expand all
171 93
172 // Creates a fake webrtc::ScreenCapturer, to mock 94 // Creates a fake webrtc::ScreenCapturer, to mock
173 // DesktopEnvironment::CreateVideoCapturer(). 95 // DesktopEnvironment::CreateVideoCapturer().
174 webrtc::ScreenCapturer* CreateVideoCapturer(); 96 webrtc::ScreenCapturer* CreateVideoCapturer();
175 97
176 // Notifies the client session that the client connection has been 98 // Notifies the client session that the client connection has been
177 // authenticated and channels have been connected. This effectively enables 99 // authenticated and channels have been connected. This effectively enables
178 // the input pipe line and starts video capturing. 100 // the input pipe line and starts video capturing.
179 void ConnectClientSession(); 101 void ConnectClientSession();
180 102
181 // Creates expectations to send an extension message and to disconnect
182 // afterwards.
183 void SetSendMessageAndDisconnectExpectation(const std::string& message_type);
184
185 // Invoked when the last reference to the AutoThreadTaskRunner has been 103 // Invoked when the last reference to the AutoThreadTaskRunner has been
186 // released and quits the message loop to finish the test. 104 // released and quits the message loop to finish the test.
187 void QuitMainMessageLoop(); 105 void QuitMainMessageLoop();
188 106
189 // Message loop passed to |client_session_| to perform all functions on. 107 // Message loop passed to |client_session_| to perform all functions on.
190 base::MessageLoop message_loop_; 108 base::MessageLoop message_loop_;
191 109
192 // ClientSession instance under test. 110 // ClientSession instance under test.
193 scoped_ptr<ClientSession> client_session_; 111 scoped_ptr<ClientSession> client_session_;
194 112
(...skipping 11 matching lines...) Expand all
206 // DesktopEnvironment owns |input_injector_|, but input injection tests need 124 // DesktopEnvironment owns |input_injector_|, but input injection tests need
207 // to express expectations on it. 125 // to express expectations on it.
208 scoped_ptr<MockInputInjector> input_injector_; 126 scoped_ptr<MockInputInjector> input_injector_;
209 127
210 // ClientSession owns |connection_| but tests need it to inject fake events. 128 // ClientSession owns |connection_| but tests need it to inject fake events.
211 MockConnectionToClient* connection_; 129 MockConnectionToClient* connection_;
212 130
213 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_; 131 scoped_ptr<MockDesktopEnvironmentFactory> desktop_environment_factory_;
214 }; 132 };
215 133
216 FakeExtension::FakeExtension(const std::string& message_type,
217 const std::string& capabilities)
218 : message_type_(message_type),
219 capabilities_(capabilities),
220 message_handled_(false) {
221 }
222
223 FakeExtension::~FakeExtension() {}
224
225 std::string FakeExtension::GetCapabilities() {
226 return capabilities_;
227 }
228
229 scoped_ptr<HostExtensionSession> FakeExtension::CreateExtensionSession(
230 ClientSession* client_session) {
231 return scoped_ptr<HostExtensionSession>(new FakeExtensionSession(this));
232 }
233
234 FakeExtension::FakeExtensionSession::FakeExtensionSession(
235 FakeExtension* extension)
236 : extension_(extension) {
237 }
238
239 FakeExtension::FakeExtensionSession::~FakeExtensionSession() {}
240
241 bool FakeExtension::FakeExtensionSession::OnExtensionMessage(
242 ClientSession* client_session,
243 const protocol::ExtensionMessage& message) {
244 if (message.type() == extension_->message_type_) {
245 extension_->message_handled_ = true;
246 return true;
247 }
248 return false;
249 }
250
251 void ClientSessionTest::SetUp() { 134 void ClientSessionTest::SetUp() {
252 // Arrange to run |message_loop_| until no components depend on it. 135 // Arrange to run |message_loop_| until no components depend on it.
253 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner( 136 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = new AutoThreadTaskRunner(
254 message_loop_.message_loop_proxy(), 137 message_loop_.message_loop_proxy(),
255 base::Bind(&ClientSessionTest::QuitMainMessageLoop, 138 base::Bind(&ClientSessionTest::QuitMainMessageLoop,
256 base::Unretained(this))); 139 base::Unretained(this)));
257 140
258 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory()); 141 desktop_environment_factory_.reset(new MockDesktopEnvironmentFactory());
259 EXPECT_CALL(*desktop_environment_factory_, CreatePtr()) 142 EXPECT_CALL(*desktop_environment_factory_, CreatePtr())
260 .Times(AnyNumber()) 143 .Times(AnyNumber())
(...skipping 29 matching lines...) Expand all
290 ui_task_runner, // Audio thread. 173 ui_task_runner, // Audio thread.
291 ui_task_runner, // Input thread. 174 ui_task_runner, // Input thread.
292 ui_task_runner, // Capture thread. 175 ui_task_runner, // Capture thread.
293 ui_task_runner, // Encode thread. 176 ui_task_runner, // Encode thread.
294 ui_task_runner, // Network thread. 177 ui_task_runner, // Network thread.
295 ui_task_runner, // UI thread. 178 ui_task_runner, // UI thread.
296 connection.PassAs<protocol::ConnectionToClient>(), 179 connection.PassAs<protocol::ConnectionToClient>(),
297 desktop_environment_factory_.get(), 180 desktop_environment_factory_.get(),
298 base::TimeDelta(), 181 base::TimeDelta(),
299 NULL)); 182 NULL));
300
301 // By default, client will report the same capabilities as the host.
302 EXPECT_CALL(client_stub_, SetCapabilities(_))
303 .Times(AtMost(1))
304 .WillOnce(Invoke(client_session_.get(), &ClientSession::SetCapabilities));
305 } 183 }
306 184
307 void ClientSessionTest::TearDown() { 185 void ClientSessionTest::TearDown() {
308 // Verify that the client session has been stopped. 186 // Verify that the client session has been stopped.
309 EXPECT_TRUE(!client_session_); 187 EXPECT_TRUE(!client_session_);
310 } 188 }
311 189
312 void ClientSessionTest::DisconnectClientSession() { 190 void ClientSessionTest::DisconnectClientSession() {
313 client_session_->DisconnectSession(); 191 client_session_->DisconnectSession();
314 // MockSession won't trigger OnConnectionClosed, so fake it. 192 // MockSession won't trigger OnConnectionClosed, so fake it.
(...skipping 11 matching lines...) Expand all
326 MockDesktopEnvironment* desktop_environment = new MockDesktopEnvironment(); 204 MockDesktopEnvironment* desktop_environment = new MockDesktopEnvironment();
327 EXPECT_CALL(*desktop_environment, CreateAudioCapturerPtr()) 205 EXPECT_CALL(*desktop_environment, CreateAudioCapturerPtr())
328 .Times(0); 206 .Times(0);
329 EXPECT_CALL(*desktop_environment, CreateInputInjectorPtr()) 207 EXPECT_CALL(*desktop_environment, CreateInputInjectorPtr())
330 .WillOnce(Invoke(this, &ClientSessionTest::CreateInputInjector)); 208 .WillOnce(Invoke(this, &ClientSessionTest::CreateInputInjector));
331 EXPECT_CALL(*desktop_environment, CreateScreenControlsPtr()) 209 EXPECT_CALL(*desktop_environment, CreateScreenControlsPtr())
332 .Times(AtMost(1)); 210 .Times(AtMost(1));
333 EXPECT_CALL(*desktop_environment, CreateVideoCapturerPtr()) 211 EXPECT_CALL(*desktop_environment, CreateVideoCapturerPtr())
334 .WillOnce(Invoke(this, &ClientSessionTest::CreateVideoCapturer)); 212 .WillOnce(Invoke(this, &ClientSessionTest::CreateVideoCapturer));
335 EXPECT_CALL(*desktop_environment, GetCapabilities()) 213 EXPECT_CALL(*desktop_environment, GetCapabilities())
336 .Times(AtMost(1)) 214 .Times(AtMost(1));
337 .WillOnce(Return(kDefaultTestCapability));
338 EXPECT_CALL(*desktop_environment, SetCapabilities(_)) 215 EXPECT_CALL(*desktop_environment, SetCapabilities(_))
339 .Times(AtMost(1)); 216 .Times(AtMost(1));
340 217
341 return desktop_environment; 218 return desktop_environment;
342 } 219 }
343 220
344 InputInjector* ClientSessionTest::CreateInputInjector() { 221 InputInjector* ClientSessionTest::CreateInputInjector() {
345 EXPECT_TRUE(input_injector_); 222 EXPECT_TRUE(input_injector_);
346 return input_injector_.release(); 223 return input_injector_.release();
347 } 224 }
348 225
349 webrtc::ScreenCapturer* ClientSessionTest::CreateVideoCapturer() { 226 webrtc::ScreenCapturer* ClientSessionTest::CreateVideoCapturer() {
350 return new ScreenCapturerFake(); 227 return new ScreenCapturerFake();
351 } 228 }
352 229
353 void ClientSessionTest::ConnectClientSession() { 230 void ClientSessionTest::ConnectClientSession() {
354 client_session_->OnConnectionAuthenticated(client_session_->connection()); 231 client_session_->OnConnectionAuthenticated(client_session_->connection());
355 client_session_->OnConnectionChannelsConnected(client_session_->connection()); 232 client_session_->OnConnectionChannelsConnected(client_session_->connection());
356 } 233 }
357 234
358 void ClientSessionTest::SetSendMessageAndDisconnectExpectation(
359 const std::string& message_type) {
360 protocol::ExtensionMessage message;
361 message.set_type(message_type);
362 message.set_data("data");
363
364 Expectation authenticated =
365 EXPECT_CALL(session_event_handler_, OnSessionAuthenticated(_))
366 .WillOnce(Return(true));
367 EXPECT_CALL(session_event_handler_, OnSessionChannelsConnected(_))
368 .After(authenticated)
369 .WillOnce(DoAll(
370 DeliverClientMessage(client_session_.get(), message),
371 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession),
372 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession)));
373 }
374
375 void ClientSessionTest::QuitMainMessageLoop() { 235 void ClientSessionTest::QuitMainMessageLoop() {
376 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 236 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
377 } 237 }
378 238
379 MATCHER_P2(EqualsClipboardEvent, m, d, "") { 239 MATCHER_P2(EqualsClipboardEvent, m, d, "") {
380 return (strcmp(arg.mime_type().c_str(), m) == 0 && 240 return (strcmp(arg.mime_type().c_str(), m) == 0 &&
381 memcmp(arg.data().data(), d, arg.data().size()) == 0); 241 memcmp(arg.data().data(), d, arg.data().size()) == 0);
382 } 242 }
383 243
384 TEST_F(ClientSessionTest, ClipboardStubFilter) { 244 TEST_F(ClientSessionTest, ClipboardStubFilter) {
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 DeliverClientMessage(client_session_.get(), message), 592 DeliverClientMessage(client_session_.get(), message),
733 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession), 593 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession),
734 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession))); 594 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession)));
735 EXPECT_CALL(*gnubby_auth_handler, DeliverClientMessage(_)); 595 EXPECT_CALL(*gnubby_auth_handler, DeliverClientMessage(_));
736 EXPECT_CALL(session_event_handler_, OnSessionClosed(_)); 596 EXPECT_CALL(session_event_handler_, OnSessionClosed(_));
737 597
738 ConnectClientSession(); 598 ConnectClientSession();
739 message_loop_.Run(); 599 message_loop_.Run();
740 } 600 }
741 601
742 // Verifies that messages can be handled by extensions.
743 TEST_F(ClientSessionTest, ExtensionMessages_MessageHandled) {
744 FakeExtension extension1("ext1", "cap1");
745 FakeExtension extension2("ext2", "cap2");
746 FakeExtension extension3("ext3", "cap3");
747 HostExtensionList extensions;
748 extensions.push_back(&extension1);
749 extensions.push_back(&extension2);
750 extensions.push_back(&extension3);
751
752 EXPECT_CALL(session_event_handler_, OnSessionClientCapabilities(_))
753 .WillOnce(Invoke(CreateFunctor(&CreateExtensionSessions, extensions)));
754
755 SetSendMessageAndDisconnectExpectation("ext2");
756 ConnectClientSession();
757 message_loop_.Run();
758
759 EXPECT_FALSE(extension1.message_handled());
760 EXPECT_TRUE(extension2.message_handled());
761 EXPECT_FALSE(extension3.message_handled());
762 }
763
764 // Verifies that extension messages not handled by extensions don't result in a
765 // crash.
766 TEST_F(ClientSessionTest, ExtensionMessages_MessageNotHandled) {
767 FakeExtension extension1("ext1", "cap1");
768 HostExtensionList extensions;
769 extensions.push_back(&extension1);
770
771 EXPECT_CALL(session_event_handler_, OnSessionClientCapabilities(_))
772 .WillOnce(Invoke(CreateFunctor(&CreateExtensionSessions, extensions)));
773
774 SetSendMessageAndDisconnectExpectation("extX");
775 ConnectClientSession();
776 message_loop_.Run();
777
778 EXPECT_FALSE(extension1.message_handled());
779 }
780
781 TEST_F(ClientSessionTest, ReportCapabilities) {
782 Expectation authenticated =
783 EXPECT_CALL(session_event_handler_, OnSessionAuthenticated(_))
784 .WillOnce(DoAll(
785 AddHostCapabilities(client_session_.get(), "capX capZ"),
786 AddHostCapabilities(client_session_.get(), ""),
787 AddHostCapabilities(client_session_.get(), "capY"),
788 Return(true)));
789 EXPECT_CALL(client_stub_,
790 SetCapabilities(EqCapabilities("capX capY capZ default")));
791 EXPECT_CALL(session_event_handler_, OnSessionChannelsConnected(_))
792 .After(authenticated)
793 .WillOnce(DoAll(
794 InvokeWithoutArgs(this, &ClientSessionTest::DisconnectClientSession),
795 InvokeWithoutArgs(this, &ClientSessionTest::StopClientSession)));
796
797 ConnectClientSession();
798 message_loop_.Run();
799 }
800
801 } // namespace remoting 602 } // namespace remoting
OLDNEW
« no previous file with comments | « trunk/src/remoting/host/client_session.cc ('k') | trunk/src/remoting/host/host_extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698