#ifndef CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
#define CONTENT_PUBLIC_TEST_RENDER_VIEW_TEST_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <string_view>
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/test_io_thread.h"
#include "build/build_config.h"
#include "components/input/native_web_keyboard_event.h"
#include "content/public/common/main_function_params.h"
#include "content/public/test/mock_policy_container_host.h"
#include "content/public/test/mock_render_thread.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/binder_map.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/page_state/page_state.h"
#include "third_party/blink/public/mojom/page/page.mojom.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_input_element.h"
#include "v8/include/v8-forward.h"
#if BUILDFLAG(IS_MAC)
#include <optional>
#include "base/apple/scoped_nsautorelease_pool.h"
#include "base/memory/stack_allocated.h"
#endif
namespace blink {
class PageState;
namespace scheduler {
class WebThreadScheduler;
}
struct VisualProperties;
class WebFrameWidget;
class WebGestureEvent;
class WebMouseEvent;
}
namespace gfx {
class Rect;
class Size;
}
namespace content {
class AgentSchedulingGroup;
class ContentBrowserClient;
class ContentClient;
class ContentRendererClient;
class FakeRenderWidgetHost;
class RendererMainPlatformDelegate;
class RendererBlinkPlatformImpl;
class RendererBlinkPlatformImplTestOverrideImpl;
class RenderFrame;
class RenderProcess;
class RenderView;
class RenderViewTest : public testing::Test {
public:
class RendererBlinkPlatformImplTestOverride {
public:
RendererBlinkPlatformImplTestOverride();
~RendererBlinkPlatformImplTestOverride();
RendererBlinkPlatformImpl* Get() const;
void Initialize();
void Shutdown();
blink::scheduler::WebThreadScheduler* GetMainThreadScheduler() {
return main_thread_scheduler_.get();
}
private:
std::unique_ptr<blink::scheduler::WebThreadScheduler>
main_thread_scheduler_;
std::unique_ptr<RendererBlinkPlatformImplTestOverrideImpl>
blink_platform_impl_;
};
explicit RenderViewTest(bool hook_render_frame_creation = true);
~RenderViewTest() override;
protected:
blink::WebLocalFrame* GetMainFrame();
RenderFrame* GetMainRenderFrame();
v8::Isolate* Isolate();
void ExecuteJavaScriptForTests(std::string_view js);
bool ExecuteJavaScriptAndReturnIntValue(const std::u16string& script,
int* result);
bool ExecuteJavaScriptAndReturnNumberValue(const std::u16string& script,
double* result);
void LoadHTML(std::string_view html);
void LoadHTMLWithUrlOverride(std::string_view html, std::string_view url);
blink::PageState GetCurrentPageState();
void GoBack(const GURL& url, const blink::PageState& state);
void GoForward(const GURL& url, const blink::PageState& state);
void SendNativeKeyEvent(const input::NativeWebKeyboardEvent& key_event);
void SendWebKeyboardEvent(const blink::WebKeyboardEvent& key_event);
void SendWebMouseEvent(const blink::WebMouseEvent& mouse_event);
void SendWebGestureEvent(const blink::WebGestureEvent& gesture_event);
gfx::Rect GetElementBounds(const std::string& element_id);
bool SimulateElementClick(const std::string& element_id);
void SimulatePointClick(const gfx::Point& point);
bool SimulateElementRightClick(const std::string& element_id);
void SimulatePointRightClick(const gfx::Point& point);
void SimulateRectTap(const gfx::Rect& rect);
void SetFocused(const blink::WebElement& element);
void ChangeFocusToNull(const blink::WebDocument& document);
void Reload(const GURL& url);
void Resize(gfx::Size new_size, bool is_fullscreen);
void SimulateUserTypingASCIICharacter(char ascii_character,
bool flush_message_loop);
void SimulateUserInputChangeForElement(blink::WebInputElement input,
std::string_view new_value);
void SimulateUserInputChangeForElementById(std::string_view id,
std::string_view new_value);
void OnSameDocumentNavigation(blink::WebLocalFrame* frame,
bool is_new_navigation);
blink::WebFrameWidget* GetWebFrameWidget();
virtual ContentClient* CreateContentClient();
virtual ContentBrowserClient* CreateContentBrowserClient();
virtual ContentRendererClient* CreateContentRendererClient();
virtual std::unique_ptr<FakeRenderWidgetHost> CreateRenderWidgetHost();
virtual blink::VisualProperties InitialVisualProperties();
void SetUp() override;
void TearDown() override;
void CreateFakeURLLoaderFactory();
base::test::TaskEnvironment task_environment_;
std::unique_ptr<RenderProcess> process_;
mojo::AssociatedRemote<blink::mojom::PageBroadcast> page_broadcast_;
raw_ptr<blink::WebView> web_view_ = nullptr;
RendererBlinkPlatformImplTestOverride blink_platform_impl_;
std::unique_ptr<ContentBrowserClient> content_browser_client_;
std::unique_ptr<ContentRendererClient> content_renderer_client_;
std::unique_ptr<ContentClient> content_client_;
std::unique_ptr<MockRenderThread> render_thread_;
std::unique_ptr<AgentSchedulingGroup> agent_scheduling_group_;
std::unique_ptr<FakeRenderWidgetHost> render_widget_host_;
std::unique_ptr<MockPolicyContainerHost> policy_container_host_;
std::unique_ptr<RendererMainPlatformDelegate> platform_;
std::unique_ptr<MainFunctionParams> params_;
std::unique_ptr<base::CommandLine> command_line_;
std::unique_ptr<base::TestIOThread> test_io_thread_;
std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
mojo::BinderMap binders_;
#if BUILDFLAG(IS_MAC)
STACK_ALLOCATED_IGNORE("https://crbug.com/1424190")
std::optional<base::apple::ScopedNSAutoreleasePool> autorelease_pool_;
#endif
private:
void GoToOffset(int offset, const GURL& url, const blink::PageState& state);
void SendInputEvent(const blink::WebInputEvent& input_event);
};
}
#endif