author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 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 |
#include <QtCore/QCoreApplication> |
|
44 |
#include <QtGui/QPushButton> |
|
45 |
#include <QtGui/QGraphicsScene> |
|
46 |
#include <QtGui/QGraphicsSceneEvent> |
|
47 |
#include <QtGui/QGraphicsTextItem> |
|
48 |
||
49 |
#include "qstatemachine.h" |
|
50 |
#include "qstate.h" |
|
51 |
#include "qhistorystate.h" |
|
52 |
#include "qkeyeventtransition.h" |
|
53 |
#include "qmouseeventtransition.h" |
|
54 |
#include "private/qstate_p.h" |
|
55 |
#include "private/qstatemachine_p.h" |
|
56 |
||
57 |
// Will try to wait for the condition while allowing event processing |
|
58 |
#define QTRY_COMPARE(__expr, __expected) \ |
|
59 |
do { \ |
|
60 |
const int __step = 50; \ |
|
61 |
const int __timeout = 5000; \ |
|
62 |
if ((__expr) != (__expected)) { \ |
|
63 |
QTest::qWait(0); \ |
|
64 |
} \ |
|
65 |
for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ |
|
66 |
QTest::qWait(__step); \ |
|
67 |
} \ |
|
68 |
QCOMPARE(__expr, __expected); \ |
|
69 |
} while(0) |
|
70 |
||
71 |
//TESTED_CLASS= |
|
72 |
//TESTED_FILES= |
|
73 |
||
74 |
static int globalTick; |
|
75 |
||
76 |
// Run exec for a maximum of TIMEOUT msecs |
|
77 |
#define QCOREAPPLICATION_EXEC(TIMEOUT) \ |
|
78 |
{ \ |
|
79 |
QTimer timer; \ |
|
80 |
timer.setSingleShot(true); \ |
|
81 |
timer.setInterval(TIMEOUT); \ |
|
82 |
timer.start(); \ |
|
83 |
connect(&timer, SIGNAL(timeout()), QCoreApplication::instance(), SLOT(quit())); \ |
|
84 |
QCoreApplication::exec(); \ |
|
85 |
} |
|
86 |
||
87 |
class SignalEmitter : public QObject |
|
88 |
{ |
|
89 |
Q_OBJECT |
|
90 |
public: |
|
91 |
SignalEmitter(QObject *parent = 0) |
|
92 |
: QObject(parent) {} |
|
93 |
void emitSignalWithNoArg() |
|
94 |
{ emit signalWithNoArg(); } |
|
95 |
void emitSignalWithIntArg(int arg) |
|
96 |
{ emit signalWithIntArg(arg); } |
|
97 |
void emitSignalWithStringArg(const QString &arg) |
|
98 |
{ emit signalWithStringArg(arg); } |
|
99 |
void emitSignalWithDefaultArg() |
|
100 |
{ emit signalWithDefaultArg(); } |
|
101 |
Q_SIGNALS: |
|
102 |
void signalWithNoArg(); |
|
103 |
void signalWithIntArg(int); |
|
104 |
void signalWithStringArg(const QString &); |
|
105 |
void signalWithDefaultArg(int i = 42); |
|
106 |
}; |
|
107 |
||
108 |
class tst_QStateMachine : public QObject |
|
109 |
{ |
|
110 |
Q_OBJECT |
|
111 |
public: |
|
112 |
tst_QStateMachine(); |
|
113 |
virtual ~tst_QStateMachine(); |
|
114 |
||
115 |
private slots: |
|
116 |
void init(); |
|
117 |
void cleanup(); |
|
118 |
||
119 |
void rootState(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
120 |
void machineWithParent(); |
0 | 121 |
void addAndRemoveState(); |
122 |
void stateEntryAndExit(); |
|
123 |
void assignProperty(); |
|
124 |
void assignPropertyWithAnimation(); |
|
125 |
void postEvent(); |
|
126 |
void cancelDelayedEvent(); |
|
127 |
void postDelayedEventAndStop(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
void stopAndPostEvent(); |
0 | 129 |
void stateFinished(); |
130 |
void parallelStates(); |
|
131 |
void parallelRootState(); |
|
132 |
void allSourceToTargetConfigurations(); |
|
133 |
void signalTransitions(); |
|
134 |
void eventTransitions(); |
|
135 |
void graphicsSceneEventTransitions(); |
|
136 |
void historyStates(); |
|
137 |
void startAndStop(); |
|
138 |
void targetStateWithNoParent(); |
|
139 |
void targetStateDeleted(); |
|
140 |
void transitionToRootState(); |
|
141 |
void transitionFromRootState(); |
|
142 |
void transitionEntersParent(); |
|
143 |
||
144 |
void defaultErrorState(); |
|
145 |
void customGlobalErrorState(); |
|
146 |
void customLocalErrorStateInBrokenState(); |
|
147 |
void customLocalErrorStateInOtherState(); |
|
148 |
void customLocalErrorStateInParentOfBrokenState(); |
|
149 |
void customLocalErrorStateOverridesParent(); |
|
150 |
void errorStateHasChildren(); |
|
151 |
void errorStateHasErrors(); |
|
152 |
void errorStateIsRootState(); |
|
153 |
void errorStateEntersParentFirst(); |
|
154 |
void customErrorStateIsNull(); |
|
155 |
void clearError(); |
|
156 |
void historyStateHasNowhereToGo(); |
|
157 |
void historyStateAsInitialState(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
158 |
void historyStateAfterRestart(); |
0 | 159 |
void brokenStateIsNeverEntered(); |
160 |
void customErrorStateNotInGraph(); |
|
161 |
void transitionToStateNotInGraph(); |
|
162 |
void restoreProperties(); |
|
163 |
||
164 |
void defaultGlobalRestorePolicy(); |
|
165 |
void globalRestorePolicySetToRestore(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
void globalRestorePolicySetToDontRestore(); |
0 | 167 |
|
168 |
void noInitialStateForInitialState(); |
|
169 |
||
170 |
//void restorePolicyNotInherited(); |
|
171 |
//void mixedRestoreProperties(); |
|
172 |
//void setRestorePolicyToDoNotRestore(); |
|
173 |
//void setGlobalRestorePolicyToGlobalRestore(); |
|
174 |
//void restorePolicyOnChildState(); |
|
175 |
||
176 |
void transitionWithParent(); |
|
177 |
void transitionsFromParallelStateWithNoChildren(); |
|
178 |
void parallelStateTransition(); |
|
179 |
void parallelStateAssignmentsDone(); |
|
180 |
void nestedRestoreProperties(); |
|
181 |
void nestedRestoreProperties2(); |
|
182 |
||
183 |
void simpleAnimation(); |
|
184 |
void twoAnimations(); |
|
185 |
void twoAnimatedTransitions(); |
|
186 |
void playAnimationTwice(); |
|
187 |
void nestedTargetStateForAnimation(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
void propertiesAssignedSignalTransitionsReuseAnimationGroup(); |
0 | 189 |
void animatedGlobalRestoreProperty(); |
190 |
void specificTargetValueOfAnimation(); |
|
191 |
||
192 |
void addDefaultAnimation(); |
|
193 |
void addDefaultAnimationWithUnusedAnimation(); |
|
194 |
void removeDefaultAnimation(); |
|
195 |
void overrideDefaultAnimationWithSpecific(); |
|
196 |
||
197 |
// void addDefaultAnimationForSource(); |
|
198 |
// void addDefaultAnimationForTarget(); |
|
199 |
// void removeDefaultAnimationForSource(); |
|
200 |
// void removeDefaultAnimationForTarget(); |
|
201 |
// void overrideDefaultAnimationWithSource(); |
|
202 |
// void overrideDefaultAnimationWithTarget(); |
|
203 |
// void overrideDefaultSourceAnimationWithSpecific(); |
|
204 |
// void overrideDefaultTargetAnimationWithSpecific(); |
|
205 |
// void overrideDefaultTargetAnimationWithSource(); |
|
206 |
||
207 |
void nestedStateMachines(); |
|
208 |
void goToState(); |
|
209 |
||
210 |
void task260403_clonedSignals(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
void postEventFromOtherThread(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
void eventFilterForApplication(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
void eventClassesExported(); |
0 | 214 |
}; |
215 |
||
216 |
tst_QStateMachine::tst_QStateMachine() |
|
217 |
{ |
|
218 |
} |
|
219 |
||
220 |
tst_QStateMachine::~tst_QStateMachine() |
|
221 |
{ |
|
222 |
} |
|
223 |
||
224 |
class TestState : public QState |
|
225 |
{ |
|
226 |
public: |
|
227 |
enum Event { |
|
228 |
Entry, |
|
229 |
Exit |
|
230 |
}; |
|
231 |
TestState(QState *parent) |
|
232 |
: QState(parent) {} |
|
233 |
QList<QPair<int, Event> > events; |
|
234 |
protected: |
|
235 |
virtual void onEntry(QEvent *) { |
|
236 |
events.append(qMakePair(globalTick++, Entry)); |
|
237 |
} |
|
238 |
virtual void onExit(QEvent *) { |
|
239 |
events.append(qMakePair(globalTick++, Exit)); |
|
240 |
} |
|
241 |
}; |
|
242 |
||
243 |
class TestTransition : public QAbstractTransition |
|
244 |
{ |
|
245 |
public: |
|
246 |
TestTransition(QAbstractState *target) |
|
247 |
: QAbstractTransition() |
|
248 |
{ setTargetState(target); } |
|
249 |
QList<int> triggers; |
|
250 |
protected: |
|
251 |
virtual bool eventTest(QEvent *) { |
|
252 |
return true; |
|
253 |
} |
|
254 |
virtual void onTransition(QEvent *) { |
|
255 |
triggers.append(globalTick++); |
|
256 |
} |
|
257 |
}; |
|
258 |
||
259 |
void tst_QStateMachine::init() |
|
260 |
{ |
|
261 |
} |
|
262 |
||
263 |
void tst_QStateMachine::cleanup() |
|
264 |
{ |
|
265 |
} |
|
266 |
||
267 |
class EventTransition : public QAbstractTransition |
|
268 |
{ |
|
269 |
public: |
|
270 |
EventTransition(QEvent::Type type, QAbstractState *target, QState *parent = 0) |
|
271 |
: QAbstractTransition(parent), m_type(type) |
|
272 |
{ setTargetState(target); } |
|
273 |
protected: |
|
274 |
virtual bool eventTest(QEvent *e) { |
|
275 |
return (e->type() == m_type); |
|
276 |
} |
|
277 |
virtual void onTransition(QEvent *) {} |
|
278 |
private: |
|
279 |
QEvent::Type m_type; |
|
280 |
}; |
|
281 |
||
282 |
void tst_QStateMachine::transitionToRootState() |
|
283 |
{ |
|
284 |
QStateMachine machine; |
|
285 |
machine.setObjectName("machine"); |
|
286 |
||
287 |
QState *initialState = new QState(); |
|
288 |
initialState->setObjectName("initial"); |
|
289 |
machine.addState(initialState); |
|
290 |
machine.setInitialState(initialState); |
|
291 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
QAbstractTransition *trans = new EventTransition(QEvent::User, &machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
initialState->addTransition(trans); |
0 | 294 |
QCOMPARE(trans->sourceState(), initialState); |
295 |
QCOMPARE(trans->targetState(), static_cast<QAbstractState *>(&machine)); |
|
296 |
||
297 |
machine.start(); |
|
298 |
QCoreApplication::processEvents(); |
|
299 |
||
300 |
QCOMPARE(machine.configuration().count(), 1); |
|
301 |
QVERIFY(machine.configuration().contains(initialState)); |
|
302 |
||
303 |
machine.postEvent(new QEvent(QEvent::User)); |
|
304 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 'initial'"); |
|
305 |
QCoreApplication::processEvents(); |
|
306 |
QVERIFY(machine.configuration().isEmpty()); |
|
307 |
QVERIFY(!machine.isRunning()); |
|
308 |
} |
|
309 |
||
310 |
void tst_QStateMachine::transitionFromRootState() |
|
311 |
{ |
|
312 |
QStateMachine machine; |
|
313 |
QState *root = &machine; |
|
314 |
QState *s1 = new QState(root); |
|
315 |
EventTransition *trans = new EventTransition(QEvent::User, s1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
316 |
root->addTransition(trans); |
0 | 317 |
QCOMPARE(trans->sourceState(), root); |
318 |
QCOMPARE(trans->targetState(), static_cast<QAbstractState *>(s1)); |
|
319 |
} |
|
320 |
||
321 |
void tst_QStateMachine::transitionEntersParent() |
|
322 |
{ |
|
323 |
QStateMachine machine; |
|
324 |
||
325 |
QObject *entryController = new QObject(&machine); |
|
326 |
entryController->setObjectName("entryController"); |
|
327 |
entryController->setProperty("greatGrandParentEntered", false); |
|
328 |
entryController->setProperty("grandParentEntered", false); |
|
329 |
entryController->setProperty("parentEntered", false); |
|
330 |
entryController->setProperty("stateEntered", false); |
|
331 |
||
332 |
QState *greatGrandParent = new QState(); |
|
333 |
greatGrandParent->setObjectName("grandParent"); |
|
334 |
greatGrandParent->assignProperty(entryController, "greatGrandParentEntered", true); |
|
335 |
machine.addState(greatGrandParent); |
|
336 |
machine.setInitialState(greatGrandParent); |
|
337 |
||
338 |
QState *grandParent = new QState(greatGrandParent); |
|
339 |
grandParent->setObjectName("grandParent"); |
|
340 |
grandParent->assignProperty(entryController, "grandParentEntered", true); |
|
341 |
||
342 |
QState *parent = new QState(grandParent); |
|
343 |
parent->setObjectName("parent"); |
|
344 |
parent->assignProperty(entryController, "parentEntered", true); |
|
345 |
||
346 |
QState *state = new QState(parent); |
|
347 |
state->setObjectName("state"); |
|
348 |
state->assignProperty(entryController, "stateEntered", true); |
|
349 |
||
350 |
QState *initialStateOfGreatGrandParent = new QState(greatGrandParent); |
|
351 |
initialStateOfGreatGrandParent->setObjectName("initialStateOfGreatGrandParent"); |
|
352 |
greatGrandParent->setInitialState(initialStateOfGreatGrandParent); |
|
353 |
||
354 |
initialStateOfGreatGrandParent->addTransition(new EventTransition(QEvent::User, state)); |
|
355 |
||
356 |
machine.start(); |
|
357 |
QCoreApplication::processEvents(); |
|
358 |
||
359 |
QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), true); |
|
360 |
QCOMPARE(entryController->property("grandParentEntered").toBool(), false); |
|
361 |
QCOMPARE(entryController->property("parentEntered").toBool(), false); |
|
362 |
QCOMPARE(entryController->property("stateEntered").toBool(), false); |
|
363 |
QCOMPARE(machine.configuration().count(), 2); |
|
364 |
QVERIFY(machine.configuration().contains(greatGrandParent)); |
|
365 |
QVERIFY(machine.configuration().contains(initialStateOfGreatGrandParent)); |
|
366 |
||
367 |
entryController->setProperty("greatGrandParentEntered", false); |
|
368 |
entryController->setProperty("grandParentEntered", false); |
|
369 |
entryController->setProperty("parentEntered", false); |
|
370 |
entryController->setProperty("stateEntered", false); |
|
371 |
||
372 |
machine.postEvent(new QEvent(QEvent::User)); |
|
373 |
QCoreApplication::processEvents(); |
|
374 |
||
375 |
QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), false); |
|
376 |
QCOMPARE(entryController->property("grandParentEntered").toBool(), true); |
|
377 |
QCOMPARE(entryController->property("parentEntered").toBool(), true); |
|
378 |
QCOMPARE(entryController->property("stateEntered").toBool(), true); |
|
379 |
QCOMPARE(machine.configuration().count(), 4); |
|
380 |
QVERIFY(machine.configuration().contains(greatGrandParent)); |
|
381 |
QVERIFY(machine.configuration().contains(grandParent)); |
|
382 |
QVERIFY(machine.configuration().contains(parent)); |
|
383 |
QVERIFY(machine.configuration().contains(state)); |
|
384 |
} |
|
385 |
||
386 |
void tst_QStateMachine::defaultErrorState() |
|
387 |
{ |
|
388 |
QStateMachine machine; |
|
389 |
QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); |
|
390 |
||
391 |
QState *brokenState = new QState(); |
|
392 |
brokenState->setObjectName("MyInitialState"); |
|
393 |
||
394 |
machine.addState(brokenState); |
|
395 |
machine.setInitialState(brokenState); |
|
396 |
||
397 |
QState *childState = new QState(brokenState); |
|
398 |
childState->setObjectName("childState"); |
|
399 |
||
400 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'MyInitialState'"); |
|
401 |
||
402 |
// initialState has no initial state |
|
403 |
machine.start(); |
|
404 |
QCoreApplication::processEvents(); |
|
405 |
||
406 |
QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); |
|
407 |
QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'MyInitialState'")); |
|
408 |
QCOMPARE(machine.isRunning(), false); |
|
409 |
} |
|
410 |
||
411 |
class CustomErrorState: public QState |
|
412 |
{ |
|
413 |
public: |
|
414 |
CustomErrorState(QStateMachine *machine, QState *parent = 0) |
|
415 |
: QState(parent), error(QStateMachine::NoError), m_machine(machine) |
|
416 |
{ |
|
417 |
} |
|
418 |
||
419 |
void onEntry(QEvent *) |
|
420 |
{ |
|
421 |
error = m_machine->error(); |
|
422 |
errorString = m_machine->errorString(); |
|
423 |
} |
|
424 |
||
425 |
QStateMachine::Error error; |
|
426 |
QString errorString; |
|
427 |
||
428 |
private: |
|
429 |
QStateMachine *m_machine; |
|
430 |
}; |
|
431 |
||
432 |
void tst_QStateMachine::customGlobalErrorState() |
|
433 |
{ |
|
434 |
QStateMachine machine; |
|
435 |
||
436 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
437 |
customErrorState->setObjectName("customErrorState"); |
|
438 |
machine.addState(customErrorState); |
|
439 |
machine.setErrorState(customErrorState); |
|
440 |
||
441 |
QState *initialState = new QState(); |
|
442 |
initialState->setObjectName("initialState"); |
|
443 |
machine.addState(initialState); |
|
444 |
machine.setInitialState(initialState); |
|
445 |
||
446 |
QState *brokenState = new QState(); |
|
447 |
brokenState->setObjectName("brokenState"); |
|
448 |
machine.addState(brokenState); |
|
449 |
QState *childState = new QState(brokenState); |
|
450 |
childState->setObjectName("childState"); |
|
451 |
||
452 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
453 |
machine.start(); |
|
454 |
QCoreApplication::processEvents(); |
|
455 |
||
456 |
QCOMPARE(machine.errorState(), static_cast<QAbstractState*>(customErrorState)); |
|
457 |
QCOMPARE(machine.configuration().count(), 1); |
|
458 |
QVERIFY(machine.configuration().contains(initialState)); |
|
459 |
||
460 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
461 |
QCOMPARE(machine.configuration().count(), 1); |
|
462 |
QVERIFY(machine.configuration().contains(initialState)); |
|
463 |
||
464 |
QCoreApplication::processEvents(); |
|
465 |
||
466 |
QCOMPARE(machine.isRunning(), true); |
|
467 |
QCOMPARE(machine.configuration().count(), 1); |
|
468 |
QVERIFY(machine.configuration().contains(customErrorState)); |
|
469 |
QCOMPARE(customErrorState->error, QStateMachine::NoInitialStateError); |
|
470 |
QCOMPARE(customErrorState->errorString, QString::fromLatin1("Missing initial state in compound state 'brokenState'")); |
|
471 |
QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); |
|
472 |
QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'brokenState'")); |
|
473 |
} |
|
474 |
||
475 |
void tst_QStateMachine::customLocalErrorStateInBrokenState() |
|
476 |
{ |
|
477 |
QStateMachine machine; |
|
478 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
479 |
machine.addState(customErrorState); |
|
480 |
||
481 |
QState *initialState = new QState(); |
|
482 |
initialState->setObjectName("initialState"); |
|
483 |
machine.addState(initialState); |
|
484 |
machine.setInitialState(initialState); |
|
485 |
||
486 |
QState *brokenState = new QState(); |
|
487 |
brokenState->setObjectName("brokenState"); |
|
488 |
machine.addState(brokenState); |
|
489 |
brokenState->setErrorState(customErrorState); |
|
490 |
||
491 |
QState *childState = new QState(brokenState); |
|
492 |
childState->setObjectName("childState"); |
|
493 |
||
494 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
495 |
||
496 |
machine.start(); |
|
497 |
QCoreApplication::processEvents(); |
|
498 |
||
499 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
500 |
QCoreApplication::processEvents(); |
|
501 |
||
502 |
QCOMPARE(machine.isRunning(), true); |
|
503 |
QCOMPARE(machine.configuration().count(), 1); |
|
504 |
QVERIFY(machine.configuration().contains(customErrorState)); |
|
505 |
QCOMPARE(customErrorState->error, QStateMachine::NoInitialStateError); |
|
506 |
} |
|
507 |
||
508 |
void tst_QStateMachine::customLocalErrorStateInOtherState() |
|
509 |
{ |
|
510 |
QStateMachine machine; |
|
511 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
512 |
machine.addState(customErrorState); |
|
513 |
||
514 |
QState *initialState = new QState(); |
|
515 |
initialState->setObjectName("initialState"); |
|
516 |
QTest::ignoreMessage(QtWarningMsg, "QState::setErrorState: error state cannot belong to a different state machine"); |
|
517 |
initialState->setErrorState(customErrorState); |
|
518 |
machine.addState(initialState); |
|
519 |
machine.setInitialState(initialState); |
|
520 |
||
521 |
QState *brokenState = new QState(); |
|
522 |
brokenState->setObjectName("brokenState"); |
|
523 |
||
524 |
machine.addState(brokenState); |
|
525 |
||
526 |
QState *childState = new QState(brokenState); |
|
527 |
childState->setObjectName("childState"); |
|
528 |
||
529 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
530 |
||
531 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'brokenState'"); |
|
532 |
machine.start(); |
|
533 |
QCoreApplication::processEvents(); |
|
534 |
||
535 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
536 |
QCoreApplication::processEvents(); |
|
537 |
||
538 |
QCOMPARE(machine.isRunning(), false); |
|
539 |
} |
|
540 |
||
541 |
void tst_QStateMachine::customLocalErrorStateInParentOfBrokenState() |
|
542 |
{ |
|
543 |
QStateMachine machine; |
|
544 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
545 |
machine.addState(customErrorState); |
|
546 |
||
547 |
QState *initialState = new QState(); |
|
548 |
initialState->setObjectName("initialState"); |
|
549 |
machine.addState(initialState); |
|
550 |
machine.setInitialState(initialState); |
|
551 |
||
552 |
QState *parentOfBrokenState = new QState(); |
|
553 |
machine.addState(parentOfBrokenState); |
|
554 |
parentOfBrokenState->setObjectName("parentOfBrokenState"); |
|
555 |
parentOfBrokenState->setErrorState(customErrorState); |
|
556 |
||
557 |
QState *brokenState = new QState(parentOfBrokenState); |
|
558 |
brokenState->setObjectName("brokenState"); |
|
559 |
parentOfBrokenState->setInitialState(brokenState); |
|
560 |
||
561 |
QState *childState = new QState(brokenState); |
|
562 |
childState->setObjectName("childState"); |
|
563 |
||
564 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
565 |
||
566 |
machine.start(); |
|
567 |
QCoreApplication::processEvents(); |
|
568 |
||
569 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
570 |
QCoreApplication::processEvents(); |
|
571 |
||
572 |
QCOMPARE(machine.isRunning(), true); |
|
573 |
QCOMPARE(machine.configuration().count(), 1); |
|
574 |
QVERIFY(machine.configuration().contains(customErrorState)); |
|
575 |
} |
|
576 |
||
577 |
void tst_QStateMachine::customLocalErrorStateOverridesParent() |
|
578 |
{ |
|
579 |
QStateMachine machine; |
|
580 |
CustomErrorState *customErrorStateForParent = new CustomErrorState(&machine); |
|
581 |
machine.addState(customErrorStateForParent); |
|
582 |
||
583 |
CustomErrorState *customErrorStateForBrokenState = new CustomErrorState(&machine); |
|
584 |
machine.addState(customErrorStateForBrokenState); |
|
585 |
||
586 |
QState *initialState = new QState(); |
|
587 |
initialState->setObjectName("initialState"); |
|
588 |
machine.addState(initialState); |
|
589 |
machine.setInitialState(initialState); |
|
590 |
||
591 |
QState *parentOfBrokenState = new QState(); |
|
592 |
machine.addState(parentOfBrokenState); |
|
593 |
parentOfBrokenState->setObjectName("parentOfBrokenState"); |
|
594 |
parentOfBrokenState->setErrorState(customErrorStateForParent); |
|
595 |
||
596 |
QState *brokenState = new QState(parentOfBrokenState); |
|
597 |
brokenState->setObjectName("brokenState"); |
|
598 |
brokenState->setErrorState(customErrorStateForBrokenState); |
|
599 |
parentOfBrokenState->setInitialState(brokenState); |
|
600 |
||
601 |
QState *childState = new QState(brokenState); |
|
602 |
childState->setObjectName("childState"); |
|
603 |
||
604 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
605 |
||
606 |
machine.start(); |
|
607 |
QCoreApplication::processEvents(); |
|
608 |
||
609 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
610 |
QCoreApplication::processEvents(); |
|
611 |
||
612 |
QCOMPARE(machine.configuration().count(), 1); |
|
613 |
QVERIFY(machine.configuration().contains(customErrorStateForBrokenState)); |
|
614 |
QCOMPARE(customErrorStateForBrokenState->error, QStateMachine::NoInitialStateError); |
|
615 |
QCOMPARE(customErrorStateForParent->error, QStateMachine::NoError); |
|
616 |
} |
|
617 |
||
618 |
void tst_QStateMachine::errorStateHasChildren() |
|
619 |
{ |
|
620 |
QStateMachine machine; |
|
621 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
622 |
customErrorState->setObjectName("customErrorState"); |
|
623 |
machine.addState(customErrorState); |
|
624 |
||
625 |
machine.setErrorState(customErrorState); |
|
626 |
||
627 |
QState *childOfErrorState = new QState(customErrorState); |
|
628 |
childOfErrorState->setObjectName("childOfErrorState"); |
|
629 |
customErrorState->setInitialState(childOfErrorState); |
|
630 |
||
631 |
QState *initialState = new QState(); |
|
632 |
initialState->setObjectName("initialState"); |
|
633 |
machine.addState(initialState); |
|
634 |
machine.setInitialState(initialState); |
|
635 |
||
636 |
QState *brokenState = new QState(); |
|
637 |
brokenState->setObjectName("brokenState"); |
|
638 |
machine.addState(brokenState); |
|
639 |
||
640 |
QState *childState = new QState(brokenState); |
|
641 |
childState->setObjectName("childState"); |
|
642 |
||
643 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
644 |
||
645 |
machine.start(); |
|
646 |
QCoreApplication::processEvents(); |
|
647 |
||
648 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
649 |
QCoreApplication::processEvents(); |
|
650 |
||
651 |
QCOMPARE(machine.isRunning(), true); |
|
652 |
QCOMPARE(machine.configuration().count(), 2); |
|
653 |
QVERIFY(machine.configuration().contains(customErrorState)); |
|
654 |
QVERIFY(machine.configuration().contains(childOfErrorState)); |
|
655 |
} |
|
656 |
||
657 |
||
658 |
void tst_QStateMachine::errorStateHasErrors() |
|
659 |
{ |
|
660 |
QStateMachine machine; |
|
661 |
CustomErrorState *customErrorState = new CustomErrorState(&machine); |
|
662 |
customErrorState->setObjectName("customErrorState"); |
|
663 |
machine.addState(customErrorState); |
|
664 |
||
665 |
machine.setErrorState(customErrorState); |
|
666 |
||
667 |
QState *childOfErrorState = new QState(customErrorState); |
|
668 |
childOfErrorState->setObjectName("childOfErrorState"); |
|
669 |
||
670 |
QState *initialState = new QState(); |
|
671 |
initialState->setObjectName("initialState"); |
|
672 |
machine.addState(initialState); |
|
673 |
machine.setInitialState(initialState); |
|
674 |
||
675 |
QState *brokenState = new QState(); |
|
676 |
brokenState->setObjectName("brokenState"); |
|
677 |
machine.addState(brokenState); |
|
678 |
||
679 |
QState *childState = new QState(brokenState); |
|
680 |
childState->setObjectName("childState"); |
|
681 |
||
682 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
683 |
||
684 |
machine.start(); |
|
685 |
QCoreApplication::processEvents(); |
|
686 |
||
687 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
688 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'customErrorState'"); |
|
689 |
QCoreApplication::processEvents(); |
|
690 |
||
691 |
QCOMPARE(machine.isRunning(), false); |
|
692 |
QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); |
|
693 |
QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'customErrorState'")); |
|
694 |
} |
|
695 |
||
696 |
void tst_QStateMachine::errorStateIsRootState() |
|
697 |
{ |
|
698 |
QStateMachine machine; |
|
699 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::setErrorState: root state cannot be error state"); |
|
700 |
machine.setErrorState(&machine); |
|
701 |
||
702 |
QState *initialState = new QState(); |
|
703 |
initialState->setObjectName("initialState"); |
|
704 |
machine.addState(initialState); |
|
705 |
machine.setInitialState(initialState); |
|
706 |
||
707 |
QState *brokenState = new QState(); |
|
708 |
brokenState->setObjectName("brokenState"); |
|
709 |
machine.addState(brokenState); |
|
710 |
||
711 |
QState *childState = new QState(brokenState); |
|
712 |
childState->setObjectName("childState"); |
|
713 |
||
714 |
initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); |
|
715 |
||
716 |
machine.start(); |
|
717 |
QCoreApplication::processEvents(); |
|
718 |
||
719 |
machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); |
|
720 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'brokenState'"); |
|
721 |
QCoreApplication::processEvents(); |
|
722 |
||
723 |
QCOMPARE(machine.isRunning(), false); |
|
724 |
} |
|
725 |
||
726 |
void tst_QStateMachine::errorStateEntersParentFirst() |
|
727 |
{ |
|
728 |
QStateMachine machine; |
|
729 |
||
730 |
QObject *entryController = new QObject(&machine); |
|
731 |
entryController->setObjectName("entryController"); |
|
732 |
entryController->setProperty("greatGrandParentEntered", false); |
|
733 |
entryController->setProperty("grandParentEntered", false); |
|
734 |
entryController->setProperty("parentEntered", false); |
|
735 |
entryController->setProperty("errorStateEntered", false); |
|
736 |
||
737 |
QState *greatGrandParent = new QState(); |
|
738 |
greatGrandParent->setObjectName("greatGrandParent"); |
|
739 |
greatGrandParent->assignProperty(entryController, "greatGrandParentEntered", true); |
|
740 |
machine.addState(greatGrandParent); |
|
741 |
machine.setInitialState(greatGrandParent); |
|
742 |
||
743 |
QState *grandParent = new QState(greatGrandParent); |
|
744 |
grandParent->setObjectName("grandParent"); |
|
745 |
grandParent->assignProperty(entryController, "grandParentEntered", true); |
|
746 |
||
747 |
QState *parent = new QState(grandParent); |
|
748 |
parent->setObjectName("parent"); |
|
749 |
parent->assignProperty(entryController, "parentEntered", true); |
|
750 |
||
751 |
QState *errorState = new QState(parent); |
|
752 |
errorState->setObjectName("errorState"); |
|
753 |
errorState->assignProperty(entryController, "errorStateEntered", true); |
|
754 |
machine.setErrorState(errorState); |
|
755 |
||
756 |
QState *initialStateOfGreatGrandParent = new QState(greatGrandParent); |
|
757 |
initialStateOfGreatGrandParent->setObjectName("initialStateOfGreatGrandParent"); |
|
758 |
greatGrandParent->setInitialState(initialStateOfGreatGrandParent); |
|
759 |
||
760 |
QState *brokenState = new QState(greatGrandParent); |
|
761 |
brokenState->setObjectName("brokenState"); |
|
762 |
||
763 |
QState *childState = new QState(brokenState); |
|
764 |
childState->setObjectName("childState"); |
|
765 |
||
766 |
initialStateOfGreatGrandParent->addTransition(new EventTransition(QEvent::User, brokenState)); |
|
767 |
||
768 |
machine.start(); |
|
769 |
QCoreApplication::processEvents(); |
|
770 |
||
771 |
QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), true); |
|
772 |
QCOMPARE(entryController->property("grandParentEntered").toBool(), false); |
|
773 |
QCOMPARE(entryController->property("parentEntered").toBool(), false); |
|
774 |
QCOMPARE(entryController->property("errorStateEntered").toBool(), false); |
|
775 |
QCOMPARE(machine.configuration().count(), 2); |
|
776 |
QVERIFY(machine.configuration().contains(greatGrandParent)); |
|
777 |
QVERIFY(machine.configuration().contains(initialStateOfGreatGrandParent)); |
|
778 |
||
779 |
entryController->setProperty("greatGrandParentEntered", false); |
|
780 |
entryController->setProperty("grandParentEntered", false); |
|
781 |
entryController->setProperty("parentEntered", false); |
|
782 |
entryController->setProperty("errorStateEntered", false); |
|
783 |
||
784 |
machine.postEvent(new QEvent(QEvent::User)); |
|
785 |
QCoreApplication::processEvents(); |
|
786 |
||
787 |
QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), false); |
|
788 |
QCOMPARE(entryController->property("grandParentEntered").toBool(), true); |
|
789 |
QCOMPARE(entryController->property("parentEntered").toBool(), true); |
|
790 |
QCOMPARE(entryController->property("errorStateEntered").toBool(), true); |
|
791 |
QCOMPARE(machine.configuration().count(), 4); |
|
792 |
QVERIFY(machine.configuration().contains(greatGrandParent)); |
|
793 |
QVERIFY(machine.configuration().contains(grandParent)); |
|
794 |
QVERIFY(machine.configuration().contains(parent)); |
|
795 |
QVERIFY(machine.configuration().contains(errorState)); |
|
796 |
} |
|
797 |
||
798 |
void tst_QStateMachine::customErrorStateIsNull() |
|
799 |
{ |
|
800 |
QStateMachine machine; |
|
801 |
machine.setErrorState(0); |
|
802 |
||
803 |
QState *initialState = new QState(); |
|
804 |
machine.addState(initialState); |
|
805 |
machine.setInitialState(initialState); |
|
806 |
||
807 |
QState *brokenState = new QState(); |
|
808 |
machine.addState(brokenState); |
|
809 |
||
810 |
new QState(brokenState); |
|
811 |
initialState->addTransition(new EventTransition(QEvent::User, brokenState)); |
|
812 |
||
813 |
machine.start(); |
|
814 |
QCoreApplication::processEvents(); |
|
815 |
||
816 |
machine.postEvent(new QEvent(QEvent::User)); |
|
817 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state ''"); |
|
818 |
QCoreApplication::processEvents(); |
|
819 |
||
820 |
QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); |
|
821 |
QCOMPARE(machine.isRunning(), false); |
|
822 |
} |
|
823 |
||
824 |
void tst_QStateMachine::clearError() |
|
825 |
{ |
|
826 |
QStateMachine machine; |
|
827 |
machine.setErrorState(new QState(&machine)); // avoid warnings |
|
828 |
||
829 |
QState *brokenState = new QState(&machine); |
|
830 |
brokenState->setObjectName("brokenState"); |
|
831 |
machine.setInitialState(brokenState); |
|
832 |
new QState(brokenState); |
|
833 |
||
834 |
machine.start(); |
|
835 |
QCoreApplication::processEvents(); |
|
836 |
||
837 |
QCOMPARE(machine.isRunning(), true); |
|
838 |
QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); |
|
839 |
QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'brokenState'")); |
|
840 |
||
841 |
machine.clearError(); |
|
842 |
||
843 |
QCOMPARE(machine.error(), QStateMachine::NoError); |
|
844 |
QVERIFY(machine.errorString().isEmpty()); |
|
845 |
} |
|
846 |
||
847 |
void tst_QStateMachine::historyStateAsInitialState() |
|
848 |
{ |
|
849 |
QStateMachine machine; |
|
850 |
||
851 |
QHistoryState *hs = new QHistoryState(&machine); |
|
852 |
machine.setInitialState(hs); |
|
853 |
||
854 |
QState *s1 = new QState(&machine); |
|
855 |
hs->setDefaultState(s1); |
|
856 |
||
857 |
QState *s2 = new QState(&machine); |
|
858 |
||
859 |
QHistoryState *s2h = new QHistoryState(s2); |
|
860 |
s2->setInitialState(s2h); |
|
861 |
||
862 |
QState *s21 = new QState(s2); |
|
863 |
s2h->setDefaultState(s21); |
|
864 |
||
865 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
866 |
||
867 |
machine.start(); |
|
868 |
QCoreApplication::processEvents(); |
|
869 |
||
870 |
QCOMPARE(machine.configuration().size(), 1); |
|
871 |
QVERIFY(machine.configuration().contains(s1)); |
|
872 |
||
873 |
machine.postEvent(new QEvent(QEvent::User)); |
|
874 |
QCoreApplication::processEvents(); |
|
875 |
||
876 |
QCOMPARE(machine.configuration().size(), 2); |
|
877 |
QVERIFY(machine.configuration().contains(s2)); |
|
878 |
QVERIFY(machine.configuration().contains(s21)); |
|
879 |
} |
|
880 |
||
881 |
void tst_QStateMachine::historyStateHasNowhereToGo() |
|
882 |
{ |
|
883 |
QStateMachine machine; |
|
884 |
||
885 |
QState *initialState = new QState(&machine); |
|
886 |
machine.setInitialState(initialState); |
|
887 |
machine.setErrorState(new QState(&machine)); // avoid warnings |
|
888 |
||
889 |
QState *brokenState = new QState(&machine); |
|
890 |
brokenState->setObjectName("brokenState"); |
|
891 |
brokenState->setInitialState(new QState(brokenState)); |
|
892 |
||
893 |
QHistoryState *historyState = new QHistoryState(brokenState); |
|
894 |
historyState->setObjectName("historyState"); |
|
895 |
initialState->addTransition(new EventTransition(QEvent::User, historyState)); |
|
896 |
||
897 |
machine.start(); |
|
898 |
QCoreApplication::processEvents(); |
|
899 |
||
900 |
machine.postEvent(new QEvent(QEvent::User)); |
|
901 |
QCoreApplication::processEvents(); |
|
902 |
||
903 |
QCOMPARE(machine.isRunning(), true); |
|
904 |
QCOMPARE(machine.configuration().count(), 1); |
|
905 |
QVERIFY(machine.configuration().contains(machine.errorState())); |
|
906 |
QCOMPARE(machine.error(), QStateMachine::NoDefaultStateInHistoryStateError); |
|
907 |
QCOMPARE(machine.errorString(), QString::fromLatin1("Missing default state in history state 'historyState'")); |
|
908 |
} |
|
909 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
910 |
void tst_QStateMachine::historyStateAfterRestart() |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
911 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
912 |
// QTBUG-8842 |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
913 |
QStateMachine machine; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
914 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
915 |
QState *s1 = new QState(&machine); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
916 |
machine.setInitialState(s1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
917 |
QState *s2 = new QState(&machine); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
918 |
QState *s21 = new QState(s2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
919 |
QState *s22 = new QState(s2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
920 |
QHistoryState *s2h = new QHistoryState(s2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
921 |
s2h->setDefaultState(s21); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
922 |
s1->addTransition(new EventTransition(QEvent::User, s2h)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
923 |
s21->addTransition(new EventTransition(QEvent::User, s22)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
924 |
s2->addTransition(new EventTransition(QEvent::User, s1)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
925 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
926 |
for (int x = 0; x < 2; ++x) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
927 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
928 |
machine.start(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
929 |
QTRY_COMPARE(startedSpy.count(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
930 |
QCOMPARE(machine.configuration().count(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
931 |
QVERIFY(machine.configuration().contains(s1)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
932 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
933 |
// s1 -> s2h -> s21 (default state) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
934 |
machine.postEvent(new QEvent(QEvent::User)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
935 |
QCoreApplication::processEvents(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
936 |
QCOMPARE(machine.configuration().count(), 2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
937 |
QVERIFY(machine.configuration().contains(s2)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
938 |
// This used to fail on the 2nd run because the |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
939 |
// history had not been cleared. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
940 |
QVERIFY(machine.configuration().contains(s21)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
941 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
942 |
// s21 -> s22 |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
943 |
machine.postEvent(new QEvent(QEvent::User)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
944 |
QCoreApplication::processEvents(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
945 |
QCOMPARE(machine.configuration().count(), 2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
946 |
QVERIFY(machine.configuration().contains(s2)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
947 |
QVERIFY(machine.configuration().contains(s22)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
948 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
949 |
// s2 -> s1 (s22 saved in s2h) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
950 |
machine.postEvent(new QEvent(QEvent::User)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
951 |
QCoreApplication::processEvents(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
952 |
QCOMPARE(machine.configuration().count(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
953 |
QVERIFY(machine.configuration().contains(s1)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
954 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
955 |
// s1 -> s2h -> s22 (saved state) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
956 |
machine.postEvent(new QEvent(QEvent::User)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
957 |
QCoreApplication::processEvents(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
958 |
QCOMPARE(machine.configuration().count(), 2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
959 |
QVERIFY(machine.configuration().contains(s2)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
960 |
QVERIFY(machine.configuration().contains(s22)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
961 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
962 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
963 |
machine.stop(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
964 |
QTRY_COMPARE(stoppedSpy.count(), 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
965 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
966 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
967 |
|
0 | 968 |
void tst_QStateMachine::brokenStateIsNeverEntered() |
969 |
{ |
|
970 |
QStateMachine machine; |
|
971 |
||
972 |
QObject *entryController = new QObject(&machine); |
|
973 |
entryController->setProperty("brokenStateEntered", false); |
|
974 |
entryController->setProperty("childStateEntered", false); |
|
975 |
entryController->setProperty("errorStateEntered", false); |
|
976 |
||
977 |
QState *initialState = new QState(&machine); |
|
978 |
machine.setInitialState(initialState); |
|
979 |
||
980 |
QState *errorState = new QState(&machine); |
|
981 |
errorState->assignProperty(entryController, "errorStateEntered", true); |
|
982 |
machine.setErrorState(errorState); |
|
983 |
||
984 |
QState *brokenState = new QState(&machine); |
|
985 |
brokenState->assignProperty(entryController, "brokenStateEntered", true); |
|
986 |
brokenState->setObjectName("brokenState"); |
|
987 |
||
988 |
QState *childState = new QState(brokenState); |
|
989 |
childState->assignProperty(entryController, "childStateEntered", true); |
|
990 |
||
991 |
initialState->addTransition(new EventTransition(QEvent::User, brokenState)); |
|
992 |
||
993 |
machine.start(); |
|
994 |
QCoreApplication::processEvents(); |
|
995 |
||
996 |
machine.postEvent(new QEvent(QEvent::User)); |
|
997 |
QCoreApplication::processEvents(); |
|
998 |
||
999 |
QCOMPARE(entryController->property("errorStateEntered").toBool(), true); |
|
1000 |
QCOMPARE(entryController->property("brokenStateEntered").toBool(), false); |
|
1001 |
QCOMPARE(entryController->property("childStateEntered").toBool(), false); |
|
1002 |
} |
|
1003 |
||
1004 |
void tst_QStateMachine::transitionToStateNotInGraph() |
|
1005 |
{ |
|
1006 |
QStateMachine machine; |
|
1007 |
||
1008 |
QState *initialState = new QState(&machine); |
|
1009 |
initialState->setObjectName("initialState"); |
|
1010 |
machine.setInitialState(initialState); |
|
1011 |
||
1012 |
QState independentState; |
|
1013 |
independentState.setObjectName("independentState"); |
|
1014 |
initialState->addTransition(&independentState); |
|
1015 |
||
1016 |
machine.start(); |
|
1017 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 'initialState'"); |
|
1018 |
QCoreApplication::processEvents(); |
|
1019 |
||
1020 |
QCOMPARE(machine.isRunning(), false); |
|
1021 |
} |
|
1022 |
||
1023 |
void tst_QStateMachine::customErrorStateNotInGraph() |
|
1024 |
{ |
|
1025 |
QStateMachine machine; |
|
1026 |
||
1027 |
QState errorState; |
|
1028 |
errorState.setObjectName("errorState"); |
|
1029 |
QTest::ignoreMessage(QtWarningMsg, "QState::setErrorState: error state cannot belong to a different state machine"); |
|
1030 |
machine.setErrorState(&errorState); |
|
1031 |
QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); |
|
1032 |
||
1033 |
QState *initialBrokenState = new QState(&machine); |
|
1034 |
initialBrokenState->setObjectName("initialBrokenState"); |
|
1035 |
machine.setInitialState(initialBrokenState); |
|
1036 |
new QState(initialBrokenState); |
|
1037 |
||
1038 |
machine.start(); |
|
1039 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'initialBrokenState'"); |
|
1040 |
QCoreApplication::processEvents(); |
|
1041 |
||
1042 |
QCOMPARE(machine.isRunning(), false); |
|
1043 |
} |
|
1044 |
||
1045 |
void tst_QStateMachine::restoreProperties() |
|
1046 |
{ |
|
1047 |
QStateMachine machine; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1048 |
QCOMPARE(machine.globalRestorePolicy(), QStateMachine::DontRestoreProperties); |
0 | 1049 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
1050 |
||
1051 |
QObject *object = new QObject(&machine); |
|
1052 |
object->setProperty("a", 1); |
|
1053 |
object->setProperty("b", 2); |
|
1054 |
||
1055 |
QState *S1 = new QState(); |
|
1056 |
S1->setObjectName("S1"); |
|
1057 |
S1->assignProperty(object, "a", 3); |
|
1058 |
machine.addState(S1); |
|
1059 |
||
1060 |
QState *S2 = new QState(); |
|
1061 |
S2->setObjectName("S2"); |
|
1062 |
S2->assignProperty(object, "b", 5); |
|
1063 |
machine.addState(S2); |
|
1064 |
||
1065 |
QState *S3 = new QState(); |
|
1066 |
S3->setObjectName("S3"); |
|
1067 |
machine.addState(S3); |
|
1068 |
||
1069 |
QFinalState *S4 = new QFinalState(); |
|
1070 |
machine.addState(S4); |
|
1071 |
||
1072 |
S1->addTransition(new EventTransition(QEvent::User, S2)); |
|
1073 |
S2->addTransition(new EventTransition(QEvent::User, S3)); |
|
1074 |
S3->addTransition(S4); |
|
1075 |
||
1076 |
machine.setInitialState(S1); |
|
1077 |
machine.start(); |
|
1078 |
QCoreApplication::processEvents(); |
|
1079 |
||
1080 |
QCOMPARE(object->property("a").toInt(), 3); |
|
1081 |
QCOMPARE(object->property("b").toInt(), 2); |
|
1082 |
||
1083 |
machine.postEvent(new QEvent(QEvent::User)); |
|
1084 |
QCoreApplication::processEvents(); |
|
1085 |
||
1086 |
QCOMPARE(object->property("a").toInt(), 1); |
|
1087 |
QCOMPARE(object->property("b").toInt(), 5); |
|
1088 |
||
1089 |
machine.postEvent(new QEvent(QEvent::User)); |
|
1090 |
QCoreApplication::processEvents(); |
|
1091 |
||
1092 |
QCOMPARE(object->property("a").toInt(), 1); |
|
1093 |
QCOMPARE(object->property("b").toInt(), 2); |
|
1094 |
} |
|
1095 |
||
1096 |
void tst_QStateMachine::rootState() |
|
1097 |
{ |
|
1098 |
QStateMachine machine; |
|
1099 |
QCOMPARE(qobject_cast<QState*>(machine.parentState()), (QState*)0); |
|
1100 |
QCOMPARE(machine.machine(), (QStateMachine*)0); |
|
1101 |
||
1102 |
QState *s1 = new QState(&machine); |
|
1103 |
QCOMPARE(s1->parentState(), static_cast<QState*>(&machine)); |
|
1104 |
||
1105 |
QState *s2 = new QState(); |
|
1106 |
s2->setParent(&machine); |
|
1107 |
QCOMPARE(s2->parentState(), static_cast<QState*>(&machine)); |
|
1108 |
} |
|
1109 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1110 |
void tst_QStateMachine::machineWithParent() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1111 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1112 |
QObject object; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1113 |
QStateMachine *machine = new QStateMachine(&object); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1114 |
QCOMPARE(machine->parent(), &object); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1115 |
QCOMPARE(machine->parentState(), (QObject*)0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1116 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1117 |
|
0 | 1118 |
void tst_QStateMachine::addAndRemoveState() |
1119 |
{ |
|
1120 |
#ifdef QT_BUILD_INTERNAL |
|
1121 |
QStateMachine machine; |
|
1122 |
QStatePrivate *root_d = QStatePrivate::get(&machine); |
|
1123 |
QCOMPARE(root_d->childStates().size(), 0); |
|
1124 |
||
1125 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: cannot add null state"); |
|
1126 |
machine.addState(0); |
|
1127 |
||
1128 |
QState *s1 = new QState(); |
|
1129 |
QCOMPARE(s1->parentState(), (QState*)0); |
|
1130 |
QCOMPARE(s1->machine(), (QStateMachine*)0); |
|
1131 |
machine.addState(s1); |
|
1132 |
QCOMPARE(s1->machine(), static_cast<QStateMachine*>(&machine)); |
|
1133 |
QCOMPARE(s1->parentState(), static_cast<QState*>(&machine)); |
|
1134 |
QCOMPARE(root_d->childStates().size(), 1); |
|
1135 |
QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); |
|
1136 |
||
1137 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: state has already been added to this machine"); |
|
1138 |
machine.addState(s1); |
|
1139 |
||
1140 |
QState *s2 = new QState(); |
|
1141 |
QCOMPARE(s2->parentState(), (QState*)0); |
|
1142 |
machine.addState(s2); |
|
1143 |
QCOMPARE(s2->parentState(), static_cast<QState*>(&machine)); |
|
1144 |
QCOMPARE(root_d->childStates().size(), 2); |
|
1145 |
QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); |
|
1146 |
QCOMPARE(root_d->childStates().at(1), (QAbstractState*)s2); |
|
1147 |
||
1148 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: state has already been added to this machine"); |
|
1149 |
machine.addState(s2); |
|
1150 |
||
1151 |
machine.removeState(s1); |
|
1152 |
QCOMPARE(s1->parentState(), (QState*)0); |
|
1153 |
QCOMPARE(root_d->childStates().size(), 1); |
|
1154 |
QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s2); |
|
1155 |
||
1156 |
machine.removeState(s2); |
|
1157 |
QCOMPARE(s2->parentState(), (QState*)0); |
|
1158 |
QCOMPARE(root_d->childStates().size(), 0); |
|
1159 |
||
1160 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::removeState: cannot remove null state"); |
|
1161 |
machine.removeState(0); |
|
1162 |
||
1163 |
{ |
|
1164 |
QStateMachine machine2; |
|
1165 |
{ |
|
1166 |
QString warning; |
|
1167 |
warning.sprintf("QStateMachine::removeState: state %p's machine (%p) is different from this machine (%p)", |
|
1168 |
&machine2, (void*)0, &machine); |
|
1169 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); |
|
1170 |
machine.removeState(&machine2); |
|
1171 |
} |
|
1172 |
// ### check this behavior |
|
1173 |
machine.addState(&machine2); |
|
1174 |
QCOMPARE(machine2.parent(), (QObject*)&machine); |
|
1175 |
} |
|
1176 |
||
1177 |
delete s1; |
|
1178 |
delete s2; |
|
1179 |
// ### how to deal with this? |
|
1180 |
// machine.removeState(machine.errorState()); |
|
1181 |
#endif |
|
1182 |
} |
|
1183 |
||
1184 |
void tst_QStateMachine::stateEntryAndExit() |
|
1185 |
{ |
|
1186 |
// Two top-level states |
|
1187 |
{ |
|
1188 |
QStateMachine machine; |
|
1189 |
||
1190 |
TestState *s1 = new TestState(&machine); |
|
1191 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); |
|
1192 |
s1->addTransition((QAbstractState*)0); |
|
1193 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add null transition"); |
|
1194 |
s1->addTransition((QAbstractTransition*)0); |
|
1195 |
QTest::ignoreMessage(QtWarningMsg, "QState::removeTransition: cannot remove null transition"); |
|
1196 |
s1->removeTransition((QAbstractTransition*)0); |
|
1197 |
||
1198 |
TestState *s2 = new TestState(&machine); |
|
1199 |
QFinalState *s3 = new QFinalState(&machine); |
|
1200 |
||
1201 |
TestTransition *t = new TestTransition(s2); |
|
1202 |
QCOMPARE(t->machine(), (QStateMachine*)0); |
|
1203 |
QCOMPARE(t->sourceState(), (QState*)0); |
|
1204 |
QCOMPARE(t->targetState(), (QAbstractState*)s2); |
|
1205 |
QCOMPARE(t->targetStates().size(), 1); |
|
1206 |
QCOMPARE(t->targetStates().at(0), (QAbstractState*)s2); |
|
1207 |
t->setTargetState(0); |
|
1208 |
QCOMPARE(t->targetState(), (QAbstractState*)0); |
|
1209 |
QVERIFY(t->targetStates().isEmpty()); |
|
1210 |
t->setTargetState(s2); |
|
1211 |
QCOMPARE(t->targetState(), (QAbstractState*)s2); |
|
1212 |
QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::setTargetStates: target state(s) cannot be null"); |
|
1213 |
t->setTargetStates(QList<QAbstractState*>() << 0); |
|
1214 |
QCOMPARE(t->targetState(), (QAbstractState*)s2); |
|
1215 |
t->setTargetStates(QList<QAbstractState*>() << s2); |
|
1216 |
QCOMPARE(t->targetState(), (QAbstractState*)s2); |
|
1217 |
QCOMPARE(t->targetStates().size(), 1); |
|
1218 |
QCOMPARE(t->targetStates().at(0), (QAbstractState*)s2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1219 |
s1->addTransition(t); |
0 | 1220 |
QCOMPARE(t->sourceState(), (QState*)s1); |
1221 |
QCOMPARE(t->machine(), &machine); |
|
1222 |
||
1223 |
{ |
|
1224 |
QAbstractTransition *trans = s2->addTransition(s3); |
|
1225 |
QVERIFY(trans != 0); |
|
1226 |
QCOMPARE(trans->sourceState(), (QState*)s2); |
|
1227 |
QCOMPARE(trans->targetState(), (QAbstractState*)s3); |
|
1228 |
{ |
|
1229 |
QString warning; |
|
1230 |
warning.sprintf("QState::removeTransition: transition %p's source state (%p) is different from this state (%p)", trans, s2, s1); |
|
1231 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); |
|
1232 |
s1->removeTransition(trans); |
|
1233 |
} |
|
1234 |
s2->removeTransition(trans); |
|
1235 |
QCOMPARE(trans->sourceState(), (QState*)0); |
|
1236 |
QCOMPARE(trans->targetState(), (QAbstractState*)s3); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1237 |
s2->addTransition(trans); |
0 | 1238 |
QCOMPARE(trans->sourceState(), (QState*)s2); |
1239 |
} |
|
1240 |
||
1241 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
1242 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
|
1243 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1244 |
machine.setInitialState(s1); |
|
1245 |
QCOMPARE(machine.initialState(), (QAbstractState*)s1); |
|
1246 |
{ |
|
1247 |
QString warning; |
|
1248 |
warning.sprintf("QState::setInitialState: state %p is not a child of this state (%p)", &machine, &machine); |
|
1249 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); |
|
1250 |
machine.setInitialState(&machine); |
|
1251 |
QCOMPARE(machine.initialState(), (QAbstractState*)s1); |
|
1252 |
} |
|
1253 |
QVERIFY(machine.configuration().isEmpty()); |
|
1254 |
globalTick = 0; |
|
1255 |
QVERIFY(!machine.isRunning()); |
|
1256 |
QSignalSpy s1EnteredSpy(s1, SIGNAL(entered())); |
|
1257 |
QSignalSpy s1ExitedSpy(s1, SIGNAL(exited())); |
|
1258 |
QSignalSpy tTriggeredSpy(t, SIGNAL(triggered())); |
|
1259 |
QSignalSpy s2EnteredSpy(s2, SIGNAL(entered())); |
|
1260 |
QSignalSpy s2ExitedSpy(s2, SIGNAL(exited())); |
|
1261 |
machine.start(); |
|
1262 |
||
1263 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
1264 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1265 |
QTRY_COMPARE(stoppedSpy.count(), 0); |
|
1266 |
QCOMPARE(machine.configuration().count(), 1); |
|
1267 |
QVERIFY(machine.configuration().contains(s3)); |
|
1268 |
||
1269 |
// s1 is entered |
|
1270 |
QCOMPARE(s1->events.count(), 2); |
|
1271 |
QCOMPARE(s1->events.at(0).first, 0); |
|
1272 |
QCOMPARE(s1->events.at(0).second, TestState::Entry); |
|
1273 |
// s1 is exited |
|
1274 |
QCOMPARE(s1->events.at(1).first, 1); |
|
1275 |
QCOMPARE(s1->events.at(1).second, TestState::Exit); |
|
1276 |
// t is triggered |
|
1277 |
QCOMPARE(t->triggers.count(), 1); |
|
1278 |
QCOMPARE(t->triggers.at(0), 2); |
|
1279 |
// s2 is entered |
|
1280 |
QCOMPARE(s2->events.count(), 2); |
|
1281 |
QCOMPARE(s2->events.at(0).first, 3); |
|
1282 |
QCOMPARE(s2->events.at(0).second, TestState::Entry); |
|
1283 |
// s2 is exited |
|
1284 |
QCOMPARE(s2->events.at(1).first, 4); |
|
1285 |
QCOMPARE(s2->events.at(1).second, TestState::Exit); |
|
1286 |
||
1287 |
QCOMPARE(s1EnteredSpy.count(), 1); |
|
1288 |
QCOMPARE(s1ExitedSpy.count(), 1); |
|
1289 |
QCOMPARE(tTriggeredSpy.count(), 1); |
|
1290 |
QCOMPARE(s2EnteredSpy.count(), 1); |
|
1291 |
QCOMPARE(s2ExitedSpy.count(), 1); |
|
1292 |
} |
|
1293 |
// Two top-level states, one has two child states |
|
1294 |
{ |
|
1295 |
QStateMachine machine; |
|
1296 |
||
1297 |
TestState *s1 = new TestState(&machine); |
|
1298 |
TestState *s11 = new TestState(s1); |
|
1299 |
TestState *s12 = new TestState(s1); |
|
1300 |
TestState *s2 = new TestState(&machine); |
|
1301 |
QFinalState *s3 = new QFinalState(&machine); |
|
1302 |
s1->setInitialState(s11); |
|
1303 |
TestTransition *t1 = new TestTransition(s12); |
|
1304 |
s11->addTransition(t1); |
|
1305 |
TestTransition *t2 = new TestTransition(s2); |
|
1306 |
s12->addTransition(t2); |
|
1307 |
s2->addTransition(s3); |
|
1308 |
||
1309 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
1310 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1311 |
machine.setInitialState(s1); |
|
1312 |
globalTick = 0; |
|
1313 |
machine.start(); |
|
1314 |
||
1315 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
1316 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1317 |
QCOMPARE(machine.configuration().count(), 1); |
|
1318 |
QVERIFY(machine.configuration().contains(s3)); |
|
1319 |
||
1320 |
// s1 is entered |
|
1321 |
QCOMPARE(s1->events.count(), 2); |
|
1322 |
QCOMPARE(s1->events.at(0).first, 0); |
|
1323 |
QCOMPARE(s1->events.at(0).second, TestState::Entry); |
|
1324 |
// s11 is entered |
|
1325 |
QCOMPARE(s11->events.count(), 2); |
|
1326 |
QCOMPARE(s11->events.at(0).first, 1); |
|
1327 |
QCOMPARE(s11->events.at(0).second, TestState::Entry); |
|
1328 |
// s11 is exited |
|
1329 |
QCOMPARE(s11->events.at(1).first, 2); |
|
1330 |
QCOMPARE(s11->events.at(1).second, TestState::Exit); |
|
1331 |
// t1 is triggered |
|
1332 |
QCOMPARE(t1->triggers.count(), 1); |
|
1333 |
QCOMPARE(t1->triggers.at(0), 3); |
|
1334 |
// s12 is entered |
|
1335 |
QCOMPARE(s12->events.count(), 2); |
|
1336 |
QCOMPARE(s12->events.at(0).first, 4); |
|
1337 |
QCOMPARE(s12->events.at(0).second, TestState::Entry); |
|
1338 |
// s12 is exited |
|
1339 |
QCOMPARE(s12->events.at(1).first, 5); |
|
1340 |
QCOMPARE(s12->events.at(1).second, TestState::Exit); |
|
1341 |
// s1 is exited |
|
1342 |
QCOMPARE(s1->events.at(1).first, 6); |
|
1343 |
QCOMPARE(s1->events.at(1).second, TestState::Exit); |
|
1344 |
// t2 is triggered |
|
1345 |
QCOMPARE(t2->triggers.count(), 1); |
|
1346 |
QCOMPARE(t2->triggers.at(0), 7); |
|
1347 |
// s2 is entered |
|
1348 |
QCOMPARE(s2->events.count(), 2); |
|
1349 |
QCOMPARE(s2->events.at(0).first, 8); |
|
1350 |
QCOMPARE(s2->events.at(0).second, TestState::Entry); |
|
1351 |
// s2 is exited |
|
1352 |
QCOMPARE(s2->events.at(1).first, 9); |
|
1353 |
QCOMPARE(s2->events.at(1).second, TestState::Exit); |
|
1354 |
} |
|
1355 |
} |
|
1356 |
||
1357 |
void tst_QStateMachine::assignProperty() |
|
1358 |
{ |
|
1359 |
QStateMachine machine; |
|
1360 |
QState *s1 = new QState(&machine); |
|
1361 |
||
1362 |
QTest::ignoreMessage(QtWarningMsg, "QState::assignProperty: cannot assign property 'foo' of null object"); |
|
1363 |
s1->assignProperty(0, "foo", QVariant()); |
|
1364 |
||
1365 |
s1->assignProperty(s1, "objectName", "s1"); |
|
1366 |
QFinalState *s2 = new QFinalState(&machine); |
|
1367 |
s1->addTransition(s2); |
|
1368 |
machine.setInitialState(s1); |
|
1369 |
machine.start(); |
|
1370 |
QTRY_COMPARE(s1->objectName(), QString::fromLatin1("s1")); |
|
1371 |
||
1372 |
s1->assignProperty(s1, "objectName", "foo"); |
|
1373 |
machine.start(); |
|
1374 |
QTRY_COMPARE(s1->objectName(), QString::fromLatin1("foo")); |
|
1375 |
||
1376 |
s1->assignProperty(s1, "noSuchProperty", 123); |
|
1377 |
machine.start(); |
|
1378 |
QTRY_COMPARE(s1->dynamicPropertyNames().size(), 1); |
|
1379 |
QCOMPARE(s1->dynamicPropertyNames().at(0), QByteArray("noSuchProperty")); |
|
1380 |
QCOMPARE(s1->objectName(), QString::fromLatin1("foo")); |
|
1381 |
||
1382 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1383 |
QSignalSpy propertiesAssignedSpy(s1, SIGNAL(propertiesAssigned())); |
0 | 1384 |
machine.start(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1385 |
QTRY_COMPARE(propertiesAssignedSpy.count(), 1); |
0 | 1386 |
} |
1387 |
||
1388 |
// nested states |
|
1389 |
{ |
|
1390 |
QState *s11 = new QState(s1); |
|
1391 |
QString str = QString::fromLatin1("set by nested state"); |
|
1392 |
s11->assignProperty(s11, "objectName", str); |
|
1393 |
s1->setInitialState(s11); |
|
1394 |
machine.start(); |
|
1395 |
QTRY_COMPARE(s11->objectName(), str); |
|
1396 |
} |
|
1397 |
} |
|
1398 |
||
1399 |
void tst_QStateMachine::assignPropertyWithAnimation() |
|
1400 |
{ |
|
1401 |
// Single animation |
|
1402 |
{ |
|
1403 |
QStateMachine machine; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1404 |
QVERIFY(machine.isAnimated()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1405 |
machine.setAnimated(false); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1406 |
QVERIFY(!machine.isAnimated()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1407 |
machine.setAnimated(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1408 |
QVERIFY(machine.isAnimated()); |
0 | 1409 |
QObject obj; |
1410 |
obj.setProperty("foo", 321); |
|
1411 |
obj.setProperty("bar", 654); |
|
1412 |
QState *s1 = new QState(&machine); |
|
1413 |
s1->assignProperty(&obj, "foo", 123); |
|
1414 |
QState *s2 = new QState(&machine); |
|
1415 |
s2->assignProperty(&obj, "foo", 456); |
|
1416 |
s2->assignProperty(&obj, "bar", 789); |
|
1417 |
QAbstractTransition *trans = s1->addTransition(s2); |
|
1418 |
QVERIFY(trans->animations().isEmpty()); |
|
1419 |
QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::addAnimation: cannot add null animation"); |
|
1420 |
trans->addAnimation(0); |
|
1421 |
QPropertyAnimation anim(&obj, "foo"); |
|
1422 |
anim.setDuration(250); |
|
1423 |
trans->addAnimation(&anim); |
|
1424 |
QCOMPARE(trans->animations().size(), 1); |
|
1425 |
QCOMPARE(trans->animations().at(0), (QAbstractAnimation*)&anim); |
|
1426 |
QCOMPARE(anim.parent(), (QObject*)0); |
|
1427 |
QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::removeAnimation: cannot remove null animation"); |
|
1428 |
trans->removeAnimation(0); |
|
1429 |
trans->removeAnimation(&anim); |
|
1430 |
QVERIFY(trans->animations().isEmpty()); |
|
1431 |
trans->addAnimation(&anim); |
|
1432 |
QCOMPARE(trans->animations().size(), 1); |
|
1433 |
QCOMPARE(trans->animations().at(0), (QAbstractAnimation*)&anim); |
|
1434 |
QFinalState *s3 = new QFinalState(&machine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1435 |
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); |
0 | 1436 |
|
1437 |
machine.setInitialState(s1); |
|
1438 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1439 |
machine.start(); |
|
1440 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1441 |
QCOMPARE(obj.property("foo").toInt(), 456); |
|
1442 |
QCOMPARE(obj.property("bar").toInt(), 789); |
|
1443 |
} |
|
1444 |
// Two animations |
|
1445 |
{ |
|
1446 |
QStateMachine machine; |
|
1447 |
QObject obj; |
|
1448 |
obj.setProperty("foo", 321); |
|
1449 |
obj.setProperty("bar", 654); |
|
1450 |
QState *s1 = new QState(&machine); |
|
1451 |
s1->assignProperty(&obj, "foo", 123); |
|
1452 |
QState *s2 = new QState(&machine); |
|
1453 |
s2->assignProperty(&obj, "foo", 456); |
|
1454 |
s2->assignProperty(&obj, "bar", 789); |
|
1455 |
QAbstractTransition *trans = s1->addTransition(s2); |
|
1456 |
QPropertyAnimation anim(&obj, "foo"); |
|
1457 |
anim.setDuration(150); |
|
1458 |
trans->addAnimation(&anim); |
|
1459 |
QPropertyAnimation anim2(&obj, "bar"); |
|
1460 |
anim2.setDuration(150); |
|
1461 |
trans->addAnimation(&anim2); |
|
1462 |
QFinalState *s3 = new QFinalState(&machine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1463 |
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); |
0 | 1464 |
|
1465 |
machine.setInitialState(s1); |
|
1466 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1467 |
machine.start(); |
|
1468 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1469 |
QCOMPARE(obj.property("foo").toInt(), 456); |
|
1470 |
QCOMPARE(obj.property("bar").toInt(), 789); |
|
1471 |
} |
|
1472 |
// Animation group |
|
1473 |
{ |
|
1474 |
QStateMachine machine; |
|
1475 |
QObject obj; |
|
1476 |
obj.setProperty("foo", 321); |
|
1477 |
obj.setProperty("bar", 654); |
|
1478 |
QState *s1 = new QState(&machine); |
|
1479 |
s1->assignProperty(&obj, "foo", 123); |
|
1480 |
s1->assignProperty(&obj, "bar", 321); |
|
1481 |
QState *s2 = new QState(&machine); |
|
1482 |
s2->assignProperty(&obj, "foo", 456); |
|
1483 |
s2->assignProperty(&obj, "bar", 654); |
|
1484 |
s2->assignProperty(&obj, "baz", 789); |
|
1485 |
QAbstractTransition *trans = s1->addTransition(s2); |
|
1486 |
QSequentialAnimationGroup group; |
|
1487 |
group.addAnimation(new QPropertyAnimation(&obj, "foo")); |
|
1488 |
group.addAnimation(new QPropertyAnimation(&obj, "bar")); |
|
1489 |
trans->addAnimation(&group); |
|
1490 |
QFinalState *s3 = new QFinalState(&machine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1491 |
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); |
0 | 1492 |
|
1493 |
machine.setInitialState(s1); |
|
1494 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1495 |
machine.start(); |
|
1496 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1497 |
QCOMPARE(obj.property("foo").toInt(), 456); |
|
1498 |
QCOMPARE(obj.property("bar").toInt(), 654); |
|
1499 |
QCOMPARE(obj.property("baz").toInt(), 789); |
|
1500 |
} |
|
1501 |
// Nested states |
|
1502 |
{ |
|
1503 |
QStateMachine machine; |
|
1504 |
QObject obj; |
|
1505 |
obj.setProperty("foo", 321); |
|
1506 |
obj.setProperty("bar", 654); |
|
1507 |
QState *s1 = new QState(&machine); |
|
1508 |
QCOMPARE(s1->childMode(), QState::ExclusiveStates); |
|
1509 |
s1->setChildMode(QState::ParallelStates); |
|
1510 |
QCOMPARE(s1->childMode(), QState::ParallelStates); |
|
1511 |
s1->setChildMode(QState::ExclusiveStates); |
|
1512 |
QCOMPARE(s1->childMode(), QState::ExclusiveStates); |
|
1513 |
QCOMPARE(s1->initialState(), (QAbstractState*)0); |
|
1514 |
s1->setObjectName("s1"); |
|
1515 |
s1->assignProperty(&obj, "foo", 123); |
|
1516 |
s1->assignProperty(&obj, "bar", 456); |
|
1517 |
QState *s2 = new QState(&machine); |
|
1518 |
s2->setObjectName("s2"); |
|
1519 |
s2->assignProperty(&obj, "foo", 321); |
|
1520 |
QState *s21 = new QState(s2); |
|
1521 |
s21->setObjectName("s21"); |
|
1522 |
s21->assignProperty(&obj, "bar", 654); |
|
1523 |
QState *s22 = new QState(s2); |
|
1524 |
s22->setObjectName("s22"); |
|
1525 |
s22->assignProperty(&obj, "bar", 789); |
|
1526 |
s2->setInitialState(s21); |
|
1527 |
QCOMPARE(s2->initialState(), (QAbstractState*)s21); |
|
1528 |
||
1529 |
QAbstractTransition *trans = s1->addTransition(s2); |
|
1530 |
QPropertyAnimation anim(&obj, "foo"); |
|
1531 |
anim.setDuration(500); |
|
1532 |
trans->addAnimation(&anim); |
|
1533 |
QPropertyAnimation anim2(&obj, "bar"); |
|
1534 |
anim2.setDuration(250); |
|
1535 |
trans->addAnimation(&anim2); |
|
1536 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1537 |
s21->addTransition(s21, SIGNAL(propertiesAssigned()), s22); |
0 | 1538 |
|
1539 |
QFinalState *s3 = new QFinalState(&machine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1540 |
s22->addTransition(s2, SIGNAL(propertiesAssigned()), s3); |
0 | 1541 |
|
1542 |
machine.setInitialState(s1); |
|
1543 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1544 |
machine.start(); |
|
1545 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1546 |
QCOMPARE(obj.property("foo").toInt(), 321); |
|
1547 |
QCOMPARE(obj.property("bar").toInt(), 789); |
|
1548 |
} |
|
1549 |
// Aborted animation |
|
1550 |
{ |
|
1551 |
QStateMachine machine; |
|
1552 |
SignalEmitter emitter; |
|
1553 |
QObject obj; |
|
1554 |
obj.setProperty("foo", 321); |
|
1555 |
obj.setProperty("bar", 654); |
|
1556 |
QState *group = new QState(&machine); |
|
1557 |
QState *s1 = new QState(group); |
|
1558 |
group->setInitialState(s1); |
|
1559 |
s1->assignProperty(&obj, "foo", 123); |
|
1560 |
QState *s2 = new QState(group); |
|
1561 |
s2->assignProperty(&obj, "foo", 456); |
|
1562 |
s2->assignProperty(&obj, "bar", 789); |
|
1563 |
QAbstractTransition *trans = s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s2); |
|
1564 |
QPropertyAnimation anim(&obj, "foo"); |
|
1565 |
anim.setDuration(8000); |
|
1566 |
trans->addAnimation(&anim); |
|
1567 |
QPropertyAnimation anim2(&obj, "bar"); |
|
1568 |
anim2.setDuration(8000); |
|
1569 |
trans->addAnimation(&anim2); |
|
1570 |
QState *s3 = new QState(group); |
|
1571 |
s3->assignProperty(&obj, "foo", 911); |
|
1572 |
s2->addTransition(&emitter, SIGNAL(signalWithNoArg()), s3); |
|
1573 |
||
1574 |
machine.setInitialState(group); |
|
1575 |
machine.start(); |
|
1576 |
QTRY_COMPARE(machine.configuration().contains(s1), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1577 |
QSignalSpy propertiesAssignedSpy(s2, SIGNAL(propertiesAssigned())); |
0 | 1578 |
emitter.emitSignalWithNoArg(); |
1579 |
QTRY_COMPARE(machine.configuration().contains(s2), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1580 |
QVERIFY(propertiesAssignedSpy.isEmpty()); |
0 | 1581 |
emitter.emitSignalWithNoArg(); // will cause animations from s1-->s2 to abort |
1582 |
QTRY_COMPARE(machine.configuration().contains(s3), true); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1583 |
QVERIFY(propertiesAssignedSpy.isEmpty()); |
0 | 1584 |
QCOMPARE(obj.property("foo").toInt(), 911); |
1585 |
QCOMPARE(obj.property("bar").toInt(), 789); |
|
1586 |
} |
|
1587 |
} |
|
1588 |
||
1589 |
struct StringEvent : public QEvent |
|
1590 |
{ |
|
1591 |
public: |
|
1592 |
StringEvent(const QString &val) |
|
1593 |
: QEvent(QEvent::Type(QEvent::User+2)), |
|
1594 |
value(val) {} |
|
1595 |
||
1596 |
QString value; |
|
1597 |
}; |
|
1598 |
||
1599 |
class StringTransition : public QAbstractTransition |
|
1600 |
{ |
|
1601 |
public: |
|
1602 |
StringTransition(const QString &value, QAbstractState *target) |
|
1603 |
: QAbstractTransition(), m_value(value) |
|
1604 |
{ setTargetState(target); } |
|
1605 |
||
1606 |
protected: |
|
1607 |
virtual bool eventTest(QEvent *e) |
|
1608 |
{ |
|
1609 |
if (e->type() != QEvent::Type(QEvent::User+2)) |
|
1610 |
return false; |
|
1611 |
StringEvent *se = static_cast<StringEvent*>(e); |
|
1612 |
return (m_value == se->value) && (!m_cond.isValid() || (m_cond.indexIn(m_value) != -1)); |
|
1613 |
} |
|
1614 |
virtual void onTransition(QEvent *) {} |
|
1615 |
||
1616 |
private: |
|
1617 |
QString m_value; |
|
1618 |
QRegExp m_cond; |
|
1619 |
}; |
|
1620 |
||
1621 |
class StringEventPoster : public QState |
|
1622 |
{ |
|
1623 |
public: |
|
1624 |
StringEventPoster(const QString &value, QState *parent = 0) |
|
1625 |
: QState(parent), m_value(value), m_delay(-1) {} |
|
1626 |
||
1627 |
void setString(const QString &value) |
|
1628 |
{ m_value = value; } |
|
1629 |
void setDelay(int delay) |
|
1630 |
{ m_delay = delay; } |
|
1631 |
||
1632 |
protected: |
|
1633 |
virtual void onEntry(QEvent *) |
|
1634 |
{ |
|
1635 |
if (m_delay == -1) |
|
1636 |
machine()->postEvent(new StringEvent(m_value)); |
|
1637 |
else |
|
1638 |
machine()->postDelayedEvent(new StringEvent(m_value), m_delay); |
|
1639 |
} |
|
1640 |
virtual void onExit(QEvent *) {} |
|
1641 |
||
1642 |
private: |
|
1643 |
QString m_value; |
|
1644 |
int m_delay; |
|
1645 |
}; |
|
1646 |
||
1647 |
void tst_QStateMachine::postEvent() |
|
1648 |
{ |
|
1649 |
for (int x = 0; x < 2; ++x) { |
|
1650 |
QStateMachine machine; |
|
1651 |
{ |
|
1652 |
QEvent e(QEvent::None); |
|
1653 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::postEvent: cannot post event when the state machine is not running"); |
|
1654 |
machine.postEvent(&e); |
|
1655 |
} |
|
1656 |
StringEventPoster *s1 = new StringEventPoster("a"); |
|
1657 |
if (x == 1) |
|
1658 |
s1->setDelay(100); |
|
1659 |
QFinalState *s2 = new QFinalState; |
|
1660 |
s1->addTransition(new StringTransition("a", s2)); |
|
1661 |
machine.addState(s1); |
|
1662 |
machine.addState(s2); |
|
1663 |
machine.setInitialState(s1); |
|
1664 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1665 |
machine.start(); |
|
1666 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1667 |
QCOMPARE(machine.configuration().size(), 1); |
|
1668 |
QVERIFY(machine.configuration().contains(s2)); |
|
1669 |
||
1670 |
s1->setString("b"); |
|
1671 |
QFinalState *s3 = new QFinalState(); |
|
1672 |
machine.addState(s3); |
|
1673 |
s1->addTransition(new StringTransition("b", s3)); |
|
1674 |
finishedSpy.clear(); |
|
1675 |
machine.start(); |
|
1676 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1677 |
QCOMPARE(machine.configuration().size(), 1); |
|
1678 |
QVERIFY(machine.configuration().contains(s3)); |
|
1679 |
} |
|
1680 |
} |
|
1681 |
||
1682 |
void tst_QStateMachine::cancelDelayedEvent() |
|
1683 |
{ |
|
1684 |
QStateMachine machine; |
|
1685 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::cancelDelayedEvent: the machine is not running"); |
|
1686 |
QVERIFY(!machine.cancelDelayedEvent(-1)); |
|
1687 |
||
1688 |
QState *s1 = new QState(&machine); |
|
1689 |
QFinalState *s2 = new QFinalState(&machine); |
|
1690 |
s1->addTransition(new StringTransition("a", s2)); |
|
1691 |
machine.setInitialState(s1); |
|
1692 |
||
1693 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
1694 |
machine.start(); |
|
1695 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
1696 |
QCOMPARE(machine.configuration().size(), 1); |
|
1697 |
QVERIFY(machine.configuration().contains(s1)); |
|
1698 |
||
1699 |
int id1 = machine.postDelayedEvent(new StringEvent("c"), 50000); |
|
1700 |
QVERIFY(id1 != -1); |
|
1701 |
int id2 = machine.postDelayedEvent(new StringEvent("b"), 25000); |
|
1702 |
QVERIFY(id2 != -1); |
|
1703 |
QVERIFY(id2 != id1); |
|
1704 |
int id3 = machine.postDelayedEvent(new StringEvent("a"), 100); |
|
1705 |
QVERIFY(id3 != -1); |
|
1706 |
QVERIFY(id3 != id2); |
|
1707 |
QVERIFY(machine.cancelDelayedEvent(id1)); |
|
1708 |
QVERIFY(!machine.cancelDelayedEvent(id1)); |
|
1709 |
QVERIFY(machine.cancelDelayedEvent(id2)); |
|
1710 |
QVERIFY(!machine.cancelDelayedEvent(id2)); |
|
1711 |
||
1712 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1713 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1714 |
QCOMPARE(machine.configuration().size(), 1); |
|
1715 |
QVERIFY(machine.configuration().contains(s2)); |
|
1716 |
} |
|
1717 |
||
1718 |
void tst_QStateMachine::postDelayedEventAndStop() |
|
1719 |
{ |
|
1720 |
QStateMachine machine; |
|
1721 |
QState *s1 = new QState(&machine); |
|
1722 |
QFinalState *s2 = new QFinalState(&machine); |
|
1723 |
s1->addTransition(new StringTransition("a", s2)); |
|
1724 |
machine.setInitialState(s1); |
|
1725 |
||
1726 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
1727 |
machine.start(); |
|
1728 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
1729 |
QCOMPARE(machine.configuration().size(), 1); |
|
1730 |
QVERIFY(machine.configuration().contains(s1)); |
|
1731 |
||
1732 |
int id1 = machine.postDelayedEvent(new StringEvent("a"), 0); |
|
1733 |
QVERIFY(id1 != -1); |
|
1734 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
|
1735 |
machine.stop(); |
|
1736 |
QTRY_COMPARE(stoppedSpy.count(), 1); |
|
1737 |
QCOMPARE(machine.configuration().size(), 1); |
|
1738 |
QVERIFY(machine.configuration().contains(s1)); |
|
1739 |
||
1740 |
machine.start(); |
|
1741 |
QTRY_COMPARE(startedSpy.count(), 2); |
|
1742 |
QCOMPARE(machine.configuration().size(), 1); |
|
1743 |
QVERIFY(machine.configuration().contains(s1)); |
|
1744 |
||
1745 |
int id2 = machine.postDelayedEvent(new StringEvent("a"), 1000); |
|
1746 |
QVERIFY(id2 != -1); |
|
1747 |
machine.stop(); |
|
1748 |
QTRY_COMPARE(stoppedSpy.count(), 2); |
|
1749 |
machine.start(); |
|
1750 |
QTRY_COMPARE(startedSpy.count(), 3); |
|
1751 |
QTestEventLoop::instance().enterLoop(2); |
|
1752 |
QCOMPARE(machine.configuration().size(), 1); |
|
1753 |
QVERIFY(machine.configuration().contains(s1)); |
|
1754 |
} |
|
1755 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1756 |
void tst_QStateMachine::stopAndPostEvent() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1757 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1758 |
QStateMachine machine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1759 |
QState *s1 = new QState(&machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1760 |
machine.setInitialState(s1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1761 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1762 |
machine.start(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1763 |
QTRY_COMPARE(startedSpy.count(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1764 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1765 |
machine.stop(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1766 |
QCOMPARE(stoppedSpy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1767 |
machine.postEvent(new QEvent(QEvent::User)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1768 |
QTRY_COMPARE(stoppedSpy.count(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1769 |
QCoreApplication::processEvents(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1770 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1771 |
|
0 | 1772 |
void tst_QStateMachine::stateFinished() |
1773 |
{ |
|
1774 |
QStateMachine machine; |
|
1775 |
QState *s1 = new QState(&machine); |
|
1776 |
QState *s1_1 = new QState(s1); |
|
1777 |
QFinalState *s1_2 = new QFinalState(s1); |
|
1778 |
s1_1->addTransition(s1_2); |
|
1779 |
s1->setInitialState(s1_1); |
|
1780 |
QFinalState *s2 = new QFinalState(&machine); |
|
1781 |
s1->addTransition(s1, SIGNAL(finished()), s2); |
|
1782 |
machine.setInitialState(s1); |
|
1783 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1784 |
machine.start(); |
|
1785 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1786 |
QCOMPARE(machine.configuration().size(), 1); |
|
1787 |
QVERIFY(machine.configuration().contains(s2)); |
|
1788 |
} |
|
1789 |
||
1790 |
void tst_QStateMachine::parallelStates() |
|
1791 |
{ |
|
1792 |
QStateMachine machine; |
|
1793 |
||
1794 |
QState *s1 = new QState(QState::ParallelStates); |
|
1795 |
QCOMPARE(s1->childMode(), QState::ParallelStates); |
|
1796 |
QState *s1_1 = new QState(s1); |
|
1797 |
QState *s1_1_1 = new QState(s1_1); |
|
1798 |
QFinalState *s1_1_f = new QFinalState(s1_1); |
|
1799 |
s1_1_1->addTransition(s1_1_f); |
|
1800 |
s1_1->setInitialState(s1_1_1); |
|
1801 |
QState *s1_2 = new QState(s1); |
|
1802 |
QState *s1_2_1 = new QState(s1_2); |
|
1803 |
QFinalState *s1_2_f = new QFinalState(s1_2); |
|
1804 |
s1_2_1->addTransition(s1_2_f); |
|
1805 |
s1_2->setInitialState(s1_2_1); |
|
1806 |
{ |
|
1807 |
QString warning; |
|
1808 |
warning.sprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", s1); |
|
1809 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); |
|
1810 |
s1->setInitialState(0); |
|
1811 |
} |
|
1812 |
machine.addState(s1); |
|
1813 |
||
1814 |
QFinalState *s2 = new QFinalState(); |
|
1815 |
machine.addState(s2); |
|
1816 |
||
1817 |
s1->addTransition(s1, SIGNAL(finished()), s2); |
|
1818 |
||
1819 |
machine.setInitialState(s1); |
|
1820 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1821 |
machine.start(); |
|
1822 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1823 |
QCOMPARE(machine.configuration().size(), 1); |
|
1824 |
QVERIFY(machine.configuration().contains(s2)); |
|
1825 |
} |
|
1826 |
||
1827 |
void tst_QStateMachine::parallelRootState() |
|
1828 |
{ |
|
1829 |
QStateMachine machine; |
|
1830 |
QState *root = &machine; |
|
1831 |
QCOMPARE(root->childMode(), QState::ExclusiveStates); |
|
1832 |
root->setChildMode(QState::ParallelStates); |
|
1833 |
QCOMPARE(root->childMode(), QState::ParallelStates); |
|
1834 |
||
1835 |
QState *s1 = new QState(root); |
|
1836 |
QFinalState *s1_f = new QFinalState(s1); |
|
1837 |
s1->setInitialState(s1_f); |
|
1838 |
QState *s2 = new QState(root); |
|
1839 |
QFinalState *s2_f = new QFinalState(s2); |
|
1840 |
s2->setInitialState(s2_f); |
|
1841 |
||
1842 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
1843 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start."); |
|
1844 |
machine.start(); |
|
1845 |
QCoreApplication::processEvents(); |
|
1846 |
QEXPECT_FAIL("", "parallel root state is not supported (task 256587)", Continue); |
|
1847 |
QCOMPARE(startedSpy.count(), 1); |
|
1848 |
} |
|
1849 |
||
1850 |
void tst_QStateMachine::allSourceToTargetConfigurations() |
|
1851 |
{ |
|
1852 |
QStateMachine machine; |
|
1853 |
QState *s0 = new QState(&machine); |
|
1854 |
s0->setObjectName("s0"); |
|
1855 |
QState *s1 = new QState(s0); |
|
1856 |
s1->setObjectName("s1"); |
|
1857 |
QState *s11 = new QState(s1); |
|
1858 |
s11->setObjectName("s11"); |
|
1859 |
QState *s2 = new QState(s0); |
|
1860 |
s2->setObjectName("s2"); |
|
1861 |
QState *s21 = new QState(s2); |
|
1862 |
s21->setObjectName("s21"); |
|
1863 |
QState *s211 = new QState(s21); |
|
1864 |
s211->setObjectName("s211"); |
|
1865 |
QFinalState *f = new QFinalState(&machine); |
|
1866 |
f->setObjectName("f"); |
|
1867 |
||
1868 |
s0->setInitialState(s1); |
|
1869 |
s1->setInitialState(s11); |
|
1870 |
s2->setInitialState(s21); |
|
1871 |
s21->setInitialState(s211); |
|
1872 |
||
1873 |
s11->addTransition(new StringTransition("g", s211)); |
|
1874 |
s1->addTransition(new StringTransition("a", s1)); |
|
1875 |
s1->addTransition(new StringTransition("b", s11)); |
|
1876 |
s1->addTransition(new StringTransition("c", s2)); |
|
1877 |
s1->addTransition(new StringTransition("d", s0)); |
|
1878 |
s1->addTransition(new StringTransition("f", s211)); |
|
1879 |
s211->addTransition(new StringTransition("d", s21)); |
|
1880 |
s211->addTransition(new StringTransition("g", s0)); |
|
1881 |
s211->addTransition(new StringTransition("h", f)); |
|
1882 |
s21->addTransition(new StringTransition("b", s211)); |
|
1883 |
s2->addTransition(new StringTransition("c", s1)); |
|
1884 |
s2->addTransition(new StringTransition("f", s11)); |
|
1885 |
s0->addTransition(new StringTransition("e", s211)); |
|
1886 |
||
1887 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1888 |
machine.setInitialState(s0); |
|
1889 |
machine.start(); |
|
1890 |
QCoreApplication::processEvents(); |
|
1891 |
||
1892 |
machine.postEvent(new StringEvent("a")); |
|
1893 |
QCoreApplication::processEvents(); |
|
1894 |
machine.postEvent(new StringEvent("b")); |
|
1895 |
QCoreApplication::processEvents(); |
|
1896 |
machine.postEvent(new StringEvent("c")); |
|
1897 |
QCoreApplication::processEvents(); |
|
1898 |
machine.postEvent(new StringEvent("d")); |
|
1899 |
QCoreApplication::processEvents(); |
|
1900 |
machine.postEvent(new StringEvent("e")); |
|
1901 |
QCoreApplication::processEvents(); |
|
1902 |
machine.postEvent(new StringEvent("f")); |
|
1903 |
QCoreApplication::processEvents(); |
|
1904 |
machine.postEvent(new StringEvent("g")); |
|
1905 |
QCoreApplication::processEvents(); |
|
1906 |
machine.postEvent(new StringEvent("h")); |
|
1907 |
QCoreApplication::processEvents(); |
|
1908 |
||
1909 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1910 |
} |
|
1911 |
||
1912 |
class TestSignalTransition : public QSignalTransition |
|
1913 |
{ |
|
1914 |
public: |
|
1915 |
TestSignalTransition(QState *sourceState = 0) |
|
1916 |
: QSignalTransition(sourceState), m_sender(0) |
|
1917 |
{} |
|
1918 |
TestSignalTransition(QObject *sender, const char *signal, |
|
1919 |
QAbstractState *target) |
|
1920 |
: QSignalTransition(sender, signal), m_sender(0) |
|
1921 |
{ setTargetState(target); } |
|
1922 |
QObject *senderReceived() const { |
|
1923 |
return m_sender; |
|
1924 |
} |
|
1925 |
int signalIndexReceived() const { |
|
1926 |
return m_signalIndex; |
|
1927 |
} |
|
1928 |
QVariantList argumentsReceived() const { |
|
1929 |
return m_args; |
|
1930 |
} |
|
1931 |
protected: |
|
1932 |
bool eventTest(QEvent *e) { |
|
1933 |
if (!QSignalTransition::eventTest(e)) |
|
1934 |
return false; |
|
1935 |
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e); |
|
1936 |
m_sender = se->sender(); |
|
1937 |
m_signalIndex = se->signalIndex(); |
|
1938 |
m_args = se->arguments(); |
|
1939 |
return true; |
|
1940 |
} |
|
1941 |
private: |
|
1942 |
QObject *m_sender; |
|
1943 |
int m_signalIndex; |
|
1944 |
QVariantList m_args; |
|
1945 |
}; |
|
1946 |
||
1947 |
void tst_QStateMachine::signalTransitions() |
|
1948 |
{ |
|
1949 |
{ |
|
1950 |
QStateMachine machine; |
|
1951 |
QState *s0 = new QState(&machine); |
|
1952 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: sender cannot be null"); |
|
1953 |
QCOMPARE(s0->addTransition(0, SIGNAL(noSuchSignal()), 0), (QSignalTransition*)0); |
|
1954 |
||
1955 |
SignalEmitter emitter; |
|
1956 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: signal cannot be null"); |
|
1957 |
QCOMPARE(s0->addTransition(&emitter, 0, 0), (QSignalTransition*)0); |
|
1958 |
||
1959 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); |
|
1960 |
QCOMPARE(s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), 0), (QSignalTransition*)0); |
|
1961 |
||
1962 |
QFinalState *s1 = new QFinalState(&machine); |
|
1963 |
QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: no such signal SignalEmitter::noSuchSignal()"); |
|
1964 |
QCOMPARE(s0->addTransition(&emitter, SIGNAL(noSuchSignal()), s1), (QSignalTransition*)0); |
|
1965 |
||
1966 |
QSignalTransition *trans = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); |
|
1967 |
QVERIFY(trans != 0); |
|
1968 |
QCOMPARE(trans->sourceState(), s0); |
|
1969 |
QCOMPARE(trans->targetState(), (QAbstractState*)s1); |
|
1970 |
QCOMPARE(trans->senderObject(), (QObject*)&emitter); |
|
1971 |
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); |
|
1972 |
||
1973 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
1974 |
machine.setInitialState(s0); |
|
1975 |
machine.start(); |
|
1976 |
QCoreApplication::processEvents(); |
|
1977 |
||
1978 |
emitter.emitSignalWithNoArg(); |
|
1979 |
||
1980 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
1981 |
||
1982 |
emitter.emitSignalWithNoArg(); |
|
1983 |
||
1984 |
trans->setSignal(SIGNAL(signalWithIntArg(int))); |
|
1985 |
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithIntArg(int)))); |
|
1986 |
machine.start(); |
|
1987 |
QCoreApplication::processEvents(); |
|
1988 |
emitter.emitSignalWithIntArg(123); |
|
1989 |
QTRY_COMPARE(finishedSpy.count(), 2); |
|
1990 |
||
1991 |
machine.start(); |
|
1992 |
QCoreApplication::processEvents(); |
|
1993 |
trans->setSignal(SIGNAL(signalWithNoArg())); |
|
1994 |
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); |
|
1995 |
emitter.emitSignalWithNoArg(); |
|
1996 |
QTRY_COMPARE(finishedSpy.count(), 3); |
|
1997 |
||
1998 |
SignalEmitter emitter2; |
|
1999 |
machine.start(); |
|
2000 |
QCoreApplication::processEvents(); |
|
2001 |
trans->setSenderObject(&emitter2); |
|
2002 |
emitter2.emitSignalWithNoArg(); |
|
2003 |
QTRY_COMPARE(finishedSpy.count(), 4); |
|
2004 |
||
2005 |
machine.start(); |
|
2006 |
QCoreApplication::processEvents(); |
|
2007 |
QTest::ignoreMessage(QtWarningMsg, "QSignalTransition: no such signal: SignalEmitter::noSuchSignal()"); |
|
2008 |
trans->setSignal(SIGNAL(noSuchSignal())); |
|
2009 |
QCOMPARE(trans->signal(), QByteArray(SIGNAL(noSuchSignal()))); |
|
2010 |
} |
|
2011 |
{ |
|
2012 |
QStateMachine machine; |
|
2013 |
QState *s0 = new QState(&machine); |
|
2014 |
QFinalState *s1 = new QFinalState(&machine); |
|
2015 |
SignalEmitter emitter; |
|
2016 |
QSignalTransition *trans = s0->addTransition(&emitter, "signalWithNoArg()", s1); |
|
2017 |
QVERIFY(trans != 0); |
|
2018 |
QCOMPARE(trans->sourceState(), s0); |
|
2019 |
QCOMPARE(trans->targetState(), (QAbstractState*)s1); |
|
2020 |
QCOMPARE(trans->senderObject(), (QObject*)&emitter); |
|
2021 |
QCOMPARE(trans->signal(), QByteArray("signalWithNoArg()")); |
|
2022 |
||
2023 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2024 |
machine.setInitialState(s0); |
|
2025 |
machine.start(); |
|
2026 |
QCoreApplication::processEvents(); |
|
2027 |
||
2028 |
emitter.emitSignalWithNoArg(); |
|
2029 |
||
2030 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2031 |
||
2032 |
trans->setSignal("signalWithIntArg(int)"); |
|
2033 |
QCOMPARE(trans->signal(), QByteArray("signalWithIntArg(int)")); |
|
2034 |
machine.start(); |
|
2035 |
QCoreApplication::processEvents(); |
|
2036 |
emitter.emitSignalWithIntArg(123); |
|
2037 |
QTRY_COMPARE(finishedSpy.count(), 2); |
|
2038 |
} |
|
2039 |
{ |
|
2040 |
QStateMachine machine; |
|
2041 |
QState *s0 = new QState(&machine); |
|
2042 |
QFinalState *s1 = new QFinalState(&machine); |
|
2043 |
SignalEmitter emitter; |
|
2044 |
TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithIntArg(int)), s1); |
|
2045 |
s0->addTransition(trans); |
|
2046 |
||
2047 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2048 |
machine.setInitialState(s0); |
|
2049 |
machine.start(); |
|
2050 |
QCoreApplication::processEvents(); |
|
2051 |
||
2052 |
emitter.emitSignalWithIntArg(123); |
|
2053 |
||
2054 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2055 |
QCOMPARE(trans->senderReceived(), (QObject*)&emitter); |
|
2056 |
QCOMPARE(trans->signalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithIntArg(int)")); |
|
2057 |
QCOMPARE(trans->argumentsReceived().size(), 1); |
|
2058 |
QCOMPARE(trans->argumentsReceived().at(0).toInt(), 123); |
|
2059 |
} |
|
2060 |
{ |
|
2061 |
QStateMachine machine; |
|
2062 |
QState *s0 = new QState(&machine); |
|
2063 |
QFinalState *s1 = new QFinalState(&machine); |
|
2064 |
SignalEmitter emitter; |
|
2065 |
TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s1); |
|
2066 |
s0->addTransition(trans); |
|
2067 |
||
2068 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2069 |
machine.setInitialState(s0); |
|
2070 |
machine.start(); |
|
2071 |
QCoreApplication::processEvents(); |
|
2072 |
||
2073 |
QString testString = QString::fromLatin1("hello"); |
|
2074 |
emitter.emitSignalWithStringArg(testString); |
|
2075 |
||
2076 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2077 |
QCOMPARE(trans->senderReceived(), (QObject*)&emitter); |
|
2078 |
QCOMPARE(trans->signalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithStringArg(QString)")); |
|
2079 |
QCOMPARE(trans->argumentsReceived().size(), 1); |
|
2080 |
QCOMPARE(trans->argumentsReceived().at(0).toString(), testString); |
|
2081 |
} |
|
2082 |
{ |
|
2083 |
QStateMachine machine; |
|
2084 |
QState *s0 = new QState(&machine); |
|
2085 |
QFinalState *s1 = new QFinalState(&machine); |
|
2086 |
||
2087 |
TestSignalTransition *trans = new TestSignalTransition(); |
|
2088 |
QCOMPARE(trans->senderObject(), (QObject*)0); |
|
2089 |
QCOMPARE(trans->signal(), QByteArray()); |
|
2090 |
||
2091 |
SignalEmitter emitter; |
|
2092 |
trans->setSenderObject(&emitter); |
|
2093 |
QCOMPARE(trans->senderObject(), (QObject*)&emitter); |
|
2094 |
trans->setSignal(SIGNAL(signalWithNoArg())); |
|
2095 |
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); |
|
2096 |
trans->setTargetState(s1); |
|
2097 |
s0->addTransition(trans); |
|
2098 |
||
2099 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2100 |
machine.setInitialState(s0); |
|
2101 |
machine.start(); |
|
2102 |
QCoreApplication::processEvents(); |
|
2103 |
||
2104 |
emitter.emitSignalWithNoArg(); |
|
2105 |
||
2106 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2107 |
} |
|
2108 |
// Multiple transitions for same (object,signal) |
|
2109 |
{ |
|
2110 |
QStateMachine machine; |
|
2111 |
SignalEmitter emitter; |
|
2112 |
QState *s0 = new QState(&machine); |
|
2113 |
QState *s1 = new QState(&machine); |
|
2114 |
QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); |
|
2115 |
QSignalTransition *t1 = s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s0); |
|
2116 |
||
2117 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2118 |
machine.setInitialState(s0); |
|
2119 |
machine.start(); |
|
2120 |
QCoreApplication::processEvents(); |
|
2121 |
QCOMPARE(machine.configuration().size(), 1); |
|
2122 |
QVERIFY(machine.configuration().contains(s0)); |
|
2123 |
||
2124 |
emitter.emitSignalWithNoArg(); |
|
2125 |
QCoreApplication::processEvents(); |
|
2126 |
QCOMPARE(machine.configuration().size(), 1); |
|
2127 |
QVERIFY(machine.configuration().contains(s1)); |
|
2128 |
||
2129 |
s0->removeTransition(t0); |
|
2130 |
emitter.emitSignalWithNoArg(); |
|
2131 |
QCoreApplication::processEvents(); |
|
2132 |
QCOMPARE(machine.configuration().size(), 1); |
|
2133 |
QVERIFY(machine.configuration().contains(s0)); |
|
2134 |
||
2135 |
emitter.emitSignalWithNoArg(); |
|
2136 |
QCoreApplication::processEvents(); |
|
2137 |
QCOMPARE(machine.configuration().size(), 1); |
|
2138 |
QVERIFY(machine.configuration().contains(s0)); |
|
2139 |
||
2140 |
s1->removeTransition(t1); |
|
2141 |
emitter.emitSignalWithNoArg(); |
|
2142 |
QCoreApplication::processEvents(); |
|
2143 |
QCOMPARE(machine.configuration().size(), 1); |
|
2144 |
QVERIFY(machine.configuration().contains(s0)); |
|
2145 |
||
2146 |
s0->addTransition(t0); |
|
2147 |
s1->addTransition(t1); |
|
2148 |
emitter.emitSignalWithNoArg(); |
|
2149 |
QCoreApplication::processEvents(); |
|
2150 |
QCOMPARE(machine.configuration().size(), 1); |
|
2151 |
QVERIFY(machine.configuration().contains(s1)); |
|
2152 |
} |
|
2153 |
// multiple signal transitions from same source |
|
2154 |
{ |
|
2155 |
QStateMachine machine; |
|
2156 |
SignalEmitter emitter; |
|
2157 |
QState *s0 = new QState(&machine); |
|
2158 |
QFinalState *s1 = new QFinalState(&machine); |
|
2159 |
s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); |
|
2160 |
QFinalState *s2 = new QFinalState(&machine); |
|
2161 |
s0->addTransition(&emitter, SIGNAL(signalWithIntArg(int)), s2); |
|
2162 |
QFinalState *s3 = new QFinalState(&machine); |
|
2163 |
s0->addTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s3); |
|
2164 |
||
2165 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2166 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2167 |
machine.setInitialState(s0); |
|
2168 |
||
2169 |
machine.start(); |
|
2170 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2171 |
emitter.emitSignalWithNoArg(); |
|
2172 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2173 |
QCOMPARE(machine.configuration().size(), 1); |
|
2174 |
QVERIFY(machine.configuration().contains(s1)); |
|
2175 |
||
2176 |
machine.start(); |
|
2177 |
QTRY_COMPARE(startedSpy.count(), 2); |
|
2178 |
emitter.emitSignalWithIntArg(123); |
|
2179 |
QTRY_COMPARE(finishedSpy.count(), 2); |
|
2180 |
QCOMPARE(machine.configuration().size(), 1); |
|
2181 |
QVERIFY(machine.configuration().contains(s2)); |
|
2182 |
||
2183 |
machine.start(); |
|
2184 |
QTRY_COMPARE(startedSpy.count(), 3); |
|
2185 |
emitter.emitSignalWithStringArg("hello"); |
|
2186 |
QTRY_COMPARE(finishedSpy.count(), 3); |
|
2187 |
QCOMPARE(machine.configuration().size(), 1); |
|
2188 |
QVERIFY(machine.configuration().contains(s3)); |
|
2189 |
} |
|
2190 |
// signature normalization |
|
2191 |
{ |
|
2192 |
QStateMachine machine; |
|
2193 |
SignalEmitter emitter; |
|
2194 |
QState *s0 = new QState(&machine); |
|
2195 |
QFinalState *s1 = new QFinalState(&machine); |
|
2196 |
QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL( signalWithNoArg( ) ), s1); |
|
2197 |
QVERIFY(t0 != 0); |
|
2198 |
QCOMPARE(t0->signal(), QByteArray(SIGNAL( signalWithNoArg( ) ))); |
|
2199 |
||
2200 |
QSignalTransition *t1 = s0->addTransition(&emitter, SIGNAL( signalWithStringArg( const QString & ) ), s1); |
|
2201 |
QVERIFY(t1 != 0); |
|
2202 |
QCOMPARE(t1->signal(), QByteArray(SIGNAL( signalWithStringArg( const QString & ) ))); |
|
2203 |
||
2204 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2205 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2206 |
machine.setInitialState(s0); |
|
2207 |
machine.start(); |
|
2208 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2209 |
QCOMPARE(finishedSpy.count(), 0); |
|
2210 |
||
2211 |
emitter.emitSignalWithNoArg(); |
|
2212 |
||
2213 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2214 |
} |
|
2215 |
} |
|
2216 |
||
2217 |
class TestEventTransition : public QEventTransition |
|
2218 |
{ |
|
2219 |
public: |
|
2220 |
TestEventTransition(QState *sourceState = 0) |
|
2221 |
: QEventTransition(sourceState), |
|
2222 |
m_eventSource(0), m_eventType(QEvent::None) |
|
2223 |
{} |
|
2224 |
TestEventTransition(QObject *object, QEvent::Type type, |
|
2225 |
QAbstractState *target) |
|
2226 |
: QEventTransition(object, type), |
|
2227 |
m_eventSource(0), m_eventType(QEvent::None) |
|
2228 |
{ setTargetState(target); } |
|
2229 |
QObject *eventSourceReceived() const { |
|
2230 |
return m_eventSource; |
|
2231 |
} |
|
2232 |
QEvent::Type eventTypeReceived() const { |
|
2233 |
return m_eventType; |
|
2234 |
} |
|
2235 |
protected: |
|
2236 |
bool eventTest(QEvent *e) { |
|
2237 |
if (!QEventTransition::eventTest(e)) |
|
2238 |
return false; |
|
2239 |
QStateMachine::WrappedEvent *we = static_cast<QStateMachine::WrappedEvent*>(e); |
|
2240 |
m_eventSource = we->object(); |
|
2241 |
m_eventType = we->event()->type(); |
|
2242 |
return true; |
|
2243 |
} |
|
2244 |
private: |
|
2245 |
QObject *m_eventSource; |
|
2246 |
QEvent::Type m_eventType; |
|
2247 |
}; |
|
2248 |
||
2249 |
void tst_QStateMachine::eventTransitions() |
|
2250 |
{ |
|
2251 |
QPushButton button; |
|
2252 |
{ |
|
2253 |
QStateMachine machine; |
|
2254 |
QState *s0 = new QState(&machine); |
|
2255 |
QFinalState *s1 = new QFinalState(&machine); |
|
2256 |
||
2257 |
QMouseEventTransition *trans; |
|
2258 |
trans = new QMouseEventTransition(&button, QEvent::MouseButtonPress, Qt::LeftButton); |
|
2259 |
QCOMPARE(trans->targetState(), (QAbstractState*)0); |
|
2260 |
trans->setTargetState(s1); |
|
2261 |
QCOMPARE(trans->eventType(), QEvent::MouseButtonPress); |
|
2262 |
QCOMPARE(trans->button(), Qt::LeftButton); |
|
2263 |
QCOMPARE(trans->targetState(), (QAbstractState*)s1); |
|
2264 |
s0->addTransition(trans); |
|
2265 |
||
2266 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2267 |
machine.setInitialState(s0); |
|
2268 |
machine.start(); |
|
2269 |
QCoreApplication::processEvents(); |
|
2270 |
||
2271 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2272 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2273 |
||
2274 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2275 |
||
2276 |
trans->setEventType(QEvent::MouseButtonRelease); |
|
2277 |
QCOMPARE(trans->eventType(), QEvent::MouseButtonRelease); |
|
2278 |
machine.start(); |
|
2279 |
QCoreApplication::processEvents(); |
|
2280 |
QTest::mouseRelease(&button, Qt::LeftButton); |
|
2281 |
QTRY_COMPARE(finishedSpy.count(), 2); |
|
2282 |
||
2283 |
machine.start(); |
|
2284 |
QCoreApplication::processEvents(); |
|
2285 |
trans->setEventType(QEvent::MouseButtonPress); |
|
2286 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2287 |
QTRY_COMPARE(finishedSpy.count(), 3); |
|
2288 |
||
2289 |
QPushButton button2; |
|
2290 |
machine.start(); |
|
2291 |
QCoreApplication::processEvents(); |
|
2292 |
trans->setEventSource(&button2); |
|
2293 |
QTest::mousePress(&button2, Qt::LeftButton); |
|
2294 |
QTRY_COMPARE(finishedSpy.count(), 4); |
|
2295 |
} |
|
2296 |
for (int x = 0; x < 2; ++x) { |
|
2297 |
QStateMachine machine; |
|
2298 |
QState *s0 = new QState(&machine); |
|
2299 |
QFinalState *s1 = new QFinalState(&machine); |
|
2300 |
||
2301 |
QEventTransition *trans; |
|
2302 |
if (x == 0) { |
|
2303 |
trans = new QEventTransition(); |
|
2304 |
QCOMPARE(trans->eventSource(), (QObject*)0); |
|
2305 |
QCOMPARE(trans->eventType(), QEvent::None); |
|
2306 |
trans->setEventSource(&button); |
|
2307 |
trans->setEventType(QEvent::MouseButtonPress); |
|
2308 |
trans->setTargetState(s1); |
|
2309 |
} else if (x == 1) { |
|
2310 |
trans = new QEventTransition(&button, QEvent::MouseButtonPress); |
|
2311 |
trans->setTargetState(s1); |
|
2312 |
} |
|
2313 |
QCOMPARE(trans->eventSource(), (QObject*)&button); |
|
2314 |
QCOMPARE(trans->eventType(), QEvent::MouseButtonPress); |
|
2315 |
QCOMPARE(trans->targetState(), (QAbstractState*)s1); |
|
2316 |
s0->addTransition(trans); |
|
2317 |
||
2318 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2319 |
machine.setInitialState(s0); |
|
2320 |
machine.start(); |
|
2321 |
QCoreApplication::processEvents(); |
|
2322 |
||
2323 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2324 |
QCoreApplication::processEvents(); |
|
2325 |
||
2326 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2327 |
} |
|
2328 |
{ |
|
2329 |
QStateMachine machine; |
|
2330 |
QState *s0 = new QState(&machine); |
|
2331 |
QFinalState *s1 = new QFinalState(&machine); |
|
2332 |
||
2333 |
QMouseEventTransition *trans = new QMouseEventTransition(); |
|
2334 |
QCOMPARE(trans->eventSource(), (QObject*)0); |
|
2335 |
QCOMPARE(trans->eventType(), QEvent::None); |
|
2336 |
QCOMPARE(trans->button(), Qt::NoButton); |
|
2337 |
trans->setEventSource(&button); |
|
2338 |
trans->setEventType(QEvent::MouseButtonPress); |
|
2339 |
trans->setButton(Qt::LeftButton); |
|
2340 |
trans->setTargetState(s1); |
|
2341 |
s0->addTransition(trans); |
|
2342 |
||
2343 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2344 |
machine.setInitialState(s0); |
|
2345 |
machine.start(); |
|
2346 |
QCoreApplication::processEvents(); |
|
2347 |
||
2348 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2349 |
QCoreApplication::processEvents(); |
|
2350 |
||
2351 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2352 |
} |
|
2353 |
||
2354 |
{ |
|
2355 |
QStateMachine machine; |
|
2356 |
QState *s0 = new QState(&machine); |
|
2357 |
QFinalState *s1 = new QFinalState(&machine); |
|
2358 |
||
2359 |
QKeyEventTransition *trans = new QKeyEventTransition(&button, QEvent::KeyPress, Qt::Key_A); |
|
2360 |
QCOMPARE(trans->eventType(), QEvent::KeyPress); |
|
2361 |
QCOMPARE(trans->key(), (int)Qt::Key_A); |
|
2362 |
trans->setTargetState(s1); |
|
2363 |
s0->addTransition(trans); |
|
2364 |
||
2365 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2366 |
machine.setInitialState(s0); |
|
2367 |
machine.start(); |
|
2368 |
QCoreApplication::processEvents(); |
|
2369 |
||
2370 |
QTest::keyPress(&button, Qt::Key_A); |
|
2371 |
QCoreApplication::processEvents(); |
|
2372 |
||
2373 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2374 |
} |
|
2375 |
{ |
|
2376 |
QStateMachine machine; |
|
2377 |
QState *s0 = new QState(&machine); |
|
2378 |
QFinalState *s1 = new QFinalState(&machine); |
|
2379 |
||
2380 |
QKeyEventTransition *trans = new QKeyEventTransition(); |
|
2381 |
QCOMPARE(trans->eventSource(), (QObject*)0); |
|
2382 |
QCOMPARE(trans->eventType(), QEvent::None); |
|
2383 |
QCOMPARE(trans->key(), 0); |
|
2384 |
trans->setEventSource(&button); |
|
2385 |
trans->setEventType(QEvent::KeyPress); |
|
2386 |
trans->setKey(Qt::Key_A); |
|
2387 |
trans->setTargetState(s1); |
|
2388 |
s0->addTransition(trans); |
|
2389 |
||
2390 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2391 |
machine.setInitialState(s0); |
|
2392 |
machine.start(); |
|
2393 |
QCoreApplication::processEvents(); |
|
2394 |
||
2395 |
QTest::keyPress(&button, Qt::Key_A); |
|
2396 |
QCoreApplication::processEvents(); |
|
2397 |
||
2398 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2399 |
} |
|
2400 |
// Multiple transitions for same (object,event) |
|
2401 |
{ |
|
2402 |
QStateMachine machine; |
|
2403 |
QState *s0 = new QState(&machine); |
|
2404 |
QState *s1 = new QState(&machine); |
|
2405 |
QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); |
|
2406 |
t0->setTargetState(s1); |
|
2407 |
s0->addTransition(t0); |
|
2408 |
QEventTransition *t1 = new QEventTransition(&button, QEvent::MouseButtonPress); |
|
2409 |
t1->setTargetState(s0); |
|
2410 |
s1->addTransition(t1); |
|
2411 |
||
2412 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2413 |
machine.setInitialState(s0); |
|
2414 |
machine.start(); |
|
2415 |
QCoreApplication::processEvents(); |
|
2416 |
QCOMPARE(machine.configuration().size(), 1); |
|
2417 |
QVERIFY(machine.configuration().contains(s0)); |
|
2418 |
||
2419 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2420 |
QCoreApplication::processEvents(); |
|
2421 |
QCOMPARE(machine.configuration().size(), 1); |
|
2422 |
QVERIFY(machine.configuration().contains(s1)); |
|
2423 |
||
2424 |
s0->removeTransition(t0); |
|
2425 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2426 |
QCoreApplication::processEvents(); |
|
2427 |
QCOMPARE(machine.configuration().size(), 1); |
|
2428 |
QVERIFY(machine.configuration().contains(s0)); |
|
2429 |
||
2430 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2431 |
QCoreApplication::processEvents(); |
|
2432 |
QCOMPARE(machine.configuration().size(), 1); |
|
2433 |
QVERIFY(machine.configuration().contains(s0)); |
|
2434 |
||
2435 |
s1->removeTransition(t1); |
|
2436 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2437 |
QCoreApplication::processEvents(); |
|
2438 |
QCOMPARE(machine.configuration().size(), 1); |
|
2439 |
QVERIFY(machine.configuration().contains(s0)); |
|
2440 |
||
2441 |
s0->addTransition(t0); |
|
2442 |
s1->addTransition(t1); |
|
2443 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2444 |
QCoreApplication::processEvents(); |
|
2445 |
QCOMPARE(machine.configuration().size(), 1); |
|
2446 |
QVERIFY(machine.configuration().contains(s1)); |
|
2447 |
} |
|
2448 |
// multiple event transitions from same source |
|
2449 |
{ |
|
2450 |
QStateMachine machine; |
|
2451 |
QState *s0 = new QState(&machine); |
|
2452 |
QFinalState *s1 = new QFinalState(&machine); |
|
2453 |
QFinalState *s2 = new QFinalState(&machine); |
|
2454 |
QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); |
|
2455 |
t0->setTargetState(s1); |
|
2456 |
s0->addTransition(t0); |
|
2457 |
QEventTransition *t1 = new QEventTransition(&button, QEvent::MouseButtonRelease); |
|
2458 |
t1->setTargetState(s2); |
|
2459 |
s0->addTransition(t1); |
|
2460 |
||
2461 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2462 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2463 |
machine.setInitialState(s0); |
|
2464 |
||
2465 |
machine.start(); |
|
2466 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2467 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2468 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2469 |
QCOMPARE(machine.configuration().size(), 1); |
|
2470 |
QVERIFY(machine.configuration().contains(s1)); |
|
2471 |
||
2472 |
machine.start(); |
|
2473 |
QTRY_COMPARE(startedSpy.count(), 2); |
|
2474 |
QTest::mouseRelease(&button, Qt::LeftButton); |
|
2475 |
QTRY_COMPARE(finishedSpy.count(), 2); |
|
2476 |
QCOMPARE(machine.configuration().size(), 1); |
|
2477 |
QVERIFY(machine.configuration().contains(s2)); |
|
2478 |
} |
|
2479 |
// custom event |
|
2480 |
{ |
|
2481 |
QStateMachine machine; |
|
2482 |
QState *s0 = new QState(&machine); |
|
2483 |
QFinalState *s1 = new QFinalState(&machine); |
|
2484 |
||
2485 |
QEventTransition *trans = new QEventTransition(&button, QEvent::Type(QEvent::User+1)); |
|
2486 |
trans->setTargetState(s1); |
|
2487 |
s0->addTransition(trans); |
|
2488 |
||
2489 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2490 |
machine.setInitialState(s0); |
|
2491 |
machine.start(); |
|
2492 |
QTest::ignoreMessage(QtWarningMsg, "QObject event transitions are not supported for custom types"); |
|
2493 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2494 |
} |
|
2495 |
// custom transition |
|
2496 |
{ |
|
2497 |
QStateMachine machine; |
|
2498 |
QState *s0 = new QState(&machine); |
|
2499 |
QFinalState *s1 = new QFinalState(&machine); |
|
2500 |
||
2501 |
TestEventTransition *trans = new TestEventTransition(&button, QEvent::MouseButtonPress, s1); |
|
2502 |
s0->addTransition(trans); |
|
2503 |
QCOMPARE(trans->eventSourceReceived(), (QObject*)0); |
|
2504 |
QCOMPARE(trans->eventTypeReceived(), QEvent::None); |
|
2505 |
||
2506 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2507 |
machine.setInitialState(s0); |
|
2508 |
machine.start(); |
|
2509 |
QCoreApplication::processEvents(); |
|
2510 |
||
2511 |
QTest::mousePress(&button, Qt::LeftButton); |
|
2512 |
QCoreApplication::processEvents(); |
|
2513 |
||
2514 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2515 |
||
2516 |
QCOMPARE(trans->eventSourceReceived(), (QObject*)&button); |
|
2517 |
QCOMPARE(trans->eventTypeReceived(), QEvent::MouseButtonPress); |
|
2518 |
} |
|
2519 |
} |
|
2520 |
||
2521 |
void tst_QStateMachine::graphicsSceneEventTransitions() |
|
2522 |
{ |
|
2523 |
QGraphicsScene scene; |
|
2524 |
QGraphicsTextItem *textItem = scene.addText("foo"); |
|
2525 |
||
2526 |
QStateMachine machine; |
|
2527 |
QState *s1 = new QState(&machine); |
|
2528 |
QFinalState *s2 = new QFinalState(&machine); |
|
2529 |
QEventTransition *t = new QEventTransition(textItem, QEvent::GraphicsSceneMouseMove); |
|
2530 |
t->setTargetState(s2); |
|
2531 |
s1->addTransition(t); |
|
2532 |
machine.setInitialState(s1); |
|
2533 |
||
2534 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2535 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2536 |
machine.start(); |
|
2537 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2538 |
QVERIFY(finishedSpy.count() == 0); |
|
2539 |
QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove); |
|
2540 |
scene.sendEvent(textItem, &mouseEvent); |
|
2541 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2542 |
} |
|
2543 |
||
2544 |
void tst_QStateMachine::historyStates() |
|
2545 |
{ |
|
2546 |
for (int x = 0; x < 2; ++x) { |
|
2547 |
QStateMachine machine; |
|
2548 |
QState *root = &machine; |
|
2549 |
QState *s0 = new QState(root); |
|
2550 |
QState *s00 = new QState(s0); |
|
2551 |
QState *s01 = new QState(s0); |
|
2552 |
QHistoryState *s0h; |
|
2553 |
if (x == 0) { |
|
2554 |
s0h = new QHistoryState(s0); |
|
2555 |
QCOMPARE(s0h->historyType(), QHistoryState::ShallowHistory); |
|
2556 |
s0h->setHistoryType(QHistoryState::DeepHistory); |
|
2557 |
} else { |
|
2558 |
s0h = new QHistoryState(QHistoryState::DeepHistory, s0); |
|
2559 |
} |
|
2560 |
QCOMPARE(s0h->historyType(), QHistoryState::DeepHistory); |
|
2561 |
s0h->setHistoryType(QHistoryState::ShallowHistory); |
|
2562 |
QCOMPARE(s0h->historyType(), QHistoryState::ShallowHistory); |
|
2563 |
QCOMPARE(s0h->defaultState(), (QAbstractState*)0); |
|
2564 |
s0h->setDefaultState(s00); |
|
2565 |
QCOMPARE(s0h->defaultState(), (QAbstractState*)s00); |
|
2566 |
QString warning; |
|
2567 |
warning.sprintf("QHistoryState::setDefaultState: state %p does not belong to this history state's group (%p)", s0, s0); |
|
2568 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); |
|
2569 |
s0h->setDefaultState(s0); |
|
2570 |
QState *s1 = new QState(root); |
|
2571 |
QFinalState *s2 = new QFinalState(root); |
|
2572 |
||
2573 |
s00->addTransition(new StringTransition("a", s01)); |
|
2574 |
s0->addTransition(new StringTransition("b", s1)); |
|
2575 |
s1->addTransition(new StringTransition("c", s0h)); |
|
2576 |
s0->addTransition(new StringTransition("d", s2)); |
|
2577 |
||
2578 |
root->setInitialState(s0); |
|
2579 |
s0->setInitialState(s00); |
|
2580 |
||
2581 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2582 |
machine.start(); |
|
2583 |
QCoreApplication::processEvents(); |
|
2584 |
QCOMPARE(machine.configuration().size(), 2); |
|
2585 |
QVERIFY(machine.configuration().contains(s0)); |
|
2586 |
QVERIFY(machine.configuration().contains(s00)); |
|
2587 |
||
2588 |
machine.postEvent(new StringEvent("a")); |
|
2589 |
QCoreApplication::processEvents(); |
|
2590 |
QCOMPARE(machine.configuration().size(), 2); |
|
2591 |
QVERIFY(machine.configuration().contains(s0)); |
|
2592 |
QVERIFY(machine.configuration().contains(s01)); |
|
2593 |
||
2594 |
machine.postEvent(new StringEvent("b")); |
|
2595 |
QCoreApplication::processEvents(); |
|
2596 |
QCOMPARE(machine.configuration().size(), 1); |
|
2597 |
QVERIFY(machine.configuration().contains(s1)); |
|
2598 |
||
2599 |
machine.postEvent(new StringEvent("c")); |
|
2600 |
QCoreApplication::processEvents(); |
|
2601 |
QCOMPARE(machine.configuration().size(), 2); |
|
2602 |
QVERIFY(machine.configuration().contains(s0)); |
|
2603 |
QVERIFY(machine.configuration().contains(s01)); |
|
2604 |
||
2605 |
machine.postEvent(new StringEvent("d")); |
|
2606 |
QCoreApplication::processEvents(); |
|
2607 |
QCOMPARE(machine.configuration().size(), 1); |
|
2608 |
QVERIFY(machine.configuration().contains(s2)); |
|
2609 |
||
2610 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
2611 |
} |
|
2612 |
} |
|
2613 |
||
2614 |
void tst_QStateMachine::startAndStop() |
|
2615 |
{ |
|
2616 |
QStateMachine machine; |
|
2617 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2618 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
|
2619 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2620 |
QVERIFY(!machine.isRunning()); |
|
2621 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start."); |
|
2622 |
machine.start(); |
|
2623 |
QCOMPARE(startedSpy.count(), 0); |
|
2624 |
QCOMPARE(stoppedSpy.count(), 0); |
|
2625 |
QCOMPARE(finishedSpy.count(), 0); |
|
2626 |
QVERIFY(!machine.isRunning()); |
|
2627 |
machine.stop(); |
|
2628 |
QCOMPARE(startedSpy.count(), 0); |
|
2629 |
QCOMPARE(stoppedSpy.count(), 0); |
|
2630 |
QCOMPARE(finishedSpy.count(), 0); |
|
2631 |
||
2632 |
QState *s1 = new QState(&machine); |
|
2633 |
machine.setInitialState(s1); |
|
2634 |
machine.start(); |
|
2635 |
QTRY_COMPARE(machine.isRunning(), true); |
|
2636 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2637 |
QCOMPARE(stoppedSpy.count(), 0); |
|
2638 |
QCOMPARE(finishedSpy.count(), 0); |
|
2639 |
QCOMPARE(machine.configuration().count(), 1); |
|
2640 |
QVERIFY(machine.configuration().contains(s1)); |
|
2641 |
||
2642 |
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start(): already running"); |
|
2643 |
machine.start(); |
|
2644 |
||
2645 |
machine.stop(); |
|
2646 |
QTRY_COMPARE(machine.isRunning(), false); |
|
2647 |
QTRY_COMPARE(stoppedSpy.count(), 1); |
|
2648 |
QCOMPARE(startedSpy.count(), 1); |
|
2649 |
QCOMPARE(finishedSpy.count(), 0); |
|
2650 |
||
2651 |
QCOMPARE(machine.configuration().count(), 1); |
|
2652 |
QVERIFY(machine.configuration().contains(s1)); |
|
2653 |
||
2654 |
machine.start(); |
|
2655 |
machine.stop(); |
|
2656 |
QTRY_COMPARE(startedSpy.count(), 2); |
|
2657 |
QCOMPARE(stoppedSpy.count(), 2); |
|
2658 |
} |
|
2659 |
||
2660 |
void tst_QStateMachine::targetStateWithNoParent() |
|
2661 |
{ |
|
2662 |
QStateMachine machine; |
|
2663 |
QState *s1 = new QState(&machine); |
|
2664 |
s1->setObjectName("s1"); |
|
2665 |
QState s2; |
|
2666 |
s1->addTransition(&s2); |
|
2667 |
machine.setInitialState(s1); |
|
2668 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
2669 |
QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); |
|
2670 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
2671 |
machine.start(); |
|
2672 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 's1'"); |
|
2673 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
2674 |
QCOMPARE(machine.isRunning(), false); |
|
2675 |
QCOMPARE(stoppedSpy.count(), 1); |
|
2676 |
QCOMPARE(finishedSpy.count(), 0); |
|
2677 |
QCOMPARE(machine.error(), QStateMachine::NoCommonAncestorForTransitionError); |
|
2678 |
} |
|
2679 |
||
2680 |
void tst_QStateMachine::targetStateDeleted() |
|
2681 |
{ |
|
2682 |
QStateMachine machine; |
|
2683 |
QState *s1 = new QState(&machine); |
|
2684 |
s1->setObjectName("s1"); |
|
2685 |
QState *s2 = new QState(&machine); |
|
2686 |
QAbstractTransition *trans = s1->addTransition(s2); |
|
2687 |
delete s2; |
|
2688 |
QCOMPARE(trans->targetState(), (QAbstractState*)0); |
|
2689 |
QVERIFY(trans->targetStates().isEmpty()); |
|
2690 |
} |
|
2691 |
||
2692 |
void tst_QStateMachine::defaultGlobalRestorePolicy() |
|
2693 |
{ |
|
2694 |
QStateMachine machine; |
|
2695 |
||
2696 |
QObject *propertyHolder = new QObject(&machine); |
|
2697 |
propertyHolder->setProperty("a", 1); |
|
2698 |
propertyHolder->setProperty("b", 2); |
|
2699 |
||
2700 |
QState *s1 = new QState(&machine); |
|
2701 |
s1->assignProperty(propertyHolder, "a", 3); |
|
2702 |
||
2703 |
QState *s2 = new QState(&machine); |
|
2704 |
s2->assignProperty(propertyHolder, "b", 4); |
|
2705 |
||
2706 |
QState *s3 = new QState(&machine); |
|
2707 |
||
2708 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
2709 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
2710 |
||
2711 |
machine.setInitialState(s1); |
|
2712 |
machine.start(); |
|
2713 |
QCoreApplication::processEvents(); |
|
2714 |
||
2715 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2716 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2717 |
||
2718 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2719 |
QCoreApplication::processEvents(); |
|
2720 |
||
2721 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2722 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2723 |
||
2724 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2725 |
QCoreApplication::processEvents(); |
|
2726 |
||
2727 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2728 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2729 |
} |
|
2730 |
||
2731 |
void tst_QStateMachine::noInitialStateForInitialState() |
|
2732 |
{ |
|
2733 |
QStateMachine machine; |
|
2734 |
||
2735 |
QState *initialState = new QState(&machine); |
|
2736 |
initialState->setObjectName("initialState"); |
|
2737 |
machine.setInitialState(initialState); |
|
2738 |
||
2739 |
QState *childState = new QState(initialState); |
|
2740 |
(void)childState; |
|
2741 |
||
2742 |
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: " |
|
2743 |
"Missing initial state in compound state 'initialState'"); |
|
2744 |
machine.start(); |
|
2745 |
QCoreApplication::processEvents(); |
|
2746 |
||
2747 |
QCOMPARE(machine.isRunning(), false); |
|
2748 |
QCOMPARE(int(machine.error()), int(QStateMachine::NoInitialStateError)); |
|
2749 |
} |
|
2750 |
||
2751 |
/* |
|
2752 |
void tst_QStateMachine::restorePolicyNotInherited() |
|
2753 |
{ |
|
2754 |
QStateMachine machine; |
|
2755 |
||
2756 |
QObject *propertyHolder = new QObject(); |
|
2757 |
propertyHolder->setProperty("a", 1); |
|
2758 |
propertyHolder->setProperty("b", 2); |
|
2759 |
||
2760 |
QState *parentState = new QState(&machine); |
|
2761 |
parentState->setObjectName("parentState"); |
|
2762 |
parentState->setRestorePolicy(QState::RestoreProperties); |
|
2763 |
||
2764 |
QState *s1 = new QState(parentState); |
|
2765 |
s1->setObjectName("s1"); |
|
2766 |
s1->assignProperty(propertyHolder, "a", 3); |
|
2767 |
parentState->setInitialState(s1); |
|
2768 |
||
2769 |
QState *s2 = new QState(parentState); |
|
2770 |
s2->setObjectName("s2"); |
|
2771 |
s2->assignProperty(propertyHolder, "b", 4); |
|
2772 |
||
2773 |
QState *s3 = new QState(parentState); |
|
2774 |
s3->setObjectName("s3"); |
|
2775 |
||
2776 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
2777 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
2778 |
||
2779 |
machine.setInitialState(parentState); |
|
2780 |
machine.start(); |
|
2781 |
QCoreApplication::processEvents(); |
|
2782 |
||
2783 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2784 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2785 |
||
2786 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2787 |
QCoreApplication::processEvents(); |
|
2788 |
||
2789 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2790 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2791 |
||
2792 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2793 |
QCoreApplication::processEvents(); |
|
2794 |
||
2795 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2796 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2797 |
||
2798 |
}*/ |
|
2799 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2800 |
void tst_QStateMachine::globalRestorePolicySetToDontRestore() |
0 | 2801 |
{ |
2802 |
QStateMachine machine; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2803 |
machine.setGlobalRestorePolicy(QStateMachine::DontRestoreProperties); |
0 | 2804 |
|
2805 |
QObject *propertyHolder = new QObject(&machine); |
|
2806 |
propertyHolder->setProperty("a", 1); |
|
2807 |
propertyHolder->setProperty("b", 2); |
|
2808 |
||
2809 |
QState *s1 = new QState(&machine); |
|
2810 |
s1->assignProperty(propertyHolder, "a", 3); |
|
2811 |
||
2812 |
QState *s2 = new QState(&machine); |
|
2813 |
s2->assignProperty(propertyHolder, "b", 4); |
|
2814 |
||
2815 |
QState *s3 = new QState(&machine); |
|
2816 |
||
2817 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
2818 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
2819 |
||
2820 |
machine.setInitialState(s1); |
|
2821 |
machine.start(); |
|
2822 |
QCoreApplication::processEvents(); |
|
2823 |
||
2824 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2825 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2826 |
||
2827 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2828 |
QCoreApplication::processEvents(); |
|
2829 |
||
2830 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2831 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2832 |
||
2833 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2834 |
QCoreApplication::processEvents(); |
|
2835 |
||
2836 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2837 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2838 |
} |
|
2839 |
||
2840 |
/* |
|
2841 |
void tst_QStateMachine::setRestorePolicyToDoNotRestore() |
|
2842 |
{ |
|
2843 |
QObject *object = new QObject(); |
|
2844 |
object->setProperty("a", 1); |
|
2845 |
object->setProperty("b", 2); |
|
2846 |
||
2847 |
QStateMachine machine; |
|
2848 |
||
2849 |
QState *S1 = new QState(); |
|
2850 |
S1->setObjectName("S1"); |
|
2851 |
S1->assignProperty(object, "a", 3); |
|
2852 |
S1->setRestorePolicy(QState::DoNotRestoreProperties); |
|
2853 |
machine.addState(S1); |
|
2854 |
||
2855 |
QState *S2 = new QState(); |
|
2856 |
S2->setObjectName("S2"); |
|
2857 |
S2->assignProperty(object, "b", 5); |
|
2858 |
S2->setRestorePolicy(QState::DoNotRestoreProperties); |
|
2859 |
machine.addState(S2); |
|
2860 |
||
2861 |
QState *S3 = new QState(); |
|
2862 |
S3->setObjectName("S3"); |
|
2863 |
S3->setRestorePolicy(QState::DoNotRestoreProperties); |
|
2864 |
machine.addState(S3); |
|
2865 |
||
2866 |
QFinalState *S4 = new QFinalState(); |
|
2867 |
machine.addState(S4); |
|
2868 |
||
2869 |
S1->addTransition(new EventTransition(QEvent::User, S2)); |
|
2870 |
S2->addTransition(new EventTransition(QEvent::User, S3)); |
|
2871 |
S3->addTransition(S4); |
|
2872 |
||
2873 |
machine.setInitialState(S1); |
|
2874 |
machine.start(); |
|
2875 |
QCoreApplication::processEvents(); |
|
2876 |
||
2877 |
QCOMPARE(object->property("a").toInt(), 3); |
|
2878 |
QCOMPARE(object->property("b").toInt(), 2); |
|
2879 |
||
2880 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2881 |
QCoreApplication::processEvents(); |
|
2882 |
||
2883 |
QCOMPARE(object->property("a").toInt(), 3); |
|
2884 |
QCOMPARE(object->property("b").toInt(), 5); |
|
2885 |
||
2886 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2887 |
QCoreApplication::processEvents(); |
|
2888 |
||
2889 |
QCOMPARE(object->property("a").toInt(), 3); |
|
2890 |
QCOMPARE(object->property("b").toInt(), 5); |
|
2891 |
} |
|
2892 |
||
2893 |
void tst_QStateMachine::setGlobalRestorePolicyToGlobalRestore() |
|
2894 |
{ |
|
2895 |
s_countWarnings = false; |
|
2896 |
QStateMachine machine; |
|
2897 |
machine.setGlobalRestorePolicy(QStateMachine::GlobalRestorePolicy); |
|
2898 |
||
2899 |
QCOMPARE(machine.globalRestorePolicy(), QStateMachine::DoNotRestoreProperties); |
|
2900 |
QCOMPARE(s_msgType, QtWarningMsg); |
|
2901 |
||
2902 |
s_msgType = QtDebugMsg; |
|
2903 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
|
2904 |
machine.setGlobalRestorePolicy(QStateMachine::GlobalRestorePolicy); |
|
2905 |
||
2906 |
QCOMPARE(machine.globalRestorePolicy(), QStateMachine::RestoreProperties); |
|
2907 |
QCOMPARE(s_msgType, QtWarningMsg); |
|
2908 |
} |
|
2909 |
||
2910 |
||
2911 |
void tst_QStateMachine::restorePolicyOnChildState() |
|
2912 |
{ |
|
2913 |
QStateMachine machine; |
|
2914 |
||
2915 |
QObject *propertyHolder = new QObject(); |
|
2916 |
propertyHolder->setProperty("a", 1); |
|
2917 |
propertyHolder->setProperty("b", 2); |
|
2918 |
||
2919 |
QState *parentState = new QState(&machine); |
|
2920 |
parentState->setObjectName("parentState"); |
|
2921 |
||
2922 |
QState *s1 = new QState(parentState); |
|
2923 |
s1->setRestorePolicy(QState::RestoreProperties); |
|
2924 |
s1->setObjectName("s1"); |
|
2925 |
s1->assignProperty(propertyHolder, "a", 3); |
|
2926 |
parentState->setInitialState(s1); |
|
2927 |
||
2928 |
QState *s2 = new QState(parentState); |
|
2929 |
s2->setRestorePolicy(QState::RestoreProperties); |
|
2930 |
s2->setObjectName("s2"); |
|
2931 |
s2->assignProperty(propertyHolder, "b", 4); |
|
2932 |
||
2933 |
QState *s3 = new QState(parentState); |
|
2934 |
s3->setRestorePolicy(QState::RestoreProperties); |
|
2935 |
s3->setObjectName("s3"); |
|
2936 |
||
2937 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
2938 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
2939 |
||
2940 |
machine.setInitialState(parentState); |
|
2941 |
machine.start(); |
|
2942 |
QCoreApplication::processEvents(); |
|
2943 |
||
2944 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2945 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2946 |
||
2947 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2948 |
QCoreApplication::processEvents(); |
|
2949 |
||
2950 |
QCOMPARE(propertyHolder->property("a").toInt(), 1); |
|
2951 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2952 |
||
2953 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2954 |
QCoreApplication::processEvents(); |
|
2955 |
||
2956 |
QCOMPARE(propertyHolder->property("a").toInt(), 1); |
|
2957 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2958 |
} |
|
2959 |
*/ |
|
2960 |
||
2961 |
void tst_QStateMachine::globalRestorePolicySetToRestore() |
|
2962 |
{ |
|
2963 |
QStateMachine machine; |
|
2964 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
|
2965 |
||
2966 |
QObject *propertyHolder = new QObject(&machine); |
|
2967 |
propertyHolder->setProperty("a", 1); |
|
2968 |
propertyHolder->setProperty("b", 2); |
|
2969 |
||
2970 |
QState *s1 = new QState(&machine); |
|
2971 |
s1->assignProperty(propertyHolder, "a", 3); |
|
2972 |
||
2973 |
QState *s2 = new QState(&machine); |
|
2974 |
s2->assignProperty(propertyHolder, "b", 4); |
|
2975 |
||
2976 |
QState *s3 = new QState(&machine); |
|
2977 |
||
2978 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
2979 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
2980 |
||
2981 |
machine.setInitialState(s1); |
|
2982 |
machine.start(); |
|
2983 |
QCoreApplication::processEvents(); |
|
2984 |
||
2985 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
2986 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2987 |
||
2988 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2989 |
QCoreApplication::processEvents(); |
|
2990 |
||
2991 |
QCOMPARE(propertyHolder->property("a").toInt(), 1); |
|
2992 |
QCOMPARE(propertyHolder->property("b").toInt(), 4); |
|
2993 |
||
2994 |
machine.postEvent(new QEvent(QEvent::User)); |
|
2995 |
QCoreApplication::processEvents(); |
|
2996 |
||
2997 |
QCOMPARE(propertyHolder->property("a").toInt(), 1); |
|
2998 |
QCOMPARE(propertyHolder->property("b").toInt(), 2); |
|
2999 |
} |
|
3000 |
||
3001 |
/* |
|
3002 |
void tst_QStateMachine::mixedRestoreProperties() |
|
3003 |
{ |
|
3004 |
QStateMachine machine; |
|
3005 |
||
3006 |
QObject *propertyHolder = new QObject(); |
|
3007 |
propertyHolder->setProperty("a", 1); |
|
3008 |
||
3009 |
QState *s1 = new QState(&machine); |
|
3010 |
s1->setRestorePolicy(QState::RestoreProperties); |
|
3011 |
s1->assignProperty(propertyHolder, "a", 3); |
|
3012 |
||
3013 |
QState *s2 = new QState(&machine); |
|
3014 |
s2->assignProperty(propertyHolder, "a", 4); |
|
3015 |
||
3016 |
QState *s3 = new QState(&machine); |
|
3017 |
||
3018 |
QState *s4 = new QState(&machine); |
|
3019 |
s4->assignProperty(propertyHolder, "a", 5); |
|
3020 |
||
3021 |
QState *s5 = new QState(&machine); |
|
3022 |
s5->setRestorePolicy(QState::RestoreProperties); |
|
3023 |
s5->assignProperty(propertyHolder, "a", 6); |
|
3024 |
||
3025 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3026 |
s2->addTransition(new EventTransition(QEvent::User, s3)); |
|
3027 |
s3->addTransition(new EventTransition(QEvent::User, s4)); |
|
3028 |
s4->addTransition(new EventTransition(QEvent::User, s5)); |
|
3029 |
s5->addTransition(new EventTransition(QEvent::User, s3)); |
|
3030 |
||
3031 |
machine.setInitialState(s1); |
|
3032 |
machine.start(); |
|
3033 |
QCoreApplication::processEvents(); |
|
3034 |
||
3035 |
// Enter s1, save current |
|
3036 |
QCOMPARE(propertyHolder->property("a").toInt(), 3); |
|
3037 |
||
3038 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3039 |
QCoreApplication::processEvents(); |
|
3040 |
||
3041 |
// Enter s2, restorePolicy == DoNotRestore, so restore all properties |
|
3042 |
QCOMPARE(propertyHolder->property("a").toInt(), 4); |
|
3043 |
||
3044 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3045 |
QCoreApplication::processEvents(); |
|
3046 |
||
3047 |
// Enter s3 |
|
3048 |
QCOMPARE(propertyHolder->property("a").toInt(), 4); |
|
3049 |
||
3050 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3051 |
QCoreApplication::processEvents(); |
|
3052 |
||
3053 |
// Enter s4 |
|
3054 |
QCOMPARE(propertyHolder->property("a").toInt(), 5); |
|
3055 |
||
3056 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3057 |
QCoreApplication::processEvents(); |
|
3058 |
||
3059 |
// Enter s5, save current |
|
3060 |
QCOMPARE(propertyHolder->property("a").toInt(), 6); |
|
3061 |
||
3062 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3063 |
QCoreApplication::processEvents(); |
|
3064 |
||
3065 |
// Enter s3, restore |
|
3066 |
QCOMPARE(propertyHolder->property("a").toInt(), 5); |
|
3067 |
} |
|
3068 |
*/ |
|
3069 |
||
3070 |
void tst_QStateMachine::transitionWithParent() |
|
3071 |
{ |
|
3072 |
QStateMachine machine; |
|
3073 |
QState *s1 = new QState(&machine); |
|
3074 |
QState *s2 = new QState(&machine); |
|
3075 |
EventTransition *trans = new EventTransition(QEvent::User, s2, s1); |
|
3076 |
QCOMPARE(trans->sourceState(), s1); |
|
3077 |
QCOMPARE(trans->targetState(), (QAbstractState*)s2); |
|
3078 |
QCOMPARE(trans->targetStates().size(), 1); |
|
3079 |
QCOMPARE(trans->targetStates().at(0), (QAbstractState*)s2); |
|
3080 |
} |
|
3081 |
||
3082 |
void tst_QStateMachine::simpleAnimation() |
|
3083 |
{ |
|
3084 |
QStateMachine machine; |
|
3085 |
||
3086 |
QObject *object = new QObject(&machine); |
|
3087 |
object->setProperty("fooBar", 1.0); |
|
3088 |
||
3089 |
QState *s1 = new QState(&machine); |
|
3090 |
QState *s2 = new QState(&machine); |
|
3091 |
s2->assignProperty(object, "fooBar", 2.0); |
|
3092 |
||
3093 |
EventTransition *et = new EventTransition(QEvent::User, s2); |
|
3094 |
QPropertyAnimation *animation = new QPropertyAnimation(object, "fooBar", s2); |
|
3095 |
et->addAnimation(animation); |
|
3096 |
s1->addTransition(et); |
|
3097 |
||
3098 |
QState *s3 = new QState(&machine); |
|
3099 |
s2->addTransition(animation, SIGNAL(finished()), s3); |
|
3100 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3101 |
||
3102 |
machine.setInitialState(s1); |
|
3103 |
machine.start(); |
|
3104 |
QCoreApplication::processEvents(); |
|
3105 |
||
3106 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3107 |
QCOREAPPLICATION_EXEC(5000); |
|
3108 |
||
3109 |
QVERIFY(machine.configuration().contains(s3)); |
|
3110 |
QCOMPARE(object->property("fooBar").toDouble(), 2.0); |
|
3111 |
} |
|
3112 |
||
3113 |
class SlotCalledCounter: public QObject |
|
3114 |
{ |
|
3115 |
Q_OBJECT |
|
3116 |
public: |
|
3117 |
SlotCalledCounter() : counter(0) {} |
|
3118 |
||
3119 |
int counter; |
|
3120 |
||
3121 |
public slots: |
|
3122 |
void slot() { counter++; } |
|
3123 |
}; |
|
3124 |
||
3125 |
void tst_QStateMachine::twoAnimations() |
|
3126 |
{ |
|
3127 |
QStateMachine machine; |
|
3128 |
||
3129 |
QObject *object = new QObject(&machine); |
|
3130 |
object->setProperty("foo", 1.0); |
|
3131 |
object->setProperty("bar", 3.0); |
|
3132 |
||
3133 |
QState *s1 = new QState(&machine); |
|
3134 |
QState *s2 = new QState(&machine); |
|
3135 |
s2->assignProperty(object, "foo", 2.0); |
|
3136 |
s2->assignProperty(object, "bar", 10.0); |
|
3137 |
||
3138 |
QPropertyAnimation *animationFoo = new QPropertyAnimation(object, "foo", s2); |
|
3139 |
QPropertyAnimation *animationBar = new QPropertyAnimation(object, "bar", s2); |
|
3140 |
animationBar->setDuration(900); |
|
3141 |
||
3142 |
SlotCalledCounter counter; |
|
3143 |
connect(animationFoo, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3144 |
connect(animationBar, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3145 |
||
3146 |
EventTransition *et = new EventTransition(QEvent::User, s2); |
|
3147 |
et->addAnimation(animationFoo); |
|
3148 |
et->addAnimation(animationBar); |
|
3149 |
s1->addTransition(et); |
|
3150 |
||
3151 |
QState *s3 = new QState(&machine); |
|
3152 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3153 |
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); |
0 | 3154 |
|
3155 |
machine.setInitialState(s1); |
|
3156 |
machine.start(); |
|
3157 |
QCoreApplication::processEvents(); |
|
3158 |
||
3159 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3160 |
QCOREAPPLICATION_EXEC(5000); |
|
3161 |
||
3162 |
QVERIFY(machine.configuration().contains(s3)); |
|
3163 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3164 |
QCOMPARE(object->property("bar").toDouble(), 10.0); |
|
3165 |
||
3166 |
QCOMPARE(counter.counter, 2); |
|
3167 |
} |
|
3168 |
||
3169 |
void tst_QStateMachine::twoAnimatedTransitions() |
|
3170 |
{ |
|
3171 |
QStateMachine machine; |
|
3172 |
||
3173 |
QObject *object = new QObject(&machine); |
|
3174 |
object->setProperty("foo", 1.0); |
|
3175 |
||
3176 |
QState *s1 = new QState(&machine); |
|
3177 |
||
3178 |
QState *s2 = new QState(&machine); |
|
3179 |
s2->assignProperty(object, "foo", 5.0); |
|
3180 |
QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3181 |
EventTransition *trans = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3182 |
s1->addTransition(trans); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3183 |
trans->addAnimation(fooAnimation); |
0 | 3184 |
|
3185 |
QState *s3 = new QState(&machine); |
|
3186 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3187 |
s2->addTransition(fooAnimation, SIGNAL(finished()), s3); |
|
3188 |
||
3189 |
QState *s4 = new QState(&machine); |
|
3190 |
s4->assignProperty(object, "foo", 2.0); |
|
3191 |
QPropertyAnimation *fooAnimation2 = new QPropertyAnimation(object, "foo", s4); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3192 |
trans = new EventTransition(QEvent::User, s4); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3193 |
s3->addTransition(trans); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3194 |
trans->addAnimation(fooAnimation2); |
0 | 3195 |
|
3196 |
QState *s5 = new QState(&machine); |
|
3197 |
QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); |
|
3198 |
s4->addTransition(fooAnimation2, SIGNAL(finished()), s5); |
|
3199 |
||
3200 |
machine.setInitialState(s1); |
|
3201 |
machine.start(); |
|
3202 |
QCoreApplication::processEvents(); |
|
3203 |
||
3204 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3205 |
QCOREAPPLICATION_EXEC(5000); |
|
3206 |
||
3207 |
QVERIFY(machine.configuration().contains(s3)); |
|
3208 |
QCOMPARE(object->property("foo").toDouble(), 5.0); |
|
3209 |
||
3210 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3211 |
QCOREAPPLICATION_EXEC(5000); |
|
3212 |
||
3213 |
QVERIFY(machine.configuration().contains(s5)); |
|
3214 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3215 |
} |
|
3216 |
||
3217 |
void tst_QStateMachine::playAnimationTwice() |
|
3218 |
{ |
|
3219 |
QStateMachine machine; |
|
3220 |
||
3221 |
QObject *object = new QObject(&machine); |
|
3222 |
object->setProperty("foo", 1.0); |
|
3223 |
||
3224 |
QState *s1 = new QState(&machine); |
|
3225 |
||
3226 |
QState *s2 = new QState(&machine); |
|
3227 |
s2->assignProperty(object, "foo", 5.0); |
|
3228 |
QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3229 |
EventTransition *trans = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3230 |
s1->addTransition(trans); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3231 |
trans->addAnimation(fooAnimation); |
0 | 3232 |
|
3233 |
QState *s3 = new QState(&machine); |
|
3234 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3235 |
s2->addTransition(fooAnimation, SIGNAL(finished()), s3); |
|
3236 |
||
3237 |
QState *s4 = new QState(&machine); |
|
3238 |
s4->assignProperty(object, "foo", 2.0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3239 |
trans = new EventTransition(QEvent::User, s4); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3240 |
s3->addTransition(trans); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3241 |
trans->addAnimation(fooAnimation); |
0 | 3242 |
|
3243 |
QState *s5 = new QState(&machine); |
|
3244 |
QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); |
|
3245 |
s4->addTransition(fooAnimation, SIGNAL(finished()), s5); |
|
3246 |
||
3247 |
machine.setInitialState(s1); |
|
3248 |
machine.start(); |
|
3249 |
QCoreApplication::processEvents(); |
|
3250 |
||
3251 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3252 |
QCOREAPPLICATION_EXEC(5000); |
|
3253 |
||
3254 |
QVERIFY(machine.configuration().contains(s3)); |
|
3255 |
QCOMPARE(object->property("foo").toDouble(), 5.0); |
|
3256 |
||
3257 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3258 |
QCOREAPPLICATION_EXEC(5000); |
|
3259 |
||
3260 |
QVERIFY(machine.configuration().contains(s5)); |
|
3261 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3262 |
} |
|
3263 |
||
3264 |
void tst_QStateMachine::nestedTargetStateForAnimation() |
|
3265 |
{ |
|
3266 |
QStateMachine machine; |
|
3267 |
||
3268 |
QObject *object = new QObject(&machine); |
|
3269 |
object->setProperty("foo", 1.0); |
|
3270 |
object->setProperty("bar", 3.0); |
|
3271 |
||
3272 |
SlotCalledCounter counter; |
|
3273 |
||
3274 |
QState *s1 = new QState(&machine); |
|
3275 |
QState *s2 = new QState(&machine); |
|
3276 |
||
3277 |
s2->assignProperty(object, "foo", 2.0); |
|
3278 |
||
3279 |
QState *s2Child = new QState(s2); |
|
3280 |
s2Child->assignProperty(object, "bar", 10.0); |
|
3281 |
s2->setInitialState(s2Child); |
|
3282 |
||
3283 |
QState *s2Child2 = new QState(s2); |
|
3284 |
s2Child2->assignProperty(object, "bar", 11.0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3285 |
QAbstractTransition *at = new EventTransition(QEvent::User, s2Child2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3286 |
s2Child->addTransition(at); |
0 | 3287 |
|
3288 |
QPropertyAnimation *animation = new QPropertyAnimation(object, "bar", s2); |
|
3289 |
animation->setDuration(2000); |
|
3290 |
connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3291 |
at->addAnimation(animation); |
|
3292 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3293 |
at = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3294 |
s1->addTransition(at); |
0 | 3295 |
|
3296 |
animation = new QPropertyAnimation(object, "foo", s2); |
|
3297 |
connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3298 |
at->addAnimation(animation); |
|
3299 |
||
3300 |
animation = new QPropertyAnimation(object, "bar", s2); |
|
3301 |
connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3302 |
at->addAnimation(animation); |
|
3303 |
||
3304 |
QState *s3 = new QState(&machine); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3305 |
s2->addTransition(s2Child, SIGNAL(propertiesAssigned()), s3); |
0 | 3306 |
|
3307 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3308 |
||
3309 |
machine.setInitialState(s1); |
|
3310 |
machine.start(); |
|
3311 |
QCoreApplication::processEvents(); |
|
3312 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3313 |
||
3314 |
QCOREAPPLICATION_EXEC(5000); |
|
3315 |
||
3316 |
QVERIFY(machine.configuration().contains(s3)); |
|
3317 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3318 |
QCOMPARE(object->property("bar").toDouble(), 10.0); |
|
3319 |
QCOMPARE(counter.counter, 2); |
|
3320 |
} |
|
3321 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3322 |
void tst_QStateMachine::propertiesAssignedSignalTransitionsReuseAnimationGroup() |
0 | 3323 |
{ |
3324 |
QStateMachine machine; |
|
3325 |
QObject *object = new QObject(&machine); |
|
3326 |
object->setProperty("foo", 0); |
|
3327 |
||
3328 |
QState *s1 = new QState(&machine); |
|
3329 |
s1->assignProperty(object, "foo", 123); |
|
3330 |
QState *s2 = new QState(&machine); |
|
3331 |
s2->assignProperty(object, "foo", 456); |
|
3332 |
QState *s3 = new QState(&machine); |
|
3333 |
s3->assignProperty(object, "foo", 789); |
|
3334 |
QFinalState *s4 = new QFinalState(&machine); |
|
3335 |
||
3336 |
QParallelAnimationGroup animationGroup; |
|
3337 |
animationGroup.addAnimation(new QPropertyAnimation(object, "foo")); |
|
3338 |
QSignalSpy animationFinishedSpy(&animationGroup, SIGNAL(finished())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3339 |
s1->addTransition(s1, SIGNAL(propertiesAssigned()), s2)->addAnimation(&animationGroup); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3340 |
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3)->addAnimation(&animationGroup); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3341 |
s3->addTransition(s3, SIGNAL(propertiesAssigned()), s4); |
0 | 3342 |
|
3343 |
machine.setInitialState(s1); |
|
3344 |
QSignalSpy machineFinishedSpy(&machine, SIGNAL(finished())); |
|
3345 |
machine.start(); |
|
3346 |
QTRY_COMPARE(machineFinishedSpy.count(), 1); |
|
3347 |
QCOMPARE(machine.configuration().size(), 1); |
|
3348 |
QVERIFY(machine.configuration().contains(s4)); |
|
3349 |
QCOMPARE(object->property("foo").toInt(), 789); |
|
3350 |
||
3351 |
QCOMPARE(animationFinishedSpy.count(), 2); |
|
3352 |
} |
|
3353 |
||
3354 |
void tst_QStateMachine::animatedGlobalRestoreProperty() |
|
3355 |
{ |
|
3356 |
QStateMachine machine; |
|
3357 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
|
3358 |
||
3359 |
QObject *object = new QObject(&machine); |
|
3360 |
object->setProperty("foo", 1.0); |
|
3361 |
||
3362 |
SlotCalledCounter counter; |
|
3363 |
||
3364 |
QState *s1 = new QState(&machine); |
|
3365 |
QState *s2 = new QState(&machine); |
|
3366 |
s2->assignProperty(object, "foo", 2.0); |
|
3367 |
||
3368 |
QState *s3 = new QState(&machine); |
|
3369 |
||
3370 |
QState *s4 = new QState(&machine); |
|
3371 |
QObject::connect(s4, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3372 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3373 |
QAbstractTransition *at = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3374 |
s1->addTransition(at); |
0 | 3375 |
QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", s2); |
3376 |
connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3377 |
at->addAnimation(pa); |
|
3378 |
||
3379 |
at = s2->addTransition(pa, SIGNAL(finished()), s3); |
|
3380 |
pa = new QPropertyAnimation(object, "foo", s3); |
|
3381 |
connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3382 |
at->addAnimation(pa); |
|
3383 |
||
3384 |
at = s3->addTransition(pa, SIGNAL(finished()), s4); |
|
3385 |
pa = new QPropertyAnimation(object, "foo", s4); |
|
3386 |
connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3387 |
at->addAnimation(pa); |
|
3388 |
||
3389 |
machine.setInitialState(s1); |
|
3390 |
machine.start(); |
|
3391 |
QCoreApplication::processEvents(); |
|
3392 |
||
3393 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3394 |
||
3395 |
QCOREAPPLICATION_EXEC(5000); |
|
3396 |
||
3397 |
QVERIFY(machine.configuration().contains(s4)); |
|
3398 |
QCOMPARE(object->property("foo").toDouble(), 1.0); |
|
3399 |
QCOMPARE(counter.counter, 2); |
|
3400 |
} |
|
3401 |
||
3402 |
void tst_QStateMachine::specificTargetValueOfAnimation() |
|
3403 |
{ |
|
3404 |
QStateMachine machine; |
|
3405 |
||
3406 |
QObject *object = new QObject(&machine); |
|
3407 |
object->setProperty("foo", 1.0); |
|
3408 |
||
3409 |
QState *s1 = new QState(&machine); |
|
3410 |
||
3411 |
QState *s2 = new QState(&machine); |
|
3412 |
s2->assignProperty(object, "foo", 2.0); |
|
3413 |
||
3414 |
QPropertyAnimation *anim = new QPropertyAnimation(object, "foo"); |
|
3415 |
anim->setEndValue(10.0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3416 |
EventTransition *trans = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3417 |
s1->addTransition(trans); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3418 |
trans->addAnimation(anim); |
0 | 3419 |
|
3420 |
QState *s3 = new QState(&machine); |
|
3421 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3422 |
s2->addTransition(anim, SIGNAL(finished()), s3); |
|
3423 |
||
3424 |
machine.setInitialState(s1); |
|
3425 |
machine.start(); |
|
3426 |
QCoreApplication::processEvents(); |
|
3427 |
||
3428 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3429 |
QCOREAPPLICATION_EXEC(5000); |
|
3430 |
||
3431 |
QVERIFY(machine.configuration().contains(s3)); |
|
3432 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3433 |
QCOMPARE(anim->endValue().toDouble(), 10.0); |
|
3434 |
||
3435 |
delete anim; |
|
3436 |
} |
|
3437 |
||
3438 |
void tst_QStateMachine::addDefaultAnimation() |
|
3439 |
{ |
|
3440 |
QStateMachine machine; |
|
3441 |
||
3442 |
QObject *object = new QObject(); |
|
3443 |
object->setProperty("foo", 1.0); |
|
3444 |
||
3445 |
QState *s1 = new QState(&machine); |
|
3446 |
||
3447 |
QState *s2 = new QState(&machine); |
|
3448 |
s2->assignProperty(object, "foo", 2.0); |
|
3449 |
||
3450 |
QState *s3 = new QState(&machine); |
|
3451 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3452 |
||
3453 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3454 |
||
3455 |
QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); |
|
3456 |
machine.addDefaultAnimation(pa); |
|
3457 |
s2->addTransition(pa, SIGNAL(finished()), s3); |
|
3458 |
||
3459 |
machine.setInitialState(s1); |
|
3460 |
machine.start(); |
|
3461 |
QCoreApplication::processEvents(); |
|
3462 |
||
3463 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3464 |
QCOREAPPLICATION_EXEC(5000); |
|
3465 |
||
3466 |
QVERIFY(machine.configuration().contains(s3)); |
|
3467 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3468 |
||
3469 |
delete object; |
|
3470 |
} |
|
3471 |
||
3472 |
void tst_QStateMachine::addDefaultAnimationWithUnusedAnimation() |
|
3473 |
{ |
|
3474 |
QStateMachine machine; |
|
3475 |
||
3476 |
QObject *object = new QObject(&machine); |
|
3477 |
object->setProperty("foo", 1.0); |
|
3478 |
object->setProperty("bar", 2.0); |
|
3479 |
||
3480 |
SlotCalledCounter counter; |
|
3481 |
||
3482 |
QState *s1 = new QState(&machine); |
|
3483 |
||
3484 |
QState *s2 = new QState(&machine); |
|
3485 |
s2->assignProperty(object, "foo", 2.0); |
|
3486 |
||
3487 |
QState *s3 = new QState(&machine); |
|
3488 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3489 |
||
3490 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3491 |
||
3492 |
QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); |
|
3493 |
connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3494 |
machine.addDefaultAnimation(pa); |
|
3495 |
s2->addTransition(pa, SIGNAL(finished()), s3); |
|
3496 |
||
3497 |
pa = new QPropertyAnimation(object, "bar", &machine); |
|
3498 |
connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); |
|
3499 |
machine.addDefaultAnimation(pa); |
|
3500 |
||
3501 |
machine.setInitialState(s1); |
|
3502 |
machine.start(); |
|
3503 |
QCoreApplication::processEvents(); |
|
3504 |
||
3505 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3506 |
QCOREAPPLICATION_EXEC(5000); |
|
3507 |
||
3508 |
QVERIFY(machine.configuration().contains(s3)); |
|
3509 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3510 |
QCOMPARE(counter.counter, 1); |
|
3511 |
} |
|
3512 |
||
3513 |
void tst_QStateMachine::removeDefaultAnimation() |
|
3514 |
{ |
|
3515 |
QStateMachine machine; |
|
3516 |
||
3517 |
QObject propertyHolder; |
|
3518 |
propertyHolder.setProperty("foo", 0); |
|
3519 |
||
3520 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3521 |
||
3522 |
QPropertyAnimation *anim = new QPropertyAnimation(&propertyHolder, "foo"); |
|
3523 |
||
3524 |
machine.addDefaultAnimation(anim); |
|
3525 |
||
3526 |
QCOMPARE(machine.defaultAnimations().size(), 1); |
|
3527 |
QVERIFY(machine.defaultAnimations().contains(anim)); |
|
3528 |
||
3529 |
machine.removeDefaultAnimation(anim); |
|
3530 |
||
3531 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3532 |
||
3533 |
machine.addDefaultAnimation(anim); |
|
3534 |
||
3535 |
QPropertyAnimation *anim2 = new QPropertyAnimation(&propertyHolder, "foo"); |
|
3536 |
machine.addDefaultAnimation(anim2); |
|
3537 |
||
3538 |
QCOMPARE(machine.defaultAnimations().size(), 2); |
|
3539 |
QVERIFY(machine.defaultAnimations().contains(anim)); |
|
3540 |
QVERIFY(machine.defaultAnimations().contains(anim2)); |
|
3541 |
||
3542 |
machine.removeDefaultAnimation(anim); |
|
3543 |
||
3544 |
QCOMPARE(machine.defaultAnimations().size(), 1); |
|
3545 |
QVERIFY(machine.defaultAnimations().contains(anim2)); |
|
3546 |
||
3547 |
machine.removeDefaultAnimation(anim2); |
|
3548 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3549 |
||
3550 |
delete anim; |
|
3551 |
delete anim2; |
|
3552 |
} |
|
3553 |
||
3554 |
void tst_QStateMachine::overrideDefaultAnimationWithSpecific() |
|
3555 |
{ |
|
3556 |
QStateMachine machine; |
|
3557 |
||
3558 |
QObject *object = new QObject(&machine); |
|
3559 |
object->setProperty("foo", 1.0); |
|
3560 |
||
3561 |
SlotCalledCounter counter; |
|
3562 |
||
3563 |
QState *s1 = new QState(&machine); |
|
3564 |
machine.setInitialState(s1); |
|
3565 |
||
3566 |
QState *s2 = new QState(&machine); |
|
3567 |
s2->assignProperty(object, "foo", 2.0); |
|
3568 |
||
3569 |
QState *s3 = new QState(&machine); |
|
3570 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3571 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3572 |
QAbstractTransition *at = new EventTransition(QEvent::User, s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3573 |
s1->addTransition(at); |
0 | 3574 |
|
3575 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3576 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3577 |
||
3578 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3579 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3580 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3581 |
||
3582 |
machine.addDefaultAnimation(defaultAnimation); |
|
3583 |
at->addAnimation(moreSpecificAnimation); |
|
3584 |
||
3585 |
machine.start(); |
|
3586 |
QCoreApplication::processEvents(); |
|
3587 |
||
3588 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3589 |
QCOREAPPLICATION_EXEC(5000); |
|
3590 |
||
3591 |
QVERIFY(machine.configuration().contains(s3)); |
|
3592 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3593 |
||
3594 |
delete defaultAnimation; |
|
3595 |
delete moreSpecificAnimation; |
|
3596 |
} |
|
3597 |
||
3598 |
/* |
|
3599 |
void tst_QStateMachine::addDefaultAnimationForSource() |
|
3600 |
{ |
|
3601 |
QStateMachine machine; |
|
3602 |
||
3603 |
QObject *object = new QObject(); |
|
3604 |
object->setProperty("foo", 1.0); |
|
3605 |
||
3606 |
QState *s1 = new QState(&machine); |
|
3607 |
||
3608 |
QState *s2 = new QState(&machine); |
|
3609 |
s2->assignProperty(object, "foo", 2.0); |
|
3610 |
||
3611 |
QState *s3 = new QState(&machine); |
|
3612 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3613 |
||
3614 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3615 |
||
3616 |
QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); |
|
3617 |
machine.addDefaultAnimationForSourceState(s1, pa); |
|
3618 |
s2->addTransition(pa, SIGNAL(finished()), s3); |
|
3619 |
||
3620 |
machine.setInitialState(s1); |
|
3621 |
machine.start(); |
|
3622 |
QCoreApplication::processEvents(); |
|
3623 |
||
3624 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3625 |
QCOREAPPLICATION_EXEC(5000); |
|
3626 |
||
3627 |
QVERIFY(machine.configuration().contains(s3)); |
|
3628 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3629 |
} |
|
3630 |
||
3631 |
void tst_QStateMachine::addDefaultAnimationForTarget() |
|
3632 |
{ |
|
3633 |
QStateMachine machine; |
|
3634 |
||
3635 |
QObject *object = new QObject(); |
|
3636 |
object->setProperty("foo", 1.0); |
|
3637 |
||
3638 |
QState *s1 = new QState(&machine); |
|
3639 |
||
3640 |
QState *s2 = new QState(&machine); |
|
3641 |
s2->assignProperty(object, "foo", 2.0); |
|
3642 |
||
3643 |
QState *s3 = new QState(&machine); |
|
3644 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3645 |
||
3646 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3647 |
||
3648 |
QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); |
|
3649 |
machine.addDefaultAnimationForTargetState(s2, pa); |
|
3650 |
s2->addTransition(pa, SIGNAL(finished()), s3); |
|
3651 |
||
3652 |
machine.setInitialState(s1); |
|
3653 |
machine.start(); |
|
3654 |
QCoreApplication::processEvents(); |
|
3655 |
||
3656 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3657 |
QCOREAPPLICATION_EXEC(5000); |
|
3658 |
||
3659 |
QVERIFY(machine.configuration().contains(s3)); |
|
3660 |
QCOMPARE(object->property("foo").toDouble(), 2.0); |
|
3661 |
} |
|
3662 |
||
3663 |
void tst_QStateMachine::removeDefaultAnimationForSource() |
|
3664 |
{ |
|
3665 |
QStateMachine machine; |
|
3666 |
||
3667 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); |
|
3668 |
||
3669 |
QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); |
|
3670 |
||
3671 |
machine.addDefaultAnimationForSourceState(&machine, anim); |
|
3672 |
||
3673 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3674 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); |
|
3675 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); |
|
3676 |
QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); |
|
3677 |
||
3678 |
machine.removeDefaultAnimationForTargetState(&machine, anim); |
|
3679 |
||
3680 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3681 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); |
|
3682 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); |
|
3683 |
QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); |
|
3684 |
||
3685 |
machine.removeDefaultAnimationForSourceState(&machine, anim); |
|
3686 |
||
3687 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); |
|
3688 |
||
3689 |
machine.addDefaultAnimationForSourceState(&machine, anim); |
|
3690 |
||
3691 |
QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); |
|
3692 |
machine.addDefaultAnimationForSourceState(&machine, anim2); |
|
3693 |
||
3694 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 2); |
|
3695 |
QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim)); |
|
3696 |
QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim2)); |
|
3697 |
||
3698 |
machine.removeDefaultAnimationForSourceState(&machine, anim); |
|
3699 |
||
3700 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 1); |
|
3701 |
QVERIFY(machine.defaultAnimationsForSourceState(&machine).contains(anim2)); |
|
3702 |
||
3703 |
machine.removeDefaultAnimationForSourceState(&machine, anim2); |
|
3704 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); |
|
3705 |
} |
|
3706 |
||
3707 |
void tst_QStateMachine::removeDefaultAnimationForTarget() |
|
3708 |
{ |
|
3709 |
QStateMachine machine; |
|
3710 |
||
3711 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); |
|
3712 |
||
3713 |
QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); |
|
3714 |
||
3715 |
machine.addDefaultAnimationForTargetState(&machine, anim); |
|
3716 |
||
3717 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3718 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); |
|
3719 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); |
|
3720 |
QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); |
|
3721 |
||
3722 |
machine.removeDefaultAnimationForSourceState(&machine, anim); |
|
3723 |
||
3724 |
QCOMPARE(machine.defaultAnimations().size(), 0); |
|
3725 |
QCOMPARE(machine.defaultAnimationsForSourceState(&machine).size(), 0); |
|
3726 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); |
|
3727 |
QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); |
|
3728 |
||
3729 |
machine.removeDefaultAnimationForTargetState(&machine, anim); |
|
3730 |
||
3731 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); |
|
3732 |
||
3733 |
machine.addDefaultAnimationForTargetState(&machine, anim); |
|
3734 |
||
3735 |
QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); |
|
3736 |
machine.addDefaultAnimationForTargetState(&machine, anim2); |
|
3737 |
||
3738 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 2); |
|
3739 |
QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim)); |
|
3740 |
QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim2)); |
|
3741 |
||
3742 |
machine.removeDefaultAnimationForTargetState(&machine, anim); |
|
3743 |
||
3744 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 1); |
|
3745 |
QVERIFY(machine.defaultAnimationsForTargetState(&machine).contains(anim2)); |
|
3746 |
||
3747 |
machine.removeDefaultAnimationForTargetState(&machine, anim2); |
|
3748 |
QCOMPARE(machine.defaultAnimationsForTargetState(&machine).size(), 0); |
|
3749 |
} |
|
3750 |
||
3751 |
void tst_QStateMachine::overrideDefaultAnimationWithSource() |
|
3752 |
{ |
|
3753 |
QStateMachine machine; |
|
3754 |
||
3755 |
QObject *object = new QObject(); |
|
3756 |
object->setProperty("foo", 1.0); |
|
3757 |
||
3758 |
SlotCalledCounter counter; |
|
3759 |
||
3760 |
QState *s1 = new QState(&machine); |
|
3761 |
machine.setInitialState(s1); |
|
3762 |
||
3763 |
QState *s2 = new QState(&machine); |
|
3764 |
s2->assignProperty(object, "foo", 2.0); |
|
3765 |
||
3766 |
QState *s3 = new QState(&machine); |
|
3767 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3768 |
||
3769 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3770 |
||
3771 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3772 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3773 |
||
3774 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3775 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3776 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3777 |
||
3778 |
machine.addDefaultAnimation(defaultAnimation); |
|
3779 |
machine.addDefaultAnimationForSourceState(s1, moreSpecificAnimation); |
|
3780 |
||
3781 |
machine.start(); |
|
3782 |
QCoreApplication::processEvents(); |
|
3783 |
||
3784 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3785 |
QCOREAPPLICATION_EXEC(5000); |
|
3786 |
||
3787 |
QVERIFY(machine.configuration().contains(s3)); |
|
3788 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3789 |
} |
|
3790 |
||
3791 |
void tst_QStateMachine::overrideDefaultAnimationWithTarget() |
|
3792 |
{ |
|
3793 |
QStateMachine machine; |
|
3794 |
||
3795 |
QObject *object = new QObject(); |
|
3796 |
object->setProperty("foo", 1.0); |
|
3797 |
||
3798 |
SlotCalledCounter counter; |
|
3799 |
||
3800 |
QState *s1 = new QState(&machine); |
|
3801 |
machine.setInitialState(s1); |
|
3802 |
||
3803 |
QState *s2 = new QState(&machine); |
|
3804 |
s2->assignProperty(object, "foo", 2.0); |
|
3805 |
||
3806 |
QState *s3 = new QState(&machine); |
|
3807 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3808 |
||
3809 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3810 |
||
3811 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3812 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3813 |
||
3814 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3815 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3816 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3817 |
||
3818 |
machine.addDefaultAnimation(defaultAnimation); |
|
3819 |
machine.addDefaultAnimationForTargetState(s2, moreSpecificAnimation); |
|
3820 |
||
3821 |
machine.start(); |
|
3822 |
QCoreApplication::processEvents(); |
|
3823 |
||
3824 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3825 |
QCOREAPPLICATION_EXEC(5000); |
|
3826 |
||
3827 |
QVERIFY(machine.configuration().contains(s3)); |
|
3828 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3829 |
||
3830 |
} |
|
3831 |
||
3832 |
void tst_QStateMachine::overrideDefaultSourceAnimationWithSpecific() |
|
3833 |
{ |
|
3834 |
QStateMachine machine; |
|
3835 |
||
3836 |
QObject *object = new QObject(); |
|
3837 |
object->setProperty("foo", 1.0); |
|
3838 |
||
3839 |
SlotCalledCounter counter; |
|
3840 |
||
3841 |
QState *s1 = new QState(&machine); |
|
3842 |
machine.setInitialState(s1); |
|
3843 |
||
3844 |
QState *s2 = new QState(&machine); |
|
3845 |
s2->assignProperty(object, "foo", 2.0); |
|
3846 |
||
3847 |
QState *s3 = new QState(&machine); |
|
3848 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3849 |
||
3850 |
QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3851 |
||
3852 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3853 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3854 |
||
3855 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3856 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3857 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3858 |
||
3859 |
machine.addDefaultAnimationForSourceState(s1, defaultAnimation); |
|
3860 |
at->addAnimation(moreSpecificAnimation); |
|
3861 |
||
3862 |
machine.start(); |
|
3863 |
QCoreApplication::processEvents(); |
|
3864 |
||
3865 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3866 |
QCOREAPPLICATION_EXEC(5000); |
|
3867 |
||
3868 |
QVERIFY(machine.configuration().contains(s3)); |
|
3869 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3870 |
} |
|
3871 |
||
3872 |
void tst_QStateMachine::overrideDefaultTargetAnimationWithSpecific() |
|
3873 |
{ |
|
3874 |
QStateMachine machine; |
|
3875 |
||
3876 |
QObject *object = new QObject(); |
|
3877 |
object->setProperty("foo", 1.0); |
|
3878 |
||
3879 |
SlotCalledCounter counter; |
|
3880 |
||
3881 |
QState *s1 = new QState(&machine); |
|
3882 |
machine.setInitialState(s1); |
|
3883 |
||
3884 |
QState *s2 = new QState(&machine); |
|
3885 |
s2->assignProperty(object, "foo", 2.0); |
|
3886 |
||
3887 |
QState *s3 = new QState(&machine); |
|
3888 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3889 |
||
3890 |
QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3891 |
||
3892 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3893 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3894 |
||
3895 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3896 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3897 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3898 |
||
3899 |
machine.addDefaultAnimationForTargetState(s2, defaultAnimation); |
|
3900 |
at->addAnimation(moreSpecificAnimation); |
|
3901 |
||
3902 |
machine.start(); |
|
3903 |
QCoreApplication::processEvents(); |
|
3904 |
||
3905 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3906 |
QCOREAPPLICATION_EXEC(5000); |
|
3907 |
||
3908 |
QVERIFY(machine.configuration().contains(s3)); |
|
3909 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3910 |
} |
|
3911 |
||
3912 |
void tst_QStateMachine::overrideDefaultTargetAnimationWithSource() |
|
3913 |
{ |
|
3914 |
QStateMachine machine; |
|
3915 |
||
3916 |
QObject *object = new QObject(); |
|
3917 |
object->setProperty("foo", 1.0); |
|
3918 |
||
3919 |
SlotCalledCounter counter; |
|
3920 |
||
3921 |
QState *s1 = new QState(&machine); |
|
3922 |
machine.setInitialState(s1); |
|
3923 |
||
3924 |
QState *s2 = new QState(&machine); |
|
3925 |
s2->assignProperty(object, "foo", 2.0); |
|
3926 |
||
3927 |
QState *s3 = new QState(&machine); |
|
3928 |
QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); |
|
3929 |
||
3930 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
3931 |
||
3932 |
QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); |
|
3933 |
connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3934 |
||
3935 |
QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); |
|
3936 |
s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); |
|
3937 |
connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); |
|
3938 |
||
3939 |
machine.addDefaultAnimationForTargetState(s2, defaultAnimation); |
|
3940 |
machine.addDefaultAnimationForSourceState(s1, moreSpecificAnimation); |
|
3941 |
||
3942 |
machine.start(); |
|
3943 |
QCoreApplication::processEvents(); |
|
3944 |
||
3945 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3946 |
QCOREAPPLICATION_EXEC(5000); |
|
3947 |
||
3948 |
QVERIFY(machine.configuration().contains(s3)); |
|
3949 |
QCOMPARE(counter.counter, 2); // specific animation started and stopped |
|
3950 |
} |
|
3951 |
||
3952 |
*/ |
|
3953 |
||
3954 |
void tst_QStateMachine::parallelStateAssignmentsDone() |
|
3955 |
{ |
|
3956 |
QStateMachine machine; |
|
3957 |
||
3958 |
QObject *propertyHolder = new QObject(&machine); |
|
3959 |
propertyHolder->setProperty("foo", 123); |
|
3960 |
propertyHolder->setProperty("bar", 456); |
|
3961 |
propertyHolder->setProperty("zoot", 789); |
|
3962 |
||
3963 |
QState *s1 = new QState(&machine); |
|
3964 |
machine.setInitialState(s1); |
|
3965 |
||
3966 |
QState *parallelState = new QState(QState::ParallelStates, &machine); |
|
3967 |
parallelState->assignProperty(propertyHolder, "foo", 321); |
|
3968 |
||
3969 |
QState *s2 = new QState(parallelState); |
|
3970 |
s2->assignProperty(propertyHolder, "bar", 654); |
|
3971 |
||
3972 |
QState *s3 = new QState(parallelState); |
|
3973 |
s3->assignProperty(propertyHolder, "zoot", 987); |
|
3974 |
||
3975 |
s1->addTransition(new EventTransition(QEvent::User, parallelState)); |
|
3976 |
machine.start(); |
|
3977 |
QCoreApplication::processEvents(); |
|
3978 |
||
3979 |
QCOMPARE(propertyHolder->property("foo").toInt(), 123); |
|
3980 |
QCOMPARE(propertyHolder->property("bar").toInt(), 456); |
|
3981 |
QCOMPARE(propertyHolder->property("zoot").toInt(), 789); |
|
3982 |
||
3983 |
machine.postEvent(new QEvent(QEvent::User)); |
|
3984 |
QCoreApplication::processEvents(); |
|
3985 |
||
3986 |
QCOMPARE(propertyHolder->property("foo").toInt(), 321); |
|
3987 |
QCOMPARE(propertyHolder->property("bar").toInt(), 654); |
|
3988 |
QCOMPARE(propertyHolder->property("zoot").toInt(), 987); |
|
3989 |
} |
|
3990 |
||
3991 |
void tst_QStateMachine::transitionsFromParallelStateWithNoChildren() |
|
3992 |
{ |
|
3993 |
QStateMachine machine; |
|
3994 |
||
3995 |
QState *parallelState = new QState(QState::ParallelStates, &machine); |
|
3996 |
machine.setInitialState(parallelState); |
|
3997 |
||
3998 |
QState *s1 = new QState(&machine); |
|
3999 |
parallelState->addTransition(new EventTransition(QEvent::User, s1)); |
|
4000 |
||
4001 |
machine.start(); |
|
4002 |
QCoreApplication::processEvents(); |
|
4003 |
||
4004 |
QCOMPARE(1, machine.configuration().size()); |
|
4005 |
QVERIFY(machine.configuration().contains(parallelState)); |
|
4006 |
||
4007 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4008 |
||
4009 |
QCoreApplication::processEvents(); |
|
4010 |
||
4011 |
QCOMPARE(1, machine.configuration().size()); |
|
4012 |
QVERIFY(machine.configuration().contains(s1)); |
|
4013 |
} |
|
4014 |
||
4015 |
void tst_QStateMachine::parallelStateTransition() |
|
4016 |
{ |
|
4017 |
QStateMachine machine; |
|
4018 |
||
4019 |
QState *parallelState = new QState(QState::ParallelStates, &machine); |
|
4020 |
machine.setInitialState(parallelState); |
|
4021 |
||
4022 |
QState *s1 = new QState(parallelState); |
|
4023 |
QState *s2 = new QState(parallelState); |
|
4024 |
||
4025 |
QState *s1InitialChild = new QState(s1); |
|
4026 |
s1->setInitialState(s1InitialChild); |
|
4027 |
||
4028 |
QState *s2InitialChild = new QState(s2); |
|
4029 |
s2->setInitialState(s2InitialChild); |
|
4030 |
||
4031 |
QState *s1OtherChild = new QState(s1); |
|
4032 |
||
4033 |
s1->addTransition(new EventTransition(QEvent::User, s1OtherChild)); |
|
4034 |
||
4035 |
machine.start(); |
|
4036 |
QCoreApplication::processEvents(); |
|
4037 |
||
4038 |
QVERIFY(machine.configuration().contains(parallelState)); |
|
4039 |
QVERIFY(machine.configuration().contains(s1)); |
|
4040 |
QVERIFY(machine.configuration().contains(s2)); |
|
4041 |
QVERIFY(machine.configuration().contains(s1InitialChild)); |
|
4042 |
QVERIFY(machine.configuration().contains(s2InitialChild)); |
|
4043 |
QCOMPARE(machine.configuration().size(), 5); |
|
4044 |
||
4045 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4046 |
QCoreApplication::processEvents(); |
|
4047 |
||
4048 |
QVERIFY(machine.configuration().contains(parallelState)); |
|
4049 |
||
4050 |
QVERIFY(machine.configuration().contains(s1)); |
|
4051 |
||
4052 |
QVERIFY(machine.configuration().contains(s2)); |
|
4053 |
QVERIFY(machine.configuration().contains(s1OtherChild)); |
|
4054 |
QVERIFY(machine.configuration().contains(s2InitialChild)); |
|
4055 |
QCOMPARE(machine.configuration().size(), 5); |
|
4056 |
||
4057 |
} |
|
4058 |
||
4059 |
void tst_QStateMachine::nestedRestoreProperties() |
|
4060 |
{ |
|
4061 |
QStateMachine machine; |
|
4062 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
|
4063 |
||
4064 |
QObject *propertyHolder = new QObject(&machine); |
|
4065 |
propertyHolder->setProperty("foo", 1); |
|
4066 |
propertyHolder->setProperty("bar", 2); |
|
4067 |
||
4068 |
QState *s1 = new QState(&machine); |
|
4069 |
machine.setInitialState(s1); |
|
4070 |
||
4071 |
QState *s2 = new QState(&machine); |
|
4072 |
s2->assignProperty(propertyHolder, "foo", 3); |
|
4073 |
||
4074 |
QState *s21 = new QState(s2); |
|
4075 |
s21->assignProperty(propertyHolder, "bar", 4); |
|
4076 |
s2->setInitialState(s21); |
|
4077 |
||
4078 |
QState *s22 = new QState(s2); |
|
4079 |
s22->assignProperty(propertyHolder, "bar", 5); |
|
4080 |
||
4081 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
4082 |
s21->addTransition(new EventTransition(QEvent::User, s22)); |
|
4083 |
||
4084 |
machine.start(); |
|
4085 |
QCoreApplication::processEvents(); |
|
4086 |
||
4087 |
QCOMPARE(machine.configuration().size(), 1); |
|
4088 |
QVERIFY(machine.configuration().contains(s1)); |
|
4089 |
QCOMPARE(propertyHolder->property("foo").toInt(), 1); |
|
4090 |
QCOMPARE(propertyHolder->property("bar").toInt(), 2); |
|
4091 |
||
4092 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4093 |
QCoreApplication::processEvents(); |
|
4094 |
||
4095 |
QCOMPARE(machine.configuration().size(), 2); |
|
4096 |
QVERIFY(machine.configuration().contains(s2)); |
|
4097 |
QVERIFY(machine.configuration().contains(s21)); |
|
4098 |
QCOMPARE(propertyHolder->property("foo").toInt(), 3); |
|
4099 |
QCOMPARE(propertyHolder->property("bar").toInt(), 4); |
|
4100 |
||
4101 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4102 |
QCoreApplication::processEvents(); |
|
4103 |
||
4104 |
QCOMPARE(machine.configuration().size(), 2); |
|
4105 |
QVERIFY(machine.configuration().contains(s2)); |
|
4106 |
QVERIFY(machine.configuration().contains(s22)); |
|
4107 |
QCOMPARE(propertyHolder->property("foo").toInt(), 3); |
|
4108 |
QCOMPARE(propertyHolder->property("bar").toInt(), 5); |
|
4109 |
} |
|
4110 |
||
4111 |
void tst_QStateMachine::nestedRestoreProperties2() |
|
4112 |
{ |
|
4113 |
QStateMachine machine; |
|
4114 |
machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); |
|
4115 |
||
4116 |
QObject *propertyHolder = new QObject(&machine); |
|
4117 |
propertyHolder->setProperty("foo", 1); |
|
4118 |
propertyHolder->setProperty("bar", 2); |
|
4119 |
||
4120 |
QState *s1 = new QState(&machine); |
|
4121 |
machine.setInitialState(s1); |
|
4122 |
||
4123 |
QState *s2 = new QState(&machine); |
|
4124 |
s2->assignProperty(propertyHolder, "foo", 3); |
|
4125 |
||
4126 |
QState *s21 = new QState(s2); |
|
4127 |
s21->assignProperty(propertyHolder, "bar", 4); |
|
4128 |
s2->setInitialState(s21); |
|
4129 |
||
4130 |
QState *s22 = new QState(s2); |
|
4131 |
s22->assignProperty(propertyHolder, "foo", 6); |
|
4132 |
s22->assignProperty(propertyHolder, "bar", 5); |
|
4133 |
||
4134 |
s1->addTransition(new EventTransition(QEvent::User, s2)); |
|
4135 |
s21->addTransition(new EventTransition(QEvent::User, s22)); |
|
4136 |
s22->addTransition(new EventTransition(QEvent::User, s21)); |
|
4137 |
||
4138 |
machine.start(); |
|
4139 |
QCoreApplication::processEvents(); |
|
4140 |
||
4141 |
QCOMPARE(machine.configuration().size(), 1); |
|
4142 |
QVERIFY(machine.configuration().contains(s1)); |
|
4143 |
QCOMPARE(propertyHolder->property("foo").toInt(), 1); |
|
4144 |
QCOMPARE(propertyHolder->property("bar").toInt(), 2); |
|
4145 |
||
4146 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4147 |
QCoreApplication::processEvents(); |
|
4148 |
||
4149 |
QCOMPARE(machine.configuration().size(), 2); |
|
4150 |
QVERIFY(machine.configuration().contains(s2)); |
|
4151 |
QVERIFY(machine.configuration().contains(s21)); |
|
4152 |
QCOMPARE(propertyHolder->property("foo").toInt(), 3); |
|
4153 |
QCOMPARE(propertyHolder->property("bar").toInt(), 4); |
|
4154 |
||
4155 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4156 |
QCoreApplication::processEvents(); |
|
4157 |
||
4158 |
QCOMPARE(machine.configuration().size(), 2); |
|
4159 |
QVERIFY(machine.configuration().contains(s2)); |
|
4160 |
QVERIFY(machine.configuration().contains(s22)); |
|
4161 |
QCOMPARE(propertyHolder->property("foo").toInt(), 6); |
|
4162 |
QCOMPARE(propertyHolder->property("bar").toInt(), 5); |
|
4163 |
||
4164 |
machine.postEvent(new QEvent(QEvent::User)); |
|
4165 |
QCoreApplication::processEvents(); |
|
4166 |
||
4167 |
QCOMPARE(machine.configuration().size(), 2); |
|
4168 |
QVERIFY(machine.configuration().contains(s2)); |
|
4169 |
QVERIFY(machine.configuration().contains(s21)); |
|
4170 |
QCOMPARE(propertyHolder->property("foo").toInt(), 3); |
|
4171 |
QCOMPARE(propertyHolder->property("bar").toInt(), 4); |
|
4172 |
||
4173 |
} |
|
4174 |
||
4175 |
void tst_QStateMachine::nestedStateMachines() |
|
4176 |
{ |
|
4177 |
QStateMachine machine; |
|
4178 |
QState *group = new QState(&machine); |
|
4179 |
group->setChildMode(QState::ParallelStates); |
|
4180 |
QStateMachine *subMachines[3]; |
|
4181 |
for (int i = 0; i < 3; ++i) { |
|
4182 |
QState *subGroup = new QState(group); |
|
4183 |
QStateMachine *subMachine = new QStateMachine(subGroup); |
|
4184 |
{ |
|
4185 |
QState *initial = new QState(subMachine); |
|
4186 |
QFinalState *done = new QFinalState(subMachine); |
|
4187 |
initial->addTransition(new EventTransition(QEvent::User, done)); |
|
4188 |
subMachine->setInitialState(initial); |
|
4189 |
} |
|
4190 |
QFinalState *subMachineDone = new QFinalState(subGroup); |
|
4191 |
subMachine->addTransition(subMachine, SIGNAL(finished()), subMachineDone); |
|
4192 |
subGroup->setInitialState(subMachine); |
|
4193 |
subMachines[i] = subMachine; |
|
4194 |
} |
|
4195 |
QFinalState *final = new QFinalState(&machine); |
|
4196 |
group->addTransition(group, SIGNAL(finished()), final); |
|
4197 |
machine.setInitialState(group); |
|
4198 |
||
4199 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
4200 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
|
4201 |
machine.start(); |
|
4202 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
4203 |
QTRY_COMPARE(machine.configuration().count(), 1+2*3); |
|
4204 |
QVERIFY(machine.configuration().contains(group)); |
|
4205 |
for (int i = 0; i < 3; ++i) |
|
4206 |
QVERIFY(machine.configuration().contains(subMachines[i])); |
|
4207 |
||
4208 |
QCoreApplication::processEvents(); // starts the submachines |
|
4209 |
||
4210 |
for (int i = 0; i < 3; ++i) |
|
4211 |
subMachines[i]->postEvent(new QEvent(QEvent::User)); |
|
4212 |
||
4213 |
QTRY_COMPARE(finishedSpy.count(), 1); |
|
4214 |
} |
|
4215 |
||
4216 |
void tst_QStateMachine::goToState() |
|
4217 |
{ |
|
4218 |
QStateMachine machine; |
|
4219 |
QState *s1 = new QState(&machine); |
|
4220 |
QState *s2 = new QState(&machine); |
|
4221 |
machine.setInitialState(s1); |
|
4222 |
QSignalSpy startedSpy(&machine, SIGNAL(started())); |
|
4223 |
machine.start(); |
|
4224 |
QTRY_COMPARE(startedSpy.count(), 1); |
|
4225 |
||
4226 |
QStateMachinePrivate::get(&machine)->goToState(s2); |
|
4227 |
QCoreApplication::processEvents(); |
|
4228 |
QCOMPARE(machine.configuration().size(), 1); |
|
4229 |
QVERIFY(machine.configuration().contains(s2)); |
|
4230 |
||
4231 |
QStateMachinePrivate::get(&machine)->goToState(s2); |
|
4232 |
QCoreApplication::processEvents(); |
|
4233 |
QCOMPARE(machine.configuration().size(), 1); |
|
4234 |
QVERIFY(machine.configuration().contains(s2)); |
|
4235 |
||
4236 |
QStateMachinePrivate::get(&machine)->goToState(s1); |
|
4237 |
QStateMachinePrivate::get(&machine)->goToState(s2); |
|
4238 |
QStateMachinePrivate::get(&machine)->goToState(s1); |
|
4239 |
QCOMPARE(machine.configuration().size(), 1); |
|
4240 |
QVERIFY(machine.configuration().contains(s2)); |
|
4241 |
||
4242 |
QCoreApplication::processEvents(); |
|
4243 |
QCOMPARE(machine.configuration().size(), 1); |
|
4244 |
QVERIFY(machine.configuration().contains(s1)); |
|
4245 |
||
4246 |
// go to state in group |
|
4247 |
QState *s2_1 = new QState(s2); |
|
4248 |
s2->setInitialState(s2_1); |
|
4249 |
QStateMachinePrivate::get(&machine)->goToState(s2_1); |
|
4250 |
QCoreApplication::processEvents(); |
|
4251 |
QCOMPARE(machine.configuration().size(), 2); |
|
4252 |
QVERIFY(machine.configuration().contains(s2)); |
|
4253 |
QVERIFY(machine.configuration().contains(s2_1)); |
|
4254 |
} |
|
4255 |
||
4256 |
class CloneSignalTransition : public QSignalTransition |
|
4257 |
{ |
|
4258 |
public: |
|
4259 |
CloneSignalTransition(QObject *sender, const char *signal, QAbstractState *target) |
|
4260 |
: QSignalTransition(sender, signal) |
|
4261 |
{ |
|
4262 |
setTargetState(target); |
|
4263 |
} |
|
4264 |
||
4265 |
void onTransition(QEvent *e) |
|
4266 |
{ |
|
4267 |
QSignalTransition::onTransition(e); |
|
4268 |
QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e); |
|
4269 |
eventSignalIndex = se->signalIndex(); |
|
4270 |
} |
|
4271 |
||
4272 |
int eventSignalIndex; |
|
4273 |
}; |
|
4274 |
||
4275 |
void tst_QStateMachine::task260403_clonedSignals() |
|
4276 |
{ |
|
4277 |
SignalEmitter emitter; |
|
4278 |
QStateMachine machine; |
|
4279 |
QState *s1 = new QState(&machine); |
|
4280 |
QState *s2 = new QState(&machine); |
|
4281 |
CloneSignalTransition *t1 = new CloneSignalTransition(&emitter, SIGNAL(signalWithDefaultArg()), s2); |
|
4282 |
s1->addTransition(t1); |
|
4283 |
||
4284 |
machine.setInitialState(s1); |
|
4285 |
machine.start(); |
|
4286 |
QTest::qWait(1); |
|
4287 |
||
4288 |
emitter.emitSignalWithDefaultArg(); |
|
4289 |
QTest::qWait(1); |
|
4290 |
QCOMPARE(t1->eventSignalIndex, emitter.metaObject()->indexOfSignal("signalWithDefaultArg()")); |
|
4291 |
} |
|
4292 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4293 |
class EventPosterThread : public QThread |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4294 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4295 |
Q_OBJECT |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4296 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4297 |
EventPosterThread(QStateMachine *machine, QObject *parent = 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4298 |
: QThread(parent), m_machine(machine), m_count(0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4299 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4300 |
moveToThread(this); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4301 |
QObject::connect(m_machine, SIGNAL(started()), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4302 |
this, SLOT(postEvent())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4303 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4304 |
protected: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4305 |
virtual void run() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4306 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4307 |
exec(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4308 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4309 |
private Q_SLOTS: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4310 |
void postEvent() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4311 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4312 |
m_machine->postEvent(new QEvent(QEvent::User)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4313 |
if (++m_count < 10000) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4314 |
QTimer::singleShot(0, this, SLOT(postEvent())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4315 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4316 |
quit(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4317 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4318 |
private: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4319 |
QStateMachine *m_machine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4320 |
int m_count; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4321 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4322 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4323 |
void tst_QStateMachine::postEventFromOtherThread() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4324 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4325 |
QStateMachine machine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4326 |
EventPosterThread poster(&machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4327 |
StringEventPoster *s1 = new StringEventPoster("foo", &machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4328 |
s1->addTransition(new EventTransition(QEvent::User, s1)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4329 |
QFinalState *f = new QFinalState(&machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4330 |
s1->addTransition(&poster, SIGNAL(finished()), f); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4331 |
machine.setInitialState(s1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4332 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4333 |
poster.start(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4334 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4335 |
QSignalSpy finishedSpy(&machine, SIGNAL(finished())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4336 |
machine.start(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4337 |
QTRY_COMPARE(finishedSpy.count(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4338 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4339 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4340 |
void tst_QStateMachine::eventFilterForApplication() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4341 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4342 |
QStateMachine machine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4343 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4344 |
QState *s1 = new QState(&machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4345 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4346 |
machine.setInitialState(s1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4347 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4348 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4349 |
QState *s2 = new QState(&machine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4350 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4351 |
QEventTransition *transition = new QEventTransition(QCoreApplication::instance(), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4352 |
QEvent::ApplicationActivate); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4353 |
transition->setTargetState(s2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4354 |
s1->addTransition(transition); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4355 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4356 |
machine.start(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4357 |
QCoreApplication::processEvents(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4358 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4359 |
QCOMPARE(machine.configuration().size(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4360 |
QVERIFY(machine.configuration().contains(s1)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4361 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4362 |
QCoreApplication::postEvent(QCoreApplication::instance(), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4363 |
new QEvent(QEvent::ApplicationActivate)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4364 |
QCoreApplication::processEvents(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4365 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4366 |
QCOMPARE(machine.configuration().size(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4367 |
QVERIFY(machine.configuration().contains(s2)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4368 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4369 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4370 |
void tst_QStateMachine::eventClassesExported() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4371 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4372 |
// make sure this links |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4373 |
QStateMachine::WrappedEvent *wrappedEvent = new QStateMachine::WrappedEvent(0, 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4374 |
QStateMachine::SignalEvent *signalEvent = new QStateMachine::SignalEvent(0, 0, QList<QVariant>()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4375 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4376 |
|
0 | 4377 |
QTEST_MAIN(tst_QStateMachine) |
4378 |
#include "tst_qstatemachine.moc" |