3

I am trying to implement a Qt unit test and I would like to "click" a qtquick Button in QML from C++. I am successfully able to use QCompare on the properties of one of my QML objects in test_case3 but I can't figure out how to create a click event in test_case4. My Files are below.

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()
            }
        }
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
slayer
  • 643
  • 7
  • 14
  • 1
    Why do you want to create a test in C++ if QML offers a test system in QML? – eyllanesc Dec 10 '18 at 02:08
  • I have c++ integrated with my QML with `mouse.save()` and I'd like to test that functionality. I am new to QML though. Is there a route in qmltest to implement the button click of `id:save` and implement the `mouse.save` call in C++? – slayer Dec 10 '18 at 02:42
  • The QML test can expose the mouse (C++ object) to the QML and then do the whole test in QML. – eyllanesc Dec 10 '18 at 02:43
  • I did not know I could do that. I tried a QML test before but I had a problem with accessing the QML Button `id: Save` if it was not the root object (nested object). I was able to access the `Window` and compare the `height` test but not the `Button` object. Is there a link in the Qt Documentation? – slayer Dec 10 '18 at 02:54
  • 1
    Maybe [TestCase](http://doc.qt.io/qt-5/qml-qttest-testcase.html) could be used instead? – folibis Dec 10 '18 at 06:55

0 Answers0