1 // Copyright 2017 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 "base/command_line.h"
6 #include "build/build_config.h"
7 #include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
8 #include "chrome/browser/media/webrtc/webrtc_browsertest_common.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/browser_test.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "media/base/media_switches.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 
21 namespace {
22 static const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html";
23 }  // namespace
24 
25 // Top-level integration test for WebRTC. Uses an actual desktop capture
26 // extension to capture whole screen.
27 class WebRtcDesktopCaptureBrowserTest : public WebRtcTestBase {
28  public:
WebRtcDesktopCaptureBrowserTest()29   WebRtcDesktopCaptureBrowserTest() : left_tab_(nullptr), right_tab_(nullptr) {}
30 
SetUpInProcessBrowserTestFixture()31   void SetUpInProcessBrowserTestFixture() override {
32     DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
33   }
34 
SetUpCommandLine(base::CommandLine * command_line)35   void SetUpCommandLine(base::CommandLine* command_line) override {
36     // Ensure the infobar is enabled, since we expect that in this test.
37     EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
38 
39     // Flags use to automatically select the right dekstop source and get
40     // around security restrictions.
41     command_line->AppendSwitchASCII(switches::kAutoSelectDesktopCaptureSource,
42                                     "Entire screen");
43     command_line->AppendSwitch(switches::kEnableUserMediaScreenCapturing);
44   }
45 
46  protected:
DetectVideoAndHangUp()47   void DetectVideoAndHangUp() {
48     StartDetectingVideo(left_tab_, "remote-view");
49     StartDetectingVideo(right_tab_, "remote-view");
50 #if !defined(OS_MAC)
51     // Video is choppy on Mac OS X. http://crbug.com/443542.
52     WaitForVideoToPlay(left_tab_);
53     WaitForVideoToPlay(right_tab_);
54 #endif
55     HangUp(left_tab_);
56     HangUp(right_tab_);
57   }
58 
59   content::WebContents* left_tab_;
60   content::WebContents* right_tab_;
61 };
62 
63 // TODO(crbug.com/796889): Enable on Mac when thread check crash is fixed.
64 // TODO(sprang): Figure out why test times out on Win 10 and ChromeOS.
65 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
66 #define MAYBE_RunsScreenshareFromOneTabToAnother \
67   RunsScreenshareFromOneTabToAnother
68 #else
69 #define MAYBE_RunsScreenshareFromOneTabToAnother \
70   DISABLED_RunsScreenshareFromOneTabToAnother
71 #endif
IN_PROC_BROWSER_TEST_F(WebRtcDesktopCaptureBrowserTest,MAYBE_RunsScreenshareFromOneTabToAnother)72 IN_PROC_BROWSER_TEST_F(WebRtcDesktopCaptureBrowserTest,
73                        MAYBE_RunsScreenshareFromOneTabToAnother) {
74   ASSERT_TRUE(embedded_test_server()->Start());
75   LoadDesktopCaptureExtension();
76   left_tab_ = OpenTestPageInNewTab(kMainWebrtcTestHtmlPage);
77   std::string stream_id = GetDesktopMediaStream(left_tab_);
78   EXPECT_NE(stream_id, "");
79 
80   LOG(INFO) << "Opened desktop media stream, got id " << stream_id;
81 
82   const std::string constraints =
83       "{audio: false, video: {mandatory: {chromeMediaSource: 'desktop',"
84       "chromeMediaSourceId: '" +
85       stream_id + "'}}}";
86   EXPECT_TRUE(GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
87       left_tab_, constraints));
88   right_tab_ = OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
89   SetupPeerconnectionWithLocalStream(left_tab_);
90   SetupPeerconnectionWithLocalStream(right_tab_);
91   NegotiateCall(left_tab_, right_tab_);
92   VerifyStatsGeneratedCallback(right_tab_);
93   DetectVideoAndHangUp();
94 }
95