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

#include "CreateTestInstances.h"

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

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

        // A null instance should not have any values for its properties.
        QVERIFY(!property->alignment());
        QVERIFY(!property->width().has_value());
        QVERIFY(!property->height().has_value());
        QVERIFY(!property->spacing().has_value());
        QVERIFY(!property->padding());
        QVERIFY(!property->inset());
        QVERIFY(!property->margins());
    }

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

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(*property->alignment(), *AlignmentPropertyGroup::empty());
        QCOMPARE(property->width().value(), emptyValue<qreal>());
        QCOMPARE(property->height().value(), emptyValue<qreal>());
        QCOMPARE(property->spacing().value(), emptyValue<qreal>());
        QCOMPARE(*property->padding(), *SizePropertyGroup::empty());
        QCOMPARE(*property->inset(), *SizePropertyGroup::empty());
        QCOMPARE(*property->margins(), *SizePropertyGroup::empty());
    }

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

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

            property->setAlignment(testAlignmentPropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

            property->setAlignment(nullptr);
            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());
        }
        {
            qreal value;
            property->setSpacing(value);
            QVERIFY(property->hasAnyValue());
            property->setSpacing(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            // Assigning an empty value to a property should have no effect.
            property->setPadding(std::make_unique<SizePropertyGroup>());
            QVERIFY(!property->hasAnyValue());

            property->setPadding(testSizePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

            property->setInset(testSizePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

            property->setMargins(testSizePropertyGroupInstance());
            QVERIFY(property->hasAnyValue());

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

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

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

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

        QVERIFY(!destination->hasAnyValue());

        source->setAlignment(testAlignmentPropertyGroupInstance());
        source->setWidth(qreal{});
        source->setHeight(qreal{});
        source->setSpacing(qreal{});
        source->setPadding(testSizePropertyGroupInstance());
        source->setInset(testSizePropertyGroupInstance());
        source->setMargins(testSizePropertyGroupInstance());

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

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

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(*destination->alignment(), *source->alignment());
        QCOMPARE(destination->width(), source->width());
        QCOMPARE(destination->height(), source->height());
        QCOMPARE(destination->spacing(), source->spacing());
        QCOMPARE(*destination->padding(), *source->padding());
        QCOMPARE(*destination->inset(), *source->inset());
        QCOMPARE(*destination->margins(), *source->margins());
    }

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

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

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

QTEST_MAIN(TestLayoutPropertyGroup)

#include "TestLayoutPropertyGroup.moc"