1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
41 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtQuick/private/qdeclarativetimer_p.h>
46 #include <QtQuick/qquickitem.h>
49 class tst_qdeclarativetimer : public QObject
53 tst_qdeclarativetimer();
57 void notRepeatingStart();
59 void noTriggerIfNotRunning();
60 void triggeredOnStart();
61 void triggeredOnStartRepeat();
62 void changeDuration();
64 void restartFromTriggered();
65 void runningFromTriggered();
66 void parentProperty();
69 class TimerHelper : public QObject
73 TimerHelper() : QObject(), count(0)
85 #define TIMEOUT_TIMEOUT 200
87 tst_qdeclarativetimer::tst_qdeclarativetimer()
91 void tst_qdeclarativetimer::notRepeating()
93 QDeclarativeEngine engine;
94 QDeclarativeComponent component(&engine);
95 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true }"), QUrl::fromLocalFile(""));
96 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
98 QVERIFY(timer->isRunning());
99 QVERIFY(!timer->isRepeating());
100 QCOMPARE(timer->interval(), 100);
103 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
105 QTest::qWait(TIMEOUT_TIMEOUT);
106 QCOMPARE(helper.count, 1);
107 QTest::qWait(TIMEOUT_TIMEOUT);
108 QCOMPARE(helper.count, 1);
109 QVERIFY(timer->isRunning() == false);
112 void tst_qdeclarativetimer::notRepeatingStart()
114 QDeclarativeEngine engine;
115 QDeclarativeComponent component(&engine);
116 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100 }"), QUrl::fromLocalFile(""));
117 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
119 QVERIFY(!timer->isRunning());
122 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
124 QTest::qWait(TIMEOUT_TIMEOUT);
125 QCOMPARE(helper.count, 0);
128 QTest::qWait(TIMEOUT_TIMEOUT);
129 QCOMPARE(helper.count, 1);
130 QTest::qWait(TIMEOUT_TIMEOUT);
131 QCOMPARE(helper.count, 1);
132 QVERIFY(timer->isRunning() == false);
137 void tst_qdeclarativetimer::repeat()
139 QDeclarativeEngine engine;
140 QDeclarativeComponent component(&engine);
141 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; repeat: true; running: true }"), QUrl::fromLocalFile(""));
142 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
146 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
147 QCOMPARE(helper.count, 0);
149 QTest::qWait(TIMEOUT_TIMEOUT);
150 QVERIFY(helper.count > 0);
151 int oldCount = helper.count;
153 QTest::qWait(TIMEOUT_TIMEOUT);
154 QVERIFY(helper.count > oldCount);
155 QVERIFY(timer->isRunning());
157 oldCount = helper.count;
160 QTest::qWait(TIMEOUT_TIMEOUT);
161 QVERIFY(helper.count == oldCount);
162 QVERIFY(timer->isRunning() == false);
164 QSignalSpy spy(timer, SIGNAL(repeatChanged()));
166 timer->setRepeating(false);
167 QVERIFY(!timer->isRepeating());
168 QCOMPARE(spy.count(),1);
170 timer->setRepeating(false);
171 QCOMPARE(spy.count(),1);
173 timer->setRepeating(true);
174 QCOMPARE(spy.count(),2);
179 void tst_qdeclarativetimer::triggeredOnStart()
181 QDeclarativeEngine engine;
182 QDeclarativeComponent component(&engine);
183 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true; triggeredOnStart: true }"), QUrl::fromLocalFile(""));
184 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
186 QVERIFY(timer->triggeredOnStart());
189 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
191 QCOMPARE(helper.count, 1);
193 QTest::qWait(TIMEOUT_TIMEOUT);
194 QCOMPARE(helper.count, 2);
195 QTest::qWait(TIMEOUT_TIMEOUT);
196 QCOMPARE(helper.count, 2);
197 QVERIFY(timer->isRunning() == false);
199 QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
201 timer->setTriggeredOnStart(false);
202 QVERIFY(!timer->triggeredOnStart());
203 QCOMPARE(spy.count(),1);
205 timer->setTriggeredOnStart(false);
206 QCOMPARE(spy.count(),1);
208 timer->setTriggeredOnStart(true);
209 QCOMPARE(spy.count(),2);
214 void tst_qdeclarativetimer::triggeredOnStartRepeat()
216 QDeclarativeEngine engine;
217 QDeclarativeComponent component(&engine);
218 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true; triggeredOnStart: true; repeat: true }"), QUrl::fromLocalFile(""));
219 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
223 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
225 QCOMPARE(helper.count, 1);
227 QTest::qWait(TIMEOUT_TIMEOUT);
228 QVERIFY(helper.count > 1);
229 int oldCount = helper.count;
230 QTest::qWait(TIMEOUT_TIMEOUT);
231 QVERIFY(helper.count > oldCount);
232 QVERIFY(timer->isRunning());
237 void tst_qdeclarativetimer::noTriggerIfNotRunning()
239 QDeclarativeEngine engine;
240 QDeclarativeComponent component(&engine);
241 component.setData(QByteArray(
242 "import QtQuick 2.0\n"
243 "Item { property bool ok: true\n"
244 "Timer { id: t1; interval: 100; repeat: true; running: true; onTriggered: if (!running) ok=false }"
245 "Timer { interval: 10; running: true; onTriggered: t1.running=false }"
247 ), QUrl::fromLocalFile(""));
248 QObject *item = component.create();
250 QTest::qWait(TIMEOUT_TIMEOUT);
251 QCOMPARE(item->property("ok").toBool(), true);
256 void tst_qdeclarativetimer::changeDuration()
258 QDeclarativeEngine engine;
259 QDeclarativeComponent component(&engine);
260 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 200; repeat: true; running: true }"), QUrl::fromLocalFile(""));
261 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
265 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
266 QCOMPARE(helper.count, 0);
269 QCOMPARE(helper.count, 2);
271 timer->setInterval(500);
274 QCOMPARE(helper.count, 3);
275 QVERIFY(timer->isRunning());
277 QSignalSpy spy(timer, SIGNAL(intervalChanged()));
279 timer->setInterval(200);
280 QCOMPARE(timer->interval(), 200);
281 QCOMPARE(spy.count(),1);
283 timer->setInterval(200);
284 QCOMPARE(spy.count(),1);
286 timer->setInterval(300);
287 QCOMPARE(spy.count(),2);
292 void tst_qdeclarativetimer::restart()
294 QDeclarativeEngine engine;
295 QDeclarativeComponent component(&engine);
296 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 500; repeat: true; running: true }"), QUrl::fromLocalFile(""));
297 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
301 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
302 QCOMPARE(helper.count, 0);
305 QCOMPARE(helper.count, 1);
313 QCOMPARE(helper.count, 2);
314 QVERIFY(timer->isRunning());
319 void tst_qdeclarativetimer::restartFromTriggered()
321 QDeclarativeEngine engine;
322 QDeclarativeComponent component(&engine);
323 component.setData(QByteArray("import QtQuick 2.0\nTimer { "
327 "onTriggered: restart()"
328 " }"), QUrl::fromLocalFile(""));
329 QScopedPointer<QObject> object(component.create());
330 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
334 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
335 QCOMPARE(helper.count, 0);
338 QCOMPARE(helper.count, 1);
339 QVERIFY(timer->isRunning());
342 QCOMPARE(helper.count, 2);
343 QVERIFY(timer->isRunning());
346 void tst_qdeclarativetimer::runningFromTriggered()
348 QDeclarativeEngine engine;
349 QDeclarativeComponent component(&engine);
350 component.setData(QByteArray("import QtQuick 2.0\nTimer { "
351 "property bool ok: false; "
355 "onTriggered: { ok = !running; running = true }"
356 " }"), QUrl::fromLocalFile(""));
357 QScopedPointer<QObject> object(component.create());
358 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
362 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
363 QCOMPARE(helper.count, 0);
366 QCOMPARE(helper.count, 1);
367 QVERIFY(timer->property("ok").toBool());
368 QVERIFY(timer->isRunning());
371 QCOMPARE(helper.count, 2);
372 QVERIFY(timer->property("ok").toBool());
373 QVERIFY(timer->isRunning());
376 void tst_qdeclarativetimer::parentProperty()
378 QDeclarativeEngine engine;
379 QDeclarativeComponent component(&engine);
380 component.setData(QByteArray("import QtQuick 2.0\nItem { Timer { objectName: \"timer\"; running: parent.visible } }"), QUrl::fromLocalFile(""));
381 QQuickItem *item = qobject_cast<QQuickItem*>(component.create());
383 QDeclarativeTimer *timer = item->findChild<QDeclarativeTimer*>("timer");
386 QVERIFY(timer->isRunning());
391 QTEST_MAIN(tst_qdeclarativetimer)
393 #include "tst_qdeclarativetimer.moc"