1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <qtest.h>
30
31#include <qqmlengine.h>
32#include <qquickitem.h>
33#include <qquickview.h>
34
35#include "../../shared/util.h"
36#include "mypropertymap.h"
37
38class tst_SignalSpy : public QQmlDataTest
39{
40 Q_OBJECT
41public:
42 tst_SignalSpy();
43
44private slots:
45 void testValid();
46 void testCount();
47
48private:
49 QQmlEngine engine;
50};
51
52tst_SignalSpy::tst_SignalSpy()
53{
54 qmlRegisterType<MyPropertyMap>(uri: "MyImport", versionMajor: 1, versionMinor: 0, qmlName: "MyPropertyMap");
55}
56
57void tst_SignalSpy::testValid()
58{
59 QQuickView window;
60 window.setSource(testFileUrl(fileName: "signalspy.qml"));
61 QVERIFY(window.rootObject() != nullptr);
62
63 QObject *mouseSpy = window.rootObject()->findChild<QObject*>(aName: "mouseSpy");
64 QVERIFY(mouseSpy->property("valid").toBool());
65
66 QObject *propertyMapSpy = window.rootObject()->findChild<QObject*>(aName: "propertyMapSpy");
67 QVERIFY(propertyMapSpy->property("valid").toBool());
68}
69
70void tst_SignalSpy::testCount()
71{
72 QQuickView window;
73 window.resize(w: 200, h: 200);
74 window.setSource(testFileUrl(fileName: "signalspy.qml"));
75 window.show();
76 QVERIFY(QTest::qWaitForWindowActive(&window));
77 QVERIFY(window.rootObject() != nullptr);
78
79 QObject *mouseSpy = window.rootObject()->findChild<QObject*>(aName: "mouseSpy");
80 QCOMPARE(mouseSpy->property("count").toInt(), 0);
81
82 QObject *propertyMapSpy = window.rootObject()->findChild<QObject*>(aName: "propertyMapSpy");
83 QCOMPARE(propertyMapSpy->property("count").toInt(), 0);
84
85 QTest::mouseClick(window: &window, button: Qt::LeftButton, stateKey: Qt::KeyboardModifiers(), pos: QPoint(100, 100));
86 QTRY_COMPARE(mouseSpy->property("count").toInt(), 1);
87
88 MyPropertyMap *propertyMap = static_cast<MyPropertyMap *>(window.rootObject()->findChild<QObject*>(aName: "propertyMap"));
89 Q_EMIT propertyMap->mySignal();
90 QCOMPARE(propertyMapSpy->property("count").toInt(), 1);
91}
92
93QTEST_MAIN(tst_SignalSpy)
94
95#include "tst_signalspy.moc"
96

source code of qtdeclarative/tests/auto/quicktest/signalspy/tst_signalspy.cpp