summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-05-30 15:53:25 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-06-06 17:47:00 -0700
commitf94e72d3d2f6eb85999cacdafc9560348113654a (patch)
treec062c2c7f86dd694272f319726e1071b242d7bf3 /tests
parent766deb0e43a1d1772054b45ba55466da3d7ff344 (diff)
tst_QTimer: ensure that timer activation respects start order
Task-number: QTBUG-114152 Change-Id: Iff484344171647888da4fffd17640daef56f2479 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/kernel/qtimer/CMakeLists.txt16
-rw-r--r--tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp99
-rw-r--r--tests/auto/gui/kernel/qguitimer/CMakeLists.txt13
3 files changed, 122 insertions, 6 deletions
diff --git a/tests/auto/corelib/kernel/qtimer/CMakeLists.txt b/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
index a4bcaca877..9b6af9b68e 100644
--- a/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
@@ -1,10 +1,6 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
-#####################################################################
-## tst_qtimer Test:
-#####################################################################
-
if (NOT QT_FEATURE_thread)
return()
endif()
@@ -16,5 +12,13 @@ qt_internal_add_test(tst_qtimer
Qt::CorePrivate
)
-## Scopes:
-#####################################################################
+if(QT_FEATURE_glib AND UNIX)
+ qt_internal_add_test(tst_qtimer_no_glib
+ SOURCES
+ tst_qtimer.cpp
+ DEFINES
+ DISABLE_GLIB
+ LIBRARIES
+ Qt::CorePrivate
+ )
+endif()
diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
index b4f7aa5bd8..17deeb4c5e 100644
--- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
+++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
@@ -4,6 +4,7 @@
#ifdef QT_GUI_LIB
# include <QtGui/QGuiApplication>
+# define tst_QTimer tst_QGuiTimer
#else
# include <QtCore/QCoreApplication>
#endif
@@ -21,6 +22,19 @@
#include <unistd.h>
#endif
+#ifdef DISABLE_GLIB
+# ifdef tst_QTimer
+# undef tst_QTimer
+# define tst_QTimer tst_QGuiTimer_NoGlib
+# else
+# define tst_QTimer tst_QTimer_NoGlib
+# endif
+static bool glibDisabled = []() {
+ qputenv("QT_NO_GLIB", "1");
+ return true;
+}();
+#endif
+
class tst_QTimer : public QObject
{
Q_OBJECT
@@ -32,6 +46,10 @@ private slots:
void zeroTimer();
void singleShotTimeout();
void timeout();
+ void sequentialTimers_data();
+ void sequentialTimers();
+ void singleShotSequentialTimers_data();
+ void singleShotSequentialTimers();
void remainingTime();
void remainingTimeInitial_data();
void remainingTimeInitial();
@@ -120,6 +138,87 @@ void tst_QTimer::timeout()
QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > oldCount, TIMEOUT_TIMEOUT);
}
+void tst_QTimer::sequentialTimers_data()
+{
+#ifdef Q_OS_WIN
+ QSKIP("The API used by QEventDispatcherWin32 doesn't respect the order");
+#endif
+ QTest::addColumn<QList<int>>("timeouts");
+ auto addRow = [](const QList<int> &l) {
+ QByteArray name;
+ int last = -1;
+ for (int i = 0; i < l.size(); ++i) {
+ Q_ASSERT_X(l[i] >= last, "tst_QTimer", "input list must be sorted");
+ name += QByteArray::number(l[i]) + ',';
+ }
+ name.chop(1);
+ QTest::addRow("%s", name.constData()) << l;
+ };
+ // PreciseTimers
+ addRow({0, 0, 0, 0, 0, 0});
+ addRow({0, 1, 2});
+ addRow({1, 1, 1, 2, 2, 2, 2});
+ addRow({1, 2, 3});
+ addRow({19, 19, 19});
+ // CoarseTimer for setInterval
+ addRow({20, 20, 20, 20, 20});
+ addRow({25, 25, 25, 25, 25, 25, 50});
+}
+
+void tst_QTimer::sequentialTimers()
+{
+ QFETCH(const QList<int>, timeouts);
+ QByteArray result, expected;
+ std::vector<std::unique_ptr<QTimer>> timers;
+ expected.resize(timeouts.size());
+ result.reserve(timeouts.size());
+ timers.reserve(timeouts.size());
+ for (int i = 0; i < timeouts.size(); ++i) {
+ auto timer = std::make_unique<QTimer>();
+ timer->setSingleShot(true);
+ timer->setInterval(timeouts[i]);
+
+ char c = 'A' + i;
+ expected[i] = c;
+ QObject::connect(timer.get(), &QTimer::timeout, this, [&result, c = c]() {
+ result.append(c);
+ });
+ timers.push_back(std::move(timer));
+ }
+
+ // start the timers
+ for (auto &timer : timers)
+ timer->start();
+
+ QTestEventLoop::instance().enterLoopMSecs(timeouts.last() * 2 + 10);
+
+ QCOMPARE(result, expected);
+}
+
+void tst_QTimer::singleShotSequentialTimers_data()
+{
+ sequentialTimers_data();
+}
+
+void tst_QTimer::singleShotSequentialTimers()
+{
+ QFETCH(const QList<int>, timeouts);
+ QByteArray result, expected;
+ expected.resize(timeouts.size());
+ result.reserve(timeouts.size());
+ for (int i = 0; i < timeouts.size(); ++i) {
+ char c = 'A' + i;
+ expected[i] = c;
+ QTimer::singleShot(timeouts[i], this, [&result, c = c]() {
+ result.append(c);
+ });
+ }
+
+ QTestEventLoop::instance().enterLoopMSecs(timeouts.last() * 2 + 10);
+
+ QCOMPARE(result, expected);
+}
+
void tst_QTimer::remainingTime()
{
QTimer tested;
diff --git a/tests/auto/gui/kernel/qguitimer/CMakeLists.txt b/tests/auto/gui/kernel/qguitimer/CMakeLists.txt
index cf63294fae..82186cce43 100644
--- a/tests/auto/gui/kernel/qguitimer/CMakeLists.txt
+++ b/tests/auto/gui/kernel/qguitimer/CMakeLists.txt
@@ -12,3 +12,16 @@ qt_internal_add_test(tst_qguitimer
Qt::CorePrivate
Qt::Gui
)
+
+if(QT_FEATURE_glib AND UNIX)
+ # only exists as a different dispatcher for XCB
+ qt_internal_add_test(tst_qguitimer_no_glib
+ SOURCES
+ ../../../corelib/kernel/qtimer/tst_qtimer.cpp
+ DEFINES
+ DISABLE_GLIB
+ LIBRARIES
+ Qt::CorePrivate
+ Qt::Gui
+ )
+endif()