|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 |
|
45 |
|
46 #include <qdatetime.h> |
|
47 |
|
48 class tst_QSignalSpy : public QObject |
|
49 { |
|
50 Q_OBJECT |
|
51 |
|
52 private slots: |
|
53 void spyWithoutArgs(); |
|
54 void spyWithBasicArgs(); |
|
55 void spyWithPointers(); |
|
56 void spyWithQtClasses(); |
|
57 void spyWithBasicQtClasses(); |
|
58 void spyWithQtTypedefs(); |
|
59 }; |
|
60 |
|
61 class QtTestObject: public QObject |
|
62 { |
|
63 Q_OBJECT |
|
64 |
|
65 public: |
|
66 QtTestObject(); |
|
67 |
|
68 signals: |
|
69 void sig0(); |
|
70 void sig1(int, int); |
|
71 void sigLong(long, long); |
|
72 void sig2(int *, int *); |
|
73 |
|
74 public: |
|
75 QString slotResult; |
|
76 friend class tst_QSignalSpy; |
|
77 }; |
|
78 |
|
79 QtTestObject::QtTestObject() |
|
80 { |
|
81 } |
|
82 |
|
83 void tst_QSignalSpy::spyWithoutArgs() |
|
84 { |
|
85 QtTestObject obj; |
|
86 |
|
87 QSignalSpy spy(&obj, SIGNAL(sig0())); |
|
88 QCOMPARE(spy.count(), 0); |
|
89 |
|
90 emit obj.sig0(); |
|
91 QCOMPARE(spy.count(), 1); |
|
92 emit obj.sig0(); |
|
93 QCOMPARE(spy.count(), 2); |
|
94 |
|
95 QList<QVariant> args = spy.takeFirst(); |
|
96 QCOMPARE(args.count(), 0); |
|
97 } |
|
98 |
|
99 void tst_QSignalSpy::spyWithBasicArgs() |
|
100 { |
|
101 QtTestObject obj; |
|
102 QSignalSpy spy(&obj, SIGNAL(sig1(int,int))); |
|
103 |
|
104 emit obj.sig1(1, 2); |
|
105 QCOMPARE(spy.count(), 1); |
|
106 |
|
107 QList<QVariant> args = spy.takeFirst(); |
|
108 QCOMPARE(args.count(), 2); |
|
109 QCOMPARE(args.at(0).toInt(), 1); |
|
110 QCOMPARE(args.at(1).toInt(), 2); |
|
111 |
|
112 QSignalSpy spyl(&obj, SIGNAL(sigLong(long,long))); |
|
113 |
|
114 emit obj.sigLong(1l, 2l); |
|
115 args = spyl.takeFirst(); |
|
116 QCOMPARE(args.count(), 2); |
|
117 QCOMPARE(qvariant_cast<long>(args.at(0)), 1l); |
|
118 QCOMPARE(qvariant_cast<long>(args.at(1)), 2l); |
|
119 } |
|
120 |
|
121 |
|
122 void tst_QSignalSpy::spyWithPointers() |
|
123 { |
|
124 qRegisterMetaType<int *>("int*"); |
|
125 |
|
126 QtTestObject obj; |
|
127 QSignalSpy spy(&obj, SIGNAL(sig2(int*,int*))); |
|
128 |
|
129 int i1 = 1; |
|
130 int i2 = 2; |
|
131 |
|
132 emit obj.sig2(&i1, &i2); |
|
133 QCOMPARE(spy.count(), 1); |
|
134 |
|
135 QList<QVariant> args = spy.takeFirst(); |
|
136 QCOMPARE(args.count(), 2); |
|
137 QCOMPARE(*static_cast<int * const *>(args.at(0).constData()), &i1); |
|
138 QCOMPARE(*static_cast<int * const *>(args.at(1).constData()), &i2); |
|
139 } |
|
140 |
|
141 class QtTestObject2: public QObject |
|
142 { |
|
143 Q_OBJECT |
|
144 friend class tst_QSignalSpy; |
|
145 |
|
146 signals: |
|
147 void sig(QString); |
|
148 void sig2(const QDateTime &dt); |
|
149 void sig3(QObject *o); |
|
150 void sig4(QChar c); |
|
151 }; |
|
152 |
|
153 void tst_QSignalSpy::spyWithBasicQtClasses() |
|
154 { |
|
155 QtTestObject2 obj; |
|
156 |
|
157 QSignalSpy spy(&obj, SIGNAL(sig(QString))); |
|
158 emit obj.sig(QString("bubu")); |
|
159 QCOMPARE(spy.count(), 1); |
|
160 QCOMPARE(spy.at(0).count(), 1); |
|
161 QCOMPARE(spy.at(0).at(0).toString(), QString("bubu")); |
|
162 } |
|
163 |
|
164 void tst_QSignalSpy::spyWithQtClasses() |
|
165 { |
|
166 QtTestObject2 obj; |
|
167 |
|
168 qRegisterMetaType<QDateTime>("QDateTime"); |
|
169 |
|
170 QSignalSpy spy(&obj, SIGNAL(sig2(QDateTime))); |
|
171 QDateTime dt = QDateTime::currentDateTime(); |
|
172 emit obj.sig2(dt); |
|
173 QCOMPARE(spy.count(), 1); |
|
174 QCOMPARE(spy.at(0).count(), 1); |
|
175 QCOMPARE(spy.at(0).at(0).typeName(), "QDateTime"); |
|
176 QCOMPARE(*static_cast<const QDateTime *>(spy.at(0).at(0).constData()), dt); |
|
177 QCOMPARE(spy.at(0).at(0).toDateTime(), dt); |
|
178 |
|
179 QSignalSpy spy2(&obj, SIGNAL(sig3(QObject*))); |
|
180 emit obj.sig3(this); |
|
181 QCOMPARE(*static_cast<QObject * const *>(spy2.value(0).value(0).constData()), |
|
182 (QObject *)this); |
|
183 QCOMPARE(qvariant_cast<QObject *>(spy2.value(0).value(0)), (QObject*)this); |
|
184 |
|
185 QSignalSpy spy3(&obj, SIGNAL(sig4(QChar))); |
|
186 emit obj.sig4(QChar('A')); |
|
187 QCOMPARE(qvariant_cast<QChar>(spy3.value(0).value(0)), QChar('A')); |
|
188 } |
|
189 |
|
190 class QtTestObject3: public QObject |
|
191 { |
|
192 Q_OBJECT |
|
193 friend class tst_QSignalSpy; |
|
194 |
|
195 signals: |
|
196 void sig1(quint16); |
|
197 void sig2(qlonglong, qulonglong); |
|
198 void sig3(qint64, quint64); |
|
199 }; |
|
200 |
|
201 void tst_QSignalSpy::spyWithQtTypedefs() |
|
202 { |
|
203 QtTestObject3 obj; |
|
204 |
|
205 // QSignalSpy spy1(&obj, SIGNAL(sig1(quint16))); |
|
206 // emit obj.sig1(42); |
|
207 // QCOMPARE(spy1.value(0).value(0).toInt(), 42); |
|
208 |
|
209 QSignalSpy spy2(&obj, SIGNAL(sig2(qlonglong,qulonglong))); |
|
210 emit obj.sig2(42, 43); |
|
211 QCOMPARE(spy2.value(0).value(0).toInt(), 42); |
|
212 QCOMPARE(spy2.value(0).value(1).toInt(), 43); |
|
213 |
|
214 // QSignalSpy spy3(&obj, SIGNAL(sig3(qint64,quint64))); |
|
215 // emit obj.sig3(44, 45); |
|
216 // QCOMPARE(spy3.value(0).value(0).toInt(), 44); |
|
217 // QCOMPARE(spy3.value(0).value(1).toInt(), 45); |
|
218 } |
|
219 |
|
220 QTEST_APPLESS_MAIN(tst_QSignalSpy) |
|
221 #include "tst_qsignalspy.moc" |