Skip to content

Truly independent web browser

16.9 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/LexicalPath.h>
#include <AK/ScopeGuard.h>
#include <AK/Vector.h>
#include <LibCore/Directory.h>
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibFileSystem/FileSystem.h>
#include <LibTest/TestCase.h>
#include <LibWebView/Settings.h>
#include <UI/Qt/CrashReportReviewWidget.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/Theme.h>

#include <QAbstractButton>
#include <QApplication>
#include <QCheckBox>
#include <QCoreApplication>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QStyleHints>
#include <unistd.h>

using Ladybird::CrashReportReviewWidget;
using WebView::ReportSubmission;

static constexpr auto report_text = "Ladybird crash report, format 1\n"
                                    "Process: WebContent\n"
                                    "Version: 1.0\n"
                                    "Termination signal: SIGSEGV\n"
                                    "Termination signal number: 11\n"
                                    "\n"
                                    "Native stack (binary build ID, object address):\n"
                                    "#0 abcdef 0x1234 Web::Page::crash()\n"sv;

static ByteString test_directory()
{
    auto name = ByteString::formatted("test-crash-report-review-widget-{}", getpid());
    return LexicalPath::join(Core::StandardPaths::tempfile_directory(), name).string();
}

static void cleanup()
{
    if (FileSystem::exists(test_directory()))
        MUST(FileSystem::remove(test_directory(), FileSystem::RecursionMode::Allowed));
}

static ByteString write_report(StringView timestamp)
{
    auto name = ByteString::formatted("{}-WebContent-abc123.txt", timestamp);
    auto directory = MUST(Core::Directory::create(test_directory(), Core::Directory::CreateDirectories::Yes));
    auto file = MUST(directory.open(name, Core::File::OpenMode::Write));
    MUST(file->write_until_depleted(report_text.bytes()));
    return name;
}

template<typename T>
static T& child(QWidget& widget, char const* name)
{
    auto* child = widget.findChild<T*>(name);
    VERIFY(child);
    return *child;
}

static QString status_title(QWidget& widget)
{
    return child<QLabel>(widget, "CrashReportStatusTitle").text();
}

TEST_CASE(a_crash_screen_offers_the_website_without_including_it)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name, "https://example.com/page"_string));
    widget.show();

    auto& website = child<QLineEdit>(widget, "CrashReportWebsite");
    auto& include_website = child<QCheckBox>(widget, "CrashReportIncludeWebsite");
    EXPECT_EQ(website.text(), "https://example.com/page");
    EXPECT(!include_website.isChecked());
    EXPECT(!website.isEnabled());
    include_website.setChecked(true);
    EXPECT(website.isEnabled());

    child<QPushButton>(widget, "CrashReportDeclineButton").click();
    EXPECT_EQ(status_title(widget), "Report not sent");
}

// The offscreen platform keeps Qt's light palette whatever color scheme is asked for, as some platform themes do.
TEST_CASE(the_details_of_a_report_take_the_dark_interface_theme_where_qt_keeps_a_light_palette)
{
    cleanup();
    ScopeGuard guard = cleanup;
    Ladybird::Theme::set_interface_theme(WebView::ThemePreference::Dark);
    ScopeGuard restore_theme = [] { Ladybird::Theme::set_interface_theme(WebView::ThemePreference::System); };

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name));
    widget.show();

    QLabel* process = nullptr;
    for (auto* label : widget.findChildren<QLabel*>()) {
        if (label->text() == QStringLiteral("WebContent"))
            process = label;
    }
    VERIFY(process);
    EXPECT(process->palette().color(process->foregroundRole()).lightness() > 128);
}

// A disclosure button has a style sheet of its own, which keeps Qt's cached palette for it through the review's restyle.
TEST_CASE(the_disclosure_follows_a_switch_to_the_dark_interface_theme)
{
    cleanup();
    ScopeGuard guard = cleanup;
    Ladybird::Theme::set_interface_theme(WebView::ThemePreference::Light);
    ScopeGuard restore_theme = [] { Ladybird::Theme::set_interface_theme(WebView::ThemePreference::System); };

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name));
    widget.show();

    Ladybird::Theme::set_interface_theme(WebView::ThemePreference::Dark);
    QEvent palette_change(QEvent::PaletteChange);
    QCoreApplication::sendEvent(&widget, &palette_change);

    size_t disclosures = 0;
    for (auto* button : widget.findChildren<QPushButton*>()) {
        if (button->styleSheet().isEmpty())
            continue;
        ++disclosures;
        EXPECT(button->palette().color(QPalette::ButtonText).lightness() > 128);
    }
    EXPECT(disclosures > 0);
}

TEST_CASE(the_review_offers_to_exit_before_and_after_answering)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    auto exits = 0;
    widget.on_exit = [&] { ++exits; };
    MUST(widget.open_report(name));
    widget.show();

    auto& review_exit = child<QPushButton>(widget, "CrashReportReviewExitButton");
    EXPECT(review_exit.isVisible());
    EXPECT(child<QPushButton>(widget, "CrashReportSendButton").isDefault());
    review_exit.click();
    EXPECT_EQ(exits, 1);

    // Once the report is answered, leaving is what is left to do.
    widget.show_sent();
    auto& exit_button = child<QPushButton>(widget, "CrashReportExitButton");
    EXPECT(exit_button.isVisible());
    EXPECT(exit_button.isDefault());
    exit_button.click();
    EXPECT_EQ(exits, 2);

    // A report that may still be sent offers to try again, or to go back to the review.
    widget.show_failure(ReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
    EXPECT(child<QPushButton>(widget, "CrashReportRetryButton").isVisible());
    EXPECT(child<QPushButton>(widget, "CrashReportCancelButton").isVisible());
    EXPECT(!exit_button.isVisible());
}

TEST_CASE(sending_can_be_canceled_until_the_report_is_sent)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    auto answers = 0;
    widget.on_answered = [&] { ++answers; };
    MUST(widget.open_report(name));
    widget.show();

    auto& description = child<QPlainTextEdit>(widget, "CrashReportDescription");
    description.setPlainText("Opening a menu");
    widget.show_progress(ReportSubmission::Stage::SolvingProof);
    auto& cancel = child<QPushButton>(widget, "CrashReportCancelButton");
    EXPECT(cancel.isVisible());
    EXPECT(!cancel.isDefault());
    EXPECT(!child<QPushButton>(widget, "CrashReportExitButton").isVisible());

    cancel.click();
    EXPECT(child<QPushButton>(widget, "CrashReportSendButton").isVisible());
    EXPECT_EQ(description.toPlainText(), "Opening a menu");
    EXPECT_EQ(answers, 0);

    widget.show_sent();
    EXPECT_EQ(answers, 1);
    EXPECT(!cancel.isVisible());
}

TEST_CASE(a_choice_that_cannot_be_sent_is_explained_in_place)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name, "https://example.com/"_string));
    widget.show();

    child<QCheckBox>(widget, "CrashReportIncludeWebsite").setChecked(true);
    child<QLineEdit>(widget, "CrashReportWebsite").setText("  ");
    child<QPushButton>(widget, "CrashReportSendButton").click();

    auto& validation = child<QLabel>(widget, "CrashReportValidation");
    EXPECT(validation.isVisible());
    EXPECT_EQ(validation.text(), "Enter a website URL or leave the option unchecked.");
    EXPECT(child<QPushButton>(widget, "CrashReportSendButton").isVisible());
}

TEST_CASE(without_a_website_there_is_nothing_to_include)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Continue"), nullptr, store };
    MUST(widget.open_report(name));
    widget.show();

    EXPECT(!child<QCheckBox>(widget, "CrashReportIncludeWebsite").isVisible());
}

TEST_CASE(sending_shows_its_progress_and_outcome)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Continue"), nullptr, store };
    MUST(widget.open_report(name));
    widget.show();

    auto& message = child<QLabel>(widget, "CrashReportStatusMessage");
    auto& retry = child<QPushButton>(widget, "CrashReportRetryButton");

    widget.show_progress(ReportSubmission::Stage::SolvingProof);
    EXPECT_EQ(status_title(widget), "Sending crash report");
    EXPECT_EQ(message.text(), "Preparing report…");

    widget.show_retry("Report server returned 503 while sending the report."_string, 2, 5, 3);
    EXPECT_EQ(message.text(), "Report server returned 503 while sending the report. Trying again in 3 seconds (2 of 5)…");

    // Only a report that failed to reach the server can be sent again.
    widget.show_failure(ReportSubmission::Failure::Sending, "Network error while contacting the report server."_string);
    EXPECT_EQ(status_title(widget), "Couldn’t send report");
    EXPECT(retry.isVisible());

    widget.show_failure(ReportSubmission::Failure::Preparation, "Could not prepare this report."_string);
    EXPECT(!retry.isVisible());
    EXPECT(child<QPushButton>(widget, "CrashReportExitButton").isVisible());

    widget.show_sent();
    EXPECT_EQ(status_title(widget), "Report sent");
    EXPECT_EQ(message.text(), "Thank you for helping Ladybird.");
}

TEST_CASE(the_full_report_is_shown_in_place_of_the_review)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name));
    Vector<bool> text_visibility;
    widget.on_report_text_visibility_change = [&](bool visible) { text_visibility.append(visible); };
    widget.show();

    auto& send = child<QPushButton>(widget, "CrashReportSendButton");
    child<QPushButton>(widget, "CrashReportShowReportButton").click();
    EXPECT_EQ(text_visibility, (Vector<bool> { true }));
    auto& text = child<QPlainTextEdit>(widget, "ReportText");
    EXPECT(text.isVisible());
    EXPECT(!send.isVisible());
    EXPECT_EQ(text.toPlainText(), QString::fromUtf8(report_text.characters_without_null_termination(), report_text.length()));

    child<QPushButton>(widget, "ReportTextBackButton").click();
    EXPECT(!text.isVisible());
    EXPECT(send.isVisible());
    EXPECT_EQ(text_visibility, (Vector<bool> { true, false }));
}

TEST_CASE(the_details_toggle_is_written_like_the_text_around_it)
{
    cleanup();
    ScopeGuard guard = cleanup;

    // Styles such as macOS's give some buttons a smaller font of their own.
    auto label_font = QApplication::font("QLabel");
    auto button_font = label_font;
    button_font.setPointSizeF(label_font.pointSizeF() - 2);
    QApplication::setFont(button_font, "QPushButton");
    ScopeGuard restore_font = [&] { QApplication::setFont(label_font, "QPushButton"); };

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name));

    QPushButton* toggle = nullptr;
    for (auto* button : widget.findChildren<QPushButton*>()) {
        if (button->text().endsWith("Report details"))
            toggle = button;
    }
    VERIFY(toggle);
    EXPECT_EQ(toggle->font().pointSizeF(), child<QLabel>(widget, "CrashReportValidation").font().pointSizeF());
}

TEST_CASE(tab_moves_through_every_control_and_wraps_around)
{
    cleanup();
    ScopeGuard guard = cleanup;

    // As on macOS unless its keyboard navigation setting is on, Tab only moves between text fields, and buttons take focus
    // from Tab alone.
    auto tab_focus_behavior = QGuiApplication::styleHints()->tabFocusBehavior();
    QGuiApplication::styleHints()->setTabFocusBehavior(Qt::TabFocusTextControls);
    ScopeGuard restore_tab_focus_behavior = [&] { QGuiApplication::styleHints()->setTabFocusBehavior(tab_focus_behavior); };

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    MUST(widget.open_report(name, "https://example.com/"_string));
    for (auto* button : widget.findChildren<QAbstractButton*>())
        button->setFocusPolicy(Qt::TabFocus);
    widget.show();

    auto press = [&](Qt::Key key) {
        auto* focused = widget.window()->focusWidget();
        VERIFY(focused);
        QKeyEvent event(QEvent::KeyPress, key, key == Qt::Key_Backtab ? Qt::ShiftModifier : Qt::NoModifier);
        QApplication::sendEvent(focused, &event);
        return widget.window()->focusWidget();
    };

    auto& description = child<QPlainTextEdit>(widget, "CrashReportDescription");
    auto& include_website = child<QCheckBox>(widget, "CrashReportIncludeWebsite");
    auto& send = child<QPushButton>(widget, "CrashReportSendButton");
    auto& exit_button = child<QPushButton>(widget, "CrashReportReviewExitButton");
    description.setFocus();

    // The URL field is skipped while its option is off, and the details' controls while they are collapsed.
    EXPECT_EQ(press(Qt::Key_Tab), &include_website);
    auto* toggle = press(Qt::Key_Tab);
    EXPECT(static_cast<QPushButton*>(toggle)->text().endsWith("Report details"));
    EXPECT_EQ(press(Qt::Key_Tab), &send);
    EXPECT_EQ(press(Qt::Key_Tab), &child<QPushButton>(widget, "CrashReportDeclineButton"));
    EXPECT_EQ(press(Qt::Key_Tab), &exit_button);
    EXPECT_EQ(press(Qt::Key_Tab), &description);
    EXPECT_EQ(press(Qt::Key_Backtab), &exit_button);

    // The details' controls follow the toggle that shows them.
    static_cast<QPushButton*>(toggle)->click();
    toggle->setFocus();
    EXPECT_EQ(press(Qt::Key_Tab), &child<QPushButton>(widget, "CrashReportShowReportButton"));
    EXPECT_EQ(press(Qt::Key_Tab), &send);

    include_website.setChecked(true);
    include_website.setFocus();
    EXPECT_EQ(press(Qt::Key_Tab), &child<QLineEdit>(widget, "CrashReportWebsite"));
}

TEST_CASE(answering_the_report_is_announced_once_the_choice_is_made)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    auto answers = 0;
    widget.on_answered = [&] { ++answers; };
    MUST(widget.open_report(name, "https://example.com/"_string));
    widget.show();

    // A choice that cannot be sent is not an answer yet.
    child<QCheckBox>(widget, "CrashReportIncludeWebsite").setChecked(true);
    child<QLineEdit>(widget, "CrashReportWebsite").clear();
    child<QPushButton>(widget, "CrashReportSendButton").click();
    EXPECT_EQ(answers, 0);

    child<QPushButton>(widget, "CrashReportDeclineButton").click();
    EXPECT_EQ(answers, 1);
}

TEST_CASE(a_report_that_cannot_be_declined_stays_unanswered)
{
    cleanup();
    ScopeGuard guard = cleanup;

    auto name = write_report("2026-10-01T12-00-00Z"sv);
    WebView::CrashReportStore store { test_directory() };
    CrashReportReviewWidget widget { QStringLiteral("Reload page"), nullptr, store };
    auto answers = 0;
    widget.on_answered = [&] { ++answers; };
    MUST(widget.open_report(name));
    widget.show();

    // The list of crash reports can answer a report while its review is open.
    MUST(store.remove_report(name));
    child<QPushButton>(widget, "CrashReportDeclineButton").click();
    EXPECT_EQ(answers, 0);
    EXPECT(child<QLabel>(widget, "CrashReportValidation").isVisible());
    EXPECT(child<QPushButton>(widget, "CrashReportSendButton").isVisible());
}