#include <QAction>
#include <QApplication>
#include <QSignalSpy>
#include <QtTest>

#include "menu-item.h"

using namespace UkuiQuick;

class menu_item_test : public QObject
{
    Q_OBJECT

private slots:
    void defaultActionIsUsable();
    void propertiesForwardToCurrentAction();
    void actionSignalsAreForwarded();
    void externalActionCanBeReplacedAndCleared();
};

void menu_item_test::defaultActionIsUsable()
{
    MenuItem item;

    QVERIFY(item.action() != nullptr);
    QCOMPARE(item.text(), QString());
    QVERIFY(item.isEnabled());
    QVERIFY(!item.checkable());
    QVERIFY(!item.checked());
}

void menu_item_test::propertiesForwardToCurrentAction()
{
    MenuItem item;

    item.setText(QStringLiteral("Open"));
    item.setEnabled(false);
    item.setCheckable(true);
    item.setChecked(true);

    QCOMPARE(item.text(), QStringLiteral("Open"));
    QCOMPARE(item.action()->text(), QStringLiteral("Open"));
    QVERIFY(!item.isEnabled());
    QVERIFY(!item.action()->isEnabled());
    QVERIFY(item.checkable());
    QVERIFY(item.action()->isCheckable());
    QVERIFY(item.checked());
    QVERIFY(item.action()->isChecked());
}

void menu_item_test::actionSignalsAreForwarded()
{
    MenuItem item;
    item.setCheckable(true);
    QSignalSpy clickedSpy(&item, &MenuItem::clicked);
    QSignalSpy toggledSpy(&item, &MenuItem::toggled);

    item.action()->trigger();

    QCOMPARE(clickedSpy.count(), 1);
    QCOMPARE(toggledSpy.count(), 1);
    QCOMPARE(toggledSpy.takeFirst().at(0).toBool(), true);
    QVERIFY(item.checked());
}

void menu_item_test::externalActionCanBeReplacedAndCleared()
{
    MenuItem item;
    QAction externalAction(QStringLiteral("External"));
    externalAction.setEnabled(false);
    QSignalSpy actionSpy(&item, &MenuItem::actionChanged);

    item.setAction(&externalAction);

    QCOMPARE(item.action(), &externalAction);
    QCOMPARE(item.text(), QStringLiteral("External"));
    QVERIFY(!item.isEnabled());
    QCOMPARE(actionSpy.count(), 1);

    item.setAction(nullptr);

    QVERIFY(item.action() != nullptr);
    QVERIFY(item.action() != &externalAction);
    QVERIFY(!item.action()->isVisible());
    QCOMPARE(actionSpy.count(), 2);
}

int main(int argc, char *argv[])
{
    qputenv("QT_QPA_PLATFORM", "offscreen");
    qputenv("QT_QPA_PLATFORMTHEME", "generic");

    QApplication app(argc, argv);
    menu_item_test testCase;
    return QTest::qExec(&testCase, argc, argv);
}

#include "test-menu-item.moc"
