Fix restarting timer from onTriggered handler.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 10 Jan 2012 00:28:11 +0000 (10:28 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 12 Jan 2012 08:33:32 +0000 (09:33 +0100)
Set the running property to false before calling the triggered handler
when a timer finishes so it does not appear to still be running and
can be restarted by setting the running property to true.

Task-number: QTBUG-22004
Change-Id: I840efa30f5b7ad7d0cda96803d4898be3f390705
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/quick/util/qdeclarativetimer.cpp
tests/auto/qtquick2/qdeclarativetimer/tst_qdeclarativetimer.cpp

index 3855521..e030947 100644 (file)
@@ -315,9 +315,9 @@ void QDeclarativeTimer::finished()
     Q_D(QDeclarativeTimer);
     if (d->repeating || !d->running)
         return;
-    emit triggered();
     d->running = false;
     d->firstTick = false;
+    emit triggered();
     emit runningChanged();
 }
 
index 6582f0f..b7045a1 100644 (file)
@@ -61,6 +61,8 @@ private slots:
     void triggeredOnStartRepeat();
     void changeDuration();
     void restart();
+    void restartFromTriggered();
+    void runningFromTriggered();
     void parentProperty();
 };
 
@@ -314,6 +316,63 @@ void tst_qdeclarativetimer::restart()
     delete timer;
 }
 
+void tst_qdeclarativetimer::restartFromTriggered()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent component(&engine);
+    component.setData(QByteArray("import QtQuick 2.0\nTimer { "
+                                    "interval: 500; "
+                                    "repeat: false; "
+                                    "running: true; "
+                                    "onTriggered: restart()"
+                                 " }"), QUrl::fromLocalFile(""));
+    QScopedPointer<QObject> object(component.create());
+    QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
+    QVERIFY(timer != 0);
+
+    TimerHelper helper;
+    connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
+    QCOMPARE(helper.count, 0);
+
+    QTest::qWait(600);
+    QCOMPARE(helper.count, 1);
+    QVERIFY(timer->isRunning());
+
+    QTest::qWait(600);
+    QCOMPARE(helper.count, 2);
+    QVERIFY(timer->isRunning());
+}
+
+void tst_qdeclarativetimer::runningFromTriggered()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent component(&engine);
+    component.setData(QByteArray("import QtQuick 2.0\nTimer { "
+                                    "property bool ok: false; "
+                                    "interval: 500; "
+                                    "repeat: false; "
+                                    "running: true; "
+                                    "onTriggered: { ok = !running; running = true }"
+                                 " }"), QUrl::fromLocalFile(""));
+    QScopedPointer<QObject> object(component.create());
+    QDeclarativeTimer *timer = qobject_cast<QDeclarativeTimer*>(object.data());
+    QVERIFY(timer != 0);
+
+    TimerHelper helper;
+    connect(timer, SIGNAL(triggered()), &helper, SLOT(timeout()));
+    QCOMPARE(helper.count, 0);
+
+    QTest::qWait(600);
+    QCOMPARE(helper.count, 1);
+    QVERIFY(timer->property("ok").toBool());
+    QVERIFY(timer->isRunning());
+
+    QTest::qWait(600);
+    QCOMPARE(helper.count, 2);
+    QVERIFY(timer->property("ok").toBool());
+    QVERIFY(timer->isRunning());
+}
+
 void tst_qdeclarativetimer::parentProperty()
 {
     QDeclarativeEngine engine;