author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite module 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 |
#include <QtTest/QtTest> |
|
43 |
||
44 |
#include "qstate.h" |
|
45 |
#include "qstatemachine.h" |
|
46 |
#include "qsignaltransition.h" |
|
47 |
||
48 |
// Will try to wait for the condition while allowing event processing |
|
49 |
#define QTRY_COMPARE(__expr, __expected) \ |
|
50 |
do { \ |
|
51 |
const int __step = 50; \ |
|
52 |
const int __timeout = 5000; \ |
|
53 |
if ((__expr) != (__expected)) { \ |
|
54 |
QTest::qWait(0); \ |
|
55 |
} \ |
|
56 |
for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ |
|
57 |
QTest::qWait(__step); \ |
|
58 |
} \ |
|
59 |
QCOMPARE(__expr, __expected); \ |
|
60 |
} while(0) |
|
61 |
||
62 |
//TESTED_CLASS= |
|
63 |
//TESTED_FILES= |
|
64 |
||
65 |
class tst_QState : public QObject |
|
66 |
{ |
|
67 |
Q_OBJECT |
|
68 |
||
69 |
public: |
|
70 |
tst_QState(); |
|
71 |
virtual ~tst_QState(); |
|
72 |
||
73 |
private slots: |
|
74 |
#if 0 |
|
75 |
void test(); |
|
76 |
#endif |
|
77 |
void assignProperty(); |
|
78 |
void assignPropertyTwice(); |
|
79 |
void historyInitialState(); |
|
80 |
||
81 |
private: |
|
82 |
bool functionCalled; |
|
83 |
}; |
|
84 |
||
85 |
tst_QState::tst_QState() : functionCalled(false) |
|
86 |
{ |
|
87 |
} |
|
88 |
||
89 |
tst_QState::~tst_QState() |
|
90 |
{ |
|
91 |
} |
|
92 |
||
93 |
#if 0 |
|
94 |
void tst_QState::test() |
|
95 |
{ |
|
96 |
QStateMachine machine; |
|
97 |
QState *s1 = new QState(&machine); |
|
98 |
||
99 |
QCOMPARE(s1->machine(), &machine); |
|
100 |
QCOMPARE(s1->parentState(), &machine); |
|
101 |
QCOMPARE(s1->initialState(), (QState*)0); |
|
102 |
QVERIFY(s1->childStates().isEmpty()); |
|
103 |
QVERIFY(s1->transitions().isEmpty()); |
|
104 |
||
105 |
QCOMPARE(s1->isFinal(), false); |
|
106 |
s1->setFinal(true); |
|
107 |
QCOMPARE(s1->isFinal(), true); |
|
108 |
s1->setFinal(false); |
|
109 |
QCOMPARE(s1->isFinal(), false); |
|
110 |
||
111 |
QCOMPARE(s1->isParallel(), false); |
|
112 |
s1->setParallel(true); |
|
113 |
QCOMPARE(s1->isParallel(), true); |
|
114 |
s1->setParallel(false); |
|
115 |
QCOMPARE(s1->isParallel(), false); |
|
116 |
||
117 |
QCOMPARE(s1->isAtomic(), true); |
|
118 |
QCOMPARE(s1->isCompound(), false); |
|
119 |
QCOMPARE(s1->isComplex(), false); |
|
120 |
||
121 |
QState *s11 = new QState(s1); |
|
122 |
QCOMPARE(s11->parentState(), s1); |
|
123 |
QCOMPARE(s11->isAtomic(), true); |
|
124 |
QCOMPARE(s11->isCompound(), false); |
|
125 |
QCOMPARE(s11->isComplex(), false); |
|
126 |
QCOMPARE(s11->machine(), s1->machine()); |
|
127 |
QVERIFY(s11->isDescendantOf(s1)); |
|
128 |
||
129 |
QCOMPARE(s1->initialState(), (QState*)0); |
|
130 |
QCOMPARE(s1->childStates().size(), 1); |
|
131 |
QCOMPARE(s1->childStates().at(0), s11); |
|
132 |
||
133 |
QCOMPARE(s1->isAtomic(), false); |
|
134 |
QCOMPARE(s1->isCompound(), true); |
|
135 |
QCOMPARE(s1->isComplex(), true); |
|
136 |
||
137 |
s1->setParallel(true); |
|
138 |
QCOMPARE(s1->isAtomic(), false); |
|
139 |
QCOMPARE(s1->isCompound(), false); |
|
140 |
QCOMPARE(s1->isComplex(), true); |
|
141 |
||
142 |
QState *s12 = new QState(s1); |
|
143 |
QCOMPARE(s12->parentState(), s1); |
|
144 |
QCOMPARE(s12->isAtomic(), true); |
|
145 |
QCOMPARE(s12->isCompound(), false); |
|
146 |
QCOMPARE(s12->isComplex(), false); |
|
147 |
QCOMPARE(s12->machine(), s1->machine()); |
|
148 |
QVERIFY(s12->isDescendantOf(s1)); |
|
149 |
QVERIFY(!s12->isDescendantOf(s11)); |
|
150 |
||
151 |
QCOMPARE(s1->initialState(), (QState*)0); |
|
152 |
QCOMPARE(s1->childStates().size(), 2); |
|
153 |
QCOMPARE(s1->childStates().at(0), s11); |
|
154 |
QCOMPARE(s1->childStates().at(1), s12); |
|
155 |
||
156 |
QCOMPARE(s1->isAtomic(), false); |
|
157 |
QCOMPARE(s1->isCompound(), false); |
|
158 |
QCOMPARE(s1->isComplex(), true); |
|
159 |
||
160 |
s1->setParallel(false); |
|
161 |
QCOMPARE(s1->isAtomic(), false); |
|
162 |
QCOMPARE(s1->isCompound(), true); |
|
163 |
QCOMPARE(s1->isComplex(), true); |
|
164 |
||
165 |
s1->setInitialState(s11); |
|
166 |
QCOMPARE(s1->initialState(), s11); |
|
167 |
||
168 |
s1->setInitialState(0); |
|
169 |
QCOMPARE(s1->initialState(), (QState*)0); |
|
170 |
||
171 |
s1->setInitialState(s12); |
|
172 |
QCOMPARE(s1->initialState(), s12); |
|
173 |
||
174 |
QState *s13 = new QState(); |
|
175 |
s1->setInitialState(s13); |
|
176 |
QCOMPARE(s13->parentState(), s1); |
|
177 |
QCOMPARE(s1->childStates().size(), 3); |
|
178 |
QCOMPARE(s1->childStates().at(0), s11); |
|
179 |
QCOMPARE(s1->childStates().at(1), s12); |
|
180 |
QCOMPARE(s1->childStates().at(2), s13); |
|
181 |
QVERIFY(s13->isDescendantOf(s1)); |
|
182 |
||
183 |
QVERIFY(s12->childStates().isEmpty()); |
|
184 |
||
185 |
QState *s121 = new QState(s12); |
|
186 |
QCOMPARE(s121->parentState(), s12); |
|
187 |
QCOMPARE(s121->isAtomic(), true); |
|
188 |
QCOMPARE(s121->isCompound(), false); |
|
189 |
QCOMPARE(s121->isComplex(), false); |
|
190 |
QCOMPARE(s121->machine(), s12->machine()); |
|
191 |
QVERIFY(s121->isDescendantOf(s12)); |
|
192 |
QVERIFY(s121->isDescendantOf(s1)); |
|
193 |
QVERIFY(!s121->isDescendantOf(s11)); |
|
194 |
||
195 |
QCOMPARE(s12->childStates().size(), 1); |
|
196 |
QCOMPARE(s12->childStates().at(0), (QState*)s121); |
|
197 |
||
198 |
QCOMPARE(s1->childStates().size(), 3); |
|
199 |
QCOMPARE(s1->childStates().at(0), s11); |
|
200 |
QCOMPARE(s1->childStates().at(1), s12); |
|
201 |
QCOMPARE(s1->childStates().at(2), s13); |
|
202 |
||
203 |
s11->addTransition(s12); |
|
204 |
QCOMPARE(s11->transitions().size(), 1); |
|
205 |
QCOMPARE(s11->transitions().at(0)->sourceState(), s11); |
|
206 |
QCOMPARE(s11->transitions().at(0)->targetStates().size(), 1); |
|
207 |
QCOMPARE(s11->transitions().at(0)->targetStates().at(0), s12); |
|
208 |
QCOMPARE(s11->transitions().at(0)->eventType(), QEvent::None); |
|
209 |
||
210 |
QState *s14 = new QState(); |
|
211 |
s12->addTransition(QList<QState*>() << s13 << s14); |
|
212 |
QCOMPARE(s12->transitions().size(), 1); |
|
213 |
QCOMPARE(s12->transitions().at(0)->sourceState(), s12); |
|
214 |
QCOMPARE(s12->transitions().at(0)->targetStates().size(), 2); |
|
215 |
QCOMPARE(s12->transitions().at(0)->targetStates().at(0), s13); |
|
216 |
QCOMPARE(s12->transitions().at(0)->targetStates().at(1), s14); |
|
217 |
QCOMPARE(s12->transitions().at(0)->eventType(), QEvent::None); |
|
218 |
||
219 |
s13->addTransition(this, SIGNAL(destroyed()), s14); |
|
220 |
QCOMPARE(s13->transitions().size(), 1); |
|
221 |
QCOMPARE(s13->transitions().at(0)->sourceState(), s13); |
|
222 |
QCOMPARE(s13->transitions().at(0)->targetStates().size(), 1); |
|
223 |
QCOMPARE(s13->transitions().at(0)->targetStates().at(0), s14); |
|
224 |
QCOMPARE(s13->transitions().at(0)->eventType(), QEvent::Signal); |
|
225 |
QVERIFY(qobject_cast<QSignalTransition*>(s13->transitions().at(0)) != 0); |
|
226 |
||
227 |
delete s13->transitions().at(0); |
|
228 |
QCOMPARE(s13->transitions().size(), 0); |
|
229 |
||
230 |
s12->addTransition(this, SIGNAL(destroyed()), s11); |
|
231 |
QCOMPARE(s12->transitions().size(), 2); |
|
232 |
} |
|
233 |
#endif |
|
234 |
||
235 |
class TestClass: public QObject |
|
236 |
{ |
|
237 |
Q_OBJECT |
|
238 |
public: |
|
239 |
TestClass() : called(false) {} |
|
240 |
bool called; |
|
241 |
||
242 |
public slots: |
|
243 |
void slot() { called = true; } |
|
244 |
||
245 |
||
246 |
}; |
|
247 |
||
248 |
void tst_QState::assignProperty() |
|
249 |
{ |
|
250 |
QStateMachine machine; |
|
251 |
||
252 |
QObject *object = new QObject(); |
|
253 |
object->setProperty("fooBar", 10); |
|
254 |
||
255 |
QState *s1 = new QState(&machine); |
|
256 |
s1->assignProperty(object, "fooBar", 20); |
|
257 |
||
258 |
machine.setInitialState(s1); |
|
259 |
machine.start(); |
|
260 |
QCoreApplication::processEvents(); |
|
261 |
||
262 |
QCOMPARE(object->property("fooBar").toInt(), 20); |
|
263 |
} |
|
264 |
||
265 |
void tst_QState::assignPropertyTwice() |
|
266 |
{ |
|
267 |
QStateMachine machine; |
|
268 |
||
269 |
QObject *object = new QObject(); |
|
270 |
object->setProperty("fooBar", 10); |
|
271 |
||
272 |
QState *s1 = new QState(&machine); |
|
273 |
s1->assignProperty(object, "fooBar", 20); |
|
274 |
s1->assignProperty(object, "fooBar", 30); |
|
275 |
||
276 |
machine.setInitialState(s1); |
|
277 |
machine.start(); |
|
278 |
QCoreApplication::processEvents(); |
|
279 |
||
280 |
QCOMPARE(object->property("fooBar").toInt(), 30); |
|
281 |
} |
|
282 |
||
283 |
class EventTestTransition: public QAbstractTransition |
|
284 |
{ |
|
285 |
public: |
|
286 |
EventTestTransition(QEvent::Type type, QState *targetState) |
|
287 |
: QAbstractTransition(), m_type(type) |
|
288 |
{ |
|
289 |
setTargetState(targetState); |
|
290 |
} |
|
291 |
||
292 |
protected: |
|
293 |
bool eventTest(QEvent *e) |
|
294 |
{ |
|
295 |
return e->type() == m_type; |
|
296 |
} |
|
297 |
||
298 |
void onTransition(QEvent *) {} |
|
299 |
||
300 |
private: |
|
301 |
QEvent::Type m_type; |
|
302 |
||
303 |
}; |
|
304 |
||
305 |
void tst_QState::historyInitialState() |
|
306 |
{ |
|
307 |
QStateMachine machine; |
|
308 |
||
309 |
QState *s1 = new QState(&machine); |
|
310 |
||
311 |
QState *s2 = new QState(&machine); |
|
312 |
QHistoryState *h1 = new QHistoryState(s2); |
|
313 |
||
314 |
s2->setInitialState(h1); |
|
315 |
||
316 |
QState *s3 = new QState(s2); |
|
317 |
h1->setDefaultState(s3); |
|
318 |
||
319 |
QState *s4 = new QState(s2); |
|
320 |
||
321 |
s1->addTransition(new EventTestTransition(QEvent::User, s2)); |
|
322 |
s2->addTransition(new EventTestTransition(QEvent::User, s1)); |
|
323 |
s3->addTransition(new EventTestTransition(QEvent::Type(QEvent::User+1), s4)); |
|
324 |
||
325 |
machine.setInitialState(s1); |
|
326 |
machine.start(); |
|
327 |
QCoreApplication::processEvents(); |
|
328 |
||
329 |
QCOMPARE(machine.configuration().size(), 1); |
|
330 |
QVERIFY(machine.configuration().contains(s1)); |
|
331 |
||
332 |
machine.postEvent(new QEvent(QEvent::User)); |
|
333 |
QCoreApplication::processEvents(); |
|
334 |
||
335 |
QCOMPARE(machine.configuration().size(), 2); |
|
336 |
QVERIFY(machine.configuration().contains(s2)); |
|
337 |
QVERIFY(machine.configuration().contains(s3)); |
|
338 |
||
339 |
machine.postEvent(new QEvent(QEvent::User)); |
|
340 |
QCoreApplication::processEvents(); |
|
341 |
||
342 |
QCOMPARE(machine.configuration().size(), 1); |
|
343 |
QVERIFY(machine.configuration().contains(s1)); |
|
344 |
||
345 |
machine.postEvent(new QEvent(QEvent::User)); |
|
346 |
QCoreApplication::processEvents(); |
|
347 |
||
348 |
QCOMPARE(machine.configuration().size(), 2); |
|
349 |
QVERIFY(machine.configuration().contains(s2)); |
|
350 |
QVERIFY(machine.configuration().contains(s3)); |
|
351 |
||
352 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User+1))); |
|
353 |
QCoreApplication::processEvents(); |
|
354 |
||
355 |
QCOMPARE(machine.configuration().size(), 2); |
|
356 |
QVERIFY(machine.configuration().contains(s2)); |
|
357 |
QVERIFY(machine.configuration().contains(s4)); |
|
358 |
||
359 |
machine.postEvent(new QEvent(QEvent::User)); |
|
360 |
QCoreApplication::processEvents(); |
|
361 |
||
362 |
QCOMPARE(machine.configuration().size(), 1); |
|
363 |
QVERIFY(machine.configuration().contains(s1)); |
|
364 |
||
365 |
machine.postEvent(new QEvent(QEvent::User)); |
|
366 |
QCoreApplication::processEvents(); |
|
367 |
||
368 |
QCOMPARE(machine.configuration().size(), 2); |
|
369 |
QVERIFY(machine.configuration().contains(s2)); |
|
370 |
QVERIFY(machine.configuration().contains(s4)); |
|
371 |
} |
|
372 |
||
373 |
||
374 |
QTEST_MAIN(tst_QState) |
|
375 |
#include "tst_qstate.moc" |