Модульное тестирование Qt C ++ с QML qtquick - PullRequest
0 голосов
/ 10 декабря 2018

Я пытаюсь реализовать модульный тест Qt, и я хотел бы «нажать» кнопку qtquick в QML из C ++.Я успешно могу использовать QCompare для свойств одного из моих объектов QML в test_case3, но не могу понять, как создать событие щелчка в test_case4.Мои файлы ниже.

tst_case_1.cpp

void tst_case_1::test_case3()
{

    QScopedPointer<MouseMemory> mouse(new MouseMemory);
    QQmlEngine engine;
    engine.rootContext()->setContextProperty("mouse", mouse.data());

    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *object = component.create();

    QQuickItem *clear = object->findChild<QQuickItem*>("clear");
    QVariant tmp = clear->property("text");
    QCOMPARE(tmp.toString(), "Clear2");

    delete object;
}

void tst_case_1::test_case4()
{
    QScopedPointer<MouseMemory> mouse(new MouseMemory);
    QQmlEngine engine;
    engine.rootContext()->setContextProperty("mouse", mouse.data());

    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *object = component.create();

    QQuickItem *clear = object->findChild<QQuickItem*>("clear");

    /* INSERT CODE HERE
    Implement QML QQuickItem Button click
    /*

    delete object;
}

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: root
    visible: true
    width: 500
    height: 500

    Row {
        id: tools

        Button {
            id: clear
            objectName: "clear"
            text: "Clear"
            onClicked: {
                canvas.clear()
            }
        }

        Button {
            id: save
            text: "Save"
            onClicked: {
                mouse.save()
            }
        }
    }

    Canvas {
        id: canvas
        anchors.top: tools.bottom
        width: 500
        height: 500
        property int lastX: 0
        property int lastY: 0

        function clear() {
            var ctx = getContext("2d")
            ctx.reset()
            canvas.requestPaint()
            mouse.clear()
        }

        onPaint: {
            var ctx = getContext("2d")
            ctx.lineWidth = 2
            ctx.strokeStyle = color.red

            ctx.beginPath()

            ctx.moveTo(lastX, lastY)

            lastX = area.mouseX
            lastY = area.mouseY

            ctx.lineTo(lastX, lastY)

            ctx.stroke()

            mouse.test()
            mouse.add(lastX, lastY)
        }

        MouseArea {
            id: area
            anchors.fill: parent
            onPressed: {
                canvas.lastX = mouseX
                canvas.lastY = mouseY
            }

            onPositionChanged: {
                canvas.requestPaint()
            }
        }
    }
}
...