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

#include "CreateTestInstances.h"

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

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

        // A null instance should not have any values for its properties.
        QVERIFY(!property->topLeft());
        QVERIFY(!property->topRight());
        QVERIFY(!property->bottomLeft());
        QVERIFY(!property->bottomRight());
    }

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

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(*property->topLeft(), *CornerPropertyGroup::empty());
        QCOMPARE(*property->topRight(), *CornerPropertyGroup::empty());
        QCOMPARE(*property->bottomLeft(), *CornerPropertyGroup::empty());
        QCOMPARE(*property->bottomRight(), *CornerPropertyGroup::empty());
    }

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

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

        {
            // Assigning an empty value to a property should have no effect.
            property->setTopLeft(std::make_unique<CornerPropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setTopLeft(testCornerPropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

            property->setTopLeft(nullptr);
            QVERIFY(!property->hasAnyValue());
        }
        {
            // Assigning an empty value to a property should have no effect.
            property->setTopRight(std::make_unique<CornerPropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setTopRight(testCornerPropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

            property->setTopRight(nullptr);
            QVERIFY(!property->hasAnyValue());
        }
        {
            // Assigning an empty value to a property should have no effect.
            property->setBottomLeft(std::make_unique<CornerPropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setBottomLeft(testCornerPropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

            property->setBottomLeft(nullptr);
            QVERIFY(!property->hasAnyValue());
        }
        {
            // Assigning an empty value to a property should have no effect.
            property->setBottomRight(std::make_unique<CornerPropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setBottomRight(testCornerPropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

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

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

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

        QVERIFY(!destination->hasAnyValue());

        source->setTopLeft(testCornerPropertyGroupInstance());
        source->setTopRight(testCornerPropertyGroupInstance());
        source->setBottomLeft(testCornerPropertyGroupInstance());
        source->setBottomRight(testCornerPropertyGroupInstance());

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

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

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(*destination->topLeft(), *source->topLeft());
        QCOMPARE(*destination->topRight(), *source->topRight());
        QCOMPARE(*destination->bottomLeft(), *source->bottomLeft());
        QCOMPARE(*destination->bottomRight(), *source->bottomRight());
    }

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

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

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

QTEST_MAIN(TestCornersPropertyGroup)

#include "TestCornersPropertyGroup.moc"