1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include "qstate.h"
#include "qstatemachine.h"
#include "qsignaltransition.h"
class tst_QState : public QObject
{
Q_OBJECT
private slots:
void assignProperty();
void assignPropertyTwice();
void historyInitialState();
void transitions();
void privateSignals();
void parallelStateAndInitialState();
};
class TestClass: public QObject
{
Q_OBJECT
public:
TestClass() : called(false) {}
bool called;
public slots:
void slot() { called = true; }
};
void tst_QState::assignProperty()
{
QStateMachine machine;
QObject object;
object.setProperty("fooBar", 10);
QState *s1 = new QState(&machine);
s1->assignProperty(&object, "fooBar", 20);
machine.setInitialState(s1);
machine.start();
QCoreApplication::processEvents();
QCOMPARE(object.property("fooBar").toInt(), 20);
}
void tst_QState::assignPropertyTwice()
{
QStateMachine machine;
QObject object;
object.setProperty("fooBar", 10);
QState *s1 = new QState(&machine);
s1->assignProperty(&object, "fooBar", 20);
s1->assignProperty(&object, "fooBar", 30);
machine.setInitialState(s1);
machine.start();
QCoreApplication::processEvents();
QCOMPARE(object.property("fooBar").toInt(), 30);
}
class EventTestTransition: public QAbstractTransition
{
public:
EventTestTransition(QEvent::Type type, QState *targetState)
: QAbstractTransition(), m_type(type)
{
setTargetState(targetState);
}
protected:
bool eventTest(QEvent *e)
{
return e->type() == m_type;
}
void onTransition(QEvent *) {}
private:
QEvent::Type m_type;
};
void tst_QState::historyInitialState()
{
QStateMachine machine;
QState *s1 = new QState(&machine);
QState *s2 = new QState(&machine);
QHistoryState *h1 = new QHistoryState(s2);
s2->setInitialState(h1);
QState *s3 = new QState(s2);
h1->setDefaultState(s3);
QState *s4 = new QState(s2);
s1->addTransition(new EventTestTransition(QEvent::User, s2));
s2->addTransition(new EventTestTransition(QEvent::User, s1));
s3->addTransition(new EventTestTransition(QEvent::Type(QEvent::User+1), s4));
machine.setInitialState(s1);
machine.start();
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1));
machine.postEvent(new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 2);
QVERIFY(machine.configuration().contains(s2));
QVERIFY(machine.configuration().contains(s3));
machine.postEvent(new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1));
machine.postEvent(new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 2);
QVERIFY(machine.configuration().contains(s2));
QVERIFY(machine.configuration().contains(s3));
machine.postEvent(new QEvent(QEvent::Type(QEvent::User+1)));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 2);
QVERIFY(machine.configuration().contains(s2));
QVERIFY(machine.configuration().contains(s4));
machine.postEvent(new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1));
machine.postEvent(new QEvent(QEvent::User));
QCoreApplication::processEvents();
QCOMPARE(machine.configuration().size(), 2);
QVERIFY(machine.configuration().contains(s2));
QVERIFY(machine.configuration().contains(s4));
}
void tst_QState::transitions()
{
QState s1;
QState s2;
QVERIFY(s1.transitions().isEmpty());
QAbstractTransition *t1 = s1.addTransition(this, SIGNAL(destroyed()), &s2);
QAbstractTransition *t1_1 = s1.addTransition(this, &tst_QState::destroyed, &s2);
QVERIFY(t1 != 0);
QVERIFY(t1_1 != 0);
QCOMPARE(s1.transitions().count(), 2);
QCOMPARE(s1.transitions().first(), t1);
QCOMPARE(s1.transitions().last(), t1_1);
QVERIFY(s2.transitions().isEmpty());
s1.removeTransition(t1);
s1.removeTransition(t1_1);
QVERIFY(s1.transitions().isEmpty());
s1.addTransition(t1);
QCOMPARE(s1.transitions().count(), 1);
QCOMPARE(s1.transitions().first(), t1);
QAbstractTransition *t2 = new QEventTransition(&s1);
QCOMPARE(s1.transitions().count(), 2);
QVERIFY(s1.transitions().contains(t1));
QVERIFY(s1.transitions().contains(t2));
// Transitions from child states should not be reported.
QState *s21 = new QState(&s2);
QAbstractTransition *t3 = s21->addTransition(this, SIGNAL(destroyed()), &s2);
QVERIFY(s2.transitions().isEmpty());
QCOMPARE(s21->transitions().count(), 1);
QCOMPARE(s21->transitions().first(), t3);
}
class MyState : public QState
{
Q_OBJECT
public:
MyState(QState *parent = 0)
: QState(parent)
{
}
void emitPrivateSignals()
{
// These deliberately do not compile
// emit entered();
// emit exited();
//
// emit entered(QPrivateSignal());
// emit exited(QPrivateSignal());
//
// emit entered(QAbstractState::QPrivateSignal());
// emit exited(QAbstractState::QPrivateSignal());
}
};
class MyTransition : public QSignalTransition
{
Q_OBJECT
public:
MyTransition(QObject * sender, const char * signal, QState *sourceState = 0)
: QSignalTransition(sender, signal, sourceState)
{
}
void emitPrivateSignals()
{
// These deliberately do not compile
// emit triggered();
//
// emit triggered(QPrivateSignal());
//
// emit triggered(QAbstractTransition::QPrivateSignal());
}
};
class SignalConnectionTester : public QObject
{
Q_OBJECT
public:
SignalConnectionTester(QObject *parent = 0)
: QObject(parent), testPassed(false)
{
}
public Q_SLOTS:
void testSlot()
{
testPassed = true;
}
public:
bool testPassed;
};
class TestTrigger : public QObject
{
Q_OBJECT
public:
TestTrigger(QObject *parent = 0)
: QObject(parent)
{
}
void emitTrigger()
{
emit trigger();
}
signals:
void trigger();
};
void tst_QState::privateSignals()
{
QStateMachine machine;
QState *s1 = new QState(&machine);
MyState *s2 = new MyState(&machine);
TestTrigger testTrigger;
MyTransition *t1 = new MyTransition(&testTrigger, SIGNAL(trigger()), s1);
s1->addTransition(t1);
t1->setTargetState(s2);
machine.setInitialState(s1);
machine.start();
QCoreApplication::processEvents();
SignalConnectionTester s1Tester;
SignalConnectionTester s2Tester;
SignalConnectionTester t1Tester;
QObject::connect(s1, &QState::exited, &s1Tester, &SignalConnectionTester::testSlot);
QObject::connect(s2, &QState::entered, &s2Tester, &SignalConnectionTester::testSlot);
QObject::connect(t1, &QSignalTransition::triggered, &t1Tester, &SignalConnectionTester::testSlot);
testTrigger.emitTrigger();
QCoreApplication::processEvents();
QVERIFY(s1Tester.testPassed);
QVERIFY(s2Tester.testPassed);
QVERIFY(t1Tester.testPassed);
}
void tst_QState::parallelStateAndInitialState()
{
QStateMachine machine;
{ // setting an initial state on a parallel state:
QState a(QState::ParallelStates, &machine);
QState b(&a);
QVERIFY(!a.initialState());
const QString warning
= QString::asprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", &a);
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
a.setInitialState(&b); // should produce a warning and do nothing.
QVERIFY(!a.initialState());
}
{ // setting the child-mode from ExclusiveStates to ParallelStates should remove the initial state:
QState a(QState::ExclusiveStates, &machine);
QState b(&a);
a.setInitialState(&b);
QCOMPARE(a.initialState(), &b);
const QString warning
= QString::asprintf("QState::setChildMode: setting the child-mode of state %p to "
"parallel removes the initial state", &a);
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
a.setChildMode(QState::ParallelStates); // should produce a warning and remove the initial state
QVERIFY(!a.initialState());
QCOMPARE(a.childMode(), QState::ParallelStates);
}
}
QTEST_MAIN(tst_QState)
#include "tst_qstate.moc"
|