#include "components/performance_manager/worker_watcher.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/not_fatal_until.h"
#include "base/observer_list.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/uuid.h"
#include "components/performance_manager/frame_node_source.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/graph/worker_node_impl.h"
#include "components/performance_manager/performance_manager_impl.h"
#include "components/performance_manager/process_node_source.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/render_process_host_id.h"
#include "components/performance_manager/public/render_process_host_proxy.h"
#include "components/performance_manager/test_support/run_in_graph.h"
#include "content/public/browser/dedicated_worker_creator.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/service_worker_running_info.h"
#include "content/public/browser/shared_worker_service.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/fake_service_worker_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace performance_manager {
namespace {
int GenerateNextId() { … }
GURL GenerateWorkerUrl() { … }
GURL GenerateUniqueDomainUrl() { … }
bool IsWorkerClient(WorkerNodeImpl* worker_node,
FrameNodeImpl* client_frame_node) { … }
bool IsWorkerClient(WorkerNodeImpl* worker_node,
WorkerNodeImpl* client_worker_node) { … }
class TestDedicatedWorkerService : public content::DedicatedWorkerService { … };
TestDedicatedWorkerService::TestDedicatedWorkerService() = default;
TestDedicatedWorkerService::~TestDedicatedWorkerService() = default;
void TestDedicatedWorkerService::AddObserver(Observer* observer) { … }
void TestDedicatedWorkerService::RemoveObserver(Observer* observer) { … }
void TestDedicatedWorkerService::EnumerateDedicatedWorkers(Observer* observer) { … }
const blink::DedicatedWorkerToken&
TestDedicatedWorkerService::CreateDedicatedWorker(
int worker_process_id,
content::DedicatedWorkerCreator creator,
const url::Origin& origin) { … }
void TestDedicatedWorkerService::DestroyDedicatedWorker(
const blink::DedicatedWorkerToken& token) { … }
class TestSharedWorkerService : public content::SharedWorkerService { … };
TestSharedWorkerService::TestSharedWorkerService() = default;
TestSharedWorkerService::~TestSharedWorkerService() = default;
void TestSharedWorkerService::AddObserver(Observer* observer) { … }
void TestSharedWorkerService::RemoveObserver(Observer* observer) { … }
void TestSharedWorkerService::EnumerateSharedWorkers(Observer* observer) { … }
bool TestSharedWorkerService::TerminateWorker(
const GURL& url,
const std::string& name,
const blink::StorageKey& storage_key,
const blink::mojom::SharedWorkerSameSiteCookies same_site_cookies) { … }
void TestSharedWorkerService::Shutdown() { … }
blink::SharedWorkerToken TestSharedWorkerService::CreateSharedWorker(
int worker_process_id,
const url::Origin& origin) { … }
void TestSharedWorkerService::DestroySharedWorker(
const blink::SharedWorkerToken& shared_worker_token) { … }
void TestSharedWorkerService::AddClient(
const blink::SharedWorkerToken& shared_worker_token,
content::GlobalRenderFrameHostId client_render_frame_host_id) { … }
void TestSharedWorkerService::RemoveClient(
const blink::SharedWorkerToken& shared_worker_token,
content::GlobalRenderFrameHostId client_render_frame_host_id) { … }
class TestServiceWorkerContextAdapter : public ServiceWorkerContextAdapter { … };
TestServiceWorkerContextAdapter::TestServiceWorkerContextAdapter() = default;
TestServiceWorkerContextAdapter::~TestServiceWorkerContextAdapter() = default;
void TestServiceWorkerContextAdapter::AddObserver(
content::ServiceWorkerContextObserver* observer) { … }
void TestServiceWorkerContextAdapter::RemoveObserver(
content::ServiceWorkerContextObserver* observer) { … }
int64_t TestServiceWorkerContextAdapter::CreateServiceWorker() { … }
void TestServiceWorkerContextAdapter::DestroyServiceWorker(int64_t version_id) { … }
void TestServiceWorkerContextAdapter::StartServiceWorker(
int64_t version_id,
int worker_process_id,
const GURL& worker_url,
const GURL& scope_url) { … }
void TestServiceWorkerContextAdapter::StopServiceWorker(int64_t version_id) { … }
std::string TestServiceWorkerContextAdapter::AddClient(
int64_t version_id,
const content::ServiceWorkerClientInfo& client_info) { … }
std::string TestServiceWorkerContextAdapter::AddClientWithClientID(
int64_t version_id,
std::string client_uuid,
const content::ServiceWorkerClientInfo& client_info) { … }
void TestServiceWorkerContextAdapter::RemoveClient(
int64_t version_id,
const std::string& client_uuid) { … }
void TestServiceWorkerContextAdapter::OnControlleeNavigationCommitted(
int64_t version_id,
const std::string& client_uuid,
content::GlobalRenderFrameHostId render_frame_host_id) { … }
class TestProcessNodeSource : public ProcessNodeSource { … };
TestProcessNodeSource::TestProcessNodeSource() = default;
TestProcessNodeSource::~TestProcessNodeSource() { … }
ProcessNodeImpl* TestProcessNodeSource::GetProcessNode(int render_process_id) { … }
int TestProcessNodeSource::CreateProcessNode() { … }
class TestFrameNodeSource : public FrameNodeSource { … };
TestFrameNodeSource::TestFrameNodeSource()
: … { … }
TestFrameNodeSource::~TestFrameNodeSource() { … }
FrameNodeImpl* TestFrameNodeSource::GetFrameNode(
content::GlobalRenderFrameHostId render_frame_host_id) { … }
void TestFrameNodeSource::SubscribeToFrameNode(
content::GlobalRenderFrameHostId render_frame_host_id,
OnbeforeFrameNodeRemovedCallback on_before_frame_node_removed_callback) { … }
void TestFrameNodeSource::UnsubscribeFromFrameNode(
content::GlobalRenderFrameHostId render_frame_host_id) { … }
content::GlobalRenderFrameHostId TestFrameNodeSource::CreateFrameNode(
int render_process_id,
ProcessNodeImpl* process_node) { … }
void TestFrameNodeSource::DeleteFrameNode(
content::GlobalRenderFrameHostId render_frame_host_id) { … }
void TestFrameNodeSource::InvokeAndRemoveCallback(FrameNodeImpl* frame_node) { … }
}
class WorkerWatcherTest : public testing::Test { … };
WorkerWatcherTest::WorkerWatcherTest() = default;
WorkerWatcherTest::~WorkerWatcherTest() = default;
void WorkerWatcherTest::SetUp() { … }
void WorkerWatcherTest::TearDown() { … }
WorkerNodeImpl* WorkerWatcherTest::GetDedicatedWorkerNode(
const blink::DedicatedWorkerToken& token) { … }
WorkerNodeImpl* WorkerWatcherTest::GetSharedWorkerNode(
const blink::SharedWorkerToken& shared_worker_token) { … }
WorkerNodeImpl* WorkerWatcherTest::GetServiceWorkerNode(int64_t version_id) { … }
TEST_F(WorkerWatcherTest, SimpleDedicatedWorker) { … }
TEST_F(WorkerWatcherTest, NestedDedicatedWorker) { … }
TEST_F(WorkerWatcherTest, SimpleSharedWorker) { … }
TEST_F(WorkerWatcherTest, ServiceWorkerFrameClient) { … }
TEST_F(WorkerWatcherTest, ServiceWorkerFrameClientOfTwoWorkers) { … }
TEST_F(WorkerWatcherTest, ServiceWorkerTwoFrameClientRelationships) { … }
TEST_F(WorkerWatcherTest, ServiceWorkerFrameClientDestroyedBeforeCommit) { … }
TEST_F(WorkerWatcherTest, AllTypesOfServiceWorkerClients) { … }
TEST_F(WorkerWatcherTest, ServiceWorkerStartsAndStopsWithExistingClients) { … }
TEST_F(WorkerWatcherTest, SharedWorkerCrossProcessClient) { … }
TEST_F(WorkerWatcherTest, SharedWorkerStartsWithDeadWorkerClients) { … }
TEST_F(WorkerWatcherTest, SharedWorkerDiesAsServiceWorkerClient) { … }
TEST_F(WorkerWatcherTest, OneSharedWorkerTwoClients) { … }
TEST_F(WorkerWatcherTest, OneClientTwoSharedWorkers) { … }
TEST_F(WorkerWatcherTest, FrameDestroyed) { … }
}