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/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/media/media_stream_devices_controller.h"
13 #include "chrome/browser/media/webrtc_browsertest_base.h"
14 #include "chrome/browser/media/webrtc_browsertest_common.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/content_settings/core/common/content_settings_types.h"
24 #include "components/infobars/core/infobar.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/common/media_stream_request.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "media/base/media_switches.h"
29 #include "net/test/spawned_test_server/spawned_test_server.h"
32 // MediaStreamInfoBarTest -----------------------------------------------------
34 class MediaStreamInfoBarTest
: public WebRtcTestBase
{
36 MediaStreamInfoBarTest() {}
37 virtual ~MediaStreamInfoBarTest() {}
39 // InProcessBrowserTest:
40 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
41 // This test expects to run with fake devices but real UI.
42 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
43 EXPECT_FALSE(command_line
->HasSwitch(switches::kUseFakeUIForMediaStream
))
44 << "Since this test tests the UI we want the real UI!";
48 content::WebContents
* LoadTestPageInTab() {
49 return LoadTestPageInBrowser(browser());
52 content::WebContents
* LoadTestPageInIncognitoTab() {
53 return LoadTestPageInBrowser(CreateIncognitoBrowser());
56 // Returns the URL of the main test page.
57 GURL
test_page_url() const {
58 const char kMainWebrtcTestHtmlPage
[] =
59 "files/webrtc/webrtc_jsep01_test.html";
60 return test_server()->GetURL(kMainWebrtcTestHtmlPage
);
63 // Denies getUserMedia requests (audio, video) for the test page.
64 // The deny setting is sticky.
65 void DenyRequest(content::WebContents
* tab_contents
,
66 content::MediaStreamRequestResult result
) const {
67 const std::string no_id
;
68 content::MediaStreamRequest
request(
69 0, 0, 0, test_page_url().GetOrigin(), false,
70 content::MEDIA_DEVICE_ACCESS
, no_id
, no_id
,
71 content::MEDIA_DEVICE_AUDIO_CAPTURE
,
72 content::MEDIA_DEVICE_VIDEO_CAPTURE
);
74 scoped_ptr
<MediaStreamDevicesController
> controller(
75 new MediaStreamDevicesController(tab_contents
, request
,
76 base::Bind(&OnMediaStreamResponse
)));
77 controller
->Deny(true, result
);
80 // Executes stopLocalStream() in the test page, which frees up an already
81 // acquired mediastream.
82 bool StopLocalStream(content::WebContents
* tab_contents
) {
84 bool ok
= content::ExecuteScriptAndExtractString(
85 tab_contents
, "stopLocalStream()", &result
);
87 return result
.compare("ok-stopped") == 0;
91 content::WebContents
* LoadTestPageInBrowser(Browser
* browser
) {
92 EXPECT_TRUE(test_server()->Start());
94 ui_test_utils::NavigateToURL(browser
, test_page_url());
95 return browser
->tab_strip_model()->GetActiveWebContents();
98 // Dummy callback for when we deny the current request directly.
99 static void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
100 content::MediaStreamRequestResult result
,
101 scoped_ptr
<content::MediaStreamUI
> ui
) {}
103 DISALLOW_COPY_AND_ASSIGN(MediaStreamInfoBarTest
);
106 // Actual tests ---------------------------------------------------------------
108 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestAllowingUserMedia
) {
109 content::WebContents
* tab_contents
= LoadTestPageInTab();
110 GetUserMediaAndAccept(tab_contents
);
113 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDenyingUserMedia
) {
114 content::WebContents
* tab_contents
= LoadTestPageInTab();
115 GetUserMediaAndDeny(tab_contents
);
118 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDismissingInfobar
) {
119 content::WebContents
* tab_contents
= LoadTestPageInTab();
120 GetUserMediaAndDismiss(tab_contents
);
123 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
, TestDenyingUserMediaIncognito
) {
124 content::WebContents
* tab_contents
= LoadTestPageInIncognitoTab();
125 GetUserMediaAndDeny(tab_contents
);
128 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest
,
129 TestAcceptThenDenyWhichShouldBeSticky
) {
130 #if defined(OS_WIN) && defined(USE_ASH)
131 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
132 if (CommandLine::ForCurrentProcess()->HasSwitch(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
);