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