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 {
PerformanceManagerLifetime(Decorators, GraphCreatedCallback);
~PerformanceManagerLifetime();
// Allows specifying an additional callback that will be invoked in tests.
static void SetAdditionalGraphCreatedCallbackForTesting(
GraphCreatedCallback graph_created_callback);
private:
std::unique_ptr<PerformanceManager> performance_manager_;
std::unique_ptr<PerformanceManagerRegistry> performance_manager_registry_;
......
......@@ -5,6 +5,7 @@
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "base/bind.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "components/performance_manager/decorators/frame_visibility_decorator.h"
......@@ -27,6 +28,12 @@ namespace performance_manager {
namespace {
GraphCreatedCallback* GetAdditionalGraphCreatedCallback() {
static base::NoDestructor<GraphCreatedCallback>
additional_graph_created_callback;
return additional_graph_created_callback.get();
}
void DefaultGraphCreatedCallback(
GraphCreatedCallback external_graph_created_callback,
GraphImpl* graph) {
......@@ -43,13 +50,19 @@ void DefaultGraphCreatedCallback(
#if !defined(OS_ANDROID)
graph->PassToGraph(std::make_unique<SiteDataRecorder>());
#endif
// Run graph created callbacks.
std::move(external_graph_created_callback).Run(graph);
if (*GetAdditionalGraphCreatedCallback())
std::move(*GetAdditionalGraphCreatedCallback()).Run(graph);
}
void NullGraphCreatedCallback(
GraphCreatedCallback external_graph_created_callback,
GraphImpl* graph) {
std::move(external_graph_created_callback).Run(graph);
if (*GetAdditionalGraphCreatedCallback())
std::move(*GetAdditionalGraphCreatedCallback()).Run(graph);
}
base::OnceCallback<void(GraphImpl*)> AddDecorators(
......@@ -84,6 +97,12 @@ PerformanceManagerLifetime::~PerformanceManagerLifetime() {
std::move(performance_manager_));
}
// static
void PerformanceManagerLifetime::SetAdditionalGraphCreatedCallbackForTesting(
GraphCreatedCallback graph_created_callback) {
*GetAdditionalGraphCreatedCallback() = std::move(graph_created_callback);
}
std::unique_ptr<PerformanceManager>
CreatePerformanceManagerWithDefaultDecorators(
GraphCreatedCallback graph_created_callback) {
......
......@@ -6,6 +6,7 @@
#include "base/bind_helpers.h"
#include "base/run_loop.h"
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "content/public/common/content_switches.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_content_browser_client.h"
......@@ -20,6 +21,16 @@ namespace performance_manager {
PerformanceManagerBrowserTestHarness::~PerformanceManagerBrowserTestHarness() =
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() {
Super::PreRunTestOnMainThread();
......@@ -37,6 +48,8 @@ void PerformanceManagerBrowserTestHarness::SetUpCommandLine(
"PerformanceManagerInstrumentation");
}
void PerformanceManagerBrowserTestHarness::OnGraphCreated(Graph* graph) {}
content::Shell* PerformanceManagerBrowserTestHarness::CreateShell() {
content::Shell* shell = CreateBrowser();
return shell;
......
......@@ -5,10 +5,15 @@
#ifndef 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"
namespace performance_manager {
class Graph;
// Like PerformanceManagerTestHarness, but for browser tests. Full process
// trees and live RFHs, etc, are created. Meant to be used from
// components_browsertests and browser_tests.
......@@ -24,10 +29,17 @@ class PerformanceManagerBrowserTestHarness
const PerformanceManagerBrowserTestHarness&) = delete;
~PerformanceManagerBrowserTestHarness() override;
// gtest::Test:
void SetUp() override;
// content::BrowserTestBase:
void PreRunTestOnMainThread() 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
// navigated to about:blank. The WebContents will have the PM helpers
// attached. Ownership of the shell rests with this object. Note that such a
......@@ -40,6 +52,21 @@ class PerformanceManagerBrowserTestHarness
// Waits for an ongoing navigation to terminate on the given |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
......
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