#ifndef CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
#define CONTENT_PUBLIC_TEST_TEST_RENDERER_HOST_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/synthetic_web_input_event_builders.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/mojom/fenced_frame/fenced_frame.mojom.h"
#include "third_party/blink/public/mojom/usb/web_usb_service.mojom-forward.h"
#include "ui/base/page_transition_types.h"
#if defined(USE_AURA)
#include "ui/aura/test/aura_test_helper.h"
#endif
#if !BUILDFLAG(IS_ANDROID)
#include "third_party/blink/public/mojom/hid/hid.mojom-forward.h"
#endif
namespace aura {
namespace test {
class AuraTestHelper;
}
}
namespace blink {
struct ParsedPermissionsPolicyDeclaration;
using ParsedPermissionsPolicy = std::vector<ParsedPermissionsPolicyDeclaration>;
namespace web_pref {
struct WebPreferences;
}
}
namespace display {
class Screen;
class ScopedNativeScreen;
}
namespace net {
namespace test {
class MockNetworkChangeNotifier;
}
}
namespace ui {
class ScopedOleInitializer;
}
namespace content {
class BrowserContext;
class ContentBrowserConsistencyChecker;
class InputMsgWatcher;
class MockAgentSchedulingGroupHostFactory;
class MockRenderProcessHost;
class MockRenderProcessHostFactory;
class NavigationController;
class RenderProcessHostFactory;
class TestNavigationURLLoaderFactory;
class TestPageFactory;
class TestRenderFrameHostFactory;
class TestRenderViewHostFactory;
class TestRenderWidgetHostFactory;
class WebContents;
class RenderFrameHostTester {
public:
enum class HeavyAdIssueType {
kNetworkTotal,
kCpuTotal,
kCpuPeak,
kAll,
};
static RenderFrameHostTester* For(RenderFrameHost* host);
static bool TestOnMessageReceived(RenderFrameHost* rfh,
const IPC::Message& msg);
static void CommitPendingLoad(NavigationController* controller);
virtual ~RenderFrameHostTester() {}
virtual void InitializeRenderFrameIfNeeded() = 0;
virtual RenderFrameHost* AppendChild(const std::string& frame_name) = 0;
virtual RenderFrameHost* AppendChildWithPolicy(
const std::string& frame_name,
const blink::ParsedPermissionsPolicy& allow) = 0;
virtual RenderFrameHost* AppendCredentiallessChild(
const std::string& frame_name) = 0;
virtual void Detach() = 0;
virtual void SimulateBeforeUnloadCompleted(bool proceed) = 0;
virtual void SimulateUnloadACK() = 0;
virtual void SimulateUserActivation() = 0;
virtual const std::vector<std::string>& GetConsoleMessages() = 0;
virtual int GetHeavyAdIssueCount(HeavyAdIssueType type) = 0;
virtual void SimulateManifestURLUpdate(const GURL& manifest_url) = 0;
virtual RenderFrameHost* AppendFencedFrame() = 0;
#if !BUILDFLAG(IS_ANDROID)
virtual void CreateHidServiceForTesting(
mojo::PendingReceiver<blink::mojom::HidService> receiever) = 0;
#endif
virtual void CreateWebUsbServiceForTesting(
mojo::PendingReceiver<blink::mojom::WebUsbService> receiver) = 0;
};
class RenderViewHostTester {
public:
static RenderViewHostTester* For(RenderViewHost* host);
static std::unique_ptr<content::InputMsgWatcher> CreateInputWatcher(
RenderViewHost* rvh,
blink::WebInputEvent::Type type);
static void SendTouchEvent(RenderViewHost* rvh,
blink::SyntheticWebTouchEvent* touch_event);
virtual ~RenderViewHostTester() {}
virtual bool CreateTestRenderView() = 0;
virtual void SimulateWasHidden() = 0;
virtual void SimulateWasShown() = 0;
virtual blink::web_pref::WebPreferences TestComputeWebPreferences() = 0;
};
class RenderViewHostTestEnabler {
public:
enum class NavigationURLLoaderFactoryType {
kTest,
kNone,
};
explicit RenderViewHostTestEnabler(
NavigationURLLoaderFactoryType navigation_url_loader_factory_type =
NavigationURLLoaderFactoryType::kTest);
RenderViewHostTestEnabler(const RenderViewHostTestEnabler&) = delete;
RenderViewHostTestEnabler& operator=(const RenderViewHostTestEnabler&) =
delete;
~RenderViewHostTestEnabler();
friend class RenderViewHostTestHarness;
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<display::Screen> screen_;
#endif
std::unique_ptr<base::test::SingleThreadTaskEnvironment> task_environment_;
std::unique_ptr<MockRenderProcessHostFactory> rph_factory_;
std::unique_ptr<MockAgentSchedulingGroupHostFactory> asgh_factory_;
std::unique_ptr<TestPageFactory> page_factory_;
std::unique_ptr<TestRenderViewHostFactory> rvh_factory_;
std::unique_ptr<TestRenderFrameHostFactory> rfh_factory_;
std::unique_ptr<TestRenderWidgetHostFactory> rwhi_factory_;
std::unique_ptr<TestNavigationURLLoaderFactory> loader_factory_;
};
class RenderViewHostTestHarness : public ::testing::Test {
public:
template <typename... TaskEnvironmentTraits>
explicit RenderViewHostTestHarness(TaskEnvironmentTraits&&... traits)
: RenderViewHostTestHarness(std::make_unique<BrowserTaskEnvironment>(
std::forward<TaskEnvironmentTraits>(traits)...)) {}
RenderViewHostTestHarness(const RenderViewHostTestHarness&) = delete;
RenderViewHostTestHarness& operator=(const RenderViewHostTestHarness&) =
delete;
~RenderViewHostTestHarness() override;
NavigationController& controller();
WebContents* web_contents();
RenderViewHost* rvh();
RenderFrameHost* main_rfh();
BrowserContext* browser_context();
MockRenderProcessHost* process();
void DeleteContents();
void SetContents(std::unique_ptr<WebContents> contents);
std::unique_ptr<WebContents> CreateTestWebContents();
void NavigateAndCommit(
const GURL& url,
ui::PageTransition transition = ui::PAGE_TRANSITION_LINK);
void FocusWebContentsOnMainFrame();
protected:
void SetUp() override;
void TearDown() override;
virtual std::unique_ptr<BrowserContext> CreateBrowserContext();
virtual BrowserContext* GetBrowserContext();
BrowserTaskEnvironment* task_environment() { return task_environment_.get(); }
#if defined(USE_AURA)
aura::Window* root_window() { return aura_test_helper_->GetContext(); }
#endif
void SetRenderProcessHostFactory(RenderProcessHostFactory* factory);
private:
explicit RenderViewHostTestHarness(
std::unique_ptr<BrowserTaskEnvironment> task_environment);
std::unique_ptr<BrowserTaskEnvironment> task_environment_;
std::unique_ptr<ContentBrowserConsistencyChecker> consistency_checker_;
std::unique_ptr<net::test::MockNetworkChangeNotifier>
network_change_notifier_;
std::unique_ptr<BrowserContext> browser_context_;
std::unique_ptr<RenderViewHostTestEnabler> rvh_test_enabler_;
std::unique_ptr<WebContents> contents_;
#if BUILDFLAG(IS_WIN)
std::unique_ptr<ui::ScopedOleInitializer> ole_initializer_;
#endif
#if BUILDFLAG(IS_MAC)
std::unique_ptr<display::ScopedNativeScreen> screen_;
#endif
#if defined(USE_AURA)
std::unique_ptr<aura::test::AuraTestHelper> aura_test_helper_;
#endif
raw_ptr<RenderProcessHostFactory> factory_ = nullptr;
};
}
#endif