1 // Copyright 2020 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 COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
6 #define COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
7 
8 #include "base/run_loop.h"
9 #include "base/test/bind.h"
10 #include "components/performance_manager/public/performance_manager.h"
11 #include "content/public/test/content_browser_test.h"
12 
13 namespace performance_manager {
14 
15 class Graph;
16 
17 // Like PerformanceManagerTestHarness, but for browser tests. Full process
18 // trees and live RFHs, etc, are created. Meant to be used from
19 // components_browsertests and browser_tests.
20 class PerformanceManagerBrowserTestHarness
21     : public content::ContentBrowserTest {
22   using Super = content::ContentBrowserTest;
23 
24  public:
25   PerformanceManagerBrowserTestHarness() = default;
26   PerformanceManagerBrowserTestHarness(
27       const PerformanceManagerBrowserTestHarness&) = delete;
28   PerformanceManagerBrowserTestHarness& operator=(
29       const PerformanceManagerBrowserTestHarness&) = delete;
30   ~PerformanceManagerBrowserTestHarness() override;
31 
32   // gtest::Test:
33   void SetUp() override;
34 
35   // content::BrowserTestBase:
36   void PreRunTestOnMainThread() override;
37   void SetUpCommandLine(base::CommandLine* command_line) override;
38 
39   // An additional seam that gets invoked as part of the PM initialization. This
40   // will be invoked on the PM sequence.
41   virtual void OnGraphCreated(Graph* graph);
42 
43   // Creates a content shell with its own window, hosting a single tab that is
44   // navigated to about:blank. The WebContents will have the PM helpers
45   // attached. Ownership of the shell rests with this object. Note that such a
46   // shell is created by default by the fixture, accessible via "shell()". Only
47   // call this if you need multiple independent WebContents.
48   content::Shell* CreateShell();
49 
50   // Starts a navigation for the given |contents|.
51   void StartNavigation(content::WebContents* contents, const GURL& url);
52 
53   // Waits for an ongoing navigation to terminate on the given |contents|.
54   void WaitForLoad(content::WebContents* contents);
55 
56   // Helper function for running a task on the graph, and waiting for it to
57   // complete. The signature of OnGraphCallback is expected to be void(Graph*).
58   template <typename OnGraphCallback>
RunInGraph(OnGraphCallback on_graph_callback)59   void RunInGraph(OnGraphCallback on_graph_callback) {
60     base::RunLoop run_loop;
61     PerformanceManager::CallOnGraph(
62         FROM_HERE,
63         base::BindLambdaForTesting([quit_loop = run_loop.QuitClosure(),
64                                     &on_graph_callback](Graph* graph) {
65           on_graph_callback(graph);
66           quit_loop.Run();
67         }));
68     run_loop.Run();
69   }
70 };
71 
72 }  // namespace performance_manager
73 
74 #endif  // COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
75