// 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/CornerProperty.h>

#include "CreateTestInstances.h"

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

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

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

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

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

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

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

        {
            qreal value;
            property->setRadius(value);
            QVERIFY(property->hasAnyValue());
            property->setRadius(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            qreal value;
            property->setWidth(value);
            QVERIFY(property->hasAnyValue());
            property->setWidth(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            qreal value;
            property->setHeight(value);
            QVERIFY(property->hasAnyValue());
            property->setHeight(std::nullopt);
            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<ImageProperty>());
            QVERIFY(!property->hasAnyValue());

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

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

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

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

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

        QVERIFY(!destination->hasAnyValue());

        source->setRadius(qreal{});
        source->setWidth(qreal{});
        source->setHeight(qreal{});
        source->setColor(Union::Color{});
        source->setImage(testImagePropertyInstance());

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

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

        QVERIFY(destination->hasAnyValue());

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

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

        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<CornerProperty>();
        readStream >> readGroup;

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

QTEST_MAIN(TestCornerProperty)

#include "TestCornerProperty.moc"