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/content_settings/host_content_settings_map_factory.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/browser/ui/website_settings/permission_bubble_manager.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/test_switches.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/content_settings/core/browser/host_content_settings_map.h"
23 #include "components/content_settings/core/common/content_settings_types.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/common/media_stream_request.h"
26 #include "content/public/common/origin_util.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "media/base/media_switches.h"
29 #include "net/dns/mock_host_resolver.h"
30 #include "net/test/spawned_test_server/spawned_test_server.h"
32 // MediaStreamPermissionTest ---------------------------------------------------
34 class MediaStreamPermissionTest
: public WebRtcTestBase
{
36 MediaStreamPermissionTest() {}
37 ~MediaStreamPermissionTest() override
{}
39 // InProcessBrowserTest:
40 void SetUpCommandLine(base::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 // Executes stopLocalStream() in the test page, which frees up an already
64 // acquired mediastream.
65 bool StopLocalStream(content::WebContents
* tab_contents
) {
67 bool ok
= content::ExecuteScriptAndExtractString(
68 tab_contents
, "stopLocalStream()", &result
);
70 return result
.compare("ok-stopped") == 0;
74 content::WebContents
* LoadTestPageInBrowser(Browser
* browser
) {
75 EXPECT_TRUE(test_server()->Start());
77 // Uses the default server.
78 GURL url
= test_page_url();
80 EXPECT_TRUE(content::IsOriginSecure(url
));
82 ui_test_utils::NavigateToURL(browser
, url
);
83 return browser
->tab_strip_model()->GetActiveWebContents();
86 // Dummy callback for when we deny the current request directly.
87 static void OnMediaStreamResponse(const content::MediaStreamDevices
& devices
,
88 content::MediaStreamRequestResult result
,
89 scoped_ptr
<content::MediaStreamUI
> ui
) {}
91 DISALLOW_COPY_AND_ASSIGN(MediaStreamPermissionTest
);
94 // Actual tests ---------------------------------------------------------------
96 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestAllowingUserMedia
) {
97 content::WebContents
* tab_contents
= LoadTestPageInTab();
98 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
101 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDenyingUserMedia
) {
102 content::WebContents
* tab_contents
= LoadTestPageInTab();
103 GetUserMediaAndDeny(tab_contents
);
106 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissingRequest
) {
107 content::WebContents
* tab_contents
= LoadTestPageInTab();
108 GetUserMediaAndDismiss(tab_contents
);
111 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
112 TestDenyingUserMediaIncognito
) {
113 content::WebContents
* tab_contents
= LoadTestPageInIncognitoTab();
114 GetUserMediaAndDeny(tab_contents
);
117 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
118 TestSecureOriginDenyIsSticky
) {
119 content::WebContents
* tab_contents
= LoadTestPageInTab();
120 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
122 GetUserMediaAndDeny(tab_contents
);
123 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
126 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
127 TestSecureOriginAcceptIsSticky
) {
128 content::WebContents
* tab_contents
= LoadTestPageInTab();
129 EXPECT_TRUE(content::IsOriginSecure(tab_contents
->GetLastCommittedURL()));
131 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents
));
132 GetUserMediaAndExpectAutoAcceptWithoutPrompt(tab_contents
);
135 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
, TestDismissIsNotSticky
) {
136 content::WebContents
* tab_contents
= LoadTestPageInTab();
138 GetUserMediaAndDismiss(tab_contents
);
139 GetUserMediaAndDismiss(tab_contents
);
142 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
143 TestDenyingThenClearingStickyException
) {
144 content::WebContents
* tab_contents
= LoadTestPageInTab();
146 GetUserMediaAndDeny(tab_contents
);
147 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents
);
149 HostContentSettingsMap
* settings_map
=
150 HostContentSettingsMapFactory::GetForProfile(browser()->profile());
152 settings_map
->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
153 settings_map
->ClearSettingsForOneType(
154 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
156 GetUserMediaAndDeny(tab_contents
);
159 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
160 DenyingMicDoesNotCauseStickyDenyForCameras
) {
161 content::WebContents
* tab_contents
= LoadTestPageInTab();
163 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
164 kAudioOnlyCallConstraints
);
165 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
166 tab_contents
, kVideoOnlyCallConstraints
));
169 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest
,
170 DenyingCameraDoesNotCauseStickyDenyForMics
) {
171 content::WebContents
* tab_contents
= LoadTestPageInTab();
173 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents
,
174 kVideoOnlyCallConstraints
);
175 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
176 tab_contents
, kAudioOnlyCallConstraints
));