61500c617a33315deac4c5ba522adfc84f6cf0de
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qdeclarativetimer / tst_qdeclarativetimer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 <QtQuick/private/qdeclarativetimer_p.h>
46 #include <QtQuick/qquickitem.h>
47 #include <QDebug>
48
49 class tst_qdeclarativetimer : public QObject
50 {
51     Q_OBJECT
52 public:
53     tst_qdeclarativetimer();
54
55 private slots:
56     void notRepeating();
57     void notRepeatingStart();
58     void repeat();
59     void noTriggerIfNotRunning();
60     void triggeredOnStart();
61     void triggeredOnStartRepeat();
62     void changeDuration();
63     void restart();
64     void restartFromTriggered();
65     void runningFromTriggered();
66     void parentProperty();
67 };
68
69 class TimerHelper : public QObject
70 {
71     Q_OBJECT
72 public:
73     TimerHelper() : QObject(), count(0)
74     {
75     }
76
77     int count;
78
79 public slots:
80     void timeout() {
81         ++count;
82     }
83 };
84
85 #define TIMEOUT_TIMEOUT 200
86
87 tst_qdeclarativetimer::tst_qdeclarativetimer()
88 {
89 }
90
91 void tst_qdeclarativetimer::notRepeating()
92 {
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());
97     QVERIFY(timer != 0);
98     QVERIFY(timer->isRunning());
99     QVERIFY(!timer->isRepeating());
100     QCOMPARE(timer->interval(), 100);
101
102     TimerHelper helper;
103     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
104
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);
110 }
111
112 void tst_qdeclarativetimer::notRepeatingStart()
113 {
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());
118     QVERIFY(timer != 0);
119     QVERIFY(!timer->isRunning());
120
121     TimerHelper helper;
122     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
123
124     QTest::qWait(TIMEOUT_TIMEOUT);
125     QCOMPARE(helper.count, 0);
126
127     timer->start();
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);
133
134     delete timer;
135 }
136
137 void tst_qdeclarativetimer::repeat()
138 {
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());
143     QVERIFY(timer != 0);
144
145     TimerHelper helper;
146     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
147     QCOMPARE(helper.count, 0);
148
149     QTest::qWait(TIMEOUT_TIMEOUT);
150     QVERIFY(helper.count > 0);
151     int oldCount = helper.count;
152
153     QTest::qWait(TIMEOUT_TIMEOUT);
154     QVERIFY(helper.count > oldCount);
155     QVERIFY(timer->isRunning());
156
157     oldCount = helper.count;
158     timer->stop();
159
160     QTest::qWait(TIMEOUT_TIMEOUT);
161     QVERIFY(helper.count == oldCount);
162     QVERIFY(timer->isRunning() == false);
163
164     QSignalSpy spy(timer, SIGNAL(repeatChanged()));
165
166     timer->setRepeating(false);
167     QVERIFY(!timer->isRepeating());
168     QCOMPARE(spy.count(),1);
169
170     timer->setRepeating(false);
171     QCOMPARE(spy.count(),1);
172
173     timer->setRepeating(true);
174     QCOMPARE(spy.count(),2);
175
176     delete timer;
177 }
178
179 void tst_qdeclarativetimer::triggeredOnStart()
180 {
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());
185     QVERIFY(timer != 0);
186     QVERIFY(timer->triggeredOnStart());
187
188     TimerHelper helper;
189     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
190     QTest::qWait(1);
191     QCOMPARE(helper.count, 1);
192
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);
198
199     QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
200
201     timer->setTriggeredOnStart(false);
202     QVERIFY(!timer->triggeredOnStart());
203     QCOMPARE(spy.count(),1);
204
205     timer->setTriggeredOnStart(false);
206     QCOMPARE(spy.count(),1);
207
208     timer->setTriggeredOnStart(true);
209     QCOMPARE(spy.count(),2);
210
211     delete timer;
212 }
213
214 void tst_qdeclarativetimer::triggeredOnStartRepeat()
215 {
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());
220     QVERIFY(timer != 0);
221
222     TimerHelper helper;
223     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
224     QTest::qWait(1);
225     QCOMPARE(helper.count, 1);
226
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());
233
234     delete timer;
235 }
236
237 void tst_qdeclarativetimer::noTriggerIfNotRunning()
238 {
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 }"
246         "}"
247     ), QUrl::fromLocalFile(""));
248     QObject *item = component.create();
249     QVERIFY(item != 0);
250     QTest::qWait(TIMEOUT_TIMEOUT);
251     QCOMPARE(item->property("ok").toBool(), true);
252
253     delete item;
254 }
255
256 void tst_qdeclarativetimer::changeDuration()
257 {
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());
262     QVERIFY(timer != 0);
263
264     TimerHelper helper;
265     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
266     QCOMPARE(helper.count, 0);
267
268     QTest::qWait(500);
269     QCOMPARE(helper.count, 2);
270
271     timer->setInterval(500);
272
273     QTest::qWait(600);
274     QCOMPARE(helper.count, 3);
275     QVERIFY(timer->isRunning());
276
277     QSignalSpy spy(timer, SIGNAL(intervalChanged()));
278
279     timer->setInterval(200);
280     QCOMPARE(timer->interval(), 200);
281     QCOMPARE(spy.count(),1);
282
283     timer->setInterval(200);
284     QCOMPARE(spy.count(),1);
285
286     timer->setInterval(300);
287     QCOMPARE(spy.count(),2);
288
289     delete timer;
290 }
291
292 void tst_qdeclarativetimer::restart()
293 {
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());
298     QVERIFY(timer != 0);
299
300     TimerHelper helper;
301     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
302     QCOMPARE(helper.count, 0);
303
304     QTest::qWait(600);
305     QCOMPARE(helper.count, 1);
306
307     QTest::qWait(300);
308
309     timer->restart();
310
311     QTest::qWait(700);
312
313     QCOMPARE(helper.count, 2);
314     QVERIFY(timer->isRunning());
315
316     delete timer;
317 }
318
319 void tst_qdeclarativetimer::restartFromTriggered()
320 {
321     QDeclarativeEngine engine;
322     QDeclarativeComponent component(&engine);
323     component.setData(QByteArray("import QtQuick 2.0\nTimer { "
324                                     "interval: 500; "
325                                     "repeat: false; "
326                                     "running: true; "
327                                     "onTriggered: restart()"
328                                  " }"), QUrl::fromLocalFile(""));
329     QScopedPointer<QObject> object(component.create());
330     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
331     QVERIFY(timer != 0);
332
333     TimerHelper helper;
334     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
335     QCOMPARE(helper.count, 0);
336
337     QTest::qWait(600);
338     QCOMPARE(helper.count, 1);
339     QVERIFY(timer->isRunning());
340
341     QTest::qWait(600);
342     QCOMPARE(helper.count, 2);
343     QVERIFY(timer->isRunning());
344 }
345
346 void tst_qdeclarativetimer::runningFromTriggered()
347 {
348     QDeclarativeEngine engine;
349     QDeclarativeComponent component(&engine);
350     component.setData(QByteArray("import QtQuick 2.0\nTimer { "
351                                     "property bool ok: false; "
352                                     "interval: 500; "
353                                     "repeat: false; "
354                                     "running: true; "
355                                     "onTriggered: { ok = !running; running = true }"
356                                  " }"), QUrl::fromLocalFile(""));
357     QScopedPointer<QObject> object(component.create());
358     QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
359     QVERIFY(timer != 0);
360
361     TimerHelper helper;
362     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
363     QCOMPARE(helper.count, 0);
364
365     QTest::qWait(600);
366     QCOMPARE(helper.count, 1);
367     QVERIFY(timer->property("ok").toBool());
368     QVERIFY(timer->isRunning());
369
370     QTest::qWait(600);
371     QCOMPARE(helper.count, 2);
372     QVERIFY(timer->property("ok").toBool());
373     QVERIFY(timer->isRunning());
374 }
375
376 void tst_qdeclarativetimer::parentProperty()
377 {
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());
382     QVERIFY(item != 0);
383     QDeclarativeTimer *timer = item->findChild<QDeclarativeTimer*>("timer");
384     QVERIFY(timer != 0);
385
386     QVERIFY(timer->isRunning());
387
388     delete timer;
389 }
390
391 QTEST_MAIN(tst_qdeclarativetimer)
392
393 #include "tst_qdeclarativetimer.moc"