1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 <private/qdeclarativetimer_p.h>
46 #include <QtDeclarative/qsgitem.h>
50 // In Symbian OS test data is located in applications private dir
54 class tst_qdeclarativetimer : public QObject
58 tst_qdeclarativetimer();
62 void notRepeatingStart();
64 void noTriggerIfNotRunning();
65 void triggeredOnStart();
66 void triggeredOnStartRepeat();
67 void changeDuration();
69 void parentProperty();
72 class TimerHelper : public QObject
76 TimerHelper() : QObject(), count(0)
88 #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
89 // Increase wait as emulator startup can cause unexpected delays
90 #define TIMEOUT_TIMEOUT 2000
92 #define TIMEOUT_TIMEOUT 200
95 tst_qdeclarativetimer::tst_qdeclarativetimer()
99 void tst_qdeclarativetimer::notRepeating()
101 QDeclarativeEngine engine;
102 QDeclarativeComponent component(&engine);
103 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true }"), QUrl::fromLocalFile(""));
104 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
106 QVERIFY(timer->isRunning());
107 QVERIFY(!timer->isRepeating());
108 QCOMPARE(timer->interval(), 100);
111 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
113 QTest::qWait(TIMEOUT_TIMEOUT);
114 QCOMPARE(helper.count, 1);
115 QTest::qWait(TIMEOUT_TIMEOUT);
116 QCOMPARE(helper.count, 1);
117 QVERIFY(timer->isRunning() == false);
120 void tst_qdeclarativetimer::notRepeatingStart()
122 QDeclarativeEngine engine;
123 QDeclarativeComponent component(&engine);
124 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100 }"), QUrl::fromLocalFile(""));
125 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
127 QVERIFY(!timer->isRunning());
130 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
132 QTest::qWait(TIMEOUT_TIMEOUT);
133 QCOMPARE(helper.count, 0);
136 QTest::qWait(TIMEOUT_TIMEOUT);
137 QCOMPARE(helper.count, 1);
138 QTest::qWait(TIMEOUT_TIMEOUT);
139 QCOMPARE(helper.count, 1);
140 QVERIFY(timer->isRunning() == false);
145 void tst_qdeclarativetimer::repeat()
147 QDeclarativeEngine engine;
148 QDeclarativeComponent component(&engine);
149 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; repeat: true; running: true }"), QUrl::fromLocalFile(""));
150 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
154 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
155 QCOMPARE(helper.count, 0);
157 QTest::qWait(TIMEOUT_TIMEOUT);
158 QVERIFY(helper.count > 0);
159 int oldCount = helper.count;
161 QTest::qWait(TIMEOUT_TIMEOUT);
162 QVERIFY(helper.count > oldCount);
163 QVERIFY(timer->isRunning());
165 oldCount = helper.count;
168 QTest::qWait(TIMEOUT_TIMEOUT);
169 QVERIFY(helper.count == oldCount);
170 QVERIFY(timer->isRunning() == false);
172 QSignalSpy spy(timer, SIGNAL(repeatChanged()));
174 timer->setRepeating(false);
175 QVERIFY(!timer->isRepeating());
176 QCOMPARE(spy.count(),1);
178 timer->setRepeating(false);
179 QCOMPARE(spy.count(),1);
181 timer->setRepeating(true);
182 QCOMPARE(spy.count(),2);
187 void tst_qdeclarativetimer::triggeredOnStart()
189 QDeclarativeEngine engine;
190 QDeclarativeComponent component(&engine);
191 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true; triggeredOnStart: true }"), QUrl::fromLocalFile(""));
192 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
194 QVERIFY(timer->triggeredOnStart());
197 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
199 QCOMPARE(helper.count, 1);
201 QTest::qWait(TIMEOUT_TIMEOUT);
202 QCOMPARE(helper.count, 2);
203 QTest::qWait(TIMEOUT_TIMEOUT);
204 QCOMPARE(helper.count, 2);
205 QVERIFY(timer->isRunning() == false);
207 QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
209 timer->setTriggeredOnStart(false);
210 QVERIFY(!timer->triggeredOnStart());
211 QCOMPARE(spy.count(),1);
213 timer->setTriggeredOnStart(false);
214 QCOMPARE(spy.count(),1);
216 timer->setTriggeredOnStart(true);
217 QCOMPARE(spy.count(),2);
222 void tst_qdeclarativetimer::triggeredOnStartRepeat()
224 QDeclarativeEngine engine;
225 QDeclarativeComponent component(&engine);
226 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 100; running: true; triggeredOnStart: true; repeat: true }"), QUrl::fromLocalFile(""));
227 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
231 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
233 QCOMPARE(helper.count, 1);
235 QTest::qWait(TIMEOUT_TIMEOUT);
236 QVERIFY(helper.count > 1);
237 int oldCount = helper.count;
238 QTest::qWait(TIMEOUT_TIMEOUT);
239 QVERIFY(helper.count > oldCount);
240 QVERIFY(timer->isRunning());
245 void tst_qdeclarativetimer::noTriggerIfNotRunning()
247 QDeclarativeEngine engine;
248 QDeclarativeComponent component(&engine);
249 component.setData(QByteArray(
250 "import QtQuick 2.0\n"
251 "Item { property bool ok: true\n"
252 "Timer { id: t1; interval: 100; repeat: true; running: true; onTriggered: if (!running) ok=false }"
253 "Timer { interval: 10; running: true; onTriggered: t1.running=false }"
255 ), QUrl::fromLocalFile(""));
256 QObject *item = component.create();
258 QTest::qWait(TIMEOUT_TIMEOUT);
259 QCOMPARE(item->property("ok").toBool(), true);
264 void tst_qdeclarativetimer::changeDuration()
266 QDeclarativeEngine engine;
267 QDeclarativeComponent component(&engine);
268 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 200; repeat: true; running: true }"), QUrl::fromLocalFile(""));
269 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
273 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
274 QCOMPARE(helper.count, 0);
277 QCOMPARE(helper.count, 2);
279 timer->setInterval(500);
282 QCOMPARE(helper.count, 3);
283 QVERIFY(timer->isRunning());
285 QSignalSpy spy(timer, SIGNAL(intervalChanged()));
287 timer->setInterval(200);
288 QCOMPARE(timer->interval(), 200);
289 QCOMPARE(spy.count(),1);
291 timer->setInterval(200);
292 QCOMPARE(spy.count(),1);
294 timer->setInterval(300);
295 QCOMPARE(spy.count(),2);
300 void tst_qdeclarativetimer::restart()
302 QDeclarativeEngine engine;
303 QDeclarativeComponent component(&engine);
304 component.setData(QByteArray("import QtQuick 2.0\nTimer { interval: 500; repeat: true; running: true }"), QUrl::fromLocalFile(""));
305 QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
309 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
310 QCOMPARE(helper.count, 0);
313 QCOMPARE(helper.count, 1);
321 QCOMPARE(helper.count, 2);
322 QVERIFY(timer->isRunning());
327 void tst_qdeclarativetimer::parentProperty()
329 QDeclarativeEngine engine;
330 QDeclarativeComponent component(&engine);
331 component.setData(QByteArray("import QtQuick 2.0\nItem { Timer { objectName: \"timer\"; running: parent.visible } }"), QUrl::fromLocalFile(""));
332 QSGItem *item = qobject_cast<QSGItem*>(component.create());
334 QDeclarativeTimer *timer = item->findChild<QDeclarativeTimer*>("timer");
337 QVERIFY(timer->isRunning());
342 QTEST_MAIN(tst_qdeclarativetimer)
344 #include "tst_qdeclarativetimer.moc"