1 // Copyright (c) 2018 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/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
12 #include "chrome/browser/media/webrtc/webrtc_event_log_manager.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/test/browser_test.h"
16 #include "media/base/media_switches.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 
20 using webrtc_event_logging::WebRtcEventLogManager;
21 
22 namespace {
23 const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html";
24 }
25 
26 class WebRTCInternalsIntegrationBrowserTest : public WebRtcTestBase {
27  public:
28   ~WebRTCInternalsIntegrationBrowserTest() override = default;
29 
SetUpCommandLine(base::CommandLine * command_line)30   void SetUpCommandLine(base::CommandLine* command_line) override {
31     InProcessBrowserTest::SetUpDefaultCommandLine(command_line);
32 
33     {
34       base::ScopedAllowBlockingForTesting allow_blocking;
35       ASSERT_TRUE(local_logs_dir_.CreateUniqueTempDir());
36     }
37     command_line->AppendSwitchASCII(switches::kWebRtcLocalEventLogging,
38                                     local_logs_dir_.GetPath().MaybeAsASCII());
39   }
40 
41   // To avoid flaky tests, we need to synchronize with WebRtcEventLogger's
42   // internal task runners (if any exist) before we examine anything we
43   // expect to be produced by WebRtcEventLogger (files, etc.).
WaitForEventLogProcessing()44   void WaitForEventLogProcessing() {
45     WebRtcEventLogManager* manager = WebRtcEventLogManager::GetInstance();
46     ASSERT_TRUE(manager);
47 
48     base::RunLoop run_loop;
49     manager->PostNullTaskForTesting(run_loop.QuitWhenIdleClosure());
50     run_loop.Run();
51   }
52 
IsDirectoryEmpty(const base::FilePath & path)53   bool IsDirectoryEmpty(const base::FilePath& path) {
54     base::ScopedAllowBlockingForTesting allow_blocking;
55     return base::IsDirectoryEmpty(path);
56   }
57 
58   base::ScopedTempDir local_logs_dir_;
59 };
60 
IN_PROC_BROWSER_TEST_F(WebRTCInternalsIntegrationBrowserTest,IntegrationWithWebRtcEventLogger)61 IN_PROC_BROWSER_TEST_F(WebRTCInternalsIntegrationBrowserTest,
62                        IntegrationWithWebRtcEventLogger) {
63   ASSERT_TRUE(embedded_test_server()->Start());
64 
65   content::WebContents* tab =
66       OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
67 
68   ASSERT_TRUE(IsDirectoryEmpty(local_logs_dir_.GetPath()));  // Sanity on test.
69 
70   // Local WebRTC event logging turned on from command line using the
71   // kWebRtcLocalEventLogging flag. When we set up a peer connection, it
72   // will be logged to a file under |local_logs_dir_|.
73   SetupPeerconnectionWithLocalStream(tab);
74 
75   WaitForEventLogProcessing();
76 
77   EXPECT_FALSE(IsDirectoryEmpty(local_logs_dir_.GetPath()));
78 }
79