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

104 lines
3.3 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#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::qWaitForWindowActive(&window));
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"