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(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 (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
135 content::WebContents
* tab_contents
= LoadTestPageInTab();
137 GetUserMediaAndAccept(tab_contents
);
138 GetUserMediaAndDeny(tab_contents
);
140 // Should fail with permission denied right away with no infobar popping up.
141 GetUserMedia(tab_contents
, kAudioVideoCallConstraints
);
142 EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()",
143 kFailedWithPermissionDeniedError
,
145 InfoBarService
* infobar_service
=
146 InfoBarService::FromWebContents(tab_contents
);
147 EXPECT_EQ(0u, infobar_service
->infobar_count());
150 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestAcceptIsNotSticky
) {
151 content::WebContents
* tab_contents
= LoadTestPageInTab();
153 // If accept were sticky the second call would hang because it hangs if an
154 // infobar does not pop up.
155 GetUserMediaAndAccept(tab_contents
);
156 GetUserMediaAndAccept(tab_contents
);
159 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDismissIsNotSticky
) {
160 content::WebContents
* tab_contents
= LoadTestPageInTab();
162 // If dismiss were sticky the second call would hang because it hangs if an
163 // infobar does not pop up.
164 GetUserMediaAndDismiss(tab_contents
);
165 GetUserMediaAndDismiss(tab_contents
);
168 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
169 TestDenyingThenClearingStickyException
) {
170 content::WebContents
* tab_contents
= LoadTestPageInTab();
172 GetUserMediaAndDeny(tab_contents
);
174 HostContentSettingsMap
* settings_map
=
175 browser()->profile()->GetHostContentSettingsMap();
177 settings_map
->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
178 settings_map
->ClearSettingsForOneType(
179 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
181 // If an infobar is not launched now, this will hang.
182 GetUserMediaAndDeny(tab_contents
);
185 // Times out on win debug builds; http://crbug.com/295723 .
186 #if defined(OS_WIN) && !defined(NDEBUG)
187 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
188 DISABLED_DenyingMicDoesNotCauseStickyDenyForCameras
190 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
191 DenyingMicDoesNotCauseStickyDenyForCameras
193 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
194 MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras
) {
195 content::WebContents
* tab_contents
= LoadTestPageInTab();
197 // If mic blocking also blocked cameras, the second call here would hang.
198 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
199 kAudioOnlyCallConstraints
);
200 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents
,
201 kVideoOnlyCallConstraints
);
204 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
205 DenyingCameraDoesNotCauseStickyDenyForMics
) {
206 content::WebContents
* tab_contents
= LoadTestPageInTab();
208 // If camera blocking also blocked mics, the second call here would hang.
209 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
210 kVideoOnlyCallConstraints
);
211 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents
,
212 kAudioOnlyCallConstraints
);