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