1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/string_util.h"
6 #include "remoting/codec/video_encoder.h"
7 #include "remoting/host/fake_host_extension.h"
8 #include "remoting/host/host_extension_session_manager.h"
9 #include "remoting/host/host_mock_objects.h"
10 #include "remoting/proto/control.pb.h"
11 #include "remoting/protocol/protocol_mock_objects.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
17 class HostExtensionSessionManagerTest
: public testing::Test
{
19 HostExtensionSessionManagerTest()
20 : extension1_("ext1", "cap1"),
21 extension2_("ext2", std::string()),
22 extension3_("ext3", "cap3") {
23 extensions_
.push_back(&extension1_
);
24 extensions_
.push_back(&extension2_
);
25 extensions_
.push_back(&extension3_
);
27 ~HostExtensionSessionManagerTest() override
{}
30 // Fake HostExtensions for testing.
31 FakeExtension extension1_
;
32 FakeExtension extension2_
;
33 FakeExtension extension3_
;
34 HostExtensionSessionManager::HostExtensions extensions_
;
36 // Mocks of interfaces provided by ClientSession.
37 MockClientSessionControl client_session_control_
;
38 protocol::MockClientStub client_stub_
;
40 DISALLOW_COPY_AND_ASSIGN(HostExtensionSessionManagerTest
);
43 // Verifies that messages are handled by the correct extension.
44 TEST_F(HostExtensionSessionManagerTest
, ExtensionMessages_MessageHandled
) {
45 HostExtensionSessionManager
extension_manager(extensions_
,
46 &client_session_control_
);
47 extension_manager
.OnNegotiatedCapabilities(
48 &client_stub_
, extension_manager
.GetCapabilities());
50 protocol::ExtensionMessage message
;
51 message
.set_type("ext2");
52 extension_manager
.OnExtensionMessage(message
);
54 EXPECT_FALSE(extension1_
.has_handled_message());
55 EXPECT_TRUE(extension2_
.has_handled_message());
56 EXPECT_FALSE(extension3_
.has_handled_message());
59 // Verifies that extension messages not handled by extensions don't result in a
61 TEST_F(HostExtensionSessionManagerTest
, ExtensionMessages_MessageNotHandled
) {
62 HostExtensionSessionManager
extension_manager(extensions_
,
63 &client_session_control_
);
64 extension_manager
.OnNegotiatedCapabilities(
65 &client_stub_
, extension_manager
.GetCapabilities());
67 protocol::ExtensionMessage message
;
68 message
.set_type("ext4");
69 extension_manager
.OnExtensionMessage(message
);
71 EXPECT_FALSE(extension1_
.has_handled_message());
72 EXPECT_FALSE(extension2_
.has_handled_message());
73 EXPECT_FALSE(extension3_
.has_handled_message());
76 // Verifies that the correct set of capabilities are reported to the client,
77 // based on the registered extensions.
78 TEST_F(HostExtensionSessionManagerTest
, ExtensionCapabilities_AreReported
) {
79 HostExtensionSessionManager
extension_manager(extensions_
,
80 &client_session_control_
);
82 std::vector
<std::string
> reported_caps
;
83 Tokenize(extension_manager
.GetCapabilities(), " ", &reported_caps
);
84 std::sort(reported_caps
.begin(), reported_caps
.end());
86 ASSERT_EQ(2U, reported_caps
.size());
87 EXPECT_EQ("cap1", reported_caps
[0]);
88 EXPECT_EQ("cap3", reported_caps
[1]);
91 // Verifies that an extension is not instantiated if the client does not
92 // support its required capability, and that it does not receive messages.
93 TEST_F(HostExtensionSessionManagerTest
, ExtensionCapabilities_AreChecked
) {
94 HostExtensionSessionManager
extension_manager(extensions_
,
95 &client_session_control_
);
96 extension_manager
.OnNegotiatedCapabilities(&client_stub_
, "cap1");
98 protocol::ExtensionMessage message
;
99 message
.set_type("ext3");
100 extension_manager
.OnExtensionMessage(message
);
102 EXPECT_TRUE(extension1_
.was_instantiated());
103 EXPECT_TRUE(extension2_
.was_instantiated());
104 EXPECT_FALSE(extension3_
.was_instantiated());
107 // Verifies that matching extensions are given the opportunity to wrap or
108 // replace the video capturer.
109 TEST_F(HostExtensionSessionManagerTest
, CanWrapVideoCapturer
) {
110 HostExtensionSessionManager
extension_manager(extensions_
,
111 &client_session_control_
);
113 // Set up all the extensions to request to modify the video pipeline.
114 extension1_
.set_steal_video_capturer(true);
115 extension2_
.set_steal_video_capturer(true);
116 extension3_
.set_steal_video_capturer(true);
117 extension_manager
.OnNegotiatedCapabilities(&client_stub_
, "cap1");
119 scoped_ptr
<webrtc::DesktopCapturer
> dummy_capturer
;
120 extension_manager
.OnCreateVideoCapturer(&dummy_capturer
);
122 EXPECT_FALSE(extension1_
.has_wrapped_video_encoder());
123 EXPECT_TRUE(extension1_
.has_wrapped_video_capturer());
124 EXPECT_FALSE(extension2_
.has_wrapped_video_encoder());
125 EXPECT_TRUE(extension2_
.has_wrapped_video_capturer());
126 EXPECT_FALSE(extension3_
.was_instantiated());
129 // Verifies that matching extensions are given the opportunity to wrap or
130 // replace the video encoders.
131 TEST_F(HostExtensionSessionManagerTest
, CanWrapVideoEncoder
) {
132 HostExtensionSessionManager
extension_manager(extensions_
,
133 &client_session_control_
);
135 // Set up all the extensions to request to modify the video pipeline.
136 extension1_
.set_steal_video_capturer(true);
137 extension2_
.set_steal_video_capturer(true);
138 extension3_
.set_steal_video_capturer(true);
139 extension_manager
.OnNegotiatedCapabilities(&client_stub_
, "cap1");
141 scoped_ptr
<VideoEncoder
> dummy_encoder
;
142 extension_manager
.OnCreateVideoEncoder(&dummy_encoder
);
144 EXPECT_TRUE(extension1_
.has_wrapped_video_encoder());
145 EXPECT_FALSE(extension1_
.has_wrapped_video_capturer());
146 EXPECT_TRUE(extension2_
.has_wrapped_video_encoder());
147 EXPECT_FALSE(extension2_
.has_wrapped_video_capturer());
148 EXPECT_FALSE(extension3_
.was_instantiated());
151 // Verifies that only extensions which report that they modify the video
152 // pipeline actually get called to modify it.
153 TEST_F(HostExtensionSessionManagerTest
, RespectModifiesVideoPipeline
) {
154 HostExtensionSessionManager
extension_manager(extensions_
,
155 &client_session_control_
);
157 // Set up the second extension to request to modify the video pipeline.
158 extension2_
.set_steal_video_capturer(true);
159 extension_manager
.OnNegotiatedCapabilities(&client_stub_
, "cap1");
161 scoped_ptr
<webrtc::DesktopCapturer
> dummy_capturer
;
162 extension_manager
.OnCreateVideoCapturer(&dummy_capturer
);
163 scoped_ptr
<VideoEncoder
> dummy_encoder
;
164 extension_manager
.OnCreateVideoEncoder(&dummy_encoder
);
166 EXPECT_FALSE(extension1_
.has_wrapped_video_encoder());
167 EXPECT_FALSE(extension1_
.has_wrapped_video_capturer());
168 EXPECT_TRUE(extension2_
.has_wrapped_video_encoder());
169 EXPECT_TRUE(extension2_
.has_wrapped_video_capturer());
170 EXPECT_FALSE(extension3_
.was_instantiated());
173 // Verifies that if an extension reports that it modifies the video pipeline
174 // then ResetVideoPipeline() is called on the ClientSessionControl interface.
175 TEST_F(HostExtensionSessionManagerTest
, CallsResetVideoPipeline
) {
176 HostExtensionSessionManager
extension_manager(extensions_
,
177 &client_session_control_
);
179 EXPECT_CALL(client_session_control_
, ResetVideoPipeline());
181 // Set up only the first extension to request to modify the video pipeline.
182 extension1_
.set_steal_video_capturer(true);
184 extension_manager
.OnNegotiatedCapabilities(&client_stub_
, "cap1");
188 } // namespace remoting