OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/test/test_timeouts.h" | |
9 #include "base/time/time.h" | |
10 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
12 #include "chrome/browser/infobars/infobar.h" | 10 #include "chrome/browser/infobars/infobar.h" |
13 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
14 #include "chrome/browser/media/media_stream_infobar_delegate.h" | 12 #include "chrome/browser/media/webrtc_browsertest_base.h" |
15 #include "chrome/browser/media/webrtc_browsertest_common.h" | 13 #include "chrome/browser/media/webrtc_browsertest_common.h" |
16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
20 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/content_settings_types.h" | 19 #include "chrome/common/content_settings_types.h" |
22 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
24 #include "chrome/test/ui/ui_test.h" | 22 #include "chrome/test/ui/ui_test.h" |
25 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
26 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
27 #include "net/test/spawned_test_server/spawned_test_server.h" | 25 #include "net/test/spawned_test_server/spawned_test_server.h" |
28 | 26 |
29 #if defined(OS_WIN) && defined(USE_ASH) | 27 #if defined(OS_WIN) && defined(USE_ASH) |
30 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
31 #endif | 29 #endif |
32 | 30 |
33 static const char kMainWebrtcTestHtmlPage[] = | 31 static const char kMainWebrtcTestHtmlPage[] = |
34 "files/webrtc/webrtc_jsep01_test.html"; | 32 "files/webrtc/webrtc_jsep01_test.html"; |
35 static const char kFailedWithErrorPermissionDenied[] = | |
36 "failed-with-error-PERMISSION_DENIED"; | |
37 | |
38 static const char kAudioVideoCallConstraints[] = "'{audio: true, video: true}'"; | |
39 static const char kAudioOnlyCallConstraints[] = "'{audio: true}'"; | |
40 static const char kVideoOnlyCallConstraints[] = "'{video: true}'"; | |
41 static const char kOkGotStream[] = "ok-got-stream"; | |
42 | 33 |
43 // Media stream infobar test for WebRTC. | 34 // Media stream infobar test for WebRTC. |
44 class MediaStreamInfobarTest : public InProcessBrowserTest { | 35 class MediaStreamInfobarTest : public WebrtcTestBase { |
45 public: | 36 public: |
46 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 37 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
47 // This test expects to run with fake devices but real UI. | 38 // This test expects to run with fake devices but real UI. |
48 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | 39 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); |
49 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)) | 40 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)) |
50 << "Since this test tests the UI we want the real UI!"; | 41 << "Since this test tests the UI we want the real UI!"; |
tommi (sloooow) - chröme
2013/07/25 14:09:09
fix indent (was previously ok)
phoglund_chromium
2013/07/25 15:22:46
Done.
| |
51 } | 42 } |
52 protected: | 43 protected: |
53 content::WebContents* LoadTestPageInTab() { | 44 content::WebContents* LoadTestPageInTab() { |
54 EXPECT_TRUE(test_server()->Start()); | 45 EXPECT_TRUE(test_server()->Start()); |
55 | 46 |
56 ui_test_utils::NavigateToURL( | 47 ui_test_utils::NavigateToURL( |
57 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage)); | 48 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage)); |
58 return browser()->tab_strip_model()->GetActiveWebContents(); | 49 return browser()->tab_strip_model()->GetActiveWebContents(); |
59 } | 50 } |
60 | |
61 // TODO(phoglund): upstream and reuse in other browser tests. | |
62 MediaStreamInfoBarDelegate* GetUserMediaAndWaitForInfobar( | |
63 content::WebContents* tab_contents, | |
64 const std::string& constraints) { | |
65 content::WindowedNotificationObserver infobar_added( | |
66 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
67 content::NotificationService::AllSources()); | |
68 | |
69 // Request user media: this will launch the media stream info bar. | |
70 GetUserMedia(constraints, tab_contents); | |
71 | |
72 // Wait for the bar to pop up, then return it | |
73 infobar_added.Wait(); | |
74 content::Details<InfoBarAddedDetails> details(infobar_added.details()); | |
75 MediaStreamInfoBarDelegate* media_infobar = | |
76 details.ptr()->AsMediaStreamInfoBarDelegate(); | |
77 return media_infobar; | |
78 } | |
79 | |
80 void CloseInfobarInTab(content::WebContents* tab_contents, | |
81 MediaStreamInfoBarDelegate* infobar) { | |
82 content::WindowedNotificationObserver infobar_removed( | |
83 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
84 content::NotificationService::AllSources()); | |
85 | |
86 InfoBarService* infobar_service = | |
87 InfoBarService::FromWebContents(tab_contents); | |
88 infobar_service->RemoveInfoBar(infobar); | |
89 | |
90 infobar_removed.Wait(); | |
91 } | |
92 | |
93 // Convenience method which executes the provided javascript in the context | |
94 // of the provided web contents and returns what it evaluated to. | |
95 std::string ExecuteJavascript(const std::string& javascript, | |
96 content::WebContents* tab_contents) { | |
97 std::string result; | |
98 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
99 tab_contents, javascript, &result)); | |
100 return result; | |
101 } | |
102 | |
103 void TestAcceptOnInfobar(content::WebContents* tab_contents) { | |
104 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | |
105 kAudioVideoCallConstraints); | |
106 } | |
107 | |
108 void TestAcceptOnInfobarWithSpecificConstraints( | |
109 content::WebContents* tab_contents, const std::string& constraints) { | |
110 MediaStreamInfoBarDelegate* media_infobar = | |
111 GetUserMediaAndWaitForInfobar(tab_contents, constraints); | |
112 | |
113 media_infobar->Accept(); | |
114 | |
115 CloseInfobarInTab(tab_contents, media_infobar); | |
116 | |
117 // Wait for WebRTC to call the success callback. | |
118 EXPECT_TRUE(PollingWaitUntil( | |
119 "obtainGetUserMediaResult()", kOkGotStream, tab_contents)); | |
120 } | |
121 | |
122 void TestDenyOnInfobar(content::WebContents* tab_contents) { | |
123 return TestDenyWithSpecificConstraints(tab_contents, | |
124 kAudioVideoCallConstraints); | |
125 } | |
126 | |
127 void TestDenyWithSpecificConstraints(content::WebContents* tab_contents, | |
128 const std::string& constraints) { | |
129 MediaStreamInfoBarDelegate* media_infobar = | |
130 GetUserMediaAndWaitForInfobar(tab_contents, constraints); | |
131 | |
132 media_infobar->Cancel(); | |
133 | |
134 CloseInfobarInTab(tab_contents, media_infobar); | |
135 | |
136 // Wait for WebRTC to call the fail callback. | |
137 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", | |
138 kFailedWithErrorPermissionDenied, | |
139 tab_contents)); | |
140 } | |
141 | |
142 void TestDismissOnInfobar(content::WebContents* tab_contents) { | |
143 MediaStreamInfoBarDelegate* media_infobar = | |
144 GetUserMediaAndWaitForInfobar(tab_contents, kAudioVideoCallConstraints); | |
145 | |
146 media_infobar->InfoBarDismissed(); | |
147 | |
148 CloseInfobarInTab(tab_contents, media_infobar); | |
149 | |
150 // A dismiss should be treated like a deny. | |
151 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", | |
152 kFailedWithErrorPermissionDenied, | |
153 tab_contents)); | |
154 } | |
155 | |
156 void GetUserMedia(const std::string& constraints, | |
157 content::WebContents* tab_contents) { | |
158 // Request user media: this will launch the media stream info bar. | |
159 EXPECT_EQ("ok-requested", | |
160 ExecuteJavascript( | |
161 base::StringPrintf("getUserMedia(%s);", constraints.c_str()), | |
162 tab_contents)); | |
163 } | |
164 }; | 51 }; |
165 | 52 |
166 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAllowingUserMedia) { | 53 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAllowingUserMedia) { |
167 content::WebContents* tab_contents = LoadTestPageInTab(); | 54 content::WebContents* tab_contents = LoadTestPageInTab(); |
168 TestAcceptOnInfobar(tab_contents); | 55 GetUserMediaAndAccept(tab_contents); |
169 } | 56 } |
170 | 57 |
171 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDenyingUserMedia) { | 58 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDenyingUserMedia) { |
172 content::WebContents* tab_contents = LoadTestPageInTab(); | 59 content::WebContents* tab_contents = LoadTestPageInTab(); |
173 TestDenyOnInfobar(tab_contents); | 60 GetUserMediaAndDeny(tab_contents); |
174 } | 61 } |
175 | 62 |
176 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDismissingInfobar) { | 63 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDismissingInfobar) { |
177 content::WebContents* tab_contents = LoadTestPageInTab(); | 64 content::WebContents* tab_contents = LoadTestPageInTab(); |
178 TestDismissOnInfobar(tab_contents); | 65 GetUserMediaAndDismiss(tab_contents); |
179 } | 66 } |
180 | 67 |
181 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, | 68 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, |
182 TestAcceptThenDenyWhichShouldBeSticky) { | 69 TestAcceptThenDenyWhichShouldBeSticky) { |
183 #if defined(OS_WIN) && defined(USE_ASH) | 70 #if defined(OS_WIN) && defined(USE_ASH) |
184 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 71 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
185 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 72 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
186 return; | 73 return; |
187 #endif | 74 #endif |
188 | 75 |
189 content::WebContents* tab_contents = LoadTestPageInTab(); | 76 content::WebContents* tab_contents = LoadTestPageInTab(); |
190 | 77 |
191 TestAcceptOnInfobar(tab_contents); | 78 GetUserMediaAndAccept(tab_contents); |
192 TestDenyOnInfobar(tab_contents); | 79 GetUserMediaAndDeny(tab_contents); |
193 | 80 |
194 // Should fail with permission denied right away with no infobar popping up. | 81 // Should fail with permission denied right away with no infobar popping up. |
195 GetUserMedia(kAudioVideoCallConstraints, tab_contents); | 82 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
196 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", | 83 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", |
197 kFailedWithErrorPermissionDenied, | 84 kFailedWithErrorPermissionDenied, |
198 tab_contents)); | 85 tab_contents)); |
199 InfoBarService* infobar_service = | 86 InfoBarService* infobar_service = |
200 InfoBarService::FromWebContents(tab_contents); | 87 InfoBarService::FromWebContents(tab_contents); |
201 EXPECT_EQ(0u, infobar_service->infobar_count()); | 88 EXPECT_EQ(0u, infobar_service->infobar_count()); |
202 } | 89 } |
203 | 90 |
204 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAcceptIsNotSticky) { | 91 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestAcceptIsNotSticky) { |
205 content::WebContents* tab_contents = LoadTestPageInTab(); | 92 content::WebContents* tab_contents = LoadTestPageInTab(); |
206 | 93 |
207 // If accept were sticky the second call would hang because it hangs if an | 94 // If accept were sticky the second call would hang because it hangs if an |
208 // infobar does not pop up. | 95 // infobar does not pop up. |
209 TestAcceptOnInfobar(tab_contents); | 96 GetUserMediaAndAccept(tab_contents); |
210 TestAcceptOnInfobar(tab_contents); | 97 GetUserMediaAndAccept(tab_contents); |
211 } | 98 } |
212 | 99 |
213 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDismissIsNotSticky) { | 100 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, TestDismissIsNotSticky) { |
214 content::WebContents* tab_contents = LoadTestPageInTab(); | 101 content::WebContents* tab_contents = LoadTestPageInTab(); |
215 | 102 |
216 // If dismiss were sticky the second call would hang because it hangs if an | 103 // If dismiss were sticky the second call would hang because it hangs if an |
217 // infobar does not pop up. | 104 // infobar does not pop up. |
218 TestDismissOnInfobar(tab_contents); | 105 GetUserMediaAndDismiss(tab_contents); |
219 TestDismissOnInfobar(tab_contents); | 106 GetUserMediaAndDismiss(tab_contents); |
220 } | 107 } |
221 | 108 |
222 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, | 109 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, |
223 TestDenyingThenClearingStickyException) { | 110 TestDenyingThenClearingStickyException) { |
224 content::WebContents* tab_contents = LoadTestPageInTab(); | 111 content::WebContents* tab_contents = LoadTestPageInTab(); |
225 | 112 |
226 TestDenyOnInfobar(tab_contents); | 113 GetUserMediaAndDeny(tab_contents); |
227 | 114 |
228 HostContentSettingsMap* settings_map = | 115 HostContentSettingsMap* settings_map = |
229 browser()->profile()->GetHostContentSettingsMap(); | 116 browser()->profile()->GetHostContentSettingsMap(); |
230 | 117 |
231 settings_map->ClearSettingsForOneType( | 118 settings_map->ClearSettingsForOneType( |
232 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | 119 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); |
233 settings_map->ClearSettingsForOneType( | 120 settings_map->ClearSettingsForOneType( |
234 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | 121 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
235 | 122 |
236 // If an infobar is not launched now, this will hang. | 123 // If an infobar is not launched now, this will hang. |
237 TestDenyOnInfobar(tab_contents); | 124 GetUserMediaAndDeny(tab_contents); |
238 } | 125 } |
239 | 126 |
240 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, | 127 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, |
241 DenyingMicDoesNotCauseStickyDenyForCameras) { | 128 DenyingMicDoesNotCauseStickyDenyForCameras) { |
242 content::WebContents* tab_contents = LoadTestPageInTab(); | 129 content::WebContents* tab_contents = LoadTestPageInTab(); |
243 | 130 |
244 // If mic blocking also blocked cameras, the second call here would hang. | 131 // If mic blocking also blocked cameras, the second call here would hang. |
245 TestDenyWithSpecificConstraints(tab_contents, kAudioOnlyCallConstraints); | 132 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
246 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | 133 kAudioOnlyCallConstraints); |
247 kVideoOnlyCallConstraints); | 134 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, |
135 kVideoOnlyCallConstraints); | |
248 } | 136 } |
249 | 137 |
250 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, | 138 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, |
251 DenyingCameraDoesNotCauseStickyDenyForMics) { | 139 DenyingCameraDoesNotCauseStickyDenyForMics) { |
252 content::WebContents* tab_contents = LoadTestPageInTab(); | 140 content::WebContents* tab_contents = LoadTestPageInTab(); |
253 | 141 |
254 // If camera blocking also blocked mics, the second call here would hang. | 142 // If camera blocking also blocked mics, the second call here would hang. |
255 TestDenyWithSpecificConstraints(tab_contents, kVideoOnlyCallConstraints); | 143 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
256 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | 144 kVideoOnlyCallConstraints); |
257 kAudioOnlyCallConstraints); | 145 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, |
146 kAudioOnlyCallConstraints); | |
258 } | 147 } |
259 | 148 |
260 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, | 149 IN_PROC_BROWSER_TEST_F(MediaStreamInfobarTest, |
261 DenyingMicStillSucceedsWithCameraForAudioVideoCalls) { | 150 DenyingMicStillSucceedsWithCameraForAudioVideoCalls) { |
262 content::WebContents* tab_contents = LoadTestPageInTab(); | 151 content::WebContents* tab_contents = LoadTestPageInTab(); |
263 | 152 |
264 // If microphone blocking also blocked a AV call, the second call here | 153 // If microphone blocking also blocked a AV call, the second call here |
265 // would hang. The requester should only be granted access to the cam though. | 154 // would hang. The requester should only be granted access to the cam though. |
266 TestDenyWithSpecificConstraints(tab_contents, kAudioOnlyCallConstraints); | 155 GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
267 TestAcceptOnInfobarWithSpecificConstraints(tab_contents, | 156 kAudioOnlyCallConstraints); |
268 kAudioVideoCallConstraints); | 157 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, |
158 kAudioVideoCallConstraints); | |
269 | 159 |
270 // TODO(phoglund): verify the requester actually only gets video tracks. | 160 // TODO(phoglund): verify the requester actually only gets video tracks. |
271 } | 161 } |
OLD | NEW |