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.
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
10 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
11 #include "chrome/browser/media/media_stream_capture_indicator.h"
12 #include "chrome/browser/media/media_stream_device_permissions.h"
13 #include "chrome/browser/media/media_stream_devices_controller.h"
14 #include "chrome/browser/media/webrtc_browsertest_base.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "content/public/common/media_stream_request.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 // Causes the controller to update the TabSpecificContentSettings associated
27 // with the same WebContents with the current permissions. This should be the
28 // last change made to the controller in the test.
29 void NotifyTabSpecificContentSettings(
30 MediaStreamDevicesController
* controller
) {
31 // Note that calling Deny() would have the same effect of passing the current
32 // permissions state to the TabSpecificContentSettings. Deny() and Accept()
33 // differ in their effect on the controller itself, but that is not important
34 // in the tests calling this.
35 if (controller
->IsAskingForAudio() || controller
->IsAskingForVideo())
36 controller
->PermissionGranted();
41 class MediaStreamDevicesControllerTest
: public WebRtcTestBase
{
43 MediaStreamDevicesControllerTest()
44 : example_audio_id_("fake_audio_dev"),
45 example_video_id_("fake_video_dev"),
46 media_stream_result_(content::NUM_MEDIA_REQUEST_RESULTS
) {}
48 // Dummy callback for when we deny the current request directly.
49 void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
50 content::MediaStreamRequestResult result
,
51 scoped_ptr
<content::MediaStreamUI
> ui
) {
52 media_stream_devices_
= devices
;
53 media_stream_result_
= result
;
57 enum DeviceType
{ DEVICE_TYPE_AUDIO
, DEVICE_TYPE_VIDEO
};
58 enum Access
{ ACCESS_ALLOWED
, ACCESS_DENIED
};
60 const GURL
& example_url() const { return example_url_
; }
62 TabSpecificContentSettings
* GetContentSettings() {
63 return TabSpecificContentSettings::FromWebContents(GetWebContents());
66 const std::string
& example_audio_id() const { return example_audio_id_
; }
67 const std::string
& example_video_id() const { return example_video_id_
; }
69 content::MediaStreamRequestResult
media_stream_result() const {
70 return media_stream_result_
;
73 // Sets the device policy-controlled |access| for |example_url_| to be for the
74 // selected |device_type|.
75 void SetDevicePolicy(DeviceType device_type
, Access access
) {
76 PrefService
* prefs
= Profile::FromBrowserContext(
77 GetWebContents()->GetBrowserContext())->GetPrefs();
78 const char* policy_name
= NULL
;
79 switch (device_type
) {
80 case DEVICE_TYPE_AUDIO
:
81 policy_name
= prefs::kAudioCaptureAllowed
;
83 case DEVICE_TYPE_VIDEO
:
84 policy_name
= prefs::kVideoCaptureAllowed
;
87 prefs
->SetBoolean(policy_name
, access
== ACCESS_ALLOWED
);
90 // Set the content settings for mic/cam.
91 void SetContentSettings(ContentSetting mic_setting
,
92 ContentSetting cam_setting
) {
93 HostContentSettingsMap
* content_settings
=
94 Profile::FromBrowserContext(GetWebContents()->GetBrowserContext())
95 ->GetHostContentSettingsMap();
96 ContentSettingsPattern pattern
=
97 ContentSettingsPattern::FromURLNoWildcard(example_url_
);
98 content_settings
->SetContentSetting(pattern
, pattern
,
99 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
100 std::string(), mic_setting
);
101 content_settings
->SetContentSetting(
102 pattern
, pattern
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
103 std::string(), cam_setting
);
106 // Checks whether the devices returned in OnMediaStreamResponse contains a
107 // microphone and/or camera device.
108 bool DevicesContains(bool needs_mic
, bool needs_cam
) {
109 bool has_mic
= false;
110 bool has_cam
= false;
111 for (const auto& device
: media_stream_devices_
) {
112 if (device
.type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
)
114 if (device
.type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
)
118 return needs_mic
== has_mic
&& needs_cam
== has_cam
;
121 content::WebContents
* GetWebContents() {
122 return browser()->tab_strip_model()->GetActiveWebContents();
125 // Creates a MediaStreamRequest, asking for those media types, which have a
126 // non-empty id string.
127 content::MediaStreamRequest
CreateRequest(const std::string
& audio_id
,
128 const std::string
& video_id
) {
129 content::MediaStreamType audio_type
=
130 audio_id
.empty() ? content::MEDIA_NO_SERVICE
131 : content::MEDIA_DEVICE_AUDIO_CAPTURE
;
132 content::MediaStreamType video_type
=
133 video_id
.empty() ? content::MEDIA_NO_SERVICE
134 : content::MEDIA_DEVICE_VIDEO_CAPTURE
;
135 return content::MediaStreamRequest(0,
140 content::MEDIA_DEVICE_ACCESS
,
147 void InitWithUrl(const GURL
& url
) {
148 DCHECK(example_url_
.is_empty());
150 ui_test_utils::NavigateToURL(browser(), example_url_
);
151 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED
,
152 GetContentSettings()->GetMicrophoneCameraState());
156 void SetUpOnMainThread() override
{
157 WebRtcTestBase::SetUpOnMainThread();
160 media_stream_devices_
.clear();
161 media_stream_result_
= content::NUM_MEDIA_REQUEST_RESULTS
;
163 content::MediaStreamDevices audio_devices
;
164 content::MediaStreamDevice
fake_audio_device(
165 content::MEDIA_DEVICE_AUDIO_CAPTURE
, example_audio_id_
,
166 "Fake Audio Device");
167 audio_devices
.push_back(fake_audio_device
);
168 MediaCaptureDevicesDispatcher::GetInstance()->SetTestAudioCaptureDevices(
171 content::MediaStreamDevices video_devices
;
172 content::MediaStreamDevice
fake_video_device(
173 content::MEDIA_DEVICE_VIDEO_CAPTURE
, example_video_id_
,
174 "Fake Video Device");
175 video_devices
.push_back(fake_video_device
);
176 MediaCaptureDevicesDispatcher::GetInstance()->SetTestVideoCaptureDevices(
181 const std::string example_audio_id_
;
182 const std::string example_video_id_
;
184 content::MediaStreamDevices media_stream_devices_
;
185 content::MediaStreamRequestResult media_stream_result_
;
188 // Request and allow microphone access.
189 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndAllowMic
) {
190 InitWithUrl(GURL("https://www.example.com"));
191 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
192 MediaStreamDevicesController
controller(
193 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
194 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
196 NotifyTabSpecificContentSettings(&controller
);
198 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
199 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
200 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
201 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
202 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
,
203 GetContentSettings()->GetMicrophoneCameraState());
204 EXPECT_EQ(example_audio_id(),
205 GetContentSettings()->media_stream_requested_audio_device());
206 EXPECT_EQ(example_audio_id(),
207 GetContentSettings()->media_stream_selected_audio_device());
208 EXPECT_EQ(std::string(),
209 GetContentSettings()->media_stream_requested_video_device());
210 EXPECT_EQ(std::string(),
211 GetContentSettings()->media_stream_selected_video_device());
214 // Request and allow camera access.
215 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndAllowCam
) {
216 InitWithUrl(GURL("https://www.example.com"));
217 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
218 MediaStreamDevicesController
controller(
219 GetWebContents(), CreateRequest(std::string(), example_video_id()),
220 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
222 NotifyTabSpecificContentSettings(&controller
);
224 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
225 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
226 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
227 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
228 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
,
229 GetContentSettings()->GetMicrophoneCameraState());
230 EXPECT_EQ(std::string(),
231 GetContentSettings()->media_stream_requested_audio_device());
232 EXPECT_EQ(std::string(),
233 GetContentSettings()->media_stream_selected_audio_device());
234 EXPECT_EQ(example_video_id(),
235 GetContentSettings()->media_stream_requested_video_device());
236 EXPECT_EQ(example_video_id(),
237 GetContentSettings()->media_stream_selected_video_device());
240 // Request and block microphone access.
241 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndBlockMic
) {
242 InitWithUrl(GURL("https://www.example.com"));
243 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
244 MediaStreamDevicesController
controller(
245 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
246 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
248 NotifyTabSpecificContentSettings(&controller
);
250 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
251 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
252 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
253 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
254 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
255 TabSpecificContentSettings::MICROPHONE_BLOCKED
,
256 GetContentSettings()->GetMicrophoneCameraState());
257 EXPECT_EQ(example_audio_id(),
258 GetContentSettings()->media_stream_requested_audio_device());
259 EXPECT_EQ(example_audio_id(),
260 GetContentSettings()->media_stream_selected_audio_device());
261 EXPECT_EQ(std::string(),
262 GetContentSettings()->media_stream_requested_video_device());
263 EXPECT_EQ(std::string(),
264 GetContentSettings()->media_stream_selected_video_device());
267 // Request and block camera access.
268 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndBlockCam
) {
269 InitWithUrl(GURL("https://www.example.com"));
270 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
271 MediaStreamDevicesController
controller(
272 GetWebContents(), CreateRequest(std::string(), example_video_id()),
273 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
275 NotifyTabSpecificContentSettings(&controller
);
277 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
278 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
279 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
280 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
281 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
|
282 TabSpecificContentSettings::CAMERA_BLOCKED
,
283 GetContentSettings()->GetMicrophoneCameraState());
284 EXPECT_EQ(std::string(),
285 GetContentSettings()->media_stream_requested_audio_device());
286 EXPECT_EQ(std::string(),
287 GetContentSettings()->media_stream_selected_audio_device());
288 EXPECT_EQ(example_video_id(),
289 GetContentSettings()->media_stream_requested_video_device());
290 EXPECT_EQ(example_video_id(),
291 GetContentSettings()->media_stream_selected_video_device());
294 // Request and allow microphone and camera access.
295 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
296 RequestAndAllowMicCam
) {
297 InitWithUrl(GURL("https://www.example.com"));
298 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
299 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
300 MediaStreamDevicesController
controller(
301 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
302 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
304 NotifyTabSpecificContentSettings(&controller
);
306 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
307 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
308 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
309 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
310 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
311 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
312 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
313 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
314 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
315 TabSpecificContentSettings::CAMERA_ACCESSED
,
316 GetContentSettings()->GetMicrophoneCameraState());
317 EXPECT_EQ(example_audio_id(),
318 GetContentSettings()->media_stream_requested_audio_device());
319 EXPECT_EQ(example_audio_id(),
320 GetContentSettings()->media_stream_selected_audio_device());
321 EXPECT_EQ(example_video_id(),
322 GetContentSettings()->media_stream_requested_video_device());
323 EXPECT_EQ(example_video_id(),
324 GetContentSettings()->media_stream_selected_video_device());
327 // Request and block microphone and camera access.
328 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
329 RequestAndBlockMicCam
) {
330 InitWithUrl(GURL("https://www.example.com"));
331 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
332 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
333 MediaStreamDevicesController
controller(
334 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
335 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
337 NotifyTabSpecificContentSettings(&controller
);
339 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
340 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
341 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
342 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
343 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
344 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
345 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
346 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
347 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
348 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
349 TabSpecificContentSettings::CAMERA_ACCESSED
|
350 TabSpecificContentSettings::CAMERA_BLOCKED
,
351 GetContentSettings()->GetMicrophoneCameraState());
352 EXPECT_EQ(example_audio_id(),
353 GetContentSettings()->media_stream_requested_audio_device());
354 EXPECT_EQ(example_audio_id(),
355 GetContentSettings()->media_stream_selected_audio_device());
356 EXPECT_EQ(example_video_id(),
357 GetContentSettings()->media_stream_requested_video_device());
358 EXPECT_EQ(example_video_id(),
359 GetContentSettings()->media_stream_selected_video_device());
362 // Request microphone and camera access. Allow microphone, block camera.
363 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
364 RequestMicCamBlockCam
) {
365 InitWithUrl(GURL("https://www.example.com"));
366 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
367 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
368 MediaStreamDevicesController
controller(
369 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
370 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
372 NotifyTabSpecificContentSettings(&controller
);
374 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
375 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
376 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
377 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
378 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
379 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
380 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
381 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
382 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
383 TabSpecificContentSettings::CAMERA_ACCESSED
|
384 TabSpecificContentSettings::CAMERA_BLOCKED
,
385 GetContentSettings()->GetMicrophoneCameraState());
386 EXPECT_EQ(example_audio_id(),
387 GetContentSettings()->media_stream_requested_audio_device());
388 EXPECT_EQ(example_audio_id(),
389 GetContentSettings()->media_stream_selected_audio_device());
390 EXPECT_EQ(example_video_id(),
391 GetContentSettings()->media_stream_requested_video_device());
392 EXPECT_EQ(example_video_id(),
393 GetContentSettings()->media_stream_selected_video_device());
396 // Request microphone and camera access. Block microphone, allow camera.
397 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
398 RequestMicCamBlockMic
) {
399 InitWithUrl(GURL("https://www.example.com"));
400 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
401 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
402 MediaStreamDevicesController
controller(
403 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
404 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
406 NotifyTabSpecificContentSettings(&controller
);
408 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
409 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
410 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
411 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
412 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
413 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
414 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
415 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
416 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
417 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
418 TabSpecificContentSettings::CAMERA_ACCESSED
,
419 GetContentSettings()->GetMicrophoneCameraState());
420 EXPECT_EQ(example_audio_id(),
421 GetContentSettings()->media_stream_requested_audio_device());
422 EXPECT_EQ(example_audio_id(),
423 GetContentSettings()->media_stream_selected_audio_device());
424 EXPECT_EQ(example_video_id(),
425 GetContentSettings()->media_stream_requested_video_device());
426 EXPECT_EQ(example_video_id(),
427 GetContentSettings()->media_stream_selected_video_device());
430 // Request microphone access. Requesting camera should not change microphone
432 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
433 RequestCamDoesNotChangeMic
) {
434 InitWithUrl(GURL("https://www.example.com"));
435 // Request mic and deny.
436 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
437 MediaStreamDevicesController
mic_controller(
438 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
439 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
441 NotifyTabSpecificContentSettings(&mic_controller
);
442 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
443 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
444 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
445 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
446 EXPECT_EQ(example_audio_id(),
447 GetContentSettings()->media_stream_requested_audio_device());
448 EXPECT_EQ(example_audio_id(),
449 GetContentSettings()->media_stream_selected_audio_device());
451 // Request cam and allow
452 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
453 MediaStreamDevicesController
cam_controller(
454 GetWebContents(), CreateRequest(std::string(), example_video_id()),
455 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
457 NotifyTabSpecificContentSettings(&cam_controller
);
458 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
459 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
460 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
461 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
462 EXPECT_EQ(example_video_id(),
463 GetContentSettings()->media_stream_requested_video_device());
464 EXPECT_EQ(example_video_id(),
465 GetContentSettings()->media_stream_selected_video_device());
467 // Mic state should not have changed.
468 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
469 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
470 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
471 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
472 EXPECT_EQ(example_audio_id(),
473 GetContentSettings()->media_stream_requested_audio_device());
474 EXPECT_EQ(example_audio_id(),
475 GetContentSettings()->media_stream_selected_audio_device());
478 // Denying mic access after camera access should still show the camera as state.
479 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
480 DenyMicDoesNotChangeCam
) {
481 InitWithUrl(GURL("https://www.example.com"));
482 // Request cam and allow
483 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
484 MediaStreamDevicesController
cam_controller(
485 GetWebContents(), CreateRequest(std::string(), example_video_id()),
486 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
488 NotifyTabSpecificContentSettings(&cam_controller
);
489 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
490 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
491 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
492 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
493 EXPECT_EQ(example_video_id(),
494 GetContentSettings()->media_stream_requested_video_device());
495 EXPECT_EQ(example_video_id(),
496 GetContentSettings()->media_stream_selected_video_device());
497 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
,
498 GetContentSettings()->GetMicrophoneCameraState());
500 // Simulate that an a video stream is now being captured.
501 content::MediaStreamDevice
fake_video_device(
502 content::MEDIA_DEVICE_VIDEO_CAPTURE
, example_video_id(),
504 content::MediaStreamDevices
video_devices(1, fake_video_device
);
505 MediaCaptureDevicesDispatcher
* dispatcher
=
506 MediaCaptureDevicesDispatcher::GetInstance();
507 dispatcher
->SetTestVideoCaptureDevices(video_devices
);
508 scoped_ptr
<content::MediaStreamUI
> video_stream_ui
=
509 dispatcher
->GetMediaStreamCaptureIndicator()->
510 RegisterMediaStream(GetWebContents(), video_devices
);
511 video_stream_ui
->OnStarted(base::Closure());
513 // Request mic and deny.
514 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
515 MediaStreamDevicesController
mic_controller(
516 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
517 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
519 NotifyTabSpecificContentSettings(&mic_controller
);
520 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
521 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
522 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
523 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
524 EXPECT_EQ(example_audio_id(),
525 GetContentSettings()->media_stream_requested_audio_device());
526 EXPECT_EQ(example_audio_id(),
527 GetContentSettings()->media_stream_selected_audio_device());
529 // Cam should still be included in the state.
530 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
531 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
532 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
533 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
534 EXPECT_EQ(example_video_id(),
535 GetContentSettings()->media_stream_requested_video_device());
536 EXPECT_EQ(example_video_id(),
537 GetContentSettings()->media_stream_selected_video_device());
538 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
539 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
540 TabSpecificContentSettings::CAMERA_ACCESSED
,
541 GetContentSettings()->GetMicrophoneCameraState());
543 // After ending the camera capture, the camera permission is no longer
544 // relevant, so it should no be included in the mic/cam state.
545 video_stream_ui
.reset();
546 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
547 TabSpecificContentSettings::MICROPHONE_BLOCKED
,
548 GetContentSettings()->GetMicrophoneCameraState());
551 // Stores the ContentSettings inputs for a particular test and has functions
552 // which return the expected outputs for that test.
553 struct ContentSettingsTestData
{
554 // The initial value of the mic/cam content settings.
557 // Whether the infobar should be accepted if it's shown.
560 // Whether the infobar should be displayed to request mic/cam for the given
561 // content settings inputs.
562 bool ExpectMicInfobar() const { return mic
== CONTENT_SETTING_ASK
; }
563 bool ExpectCamInfobar() const { return cam
== CONTENT_SETTING_ASK
; }
565 // Whether or not the mic/cam should be allowed after clicking accept/deny for
567 bool ExpectMicAllowed() const {
568 return mic
== CONTENT_SETTING_ALLOW
||
569 (mic
== CONTENT_SETTING_ASK
&& accept_infobar
);
571 bool ExpectCamAllowed() const {
572 return cam
== CONTENT_SETTING_ALLOW
||
573 (cam
== CONTENT_SETTING_ASK
&& accept_infobar
);
576 // The expected media stream result after clicking accept/deny for the given
578 content::MediaStreamRequestResult
ExpectedMediaStreamResult() const {
579 if (ExpectMicAllowed() || ExpectCamAllowed())
580 return content::MEDIA_DEVICE_OK
;
581 return content::MEDIA_DEVICE_PERMISSION_DENIED
;
585 // Test all combinations of cam/mic content settings. Then tests the result of
586 // clicking both accept/deny on the infobar. Both cam/mic are requested.
587 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, ContentSettings
) {
588 InitWithUrl(GURL("https://www.example.com"));
589 static const ContentSettingsTestData tests
[] = {
590 // Settings that won't result in an infobar.
591 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
, false},
592 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_BLOCK
, false},
593 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ALLOW
, false},
594 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_BLOCK
, false},
596 // Settings that will result in an infobar. Test both accept and deny.
597 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ASK
, false},
598 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ASK
, true},
600 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
, false},
601 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
, true},
603 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ASK
, false},
604 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ASK
, true},
606 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ALLOW
, false},
607 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ALLOW
, true},
609 {CONTENT_SETTING_ASK
, CONTENT_SETTING_BLOCK
, false},
610 {CONTENT_SETTING_ASK
, CONTENT_SETTING_BLOCK
, true},
613 for (auto& test
: tests
) {
614 SetContentSettings(test
.mic
, test
.cam
);
615 MediaStreamDevicesController
controller(
616 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
617 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
620 // Check that the infobar is requesting the expected cam/mic values.
621 ASSERT_EQ(test
.ExpectMicInfobar(), controller
.IsAskingForAudio());
622 ASSERT_EQ(test
.ExpectCamInfobar(), controller
.IsAskingForVideo());
624 // Accept or deny the infobar if it's showing.
625 if (test
.ExpectMicInfobar() || test
.ExpectCamInfobar()) {
626 if (test
.accept_infobar
)
627 controller
.PermissionGranted();
629 controller
.PermissionDenied();
632 // Check the media stream result is expected and the devices returned are
634 ASSERT_EQ(test
.ExpectedMediaStreamResult(), media_stream_result());
636 DevicesContains(test
.ExpectMicAllowed(), test
.ExpectCamAllowed()));
640 // Request and allow camera access on WebUI pages without prompting.
641 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
642 WebUIRequestAndAllowCam
) {
643 InitWithUrl(GURL("chrome://test-page"));
644 MediaStreamDevicesController
controller(
645 GetWebContents(), CreateRequest(std::string(), example_video_id()),
646 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
649 ASSERT_FALSE(controller
.IsAskingForAudio());
650 ASSERT_FALSE(controller
.IsAskingForVideo());
652 ASSERT_EQ(content::MEDIA_DEVICE_OK
, media_stream_result());
653 ASSERT_TRUE(DevicesContains(false, true));
656 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
657 ExtensionRequestMicCam
) {
658 InitWithUrl(GURL("chrome-extension://test-page"));
659 // Test that a prompt is required.
660 MediaStreamDevicesController
controller(
661 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
662 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
664 ASSERT_TRUE(controller
.IsAskingForAudio());
665 ASSERT_TRUE(controller
.IsAskingForVideo());
667 // Accept the prompt.
668 controller
.PermissionGranted();
669 ASSERT_EQ(content::MEDIA_DEVICE_OK
, media_stream_result());
670 ASSERT_TRUE(DevicesContains(true, true));
672 // Check that re-requesting allows without prompting.
673 MediaStreamDevicesController
controller2(
674 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
675 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
677 ASSERT_FALSE(controller2
.IsAskingForAudio());
678 ASSERT_FALSE(controller2
.IsAskingForVideo());
680 ASSERT_EQ(content::MEDIA_DEVICE_OK
, media_stream_result());
681 ASSERT_TRUE(DevicesContains(true, true));