Skip to content

Commit

Permalink
headless-browser: Let tests set their own timeout duration
Browse files Browse the repository at this point in the history
Some tests take longer than others, and so may want to set a custom
timeout so that they pass, without increasing the timeout for all other
tests. For example, this is done in WPT.

Add an `internals.setTestTimeout(milliseconds)` method that overrides
the test runner's default timeout for the currently-run test.
  • Loading branch information
AtkinsSJ committed Dec 19, 2024
1 parent 61755b2 commit 6b9539e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Libraries/LibWeb/Internals/Internals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void Internals::signal_text_test_is_done(String const& text)
internals_page().client().page_did_finish_text_test(text);
}

void Internals::set_test_timeout(double milliseconds)
{
internals_page().client().page_did_set_test_timeout(milliseconds);
}

void Internals::gc()
{
vm().heap().collect_garbage();
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibWeb/Internals/Internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Internals final : public Bindings::PlatformObject {
virtual ~Internals() override;

void signal_text_test_is_done(String const& text);
void set_test_timeout(double milliseconds);

void gc();
JS::Object* hit_test(double x, double y);
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibWeb/Internals/Internals.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
interface Internals {

undefined signalTextTestIsDone(DOMString text);
undefined setTestTimeout(double milliseconds);

undefined gc();
object hitTest(double x, double y);

Expand Down
1 change: 1 addition & 0 deletions Libraries/LibWeb/Page/Page.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class PageClient : public JS::Cell {
virtual void page_did_request_select_dropdown([[maybe_unused]] Web::CSSPixelPoint content_position, [[maybe_unused]] Web::CSSPixels minimum_width, [[maybe_unused]] Vector<Web::HTML::SelectItem> items) { }

virtual void page_did_finish_text_test([[maybe_unused]] String const& text) { }
virtual void page_did_set_test_timeout([[maybe_unused]] double milliseconds) { }

virtual void page_did_change_theme_color(Gfx::Color) { }

Expand Down
1 change: 1 addition & 0 deletions Libraries/LibWebView/ViewImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class ViewImplementation {
Function<void(Web::KeyEvent const&)> on_finish_handling_key_event;
Function<void(Web::DragEvent const&)> on_finish_handling_drag_event;
Function<void(String const&)> on_text_test_finish;
Function<void(double milliseconds)> on_set_test_timeout;
Function<void(size_t current_match_index, Optional<size_t> const& total_match_count)> on_find_in_page;
Function<void(Gfx::Color)> on_theme_color_change;
Function<void(String const&, String const&, String const&)> on_insert_clipboard_entry;
Expand Down
8 changes: 8 additions & 0 deletions Libraries/LibWebView/WebContentClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ void WebContentClient::did_finish_text_test(u64 page_id, String const& text)
}
}

void WebContentClient::did_set_test_timeout(u64 page_id, double milliseconds)
{
if (auto view = view_for_page_id(page_id); view.has_value()) {
if (view->on_set_test_timeout)
view->on_set_test_timeout(milliseconds);
}
}

void WebContentClient::did_find_in_page(u64 page_id, size_t current_match_index, Optional<size_t> const& total_match_count)
{
if (auto view = view_for_page_id(page_id); view.has_value()) {
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibWebView/WebContentClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class WebContentClient final
virtual void did_request_select_dropdown(u64 page_id, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> const& items) override;
virtual void did_finish_handling_input_event(u64 page_id, Web::EventResult event_result) override;
virtual void did_finish_text_test(u64 page_id, String const& text) override;
virtual void did_set_test_timeout(u64 page_id, double milliseconds) override;
virtual void did_find_in_page(u64 page_id, size_t current_match_index, Optional<size_t> const& total_match_count) override;
virtual void did_change_theme_color(u64 page_id, Gfx::Color color) override;
virtual void did_insert_clipboard_entry(u64 page_id, String const& data, String const& presentation_style, String const& mime_type) override;
Expand Down
5 changes: 5 additions & 0 deletions Services/WebContent/PageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ void PageClient::page_did_finish_text_test(String const& text)
client().async_did_finish_text_test(m_id, text);
}

void PageClient::page_did_set_test_timeout(double milliseconds)
{
client().async_did_set_test_timeout(m_id, milliseconds);
}

void PageClient::page_did_request_context_menu(Web::CSSPixelPoint content_position)
{
client().async_did_request_context_menu(m_id, page().css_to_device_point(content_position).to_type<int>());
Expand Down
1 change: 1 addition & 0 deletions Services/WebContent/PageClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class PageClient final : public Web::PageClient {
virtual void page_did_request_file_picker(Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles) override;
virtual void page_did_request_select_dropdown(Web::CSSPixelPoint content_position, Web::CSSPixels minimum_width, Vector<Web::HTML::SelectItem> items) override;
virtual void page_did_finish_text_test(String const& text) override;
virtual void page_did_set_test_timeout(double milliseconds) override;
virtual void page_did_change_theme_color(Gfx::Color color) override;
virtual void page_did_insert_clipboard_entry(String data, String presentation_style, String mime_type) override;
virtual void page_did_change_audio_play_state(Web::HTML::AudioPlayState) override;
Expand Down
1 change: 1 addition & 0 deletions Services/WebContent/WebContentClient.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ endpoint WebContentClient
did_get_js_console_messages(u64 page_id, i32 start_index, Vector<ByteString> message_types, Vector<ByteString> messages) =|

did_finish_text_test(u64 page_id, String text) =|
did_set_test_timeout(u64 page_id, double milliseconds) =|

did_find_in_page(u64 page_id, size_t current_match_index, Optional<size_t> total_match_count) =|

Expand Down
18 changes: 18 additions & 0 deletions UI/Headless/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void run_dump_test(HeadlessWebView& view, Test& test, URL::URL const& url, int t
auto timer = Core::Timer::create_single_shot(timeout_in_milliseconds, [&view, &test]() {
view.on_load_finish = {};
view.on_text_test_finish = {};
view.on_set_test_timeout = {};

view.on_test_complete({ test, TestResult::Timeout });
});
Expand Down Expand Up @@ -233,6 +234,14 @@ void run_dump_test(HeadlessWebView& view, Test& test, URL::URL const& url, int t
};
}

view.on_set_test_timeout = [&test, timer, timeout_in_milliseconds](double milliseconds) {
if (milliseconds == timeout_in_milliseconds)
return;
timer->stop();
auto milliseconds_elapsed = (UnixDateTime::now() - test.start_time).to_milliseconds();
timer->start(milliseconds - milliseconds_elapsed);
};

view.load(url);
timer->start();
}
Expand All @@ -242,6 +251,7 @@ static void run_ref_test(HeadlessWebView& view, Test& test, URL::URL const& url,
auto timer = Core::Timer::create_single_shot(timeout_in_milliseconds, [&view, &test]() {
view.on_load_finish = {};
view.on_text_test_finish = {};
view.on_set_test_timeout = {};

view.on_test_complete({ test, TestResult::Timeout });
});
Expand Down Expand Up @@ -315,6 +325,14 @@ static void run_ref_test(HeadlessWebView& view, Test& test, URL::URL const& url,
dbgln("Unexpected text test finished during ref test for {}", url);
};

view.on_set_test_timeout = [&test, timer, timeout_in_milliseconds](double milliseconds) {
if (milliseconds == timeout_in_milliseconds)
return;
timer->stop();
auto milliseconds_elapsed = (UnixDateTime::now() - test.start_time).to_milliseconds();
timer->start(milliseconds - milliseconds_elapsed);
};

view.load(url);
timer->start();
}
Expand Down

0 comments on commit 6b9539e

Please sign in to comment.