// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
// SPDX-FileCopyrightText: 2024 Arjen Hiemstra <ahiemstra@heimr.nl>
//
// This file is automatically generated from autotest.cpp.j2.
// To regenerate, run `tools/propertygenerator/generate_properties.py`.

#include <QtTest>

#include <properties/BackgroundPropertyGroup.h>

#include "CreateTestInstances.h"

using namespace Union::Properties;
using namespace Qt::StringLiterals;

class TestBackgroundPropertyGroup : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testNull()
    {
        auto property = std::make_unique<BackgroundPropertyGroup>();

        // A null instance should not have any values for its properties.
        QVERIFY(!property->color().has_value());
        QVERIFY(!property->image());
    }

    void testEmpty()
    {
        auto property = BackgroundPropertyGroup::empty();

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(property->color().value(), emptyValue<Union::Color>());
        QCOMPARE(*property->image(), *ImagePropertyGroup::empty());
    }

    void testHasAnyValue()
    {
        auto property = std::make_unique<BackgroundPropertyGroup>();

        // An empty instance should not have any values for its properties.
        QVERIFY(!property->hasAnyValue());

        {
            Union::Color value;
            property->setColor(value);
            QVERIFY(property->hasAnyValue());
            property->setColor(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            // Assigning an empty value to a property should have no effect.
            property->setImage(std::make_unique<ImagePropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setImage(testImagePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

            property->setImage(nullptr);
            QVERIFY(!property->hasAnyValue());
        }
    }

    void testResolveProperties()
    {
        auto source = std::make_unique<BackgroundPropertyGroup>();
        auto destination = std::make_unique<BackgroundPropertyGroup>();

        QVERIFY(!source->hasAnyValue());
        QVERIFY(!destination->hasAnyValue());

        // Calling resolve on empty source and destination should have no effect.
        BackgroundPropertyGroup::resolveProperties(source.get(), destination.get());

        QVERIFY(!destination->hasAnyValue());

        source->setColor(Union::Color{});
        source->setImage(testImagePropertyGroupInstance());

        QVERIFY(source->hasAnyValue());
        QVERIFY(!destination->hasAnyValue());

        BackgroundPropertyGroup::resolveProperties(source.get(), destination.get());

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(destination->color(), source->color());
        QCOMPARE(*destination->image(), *source->image());
    }

    void testDataStream()
    {
        auto group = testBackgroundPropertyGroupInstance();

        QByteArray data;
        QDataStream writeStream(&data, QIODevice::WriteOnly);
        writeStream << group.get();

        QCOMPARE(writeStream.status(), QDataStream::Status::Ok);
        QVERIFY(data.size() > 0);

        QDataStream readStream(data);
        auto readGroup = std::make_unique<BackgroundPropertyGroup>();
        readStream >> readGroup;

        QCOMPARE(readStream.status(), QDataStream::Status::Ok);
        QCOMPARE(*readGroup, *group);
    }
};

QTEST_MAIN(TestBackgroundPropertyGroup)

#include "TestBackgroundPropertyGroup.moc"