Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativetimer / tst_qdeclarativetimer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QSignalSpy>
42 #include <qtest.h>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <private/qdeclarativetimer_p.h>
46 #include <QtDeclarative/qdeclarativeitem.h>
47 #include <QDebug>
48
49 #ifdef Q_OS_SYMBIAN
50 // In Symbian OS test data is located in applications private dir
51 #define SRCDIR "."
52 #endif
53
54 class tst_qdeclarativetimer : public QObject
55 {
56     Q_OBJECT
57 public:
58     tst_qdeclarativetimer();
59
60 private slots:
61     void notRepeating();
62     void notRepeatingStart();
63     void repeat();
64     void noTriggerIfNotRunning();
65     void triggeredOnStart();
66     void triggeredOnStartRepeat();
67     void changeDuration();
68     void restart();
69     void parentProperty();
70 };
71
72 class TimerHelper : public QObject
73 {
74     Q_OBJECT
75 public:
76     TimerHelper() : QObject(), count(0)
77     {
78     }
79
80     int count;
81
82 public slots:
83     void timeout() {
84         ++count;
85     }
86 };
87
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
91 #else
92 #define TIMEOUT_TIMEOUT 200
93 #endif
94
95 tst_qdeclarativetimer::tst_qdeclarativetimer()
96 {
97 }
98
99 void tst_qdeclarativetimer::notRepeating()
100 {
101     QDeclarativeEngine engine;
102     QDeclarativeComponent component(&engine);
103     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true }"), QUrl::fromLocalFile(""));
104     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
105     QVERIFY(timer != 0);
106     QVERIFY(timer->isRunning());
107     QVERIFY(!timer->isRepeating());
108     QCOMPARE(timer->interval(), 100);
109
110     TimerHelper helper;
111     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
112
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);
118 }
119
120 void tst_qdeclarativetimer::notRepeatingStart()
121 {
122     QDeclarativeEngine engine;
123     QDeclarativeComponent component(&engine);
124     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100 }"), QUrl::fromLocalFile(""));
125     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
126     QVERIFY(timer != 0);
127     QVERIFY(!timer->isRunning());
128
129     TimerHelper helper;
130     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
131
132     QTest::qWait(TIMEOUT_TIMEOUT);
133     QCOMPARE(helper.count, 0);
134
135     timer->start();
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);
141
142     delete timer;
143 }
144
145 void tst_qdeclarativetimer::repeat()
146 {
147     QDeclarativeEngine engine;
148     QDeclarativeComponent component(&engine);
149     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; repeat: true; running: true }"), QUrl::fromLocalFile(""));
150     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
151     QVERIFY(timer != 0);
152
153     TimerHelper helper;
154     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
155     QCOMPARE(helper.count, 0);
156
157     QTest::qWait(TIMEOUT_TIMEOUT);
158     QVERIFY(helper.count > 0);
159     int oldCount = helper.count;
160
161     QTest::qWait(TIMEOUT_TIMEOUT);
162     QVERIFY(helper.count > oldCount);
163     QVERIFY(timer->isRunning());
164
165     oldCount = helper.count;
166     timer->stop();
167
168     QTest::qWait(TIMEOUT_TIMEOUT);
169     QVERIFY(helper.count == oldCount);
170     QVERIFY(timer->isRunning() == false);
171
172     QSignalSpy spy(timer, SIGNAL(repeatChanged()));
173
174     timer->setRepeating(false);
175     QVERIFY(!timer->isRepeating());
176     QCOMPARE(spy.count(),1);
177
178     timer->setRepeating(false);
179     QCOMPARE(spy.count(),1);
180
181     timer->setRepeating(true);
182     QCOMPARE(spy.count(),2);
183
184     delete timer;
185 }
186
187 void tst_qdeclarativetimer::triggeredOnStart()
188 {
189     QDeclarativeEngine engine;
190     QDeclarativeComponent component(&engine);
191     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true; triggeredOnStart: true }"), QUrl::fromLocalFile(""));
192     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
193     QVERIFY(timer != 0);
194     QVERIFY(timer->triggeredOnStart());
195
196     TimerHelper helper;
197     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
198     QTest::qWait(1);
199     QCOMPARE(helper.count, 1);
200
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);
206
207     QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
208
209     timer->setTriggeredOnStart(false);
210     QVERIFY(!timer->triggeredOnStart());
211     QCOMPARE(spy.count(),1);
212
213     timer->setTriggeredOnStart(false);
214     QCOMPARE(spy.count(),1);
215
216     timer->setTriggeredOnStart(true);
217     QCOMPARE(spy.count(),2);
218
219     delete timer;
220 }
221
222 void tst_qdeclarativetimer::triggeredOnStartRepeat()
223 {
224     QDeclarativeEngine engine;
225     QDeclarativeComponent component(&engine);
226     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 100; running: true; triggeredOnStart: true; repeat: true }"), QUrl::fromLocalFile(""));
227     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
228     QVERIFY(timer != 0);
229
230     TimerHelper helper;
231     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
232     QTest::qWait(1);
233     QCOMPARE(helper.count, 1);
234
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());
241
242     delete timer;
243 }
244
245 void tst_qdeclarativetimer::noTriggerIfNotRunning()
246 {
247     QDeclarativeEngine engine;
248     QDeclarativeComponent component(&engine);
249     component.setData(QByteArray(
250         "import QtQuick 1.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 }"
254         "}"
255     ), QUrl::fromLocalFile(""));
256     QObject *item = component.create();
257     QVERIFY(item != 0);
258     QTest::qWait(TIMEOUT_TIMEOUT);
259     QCOMPARE(item->property("ok").toBool(), true);
260
261     delete item;
262 }
263
264 void tst_qdeclarativetimer::changeDuration()
265 {
266     QDeclarativeEngine engine;
267     QDeclarativeComponent component(&engine);
268     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 200; repeat: true; running: true }"), QUrl::fromLocalFile(""));
269     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
270     QVERIFY(timer != 0);
271
272     TimerHelper helper;
273     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
274     QCOMPARE(helper.count, 0);
275
276     QTest::qWait(500);
277     QCOMPARE(helper.count, 2);
278
279     timer->setInterval(500);
280
281     QTest::qWait(600);
282     QCOMPARE(helper.count, 3);
283     QVERIFY(timer->isRunning());
284
285     QSignalSpy spy(timer, SIGNAL(intervalChanged()));
286
287     timer->setInterval(200);
288     QCOMPARE(timer->interval(), 200);
289     QCOMPARE(spy.count(),1);
290
291     timer->setInterval(200);
292     QCOMPARE(spy.count(),1);
293
294     timer->setInterval(300);
295     QCOMPARE(spy.count(),2);
296
297     delete timer;
298 }
299
300 void tst_qdeclarativetimer::restart()
301 {
302     QDeclarativeEngine engine;
303     QDeclarativeComponent component(&engine);
304     component.setData(QByteArray("import QtQuick 1.0\nTimer { interval: 500; repeat: true; running: true }"), QUrl::fromLocalFile(""));
305     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(component.create());
306     QVERIFY(timer != 0);
307
308     TimerHelper helper;
309     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
310     QCOMPARE(helper.count, 0);
311
312     QTest::qWait(600);
313     QCOMPARE(helper.count, 1);
314
315     QTest::qWait(300);
316
317     timer->restart();
318
319     QTest::qWait(700);
320
321     QCOMPARE(helper.count, 2);
322     QVERIFY(timer->isRunning());
323
324     delete timer;
325 }
326
327 void tst_qdeclarativetimer::parentProperty()
328 {
329     QDeclarativeEngine engine;
330     QDeclarativeComponent component(&engine);
331     component.setData(QByteArray("import QtQuick 1.0\nItem { Timer { objectName: \"timer\"; running: parent.visible } }"), QUrl::fromLocalFile(""));
332     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
333     QVERIFY(item != 0);
334     QDeclarativeTimer *timer = item->findChild<QDeclarativeTimer*>("timer");
335     QVERIFY(timer != 0);
336
337     QVERIFY(timer->isRunning());
338
339     delete timer;
340 }
341
342 QTEST_MAIN(tst_qdeclarativetimer)
343
344 #include "tst_qdeclarativetimer.moc"