#include <QSettings>
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QtTest>

#include "ini-config.h"

using namespace UkuiQuick;

class ini_config_test : public QObject
{
    Q_OBJECT

private slots:
    void valuesAreStoredReadAndSynced();
};

void ini_config_test::valuesAreStoredReadAndSynced()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString fileName = dir.filePath(QStringLiteral("settings.conf"));
    IniConfig config(fileName);
    QSignalSpy changedSpy(&config, &IniConfig::configChanged);

    QVERIFY(config.keys().isEmpty());
    QVERIFY(config.settings() != nullptr);

    config.setValue(QStringLiteral("General/enabled"), true);
    config.setValue(QStringLiteral("General/count"), 12);
    config.forceSync();

    QCOMPARE(changedSpy.count(), 2);
    QCOMPARE(config.getValue(QStringLiteral("General/enabled")).toBool(), true);
    QCOMPARE(config.getValue(QStringLiteral("General/count")).toInt(), 12);
    QVERIFY(config.keys().contains(QStringLiteral("General/enabled")));
    QVERIFY(config.keys().contains(QStringLiteral("General/count")));

    QSettings settings(fileName, QSettings::IniFormat);
    QCOMPARE(settings.value(QStringLiteral("General/enabled")).toBool(), true);
    QCOMPARE(settings.value(QStringLiteral("General/count")).toInt(), 12);
}

QTEST_GUILESS_MAIN(ini_config_test)

#include "test-ini-config.moc"
