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 "chrome/browser/media/webrtc_browsertest_base.h" | 5 #include "chrome/browser/media/webrtc_browsertest_base.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/media/webrtc_browsertest_common.h" | 10 #include "chrome/browser/media/webrtc_browsertest_common.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 | 71 |
72 bool contains_uncaught = str.find("\"Uncaught ") != std::string::npos; | 72 bool contains_uncaught = str.find("\"Uncaught ") != std::string::npos; |
73 if (severity == logging::LOG_ERROR || | 73 if (severity == logging::LOG_ERROR || |
74 (severity == logging::LOG_INFO && contains_uncaught)) { | 74 (severity == logging::LOG_INFO && contains_uncaught)) { |
75 hit_javascript_errors_.Get() = true; | 75 hit_javascript_errors_.Get() = true; |
76 } | 76 } |
77 | 77 |
78 return false; | 78 return false; |
79 } | 79 } |
80 | 80 |
81 // PermissionRequestObserver --------------------------------------------------- | |
82 | |
83 // Used to observe the creation of permission prompt without responding. | |
84 class PermissionRequestObserver : public PermissionBubbleManager::Observer { | |
85 public: | |
86 explicit PermissionRequestObserver(content::WebContents* web_contents) | |
87 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)), | |
88 request_shown_(false), | |
89 message_loop_runner_(new content::MessageLoopRunner) { | |
90 bubble_manager_->AddObserver(this); | |
91 } | |
92 ~PermissionRequestObserver() override { | |
93 // Safe to remove twice if it happens. | |
94 bubble_manager_->RemoveObserver(this); | |
95 } | |
96 | |
97 void Wait() { message_loop_runner_->Run(); } | |
98 | |
99 bool request_shown() { return request_shown_; } | |
tommi (sloooow) - chröme
2015/08/13 13:03:17
nit: const?
| |
100 | |
101 private: | |
102 // PermissionBubbleManager::Observer | |
103 void OnBubbleAdded() override { | |
104 request_shown_ = true; | |
105 bubble_manager_->RemoveObserver(this); | |
106 message_loop_runner_->Quit(); | |
107 } | |
108 | |
109 PermissionBubbleManager* bubble_manager_; | |
110 bool request_shown_; | |
111 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | |
114 }; | |
115 | |
81 } // namespace | 116 } // namespace |
82 | 117 |
83 WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) { | 118 WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) { |
84 // The handler gets set for each test method, but that's fine since this | 119 // The handler gets set for each test method, but that's fine since this |
85 // set operation is idempotent. | 120 // set operation is idempotent. |
86 logging::SetLogMessageHandler(&JavascriptErrorDetectingLogHandler); | 121 logging::SetLogMessageHandler(&JavascriptErrorDetectingLogHandler); |
87 hit_javascript_errors_.Get() = false; | 122 hit_javascript_errors_.Get() = false; |
88 | 123 |
89 EnablePixelOutput(); | 124 EnablePixelOutput(); |
90 } | 125 } |
(...skipping 11 matching lines...) Expand all Loading... | |
102 return GetUserMediaWithSpecificConstraintsAndAccept( | 137 return GetUserMediaWithSpecificConstraintsAndAccept( |
103 tab_contents, kAudioVideoCallConstraints); | 138 tab_contents, kAudioVideoCallConstraints); |
104 } | 139 } |
105 | 140 |
106 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( | 141 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( |
107 content::WebContents* tab_contents, | 142 content::WebContents* tab_contents, |
108 const std::string& constraints) const { | 143 const std::string& constraints) const { |
109 std::string result; | 144 std::string result; |
110 PermissionBubbleManager::FromWebContents(tab_contents) | 145 PermissionBubbleManager::FromWebContents(tab_contents) |
111 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | 146 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); |
147 PermissionRequestObserver permissionRequestObserver(tab_contents); | |
148 GetUserMedia(tab_contents, constraints); | |
149 EXPECT_TRUE(permissionRequestObserver.request_shown()); | |
150 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
151 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | |
152 return kOkGotStream == result; | |
153 } | |
154 | |
155 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted( | |
156 content::WebContents* tab_contents, | |
157 const std::string& constraints) const { | |
158 std::string result; | |
159 PermissionBubbleManager::FromWebContents(tab_contents) | |
160 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | |
112 GetUserMedia(tab_contents, constraints); | 161 GetUserMedia(tab_contents, constraints); |
113 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 162 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
114 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 163 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
115 return kOkGotStream == result; | 164 return kOkGotStream == result; |
116 } | 165 } |
117 | 166 |
118 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { | 167 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { |
119 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, | 168 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
120 kAudioVideoCallConstraints); | 169 kAudioVideoCallConstraints); |
121 } | 170 } |
122 | 171 |
123 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( | 172 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( |
124 content::WebContents* tab_contents, | 173 content::WebContents* tab_contents, |
125 const std::string& constraints) const { | 174 const std::string& constraints) const { |
126 std::string result; | 175 std::string result; |
127 PermissionBubbleManager::FromWebContents(tab_contents) | 176 PermissionBubbleManager::FromWebContents(tab_contents) |
128 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); | 177 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); |
178 PermissionRequestObserver permissionRequestObserver(tab_contents); | |
129 GetUserMedia(tab_contents, constraints); | 179 GetUserMedia(tab_contents, constraints); |
180 EXPECT_TRUE(permissionRequestObserver.request_shown()); | |
130 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 181 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
131 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 182 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
132 EXPECT_EQ(kFailedWithPermissionDeniedError, result); | 183 EXPECT_EQ(kFailedWithPermissionDeniedError, result); |
133 } | 184 } |
134 | 185 |
135 void WebRtcTestBase::GetUserMediaAndDismiss( | 186 void WebRtcTestBase::GetUserMediaAndDismiss( |
136 content::WebContents* tab_contents) const { | 187 content::WebContents* tab_contents) const { |
137 std::string result; | 188 std::string result; |
138 PermissionBubbleManager::FromWebContents(tab_contents) | 189 PermissionBubbleManager::FromWebContents(tab_contents) |
139 ->set_auto_response_for_test(PermissionBubbleManager::DISMISS); | 190 ->set_auto_response_for_test(PermissionBubbleManager::DISMISS); |
191 PermissionRequestObserver permissionRequestObserver(tab_contents); | |
140 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | 192 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
193 EXPECT_TRUE(permissionRequestObserver.request_shown()); | |
141 // A dismiss should be treated like a deny. | 194 // A dismiss should be treated like a deny. |
142 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 195 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
143 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 196 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
144 EXPECT_EQ(kFailedWithPermissionDismissedError, result); | 197 EXPECT_EQ(kFailedWithPermissionDismissedError, result); |
145 } | 198 } |
146 | 199 |
200 void WebRtcTestBase::GetUserMediaAndExpectAutoAcceptWithoutPrompt( | |
201 content::WebContents* tab_contents) const { | |
202 std::string result; | |
203 // We issue a GetUserMedia() request. We expect that the origin already has a | |
204 // sticky "accept" permission (e.g. because the caller previously called | |
205 // GetUserMediaAndAccept()), and therefore the GetUserMedia() request | |
206 // automatically succeeds without a prompt. | |
207 // If the caller made a mistake, a prompt may show up instead. For this case, | |
208 // we set an auto-response to avoid leaving the prompt hanging. The choice of | |
209 // DENY_ALL makes sure that the response to the prompt doesn't accidentally | |
210 // result in a newly granted media stream permission. | |
211 PermissionBubbleManager::FromWebContents(tab_contents) | |
212 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); | |
213 PermissionRequestObserver permissionRequestObserver(tab_contents); | |
214 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | |
215 EXPECT_FALSE(permissionRequestObserver.request_shown()); | |
216 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
217 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | |
218 EXPECT_EQ(kOkGotStream, result); | |
219 } | |
220 | |
221 void WebRtcTestBase::GetUserMediaAndExpectAutoDenyWithoutPrompt( | |
222 content::WebContents* tab_contents) const { | |
223 std::string result; | |
224 // We issue a GetUserMedia() request. We expect that the origin already has a | |
225 // sticky "deny" permission (e.g. because the caller previously called | |
226 // GetUserMediaAndDeny()), and therefore the GetUserMedia() request | |
227 // automatically succeeds without a prompt. | |
228 // If the caller made a mistake, a prompt may show up instead. For this case, | |
229 // we set an auto-response to avoid leaving the prompt hanging. The choice of | |
230 // ACCEPT_ALL makes sure that the response to the prompt doesn't accidentally | |
231 // result in a newly granted media stream permission. | |
232 PermissionBubbleManager::FromWebContents(tab_contents) | |
233 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | |
234 PermissionRequestObserver permissionRequestObserver(tab_contents); | |
235 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | |
236 EXPECT_FALSE(permissionRequestObserver.request_shown()); | |
237 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
238 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | |
239 EXPECT_EQ(kFailedWithPermissionDeniedError, result); | |
240 } | |
241 | |
147 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, | 242 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, |
148 const std::string& constraints) const { | 243 const std::string& constraints) const { |
149 // Request user media: this will launch the media stream info bar or bubble. | 244 // Request user media: this will launch the media stream info bar or bubble. |
150 std::string result; | 245 std::string result; |
151 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 246 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
152 tab_contents, "doGetUserMedia(" + constraints + ");", &result)); | 247 tab_contents, "doGetUserMedia(" + constraints + ");", &result)); |
153 EXPECT_TRUE(result == "request-callback-denied" || | 248 EXPECT_TRUE(result == "request-callback-denied" || |
154 result == "request-callback-granted"); | 249 result == "request-callback-granted"); |
155 } | 250 } |
156 | 251 |
157 content::WebContents* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab( | 252 content::WebContents* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab( |
158 const GURL& url) const { | 253 const GURL& url) const { |
159 return OpenPageAndGetUserMediaInNewTabWithConstraints( | 254 return OpenPageAndGetUserMediaInNewTabWithConstraints( |
160 url, kAudioVideoCallConstraints); | 255 url, kAudioVideoCallConstraints); |
161 } | 256 } |
162 | 257 |
163 content::WebContents* | 258 content::WebContents* |
164 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( | 259 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( |
165 const GURL& url, | 260 const GURL& url, |
166 const std::string& constraints) const { | 261 const std::string& constraints) const { |
167 chrome::AddTabAt(browser(), GURL(), -1, true); | 262 chrome::AddTabAt(browser(), GURL(), -1, true); |
168 ui_test_utils::NavigateToURL(browser(), url); | 263 ui_test_utils::NavigateToURL(browser(), url); |
169 content::WebContents* new_tab = | 264 content::WebContents* new_tab = |
170 browser()->tab_strip_model()->GetActiveWebContents(); | 265 browser()->tab_strip_model()->GetActiveWebContents(); |
171 EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAccept( | 266 // Accept if necessary, but don't expect a prompt (because auto-accept is also |
172 new_tab, constraints)); | 267 // okay). |
268 PermissionBubbleManager::FromWebContents(new_tab) | |
269 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | |
270 GetUserMedia(new_tab, constraints); | |
271 std::string result; | |
272 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
273 new_tab->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | |
274 EXPECT_EQ(kOkGotStream, result); | |
173 return new_tab; | 275 return new_tab; |
174 } | 276 } |
175 | 277 |
176 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( | 278 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( |
177 const std::string& test_page) const { | 279 const std::string& test_page) const { |
178 return OpenPageAndGetUserMediaInNewTab( | 280 return OpenPageAndGetUserMediaInNewTab( |
179 embedded_test_server()->GetURL(test_page)); | 281 embedded_test_server()->GetURL(test_page)); |
180 } | 282 } |
181 | 283 |
182 void WebRtcTestBase::CloseLastLocalStream( | 284 void WebRtcTestBase::CloseLastLocalStream( |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 #endif | 413 #endif |
312 } | 414 } |
313 | 415 |
314 bool WebRtcTestBase::OnWin8() const { | 416 bool WebRtcTestBase::OnWin8() const { |
315 #if defined(OS_WIN) | 417 #if defined(OS_WIN) |
316 return base::win::GetVersion() > base::win::VERSION_WIN7; | 418 return base::win::GetVersion() > base::win::VERSION_WIN7; |
317 #else | 419 #else |
318 return false; | 420 return false; |
319 #endif | 421 #endif |
320 } | 422 } |
OLD | NEW |