Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Compositor/RenderClock.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/WebDriver/InputState.h>
#include <WebContent/ConnectionFromClient.h>
#include <WebContent/PageClient.h>
#include <WebContent/TestConnection.h>
namespace WebContent {
TestConnection::TestConnection(NonnullOwnPtr<IPC::Transport> transport, WebContent::ConnectionFromClient& client)
: IPC::ConnectionFromClient<WebContentTestClientEndpoint, WebContentTestServerEndpoint>(*this, move(transport), 0)
, m_client(client)
{
}
TestConnection::~TestConnection() = default;
// This transport has its own send queue, so what the main connection queued first has to reach its socket first.
void TestConnection::prepare_to_send()
{
m_client.transport().flush();
}
void TestConnection::die()
{
}
void TestConnection::reset_state_for_testing(Web::PageId page_id)
{
m_client.dispatch_pending_messages();
if (auto page = m_client.page(page_id); page.has_value()) {
// A timed-out test skips its cleanup, and document replacement preserves the stationary pointer.
for (auto const& navigable : page->page().hosted_navigables()) {
navigable->event_handler().clear_per_test_input_state();
navigable->event_handler().clear_hover_for_next_test();
// The compositor keeps wheel gestures and scroll momentum of its own, and a gesture the previous test left
// unended would take in the next test's wheel steps.
if (navigable->has_compositor_context())
navigable->compositor_context().clear_per_test_input_state_for_testing();
}
Web::WebDriver::reset_input_state(*page->page().top_level_traversable());
// A test that times out may never restore automatic rendering in its finally block. Both controls outlive
// the document, so restore them at the test boundary before acknowledging the reset.
Web::Compositor::RenderClock::the().set_manual_for_testing(false);
page->page().client().set_manual_rendering_opportunities(false);
as<Web::HTML::LocalTraversableNavigable>(*page->page().local_traversable()).reset_session_history_for_testing();
// The navigable outlives the document too, and with it the navigations and history updates the previous test
// made, so a test that made many of them would leave the next test in this view blocked from making its own.
page->page().local_traversable()->reset_navigation_or_history_update_rate_limit_for_testing();
// A test that sets a preferred color scheme may never put it back, and the override outlives the document.
page->page().set_preferred_color_scheme_override_for_testing({});
// A test that asks for accessibility updates never stops asking, and the page outlives the document, so every
// later test in this view would build and serialize the accessibility tree on each change.
page->page().set_accessibility_interested(false);
auto active_entry = page->page().local_traversable()->active_session_history_entry();
VERIFY(active_entry);
async_did_reset_state_for_testing(page_id, Web::HTML::create_session_history_entry_descriptor(*active_entry));
}
}
}