#include "desktop_integration.h"

#include <QtTest/QtTest>

class DesktopIntegrationTest : public QObject {
    Q_OBJECT

private slots:
    void linuxBrowserCommandsPreferXdgOpen();
    void ddeFractionalScalingPrefersPlainXcb();
    void nonDdeDesktopKeepsPlatformChain();
    void waylandKeepsExistingPlatform();
};

void DesktopIntegrationTest::linuxBrowserCommandsPreferXdgOpen()
{
    const QList<DetachedCommand> commands =
        browserOpenCommands(QStringLiteral("https://login.example.invalid/flow"));

#ifdef Q_OS_LINUX
    QVERIFY(commands.size() >= 2);
    QCOMPARE(commands.at(0).program, QStringLiteral("xdg-open"));
    QCOMPARE(commands.at(0).arguments, QStringList(QStringList{QStringLiteral("https://login.example.invalid/flow")}));
    QCOMPARE(commands.at(1).program, QStringLiteral("gio"));
    QCOMPARE(commands.at(1).arguments,
             QStringList(QStringList{QStringLiteral("open"), QStringLiteral("https://login.example.invalid/flow")}));
#else
    QVERIFY(commands.isEmpty());
#endif
}

void DesktopIntegrationTest::ddeFractionalScalingPrefersPlainXcb()
{
    QCOMPARE(normalizedQtPlatformPlugin(QStringLiteral("dxcb;xcb"), QStringLiteral("DDE"), false),
             QStringLiteral("xcb"));
    QCOMPARE(normalizedQtPlatformPlugin(QStringLiteral("dxcb"), QStringLiteral("deepin"), false),
             QStringLiteral("xcb"));
}

void DesktopIntegrationTest::nonDdeDesktopKeepsPlatformChain()
{
    QCOMPARE(normalizedQtPlatformPlugin(QStringLiteral("dxcb;xcb"), QStringLiteral("KDE"), false),
             QStringLiteral("dxcb;xcb"));
}

void DesktopIntegrationTest::waylandKeepsExistingPlatform()
{
    QCOMPARE(normalizedQtPlatformPlugin(QStringLiteral("dxcb;xcb"), QStringLiteral("DDE"), true),
             QStringLiteral("dxcb;xcb"));
}

QTEST_MAIN(DesktopIntegrationTest)

#include "desktop_integration_test.moc"
