1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtFeedback 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 | |
29 | #include <qtest.h> |
30 | #include <QtQml/qqmlcomponent.h> |
31 | #include <QtQml/qqmlengine.h> |
32 | #include <qfeedbackeffect.h> |
33 | #include <qfeedbackactuator.h> |
34 | #include <QSignalSpy> |
35 | |
36 | Q_DECLARE_METATYPE(QFeedbackEffect::Effect); |
37 | |
38 | class tst_qdeclarativefeedback : public QObject |
39 | |
40 | { |
41 | Q_OBJECT |
42 | public: |
43 | tst_qdeclarativefeedback(); |
44 | |
45 | private slots: |
46 | void hapticsEffect(); |
47 | void fileEffect(); |
48 | void actuator(); |
49 | void themeEffect(); |
50 | |
51 | private: |
52 | QQmlEngine engine; |
53 | }; |
54 | |
55 | tst_qdeclarativefeedback::tst_qdeclarativefeedback() |
56 | { |
57 | } |
58 | |
59 | void tst_qdeclarativefeedback::hapticsEffect() |
60 | { |
61 | QQmlComponent component(&engine); |
62 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/hapticseffect.qml" )); |
63 | |
64 | QObject *hapticsEffect = component.create(); |
65 | QVERIFY(hapticsEffect != 0); |
66 | |
67 | QSignalSpy stateSpy(hapticsEffect, SIGNAL(stateChanged())); |
68 | QVERIFY(stateSpy.isValid()); |
69 | QCOMPARE(stateSpy.count(), 0); |
70 | |
71 | QCOMPARE(hapticsEffect->property("attackIntensity" ).toReal(), 0.0); |
72 | QCOMPARE(hapticsEffect->property("attackTime" ).toInt(), 250); |
73 | QCOMPARE(hapticsEffect->property("intensity" ).toReal(), 1.0); |
74 | QCOMPARE(hapticsEffect->property("duration" ).toInt(), 100); |
75 | QCOMPARE(hapticsEffect->property("fadeTime" ).toInt(), 250); |
76 | QCOMPARE(hapticsEffect->property("fadeIntensity" ).toReal(), 0.0); |
77 | QVERIFY(!hapticsEffect->property("actuator" ).isNull()); |
78 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Stopped)); |
79 | |
80 | hapticsEffect->setProperty(name: "attackIntensity" , value: 0.2); |
81 | QCOMPARE(hapticsEffect->property("attackIntensity" ).toReal(), 0.2); |
82 | hapticsEffect->setProperty(name: "attackTime" , value: 500); |
83 | QCOMPARE(hapticsEffect->property("attackTime" ).toInt(), 500); |
84 | hapticsEffect->setProperty(name: "intensity" , value: 0.8); |
85 | QCOMPARE(hapticsEffect->property("intensity" ).toReal(), 0.8); |
86 | hapticsEffect->setProperty(name: "duration" , value: 500); |
87 | QCOMPARE(hapticsEffect->property("duration" ).toInt(), 500); |
88 | hapticsEffect->setProperty(name: "fadeTime" , value: 500); |
89 | QCOMPARE(hapticsEffect->property("fadeTime" ).toInt(), 500); |
90 | hapticsEffect->setProperty(name: "fadeIntensity" , value: 0.2); |
91 | QCOMPARE(hapticsEffect->property("fadeIntensity" ).toReal(), 0.2); |
92 | |
93 | if (!hapticsEffect->property(name: "availableActuators" ).toList().isEmpty()) { |
94 | QCOMPARE(hapticsEffect->property("running" ).toBool(), false); |
95 | QCOMPARE(hapticsEffect->property("paused" ).toBool(), false); |
96 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Stopped)); |
97 | |
98 | hapticsEffect->setProperty(name: "running" , value: true); |
99 | QCOMPARE(hapticsEffect->property("running" ).toBool(), true); |
100 | QCOMPARE(hapticsEffect->property("paused" ).toBool(), false); |
101 | QCOMPARE(stateSpy.count(), 1); |
102 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Running)); |
103 | |
104 | hapticsEffect->setProperty(name: "paused" , value: true); |
105 | // XXX make sure we just test dummy backend |
106 | QCOMPARE(hapticsEffect->property("running" ).toBool(), false); |
107 | QCOMPARE(hapticsEffect->property("paused" ).toBool(), true); |
108 | QCOMPARE(stateSpy.count(), 2); |
109 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Paused)); |
110 | |
111 | hapticsEffect->setProperty(name: "running" , value: true); |
112 | QCOMPARE(hapticsEffect->property("running" ).toBool(), true); |
113 | QCOMPARE(hapticsEffect->property("paused" ).toBool(), false); |
114 | QCOMPARE(stateSpy.count(), 3); |
115 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Running)); |
116 | |
117 | hapticsEffect->setProperty(name: "running" , value: false); |
118 | QCOMPARE(hapticsEffect->property("running" ).toBool(), false); |
119 | QCOMPARE(hapticsEffect->property("paused" ).toBool(), false); |
120 | QCOMPARE(stateSpy.count(), 4); |
121 | QCOMPARE(hapticsEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Stopped)); |
122 | } |
123 | delete hapticsEffect; |
124 | } |
125 | |
126 | void tst_qdeclarativefeedback::fileEffect() |
127 | { |
128 | QQmlComponent component(&engine); |
129 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/fileeffect.qml" )); |
130 | |
131 | QObject *fileEffect = component.create(); |
132 | QVERIFY(fileEffect != 0); |
133 | |
134 | QCOMPARE(fileEffect->property("source" ).toUrl(), QUrl("qrc:nonexistingfile.haptic" )); |
135 | QCOMPARE(fileEffect->property("loaded" ).toBool(), false); |
136 | QTRY_COMPARE(fileEffect->property("state" ).toInt(), (int)(QFeedbackEffect::Stopped)); |
137 | |
138 | QCOMPARE(fileEffect->property("running" ).toBool(), false); |
139 | QCOMPARE(fileEffect->property("paused" ).toBool(), false); |
140 | fileEffect->setProperty(name: "running" , value: true); |
141 | fileEffect->setProperty(name: "paused" , value: true); |
142 | |
143 | // dummy backend |
144 | QTRY_COMPARE(fileEffect->property("running" ).toBool(), false); |
145 | QTRY_COMPARE(fileEffect->property("paused" ).toBool(), false); |
146 | |
147 | delete fileEffect; |
148 | } |
149 | |
150 | void tst_qdeclarativefeedback::actuator() |
151 | { |
152 | QQmlComponent component(&engine); |
153 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/actuator.qml" )); |
154 | |
155 | QObject *actuator = component.create(); |
156 | QVERIFY(actuator != 0); |
157 | QCOMPARE(actuator->property("enabled" ).toBool(), false); |
158 | |
159 | delete actuator; |
160 | } |
161 | |
162 | void tst_qdeclarativefeedback::themeEffect() |
163 | { |
164 | QQmlComponent component(&engine); |
165 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/themeeffect.qml" )); |
166 | |
167 | // We don't export this class, but we're just poking at properties anyway |
168 | QObject *dte = component.create(); |
169 | QCOMPARE(component.errorString(), QString()); |
170 | QVERIFY(dte != 0); |
171 | |
172 | // Test the effect property gets assigned |
173 | QMetaProperty p = dte->metaObject()->property(index: dte->metaObject()->indexOfProperty(name: "effect" )); |
174 | QCOMPARE(p.read(dte).value<int>(), (int)QFeedbackEffect::Press); |
175 | |
176 | p = dte->metaObject()->property(index: dte->metaObject()->indexOfProperty(name: "supported" )); |
177 | QCOMPARE(p.read(dte).value<bool>(), QFeedbackEffect::supportsThemeEffect()); |
178 | |
179 | delete dte; |
180 | |
181 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/themeeffect2.qml" )); |
182 | |
183 | dte = component.create(); |
184 | QCOMPARE(component.errorString(), QString()); |
185 | |
186 | QVERIFY(dte != 0); |
187 | // Effect property as a string |
188 | p = dte->metaObject()->property(index: dte->metaObject()->indexOfProperty(name: "effect" )); |
189 | QCOMPARE(p.read(dte).value<int>(), (int)QFeedbackEffect::Press); |
190 | |
191 | delete dte; |
192 | |
193 | // Now test the default method |
194 | component.loadUrl(url: QUrl::fromLocalFile(SRCDIR "/data/themeeffect3.qml" )); |
195 | |
196 | dte = component.create(); |
197 | QCOMPARE(component.errorString(), QString()); |
198 | QVERIFY(dte != 0); |
199 | |
200 | // Well, we can only verify that through a dummy plugin (TODO). verified through qdebug atm :) |
201 | |
202 | delete dte; |
203 | } |
204 | |
205 | QTEST_MAIN(tst_qdeclarativefeedback) |
206 | |
207 | #include "tst_qdeclarativefeedback.moc" |
208 | |