qtdeclarative/tests/auto/quicktest/signalspy/tst_signalspy.cpp

79 lines
2.2 KiB
C++
Raw Permalink Normal View History

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <qtest.h>
#include <qqmlengine.h>
#include <qquickitem.h>
#include <qquickview.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include "mypropertymap.h"
class tst_SignalSpy : public QQmlDataTest
{
Q_OBJECT
public:
tst_SignalSpy();
private slots:
void testValid();
void testCount();
private:
QQmlEngine engine;
};
tst_SignalSpy::tst_SignalSpy()
: QQmlDataTest(QT_QMLTEST_DATADIR)
{
qmlRegisterType<MyPropertyMap>("MyImport", 1, 0, "MyPropertyMap");
}
void tst_SignalSpy::testValid()
{
QQuickView window;
window.setSource(testFileUrl("signalspy.qml"));
QVERIFY(window.rootObject() != nullptr);
QObject *mouseSpy = window.rootObject()->findChild<QObject*>("mouseSpy");
QVERIFY(mouseSpy->property("valid").toBool());
QObject *propertyMapSpy = window.rootObject()->findChild<QObject*>("propertyMapSpy");
QVERIFY(propertyMapSpy->property("valid").toBool());
}
void tst_SignalSpy::testCount()
{
Make qmltest/SignalSpy.qml aware of recursive qtest_update() calls Apparently looking up a property can cause it to be evaluated (and its signal handler called directly). From the debugging the following is visible: 1) assume signalName is set first and target has a pending binding (not evaluated yet) 2) once signalName setter is triggered, it calls the relevant signal handler - onSignalNameChanged, which subsequently calls qtest_update() 3) in qtest_update(): a. we first check whether there's any old state "if (qtest_prevTarget != null)" -> there's none yet, because we entered this function for the first time ever -> jump to: "if (target != null && signalName != "")" b. once at "target != null" the engine attempts to get the value of the `target` property. if the property `target` happens to have an unevaluated binding, it is evaluated immediately. [the logic is in QObjectWrapper::getQmlProperty() which calls QObjectWrapper::getProperty() and that one calls QQmlData::flushPendingBinding()] c. the binding evaluation causes onTargetChanged signal handler to trigger which in turn again enters qtest_update 4) in the "recursive" call of qtest_update(): a. same as 3.a. b. target is now evaluated and signalName is evaluated as well (due to being already set at step 2) c. "if (target != null && signalName != "")" succeeds and we proceed to connect to spy.qtest_activated d. we return from this function and get back to the outer qtest_update() 5) we are now back at 3.c in qtest_update(): a. target is evaluated, signalName is known, so "if (target != null && signalName != "")" succeeds -> again, we proceed to connect to spy.qtest_activated (for the second time now - see 4.c) b. we return from qtest_update() (the outer one) back to onSignalNameChanged 6) the end. bottom line: - two signals were connected to target[signalName] - zero signals were disconnected This seems like a very nasty thing to have in a UI-centric language but for now let's just attempt to fix the SignalSpy class Pick-to: 6.2 Task-number: QTBUG-98722 Change-Id: I70f11000b8383e6a8fc82d0034c62a2094f6d832 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
2021-11-26 15:27:16 +00:00
const QUrl urls[] = {
testFileUrl("signalspy.qml"),
testFileUrl("signalspy2.qml"),
};
for (const auto &url : urls) {
QQuickView window;
window.resize(200, 200);
window.setSource(url);
window.show();
QVERIFY(QTest::qWaitForWindowExposed(&window));
Make qmltest/SignalSpy.qml aware of recursive qtest_update() calls Apparently looking up a property can cause it to be evaluated (and its signal handler called directly). From the debugging the following is visible: 1) assume signalName is set first and target has a pending binding (not evaluated yet) 2) once signalName setter is triggered, it calls the relevant signal handler - onSignalNameChanged, which subsequently calls qtest_update() 3) in qtest_update(): a. we first check whether there's any old state "if (qtest_prevTarget != null)" -> there's none yet, because we entered this function for the first time ever -> jump to: "if (target != null && signalName != "")" b. once at "target != null" the engine attempts to get the value of the `target` property. if the property `target` happens to have an unevaluated binding, it is evaluated immediately. [the logic is in QObjectWrapper::getQmlProperty() which calls QObjectWrapper::getProperty() and that one calls QQmlData::flushPendingBinding()] c. the binding evaluation causes onTargetChanged signal handler to trigger which in turn again enters qtest_update 4) in the "recursive" call of qtest_update(): a. same as 3.a. b. target is now evaluated and signalName is evaluated as well (due to being already set at step 2) c. "if (target != null && signalName != "")" succeeds and we proceed to connect to spy.qtest_activated d. we return from this function and get back to the outer qtest_update() 5) we are now back at 3.c in qtest_update(): a. target is evaluated, signalName is known, so "if (target != null && signalName != "")" succeeds -> again, we proceed to connect to spy.qtest_activated (for the second time now - see 4.c) b. we return from qtest_update() (the outer one) back to onSignalNameChanged 6) the end. bottom line: - two signals were connected to target[signalName] - zero signals were disconnected This seems like a very nasty thing to have in a UI-centric language but for now let's just attempt to fix the SignalSpy class Pick-to: 6.2 Task-number: QTBUG-98722 Change-Id: I70f11000b8383e6a8fc82d0034c62a2094f6d832 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
2021-11-26 15:27:16 +00:00
QVERIFY(window.rootObject() != nullptr);
QObject *mouseSpy = window.rootObject()->findChild<QObject *>("mouseSpy");
QCOMPARE(mouseSpy->property("count").toInt(), 0);
QObject *propertyMapSpy = window.rootObject()->findChild<QObject *>("propertyMapSpy");
QCOMPARE(propertyMapSpy->property("count").toInt(), 0);
QTest::mouseClick(&window, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(100, 100));
QTRY_COMPARE(mouseSpy->property("count").toInt(), 1);
MyPropertyMap *propertyMap = static_cast<MyPropertyMap *>(
window.rootObject()->findChild<QObject *>("propertyMap"));
Q_EMIT propertyMap->mySignal();
QCOMPARE(propertyMapSpy->property("count").toInt(), 1);
}
}
QTEST_MAIN(tst_SignalSpy)
#include "tst_signalspy.moc"