| 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 "base/command_line.h" | |
| 6 #include "base/json/json_reader.h" | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "chrome/browser/media/webrtc_browsertest_base.h" | |
| 9 #include "chrome/browser/media/webrtc_browsertest_common.h" | |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 13 #include "chrome/common/chrome_switches.h" | |
| 14 #include "chrome/test/base/in_process_browser_test.h" | |
| 15 #include "chrome/test/base/ui_test_utils.h" | |
| 16 #include "content/public/common/content_switches.h" | |
| 17 #include "content/public/test/browser_test_utils.h" | |
| 18 #include "media/audio/audio_manager.h" | |
| 19 #include "media/base/media_switches.h" | |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html"; | |
| 25 | |
| 26 const char kDeviceKindAudioInput[] = "audioinput"; | |
| 27 const char kDeviceKindVideoInput[] = "videoinput"; | |
| 28 const char kDeviceKindAudioOutput[] = "audiooutput"; | |
| 29 | |
| 30 const char kSourceKindAudioInput[] = "audio"; | |
| 31 const char kSourceKindVideoInput[] = "video"; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 // Integration test for WebRTC getMediaDevices. It always uses fake devices. | |
| 36 // It needs to be a browser test (and not content browser test) to be able to | |
| 37 // test that labels are cleared or not depending on if access to devices has | |
| 38 // been granted. | |
| 39 class WebRtcGetMediaDevicesBrowserTest | |
| 40 : public WebRtcTestBase, | |
| 41 public testing::WithParamInterface<bool> { | |
| 42 public: | |
| 43 WebRtcGetMediaDevicesBrowserTest() | |
| 44 : has_audio_output_devices_initialized_(false), | |
| 45 has_audio_output_devices_(false) {} | |
| 46 | |
| 47 void SetUpInProcessBrowserTestFixture() override { | |
| 48 DetectErrorsInJavaScript(); // Look for errors in our rather complex js. | |
| 49 } | |
| 50 | |
| 51 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 52 // Ensure the infobar is enabled, since we expect that in this test. | |
| 53 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)); | |
| 54 | |
| 55 // Always use fake devices. | |
| 56 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | |
| 57 } | |
| 58 | |
| 59 protected: | |
| 60 // This is used for media devices and sources. | |
| 61 struct MediaDeviceInfo { | |
| 62 std::string device_id; // Domain specific device ID. | |
| 63 std::string kind; | |
| 64 std::string label; | |
| 65 std::string group_id; | |
| 66 }; | |
| 67 | |
| 68 bool HasOutputDevices() { | |
| 69 // There's no fake audio output devices supported yet. We can't test audio | |
| 70 // output devices on bots with no output devices, so skip testing for that | |
| 71 // on such bots. We cache the result since querying for devices can take | |
| 72 // considerable time. | |
| 73 if (!has_audio_output_devices_initialized_) { | |
| 74 has_audio_output_devices_ = | |
| 75 media::AudioManager::Get()->HasAudioOutputDevices(); | |
| 76 has_audio_output_devices_initialized_ = true; | |
| 77 } | |
| 78 return has_audio_output_devices_; | |
| 79 } | |
| 80 | |
| 81 // If |get_sources| is true, use getSources API and leave groupId empty, | |
| 82 // otherwise use getMediaDevices API. | |
| 83 void GetMediaDevicesOrSources(content::WebContents* tab, | |
| 84 std::vector<MediaDeviceInfo>* devices, | |
| 85 bool get_sources) { | |
| 86 std::string devices_as_json = | |
| 87 ExecuteJavascript(get_sources ? "getSources()" : "getMediaDevices()", | |
| 88 tab); | |
| 89 EXPECT_FALSE(devices_as_json.empty()); | |
| 90 | |
| 91 int error_code; | |
| 92 std::string error_message; | |
| 93 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( | |
| 94 devices_as_json, base::JSON_ALLOW_TRAILING_COMMAS, &error_code, | |
| 95 &error_message); | |
| 96 | |
| 97 ASSERT_TRUE(value.get() != NULL) << error_message; | |
| 98 EXPECT_EQ(value->GetType(), base::Value::TYPE_LIST); | |
| 99 | |
| 100 base::ListValue* values; | |
| 101 ASSERT_TRUE(value->GetAsList(&values)); | |
| 102 ASSERT_FALSE(values->empty()); | |
| 103 bool found_audio_input = false; | |
| 104 bool found_video_input = false; | |
| 105 bool found_audio_output = false; | |
| 106 | |
| 107 for (base::ListValue::iterator it = values->begin(); | |
| 108 it != values->end(); ++it) { | |
| 109 const base::DictionaryValue* dict; | |
| 110 MediaDeviceInfo device; | |
| 111 ASSERT_TRUE((*it)->GetAsDictionary(&dict)); | |
| 112 ASSERT_TRUE(dict->GetString(get_sources ? "id" : "deviceId", | |
| 113 &device.device_id)); | |
| 114 ASSERT_TRUE(dict->GetString("kind", &device.kind)); | |
| 115 ASSERT_TRUE(dict->GetString("label", &device.label)); | |
| 116 if (!get_sources) | |
| 117 ASSERT_TRUE(dict->GetString("groupId", &device.group_id)); | |
| 118 | |
| 119 // Should be HMAC SHA256. | |
| 120 EXPECT_EQ(64ul, device.device_id.length()); | |
| 121 EXPECT_TRUE(base::ContainsOnlyChars(device.device_id, | |
| 122 "0123456789abcdef")); | |
| 123 | |
| 124 const char* kAudioInputKind = | |
| 125 get_sources ? kSourceKindAudioInput : kDeviceKindAudioInput; | |
| 126 const char* kVideoInputKind = | |
| 127 get_sources ? kSourceKindVideoInput : kDeviceKindVideoInput; | |
| 128 if (get_sources) { | |
| 129 EXPECT_TRUE(device.kind == kAudioInputKind || | |
| 130 device.kind == kVideoInputKind); | |
| 131 } else { | |
| 132 EXPECT_TRUE(device.kind == kAudioInputKind || | |
| 133 device.kind == kVideoInputKind || | |
| 134 device.kind == kDeviceKindAudioOutput); | |
| 135 } | |
| 136 if (device.kind == kAudioInputKind) { | |
| 137 found_audio_input = true; | |
| 138 } else if (device.kind == kVideoInputKind) { | |
| 139 found_video_input = true; | |
| 140 } else { | |
| 141 found_audio_output = true; | |
| 142 } | |
| 143 | |
| 144 // getSources doesn't have group ID support. getMediaDevices doesn't have | |
| 145 // group ID support for video input devices. | |
| 146 if (get_sources || device.kind == kDeviceKindVideoInput) { | |
| 147 EXPECT_TRUE(device.group_id.empty()); | |
| 148 } else { | |
| 149 EXPECT_FALSE(device.group_id.empty()); | |
| 150 } | |
| 151 | |
| 152 devices->push_back(device); | |
| 153 } | |
| 154 | |
| 155 EXPECT_TRUE(found_audio_input); | |
| 156 EXPECT_TRUE(found_video_input); | |
| 157 if (get_sources) { | |
| 158 EXPECT_FALSE(found_audio_output); | |
| 159 } else { | |
| 160 EXPECT_EQ(HasOutputDevices(), found_audio_output); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void GetMediaDevices(content::WebContents* tab, | |
| 165 std::vector<MediaDeviceInfo>* devices) { | |
| 166 GetMediaDevicesOrSources(tab, devices, false); | |
| 167 } | |
| 168 | |
| 169 void GetSources(content::WebContents* tab, | |
| 170 std::vector<MediaDeviceInfo>* sources) { | |
| 171 GetMediaDevicesOrSources(tab, sources, true); | |
| 172 } | |
| 173 | |
| 174 bool has_audio_output_devices_initialized_; | |
| 175 bool has_audio_output_devices_; | |
| 176 }; | |
| 177 | |
| 178 static const bool kParamsToRunTestsWith[] = { false, true }; | |
| 179 INSTANTIATE_TEST_CASE_P(WebRtcGetMediaDevicesBrowserTests, | |
| 180 WebRtcGetMediaDevicesBrowserTest, | |
| 181 testing::ValuesIn(kParamsToRunTestsWith)); | |
| 182 | |
| 183 // getMediaDevices has been removed and will be replaced | |
| 184 // MediaDevices.enumerateDevices. http://crbug.com/388648. | |
| 185 IN_PROC_BROWSER_TEST_P(WebRtcGetMediaDevicesBrowserTest, | |
| 186 DISABLED_GetMediaDevicesWithoutAccess) { | |
| 187 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 188 GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); | |
| 189 ui_test_utils::NavigateToURL(browser(), url); | |
| 190 content::WebContents* tab = | |
| 191 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 192 | |
| 193 std::vector<MediaDeviceInfo> devices; | |
| 194 GetMediaDevices(tab, &devices); | |
| 195 | |
| 196 // Labels should be empty if access has not been allowed. | |
| 197 for (std::vector<MediaDeviceInfo>::iterator it = devices.begin(); | |
| 198 it != devices.end(); ++it) { | |
| 199 EXPECT_TRUE(it->label.empty()); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 // getMediaDevices has been removed and will be replaced | |
| 204 // MediaDevices.enumerateDevices. http://crbug.com/388648. | |
| 205 // Disabled, fails due to http://crbug.com/382391. | |
| 206 IN_PROC_BROWSER_TEST_P(WebRtcGetMediaDevicesBrowserTest, | |
| 207 DISABLED_GetMediaDevicesWithAccess) { | |
| 208 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 209 GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); | |
| 210 ui_test_utils::NavigateToURL(browser(), url); | |
| 211 content::WebContents* tab = | |
| 212 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 213 | |
| 214 EXPECT_TRUE(GetUserMediaAndAccept(tab)); | |
| 215 | |
| 216 std::vector<MediaDeviceInfo> devices; | |
| 217 GetMediaDevices(tab, &devices); | |
| 218 | |
| 219 // Labels should be non-empty if access has been allowed. | |
| 220 for (std::vector<MediaDeviceInfo>::iterator it = devices.begin(); | |
| 221 it != devices.end(); ++it) { | |
| 222 EXPECT_TRUE(!it->label.empty()); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 // getMediaDevices has been removed and will be replaced | |
| 227 // MediaDevices.enumerateDevices. http://crbug.com/388648. | |
| 228 IN_PROC_BROWSER_TEST_P(WebRtcGetMediaDevicesBrowserTest, | |
| 229 DISABLED_GetMediaDevicesEqualsGetSourcesWithoutAccess) { | |
| 230 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 231 GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); | |
| 232 ui_test_utils::NavigateToURL(browser(), url); | |
| 233 content::WebContents* tab = | |
| 234 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 235 | |
| 236 std::vector<MediaDeviceInfo> devices; | |
| 237 GetMediaDevices(tab, &devices); | |
| 238 | |
| 239 std::vector<MediaDeviceInfo> sources; | |
| 240 GetSources(tab, &sources); | |
| 241 | |
| 242 std::vector<MediaDeviceInfo>::iterator sources_it = sources.begin(); | |
| 243 for (std::vector<MediaDeviceInfo>::iterator devices_it = devices.begin(); | |
| 244 devices_it != devices.end(); ++devices_it) { | |
| 245 if (devices_it->kind == kDeviceKindAudioOutput) | |
| 246 continue; | |
| 247 EXPECT_STREQ(devices_it->device_id.c_str(), sources_it->device_id.c_str()); | |
| 248 if (devices_it->kind == kDeviceKindAudioInput) { | |
| 249 EXPECT_STREQ(kSourceKindAudioInput, sources_it->kind.c_str()); | |
| 250 } else { | |
| 251 EXPECT_STREQ(kSourceKindVideoInput, sources_it->kind.c_str()); | |
| 252 } | |
| 253 EXPECT_TRUE(devices_it->label.empty()); | |
| 254 EXPECT_TRUE(sources_it->label.empty()); | |
| 255 ++sources_it; | |
| 256 } | |
| 257 EXPECT_EQ(sources.end(), sources_it); | |
| 258 } | |
| 259 | |
| 260 // getMediaDevices has been removed and will be replaced | |
| 261 // MediaDevices.enumerateDevices. http://crbug.com/388648. | |
| 262 // Disabled, fails due to http://crbug.com/382391. | |
| 263 IN_PROC_BROWSER_TEST_P(WebRtcGetMediaDevicesBrowserTest, | |
| 264 DISABLED_GetMediaDevicesEqualsGetSourcesWithAccess) { | |
| 265 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 266 GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); | |
| 267 ui_test_utils::NavigateToURL(browser(), url); | |
| 268 content::WebContents* tab = | |
| 269 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 270 | |
| 271 EXPECT_TRUE(GetUserMediaAndAccept(tab)); | |
| 272 | |
| 273 std::vector<MediaDeviceInfo> devices; | |
| 274 GetMediaDevices(tab, &devices); | |
| 275 | |
| 276 std::vector<MediaDeviceInfo> sources; | |
| 277 GetSources(tab, &sources); | |
| 278 | |
| 279 std::vector<MediaDeviceInfo>::iterator sources_it = sources.begin(); | |
| 280 for (std::vector<MediaDeviceInfo>::iterator devices_it = devices.begin(); | |
| 281 devices_it != devices.end(); ++devices_it) { | |
| 282 if (devices_it->kind == kDeviceKindAudioOutput) | |
| 283 continue; | |
| 284 EXPECT_STREQ(devices_it->device_id.c_str(), sources_it->device_id.c_str()); | |
| 285 if (devices_it->kind == kDeviceKindAudioInput) { | |
| 286 EXPECT_STREQ(kSourceKindAudioInput, sources_it->kind.c_str()); | |
| 287 } else { | |
| 288 EXPECT_STREQ(kSourceKindVideoInput, sources_it->kind.c_str()); | |
| 289 } | |
| 290 EXPECT_TRUE(!devices_it->label.empty()); | |
| 291 EXPECT_STREQ(devices_it->label.c_str(), sources_it->label.c_str()); | |
| 292 ++sources_it; | |
| 293 } | |
| 294 EXPECT_EQ(sources.end(), sources_it); | |
| 295 } | |
| OLD | NEW |