#include <QQmlComponent>
#include <QQmlEngine>
#include <QtTest>

#include "shared-qml-engine.h"

using namespace UkuiQuick;

class shared_qml_engine_test : public QObject
{
    Q_OBJECT

private slots:
    void sharedEngineReturnsSameInstance();
    void sharedEngineCanCreateSimpleQmlObject();
};

void shared_qml_engine_test::sharedEngineReturnsSameInstance()
{
    QQmlEngine *first = SharedQmlEngine::sharedQmlEngine();
    QQmlEngine *second = SharedQmlEngine::sharedQmlEngine();

    QVERIFY(first != nullptr);
    QCOMPARE(first, second);
}

void shared_qml_engine_test::sharedEngineCanCreateSimpleQmlObject()
{
    QQmlEngine *engine = SharedQmlEngine::sharedQmlEngine();
    QVERIFY(engine != nullptr);

    QQmlComponent component(engine);
    component.setData(R"(
        import QtQml 2.15
        QtObject {
            property int answer: 42
        }
    )", QUrl());

    std::unique_ptr<QObject> object(component.create());
    QVERIFY2(object != nullptr, qPrintable(component.errorString()));
    QCOMPARE(object->property("answer").toInt(), 42);
    QCOMPARE(object->parent(), nullptr);
}

QTEST_GUILESS_MAIN(shared_qml_engine_test)

#include "test-shared-qml-engine.moc"
