Commit 21ff11f6 authored by Chris Hamilton's avatar Chris Hamilton Committed by Commit Bot

[PM] Add additional helpers to PerformanceManagerBrowserHarness.

This adds OnGraphCreated test seams to the browser harness, as well as
a utility function for running a lambda synchronously in the graph
(a common operation in tests).

Change-Id: I4c10dbba34bc8c1d81722ac3780491079a8cadf6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2493687Reviewed-by: default avatarJoe Mason <joenotcharles@chromium.org>
Commit-Queue: Chris Hamilton <chrisha@chromium.org>
Auto-Submit: Chris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821281}
parent 1f9c3dc1
...@@ -26,6 +26,10 @@ class PerformanceManagerLifetime { ...@@ -26,6 +26,10 @@ class PerformanceManagerLifetime {
PerformanceManagerLifetime(Decorators, GraphCreatedCallback); PerformanceManagerLifetime(Decorators, GraphCreatedCallback);
~PerformanceManagerLifetime(); ~PerformanceManagerLifetime();
// Allows specifying an additional callback that will be invoked in tests.
static void SetAdditionalGraphCreatedCallbackForTesting(
GraphCreatedCallback graph_created_callback);
private: private:
std::unique_ptr<PerformanceManager> performance_manager_; std::unique_ptr<PerformanceManager> performance_manager_;
std::unique_ptr<PerformanceManagerRegistry> performance_manager_registry_; std::unique_ptr<PerformanceManagerRegistry> performance_manager_registry_;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "components/performance_manager/embedder/performance_manager_lifetime.h" #include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/no_destructor.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/performance_manager/decorators/frame_visibility_decorator.h" #include "components/performance_manager/decorators/frame_visibility_decorator.h"
...@@ -27,6 +28,12 @@ namespace performance_manager { ...@@ -27,6 +28,12 @@ namespace performance_manager {
namespace { namespace {
GraphCreatedCallback* GetAdditionalGraphCreatedCallback() {
static base::NoDestructor<GraphCreatedCallback>
additional_graph_created_callback;
return additional_graph_created_callback.get();
}
void DefaultGraphCreatedCallback( void DefaultGraphCreatedCallback(
GraphCreatedCallback external_graph_created_callback, GraphCreatedCallback external_graph_created_callback,
GraphImpl* graph) { GraphImpl* graph) {
...@@ -43,13 +50,19 @@ void DefaultGraphCreatedCallback( ...@@ -43,13 +50,19 @@ void DefaultGraphCreatedCallback(
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
graph->PassToGraph(std::make_unique<SiteDataRecorder>()); graph->PassToGraph(std::make_unique<SiteDataRecorder>());
#endif #endif
// Run graph created callbacks.
std::move(external_graph_created_callback).Run(graph); std::move(external_graph_created_callback).Run(graph);
if (*GetAdditionalGraphCreatedCallback())
std::move(*GetAdditionalGraphCreatedCallback()).Run(graph);
} }
void NullGraphCreatedCallback( void NullGraphCreatedCallback(
GraphCreatedCallback external_graph_created_callback, GraphCreatedCallback external_graph_created_callback,
GraphImpl* graph) { GraphImpl* graph) {
std::move(external_graph_created_callback).Run(graph); std::move(external_graph_created_callback).Run(graph);
if (*GetAdditionalGraphCreatedCallback())
std::move(*GetAdditionalGraphCreatedCallback()).Run(graph);
} }
base::OnceCallback<void(GraphImpl*)> AddDecorators( base::OnceCallback<void(GraphImpl*)> AddDecorators(
...@@ -84,6 +97,12 @@ PerformanceManagerLifetime::~PerformanceManagerLifetime() { ...@@ -84,6 +97,12 @@ PerformanceManagerLifetime::~PerformanceManagerLifetime() {
std::move(performance_manager_)); std::move(performance_manager_));
} }
// static
void PerformanceManagerLifetime::SetAdditionalGraphCreatedCallbackForTesting(
GraphCreatedCallback graph_created_callback) {
*GetAdditionalGraphCreatedCallback() = std::move(graph_created_callback);
}
std::unique_ptr<PerformanceManager> std::unique_ptr<PerformanceManager>
CreatePerformanceManagerWithDefaultDecorators( CreatePerformanceManagerWithDefaultDecorators(
GraphCreatedCallback graph_created_callback) { GraphCreatedCallback graph_created_callback) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "content/shell/browser/shell.h" #include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_content_browser_client.h" #include "content/shell/browser/shell_content_browser_client.h"
...@@ -20,6 +21,16 @@ namespace performance_manager { ...@@ -20,6 +21,16 @@ namespace performance_manager {
PerformanceManagerBrowserTestHarness::~PerformanceManagerBrowserTestHarness() = PerformanceManagerBrowserTestHarness::~PerformanceManagerBrowserTestHarness() =
default; default;
void PerformanceManagerBrowserTestHarness::SetUp() {
PerformanceManagerLifetime::SetAdditionalGraphCreatedCallbackForTesting(
base::BindLambdaForTesting(
[self = this](Graph* graph) { self->OnGraphCreated(graph); }));
// The PM gets initialized in the following, so this must occur after
// setting the callback.
Super::SetUp();
}
void PerformanceManagerBrowserTestHarness::PreRunTestOnMainThread() { void PerformanceManagerBrowserTestHarness::PreRunTestOnMainThread() {
Super::PreRunTestOnMainThread(); Super::PreRunTestOnMainThread();
...@@ -37,6 +48,8 @@ void PerformanceManagerBrowserTestHarness::SetUpCommandLine( ...@@ -37,6 +48,8 @@ void PerformanceManagerBrowserTestHarness::SetUpCommandLine(
"PerformanceManagerInstrumentation"); "PerformanceManagerInstrumentation");
} }
void PerformanceManagerBrowserTestHarness::OnGraphCreated(Graph* graph) {}
content::Shell* PerformanceManagerBrowserTestHarness::CreateShell() { content::Shell* PerformanceManagerBrowserTestHarness::CreateShell() {
content::Shell* shell = CreateBrowser(); content::Shell* shell = CreateBrowser();
return shell; return shell;
......
...@@ -5,10 +5,15 @@ ...@@ -5,10 +5,15 @@
#ifndef COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_ #ifndef COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
#define COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_ #define COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "components/performance_manager/public/performance_manager.h"
#include "content/public/test/content_browser_test.h" #include "content/public/test/content_browser_test.h"
namespace performance_manager { namespace performance_manager {
class Graph;
// Like PerformanceManagerTestHarness, but for browser tests. Full process // Like PerformanceManagerTestHarness, but for browser tests. Full process
// trees and live RFHs, etc, are created. Meant to be used from // trees and live RFHs, etc, are created. Meant to be used from
// components_browsertests and browser_tests. // components_browsertests and browser_tests.
...@@ -24,10 +29,17 @@ class PerformanceManagerBrowserTestHarness ...@@ -24,10 +29,17 @@ class PerformanceManagerBrowserTestHarness
const PerformanceManagerBrowserTestHarness&) = delete; const PerformanceManagerBrowserTestHarness&) = delete;
~PerformanceManagerBrowserTestHarness() override; ~PerformanceManagerBrowserTestHarness() override;
// gtest::Test:
void SetUp() override;
// content::BrowserTestBase: // content::BrowserTestBase:
void PreRunTestOnMainThread() override; void PreRunTestOnMainThread() override;
void SetUpCommandLine(base::CommandLine* command_line) override; void SetUpCommandLine(base::CommandLine* command_line) override;
// An additional seam that gets invoked as part of the PM initialization. This
// will be invoked on the PM sequence.
virtual void OnGraphCreated(Graph* graph);
// Creates a content shell with its own window, hosting a single tab that is // Creates a content shell with its own window, hosting a single tab that is
// navigated to about:blank. The WebContents will have the PM helpers // navigated to about:blank. The WebContents will have the PM helpers
// attached. Ownership of the shell rests with this object. Note that such a // attached. Ownership of the shell rests with this object. Note that such a
...@@ -40,6 +52,21 @@ class PerformanceManagerBrowserTestHarness ...@@ -40,6 +52,21 @@ class PerformanceManagerBrowserTestHarness
// Waits for an ongoing navigation to terminate on the given |contents|. // Waits for an ongoing navigation to terminate on the given |contents|.
void WaitForLoad(content::WebContents* contents); void WaitForLoad(content::WebContents* contents);
// Helper function for running a task on the graph, and waiting for it to
// complete. The signature of OnGraphCallback is expected to be void(Graph*).
template <typename OnGraphCallback>
void RunInGraph(OnGraphCallback on_graph_callback) {
base::RunLoop run_loop;
PerformanceManager::CallOnGraph(
FROM_HERE,
base::BindLambdaForTesting([quit_loop = run_loop.QuitClosure(),
&on_graph_callback](Graph* graph) {
on_graph_callback(graph);
quit_loop.Run();
}));
run_loop.Run();
}
}; };
} // namespace performance_manager } // namespace performance_manager
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment