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

#include "CreateTestInstances.h"

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

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

        // A null instance should not have any values for its properties.
        QVERIFY(!property->alignment());
        QVERIFY(!property->font().has_value());
        QVERIFY(!property->color().has_value());
        QVERIFY(!property->wrapMode().has_value());
        QVERIFY(!property->elide().has_value());
    }

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

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(*property->alignment(), *AlignmentProperty::empty());
        QCOMPARE(property->font().value(), emptyValue<QFont>());
        QCOMPARE(property->color().value(), emptyValue<Union::Color>());
        QCOMPARE(property->wrapMode().value(), emptyValue<Union::Properties::TextWrapMode>());
        QCOMPARE(property->elide().value(), emptyValue<Union::Properties::TextElide>());
    }

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

        // 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());
        }
        {
            QFont value;
            property->setFont(value);
            QVERIFY(property->hasAnyValue());
            property->setFont(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            Union::Color value;
            property->setColor(value);
            QVERIFY(property->hasAnyValue());
            property->setColor(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            Union::Properties::TextWrapMode value;
            property->setWrapMode(value);
            QVERIFY(property->hasAnyValue());
            property->setWrapMode(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            Union::Properties::TextElide value;
            property->setElide(value);
            QVERIFY(property->hasAnyValue());
            property->setElide(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
    }

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

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

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

        QVERIFY(!destination->hasAnyValue());

        source->setAlignment(testAlignmentPropertyInstance());
        source->setFont(QFont{});
        source->setColor(Union::Color{});
        source->setWrapMode(Union::Properties::TextWrapMode{});
        source->setElide(Union::Properties::TextElide{});

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

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

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(*destination->alignment(), *source->alignment());
        QCOMPARE(destination->font(), source->font());
        QCOMPARE(destination->color(), source->color());
        QCOMPARE(destination->wrapMode(), source->wrapMode());
        QCOMPARE(destination->elide(), source->elide());
    }

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

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

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

QTEST_MAIN(TestTextProperty)

#include "TestTextProperty.moc"