OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 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_stream_device_permissions.h" |
| 11 #include "chrome/browser/media/media_stream_devices_controller.h" |
| 12 #include "chrome/browser/media/webrtc_browsertest_base.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/common/media_stream_request.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Causes the controller to update the TabSpecificContentSettings associated |
| 24 // with the same WebContents with the current permissions. This should be the |
| 25 // last change made to the controller in the test. |
| 26 void NotifyTabSpecificContentSettings( |
| 27 MediaStreamDevicesController* controller) { |
| 28 // Note that calling Deny() would have the same effect of passing the current |
| 29 // permissions state to the TabSpecificContentSettings. Deny() and Accept() |
| 30 // differ in their effect on the controller itself, but that is not important |
| 31 // in the tests calling this. |
| 32 controller->Accept(false); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class MediaStreamDevicesControllerTest : public WebRtcTestBase { |
| 38 public: |
| 39 MediaStreamDevicesControllerTest() |
| 40 : example_url_("about:blank"), |
| 41 example_audio_id_("example audio ID"), |
| 42 example_video_id_("example video ID") {} |
| 43 |
| 44 protected: |
| 45 enum DeviceType { DEVICE_TYPE_AUDIO, DEVICE_TYPE_VIDEO }; |
| 46 enum Access { ACCESS_ALLOWED, ACCESS_DENIED }; |
| 47 |
| 48 const GURL& example_url() const { return example_url_; } |
| 49 |
| 50 TabSpecificContentSettings* GetContentSettings() { |
| 51 return TabSpecificContentSettings::FromWebContents(GetWebContents()); |
| 52 } |
| 53 |
| 54 const std::string& example_audio_id() const { return example_audio_id_; } |
| 55 const std::string& example_video_id() const { return example_video_id_; } |
| 56 |
| 57 // Sets the device policy-controlled |access| for |example_url_| to be for the |
| 58 // selected |device_type|. |
| 59 void SetDevicePolicy(DeviceType device_type, Access access) { |
| 60 PrefService* prefs = Profile::FromBrowserContext( |
| 61 GetWebContents()->GetBrowserContext())->GetPrefs(); |
| 62 const char* policy_name = NULL; |
| 63 switch (device_type) { |
| 64 case DEVICE_TYPE_AUDIO: |
| 65 policy_name = prefs::kAudioCaptureAllowed; |
| 66 break; |
| 67 case DEVICE_TYPE_VIDEO: |
| 68 policy_name = prefs::kVideoCaptureAllowed; |
| 69 break; |
| 70 } |
| 71 prefs->SetBoolean(policy_name, access == ACCESS_ALLOWED); |
| 72 } |
| 73 |
| 74 content::WebContents* GetWebContents() { |
| 75 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 76 } |
| 77 |
| 78 // Creates a MediaStreamRequest, asking for those media types, which have a |
| 79 // non-empty id string. |
| 80 content::MediaStreamRequest CreateRequest(const std::string& audio_id, |
| 81 const std::string& video_id) { |
| 82 content::MediaStreamType audio_type = |
| 83 audio_id.empty() ? content::MEDIA_NO_SERVICE |
| 84 : content::MEDIA_DEVICE_AUDIO_CAPTURE; |
| 85 content::MediaStreamType video_type = |
| 86 video_id.empty() ? content::MEDIA_NO_SERVICE |
| 87 : content::MEDIA_DEVICE_VIDEO_CAPTURE; |
| 88 return content::MediaStreamRequest(0, |
| 89 0, |
| 90 0, |
| 91 example_url(), |
| 92 false, |
| 93 content::MEDIA_DEVICE_ACCESS, |
| 94 audio_id, |
| 95 video_id, |
| 96 audio_type, |
| 97 video_type); |
| 98 } |
| 99 |
| 100 // Dummy callback for when we deny the current request directly. |
| 101 static void OnMediaStreamResponse(const content::MediaStreamDevices& devices, |
| 102 content::MediaStreamRequestResult result, |
| 103 scoped_ptr<content::MediaStreamUI> ui) {} |
| 104 |
| 105 private: |
| 106 void SetUpOnMainThread() override { |
| 107 WebRtcTestBase::SetUpOnMainThread(); |
| 108 ui_test_utils::NavigateToURL(browser(), example_url_); |
| 109 |
| 110 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED, |
| 111 GetContentSettings()->GetMicrophoneCameraState()); |
| 112 } |
| 113 |
| 114 const GURL example_url_; |
| 115 const std::string example_audio_id_; |
| 116 const std::string example_video_id_; |
| 117 }; |
| 118 |
| 119 // Request and allow microphone access. |
| 120 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowMic) { |
| 121 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED); |
| 122 MediaStreamDevicesController controller( |
| 123 GetWebContents(), |
| 124 CreateRequest(example_audio_id(), std::string()), |
| 125 base::Bind(&OnMediaStreamResponse)); |
| 126 NotifyTabSpecificContentSettings(&controller); |
| 127 |
| 128 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 129 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 130 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 131 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 132 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED, |
| 133 GetContentSettings()->GetMicrophoneCameraState()); |
| 134 EXPECT_EQ(example_audio_id(), |
| 135 GetContentSettings()->media_stream_requested_audio_device()); |
| 136 EXPECT_EQ(example_audio_id(), |
| 137 GetContentSettings()->media_stream_selected_audio_device()); |
| 138 EXPECT_EQ(std::string(), |
| 139 GetContentSettings()->media_stream_requested_video_device()); |
| 140 EXPECT_EQ(std::string(), |
| 141 GetContentSettings()->media_stream_selected_video_device()); |
| 142 } |
| 143 |
| 144 // Request and allow camera access. |
| 145 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndAllowCam) { |
| 146 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED); |
| 147 MediaStreamDevicesController controller( |
| 148 GetWebContents(), |
| 149 CreateRequest(std::string(), example_video_id()), |
| 150 base::Bind(&OnMediaStreamResponse)); |
| 151 NotifyTabSpecificContentSettings(&controller); |
| 152 |
| 153 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 154 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 155 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 156 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 157 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED, |
| 158 GetContentSettings()->GetMicrophoneCameraState()); |
| 159 EXPECT_EQ(std::string(), |
| 160 GetContentSettings()->media_stream_requested_audio_device()); |
| 161 EXPECT_EQ(std::string(), |
| 162 GetContentSettings()->media_stream_selected_audio_device()); |
| 163 EXPECT_EQ(example_video_id(), |
| 164 GetContentSettings()->media_stream_requested_video_device()); |
| 165 EXPECT_EQ(example_video_id(), |
| 166 GetContentSettings()->media_stream_selected_video_device()); |
| 167 } |
| 168 |
| 169 // Request and block microphone access. |
| 170 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockMic) { |
| 171 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED); |
| 172 MediaStreamDevicesController controller( |
| 173 GetWebContents(), |
| 174 CreateRequest(example_audio_id(), std::string()), |
| 175 base::Bind(&OnMediaStreamResponse)); |
| 176 NotifyTabSpecificContentSettings(&controller); |
| 177 |
| 178 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 179 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 180 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 181 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 182 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 183 TabSpecificContentSettings::MICROPHONE_BLOCKED, |
| 184 GetContentSettings()->GetMicrophoneCameraState()); |
| 185 EXPECT_EQ(example_audio_id(), |
| 186 GetContentSettings()->media_stream_requested_audio_device()); |
| 187 EXPECT_EQ(example_audio_id(), |
| 188 GetContentSettings()->media_stream_selected_audio_device()); |
| 189 EXPECT_EQ(std::string(), |
| 190 GetContentSettings()->media_stream_requested_video_device()); |
| 191 EXPECT_EQ(std::string(), |
| 192 GetContentSettings()->media_stream_selected_video_device()); |
| 193 } |
| 194 |
| 195 // Request and block camera access. |
| 196 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, RequestAndBlockCam) { |
| 197 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED); |
| 198 MediaStreamDevicesController controller( |
| 199 GetWebContents(), |
| 200 CreateRequest(std::string(), example_video_id()), |
| 201 base::Bind(&OnMediaStreamResponse)); |
| 202 NotifyTabSpecificContentSettings(&controller); |
| 203 |
| 204 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 205 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 206 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 207 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 208 EXPECT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED | |
| 209 TabSpecificContentSettings::CAMERA_BLOCKED, |
| 210 GetContentSettings()->GetMicrophoneCameraState()); |
| 211 EXPECT_EQ(std::string(), |
| 212 GetContentSettings()->media_stream_requested_audio_device()); |
| 213 EXPECT_EQ(std::string(), |
| 214 GetContentSettings()->media_stream_selected_audio_device()); |
| 215 EXPECT_EQ(example_video_id(), |
| 216 GetContentSettings()->media_stream_requested_video_device()); |
| 217 EXPECT_EQ(example_video_id(), |
| 218 GetContentSettings()->media_stream_selected_video_device()); |
| 219 } |
| 220 |
| 221 // Request and allow microphone and camera access. |
| 222 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 223 RequestAndAllowMicCam) { |
| 224 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED); |
| 225 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED); |
| 226 MediaStreamDevicesController controller( |
| 227 GetWebContents(), |
| 228 CreateRequest(example_audio_id(), example_video_id()), |
| 229 base::Bind(&OnMediaStreamResponse)); |
| 230 NotifyTabSpecificContentSettings(&controller); |
| 231 |
| 232 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 233 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 234 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 235 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 236 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 237 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 238 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 239 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 240 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 241 TabSpecificContentSettings::CAMERA_ACCESSED, |
| 242 GetContentSettings()->GetMicrophoneCameraState()); |
| 243 EXPECT_EQ(example_audio_id(), |
| 244 GetContentSettings()->media_stream_requested_audio_device()); |
| 245 EXPECT_EQ(example_audio_id(), |
| 246 GetContentSettings()->media_stream_selected_audio_device()); |
| 247 EXPECT_EQ(example_video_id(), |
| 248 GetContentSettings()->media_stream_requested_video_device()); |
| 249 EXPECT_EQ(example_video_id(), |
| 250 GetContentSettings()->media_stream_selected_video_device()); |
| 251 } |
| 252 |
| 253 // Request and block microphone and camera access. |
| 254 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 255 RequestAndBlockMicCam) { |
| 256 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED); |
| 257 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED); |
| 258 MediaStreamDevicesController controller( |
| 259 GetWebContents(), |
| 260 CreateRequest(example_audio_id(), example_video_id()), |
| 261 base::Bind(&OnMediaStreamResponse)); |
| 262 NotifyTabSpecificContentSettings(&controller); |
| 263 |
| 264 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 265 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 266 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 267 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 268 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 269 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 270 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 271 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 272 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 273 TabSpecificContentSettings::MICROPHONE_BLOCKED | |
| 274 TabSpecificContentSettings::CAMERA_ACCESSED | |
| 275 TabSpecificContentSettings::CAMERA_BLOCKED, |
| 276 GetContentSettings()->GetMicrophoneCameraState()); |
| 277 EXPECT_EQ(example_audio_id(), |
| 278 GetContentSettings()->media_stream_requested_audio_device()); |
| 279 EXPECT_EQ(example_audio_id(), |
| 280 GetContentSettings()->media_stream_selected_audio_device()); |
| 281 EXPECT_EQ(example_video_id(), |
| 282 GetContentSettings()->media_stream_requested_video_device()); |
| 283 EXPECT_EQ(example_video_id(), |
| 284 GetContentSettings()->media_stream_selected_video_device()); |
| 285 } |
| 286 |
| 287 // Request microphone and camera access. Allow microphone, block camera. |
| 288 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 289 RequestMicCamBlockCam) { |
| 290 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_ALLOWED); |
| 291 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_DENIED); |
| 292 MediaStreamDevicesController controller( |
| 293 GetWebContents(), |
| 294 CreateRequest(example_audio_id(), example_video_id()), |
| 295 base::Bind(&OnMediaStreamResponse)); |
| 296 NotifyTabSpecificContentSettings(&controller); |
| 297 |
| 298 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 299 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 300 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 301 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 302 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 303 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 304 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 305 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 306 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 307 TabSpecificContentSettings::CAMERA_ACCESSED | |
| 308 TabSpecificContentSettings::CAMERA_BLOCKED, |
| 309 GetContentSettings()->GetMicrophoneCameraState()); |
| 310 EXPECT_EQ(example_audio_id(), |
| 311 GetContentSettings()->media_stream_requested_audio_device()); |
| 312 EXPECT_EQ(example_audio_id(), |
| 313 GetContentSettings()->media_stream_selected_audio_device()); |
| 314 EXPECT_EQ(example_video_id(), |
| 315 GetContentSettings()->media_stream_requested_video_device()); |
| 316 EXPECT_EQ(example_video_id(), |
| 317 GetContentSettings()->media_stream_selected_video_device()); |
| 318 } |
| 319 |
| 320 // Request microphone and camera access. Block microphone, allow camera. |
| 321 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 322 RequestMicCamBlockMic) { |
| 323 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED); |
| 324 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED); |
| 325 MediaStreamDevicesController controller( |
| 326 GetWebContents(), |
| 327 CreateRequest(example_audio_id(), example_video_id()), |
| 328 base::Bind(&OnMediaStreamResponse)); |
| 329 NotifyTabSpecificContentSettings(&controller); |
| 330 |
| 331 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 332 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 333 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 334 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 335 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 336 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 337 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 338 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 339 EXPECT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 340 TabSpecificContentSettings::MICROPHONE_BLOCKED | |
| 341 TabSpecificContentSettings::CAMERA_ACCESSED, |
| 342 GetContentSettings()->GetMicrophoneCameraState()); |
| 343 EXPECT_EQ(example_audio_id(), |
| 344 GetContentSettings()->media_stream_requested_audio_device()); |
| 345 EXPECT_EQ(example_audio_id(), |
| 346 GetContentSettings()->media_stream_selected_audio_device()); |
| 347 EXPECT_EQ(example_video_id(), |
| 348 GetContentSettings()->media_stream_requested_video_device()); |
| 349 EXPECT_EQ(example_video_id(), |
| 350 GetContentSettings()->media_stream_selected_video_device()); |
| 351 } |
| 352 |
| 353 // Request microphone access. Requesting camera should not change microphone |
| 354 // state. |
| 355 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 356 RequestCamDoesNotChangeMic) { |
| 357 // Request mic and deny. |
| 358 SetDevicePolicy(DEVICE_TYPE_AUDIO, ACCESS_DENIED); |
| 359 MediaStreamDevicesController mic_controller( |
| 360 GetWebContents(), |
| 361 CreateRequest(example_audio_id(), std::string()), |
| 362 base::Bind(&OnMediaStreamResponse)); |
| 363 NotifyTabSpecificContentSettings(&mic_controller); |
| 364 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 365 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 366 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 367 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 368 EXPECT_EQ(example_audio_id(), |
| 369 GetContentSettings()->media_stream_requested_audio_device()); |
| 370 EXPECT_EQ(example_audio_id(), |
| 371 GetContentSettings()->media_stream_selected_audio_device()); |
| 372 |
| 373 // Request cam and allow |
| 374 SetDevicePolicy(DEVICE_TYPE_VIDEO, ACCESS_ALLOWED); |
| 375 MediaStreamDevicesController cam_controller( |
| 376 GetWebContents(), |
| 377 CreateRequest(std::string(), example_video_id()), |
| 378 base::Bind(&OnMediaStreamResponse)); |
| 379 NotifyTabSpecificContentSettings(&cam_controller); |
| 380 EXPECT_TRUE(GetContentSettings()->IsContentAllowed( |
| 381 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 382 EXPECT_FALSE(GetContentSettings()->IsContentBlocked( |
| 383 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)); |
| 384 EXPECT_EQ(example_video_id(), |
| 385 GetContentSettings()->media_stream_requested_video_device()); |
| 386 EXPECT_EQ(example_video_id(), |
| 387 GetContentSettings()->media_stream_selected_video_device()); |
| 388 |
| 389 // Mic state should not have changed. |
| 390 EXPECT_FALSE(GetContentSettings()->IsContentAllowed( |
| 391 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 392 EXPECT_TRUE(GetContentSettings()->IsContentBlocked( |
| 393 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)); |
| 394 EXPECT_EQ(example_audio_id(), |
| 395 GetContentSettings()->media_stream_requested_audio_device()); |
| 396 EXPECT_EQ(example_audio_id(), |
| 397 GetContentSettings()->media_stream_selected_audio_device()); |
| 398 } |
OLD | NEW |