Truly independent web browser
LibWeb+WebContent: Reset the history rate limit between tests
sideshowbarker0cb40ded7221
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex bee714b90d3..b6663914fc2 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -1675,6 +1675,12 @@ LocalNavigable::NavigationOrHistoryUpdatePermission LocalNavigable::allowed_to_preturn NavigationOrHistoryUpdatePermission::Allowed;}+void LocalNavigable::reset_navigation_or_history_update_rate_limit_for_testing()+{+ m_navigation_or_history_update_rate_limit_window_start = MonotonicTime::now_coarse();+ m_navigation_or_history_updates_in_rate_limit_window = 0;+}+Optional<u64> LocalNavigable::browsing_context_group_id() const{Navigable const* top_level_traversable = this;diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.hindex d965c47161c..60f38d23aeb 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.h+++ b/Libraries/LibWeb/HTML/LocalNavigable.h@@ -233,6 +233,7 @@ public:Blocked,};NavigationOrHistoryUpdatePermission allowed_to_perform_a_navigation_or_history_update();+ void reset_navigation_or_history_update_rate_limit_for_testing();virtual bool is_local_navigable() const override { return true; }diff --git a/Services/WebContent/TestConnection.cpp b/Services/WebContent/TestConnection.cppindex 246a4e85b83..6e730773497 100644--- a/Services/WebContent/TestConnection.cpp+++ b/Services/WebContent/TestConnection.cpp@@ -40,6 +40,9 @@ void TestConnection::reset_state_for_testing(Web::PageId page_id)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();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));diff --git a/Tests/LibWeb/test-web/CMakeLists.txt b/Tests/LibWeb/test-web/CMakeLists.txtindex 5d4c24d88ac..4422e5038ee 100644--- a/Tests/LibWeb/test-web/CMakeLists.txt+++ b/Tests/LibWeb/test-web/CMakeLists.txt@@ -37,6 +37,13 @@ if (BUILD_TESTING))set_tests_properties(LibWebManualRenderingReset PROPERTIES TIMEOUT 240)+ add_test(+ NAME LibWebHistoryRateLimitReset+ COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test-history-rate-limit-reset.py+ $<TARGET_FILE:test-web> ${LADYBIRD_SOURCE_DIR}+ )+ set_tests_properties(LibWebHistoryRateLimitReset PROPERTIES TIMEOUT 120)+add_test(NAME LibWebCOMMAND $<TARGET_FILE:test-web> --python-executable ${Python3_EXECUTABLE} --per-test-timeout 120 -v -vdiff --git a/Tests/LibWeb/test-web/test-history-rate-limit-reset.py b/Tests/LibWeb/test-web/test-history-rate-limit-reset.pynew file mode 100644index 00000000000..7c0db0b4e0f--- /dev/null+++ b/Tests/LibWeb/test-web/test-history-rate-limit-reset.py@@ -0,0 +1,102 @@+#!/usr/bin/env python3+#+# Copyright (c) 2026-present, the Ladybird developers.+#+# SPDX-License-Identifier: BSD-2-Clause++import argparse+import os+import re+import shutil+import subprocess+import sys+import tempfile++from pathlib import Path+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument("test_web", type=Path)+ parser.add_argument("source", type=Path)+ args = parser.parse_args()+ source = args.source.resolve()++ with tempfile.TemporaryDirectory(prefix="test-web-history-rate-limit-reset-") as temporary:+ root = Path(temporary)+ for directory in ("Accessibility", "Layout", "Text", "Ref", "Screenshot"):+ for kind in ("input", "expected"):+ (root / directory / kind).mkdir(parents=True)+ (root / "Crash").mkdir()+ (root / "TestConfig.ini").write_text("")+ (root / "Fixtures").symlink_to(source / "Tests/LibWeb/Fixtures", target_is_directory=True)+ (root / "test-web").symlink_to(source / "Tests/LibWeb/test-web", target_is_directory=True)+ (root / "Text/input/wpt-import").mkdir()+ shutil.copyfile(source / "Tests/LibWeb/Text/input/include.js", root / "Text/input/include.js")+ # More history updates than a navigable may make in one rate-limit window.+ (root / "Text/input/flood.html").write_text(+ '<!doctype html><script src="include.js"></script><script>asyncTest(done => {'+ 'console.log("HISTORY_RESET_FLOOD");'+ 'for (let i = 0; i < 250; ++i) history.pushState(null, "", "#" + i);'+ 'println("flooded"); done();'+ "});</script>"+ )+ (root / "Text/expected/flood.txt").write_text("flooded\n")+ # Text tests precede reference tests. At -j1 these two files use the same view, without relying on directory+ # order. The page turns green only if its own history update goes through.+ (root / "Ref/input/consumer.html").write_text(+ '<!doctype html><html class="reftest-wait">'+ '<link rel="match" href="../expected/green.html">'+ "<style>html{background:red}body{margin:0}</style>"+ '<script>console.log("HISTORY_RESET_CONSUMER");'+ "const length = history.length;"+ 'history.pushState(null, "", "#consumer");'+ 'if (history.length === length + 1) document.documentElement.style.background = "green";'+ 'requestAnimationFrame(() => document.documentElement.classList.remove("reftest-wait"));'+ "</script></html>"+ )+ (root / "Ref/expected/green.html").write_text(+ "<!doctype html><style>html,body{margin:0;background:green}</style>"+ )++ results = root / "results"+ env = os.environ.copy()+ env["LADYBIRD_SOURCE_DIR"] = str(source)+ # The echo server imports its vendored tools even though these fixtures use only file URLs. Keep those tools+ # outside the private input tree so test discovery cannot collect their own HTML fixtures.+ wpt = source / "Tests/LibWeb/Text/input/wpt-import"+ env["PYTHONPATH"] = os.pathsep.join((str(wpt), str(wpt / "_wpttools")))+ process = subprocess.run(+ [+ str(args.test_web.resolve()),+ "--test-path",+ str(root),+ "-j1",+ "-v",+ "-v",+ "--per-test-timeout",+ "30",+ "--python-executable",+ sys.executable,+ "--results-dir",+ str(results),+ ],+ env=env,+ stdout=subprocess.PIPE,+ stderr=subprocess.STDOUT,+ text=True,+ timeout=100,+ )+ print(process.stdout, flush=True)+ assert process.returncode == 0, f"unexpected exit status {process.returncode}"+ status = dict(+ line.split(": ", 1) for line in (results / "harness-status.txt").read_text().splitlines() if ": " in line+ )+ assert status["completed"] == "2/2" and status["pass"] == "2", status+ processes = re.findall(r"WebContent\((\d+)\).*HISTORY_RESET_(FLOOD|CONSUMER)", process.stdout)+ assert len(processes) == 2 and processes[0][1] == "FLOOD" and processes[1][1] == "CONSUMER", processes+ assert processes[0][0] == processes[1][0], f"WebContent was not reused: {processes}"+++if __name__ == "__main__":+ main()