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/media/media_stream_devices_controller.h"
10 #include "chrome/browser/media/webrtc_browsertest_base.h"
11 #include "chrome/browser/media/webrtc_browsertest_common.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_tabstrip.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/website_settings/permission_bubble_manager.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 "content/public/browser/notification_service.h"
24 #include "content/public/common/media_stream_request.h"
25 #include "content/public/common/origin_util.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "media/base/media_switches.h"
28 #include "net/dns/mock_host_resolver.h"
29 #include "net/test/spawned_test_server/spawned_test_server.h"
31 // MediaStreamPermissionTest ---------------------------------------------------
33 class MediaStreamPermissionTest
: public WebRtcTestBase
{
35 MediaStreamPermissionTest() {}
36 ~MediaStreamPermissionTest() 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 // Executes stopLocalStream() in the test page, which frees up an already
63 // acquired mediastream.
64 bool StopLocalStream(content::WebContents
* tab_contents
) {
66 bool ok
= content::ExecuteScriptAndExtractString(
67 tab_contents
, "stopLocalStream()", &result
);
69 return result
.compare("ok-stopped") == 0;
72 void useNonSecureOriginForTestPage() {
73 use_secure_origin_for_test_page_
= false;
74 host_resolver()->AddRule("*", "127.0.0.1");
78 // The default test server is localhost, which is considered secure:
79 // http://www.w3.org/TR/powerful-features/#is-origin-trustworthy
80 bool use_secure_origin_for_test_page_
= true;
82 content::WebContents
* LoadTestPageInBrowser(Browser
* browser
) {
83 EXPECT_TRUE(test_server()->Start());
87 if (use_secure_origin_for_test_page_
) {
88 // Uses the default server.
89 url
= test_page_url();
91 static const char kFoo
[] = "not-secure.example.com";
92 GURL::Replacements replacements
;
93 replacements
.SetSchemeStr(url::kHttpScheme
);
94 replacements
.SetHostStr(kFoo
);
95 url
= test_page_url().ReplaceComponents(replacements
);
98 EXPECT_EQ(use_secure_origin_for_test_page_
, content::IsOriginSecure(url
));
100 ui_test_utils::NavigateToURL(browser
, url
);
101 return browser
->tab_strip_model()->GetActiveWebContents();
104 // Dummy callback for when we deny the current request directly.
105 static void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
106 content::MediaStreamRequestResult result
,
107 scoped_ptr
<content::MediaStreamUI
> ui
) {}
109 DISALLOW_COPY_AND_ASSIGN(MediaStreamPermissionTest
);
112 // Actual tests ---------------------------------------------------------------
114 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestAllowingUserMedia
) {
115 content::WebContents
* tab_contents
= LoadTestPageInTab();
116 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
119 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDenyingUserMedia
) {
120 content::WebContents
* tab_contents
= LoadTestPageInTab();
121 GetUserMediaAndDeny(tab_contents
);
124 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissingRequest
) {
125 content::WebContents
* tab_contents
= LoadTestPageInTab();
126 GetUserMediaAndDismiss(tab_contents
);
129 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
130 TestDenyingUserMediaIncognito
) {
131 content::WebContents
* tab_contents
= LoadTestPageInIncognitoTab();
132 GetUserMediaAndDeny(tab_contents
);
135 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
136 TestNonSecureOriginAcceptThenDenyIsSticky
) {
137 #if defined(OS_WIN) && defined(USE_ASH)
138 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
139 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
140 switches::kAshBrowserTests
))
144 useNonSecureOriginForTestPage();
145 content::WebContents
* tab_contents
= LoadTestPageInTab();
146 EXPECT_FALSE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
148 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
149 GetUserMediaAndDeny(tab_contents
);
151 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
154 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
155 TestNonSecureOriginDenyIsSticky
) {
156 useNonSecureOriginForTestPage();
157 content::WebContents
* tab_contents
= LoadTestPageInTab();
158 EXPECT_FALSE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
160 GetUserMediaAndDeny(tab_contents
);
161 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
164 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
165 TestSecureOriginDenyIsSticky
) {
166 content::WebContents
* tab_contents
= LoadTestPageInTab();
167 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
169 GetUserMediaAndDeny(tab_contents
);
170 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
173 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
174 TestNonSecureOriginAcceptIsNotSticky
) {
175 useNonSecureOriginForTestPage();
176 content::WebContents
* tab_contents
= LoadTestPageInTab();
177 EXPECT_FALSE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
179 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
180 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
183 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
184 TestSecureOriginAcceptIsSticky
) {
185 content::WebContents
* tab_contents
= LoadTestPageInTab();
186 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
188 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
189 GetUserMediaAndExpectAutoAcceptWithoutPrompt(tab_contents
);
192 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissIsNotSticky
) {
193 content::WebContents
* tab_contents
= LoadTestPageInTab();
195 GetUserMediaAndDismiss(tab_contents
);
196 GetUserMediaAndDismiss(tab_contents
);
199 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
200 TestDenyingThenClearingStickyException
) {
201 content::WebContents
* tab_contents
= LoadTestPageInTab();
203 GetUserMediaAndDeny(tab_contents
);
204 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
206 HostContentSettingsMap
* settings_map
=
207 browser()->profile()->GetHostContentSettingsMap();
209 settings_map
->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
210 settings_map
->ClearSettingsForOneType(
211 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
213 GetUserMediaAndDeny(tab_contents
);
216 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
217 DenyingMicDoesNotCauseStickyDenyForCameras
) {
218 content::WebContents
* tab_contents
= LoadTestPageInTab();
220 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
221 kAudioOnlyCallConstraints
);
222 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
223 tab_contents
, kVideoOnlyCallConstraints
));
226 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
227 DenyingCameraDoesNotCauseStickyDenyForMics
) {
228 content::WebContents
* tab_contents
= LoadTestPageInTab();
230 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
231 kVideoOnlyCallConstraints
);
232 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
233 tab_contents
, kAudioOnlyCallConstraints
));