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

#include "CreateTestInstances.h"

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

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

        // A null instance should not have any values for its properties.
        QVERIFY(!property->left());
        QVERIFY(!property->right());
        QVERIFY(!property->top());
        QVERIFY(!property->bottom());
    }

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

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(*property->left(), *LinePropertyGroup::empty());
        QCOMPARE(*property->right(), *LinePropertyGroup::empty());
        QCOMPARE(*property->top(), *LinePropertyGroup::empty());
        QCOMPARE(*property->bottom(), *LinePropertyGroup::empty());
    }

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

        // 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->setLeft(std::make_unique<LinePropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setLeft(testLinePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

            property->setRight(testLinePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

            property->setTop(testLinePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

            property->setBottom(testLinePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

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

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

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

        QVERIFY(!destination->hasAnyValue());

        source->setLeft(testLinePropertyGroupInstance());
        source->setRight(testLinePropertyGroupInstance());
        source->setTop(testLinePropertyGroupInstance());
        source->setBottom(testLinePropertyGroupInstance());

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

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

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(*destination->left(), *source->left());
        QCOMPARE(*destination->right(), *source->right());
        QCOMPARE(*destination->top(), *source->top());
        QCOMPARE(*destination->bottom(), *source->bottom());
    }

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

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

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

QTEST_MAIN(TestOutlinePropertyGroup)

#include "TestOutlinePropertyGroup.moc"