#include <QtTest>

#include "window-manager.h"

using namespace UkuiQuick;

namespace {

class SessionTypeGuard
{
public:
    SessionTypeGuard()
        : m_hadValue(qEnvironmentVariableIsSet("XDG_SESSION_TYPE"))
        , m_original(qgetenv("XDG_SESSION_TYPE"))
    {
    }

    ~SessionTypeGuard()
    {
        if (m_hadValue) {
            qputenv("XDG_SESSION_TYPE", m_original);
        } else {
            qunsetenv("XDG_SESSION_TYPE");
        }
    }

private:
    bool m_hadValue;
    QByteArray m_original;
};

}

class window_manager_test : public QObject
{
    Q_OBJECT

private slots:
    void detectsWaylandSessionFromEnvironment();
    void treatsOtherOrMissingSessionTypesAsNonWayland();
};

void window_manager_test::detectsWaylandSessionFromEnvironment()
{
    SessionTypeGuard guard;

    qputenv("XDG_SESSION_TYPE", QByteArrayLiteral("wayland"));
    QVERIFY(WindowManager::isWaylandSession());
}

void window_manager_test::treatsOtherOrMissingSessionTypesAsNonWayland()
{
    SessionTypeGuard guard;

    qputenv("XDG_SESSION_TYPE", QByteArrayLiteral("x11"));
    QVERIFY(!WindowManager::isWaylandSession());

    qputenv("XDG_SESSION_TYPE", QByteArrayLiteral("tty"));
    QVERIFY(!WindowManager::isWaylandSession());

    qunsetenv("XDG_SESSION_TYPE");
    QVERIFY(!WindowManager::isWaylandSession());
}

QTEST_GUILESS_MAIN(window_manager_test)

#include "test-window-manager.moc"
