Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / remoting / host / host_extension_session_manager_unittest.cc
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.
4
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"
14
15 namespace remoting {
16
17 class HostExtensionSessionManagerTest : public testing::Test {
18  public:
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_);
26   }
27   ~HostExtensionSessionManagerTest() override {}
28
29  protected:
30   // Fake HostExtensions for testing.
31   FakeExtension extension1_;
32   FakeExtension extension2_;
33   FakeExtension extension3_;
34   HostExtensionSessionManager::HostExtensions extensions_;
35
36   // Mocks of interfaces provided by ClientSession.
37   MockClientSessionControl client_session_control_;
38   protocol::MockClientStub client_stub_;
39
40   DISALLOW_COPY_AND_ASSIGN(HostExtensionSessionManagerTest);
41 };
42
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());
49
50   protocol::ExtensionMessage message;
51   message.set_type("ext2");
52   extension_manager.OnExtensionMessage(message);
53
54   EXPECT_FALSE(extension1_.has_handled_message());
55   EXPECT_TRUE(extension2_.has_handled_message());
56   EXPECT_FALSE(extension3_.has_handled_message());
57 }
58
59 // Verifies that extension messages not handled by extensions don't result in a
60 // crash.
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());
66
67   protocol::ExtensionMessage message;
68   message.set_type("ext4");
69   extension_manager.OnExtensionMessage(message);
70
71   EXPECT_FALSE(extension1_.has_handled_message());
72   EXPECT_FALSE(extension2_.has_handled_message());
73   EXPECT_FALSE(extension3_.has_handled_message());
74 }
75
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_);
81
82   std::vector<std::string> reported_caps;
83   Tokenize(extension_manager.GetCapabilities(), " ", &reported_caps);
84   std::sort(reported_caps.begin(), reported_caps.end());
85
86   ASSERT_EQ(2U, reported_caps.size());
87   EXPECT_EQ("cap1", reported_caps[0]);
88   EXPECT_EQ("cap3", reported_caps[1]);
89 }
90
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");
97
98   protocol::ExtensionMessage message;
99   message.set_type("ext3");
100   extension_manager.OnExtensionMessage(message);
101
102   EXPECT_TRUE(extension1_.was_instantiated());
103   EXPECT_TRUE(extension2_.was_instantiated());
104   EXPECT_FALSE(extension3_.was_instantiated());
105 }
106
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_);
112
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");
118
119   scoped_ptr<webrtc::DesktopCapturer> dummy_capturer;
120   extension_manager.OnCreateVideoCapturer(&dummy_capturer);
121
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());
127 }
128
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_);
134
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");
140
141   scoped_ptr<VideoEncoder> dummy_encoder;
142   extension_manager.OnCreateVideoEncoder(&dummy_encoder);
143
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());
149 }
150
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_);
156
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");
160
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);
165
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());
171 }
172
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_);
178
179   EXPECT_CALL(client_session_control_, ResetVideoPipeline());
180
181   // Set up only the first extension to request to modify the video pipeline.
182   extension1_.set_steal_video_capturer(true);
183
184   extension_manager.OnNegotiatedCapabilities(&client_stub_, "cap1");
185 }
186
187
188 }  // namespace remoting