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

Side by Side Diff: content/browser/renderer_host/media/media_stream_dispatcher_host_unittest.cc

Issue 1918173002: Add support for device-change notifications to MediaStreamDispatcher and MediaStreamDispatcherHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase and palmer's comments Created 4 years, 7 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
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 "content/browser/renderer_host/media/media_stream_dispatcher_host.h" 5 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <queue> 8 #include <queue>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback_helpers.h" 13 #include "base/callback_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/system_monitor/system_monitor.h"
18 #include "base/thread_task_runner_handle.h" 19 #include "base/thread_task_runner_handle.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "content/browser/browser_thread_impl.h" 21 #include "content/browser/browser_thread_impl.h"
21 #include "content/browser/renderer_host/media/audio_input_device_manager.h" 22 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
22 #include "content/browser/renderer_host/media/media_stream_manager.h" 23 #include "content/browser/renderer_host/media/media_stream_manager.h"
23 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" 24 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h"
24 #include "content/browser/renderer_host/media/video_capture_manager.h" 25 #include "content/browser/renderer_host/media/video_capture_manager.h"
25 #include "content/common/media/media_stream_messages.h" 26 #include "content/common/media/media_stream_messages.h"
26 #include "content/common/media/media_stream_options.h" 27 #include "content/common/media/media_stream_options.h"
27 #include "content/public/browser/media_device_id.h" 28 #include "content/public/browser/media_device_id.h"
(...skipping 30 matching lines...) Expand all
58 59
59 namespace content { 60 namespace content {
60 61
61 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, 62 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost,
62 public TestContentBrowserClient { 63 public TestContentBrowserClient {
63 public: 64 public:
64 MockMediaStreamDispatcherHost( 65 MockMediaStreamDispatcherHost(
65 const ResourceContext::SaltCallback salt_callback, 66 const ResourceContext::SaltCallback salt_callback,
66 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 67 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
67 MediaStreamManager* manager) 68 MediaStreamManager* manager)
68 : MediaStreamDispatcherHost(kProcessId, salt_callback, manager), 69 : MediaStreamDispatcherHost(kProcessId, salt_callback, manager, true),
69 task_runner_(task_runner), 70 task_runner_(task_runner),
70 current_ipc_(NULL) {} 71 current_ipc_(NULL) {}
71 72
72 // A list of mock methods. 73 // A list of mock methods.
73 MOCK_METHOD4(OnStreamGenerated, 74 MOCK_METHOD4(OnStreamGenerated,
74 void(int routing_id, int request_id, int audio_array_size, 75 void(int routing_id, int request_id, int audio_array_size,
75 int video_array_size)); 76 int video_array_size));
76 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id, 77 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id,
77 int request_id, 78 int request_id,
78 MediaStreamRequestResult result)); 79 MediaStreamRequestResult result));
(...skipping 30 matching lines...) Expand all
109 void OnEnumerateDevices(int render_frame_id, 110 void OnEnumerateDevices(int render_frame_id,
110 int page_request_id, 111 int page_request_id,
111 MediaStreamType type, 112 MediaStreamType type,
112 const url::Origin& security_origin, 113 const url::Origin& security_origin,
113 const base::Closure& quit_closure) { 114 const base::Closure& quit_closure) {
114 quit_closures_.push(quit_closure); 115 quit_closures_.push(quit_closure);
115 MediaStreamDispatcherHost::OnEnumerateDevices( 116 MediaStreamDispatcherHost::OnEnumerateDevices(
116 render_frame_id, page_request_id, type, security_origin); 117 render_frame_id, page_request_id, type, security_origin);
117 } 118 }
118 119
120 void OnCancelEnumerateDevices(int render_frame_id, int page_request_id) {
121 MediaStreamDispatcherHost::OnCancelEnumerateDevices(render_frame_id,
122 page_request_id);
123 }
124
125 void OnSubscribeToDeviceChangeNotifications(
126 int render_frame_id,
127 const url::Origin& security_origin,
128 const base::Closure& quit_closure) {
129 quit_closures_.push(quit_closure);
130 MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications(
131 render_frame_id, security_origin);
132 }
133
119 std::string label_; 134 std::string label_;
120 StreamDeviceInfoArray audio_devices_; 135 StreamDeviceInfoArray audio_devices_;
121 StreamDeviceInfoArray video_devices_; 136 StreamDeviceInfoArray video_devices_;
122 StreamDeviceInfo opened_device_; 137 StreamDeviceInfo opened_device_;
123 StreamDeviceInfoArray enumerated_devices_; 138 StreamDeviceInfoArray enumerated_devices_;
124 139
125 private: 140 private:
126 ~MockMediaStreamDispatcherHost() override {} 141 ~MockMediaStreamDispatcherHost() override {}
127 142
128 // This method is used to dispatch IPC messages to the renderer. We intercept 143 // This method is used to dispatch IPC messages to the renderer. We intercept
129 // these messages here and dispatch to our mock methods to verify the 144 // these messages here and dispatch to our mock methods to verify the
130 // conversation between this object and the renderer. 145 // conversation between this object and the renderer.
131 bool Send(IPC::Message* message) override { 146 bool Send(IPC::Message* message) override {
132 CHECK(message); 147 CHECK(message);
133 current_ipc_ = message; 148 current_ipc_ = message;
134 149
135 // In this method we dispatch the messages to the according handlers as if 150 // In this method we dispatch the messages to the corresponding handlers as
136 // we are the renderer. 151 // if we are the renderer.
137 bool handled = true; 152 bool handled = true;
138 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message) 153 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message)
139 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, 154 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated,
140 OnStreamGeneratedInternal) 155 OnStreamGeneratedInternal)
141 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, 156 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed,
142 OnStreamGenerationFailedInternal) 157 OnStreamGenerationFailedInternal)
143 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal) 158 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal)
144 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal) 159 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal)
145 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, OnDevicesEnumerated) 160 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, OnDevicesEnumerated)
161 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesChanged, OnDevicesChanged)
146 IPC_MESSAGE_UNHANDLED(handled = false) 162 IPC_MESSAGE_UNHANDLED(handled = false)
147 IPC_END_MESSAGE_MAP() 163 IPC_END_MESSAGE_MAP()
148 EXPECT_TRUE(handled); 164 EXPECT_TRUE(handled);
149 165
150 delete message; 166 delete message;
151 current_ipc_ = NULL; 167 current_ipc_ = NULL;
152 return true; 168 return true;
153 } 169 }
154 170
155 // These handler methods do minimal things and delegate to the mock methods. 171 // These handler methods do minimal things and delegate to the mock methods.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 220 }
205 221
206 void OnDevicesEnumerated(int request_id, 222 void OnDevicesEnumerated(int request_id,
207 const StreamDeviceInfoArray& devices) { 223 const StreamDeviceInfoArray& devices) {
208 base::Closure quit_closure = quit_closures_.front(); 224 base::Closure quit_closure = quit_closures_.front();
209 quit_closures_.pop(); 225 quit_closures_.pop();
210 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); 226 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure));
211 enumerated_devices_ = devices; 227 enumerated_devices_ = devices;
212 } 228 }
213 229
230 void OnDevicesChanged() {
231 base::Closure quit_closure = quit_closures_.front();
232 quit_closures_.pop();
233 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure));
234 }
235
214 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 236 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
215 IPC::Message* current_ipc_; 237 IPC::Message* current_ipc_;
216 std::queue<base::Closure> quit_closures_; 238 std::queue<base::Closure> quit_closures_;
217 }; 239 };
218 240
219 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy { 241 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy {
220 public: 242 public:
221 MOCK_METHOD2( 243 MOCK_METHOD2(
222 OnStarted, 244 OnStarted,
223 void(const base::Closure& stop, 245 void(const base::Closure& stop,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 void EnumerateDevicesAndWaitForResult(int render_frame_id, 366 void EnumerateDevicesAndWaitForResult(int render_frame_id,
345 int page_request_id, 367 int page_request_id,
346 MediaStreamType type) { 368 MediaStreamType type) {
347 base::RunLoop run_loop; 369 base::RunLoop run_loop;
348 host_->OnEnumerateDevices(render_frame_id, page_request_id, type, origin_, 370 host_->OnEnumerateDevices(render_frame_id, page_request_id, type, origin_,
349 run_loop.QuitClosure()); 371 run_loop.QuitClosure());
350 run_loop.Run(); 372 run_loop.Run();
351 ASSERT_FALSE(host_->enumerated_devices_.empty()); 373 ASSERT_FALSE(host_->enumerated_devices_.empty());
352 EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_)); 374 EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_));
353 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_)); 375 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_));
376 // Enumeration requests must be cancelled manually.
377 host_->OnCancelEnumerateDevices(render_frame_id, page_request_id);
378 }
379
380 void SubscribeToDeviceChangeNotificationsAndWaitForNotification(
381 int render_frame_id) {
382 base::RunLoop run_loop;
383 host_->OnSubscribeToDeviceChangeNotifications(render_frame_id, origin_,
384 run_loop.QuitClosure());
385 // Simulate a change in the set of devices.
386 video_capture_device_factory_->set_number_of_devices(5);
387 media_stream_manager_->OnDevicesChanged(
388 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
389 run_loop.Run();
354 } 390 }
355 391
356 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) { 392 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) {
357 for (size_t i = 0; i < devices.size(); ++i) { 393 for (size_t i = 0; i < devices.size(); ++i) {
358 media::AudioDeviceNames::const_iterator audio_it = 394 media::AudioDeviceNames::const_iterator audio_it =
359 physical_audio_devices_.begin(); 395 physical_audio_devices_.begin();
360 for (; audio_it != physical_audio_devices_.end(); ++audio_it) { 396 for (; audio_it != physical_audio_devices_.end(); ++audio_it) {
361 if (audio_it->unique_id == devices[i].device.id) 397 if (audio_it->unique_id == devices[i].device.id)
362 return true; 398 return true;
363 } 399 }
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 } 945 }
910 946
911 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevicesNoAccess) { 947 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevicesNoAccess) {
912 SetupFakeUI(false); 948 SetupFakeUI(false);
913 stream_ui_->SetCameraAccess(false); 949 stream_ui_->SetCameraAccess(false);
914 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, 950 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId,
915 MEDIA_DEVICE_VIDEO_CAPTURE); 951 MEDIA_DEVICE_VIDEO_CAPTURE);
916 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_)); 952 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_));
917 } 953 }
918 954
955 TEST_F(MediaStreamDispatcherHostTest, DeviceChangeNotification) {
956 SetupFakeUI(false);
957 // warm up the cache
958 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId,
959 MEDIA_DEVICE_VIDEO_CAPTURE);
960 SubscribeToDeviceChangeNotificationsAndWaitForNotification(kRenderId);
961 }
962
919 }; // namespace content 963 }; // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698