Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / media / media_stream_devices_controller_browsertest.cc
blob32d5d4d9c5ad0a705522d9b903f83ceca85b53d9
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/host_content_settings_map_factory.h"
10 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
11 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
12 #include "chrome/browser/media/media_stream_capture_indicator.h"
13 #include "chrome/browser/media/media_stream_device_permissions.h"
14 #include "chrome/browser/media/media_stream_devices_controller.h"
15 #include "chrome/browser/media/webrtc_browsertest_base.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "content/public/common/media_stream_request.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace {
27 // Causes the controller to update the TabSpecificContentSettings associated
28 // with the same WebContents with the current permissions. This should be the
29 // last change made to the controller in the test.
30 void NotifyTabSpecificContentSettings(
31 MediaStreamDevicesController* controller) {
32 // Note that calling Deny() would have the same effect of passing the current
33 // permissions state to the TabSpecificContentSettings. Deny() and Accept()
34 // differ in their effect on the controller itself, but that is not important
35 // in the tests calling this.
36 if (controller->IsAskingForAudio() || controller->IsAskingForVideo())
37 controller->PermissionGranted();
40 } // namespace
42 class MediaStreamDevicesControllerTest : public WebRtcTestBase {
43 public:
44 MediaStreamDevicesControllerTest()
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;
57 protected:
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;
83 break;
84 case DEVICE_TYPE_VIDEO:
85 policy_name = prefs::kVideoCaptureAllowed;
86 break;
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 HostContentSettingsMapFactory::GetForProfile(
96 Profile::FromBrowserContext(GetWebContents()->GetBrowserContext()));
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)
114 has_mic = true;
115 if (device.type == content::MEDIA_DEVICE_VIDEO_CAPTURE)
116 has_cam = true;
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 CreateRequestWithType(
129 const std::string& audio_id,
130 const std::string& video_id,
131 content::MediaStreamRequestType request_type) {
132 content::MediaStreamType audio_type =
133 audio_id.empty() ? content::MEDIA_NO_SERVICE
134 : content::MEDIA_DEVICE_AUDIO_CAPTURE;
135 content::MediaStreamType video_type =
136 video_id.empty() ? content::MEDIA_NO_SERVICE
137 : content::MEDIA_DEVICE_VIDEO_CAPTURE;
138 return content::MediaStreamRequest(0, 0, 0, example_url(), false,
139 request_type, audio_id, video_id,
140 audio_type, video_type);
143 content::MediaStreamRequest CreateRequest(const std::string& audio_id,
144 const std::string& video_id) {
145 return CreateRequestWithType(audio_id, video_id,
146 content::MEDIA_DEVICE_ACCESS);
149 void InitWithUrl(const GURL& url) {
150 DCHECK(example_url_.is_empty());
151 example_url_ = url;
152 ui_test_utils::NavigateToURL(browser(), example_url_);
153 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED,
154 GetContentSettings()->GetMicrophoneCameraState());
157 private:
158 void SetUpOnMainThread() override {
159 WebRtcTestBase::SetUpOnMainThread();
161 // Cleanup.
162 media_stream_devices_.clear();
163 media_stream_result_ = content::NUM_MEDIA_REQUEST_RESULTS;
165 content::MediaStreamDevices audio_devices;
166 content::MediaStreamDevice fake_audio_device(
167 content::MEDIA_DEVICE_AUDIO_CAPTURE, example_audio_id_,
168 "Fake Audio Device");
169 audio_devices.push_back(fake_audio_device);
170 MediaCaptureDevicesDispatcher::GetInstance()->SetTestAudioCaptureDevices(
171 audio_devices);
173 content::MediaStreamDevices video_devices;
174 content::MediaStreamDevice fake_video_device(
175 content::MEDIA_DEVICE_VIDEO_CAPTURE, example_video_id_,
176 "Fake Video Device");
177 video_devices.push_back(fake_video_device);
178 MediaCaptureDevicesDispatcher::GetInstance()->SetTestVideoCaptureDevices(
179 video_devices);
182 GURL example_url_;
183 const std::string example_audio_id_;
184 const std::string example_video_id_;
186 content::MediaStreamDevices media_stream_devices_;
187 content::MediaStreamRequestResult media_stream_result_;
190 // Request and allow microphone access.
191 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowMic) {
192 InitWithUrl(GURL("https://www.example.com"));
193 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
194 MediaStreamDevicesController controller(
195 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
196 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
197 this));
198 NotifyTabSpecificContentSettings(&controller);
200 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
201 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
202 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
203 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
204 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED,
205 GetContentSettings()->GetMicrophoneCameraState());
206 EXPECT_EQ(example_audio_id(),
207 GetContentSettings()->media_stream_requested_audio_device());
208 EXPECT_EQ(example_audio_id(),
209 GetContentSettings()->media_stream_selected_audio_device());
210 EXPECT_EQ(std::string(),
211 GetContentSettings()->media_stream_requested_video_device());
212 EXPECT_EQ(std::string(),
213 GetContentSettings()->media_stream_selected_video_device());
216 // Request and allow camera access.
217 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowCam) {
218 InitWithUrl(GURL("https://www.example.com"));
219 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
220 MediaStreamDevicesController controller(
221 GetWebContents(), CreateRequest(std::string(), example_video_id()),
222 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
223 this));
224 NotifyTabSpecificContentSettings(&controller);
226 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
227 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
228 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
229 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
230 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
231 GetContentSettings()->GetMicrophoneCameraState());
232 EXPECT_EQ(std::string(),
233 GetContentSettings()->media_stream_requested_audio_device());
234 EXPECT_EQ(std::string(),
235 GetContentSettings()->media_stream_selected_audio_device());
236 EXPECT_EQ(example_video_id(),
237 GetContentSettings()->media_stream_requested_video_device());
238 EXPECT_EQ(example_video_id(),
239 GetContentSettings()->media_stream_selected_video_device());
242 // Request and block microphone access.
243 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockMic) {
244 InitWithUrl(GURL("https://www.example.com"));
245 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
246 MediaStreamDevicesController controller(
247 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
248 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
249 this));
250 NotifyTabSpecificContentSettings(&controller);
252 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
253 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
254 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
255 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
256 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
257 TabSpecificContentSettings::MICROPHONE_BLOCKED,
258 GetContentSettings()->GetMicrophoneCameraState());
259 EXPECT_EQ(example_audio_id(),
260 GetContentSettings()->media_stream_requested_audio_device());
261 EXPECT_EQ(example_audio_id(),
262 GetContentSettings()->media_stream_selected_audio_device());
263 EXPECT_EQ(std::string(),
264 GetContentSettings()->media_stream_requested_video_device());
265 EXPECT_EQ(std::string(),
266 GetContentSettings()->media_stream_selected_video_device());
269 // Request and block camera access.
270 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockCam) {
271 InitWithUrl(GURL("https://www.example.com"));
272 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
273 MediaStreamDevicesController controller(
274 GetWebContents(), CreateRequest(std::string(), example_video_id()),
275 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
276 this));
277 NotifyTabSpecificContentSettings(&controller);
279 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
280 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
281 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
282 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
283 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED |
284 TabSpecificContentSettings::CAMERA_BLOCKED,
285 GetContentSettings()->GetMicrophoneCameraState());
286 EXPECT_EQ(std::string(),
287 GetContentSettings()->media_stream_requested_audio_device());
288 EXPECT_EQ(std::string(),
289 GetContentSettings()->media_stream_selected_audio_device());
290 EXPECT_EQ(example_video_id(),
291 GetContentSettings()->media_stream_requested_video_device());
292 EXPECT_EQ(example_video_id(),
293 GetContentSettings()->media_stream_selected_video_device());
296 // Request and allow microphone and camera access.
297 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
298 RequestAndAllowMicCam) {
299 InitWithUrl(GURL("https://www.example.com"));
300 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
301 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
302 MediaStreamDevicesController controller(
303 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
304 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
305 this));
306 NotifyTabSpecificContentSettings(&controller);
308 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
309 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
310 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
311 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
312 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
313 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
314 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
315 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
316 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
317 TabSpecificContentSettings::CAMERA_ACCESSED,
318 GetContentSettings()->GetMicrophoneCameraState());
319 EXPECT_EQ(example_audio_id(),
320 GetContentSettings()->media_stream_requested_audio_device());
321 EXPECT_EQ(example_audio_id(),
322 GetContentSettings()->media_stream_selected_audio_device());
323 EXPECT_EQ(example_video_id(),
324 GetContentSettings()->media_stream_requested_video_device());
325 EXPECT_EQ(example_video_id(),
326 GetContentSettings()->media_stream_selected_video_device());
329 // Request and block microphone and camera access.
330 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
331 RequestAndBlockMicCam) {
332 InitWithUrl(GURL("https://www.example.com"));
333 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
334 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
335 MediaStreamDevicesController controller(
336 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
337 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
338 this));
339 NotifyTabSpecificContentSettings(&controller);
341 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
342 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
343 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
344 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
345 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
346 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
347 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
348 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
349 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
350 TabSpecificContentSettings::MICROPHONE_BLOCKED |
351 TabSpecificContentSettings::CAMERA_ACCESSED |
352 TabSpecificContentSettings::CAMERA_BLOCKED,
353 GetContentSettings()->GetMicrophoneCameraState());
354 EXPECT_EQ(example_audio_id(),
355 GetContentSettings()->media_stream_requested_audio_device());
356 EXPECT_EQ(example_audio_id(),
357 GetContentSettings()->media_stream_selected_audio_device());
358 EXPECT_EQ(example_video_id(),
359 GetContentSettings()->media_stream_requested_video_device());
360 EXPECT_EQ(example_video_id(),
361 GetContentSettings()->media_stream_selected_video_device());
364 // Request microphone and camera access. Allow microphone, block camera.
365 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
366 RequestMicCamBlockCam) {
367 InitWithUrl(GURL("https://www.example.com"));
368 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED);
369 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED);
370 MediaStreamDevicesController controller(
371 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
372 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
373 this));
374 NotifyTabSpecificContentSettings(&controller);
376 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
377 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
378 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
379 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
380 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
381 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
382 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
383 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
384 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
385 TabSpecificContentSettings::CAMERA_ACCESSED |
386 TabSpecificContentSettings::CAMERA_BLOCKED,
387 GetContentSettings()->GetMicrophoneCameraState());
388 EXPECT_EQ(example_audio_id(),
389 GetContentSettings()->media_stream_requested_audio_device());
390 EXPECT_EQ(example_audio_id(),
391 GetContentSettings()->media_stream_selected_audio_device());
392 EXPECT_EQ(example_video_id(),
393 GetContentSettings()->media_stream_requested_video_device());
394 EXPECT_EQ(example_video_id(),
395 GetContentSettings()->media_stream_selected_video_device());
398 // Request microphone and camera access. Block microphone, allow camera.
399 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
400 RequestMicCamBlockMic) {
401 InitWithUrl(GURL("https://www.example.com"));
402 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
403 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
404 MediaStreamDevicesController controller(
405 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
406 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
407 this));
408 NotifyTabSpecificContentSettings(&controller);
410 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
411 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
412 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
413 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
414 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
415 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
416 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
417 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
418 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
419 TabSpecificContentSettings::MICROPHONE_BLOCKED |
420 TabSpecificContentSettings::CAMERA_ACCESSED,
421 GetContentSettings()->GetMicrophoneCameraState());
422 EXPECT_EQ(example_audio_id(),
423 GetContentSettings()->media_stream_requested_audio_device());
424 EXPECT_EQ(example_audio_id(),
425 GetContentSettings()->media_stream_selected_audio_device());
426 EXPECT_EQ(example_video_id(),
427 GetContentSettings()->media_stream_requested_video_device());
428 EXPECT_EQ(example_video_id(),
429 GetContentSettings()->media_stream_selected_video_device());
432 // Request microphone access. Requesting camera should not change microphone
433 // state.
434 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
435 RequestCamDoesNotChangeMic) {
436 InitWithUrl(GURL("https://www.example.com"));
437 // Request mic and deny.
438 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED);
439 MediaStreamDevicesController mic_controller(
440 GetWebContents(), CreateRequest(example_audio_id(), std::string()),
441 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
442 this));
443 NotifyTabSpecificContentSettings(&mic_controller);
444 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
445 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
446 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
447 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
448 EXPECT_EQ(example_audio_id(),
449 GetContentSettings()->media_stream_requested_audio_device());
450 EXPECT_EQ(example_audio_id(),
451 GetContentSettings()->media_stream_selected_audio_device());
453 // Request cam and allow
454 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED);
455 MediaStreamDevicesController cam_controller(
456 GetWebContents(), CreateRequest(std::string(), example_video_id()),
457 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
458 this));
459 NotifyTabSpecificContentSettings(&cam_controller);
460 EXPECT_TRUE(GetContentSettings()->IsContentAllowed(
461 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
462 EXPECT_FALSE(GetContentSettings()->IsContentBlocked(
463 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
464 EXPECT_EQ(example_video_id(),
465 GetContentSettings()->media_stream_requested_video_device());
466 EXPECT_EQ(example_video_id(),
467 GetContentSettings()->media_stream_selected_video_device());
469 // Mic state should not have changed.
470 EXPECT_FALSE(GetContentSettings()->IsContentAllowed(
471 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
472 EXPECT_TRUE(GetContentSettings()->IsContentBlocked(
473 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
474 EXPECT_EQ(example_audio_id(),
475 GetContentSettings()->media_stream_requested_audio_device());
476 EXPECT_EQ(example_audio_id(),
477 GetContentSettings()->media_stream_selected_audio_device());
480 // Denying mic access after camera access should still show the camera as state.
481 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
482 DenyMicDoesNotChangeCam) {
483 InitWithUrl(GURL("https://www.example.com"));
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,
489 this));
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(),
505 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,
520 this));
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.
557 ContentSetting mic;
558 ContentSetting cam;
559 // Whether the infobar should be accepted if it's shown.
560 bool accept_infobar;
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
568 // the given inputs.
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
579 // inputs.
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 InitWithUrl(GURL("https://www.example.com"));
591 static const ContentSettingsTestData tests[] = {
592 // Settings that won't result in an infobar.
593 {CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW, false},
594 {CONTENT_SETTING_ALLOW, CONTENT_SETTING_BLOCK, false},
595 {CONTENT_SETTING_BLOCK, CONTENT_SETTING_ALLOW, false},
596 {CONTENT_SETTING_BLOCK, CONTENT_SETTING_BLOCK, false},
598 // Settings that will result in an infobar. Test both accept and deny.
599 {CONTENT_SETTING_ALLOW, CONTENT_SETTING_ASK, false},
600 {CONTENT_SETTING_ALLOW, CONTENT_SETTING_ASK, true},
602 {CONTENT_SETTING_ASK, CONTENT_SETTING_ASK, false},
603 {CONTENT_SETTING_ASK, CONTENT_SETTING_ASK, true},
605 {CONTENT_SETTING_BLOCK, CONTENT_SETTING_ASK, false},
606 {CONTENT_SETTING_BLOCK, CONTENT_SETTING_ASK, true},
608 {CONTENT_SETTING_ASK, CONTENT_SETTING_ALLOW, false},
609 {CONTENT_SETTING_ASK, CONTENT_SETTING_ALLOW, true},
611 {CONTENT_SETTING_ASK, CONTENT_SETTING_BLOCK, false},
612 {CONTENT_SETTING_ASK, CONTENT_SETTING_BLOCK, true},
615 for (auto& test : tests) {
616 SetContentSettings(test.mic, test.cam);
617 MediaStreamDevicesController controller(
618 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
619 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
620 this));
622 // Check that the infobar is requesting the expected cam/mic values.
623 ASSERT_EQ(test.ExpectMicInfobar(), controller.IsAskingForAudio());
624 ASSERT_EQ(test.ExpectCamInfobar(), controller.IsAskingForVideo());
626 // Accept or deny the infobar if it's showing.
627 if (test.ExpectMicInfobar() || test.ExpectCamInfobar()) {
628 if (test.accept_infobar)
629 controller.PermissionGranted();
630 else
631 controller.PermissionDenied();
634 // Check the media stream result is expected and the devices returned are
635 // expected;
636 ASSERT_EQ(test.ExpectedMediaStreamResult(), media_stream_result());
637 ASSERT_TRUE(
638 DevicesContains(test.ExpectMicAllowed(), test.ExpectCamAllowed()));
642 // Request and allow camera access on WebUI pages without prompting.
643 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
644 WebUIRequestAndAllowCam) {
645 InitWithUrl(GURL("chrome://test-page"));
646 MediaStreamDevicesController controller(
647 GetWebContents(), CreateRequest(std::string(), example_video_id()),
648 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
649 this));
651 ASSERT_FALSE(controller.IsAskingForAudio());
652 ASSERT_FALSE(controller.IsAskingForVideo());
654 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
655 ASSERT_TRUE(DevicesContains(false, true));
658 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
659 ExtensionRequestMicCam) {
660 InitWithUrl(GURL("chrome-extension://test-page"));
661 // Test that a prompt is required.
662 MediaStreamDevicesController controller(
663 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
664 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
665 this));
666 ASSERT_TRUE(controller.IsAskingForAudio());
667 ASSERT_TRUE(controller.IsAskingForVideo());
669 // Accept the prompt.
670 controller.PermissionGranted();
671 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
672 ASSERT_TRUE(DevicesContains(true, true));
674 // Check that re-requesting allows without prompting.
675 MediaStreamDevicesController controller2(
676 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
677 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
678 this));
679 ASSERT_FALSE(controller2.IsAskingForAudio());
680 ASSERT_FALSE(controller2.IsAskingForVideo());
682 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result());
683 ASSERT_TRUE(DevicesContains(true, true));
686 // For Pepper request from insecure origin, even if it's ALLOW, it won't be
687 // changed to ASK.
688 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
689 PepperRequestInsecure) {
690 InitWithUrl(GURL("http://www.example.com"));
691 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW);
693 MediaStreamDevicesController controller(
694 GetWebContents(), CreateRequestWithType(example_audio_id(), std::string(),
695 content::MEDIA_OPEN_DEVICE),
696 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
697 this));
698 ASSERT_FALSE(controller.IsAskingForAudio());
699 ASSERT_FALSE(controller.IsAskingForVideo());
702 // For non-Pepper request from insecure origin, if it's ALLOW, it will be
703 // changed to ASK.
704 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest,
705 NonPepperRequestInsecure) {
706 InitWithUrl(GURL("http://www.example.com"));
707 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW);
709 MediaStreamDevicesController controller(
710 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()),
711 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse,
712 this));
713 ASSERT_TRUE(controller.IsAskingForAudio());
714 ASSERT_TRUE(controller.IsAskingForVideo());