Skip to content

Commit

Permalink
headless-browser: Implement dialog-related WebView callbacks
Browse files Browse the repository at this point in the history
This allows us to headlessly run WPT tests which involve dialogs.
  • Loading branch information
trflynn89 authored and awesomekling committed Oct 28, 2024
1 parent 6590b8a commit 777eec0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Ladybird/Headless/HeadlessWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,62 @@ HeadlessWebView::HeadlessWebView(Core::AnonymousBuffer theme, Gfx::IntSize viewp

return worker_client->clone_transport();
};

on_request_alert = [this](auto const&) {
m_pending_dialog = Web::Page::PendingDialog::Alert;
};

on_request_confirm = [this](auto const&) {
m_pending_dialog = Web::Page::PendingDialog::Confirm;
};

on_request_prompt = [this](auto const&, auto const& prompt_text) {
m_pending_dialog = Web::Page::PendingDialog::Prompt;
m_pending_prompt_text = prompt_text;
};

on_request_set_prompt_text = [this](auto const& prompt_text) {
m_pending_prompt_text = prompt_text;
};

on_request_accept_dialog = [this]() {
switch (m_pending_dialog) {
case Web::Page::PendingDialog::None:
VERIFY_NOT_REACHED();
break;
case Web::Page::PendingDialog::Alert:
alert_closed();
break;
case Web::Page::PendingDialog::Confirm:
confirm_closed(true);
break;
case Web::Page::PendingDialog::Prompt:
prompt_closed(move(m_pending_prompt_text));
break;
}

m_pending_dialog = Web::Page::PendingDialog::None;
};

on_request_dismiss_dialog = [this]() {
switch (m_pending_dialog) {
case Web::Page::PendingDialog::None:
VERIFY_NOT_REACHED();
break;
case Web::Page::PendingDialog::Alert:
alert_closed();
break;
case Web::Page::PendingDialog::Confirm:
confirm_closed(false);
break;
case Web::Page::PendingDialog::Prompt:
prompt_closed({});
break;
}

m_pending_dialog = Web::Page::PendingDialog::None;
m_pending_prompt_text.clear();
};
}

NonnullOwnPtr<HeadlessWebView> HeadlessWebView::create(Core::AnonymousBuffer theme, Gfx::IntSize window_size)
Expand Down
4 changes: 4 additions & 0 deletions Ladybird/Headless/HeadlessWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibCore/Promise.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Size.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/PixelUnits.h>
#include <LibWebView/ViewImplementation.h>

Expand Down Expand Up @@ -48,6 +49,9 @@ class HeadlessWebView final : public WebView::ViewImplementation {
RefPtr<Core::Promise<RefPtr<Gfx::Bitmap>>> m_pending_screenshot;

NonnullRefPtr<TestPromise> m_test_promise;

Web::Page::PendingDialog m_pending_dialog { Web::Page::PendingDialog::None };
Optional<String> m_pending_prompt_text;
};

}

0 comments on commit 777eec0

Please sign in to comment.