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;
73 content::WebContents
* LoadTestPageInBrowser(Browser
* browser
) {
74 EXPECT_TRUE(test_server()->Start());
76 // Uses the default server.
77 GURL url
= test_page_url();
79 EXPECT_TRUE(content::IsOriginSecure(url
));
81 ui_test_utils::NavigateToURL(browser
, url
);
82 return browser
->tab_strip_model()->GetActiveWebContents();
85 // Dummy callback for when we deny the current request directly.
86 static void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
87 content::MediaStreamRequestResult result
,
88 scoped_ptr
<content::MediaStreamUI
> ui
) {}
90 DISALLOW_COPY_AND_ASSIGN(MediaStreamPermissionTest
);
93 // Actual tests ---------------------------------------------------------------
95 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestAllowingUserMedia
) {
96 content::WebContents
* tab_contents
= LoadTestPageInTab();
97 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
100 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDenyingUserMedia
) {
101 content::WebContents
* tab_contents
= LoadTestPageInTab();
102 GetUserMediaAndDeny(tab_contents
);
105 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissingRequest
) {
106 content::WebContents
* tab_contents
= LoadTestPageInTab();
107 GetUserMediaAndDismiss(tab_contents
);
110 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
111 TestDenyingUserMediaIncognito
) {
112 content::WebContents
* tab_contents
= LoadTestPageInIncognitoTab();
113 GetUserMediaAndDeny(tab_contents
);
116 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
117 TestSecureOriginDenyIsSticky
) {
118 content::WebContents
* tab_contents
= LoadTestPageInTab();
119 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
121 GetUserMediaAndDeny(tab_contents
);
122 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
125 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
126 TestSecureOriginAcceptIsSticky
) {
127 content::WebContents
* tab_contents
= LoadTestPageInTab();
128 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
130 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
131 GetUserMediaAndExpectAutoAcceptWithoutPrompt(tab_contents
);
134 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissIsNotSticky
) {
135 content::WebContents
* tab_contents
= LoadTestPageInTab();
137 GetUserMediaAndDismiss(tab_contents
);
138 GetUserMediaAndDismiss(tab_contents
);
141 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
142 TestDenyingThenClearingStickyException
) {
143 content::WebContents
* tab_contents
= LoadTestPageInTab();
145 GetUserMediaAndDeny(tab_contents
);
146 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
148 HostContentSettingsMap
* settings_map
=
149 browser()->profile()->GetHostContentSettingsMap();
151 settings_map
->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
152 settings_map
->ClearSettingsForOneType(
153 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
155 GetUserMediaAndDeny(tab_contents
);
158 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
159 DenyingMicDoesNotCauseStickyDenyForCameras
) {
160 content::WebContents
* tab_contents
= LoadTestPageInTab();
162 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
163 kAudioOnlyCallConstraints
);
164 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
165 tab_contents
, kVideoOnlyCallConstraints
));
168 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
169 DenyingCameraDoesNotCauseStickyDenyForMics
) {
170 content::WebContents
* tab_contents
= LoadTestPageInTab();
172 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
173 kVideoOnlyCallConstraints
);
174 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
175 tab_contents
, kAudioOnlyCallConstraints
));