#include "popup_menu_window.h"

#include <QtTest/QtTest>

class PopupMenuWindowSmokeTest : public QObject {
    Q_OBJECT

private slots:
    void rendersActionsIntoRows();
    void capsWidthAndUsesOpaqueBackground();
    void longTextWrapsInsteadOfStayingSingleLine();
    void compactLayoutKeepsSimpleMenuShort();
};

void PopupMenuWindowSmokeTest::rendersActionsIntoRows()
{
    QAction normal(QStringLiteral("普通动作"));
    QAction launcher(QStringLiteral("出口节点 ▸"));

    PopupMenuWindow window;
    window.setActions({&normal, &launcher}, QSet<QAction *>{&launcher});

    QCOMPARE(window.actionCount(), 2);
    QVERIFY(window.sizeHint().height() > 0);
}

void PopupMenuWindowSmokeTest::capsWidthAndUsesOpaqueBackground()
{
    QAction longAction(QStringLiteral("控制器: https://sdp.mm.md/organizations/example/controllers/very-long-controller-name"));
    QAction launcher(QStringLiteral("出口节点 ▸"));

    PopupMenuWindow window;
    window.setPreferredMaxWidth(360);
    window.setActions({&longAction, &launcher}, QSet<QAction *>{&launcher});

    QVERIFY2(window.sizeHint().width() <= 360, "popup width should respect preferred max width");
    QVERIFY2(window.backgroundFillColor().alpha() >= 250, "popup background should be close to opaque");
}

void PopupMenuWindowSmokeTest::longTextWrapsInsteadOfStayingSingleLine()
{
    QAction shortAction(QStringLiteral("短文本"));
    QAction longAction(QStringLiteral("控制器: https://sdp.mm.md/organizations/example/controllers/very-long-controller-name-without-any-short-break"));

    PopupMenuWindow shortWindow;
    shortWindow.setPreferredMaxWidth(280);
    shortWindow.setActions({&shortAction});

    PopupMenuWindow longWindow;
    longWindow.setPreferredMaxWidth(280);
    longWindow.setActions({&longAction});

    QVERIFY2(longWindow.sizeHint().height() > shortWindow.sizeHint().height(),
             "long popup rows should wrap and increase height instead of staying single-line");
}

void PopupMenuWindowSmokeTest::compactLayoutKeepsSimpleMenuShort()
{
    QAction first(QStringLiteral("刷新状态"));
    QAction second(QStringLiteral("退出"));

    PopupMenuWindow window;
    window.setActions({&first, &second});

    QVERIFY2(window.sizeHint().height() <= 72,
             "simple two-row popup should stay visually compact");
}

QTEST_MAIN(PopupMenuWindowSmokeTest)

#include "popup_menu_window_smoke_test.moc"
