Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / chrome_media_stream_infobar_browsertest.cc
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/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/common/content_settings_types.h"
21 #include "chrome/test/base/in_process_browser_test.h"
22 #include "chrome/test/base/test_switches.h"
23 #include "chrome/test/base/ui_test_utils.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 "net/test/spawned_test_server/spawned_test_server.h"
29
30
31 // MediaStreamInfoBarTest -----------------------------------------------------
32
33 class MediaStreamInfoBarTest : public WebRtcTestBase {
34  public:
35   MediaStreamInfoBarTest() {}
36   virtual ~MediaStreamInfoBarTest() {}
37
38   // InProcessBrowserTest:
39   virtual void SetUpCommandLine(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!";
44   }
45
46  protected:
47   content::WebContents* LoadTestPageInTab() {
48     return LoadTestPageInBrowser(browser());
49   }
50
51   content::WebContents* LoadTestPageInIncognitoTab() {
52     return LoadTestPageInBrowser(CreateIncognitoBrowser());
53   }
54
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);
60   }
61
62   // Denies getUserMedia requests (audio, video) for the test page.
63   // The deny setting is sticky.
64   void DenyRequest(content::WebContents* tab_contents,
65                    content::MediaStreamRequestResult result) const {
66     const std::string no_id;
67     content::MediaStreamRequest request(
68         0, 0, 0, test_page_url().GetOrigin(), false,
69         content::MEDIA_DEVICE_ACCESS, no_id, no_id,
70         content::MEDIA_DEVICE_AUDIO_CAPTURE,
71         content::MEDIA_DEVICE_VIDEO_CAPTURE);
72
73     scoped_ptr<MediaStreamDevicesController> controller(
74         new MediaStreamDevicesController(tab_contents, request,
75             base::Bind(&OnMediaStreamResponse)));
76     controller->Deny(true, result);
77   }
78
79   // Executes stopLocalStream() in the test page, which frees up an already
80   // acquired mediastream.
81   bool StopLocalStream(content::WebContents* tab_contents) {
82     std::string result;
83     bool ok = content::ExecuteScriptAndExtractString(
84         tab_contents, "stopLocalStream()", &result);
85     DCHECK(ok);
86     return result.compare("ok-stopped") == 0;
87   }
88
89  private:
90   content::WebContents* LoadTestPageInBrowser(Browser* browser) {
91     EXPECT_TRUE(test_server()->Start());
92
93     ui_test_utils::NavigateToURL(browser, test_page_url());
94     return browser->tab_strip_model()->GetActiveWebContents();
95   }
96
97   // Dummy callback for when we deny the current request directly.
98   static void OnMediaStreamResponse(const content::MediaStreamDevices& devices,
99                                     content::MediaStreamRequestResult result,
100                                     scoped_ptr<content::MediaStreamUI> ui) {}
101
102   DISALLOW_COPY_AND_ASSIGN(MediaStreamInfoBarTest);
103 };
104
105 // Actual tests ---------------------------------------------------------------
106
107 // Failing on ChromiumOS Debug and Win Aura, so disabling on both.
108 // See http://crbug.com/263333.
109 #if (defined(OS_CHROMEOS) && !defined(NDEBUG)) || defined(USE_AURA)
110 #define MAYBE_TestAllowingUserMedia DISABLED_TestAllowingUserMedia
111 #else
112 #define MAYBE_TestAllowingUserMedia TestAllowingUserMedia
113 #endif
114 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, MAYBE_TestAllowingUserMedia) {
115   content::WebContents* tab_contents = LoadTestPageInTab();
116   GetUserMediaAndAccept(tab_contents);
117 }
118
119 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDenyingUserMedia) {
120   content::WebContents* tab_contents = LoadTestPageInTab();
121   GetUserMediaAndDeny(tab_contents);
122 }
123
124 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDismissingInfobar) {
125   content::WebContents* tab_contents = LoadTestPageInTab();
126   GetUserMediaAndDismiss(tab_contents);
127 }
128
129 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDenyingUserMediaIncognito) {
130   content::WebContents* tab_contents = LoadTestPageInIncognitoTab();
131   GetUserMediaAndDeny(tab_contents);
132 }
133
134 // Failing on ChromiumOS Debug and Win Aura, so disabling on Aura.
135 // See http://crbug.com/263333.
136 #if defined(USE_AURA)
137 #define MAYBE_TestAcceptThenDenyWhichShouldBeSticky \
138   DISABLED_TestAcceptThenDenyWhichShouldBeSticky
139 #else
140 #define MAYBE_TestAcceptThenDenyWhichShouldBeSticky \
141   TestAcceptThenDenyWhichShouldBeSticky
142 #endif
143 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
144                        MAYBE_TestAcceptThenDenyWhichShouldBeSticky) {
145 #if defined(OS_WIN) && defined(USE_ASH)
146   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
147   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
148     return;
149 #endif
150
151   content::WebContents* tab_contents = LoadTestPageInTab();
152
153   GetUserMediaAndAccept(tab_contents);
154   DenyRequest(tab_contents, content::MEDIA_DEVICE_PERMISSION_DENIED);
155
156   // Should fail with permission denied right away with no infobar popping up.
157   GetUserMedia(tab_contents, kAudioVideoCallConstraints);
158   EXPECT_TRUE(test::PollingWaitUntil("obtainGetUserMediaResult()",
159                                      kFailedWithPermissionDeniedError,
160                                      tab_contents));
161   InfoBarService* infobar_service =
162       InfoBarService::FromWebContents(tab_contents);
163   EXPECT_EQ(0u, infobar_service->infobar_count());
164 }
165
166 // Failing on Win Aura, so disabling on that.
167 // See http://crbug.com/263333.
168 #if defined(USE_AURA)
169 #define MAYBE_TestAcceptIsNotSticky DISABLED_TestAcceptIsNotSticky
170 #else
171 #define MAYBE_TestAcceptIsNotSticky TestAcceptIsNotSticky
172 #endif
173 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, MAYBE_TestAcceptIsNotSticky) {
174   content::WebContents* tab_contents = LoadTestPageInTab();
175
176   // If accept were sticky the second call would hang because it hangs if an
177   // infobar does not pop up.
178   GetUserMediaAndAccept(tab_contents);
179
180   // Because http request permissions are sticky per navigation, we need to
181   // navigate away from the current page in order to verify that the granted
182   // permissions are not permanently sticky.
183   ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(),
184       GURL("about:blank"), 1);
185
186   // Now navigate back to our test page.
187   ui_test_utils::NavigateToURL(browser(), test_page_url());
188   tab_contents = browser()->tab_strip_model()->GetActiveWebContents();
189
190   GetUserMediaAndAccept(tab_contents);
191 }
192
193 // Test that accepting one getUserMedia request will not require a second
194 // prompt when issuing a second getUserMedia request.
195 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
196                        TestAcceptIsStickyPerNavigation) {
197   content::WebContents* tab_contents = LoadTestPageInTab();
198
199   GetUserMediaAndAccept(tab_contents);
200
201   // Before issuing the second gUM request, make sure we first stop the tracks
202   // we started with the first request. If they're still running the permissions
203   // will be active for other reasons and we won't be testing the temporary
204   // stickiness properly.
205   EXPECT_TRUE(StopLocalStream(tab_contents));
206
207   // Now no media tracks are running, so let's issue the second request.
208   GetUserMedia(tab_contents, kAudioVideoCallConstraints);
209 }
210
211 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
212                        TestTwoAcceptsPlusStickyPerNavigation) {
213   content::WebContents* tab_contents = LoadTestPageInTab();
214
215   // First ask for audio only and approve.
216   GetUserMediaWithSpecificConstraintsAndAccept(tab_contents,
217                                                kAudioOnlyCallConstraints);
218   EXPECT_TRUE(StopLocalStream(tab_contents));
219
220   // Next ask for video permissions.
221   // This will hang if the previous gUM call somehow gave video permissions.
222   GetUserMediaWithSpecificConstraintsAndAccept(tab_contents,
223                                                kVideoOnlyCallConstraints);
224   EXPECT_TRUE(StopLocalStream(tab_contents));
225
226   // Now ask for both audio and video and expect the call to go through without
227   // showing any UI.
228   GetUserMedia(tab_contents, kAudioVideoCallConstraints);
229 }
230
231 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest, TestDismissIsNotSticky) {
232   content::WebContents* tab_contents = LoadTestPageInTab();
233
234   // If dismiss were sticky the second call would hang because it hangs if an
235   // infobar does not pop up.
236   GetUserMediaAndDismiss(tab_contents);
237   GetUserMediaAndDismiss(tab_contents);
238 }
239
240 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
241                        TestDenyingThenClearingStickyException) {
242   content::WebContents* tab_contents = LoadTestPageInTab();
243
244   GetUserMediaAndDeny(tab_contents);
245
246   HostContentSettingsMap* settings_map =
247       browser()->profile()->GetHostContentSettingsMap();
248
249   settings_map->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
250   settings_map->ClearSettingsForOneType(
251       CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
252
253   // If an infobar is not launched now, this will hang.
254   GetUserMediaAndDeny(tab_contents);
255 }
256
257 // Times out on win debug builds; http://crbug.com/295723 .
258 #if defined(OS_WIN) && !defined(NDEBUG)
259 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
260         DISABLED_DenyingMicDoesNotCauseStickyDenyForCameras
261 #else
262 #define MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras \
263         DenyingMicDoesNotCauseStickyDenyForCameras
264 #endif
265 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
266                        MAYBE_DenyingMicDoesNotCauseStickyDenyForCameras) {
267   content::WebContents* tab_contents = LoadTestPageInTab();
268
269   // If mic blocking also blocked cameras, the second call here would hang.
270   GetUserMediaWithSpecificConstraintsAndDeny(tab_contents,
271                                              kAudioOnlyCallConstraints);
272   GetUserMediaWithSpecificConstraintsAndAccept(tab_contents,
273                                                kVideoOnlyCallConstraints);
274 }
275
276 #if defined(OS_CHROMEOS) && !defined(NDEBUG)
277 #define MAYBE_DenyingCameraDoesNotCauseStickyDenyForMics \
278   DISABLED_DenyingCameraDoesNotCauseStickyDenyForMics
279 #else
280 #define MAYBE_DenyingCameraDoesNotCauseStickyDenyForMics \
281   DenyingCameraDoesNotCauseStickyDenyForMics
282 #endif
283 IN_PROC_BROWSER_TEST_F(MediaStreamInfoBarTest,
284                        MAYBE_DenyingCameraDoesNotCauseStickyDenyForMics) {
285   content::WebContents* tab_contents = LoadTestPageInTab();
286
287   // If camera blocking also blocked mics, the second call here would hang.
288   GetUserMediaWithSpecificConstraintsAndDeny(tab_contents,
289                                              kVideoOnlyCallConstraints);
290   GetUserMediaWithSpecificConstraintsAndAccept(tab_contents,
291                                                kAudioOnlyCallConstraints);
292 }