Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / media / media_stream_devices_controller_browsertest.cc
blobb4c217125954abeae7eba88eaf6acd5491bae7a0
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 <string>
7 #include "base/bind.h"
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"
24 namespace {
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();
39 } // namespace
41 class MediaStreamDevicesControllerTest : public WebRtcTestBase {
42 public:
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;
56 protected:
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;
82 break;
83 case DEVICE_TYPE_VIDEO:
84 policy_name = prefs::kVideoCaptureAllowed;
85 break;
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)
113 has_mic = true;
114 if (device.type == content::MEDIA_DEVICE_VIDEO_CAPTURE)
115 has_cam = true;
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 CreateRequestWithType(
128 const std::string& audio_id,
129 const std::string& video_id,
130 content::MediaStreamRequestType request_type) {
131 content::MediaStreamType audio_type =
132 audio_id.empty() ? content::MEDIA_NO_SERVICE
133 : content::MEDIA_DEVICE_AUDIO_CAPTURE;
134 content::MediaStreamType video_type =
135 video_id.empty() ? content::MEDIA_NO_SERVICE
136 : content::MEDIA_DEVICE_VIDEO_CAPTURE;
137 return content::MediaStreamRequest(0, 0, 0, example_url(), false,
138 request_type, audio_id, video_id,
139 audio_type, video_type);
142 content::MediaStreamRequest CreateRequest(const std::string& audio_id,
143 const std::string& video_id) {
144 return CreateRequestWithType(audio_id, video_id,
145 content::MEDIA_DEVICE_ACCESS);
148 void InitWithUrl(const GURL& url) {
149 DCHECK(example_url_.is_empty());
150 example_url_ = url;
151 ui_test_utils::NavigateToURL(browser(), example_url_);
152 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED,
153 GetContentSettings()->GetMicrophoneCameraState());
156 private:
157 void SetUpOnMainThread() override {
158 WebRtcTestBase::SetUpOnMainThread();
160 // Cleanup.
161 media_stream_devices_.clear();
162 media_stream_result_ = content::NUM_MEDIA_REQUEST_RESULTS;
164 content::MediaStreamDevices audio_devices;
165 content::MediaStreamDevice fake_audio_device(
166 content::MEDIA_DEVICE_AUDIO_CAPTURE, example_audio_id_,
167 "Fake Audio Device");
168 audio_devices.push_back(fake_audio_device);
169 MediaCaptureDevicesDispatcher::GetInstance()->SetTestAudioCaptureDevices(
170 audio_devices);
172 content::MediaStreamDevices video_devices;
173 content::MediaStreamDevice fake_video_device(
174 content::MEDIA_DEVICE_VIDEO_CAPTURE, example_video_id_,
175 "Fake Video Device");
176 video_devices.push_back(fake_video_device);
177 MediaCaptureDevicesDispatcher::GetInstance()->SetTestVideoCaptureDevices(
178 video_devices);
181 GURL example_url_;
182 const std::string example_audio_id_;
183 const std::string example_video_id_;
185 content::MediaStreamDevices media_stream_devices_;
186 content::MediaStreamRequestResult media_stream_result_;
189 // Request and allow microphone access.
190 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowMic) {
191 InitWithUrl(GURL("https://www.example.com"));
192 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
193 MediaStreamDevicesController controller(
194 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
195 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
196 this));
197 NotifyTabSpecificContentSettings(&controller);
199 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
200 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
201 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
202 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
203 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED,
204 GetContentSettings()->GetMicrophoneCameraState());
205 EXPECT_EQ(example_audio_id(),
206 GetContentSettings()->media_stream_requested_audio_device());
207 EXPECT_EQ(example_audio_id(),
208 GetContentSettings()->media_stream_selected_audio_device());
209 EXPECT_EQ(std::string(),
210 GetContentSettings()->media_stream_requested_video_device());
211 EXPECT_EQ(std::string(),
212 GetContentSettings()->media_stream_selected_video_device());
215 // Request and allow camera access.
216 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowCam) {
217 InitWithUrl(GURL("https://www.example.com"));
218 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
219 MediaStreamDevicesController controller(
220 GetWebContents(), CreateRequest(std::string(), example_video_id()),
221 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
222 this));
223 NotifyTabSpecificContentSettings(&controller);
225 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
226 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
227 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
228 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
229 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
230 GetContentSettings()->GetMicrophoneCameraState());
231 EXPECT_EQ(std::string(),
232 GetContentSettings()->media_stream_requested_audio_device());
233 EXPECT_EQ(std::string(),
234 GetContentSettings()->media_stream_selected_audio_device());
235 EXPECT_EQ(example_video_id(),
236 GetContentSettings()->media_stream_requested_video_device());
237 EXPECT_EQ(example_video_id(),
238 GetContentSettings()->media_stream_selected_video_device());
241 // Request and block microphone access.
242 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockMic) {
243 InitWithUrl(GURL("https://www.example.com"));
244 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
245 MediaStreamDevicesController controller(
246 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
247 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
248 this));
249 NotifyTabSpecificContentSettings(&controller);
251 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
252 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
253 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
254 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
255 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
256 TabSpecificContentSettings::MICROPHONE_BLOCKED,
257 GetContentSettings()->GetMicrophoneCameraState());
258 EXPECT_EQ(example_audio_id(),
259 GetContentSettings()->media_stream_requested_audio_device());
260 EXPECT_EQ(example_audio_id(),
261 GetContentSettings()->media_stream_selected_audio_device());
262 EXPECT_EQ(std::string(),
263 GetContentSettings()->media_stream_requested_video_device());
264 EXPECT_EQ(std::string(),
265 GetContentSettings()->media_stream_selected_video_device());
268 // Request and block camera access.
269 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockCam) {
270 InitWithUrl(GURL("https://www.example.com"));
271 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
272 MediaStreamDevicesController controller(
273 GetWebContents(), CreateRequest(std::string(), example_video_id()),
274 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
275 this));
276 NotifyTabSpecificContentSettings(&controller);
278 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
279 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
280 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
281 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
282 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED |
283 TabSpecificContentSettings::CAMERA_BLOCKED,
284 GetContentSettings()->GetMicrophoneCameraState());
285 EXPECT_EQ(std::string(),
286 GetContentSettings()->media_stream_requested_audio_device());
287 EXPECT_EQ(std::string(),
288 GetContentSettings()->media_stream_selected_audio_device());
289 EXPECT_EQ(example_video_id(),
290 GetContentSettings()->media_stream_requested_video_device());
291 EXPECT_EQ(example_video_id(),
292 GetContentSettings()->media_stream_selected_video_device());
295 // Request and allow microphone and camera access.
296 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
297 RequestAndAllowMicCam) {
298 InitWithUrl(GURL("https://www.example.com"));
299 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
300 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
301 MediaStreamDevicesController controller(
302 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
303 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
304 this));
305 NotifyTabSpecificContentSettings(&controller);
307 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
308 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
309 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
310 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
311 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
312 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
313 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
314 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
315 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
316 TabSpecificContentSettings::CAMERA_ACCESSED,
317 GetContentSettings()->GetMicrophoneCameraState());
318 EXPECT_EQ(example_audio_id(),
319 GetContentSettings()->media_stream_requested_audio_device());
320 EXPECT_EQ(example_audio_id(),
321 GetContentSettings()->media_stream_selected_audio_device());
322 EXPECT_EQ(example_video_id(),
323 GetContentSettings()->media_stream_requested_video_device());
324 EXPECT_EQ(example_video_id(),
325 GetContentSettings()->media_stream_selected_video_device());
328 // Request and block microphone and camera access.
329 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
330 RequestAndBlockMicCam) {
331 InitWithUrl(GURL("https://www.example.com"));
332 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
333 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
334 MediaStreamDevicesController controller(
335 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
336 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
337 this));
338 NotifyTabSpecificContentSettings(&controller);
340 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
341 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
342 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
343 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
344 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
345 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
346 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
347 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
348 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
349 TabSpecificContentSettings::MICROPHONE_BLOCKED |
350 TabSpecificContentSettings::CAMERA_ACCESSED |
351 TabSpecificContentSettings::CAMERA_BLOCKED,
352 GetContentSettings()->GetMicrophoneCameraState());
353 EXPECT_EQ(example_audio_id(),
354 GetContentSettings()->media_stream_requested_audio_device());
355 EXPECT_EQ(example_audio_id(),
356 GetContentSettings()->media_stream_selected_audio_device());
357 EXPECT_EQ(example_video_id(),
358 GetContentSettings()->media_stream_requested_video_device());
359 EXPECT_EQ(example_video_id(),
360 GetContentSettings()->media_stream_selected_video_device());
363 // Request microphone and camera access. Allow microphone, block camera.
364 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
365 RequestMicCamBlockCam) {
366 InitWithUrl(GURL("https://www.example.com"));
367 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
368 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
369 MediaStreamDevicesController controller(
370 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
371 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
372 this));
373 NotifyTabSpecificContentSettings(&controller);
375 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
376 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
377 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
378 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
379 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
380 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
381 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
382 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
383 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
384 TabSpecificContentSettings::CAMERA_ACCESSED |
385 TabSpecificContentSettings::CAMERA_BLOCKED,
386 GetContentSettings()->GetMicrophoneCameraState());
387 EXPECT_EQ(example_audio_id(),
388 GetContentSettings()->media_stream_requested_audio_device());
389 EXPECT_EQ(example_audio_id(),
390 GetContentSettings()->media_stream_selected_audio_device());
391 EXPECT_EQ(example_video_id(),
392 GetContentSettings()->media_stream_requested_video_device());
393 EXPECT_EQ(example_video_id(),
394 GetContentSettings()->media_stream_selected_video_device());
397 // Request microphone and camera access. Block microphone, allow camera.
398 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
399 RequestMicCamBlockMic) {
400 InitWithUrl(GURL("https://www.example.com"));
401 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
402 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
403 MediaStreamDevicesController controller(
404 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
405 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
406 this));
407 NotifyTabSpecificContentSettings(&controller);
409 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
410 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
411 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
412 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
413 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
414 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
415 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
416 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
417 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
418 TabSpecificContentSettings::MICROPHONE_BLOCKED |
419 TabSpecificContentSettings::CAMERA_ACCESSED,
420 GetContentSettings()->GetMicrophoneCameraState());
421 EXPECT_EQ(example_audio_id(),
422 GetContentSettings()->media_stream_requested_audio_device());
423 EXPECT_EQ(example_audio_id(),
424 GetContentSettings()->media_stream_selected_audio_device());
425 EXPECT_EQ(example_video_id(),
426 GetContentSettings()->media_stream_requested_video_device());
427 EXPECT_EQ(example_video_id(),
428 GetContentSettings()->media_stream_selected_video_device());
431 // Request microphone access. Requesting camera should not change microphone
432 // state.
433 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
434 RequestCamDoesNotChangeMic) {
435 InitWithUrl(GURL("https://www.example.com"));
436 // Request mic and deny.
437 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
438 MediaStreamDevicesController mic_controller(
439 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
440 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
441 this));
442 NotifyTabSpecificContentSettings(&mic_controller);
443 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
444 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
445 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
446 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
447 EXPECT_EQ(example_audio_id(),
448 GetContentSettings()->media_stream_requested_audio_device());
449 EXPECT_EQ(example_audio_id(),
450 GetContentSettings()->media_stream_selected_audio_device());
452 // Request cam and allow
453 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
454 MediaStreamDevicesController cam_controller(
455 GetWebContents(), CreateRequest(std::string(), example_video_id()),
456 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
457 this));
458 NotifyTabSpecificContentSettings(&cam_controller);
459 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
460 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
461 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
462 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
463 EXPECT_EQ(example_video_id(),
464 GetContentSettings()->media_stream_requested_video_device());
465 EXPECT_EQ(example_video_id(),
466 GetContentSettings()->media_stream_selected_video_device());
468 // Mic state should not have changed.
469 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
470 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
471 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
472 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
473 EXPECT_EQ(example_audio_id(),
474 GetContentSettings()->media_stream_requested_audio_device());
475 EXPECT_EQ(example_audio_id(),
476 GetContentSettings()->media_stream_selected_audio_device());
479 // Denying mic access after camera access should still show the camera as state.
480 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
481 DenyMicDoesNotChangeCam) {
482 InitWithUrl(GURL("https://www.example.com"));
483 // Request cam and allow
484 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
485 MediaStreamDevicesController cam_controller(
486 GetWebContents(), CreateRequest(std::string(), example_video_id()),
487 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
488 this));
489 NotifyTabSpecificContentSettings(&cam_controller);
490 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
491 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
492 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
493 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
494 EXPECT_EQ(example_video_id(),
495 GetContentSettings()->media_stream_requested_video_device());
496 EXPECT_EQ(example_video_id(),
497 GetContentSettings()->media_stream_selected_video_device());
498 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
499 GetContentSettings()->GetMicrophoneCameraState());
501 // Simulate that an a video stream is now being captured.
502 content::MediaStreamDevice fake_video_device(
503 content::MEDIA_DEVICE_VIDEO_CAPTURE, example_video_id(),
504 example_video_id());
505 content::MediaStreamDevices video_devices(1, fake_video_device);
506 MediaCaptureDevicesDispatcher* dispatcher =
507 MediaCaptureDevicesDispatcher::GetInstance();
508 dispatcher->SetTestVideoCaptureDevices(video_devices);
509 scoped_ptr<content::MediaStreamUI> video_stream_ui =
510 dispatcher->GetMediaStreamCaptureIndicator()->
511 RegisterMediaStream(GetWebContents(), video_devices);
512 video_stream_ui->OnStarted(base::Closure());
514 // Request mic and deny.
515 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
516 MediaStreamDevicesController mic_controller(
517 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
518 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
519 this));
520 NotifyTabSpecificContentSettings(&mic_controller);
521 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
522 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
523 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
524 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
525 EXPECT_EQ(example_audio_id(),
526 GetContentSettings()->media_stream_requested_audio_device());
527 EXPECT_EQ(example_audio_id(),
528 GetContentSettings()->media_stream_selected_audio_device());
530 // Cam should still be included in the state.
531 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
532 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
533 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
534 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
535 EXPECT_EQ(example_video_id(),
536 GetContentSettings()->media_stream_requested_video_device());
537 EXPECT_EQ(example_video_id(),
538 GetContentSettings()->media_stream_selected_video_device());
539 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
540 TabSpecificContentSettings::MICROPHONE_BLOCKED |
541 TabSpecificContentSettings::CAMERA_ACCESSED,
542 GetContentSettings()->GetMicrophoneCameraState());
544 // After ending the camera capture, the camera permission is no longer
545 // relevant, so it should no be included in the mic/cam state.
546 video_stream_ui.reset();
547 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
548 TabSpecificContentSettings::MICROPHONE_BLOCKED,
549 GetContentSettings()->GetMicrophoneCameraState());
552 // Stores the ContentSettings inputs for a particular test and has functions
553 // which return the expected outputs for that test.
554 struct ContentSettingsTestData {
555 // The initial value of the mic/cam content settings.
556 ContentSetting mic;
557 ContentSetting cam;
558 // Whether the infobar should be accepted if it's shown.
559 bool accept_infobar;
561 // Whether the infobar should be displayed to request mic/cam for the given
562 // content settings inputs.
563 bool ExpectMicInfobar() const { return mic == CONTENT_SETTING_ASK; }
564 bool ExpectCamInfobar() const { return cam == CONTENT_SETTING_ASK; }
566 // Whether or not the mic/cam should be allowed after clicking accept/deny for
567 // the given inputs.
568 bool ExpectMicAllowed() const {
569 return mic == CONTENT_SETTING_ALLOW ||
570 (mic == CONTENT_SETTING_ASK && accept_infobar);
572 bool ExpectCamAllowed() const {
573 return cam == CONTENT_SETTING_ALLOW ||
574 (cam == CONTENT_SETTING_ASK && accept_infobar);
577 // The expected media stream result after clicking accept/deny for the given
578 // inputs.
579 content::MediaStreamRequestResult ExpectedMediaStreamResult() const {
580 if (ExpectMicAllowed() || ExpectCamAllowed())
581 return content::MEDIA_DEVICE_OK;
582 return content::MEDIA_DEVICE_PERMISSION_DENIED;
586 // Test all combinations of cam/mic content settings. Then tests the result of
587 // clicking both accept/deny on the infobar. Both cam/mic are requested.
588 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, ContentSettings) {
589 InitWithUrl(GURL("https://www.example.com"));
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,
619 this));
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();
629 else
630 controller.PermissionDenied();
633 // Check the media stream result is expected and the devices returned are
634 // expected;
635 ASSERT_EQ(test.ExpectedMediaStreamResult(), media_stream_result());
636 ASSERT_TRUE(
637 DevicesContains(test.ExpectMicAllowed(), test.ExpectCamAllowed()));
641 // Request and allow camera access on WebUI pages without prompting.
642 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
643 WebUIRequestAndAllowCam) {
644 InitWithUrl(GURL("chrome://test-page"));
645 MediaStreamDevicesController controller(
646 GetWebContents(), CreateRequest(std::string(), example_video_id()),
647 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
648 this));
650 ASSERT_FALSE(controller.IsAskingForAudio());
651 ASSERT_FALSE(controller.IsAskingForVideo());
653 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
654 ASSERT_TRUE(DevicesContains(false, true));
657 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
658 ExtensionRequestMicCam) {
659 InitWithUrl(GURL("chrome-extension://test-page"));
660 // Test that a prompt is required.
661 MediaStreamDevicesController controller(
662 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
663 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
664 this));
665 ASSERT_TRUE(controller.IsAskingForAudio());
666 ASSERT_TRUE(controller.IsAskingForVideo());
668 // Accept the prompt.
669 controller.PermissionGranted();
670 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
671 ASSERT_TRUE(DevicesContains(true, true));
673 // Check that re-requesting allows without prompting.
674 MediaStreamDevicesController controller2(
675 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
676 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
677 this));
678 ASSERT_FALSE(controller2.IsAskingForAudio());
679 ASSERT_FALSE(controller2.IsAskingForVideo());
681 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
682 ASSERT_TRUE(DevicesContains(true, true));
685 // For Pepper request from insecure origin, even if it's ALLOW, it won't be
686 // changed to ASK.
687 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
688 PepperRequestInsecure) {
689 InitWithUrl(GURL("http://www.example.com"));
690 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW);
692 MediaStreamDevicesController controller(
693 GetWebContents(), CreateRequestWithType(example_audio_id(), std::string(),
694 content::MEDIA_OPEN_DEVICE),
695 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
696 this));
697 ASSERT_FALSE(controller.IsAskingForAudio());
698 ASSERT_FALSE(controller.IsAskingForVideo());
701 // For non-Pepper request from insecure origin, if it's ALLOW, it will be
702 // changed to ASK.
703 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
704 NonPepperRequestInsecure) {
705 InitWithUrl(GURL("http://www.example.com"));
706 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW);
708 MediaStreamDevicesController controller(
709 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
710 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
711 this));
712 ASSERT_TRUE(controller.IsAskingForAudio());
713 ASSERT_TRUE(controller.IsAskingForVideo());