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 MediaStreamDevicesControllerTestBase
: public WebRtcTestBase
{
43 explicit MediaStreamDevicesControllerTestBase(const GURL
& url
)
45 example_audio_id_("fake_audio_dev"),
46 example_video_id_("fake_video_dev"),
47 media_stream_result_(content::NUM_MEDIA_REQUEST_RESULTS
) {}
49 // Dummy callback for when we deny the current request directly.
50 void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
51 content::MediaStreamRequestResult result
,
52 scoped_ptr
<content::MediaStreamUI
> ui
) {
53 media_stream_devices_
= devices
;
54 media_stream_result_
= result
;
58 enum DeviceType
{ DEVICE_TYPE_AUDIO
, DEVICE_TYPE_VIDEO
};
59 enum Access
{ ACCESS_ALLOWED
, ACCESS_DENIED
};
61 const GURL
& example_url() const { return example_url_
; }
63 TabSpecificContentSettings
* GetContentSettings() {
64 return TabSpecificContentSettings::FromWebContents(GetWebContents());
67 const std::string
& example_audio_id() const { return example_audio_id_
; }
68 const std::string
& example_video_id() const { return example_video_id_
; }
70 content::MediaStreamRequestResult
media_stream_result() const {
71 return media_stream_result_
;
74 // Sets the device policy-controlled |access| for |example_url_| to be for the
75 // selected |device_type|.
76 void SetDevicePolicy(DeviceType device_type
, Access access
) {
77 PrefService
* prefs
= Profile::FromBrowserContext(
78 GetWebContents()->GetBrowserContext())->GetPrefs();
79 const char* policy_name
= NULL
;
80 switch (device_type
) {
81 case DEVICE_TYPE_AUDIO
:
82 policy_name
= prefs::kAudioCaptureAllowed
;
84 case DEVICE_TYPE_VIDEO
:
85 policy_name
= prefs::kVideoCaptureAllowed
;
88 prefs
->SetBoolean(policy_name
, access
== ACCESS_ALLOWED
);
91 // Set the content settings for mic/cam.
92 void SetContentSettings(ContentSetting mic_setting
,
93 ContentSetting cam_setting
) {
94 HostContentSettingsMap
* content_settings
=
95 Profile::FromBrowserContext(GetWebContents()->GetBrowserContext())
96 ->GetHostContentSettingsMap();
97 ContentSettingsPattern pattern
=
98 ContentSettingsPattern::FromURLNoWildcard(example_url_
);
99 content_settings
->SetContentSetting(pattern
, pattern
,
100 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
101 std::string(), mic_setting
);
102 content_settings
->SetContentSetting(
103 pattern
, pattern
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
104 std::string(), cam_setting
);
107 // Checks whether the devices returned in OnMediaStreamResponse contains a
108 // microphone and/or camera device.
109 bool DevicesContains(bool needs_mic
, bool needs_cam
) {
110 bool has_mic
= false;
111 bool has_cam
= false;
112 for (const auto& device
: media_stream_devices_
) {
113 if (device
.type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
)
115 if (device
.type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
)
119 return needs_mic
== has_mic
&& needs_cam
== has_cam
;
122 content::WebContents
* GetWebContents() {
123 return browser()->tab_strip_model()->GetActiveWebContents();
126 // Creates a MediaStreamRequest, asking for those media types, which have a
127 // non-empty id string.
128 content::MediaStreamRequest
CreateRequest(const std::string
& audio_id
,
129 const std::string
& video_id
) {
130 content::MediaStreamType audio_type
=
131 audio_id
.empty() ? content::MEDIA_NO_SERVICE
132 : content::MEDIA_DEVICE_AUDIO_CAPTURE
;
133 content::MediaStreamType video_type
=
134 video_id
.empty() ? content::MEDIA_NO_SERVICE
135 : content::MEDIA_DEVICE_VIDEO_CAPTURE
;
136 return content::MediaStreamRequest(0,
141 content::MEDIA_DEVICE_ACCESS
,
149 void SetUpOnMainThread() override
{
150 WebRtcTestBase::SetUpOnMainThread();
153 media_stream_devices_
.clear();
154 media_stream_result_
= content::NUM_MEDIA_REQUEST_RESULTS
;
156 content::MediaStreamDevices audio_devices
;
157 content::MediaStreamDevice
fake_audio_device(
158 content::MEDIA_DEVICE_AUDIO_CAPTURE
, example_audio_id_
,
159 "Fake Audio Device");
160 audio_devices
.push_back(fake_audio_device
);
161 MediaCaptureDevicesDispatcher::GetInstance()->SetTestAudioCaptureDevices(
164 content::MediaStreamDevices video_devices
;
165 content::MediaStreamDevice
fake_video_device(
166 content::MEDIA_DEVICE_VIDEO_CAPTURE
, example_video_id_
,
167 "Fake Video Device");
168 video_devices
.push_back(fake_video_device
);
169 MediaCaptureDevicesDispatcher::GetInstance()->SetTestVideoCaptureDevices(
172 ui_test_utils::NavigateToURL(browser(), example_url_
);
174 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED
,
175 GetContentSettings()->GetMicrophoneCameraState());
178 const GURL example_url_
;
179 const std::string example_audio_id_
;
180 const std::string example_video_id_
;
182 content::MediaStreamDevices media_stream_devices_
;
183 content::MediaStreamRequestResult media_stream_result_
;
186 class MediaStreamDevicesControllerTest
187 : public MediaStreamDevicesControllerTestBase
{
189 MediaStreamDevicesControllerTest()
190 : MediaStreamDevicesControllerTestBase(GURL("https://www.example.com")) {}
193 class MediaStreamDevicesControllerWebUITest
194 : public MediaStreamDevicesControllerTestBase
{
196 MediaStreamDevicesControllerWebUITest()
197 : MediaStreamDevicesControllerTestBase(GURL("chrome://test-page")) {}
200 // Request and allow microphone access.
201 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndAllowMic
) {
202 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
203 MediaStreamDevicesController
controller(
204 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
205 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
207 NotifyTabSpecificContentSettings(&controller
);
209 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
210 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
211 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
212 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
213 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
,
214 GetContentSettings()->GetMicrophoneCameraState());
215 EXPECT_EQ(example_audio_id(),
216 GetContentSettings()->media_stream_requested_audio_device());
217 EXPECT_EQ(example_audio_id(),
218 GetContentSettings()->media_stream_selected_audio_device());
219 EXPECT_EQ(std::string(),
220 GetContentSettings()->media_stream_requested_video_device());
221 EXPECT_EQ(std::string(),
222 GetContentSettings()->media_stream_selected_video_device());
225 // Request and allow camera access.
226 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndAllowCam
) {
227 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
228 MediaStreamDevicesController
controller(
229 GetWebContents(), CreateRequest(std::string(), example_video_id()),
230 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
232 NotifyTabSpecificContentSettings(&controller
);
234 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
235 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
236 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
237 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
238 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
,
239 GetContentSettings()->GetMicrophoneCameraState());
240 EXPECT_EQ(std::string(),
241 GetContentSettings()->media_stream_requested_audio_device());
242 EXPECT_EQ(std::string(),
243 GetContentSettings()->media_stream_selected_audio_device());
244 EXPECT_EQ(example_video_id(),
245 GetContentSettings()->media_stream_requested_video_device());
246 EXPECT_EQ(example_video_id(),
247 GetContentSettings()->media_stream_selected_video_device());
250 // Request and block microphone access.
251 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndBlockMic
) {
252 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
253 MediaStreamDevicesController
controller(
254 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
255 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
257 NotifyTabSpecificContentSettings(&controller
);
259 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
260 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
261 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
262 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
263 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
264 TabSpecificContentSettings::MICROPHONE_BLOCKED
,
265 GetContentSettings()->GetMicrophoneCameraState());
266 EXPECT_EQ(example_audio_id(),
267 GetContentSettings()->media_stream_requested_audio_device());
268 EXPECT_EQ(example_audio_id(),
269 GetContentSettings()->media_stream_selected_audio_device());
270 EXPECT_EQ(std::string(),
271 GetContentSettings()->media_stream_requested_video_device());
272 EXPECT_EQ(std::string(),
273 GetContentSettings()->media_stream_selected_video_device());
276 // Request and block camera access.
277 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, RequestAndBlockCam
) {
278 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
279 MediaStreamDevicesController
controller(
280 GetWebContents(), CreateRequest(std::string(), example_video_id()),
281 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
283 NotifyTabSpecificContentSettings(&controller
);
285 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
286 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
287 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
288 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
289 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
|
290 TabSpecificContentSettings::CAMERA_BLOCKED
,
291 GetContentSettings()->GetMicrophoneCameraState());
292 EXPECT_EQ(std::string(),
293 GetContentSettings()->media_stream_requested_audio_device());
294 EXPECT_EQ(std::string(),
295 GetContentSettings()->media_stream_selected_audio_device());
296 EXPECT_EQ(example_video_id(),
297 GetContentSettings()->media_stream_requested_video_device());
298 EXPECT_EQ(example_video_id(),
299 GetContentSettings()->media_stream_selected_video_device());
302 // Request and allow microphone and camera access.
303 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
304 RequestAndAllowMicCam
) {
305 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
306 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
307 MediaStreamDevicesController
controller(
308 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
309 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
311 NotifyTabSpecificContentSettings(&controller
);
313 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
314 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
315 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
316 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
317 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
318 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
319 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
320 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
321 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
322 TabSpecificContentSettings::CAMERA_ACCESSED
,
323 GetContentSettings()->GetMicrophoneCameraState());
324 EXPECT_EQ(example_audio_id(),
325 GetContentSettings()->media_stream_requested_audio_device());
326 EXPECT_EQ(example_audio_id(),
327 GetContentSettings()->media_stream_selected_audio_device());
328 EXPECT_EQ(example_video_id(),
329 GetContentSettings()->media_stream_requested_video_device());
330 EXPECT_EQ(example_video_id(),
331 GetContentSettings()->media_stream_selected_video_device());
334 // Request and block microphone and camera access.
335 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
336 RequestAndBlockMicCam
) {
337 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
338 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
339 MediaStreamDevicesController
controller(
340 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
341 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
343 NotifyTabSpecificContentSettings(&controller
);
345 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
346 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
347 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
348 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
349 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
350 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
351 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
352 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
353 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
354 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
355 TabSpecificContentSettings::CAMERA_ACCESSED
|
356 TabSpecificContentSettings::CAMERA_BLOCKED
,
357 GetContentSettings()->GetMicrophoneCameraState());
358 EXPECT_EQ(example_audio_id(),
359 GetContentSettings()->media_stream_requested_audio_device());
360 EXPECT_EQ(example_audio_id(),
361 GetContentSettings()->media_stream_selected_audio_device());
362 EXPECT_EQ(example_video_id(),
363 GetContentSettings()->media_stream_requested_video_device());
364 EXPECT_EQ(example_video_id(),
365 GetContentSettings()->media_stream_selected_video_device());
368 // Request microphone and camera access. Allow microphone, block camera.
369 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
370 RequestMicCamBlockCam
) {
371 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_ALLOWED
);
372 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_DENIED
);
373 MediaStreamDevicesController
controller(
374 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
375 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
377 NotifyTabSpecificContentSettings(&controller
);
379 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
380 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
381 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
382 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
383 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
384 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
385 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
386 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
387 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
388 TabSpecificContentSettings::CAMERA_ACCESSED
|
389 TabSpecificContentSettings::CAMERA_BLOCKED
,
390 GetContentSettings()->GetMicrophoneCameraState());
391 EXPECT_EQ(example_audio_id(),
392 GetContentSettings()->media_stream_requested_audio_device());
393 EXPECT_EQ(example_audio_id(),
394 GetContentSettings()->media_stream_selected_audio_device());
395 EXPECT_EQ(example_video_id(),
396 GetContentSettings()->media_stream_requested_video_device());
397 EXPECT_EQ(example_video_id(),
398 GetContentSettings()->media_stream_selected_video_device());
401 // Request microphone and camera access. Block microphone, allow camera.
402 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
403 RequestMicCamBlockMic
) {
404 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
405 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
406 MediaStreamDevicesController
controller(
407 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
408 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
410 NotifyTabSpecificContentSettings(&controller
);
412 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
413 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
414 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
415 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
416 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
417 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
418 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
419 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
420 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
421 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
422 TabSpecificContentSettings::CAMERA_ACCESSED
,
423 GetContentSettings()->GetMicrophoneCameraState());
424 EXPECT_EQ(example_audio_id(),
425 GetContentSettings()->media_stream_requested_audio_device());
426 EXPECT_EQ(example_audio_id(),
427 GetContentSettings()->media_stream_selected_audio_device());
428 EXPECT_EQ(example_video_id(),
429 GetContentSettings()->media_stream_requested_video_device());
430 EXPECT_EQ(example_video_id(),
431 GetContentSettings()->media_stream_selected_video_device());
434 // Request microphone access. Requesting camera should not change microphone
436 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
437 RequestCamDoesNotChangeMic
) {
438 // Request mic and deny.
439 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
440 MediaStreamDevicesController
mic_controller(
441 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
442 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
444 NotifyTabSpecificContentSettings(&mic_controller
);
445 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
446 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
447 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
448 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
449 EXPECT_EQ(example_audio_id(),
450 GetContentSettings()->media_stream_requested_audio_device());
451 EXPECT_EQ(example_audio_id(),
452 GetContentSettings()->media_stream_selected_audio_device());
454 // Request cam and allow
455 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
456 MediaStreamDevicesController
cam_controller(
457 GetWebContents(), CreateRequest(std::string(), example_video_id()),
458 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
460 NotifyTabSpecificContentSettings(&cam_controller
);
461 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
462 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
463 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
464 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
465 EXPECT_EQ(example_video_id(),
466 GetContentSettings()->media_stream_requested_video_device());
467 EXPECT_EQ(example_video_id(),
468 GetContentSettings()->media_stream_selected_video_device());
470 // Mic state should not have changed.
471 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
472 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
473 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
474 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
475 EXPECT_EQ(example_audio_id(),
476 GetContentSettings()->media_stream_requested_audio_device());
477 EXPECT_EQ(example_audio_id(),
478 GetContentSettings()->media_stream_selected_audio_device());
481 // Denying mic access after camera access should still show the camera as state.
482 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
,
483 DenyMicDoesNotChangeCam
) {
484 // Request cam and allow
485 SetDevicePolicy(DEVICE_TYPE_VIDEO
, ACCESS_ALLOWED
);
486 MediaStreamDevicesController
cam_controller(
487 GetWebContents(), CreateRequest(std::string(), example_video_id()),
488 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
490 NotifyTabSpecificContentSettings(&cam_controller
);
491 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
492 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
493 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
494 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
495 EXPECT_EQ(example_video_id(),
496 GetContentSettings()->media_stream_requested_video_device());
497 EXPECT_EQ(example_video_id(),
498 GetContentSettings()->media_stream_selected_video_device());
499 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED
,
500 GetContentSettings()->GetMicrophoneCameraState());
502 // Simulate that an a video stream is now being captured.
503 content::MediaStreamDevice
fake_video_device(
504 content::MEDIA_DEVICE_VIDEO_CAPTURE
, example_video_id(),
506 content::MediaStreamDevices
video_devices(1, fake_video_device
);
507 MediaCaptureDevicesDispatcher
* dispatcher
=
508 MediaCaptureDevicesDispatcher::GetInstance();
509 dispatcher
->SetTestVideoCaptureDevices(video_devices
);
510 scoped_ptr
<content::MediaStreamUI
> video_stream_ui
=
511 dispatcher
->GetMediaStreamCaptureIndicator()->
512 RegisterMediaStream(GetWebContents(), video_devices
);
513 video_stream_ui
->OnStarted(base::Closure());
515 // Request mic and deny.
516 SetDevicePolicy(DEVICE_TYPE_AUDIO
, ACCESS_DENIED
);
517 MediaStreamDevicesController
mic_controller(
518 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
519 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
521 NotifyTabSpecificContentSettings(&mic_controller
);
522 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
523 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
524 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
525 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
526 EXPECT_EQ(example_audio_id(),
527 GetContentSettings()->media_stream_requested_audio_device());
528 EXPECT_EQ(example_audio_id(),
529 GetContentSettings()->media_stream_selected_audio_device());
531 // Cam should still be included in the state.
532 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
533 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
534 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
535 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
536 EXPECT_EQ(example_video_id(),
537 GetContentSettings()->media_stream_requested_video_device());
538 EXPECT_EQ(example_video_id(),
539 GetContentSettings()->media_stream_selected_video_device());
540 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
541 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
542 TabSpecificContentSettings::CAMERA_ACCESSED
,
543 GetContentSettings()->GetMicrophoneCameraState());
545 // After ending the camera capture, the camera permission is no longer
546 // relevant, so it should no be included in the mic/cam state.
547 video_stream_ui
.reset();
548 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED
|
549 TabSpecificContentSettings::MICROPHONE_BLOCKED
,
550 GetContentSettings()->GetMicrophoneCameraState());
553 // Stores the ContentSettings inputs for a particular test and has functions
554 // which return the expected outputs for that test.
555 struct ContentSettingsTestData
{
556 // The initial value of the mic/cam content settings.
559 // Whether the infobar should be accepted if it's shown.
562 // Whether the infobar should be displayed to request mic/cam for the given
563 // content settings inputs.
564 bool ExpectMicInfobar() const { return mic
== CONTENT_SETTING_ASK
; }
565 bool ExpectCamInfobar() const { return cam
== CONTENT_SETTING_ASK
; }
567 // Whether or not the mic/cam should be allowed after clicking accept/deny for
569 bool ExpectMicAllowed() const {
570 return mic
== CONTENT_SETTING_ALLOW
||
571 (mic
== CONTENT_SETTING_ASK
&& accept_infobar
);
573 bool ExpectCamAllowed() const {
574 return cam
== CONTENT_SETTING_ALLOW
||
575 (cam
== CONTENT_SETTING_ASK
&& accept_infobar
);
578 // The expected media stream result after clicking accept/deny for the given
580 content::MediaStreamRequestResult
ExpectedMediaStreamResult() const {
581 if (ExpectMicAllowed() || ExpectCamAllowed())
582 return content::MEDIA_DEVICE_OK
;
583 return content::MEDIA_DEVICE_PERMISSION_DENIED
;
587 // Test all combinations of cam/mic content settings. Then tests the result of
588 // clicking both accept/deny on the infobar. Both cam/mic are requested.
589 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest
, ContentSettings
) {
590 static const ContentSettingsTestData tests
[] = {
591 // Settings that won't result in an infobar.
592 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ALLOW
, false},
593 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_BLOCK
, false},
594 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ALLOW
, false},
595 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_BLOCK
, false},
597 // Settings that will result in an infobar. Test both accept and deny.
598 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ASK
, false},
599 {CONTENT_SETTING_ALLOW
, CONTENT_SETTING_ASK
, true},
601 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
, false},
602 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ASK
, true},
604 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ASK
, false},
605 {CONTENT_SETTING_BLOCK
, CONTENT_SETTING_ASK
, true},
607 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ALLOW
, false},
608 {CONTENT_SETTING_ASK
, CONTENT_SETTING_ALLOW
, true},
610 {CONTENT_SETTING_ASK
, CONTENT_SETTING_BLOCK
, false},
611 {CONTENT_SETTING_ASK
, CONTENT_SETTING_BLOCK
, true},
614 for (auto& test
: tests
) {
615 SetContentSettings(test
.mic
, test
.cam
);
616 MediaStreamDevicesController
controller(
617 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
618 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse
,
621 // Check that the infobar is requesting the expected cam/mic values.
622 ASSERT_EQ(test
.ExpectMicInfobar(), controller
.IsAskingForAudio());
623 ASSERT_EQ(test
.ExpectCamInfobar(), controller
.IsAskingForVideo());
625 // Accept or deny the infobar if it's showing.
626 if (test
.ExpectMicInfobar() || test
.ExpectCamInfobar()) {
627 if (test
.accept_infobar
)
628 controller
.PermissionGranted();
630 controller
.PermissionDenied();
633 // Check the media stream result is expected and the devices returned are
635 ASSERT_EQ(test
.ExpectedMediaStreamResult(), media_stream_result());
637 DevicesContains(test
.ExpectMicAllowed(), test
.ExpectCamAllowed()));
641 // Request and allow camera access on WebUI pages without prompting.
642 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerWebUITest
,
643 RequestAndAllowCam
) {
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));