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

#include "CreateTestInstances.h"

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

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

        // 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 = LayoutProperty::empty();

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

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

        // 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<AlignmentProperty>());
            QVERIFY(!property->hasAnyValue());

            property->setAlignment(testAlignmentPropertyInstance());
            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<SizeProperty>());
            QVERIFY(!property->hasAnyValue());

            property->setPadding(testSizePropertyInstance());
            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<SizeProperty>());
            QVERIFY(!property->hasAnyValue());

            property->setInset(testSizePropertyInstance());
            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<SizeProperty>());
            QVERIFY(!property->hasAnyValue());

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

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

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

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

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

        QVERIFY(!destination->hasAnyValue());

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

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

        LayoutProperty::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 = testLayoutPropertyInstance();

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

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

QTEST_MAIN(TestLayoutProperty)

#include "TestLayoutProperty.moc"