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 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_BROWSERTEST_BASE_H_ 6 #define CHROME_BROWSER_MEDIA_WEBRTC_BROWSERTEST_BASE_H_ 7 8 #include <string> 9 10 #include "chrome/test/base/in_process_browser_test.h" 11 12 namespace infobars { 13 class InfoBar; 14 } 15 16 namespace content { 17 class WebContents; 18 } 19 20 // Base class for WebRTC browser tests with useful primitives for interacting 21 // getUserMedia. We use inheritance here because it makes the test code look 22 // as clean as it can be. 23 class WebRtcTestBase : public InProcessBrowserTest { 24 public: 25 // Typical constraints. 26 static const char kAudioVideoCallConstraints[]; 27 static const char kAudioOnlyCallConstraints[]; 28 static const char kVideoOnlyCallConstraints[]; 29 static const char kAudioVideoCallConstraintsQVGA[]; 30 static const char kAudioVideoCallConstraints360p[]; 31 static const char kAudioVideoCallConstraintsVGA[]; 32 static const char kAudioVideoCallConstraints720p[]; 33 static const char kAudioVideoCallConstraints1080p[]; 34 35 static const char kFailedWithPermissionDeniedError[]; 36 static const char kFailedWithPermissionDismissedError[]; 37 38 protected: 39 WebRtcTestBase(); 40 virtual ~WebRtcTestBase(); 41 42 // These all require that the loaded page fulfills the public interface in 43 // chrome/test/data/webrtc/message_handling.js. 44 void GetUserMediaAndAccept(content::WebContents* tab_contents) const; 45 void GetUserMediaWithSpecificConstraintsAndAccept( 46 content::WebContents* tab_contents, 47 const std::string& constraints) const; 48 void GetUserMediaAndDeny(content::WebContents* tab_contents); 49 void GetUserMediaWithSpecificConstraintsAndDeny( 50 content::WebContents* tab_contents, 51 const std::string& constraints) const; 52 void GetUserMediaAndDismiss(content::WebContents* tab_contents) const; 53 void GetUserMedia(content::WebContents* tab_contents, 54 const std::string& constraints) const; 55 56 // Convenience method which opens the page at url, calls GetUserMediaAndAccept 57 // and returns the new tab. 58 content::WebContents* OpenPageAndGetUserMediaInNewTab(const GURL& url) const; 59 60 // Convenience method which opens the page at url, calls 61 // GetUserMediaAndAcceptWithSpecificConstraints and returns the new tab. 62 content::WebContents* OpenPageAndGetUserMediaInNewTabWithConstraints( 63 const GURL& url, const std::string& constraints) const; 64 65 // Convenience method which gets the URL for |test_page| and calls 66 // OpenPageAndGetUserMediaInNewTab(). 67 content::WebContents* OpenTestPageAndGetUserMediaInNewTab( 68 const std::string& test_page) const; 69 70 // Opens the page at |url| where getUserMedia has been invoked through other 71 // means and accepts the user media request. 72 content::WebContents* OpenPageAndAcceptUserMedia(const GURL& url) const; 73 74 // Closes the last local stream acquired by the GetUserMedia* methods. 75 void CloseLastLocalStream(content::WebContents* tab_contents) const; 76 77 std::string ExecuteJavascript(const std::string& javascript, 78 content::WebContents* tab_contents) const; 79 80 // Sets up a peer connection in the tab and adds the current local stream 81 // (which you can prepare by calling one of the GetUserMedia* methods above). 82 void SetupPeerconnectionWithLocalStream(content::WebContents* tab) const; 83 84 // Exchanges offers and answers between the peer connections in the 85 // respective tabs. Before calling this, you must have prepared peer 86 // connections in both tabs and configured them as you like (for instance by 87 // calling SetupPeerconnectionWithLocalStream). 88 void NegotiateCall(content::WebContents* from_tab, 89 content::WebContents* to_tab) const; 90 91 // Hangs up a negotiated call. 92 void HangUp(content::WebContents* from_tab) const; 93 94 // Call this to enable monitoring of javascript errors for this test method. 95 // This will only work if the tests are run sequentially by the test runner 96 // (i.e. with --test-launcher-developer-mode or --test-launcher-jobs=1). 97 void DetectErrorsInJavaScript(); 98 99 // Methods for detecting if video is playing (the loaded page must have 100 // chrome/test/data/webrtc/video_detector.js and its dependencies loaded to 101 // make that work). Looks at a 320x240 area of the target video tag. 102 void StartDetectingVideo(content::WebContents* tab_contents, 103 const std::string& video_element) const; 104 void WaitForVideoToPlay(content::WebContents* tab_contents) const; 105 106 // Returns the stream size as a string on the format <width>x<height>. 107 std::string GetStreamSize(content::WebContents* tab_contents, 108 const std::string& video_element) const; 109 110 // Methods to check what devices we have on the system. 111 bool HasWebcamAvailableOnSystem(content::WebContents* tab_contents) const; 112 113 // Returns true if we're on WinXP, that lovely operating system of bliss. 114 bool OnWinXp() const; 115 116 // Returns true if we're on win 8. 117 bool OnWin8() const; 118 119 private: 120 void CloseInfoBarInTab(content::WebContents* tab_contents, 121 infobars::InfoBar* infobar) const; 122 123 std::string CreateLocalOffer(content::WebContents* from_tab) const; 124 std::string CreateAnswer(std::string local_offer, 125 content::WebContents* to_tab) const; 126 void ReceiveAnswer(std::string answer, content::WebContents* from_tab) const; 127 void GatherAndSendIceCandidates(content::WebContents* from_tab, 128 content::WebContents* to_tab) const; 129 130 infobars::InfoBar* GetUserMediaAndWaitForInfoBar( 131 content::WebContents* tab_contents, 132 const std::string& constraints) const; 133 134 bool detect_errors_in_javascript_; 135 136 DISALLOW_COPY_AND_ASSIGN(WebRtcTestBase); 137 }; 138 139 #endif // CHROME_BROWSER_MEDIA_WEBRTC_BROWSERTEST_BASE_H_ 140