1/****************************************************************************
2**
3** Copyright (C) 2016 Ford Motor Company
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28#include <QQmlComponent>
29#include <QQmlContext>
30#include <QQmlEngine>
31#include <QTest>
32#include "../../shared/util.h"
33
34class tst_qqmlstatemachine : public QQmlDataTest
35{
36 Q_OBJECT
37public:
38 tst_qqmlstatemachine();
39
40private slots:
41 void tst_cppObjectSignal();
42};
43
44
45class CppObject : public QObject
46{
47 Q_OBJECT
48 Q_PROPERTY(ObjectState objectState READ objectState WRITE setObjectState NOTIFY objectStateChanged)
49 Q_ENUMS(ObjectState)
50public:
51 enum ObjectState {
52 State0,
53 State1,
54 State2
55 };
56
57public:
58 CppObject() {}
59
60 ObjectState objectState() const { return m_objectState; }
61 void setObjectState(ObjectState objectState) { m_objectState = objectState; emit objectStateChanged();}
62
63signals:
64 void objectStateChanged();
65 void mySignal(int signalState);
66
67private:
68 ObjectState m_objectState = State0;
69};
70
71tst_qqmlstatemachine::tst_qqmlstatemachine()
72{
73 QVERIFY(-1 != qmlRegisterUncreatableType<CppObject>("CppObjectEnum", 1, 0, "CppObject", QString()));
74}
75
76void tst_qqmlstatemachine::tst_cppObjectSignal()
77{
78 CppObject cppObject;
79 QQmlEngine engine;
80 QQmlComponent component(&engine, testFileUrl(fileName: "cppsignal.qml"));
81 QVERIFY2(!component.isError(), qPrintable(component.errorString()));
82
83 QQmlContext *ctxt = engine.rootContext();
84 ctxt->setContextProperty("_cppObject", &cppObject);
85 QScopedPointer<QObject> rootObject(component.create());
86 QVERIFY(rootObject != nullptr);
87
88 // wait for state machine to start
89 QTRY_VERIFY(rootObject->property("running").toBool());
90
91 // emit signal from cpp
92 emit cppObject.mySignal(signalState: CppObject::State1);
93
94 // check if the signal was propagated
95 QTRY_COMPARE(cppObject.objectState(), CppObject::State1);
96
97 // emit signal from cpp
98 emit cppObject.mySignal(signalState: CppObject::State2);
99
100 // check if the signal was propagated
101 QTRY_COMPARE(cppObject.objectState(), CppObject::State2);
102
103 // wait for state machine to finish
104 QTRY_VERIFY(!rootObject->property("running").toBool());
105}
106
107
108QTEST_MAIN(tst_qqmlstatemachine)
109
110#include "tst_qqmlstatemachine.moc"
111

source code of qtdeclarative/tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp