OLD | NEW |
(Empty) | |
| 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 "chrome/browser/media/webrtc_browsertest_base.h" |
| 6 |
| 7 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/infobars/infobar.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/media/media_stream_infobar_delegate.h" |
| 12 #include "chrome/browser/media/webrtc_browsertest_common.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/test/browser_test_utils.h" |
| 17 |
| 18 const char WebRtcTestBase::kAudioVideoCallConstraints[] = |
| 19 "'{audio: true, video: true}'"; |
| 20 const char WebRtcTestBase::kAudioOnlyCallConstraints[] = "'{audio: true}'"; |
| 21 const char WebRtcTestBase::kVideoOnlyCallConstraints[] = "'{video: true}'"; |
| 22 const char WebRtcTestBase::kFailedWithErrorPermissionDenied[] = |
| 23 "failed-with-error-PERMISSION_DENIED"; |
| 24 const char WebRtcTestBase::kOkGotStream[] = "ok-got-stream"; |
| 25 |
| 26 // Convenience method which executes the provided javascript in the context |
| 27 // of the provided web contents and returns what it evaluated to. |
| 28 static std::string ExecuteJavascript(const std::string& javascript, |
| 29 content::WebContents* tab_contents) { |
| 30 std::string result; |
| 31 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 32 tab_contents, javascript, &result)); |
| 33 return result; |
| 34 } |
| 35 |
| 36 MediaStreamInfoBarDelegate* WebRtcTestBase::GetUserMediaAndWaitForInfobar( |
| 37 content::WebContents* tab_contents, |
| 38 const std::string& constraints) { |
| 39 content::WindowedNotificationObserver infobar_added( |
| 40 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
| 41 content::NotificationService::AllSources()); |
| 42 |
| 43 // Request user media: this will launch the media stream info bar. |
| 44 GetUserMedia(tab_contents, constraints); |
| 45 |
| 46 // Wait for the bar to pop up, then return it. |
| 47 infobar_added.Wait(); |
| 48 content::Details<InfoBarAddedDetails> details(infobar_added.details()); |
| 49 MediaStreamInfoBarDelegate* media_infobar = |
| 50 details.ptr()->AsMediaStreamInfoBarDelegate(); |
| 51 return media_infobar; |
| 52 } |
| 53 |
| 54 void WebRtcTestBase::CloseInfobarInTab(content::WebContents* tab_contents, |
| 55 MediaStreamInfoBarDelegate* infobar) { |
| 56 content::WindowedNotificationObserver infobar_removed( |
| 57 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 58 content::NotificationService::AllSources()); |
| 59 |
| 60 InfoBarService* infobar_service = |
| 61 InfoBarService::FromWebContents(tab_contents); |
| 62 infobar_service->RemoveInfoBar(infobar); |
| 63 |
| 64 infobar_removed.Wait(); |
| 65 } |
| 66 |
| 67 void WebRtcTestBase::GetUserMediaAndAccept(content::WebContents* tab_contents) { |
| 68 GetUserMediaWithSpecificConstraintsAndAccept(tab_contents, |
| 69 kAudioVideoCallConstraints); |
| 70 } |
| 71 |
| 72 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept( |
| 73 content::WebContents* tab_contents, const std::string& constraints) { |
| 74 MediaStreamInfoBarDelegate* media_infobar = |
| 75 GetUserMediaAndWaitForInfobar(tab_contents, constraints); |
| 76 |
| 77 media_infobar->Accept(); |
| 78 |
| 79 CloseInfobarInTab(tab_contents, media_infobar); |
| 80 |
| 81 // Wait for WebRTC to call the success callback. |
| 82 EXPECT_TRUE(PollingWaitUntil( |
| 83 "obtainGetUserMediaResult()", kOkGotStream, tab_contents)); |
| 84 } |
| 85 |
| 86 void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) { |
| 87 return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents, |
| 88 kAudioVideoCallConstraints); |
| 89 } |
| 90 |
| 91 void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny( |
| 92 content::WebContents* tab_contents, |
| 93 const std::string& constraints) { |
| 94 MediaStreamInfoBarDelegate* media_infobar = |
| 95 GetUserMediaAndWaitForInfobar(tab_contents, constraints); |
| 96 |
| 97 media_infobar->Cancel(); |
| 98 |
| 99 CloseInfobarInTab(tab_contents, media_infobar); |
| 100 |
| 101 // Wait for WebRTC to call the fail callback. |
| 102 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", |
| 103 kFailedWithErrorPermissionDenied, |
| 104 tab_contents)); |
| 105 } |
| 106 |
| 107 void WebRtcTestBase::GetUserMediaAndDismiss( |
| 108 content::WebContents* tab_contents) { |
| 109 MediaStreamInfoBarDelegate* media_infobar = |
| 110 GetUserMediaAndWaitForInfobar(tab_contents, kAudioVideoCallConstraints); |
| 111 |
| 112 media_infobar->InfoBarDismissed(); |
| 113 |
| 114 CloseInfobarInTab(tab_contents, media_infobar); |
| 115 |
| 116 // A dismiss should be treated like a deny. |
| 117 EXPECT_TRUE(PollingWaitUntil("obtainGetUserMediaResult()", |
| 118 kFailedWithErrorPermissionDenied, |
| 119 tab_contents)); |
| 120 } |
| 121 |
| 122 void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents, |
| 123 const std::string& constraints) { |
| 124 // Request user media: this will launch the media stream info bar. |
| 125 EXPECT_EQ("ok-requested", |
| 126 ExecuteJavascript( |
| 127 base::StringPrintf("getUserMedia(%s);", constraints.c_str()), |
| 128 tab_contents)); |
| 129 } |
| 130 |
OLD | NEW |