Truly independent web browser
UI/Qt: Add beforeunload presentation to JavaScriptDialog
Sam Atkins0ab147671f57
diff --git a/Tests/UI/Qt/TestJavaScriptDialog.cpp b/Tests/UI/Qt/TestJavaScriptDialog.cppindex e19febf40fd..16e51229924 100644--- a/Tests/UI/Qt/TestJavaScriptDialog.cpp+++ b/Tests/UI/Qt/TestJavaScriptDialog.cpp@@ -16,6 +16,7 @@#include <QMouseEvent>#include <QPushButton>#include <QScrollArea>+#include <QScrollBar>#include <QShortcut>#include <QStackedWidget>#include <QVBoxLayout>@@ -181,6 +182,59 @@ TEST_CASE(prompt_supports_defaults_updates_and_keyboard_completion)EXPECT(!completion.accepted);}+TEST_CASE(beforeunload_uses_fixed_copy_and_navigation_button_roles)+{+ QWidget web_view;+ web_view.resize(800, 600);+ web_view.show();++ Ladybird::JavaScriptDialog dialog(&web_view);+ Completion completion;+ record_completions(dialog, completion);++ dialog.show_before_unload("https://example.com");+ QApplication::processEvents();+ auto* title_label = dialog.findChild<QLabel*>("LadybirdJavaScriptDialogTitle");+ auto* message_label = dialog.findChild<QLabel*>("LadybirdJavaScriptDialogMessage");+ auto* message_area = dialog.findChild<QScrollArea*>("LadybirdJavaScriptDialogMessageArea");+ auto* button_box = dialog.findChild<QDialogButtonBox*>("LadybirdJavaScriptDialogButtons");+ VERIFY(title_label);+ VERIFY(message_label);+ VERIFY(message_area);+ VERIFY(button_box);+ auto* leave_button = button_box->button(QDialogButtonBox::Ok);+ auto* stay_button = button_box->button(QDialogButtonBox::Cancel);+ VERIFY(leave_button);+ VERIFY(stay_button);+ EXPECT(title_label->text() == "https://example.com");+ EXPECT(message_label->text() == "Leave this page? Your changes may not be saved.");+ EXPECT(leave_button->text() == "Leave");+ EXPECT(stay_button->text() == "Stay");+ EXPECT(leave_button->isDefault());+ EXPECT(dialog.focusWidget() == leave_button);+ EXPECT(!message_area->verticalScrollBar()->isVisible());+ EXPECT(message_area->viewport()->height() >= message_label->heightForWidth(message_area->viewport()->width()));++ send_key(*leave_button, Qt::Key_Return);+ EXPECT(completion.type == Ladybird::JavaScriptDialog::Type::BeforeUnload);+ EXPECT(completion.accepted);++ completion = {};+ dialog.show_before_unload("https://example.com");+ trigger_shortcut(dialog, Qt::Key_Escape);+ EXPECT(completion.type == Ladybird::JavaScriptDialog::Type::BeforeUnload);+ EXPECT(!completion.accepted);++ completion = {};+ dialog.show_before_unload("https://example.com");+ stay_button = button_box->button(QDialogButtonBox::Cancel);+ VERIFY(stay_button);+ stay_button->setFocus();+ send_key(*stay_button, Qt::Key_Return);+ EXPECT(completion.type == Ladybird::JavaScriptDialog::Type::BeforeUnload);+ EXPECT(!completion.accepted);+}+TEST_CASE(dialog_resizes_with_and_blocks_only_its_parent_view){QWidget window;diff --git a/UI/Qt/JavaScriptDialog.cpp b/UI/Qt/JavaScriptDialog.cppindex e223c98f46d..fa8235477a0 100644--- a/UI/Qt/JavaScriptDialog.cpp+++ b/UI/Qt/JavaScriptDialog.cpp@@ -118,6 +118,11 @@ void JavaScriptDialog::show_alert(QString const& title, QString const& message)open(Type::Alert, title, message);}+void JavaScriptDialog::show_before_unload(QString const& title)+{+ open(Type::BeforeUnload, title, "Leave this page? Your changes may not be saved.");+}+void JavaScriptDialog::show_confirm(QString const& title, QString const& message){open(Type::Confirm, title, message);@@ -155,6 +160,12 @@ void JavaScriptDialog::open(Type type, QString const& title, QString const& mess}auto* ok_button = m_button_box->button(QDialogButtonBox::Ok);auto* cancel_button = m_button_box->button(QDialogButtonBox::Cancel);+ if (type == Type::BeforeUnload) {+ VERIFY(ok_button);+ VERIFY(cancel_button);+ ok_button->setText("Leave");+ cancel_button->setText("Stay");+ }if (ok_button)ok_button->setDefault(type != Type::Prompt);diff --git a/UI/Qt/JavaScriptDialog.h b/UI/Qt/JavaScriptDialog.hindex 1837dd3bda8..d5a46bd3927 100644--- a/UI/Qt/JavaScriptDialog.h+++ b/UI/Qt/JavaScriptDialog.h@@ -27,6 +27,7 @@ public:enum class Type {Alert,+ BeforeUnload,Confirm,Prompt,};@@ -36,6 +37,7 @@ public:bool is_open() const { return m_type.has_value(); }void show_alert(QString const& title, QString const& message);+ void show_before_unload(QString const& title);void show_confirm(QString const& title, QString const& message);void show_prompt(QString const& title, QString const& message, QString const& default_text);void set_prompt_text(QString const& text);diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 3cec7a3831e..d0ae6c5e562 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -854,6 +854,8 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traverscase JavaScriptDialog::Type::Alert:view().alert_closed();break;+ case JavaScriptDialog::Type::BeforeUnload:+ VERIFY_NOT_REACHED();case JavaScriptDialog::Type::Confirm:view().confirm_closed(accepted);break;