38cab4522c06feca48b13743bb1df72d40f51c26
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / 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 <QtQuick1/private/qdeclarativetimer_p.h>
46 #include <QtQuick1/qdeclarativeitem.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 parentProperty();
65 };
66
67 class TimerHelper : public QObject
68 {
69     Q_OBJECT
70 public:
71     TimerHelper() : QObject(), count(0)
72     {
73     }
74
75     int count;
76
77 public slots:
78     void timeout() {
79         ++count;
80     }
81 };
82
83 #define TIMEOUT_TIMEOUT 200
84
85 tst_qdeclarativetimer::tst_qdeclarativetimer()
86 {
87 }
88
89 void tst_qdeclarativetimer::notRepeating()
90 {
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());
95     QVERIFY(timer != 0);
96     QVERIFY(timer->isRunning());
97     QVERIFY(!timer->isRepeating());
98     QCOMPARE(timer->interval(), 100);
99
100     TimerHelper helper;
101     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
102
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);
108 }
109
110 void tst_qdeclarativetimer::notRepeatingStart()
111 {
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());
116     QVERIFY(timer != 0);
117     QVERIFY(!timer->isRunning());
118
119     TimerHelper helper;
120     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
121
122     QTest::qWait(TIMEOUT_TIMEOUT);
123     QCOMPARE(helper.count, 0);
124
125     timer->start();
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);
131
132     delete timer;
133 }
134
135 void tst_qdeclarativetimer::repeat()
136 {
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());
141     QVERIFY(timer != 0);
142
143     TimerHelper helper;
144     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
145     QCOMPARE(helper.count, 0);
146
147     QTest::qWait(TIMEOUT_TIMEOUT);
148     QVERIFY(helper.count > 0);
149     int oldCount = helper.count;
150
151     QTest::qWait(TIMEOUT_TIMEOUT);
152     QVERIFY(helper.count > oldCount);
153     QVERIFY(timer->isRunning());
154
155     oldCount = helper.count;
156     timer->stop();
157
158     QTest::qWait(TIMEOUT_TIMEOUT);
159     QVERIFY(helper.count == oldCount);
160     QVERIFY(timer->isRunning() == false);
161
162     QSignalSpy spy(timer, SIGNAL(repeatChanged()));
163
164     timer->setRepeating(false);
165     QVERIFY(!timer->isRepeating());
166     QCOMPARE(spy.count(),1);
167
168     timer->setRepeating(false);
169     QCOMPARE(spy.count(),1);
170
171     timer->setRepeating(true);
172     QCOMPARE(spy.count(),2);
173
174     delete timer;
175 }
176
177 void tst_qdeclarativetimer::triggeredOnStart()
178 {
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());
183     QVERIFY(timer != 0);
184     QVERIFY(timer->triggeredOnStart());
185
186     TimerHelper helper;
187     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
188     QTest::qWait(1);
189     QCOMPARE(helper.count, 1);
190
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);
196
197     QSignalSpy spy(timer, SIGNAL(triggeredOnStartChanged()));
198
199     timer->setTriggeredOnStart(false);
200     QVERIFY(!timer->triggeredOnStart());
201     QCOMPARE(spy.count(),1);
202
203     timer->setTriggeredOnStart(false);
204     QCOMPARE(spy.count(),1);
205
206     timer->setTriggeredOnStart(true);
207     QCOMPARE(spy.count(),2);
208
209     delete timer;
210 }
211
212 void tst_qdeclarativetimer::triggeredOnStartRepeat()
213 {
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());
218     QVERIFY(timer != 0);
219
220     TimerHelper helper;
221     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
222     QTest::qWait(1);
223     QCOMPARE(helper.count, 1);
224
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());
231
232     delete timer;
233 }
234
235 void tst_qdeclarativetimer::noTriggerIfNotRunning()
236 {
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 }"
244         "}"
245     ), QUrl::fromLocalFile(""));
246     QObject *item = component.create();
247     QVERIFY(item != 0);
248     QTest::qWait(TIMEOUT_TIMEOUT);
249     QCOMPARE(item->property("ok").toBool(), true);
250
251     delete item;
252 }
253
254 void tst_qdeclarativetimer::changeDuration()
255 {
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());
260     QVERIFY(timer != 0);
261
262     TimerHelper helper;
263     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
264     QCOMPARE(helper.count, 0);
265
266     QTest::qWait(500);
267     QCOMPARE(helper.count, 2);
268
269     timer->setInterval(500);
270
271     QTest::qWait(600);
272     QCOMPARE(helper.count, 3);
273     QVERIFY(timer->isRunning());
274
275     QSignalSpy spy(timer, SIGNAL(intervalChanged()));
276
277     timer->setInterval(200);
278     QCOMPARE(timer->interval(), 200);
279     QCOMPARE(spy.count(),1);
280
281     timer->setInterval(200);
282     QCOMPARE(spy.count(),1);
283
284     timer->setInterval(300);
285     QCOMPARE(spy.count(),2);
286
287     delete timer;
288 }
289
290 void tst_qdeclarativetimer::restart()
291 {
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());
296     QVERIFY(timer != 0);
297
298     TimerHelper helper;
299     connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
300     QCOMPARE(helper.count, 0);
301
302     QTest::qWait(600);
303     QCOMPARE(helper.count, 1);
304
305     QTest::qWait(300);
306
307     timer->restart();
308
309     QTest::qWait(700);
310
311     QCOMPARE(helper.count, 2);
312     QVERIFY(timer->isRunning());
313
314     delete timer;
315 }
316
317 void tst_qdeclarativetimer::parentProperty()
318 {
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());
323     QVERIFY(item != 0);
324     QDeclarative1Timer *timer = item->findChild<QDeclarative1Timer*>("timer");
325     QVERIFY(timer != 0);
326
327     QVERIFY(timer->isRunning());
328
329     delete timer;
330 }
331
332 QTEST_MAIN(tst_qdeclarativetimer)
333
334 #include "tst_qdeclarativetimer.moc"