| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/browser/media/webrtc_browsertest_common.h" | 14 #include "chrome/browser/media/webrtc_browsertest_common.h" |
| 15 #include "chrome/browser/permissions/permission_request_manager.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | |
| 19 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 | 22 |
| 23 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 24 // For fine-grained suppression. | 24 // For fine-grained suppression. |
| 25 #include "base/win/windows_version.h" | 25 #include "base/win/windows_version.h" |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 const char WebRtcTestBase::kAudioVideoCallConstraints[] = | 28 const char WebRtcTestBase::kAudioVideoCallConstraints[] = |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 (severity == logging::LOG_INFO && contains_uncaught)) { | 80 (severity == logging::LOG_INFO && contains_uncaught)) { |
| 81 hit_javascript_errors_.Get() = true; | 81 hit_javascript_errors_.Get() = true; |
| 82 } | 82 } |
| 83 | 83 |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 | 86 |
| 87 // PermissionRequestObserver --------------------------------------------------- | 87 // PermissionRequestObserver --------------------------------------------------- |
| 88 | 88 |
| 89 // Used to observe the creation of permission prompt without responding. | 89 // Used to observe the creation of permission prompt without responding. |
| 90 class PermissionRequestObserver : public PermissionBubbleManager::Observer { | 90 class PermissionRequestObserver : public PermissionRequestManager::Observer { |
| 91 public: | 91 public: |
| 92 explicit PermissionRequestObserver(content::WebContents* web_contents) | 92 explicit PermissionRequestObserver(content::WebContents* web_contents) |
| 93 : bubble_manager_(PermissionBubbleManager::FromWebContents(web_contents)), | 93 : request_manager_( |
| 94 PermissionRequestManager::FromWebContents(web_contents)), |
| 94 request_shown_(false), | 95 request_shown_(false), |
| 95 message_loop_runner_(new content::MessageLoopRunner) { | 96 message_loop_runner_(new content::MessageLoopRunner) { |
| 96 bubble_manager_->AddObserver(this); | 97 request_manager_->AddObserver(this); |
| 97 } | 98 } |
| 98 ~PermissionRequestObserver() override { | 99 ~PermissionRequestObserver() override { |
| 99 // Safe to remove twice if it happens. | 100 // Safe to remove twice if it happens. |
| 100 bubble_manager_->RemoveObserver(this); | 101 request_manager_->RemoveObserver(this); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void Wait() { message_loop_runner_->Run(); } | 104 void Wait() { message_loop_runner_->Run(); } |
| 104 | 105 |
| 105 bool request_shown() const { return request_shown_; } | 106 bool request_shown() const { return request_shown_; } |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 // PermissionBubbleManager::Observer | 109 // PermissionRequestManager::Observer |
| 109 void OnBubbleAdded() override { | 110 void OnBubbleAdded() override { |
| 110 request_shown_ = true; | 111 request_shown_ = true; |
| 111 bubble_manager_->RemoveObserver(this); | 112 request_manager_->RemoveObserver(this); |
| 112 message_loop_runner_->Quit(); | 113 message_loop_runner_->Quit(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 PermissionBubbleManager* bubble_manager_; | 116 PermissionRequestManager* request_manager_; |
| 116 bool request_shown_; | 117 bool request_shown_; |
| 117 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 118 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 118 | 119 |
| 119 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); | 120 DISALLOW_COPY_AND_ASSIGN(PermissionRequestObserver); |
| 120 }; | 121 }; |
| 121 | 122 |
| 122 } // namespace | 123 } // namespace |
| 123 | 124 |
| 124 WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) { | 125 WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) { |
| 125 // The handler gets set for each test method, but that's fine since this | 126 // The handler gets set for each test method, but that's fine since this |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 bool WebRtcTestBase::GetUserMediaAndAccept( | 142 bool WebRtcTestBase::GetUserMediaAndAccept( |
| 142 content::WebContents* tab_contents) const { | 143 content::WebContents* tab_contents) const { |
| 143 return GetUserMediaWithSpecificConstraintsAndAccept( | 144 return GetUserMediaWithSpecificConstraintsAndAccept( |
| 144 tab_contents, kAudioVideoCallConstraints); | 145 tab_contents, kAudioVideoCallConstraints); |
| 145 } | 146 } |
| 146 | 147 |
| 147 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( | 148 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( |
| 148 content::WebContents* tab_contents, | 149 content::WebContents* tab_contents, |
| 149 const std::string& constraints) const { | 150 const std::string& constraints) const { |
| 150 std::string result; | 151 std::string result; |
| 151 PermissionBubbleManager::FromWebContents(tab_contents) | 152 PermissionRequestManager::FromWebContents(tab_contents) |
| 152 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | 153 ->set_auto_response_for_test(PermissionRequestManager::ACCEPT_ALL); |
| 153 PermissionRequestObserver permissionRequestObserver(tab_contents); | 154 PermissionRequestObserver permissionRequestObserver(tab_contents); |
| 154 GetUserMedia(tab_contents, constraints); | 155 GetUserMedia(tab_contents, constraints); |
| 155 EXPECT_TRUE(permissionRequestObserver.request_shown()); | 156 EXPECT_TRUE(permissionRequestObserver.request_shown()); |
| 156 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 157 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 157 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 158 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 158 return kOkGotStream == result; | 159 return kOkGotStream == result; |
| 159 } | 160 } |
| 160 | 161 |
| 161 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted( | 162 bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted( |
| 162 content::WebContents* tab_contents, | 163 content::WebContents* tab_contents, |
| 163 const std::string& constraints) const { | 164 const std::string& constraints) const { |
| 164 std::string result; | 165 std::string result; |
| 165 PermissionBubbleManager::FromWebContents(tab_contents) | 166 PermissionRequestManager::FromWebContents(tab_contents) |
| 166 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | 167 ->set_auto_response_for_test(PermissionRequestManager::ACCEPT_ALL); |
| 167 GetUserMedia(tab_contents, constraints); | 168 GetUserMedia(tab_contents, constraints); |
| 168 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 169 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 169 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 170 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 170 return kOkGotStream == result; | 171 return kOkGotStream == result; |
| 171 } | 172 } |
| 172 | 173 |
| 173 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { | 174 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { |
| 174 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, | 175 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
| 175 kAudioVideoCallConstraints); | 176 kAudioVideoCallConstraints); |
| 176 } | 177 } |
| 177 | 178 |
| 178 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( | 179 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( |
| 179 content::WebContents* tab_contents, | 180 content::WebContents* tab_contents, |
| 180 const std::string& constraints) const { | 181 const std::string& constraints) const { |
| 181 std::string result; | 182 std::string result; |
| 182 PermissionBubbleManager::FromWebContents(tab_contents) | 183 PermissionRequestManager::FromWebContents(tab_contents) |
| 183 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); | 184 ->set_auto_response_for_test(PermissionRequestManager::DENY_ALL); |
| 184 PermissionRequestObserver permissionRequestObserver(tab_contents); | 185 PermissionRequestObserver permissionRequestObserver(tab_contents); |
| 185 GetUserMedia(tab_contents, constraints); | 186 GetUserMedia(tab_contents, constraints); |
| 186 EXPECT_TRUE(permissionRequestObserver.request_shown()); | 187 EXPECT_TRUE(permissionRequestObserver.request_shown()); |
| 187 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 188 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 188 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 189 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 189 EXPECT_EQ(kFailedWithPermissionDeniedError, result); | 190 EXPECT_EQ(kFailedWithPermissionDeniedError, result); |
| 190 } | 191 } |
| 191 | 192 |
| 192 void WebRtcTestBase::GetUserMediaAndDismiss( | 193 void WebRtcTestBase::GetUserMediaAndDismiss( |
| 193 content::WebContents* tab_contents) const { | 194 content::WebContents* tab_contents) const { |
| 194 std::string result; | 195 std::string result; |
| 195 PermissionBubbleManager::FromWebContents(tab_contents) | 196 PermissionRequestManager::FromWebContents(tab_contents) |
| 196 ->set_auto_response_for_test(PermissionBubbleManager::DISMISS); | 197 ->set_auto_response_for_test(PermissionRequestManager::DISMISS); |
| 197 PermissionRequestObserver permissionRequestObserver(tab_contents); | 198 PermissionRequestObserver permissionRequestObserver(tab_contents); |
| 198 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | 199 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
| 199 EXPECT_TRUE(permissionRequestObserver.request_shown()); | 200 EXPECT_TRUE(permissionRequestObserver.request_shown()); |
| 200 // A dismiss should be treated like a deny. | 201 // A dismiss should be treated like a deny. |
| 201 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 202 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 202 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 203 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 203 EXPECT_EQ(kFailedWithPermissionDismissedError, result); | 204 EXPECT_EQ(kFailedWithPermissionDismissedError, result); |
| 204 } | 205 } |
| 205 | 206 |
| 206 void WebRtcTestBase::GetUserMediaAndExpectAutoAcceptWithoutPrompt( | 207 void WebRtcTestBase::GetUserMediaAndExpectAutoAcceptWithoutPrompt( |
| 207 content::WebContents* tab_contents) const { | 208 content::WebContents* tab_contents) const { |
| 208 std::string result; | 209 std::string result; |
| 209 // We issue a GetUserMedia() request. We expect that the origin already has a | 210 // We issue a GetUserMedia() request. We expect that the origin already has a |
| 210 // sticky "accept" permission (e.g. because the caller previously called | 211 // sticky "accept" permission (e.g. because the caller previously called |
| 211 // GetUserMediaAndAccept()), and therefore the GetUserMedia() request | 212 // GetUserMediaAndAccept()), and therefore the GetUserMedia() request |
| 212 // automatically succeeds without a prompt. | 213 // automatically succeeds without a prompt. |
| 213 // If the caller made a mistake, a prompt may show up instead. For this case, | 214 // If the caller made a mistake, a prompt may show up instead. For this case, |
| 214 // we set an auto-response to avoid leaving the prompt hanging. The choice of | 215 // we set an auto-response to avoid leaving the prompt hanging. The choice of |
| 215 // DENY_ALL makes sure that the response to the prompt doesn't accidentally | 216 // DENY_ALL makes sure that the response to the prompt doesn't accidentally |
| 216 // result in a newly granted media stream permission. | 217 // result in a newly granted media stream permission. |
| 217 PermissionBubbleManager::FromWebContents(tab_contents) | 218 PermissionRequestManager::FromWebContents(tab_contents) |
| 218 ->set_auto_response_for_test(PermissionBubbleManager::DENY_ALL); | 219 ->set_auto_response_for_test(PermissionRequestManager::DENY_ALL); |
| 219 PermissionRequestObserver permissionRequestObserver(tab_contents); | 220 PermissionRequestObserver permissionRequestObserver(tab_contents); |
| 220 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | 221 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
| 221 EXPECT_FALSE(permissionRequestObserver.request_shown()); | 222 EXPECT_FALSE(permissionRequestObserver.request_shown()); |
| 222 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 223 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 223 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 224 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 224 EXPECT_EQ(kOkGotStream, result); | 225 EXPECT_EQ(kOkGotStream, result); |
| 225 } | 226 } |
| 226 | 227 |
| 227 void WebRtcTestBase::GetUserMediaAndExpectAutoDenyWithoutPrompt( | 228 void WebRtcTestBase::GetUserMediaAndExpectAutoDenyWithoutPrompt( |
| 228 content::WebContents* tab_contents) const { | 229 content::WebContents* tab_contents) const { |
| 229 std::string result; | 230 std::string result; |
| 230 // We issue a GetUserMedia() request. We expect that the origin already has a | 231 // We issue a GetUserMedia() request. We expect that the origin already has a |
| 231 // sticky "deny" permission (e.g. because the caller previously called | 232 // sticky "deny" permission (e.g. because the caller previously called |
| 232 // GetUserMediaAndDeny()), and therefore the GetUserMedia() request | 233 // GetUserMediaAndDeny()), and therefore the GetUserMedia() request |
| 233 // automatically succeeds without a prompt. | 234 // automatically succeeds without a prompt. |
| 234 // If the caller made a mistake, a prompt may show up instead. For this case, | 235 // If the caller made a mistake, a prompt may show up instead. For this case, |
| 235 // we set an auto-response to avoid leaving the prompt hanging. The choice of | 236 // we set an auto-response to avoid leaving the prompt hanging. The choice of |
| 236 // ACCEPT_ALL makes sure that the response to the prompt doesn't accidentally | 237 // ACCEPT_ALL makes sure that the response to the prompt doesn't accidentally |
| 237 // result in a newly granted media stream permission. | 238 // result in a newly granted media stream permission. |
| 238 PermissionBubbleManager::FromWebContents(tab_contents) | 239 PermissionRequestManager::FromWebContents(tab_contents) |
| 239 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | 240 ->set_auto_response_for_test(PermissionRequestManager::ACCEPT_ALL); |
| 240 PermissionRequestObserver permissionRequestObserver(tab_contents); | 241 PermissionRequestObserver permissionRequestObserver(tab_contents); |
| 241 GetUserMedia(tab_contents, kAudioVideoCallConstraints); | 242 GetUserMedia(tab_contents, kAudioVideoCallConstraints); |
| 242 EXPECT_FALSE(permissionRequestObserver.request_shown()); | 243 EXPECT_FALSE(permissionRequestObserver.request_shown()); |
| 243 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 244 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 244 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 245 tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 245 EXPECT_EQ(kFailedWithPermissionDeniedError, result); | 246 EXPECT_EQ(kFailedWithPermissionDeniedError, result); |
| 246 } | 247 } |
| 247 | 248 |
| 248 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, | 249 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, |
| 249 const std::string& constraints) const { | 250 const std::string& constraints) const { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 264 content::WebContents* | 265 content::WebContents* |
| 265 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( | 266 WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints( |
| 266 const GURL& url, | 267 const GURL& url, |
| 267 const std::string& constraints) const { | 268 const std::string& constraints) const { |
| 268 chrome::AddTabAt(browser(), GURL(), -1, true); | 269 chrome::AddTabAt(browser(), GURL(), -1, true); |
| 269 ui_test_utils::NavigateToURL(browser(), url); | 270 ui_test_utils::NavigateToURL(browser(), url); |
| 270 content::WebContents* new_tab = | 271 content::WebContents* new_tab = |
| 271 browser()->tab_strip_model()->GetActiveWebContents(); | 272 browser()->tab_strip_model()->GetActiveWebContents(); |
| 272 // Accept if necessary, but don't expect a prompt (because auto-accept is also | 273 // Accept if necessary, but don't expect a prompt (because auto-accept is also |
| 273 // okay). | 274 // okay). |
| 274 PermissionBubbleManager::FromWebContents(new_tab) | 275 PermissionRequestManager::FromWebContents(new_tab) |
| 275 ->set_auto_response_for_test(PermissionBubbleManager::ACCEPT_ALL); | 276 ->set_auto_response_for_test(PermissionRequestManager::ACCEPT_ALL); |
| 276 GetUserMedia(new_tab, constraints); | 277 GetUserMedia(new_tab, constraints); |
| 277 std::string result; | 278 std::string result; |
| 278 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 279 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 279 new_tab->GetMainFrame(), "obtainGetUserMediaResult();", &result)); | 280 new_tab->GetMainFrame(), "obtainGetUserMediaResult();", &result)); |
| 280 EXPECT_EQ(kOkGotStream, result); | 281 EXPECT_EQ(kOkGotStream, result); |
| 281 return new_tab; | 282 return new_tab; |
| 282 } | 283 } |
| 283 | 284 |
| 284 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( | 285 content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab( |
| 285 const std::string& test_page) const { | 286 const std::string& test_page) const { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 } | 478 } |
| 478 | 479 |
| 479 void WebRtcTestBase::GenerateAndCloneCertificate( | 480 void WebRtcTestBase::GenerateAndCloneCertificate( |
| 480 content::WebContents* tab, const std::string& keygen_algorithm) const { | 481 content::WebContents* tab, const std::string& keygen_algorithm) const { |
| 481 std::string javascript = base::StringPrintf( | 482 std::string javascript = base::StringPrintf( |
| 482 "generateAndCloneCertificate(%s)", keygen_algorithm.c_str()); | 483 "generateAndCloneCertificate(%s)", keygen_algorithm.c_str()); |
| 483 std::string response = ExecuteJavascript(javascript, tab); | 484 std::string response = ExecuteJavascript(javascript, tab); |
| 484 EXPECT_EQ("ok-generated-and-cloned", response) << "Failed to generate and " | 485 EXPECT_EQ("ok-generated-and-cloned", response) << "Failed to generate and " |
| 485 "clone certificate: " << response; | 486 "clone certificate: " << response; |
| 486 } | 487 } |
| OLD | NEW |