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#include <qsignalspy.h>
31#include <private/qqmlglobal_p.h>
32
33class tst_qqmlcpputils : public QObject
34{
35 Q_OBJECT
36public:
37 tst_qqmlcpputils() {}
38
39private slots:
40 void fastConnect();
41 void fastCast();
42};
43
44class MyObject : public QObject {
45 Q_OBJECT
46public:
47 MyObject() : slotCount(0) {}
48 friend class tst_qqmlcpputils;
49
50 int slotCount;
51
52signals:
53 void signal1();
54 void signal2();
55
56public slots:
57 void slot1() { slotCount++; }
58};
59
60void tst_qqmlcpputils::fastConnect()
61{
62 {
63 MyObject *obj = new MyObject;
64 qmlobject_connect(obj, MyObject, SIGNAL(signal1()), obj, MyObject, SLOT(slot1()));
65
66 obj->signal1();
67 QCOMPARE(obj->slotCount, 1);
68
69 delete obj;
70 }
71
72 {
73 MyObject obj;
74 qmlobject_connect(&obj, MyObject, SIGNAL(signal1()), &obj, MyObject, SLOT(slot1()));
75
76 obj.signal1();
77 QCOMPARE(obj.slotCount, 1);
78 }
79
80 {
81 MyObject *obj = new MyObject;
82 QSignalSpy spy(obj, SIGNAL(signal2()));
83 qmlobject_connect(obj, MyObject, SIGNAL(signal1()), obj, MyObject, SIGNAL(signal2()));
84
85 obj->signal1();
86 QCOMPARE(spy.count(), 1);
87
88 delete obj;
89 }
90}
91
92void tst_qqmlcpputils::fastCast()
93{
94 {
95 QObject *myObj = new MyObject;
96 MyObject *obj = qmlobject_cast<MyObject*>(object: myObj);
97 QVERIFY(obj);
98 QCOMPARE(obj->metaObject(), myObj->metaObject());
99 obj->slot1();
100 QCOMPARE(obj->slotCount, 1);
101 delete myObj;
102 }
103
104 {
105 QObject *nullObj = nullptr;
106 QObject *obj = qmlobject_cast<QObject *>(object: nullObj);
107 QCOMPARE(obj, nullObj); // shouldn't crash/assert.
108 }
109}
110
111QTEST_MAIN(tst_qqmlcpputils)
112
113#include "tst_qqmlcpputils.moc"
114

source code of qtdeclarative/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp