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.
4
5 #include "base/command_line.h"
6 #include "base/files/file_util.h"
7 #include "base/macros.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11 #include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
12 #include "chrome/browser/media/webrtc/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 "content/public/browser/notification_service.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/test/browser_test.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/embedded_test_server/embedded_test_server.h"
30 #include "third_party/blink/public/common/loader/network_utils.h"
31 #include "third_party/blink/public/common/mediastream/media_stream_request.h"
32 #include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
33
34 // MediaStreamPermissionTest ---------------------------------------------------
35
36 class MediaStreamPermissionTest : public WebRtcTestBase {
37 public:
MediaStreamPermissionTest()38 MediaStreamPermissionTest() {}
~MediaStreamPermissionTest()39 ~MediaStreamPermissionTest() override {}
40
41 // InProcessBrowserTest:
SetUp()42 void SetUp() override {
43 WebRtcTestBase::SetUp();
44 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
45 // This test expects to run with fake devices but real UI.
46 EXPECT_TRUE(
47 command_line->HasSwitch(switches::kUseFakeDeviceForMediaStream));
48 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream))
49 << "Since this test tests the UI we want the real UI!";
50 }
51
52 protected:
LoadTestPageInTab()53 content::WebContents* LoadTestPageInTab() {
54 return LoadTestPageInBrowser(browser());
55 }
56
LoadTestPageInIncognitoTab()57 content::WebContents* LoadTestPageInIncognitoTab() {
58 return LoadTestPageInBrowser(CreateIncognitoBrowser());
59 }
60
61 // Returns the URL of the main test page.
test_page_url() const62 GURL test_page_url() const {
63 const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html";
64 return embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage);
65 }
66
67 private:
LoadTestPageInBrowser(Browser * browser)68 content::WebContents* LoadTestPageInBrowser(Browser* browser) {
69 if (!embedded_test_server()->Started()) {
70 EXPECT_TRUE(embedded_test_server()->Start());
71 }
72
73 // Uses the default server.
74 GURL url = test_page_url();
75
76 EXPECT_TRUE(blink::network_utils::IsOriginSecure(url));
77
78 ui_test_utils::NavigateToURL(browser, url);
79 return browser->tab_strip_model()->GetActiveWebContents();
80 }
81
82 DISALLOW_COPY_AND_ASSIGN(MediaStreamPermissionTest);
83 };
84
85 // Actual tests ---------------------------------------------------------------
86
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestAllowingUserMedia)87 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest, TestAllowingUserMedia) {
88 content::WebContents* tab_contents = LoadTestPageInTab();
89 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents));
90 }
91
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestDenyingUserMedia)92 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest, TestDenyingUserMedia) {
93 content::WebContents* tab_contents = LoadTestPageInTab();
94 GetUserMediaAndDeny(tab_contents);
95 }
96
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestDismissingRequest)97 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest, TestDismissingRequest) {
98 content::WebContents* tab_contents = LoadTestPageInTab();
99 GetUserMediaAndDismiss(tab_contents);
100 }
101
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestDenyingUserMediaIncognito)102 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
103 TestDenyingUserMediaIncognito) {
104 content::WebContents* tab_contents = LoadTestPageInIncognitoTab();
105 GetUserMediaAndDeny(tab_contents);
106 }
107
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestSecureOriginDenyIsSticky)108 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
109 TestSecureOriginDenyIsSticky) {
110 content::WebContents* tab_contents = LoadTestPageInTab();
111 EXPECT_TRUE(blink::network_utils::IsOriginSecure(
112 tab_contents->GetLastCommittedURL()));
113
114 GetUserMediaAndDeny(tab_contents);
115 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents);
116 }
117
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestSecureOriginAcceptIsSticky)118 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
119 TestSecureOriginAcceptIsSticky) {
120 content::WebContents* tab_contents = LoadTestPageInTab();
121 EXPECT_TRUE(blink::network_utils::IsOriginSecure(
122 tab_contents->GetLastCommittedURL()));
123
124 EXPECT_TRUE(GetUserMediaAndAccept(tab_contents));
125 GetUserMediaAndExpectAutoAcceptWithoutPrompt(tab_contents);
126 }
127
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestDismissIsNotSticky)128 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest, TestDismissIsNotSticky) {
129 content::WebContents* tab_contents = LoadTestPageInTab();
130
131 GetUserMediaAndDismiss(tab_contents);
132 GetUserMediaAndDismiss(tab_contents);
133 }
134
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,TestDenyingThenClearingStickyException)135 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
136 TestDenyingThenClearingStickyException) {
137 content::WebContents* tab_contents = LoadTestPageInTab();
138
139 GetUserMediaAndDeny(tab_contents);
140 GetUserMediaAndExpectAutoDenyWithoutPrompt(tab_contents);
141
142 HostContentSettingsMap* settings_map =
143 HostContentSettingsMapFactory::GetForProfile(browser()->profile());
144
145 settings_map->ClearSettingsForOneType(ContentSettingsType::MEDIASTREAM_MIC);
146 settings_map->ClearSettingsForOneType(
147 ContentSettingsType::MEDIASTREAM_CAMERA);
148
149 GetUserMediaAndDeny(tab_contents);
150 }
151
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,DenyingMicDoesNotCauseStickyDenyForCameras)152 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
153 DenyingMicDoesNotCauseStickyDenyForCameras) {
154 content::WebContents* tab_contents = LoadTestPageInTab();
155
156 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents,
157 kAudioOnlyCallConstraints);
158 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
159 tab_contents, kVideoOnlyCallConstraints));
160 }
161
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,DenyingCameraDoesNotCauseStickyDenyForMics)162 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
163 DenyingCameraDoesNotCauseStickyDenyForMics) {
164 content::WebContents* tab_contents = LoadTestPageInTab();
165
166 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents,
167 kVideoOnlyCallConstraints);
168 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept(
169 tab_contents, kAudioOnlyCallConstraints));
170 }
171
IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,DenyingPermissionStopsStreamWhenRelevant)172 IN_PROC_BROWSER_TEST_F(MediaStreamPermissionTest,
173 DenyingPermissionStopsStreamWhenRelevant) {
174 struct {
175 std::string constraints;
176 ContentSettingsType setting_to_clear;
177 bool should_video_stop;
178 } kTests[] = {
179 {kAudioVideoCallConstraints, ContentSettingsType::MEDIASTREAM_CAMERA,
180 true},
181 {kAudioVideoCallConstraints, ContentSettingsType::MEDIASTREAM_MIC, true},
182 {kVideoOnlyCallConstraints, ContentSettingsType::MEDIASTREAM_CAMERA,
183 true},
184 {kVideoOnlyCallConstraints, ContentSettingsType::MEDIASTREAM_MIC, false},
185 };
186
187 HostContentSettingsMap* settings_map =
188 HostContentSettingsMapFactory::GetForProfile(browser()->profile());
189
190 for (const auto& kTest : kTests) {
191 content::WebContents* tab_contents = LoadTestPageInTab();
192
193 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
194 tab_contents, kTest.constraints));
195
196 StartDetectingVideo(tab_contents, "local-view");
197 EXPECT_TRUE(WaitForVideoToPlay(tab_contents));
198
199 settings_map->ClearSettingsForOneType(kTest.setting_to_clear);
200
201 // Let all the cross-thread tasks do their work.
202 base::RunLoop().RunUntilIdle();
203
204 StartDetectingVideo(tab_contents, "local-view");
205
206 if (kTest.should_video_stop) {
207 EXPECT_TRUE(WaitForVideoToStop(tab_contents));
208 } else {
209 EXPECT_TRUE(WaitForVideoToPlay(tab_contents));
210 }
211
212 // Clean up settings for the following tests.
213 settings_map->ClearSettingsForOneType(ContentSettingsType::MEDIASTREAM_MIC);
214 settings_map->ClearSettingsForOneType(
215 ContentSettingsType::MEDIASTREAM_CAMERA);
216 }
217 }
218