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 <QtQuick1/private/qdeclarativetimer_p.h>
46 #include <QtQuick1/qdeclarativeitem.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 parentProperty();
67 class TimerHelper : public QObject
71 TimerHelper() : QObject(), count(0)
83 #define TIMEOUT_TIMEOUT 200
85 tst_qdeclarativetimer::tst_qdeclarativetimer()
89 void tst_qdeclarativetimer::notRepeating()
91 QDeclarativeEngine engine;
92 QDeclarativeComponent component(&engine);
93 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true }"), QUrl::fromLocalFile(""));
94 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
96 QVERIFY(timer->isRunning());
97 QVERIFY(!timer->isRepeating());
98 QCOMPARE(timer->interval(), 100);
101 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
103 QTest::qWait(TIMEOUT_TIMEOUT);
104 QCOMPARE(helper.count, 1);
105 QTest::qWait(TIMEOUT_TIMEOUT);
106 QCOMPARE(helper.count, 1);
107 QVERIFY(timer->isRunning() == false);
110 void tst_qdeclarativetimer::notRepeatingStart()
112 QDeclarativeEngine engine;
113 QDeclarativeComponent component(&engine);
114 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100 }"), QUrl::fromLocalFile(""));
115 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
117 QVERIFY(!timer->isRunning());
120 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
122 QTest::qWait(TIMEOUT_TIMEOUT);
123 QCOMPARE(helper.count, 0);
126 QTest::qWait(TIMEOUT_TIMEOUT);
127 QCOMPARE(helper.count, 1);
128 QTest::qWait(TIMEOUT_TIMEOUT);
129 QCOMPARE(helper.count, 1);
130 QVERIFY(timer->isRunning() == false);
135 void tst_qdeclarativetimer::repeat()
137 QDeclarativeEngine engine;
138 QDeclarativeComponent component(&engine);
139 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; repeat: true; running: true }"), QUrl::fromLocalFile(""));
140 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
144 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
145 QCOMPARE(helper.count, 0);
147 QTest::qWait(TIMEOUT_TIMEOUT);
148 QVERIFY(helper.count > 0);
149 int oldCount = helper.count;
151 QTest::qWait(TIMEOUT_TIMEOUT);
152 QVERIFY(helper.count > oldCount);
153 QVERIFY(timer->isRunning());
155 oldCount = helper.count;
158 QTest::qWait(TIMEOUT_TIMEOUT);
159 QVERIFY(helper.count == oldCount);
160 QVERIFY(timer->isRunning() == false);
162 QSignalSpy spy(timer, SIGNAL(repeatChanged()));
164 timer->setRepeating(false);
165 QVERIFY(!timer->isRepeating());
166 QCOMPARE(spy.count(),1);
168 timer->setRepeating(false);
169 QCOMPARE(spy.count(),1);
171 timer->setRepeating(true);
172 QCOMPARE(spy.count(),2);
177 void tst_qdeclarativetimer::triggeredOnStart()
179 QDeclarativeEngine engine;
180 QDeclarativeComponent component(&engine);
181 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true; triggeredOnStart: true }"), QUrl::fromLocalFile(""));
182 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
184 QVERIFY(timer->triggeredOnStart());
187 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
189 QCOMPARE(helper.count, 1);
191 QTest::qWait(TIMEOUT_TIMEOUT);
192 QCOMPARE(helper.count, 2);
193 QTest::qWait(TIMEOUT_TIMEOUT);
194 QCOMPARE(helper.count, 2);
195 QVERIFY(timer->isRunning() == false);
197 QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
199 timer->setTriggeredOnStart(false);
200 QVERIFY(!timer->triggeredOnStart());
201 QCOMPARE(spy.count(),1);
203 timer->setTriggeredOnStart(false);
204 QCOMPARE(spy.count(),1);
206 timer->setTriggeredOnStart(true);
207 QCOMPARE(spy.count(),2);
212 void tst_qdeclarativetimer::triggeredOnStartRepeat()
214 QDeclarativeEngine engine;
215 QDeclarativeComponent component(&engine);
216 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true; triggeredOnStart: true; repeat: true }"), QUrl::fromLocalFile(""));
217 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
221 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
223 QCOMPARE(helper.count, 1);
225 QTest::qWait(TIMEOUT_TIMEOUT);
226 QVERIFY(helper.count > 1);
227 int oldCount = helper.count;
228 QTest::qWait(TIMEOUT_TIMEOUT);
229 QVERIFY(helper.count > oldCount);
230 QVERIFY(timer->isRunning());
235 void tst_qdeclarativetimer::noTriggerIfNotRunning()
237 QDeclarativeEngine engine;
238 QDeclarativeComponent component(&engine);
239 component.setData(QByteArray(
240 "import QtQuick 1.0\n"
241 "Item { property bool ok: true\n"
242 "Timer { id: t1; interval: 100; repeat: true; running: true; onTriggered: if (!running) ok=false }"
243 "Timer { interval: 10; running: true; onTriggered: t1.running=false }"
245 ), QUrl::fromLocalFile(""));
246 QObject *item = component.create();
248 QTest::qWait(TIMEOUT_TIMEOUT);
249 QCOMPARE(item->property("ok").toBool(), true);
254 void tst_qdeclarativetimer::changeDuration()
256 QDeclarativeEngine engine;
257 QDeclarativeComponent component(&engine);
258 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 200; repeat: true; running: true }"), QUrl::fromLocalFile(""));
259 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
263 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
264 QCOMPARE(helper.count, 0);
267 QCOMPARE(helper.count, 2);
269 timer->setInterval(500);
272 QCOMPARE(helper.count, 3);
273 QVERIFY(timer->isRunning());
275 QSignalSpy spy(timer, SIGNAL(intervalChanged()));
277 timer->setInterval(200);
278 QCOMPARE(timer->interval(), 200);
279 QCOMPARE(spy.count(),1);
281 timer->setInterval(200);
282 QCOMPARE(spy.count(),1);
284 timer->setInterval(300);
285 QCOMPARE(spy.count(),2);
290 void tst_qdeclarativetimer::restart()
292 QDeclarativeEngine engine;
293 QDeclarativeComponent component(&engine);
294 component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 500; repeat: true; running: true }"), QUrl::fromLocalFile(""));
295 QDeclarative1Timer *timer = qobject_cast<QDeclarative1Timer*>(component.create());
299 connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
300 QCOMPARE(helper.count, 0);
303 QCOMPARE(helper.count, 1);
311 QCOMPARE(helper.count, 2);
312 QVERIFY(timer->isRunning());
317 void tst_qdeclarativetimer::parentProperty()
319 QDeclarativeEngine engine;
320 QDeclarativeComponent component(&engine);
321 component.setData(QByteArray("import QtQuick 1.0\nItem { Timer { objectName: \"timer\"; running: parent.visible } }"), QUrl::fromLocalFile(""));
322 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
324 QDeclarative1Timer *timer = item->findChild<QDeclarative1Timer*>("timer");
327 QVERIFY(timer->isRunning());
332 QTEST_MAIN(tst_qdeclarativetimer)
334 #include "tst_qdeclarativetimer.moc"