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 "base/test/scoped_feature_list.h"
7 #include "build/build_config.h"
8 #include "content/browser/webrtc/webrtc_webcam_browsertest.h"
9 #include "content/public/browser/browser_child_process_host.h"
10 #include "content/public/browser/video_capture_service.h"
11 #include "content/public/common/child_process_host.h"
12 #include "content/public/common/content_features.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test_utils.h"
16 #include "content/shell/browser/shell.h"
17 #include "media/base/media_switches.h"
18 #include "mojo/public/cpp/bindings/remote.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "services/video_capture/public/mojom/testing_controls.mojom.h"
21 
22 namespace content {
23 
24 namespace {
25 
26 static const char kVideoCaptureHtmlFile[] = "/media/video_capture_test.html";
27 static const char kStartVideoCaptureAndVerifySize[] =
28     "startVideoCaptureAndVerifySize(320, 200)";
29 static const char kWaitForVideoToTurnBlack[] = "waitForVideoToTurnBlack()";
30 static const char kVerifyHasReceivedTrackEndedEvent[] =
31     "verifyHasReceivedTrackEndedEvent()";
32 
33 }  // anonymous namespace
34 
35 // Integration test that exercises video capture functionality from the
36 // JavaScript level.
37 class WebRtcVideoCaptureBrowserTest : public ContentBrowserTest {
38  protected:
WebRtcVideoCaptureBrowserTest()39   WebRtcVideoCaptureBrowserTest() {
40     scoped_feature_list_.InitAndEnableFeature(features::kMojoVideoCapture);
41   }
42 
~WebRtcVideoCaptureBrowserTest()43   ~WebRtcVideoCaptureBrowserTest() override {}
44 
SetUpCommandLine(base::CommandLine * command_line)45   void SetUpCommandLine(base::CommandLine* command_line) override {
46     command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
47     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
48         switches::kEnableBlinkFeatures, "GetUserMedia");
49   }
50 
SetUp()51   void SetUp() override {
52     ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
53     EnablePixelOutput();
54     embedded_test_server()->StartAcceptingConnections();
55     ContentBrowserTest::SetUp();
56   }
57 
58  private:
59   base::test::ScopedFeatureList scoped_feature_list_;
60 
61   DISALLOW_COPY_AND_ASSIGN(WebRtcVideoCaptureBrowserTest);
62 };
63 
IN_PROC_BROWSER_TEST_F(WebRtcVideoCaptureBrowserTest,RecoverFromCrashInVideoCaptureProcess)64 IN_PROC_BROWSER_TEST_F(WebRtcVideoCaptureBrowserTest,
65                        RecoverFromCrashInVideoCaptureProcess) {
66   // This test only makes sense if the video capture service runs in a
67   // separate process.
68   if (!features::IsVideoCaptureServiceEnabledForOutOfProcess())
69     return;
70 
71   GURL url(embedded_test_server()->GetURL(kVideoCaptureHtmlFile));
72   EXPECT_TRUE(NavigateToURL(shell(), url));
73 
74   std::string result;
75   // Start video capture and wait until it started rendering
76   ASSERT_TRUE(ExecuteScriptAndExtractString(
77       shell(), kStartVideoCaptureAndVerifySize, &result));
78   ASSERT_EQ("OK", result);
79 
80   // Simulate crash in video capture process
81   mojo::Remote<video_capture::mojom::TestingControls> service_controls;
82   GetVideoCaptureService().BindControlsForTesting(
83       service_controls.BindNewPipeAndPassReceiver());
84   service_controls->Crash();
85 
86   // Wait for video element to turn black
87   ASSERT_TRUE(ExecuteScriptAndExtractString(shell(), kWaitForVideoToTurnBlack,
88                                             &result));
89   ASSERT_EQ("OK", result);
90   ASSERT_TRUE(ExecuteScriptAndExtractString(
91       shell(), kVerifyHasReceivedTrackEndedEvent, &result));
92   ASSERT_EQ("OK", result);
93 
94   // Start capturing again and expect it to work.
95   ASSERT_TRUE(ExecuteScriptAndExtractString(
96       shell(), kStartVideoCaptureAndVerifySize, &result));
97   ASSERT_EQ("OK", result);
98 }
99 
100 }  // namespace content
101