/*
    SPDX-FileCopyrightText: 2010 Martin Blumenstingl <darklight.xdarklight@googlemail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "kconfigloadertest.h"

#include <kconfig.h>
#include <kconfiggroup.h>
#include <kconfigskeleton.h>
#include <kconfigloader.h>

Q_DECLARE_METATYPE(QList<int>)

#define TEST_NAME QString::fromLatin1("kconfigloadertest")

#define GET_CONFIG_ITEM_VALUE(type, configName) \
    KConfigSkeletonItem* item = cl->findItem(TEST_NAME, configName); \
    /* Check if we got back a valid item. */ \
    QVERIFY(item != nullptr); \
    /* Cast the item to the given type. */ \
    type typeItem = dynamic_cast<type>(item); \
    /* Make sure the cast was successful. */ \
    QVERIFY(typeItem != nullptr);

void ConfigLoaderTest::init()
{
    QString fileName = TEST_NAME + QLatin1String(".xml");
    configFile = new QFile(QFINDTESTDATA(QString::fromLatin1("/") + fileName));
    cl = new KConfigLoader(configFile->fileName(), configFile);
}

void ConfigLoaderTest::cleanup()
{
    delete cl;
    delete configFile;
}

void ConfigLoaderTest::boolDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemBool *, "DefaultBoolItem");

    QVERIFY(typeItem->isEqual(true));
}

void ConfigLoaderTest::colorDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemColor *, "DefaultColorItem");

    QVERIFY(typeItem->isEqual(QColor("#00FF00")));
}

void ConfigLoaderTest::dateTimeDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemDateTime *, "DefaultDateTimeItem");

    QVERIFY(typeItem->isEqual(QDateTime::fromString("Thu Sep 09 2010")));
}

void ConfigLoaderTest::enumDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemEnum *, "DefaultEnumItem");

    QVERIFY(typeItem->isEqual(3));
}

void ConfigLoaderTest::fontDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemFont *, "DefaultFontItem");

    QVERIFY(typeItem->isEqual(QFont("DejaVu Sans")));
}

void ConfigLoaderTest::intDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemInt *, "DefaultIntItem");

    QVERIFY(typeItem->isEqual(27));
}

void ConfigLoaderTest::passwordDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemPassword *, "DefaultPasswordItem");

    QVERIFY(typeItem->isEqual(QString::fromLatin1("h4x.")));
}

void ConfigLoaderTest::pathDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemPath *, "DefaultPathItem");

    QVERIFY(typeItem->isEqual(QString::fromLatin1("/dev/null")));
}

void ConfigLoaderTest::stringDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemString *, "DefaultStringItem");

    QVERIFY(typeItem->isEqual(QString::fromLatin1("TestString")));
}

void ConfigLoaderTest::stringListDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KConfigSkeleton::ItemStringList *, "DefaultStringListItem");

    // Create a string list with the expected values.
    QStringList expected;
    expected.append(QStringLiteral("One"));
    expected.append(QStringLiteral("Two"));
    expected.append(QStringLiteral("Three"));
    expected.append(QStringLiteral("Four"));
    expected.append(QStringLiteral("Five"));

    QVERIFY(typeItem->isEqual(expected));
}

void ConfigLoaderTest::uintDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemUInt *, "DefaultUIntItem");

    QVERIFY(typeItem->isEqual(7U));
}

void ConfigLoaderTest::urlDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemUrl *, "DefaultUrlItem");

    QVERIFY(typeItem->isEqual(QUrl("http://kde.org")));
}

void ConfigLoaderTest::doubleDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemDouble *, "DefaultDoubleItem");

    QVERIFY(typeItem->isEqual(13.37));
}

void ConfigLoaderTest::intListDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemIntList *, "DefaultIntListItem");

    // Create a int list with the expected values.
    QList<int> expected;
    expected.append(1);
    expected.append(1);
    expected.append(2);
    expected.append(3);
    expected.append(5);
    expected.append(8);

    QVERIFY(typeItem->isEqual(QVariant::fromValue(expected)));
}

void ConfigLoaderTest::longLongDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemLongLong *, "DefaultLongLongItem");

    QVERIFY(typeItem->isEqual(Q_INT64_C(-9211372036854775808)));
}

void ConfigLoaderTest::pointDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemPoint *, "DefaultPointItem");

    QVERIFY(typeItem->isEqual(QPoint(185, 857)));
}

void ConfigLoaderTest::rectDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemRect *, "DefaultRectItem");

    // Create a new QRect with the expected value.
    QRect expected;
    expected.setCoords(3, 7, 951, 358);

    QVERIFY(typeItem->isEqual(expected));
}

void ConfigLoaderTest::sizeDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemSize *, "DefaultSizeItem");

    QVERIFY(typeItem->isEqual(QSize(640, 480)));
}

void ConfigLoaderTest::ulongLongDefaultValue()
{
    GET_CONFIG_ITEM_VALUE(KCoreConfigSkeleton::ItemULongLong *, "DefaultULongLongItem");

    QVERIFY(typeItem->isEqual(Q_UINT64_C(9223372036854775806)));
}

QTEST_MAIN(ConfigLoaderTest)

