1 // Copyright 2013 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/files/file_util.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/media/media_stream_devices_controller.h"
11 #include "chrome/browser/media/webrtc_browsertest_base.h"
12 #include "chrome/browser/media/webrtc_browsertest_common.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/test_switches.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "components/content_settings/core/common/content_settings_types.h"
23 #include "components/infobars/core/infobar.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/common/media_stream_request.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "media/base/media_switches.h"
28 #include "net/test/spawned_test_server/spawned_test_server.h"
31 // MediaStreamInfoBarTest -----------------------------------------------------
33 class MediaStreamInfoBarTest
: public WebRtcTestBase
{
35 MediaStreamInfoBarTest() {}
36 ~MediaStreamInfoBarTest() override
{}
38 // InProcessBrowserTest:
39 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
40 // This test expects to run with fake devices but real UI.
41 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
42 EXPECT_FALSE(command_line
->HasSwitch(switches::kUseFakeUIForMediaStream
))
43 << "Since this test tests the UI we want the real UI!";
47 content::WebContents
* LoadTestPageInTab() {
48 return LoadTestPageInBrowser(browser());
51 content::WebContents
* LoadTestPageInIncognitoTab() {
52 return LoadTestPageInBrowser(CreateIncognitoBrowser());
55 // Returns the URL of the main test page.
56 GURL
test_page_url() const {
57 const char kMainWebrtcTestHtmlPage
[] =
58 "files/webrtc/webrtc_jsep01_test.html";
59 return test_server()->GetURL(kMainWebrtcTestHtmlPage
);
62 // Denies getUserMedia requests (audio, video) for the test page.
63 // The deny setting is sticky.
64 void DenyRequest(content::WebContents
* tab_contents
,
65 content::MediaStreamRequestResult result
) const {
66 const std::string no_id
;
67 content::MediaStreamRequest
request(
68 0, 0, 0, test_page_url().GetOrigin(), false,
69 content::MEDIA_DEVICE_ACCESS
, no_id
, no_id
,
70 content::MEDIA_DEVICE_AUDIO_CAPTURE
,
71 content::MEDIA_DEVICE_VIDEO_CAPTURE
);
73 scoped_ptr
<MediaStreamDevicesController
> controller(
74 new MediaStreamDevicesController(tab_contents
, request
,
75 base::Bind(&OnMediaStreamResponse
)));
76 controller
->Deny(true, result
);
79 // Executes stopLocalStream() in the test page, which frees up an already
80 // acquired mediastream.
81 bool StopLocalStream(content::WebContents
* tab_contents
) {
83 bool ok
= content::ExecuteScriptAndExtractString(
84 tab_contents
, "stopLocalStream()", &result
);
86 return result
.compare("ok-stopped") == 0;
90 content::WebContents
* LoadTestPageInBrowser(Browser
* browser
) {
91 EXPECT_TRUE(test_server()->Start());
93 ui_test_utils::NavigateToURL(browser
, test_page_url());
94 return browser
->tab_strip_model()->GetActiveWebContents();
97 // Dummy callback for when we deny the current request directly.
98 static void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
99 content::MediaStreamRequestResult result
,
100 scoped_ptr
<content::MediaStreamUI
> ui
) {}
102 DISALLOW_COPY_AND_ASSIGN(MediaStreamInfoBarTest
);
105 // Actual tests ---------------------------------------------------------------
107 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestAllowingUserMedia
) {
108 content::WebContents
* tab_contents
= LoadTestPageInTab();
109 GetUserMediaAndAccept(tab_contents
);
112 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDenyingUserMedia
) {
113 content::WebContents
* tab_contents
= LoadTestPageInTab();
114 GetUserMediaAndDeny(tab_contents
);
117 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDismissingInfobar
) {
118 content::WebContents
* tab_contents
= LoadTestPageInTab();
119 GetUserMediaAndDismiss(tab_contents
);
122 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDenyingUserMediaIncognito
) {
123 content::WebContents
* tab_contents
= LoadTestPageInIncognitoTab();
124 GetUserMediaAndDeny(tab_contents
);
127 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
128 TestAcceptThenDenyWhichShouldBeSticky
) {
129 #if defined(OS_WIN) && defined(USE_ASH)
130 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
131 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kAshBrowserTests
))
136 content::WebContents
* tab_contents
= LoadTestPageInTab();
138 GetUserMediaAndAccept(tab_contents
);
139 GetUserMediaAndDeny(tab_contents
);
141 // Should fail with permission denied right away with no infobar popping up.
142 GetUserMedia(tab_contents
, kAudioVideoCallConstraints
);
143 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()",
144 kFailedWithPermissionDeniedError
,
146 InfoBarService
* infobar_service
=
147 InfoBarService::FromWebContents(tab_contents
);
148 EXPECT_EQ(0u, infobar_service
->infobar_count());
151 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestAcceptIsNotSticky
) {
152 content::WebContents
* tab_contents
= LoadTestPageInTab();
154 // If accept were sticky the second call would hang because it hangs if an
155 // infobar does not pop up.
156 GetUserMediaAndAccept(tab_contents
);
157 GetUserMediaAndAccept(tab_contents
);
160 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDismissIsNotSticky
) {
161 content::WebContents
* tab_contents
= LoadTestPageInTab();
163 // If dismiss were sticky the second call would hang because it hangs if an
164 // infobar does not pop up.
165 GetUserMediaAndDismiss(tab_contents
);
166 GetUserMediaAndDismiss(tab_contents
);
169 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
170 TestDenyingThenClearingStickyException
) {
171 content::WebContents
* tab_contents
= LoadTestPageInTab();
173 GetUserMediaAndDeny(tab_contents
);
175 HostContentSettingsMap
* settings_map
=
176 browser()->profile()->GetHostContentSettingsMap();
178 settings_map
->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
179 settings_map
->ClearSettingsForOneType(
180 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
182 // If an infobar is not launched now, this will hang.
183 GetUserMediaAndDeny(tab_contents
);
186 // Times out on win debug builds; http://crbug.com/295723 .
187 #if defined(OS_WIN) && !defined(NDEBUG)
188 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
189 DISABLED_DenyingMicDoesNotCauseStickyDenyForCameras
191 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
192 DenyingMicDoesNotCauseStickyDenyForCameras
194 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
195 MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras
) {
196 content::WebContents
* tab_contents
= LoadTestPageInTab();
198 // If mic blocking also blocked cameras, the second call here would hang.
199 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
200 kAudioOnlyCallConstraints
);
201 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents
,
202 kVideoOnlyCallConstraints
);
205 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
206 DenyingCameraDoesNotCauseStickyDenyForMics
) {
207 content::WebContents
* tab_contents
= LoadTestPageInTab();
209 // If camera blocking also blocked mics, the second call here would hang.
210 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
211 kVideoOnlyCallConstraints
);
212 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents
,
213 kAudioOnlyCallConstraints
);