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 #include "components/performance_manager/test_support/performance_manager_browsertest_harness.h" 6 7 #include "base/callback_helpers.h" 8 #include "base/run_loop.h" 9 #include "components/performance_manager/embedder/performance_manager_lifetime.h" 10 #include "content/public/common/content_switches.h" 11 #include "content/shell/browser/shell.h" 12 #include "content/shell/browser/shell_content_browser_client.h" 13 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h" 14 #include "mojo/public/cpp/bindings/binder_map.h" 15 #include "net/dns/mock_host_resolver.h" 16 #include "net/test/embedded_test_server/embedded_test_server.h" 17 #include "services/service_manager/public/cpp/binder_registry.h" 18 19 namespace performance_manager { 20 21 PerformanceManagerBrowserTestHarness::~PerformanceManagerBrowserTestHarness() = 22 default; 23 SetUp()24void PerformanceManagerBrowserTestHarness::SetUp() { 25 PerformanceManagerLifetime::SetAdditionalGraphCreatedCallbackForTesting( 26 base::BindLambdaForTesting( 27 [self = this](Graph* graph) { self->OnGraphCreated(graph); })); 28 29 // The PM gets initialized in the following, so this must occur after 30 // setting the callback. 31 Super::SetUp(); 32 } 33 PreRunTestOnMainThread()34void PerformanceManagerBrowserTestHarness::PreRunTestOnMainThread() { 35 Super::PreRunTestOnMainThread(); 36 37 // Set up the embedded web server. 38 host_resolver()->AddRule("*", "127.0.0.1"); 39 embedded_test_server()->ServeFilesFromSourceDirectory( 40 "components/test/data/performance_manager"); 41 ASSERT_TRUE(embedded_test_server()->Start()); 42 } 43 SetUpCommandLine(base::CommandLine * command_line)44void PerformanceManagerBrowserTestHarness::SetUpCommandLine( 45 base::CommandLine* command_line) { 46 // Ensure the PM logic is enabled in renderers. 47 command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures, 48 "PerformanceManagerInstrumentation"); 49 } 50 OnGraphCreated(Graph * graph)51void PerformanceManagerBrowserTestHarness::OnGraphCreated(Graph* graph) {} 52 CreateShell()53content::Shell* PerformanceManagerBrowserTestHarness::CreateShell() { 54 content::Shell* shell = CreateBrowser(); 55 return shell; 56 } 57 StartNavigation(content::WebContents * contents,const GURL & url)58void PerformanceManagerBrowserTestHarness::StartNavigation( 59 content::WebContents* contents, 60 const GURL& url) { 61 // See content/public/test/browser_test_utils.cc 62 content::NavigationController::LoadURLParams params(url); 63 params.transition_type = ui::PageTransitionFromInt( 64 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); 65 contents->GetController().LoadURLWithParams(params); 66 contents->Focus(); 67 } 68 69 namespace { 70 71 class WaitForLoadObserver : public content::WebContentsObserver { 72 public: WaitForLoadObserver(content::WebContents * contents)73 explicit WaitForLoadObserver(content::WebContents* contents) 74 : content::WebContentsObserver(contents) {} 75 ~WaitForLoadObserver() override = default; 76 Wait()77 void Wait() { 78 if (!web_contents()->IsLoading()) 79 return; 80 run_loop_.Run(); 81 } 82 83 private: 84 // WebContentsObserver implementation DidStopLoading()85 void DidStopLoading() override { run_loop_.Quit(); } 86 87 base::RunLoop run_loop_; 88 }; 89 90 } // namespace 91 WaitForLoad(content::WebContents * contents)92void PerformanceManagerBrowserTestHarness::WaitForLoad( 93 content::WebContents* contents) { 94 WaitForLoadObserver observer(contents); 95 observer.Wait(); 96 } 97 98 } // namespace performance_manager 99