summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/kernel')
-rw-r--r--tests/auto/corelib/kernel/CMakeLists.txt19
-rw-r--r--tests/auto/corelib/kernel/qapplicationstatic/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qapplicationstatic/tst_qapplicationstatic.cpp2
-rw-r--r--tests/auto/corelib/kernel/qchronotimer/.gitignore1
-rw-r--r--tests/auto/corelib/kernel/qchronotimer/CMakeLists.txt33
-rw-r--r--tests/auto/corelib/kernel/qchronotimer/tst_qchronotimer.cpp1266
-rw-r--r--tests/auto/corelib/kernel/qcoreapplication/CMakeLists.txt31
-rw-r--r--tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp132
-rw-r--r--tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h7
-rw-r--r--tests/auto/corelib/kernel/qdeadlinetimer/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp450
-rw-r--r--tests/auto/corelib/kernel/qelapsedtimer/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp26
-rw-r--r--tests/auto/corelib/kernel/qeventdispatcher/CMakeLists.txt33
-rw-r--r--tests/auto/corelib/kernel/qeventdispatcher/tst_qeventdispatcher.cpp140
-rw-r--r--tests/auto/corelib/kernel/qeventloop/BLACKLIST2
-rw-r--r--tests/auto/corelib/kernel/qeventloop/CMakeLists.txt13
-rw-r--r--tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp40
-rw-r--r--tests/auto/corelib/kernel/qjniarray/CMakeLists.txt13
-rw-r--r--tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp165
-rw-r--r--tests/auto/corelib/kernel/qjnienvironment/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java26
-rw-r--r--tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp138
-rw-r--r--tests/auto/corelib/kernel/qjniobject/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java175
-rw-r--r--tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp674
-rw-r--r--tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp246
-rw-r--r--tests/auto/corelib/kernel/qmath/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qmath/tst_qmath.cpp2
-rw-r--r--tests/auto/corelib/kernel/qmetacontainer/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qmetacontainer/tst_qmetacontainer.cpp10
-rw-r--r--tests/auto/corelib/kernel/qmetaenum/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp5
-rw-r--r--tests/auto/corelib/kernel/qmetamethod/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp48
-rw-r--r--tests/auto/corelib/kernel/qmetaobject/CMakeLists.txt31
-rw-r--r--tests/auto/corelib/kernel/qmetaobject/forwarddeclared.cpp17
-rw-r--r--tests/auto/corelib/kernel/qmetaobject/forwarddeclared.h12
-rw-r--r--tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp1074
-rw-r--r--tests/auto/corelib/kernel/qmetaobjectbuilder/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp33
-rw-r--r--tests/auto/corelib/kernel/qmetaproperty/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp30
-rw-r--r--tests/auto/corelib/kernel/qmetatype/CMakeLists.txt48
-rw-r--r--tests/auto/corelib/kernel/qmetatype/lib1.cpp5
-rw-r--r--tests/auto/corelib/kernel/qmetatype/lib2.cpp5
-rw-r--r--tests/auto/corelib/kernel/qmetatype/lib_common.cpp13
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp841
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h27
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp268
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp2
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h5
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h24
-rw-r--r--tests/auto/corelib/kernel/qmimedata/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp18
-rw-r--r--tests/auto/corelib/kernel/qobject/CMakeLists.txt13
-rw-r--r--tests/auto/corelib/kernel/qobject/signalbug/CMakeLists.txt3
-rw-r--r--tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp2
-rw-r--r--tests/auto/corelib/kernel/qobject/signalbug/signalbug.h2
-rw-r--r--tests/auto/corelib/kernel/qobject/tst_qobject.cpp705
-rw-r--r--tests/auto/corelib/kernel/qpermission/.gitignore1
-rw-r--r--tests/auto/corelib/kernel/qpermission/CMakeLists.txt19
-rw-r--r--tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp284
-rw-r--r--tests/auto/corelib/kernel/qpointer/CMakeLists.txt17
-rw-r--r--tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp104
-rw-r--r--tests/auto/corelib/kernel/qproperty/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp649
-rw-r--r--tests/auto/corelib/kernel/qsharedmemory/CMakeLists.txt24
-rw-r--r--tests/auto/corelib/kernel/qsharedmemory/producerconsumer/CMakeLists.txt12
-rw-r--r--tests/auto/corelib/kernel/qsharedmemory/producerconsumer/main.cpp173
-rw-r--r--tests/auto/corelib/kernel/qsharedmemory/tst_qsharedmemory.cpp811
-rw-r--r--tests/auto/corelib/kernel/qsignalblocker/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp13
-rw-r--r--tests/auto/corelib/kernel/qsignalmapper/CMakeLists.txt9
-rw-r--r--tests/auto/corelib/kernel/qsignalmapper/tst_qsignalmapper.cpp2
-rw-r--r--tests/auto/corelib/kernel/qsocketnotifier/BLACKLIST3
-rw-r--r--tests/auto/corelib/kernel/qsocketnotifier/CMakeLists.txt16
-rw-r--r--tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp30
-rw-r--r--tests/auto/corelib/kernel/qsystemsemaphore/CMakeLists.txt13
-rw-r--r--tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/CMakeLists.txt12
-rw-r--r--tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/main.cpp76
-rw-r--r--tests/auto/corelib/kernel/qsystemsemaphore/tst_qsystemsemaphore.cpp271
-rw-r--r--tests/auto/corelib/kernel/qtimer/BLACKLIST0
-rw-r--r--tests/auto/corelib/kernel/qtimer/CMakeLists.txt43
-rw-r--r--tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp379
-rw-r--r--tests/auto/corelib/kernel/qtranslator/CMakeLists.txt17
-rw-r--r--tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp8
-rw-r--r--tests/auto/corelib/kernel/qvariant/CMakeLists.txt14
-rw-r--r--tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp1960
-rw-r--r--tests/auto/corelib/kernel/qwineventnotifier/CMakeLists.txt11
-rw-r--r--tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp2
-rw-r--r--tests/auto/corelib/kernel/qwinregistrykey/.gitignore1
-rw-r--r--tests/auto/corelib/kernel/qwinregistrykey/CMakeLists.txt15
-rw-r--r--tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp242
95 files changed, 9552 insertions, 2703 deletions
diff --git a/tests/auto/corelib/kernel/CMakeLists.txt b/tests/auto/corelib/kernel/CMakeLists.txt
index 3083652945..c2feb38641 100644
--- a/tests/auto/corelib/kernel/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/CMakeLists.txt
@@ -1,6 +1,8 @@
-# Generated from kernel.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
add_subdirectory(qapplicationstatic)
+add_subdirectory(qchronotimer)
add_subdirectory(qcoreapplication)
add_subdirectory(qdeadlinetimer)
add_subdirectory(qelapsedtimer)
@@ -11,12 +13,11 @@ add_subdirectory(qmetaobjectbuilder)
add_subdirectory(qmetamethod)
add_subdirectory(qmetaproperty)
add_subdirectory(qmetaenum)
-add_subdirectory(qpointer)
add_subdirectory(qsignalblocker)
add_subdirectory(qsignalmapper)
add_subdirectory(qtimer)
add_subdirectory(qtranslator)
-# QTBUG-88135 # special case
+# QTBUG-88135
if(NOT ANDROID)
add_subdirectory(qeventdispatcher)
endif()
@@ -26,22 +27,21 @@ endif()
if(TARGET Qt::Gui)
add_subdirectory(qmetatype)
add_subdirectory(qmimedata)
+ add_subdirectory(qpointer)
add_subdirectory(qvariant)
endif()
if(TARGET Qt::Network AND NOT ANDROID AND NOT UIKIT)
add_subdirectory(qobject)
endif()
-if(QT_FEATURE_private_tests AND NOT ANDROID AND NOT UIKIT)
- add_subdirectory(qsharedmemory)
-endif()
if(QT_FEATURE_private_tests AND TARGET Qt::Network)
add_subdirectory(qsocketnotifier)
endif()
-if(QT_FEATURE_systemsemaphore AND NOT ANDROID AND NOT UIKIT)
- add_subdirectory(qsystemsemaphore)
-endif()
if(WIN32)
add_subdirectory(qwineventnotifier)
+ add_subdirectory(qwinregistrykey)
+endif()
+if(QT_FEATURE_permissions)
+ add_subdirectory(qpermission)
endif()
if(QT_FEATURE_private_tests)
add_subdirectory(qproperty)
@@ -50,4 +50,5 @@ if(ANDROID)
add_subdirectory(qjnienvironment)
add_subdirectory(qjniobject)
add_subdirectory(qjnitypes)
+ add_subdirectory(qjniarray)
endif()
diff --git a/tests/auto/corelib/kernel/qapplicationstatic/CMakeLists.txt b/tests/auto/corelib/kernel/qapplicationstatic/CMakeLists.txt
index 0890113c55..9332b6ecd0 100644
--- a/tests/auto/corelib/kernel/qapplicationstatic/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qapplicationstatic/CMakeLists.txt
@@ -1,7 +1,16 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
#####################################################################
## tst_qapplicationstatic Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qapplicationstatic LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qapplicationstatic
SOURCES
tst_qapplicationstatic.cpp
diff --git a/tests/auto/corelib/kernel/qapplicationstatic/tst_qapplicationstatic.cpp b/tests/auto/corelib/kernel/qapplicationstatic/tst_qapplicationstatic.cpp
index 987a20a589..dd9a415a52 100644
--- a/tests/auto/corelib/kernel/qapplicationstatic/tst_qapplicationstatic.cpp
+++ b/tests/auto/corelib/kernel/qapplicationstatic/tst_qapplicationstatic.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QPointer>
diff --git a/tests/auto/corelib/kernel/qchronotimer/.gitignore b/tests/auto/corelib/kernel/qchronotimer/.gitignore
new file mode 100644
index 0000000000..254f7a0281
--- /dev/null
+++ b/tests/auto/corelib/kernel/qchronotimer/.gitignore
@@ -0,0 +1 @@
+tst_qchronotimer
diff --git a/tests/auto/corelib/kernel/qchronotimer/CMakeLists.txt b/tests/auto/corelib/kernel/qchronotimer/CMakeLists.txt
new file mode 100644
index 0000000000..43164858c5
--- /dev/null
+++ b/tests/auto/corelib/kernel/qchronotimer/CMakeLists.txt
@@ -0,0 +1,33 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qchronotimer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if (NOT QT_FEATURE_thread)
+ return()
+endif()
+
+function(addChronoTimerTest test)
+ qt_internal_add_test(${test}
+ SOURCES
+ tst_qchronotimer.cpp
+ LIBRARIES
+ Qt::CorePrivate
+ Qt::TestPrivate
+ )
+endfunction()
+
+addChronoTimerTest(tst_qchronotimer)
+
+if(QT_FEATURE_glib AND UNIX)
+ addChronoTimerTest(tst_qchronotimer_no_glib)
+ qt_internal_extend_target(tst_qchronotimer_no_glib
+ DEFINES
+ DISABLE_GLIB
+ tst_QChronoTimer=tst_QChronoTimer_no_glib # Class name in the unittest
+ )
+endif()
diff --git a/tests/auto/corelib/kernel/qchronotimer/tst_qchronotimer.cpp b/tests/auto/corelib/kernel/qchronotimer/tst_qchronotimer.cpp
new file mode 100644
index 0000000000..62c402ae24
--- /dev/null
+++ b/tests/auto/corelib/kernel/qchronotimer/tst_qchronotimer.cpp
@@ -0,0 +1,1266 @@
+// Copyright (C) 2020 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#ifdef QT_GUI_LIB
+# include <QtGui/QGuiApplication>
+#else
+# include <QtCore/QCoreApplication>
+#endif
+
+#include <QtCore/private/qglobal_p.h>
+#include <QTest>
+#include <QSignalSpy>
+#include <QtTest/private/qpropertytesthelper_p.h>
+
+#include <qbasictimer.h>
+#include <qchronotimer.h>
+#include <qthread.h>
+#include <qtimer.h>
+#include <qelapsedtimer.h>
+#include <qproperty.h>
+
+#if defined Q_OS_UNIX
+#include <unistd.h>
+#endif
+
+using namespace std::chrono_literals;
+
+#ifdef DISABLE_GLIB
+static bool glibDisabled = []() {
+ qputenv("QT_NO_GLIB", "1");
+ return true;
+}();
+#endif
+
+class tst_QChronoTimer : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void zeroTimer();
+ void singleShotTimeout(); // Non-static singleShot()
+ void timeout();
+ void sequentialTimers_data();
+ void sequentialTimers();
+ void remainingTime();
+ void remainingTimeInitial_data();
+ void remainingTimeInitial();
+ void remainingTimeDuringActivation_data();
+ void remainingTimeDuringActivation();
+ void basic_chrono();
+ void livelock_data();
+ void livelock();
+ void timerInfiniteRecursion_data();
+ void timerInfiniteRecursion();
+ void recurringTimer_data();
+ void recurringTimer();
+ void deleteLaterOnQChronoTimer(); // long name, don't want to shadow QObject::deleteLater()
+ void moveToThread();
+ void restartedTimerFiresTooSoon();
+ void timerFiresOnlyOncePerProcessEvents_data();
+ void timerFiresOnlyOncePerProcessEvents();
+ void timerIdPersistsAfterThreadExit();
+ void cancelLongTimer();
+ void recurseOnTimeoutAndStopTimer();
+ void timerOrder();
+ void timerOrder_data();
+ void timerOrderBackgroundThread();
+ void timerOrderBackgroundThread_data() { timerOrder_data(); }
+ void timerPrecision();
+
+ void dontBlockEvents();
+ void postedEventsShouldNotStarveTimers();
+ void callOnTimeout();
+
+ void bindToTimer();
+ void bindTimer();
+ void automatedBindingTests();
+
+ void negativeInterval();
+};
+
+void tst_QChronoTimer::zeroTimer()
+{
+ QChronoTimer timer;
+ QVERIFY(!timer.isSingleShot());
+ timer.setInterval(0ns);
+ timer.setSingleShot(true);
+ QVERIFY(timer.isSingleShot());
+
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.start();
+
+ // Pass timeout to work round glib issue, see QTBUG-84291.
+ QCoreApplication::processEvents(QEventLoop::AllEvents, INT_MAX);
+
+ QCOMPARE(timeoutSpy.size(), 1);
+}
+
+void tst_QChronoTimer::singleShotTimeout()
+{
+ QChronoTimer timer;
+ QVERIFY(!timer.isSingleShot());
+ timer.setSingleShot(true);
+ QVERIFY(timer.isSingleShot());
+
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.setInterval(100ms);
+ timer.start();
+
+ QVERIFY(timeoutSpy.wait(500ms));
+ QCOMPARE(timeoutSpy.size(), 1);
+ QTest::qWait(500ms);
+ QCOMPARE(timeoutSpy.size(), 1);
+}
+
+static constexpr auto Timeout_Interval = 200ms;
+
+void tst_QChronoTimer::timeout()
+{
+ QChronoTimer timer{100ms};
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.start();
+
+ QCOMPARE(timeoutSpy.size(), 0);
+
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > 0, Timeout_Interval);
+ const qsizetype oldCount = timeoutSpy.size();
+
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > oldCount, Timeout_Interval);
+}
+
+void tst_QChronoTimer::sequentialTimers_data()
+{
+#ifdef Q_OS_WIN
+ QSKIP("The API used by QEventDispatcherWin32 doesn't respect the order");
+#endif
+ QTest::addColumn<QList<std::chrono::milliseconds>>("timeouts");
+ auto addRow = [](const QList<std::chrono::milliseconds> &l) {
+ Q_ASSERT_X(std::is_sorted(l.begin(), l.end()),
+ "tst_QChronoTimer", "input list must be sorted");
+ QByteArray name;
+ for (auto msec : l)
+ name += QByteArray::number(msec.count()) + ',';
+ name.chop(1);
+ QTest::addRow("%s", name.constData()) << l;
+ };
+ // PreciseTimers
+ addRow({0ms, 0ms, 0ms, 0ms, 0ms, 0ms});
+ addRow({0ms, 1ms, 2ms});
+ addRow({1ms, 1ms, 1ms, 2ms, 2ms, 2ms, 2ms});
+ addRow({1ms, 2ms, 3ms});
+ addRow({19ms, 19ms, 19ms});
+ // CoarseTimer for setinterval
+ addRow({20ms, 20ms, 20ms, 20ms, 20ms});
+ addRow({25ms, 25ms, 25ms, 25ms, 25ms, 25ms, 50ms});
+}
+
+void tst_QChronoTimer::sequentialTimers()
+{
+ QFETCH(const QList<std::chrono::milliseconds>, timeouts);
+ QByteArray result, expected;
+ std::vector<std::unique_ptr<QChronoTimer>> 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<QChronoTimer>(timeouts[i]);
+ timer->setSingleShot(true);
+
+ char c = 'A' + i;
+ expected[i] = c;
+ QObject::connect(timer.get(), &QChronoTimer::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().enterLoop(timeouts.last() * 2 + 10ms);
+
+ QCOMPARE(result, expected);
+}
+
+void tst_QChronoTimer::remainingTime()
+{
+ QChronoTimer tested;
+ tested.setTimerType(Qt::PreciseTimer);
+
+ QChronoTimer tester;
+ tester.setTimerType(Qt::PreciseTimer);
+ tester.setSingleShot(true);
+
+ constexpr auto tested_interval = 200ms;
+ constexpr auto tester_interval = 50ms;
+ constexpr auto expectedRemainingTime = tested_interval - tester_interval;
+
+ int testIteration = 0;
+ const int desiredTestCount = 2;
+
+ // We let tested (which isn't a single-shot) run repeatedly, to verify
+ // it *does* repeat, and check that the single-shot tester, starting
+ // at the same time, does finish first each time, by about the right duration.
+ auto connection = QObject::connect(&tested, &QChronoTimer::timeout,
+ &tester, &QChronoTimer::start);
+
+ QObject::connect(&tester, &QChronoTimer::timeout, this, [&]() {
+ const std::chrono::nanoseconds remainingTime = tested.remainingTime();
+ // We expect that remainingTime is at most 150 and not overdue.
+ const bool remainingTimeInRange = remainingTime > 0ns
+ && remainingTime <= expectedRemainingTime;
+ if (remainingTimeInRange)
+ ++testIteration;
+ else
+ testIteration = desiredTestCount; // We are going to fail on QVERIFY2()
+ // below, so we don't want to iterate
+ // anymore and quickly exit the QTRY_...()
+ // with this failure.
+ if (testIteration == desiredTestCount)
+ QObject::disconnect(connection); // Last iteration, don't start tester again.
+ QVERIFY2(remainingTimeInRange, qPrintable("Remaining time "
+ + QByteArray::number(remainingTime.count()) + "ms outside expected range (0ns, "
+ + QByteArray::number(expectedRemainingTime.count()) + "ms]"));
+ });
+
+ tested.setInterval(tested_interval);
+ tested.start();
+ tester.setInterval(tester_interval);
+ tester.start(); // Start tester for the 1st time.
+
+ // Test it desiredTestCount times, give it reasonable amount of time
+ // (twice as much as needed).
+ const auto tryTimeout = tested_interval * desiredTestCount * 2;
+ QTRY_COMPARE_WITH_TIMEOUT(testIteration, desiredTestCount, tryTimeout);
+}
+
+void tst_QChronoTimer::remainingTimeInitial_data()
+{
+ using namespace std::chrono;
+
+ QTest::addColumn<nanoseconds>("startTimeNs");
+ QTest::addColumn<Qt::TimerType>("timerType");
+
+ QTest::addRow("precisetiemr-0ns") << 0ns << Qt::PreciseTimer;
+ QTest::addRow("precisetimer-1ms") << nanoseconds{1ms} << Qt::PreciseTimer;
+ QTest::addRow("precisetimer-10ms") <<nanoseconds{10ms} << Qt::PreciseTimer;
+
+ QTest::addRow("coarsetimer-0ns") << 0ns << Qt::CoarseTimer;
+ QTest::addRow("coarsetimer-1ms") << nanoseconds{1ms} << Qt::CoarseTimer;
+ QTest::addRow("coarsetimer-10ms") << nanoseconds{10ms} << Qt::CoarseTimer;
+}
+
+void tst_QChronoTimer::remainingTimeInitial()
+{
+ QFETCH(std::chrono::nanoseconds, startTimeNs);
+ QFETCH(Qt::TimerType, timerType);
+
+ QChronoTimer timer;
+ QCOMPARE(timer.timerType(), Qt::CoarseTimer);
+ timer.setTimerType(timerType);
+ QCOMPARE(timer.timerType(), timerType);
+ timer.setInterval(startTimeNs);
+ timer.start();
+
+ const std::chrono::nanoseconds rt = timer.remainingTime();
+ QCOMPARE_GE(rt, 0ns);
+ QCOMPARE_LE(rt, startTimeNs);
+}
+
+void tst_QChronoTimer::remainingTimeDuringActivation_data()
+{
+ QTest::addColumn<bool>("singleShot");
+ QTest::newRow("repeating") << false;
+ QTest::newRow("single-shot") << true;
+}
+
+void tst_QChronoTimer::remainingTimeDuringActivation()
+{
+ QFETCH(bool, singleShot);
+
+ QChronoTimer timer;
+ timer.setSingleShot(singleShot);
+
+ auto remainingTime = 0ns; // not the expected value in either case
+ connect(&timer, &QChronoTimer::timeout, this, [&]() { remainingTime = timer.remainingTime(); });
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ // 20 ms is short enough and should not round down to 0 in any timer mode
+ constexpr auto timeout = 20ms;
+ timer.setInterval(timeout);
+ timer.start();
+
+ QVERIFY(timeoutSpy.wait());
+ if (singleShot)
+ QCOMPARE_LT(remainingTime, 0ns); // timer not running
+ else {
+ QCOMPARE_LE(remainingTime, timeout);
+ QCOMPARE_GT(remainingTime, 0ns);
+ }
+
+ if (!singleShot) {
+ // do it again - see QTBUG-46940
+ remainingTime = std::chrono::milliseconds::min();
+ QVERIFY(timeoutSpy.wait());
+ QCOMPARE_LE(remainingTime, timeout);
+ QCOMPARE_GT(remainingTime, 0ns);
+ }
+}
+
+namespace {
+ template <typename T>
+ auto to_ms(T t)
+ {
+ using namespace std::chrono;
+ return duration_cast<milliseconds>(t);
+ }
+} // unnamed namespace
+
+void tst_QChronoTimer::basic_chrono()
+{
+ // duplicates zeroTimer, singleShotTimeout, interval and remainingTime
+ using namespace std::chrono;
+ QChronoTimer timer;
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.start();
+ QCOMPARE(timer.interval(), 0ns);
+ QCOMPARE(timer.remainingTime(), 0ns);
+
+ QCoreApplication::processEvents();
+
+ QCOMPARE(timeoutSpy.size(), 1);
+
+ timeoutSpy.clear();
+ timer.setInterval(100ms);
+ timer.start();
+ QCOMPARE(timeoutSpy.size(), 0);
+
+ QVERIFY(timeoutSpy.wait(Timeout_Interval));
+ QVERIFY(timeoutSpy.size() > 0);
+ const qsizetype oldCount = timeoutSpy.size();
+
+ QVERIFY(timeoutSpy.wait(Timeout_Interval));
+ QVERIFY(timeoutSpy.size() > oldCount);
+
+ timeoutSpy.clear();
+ timer.setInterval(200ms);
+ timer.start();
+ QCOMPARE(timer.interval(), 200ms);
+ QTest::qWait(50ms);
+ QCOMPARE(timeoutSpy.size(), 0);
+
+ nanoseconds rt = timer.remainingTime();
+ QCOMPARE_GE(rt, 50ms);
+ QCOMPARE_LE(rt, 200ms);
+
+ timeoutSpy.clear();
+ timer.setSingleShot(true);
+ timer.setInterval(100ms);
+ timer.start();
+ QVERIFY(timeoutSpy.wait(Timeout_Interval));
+ QCOMPARE(timeoutSpy.size(), 1);
+ QTest::qWait(500ms);
+ QCOMPARE(timeoutSpy.size(), 1);
+}
+
+void tst_QChronoTimer::livelock_data()
+{
+ QTest::addColumn<std::chrono::nanoseconds>("interval");
+ QTest::newRow("zero-timer") << 0ns;
+ QTest::newRow("non-zero-timer") << std::chrono::nanoseconds{1ms};
+ QTest::newRow("longer-than-sleep") << std::chrono::nanoseconds{20ms};
+}
+
+/*!
+ *
+ * DO NOT "FIX" THIS TEST! it is written like this for a reason, do
+ * not *change it without first dicussing it with its maintainers.
+ *
+*/
+class LiveLockTester : public QObject
+{
+ static constexpr QEvent::Type PostEventType = static_cast<QEvent::Type>(4002);
+public:
+ LiveLockTester(std::chrono::nanoseconds i)
+ : interval(i)
+ {
+ firstTimerId = startTimer(interval);
+ extraTimerId = startTimer(interval + 80ms);
+ secondTimerId = -1; // started later
+ }
+
+ bool event(QEvent *e) override
+ {
+ if (e->type() == PostEventType) {
+ // got the posted event
+ if (timeoutsForFirst == 1 && timeoutsForSecond == 0)
+ postEventAtRightTime = true;
+ return true;
+ }
+ return QObject::event(e);
+ }
+
+ void timerEvent(QTimerEvent *te) override
+ {
+ if (te->timerId() == firstTimerId) {
+ if (++timeoutsForFirst == 1) {
+ killTimer(extraTimerId);
+ extraTimerId = -1;
+ QCoreApplication::postEvent(this, new QEvent(PostEventType));
+ secondTimerId = startTimer(interval);
+ }
+ } else if (te->timerId() == secondTimerId) {
+ ++timeoutsForSecond;
+ } else if (te->timerId() == extraTimerId) {
+ ++timeoutsForExtra;
+ }
+
+ // sleep for 2ms
+ QTest::qSleep(2);
+ killTimer(te->timerId());
+ }
+
+ const std::chrono::nanoseconds interval;
+ int firstTimerId = -1;
+ int secondTimerId = -1;
+ int extraTimerId = -1;
+ int timeoutsForFirst = 0;
+ int timeoutsForExtra = 0;
+ int timeoutsForSecond = 0;
+ bool postEventAtRightTime = false;
+};
+
+void tst_QChronoTimer::livelock()
+{
+ /*
+ New timers created in timer event handlers should not be sent
+ until the next iteration of the eventloop. Note: this test
+ depends on the fact that we send posted events before timer
+ events (since new posted events are not sent until the next
+ iteration of the eventloop either).
+ */
+ QFETCH(std::chrono::nanoseconds, interval);
+ LiveLockTester tester(interval);
+ QTest::qWait(180ms); // we have to use wait here, since we're testing timers with a non-zero timeout
+ QTRY_COMPARE(tester.timeoutsForFirst, 1);
+ QCOMPARE(tester.timeoutsForExtra, 0);
+ QTRY_COMPARE(tester.timeoutsForSecond, 1);
+ QVERIFY(tester.postEventAtRightTime);
+}
+
+class TimerInfiniteRecursionObject : public QObject
+{
+public:
+ bool inTimerEvent = false;
+ bool timerEventRecursed = false;
+ std::chrono::nanoseconds interval;
+
+ TimerInfiniteRecursionObject(std::chrono::nanoseconds interval)
+ : interval(interval)
+ { }
+
+ void timerEvent(QTimerEvent *timerEvent) override
+ {
+ timerEventRecursed = inTimerEvent;
+ if (timerEventRecursed) {
+ // bug detected!
+ return;
+ }
+
+ inTimerEvent = true;
+
+ QEventLoop eventLoop;
+ QChronoTimer::singleShot(std::max<std::chrono::nanoseconds>(100ms, interval * 2),
+ &eventLoop, &QEventLoop::quit);
+ eventLoop.exec();
+
+ inTimerEvent = false;
+
+ killTimer(timerEvent->timerId());
+ }
+};
+
+void tst_QChronoTimer::timerInfiniteRecursion_data()
+{
+ QTest::addColumn<std::chrono::nanoseconds>("interval");
+ QTest::newRow("zero timer") << 0ns;
+ QTest::newRow("non-zero timer") << std::chrono::nanoseconds{1ms};
+ QTest::newRow("10ms timer") << std::chrono::nanoseconds{10ms};
+ QTest::newRow("11ms timer") << std::chrono::nanoseconds{11ms};
+ QTest::newRow("100ms timer") << std::chrono::nanoseconds{100ms};
+ QTest::newRow("1s timer") << std::chrono::nanoseconds{1000ms};
+}
+
+
+void tst_QChronoTimer::timerInfiniteRecursion()
+{
+ QFETCH(std::chrono::nanoseconds, interval);
+ TimerInfiniteRecursionObject object(interval);
+ (void) object.startTimer(interval);
+
+ QEventLoop eventLoop;
+ QChronoTimer::singleShot(std::max<std::chrono::nanoseconds>(100ms, interval * 2), &eventLoop,
+ &QEventLoop::quit);
+ eventLoop.exec();
+
+ QVERIFY(!object.timerEventRecursed);
+}
+
+class RecurringTimerObject : public QObject
+{
+Q_OBJECT
+public:
+ int times;
+ int target;
+ bool recurse;
+
+ RecurringTimerObject(int target)
+ : times(0), target(target), recurse(false)
+ { }
+
+ void timerEvent(QTimerEvent *timerEvent) override
+ {
+ if (++times == target) {
+ killTimer(timerEvent->timerId());
+ Q_EMIT done();
+ } if (recurse) {
+ QEventLoop eventLoop;
+ QChronoTimer::singleShot(100ms, &eventLoop, &QEventLoop::quit);
+ eventLoop.exec();
+ }
+ }
+
+signals:
+ void done();
+};
+
+void tst_QChronoTimer::recurringTimer_data()
+{
+ QTest::addColumn<std::chrono::nanoseconds>("interval");
+ QTest::addColumn<bool>("recurse");
+ // make sure that eventloop recursion doesn't affect timer recurrence
+ QTest::newRow("zero timer, don't recurse") << 0ns << false;
+ QTest::newRow("zero timer, recurse") << 0ns << true;
+ QTest::newRow("non-zero timer, don't recurse") << std::chrono::nanoseconds{1ms} << false;
+ QTest::newRow("non-zero timer, recurse") << std::chrono::nanoseconds{1ms} << true;
+}
+
+void tst_QChronoTimer::recurringTimer()
+{
+ const int target = 5;
+ QFETCH(std::chrono::nanoseconds, interval);
+ QFETCH(bool, recurse);
+
+ RecurringTimerObject object(target);
+ object.recurse = recurse;
+ QSignalSpy doneSpy(&object, &RecurringTimerObject::done);
+
+ (void) object.startTimer(interval);
+ QVERIFY(doneSpy.wait());
+
+ QCOMPARE(object.times, target);
+}
+
+void tst_QChronoTimer::deleteLaterOnQChronoTimer()
+{
+ QChronoTimer *timer = new QChronoTimer;
+ connect(timer, &QChronoTimer::timeout, timer, &QObject::deleteLater);
+ QSignalSpy destroyedSpy(timer, &QObject::destroyed);
+ timer->setInterval(1ms);
+ timer->setSingleShot(true);
+ timer->start();
+ QPointer<QChronoTimer> pointer = timer;
+ QVERIFY(destroyedSpy.wait());
+ QVERIFY(pointer.isNull());
+}
+
+namespace {
+int operator&(Qt::TimerId id, int i) { return qToUnderlying(id) & i; }
+}
+
+static constexpr auto MoveToThread_Timeout = 200ms;
+static constexpr auto MoveToThread_Wait = 300ms;
+
+void tst_QChronoTimer::moveToThread()
+{
+#if defined(Q_OS_WIN32)
+ QSKIP("Does not work reliably on Windows :(");
+#elif defined(Q_OS_MACOS)
+ QSKIP("Does not work reliably on macOS 10.12+ (QTBUG-59679)");
+#endif
+ QChronoTimer timer1{MoveToThread_Timeout};
+ QChronoTimer timer2{MoveToThread_Timeout};
+ timer1.setSingleShot(true);
+ timer1.start();
+ timer2.start();
+ QVERIFY((timer1.id() & 0xffffff) != (timer2.id() & 0xffffff));
+ QThread tr;
+ timer1.moveToThread(&tr);
+ connect(&timer1, &QChronoTimer::timeout, &tr, &QThread::quit);
+ tr.start();
+ QChronoTimer ti3{MoveToThread_Timeout};
+ ti3.start();
+ QVERIFY((ti3.id() & 0xffffff) != (timer2.id() & 0xffffff));
+ QVERIFY((ti3.id() & 0xffffff) != (timer1.id() & 0xffffff));
+ QTest::qWait(MoveToThread_Wait);
+ QVERIFY(tr.wait());
+ timer2.stop();
+ QChronoTimer ti4{MoveToThread_Timeout};
+ ti4.start();
+ ti3.stop();
+ timer2.setInterval(MoveToThread_Timeout);
+ timer2.start();
+ ti3.setInterval(MoveToThread_Timeout);
+ ti3.start();
+ QVERIFY((ti4.id() & 0xffffff) != (timer2.id() & 0xffffff));
+ QVERIFY((ti3.id() & 0xffffff) != (timer2.id() & 0xffffff));
+ QVERIFY((ti3.id() & 0xffffff) != (timer1.id() & 0xffffff));
+}
+
+class RestartedTimerFiresTooSoonObject : public QObject
+{
+ Q_OBJECT
+
+public:
+ QBasicTimer m_timer;
+
+ std::chrono::milliseconds m_interval = 0ms;
+ QElapsedTimer m_elapsedTimer;
+ QEventLoop eventLoop;
+
+ RestartedTimerFiresTooSoonObject() = default;
+
+ void timerFired()
+ {
+ static std::chrono::milliseconds interval = 1s;
+
+ m_interval = interval;
+ m_elapsedTimer.start();
+ m_timer.start(interval, this);
+
+ // alternate between single-shot and 1 sec
+ interval = interval > 0ms ? 0ms : 1s;
+ }
+
+ void timerEvent(QTimerEvent* ev) override
+ {
+ if (ev->timerId() != m_timer.timerId())
+ return;
+
+ m_timer.stop();
+
+ std::chrono::nanoseconds elapsed = m_elapsedTimer.durationElapsed();
+
+ if (elapsed < m_interval / 2) {
+ // severely too early!
+ m_timer.stop();
+ eventLoop.exit(-1);
+ return;
+ }
+
+ timerFired();
+
+ // don't do this forever
+ static int count = 0;
+ if (count++ > 20) {
+ m_timer.stop();
+ eventLoop.quit();
+ return;
+ }
+ }
+};
+
+void tst_QChronoTimer::restartedTimerFiresTooSoon()
+{
+ RestartedTimerFiresTooSoonObject object;
+ object.timerFired();
+ QCOMPARE(object.eventLoop.exec(), 0);
+}
+
+class LongLastingSlotClass : public QObject
+{
+ Q_OBJECT
+
+public:
+ LongLastingSlotClass(QChronoTimer *timer) : timer(timer) { }
+
+public slots:
+ void longLastingSlot()
+ {
+ // Don't use QChronoTimer for this, because we are testing it.
+ QElapsedTimer control;
+ control.start();
+ while (control.durationElapsed() < 200ms) {
+ for (int c = 0; c < 100'000; c++) {} // Mindless looping.
+ }
+ if (++count >= 2) {
+ timer->stop();
+ }
+ }
+
+public:
+ int count = 0;
+ QChronoTimer *timer;
+};
+
+void tst_QChronoTimer::timerFiresOnlyOncePerProcessEvents_data()
+{
+ QTest::addColumn<std::chrono::nanoseconds>("interval");
+ QTest::newRow("zero-timer") << 0ns;
+ QTest::newRow("non-zero-timer") << std::chrono::nanoseconds{10ms};
+}
+
+void tst_QChronoTimer::timerFiresOnlyOncePerProcessEvents()
+{
+ QFETCH(std::chrono::nanoseconds, interval);
+
+ QChronoTimer t{interval};
+ LongLastingSlotClass longSlot(&t);
+ t.start();
+ connect(&t, &QChronoTimer::timeout, &longSlot, &LongLastingSlotClass::longLastingSlot);
+ // Loop because there may be other events pending.
+ while (longSlot.count == 0)
+ QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+
+ QCOMPARE(longSlot.count, 1);
+}
+
+class TimerIdPersistsAfterThreadExitThread : public QThread
+{
+public:
+ std::unique_ptr<QChronoTimer> timer;
+ Qt::TimerId timerId = Qt::TimerId::Invalid;
+ int returnValue = -1;
+
+ void run() override
+ {
+ QEventLoop eventLoop;
+ timer = std::make_unique<QChronoTimer>();
+ connect(timer.get(), &QChronoTimer::timeout, &eventLoop, &QEventLoop::quit);
+ timer->setInterval(100ms);
+ timer->start();
+ timerId = timer->id();
+ returnValue = eventLoop.exec();
+ }
+};
+
+void tst_QChronoTimer::timerIdPersistsAfterThreadExit()
+{
+ TimerIdPersistsAfterThreadExitThread thread;
+ thread.start();
+ QVERIFY(thread.wait(30s));
+ QCOMPARE(thread.returnValue, 0);
+
+ // even though the thread has exited, and the event dispatcher destroyed, the timer is still
+ // "active", meaning the timer id should NOT be reused (i.e. the event dispatcher should not
+ // have unregistered it)
+ int timerId = thread.startTimer(100ms);
+ QVERIFY((timerId & 0xffffff) != (thread.timerId & 0xffffff));
+}
+
+void tst_QChronoTimer::cancelLongTimer()
+{
+ QChronoTimer timer{1h};
+ timer.setSingleShot(true);
+ timer.start();
+ QCoreApplication::processEvents();
+ // If the timer completes immediately with an error, then this will fail
+ QVERIFY(timer.isActive());
+ timer.stop();
+ QVERIFY(!timer.isActive());
+}
+
+class TimeoutCounter : public QObject
+{
+ Q_OBJECT
+public slots:
+ void timeout() { ++count; };
+public:
+ int count = 0;
+};
+
+class RecursOnTimeoutAndStopTimerTimer : public QObject
+{
+ Q_OBJECT
+
+public:
+ QChronoTimer *one;
+ QChronoTimer *two;
+
+public slots:
+ void onetrigger()
+ {
+ QCoreApplication::processEvents();
+ }
+
+ void twotrigger()
+ {
+ one->stop();
+ }
+};
+
+void tst_QChronoTimer::recurseOnTimeoutAndStopTimer()
+{
+ QEventLoop eventLoop;
+ QChronoTimer::singleShot(1s, &eventLoop, &QEventLoop::quit);
+
+ RecursOnTimeoutAndStopTimerTimer t;
+ t.one = new QChronoTimer(&t);
+ t.two = new QChronoTimer(&t);
+
+ QObject::connect(t.one, SIGNAL(timeout()), &t, SLOT(onetrigger()));
+ QObject::connect(t.two, SIGNAL(timeout()), &t, SLOT(twotrigger()));
+
+ t.two->setSingleShot(true);
+
+ t.one->start();
+ t.two->start();
+
+ (void) eventLoop.exec();
+
+ QVERIFY(!t.one->isActive());
+ QVERIFY(!t.two->isActive());
+}
+
+struct CountedStruct
+{
+ CountedStruct(int *count, QThread *t = nullptr) : count(count), thread(t) { }
+ ~CountedStruct() { }
+ void operator()() const { ++(*count); if (thread) QCOMPARE(QThread::currentThread(), thread); }
+
+ int *count;
+ QThread *thread;
+};
+
+static QScopedPointer<QEventLoop> _e;
+static QThread *_t = nullptr;
+
+class StaticEventLoop
+{
+public:
+ static void quitEventLoop()
+ {
+ quitEventLoop_noexcept();
+ }
+
+ static void quitEventLoop_noexcept() noexcept
+ {
+ QVERIFY(!_e.isNull());
+ _e->quit();
+ if (_t)
+ QCOMPARE(QThread::currentThread(), _t);
+ }
+};
+
+class DontBlockEvents : public QObject
+{
+ Q_OBJECT
+public:
+ DontBlockEvents();
+ void timerEvent(QTimerEvent*) override;
+
+ int count;
+ int total;
+ QBasicTimer m_timer;
+
+public slots:
+ void paintEvent();
+
+};
+
+DontBlockEvents::DontBlockEvents()
+{
+ count = 0;
+ total = 0;
+
+ const std::chrono::milliseconds intervals[] = {2s, 2500ms, 3s, 5s, 1s, 2s};
+ // need a few unrelated timers running to reproduce the bug.
+ for (auto dur : intervals) {
+ auto *t = new QChronoTimer(dur, this);
+ t->start();
+ }
+
+ m_timer.start(1ms, this);
+}
+
+void DontBlockEvents::timerEvent(QTimerEvent* event)
+{
+ if (event->timerId() == m_timer.timerId()) {
+ QMetaObject::invokeMethod(this, &DontBlockEvents::paintEvent, Qt::QueuedConnection);
+ m_timer.start(0ms, this);
+ count++;
+ QCOMPARE(count, 1);
+ total++;
+ }
+}
+
+void DontBlockEvents::paintEvent()
+{
+ count--;
+ QCOMPARE(count, 0);
+}
+
+// This is a regression test for QTBUG-13633, where a timer with a zero
+// timeout that was restarted by the event handler could starve other timers.
+void tst_QChronoTimer::dontBlockEvents()
+{
+ DontBlockEvents t;
+ QTest::qWait(60ms);
+ QTRY_VERIFY(t.total > 2);
+}
+
+class SlotRepeater : public QObject {
+ Q_OBJECT
+public:
+ SlotRepeater() {}
+
+public slots:
+ void repeatThisSlot()
+ {
+ QMetaObject::invokeMethod(this, &SlotRepeater::repeatThisSlot, Qt::QueuedConnection);
+ }
+};
+
+void tst_QChronoTimer::postedEventsShouldNotStarveTimers()
+{
+ QChronoTimer timer;
+ timer.setInterval(0ns);
+ timer.setSingleShot(false);
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.start();
+ SlotRepeater slotRepeater;
+ slotRepeater.repeatThisSlot();
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > 5, 100);
+}
+
+struct DummyFunctor {
+ static QThread *callThread;
+ void operator()() {
+ callThread = QThread::currentThread();
+ callThread->quit();
+ }
+};
+QThread *DummyFunctor::callThread = nullptr;
+
+void tst_QChronoTimer::callOnTimeout()
+{
+ QChronoTimer timer;
+ QSignalSpy timeoutSpy(&timer, &QChronoTimer::timeout);
+ timer.start();
+
+ auto context = std::make_unique<QObject>();
+
+ int count = 0;
+ timer.callOnTimeout([&count] { count++; });
+ QMetaObject::Connection connection = timer.callOnTimeout(context.get(), [&count] { count++; });
+ timer.callOnTimeout(&timer, &QChronoTimer::stop);
+
+
+ QTest::qWait(100ms);
+ QCOMPARE(count, 2);
+ QCOMPARE(timeoutSpy.size(), 1);
+
+ // Test that connection is bound to context lifetime
+ QVERIFY(connection);
+ context.reset();
+ QVERIFY(!connection);
+}
+
+void tst_QChronoTimer::bindToTimer()
+{
+ QChronoTimer timer;
+
+ // singleShot property
+ QProperty<bool> singleShot;
+ singleShot.setBinding(timer.bindableSingleShot().makeBinding());
+ QCOMPARE(timer.isSingleShot(), singleShot);
+
+ timer.setSingleShot(true);
+ QVERIFY(singleShot);
+ timer.setSingleShot(false);
+ QVERIFY(!singleShot);
+
+ // interval property
+ QProperty<std::chrono::nanoseconds> interval;
+ interval.setBinding([&](){ return timer.interval(); });
+ QCOMPARE(timer.interval(), interval.value());
+
+ timer.setInterval(10ms);
+ QCOMPARE(interval.value(), 10ms);
+ timer.setInterval(100ms);
+ QCOMPARE(interval.value(), 100ms);
+
+ // timerType property
+ QProperty<Qt::TimerType> timerType;
+ timerType.setBinding(timer.bindableTimerType().makeBinding());
+ QCOMPARE(timer.timerType(), timerType);
+
+ timer.setTimerType(Qt::PreciseTimer);
+ QCOMPARE(timerType, Qt::PreciseTimer);
+
+ timer.setTimerType(Qt::VeryCoarseTimer);
+ QCOMPARE(timerType, Qt::VeryCoarseTimer);
+
+ // active property
+ QProperty<bool> active;
+ active.setBinding([&](){ return timer.isActive(); });
+ QCOMPARE(active, timer.isActive());
+
+ timer.setInterval(1s);
+ timer.start();
+ QVERIFY(active);
+
+ timer.stop();
+ QVERIFY(!active);
+
+ // Also test that using negative interval updates the binding correctly
+ timer.setInterval(100ms);
+ timer.start();
+ QVERIFY(active);
+
+ auto ignoreMsg = [] {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QObject::startTimer: Timers cannot have negative intervals");
+ };
+
+ ignoreMsg();
+ timer.setInterval(-100ms);
+ ignoreMsg();
+ timer.start();
+ QVERIFY(!active);
+
+ timer.setInterval(100ms);
+ timer.start();
+ QVERIFY(active);
+
+ ignoreMsg();
+ timer.setInterval(-100ms);
+ ignoreMsg();
+ timer.start();
+ QVERIFY(!active);
+}
+
+void tst_QChronoTimer::bindTimer()
+{
+ QChronoTimer timer;
+
+ // singleShot property
+ QVERIFY(!timer.isSingleShot());
+
+ QProperty<bool> singleShot;
+ timer.bindableSingleShot().setBinding(Qt::makePropertyBinding(singleShot));
+
+ singleShot = true;
+ QVERIFY(timer.isSingleShot());
+ singleShot = false;
+ QVERIFY(!timer.isSingleShot());
+
+ // interval property
+ QCOMPARE(timer.interval(), 0ns);
+
+ QProperty<std::chrono::nanoseconds> interval;
+ timer.bindableInterval().setBinding(Qt::makePropertyBinding(interval));
+
+ interval = 10ms;
+ QCOMPARE(timer.interval(), 10ms);
+ interval = 100ms;
+ QCOMPARE(timer.interval(), 100ms);
+ timer.setInterval(50ms);
+ QCOMPARE(timer.interval(), 50ms);
+ interval = 30ms;
+ QCOMPARE(timer.interval(), 50ms);
+
+ // timerType property
+ QCOMPARE(timer.timerType(), Qt::CoarseTimer);
+
+ QProperty<Qt::TimerType> timerType;
+ timer.bindableTimerType().setBinding(Qt::makePropertyBinding(timerType));
+
+ timerType = Qt::PreciseTimer;
+ QCOMPARE(timer.timerType(), Qt::PreciseTimer);
+ timerType = Qt::VeryCoarseTimer;
+ QCOMPARE(timer.timerType(), Qt::VeryCoarseTimer);
+}
+
+void tst_QChronoTimer::automatedBindingTests()
+{
+ QChronoTimer timer;
+
+ QVERIFY(!timer.isSingleShot());
+ QTestPrivate::testReadWritePropertyBasics(timer, true, false, "singleShot");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QChronoTimer::singleShot");
+ return;
+ }
+
+ QCOMPARE_NE(timer.interval(), 10ms);
+ using NSec = std::chrono::nanoseconds;
+ QTestPrivate::testReadWritePropertyBasics(timer, NSec{10ms}, NSec{20ms}, "interval");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QChronoTimer::interval");
+ return;
+ }
+
+ QCOMPARE_NE(timer.timerType(), Qt::PreciseTimer);
+ QTestPrivate::testReadWritePropertyBasics(timer, Qt::PreciseTimer, Qt::CoarseTimer,
+ "timerType");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QChronoTimer::timerType");
+ return;
+ }
+
+ timer.setInterval(1s);
+ timer.start();
+ QVERIFY(timer.isActive());
+ QTestPrivate::testReadOnlyPropertyBasics(timer, true, false, "active",
+ [&timer]() { timer.stop(); });
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QChronoTimer::active");
+ return;
+ }
+}
+
+void tst_QChronoTimer::negativeInterval()
+{
+ QChronoTimer timer;
+
+ auto ignoreMsg = [] {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QObject::startTimer: Timers cannot have negative intervals");
+ };
+
+ ignoreMsg();
+ // Setting a negative interval does not change the active state.
+ timer.setInterval(-100ms);
+ ignoreMsg();
+ timer.start();
+ QVERIFY(!timer.isActive());
+
+ // Starting a timer that has a positive interval, the active state is changed
+ timer.setInterval(100ms);
+ timer.start();
+ QVERIFY(timer.isActive());
+
+ ignoreMsg();
+ // Setting a negative interval on an already running timer...
+ timer.setInterval(-100ms);
+ // ... the timer is stopped and the active state is changed
+ QVERIFY(!timer.isActive());
+
+ // Calling start on a timer that has a negative interval, does not change the active state
+ timer.start();
+ QVERIFY(!timer.isActive());
+}
+
+class OrderHelper : public QObject
+{
+ Q_OBJECT
+public:
+ enum CallType
+ {
+ String,
+ PMF,
+ Functor,
+ FunctorNoCtx
+ };
+ Q_ENUM(CallType)
+ QList<CallType> calls;
+
+ void triggerCall(CallType callType)
+ {
+ switch (callType)
+ {
+ case String:
+ QChronoTimer::singleShot(0ns, this, SLOT(stringSlot()));
+ break;
+ case PMF:
+ QChronoTimer::singleShot(0ns, this, &OrderHelper::pmfSlot);
+ break;
+ case Functor:
+ QChronoTimer::singleShot(0ns, this, [this]() { functorSlot(); });
+ break;
+ case FunctorNoCtx:
+ QChronoTimer::singleShot(0ns, [this]() { functorNoCtxSlot(); });
+ break;
+ }
+ }
+
+public slots:
+ void stringSlot() { calls << String; }
+ void pmfSlot() { calls << PMF; }
+ void functorSlot() { calls << Functor; }
+ void functorNoCtxSlot() { calls << FunctorNoCtx; }
+};
+
+Q_DECLARE_METATYPE(OrderHelper::CallType)
+
+void tst_QChronoTimer::timerOrder()
+{
+ QFETCH(QList<OrderHelper::CallType>, calls);
+
+ OrderHelper helper;
+
+ for (const auto call : calls)
+ helper.triggerCall(call);
+
+ QTRY_COMPARE(helper.calls, calls);
+}
+
+void tst_QChronoTimer::timerOrder_data()
+{
+ QTest::addColumn<QList<OrderHelper::CallType>>("calls");
+
+ QList<OrderHelper::CallType> calls = {
+ OrderHelper::String, OrderHelper::PMF,
+ OrderHelper::Functor, OrderHelper::FunctorNoCtx
+ };
+ std::sort(calls.begin(), calls.end());
+
+ int permutation = 0;
+ do {
+ QTest::addRow("permutation=%d", permutation) << calls;
+ ++permutation;
+ } while (std::next_permutation(calls.begin(), calls.end()));
+}
+
+void tst_QChronoTimer::timerOrderBackgroundThread()
+{
+ auto *thread = QThread::create([this]() { timerOrder(); });
+ thread->start();
+ QVERIFY(thread->wait());
+ delete thread;
+}
+
+void tst_QChronoTimer::timerPrecision()
+{
+ using namespace std::chrono;
+ steady_clock::time_point t1{};
+ steady_clock::time_point t2{};
+
+ QEventLoop loop;
+
+ QChronoTimer zeroTimer{0ns};
+ zeroTimer.setTimerType(Qt::PreciseTimer);
+ zeroTimer.setSingleShot(true);
+ connect(&zeroTimer, &QChronoTimer::timeout, this, [&t1] { t1 = steady_clock::now(); });
+
+ QChronoTimer oneNSecTimer{1ns};
+ oneNSecTimer.setTimerType(Qt::PreciseTimer);
+ oneNSecTimer.setSingleShot(true);
+ connect(&oneNSecTimer, &QChronoTimer::timeout, this, [&t2, &loop] {
+ t2 = steady_clock::now();
+ loop.quit();
+ });
+
+ zeroTimer.start();
+ oneNSecTimer.start();
+ loop.exec();
+ QCOMPARE_GT(t2, t1);
+ // qDebug() << "t2 - t1" << duration<double, std::chrono::milliseconds::period>{t2 - t1};
+}
+
+QTEST_MAIN(tst_QChronoTimer)
+
+#include "tst_qchronotimer.moc"
diff --git a/tests/auto/corelib/kernel/qcoreapplication/CMakeLists.txt b/tests/auto/corelib/kernel/qcoreapplication/CMakeLists.txt
index 92d4b03d26..8f9783088c 100644
--- a/tests/auto/corelib/kernel/qcoreapplication/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qcoreapplication/CMakeLists.txt
@@ -1,4 +1,11 @@
-# Generated from qcoreapplication.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qcoreapplication LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
if(NOT QT_FEATURE_private_tests)
return()
@@ -8,43 +15,25 @@ endif()
## tst_qcoreapplication Test:
#####################################################################
-# special case begin
if (WIN32)
set(target_version "1.2.3.4")
else()
set(target_version "1.2.3")
endif()
-# special case end
qt_internal_add_test(tst_qcoreapplication
- VERSION ${target_version} # special case
+ VERSION ${target_version}
SOURCES
tst_qcoreapplication.cpp tst_qcoreapplication.h
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
)
-# special case begin
if (APPLE)
set_property(TARGET tst_qcoreapplication PROPERTY MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist")
set_property(TARGET tst_qcoreapplication PROPERTY PROPERTY MACOSX_BUNDLE TRUE)
endif()
-# special case end
if (ANDROID)
set_property(TARGET tst_qcoreapplication PROPERTY QT_ANDROID_VERSION_NAME ${target_version})
endif()
-
-
-#### Keys ignored in scope 1:.:.:qcoreapplication.pro:<TRUE>:
-# QMAKE_INFO_PLIST = "$$PWD/Info.plist"
-# _REQUIREMENTS = "qtConfig(private_tests)"
-
-## Scopes:
-#####################################################################
-
-#### Keys ignored in scope 2:.:.:qcoreapplication.pro:WIN32:
-# VERSION = "1.2.3.4"
-
-#### Keys ignored in scope 3:.:.:qcoreapplication.pro:else:
-# VERSION = "1.2.3"
diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
index 8ddca93cb7..0745ea6ceb 100644
--- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
+++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp
@@ -1,6 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "tst_qcoreapplication.h"
@@ -9,6 +9,7 @@
#include <private/qabstracteventdispatcher_p.h> // for qGlobalPostedEventsCount()
#include <private/qcoreapplication_p.h>
+#include <private/qcoreevent_p.h>
#include <private/qeventloop_p.h>
#include <private/qthread_p.h>
@@ -24,9 +25,12 @@ class EventSpy : public QObject
public:
QList<int> recordedEvents;
- bool eventFilter(QObject *, QEvent *event) override
+ std::function<void(QObject *, QEvent *)> eventCallback;
+ bool eventFilter(QObject *target, QEvent *event) override
{
recordedEvents.append(event->type());
+ if (eventCallback)
+ eventCallback(target, event);
return false;
}
};
@@ -175,7 +179,7 @@ void tst_QCoreApplication::argc()
char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
TestApplication app(argc, argv);
QCOMPARE(argc, 1);
- QCOMPARE(app.arguments().count(), 1);
+ QCOMPARE(app.arguments().size(), 1);
}
{
@@ -186,7 +190,7 @@ void tst_QCoreApplication::argc()
const_cast<char*>("arg3") };
TestApplication app(argc, argv);
QCOMPARE(argc, 4);
- QCOMPARE(app.arguments().count(), 4);
+ QCOMPARE(app.arguments().size(), 4);
}
{
@@ -194,7 +198,7 @@ void tst_QCoreApplication::argc()
char **argv = 0;
TestApplication app(argc, argv);
QCOMPARE(argc, 0);
- QCOMPARE(app.arguments().count(), 0);
+ QCOMPARE(app.arguments().size(), 0);
}
{
@@ -203,7 +207,7 @@ void tst_QCoreApplication::argc()
const_cast<char*>("-qmljsdebugger=port:3768,block") };
TestApplication app(argc, argv);
QCOMPARE(argc, 1);
- QCOMPARE(app.arguments().count(), 1);
+ QCOMPARE(app.arguments().size(), 1);
}
}
@@ -1021,7 +1025,7 @@ void tst_QCoreApplication::addRemoveLibPaths()
TestApplication app(argc, argv);
// If libraryPaths only contains currentDir, neither will be in libraryPaths now.
- if (paths.length() != 1 && currentDir != paths[0]) {
+ if (paths.size() != 1 && currentDir != paths[0]) {
// Check that modifications stay alive across the creation of an application.
QVERIFY(QCoreApplication::libraryPaths().contains(currentDir));
QVERIFY(!QCoreApplication::libraryPaths().contains(paths[0]));
@@ -1034,19 +1038,127 @@ void tst_QCoreApplication::addRemoveLibPaths()
}
#endif
+static bool theMainThreadIsSet()
+{
+ // QCoreApplicationPrivate::mainThread() has a Q_ASSERT we'd trigger
+ return QCoreApplicationPrivate::theMainThread.loadRelaxed() != nullptr;
+}
+
+static bool theMainThreadWasUnset = !theMainThreadIsSet(); // global static
+void tst_QCoreApplication::theMainThread()
+{
+ QVERIFY2(theMainThreadWasUnset, "Something set the theMainThread before main()");
+ QVERIFY(theMainThreadIsSet()); // we have at LEAST one QObject alive: tst_QCoreApplication
+
+ int argc = 1;
+ char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
+ TestApplication app(argc, argv);
+ QVERIFY(QCoreApplicationPrivate::theMainThread.loadRelaxed());
+ QCOMPARE(QCoreApplicationPrivate::theMainThread.loadRelaxed(), thread());
+ QCOMPARE(app.thread(), thread());
+ QCOMPARE(app.thread(), QThread::currentThread());
+}
+
static void createQObjectOnDestruction()
{
- // Make sure that we can create a QObject after the last QObject has been
- // destroyed (especially after QCoreApplication has).
- //
+ // Make sure that we can create a QObject (and thus have an associated
+ // QThread) after the last QObject has been destroyed (especially after
+ // QCoreApplication has).
+
+#if !defined(QT_QGUIAPPLICATIONTEST) && !defined(Q_OS_WIN)
+ // QCoreApplicationData's global static destructor has run and cleaned up
+ // the QAdoptedThrad.
+ if (theMainThreadIsSet())
+ qFatal("theMainThreadIsSet() returned true; some QObject must have leaked");
+#endif
+
// Before the fixes, this would cause a dangling pointer dereference. If
// the problem comes back, it's possible that the following causes no
// effect.
QObject obj;
obj.thread()->setProperty("testing", 1);
+ if (!theMainThreadIsSet())
+ qFatal("theMainThreadIsSet() returned false");
+
+ // because we created a QObject after QCoreApplicationData was destroyed,
+ // the QAdoptedThread won't get cleaned up
}
Q_DESTRUCTOR_FUNCTION(createQObjectOnDestruction)
+void tst_QCoreApplication::testDeleteLaterFromBeforeOutermostEventLoop()
+{
+ int argc = 0;
+ QCoreApplication app(argc, nullptr);
+
+ EventSpy *spy = new EventSpy();
+ QPointer<QObject> spyPointer = spy;
+
+ app.installEventFilter(spy);
+ spy->eventCallback = [spy](QObject *, QEvent *event) {
+ if (event->type() == QEvent::User + 1)
+ spy->deleteLater();
+ };
+
+ QCoreApplication::postEvent(&app, new QEvent(QEvent::Type(QEvent::User + 1)));
+ QCoreApplication::processEvents();
+
+ QEventLoop loop;
+ QTimer::singleShot(0, &loop, &QEventLoop::quit);
+ loop.exec();
+ QVERIFY(!spyPointer);
+}
+
+void tst_QCoreApplication::setIndividualAttributes_data()
+{
+ QTest::addColumn<Qt::ApplicationAttribute>("attribute");
+
+ const QMetaEnum &metaEnum = Qt::staticMetaObject.enumerator(Qt::staticMetaObject.indexOfEnumerator("ApplicationAttribute"));
+ // - 1 to avoid AA_AttributeCount.
+ for (int i = 0; i < metaEnum.keyCount(); ++i) {
+ const auto attribute = static_cast<Qt::ApplicationAttribute>(metaEnum.value(i));
+ if (attribute == Qt::AA_AttributeCount)
+ continue;
+
+ QTest::addRow("%s", metaEnum.key(i)) << attribute;
+ }
+}
+
+void tst_QCoreApplication::setIndividualAttributes()
+{
+ QFETCH(Qt::ApplicationAttribute, attribute);
+
+ const auto originalValue = QCoreApplication::testAttribute(attribute);
+ auto cleanup = qScopeGuard([=]() {
+ QCoreApplication::setAttribute(attribute, originalValue);
+ });
+
+ QCoreApplication::setAttribute(attribute, true);
+ QVERIFY(QCoreApplication::testAttribute(attribute));
+
+ QCoreApplication::setAttribute(attribute, false);
+ QVERIFY(!QCoreApplication::testAttribute(attribute));
+}
+
+void tst_QCoreApplication::setMultipleAttributes()
+{
+ const auto originalDontUseNativeMenuWindowsValue = QCoreApplication::testAttribute(Qt::AA_DontUseNativeMenuWindows);
+ const auto originalDisableSessionManagerValue = QCoreApplication::testAttribute(Qt::AA_DisableSessionManager);
+ auto cleanup = qScopeGuard([=]() {
+ QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows, originalDontUseNativeMenuWindowsValue);
+ QCoreApplication::setAttribute(Qt::AA_DisableSessionManager, originalDisableSessionManagerValue);
+ });
+
+ QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows, true);
+ QCoreApplication::setAttribute(Qt::AA_DisableSessionManager, true);
+ QVERIFY(QCoreApplication::testAttribute(Qt::AA_DontUseNativeMenuWindows));
+ QVERIFY(QCoreApplication::testAttribute(Qt::AA_DisableSessionManager));
+
+ QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuWindows, false);
+ QCoreApplication::setAttribute(Qt::AA_DisableSessionManager, false);
+ QVERIFY(!QCoreApplication::testAttribute(Qt::AA_DontUseNativeMenuWindows));
+ QVERIFY(!QCoreApplication::testAttribute(Qt::AA_DisableSessionManager));
+}
+
#ifndef QT_QGUIAPPLICATIONTEST
QTEST_APPLESS_MAIN(tst_QCoreApplication)
#endif
diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h
index 0894b64f33..1c25f63534 100644
--- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h
+++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.h
@@ -1,6 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#ifndef TST_QCOREAPPLICATION_H
#define TST_QCOREAPPLICATION_H
@@ -44,6 +44,11 @@ private slots:
#if QT_CONFIG(library)
void addRemoveLibPaths();
#endif
+ void theMainThread();
+ void testDeleteLaterFromBeforeOutermostEventLoop();
+ void setIndividualAttributes_data();
+ void setIndividualAttributes();
+ void setMultipleAttributes();
};
#endif // TST_QCOREAPPLICATION_H
diff --git a/tests/auto/corelib/kernel/qdeadlinetimer/CMakeLists.txt b/tests/auto/corelib/kernel/qdeadlinetimer/CMakeLists.txt
index 8c8721abf5..2031cd9d48 100644
--- a/tests/auto/corelib/kernel/qdeadlinetimer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qdeadlinetimer/CMakeLists.txt
@@ -1,10 +1,19 @@
-# Generated from qdeadlinetimer.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qdeadlinetimer Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qdeadlinetimer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qdeadlinetimer
SOURCES
tst_qdeadlinetimer.cpp
+ LIBRARIES
+ Qt::TestPrivate
)
diff --git a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp
index b8ed0031cd..79416faaf9 100644
--- a/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp
+++ b/tests/auto/corelib/kernel/qdeadlinetimer/tst_qdeadlinetimer.cpp
@@ -1,27 +1,42 @@
// Copyright (C) 2016 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QString>
#include <QtCore/QTime>
#include <QtCore/QDeadlineTimer>
#include <QtCore/QElapsedTimer>
#include <QTest>
+#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QTimer>
-#if __has_include(<chrono>)
-# include <chrono>
-#endif
+#include <chrono>
+#include <inttypes.h>
static const int minResolution = 400; // the minimum resolution for the tests
-Q_DECLARE_METATYPE(Qt::TimerType)
+QT_BEGIN_NAMESPACE
+namespace QTest {
+template<> char *toString(const QDeadlineTimer &dt)
+{
+ if (dt.isForever())
+ return qstrdup("QDeadlineTimer::Forever");
+
+ qint64 deadline = dt.deadlineNSecs();
+ char *buf = new char[256];
+ qsnprintf(buf, 256, "%lld.%09d%s",
+ deadline / 1000 / 1000 / 1000, qAbs(deadline) % (1000 * 1000 * 1000),
+ dt.hasExpired() ? " (expired)" : "");
+ return buf;
+}
+}
+QT_END_NAMESPACE
class tst_QDeadlineTimer : public QObject
{
Q_OBJECT
private Q_SLOTS:
- void initTestCase_data();
+ void compareCompiles();
void basics();
void foreverness();
void current();
@@ -32,12 +47,11 @@ private Q_SLOTS:
void stdchrono();
};
-void tst_QDeadlineTimer::initTestCase_data()
+static constexpr auto timerType = Qt::PreciseTimer;
+
+void tst_QDeadlineTimer::compareCompiles()
{
- qRegisterMetaType<Qt::TimerType>();
- QTest::addColumn<Qt::TimerType>("timerType");
- QTest::newRow("precise") << Qt::PreciseTimer;
- QTest::newRow("coarse") << Qt::CoarseTimer;
+ QTestPrivate::testAllComparisonOperatorsCompile<QDeadlineTimer>();
}
void tst_QDeadlineTimer::basics()
@@ -45,20 +59,22 @@ void tst_QDeadlineTimer::basics()
QDeadlineTimer deadline;
QCOMPARE(deadline.timerType(), Qt::CoarseTimer);
- QFETCH_GLOBAL(Qt::TimerType, timerType);
deadline = QDeadlineTimer(timerType);
QCOMPARE(deadline.timerType(), timerType);
QVERIFY(!deadline.isForever());
QCOMPARE(deadline, QDeadlineTimer(timerType));
QVERIFY(!(deadline != QDeadlineTimer(timerType)));
QVERIFY(!(deadline < QDeadlineTimer()));
- QVERIFY(deadline <= QDeadlineTimer());
- QVERIFY(deadline >= QDeadlineTimer());
+ QCOMPARE_LE(deadline, QDeadlineTimer());
+ QCOMPARE_GE(deadline, QDeadlineTimer());
QVERIFY(!(deadline > QDeadlineTimer()));
QVERIFY(!(deadline < deadline));
- QVERIFY(deadline <= deadline);
- QVERIFY(deadline >= deadline);
+ QCOMPARE_LE(deadline, deadline);
+ QCOMPARE_GE(deadline, deadline);
QVERIFY(!(deadline > deadline));
+ QT_TEST_ALL_COMPARISON_OPS(deadline, QDeadlineTimer(timerType), Qt::strong_ordering::equal);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, QDeadlineTimer(), Qt::strong_ordering::equal);
+ QT_TEST_ALL_COMPARISON_OPS(QDeadlineTimer(), QDeadlineTimer(), Qt::strong_ordering::equal);
// should have expired, but we may be running too early after boot
QTRY_VERIFY_WITH_TIMEOUT(deadline.hasExpired(), 100);
@@ -71,18 +87,18 @@ void tst_QDeadlineTimer::basics()
deadline.setRemainingTime(0, timerType);
QCOMPARE(deadline.remainingTime(), qint64(0));
QCOMPARE(deadline.remainingTimeNSecs(), qint64(0));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
deadline.setPreciseRemainingTime(0, 0, timerType);
QCOMPARE(deadline.remainingTime(), qint64(0));
QCOMPARE(deadline.remainingTimeNSecs(), qint64(0));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
deadline.setDeadline(0, timerType);
QCOMPARE(deadline.remainingTime(), qint64(0));
@@ -99,9 +115,6 @@ void tst_QDeadlineTimer::basics()
void tst_QDeadlineTimer::foreverness()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
- // we don't check whether timerType() is our type since it's possible it detects it's forever
-
QDeadlineTimer deadline = QDeadlineTimer::Forever;
QCOMPARE(deadline.timerType(), Qt::CoarseTimer);
QVERIFY(deadline.isForever());
@@ -161,9 +174,10 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE(deadline, deadline);
QVERIFY(!(deadline < deadline));
- QVERIFY(deadline <= deadline);
- QVERIFY(deadline >= deadline);
+ QCOMPARE_LE(deadline, deadline);
+ QCOMPARE_GE(deadline, deadline);
QVERIFY(!(deadline > deadline));
+ QT_TEST_ALL_COMPARISON_OPS(deadline, deadline, Qt::strong_ordering::equal);
// adding to forever must still be forever
QDeadlineTimer deadline2 = deadline + 1;
@@ -178,9 +192,10 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE(deadline2 - deadline, qint64(0));
QCOMPARE(deadline2, deadline);
QVERIFY(!(deadline2 < deadline));
- QVERIFY(deadline2 <= deadline);
- QVERIFY(deadline2 >= deadline);
+ QCOMPARE_LE(deadline2, deadline);
+ QCOMPARE_GE(deadline2, deadline);
QVERIFY(!(deadline2 > deadline));
+ QT_TEST_ALL_COMPARISON_OPS(deadline2, deadline, Qt::strong_ordering::equal);
// subtracting from forever is *also* forever
deadline2 = deadline - 1;
@@ -195,33 +210,34 @@ void tst_QDeadlineTimer::foreverness()
QCOMPARE(deadline2 - deadline, qint64(0));
QCOMPARE(deadline2, deadline);
QVERIFY(!(deadline2 < deadline));
- QVERIFY(deadline2 <= deadline);
- QVERIFY(deadline2 >= deadline);
+ QCOMPARE_LE(deadline2, deadline);
+ QCOMPARE_GE(deadline2, deadline);
QVERIFY(!(deadline2 > deadline));
+ QT_TEST_ALL_COMPARISON_OPS(deadline2, deadline, Qt::strong_ordering::equal);
// compare and order against a default-constructed object
QDeadlineTimer expired;
QVERIFY(!(deadline == expired));
- QVERIFY(deadline != expired);
+ QCOMPARE_NE(deadline, expired);
QVERIFY(!(deadline < expired));
QVERIFY(!(deadline <= expired));
- QVERIFY(deadline >= expired);
- QVERIFY(deadline > expired);
+ QCOMPARE_GE(deadline, expired);
+ QCOMPARE_GT(deadline, expired);
+ QT_TEST_EQUALITY_OPS(deadline, expired, false);
}
void tst_QDeadlineTimer::current()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
auto deadline = QDeadlineTimer::current(timerType);
QVERIFY(deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
QCOMPARE(deadline.remainingTime(), qint64(0));
QCOMPARE(deadline.remainingTimeNSecs(), qint64(0));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
// subtracting from current should be "more expired"
QDeadlineTimer earlierDeadline = deadline - 1;
@@ -230,122 +246,123 @@ void tst_QDeadlineTimer::current()
QCOMPARE(earlierDeadline.timerType(), timerType);
QCOMPARE(earlierDeadline.remainingTime(), qint64(0));
QCOMPARE(earlierDeadline.remainingTimeNSecs(), qint64(0));
- QVERIFY(earlierDeadline.deadline() != 0);
- QVERIFY(earlierDeadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(earlierDeadline.deadlineNSecs() != 0);
- QVERIFY(earlierDeadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(earlierDeadline.deadline(), 0);
+ QCOMPARE_NE(earlierDeadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(earlierDeadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(earlierDeadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
QCOMPARE(earlierDeadline.deadline(), deadline.deadline() - 1);
QCOMPARE(earlierDeadline.deadlineNSecs(), deadline.deadlineNSecs() - 1000*1000);
QCOMPARE(earlierDeadline - deadline, qint64(-1));
- QVERIFY(earlierDeadline != deadline);
- QVERIFY(earlierDeadline < deadline);
- QVERIFY(earlierDeadline <= deadline);
+ QCOMPARE_NE(earlierDeadline, deadline);
+ QCOMPARE_LT(earlierDeadline, deadline);
+ QCOMPARE_LE(earlierDeadline, deadline);
QVERIFY(!(earlierDeadline >= deadline));
QVERIFY(!(earlierDeadline > deadline));
+ QT_TEST_ALL_COMPARISON_OPS(earlierDeadline, deadline, Qt::strong_ordering::less);
}
void tst_QDeadlineTimer::deadlines()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
-
QDeadlineTimer deadline(4 * minResolution, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (3 * minResolution));
- QVERIFY(deadline.remainingTime() <= (4 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() > (3000000 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() <= (4000000 * minResolution));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_GT(deadline.remainingTime(), (3 * minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), (4 * minResolution));
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (3000000 * minResolution));
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (4000000 * minResolution));
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
deadline.setRemainingTime(4 * minResolution, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (3 * minResolution));
- QVERIFY(deadline.remainingTime() <= (4 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() > (3000000 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() <= (4000000 * minResolution));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_GT(deadline.remainingTime(), (3 * minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), (4 * minResolution));
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (3000000 * minResolution));
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (4000000 * minResolution));
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
deadline.setPreciseRemainingTime(0, 4000000 * minResolution, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (3 * minResolution));
- QVERIFY(deadline.remainingTime() <= (4 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() > (3000000 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() <= (4000000 * minResolution));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_GT(deadline.remainingTime(), (3 * minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), (4 * minResolution));
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (3000000 * minResolution));
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (4000000 * minResolution));
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
deadline.setPreciseRemainingTime(1, 0, timerType); // 1 sec
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (1000 - minResolution));
- QVERIFY(deadline.remainingTime() <= 1000);
- QVERIFY(deadline.remainingTimeNSecs() > (1000 - minResolution)*1000*1000);
- QVERIFY(deadline.remainingTimeNSecs() <= (1000*1000*1000));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_GT(deadline.remainingTime(), (1000 - minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), 1000);
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (1000 - minResolution)*1000*1000);
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (1000*1000*1000));
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
// adding to a future deadline must still be further in the future
QDeadlineTimer laterDeadline = deadline + 1;
QVERIFY(!laterDeadline.hasExpired());
QVERIFY(!laterDeadline.isForever());
QCOMPARE(laterDeadline.timerType(), timerType);
- QVERIFY(laterDeadline.remainingTime() > (1000 - minResolution));
- QVERIFY(laterDeadline.remainingTime() <= 1001);
- QVERIFY(laterDeadline.remainingTimeNSecs() > (1001 - minResolution)*1000*1000);
- QVERIFY(laterDeadline.remainingTimeNSecs() <= (1001*1000*1000));
- QVERIFY(laterDeadline.deadline() != 0);
- QVERIFY(laterDeadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(laterDeadline.deadlineNSecs() != 0);
- QVERIFY(laterDeadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_GT(laterDeadline.remainingTime(), (1000 - minResolution));
+ QCOMPARE_LE(laterDeadline.remainingTime(), 1001);
+ QCOMPARE_GT(laterDeadline.remainingTimeNSecs(), (1001 - minResolution)*1000*1000);
+ QCOMPARE_LE(laterDeadline.remainingTimeNSecs(), (1001*1000*1000));
+ QCOMPARE_NE(laterDeadline.deadline(), 0);
+ QCOMPARE_NE(laterDeadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(laterDeadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(laterDeadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
QCOMPARE(laterDeadline.deadline(), deadline.deadline() + 1);
QCOMPARE(laterDeadline.deadlineNSecs(), deadline.deadlineNSecs() + 1000*1000);
QCOMPARE(laterDeadline - deadline, qint64(1));
- QVERIFY(laterDeadline != deadline);
+ QCOMPARE_NE(laterDeadline, deadline);
QVERIFY(!(laterDeadline < deadline));
QVERIFY(!(laterDeadline <= deadline));
- QVERIFY(laterDeadline >= deadline);
- QVERIFY(laterDeadline > deadline);
+ QCOMPARE_GE(laterDeadline, deadline);
+ QCOMPARE_GT(laterDeadline, deadline);
+ QT_TEST_ALL_COMPARISON_OPS(laterDeadline, deadline, Qt::strong_ordering::greater);
// compare and order against a default-constructed object
QDeadlineTimer expired;
QVERIFY(!(deadline == expired));
- QVERIFY(deadline != expired);
+ QCOMPARE_NE(deadline, expired);
QVERIFY(!(deadline < expired));
QVERIFY(!(deadline <= expired));
- QVERIFY(deadline >= expired);
- QVERIFY(deadline > expired);
+ QCOMPARE_GE(deadline, expired);
+ QCOMPARE_GT(deadline, expired);
+ QT_TEST_EQUALITY_OPS(deadline, expired, false);
// compare and order against a forever deadline
QDeadlineTimer forever_(QDeadlineTimer::Forever);
+ QT_TEST_EQUALITY_OPS(deadline, forever_, false);
QVERIFY(!(deadline == forever_));
- QVERIFY(deadline != forever_);
- QVERIFY(deadline < forever_);
- QVERIFY(deadline <= forever_);
+ QCOMPARE_NE(deadline, forever_);
+ QCOMPARE_LT(deadline, forever_);
+ QCOMPARE_LE(deadline, forever_);
QVERIFY(!(deadline >= forever_));
QVERIFY(!(deadline > forever_));
}
void tst_QDeadlineTimer::setDeadline()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
auto now = QDeadlineTimer::current(timerType);
QDeadlineTimer deadline;
@@ -373,10 +390,10 @@ void tst_QDeadlineTimer::setDeadline()
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (3 * minResolution));
- QVERIFY(deadline.remainingTime() <= (4 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() > (3000000 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() <= (4000000 * minResolution));
+ QCOMPARE_GT(deadline.remainingTime(), (3 * minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), (4 * minResolution));
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (3000000 * minResolution));
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (4000000 * minResolution));
QCOMPARE(deadline.deadline(), now.deadline() + 4 * minResolution); // yes, it's exact
// don't check deadlineNSecs!
@@ -387,17 +404,16 @@ void tst_QDeadlineTimer::setDeadline()
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTime() > (3 * minResolution));
- QVERIFY(deadline.remainingTime() <= (4 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() > (3000000 * minResolution));
- QVERIFY(deadline.remainingTimeNSecs() <= (4000000 * minResolution));
+ QCOMPARE_GT(deadline.remainingTime(), (3 * minResolution));
+ QCOMPARE_LE(deadline.remainingTime(), (4 * minResolution));
+ QCOMPARE_GT(deadline.remainingTimeNSecs(), (3000000 * minResolution));
+ QCOMPARE_LE(deadline.remainingTimeNSecs(), (4000000 * minResolution));
QCOMPARE(deadline.deadline(), nsec / (1000 * 1000));
QCOMPARE(deadline.deadlineNSecs(), nsec);
}
void tst_QDeadlineTimer::overflow()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
// Check the constructor for overflows (should also cover saturating the result of the deadline() method if overflowing)
QDeadlineTimer now = QDeadlineTimer::current(timerType), deadline(std::numeric_limits<qint64>::max() - 1, timerType);
QVERIFY(deadline.isForever() || deadline.deadline() >= now.deadline());
@@ -455,27 +471,48 @@ void tst_QDeadlineTimer::overflow()
// However we are tracking the elapsed time, so it shouldn't be a problem.
deadline.setPreciseRemainingTime(1, -1000, timerType);
qint64 difference = (deadline.deadlineNSecs() - nsDeadline) - nsExpected;
- QVERIFY(difference >= 0); // Should always be true, but just in case
- QVERIFY(difference <= callTimer.nsecsElapsed()); // Ideally difference should be 0 exactly
+ QCOMPARE_GE(difference, 0); // Should always be true, but just in case
+ QCOMPARE_LE(difference, callTimer.nsecsElapsed()); // Ideally difference should be 0 exactly
// Make sure setRemainingTime underflows gracefully
deadline.setPreciseRemainingTime(std::numeric_limits<qint64>::min() / 10, 0, timerType);
- QVERIFY(!deadline.isForever()); // On Win/macOS the above underflows, make sure we don't saturate to Forever
- QVERIFY(deadline.remainingTime() == 0);
+ QVERIFY(deadline.isForever()); // The above could underflow, so make sure we did set to Forever
+ QCOMPARE(deadline.remainingTimeNSecs(), -1);
+ QCOMPARE(deadline.remainingTime(), -1);
// If the timer is saturated we don't want to get a valid number of milliseconds
- QVERIFY(deadline.deadline() == std::numeric_limits<qint64>::min());
+ QCOMPARE(deadline.deadline(), std::numeric_limits<qint64>::max());
// Check that the conversion to milliseconds and nanoseconds underflows gracefully
deadline.setPreciseDeadline(std::numeric_limits<qint64>::min() / 10, 0, timerType);
- QVERIFY(!deadline.isForever()); // On Win/macOS the above underflows, make sure we don't saturate to Forever
+ QVERIFY(!deadline.isForever()); // The above underflows, make sure we don't saturate to Forever
QVERIFY(deadline.deadline() == std::numeric_limits<qint64>::min());
QVERIFY(deadline.deadlineNSecs() == std::numeric_limits<qint64>::min());
+
+ // Check that subtracting max() twice doesn't make it become positive
+ deadline.setPreciseDeadline(0);
+ deadline -= std::numeric_limits<qint64>::max();
+ deadline -= std::numeric_limits<qint64>::max();
+ QVERIFY(!deadline.isForever());
+ QCOMPARE(deadline.deadline(), std::numeric_limits<qint64>::min());
+ QCOMPARE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::min());
+
+ // Ditto for adding max()
+ deadline.setPreciseDeadline(0);
+ deadline += std::numeric_limits<qint64>::max();
+ deadline += std::numeric_limits<qint64>::max();
+ QVERIFY(deadline.isForever()); // it's so far in the future it's effectively forever
+ QCOMPARE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
+
+ // But we don't un-become forever after saturation
+ deadline -= std::numeric_limits<qint64>::max();
+ QVERIFY(deadline.isForever());
+ QCOMPARE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
}
void tst_QDeadlineTimer::expire()
{
- QFETCH_GLOBAL(Qt::TimerType, timerType);
-
QDeadlineTimer deadline(minResolution, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
@@ -486,20 +523,17 @@ void tst_QDeadlineTimer::expire()
QCOMPARE(deadline.remainingTime(), qint64(0));
QCOMPARE(deadline.remainingTimeNSecs(), qint64(0));
- QVERIFY(deadline.deadline() != 0);
- QVERIFY(deadline.deadline() != std::numeric_limits<qint64>::max());
- QVERIFY(deadline.deadlineNSecs() != 0);
- QVERIFY(deadline.deadlineNSecs() != std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadline(), 0);
+ QCOMPARE_NE(deadline.deadline(), std::numeric_limits<qint64>::max());
+ QCOMPARE_NE(deadline.deadlineNSecs(), 0);
+ QCOMPARE_NE(deadline.deadlineNSecs(), std::numeric_limits<qint64>::max());
QCOMPARE(deadline.deadlineNSecs(), previousDeadline);
}
void tst_QDeadlineTimer::stdchrono()
{
-#if !__has_include(<chrono>)
- QSKIP("std::chrono not found on this system");
-#else
using namespace std::chrono;
- QFETCH_GLOBAL(Qt::TimerType, timerType);
+ using namespace std::chrono_literals;
// create some forevers
QDeadlineTimer deadline = milliseconds::max();
@@ -572,132 +606,134 @@ void tst_QDeadlineTimer::stdchrono()
QTRY_VERIFY2_WITH_TIMEOUT(timersExecuted,
"Looks like timers didn't fire on time.", 4 * minResolution);
-#if defined(Q_OS_DARWIN) || defined(Q_OS_LINUX) || (defined(Q_CC_MSVC) && Q_CC_MSVC >= 1900)
{
- // We know for these OS/compilers that the std::chrono::steady_clock uses the same
- // reference time as QDeadlineTimer
qint64 before = duration_cast<nanoseconds>(steady_before.time_since_epoch()).count();
qint64 after = duration_cast<nanoseconds>(steady_after.time_since_epoch()).count();
- QVERIFY2(now.deadlineNSecs() > before, QByteArray::number(now.deadlineNSecs()) +
- " > " + QByteArray::number(before));
- QVERIFY2(now.deadlineNSecs() < after, QByteArray::number(now.deadlineNSecs()) +
- " < " + QByteArray::number(after));
+ QCOMPARE_GT(now.deadlineNSecs(), before);
+ QCOMPARE_LT(now.deadlineNSecs(), after);
}
-#endif
+
{
auto diff = duration_cast<milliseconds>(steady_after - steady_deadline);
- QVERIFY2(diff.count() > minResolution / 2, QByteArray::number(qint64(diff.count())));
- QVERIFY2(diff.count() < 3 * minResolution / 2, QByteArray::number(qint64(diff.count())));
+ QCOMPARE_GT(diff.count(), minResolution / 2);
+ QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_after(steady_after, timerType);
- QVERIFY2(now < dt_after,
- ("now = " + QLocale().toString(now.deadlineNSecs()) +
- "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
+ QCOMPARE_LT(now, dt_after);
+ QT_TEST_ALL_COMPARISON_OPS(now, dt_after, Qt::strong_ordering::less);
diff = duration_cast<milliseconds>(steady_deadline - steady_before);
- QVERIFY2(diff.count() > minResolution / 2, QByteArray::number(qint64(diff.count())));
- QVERIFY2(diff.count() < 3 * minResolution / 2, QByteArray::number(qint64(diff.count())));
+ QCOMPARE_GT(diff.count(), minResolution / 2);
+ QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_before(steady_before, timerType);
- QVERIFY2(now > dt_before,
- ("now = " + QLocale().toString(now.deadlineNSecs()) +
- "; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
+ QCOMPARE_GT(now, dt_before);
+ QT_TEST_ALL_COMPARISON_OPS(now, dt_before, Qt::strong_ordering::greater);
}
{
auto diff = duration_cast<milliseconds>(system_after - system_deadline);
- QVERIFY2(diff.count() > minResolution / 2, QByteArray::number(qint64(diff.count())));
- QVERIFY2(diff.count() < 3 * minResolution / 2, QByteArray::number(qint64(diff.count())));
+ QCOMPARE_GT(diff.count(), minResolution / 2);
+ QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_after(system_after, timerType);
- QVERIFY2(now < dt_after,
- ("now = " + QLocale().toString(now.deadlineNSecs()) +
- "; after = " + QLocale().toString(dt_after.deadlineNSecs())).toLatin1());
+ QCOMPARE_LT(now, dt_after);
+ QT_TEST_ALL_COMPARISON_OPS(now, dt_after, Qt::strong_ordering::less);
diff = duration_cast<milliseconds>(system_deadline - system_before);
- QVERIFY2(diff.count() > minResolution / 2, QByteArray::number(qint64(diff.count())));
- QVERIFY2(diff.count() < 3 * minResolution / 2, QByteArray::number(qint64(diff.count())));
+ QCOMPARE_GT(diff.count(), minResolution / 2);
+ QCOMPARE_LT(diff.count(), 3 * minResolution / 2);
QDeadlineTimer dt_before(system_before, timerType);
- QVERIFY2(now > dt_before,
- ("now = " + QLocale().toString(now.deadlineNSecs()) +
- "; before = " + QLocale().toString(dt_before.deadlineNSecs())).toLatin1());
+ QCOMPARE_GT(now, dt_before);
+ QT_TEST_ALL_COMPARISON_OPS(now, dt_before, Qt::strong_ordering::greater);
}
// make it regular
now = QDeadlineTimer::current(timerType);
- deadline.setRemainingTime(milliseconds(4 * minResolution), timerType);
+ deadline.setRemainingTime(4ms * minResolution, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTimeAsDuration() > milliseconds(3 * minResolution));
- QVERIFY(deadline.remainingTimeAsDuration() < milliseconds(5 * minResolution));
- QVERIFY(deadline.remainingTimeAsDuration() > nanoseconds(3000000 * minResolution));
- QVERIFY(deadline.remainingTimeAsDuration() < nanoseconds(5000000 * minResolution));
- QVERIFY(deadline.deadline<steady_clock>() > (steady_clock::now() + milliseconds(3 * minResolution)));
- QVERIFY(deadline.deadline<steady_clock>() < (steady_clock::now() + milliseconds(5 * minResolution)));
- QVERIFY(deadline.deadline<system_clock>() > (system_clock::now() + milliseconds(3 * minResolution)));
- QVERIFY(deadline.deadline<system_clock>() < (system_clock::now() + milliseconds(5 * minResolution)));
- if (timerType == Qt::CoarseTimer) {
- QVERIFY(deadline > (now + milliseconds(3 * minResolution)));
- QVERIFY(deadline < (now + milliseconds(5 * minResolution)));
- QVERIFY(deadline > (now + nanoseconds(3000000 * minResolution)));
- QVERIFY(deadline < (now + nanoseconds(5000000 * minResolution)));
- QVERIFY(deadline > milliseconds(3 * minResolution));
- QVERIFY(deadline < milliseconds(5 * minResolution));
- QVERIFY(deadline > nanoseconds(3000000 * minResolution));
- QVERIFY(deadline < nanoseconds(5000000 * minResolution));
- QVERIFY(deadline >= steady_clock::now());
- QVERIFY(deadline >= system_clock::now());
- }
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 3ms * minResolution);
+ QCOMPARE_LT(deadline.remainingTimeAsDuration(), 5ms * minResolution);
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 3'000'000ns * minResolution);
+ QCOMPARE_LT(deadline.remainingTimeAsDuration(), 5'000'000ns * minResolution);
+ QCOMPARE_GT(deadline.deadline<steady_clock>(), (steady_clock::now() + 3ms * minResolution));
+ QCOMPARE_LT(deadline.deadline<steady_clock>(), (steady_clock::now() + 5ms * minResolution));
+ QCOMPARE_GT(deadline.deadline<system_clock>(), (system_clock::now() + 3ms * minResolution));
+ QCOMPARE_LT(deadline.deadline<system_clock>(), (system_clock::now() + 5ms * minResolution));
+ QCOMPARE_GT((deadline.deadline<steady_clock, milliseconds>()),
+ steady_clock::now() + 3ms * minResolution);
+ QCOMPARE_LT((deadline.deadline<steady_clock, milliseconds>()),
+ steady_clock::now() + 5ms * minResolution);
+ QCOMPARE_GT((deadline.deadline<system_clock, milliseconds>()),
+ system_clock::now() + 3ms * minResolution);
+ QCOMPARE_LT((deadline.deadline<system_clock, milliseconds>()),
+ system_clock::now() + 5ms * minResolution);
+ QCOMPARE_GT(deadline, now + 3ms * minResolution);
+ QCOMPARE_LT(deadline, now + 5ms * minResolution);
+ QCOMPARE_GT(deadline, now + 3000000ns * minResolution);
+ QCOMPARE_LT(deadline, now + 5000000ns * minResolution);
+ QCOMPARE_GT(deadline, 3ms * minResolution);
+ QCOMPARE_LT(deadline, 5ms * minResolution);
+ QCOMPARE_GT(deadline, 3000000ns * minResolution);
+ QCOMPARE_LT(deadline, 5000000ns * minResolution);
+ QCOMPARE_GE(deadline, steady_clock::now());
+ QCOMPARE_GE(deadline, system_clock::now());
+ QT_TEST_ALL_COMPARISON_OPS(deadline, now + 3ms * minResolution, Qt::strong_ordering::greater);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, now + 5ms * minResolution, Qt::strong_ordering::less);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, now + 3000000ns * minResolution, Qt::strong_ordering::greater);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, now + 5000000ns * minResolution, Qt::strong_ordering::less);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, 3ms * minResolution, Qt::strong_ordering::greater);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, 5ms * minResolution, Qt::strong_ordering::less);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, steady_clock::now(), Qt::strong_ordering::greater);
+ QT_TEST_ALL_COMPARISON_OPS(deadline, system_clock::now(), Qt::strong_ordering::greater);
now = QDeadlineTimer::current(timerType);
- deadline = QDeadlineTimer(seconds(1), timerType);
+ deadline = QDeadlineTimer(1s, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTimeAsDuration() > (seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.remainingTimeAsDuration() <= seconds(1));
- QVERIFY(deadline.deadline<steady_clock>() > (steady_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<steady_clock>() <= (steady_clock::now() + seconds(1) + milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() > (system_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() <= (system_clock::now() + seconds(1) + milliseconds(minResolution)));
- if (timerType == Qt::CoarseTimer) {
- QVERIFY(deadline > (seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline <= seconds(1));
- }
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.remainingTimeAsDuration(), 1s);
+ QCOMPARE_GT(deadline.deadline<steady_clock>(), steady_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<steady_clock>(), steady_clock::now() + 1s + 1ms * minResolution);
+ QCOMPARE_GT(deadline.deadline<system_clock>(), system_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<system_clock>(), system_clock::now() + 1s + 1ms * minResolution);
+ QCOMPARE_GT(deadline, 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline, 1s);
now = QDeadlineTimer::current(timerType);
- deadline.setRemainingTime(hours(1), timerType);
+ deadline.setRemainingTime(1h, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTimeAsDuration() > (hours(1) - milliseconds(minResolution)));
- QVERIFY(deadline.remainingTimeAsDuration() <= hours(1));
- QVERIFY(deadline.deadline<steady_clock>() > (steady_clock::now() + hours(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<steady_clock>() <= (steady_clock::now() + hours(1) + milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() > (system_clock::now() + hours(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() <= (system_clock::now() + hours(1) + milliseconds(minResolution)));
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 1h - 1ms * minResolution);
+ QCOMPARE_LE(deadline.remainingTimeAsDuration(), 1h);
+ QCOMPARE_GT(deadline.deadline<steady_clock>(), steady_clock::now() + 1h - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<steady_clock>(), steady_clock::now() + 1h + 1ms * minResolution);
+ QCOMPARE_GT(deadline.deadline<system_clock>(), system_clock::now() + 1h - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<system_clock>(), system_clock::now() + 1h + 1ms * minResolution);
now = QDeadlineTimer::current(timerType);
- deadline.setDeadline(system_clock::now() + seconds(1), timerType);
+ deadline.setDeadline(system_clock::now() + 1s, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTimeAsDuration() > (seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.remainingTimeAsDuration() <= seconds(1));
- QVERIFY(deadline.deadline<steady_clock>() > (steady_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<steady_clock>() <= (steady_clock::now() + seconds(1) + milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() > (system_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() <= (system_clock::now() + seconds(1) + milliseconds(minResolution)));
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.remainingTimeAsDuration(), 1s);
+ QCOMPARE_GT(deadline.deadline<steady_clock>(), steady_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<steady_clock>(), steady_clock::now() + 1s + 1ms * minResolution);
+ QCOMPARE_GT(deadline.deadline<system_clock>(), system_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<system_clock>(), system_clock::now() + 1s + 1ms * minResolution);
now = QDeadlineTimer::current(timerType);
- deadline.setDeadline(steady_clock::now() + seconds(1), timerType);
+ deadline.setDeadline(steady_clock::now() + 1s, timerType);
QVERIFY(!deadline.hasExpired());
QVERIFY(!deadline.isForever());
QCOMPARE(deadline.timerType(), timerType);
- QVERIFY(deadline.remainingTimeAsDuration() > (seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.remainingTimeAsDuration() <= seconds(1));
- QVERIFY(deadline.deadline<steady_clock>() > (steady_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<steady_clock>() <= (steady_clock::now() + seconds(1) + milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() > (system_clock::now() + seconds(1) - milliseconds(minResolution)));
- QVERIFY(deadline.deadline<system_clock>() <= (system_clock::now() + seconds(1) + milliseconds(minResolution)));
-#endif
+ QCOMPARE_GT(deadline.remainingTimeAsDuration(), 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.remainingTimeAsDuration(), 1s);
+ QCOMPARE_GT(deadline.deadline<steady_clock>(), steady_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<steady_clock>(), steady_clock::now() + 1s + 1ms * minResolution);
+ QCOMPARE_GT(deadline.deadline<system_clock>(), system_clock::now() + 1s - 1ms * minResolution);
+ QCOMPARE_LE(deadline.deadline<system_clock>(), system_clock::now() + 1s + 1ms * minResolution);
}
QTEST_MAIN(tst_QDeadlineTimer)
diff --git a/tests/auto/corelib/kernel/qelapsedtimer/CMakeLists.txt b/tests/auto/corelib/kernel/qelapsedtimer/CMakeLists.txt
index 1592303ae3..9a40a2f905 100644
--- a/tests/auto/corelib/kernel/qelapsedtimer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qelapsedtimer/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qelapsedtimer.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qelapsedtimer Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qelapsedtimer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qelapsedtimer
SOURCES
tst_qelapsedtimer.cpp
diff --git a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
index 652a62f640..7623fd2e43 100644
--- a/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
+++ b/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp
@@ -1,19 +1,21 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+#include <QtCore/QDateTime>
#include <QtCore/QString>
-#include <QtCore/QTime>
#include <QtCore/QElapsedTimer>
#include <QTest>
#include <QTimer>
static const int minResolution = 100; // the minimum resolution for the tests
+QT_BEGIN_NAMESPACE
QDebug operator<<(QDebug s, const QElapsedTimer &t)
{
s.nospace() << "(" << t.msecsSinceReference() << ")";
return s.space();
}
+QT_END_NAMESPACE
class tst_QElapsedTimer : public QObject
{
@@ -29,11 +31,25 @@ private Q_SLOTS:
void tst_QElapsedTimer::statics()
{
- qDebug() << "Clock type is" << QElapsedTimer::clockType();
- qDebug() << "Said clock is" << (QElapsedTimer::isMonotonic() ? "monotonic" : "not monotonic");
+ // these have been required since Qt 6.6
+ QCOMPARE(QElapsedTimer::clockType(), QElapsedTimer::MonotonicClock);
+ QVERIFY(QElapsedTimer::isMonotonic());
+
QElapsedTimer t;
t.start();
- qDebug() << "Current time is" << t.msecsSinceReference();
+ qint64 system_now = QDateTime::currentMSecsSinceEpoch();
+
+ auto setprecision = +[](QTextStream &s) -> QTextStream & {
+ s.setRealNumberNotation(QTextStream::FixedNotation);
+ s.setRealNumberPrecision(3);
+ return s;
+ };
+ qDebug() << setprecision
+ << "Current monotonic time is" << (t.msecsSinceReference() / 1000.)
+ << "s and current system time is" << (system_now / 1000.) << 's';
+ if (qAbs(system_now - t.msecsSinceReference()) < 5 * minResolution)
+ qWarning() << "The monotonic clock is awfully close to the system clock"
+ " (it may not be monotonic at all!)";
}
void tst_QElapsedTimer::validity()
diff --git a/tests/auto/corelib/kernel/qeventdispatcher/CMakeLists.txt b/tests/auto/corelib/kernel/qeventdispatcher/CMakeLists.txt
index 409764a2c7..1f9cfb9449 100644
--- a/tests/auto/corelib/kernel/qeventdispatcher/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qeventdispatcher/CMakeLists.txt
@@ -1,10 +1,33 @@
-# Generated from qeventdispatcher.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qeventdispatcher Test:
#####################################################################
-qt_internal_add_test(tst_qeventdispatcher
- SOURCES
- tst_qeventdispatcher.cpp
-)
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qeventdispatcher LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+set(test_names "tst_qeventdispatcher")
+if(QT_FEATURE_glib AND UNIX)
+ list(APPEND test_names "tst_qeventdispatcher_no_glib")
+endif()
+
+foreach(test ${test_names})
+ qt_internal_add_test(${test}
+ NO_BATCH
+ SOURCES
+ tst_qeventdispatcher.cpp
+ )
+endforeach()
+
+if (TARGET tst_qeventdispatcher_no_glib)
+ qt_internal_extend_target(tst_qeventdispatcher_no_glib
+ DEFINES
+ DISABLE_GLIB
+ tst_QEventDispatcher=tst_QEventDispatcher_no_glib
+ )
+endif()
diff --git a/tests/auto/corelib/kernel/qeventdispatcher/tst_qeventdispatcher.cpp b/tests/auto/corelib/kernel/qeventdispatcher/tst_qeventdispatcher.cpp
index df5f5fef1d..285d080960 100644
--- a/tests/auto/corelib/kernel/qeventdispatcher/tst_qeventdispatcher.cpp
+++ b/tests/auto/corelib/kernel/qeventdispatcher/tst_qeventdispatcher.cpp
@@ -1,9 +1,8 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#ifdef QT_GUI_LIB
# include <QtGui/QGuiApplication>
-# define tst_QEventDispatcher tst_QGuiEventDispatcher
#else
# include <QtCore/QCoreApplication>
#endif
@@ -12,11 +11,39 @@
#include <QTimer>
#include <QThreadPool>
-enum {
- PreciseTimerInterval = 10,
- CoarseTimerInterval = 200,
- VeryCoarseTimerInterval = 1000
-};
+#ifdef DISABLE_GLIB
+static bool glibDisabled = []() {
+ qputenv("QT_NO_GLIB", "1");
+ return true;
+}();
+#endif
+
+#include <chrono>
+
+using namespace std::chrono_literals;
+
+static constexpr auto PreciseTimerInterval = 10ms;
+static constexpr auto CoarseTimerInterval = 200ms;
+static constexpr auto VeryCoarseTimerInterval = 1s;
+
+static constexpr
+std::chrono::nanoseconds fudgeInterval(std::chrono::nanoseconds interval, Qt::TimerType timerType)
+{
+ // Make the intervals have have fractions of milliseconds so we can check
+ // that they have been rounded & stored properly (where applicable).
+ switch (timerType) {
+ case Qt::VeryCoarseTimer:
+ // rounds down (floor) to seconds
+ return interval + 1010us;
+ case Qt::CoarseTimer:
+ // rounds up (ceil) to milliseconds
+ return interval - 10us;
+ case Qt::PreciseTimer:
+ // not rounded using QAbstractEventDispatcherV2; rounded up (ceil) on V1
+ return interval - 10us;
+ }
+ Q_UNREACHABLE_RETURN(std::chrono::nanoseconds::min());
+}
class tst_QEventDispatcher : public QObject
{
@@ -81,11 +108,9 @@ bool tst_QEventDispatcher::event(QEvent *e)
// drain the system event queue after the test starts to avoid destabilizing the test functions
void tst_QEventDispatcher::initTestCase()
{
- QElapsedTimer elapsedTimer;
- elapsedTimer.start();
- while (!elapsedTimer.hasExpired(CoarseTimerInterval) && eventDispatcher->processEvents(QEventLoop::AllEvents)) {
- ;
- }
+ QDeadlineTimer deadline(CoarseTimerInterval);
+ while (!deadline.hasExpired() && eventDispatcher->processEvents(QEventLoop::AllEvents))
+ ;
}
// consume pending posted events to avoid impact on the next test function
@@ -112,35 +137,36 @@ public:
TimerManager(TimerManager &&) = delete;
TimerManager &operator=(TimerManager &&) = delete;
- int preciseTimerId() const { return m_preciseTimerId; }
- int coarseTimerId() const { return m_coarseTimerId; }
- int veryCoarseTimerId() const { return m_veryCoarseTimerId; }
+ int preciseTimerId() const { return int(m_preciseTimerId); }
+ int coarseTimerId() const { return int(m_coarseTimerId); }
+ int veryCoarseTimerId() const { return int(m_veryCoarseTimerId); }
- bool foundPrecise() const { return m_preciseTimerId > 0; }
- bool foundCoarse() const { return m_coarseTimerId > 0; }
- bool foundVeryCoarse() const { return m_veryCoarseTimerId > 0; }
+ bool foundPrecise() const { return preciseTimerId() > 0; }
+ bool foundCoarse() const { return coarseTimerId() > 0; }
+ bool foundVeryCoarse() const { return veryCoarseTimerId() > 0; }
- QList<QAbstractEventDispatcher::TimerInfo> registeredTimers() const
+ QList<QAbstractEventDispatcher::TimerInfoV2> registeredTimers() const
{
- return m_eventDispatcher->registeredTimers(m_parent);
+ return m_eventDispatcher->timersForObject(m_parent);
}
void registerAll()
{
// start 3 timers, each with the different timer types and different intervals
- m_preciseTimerId = m_eventDispatcher->registerTimer(
- PreciseTimerInterval, Qt::PreciseTimer, m_parent);
- m_coarseTimerId = m_eventDispatcher->registerTimer(
- CoarseTimerInterval, Qt::CoarseTimer, m_parent);
- m_veryCoarseTimerId = m_eventDispatcher->registerTimer(
- VeryCoarseTimerInterval, Qt::VeryCoarseTimer, m_parent);
- QVERIFY(m_preciseTimerId > 0);
- QVERIFY(m_coarseTimerId > 0);
- QVERIFY(m_veryCoarseTimerId > 0);
+ auto registerTimer = [&](std::chrono::milliseconds interval, Qt::TimerType timerType) {
+ return m_eventDispatcher->registerTimer(fudgeInterval(interval, timerType), timerType,
+ m_parent);
+ };
+ m_preciseTimerId = registerTimer(PreciseTimerInterval, Qt::PreciseTimer);
+ m_coarseTimerId = registerTimer(CoarseTimerInterval, Qt::CoarseTimer);
+ m_veryCoarseTimerId = registerTimer(VeryCoarseTimerInterval, Qt::VeryCoarseTimer);
+ QVERIFY(foundPrecise());
+ QVERIFY(foundCoarse());
+ QVERIFY(foundVeryCoarse());
findTimers();
}
- void unregister(int timerId)
+ void unregister(Qt::TimerId timerId)
{
m_eventDispatcher->unregisterTimer(timerId);
findTimers();
@@ -158,36 +184,43 @@ private:
bool foundPrecise = false;
bool foundCoarse = false;
bool foundVeryCoarse = false;
- const QList<QAbstractEventDispatcher::TimerInfo> timers = registeredTimers();
- for (int i = 0; i < timers.count(); ++i) {
- const QAbstractEventDispatcher::TimerInfo &timerInfo = timers.at(i);
+ const QList<QAbstractEventDispatcher::TimerInfoV2> timers = registeredTimers();
+ for (const QAbstractEventDispatcher::TimerInfoV2 &timerInfo : timers) {
if (timerInfo.timerId == m_preciseTimerId) {
- QCOMPARE(timerInfo.interval, int(PreciseTimerInterval));
+ // For precise timers, we expect the fudge factor to be present
+ QAbstractEventDispatcher::Duration interval =
+ fudgeInterval(PreciseTimerInterval, Qt::PreciseTimer);
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+ if (!qobject_cast<QAbstractEventDispatcherV2 *>(m_eventDispatcher))
+ interval = PreciseTimerInterval;
+#endif
+ QCOMPARE(timerInfo.interval, interval);
QCOMPARE(timerInfo.timerType, Qt::PreciseTimer);
foundPrecise = true;
} else if (timerInfo.timerId == m_coarseTimerId) {
- QCOMPARE(timerInfo.interval, int(CoarseTimerInterval));
+ // For other timers, the fudge factors ought to have been rounded away
+ QCOMPARE(timerInfo.interval, CoarseTimerInterval);
QCOMPARE(timerInfo.timerType, Qt::CoarseTimer);
foundCoarse = true;
} else if (timerInfo.timerId == m_veryCoarseTimerId) {
- QCOMPARE(timerInfo.interval, int(VeryCoarseTimerInterval));
+ QCOMPARE(timerInfo.interval, VeryCoarseTimerInterval);
QCOMPARE(timerInfo.timerType, Qt::VeryCoarseTimer);
foundVeryCoarse = true;
}
}
if (!foundPrecise)
- m_preciseTimerId = -1;
+ m_preciseTimerId = Qt::TimerId::Invalid;
if (!foundCoarse)
- m_coarseTimerId = -1;
+ m_coarseTimerId = Qt::TimerId::Invalid;
if (!foundVeryCoarse)
- m_veryCoarseTimerId = -1;
+ m_veryCoarseTimerId = Qt::TimerId::Invalid;
}
QAbstractEventDispatcher *m_eventDispatcher = nullptr;
- int m_preciseTimerId = -1;
- int m_coarseTimerId = -1;
- int m_veryCoarseTimerId = -1;
+ Qt::TimerId m_preciseTimerId = Qt::TimerId::Invalid;
+ Qt::TimerId m_coarseTimerId = Qt::TimerId::Invalid;
+ Qt::TimerId m_veryCoarseTimerId = Qt::TimerId::Invalid;
QObject *m_parent = nullptr;
};
@@ -201,7 +234,7 @@ void tst_QEventDispatcher::registerTimer()
return;
// check that all 3 are present in the eventDispatcher's registeredTimer() list
- QCOMPARE(timers.registeredTimers().count(), 3);
+ QCOMPARE(timers.registeredTimers().size(), 3);
QVERIFY(timers.foundPrecise());
QVERIFY(timers.foundCoarse());
QVERIFY(timers.foundVeryCoarse());
@@ -227,16 +260,16 @@ void tst_QEventDispatcher::registerTimer()
if (doubleTimer)
QSKIP("Double timer during a single timeout - aborting test as flaky on macOS");
if (timerIdFromEvent != timers.preciseTimerId()
- && elapsedTimer.elapsed() > PreciseTimerInterval * 3)
+ && elapsedTimer.durationElapsed() > PreciseTimerInterval * 3)
QSKIP("Ignore flaky test behavior due to VM scheduling on macOS");
#endif
QCOMPARE(timerIdFromEvent, timers.preciseTimerId());
// now unregister it and make sure it's gone
- timers.unregister(timers.preciseTimerId());
+ timers.unregister(Qt::TimerId(timers.preciseTimerId()));
if (QTest::currentTestFailed())
return;
- QCOMPARE(timers.registeredTimers().count(), 2);
+ QCOMPARE(timers.registeredTimers().size(), 2);
QVERIFY(!timers.foundPrecise());
QVERIFY(timers.foundCoarse());
QVERIFY(timers.foundVeryCoarse());
@@ -251,16 +284,16 @@ void tst_QEventDispatcher::registerTimer()
if (doubleTimer)
QSKIP("Double timer during a single timeout - aborting test as flaky on macOS");
if (timerIdFromEvent != timers.coarseTimerId()
- && elapsedTimer.elapsed() > CoarseTimerInterval * 3)
+ && elapsedTimer.durationElapsed() > CoarseTimerInterval * 3)
QSKIP("Ignore flaky test behavior due to VM scheduling on macOS");
#endif
QCOMPARE(timerIdFromEvent, timers.coarseTimerId());
// now unregister it and make sure it's gone
- timers.unregister(timers.coarseTimerId());
+ timers.unregister(Qt::TimerId(timers.coarseTimerId()));
if (QTest::currentTestFailed())
return;
- QCOMPARE(timers.registeredTimers().count(), 1);
+ QCOMPARE(timers.registeredTimers().size(), 1);
QVERIFY(!timers.foundPrecise());
QVERIFY(!timers.foundCoarse());
QVERIFY(timers.foundVeryCoarse());
@@ -286,9 +319,8 @@ void tst_QEventDispatcher::sendPostedEvents()
QFETCH(int, processEventsFlagsInt);
QEventLoop::ProcessEventsFlags processEventsFlags = QEventLoop::ProcessEventsFlags(processEventsFlagsInt);
- QElapsedTimer elapsedTimer;
- elapsedTimer.start();
- while (!elapsedTimer.hasExpired(200)) {
+ QDeadlineTimer deadline(200ms);
+ while (!deadline.hasExpired()) {
receivedEventType = -1;
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
@@ -355,7 +387,7 @@ void tst_QEventDispatcher::postEventFromThread()
threadPool->start([&]{
int loop = 1000 / 10; // give it a second
while (!done && --loop)
- QThread::msleep(10);
+ QThread::sleep(std::chrono::milliseconds{10});
if (done)
return;
hadToQuit = true;
@@ -393,7 +425,7 @@ void tst_QEventDispatcher::postEventFromEventHandler()
threadPool->start([&]{
int loop = 250 / 10; // give it 250ms
while (!done && --loop)
- QThread::msleep(10);
+ QThread::sleep(std::chrono::milliseconds{10});
if (done)
return;
hadToQuit = true;
diff --git a/tests/auto/corelib/kernel/qeventloop/BLACKLIST b/tests/auto/corelib/kernel/qeventloop/BLACKLIST
new file mode 100644
index 0000000000..772ec3312c
--- /dev/null
+++ b/tests/auto/corelib/kernel/qeventloop/BLACKLIST
@@ -0,0 +1,2 @@
+[processEvents]
+qnx # QTBUG-119359
diff --git a/tests/auto/corelib/kernel/qeventloop/CMakeLists.txt b/tests/auto/corelib/kernel/qeventloop/CMakeLists.txt
index d3b817598e..5d90f97b21 100644
--- a/tests/auto/corelib/kernel/qeventloop/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qeventloop/CMakeLists.txt
@@ -1,13 +1,20 @@
-# Generated from qeventloop.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qeventloop Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qeventloop LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qeventloop
SOURCES
tst_qeventloop.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Network
)
@@ -16,7 +23,7 @@ qt_internal_add_test(tst_qeventloop
#####################################################################
qt_internal_extend_target(tst_qeventloop CONDITION WIN32
- PUBLIC_LIBRARIES
+ LIBRARIES
user32
)
diff --git a/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp b/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp
index 74b08601b7..06175e6f2f 100644
--- a/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp
+++ b/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -9,7 +9,9 @@
#include <qeventloop.h>
#include <private/qeventloop_p.h>
#if defined(Q_OS_UNIX)
- #include <private/qeventdispatcher_unix_p.h>
+ #if !defined(Q_OS_WASM)
+ #include <private/qeventdispatcher_unix_p.h>
+ #endif
#include <QtCore/private/qcore_unix_p.h>
#if defined(HAVE_GLIB)
#include <private/qeventdispatcher_glib_p.h>
@@ -138,6 +140,7 @@ class tst_QEventLoop : public QObject
Q_OBJECT
private slots:
// This test *must* run first. See the definition for why.
+ void processEvents_data();
void processEvents();
void exec();
void reexec();
@@ -159,8 +162,21 @@ protected:
void customEvent(QEvent *e) override;
};
+void tst_QEventLoop::processEvents_data()
+{
+ QTest::addColumn<QString>("mode");
+
+#ifdef QT_GUI_LIB
+ QTest::addRow("gui") << "gui";
+#else
+ QTest::addRow("core") << "core";
+#endif
+}
+
void tst_QEventLoop::processEvents()
{
+ QFETCH(QString, mode);
+
QSignalSpy aboutToBlockSpy(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock);
QSignalSpy awakeSpy(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::awake);
@@ -174,8 +190,8 @@ void tst_QEventLoop::processEvents()
// process posted events, QEventLoop::processEvents() should return
// true
QVERIFY(eventLoop.processEvents());
- QCOMPARE(aboutToBlockSpy.count(), 0);
- QCOMPARE(awakeSpy.count(), 1);
+ QCOMPARE(aboutToBlockSpy.size(), 0);
+ QCOMPARE(awakeSpy.size(), 1);
// allow any session manager to complete its handshake, so that
// there are no pending events left. This tests that we are able
@@ -196,8 +212,8 @@ void tst_QEventLoop::processEvents()
// processEvents is entered. There is no guarantee that that the
// processEvents call actually blocked, since the OS may introduce
// native events at any time.
- QVERIFY(awakeSpy.count() > 0);
- QVERIFY(awakeSpy.count() >= aboutToBlockSpy.count());
+ QVERIFY(awakeSpy.size() > 0);
+ QVERIFY(awakeSpy.size() >= aboutToBlockSpy.size());
killTimer(timerId);
}
@@ -240,7 +256,7 @@ void tst_QEventLoop::exec()
QVERIFY(spy.isValid());
thread.cond.wakeOne();
thread.cond.wait(&thread.mutex);
- QVERIFY(spy.count() > 0);
+ QVERIFY(spy.size() > 0);
int v = thread.result1;
QCOMPARE(v, 0);
@@ -249,7 +265,7 @@ void tst_QEventLoop::exec()
thread.cond.wakeOne();
thread.mutex.unlock();
thread.wait();
- QCOMPARE(spy.count(), 0);
+ QCOMPARE(spy.size(), 0);
v = thread.result2;
QCOMPARE(v, -1);
}
@@ -307,7 +323,7 @@ void tst_QEventLoop::wakeUp()
QTimer::singleShot(1000, &eventLoop, SLOT(quit()));
(void) eventLoop.exec();
- QVERIFY(spy.count() > 0);
+ QVERIFY(spy.size() > 0);
thread.quit();
(void) eventLoop.exec();
@@ -400,8 +416,8 @@ public slots:
dataSent = serverSocket->waitForBytesWritten(-1);
if (dataSent) {
- pollfd pfd = qt_make_pollfd(socket->socketDescriptor(), POLLIN);
- dataReadable = (1 == qt_safe_poll(&pfd, 1, nullptr));
+ pollfd pfd = qt_make_pollfd(int(socket->socketDescriptor()), POLLIN);
+ dataReadable = (1 == qt_safe_poll(&pfd, 1, QDeadlineTimer::Forever));
}
if (!dataReadable) {
@@ -486,7 +502,7 @@ void tst_QEventLoop::processEventsExcludeTimers()
// but not if we exclude timers
eventLoop.processEvents(QEventLoop::X11ExcludeTimers);
-#if defined(Q_OS_UNIX)
+#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
QAbstractEventDispatcher *eventDispatcher = QCoreApplication::eventDispatcher();
if (!qobject_cast<QEventDispatcherUNIX *>(eventDispatcher)
#if defined(HAVE_GLIB)
diff --git a/tests/auto/corelib/kernel/qjniarray/CMakeLists.txt b/tests/auto/corelib/kernel/qjniarray/CMakeLists.txt
new file mode 100644
index 0000000000..308b2c6427
--- /dev/null
+++ b/tests/auto/corelib/kernel/qjniarray/CMakeLists.txt
@@ -0,0 +1,13 @@
+# Copyright (C) 2023 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qjnitypes LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qjniarray
+ SOURCES
+ tst_qjniarray.cpp
+)
diff --git a/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp
new file mode 100644
index 0000000000..b7b1f95b39
--- /dev/null
+++ b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp
@@ -0,0 +1,165 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QtTest>
+
+#include <QtCore/qjnitypes.h>
+#include <QtCore/qjniarray.h>
+
+using namespace Qt::StringLiterals;
+
+class tst_QJniArray : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QJniArray() = default;
+
+private slots:
+ void construct();
+ void size();
+ void operators();
+};
+
+using namespace QtJniTypes;
+
+// fake type so that we can compile-time test and assert correct type mappings
+Q_DECLARE_JNI_CLASS(List, "qt/test/List");
+
+// verify that we get the return type we expect for the specified return type
+#define VERIFY_RETURN_FOR_TYPE(In, Out) \
+static_assert(std::is_same_v<decltype(List::callStaticMethod<In>("toByteArray")), Out>)
+
+VERIFY_RETURN_FOR_TYPE(jobjectArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jobject[], QJniArray<jobject>);
+//VERIFY_RETURN_FOR_TYPE(QList<QJniObject>, QList<QJniObject>);
+VERIFY_RETURN_FOR_TYPE(QList<jobject>, QList<jobject>);
+
+VERIFY_RETURN_FOR_TYPE(jbyteArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jbyte[], QJniArray<jbyte>);
+VERIFY_RETURN_FOR_TYPE(QByteArray, QByteArray);
+
+VERIFY_RETURN_FOR_TYPE(jbooleanArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jboolean[], QJniArray<jboolean>);
+VERIFY_RETURN_FOR_TYPE(QList<jboolean>, QList<jboolean>);
+VERIFY_RETURN_FOR_TYPE(QList<bool>, QList<jboolean>);
+
+VERIFY_RETURN_FOR_TYPE(jcharArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jchar[], QJniArray<jchar>);
+VERIFY_RETURN_FOR_TYPE(QList<jchar>, QList<jchar>);
+
+VERIFY_RETURN_FOR_TYPE(jshortArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jshort[], QJniArray<jshort>);
+VERIFY_RETURN_FOR_TYPE(QList<jshort>, QList<jshort>);
+VERIFY_RETURN_FOR_TYPE(QList<short>, QList<short>);
+
+VERIFY_RETURN_FOR_TYPE(jintArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jint[], QJniArray<jint>);
+VERIFY_RETURN_FOR_TYPE(QList<jint>, QList<jint>);
+VERIFY_RETURN_FOR_TYPE(QList<int>, QList<int>);
+
+VERIFY_RETURN_FOR_TYPE(jlongArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jlong[], QJniArray<jlong>);
+VERIFY_RETURN_FOR_TYPE(QList<jlong>, QList<jlong>);
+// VERIFY_RETURN_FOR_TYPE(QList<long>, QList<long>); // assumes long is 64bit
+
+VERIFY_RETURN_FOR_TYPE(jfloatArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jfloat[], QJniArray<jfloat>);
+VERIFY_RETURN_FOR_TYPE(QList<jfloat>, QList<jfloat>);
+VERIFY_RETURN_FOR_TYPE(QList<float>, QList<float>);
+
+VERIFY_RETURN_FOR_TYPE(jdoubleArray, QJniObject);
+VERIFY_RETURN_FOR_TYPE(jdouble[], QJniArray<jdouble>);
+VERIFY_RETURN_FOR_TYPE(QList<jdouble>, QList<jdouble>);
+VERIFY_RETURN_FOR_TYPE(QList<double>, QList<double>);
+
+VERIFY_RETURN_FOR_TYPE(QString, QString);
+
+VERIFY_RETURN_FOR_TYPE(List, List);
+VERIFY_RETURN_FOR_TYPE(List[], QJniArray<List>);
+VERIFY_RETURN_FOR_TYPE(QJniArray<List>, QJniArray<List>);
+
+void tst_QJniArray::construct()
+{
+ {
+ QStringList strings;
+ for (int i = 0; i < 10000; ++i)
+ strings << QString::number(i);
+ QJniArray<QString> list(strings);
+ QCOMPARE(list.size(), 10000);
+ }
+ {
+ QJniArray<jint> list{1, 2, 3};
+ QCOMPARE(list.size(), 3);
+ }
+ {
+ QJniArray<jint> list(QList<int>{1, 2, 3});
+ QCOMPARE(list.size(), 3);
+ }
+ {
+ QJniArray<jint> list{QList<int>{1, 2, 3}};
+ QCOMPARE(list.size(), 3);
+ }
+}
+
+void tst_QJniArray::size()
+{
+ QJniArray<jint> array;
+ QVERIFY(!array.isValid());
+ QCOMPARE(array.size(), 0);
+
+ QList<int> intList;
+ intList.resize(10);
+ auto intArray = QJniArrayBase::fromContainer(intList);
+ QCOMPARE(intArray.size(), 10);
+}
+
+void tst_QJniArray::operators()
+{
+ QByteArray bytes("abcde");
+ QJniArray<jbyte> array(bytes);
+ QVERIFY(array.isValid());
+
+ {
+ auto it = array.begin();
+ QCOMPARE(*it, 'a');
+ QCOMPARE(*++it, 'b');
+ QCOMPARE(*it++, 'b');
+ QCOMPARE(*it, 'c');
+ ++it;
+ it++;
+ QCOMPARE(*it, 'e');
+ QCOMPARE(++it, array.end());
+ }
+ {
+ auto it = array.rbegin();
+ QCOMPARE(*it, 'e');
+ QCOMPARE(*++it, 'd');
+ QCOMPARE(*it++, 'd');
+ QCOMPARE(*it, 'c');
+ ++it;
+ it++;
+ QCOMPARE(*it, 'a');
+ QCOMPARE(++it, array.rend());
+ }
+ {
+ QJniArray<jbyte>::const_iterator it = {};
+ QCOMPARE(it, QJniArray<jbyte>::const_iterator{});
+ QCOMPARE_NE(array.begin(), array.end());
+
+ it = array.begin();
+ QCOMPARE(it, array.begin());
+ }
+
+ QCOMPARE(std::distance(array.begin(), array.end()), array.size());
+
+ qsizetype index = 0;
+ for (const auto &value : array) {
+ QCOMPARE(value, bytes.at(index));
+ ++index;
+ }
+}
+
+QTEST_MAIN(tst_QJniArray)
+
+#include "tst_qjniarray.moc"
diff --git a/tests/auto/corelib/kernel/qjnienvironment/CMakeLists.txt b/tests/auto/corelib/kernel/qjnienvironment/CMakeLists.txt
index 25e7e51ba6..f405438314 100644
--- a/tests/auto/corelib/kernel/qjnienvironment/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qjnienvironment/CMakeLists.txt
@@ -1,7 +1,16 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
#####################################################################
## tst_qjnienvironment Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qjnienvironment LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qjnienvironment
SOURCES
tst_qjnienvironment.cpp
@@ -11,6 +20,4 @@ if(ANDROID)
set_property(TARGET tst_qjnienvironment APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/testdata
)
- # QTBUG-88840
- qt_android_generate_deployment_settings(tst_qjnienvironment)
endif()
diff --git a/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java b/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
index 0fc70f2567..4f307b3bc7 100644
--- a/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
+++ b/tests/auto/corelib/kernel/qjnienvironment/testdata/src/org/qtproject/qt/android/testdata/QtJniEnvironmentTestClass.java
@@ -1,11 +1,15 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
package org.qtproject.qt.android.testdatapackage;
public class QtJniEnvironmentTestClass
{
private static native void callbackFromJava(String message);
+ private static native void namedCallbackFromJava(String message);
+ private static native void memberCallbackFromJava(String message);
+ private static native void namedMemberCallbackFromJava(String message);
+ private static native void namespaceCallbackFromJava(String message);
private static native void intCallbackFromJava(int value);
public final int INT_FIELD = 123;
@@ -18,6 +22,26 @@ public class QtJniEnvironmentTestClass
callbackFromJava("From Java: " + message);
}
+ public static void namedAppendJavaToString(String message)
+ {
+ namedCallbackFromJava("From Java (named): " + message);
+ }
+
+ public static void memberAppendJavaToString(String message)
+ {
+ memberCallbackFromJava("From Java (member): " + message);
+ }
+
+ public static void namedMemberAppendJavaToString(String message)
+ {
+ namedMemberCallbackFromJava("From Java (named member): " + message);
+ }
+
+ public static void namespaceAppendJavaToString(String message)
+ {
+ namespaceCallbackFromJava("From Java (namespace): " + message);
+ }
+
public static void convertToInt(String message)
{
intCallbackFromJava(Integer.parseInt(message));
diff --git a/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp b/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
index bcc5556d60..95748f46f7 100644
--- a/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
+++ b/tests/auto/corelib/kernel/qjnienvironment/tst_qjnienvironment.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <jni.h>
@@ -20,6 +20,7 @@ class tst_QJniEnvironment : public QObject
Q_OBJECT
private slots:
+ void init();
void jniEnv();
void javaVM();
void registerNativeMethods();
@@ -30,6 +31,14 @@ private slots:
void findStaticField();
};
+void tst_QJniEnvironment::init()
+{
+ // Unless explicitly ignored to test error handling, warning messages
+ // in this test about a failure to look up a field, method, or class
+ // make the test fail.
+ QTest::failOnWarning(QRegularExpression("java.lang.NoSuch.*Error"));
+}
+
void tst_QJniEnvironment::jniEnv()
{
QJniEnvironment env;
@@ -59,17 +68,21 @@ void tst_QJniEnvironment::jniEnv()
QVERIFY(!QJniEnvironment::checkAndClearExceptions(env.jniEnv()));
// try to find a nonexistent class
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.ClassNotFoundException: .*"));
QVERIFY(!env->FindClass("this/doesnt/Exist"));
QVERIFY(QJniEnvironment::checkAndClearExceptions(env.jniEnv()));
// try to find an existing class with QJniEnvironment
QJniEnvironment env;
QVERIFY(env.findClass("java/lang/Object"));
+ QVERIFY(env.findClass<jstring>());
// try to find a nonexistent class
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.ClassNotFoundException: .*"));
QVERIFY(!env.findClass("this/doesnt/Exist"));
// clear exception with member function
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.ClassNotFoundException: .*"));
QVERIFY(!env->FindClass("this/doesnt/Exist"));
QVERIFY(env.checkAndClearExceptions());
}
@@ -98,12 +111,49 @@ static void callbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
Q_UNUSED(env)
registerNativesString = QJniObject(value).toString();
}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackFromJava);
+
+static void tediouslyLongNamed_callbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
+{
+ Q_UNUSED(env)
+ registerNativesString = QJniObject(value).toString();
+}
+Q_DECLARE_JNI_NATIVE_METHOD(tediouslyLongNamed_callbackFromJava, namedCallbackFromJava)
static void callbackFromJavaNoCtor(JNIEnv *env, jobject /*thiz*/, jstring value)
{
Q_UNUSED(env)
registerNativesString = QJniObject(value).toString();
}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackFromJavaNoCtor);
+
+class CallbackClass {
+public:
+ static void memberCallbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
+ {
+ Q_UNUSED(env)
+ registerNativesString = QJniObject(value).toString();
+ }
+ Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(memberCallbackFromJava)
+
+ static void tediouslyLongNamed_memberCallbackFromJava(JNIEnv *env, jobject /*thiz*/,
+ jstring value)
+ {
+ Q_UNUSED(env)
+ registerNativesString = QJniObject(value).toString();
+ }
+ Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(tediouslyLongNamed_memberCallbackFromJava,
+ namedMemberCallbackFromJava)
+};
+
+namespace CallbackNamespace {
+ static void namespaceCallbackFromJava(JNIEnv *env, jobject /*thiz*/, jstring value)
+ {
+ Q_UNUSED(env)
+ registerNativesString = QJniObject(value).toString();
+ }
+ Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(namespaceCallbackFromJava)
+}
void tst_QJniEnvironment::registerNativeMethods()
{
@@ -111,11 +161,9 @@ void tst_QJniEnvironment::registerNativeMethods()
QJniEnvironment env;
{
- const JNINativeMethod methods[] {
- {"callbackFromJava", "(Ljava/lang/String;)V", reinterpret_cast<void *>(callbackFromJava)}
- };
-
- QVERIFY(env.registerNativeMethods(javaTestClass, methods, 1));
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_METHOD(callbackFromJava)
+ }));
QJniObject::callStaticMethod<void>(javaTestClass,
"appendJavaToString",
@@ -125,12 +173,68 @@ void tst_QJniEnvironment::registerNativeMethods()
QVERIFY(registerNativesString == QStringLiteral("From Java: Qt"));
}
- // No default constructor in class
+ // Named native function
+ {
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_METHOD(tediouslyLongNamed_callbackFromJava)
+ }));
+
+ QJniObject::callStaticMethod<void>(javaTestClass,
+ "namedAppendJavaToString",
+ "(Ljava/lang/String;)V",
+ QtString.object<jstring>());
+ QTest::qWait(200);
+ QVERIFY(registerNativesString == QStringLiteral("From Java (named): Qt"));
+ }
+
+ // Static class member as callback
{
- const JNINativeMethod methods[] {{"callbackFromJavaNoCtor", "(Ljava/lang/String;)V",
- reinterpret_cast<void *>(callbackFromJavaNoCtor)}};
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_SCOPED_METHOD(memberCallbackFromJava, CallbackClass)
+ }));
+
+ QJniObject::callStaticMethod<void>(javaTestClass,
+ "memberAppendJavaToString",
+ "(Ljava/lang/String;)V",
+ QtString.object<jstring>());
+ QTest::qWait(200);
+ QVERIFY(registerNativesString == QStringLiteral("From Java (member): Qt"));
+ }
- QVERIFY(env.registerNativeMethods(javaTestClassNoCtor, methods, 1));
+ // Static named class member as callback
+ {
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_SCOPED_METHOD(tediouslyLongNamed_memberCallbackFromJava,
+ CallbackClass)
+ }));
+
+ QJniObject::callStaticMethod<void>(javaTestClass,
+ "namedMemberAppendJavaToString",
+ "(Ljava/lang/String;)V",
+ QtString.object<jstring>());
+ QTest::qWait(200);
+ QVERIFY(registerNativesString == QStringLiteral("From Java (named member): Qt"));
+ }
+
+ // Function generally just in namespace as callback
+ {
+ QVERIFY(env.registerNativeMethods(javaTestClass, {
+ Q_JNI_NATIVE_SCOPED_METHOD(namespaceCallbackFromJava, CallbackNamespace)
+ }));
+
+ QJniObject::callStaticMethod<void>(javaTestClass,
+ "namespaceAppendJavaToString",
+ "(Ljava/lang/String;)V",
+ QtString.object<jstring>());
+ QTest::qWait(200);
+ QVERIFY(registerNativesString == QStringLiteral("From Java (namespace): Qt"));
+ }
+
+ // No default constructor in class
+ {
+ QVERIFY(env.registerNativeMethods(javaTestClassNoCtor, {
+ Q_JNI_NATIVE_METHOD(callbackFromJavaNoCtor)
+ }));
QJniObject::callStaticMethod<void>(javaTestClassNoCtor,
"appendJavaToString",
@@ -146,17 +250,16 @@ static void intCallbackFromJava(JNIEnv *env, jobject /*thiz*/, jint value)
Q_UNUSED(env)
registerNativeInteger = static_cast<int>(value);
}
+Q_DECLARE_JNI_NATIVE_METHOD(intCallbackFromJava);
void tst_QJniEnvironment::registerNativeMethodsByJclass()
{
- const JNINativeMethod methods[] {
- { "intCallbackFromJava", "(I)V", reinterpret_cast<void *>(intCallbackFromJava) }
- };
-
QJniEnvironment env;
jclass clazz = env.findClass(javaTestClass);
QVERIFY(clazz != 0);
- QVERIFY(env.registerNativeMethods(clazz, methods, 1));
+ QVERIFY(env.registerNativeMethods(clazz, {
+ Q_JNI_NATIVE_METHOD(intCallbackFromJava)
+ }));
QCOMPARE(registerNativeInteger, 0);
@@ -182,6 +285,7 @@ void tst_QJniEnvironment::findMethod()
QVERIFY(methodId != nullptr);
// invalid signature
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.NoSuchMethodError: .*"));
jmethodID invalid = env.findMethod(clazz, "unknown", "()I");
QVERIFY(invalid == nullptr);
// check that all exceptions are already cleared
@@ -208,8 +312,10 @@ void tst_QJniEnvironment::findStaticMethod()
QCOMPARE(result, 123);
// invalid method
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.NoSuchMethodError: .*"));
jmethodID invalid = env.findStaticMethod(clazz, "unknown", "()I");
QVERIFY(invalid == nullptr);
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.NoSuchMethodError: .*"));
invalid = env.findStaticMethod<jint>(clazz, "unknown");
QVERIFY(invalid == nullptr);
// check that all exceptions are already cleared
@@ -237,6 +343,7 @@ void tst_QJniEnvironment::findField()
QVERIFY(value == 123);
// invalid signature
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.NoSuchFieldError: .*"));
jfieldID invalidId = env.findField(clazz, "unknown", "I");
QVERIFY(invalidId == nullptr);
// check that all exceptions are already cleared
@@ -260,6 +367,7 @@ void tst_QJniEnvironment::findStaticField()
QVERIFY(size == 321);
// invalid signature
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.NoSuchFieldError: .*"));
jfieldID invalidId = env.findStaticField(clazz, "unknown", "I");
QVERIFY(invalidId == nullptr);
// check that all exceptions are already cleared
diff --git a/tests/auto/corelib/kernel/qjniobject/CMakeLists.txt b/tests/auto/corelib/kernel/qjniobject/CMakeLists.txt
index b0f5d0b640..98509dc0e5 100644
--- a/tests/auto/corelib/kernel/qjniobject/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qjniobject/CMakeLists.txt
@@ -1,7 +1,16 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
#####################################################################
## tst_qjniobject Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qjniobject LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qjniobject
SOURCES
tst_qjniobject.cpp
@@ -11,6 +20,4 @@ if(ANDROID)
set_property(TARGET tst_qjniobject APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/testdata
)
- # QTBUG-88840 # special case
- qt_android_generate_deployment_settings(tst_qjniobject) # special case
endif()
diff --git a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
index dc55ad0f89..07a94d1cac 100644
--- a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
+++ b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
package org.qtproject.qt.android.testdatapackage;
@@ -20,6 +20,9 @@ public class QtJniObjectTestClass
// --------------------------------------------------------------------------------------------
+ final int INT_FIELD = 123;
+ final boolean BOOL_FIELD = true;
+
byte BYTE_VAR;
short SHORT_VAR;
int INT_VAR;
@@ -125,6 +128,9 @@ public class QtJniObjectTestClass
public String stringMethod() { return staticStringMethod(); }
// --------------------------------------------------------------------------------------------
+ public static String staticEchoMethod(String value) { return value; }
+
+ // --------------------------------------------------------------------------------------------
public static Throwable staticThrowableMethod() { return A_THROWABLE_OBJECT; }
public Throwable throwableMethod() { return staticThrowableMethod(); }
@@ -132,43 +138,208 @@ public class QtJniObjectTestClass
public static Object[] staticObjectArrayMethod()
{ Object[] array = { new Object(), new Object(), new Object() }; return array; }
public Object[] objectArrayMethod() { return staticObjectArrayMethod(); }
+ public static Object[] staticReverseObjectArray(Object[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ Object old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public Object[] reverseObjectArray(Object[] array)
+ { return staticReverseObjectArray(array); }
// --------------------------------------------------------------------------------------------
public static boolean[] staticBooleanArrayMethod()
{ boolean[] array = { true, true, true }; return array; }
public boolean[] booleanArrayMethod() { return staticBooleanArrayMethod(); }
+ public static boolean[] staticReverseBooleanArray(boolean[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ boolean old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public boolean[] reverseBooleanArray(boolean[] array)
+ { return staticReverseBooleanArray(array); }
// --------------------------------------------------------------------------------------------
public static byte[] staticByteArrayMethod()
{ byte[] array = { 'a', 'b', 'c' }; return array; }
public byte[] byteArrayMethod() { return staticByteArrayMethod(); }
+ public static byte[] staticReverseByteArray(byte[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ byte old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public byte[] reverseByteArray(byte[] array)
+ { return staticReverseByteArray(array); }
// --------------------------------------------------------------------------------------------
public static char[] staticCharArrayMethod()
{ char[] array = { 'a', 'b', 'c' }; return array; }
public char[] charArrayMethod() { return staticCharArrayMethod(); }
+ public static char[] staticReverseCharArray(char[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ char old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public char[] reverseCharArray(char[] array)
+ { return staticReverseCharArray(array); }
// --------------------------------------------------------------------------------------------
public static short[] staticShortArrayMethod() { short[] array = { 3, 2, 1 }; return array; }
public short[] shortArrayMethod() { return staticShortArrayMethod(); }
+ public static short[] staticReverseShortArray(short[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ short old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public short[] reverseShortArray(short[] array)
+ { return staticReverseShortArray(array); }
// --------------------------------------------------------------------------------------------
public static int[] staticIntArrayMethod() { int[] array = { 3, 2, 1 }; return array; }
public int[] intArrayMethod() { return staticIntArrayMethod(); }
+ public static int[] staticReverseIntArray(int[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ int old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public int[] reverseIntArray(int[] array)
+ { return staticReverseIntArray(array); }
// --------------------------------------------------------------------------------------------
public static long[] staticLongArrayMethod()
{ long[] array = { 3, 2, 1 }; return array; }
public long[] longArrayMethod() { return staticLongArrayMethod(); }
+ public static long[] staticReverseLongArray(long[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ long old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public long[] reverseLongArray(long[] array)
+ { return staticReverseLongArray(array); }
// --------------------------------------------------------------------------------------------
public static float[] staticFloatArrayMethod()
{ float[] array = { 1.0f, 2.0f, 3.0f }; return array; }
public float[] floatArrayMethod() { return staticFloatArrayMethod(); }
+ public static float[] staticReverseFloatArray(float[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ float old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public float[] reverseFloatArray(float[] array)
+ { return staticReverseFloatArray(array); }
// --------------------------------------------------------------------------------------------
public static double[] staticDoubleArrayMethod()
{ double[] array = { 3.0, 2.0, 1.0 }; return array; }
public double[] doubleArrayMethod() { return staticDoubleArrayMethod(); }
-}
+ public static double[] staticReverseDoubleArray(double[] array)
+ {
+ for (int i = 0; i < array.length / 2; ++i) {
+ double old = array[array.length - i - 1];
+ array[array.length - i - 1] = array[i];
+ array[i] = old;
+ }
+ return array;
+ }
+ public double[] reverseDoubleArray(double[] array)
+ { return staticReverseDoubleArray(array); }
+ // --------------------------------------------------------------------------------------------
+ native public int callbackWithObject(QtJniObjectTestClass that);
+ native public int callbackWithObjectRef(QtJniObjectTestClass that);
+ native public int callbackWithString(String string);
+ native public int callbackWithByte(byte value);
+ native public int callbackWithBoolean(boolean value);
+ native public int callbackWithInt(int value);
+ native public int callbackWithDouble(double value);
+ native public int callbackWithJniArray(double[] value);
+ native public int callbackWithQList(double[] value);
+ native public int callbackWithStringList(String[] value);
+
+ public int callMeBackWithObject(QtJniObjectTestClass that)
+ {
+ return callbackWithObject(that);
+ }
+
+ public int callMeBackWithObjectRef(QtJniObjectTestClass that)
+ {
+ return callbackWithObjectRef(that);
+ }
+
+ public int callMeBackWithString(String string)
+ {
+ return callbackWithString(string);
+ }
+
+ public int callMeBackWithByte(byte value)
+ {
+ return callbackWithByte(value);
+ }
+
+ public int callMeBackWithBoolean(boolean value)
+ {
+ return callbackWithBoolean(value);
+ }
+
+ public int callMeBackWithInt(int value)
+ {
+ return callbackWithInt(value);
+ }
+
+ public int callMeBackWithDouble(double value)
+ {
+ return callbackWithDouble(value);
+ }
+ public int callMeBackWithJniArray(double[] value)
+ {
+ return callbackWithJniArray(value);
+ }
+ public int callMeBackWithQList(double[] value)
+ {
+ return callbackWithQList(value);
+ }
+ public int callMeBackWithStringList(String[] value)
+ {
+ return callbackWithStringList(value);
+ }
+
+ public Object callMethodThrowsException() throws Exception {
+ throw new Exception();
+ }
+
+ public static Object callStaticMethodThrowsException() throws Exception {
+ throw new Exception();
+ }
+}
diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
index 8faa9b6fbb..64b464e002 100644
--- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
+++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <jni.h>
@@ -8,7 +8,11 @@
#include <QtCore/QJniObject>
#include <QtTest>
-static const char testClassName[] = "org/qtproject/qt/android/testdatapackage/QtJniObjectTestClass";
+using namespace Qt::StringLiterals;
+
+static constexpr const char testClassName[] = "org/qtproject/qt/android/testdatapackage/QtJniObjectTestClass";
+Q_DECLARE_JNI_CLASS(QtJniObjectTestClass, testClassName)
+using TestClass = QtJniTypes::QtJniObjectTestClass;
static const jbyte A_BYTE_VALUE = 127;
static const jshort A_SHORT_VALUE = 32767;
@@ -32,12 +36,16 @@ public:
private slots:
void initTestCase();
+ void init();
void ctor();
void callMethodTest();
+ void callMethodThrowsException();
void callObjectMethodTest();
void stringConvertionTest();
void compareOperatorTests();
+ void className();
+ void callStaticMethodThrowsException();
void callStaticObjectMethodClassName();
void callStaticObjectMethod();
void callStaticObjectMethodById();
@@ -71,6 +79,7 @@ private slots:
void getStaticIntField();
void getStaticByteFieldClassName();
void getStaticByteField();
+ void getStaticBooleanField();
void getStaticLongFieldClassName();
void getStaticLongField();
void getStaticDoubleFieldClassName();
@@ -106,6 +115,11 @@ private slots:
void templateApiCheck();
void isClassAvailable();
void fromLocalRef();
+ void largeObjectArray();
+
+ void callback_data();
+ void callback();
+ void callStaticOverloadResolution();
void cleanupTestCase();
};
@@ -118,6 +132,14 @@ void tst_QJniObject::initTestCase()
{
}
+void tst_QJniObject::init()
+{
+ // Unless explicitly ignored to test error handling, warning messages
+ // in this test about a failure to look up a field, method, or class
+ // make the test fail.
+ QTest::failOnWarning(QRegularExpression("java.lang.NoSuch.*Error"));
+}
+
void tst_QJniObject::cleanupTestCase()
{
}
@@ -135,6 +157,21 @@ void tst_QJniObject::ctor()
}
{
+ QJniObject object = QJniObject::construct<jstring>();
+ QVERIFY(object.isValid());
+ }
+
+ {
+ // from Qt 6.7 on we can construct declared classes through the helper type
+ QJniObject object = TestClass::construct();
+ QVERIFY(object.isValid());
+
+ // or even directly
+ TestClass testObject;
+ QVERIFY(testObject.isValid());
+ }
+
+ {
QJniObject string = QJniObject::fromString(QLatin1String("Hello, Java"));
QJniObject object("java/lang/String", "(Ljava/lang/String;)V", string.object<jstring>());
QVERIFY(object.isValid());
@@ -143,7 +180,7 @@ void tst_QJniObject::ctor()
{
QJniObject string = QJniObject::fromString(QLatin1String("Hello, Java"));
- QJniObject object("java/lang/String", string.object<jstring>());
+ QJniObject object = QJniObject::construct<jstring>(string.object<jstring>());
QVERIFY(object.isValid());
QCOMPARE(string.toString(), object.toString());
}
@@ -179,8 +216,10 @@ void tst_QJniObject::ctor()
void tst_QJniObject::callMethodTest()
{
{
- QJniObject jString1 = QJniObject::fromString(QLatin1String("Hello, Java"));
- QJniObject jString2 = QJniObject::fromString(QLatin1String("hELLO, jAVA"));
+ const QString qString1 = u"Hello, Java"_s;
+ const QString qString2 = u"hELLO, jAVA"_s;
+ QJniObject jString1 = QJniObject::fromString(qString1);
+ QJniObject jString2 = QJniObject::fromString(qString2);
QVERIFY(jString1 != jString2);
const jboolean isEmpty = jString1.callMethod<jboolean>("isEmpty");
@@ -193,6 +232,10 @@ void tst_QJniObject::callMethodTest()
ret = jString1.callMethod<jint>("compareToIgnoreCase", jString2.object<jstring>());
QVERIFY(0 == ret);
+
+ // as of Qt 6.7, we can pass QString directly
+ ret = jString1.callMethod<jint>("compareToIgnoreCase", qString2);
+ QVERIFY(0 == ret);
}
{
@@ -211,9 +254,25 @@ void tst_QJniObject::callMethodTest()
QJniObject subString = jString.callMethod<jstring>("substring", 0, 4);
QCOMPARE(subString.toString(), qString.mid(0, 4));
+
+ // and as of Qt 6.7, we can return and take QString directly
+ QCOMPARE(jString.callMethod<QString>("substring", 0, 4), qString.mid(0, 4));
+
+ QCOMPARE(jString.callMethod<jstring>("substring", 0, 7)
+ .callMethod<jstring>("toUpperCase")
+ .callMethod<QString>("concat", u"C++"_s), u"HELLO, C++"_s);
}
}
+void tst_QJniObject::callMethodThrowsException()
+{
+ QtJniTypes::QtJniObjectTestClass instance;
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.Exception"));
+ auto res = instance.callMethod<jobject>("callMethodThrowsException");
+ QVERIFY(!res.isValid());
+ QVERIFY(!QJniEnvironment().checkAndClearExceptions());
+}
+
void tst_QJniObject::callObjectMethodTest()
{
const QString qString = QLatin1String("Hello, Java");
@@ -261,7 +320,7 @@ void tst_QJniObject::compareOperatorTests()
QJniObject stringObject2 = QJniObject::fromString(str);
QVERIFY(stringObject != stringObject2);
- jstring jstrobj = 0;
+ jstring jstrobj = nullptr;
QJniObject invalidStringObject;
QVERIFY(invalidStringObject == jstrobj);
@@ -270,6 +329,36 @@ void tst_QJniObject::compareOperatorTests()
QVERIFY(!invalidStringObject.isValid());
}
+void tst_QJniObject::className()
+{
+ const QString str("Hello!");
+ QJniObject jString = QJniObject::fromString(str);
+ {
+ QCOMPARE(jString.className(), "java/lang/String");
+ QCOMPARE(jString.toString(), str);
+ }
+
+ {
+ QJniObject strObject = QJniObject("java/lang/String", str);
+ QCOMPARE(strObject.className(), "java/lang/String");
+ QCOMPARE(strObject.toString(), str);
+ }
+
+ {
+ TestClass test;
+ QCOMPARE(test.className(), testClassName);
+ }
+}
+
+void tst_QJniObject::callStaticMethodThrowsException()
+{
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.Exception"));
+ auto res = QtJniTypes::QtJniObjectTestClass::callStaticMethod<jobject>(
+ "callStaticMethodThrowsException");
+ QVERIFY(!res.isValid());
+ QVERIFY(!QJniEnvironment().checkAndClearExceptions());
+}
+
void tst_QJniObject::callStaticObjectMethodClassName()
{
QJniObject formatString = QJniObject::fromString(QLatin1String("test format"));
@@ -304,7 +393,8 @@ void tst_QJniObject::callStaticObjectMethod()
jclass cls = env->FindClass("java/lang/String");
QVERIFY(cls != 0);
- QJniObject formatString = QJniObject::fromString(QLatin1String("test format"));
+ const QString string = u"test format"_s;
+ QJniObject formatString = QJniObject::fromString(string);
QVERIFY(formatString.isValid());
QJniObject returnValue = QJniObject::callStaticObjectMethod(cls,
@@ -313,20 +403,14 @@ void tst_QJniObject::callStaticObjectMethod()
formatString.object<jstring>(),
jobjectArray(0));
QVERIFY(returnValue.isValid());
-
- QString returnedString = returnValue.toString();
-
- QCOMPARE(returnedString, QString::fromLatin1("test format"));
+ QCOMPARE(returnValue.toString(), string);
returnValue = QJniObject::callStaticObjectMethod<jstring>(cls,
"format",
formatString.object<jstring>(),
jobjectArray(0));
QVERIFY(returnValue.isValid());
-
- returnedString = returnValue.toString();
-
- QCOMPARE(returnedString, QString::fromLatin1("test format"));
+ QCOMPARE(returnValue.toString(), string);
// from 6.4 on we can use callStaticMethod
returnValue = QJniObject::callStaticMethod<jstring>(cls,
@@ -334,10 +418,20 @@ void tst_QJniObject::callStaticObjectMethod()
formatString.object<jstring>(),
jobjectArray(0));
QVERIFY(returnValue.isValid());
+ QCOMPARE(returnValue.toString(), string);
- returnedString = returnValue.toString();
+ // from 6.7 we can use callStaticMethod without specifying the class string
+ returnValue = QJniObject::callStaticMethod<jstring, jstring>("format",
+ formatString.object<jstring>(),
+ jobjectArray(0));
+ QVERIFY(returnValue.isValid());
+ QCOMPARE(returnValue.toString(), string);
- QCOMPARE(returnedString, QString::fromLatin1("test format"));
+ // from 6.7 we can pass QString directly, both as parameters and return type
+ QString result = QJniObject::callStaticMethod<jstring, QString>("format",
+ string,
+ jobjectArray(0));
+ QCOMPARE(result, string);
}
void tst_QJniObject::callStaticObjectMethodById()
@@ -839,6 +933,10 @@ void tst_QJniObject::getStaticIntField()
jint i = QJniObject::getStaticField<jint>(cls, "SIZE");
QCOMPARE(i, 64);
+
+ enum class Enum { SIZE = 64 };
+ Enum e = QJniObject::getStaticField<Enum>(cls, "SIZE");
+ QCOMPARE(e, Enum::SIZE);
}
void tst_QJniObject::getStaticByteFieldClassName()
@@ -855,6 +953,16 @@ void tst_QJniObject::getStaticByteField()
jbyte i = QJniObject::getStaticField<jbyte>(cls, "MAX_VALUE");
QCOMPARE(i, jbyte(127));
+
+ enum class Enum : jbyte { MAX_VALUE = 127 };
+ Enum e = QJniObject::getStaticField<Enum>(cls, "MAX_VALUE");
+ QCOMPARE(e, Enum::MAX_VALUE);
+}
+
+void tst_QJniObject::getStaticBooleanField()
+{
+ QCOMPARE(TestClass::getStaticField<jboolean>("S_BOOLEAN_VAR"),
+ TestClass::getStaticField<bool>("S_BOOLEAN_VAR"));
}
void tst_QJniObject::getStaticLongFieldClassName()
@@ -871,6 +979,10 @@ void tst_QJniObject::getStaticLongField()
jlong i = QJniObject::getStaticField<jlong>(cls, "MAX_VALUE");
QCOMPARE(i, jlong(9223372036854775807L));
+
+ enum class Enum : jlong { MAX_VALUE = 9223372036854775807L };
+ Enum e = QJniObject::getStaticField<Enum>(cls, "MAX_VALUE");
+ QCOMPARE(e, Enum::MAX_VALUE);
}
void tst_QJniObject::getStaticDoubleFieldClassName()
@@ -923,6 +1035,9 @@ void tst_QJniObject::getStaticShortField()
jshort i = QJniObject::getStaticField<jshort>(cls, "MAX_VALUE");
QCOMPARE(i, jshort(32767));
+ enum class Enum : jshort { MAX_VALUE = 32767 };
+ Enum e = QJniObject::getStaticField<Enum>(cls, "MAX_VALUE");
+ QCOMPARE(e, Enum::MAX_VALUE);
}
void tst_QJniObject::getStaticCharFieldClassName()
@@ -939,24 +1054,29 @@ void tst_QJniObject::getStaticCharField()
jchar i = QJniObject::getStaticField<jchar>(cls, "MAX_VALUE");
QCOMPARE(i, jchar(0xffff));
+
+ enum class Enum : jchar { MAX_VALUE = 0xffff };
+ Enum e = QJniObject::getStaticField<Enum>(cls, "MAX_VALUE");
+ QCOMPARE(e, Enum::MAX_VALUE);
}
void tst_QJniObject::getBooleanField()
{
- QJniObject obj("org/qtproject/qt/android/QtActivityDelegate");
+ QJniObject obj(testClassName);
QVERIFY(obj.isValid());
- QVERIFY(!obj.getField<jboolean>("m_fullScreen"));
+ QVERIFY(obj.getField<jboolean>("BOOL_FIELD"));
+ QVERIFY(obj.getField<bool>("BOOL_FIELD"));
}
void tst_QJniObject::getIntField()
{
- QJniObject obj("org/qtproject/qt/android/QtActivityDelegate");
+ QJniObject obj(testClassName);
QVERIFY(obj.isValid());
- jint res = obj.getField<jint>("m_currentRotation");
- QCOMPARE(res, -1);
+ jint res = obj.getField<jint>("INT_FIELD");
+ QCOMPARE(res, 123);
}
template <typename T>
@@ -974,16 +1094,22 @@ void setField(const char *fieldName, T testValue)
void tst_QJniObject::setIntField()
{
setField("INT_VAR", 555);
+ enum class Enum : jint { VALUE = 555 };
+ setField("INT_VAR", Enum::VALUE);
}
void tst_QJniObject::setByteField()
{
- setField("BYTE_VAR", jbyte(555));
+ setField("BYTE_VAR", jbyte(123));
+ enum class Enum : jbyte { VALUE = 123 };
+ setField("BYTE_VAR", Enum::VALUE);
}
void tst_QJniObject::setLongField()
{
setField("LONG_VAR", jlong(9223372036847758232L));
+ enum class Enum : jlong { VALUE = 9223372036847758232L };
+ setField("LONG_VAR", Enum::VALUE);
}
void tst_QJniObject::setDoubleField()
@@ -998,17 +1124,22 @@ void tst_QJniObject::setFloatField()
void tst_QJniObject::setShortField()
{
- setField("SHORT_VAR", jshort(123));
+ setField("SHORT_VAR", jshort(555));
+ enum class Enum : jshort { VALUE = 555 };
+ setField("SHORT_VAR", Enum::VALUE);
}
void tst_QJniObject::setCharField()
{
setField("CHAR_VAR", jchar('A'));
+ enum class Enum : jchar { VALUE = 'A' };
+ setField("CHAR_VAR", Enum::VALUE);
}
void tst_QJniObject::setBooleanField()
{
setField("BOOLEAN_VAR", jboolean(true));
+ setField("BOOLEAN_VAR", true);
}
void tst_QJniObject::setObjectField()
@@ -1016,11 +1147,16 @@ void tst_QJniObject::setObjectField()
QJniObject obj(testClassName);
QVERIFY(obj.isValid());
- QJniObject testValue = QJniObject::fromString(QStringLiteral("Hello"));
+ const QString qString = u"Hello"_s;
+ QJniObject testValue = QJniObject::fromString(qString);
obj.setField("STRING_OBJECT_VAR", testValue.object<jstring>());
QJniObject res = obj.getObjectField<jstring>("STRING_OBJECT_VAR");
QCOMPARE(res.toString(), testValue.toString());
+
+ // as of Qt 6.7, we can set and get strings directly
+ obj.setField("STRING_OBJECT_VAR", qString);
+ QCOMPARE(obj.getField<QString>("STRING_OBJECT_VAR"), qString);
}
template <typename T>
@@ -1030,21 +1166,33 @@ void setStaticField(const char *fieldName, T testValue)
T res = QJniObject::getStaticField<T>(testClassName, fieldName);
QCOMPARE(res, testValue);
+
+ // use template overload to reset to default
+ T defaultValue = {};
+ TestClass::setStaticField(fieldName, defaultValue);
+ res = TestClass::getStaticField<T>(fieldName);
+ QCOMPARE(res, defaultValue);
}
void tst_QJniObject::setStaticIntField()
{
setStaticField("S_INT_VAR", 555);
+ enum class Enum : jint { VALUE = 555 };
+ setStaticField("S_INT_VAR", Enum::VALUE);
}
void tst_QJniObject::setStaticByteField()
{
- setStaticField("S_BYTE_VAR", jbyte(555));
+ setStaticField("S_BYTE_VAR", jbyte(123));
+ enum class Enum : jbyte { VALUE = 123 };
+ setStaticField("S_BYTE_VAR", Enum::VALUE);
}
void tst_QJniObject::setStaticLongField()
{
setStaticField("S_LONG_VAR", jlong(9223372036847758232L));
+ enum class Enum : jlong { VALUE = 9223372036847758232L };
+ setStaticField("S_LONG_VAR", Enum::VALUE);
}
void tst_QJniObject::setStaticDoubleField()
@@ -1059,26 +1207,37 @@ void tst_QJniObject::setStaticFloatField()
void tst_QJniObject::setStaticShortField()
{
- setStaticField("S_SHORT_VAR", jshort(123));
+ setStaticField("S_SHORT_VAR", jshort(555));
+ enum class Enum : jshort { VALUE = 555 };
+ setStaticField("S_SHORT_VAR", Enum::VALUE);
}
void tst_QJniObject::setStaticCharField()
{
setStaticField("S_CHAR_VAR", jchar('A'));
+ enum class Enum : jchar { VALUE = 'A' };
+ setStaticField("S_CHAR_VAR", Enum::VALUE);
}
void tst_QJniObject::setStaticBooleanField()
{
setStaticField("S_BOOLEAN_VAR", jboolean(true));
+ setStaticField("S_BOOLEAN_VAR", true);
}
void tst_QJniObject::setStaticObjectField()
{
- QJniObject testValue = QJniObject::fromString(QStringLiteral("Hello"));
+ const QString qString = u"Hello"_s;
+ QJniObject testValue = QJniObject::fromString(qString);
QJniObject::setStaticField(testClassName, "S_STRING_OBJECT_VAR", testValue.object<jstring>());
QJniObject res = QJniObject::getStaticObjectField<jstring>(testClassName, "S_STRING_OBJECT_VAR");
QCOMPARE(res.toString(), testValue.toString());
+
+ // as of Qt 6.7, we can set and get strings directly
+ using namespace QtJniTypes;
+ QtJniObjectTestClass::setStaticField("S_STRING_OBJECT_VAR", qString);
+ QCOMPARE(QtJniObjectTestClass::getStaticField<QString>("S_STRING_OBJECT_VAR"), qString);
}
void tst_QJniObject::templateApiCheck()
@@ -1365,11 +1524,42 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jobjectArray>(testClassName,
"staticObjectArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jobject[]>("staticObjectArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+
+ QJniArray<jobject> newArray(QList<QJniObject>{QJniObject::fromString(u"one"_s),
+ QJniObject::fromString(u"two"_s),
+ QJniObject::fromString(u"three"_s)});
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jobject[]>("staticReverseObjectArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.size(), 3);
+ QCOMPARE(QJniObject(reverse.at(0)).toString(), u"three"_s);
+ QCOMPARE(QJniObject(reverse.at(1)).toString(), u"two"_s);
+ QCOMPARE(QJniObject(reverse.at(2)).toString(), u"one"_s);
}
{
QJniObject res = testClass.callObjectMethod<jobjectArray>("objectArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jobject[]>("objectArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+
+ QJniArray<jobject> newArray(QList<QJniObject>{QJniObject::fromString(u"one"_s),
+ QJniObject::fromString(u"two"_s),
+ QJniObject::fromString(u"three"_s)});
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jobject[]>("reverseObjectArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.size(), 3);
+ // QJniArray::at returns a jobject that's a local reference; make sure we don't free it twice
+ QCOMPARE(QJniObject::fromLocalRef(reverse.at(0)).toString(), u"three"_s);
+ QCOMPARE(QJniObject::fromLocalRef(reverse.at(1)).toString(), u"two"_s);
+ QCOMPARE(QJniObject::fromLocalRef(reverse.at(2)).toString(), u"one"_s);
}
// jbooleanArray ------------------------------------------------------------------------------
@@ -1377,11 +1567,33 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jbooleanArray>(testClassName,
"staticBooleanArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jboolean[]>("staticBooleanArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jboolean>{true, true, true}));
+
+ QJniArray<jboolean> newArray(QList<jboolean>{true, false, false});
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jboolean[]>("staticReverseBooleanArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jboolean>{false, false, true}));
}
{
QJniObject res = testClass.callObjectMethod<jbooleanArray>("booleanArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jboolean[]>("booleanArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jboolean>{true, true, true}));
+
+ QJniArray<jboolean> newArray(QList<jboolean>{true, false, false});
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jboolean[]>("reverseBooleanArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jboolean>{false, false, true}));
}
// jbyteArray ---------------------------------------------------------------------------------
@@ -1389,11 +1601,37 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jbyteArray>(testClassName,
"staticByteArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jbyte[]>("staticByteArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), "abc");
+
+ QJniArray<jbyte> newArray(QByteArray{"cba"});
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jbyte[]>("staticReverseByteArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), "abc");
+
+ const QByteArray reverse2 = TestClass::callStaticMethod<QByteArray>("staticReverseByteArray",
+ QByteArray("abc"));
+ QCOMPARE(reverse2, "cba");
}
{
QJniObject res = testClass.callObjectMethod<jbyteArray>("byteArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jbyte[]>("byteArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), "abc");
+
+ QJniArray<jbyte> newArray = QJniArrayBase::fromContainer(QByteArray{"cba"});
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jbyte[]>("reverseByteArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), "abc");
}
// jcharArray ---------------------------------------------------------------------------------
@@ -1401,11 +1639,37 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jcharArray>(testClassName,
"staticCharArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jchar[]>("staticCharArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jchar>{u'a', u'b', u'c'}));
+
+ QJniArray<jchar> newArray = {u'c', u'b', u'a'};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jchar[]>("staticReverseCharArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jchar>{u'a', u'b', u'c'}));
+
+ const QList<jchar> reverse2 = TestClass::callStaticMethod<QList<jchar>>("staticReverseCharArray",
+ (QList<jchar>{u'c', u'b', u'a'}));
+ QCOMPARE(reverse2, (QList<jchar>{u'a', u'b', u'c'}));
}
{
QJniObject res = testClass.callObjectMethod<jcharArray>("charArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jchar[]>("charArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jchar>{u'a', u'b', u'c'}));
+
+ QJniArray<jchar> newArray = {u'c', u'b', u'a'};
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jchar[]>("reverseCharArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jchar>{u'a', u'b', u'c'}));
}
// jshortArray --------------------------------------------------------------------------------
@@ -1413,11 +1677,38 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jshortArray>(testClassName,
"staticShortArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jshort[]>("staticShortArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jshort>{3, 2, 1}));
+
+ QJniArray<jshort> newArray = {3, 2, 1};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jshort[]>("staticReverseShortArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jshort>{1, 2, 3}));
+
+ const QList<jshort> reverse2 = TestClass::callStaticMethod<QList<jshort>>("staticReverseShortArray",
+ (QList<jshort>{1, 2, 3}));
+ QCOMPARE(reverse2, (QList<jshort>{3, 2, 1}));
}
{
QJniObject res = testClass.callObjectMethod<jshortArray>("shortArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jshort[]>("shortArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jshort>{3, 2, 1}));
+
+ QJniArray<jshort> newArray = {3, 2, 1};
+ static_assert(std::is_same_v<decltype(newArray)::Type, jshort>);
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jshort[]>("reverseShortArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jshort>{1, 2, 3}));
}
// jintArray ----------------------------------------------------------------------------------
@@ -1425,11 +1716,33 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jintArray>(testClassName,
"staticIntArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jint[]>("staticIntArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jint>{3, 2, 1}));
+
+ QJniArray<jint> newArray = {3, 2, 1};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jint[]>("staticReverseIntArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jint>{1, 2, 3}));
}
{
QJniObject res = testClass.callObjectMethod<jintArray>("intArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jint[]>("intArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jint>{3, 2, 1}));
+
+ QJniArray<jint> newArray = {3, 2, 1};
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jint[]>("reverseIntArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jint>{1, 2, 3}));
}
// jlongArray ---------------------------------------------------------------------------------
@@ -1437,11 +1750,33 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jlongArray>(testClassName,
"staticLongArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jlong[]>("staticLongArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jlong>{3, 2, 1}));
+
+ QJniArray<jlong> newArray = {3, 2, 1};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jlong[]>("staticReverseLongArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jlong>{1, 2, 3}));
}
{
QJniObject res = testClass.callObjectMethod<jlongArray>("longArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jlong[]>("longArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jlong>{3, 2, 1}));
+
+ QJniArray<jlong> newArray = {3, 2, 1};
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jlong[]>("reverseLongArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jlong>{1, 2, 3}));
}
// jfloatArray --------------------------------------------------------------------------------
@@ -1449,11 +1784,33 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jfloatArray>(testClassName,
"staticFloatArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jfloat[]>("staticFloatArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jfloat>{1.0f, 2.0f, 3.0f}));
+
+ QJniArray<jfloat> newArray = {3.0f, 2.0f, 1.0f};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jfloat[]>("staticReverseFloatArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jfloat>{1.0f, 2.0f, 3.0f}));
}
{
QJniObject res = testClass.callObjectMethod<jfloatArray>("floatArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jfloat[]>("floatArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jfloat>{1.0f, 2.0f, 3.0f}));
+
+ QJniArray<jfloat> newArray = {3.0f, 2.0f, 1.0f};
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jfloat[]>("reverseFloatArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jfloat>{1.0f, 2.0f, 3.0f}));
}
// jdoubleArray -------------------------------------------------------------------------------
@@ -1461,11 +1818,33 @@ void tst_QJniObject::templateApiCheck()
QJniObject res = QJniObject::callStaticObjectMethod<jdoubleArray>(testClassName,
"staticDoubleArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = TestClass::callStaticMethod<jdouble[]>("staticDoubleArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jdouble>{3.0, 2.0, 1.0}));
+
+ QJniArray<jdouble> newArray = {3.0, 2.0, 1.0};
+ QVERIFY(newArray.isValid());
+ const auto reverse = TestClass::callStaticMethod<jdouble[]>("staticReverseDoubleArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jdouble>{1.0, 2.0, 3.0}));
}
{
QJniObject res = testClass.callObjectMethod<jdoubleArray>("doubleArrayMethod");
QVERIFY(res.isValid());
+
+ const auto array = testClass.callMethod<jdouble[]>("doubleArrayMethod");
+ QVERIFY(array.isValid());
+ QCOMPARE(array.size(), 3);
+ QCOMPARE(array.toContainer(), (QList<jdouble>{3.0, 2.0, 1.0}));
+
+ QJniArray<jdouble> newArray = {3.0, 2.0, 1.0};
+ QVERIFY(newArray.isValid());
+ const auto reverse = testClass.callMethod<jdouble[]>("reverseDoubleArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.toContainer(), (QList<jdouble>{1.0, 2.0, 3.0}));
}
}
@@ -1473,6 +1852,7 @@ void tst_QJniObject::templateApiCheck()
void tst_QJniObject::isClassAvailable()
{
QVERIFY(QJniObject::isClassAvailable("java/lang/String"));
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("java.lang.ClassNotFoundException"));
QVERIFY(!QJniObject::isClassAvailable("class/not/Available"));
QVERIFY(QJniObject::isClassAvailable("org/qtproject/qt/android/QtActivityDelegate"));
}
@@ -1485,6 +1865,244 @@ void tst_QJniObject::fromLocalRef()
QJniObject o = QJniObject::fromLocalRef(env->FindClass("java/lang/String"));
}
+void tst_QJniObject::largeObjectArray()
+{
+ QJniArray<jobject> newArray(QList<QJniObject>{QJniObject::fromString(u"one"_s),
+ QJniObject::fromString(u"two"_s),
+ QJniObject::fromString(u"three"_s)});
+ QVERIFY(newArray.isValid());
+ const QJniArray<QJniObject> reverse = TestClass::callStaticMethod<jobject[]>(
+ "staticReverseObjectArray", newArray);
+ QVERIFY(reverse.isValid());
+ QCOMPARE(reverse.size(), 3);
+
+ // make sure we don't leak local references
+ for (int i = 0; i < 10000; ++i) {
+ QVERIFY(reverse.at(0).isValid());
+ QVERIFY(reverse.at(1).isValid());
+ QVERIFY(reverse.at(2).isValid());
+ }
+}
+
+enum class CallbackParameterType
+{
+ Object,
+ ObjectRef,
+ String,
+ Byte,
+ Boolean,
+ Int,
+ Double,
+ JniArray,
+ QList,
+ QStringList,
+};
+
+static std::optional<TestClass> calledWithObject;
+static int callbackWithObject(JNIEnv *, jobject, TestClass that)
+{
+ calledWithObject.emplace(that);
+ return int(CallbackParameterType::Object);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithObject)
+static int callbackWithObjectRef(JNIEnv *, jobject, const TestClass &that)
+{
+ calledWithObject.emplace(that);
+ return int(CallbackParameterType::ObjectRef);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithObjectRef)
+
+static std::optional<QString> calledWithString;
+static int callbackWithString(JNIEnv *, jobject, const QString &string)
+{
+ calledWithString.emplace(string);
+ return int(CallbackParameterType::String);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithString)
+
+static std::optional<jbyte> calledWithByte;
+static int callbackWithByte(JNIEnv *, jobject, jbyte value)
+{
+ calledWithByte.emplace(value);
+ return int(CallbackParameterType::Byte);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithByte)
+
+static std::optional<jbyte> calledWithBoolean;
+static int callbackWithBoolean(JNIEnv *, jobject, bool value)
+{
+ calledWithBoolean.emplace(value);
+ return int(CallbackParameterType::Boolean);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithBoolean)
+
+static std::optional<int> calledWithInt;
+static int callbackWithInt(JNIEnv *, jobject, int value)
+{
+ calledWithInt.emplace(value);
+ return int(CallbackParameterType::Int);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithInt)
+
+static std::optional<double> calledWithDouble;
+static int callbackWithDouble(JNIEnv *, jobject, double value)
+{
+ calledWithDouble.emplace(value);
+ return int(CallbackParameterType::Double);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithDouble)
+
+static std::optional<QJniArray<jdouble>> calledWithJniArray;
+static int callbackWithJniArray(JNIEnv *, jobject, const QJniArray<jdouble> &value)
+{
+ calledWithJniArray.emplace(value);
+ return int(CallbackParameterType::JniArray);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithJniArray)
+
+static std::optional<QList<double>> calledWithQList;
+static int callbackWithQList(JNIEnv *, jobject, const QList<double> &value)
+{
+ calledWithQList.emplace(value);
+ return int(CallbackParameterType::QList);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithQList)
+
+static std::optional<QStringList> calledWithStringList;
+static int callbackWithStringList(JNIEnv *, jobject, const QStringList &value)
+{
+ calledWithStringList.emplace(value);
+ return int(CallbackParameterType::QStringList);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithStringList)
+
+void tst_QJniObject::callback_data()
+{
+ QTest::addColumn<CallbackParameterType>("parameterType");
+
+ QTest::addRow("Object") << CallbackParameterType::Object;
+ QTest::addRow("ObjectRef") << CallbackParameterType::ObjectRef;
+ QTest::addRow("String") << CallbackParameterType::String;
+ QTest::addRow("Byte") << CallbackParameterType::Byte;
+ QTest::addRow("Boolean") << CallbackParameterType::Boolean;
+ QTest::addRow("Int") << CallbackParameterType::Int;
+ QTest::addRow("Double") << CallbackParameterType::Double;
+ QTest::addRow("JniArray") << CallbackParameterType::JniArray;
+ QTest::addRow("QList") << CallbackParameterType::QList;
+ QTest::addRow("QStringList") << CallbackParameterType::QStringList;
+}
+
+void tst_QJniObject::callback()
+{
+ QFETCH(const CallbackParameterType, parameterType);
+
+ TestClass testObject;
+ int result = -1;
+
+ switch (parameterType) {
+ case CallbackParameterType::Object:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithObject)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithObject", testObject);
+ QVERIFY(calledWithObject);
+ QCOMPARE(calledWithObject.value(), testObject);
+ break;
+ case CallbackParameterType::ObjectRef:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithObjectRef)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithObjectRef", testObject);
+ QVERIFY(calledWithObject);
+ QCOMPARE(calledWithObject.value(), testObject);
+ break;
+ case CallbackParameterType::String:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithString)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithString", QString::number(123));
+ QVERIFY(calledWithString);
+ QCOMPARE(calledWithString.value(), "123");
+ break;
+ case CallbackParameterType::Byte:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithByte)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithByte", jbyte(123));
+ QVERIFY(calledWithByte);
+ QCOMPARE(calledWithByte.value(), 123);
+ break;
+ case CallbackParameterType::Boolean:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithBoolean)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithBoolean", true);
+ QVERIFY(calledWithBoolean);
+ QCOMPARE(calledWithBoolean.value(), true);
+ break;
+ case CallbackParameterType::Int:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithInt)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithInt", 12345);
+ QVERIFY(calledWithInt);
+ QCOMPARE(calledWithInt.value(), 12345);
+ break;
+ case CallbackParameterType::Double:
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithDouble)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithDouble", 1.2345);
+ QVERIFY(calledWithDouble);
+ QCOMPARE(calledWithDouble.value(), 1.2345);
+ break;
+ case CallbackParameterType::JniArray: {
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithJniArray)
+ }));
+ const QJniArray<double> doubles = { 1.2, 3.4, 5.6 };
+ result = testObject.callMethod<int>("callMeBackWithJniArray", doubles);
+ QVERIFY(calledWithJniArray);
+ QCOMPARE(calledWithJniArray, doubles);
+ break;
+ }
+ case CallbackParameterType::QList: {
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithQList)
+ }));
+ const QList<double> doubles = { 1.2, 3.4, 5.6 };
+ result = testObject.callMethod<int>("callMeBackWithQList", doubles);
+ QVERIFY(calledWithQList);
+ QCOMPARE(calledWithQList.value(), doubles);
+ break;
+ }
+ case CallbackParameterType::QStringList: {
+ QVERIFY(TestClass::registerNativeMethods({
+ Q_JNI_NATIVE_METHOD(callbackWithStringList)
+ }));
+ const QStringList strings = { "one", "two" };
+ result = testObject.callMethod<int>("callMeBackWithStringList", strings);
+ QVERIFY(calledWithStringList);
+ QCOMPARE(calledWithStringList.value(), strings);
+ break;
+ }
+ }
+ QCOMPARE(result, int(parameterType));
+}
+
+// Make sure the new callStaticMethod overload taking a class, return type,
+// and argument as template parameters, doesn't break overload resolution
+// and that the class name doesn't get interpreted as the function name.
+void tst_QJniObject::callStaticOverloadResolution()
+{
+ const QString value = u"Hello World"_s;
+ QJniObject str = QJniObject::fromString(value);
+ const auto result = QJniObject::callStaticMethod<jstring, jstring>(
+ QtJniTypes::Traits<TestClass>::className(),
+ "staticEchoMethod", str.object<jstring>()).toString();
+ QCOMPARE(result, value);
+}
+
QTEST_MAIN(tst_QJniObject)
#include "tst_qjniobject.moc"
diff --git a/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt b/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt
index 8184e91a11..104b039d4d 100644
--- a/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qjnitypes/CMakeLists.txt
@@ -1,3 +1,12 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qjnitypes LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qjnitypes
SOURCES
tst_qjnitypes.cpp
diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
index c4abfe399a..bf582041f3 100644
--- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
+++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
@@ -1,9 +1,12 @@
// Copyright (C) 2022 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest>
#include <QtCore/qjnitypes.h>
+#include <QtCore/qjniarray.h>
+
+using namespace Qt::StringLiterals;
class tst_QJniTypes : public QObject
{
@@ -12,48 +15,75 @@ class tst_QJniTypes : public QObject
public:
tst_QJniTypes() = default;
+ static void nativeClassMethod(JNIEnv *, jclass, int);
+ Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(nativeClassMethod);
+
private slots:
void initTestCase();
+ void nativeMethod();
+ void construct();
+ void stringTypeCantBeArgument();
};
struct QtJavaWrapper {};
template<>
-constexpr auto QtJniTypes::typeSignature<QtJavaWrapper>()
+struct QtJniTypes::Traits<QtJavaWrapper>
{
- return QtJniTypes::String("Lorg/qtproject/qt/android/QtJavaWrapper;");
-}
+ static constexpr auto signature()
+ {
+ return QtJniTypes::CTString("Lorg/qtproject/qt/android/QtJavaWrapper;");
+ }
+};
template<>
-constexpr auto QtJniTypes::typeSignature<QJniObject>()
+struct QtJniTypes::Traits<QJniObject>
{
- return QtJniTypes::String("Ljava/lang/Object;");
-}
+ static constexpr auto signature()
+ {
+ return QtJniTypes::CTString("Ljava/lang/Object;");
+ }
+};
struct QtCustomJniObject : QJniObject {};
+
template<>
-constexpr auto QtJniTypes::typeSignature<QtCustomJniObject>()
+struct QtJniTypes::Traits<QtCustomJniObject>
{
- return QtJniTypes::String("Lorg/qtproject/qt/android/QtCustomJniObject;");
-}
+ static constexpr auto signature()
+ {
+ return QtJniTypes::CTString("Lorg/qtproject/qt/android/QtCustomJniObject;");
+ }
+};
+
+static_assert(QtJniTypes::Traits<QtJavaWrapper>::signature() == "Lorg/qtproject/qt/android/QtJavaWrapper;");
+static_assert(QtJniTypes::Traits<QtJavaWrapper>::signature() != "Ljava/lang/Object;");
+static_assert(!(QtJniTypes::Traits<QtJavaWrapper>::signature() == "X"));
+
+Q_DECLARE_JNI_CLASS(JavaType, "org/qtproject/qt/JavaType");
+static_assert(QtJniTypes::Traits<QtJniTypes::JavaType>::signature() == "Lorg/qtproject/qt/JavaType;");
+static_assert(QtJniTypes::Traits<QtJniTypes::JavaType[]>::signature() == "[Lorg/qtproject/qt/JavaType;");
-static_assert(QtJniTypes::typeSignature<QtJavaWrapper>() == "Lorg/qtproject/qt/android/QtJavaWrapper;");
-static_assert(QtJniTypes::typeSignature<QtJavaWrapper>() != "Ljava/lang/Object;");
-static_assert(!(QtJniTypes::typeSignature<QtJavaWrapper>() == "X"));
+Q_DECLARE_JNI_CLASS(String, "java/lang/String");
+static_assert(QtJniTypes::Traits<jstring>::className() == "java/lang/String");
+static_assert(QtJniTypes::Traits<QtJniTypes::String>::className() == "java/lang/String");
+static_assert(QtJniTypes::Traits<QtJniTypes::String>::signature() == "Ljava/lang/String;");
+static_assert(QtJniTypes::Traits<QtJniTypes::String[]>::signature() == "[Ljava/lang/String;");
-Q_DECLARE_JNI_TYPE(JavaType, "Lorg/qtproject/qt/JavaType;");
-static_assert(QtJniTypes::typeSignature<QtJniTypes::JavaType>() == "Lorg/qtproject/qt/JavaType;");
-Q_DECLARE_JNI_TYPE(ArrayType, "[Lorg/qtproject/qt/ArrayType;")
-static_assert(QtJniTypes::typeSignature<QtJniTypes::ArrayType>() == "[Lorg/qtproject/qt/ArrayType;");
+Q_DECLARE_JNI_CLASS(QtTextToSpeech, "org/qtproject/qt/android/speech/QtTextToSpeech")
+static_assert(QtJniTypes::Traits<QtJniTypes::QtTextToSpeech>::className() == "org/qtproject/qt/android/speech/QtTextToSpeech");
static_assert(QtJniTypes::fieldSignature<jint>() == "I");
+static_assert(QtJniTypes::fieldSignature<jint[]>() == "[I");
static_assert(QtJniTypes::fieldSignature<jint>() != "X");
static_assert(QtJniTypes::fieldSignature<jint>() != "Ljava/lang/Object;");
static_assert(QtJniTypes::fieldSignature<jlong>() == "J");
static_assert(QtJniTypes::fieldSignature<jstring>() == "Ljava/lang/String;");
static_assert(QtJniTypes::fieldSignature<jobject>() == "Ljava/lang/Object;");
+static_assert(QtJniTypes::fieldSignature<jobject[]>() == "[Ljava/lang/Object;");
static_assert(QtJniTypes::fieldSignature<jobjectArray>() == "[Ljava/lang/Object;");
static_assert(QtJniTypes::fieldSignature<QJniObject>() == "Ljava/lang/Object;");
static_assert(QtJniTypes::fieldSignature<QtJavaWrapper>() == "Lorg/qtproject/qt/android/QtJavaWrapper;");
+static_assert(QtJniTypes::fieldSignature<QtJavaWrapper[]>() == "[Lorg/qtproject/qt/android/QtJavaWrapper;");
static_assert(QtJniTypes::fieldSignature<QtCustomJniObject>() == "Lorg/qtproject/qt/android/QtCustomJniObject;");
static_assert(QtJniTypes::methodSignature<void>() == "()V");
@@ -76,27 +106,179 @@ static_assert(QtJniTypes::isObjectType<jobject>());
static_assert(QtJniTypes::isObjectType<jobjectArray>());
static_assert(QtJniTypes::isObjectType<QtCustomJniObject>());
-static_assert(QtJniTypes::String("ABCDE").startsWith("ABC"));
-static_assert(QtJniTypes::String("ABCDE").startsWith("A"));
-static_assert(QtJniTypes::String("ABCDE").startsWith("ABCDE"));
-static_assert(!QtJniTypes::String("ABCDE").startsWith("ABCDEF"));
-static_assert(!QtJniTypes::String("ABCDE").startsWith("9AB"));
-static_assert(QtJniTypes::String("ABCDE").startsWith('A'));
-static_assert(!QtJniTypes::String("ABCDE").startsWith('B'));
-
-static_assert(QtJniTypes::String("ABCDE").endsWith("CDE"));
-static_assert(QtJniTypes::String("ABCDE").endsWith("E"));
-static_assert(QtJniTypes::String("ABCDE").endsWith("ABCDE"));
-static_assert(!QtJniTypes::String("ABCDE").endsWith("DEF"));
-static_assert(!QtJniTypes::String("ABCDE").endsWith("ABCDEF"));
-static_assert(QtJniTypes::String("ABCDE").endsWith('E'));
-static_assert(!QtJniTypes::String("ABCDE").endsWith('F'));
+static_assert(!QtJniTypes::isArrayType<jint>());
+static_assert(QtJniTypes::isArrayType<jint[]>());
+static_assert(QtJniTypes::isArrayType<jobject[]>());
+static_assert(QtJniTypes::isArrayType<jobjectArray>());
+static_assert(QtJniTypes::isArrayType<QtJavaWrapper[]>());
+
+static_assert(QtJniTypes::CTString("ABCDE").startsWith("ABC"));
+static_assert(QtJniTypes::CTString("ABCDE").startsWith("A"));
+static_assert(QtJniTypes::CTString("ABCDE").startsWith("ABCDE"));
+static_assert(!QtJniTypes::CTString("ABCDE").startsWith("ABCDEF"));
+static_assert(!QtJniTypes::CTString("ABCDE").startsWith("9AB"));
+static_assert(QtJniTypes::CTString("ABCDE").startsWith('A'));
+static_assert(!QtJniTypes::CTString("ABCDE").startsWith('B'));
+
+static_assert(QtJniTypes::Traits<QJniArray<jobject>>::signature() == "[Ljava/lang/Object;");
+static_assert(QtJniTypes::Traits<QJniArray<jbyte>>::signature() == "[B");
+static_assert(QtJniTypes::isObjectType<QJniArray<jbyte>>());
+
+static_assert(QtJniTypes::CTString("ABCDE").endsWith("CDE"));
+static_assert(QtJniTypes::CTString("ABCDE").endsWith("E"));
+static_assert(QtJniTypes::CTString("ABCDE").endsWith("ABCDE"));
+static_assert(!QtJniTypes::CTString("ABCDE").endsWith("DEF"));
+static_assert(!QtJniTypes::CTString("ABCDE").endsWith("ABCDEF"));
+static_assert(QtJniTypes::CTString("ABCDE").endsWith('E'));
+static_assert(!QtJniTypes::CTString("ABCDE").endsWith('F'));
+
+enum UnscopedEnum {};
+enum class ScopedEnum {};
+enum class IntEnum : int {};
+enum class UnsignedEnum : unsigned {};
+enum class Int8Enum : int8_t {};
+enum class ShortEnum : short {};
+enum class LongEnum : quint64 {};
+enum class JIntEnum : jint {};
+
+static_assert(QtJniTypes::Traits<UnscopedEnum>::signature() == "I");
+static_assert(QtJniTypes::Traits<ScopedEnum>::signature() == "I");
+static_assert(QtJniTypes::Traits<IntEnum>::signature() == "I");
+static_assert(QtJniTypes::Traits<UnsignedEnum>::signature() == "I");
+static_assert(QtJniTypes::Traits<Int8Enum>::signature() == "B");
+static_assert(QtJniTypes::Traits<LongEnum>::signature() == "J");
+static_assert(QtJniTypes::Traits<JIntEnum>::signature() == "I");
void tst_QJniTypes::initTestCase()
{
}
+static bool nativeFunction(JNIEnv *, jclass, int, jstring, quint64)
+{
+ return true;
+}
+Q_DECLARE_JNI_NATIVE_METHOD(nativeFunction)
+
+static_assert(QtJniTypes::nativeMethodSignature(nativeFunction) == "(ILjava/lang/String;J)Z");
+
+static QString nativeFunctionStrings(JNIEnv *, jclass, const QString &, const QtJniTypes::String &)
+{
+ return QString();
+}
+Q_DECLARE_JNI_NATIVE_METHOD(nativeFunctionStrings)
+
+static_assert(QtJniTypes::nativeMethodSignature(nativeFunctionStrings)
+ == "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
+
+static int forwardDeclaredNativeFunction(JNIEnv *, jobject, bool);
+Q_DECLARE_JNI_NATIVE_METHOD(forwardDeclaredNativeFunction)
+static int forwardDeclaredNativeFunction(JNIEnv *, jobject, bool) { return 0; }
+static_assert(QtJniTypes::nativeMethodSignature(forwardDeclaredNativeFunction) == "(Z)I");
+
+static_assert(QtJniTypes::nativeMethodSignature(tst_QJniTypes::nativeClassMethod) == "(I)V");
+void tst_QJniTypes::nativeClassMethod(JNIEnv *, jclass, int) {}
+
+void tst_QJniTypes::nativeMethod()
+{
+ {
+ const auto method = Q_JNI_NATIVE_METHOD(nativeFunction);
+ QVERIFY(method.fnPtr == QtJniMethods::va_nativeFunction);
+ QCOMPARE(method.name, "nativeFunction");
+ QCOMPARE(method.signature, "(ILjava/lang/String;J)Z");
+ }
+
+ {
+ const auto method = Q_JNI_NATIVE_METHOD(forwardDeclaredNativeFunction);
+ QVERIFY(method.fnPtr == QtJniMethods::va_forwardDeclaredNativeFunction);
+ }
+
+ {
+ const auto method = Q_JNI_NATIVE_SCOPED_METHOD(nativeClassMethod, tst_QJniTypes);
+ QVERIFY(method.fnPtr == va_nativeClassMethod);
+ }
+}
+
+void tst_QJniTypes::construct()
+{
+ using namespace QtJniTypes;
+
+ const QString text = u"Java String"_s;
+ String str(text);
+ QVERIFY(str.isValid());
+ QCOMPARE(str.toString(), text);
+
+ jobject jref = nullptr; // must be jobject, not jstring
+ {
+ // if jref would be a jstring, then this would call the
+ // Java String copy constructor!
+ String jstr(jref);
+ QVERIFY(!jstr.isValid());
+ }
+ jref = str.object<jstring>();
+ {
+ String jstr(jref);
+ QVERIFY(jstr.isValid());
+ QCOMPARE(jstr.toString(), text);
+ }
+
+ String str2 = str;
+ QCOMPARE(str.toString(), text);
+ String str3 = std::move(str2);
+ QCOMPARE(str3.toString(), text);
+}
+
+template <typename ...Arg>
+static constexpr bool isValidArgument(Arg &&...) noexcept
+{
+ return QtJniTypes::ValidSignatureTypesDetail<q20::remove_cvref_t<Arg>...>;
+}
+
+enum class Overload
+{
+ ClassNameAndMethod,
+ OnlyMethod,
+};
+
+template <typename Ret, typename ...Args
+#ifndef Q_QDOC
+ , QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
+#endif
+>
+static constexpr auto callStaticMethod(const char *className, const char *methodName, Args &&...)
+{
+ Q_UNUSED(className);
+ Q_UNUSED(methodName);
+ return Overload::ClassNameAndMethod;
+}
+
+template <typename Klass, typename Ret, typename ...Args
+#ifndef Q_QDOC
+ , QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
+#endif
+>
+static constexpr auto callStaticMethod(const char *methodName, Args &&...)
+{
+ Q_UNUSED(methodName);
+ return Overload::OnlyMethod;
+}
+
+void tst_QJniTypes::stringTypeCantBeArgument()
+{
+ const char *methodName = "staticEchoMethod";
+
+ static_assert(!isValidArgument(QtJniTypes::Traits<QtJniTypes::JavaType>::className()));
+ static_assert(!isValidArgument("someFunctionName"));
+ static_assert(!isValidArgument(methodName));
+ static_assert(!isValidArgument(QtJniTypes::Traits<QtJniTypes::JavaType>::className(),
+ "someFunctionName", methodName, 42));
+
+ static_assert(callStaticMethod<jstring, jint>("class name", "method name", 42)
+ == Overload::ClassNameAndMethod);
+ static_assert(callStaticMethod<QtJniTypes::JavaType, jint>("method name", 42)
+ == Overload::OnlyMethod);
+}
+
QTEST_MAIN(tst_QJniTypes)
#include "tst_qjnitypes.moc"
diff --git a/tests/auto/corelib/kernel/qmath/CMakeLists.txt b/tests/auto/corelib/kernel/qmath/CMakeLists.txt
index 7a6d4bd688..39a5a8b6df 100644
--- a/tests/auto/corelib/kernel/qmath/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmath/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qmath.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmath Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmath LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmath
SOURCES
tst_qmath.cpp
diff --git a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp
index d3d470ea5f..1961b71d83 100644
--- a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp
+++ b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp
@@ -1,6 +1,6 @@
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <qmath.h>
diff --git a/tests/auto/corelib/kernel/qmetacontainer/CMakeLists.txt b/tests/auto/corelib/kernel/qmetacontainer/CMakeLists.txt
index 79bb6a9324..a9ebcdf72f 100644
--- a/tests/auto/corelib/kernel/qmetacontainer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetacontainer/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qmetacontainer.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetacontainer Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetacontainer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmetacontainer
SOURCES
tst_qmetacontainer.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
)
diff --git a/tests/auto/corelib/kernel/qmetacontainer/tst_qmetacontainer.cpp b/tests/auto/corelib/kernel/qmetacontainer/tst_qmetacontainer.cpp
index 606e94787a..dff2176a11 100644
--- a/tests/auto/corelib/kernel/qmetacontainer/tst_qmetacontainer.cpp
+++ b/tests/auto/corelib/kernel/qmetacontainer/tst_qmetacontainer.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2020 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/qtest.h>
#include <QtCore/qcontainerinfo.h>
@@ -497,6 +497,10 @@ void tst_QMetaContainer::testSequence()
QVERIFY(metaSequence.compareConstIterator(constIt, constEnd));
metaSequence.destroyConstIterator(constIt);
metaSequence.destroyConstIterator(constEnd);
+
+ QVERIFY(metaSequence.iface() != nullptr);
+ QMetaSequence defaultConstructed;
+ QVERIFY(defaultConstructed.iface() == nullptr);
}
void tst_QMetaContainer::testAssociation_data()
@@ -722,6 +726,10 @@ void tst_QMetaContainer::testAssociation()
QVERIFY(metaAssociation.compareConstIterator(constIt, constEnd));
metaAssociation.destroyConstIterator(constIt);
metaAssociation.destroyConstIterator(constEnd);
+
+ QVERIFY(metaAssociation.iface() != nullptr);
+ QMetaSequence defaultConstructed;
+ QVERIFY(defaultConstructed.iface() == nullptr);
}
QTEST_MAIN(tst_QMetaContainer)
diff --git a/tests/auto/corelib/kernel/qmetaenum/CMakeLists.txt b/tests/auto/corelib/kernel/qmetaenum/CMakeLists.txt
index daee210588..46f3ece069 100644
--- a/tests/auto/corelib/kernel/qmetaenum/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetaenum/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qmetaenum.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetaenum Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetaenum LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmetaenum
SOURCES
tst_qmetaenum.cpp
diff --git a/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp b/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp
index 980830e9ba..3d958a78fe 100644
--- a/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp
+++ b/tests/auto/corelib/kernel/qmetaenum/tst_qmetaenum.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -73,6 +73,9 @@ void tst_QMetaEnum::valuesToKeys()
QMetaEnum me = QMetaEnum::fromType<Qt::WindowFlags>();
QCOMPARE(me.valueToKeys(windowFlags), expected);
+ bool ok = false;
+ QCOMPARE(uint(me.keysToValue(expected, &ok)), windowFlags.toInt());
+ QVERIFY(ok);
}
void tst_QMetaEnum::defaultConstructed()
diff --git a/tests/auto/corelib/kernel/qmetamethod/CMakeLists.txt b/tests/auto/corelib/kernel/qmetamethod/CMakeLists.txt
index 6ab3f143ac..29a6e3c64b 100644
--- a/tests/auto/corelib/kernel/qmetamethod/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetamethod/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qmetamethod.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetamethod Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetamethod LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmetamethod
SOURCES
tst_qmetamethod.cpp
diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
index 2b8ef8358d..47012f9a28 100644
--- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
+++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
@@ -1,6 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com>
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -22,6 +22,7 @@ private slots:
void comparisonOperators();
void fromSignal();
+ void fromSignalOfNullSignalIsInvalid();
void gadget();
void revision();
@@ -32,6 +33,9 @@ private slots:
void parameterTypeName();
void isConst();
+
+ void methodIndexes_data();
+ void methodIndexes();
};
struct CustomType { };
@@ -716,6 +720,12 @@ void tst_QMetaMethod::fromSignal()
#undef FROMSIGNAL_HELPER
}
+void tst_QMetaMethod::fromSignalOfNullSignalIsInvalid()
+{
+ constexpr decltype(&QObject::destroyed) ptr = nullptr;
+ QVERIFY(!QMetaMethod::fromSignal(ptr).isValid());
+}
+
class MyGadget {
Q_GADGET
public:
@@ -862,6 +872,42 @@ void tst_QMetaMethod::isConst()
}
}
+void tst_QMetaMethod::methodIndexes_data()
+{
+ QTest::addColumn<QByteArray>("signature");
+ QTest::addColumn<QMetaMethod::MethodType>("methodType");
+
+ QTest::newRow("constructor1") << QByteArray("MethodTestObject()") << QMetaMethod::Constructor;
+ QTest::newRow("constructor5") << QByteArray("MethodTestObject(CustomUnregisteredType)")
+ << QMetaMethod::Constructor;
+ QTest::newRow("method0") << QByteArray("voidInvokable()") << QMetaMethod::Method;
+ QTest::newRow("method6") << QByteArray("boolInvokable()") << QMetaMethod::Method;
+}
+
+void tst_QMetaMethod::methodIndexes()
+{
+ QFETCH(QByteArray, signature);
+ QFETCH(QMetaMethod::MethodType, methodType);
+
+ const bool isConstructor = methodType == QMetaMethod::Constructor;
+
+ // roundtrip: index = QMetaObject::indexOfConstructor/Method()
+ // <-> method = QMetaObject::constructor/method()
+ // <-> indexThatShouldBeEqualToAboveIndex = QMetaMethod::methodIndex()
+
+ const QMetaObject *mo = &MethodTestObject::staticMetaObject;
+ const int index =
+ isConstructor ? mo->indexOfConstructor(signature) : mo->indexOfMethod(signature);
+ QVERIFY(index != -1);
+
+ QMetaMethod methodFromMetaObject =
+ mo->method(index); // should work on all methods (constructors, signals, ...)
+ const int absoluteMethodIndex =
+ methodFromMetaObject
+ .methodIndex(); // should work on all methods (constructors, signals, ...)
+
+ QCOMPARE(absoluteMethodIndex, index);
+}
QTEST_MAIN(tst_QMetaMethod)
#include "tst_qmetamethod.moc"
diff --git a/tests/auto/corelib/kernel/qmetaobject/CMakeLists.txt b/tests/auto/corelib/kernel/qmetaobject/CMakeLists.txt
index d0736f418d..d17773ada6 100644
--- a/tests/auto/corelib/kernel/qmetaobject/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetaobject/CMakeLists.txt
@@ -1,4 +1,18 @@
-# Generated from qmetaobject.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetaobject LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+set(tst_qmetaobject_SOURCES
+ tst_qmetaobject.cpp
+ forwarddeclared.h
+ forwarddeclared.cpp
+)
+
#####################################################################
## tst_qmetaobject Test:
@@ -6,10 +20,17 @@
qt_internal_add_test(tst_qmetaobject
SOURCES
- tst_qmetaobject.cpp
- PUBLIC_LIBRARIES
+ ${tst_qmetaobject_SOURCES}
+ LIBRARIES
Qt::CorePrivate
)
-## Scopes:
-#####################################################################
+qt_internal_add_test(tst_qmetaobject_compat
+ SOURCES
+ ${tst_qmetaobject_SOURCES}
+ DEFINES
+ USE_COMPAT_Q_ARG=1
+ LIBRARIES
+ Qt::CorePrivate
+ NO_BATCH
+)
diff --git a/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.cpp b/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.cpp
new file mode 100644
index 0000000000..c736a63227
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.cpp
@@ -0,0 +1,17 @@
+// Copyright (C) 2022 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include "forwarddeclared.h"
+
+struct MyForwardDeclaredType { };
+static MyForwardDeclaredType t;
+
+const MyForwardDeclaredType &getForwardDeclaredType() noexcept
+{
+ return t;
+}
+
+MyForwardDeclaredType *getForwardDeclaredPointer() noexcept
+{
+ return &t;
+}
diff --git a/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.h b/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.h
new file mode 100644
index 0000000000..a0158f19cd
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetaobject/forwarddeclared.h
@@ -0,0 +1,12 @@
+// Copyright (C) 2022 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#ifndef FORWARDDECLARED_H
+#define FORWARDDECLARED_H
+
+struct MyForwardDeclaredType; // and ONLY forward-declared
+
+const MyForwardDeclaredType &getForwardDeclaredType() noexcept;
+MyForwardDeclaredType *getForwardDeclaredPointer() noexcept;
+
+#endif // FORWARDDECLARED_H
diff --git a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
index 3b2066ba5d..ee13c32353 100644
--- a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
+++ b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QSignalSpy>
@@ -12,6 +12,27 @@
Q_DECLARE_METATYPE(const QMetaObject *)
+#include "forwarddeclared.h"
+
+#ifdef USE_COMPAT_Q_ARG
+# define tst_QMetaObject tst_QMetaObject_CompatQArg
+# if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
+# error "This is a Qt 6 compatibility check test"
+# endif
+
+# undef Q_ARG
+# undef Q_RETURN_ARG
+# define Q_ARG(type, data) QArgument<type >(#type, data)
+# define Q_RETURN_ARG(type, data) QReturnArgument<type >(#type, data)
+# define Q_NO_ARG , QGenericArgument()
+#else
+// This macro is used to force the overload selection to the compat
+// (non-variadic) code above
+# define Q_NO_ARG
+#endif
+
+using namespace Qt::StringLiterals;
+
struct MyStruct
{
int i;
@@ -215,8 +236,7 @@ namespace MyNamespace {
int m_value2 = 0;
int m_value3 = 0;
};
-}
-
+} // namespace MyNamespace
class tst_QMetaObject : public QObject
{
@@ -265,27 +285,35 @@ public:
QList<QVariant> value4;
QVariantList value5;
+ tst_QMetaObject();
+
private slots:
void connectSlotsByName();
void invokeMetaMember();
+ void invokeMetaMemberNoMacros();
void invokePointer();
void invokeQueuedMetaMember();
+ void invokeQueuedMetaMemberNoMacro();
void invokeQueuedPointer();
void invokeBlockingQueuedMetaMember();
+ void invokeBlockingQueuedMetaMemberNoMacros();
void invokeBlockingQueuedPointer();
void invokeCustomTypes();
void invokeMetaConstructor();
+ void invokeMetaConstructorNoMacro();
void invokeTypedefTypes();
void invokeException();
void invokeQueuedAutoRegister();
+ void invokeFreeFunction();
+ void invokeBind();
void qtMetaObjectInheritance();
void normalizedSignature_data();
void normalizedSignature();
void normalizedType_data();
void normalizedType();
void customPropertyType();
- void checkScope_data();
- void checkScope();
+ void keysToValue_data();
+ void keysToValue(); // Also keyToValue()
void propertyNotify();
void propertyConstant();
void propertyFinal();
@@ -296,6 +324,7 @@ private slots:
void classInfo();
void metaMethod();
+ void metaMethodNoMacro();
void indexOfMethod_data();
void indexOfMethod();
@@ -401,6 +430,11 @@ private slots:
#define FUNCTION(x) "QMetaObject::" x ": "
+tst_QMetaObject::tst_QMetaObject()
+{
+ qRegisterMetaType<qlonglong *>();
+}
+
void tst_QMetaObject::connectSlotsByName()
{
CTestObject obj;
@@ -435,8 +469,6 @@ void tst_QMetaObject::connectSlotsByName()
QCOMPARE(obj2.invokeCount2, 1);
}
-struct MyUnregisteredType { };
-
static int countedStructObjectsCount = 0;
struct CountedStruct
{
@@ -459,6 +491,8 @@ public:
QtTestObject();
QtTestObject(const QString &s) : slotResult(s) {}
Q_INVOKABLE QtTestObject(QObject *parent);
+ Q_INVOKABLE QtTestObject(QObject *parent, int, int);
+ Q_INVOKABLE QtTestObject(QObject *parent, int);
public slots:
void sl0();
@@ -479,17 +513,24 @@ public slots:
const char *sl12();
QList<QString> sl13(QList<QString> l1);
qint64 sl14();
- void testSender();
+ qlonglong *sl15(qlonglong *);
+ MyForwardDeclaredType *sl16(MyForwardDeclaredType *);
- void testReference(QString &str);
+ void sl17(int *i) { *i = 242; }
+
+ void overloadedSlot();
+ void overloadedSlot(int, int);
+ void overloadedSlot(int);
+ void testSender();
+ void testReference(QString &str);
void testLongLong(qint64 ll1, quint64 ll2);
void moveToThread(QThread *t)
{ QObject::moveToThread(t); }
- void slotWithUnregisteredParameterType(MyUnregisteredType);
- void slotWithOneUnregisteredParameterType(QString a1, MyUnregisteredType a2);
+ void slotWithUnregisteredParameterType(const MyForwardDeclaredType &);
+ void slotWithOneUnregisteredParameterType(QString a1, const MyForwardDeclaredType &a2);
CountedStruct throwingSlot(const CountedStruct &, CountedStruct s2) {
#ifndef QT_NO_EXCEPTIONS
@@ -540,6 +581,14 @@ QtTestObject::QtTestObject(QObject *parent)
{
}
+QtTestObject::QtTestObject(QObject *parent, int, int)
+ : QObject(parent)
+{ slotResult = "ii"; }
+
+QtTestObject::QtTestObject(QObject *parent, int)
+ : QObject(parent)
+{ slotResult = "i"; }
+
void QtTestObject::sl0() { slotResult = "sl0"; };
QString QtTestObject::sl1(QString s1) { slotResult = "sl1:" + s1; return "yessir"; }
void QtTestObject::sl2(QString s1, QString s2) { slotResult = "sl2:" + s1 + s2; }
@@ -572,6 +621,27 @@ QList<QString> QtTestObject::sl13(QList<QString> l1)
{ slotResult = "sl13"; return l1; }
qint64 QtTestObject::sl14()
{ slotResult = "sl14"; return Q_INT64_C(123456789)*123456789; }
+qlonglong *QtTestObject::sl15(qlonglong *ptr)
+{ slotResult = "sl15"; return ptr; }
+MyForwardDeclaredType *QtTestObject::sl16(MyForwardDeclaredType *ptr)
+{
+ slotResult = "sl16:";
+ if (ptr) {
+ slotResult += "notnull";
+ return nullptr;
+ }
+ slotResult += "null";
+ return getForwardDeclaredPointer();
+}
+
+void QtTestObject::overloadedSlot()
+{ slotResult = "overloadedSlot"; }
+
+void QtTestObject::overloadedSlot(int x, int y)
+{ slotResult = "overloadedSlot:" + QString::number(x) + ',' + QString::number(y); }
+
+void QtTestObject::overloadedSlot(int x)
+{ slotResult = "overloadedSlot:" + QString::number(x); }
void QtTestObject::testReference(QString &str)
{ slotResult = "testReference:" + str; str = "gotcha"; }
@@ -584,10 +654,10 @@ void QtTestObject::testSender()
slotResult = QString::asprintf("%p", sender());
}
-void QtTestObject::slotWithUnregisteredParameterType(MyUnregisteredType)
+void QtTestObject::slotWithUnregisteredParameterType(const MyForwardDeclaredType &)
{ slotResult = "slotWithUnregisteredReturnType"; }
-void QtTestObject::slotWithOneUnregisteredParameterType(QString a1, MyUnregisteredType)
+void QtTestObject::slotWithOneUnregisteredParameterType(QString a1, const MyForwardDeclaredType &)
{ slotResult = "slotWithUnregisteredReturnType-" + a1; }
void QtTestObject::staticFunction0()
@@ -598,6 +668,7 @@ void QtTestObject::staticFunction0()
qint64 QtTestObject::staticFunction1()
{ staticResult = "staticFunction1"; return Q_INT64_C(123456789)*123456789; }
+// this test is duplicated below
void tst_QMetaObject::invokeMetaMember()
{
QtTestObject obj;
@@ -608,17 +679,17 @@ void tst_QMetaObject::invokeMetaMember()
// Test nullptr
char *nullCharArray = nullptr;
const char *nullConstCharArray = nullptr;
- QVERIFY(!QMetaObject::invokeMethod(nullptr, nullCharArray));
- QVERIFY(!QMetaObject::invokeMethod(nullptr, nullConstCharArray));
- QVERIFY(!QMetaObject::invokeMethod(nullptr, "sl0"));
- QVERIFY(!QMetaObject::invokeMethod(&obj, nullCharArray));
- QVERIFY(!QMetaObject::invokeMethod(&obj, nullConstCharArray));
- QVERIFY(!QMetaObject::invokeMethod(&obj, nullCharArray, Qt::AutoConnection));
- QVERIFY(!QMetaObject::invokeMethod(&obj, nullConstCharArray, Qt::AutoConnection));
+ QVERIFY(!QMetaObject::invokeMethod(nullptr, nullCharArray Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(nullptr, nullConstCharArray Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(nullptr, "sl0" Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(&obj, nullCharArray Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(&obj, nullConstCharArray Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(&obj, nullCharArray, Qt::AutoConnection Q_NO_ARG));
+ QVERIFY(!QMetaObject::invokeMethod(&obj, nullConstCharArray, Qt::AutoConnection Q_NO_ARG));
QVERIFY(!QMetaObject::invokeMethod(&obj, nullCharArray, Qt::AutoConnection, QGenericReturnArgument()));
QVERIFY(!QMetaObject::invokeMethod(&obj, nullConstCharArray, Qt::AutoConnection, QGenericReturnArgument()));
- QVERIFY(QMetaObject::invokeMethod(&obj, "sl0"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl0" Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("sl0"));
QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", Q_ARG(QString, t1)));
@@ -657,16 +728,23 @@ void tst_QMetaObject::invokeMetaMember()
Q_ARG(QString, t7), Q_ARG(QString, t8), Q_ARG(QString, t9)));
QCOMPARE(obj.slotResult, QString("sl9:123456789"));
- QVERIFY(QMetaObject::invokeMethod(&obj, "sl11"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11" Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("sl11"));
- QVERIFY(QMetaObject::invokeMethod(&obj, "testSender"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testSender" Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("0x0"));
QString refStr("whatever");
+#ifdef USE_COMPAT_Q_ARG
QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", QGenericArgument("QString&", &refStr)));
QCOMPARE(obj.slotResult, QString("testReference:whatever"));
QCOMPARE(refStr, QString("gotcha"));
+ obj.slotResult.clear();
+#endif
+ refStr = "whatever";
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", Q_ARG(QString&, refStr)));
+ QCOMPARE(obj.slotResult, QString("testReference:whatever"));
+ QCOMPARE(refStr, QString("gotcha"));
qint64 ll1 = -1;
quint64 ll2 = 0;
@@ -717,8 +795,37 @@ void tst_QMetaObject::invokeMetaMember()
QCOMPARE(return64, Q_INT64_C(123456789)*123456789);
QCOMPARE(obj.slotResult, QString("sl14"));
+ // pointers
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Q_ARG(qlonglong*, &return64)));
+ QCOMPARE(obj.slotResult, QString("sl15"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Q_ARG(MyForwardDeclaredType*, getForwardDeclaredPointer())));
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+ obj.slotResult.clear();
+ qint64 *return64Ptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Q_RETURN_ARG(qlonglong*, return64Ptr), Q_ARG(qlonglong*, &return64)));
+ QCOMPARE(return64Ptr, &return64);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ obj.slotResult.clear();
+ MyForwardDeclaredType *forwardPtr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Q_RETURN_ARG(MyForwardDeclaredType*, forwardPtr),
+ Q_ARG(MyForwardDeclaredType*, nullptr)));
+ QCOMPARE(forwardPtr, getForwardDeclaredPointer());
+ QCOMPARE(obj.slotResult, QString("sl16:null"));
+
+#ifndef QT_NO_DATA_RELOCATION // this doesn't work with the new API on Windows
+#endif
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot" Q_NO_ARG));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Q_ARG(int, 1)));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Q_ARG(int, 1), Q_ARG(int, 42)));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
//test signals
- QVERIFY(QMetaObject::invokeMethod(&obj, "sig0"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0" Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("sl0"));
QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", Q_ARG(QString, "baba")));
@@ -739,6 +846,175 @@ void tst_QMetaObject::invokeMetaMember()
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString,QString,QString)\n"
"Candidates are:\n sl1(QString)");
QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1", Q_ARG(QString, "arg"), Q_ARG(QString, "arg"), Q_ARG(QString, "arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Q_ARG(QString, exp)));
+
+ //should not have changed since last test.
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:hehe"));
+}
+
+// this is a copy-paste-adapt of the above
+void tst_QMetaObject::invokeMetaMemberNoMacros()
+{
+ QtTestObject obj;
+
+ QString t1("1"); QString t2("2"); QString t3("3"); QString t4("4"); QString t5("5");
+ QString t6("6"); QString t7("7"); QString t8("8"); QString t9("9"); QString t10("X");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl0"));
+ QCOMPARE(obj.slotResult, QString("sl0"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", t1));
+ QCOMPARE(obj.slotResult, QString("sl1:1"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl2", std::as_const(t1), t2));
+ QCOMPARE(obj.slotResult, QString("sl2:12"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl3", t1, t2, t3));
+ QCOMPARE(obj.slotResult, QString("sl3:123"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl4", t1, t2, t3,
+ t4));
+ QCOMPARE(obj.slotResult, QString("sl4:1234"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl5", t1, t2, t3,
+ t4, QStringLiteral("5")));
+ QCOMPARE(obj.slotResult, QString("sl5:12345"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl6", t1, t2, t3,
+ t4, t5, t6));
+ QCOMPARE(obj.slotResult, QString("sl6:123456"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl7", t1, t2, t3,
+ t4, t5, t6,
+ t7));
+ QCOMPARE(obj.slotResult, QString("sl7:1234567"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl8", t1, t2, t3,
+ t4, t5, t6,
+ t7, t8));
+ QCOMPARE(obj.slotResult, QString("sl8:12345678"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl9", t1, t2, t3,
+ t4, t5, t6,
+ t7, t8, t9));
+ QCOMPARE(obj.slotResult, QString("sl9:123456789"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11"));
+ QCOMPARE(obj.slotResult, QString("sl11"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testSender"));
+ QCOMPARE(obj.slotResult, QString("0x0"));
+
+ // this is not working for now
+// QString refStr("whatever");
+// QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", refStr));
+// QCOMPARE(obj.slotResult, QString("testReference:whatever"));
+// QCOMPARE(refStr, QString("gotcha"));
+
+ qint64 ll1 = -1;
+ quint64 ll2 = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj,
+ "testLongLong",
+ ll1,
+ ll2));
+ QCOMPARE(obj.slotResult, QString("testLongLong:-1,0"));
+
+ QString exp;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", qReturnArg(exp), QStringLiteral("bubu")));
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:bubu"));
+
+ QObject *ptr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", qReturnArg(ptr)));
+ QCOMPARE(ptr, (QObject *)&obj);
+ QCOMPARE(obj.slotResult, QString("sl11"));
+ // try again with a space:
+ ptr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", qReturnArg(ptr)));
+ QCOMPARE(ptr, (QObject *)&obj);
+ QCOMPARE(obj.slotResult, QString("sl11"));
+
+ const char *ptr2 = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl12", qReturnArg(ptr2)));
+ QVERIFY(ptr2 != nullptr);
+ QCOMPARE(obj.slotResult, QString("sl12"));
+ // try again with a space:
+ ptr2 = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl12", qReturnArg(ptr2)));
+ QVERIFY(ptr2 != nullptr);
+ QCOMPARE(obj.slotResult, QString("sl12"));
+
+ // test w/ template args
+ QList<QString> returnValue, argument;
+ argument << QString("one") << QString("two") << QString("three");
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl13",
+ qReturnArg(returnValue),
+ argument));
+ QCOMPARE(returnValue, argument);
+ QCOMPARE(obj.slotResult, QString("sl13"));
+
+ // return qint64
+ qint64 return64;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl14",
+ qReturnArg(return64)));
+ QCOMPARE(return64, Q_INT64_C(123456789)*123456789);
+ QCOMPARE(obj.slotResult, QString("sl14"));
+
+ // pointers
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", &return64));
+ QCOMPARE(obj.slotResult, QString("sl15"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", getForwardDeclaredPointer()));
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+ obj.slotResult.clear();
+ qint64 *return64Ptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", qReturnArg(return64Ptr), &return64));
+ QCOMPARE(return64Ptr, &return64);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ obj.slotResult.clear();
+ MyForwardDeclaredType *forwardPtr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", qReturnArg(forwardPtr),
+ forwardPtr));
+ QCOMPARE(forwardPtr, getForwardDeclaredPointer());
+ QCOMPARE(obj.slotResult, QString("sl16:null"));
+
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot"));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", 1));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", 1, 42));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
+ //test signals
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0"));
+ QCOMPARE(obj.slotResult, QString("sl0"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", QStringLiteral("baba")));
+ QCOMPARE(obj.slotResult, QString("sl1:baba"));
+
+ exp.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", qReturnArg(exp), QStringLiteral("hehe")));
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:hehe"));
+
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::doesNotExist()");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "doesNotExist"));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString)(QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1(QString)", QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl3(QString)\n"
+ "Candidates are:\n sl3(QString,QString,QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl3", QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString,QString,QString)\n"
+ "Candidates are:\n sl1(QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1", QStringLiteral("arg"), QStringLiteral("arg"), QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", exp));
//should not have changed since last test.
QCOMPARE(exp, QString("yessir"));
@@ -809,13 +1085,123 @@ void tst_QMetaObject::invokePointer()
QCOMPARE(obj.slotResult, QString("sl1:1"));
}
QCOMPARE(countedStructObjectsCount, 0);
+
+ // Invoking with parameters
+ QString result;
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, qReturnArg(result), u"bubu"_s));
+ QCOMPARE(obj.slotResult, u"sl1:bubu");
+ QCOMPARE(result, u"yessir");
+
+ // without taking return value
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, u"bubu"_s));
+ QCOMPARE(obj.slotResult, u"sl1:bubu");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, Qt::DirectConnection, qReturnArg(result),
+ u"byebye"_s));
+ QCOMPARE(obj.slotResult, u"sl1:byebye");
+ QCOMPARE(result, u"yessir");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, qOverload<int, int>(&QtTestObject::overloadedSlot), 1, 2));
+ QCOMPARE(obj.slotResult, u"overloadedSlot:1,2");
+
+ // non-const ref parameter
+ QString original = u"bubu"_s;
+ QString &ref = original;
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, qReturnArg(result), ref));
+ QCOMPARE(obj.slotResult, u"sl1:bubu");
+ QCOMPARE(result, u"yessir");
+
+ struct R {
+ bool operator()(int) { return true; }
+ int operator()(char) { return 15; }
+ int operator()(QString = {}, int = {}, int = {}) { return 242; }
+ } r;
+
+ // Test figuring out which operator() to call:
+ {
+ bool res = false;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res), 1));
+ QCOMPARE(res, true);
+ }
+ {
+ int res;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res), 'c'));
+ QCOMPARE(res, 15);
+ }
+ {
+ int res;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res)));
+ QCOMPARE(res, 242);
+ res = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res), u"bu"_s));
+ QCOMPARE(res, 242);
+ res = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res), u"bu"_s, 1));
+ QCOMPARE(res, 242);
+ res = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, r, qReturnArg(res), u"bu"_s, 1, 2));
+ QCOMPARE(res, 242);
+ }
+
+ {
+ auto lambda = [](const QString &s) { return s + s; };
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, qReturnArg(result), u"bu"_s));
+ QCOMPARE(result, u"bubu");
+ }
+
+ {
+ auto lambda = [](const QString &s = u"bu"_s) { return s + s; };
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, qReturnArg(result)));
+ QCOMPARE(result, u"bubu");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, qReturnArg(result), u"bye"_s));
+ QCOMPARE(result, u"byebye");
+ }
+
+ {
+ auto lambda = [](const QString &s, qint64 a, qint16 b, qint8 c) {
+ return s + QString::number(a) + QString::number(b) + QString::number(c);
+ };
+ // Testing mismatching argument (int for qint64). The other arguments
+ // would static_assert for potential truncation if they were ints.
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, qReturnArg(result), u"bu"_s, 1, qint16(2), qint8(3)));
+ QCOMPARE(result, u"bu123");
+ }
+ {
+ // Testing deduction
+ auto lambda = [](const QString &s, auto a) { return s + a; };
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, qReturnArg(result), u"bu"_s, "bu"_L1));
+ QCOMPARE(result, u"bubu");
+
+ auto variadic = [](const QString &s, auto... a) { return s + (QString::number(a) + ...); };
+ QVERIFY(QMetaObject::invokeMethod(&obj, variadic, qReturnArg(result), u"bu"_s, 1, 2, 3, 4, 5, 6));
+ QCOMPARE(result, u"bu123456");
+ }
+ {
+ // Testing a functor returning void and accepting a pointer,
+ // this may trigger the pointer to be interpreted as the old void*
+ // return parameter.
+ bool invoked = false;
+ auto lambda = [&invoked](void *ptr) -> void {
+ Q_UNUSED(ptr);
+ invoked = true;
+ };
+ int i = 242;
+ QVERIFY(QMetaObject::invokeMethod(&obj, lambda, &i));
+ QVERIFY(invoked);
+
+ // member fn
+ i = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl17, &i));
+ QCOMPARE(i, 242);
+ }
}
void tst_QMetaObject::invokeQueuedMetaMember()
{
QtTestObject obj;
- QVERIFY(QMetaObject::invokeMethod(&obj, "sl0", Qt::QueuedConnection));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl0", Qt::QueuedConnection Q_NO_ARG));
QVERIFY(obj.slotResult.isEmpty());
qApp->processEvents(QEventLoop::AllEvents);
QCOMPARE(obj.slotResult, QString("sl0"));
@@ -835,10 +1221,36 @@ void tst_QMetaObject::invokeQueuedMetaMember()
qApp->processEvents(QEventLoop::AllEvents);
QCOMPARE(obj.slotResult, QString("sl9:123456789"));
+ // pointers
+ qint64 return64;
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::QueuedConnection, Q_ARG(qlonglong*, &return64)));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ // since Qt 6.5, this works even for pointers to forward-declared types
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::QueuedConnection, Q_ARG(MyForwardDeclaredType*, getForwardDeclaredPointer())));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+#ifndef QT_NO_DATA_RELOCATION // this doesn't work with the new API on Windows
+#endif
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection Q_NO_ARG));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection, Q_ARG(int, 1)));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection, Q_ARG(int, 1), Q_ARG(int, 42)));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
// signals
obj.slotResult.clear();
- QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::QueuedConnection));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::QueuedConnection Q_NO_ARG));
QVERIFY(obj.slotResult.isEmpty());
qApp->processEvents(QEventLoop::AllEvents);
QCOMPARE(obj.slotResult, QString("sl0"));
@@ -862,23 +1274,142 @@ void tst_QMetaObject::invokeQueuedMetaMember()
qApp->processEvents(QEventLoop::AllEvents);
QCOMPARE(obj.slotResult, QString("testLongLong:-1,0"));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Qt::QueuedConnection, Q_ARG(QString, exp)));
+
+ QString refStr = "whatever";
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'QString&'");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Qt::QueuedConnection, Q_ARG(QString&, refStr)));
+ QCOMPARE(refStr, "whatever");
+
+#ifdef USE_COMPAT_Q_ARG // this doesn't compile with the new API
obj.slotResult.clear();
{
- MyUnregisteredType t;
- QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyUnregisteredType'");
- QVERIFY(!QMetaObject::invokeMethod(&obj, "slotWithUnregisteredParameterType", Qt::QueuedConnection, Q_ARG(MyUnregisteredType, t)));
+ const MyForwardDeclaredType &t = getForwardDeclaredType();
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyForwardDeclaredType'");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "slotWithUnregisteredParameterType", Qt::QueuedConnection, Q_ARG(MyForwardDeclaredType, t)));
QVERIFY(obj.slotResult.isEmpty());
}
obj.slotResult.clear();
{
QString a1("Cannot happen");
- MyUnregisteredType t;
- QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyUnregisteredType'");
+ const MyForwardDeclaredType &t = getForwardDeclaredType();
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyForwardDeclaredType'");
QVERIFY(!QMetaObject::invokeMethod(&obj, "slotWithOneUnregisteredParameterType", Qt::QueuedConnection,
- Q_ARG(QString, a1), Q_ARG(MyUnregisteredType, t)));
+ Q_ARG(QString, a1), Q_ARG(MyForwardDeclaredType, t)));
QVERIFY(obj.slotResult.isEmpty());
}
+#endif
+}
+
+// this is a copy-paste-adapt of the above
+void tst_QMetaObject::invokeQueuedMetaMemberNoMacro()
+{
+ QtTestObject obj;
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl0", Qt::QueuedConnection));
+ QVERIFY(obj.slotResult.isEmpty());
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl0"));
+ obj.slotResult = QString();
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", Qt::QueuedConnection, QString("hallo")));
+ QVERIFY(obj.slotResult.isEmpty());
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl1:hallo"));
+ obj.slotResult = QString();
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl9", Qt::QueuedConnection, QStringLiteral("1"), QStringLiteral("2"),
+ QStringLiteral("3"), QStringLiteral("4"), QStringLiteral("5"),
+ QStringLiteral("6"), QStringLiteral("7"), QStringLiteral("8"),
+ QStringLiteral("9")));
+ QVERIFY(obj.slotResult.isEmpty());
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl9:123456789"));
+
+ // pointers
+ qint64 return64;
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::QueuedConnection, &return64));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::QueuedConnection, getForwardDeclaredPointer()));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection, 1));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::QueuedConnection, 1, 42));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
+ // signals
+
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::QueuedConnection));
+ QVERIFY(obj.slotResult.isEmpty());
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl0"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", Qt::QueuedConnection, QStringLiteral("gogo")));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("sl1:gogo"));
+
+ QString exp;
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to invoke methods with return values in queued connections");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sig1", Qt::QueuedConnection, qReturnArg(exp),
+ QStringLiteral("nono")));
+
+ qint64 ll1 = -1;
+ quint64 ll2 = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj,
+ "testLongLong",
+ Qt::QueuedConnection,
+ ll1,
+ ll2));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, QString("testLongLong:-1,0"));
+
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", exp));
+ QCOMPARE(obj.slotResult, QString("testLongLong:-1,0"));
+ QVERIFY(exp.isEmpty());
+
+ // this doesn't work yet
+// QString refStr = "whatever";
+// QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'QString&'");
+// QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Qt::QueuedConnection, Q_ARG(QString&, refStr)));
+// QCOMPARE(refStr, "whatever");
+
+#if 0 // this won't even compile any more
+ obj.slotResult.clear();
+ {
+ const MyForwardDeclaredType &t = getForwardDeclaredType();
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyForwardDeclaredType'");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "slotWithUnregisteredParameterType", Qt::QueuedConnection, t));
+ QVERIFY(obj.slotResult.isEmpty());
+ }
+
+ obj.slotResult.clear();
+ {
+ QString a1("Cannot happen");
+ const MyForwardDeclaredType &t = getForwardDeclaredType();
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: Unable to handle unregistered datatype 'MyForwardDeclaredType'");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "slotWithOneUnregisteredParameterType", Qt::QueuedConnection,
+ a1, t));
+ QVERIFY(obj.slotResult.isEmpty());
+ }
+#endif
}
void tst_QMetaObject::invokeQueuedPointer()
@@ -928,9 +1459,15 @@ void tst_QMetaObject::invokeQueuedPointer()
QCOMPARE(var, 0);
}
QCOMPARE(countedStructObjectsCount, 0);
-}
+ // Invoking with parameters
+ using namespace Qt::StringLiterals;
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, Qt::QueuedConnection, u"bubu"_s));
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult, u"sl1:bubu");
+}
+// this test is duplicated below
void tst_QMetaObject::invokeBlockingQueuedMetaMember()
{
QThread t;
@@ -977,16 +1514,23 @@ void tst_QMetaObject::invokeBlockingQueuedMetaMember()
Q_ARG(QString, t7), Q_ARG(QString, t8), Q_ARG(QString, t9)));
QCOMPARE(obj.slotResult, QString("sl9:123456789"));
- QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", Qt::BlockingQueuedConnection));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", Qt::BlockingQueuedConnection Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("sl11"));
- QVERIFY(QMetaObject::invokeMethod(&obj, "testSender", Qt::BlockingQueuedConnection));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testSender", Qt::BlockingQueuedConnection Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("0x0"));
QString refStr("whatever");
+#ifdef USE_COMPAT_Q_ARG
QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", Qt::BlockingQueuedConnection, QGenericArgument("QString&", &refStr)));
QCOMPARE(obj.slotResult, QString("testReference:whatever"));
QCOMPARE(refStr, QString("gotcha"));
+ obj.slotResult.clear();
+#endif
+ refStr = "whatever";
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", Qt::BlockingQueuedConnection, Q_ARG(QString&, refStr)));
+ QCOMPARE(obj.slotResult, QString("testReference:whatever"));
+ QCOMPARE(refStr, QString("gotcha"));
qint64 ll1 = -1;
quint64 ll2 = 0;
@@ -1031,8 +1575,44 @@ void tst_QMetaObject::invokeBlockingQueuedMetaMember()
QCOMPARE(returnValue, argument);
QCOMPARE(obj.slotResult, QString("sl13"));
+ // return qint64
+ qint64 return64;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl14", Qt::BlockingQueuedConnection,
+ Q_RETURN_ARG(qint64, return64)));
+ QCOMPARE(return64, Q_INT64_C(123456789)*123456789);
+ QCOMPARE(obj.slotResult, QString("sl14"));
+
+ // pointers
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::BlockingQueuedConnection, Q_ARG(qlonglong*, &return64)));
+ QCOMPARE(obj.slotResult, QString("sl15"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::BlockingQueuedConnection, Q_ARG(MyForwardDeclaredType*, getForwardDeclaredPointer())));
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+ obj.slotResult.clear();
+ qint64 *return64Ptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::BlockingQueuedConnection, Q_RETURN_ARG(qlonglong*, return64Ptr), Q_ARG(qlonglong*, &return64)));
+ QCOMPARE(return64Ptr, &return64);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ obj.slotResult.clear();
+ MyForwardDeclaredType *forwardPtr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::BlockingQueuedConnection, Q_RETURN_ARG(MyForwardDeclaredType*, forwardPtr),
+ Q_ARG(MyForwardDeclaredType*, nullptr)));
+ QCOMPARE(forwardPtr, getForwardDeclaredPointer());
+ QCOMPARE(obj.slotResult, QString("sl16:null"));
+
+#ifndef QT_NO_DATA_RELOCATION // this doesn't work with the new API on Windows
+#endif
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection Q_NO_ARG));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection, Q_ARG(int, 1)));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection, Q_ARG(int, 1), Q_ARG(int, 42)));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
//test signals
- QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::BlockingQueuedConnection));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::BlockingQueuedConnection Q_NO_ARG));
QCOMPARE(obj.slotResult, QString("sl0"));
QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", Qt::BlockingQueuedConnection, Q_ARG(QString, "baba")));
@@ -1044,7 +1624,7 @@ void tst_QMetaObject::invokeBlockingQueuedMetaMember()
QCOMPARE(obj.slotResult, QString("sl1:hehe"));
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::doesNotExist()");
- QVERIFY(!QMetaObject::invokeMethod(&obj, "doesNotExist", Qt::BlockingQueuedConnection));
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "doesNotExist", Qt::BlockingQueuedConnection Q_NO_ARG));
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString)(QString)");
QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1(QString)", Qt::BlockingQueuedConnection, Q_ARG(QString, "arg")));
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl3(QString)\n"
@@ -1053,6 +1633,9 @@ void tst_QMetaObject::invokeBlockingQueuedMetaMember()
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString,QString,QString)\n"
"Candidates are:\n sl1(QString)");
QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1", Qt::BlockingQueuedConnection, Q_ARG(QString, "arg"), Q_ARG(QString, "arg"), Q_ARG(QString, "arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Qt::BlockingQueuedConnection, Q_ARG(QString, exp)));
//should not have changed since last test.
QCOMPARE(exp, QString("yessir"));
@@ -1061,7 +1644,177 @@ void tst_QMetaObject::invokeBlockingQueuedMetaMember()
QVERIFY(QMetaObject::invokeMethod(&obj, "moveToThread", Qt::BlockingQueuedConnection, Q_ARG(QThread*, QThread::currentThread())));
t.quit();
QVERIFY(t.wait());
+}
+
+// this is a copy-paste-adapt of the above
+void tst_QMetaObject::invokeBlockingQueuedMetaMemberNoMacros()
+{
+ QThread t;
+ t.start();
+ QtTestObject obj;
+ obj.moveToThread(&t);
+ QString t1("1"); QString t2("2"); QString t3("3"); QString t4("4"); QString t5("5");
+ QString t6("6"); QString t7("7"); QString t8("8"); QString t9("9"); QString t10("X");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", Qt::BlockingQueuedConnection, t1));
+ QCOMPARE(obj.slotResult, QString("sl1:1"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl2", Qt::BlockingQueuedConnection, t1, t2));
+ QCOMPARE(obj.slotResult, QString("sl2:12"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl3", Qt::BlockingQueuedConnection, t1, t2, t3));
+ QCOMPARE(obj.slotResult, QString("sl3:123"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl4", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4));
+ QCOMPARE(obj.slotResult, QString("sl4:1234"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl5", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4, QStringLiteral("5")));
+ QCOMPARE(obj.slotResult, QString("sl5:12345"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl6", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4, t5, t6));
+ QCOMPARE(obj.slotResult, QString("sl6:123456"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl7", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4, t5, t6,
+ t7));
+ QCOMPARE(obj.slotResult, QString("sl7:1234567"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl8", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4, t5, t6,
+ t7, t8));
+ QCOMPARE(obj.slotResult, QString("sl8:12345678"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl9", Qt::BlockingQueuedConnection, t1, t2,
+ t3, t4, t5, t6,
+ t7, t8, t9));
+ QCOMPARE(obj.slotResult, QString("sl9:123456789"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", Qt::BlockingQueuedConnection));
+ QCOMPARE(obj.slotResult, QString("sl11"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "testSender", Qt::BlockingQueuedConnection));
+ QCOMPARE(obj.slotResult, QString("0x0"));
+
+ // this is not working
+// QString refStr("whatever");
+// QVERIFY(QMetaObject::invokeMethod(&obj, "testReference", Qt::BlockingQueuedConnection, refStr));
+// QCOMPARE(obj.slotResult, QString("testReference:whatever"));
+// QCOMPARE(refStr, QString("gotcha"));
+
+ qint64 ll1 = -1;
+ quint64 ll2 = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj,
+ "testLongLong",
+ Qt::BlockingQueuedConnection,
+ ll1,
+ ll2));
+ QCOMPARE(obj.slotResult, QString("testLongLong:-1,0"));
+
+ QString exp;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", Qt::BlockingQueuedConnection, qReturnArg(exp), QStringLiteral("bubu")));
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:bubu"));
+
+ QObject *ptr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", Qt::BlockingQueuedConnection, qReturnArg(ptr)));
+ QCOMPARE(ptr, (QObject *)&obj);
+ QCOMPARE(obj.slotResult, QString("sl11"));
+ // try again with a space:
+ ptr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl11", Qt::BlockingQueuedConnection, qReturnArg(ptr)));
+ QCOMPARE(ptr, (QObject *)&obj);
+ QCOMPARE(obj.slotResult, QString("sl11"));
+
+ const char *ptr2 = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl12", Qt::BlockingQueuedConnection, qReturnArg(ptr2)));
+ QVERIFY(ptr2 != 0);
+ QCOMPARE(obj.slotResult, QString("sl12"));
+ // try again with a space:
+ ptr2 = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl12", Qt::BlockingQueuedConnection, qReturnArg(ptr2)));
+ QVERIFY(ptr2 != 0);
+ QCOMPARE(obj.slotResult, QString("sl12"));
+
+ // test w/ template args
+ QList<QString> returnValue, argument;
+ argument << QString("one") << QString("two") << QString("three");
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl13", Qt::BlockingQueuedConnection,
+ qReturnArg(returnValue),
+ argument));
+ QCOMPARE(returnValue, argument);
+ QCOMPARE(obj.slotResult, QString("sl13"));
+
+ // return qint64
+ qint64 return64;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl14", Qt::BlockingQueuedConnection,
+ qReturnArg(return64)));
+ QCOMPARE(return64, Q_INT64_C(123456789)*123456789);
+ QCOMPARE(obj.slotResult, QString("sl14"));
+
+ // pointers
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::BlockingQueuedConnection, &return64));
+ QCOMPARE(obj.slotResult, QString("sl15"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::BlockingQueuedConnection, getForwardDeclaredPointer()));
+ QCOMPARE(obj.slotResult, QString("sl16:notnull"));
+
+ obj.slotResult.clear();
+ qint64 *return64Ptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl15", Qt::BlockingQueuedConnection, qReturnArg(return64Ptr), &return64));
+ QCOMPARE(return64Ptr, &return64);
+ QCOMPARE(obj.slotResult, QString("sl15"));
+
+ obj.slotResult.clear();
+ MyForwardDeclaredType *forwardPtr = nullptr;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl16", Qt::BlockingQueuedConnection, qReturnArg(forwardPtr),
+ forwardPtr));
+ QCOMPARE(forwardPtr, getForwardDeclaredPointer());
+ QCOMPARE(obj.slotResult, QString("sl16:null"));
+
+ // test overloads
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection, 1));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1"));
+ QVERIFY(QMetaObject::invokeMethod(&obj, "overloadedSlot", Qt::BlockingQueuedConnection, 1, 42));
+ QCOMPARE(obj.slotResult, QString("overloadedSlot:1,42"));
+
+ //test signals
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig0", Qt::BlockingQueuedConnection));
+ QCOMPARE(obj.slotResult, QString("sl0"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", Qt::BlockingQueuedConnection, QStringLiteral("baba")));
+ QCOMPARE(obj.slotResult, QString("sl1:baba"));
+
+ exp.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig1", Qt::BlockingQueuedConnection, qReturnArg(exp), QStringLiteral("hehe")));
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:hehe"));
+
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::doesNotExist()");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "doesNotExist", Qt::BlockingQueuedConnection));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString)(QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1(QString)", Qt::BlockingQueuedConnection, QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl3(QString)\n"
+ "Candidates are:\n sl3(QString,QString,QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl3", Qt::BlockingQueuedConnection, QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::sl1(QString,QString,QString)\n"
+ "Candidates are:\n sl1(QString)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "sl1", Qt::BlockingQueuedConnection, QStringLiteral("arg"), QStringLiteral("arg"), QStringLiteral("arg")));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::invokeMethod: No such method QtTestObject::testReference(QString)\n"
+ "Candidates are:\n testReference(QString&)");
+ QVERIFY(!QMetaObject::invokeMethod(&obj, "testReference", Qt::BlockingQueuedConnection, exp));
+
+ //should not have changed since last test.
+ QCOMPARE(exp, QString("yessir"));
+ QCOMPARE(obj.slotResult, QString("sl1:hehe"));
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, "moveToThread", Qt::BlockingQueuedConnection, QThread::currentThread()));
+ t.quit();
+ QVERIFY(t.wait());
}
void tst_QMetaObject::invokeBlockingQueuedPointer()
@@ -1131,6 +1884,18 @@ void tst_QMetaObject::invokeBlockingQueuedPointer()
Qt::BlockingQueuedConnection));
QCOMPARE(obj.slotResult, QString("sl1:hehe"));
}
+
+ // Test with parameters
+ QString result;
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl1, Qt::BlockingQueuedConnection,
+ qReturnArg(result), u"bubu"_s));
+ QCOMPARE(result, u"yessir");
+ QCOMPARE(obj.slotResult, u"sl1:bubu");
+
+ QVERIFY(QMetaObject::invokeMethod(&obj, &QtTestObject::sl2, Qt::BlockingQueuedConnection,
+ u"bubu"_s, u"baba"_s));
+ QCOMPARE(obj.slotResult, u"sl2:bububaba");
+
QVERIFY(QMetaObject::invokeMethod(&obj, [&](){obj.moveToThread(QThread::currentThread());}, Qt::BlockingQueuedConnection));
t.quit();
QVERIFY(t.wait());
@@ -1187,6 +1952,10 @@ void tst_QMetaObject::invokeCustomTypes()
QCOMPARE(obj.sum, 0);
QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", Q_ARG(MyType, tp)));
QCOMPARE(obj.sum, 3);
+
+ obj.sum = 0;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sl1", tp));
+ QCOMPARE(obj.sum, 3);
}
namespace NamespaceWithConstructibleClass
@@ -1202,13 +1971,16 @@ public:
}
+// this test is duplicated below
void tst_QMetaObject::invokeMetaConstructor()
{
const QMetaObject *mo = &QtTestObject::staticMetaObject;
+#ifdef USE_COMPAT_Q_ARG
{
- QObject *obj = mo->newInstance();
+ QObject *obj = mo->newInstance(QGenericArgument());
QVERIFY(!obj);
}
+#endif
{
QtTestObject obj;
QObject *obj2 = mo->newInstance(Q_ARG(QObject*, &obj));
@@ -1231,6 +2003,56 @@ void tst_QMetaObject::invokeMetaConstructor()
QTest::ignoreMessage(QtWarningMsg, "QMetaObject::newInstance: type MyGadget does not inherit QObject");
QVERIFY(!MyGadget::staticMetaObject.newInstance());
}
+
+ // overloaded constructors
+ QObject parent;
+ {
+ QObject *obj = mo->newInstance(Q_ARG(QObject*, &parent));
+ QVERIFY(obj);
+ QCOMPARE(static_cast<QtTestObject*>(obj)->slotResult, "");
+ }
+ {
+ QObject *obj = mo->newInstance(Q_ARG(QObject*, &parent), Q_ARG(int, 1));
+ QVERIFY(obj);
+ QCOMPARE(static_cast<QtTestObject*>(obj)->slotResult, "i");
+ }
+ {
+ QObject *obj = mo->newInstance(Q_ARG(QObject*, &parent), Q_ARG(int, 1), Q_ARG(int, 42));
+ QVERIFY(obj);
+ QCOMPARE(static_cast<QtTestObject*>(obj)->slotResult, "ii");
+ }
+}
+
+// this is a copy-paste-adapt of the above
+void tst_QMetaObject::invokeMetaConstructorNoMacro()
+{
+ const QMetaObject *mo = &QtTestObject::staticMetaObject;
+ {
+ QObject *obj = mo->newInstance();
+ QVERIFY(!obj);
+ }
+ {
+ QtTestObject obj;
+ QObject *obj2 = mo->newInstance(static_cast<QObject *>(&obj));
+ QVERIFY(obj2 != 0);
+ QCOMPARE(obj2->parent(), (QObject*)&obj);
+ QVERIFY(qobject_cast<QtTestObject*>(obj2) != 0);
+ }
+ // class in namespace
+ const QMetaObject *nsmo = &NamespaceWithConstructibleClass::ConstructibleClass::staticMetaObject;
+ {
+ QtTestObject obj;
+ QObject *obj2 = nsmo->newInstance(static_cast<QObject *>(&obj));
+ QVERIFY(obj2 != 0);
+ QCOMPARE(obj2->parent(), (QObject*)&obj);
+ QVERIFY(qobject_cast<NamespaceWithConstructibleClass::ConstructibleClass*>(obj2) != 0);
+ }
+ // gadget shouldn't return a valid pointer
+ {
+ QCOMPARE(MyGadget::staticMetaObject.constructorCount(), 1);
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::newInstance: type MyGadget does not inherit QObject");
+ QVERIFY(!MyGadget::staticMetaObject.newInstance());
+ }
}
void tst_QMetaObject::invokeTypedefTypes()
@@ -1240,11 +2062,17 @@ void tst_QMetaObject::invokeTypedefTypes()
QSignalSpy spy(&obj, &QtTestCustomObject::sig_custom);
QVERIFY(spy.isValid());
- QCOMPARE(spy.count(), 0);
+ QCOMPARE(spy.size(), 0);
CustomString arg("hello");
QVERIFY(QMetaObject::invokeMethod(&obj, "sig_custom", Q_ARG(CustomString, arg)));
- QCOMPARE(spy.count(), 1);
- QCOMPARE(spy.at(0).count(), 1);
+ QCOMPARE(spy.size(), 1);
+ QCOMPARE(spy.at(0).size(), 1);
+ QCOMPARE(spy.at(0).at(0), QVariant(arg));
+
+ spy.clear();
+ QVERIFY(QMetaObject::invokeMethod(&obj, "sig_custom", arg));
+ QCOMPARE(spy.size(), 1);
+ QCOMPARE(spy.at(0).size(), 1);
QCOMPARE(spy.at(0).at(0), QVariant(arg));
}
@@ -1260,6 +2088,14 @@ void tst_QMetaObject::invokeException()
QFAIL("Did not throw");
} catch(ObjectException &) {}
QCOMPARE(countedStructObjectsCount, 0);
+
+ try {
+ CountedStruct s;
+ QVERIFY(QMetaObject::invokeMethod(&obj, "throwingSlot", qReturnArg(s),
+ s, s));
+ QFAIL("Did not throw");
+ } catch(ObjectException &) {}
+ QCOMPARE(countedStructObjectsCount, 0);
#else
QSKIP("Needs exceptions");
#endif
@@ -1281,6 +2117,61 @@ void tst_QMetaObject::invokeQueuedAutoRegister()
qApp->processEvents(QEventLoop::AllEvents);
QCOMPARE(obj.slotResult,
QString("slotWithRegistrableArgument:myShared-myShared-myShared-myShared-00"));
+
+ obj.slotResult.clear();
+ QVERIFY(QMetaObject::invokeMethod(
+ &obj, "slotWithRegistrableArgument", Qt::QueuedConnection,
+ shared.data(), QPointer<QtTestObject>(shared.data()),
+ QSharedPointer<QtTestObject>(shared), QWeakPointer<QtTestObject>(shared),
+ QList<QtTestObject *>(),
+ QList<QtTestObject *>()));
+ QVERIFY(obj.slotResult.isEmpty());
+ qApp->processEvents(QEventLoop::AllEvents);
+ QCOMPARE(obj.slotResult,
+ QString("slotWithRegistrableArgument:myShared-myShared-myShared-myShared-00"));
+}
+
+namespace FunctionTest {
+ static void function0() {}
+ static int functionNoExcept() noexcept
+ {
+ return 42;
+ }
+}
+
+void tst_QMetaObject::invokeFreeFunction()
+{
+ using namespace FunctionTest;
+ QtTestObject obj;
+ QMetaObject::invokeMethod(&obj, function0);
+ int retInt = -1;
+ QMetaObject::invokeMethod(&obj, functionNoExcept, &retInt);
+ QCOMPARE(retInt, functionNoExcept());
+}
+
+void tst_QMetaObject::invokeBind()
+{
+ QtTestObject obj;
+
+ struct {
+ int number;
+ QString string;
+ } results;
+
+ const auto function = [&results](int number, const QString &string) -> bool {
+ results.number = number;
+ results.string = string;
+ return true;
+ };
+
+ const int number = 42;
+ const QString string("Test");
+ const auto binding = std::bind(function, number, string);
+ bool ret = false;
+ QMetaObject::invokeMethod(&obj, binding, &ret);
+ QVERIFY(ret);
+ QCOMPARE(results.number, number);
+ QCOMPARE(results.string, string);
}
void tst_QMetaObject::normalizedSignature_data()
@@ -1448,7 +2339,7 @@ void tst_QMetaObject::customPropertyType()
QCOMPARE(prop.metaType().id(), QMetaType::QVariantList);
}
-void tst_QMetaObject::checkScope_data()
+void tst_QMetaObject::keysToValue_data()
{
QTest::addColumn<QObject *>("object");
QTest::addColumn<QByteArray>("name");
@@ -1462,7 +2353,7 @@ void tst_QMetaObject::checkScope_data()
}
-void tst_QMetaObject::checkScope()
+void tst_QMetaObject::keysToValue()
{
QFETCH(QObject *, object);
QFETCH(QByteArray, name);
@@ -1475,6 +2366,8 @@ void tst_QMetaObject::checkScope()
QVERIFY(!me.isFlag());
QCOMPARE(QByteArray(me.scope()), QByteArray("MyNamespace::" + name));
QCOMPARE(me.keyToValue("MyNamespace::" + name + "::MyEnum2", &ok), 1);
+ // Fully qualified unscoped enumerator
+ QCOMPARE(me.keyToValue("MyNamespace::" + name + "::MyEnum::MyEnum2", &ok), 1);
QCOMPARE(ok, true);
QCOMPARE(me.keyToValue(name + "::MyEnum2", &ok), -1);
QCOMPARE(ok, false);
@@ -1504,6 +2397,9 @@ void tst_QMetaObject::checkScope()
QCOMPARE(QByteArray(mf.scope()), QByteArray("MyNamespace::" + name));
QCOMPARE(mf.keysToValue("MyNamespace::" + name + "::MyFlag2", &ok), 2);
QCOMPARE(ok, true);
+ // Fully qualified
+ QCOMPARE(mf.keysToValue("MyNamespace::" + name + "::MyFlag::MyFlag2", &ok), 2);
+ QCOMPARE(ok, true);
QCOMPARE(mf.keysToValue(name + "::MyFlag2", &ok), -1);
QCOMPARE(ok, false);
QCOMPARE(mf.keysToValue("MyNamespace::MyFlag2", &ok), -1);
@@ -1513,7 +2409,12 @@ void tst_QMetaObject::checkScope()
QCOMPARE(mf.keysToValue("MyFlag", &ok), -1);
QCOMPARE(ok, false);
QCOMPARE(QLatin1String(mf.valueToKey(2)), QLatin1String("MyFlag2"));
- QCOMPARE(mf.keysToValue("MyNamespace::" + name + "::MyFlag1|MyNamespace::" + name + "::MyFlag2", &ok), 3);
+
+ const QByteArray prefix = "MyNamespace::" + name;
+ QCOMPARE(mf.keysToValue(prefix + "::MyFlag1|" + prefix + "::MyFlag2", &ok), 3);
+ QCOMPARE(ok, true);
+ // Fully qualified
+ QCOMPARE(mf.keysToValue(prefix + "::MyFlag::MyFlag1|" + prefix + "::MyFlag::MyFlag2", &ok), 3);
QCOMPARE(ok, true);
QCOMPARE(mf.keysToValue(name + "::MyFlag1|" + name + "::MyFlag2", &ok), -1);
QCOMPARE(ok, false);
@@ -1525,9 +2426,34 @@ void tst_QMetaObject::checkScope()
QCOMPARE(ok, true);
QCOMPARE(mf.keysToValue("MyFlag1|MyNamespace::" + name + "::MyFlag2", &ok), 3);
QCOMPARE(ok, true);
- QCOMPARE(mf.keysToValue("MyNamespace::" + name + "::MyFlag2|MyNamespace::" + name + "::MyFlag2", &ok), 2);
+ QCOMPARE(mf.keysToValue(prefix + "::MyFlag2|" + prefix + "::MyFlag2", &ok), 2);
+ QCOMPARE(ok, true);
+ // Fully qualified
+ QCOMPARE(mf.keysToValue(prefix + "::MyFlag::MyFlag2|" + prefix + "::MyFlag::MyFlag2", &ok), 2);
QCOMPARE(ok, true);
QCOMPARE(QLatin1String(mf.valueToKeys(3)), QLatin1String("MyFlag1|MyFlag2"));
+
+ // Test flags with extra '|'
+ QTest::ignoreMessage(QtWarningMsg,
+ QRegularExpression(u"QMetaEnum::keysToValue: malformed keys string, ends with '|'.+"_s));
+ QCOMPARE(mf.keysToValue("MyFlag1|MyFlag2|", &ok), -1);
+ QCOMPARE(ok, false);
+
+ QTest::ignoreMessage(QtWarningMsg,
+ QRegularExpression(u"QMetaEnum::keysToValue: malformed keys string, starts with '|'.+"_s));
+ QCOMPARE(mf.keysToValue("|MyFlag1|MyFlag2|", &ok), -1);
+ QCOMPARE(ok, false);
+
+ QTest::ignoreMessage(QtWarningMsg,
+ QRegularExpression(
+ u"QMetaEnum::keysToValue: malformed keys string, has two consecutive '|'.+"_s));
+ QCOMPARE(mf.keysToValue("MyFlag1||MyFlag2", &ok), -1);
+ QCOMPARE(ok, false);
+
+ // Test empty string
+ QTest::ignoreMessage(QtWarningMsg, "QMetaEnum::keysToValue: empty keys string.");
+ QCOMPARE(mf.keysToValue("", &ok), -1);
+ QCOMPARE(ok, false);
}
void tst_QMetaObject::propertyNotify()
@@ -1614,6 +2540,7 @@ void tst_QMetaObject::classInfo()
QCOMPARE(QLatin1String(b.metaObject()->classInfo(index).value()), QLatin1String("Christopher Pike"));
}
+// this test is duplicated below
void tst_QMetaObject::metaMethod()
{
QString str("foo");
@@ -1633,9 +2560,13 @@ void tst_QMetaObject::metaMethod()
QVERIFY(index > 0);
method = QtTestObject::staticMetaObject.method(index);
//wrong args
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: too few arguments (5) in call to QtTestObject::sl5(QString,QString,QString,QString,QString)");
QVERIFY(!method.invoke(&obj, Q_ARG(QString, "1"), Q_ARG(QString, "2"), Q_ARG(QString, "3"), Q_ARG(QString, "4")));
//QVERIFY(!method.invoke(&obj, Q_ARG(QString, "1"), Q_ARG(QString, "2"), Q_ARG(QString, "3"), Q_ARG(QString, "4"), Q_ARG(QString, "5"), Q_ARG(QString, "6")));
//QVERIFY(!method.invoke(&obj, Q_ARG(QString, "1"), Q_ARG(QString, "2"), Q_ARG(QString, "3"), Q_ARG(QString, "4"), Q_ARG(int, 5)));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invokeMethod: return type mismatch for method "
+ "QtTestObject::sl5(QString,QString,QString,QString,QString): "
+ "cannot convert from void to QString during invocation");
QVERIFY(!method.invoke(&obj, Q_RETURN_ARG(QString, ret), Q_ARG(QString, "1"), Q_ARG(QString, "2"), Q_ARG(QString, "3"), Q_ARG(QString, "4"), Q_ARG(QString, "5")));
//wrong object
@@ -1662,6 +2593,59 @@ void tst_QMetaObject::metaMethod()
QCOMPARE(obj.slotResult, QString("sl13"));
}
+// this is a copy-paste-adapt of the above
+void tst_QMetaObject::metaMethodNoMacro()
+{
+ QString str("foo");
+ QString ret("bar");
+ QMetaMethod method;
+ QVERIFY(!method.invoke(this));
+ QVERIFY(!method.invoke(this, str));
+ QVERIFY(!method.invoke(this, qReturnArg(ret), str));
+ QCOMPARE(str, QString("foo"));
+ QCOMPARE(ret, QString("bar"));
+
+ QtTestObject obj;
+ QString t1("1"); QString t2("2"); QString t3("3"); QString t4("4"); QString t5("5");
+ QString t6("6"); QString t7("7"); QString t8("8"); QString t9("9"); QString t10("X");
+
+ int index = QtTestObject::staticMetaObject.indexOfMethod("sl5(QString,QString,QString,QString,QString)");
+ QVERIFY(index > 0);
+ method = QtTestObject::staticMetaObject.method(index);
+ //wrong args
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invoke: too few arguments (5) in call to QtTestObject::sl5(QString,QString,QString,QString,QString)");
+ QVERIFY(!method.invoke(&obj, QStringLiteral("1"), QStringLiteral("2"), QStringLiteral("3"), QStringLiteral("4")));
+ //QVERIFY(!method.invoke(&obj, "1", "2", "3", "4", "5", "6"));
+ //QVERIFY(!method.invoke(&obj, "1", "2", "3", "4", 5));
+ QTest::ignoreMessage(QtWarningMsg, "QMetaMethod::invokeMethod: return type mismatch for method "
+ "QtTestObject::sl5(QString,QString,QString,QString,QString): "
+ "cannot convert from void to QString during invocation");
+ QVERIFY(!method.invoke(&obj, qReturnArg(ret), QStringLiteral("1"), QStringLiteral("2"), QStringLiteral("3"), QStringLiteral("4"), QStringLiteral("5")));
+
+ //wrong object
+ //QVERIFY(!method.invoke(this, "1", "2", "3", "4", "5"));
+ QVERIFY(!method.invoke(0, QStringLiteral("1"), QStringLiteral("2"), QStringLiteral("3"), QStringLiteral("4"), QStringLiteral("5")));
+ QCOMPARE(ret, QString("bar"));
+ QCOMPARE(obj.slotResult, QString());
+
+ QVERIFY(method.invoke(&obj, QStringLiteral("1"), QStringLiteral("2"), QStringLiteral("3"), QStringLiteral("4"), QStringLiteral("5")));
+ QCOMPARE(obj.slotResult, QString("sl5:12345"));
+
+ index = QtTestObject::staticMetaObject.indexOfMethod("sl13(QList<QString>)");
+ QVERIFY(index > 0);
+ QMetaMethod sl13 = QtTestObject::staticMetaObject.method(index);
+ QList<QString> returnValue, argument;
+ argument << QString("one") << QString("two") << QString("three");
+ //wrong object
+ //QVERIFY(!sl13.invoke(this, qReturnArg(returnValue), argument));
+ QVERIFY(!sl13.invoke(0, qReturnArg(returnValue), argument));
+ QVERIFY(returnValue.isEmpty());
+
+ QVERIFY(sl13.invoke(&obj, qReturnArg(returnValue), argument));
+ QCOMPARE(returnValue, argument);
+ QCOMPARE(obj.slotResult, QString("sl13"));
+}
+
void tst_QMetaObject::indexOfMethod_data()
{
QTest::addColumn<QObject *>("object");
diff --git a/tests/auto/corelib/kernel/qmetaobjectbuilder/CMakeLists.txt b/tests/auto/corelib/kernel/qmetaobjectbuilder/CMakeLists.txt
index 2171b18b2f..8551749db3 100644
--- a/tests/auto/corelib/kernel/qmetaobjectbuilder/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetaobjectbuilder/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qmetaobjectbuilder.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetaobjectbuilder Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetaobjectbuilder LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmetaobjectbuilder
SOURCES
tst_qmetaobjectbuilder.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
)
diff --git a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp
index 36f792ec8a..18e4e2a4a9 100644
--- a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp
+++ b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QSignalSpy>
@@ -74,7 +74,7 @@ class SomethingOfEverything : public QObject
Q_PROPERTY(SomethingFlagEnum fprop READ fprop)
Q_PROPERTY(QLocale::Language language READ language)
Q_ENUMS(SomethingEnum)
- Q_FLAGS(SomethingFlagEnum)
+ Q_FLAGS(SomethingFlag)
public:
Q_INVOKABLE SomethingOfEverything() {}
~SomethingOfEverything() {}
@@ -90,6 +90,7 @@ public:
XYZ = 1,
UVW = 8
};
+ Q_DECLARE_FLAGS(SomethingFlag, SomethingFlagEnum)
Q_INVOKABLE Q_SCRIPTABLE void method1() {}
@@ -778,6 +779,26 @@ void tst_QMetaObjectBuilder::notifySignal()
void tst_QMetaObjectBuilder::enumerator()
{
+ static const QtPrivate::QMetaTypeInterface fooFlagMetaType = {
+ 0,
+ 8,
+ 8,
+ QMetaType::IsEnumeration | QMetaType::IsUnsignedEnumeration | QMetaType::RelocatableType,
+ {},
+ nullptr,
+ "fooFlag",
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ };
+
QMetaObjectBuilder builder;
// Add an enumerator and check its attributes.
@@ -809,6 +830,7 @@ void tst_QMetaObjectBuilder::enumerator()
enum1.setIsFlag(true);
enum1.setIsScoped(true);
enum1.setEnumName(QByteArrayLiteral("fooFlag"));
+ enum1.setMetaType(QMetaType(&fooFlagMetaType));
QCOMPARE(enum1.addKey("ABC", 0), 0);
QCOMPARE(enum1.addKey("DEF", 1), 1);
QCOMPARE(enum1.addKey("GHI", -1), 2);
@@ -818,6 +840,7 @@ void tst_QMetaObjectBuilder::enumerator()
QVERIFY(enum1.isFlag());
QVERIFY(enum1.isScoped());
QCOMPARE(enum1.enumName(), QByteArray("fooFlag"));
+ QCOMPARE(enum1.metaType(), QMetaType(&fooFlagMetaType));
QCOMPARE(enum1.keyCount(), 3);
QCOMPARE(enum1.index(), 0);
QCOMPARE(enum1.key(0), QByteArray("ABC"));
@@ -1498,8 +1521,8 @@ void tst_QMetaObjectBuilder::usage_signal()
QSignalSpy propChangedSpy(testObject.data(), &TestObject::intPropChanged);
testObject->emitIntPropChanged();
- QCOMPARE(propChangedSpy.count(), 1);
- QCOMPARE(propChangedSpy.at(0).count(), 1);
+ QCOMPARE(propChangedSpy.size(), 1);
+ QCOMPARE(propChangedSpy.at(0).size(), 1);
QCOMPARE(propChangedSpy.at(0).at(0).toInt(), testObject->intProp());
}
@@ -1514,7 +1537,7 @@ void tst_QMetaObjectBuilder::usage_property()
QSignalSpy propChangedSpy(testObject.data(), &TestObject::intPropChanged);
QVERIFY(testObject->intProp() != 123);
testObject->setProperty("intProp", 123);
- QCOMPARE(propChangedSpy.count(), 1);
+ QCOMPARE(propChangedSpy.size(), 1);
prop = testObject->property("intProp");
QCOMPARE(prop.metaType(), QMetaType(QMetaType::Int));
QCOMPARE(prop.toInt(), 123);
diff --git a/tests/auto/corelib/kernel/qmetaproperty/CMakeLists.txt b/tests/auto/corelib/kernel/qmetaproperty/CMakeLists.txt
index b69be31b1e..49c07afd87 100644
--- a/tests/auto/corelib/kernel/qmetaproperty/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetaproperty/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qmetaproperty.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetaproperty Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetaproperty LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmetaproperty
SOURCES
tst_qmetaproperty.cpp
diff --git a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp
index f459068bef..c8053ca43a 100644
--- a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp
+++ b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp
@@ -1,6 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -22,7 +22,14 @@ struct CustomType
Q_DECLARE_METATYPE(CustomType)
-class tst_QMetaProperty : public QObject
+class Base : public QObject
+{
+ Q_OBJECT
+signals:
+ void baseSignal(int);
+};
+
+class tst_QMetaProperty : public Base
{
Q_OBJECT
Q_PROPERTY(EnumType value WRITE setValue READ getValue)
@@ -33,6 +40,7 @@ class tst_QMetaProperty : public QObject
Q_PROPERTY(int value10 READ value10 FINAL)
Q_PROPERTY(QMap<int, int> map MEMBER map)
Q_PROPERTY(CustomType custom MEMBER custom)
+ Q_PROPERTY(int propWithInheritedSig READ propWithInheritedSig NOTIFY baseSignal)
private slots:
void hasStdCppSet();
@@ -43,6 +51,7 @@ private slots:
void mapProperty();
void conversion();
void enumsFlags();
+ void notifySignalIndex();
public:
enum EnumType { EnumType1 };
@@ -57,6 +66,8 @@ public:
int value9() const { return 1; }
int value10() const { return 1; }
+ int propWithInheritedSig() const { return 0; }
+
QString value7;
QMap<int, int> map;
CustomType custom;
@@ -274,5 +285,20 @@ void tst_QMetaProperty::enumsFlags()
QCOMPARE(t.flagProperty(), EnumFlagsTester::flag2);
}
+
+void tst_QMetaProperty::notifySignalIndex()
+{
+ auto mo = this->metaObject();
+ auto propIndex = mo->indexOfProperty("propWithInheritedSig");
+ auto propWithInheritedSig = mo->property(propIndex);
+ QVERIFY(propWithInheritedSig.isValid());
+ QVERIFY(propWithInheritedSig.hasNotifySignal());
+ QVERIFY(propWithInheritedSig.notifySignalIndex() != -1);
+ QMetaMethod notifySignal = propWithInheritedSig.notifySignal();
+ QVERIFY(notifySignal.isValid());
+ QCOMPARE(notifySignal.name(), "baseSignal");
+ QCOMPARE(notifySignal.parameterCount(), 1);
+}
+
QTEST_MAIN(tst_QMetaProperty)
#include "tst_qmetaproperty.moc"
diff --git a/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt b/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt
index 264c459a89..b93d961109 100644
--- a/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt
@@ -1,23 +1,59 @@
-# Generated from qmetatype.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmetatype Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmetatype LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
# Collect test data
list(APPEND test_data "./typeFlags.bin")
+qt_internal_add_cmake_library(qmetatype_lib1
+ INSTALL_DIRECTORY "${INSTALL_TESTSDIR}/tst_qmetatype"
+ SOURCES
+ lib1.cpp
+ PUBLIC_LIBRARIES
+ Qt::Core
+)
+qt_internal_add_cmake_library(qmetatype_lib2
+ INSTALL_DIRECTORY "${INSTALL_TESTSDIR}/tst_qmetatype"
+ SOURCES
+ lib2.cpp
+ PUBLIC_LIBRARIES
+ Qt::Core
+)
+set_target_properties(qmetatype_lib1 PROPERTIES
+ VERSION 1.0.0
+ SOVERSION 0
+ C_VISIBILITY_PRESET "hidden"
+ CXX_VISIBILITY_PRESET "hidden"
+ VISIBILITY_INLINES_HIDDEN ON
+)
+set_target_properties(qmetatype_lib2 PROPERTIES
+ VERSION 1.0.0
+ SOVERSION 0
+ C_VISIBILITY_PRESET "hidden"
+ CXX_VISIBILITY_PRESET "hidden"
+ VISIBILITY_INLINES_HIDDEN ON
+)
+
qt_internal_add_test(tst_qmetatype
SOURCES
tst_qmetatype.h tst_qmetatype.cpp tst_qmetatype2.cpp
tst_qmetatype3.cpp
- DEFINES
- QT_DISABLE_DEPRECATED_BEFORE=0
INCLUDE_DIRECTORIES
../../../other/qvariant_common
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Gui
+ qmetatype_lib1
+ qmetatype_lib2
TESTDATA ${test_data}
)
@@ -25,7 +61,3 @@ qt_internal_extend_target(tst_qmetatype CONDITION MSVC
COMPILE_OPTIONS
/bigobj
)
-
-#### Keys ignored in scope 3:.:.:qmetatype.pro:CLANG:
-# QMAKE_CFLAGS_RELEASE = "--O2" "--g"
-# QMAKE_CXXFLAGS_RELEASE = "--O2" "--g"
diff --git a/tests/auto/corelib/kernel/qmetatype/lib1.cpp b/tests/auto/corelib/kernel/qmetatype/lib1.cpp
new file mode 100644
index 0000000000..e5478f87d9
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetatype/lib1.cpp
@@ -0,0 +1,5 @@
+// Copyright (C) 2022 Intel Corporation
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#define LIB_NAMESPACE Lib1
+#include "lib_common.cpp"
diff --git a/tests/auto/corelib/kernel/qmetatype/lib2.cpp b/tests/auto/corelib/kernel/qmetatype/lib2.cpp
new file mode 100644
index 0000000000..0a905e669a
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetatype/lib2.cpp
@@ -0,0 +1,5 @@
+// Copyright (C) 2022 Intel Corporation
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#define LIB_NAMESPACE Lib2
+#include "lib_common.cpp"
diff --git a/tests/auto/corelib/kernel/qmetatype/lib_common.cpp b/tests/auto/corelib/kernel/qmetatype/lib_common.cpp
new file mode 100644
index 0000000000..179f539ccc
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetatype/lib_common.cpp
@@ -0,0 +1,13 @@
+// Copyright (C) 2022 Intel Corporation
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <qcollator.h>
+#include "tst_qmetatype_libs.h"
+
+#define DECLARE_FUNCTION(TYPE, ID) \
+ Q_DECL_EXPORT QMetaType metatype_ ## TYPE() \
+ { return QMetaType::fromType<TYPE>(); }
+
+namespace LIB_NAMESPACE {
+FOR_EACH_METATYPE_LIBS(DECLARE_FUNCTION)
+}
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index ed413107c0..81bf5d5ea8 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "tst_qmetatype.h"
@@ -13,6 +13,8 @@
#include <memory>
#include <vector>
+#include <QtCore/qflags.h>
+
Q_DECLARE_METATYPE(QMetaType::Type)
Q_DECLARE_METATYPE(QPartialOrdering)
@@ -121,6 +123,26 @@ static_assert(!QTypeTraits::has_operator_less_than_v<std::tuple<int, Nested2>>);
static_assert(QTypeTraits::has_operator_equal_v<std::tuple<int, Nested2>>);
static_assert(!QTypeTraits::has_operator_less_than_v<std::tuple<int, Nested2>>);
+// optionals of nesteds
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::variant<QString>>>);
+static_assert(QTypeTraits::has_operator_less_than_v<std::optional<std::variant<QString>>>);
+static_assert(!QTypeTraits::has_operator_equal_v<std::optional<std::variant<NoOperators>>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::variant<NoOperators>>>);
+
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<Nested>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<Nested>>);
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested>>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested>>>);
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested>>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested>>>);
+
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<Nested2>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<Nested2>>);
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested2>>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested2>>>);
+static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested2>>>);
+static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested2>>>);
+
}
struct BaseGenericType
@@ -376,6 +398,7 @@ public:
int Bar::failureCount = 0;
+#if QT_CONFIG(thread)
class MetaTypeTorturer: public QThread
{
Q_OBJECT
@@ -397,55 +420,65 @@ protected:
++failureCount;
qWarning() << "Wrong typeInfo returned for" << tp;
}
- if (!info.isRegistered()) {
+ if (info.flags() != (QMetaType::NeedsConstruction | QMetaType::NeedsDestruction |
+ QMetaType::NeedsCopyConstruction | QMetaType::NeedsMoveConstruction)) {
++failureCount;
- qWarning() << name << "is not a registered metatype";
+ qWarning() << "Wrong typeInfo returned for" << tp << "got"
+ << Qt::showbase << Qt::hex << info.flags();
}
- if (QMetaType::typeFlags(tp) != (QMetaType::NeedsConstruction | QMetaType::NeedsDestruction)) {
+ if (!info.isRegistered()) {
++failureCount;
- qWarning() << "Wrong typeInfo returned for" << tp;
+ qWarning() << name << "is not a registered metatype";
}
if (!QMetaType::isRegistered(tp)) {
++failureCount;
qWarning() << name << "is not a registered metatype";
}
- if (QMetaType::type(nm) != tp) {
+ if (QMetaType::fromName(nm).id() != tp) {
++failureCount;
qWarning() << "Wrong metatype returned for" << name;
}
- void *buf1 = QMetaType::create(tp, 0);
- void *buf2 = QMetaType::create(tp, buf1);
- void *buf3 = info.create(tp, 0);
- void *buf4 = info.create(tp, buf1);
- QMetaType::construct(tp, space, 0);
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *buf1 = QMetaType::create(tp, nullptr);
+ void *buf2 = QMetaType::create(tp, buf1);
+ QMetaType::construct(tp, space, nullptr);
QMetaType::destruct(tp, space);
QMetaType::construct(tp, space, buf1);
QMetaType::destruct(tp, space);
- info.construct(space, 0);
- info.destruct(space);
- info.construct(space, buf1);
- info.destruct(space);
-
if (!buf1) {
++failureCount;
- qWarning() << "Null buffer returned by QMetaType::create(tp, 0)";
+ qWarning() << "Null buffer returned by QMetaType::create(tp, nullptr)";
}
if (!buf2) {
++failureCount;
qWarning() << "Null buffer returned by QMetaType::create(tp, buf)";
}
+
+ QMetaType::destroy(tp, buf1);
+ QMetaType::destroy(tp, buf2);
+QT_WARNING_POP
+#endif
+
+ void *buf3 = info.create(nullptr);
+ void *buf4 = info.create(buf3);
+
+ info.construct(space, nullptr);
+ info.destruct(space);
+ info.construct(space, buf3);
+ info.destruct(space);
+
if (!buf3) {
++failureCount;
- qWarning() << "Null buffer returned by info.create(tp, 0)";
+ qWarning() << "Null buffer returned by info.create(nullptr)";
}
if (!buf4) {
++failureCount;
- qWarning() << "Null buffer returned by infocreate(tp, buf)";
+ qWarning() << "Null buffer returned by info.create(buf)";
}
- QMetaType::destroy(tp, buf1);
- QMetaType::destroy(tp, buf2);
+
info.destroy(buf3);
info.destroy(buf4);
}
@@ -475,6 +508,7 @@ void tst_QMetaType::threadSafety()
QCOMPARE(t3.failureCount, 0);
QCOMPARE(Bar::failureCount, 0);
}
+#endif
namespace TestSpace
{
@@ -493,7 +527,7 @@ void tst_QMetaType::namespaces()
QCOMPARE(qvariant_cast<TestSpace::Foo>(v).d, 11.12);
int qungTfuId = qRegisterMetaType<ADD_TESTSPACE(QungTfu)>();
- QCOMPARE(QMetaType::typeName(qungTfuId), "TestSpace::QungTfu");
+ QCOMPARE(QMetaType(qungTfuId).name(), "TestSpace::QungTfu");
}
void tst_QMetaType::id()
@@ -507,13 +541,13 @@ void tst_QMetaType::qMetaTypeId()
{
QCOMPARE(::qMetaTypeId<QString>(), int(QMetaType::QString));
QCOMPARE(::qMetaTypeId<int>(), int(QMetaType::Int));
- QCOMPARE(::qMetaTypeId<TestSpace::Foo>(), QMetaType::type("TestSpace::Foo"));
+ QCOMPARE(::qMetaTypeId<TestSpace::Foo>(), QMetaType::fromType<TestSpace::Foo>().id());
- QCOMPARE(::qMetaTypeId<char>(), QMetaType::type("char"));
- QCOMPARE(::qMetaTypeId<uchar>(), QMetaType::type("unsigned char"));
- QCOMPARE(::qMetaTypeId<signed char>(), QMetaType::type("signed char"));
+ QCOMPARE(::qMetaTypeId<char>(), QMetaType::fromType<char>().id());
+ QCOMPARE(::qMetaTypeId<uchar>(), QMetaType::fromType<unsigned char>().id());
+ QCOMPARE(::qMetaTypeId<signed char>(), QMetaType::fromType<signed char>().id());
QVERIFY(::qMetaTypeId<signed char>() != ::qMetaTypeId<char>());
- QCOMPARE(::qMetaTypeId<qint8>(), QMetaType::type("qint8"));
+ QCOMPARE(::qMetaTypeId<qint8>(), QMetaType::fromType<qint8>().id());
}
void tst_QMetaType::properties()
@@ -525,14 +559,14 @@ void tst_QMetaType::properties()
QCOMPARE(v.typeName(), "QVariantList");
QList<QVariant> values = v.toList();
- QCOMPARE(values.count(), 2);
+ QCOMPARE(values.size(), 2);
QCOMPARE(values.at(0).toInt(), 42);
values << 43 << "world";
QVERIFY(setProperty("prop", values));
v = property("prop");
- QCOMPARE(v.toList().count(), 4);
+ QCOMPARE(v.toList().size(), 4);
}
void tst_QMetaType::normalizedTypes()
@@ -540,13 +574,13 @@ void tst_QMetaType::normalizedTypes()
int WhityIntId = ::qMetaTypeId<Whity<int> >();
int WhityDoubleId = ::qMetaTypeId<Whity<double> >();
- QCOMPARE(QMetaType::type("Whity<int>"), WhityIntId);
- QCOMPARE(QMetaType::type(" Whity < int > "), WhityIntId);
- QCOMPARE(QMetaType::type("Whity<int >"), WhityIntId);
+ QCOMPARE(QMetaType::fromName("Whity<int>").id(), WhityIntId);
+ QCOMPARE(QMetaType::fromName(" Whity < int > ").id(), WhityIntId);
+ QCOMPARE(QMetaType::fromName("Whity<int >").id(), WhityIntId);
- QCOMPARE(QMetaType::type("Whity<double>"), WhityDoubleId);
- QCOMPARE(QMetaType::type(" Whity< double > "), WhityDoubleId);
- QCOMPARE(QMetaType::type("Whity<double >"), WhityDoubleId);
+ QCOMPARE(QMetaType::fromName("Whity<double>").id(), WhityDoubleId);
+ QCOMPARE(QMetaType::fromName(" Whity< double > ").id(), WhityDoubleId);
+ QCOMPARE(QMetaType::fromName("Whity<double >").id(), WhityDoubleId);
QCOMPARE(qRegisterMetaType<Whity<int> >(" Whity < int > "), WhityIntId);
QCOMPARE(qRegisterMetaType<Whity<int> >("Whity<int>"), WhityIntId);
@@ -564,6 +598,17 @@ namespace enumerations {
enum Test { a = 0 };
}
+static void ignoreInvalidMetaTypeWarning(int typeId)
+{
+ if (typeId < 0 || typeId > QMetaType::User + 500 ||
+ (typeId > QMetaType::LastCoreType && typeId < QMetaType::FirstGuiType) ||
+ (typeId > QMetaType::LastGuiType && typeId < QMetaType::FirstWidgetsType) ||
+ (typeId > QMetaType::LastWidgetsType && typeId < QMetaType::User)) {
+ QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: "
+ + QByteArray::number(typeId));
+ }
+}
+
void tst_QMetaType::typeName_data()
{
QTest::addColumn<int>("aType");
@@ -612,13 +657,15 @@ void tst_QMetaType::typeName()
if (aType >= QMetaType::FirstWidgetsType)
QSKIP("The test doesn't link against QtWidgets.");
- const char *rawname = QMetaType::typeName(aType);
+ ignoreInvalidMetaTypeWarning(aType);
+ const char *rawname = QMetaType(aType).name();
QString name = QString::fromLatin1(rawname);
QCOMPARE(name, aTypeName);
QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData()));
QCOMPARE(rawname == nullptr, aTypeName.isNull());
+ ignoreInvalidMetaTypeWarning(aType);
QMetaType mt(aType);
if (mt.isValid()) { // Gui type are not valid
QCOMPARE(QString::fromLatin1(QMetaType(aType).name()), aTypeName);
@@ -652,10 +699,8 @@ void tst_QMetaType::type()
if (aType >= QMetaType::FirstWidgetsType)
QSKIP("The test doesn't link against QtWidgets.");
- // QMetaType::type(QByteArray)
- QCOMPARE(QMetaType::type(aTypeName), aType);
- // QMetaType::type(const char *)
- QCOMPARE(QMetaType::type(aTypeName.constData()), aType);
+ QCOMPARE(QMetaType::fromName(aTypeName).id(), aType);
+ QCOMPARE(QMetaType::fromName(aTypeName.constData()).id(), aType);
}
void tst_QMetaType::type_fromSubString_data()
@@ -680,7 +725,7 @@ void tst_QMetaType::type_fromSubString()
QFETCH(int, size);
QFETCH(int, expectedType);
QByteArray ba = QByteArray::fromRawData(types + offset, size);
- QCOMPARE(QMetaType::type(ba), expectedType);
+ QCOMPARE(QMetaType::fromName(ba).id(), expectedType);
}
namespace {
@@ -707,7 +752,7 @@ void tst_QMetaType::create_data()
QTest::addColumn<int>("type");
QTest::newRow("unknown-type") << int(QMetaType::UnknownType);
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
- QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << int(QMetaType::MetaTypeName);
+ QTest::newRow(QMetaType(QMetaType::MetaTypeName).name()) << int(QMetaType::MetaTypeName);
FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
}
@@ -716,23 +761,36 @@ template<int ID>
static void testCreateHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
- QMetaType info(ID);
+ auto expected = std::unique_ptr<Type>(DefaultValueFactory<ID>::create());
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
void *actual1 = QMetaType::create(ID);
- void *actual2 = info.create();
- Type *expected = DefaultValueFactory<ID>::create();
+ auto cleanup1 = qScopeGuard([actual1]() {
+ QMetaType::destroy(ID, actual1);
+ });
QCOMPARE(*static_cast<Type *>(actual1), *expected);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(ID);
+ void *actual2 = info.create();
+ auto cleanup2 = qScopeGuard([&info, actual2]() {
+ info.destroy(actual2);
+ });
QCOMPARE(*static_cast<Type *>(actual2), *expected);
- delete expected;
-
- QMetaType::destroy(ID, actual1);
- info.destroy(actual2);
}
template<>
void testCreateHelper<QMetaType::Void>()
{
- void *actual = QMetaType::create(QMetaType::Void);
- QMetaType::destroy(QMetaType::Void, actual);
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *actual1 = QMetaType::create(QMetaType::Void);
+ QMetaType::destroy(QMetaType::Void, actual1);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(QMetaType::Void);
+ void *actual2 = info.create();
+ info.destroy(actual2);
}
@@ -767,15 +825,22 @@ template<int ID>
static void testCreateCopyHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
- Type *expected = TestValueFactory<ID>::create();
- QMetaType info(ID);
- void *actual1 = QMetaType::create(ID, expected);
- void *actual2 = info.create(expected);
+ auto expected = std::unique_ptr<Type>(TestValueFactory<ID>::create());
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *actual1 = QMetaType::create(ID, expected.get());
+ auto cleanup1 = qScopeGuard([actual1]() {
+ QMetaType::destroy(ID, actual1);
+ });
QCOMPARE(*static_cast<Type *>(actual1), *expected);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(ID);
+ void *actual2 = info.create(expected.get());
+ auto cleanup2 = qScopeGuard([&info, actual2]() {
+ info.destroy(actual2);
+ });
QCOMPARE(*static_cast<Type *>(actual2), *expected);
- QMetaType::destroy(ID, actual1);
- info.destroy(actual2);
- delete expected;
}
template<>
@@ -783,9 +848,22 @@ void testCreateCopyHelper<QMetaType::Void>()
{
typedef MetaEnumToType<QMetaType::Void>::Type Type;
Type *expected = TestValueFactory<QMetaType::Void>::create();
- void *actual = QMetaType::create(QMetaType::Void, expected);
- QCOMPARE(static_cast<Type *>(actual), expected);
- QMetaType::destroy(QMetaType::Void, actual);
+ QCOMPARE(expected, nullptr); // we do not need to delete it
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *actual1 = QMetaType::create(QMetaType::Void, expected);
+ auto cleanup1 = qScopeGuard([actual1]() {
+ QMetaType::destroy(QMetaType::Void, actual1);
+ });
+ QCOMPARE(static_cast<Type *>(actual1), expected);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(QMetaType::Void);
+ void *actual2 = info.create(expected);
+ auto cleanup2 = qScopeGuard([&info, actual2]() {
+ info.destroy(actual2);
+ });
+ QCOMPARE(static_cast<Type *>(actual2), expected);
}
void tst_QMetaType::createCopy_data()
@@ -802,7 +880,7 @@ void tst_QMetaType::createCopy()
switch (type) {
case QMetaType::UnknownType:
return []() {
- char buf[1];
+ char buf[1] = {};
QCOMPARE(QMetaType().create(&buf), nullptr);
};
#define RETURN_CREATE_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \
@@ -848,7 +926,9 @@ void tst_QMetaType::sizeOf()
{
QFETCH(int, type);
QFETCH(size_t, size);
- QCOMPARE(size_t(QMetaType::sizeOf(type)), size);
+ ignoreInvalidMetaTypeWarning(type);
+ QMetaType metaType(type);
+ QCOMPARE(size_t(metaType.sizeOf()), size);
}
void tst_QMetaType::sizeOfStaticLess_data()
@@ -860,6 +940,7 @@ void tst_QMetaType::sizeOfStaticLess()
{
QFETCH(int, type);
QFETCH(size_t, size);
+ ignoreInvalidMetaTypeWarning(type);
QCOMPARE(size_t(QMetaType(type).sizeOf()), size);
}
@@ -896,6 +977,7 @@ void tst_QMetaType::alignOf()
{
QFETCH(int, type);
QFETCH(size_t, size);
+ ignoreInvalidMetaTypeWarning(type);
QCOMPARE(size_t(QMetaType(type).alignOf()), size);
}
@@ -960,80 +1042,157 @@ Q_DECLARE_METATYPE(QPairPP)
enum FlagsDataEnum {};
Q_DECLARE_METATYPE(FlagsDataEnum);
+template <typename T> void addFlagsRow(const char *name, int id = qMetaTypeId<T>())
+{
+ QTest::newRow(name)
+ << id
+ << bool(QTypeInfo<T>::isRelocatable)
+ << bool(!std::is_default_constructible_v<T> || !QTypeInfo<T>::isValueInitializationBitwiseZero)
+ << bool(!std::is_trivially_copy_constructible_v<T>)
+ << bool(!std::is_trivially_destructible_v<T>)
+ << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value)
+ << bool(std::is_enum<T>::value)
+ << false;
+}
+
void tst_QMetaType::flags_data()
{
QTest::addColumn<int>("type");
- QTest::addColumn<bool>("isMovable");
- QTest::addColumn<bool>("isComplex");
+ QTest::addColumn<bool>("isRelocatable");
+ QTest::addColumn<bool>("needsConstruction");
+ QTest::addColumn<bool>("needsCopyConstruction");
+ QTest::addColumn<bool>("needsDestruction");
QTest::addColumn<bool>("isPointerToQObject");
QTest::addColumn<bool>("isEnum");
QTest::addColumn<bool>("isQmlList");
+ // invalid ids.
+ QTest::newRow("-1") << -1 << false << false << false << false << false << false << false;
+ QTest::newRow("-124125534") << -124125534 << false << false << false << false << false << false << false;
+ QTest::newRow("124125534") << 124125534 << false << false << false << false << false << false << false;
+
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
- QTest::newRow(#RealType) << MetaTypeId \
- << bool(QTypeInfo<RealType>::isRelocatable) \
- << bool(QTypeInfo<RealType>::isComplex) \
- << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<RealType>::Value) \
- << bool(std::is_enum<RealType>::value) \
- << false;
+ addFlagsRow<RealType>(#RealType, MetaTypeId);
+QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
- QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true << false << false << false;
- QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << true << true << false << false << false;
- QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true << false << false << false;
- QTest::newRow("CustomObject*") << ::qMetaTypeId<CustomObject*>() << true << false << true << false << false;
- QTest::newRow("CustomMultiInheritanceObject*") << ::qMetaTypeId<CustomMultiInheritanceObject*>() << true << false << true << false << false;
- QTest::newRow("QPair<C,C>") << ::qMetaTypeId<QPair<C,C> >() << false << true << false << false << false;
- QTest::newRow("QPair<C,M>") << ::qMetaTypeId<QPair<C,M> >() << false << true << false << false << false;
- QTest::newRow("QPair<C,P>") << ::qMetaTypeId<QPair<C,P> >() << false << true << false << false << false;
- QTest::newRow("QPair<M,C>") << ::qMetaTypeId<QPair<M,C> >() << false << true << false << false << false;
- QTest::newRow("QPair<M,M>") << ::qMetaTypeId<QPair<M,M> >() << true << true << false << false << false;
- QTest::newRow("QPair<M,P>") << ::qMetaTypeId<QPair<M,P> >() << true << true << false << false << false;
- QTest::newRow("QPair<P,C>") << ::qMetaTypeId<QPair<P,C> >() << false << true << false << false << false;
- QTest::newRow("QPair<P,M>") << ::qMetaTypeId<QPair<P,M> >() << true << true << false << false << false;
- QTest::newRow("QPair<P,P>") << ::qMetaTypeId<QPair<P,P> >() << true << false << false << false << false;
- QTest::newRow("FlagsDataEnum") << ::qMetaTypeId<FlagsDataEnum>() << true << false << false << true << false;
-
- // invalid ids.
- QTest::newRow("-1") << -1 << false << false << false << false << false;
- QTest::newRow("-124125534") << -124125534 << false << false << false << false << false;
- QTest::newRow("124125534") << 124125534 << false << false << false << false << false;
+ addFlagsRow<TestSpace::Foo>("TestSpace::Foo");
+ addFlagsRow<Whity<double> >("Whity<double> ");
+ addFlagsRow<CustomMovable>("CustomMovable");
+ addFlagsRow<CustomObject*>("CustomObject*");
+ addFlagsRow<CustomMultiInheritanceObject*>("CustomMultiInheritanceObject*");
+ addFlagsRow<QPair<C,C> >("QPair<C,C>");
+ addFlagsRow<QPair<C,M> >("QPair<C,M>");
+ addFlagsRow<QPair<C,P> >("QPair<C,P>");
+ addFlagsRow<QPair<M,C> >("QPair<M,C>");
+ addFlagsRow<QPair<M,M> >("QPair<M,M>");
+ addFlagsRow<QPair<M,P> >("QPair<M,P>");
+ addFlagsRow<QPair<P,C> >("QPair<P,C>");
+ addFlagsRow<QPair<P,M> >("QPair<P,M>");
+ addFlagsRow<QPair<P,P> >("QPair<P,P>");
+ addFlagsRow<FlagsDataEnum>("FlagsDataEnum");
}
void tst_QMetaType::flags()
{
QFETCH(int, type);
- QFETCH(bool, isMovable);
- QFETCH(bool, isComplex);
+ QFETCH(bool, isRelocatable);
+ QFETCH(bool, needsConstruction);
+ QFETCH(bool, needsCopyConstruction);
+ QFETCH(bool, needsDestruction);
QFETCH(bool, isPointerToQObject);
QFETCH(bool, isEnum);
QFETCH(bool, isQmlList);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsConstruction), isComplex);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsDestruction), isComplex);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::RelocatableType), isMovable);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::PointerToQObject), isPointerToQObject);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::IsEnumeration), isEnum);
- QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::IsQmlList), isQmlList);
+ ignoreInvalidMetaTypeWarning(type);
+ QMetaType meta(type);
+
+ QCOMPARE(bool(meta.flags() & QMetaType::NeedsConstruction), needsConstruction);
+ QCOMPARE(bool(meta.flags() & QMetaType::NeedsCopyConstruction), needsCopyConstruction);
+ QCOMPARE(bool(meta.flags() & QMetaType::NeedsDestruction), needsDestruction);
+ QCOMPARE(bool(meta.flags() & QMetaType::RelocatableType), isRelocatable);
+ QCOMPARE(bool(meta.flags() & QMetaType::PointerToQObject), isPointerToQObject);
+ QCOMPARE(bool(meta.flags() & QMetaType::IsEnumeration), isEnum);
+ QCOMPARE(bool(meta.flags() & QMetaType::IsQmlList), isQmlList);
}
-void tst_QMetaType::flagsStaticLess_data()
+class NonDefaultConstructible
{
- flags_data();
+ NonDefaultConstructible(int) {}
+};
+
+struct MoveOnly
+{
+ MoveOnly() = default;
+ MoveOnly(const MoveOnly &) = delete;
+ MoveOnly(MoveOnly &&) = default;
+ MoveOnly &operator=(const MoveOnly &) = delete;
+ MoveOnly &operator=(MoveOnly &&) = default;
+};
+
+class Indestructible
+{
+protected:
+ ~Indestructible() {}
+};
+
+template <typename T> static void addFlags2Row(QMetaType metaType = QMetaType::fromType<T>())
+{
+ QTest::newRow(metaType.name() ? metaType.name() : "UnknownType")
+ << metaType
+ << std::is_default_constructible_v<T>
+ << std::is_copy_constructible_v<T>
+ << std::is_move_constructible_v<T>
+ << std::is_destructible_v<T>
+ << (QTypeTraits::has_operator_equal<T>::value || QTypeTraits::has_operator_less_than<T>::value)
+ << QTypeTraits::has_operator_less_than<T>::value;
+};
+
+void tst_QMetaType::flags2_data()
+{
+ QTest::addColumn<QMetaType>("type");
+ QTest::addColumn<bool>("isDefaultConstructible");
+ QTest::addColumn<bool>("isCopyConstructible");
+ QTest::addColumn<bool>("isMoveConstructible");
+ QTest::addColumn<bool>("isDestructible");
+ QTest::addColumn<bool>("isEqualityComparable");
+ QTest::addColumn<bool>("isOrdered");
+
+ addFlags2Row<void>(QMetaType());
+ addFlags2Row<void>();
+
+#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
+ addFlags2Row<RealType>();
+ QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_METATYPE_TEST_ROW)
+ QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW)
+ QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW)
+ QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW)
+#undef ADD_METATYPE_TEST_ROW
+
+ addFlags2Row<NonDefaultConstructible>();
+ addFlags2Row<MoveOnly>();
+ addFlags2Row<QObject>();
+ addFlags2Row<Indestructible>();
}
-void tst_QMetaType::flagsStaticLess()
+void tst_QMetaType::flags2()
{
- QFETCH(int, type);
- QFETCH(bool, isMovable);
- QFETCH(bool, isComplex);
+ QFETCH(QMetaType, type);
+ QFETCH(bool, isDefaultConstructible);
+ QFETCH(bool, isCopyConstructible);
+ QFETCH(bool, isMoveConstructible);
+ QFETCH(bool, isDestructible);
+ QFETCH(bool, isEqualityComparable);
+ QFETCH(bool, isOrdered);
- int flags = QMetaType(type).flags();
- QCOMPARE(bool(flags & QMetaType::NeedsConstruction), isComplex);
- QCOMPARE(bool(flags & QMetaType::NeedsDestruction), isComplex);
- QCOMPARE(bool(flags & QMetaType::RelocatableType), isMovable);
+ QCOMPARE(type.isDefaultConstructible(), isDefaultConstructible);
+ QCOMPARE(type.isCopyConstructible(), isCopyConstructible);
+ QCOMPARE(type.isMoveConstructible(), isMoveConstructible);
+ QCOMPARE(type.isDestructible(), isDestructible);
+ QCOMPARE(type.isEqualityComparable(), isEqualityComparable);
+ QCOMPARE(type.isOrdered(), isOrdered);
}
void tst_QMetaType::flagsBinaryCompatibility6_0_data()
@@ -1061,7 +1220,7 @@ void tst_QMetaType::flagsBinaryCompatibility6_0_data()
QTest::addColumn<quint32>("flags");
QFile file(QFINDTESTDATA("typeFlags.bin"));
- file.open(QIODevice::ReadOnly);
+ QVERIFY(file.open(QIODevice::ReadOnly));
QList<quint32> buffer;
QDataStream ds(&file);
ds >> buffer;
@@ -1072,7 +1231,7 @@ void tst_QMetaType::flagsBinaryCompatibility6_0_data()
if (id > QMetaType::LastCoreType)
continue; // We do not link against QtGui, so we do longer consider such type as registered
QVERIFY2(QMetaType::isRegistered(id), "A type could not be removed in BC way");
- QTest::newRow(QMetaType::typeName(id)) << id << flags;
+ QTest::newRow(QMetaType(id).name()) << id << flags;
}
}
@@ -1081,16 +1240,14 @@ void tst_QMetaType::flagsBinaryCompatibility6_0()
QFETCH(quint32, id);
QFETCH(quint32, flags);
- const auto currentFlags = QMetaType::typeFlags(id);
+ const auto currentFlags = QMetaType(id).flags();
auto expectedFlags = QMetaType::TypeFlags(flags);
- if (!(currentFlags.testFlag(QMetaType::NeedsConstruction) && currentFlags.testFlag(QMetaType::NeedsDestruction))) {
- if (expectedFlags.testFlag(QMetaType::NeedsConstruction) && expectedFlags.testFlag(QMetaType::NeedsDestruction)) {
- // If type changed from RELOCATABLE to trivial, that's fine
- expectedFlags.setFlag(QMetaType::NeedsConstruction, false);
- expectedFlags.setFlag(QMetaType::NeedsDestruction, false);
- }
- }
- quint32 mask_5_0 = 0x1fb; // Only compare the values that were already defined in 5.0
+
+ // Only compare the values that were already defined in 5.0.
+ // In 6.5, some types lost NeedsConstruction and NeedsDestruction, but
+ // that's acceptable if that's because they were trivial
+ quint32 mask_5_0 = 0x1ff & ~quint32(QMetaType::NeedsConstruction | QMetaType::NeedsDestruction
+ | QMetaType::RelocatableType);
QCOMPARE(quint32(currentFlags) & mask_5_0, quint32(expectedFlags) & mask_5_0);
}
@@ -1104,42 +1261,61 @@ template<int ID>
static void testConstructHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
+ auto expected = std::unique_ptr<Type>(DefaultValueFactory<ID>::create());
QMetaType info(ID);
int size = info.sizeOf();
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
void *storage1 = qMallocAligned(size, alignof(Type));
- void *actual1 = QMetaType::construct(ID, storage1, /*copy=*/0);
- void *storage2 = qMallocAligned(size, alignof(Type));
- void *actual2 = info.construct(storage2, /*copy=*/0);
+ void *actual1 = QMetaType::construct(ID, storage1, /*copy=*/nullptr);
+ auto cleanup1 = qScopeGuard([storage1, actual1]() {
+ QMetaType::destruct(ID, actual1);
+ qFreeAligned(storage1);
+ });
QCOMPARE(actual1, storage1);
- QCOMPARE(actual2, storage2);
- Type *expected = DefaultValueFactory<ID>::create();
QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(QMetaType::construct(ID, nullptr, /*copy=*/nullptr), nullptr);
+ QMetaType::destruct(ID, nullptr);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ void *storage2 = qMallocAligned(size, alignof(Type));
+ void *actual2 = info.construct(storage2, /*copy=*/nullptr);
+ auto cleanup2 = qScopeGuard([&info, storage2, actual2]() {
+ info.destruct(actual2);
+ qFreeAligned(storage2);
+ });
+ QCOMPARE(actual2, storage2);
QCOMPARE(*static_cast<Type *>(actual2), *expected);
- delete expected;
- QMetaType::destruct(ID, actual1);
- qFreeAligned(storage1);
- info.destruct(actual2);
- qFreeAligned(storage2);
-
- QVERIFY(QMetaType::construct(ID, 0, /*copy=*/0) == 0);
- QMetaType::destruct(ID, 0);
-
- QVERIFY(info.construct(0, /*copy=*/0) == 0);
- info.destruct(0);
+ QCOMPARE(info.construct(nullptr, /*copy=*/nullptr), nullptr);
+ info.destruct(nullptr);
}
template<>
void testConstructHelper<QMetaType::Void>()
{
- /*int size = */ QMetaType::sizeOf(QMetaType::Void);
- void *storage = 0;
- void *actual = QMetaType::construct(QMetaType::Void, storage, /*copy=*/0);
- QCOMPARE(actual, storage);
- QMetaType::destruct(QMetaType::Void, actual);
- qFreeAligned(storage);
-
- QVERIFY(QMetaType::construct(QMetaType::Void, 0, /*copy=*/0) == 0);
- QMetaType::destruct(QMetaType::Void, 0);
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *storage1 = nullptr;
+ void *actual1 = QMetaType::construct(QMetaType::Void, storage1, /*copy=*/nullptr);
+ auto cleanup1 = qScopeGuard([storage1, actual1]() {
+ QMetaType::destruct(QMetaType::Void, actual1);
+ qFreeAligned(storage1);
+ });
+ QCOMPARE(actual1, storage1);
+ QCOMPARE(QMetaType::construct(QMetaType::Void, nullptr, /*copy=*/nullptr), nullptr);
+ QMetaType::destruct(QMetaType::Void, nullptr);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(QMetaType::Void);
+ void *storage2 = nullptr;
+ void *actual2 = info.construct(storage2, /*copy=*/nullptr);
+ auto cleanup2 = qScopeGuard([&info, storage2, actual2]() {
+ info.destruct(actual2);
+ qFreeAligned(storage2);
+ });
+ QCOMPARE(actual2, storage2);
+ QVERIFY(info.construct(nullptr, /*copy=*/nullptr) == nullptr);
+ info.destruct(nullptr);
}
void tst_QMetaType::construct()
@@ -1168,11 +1344,137 @@ FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_FUNCTION)
TypeTestFunctionGetter::get(type)();
}
+
+namespace TriviallyConstructibleTests {
+
+enum Enum0 {};
+enum class Enum1 {};
+
+static_assert(QTypeInfo<int>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<double>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<Enum0>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<Enum1>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<int *>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<void *>::isValueInitializationBitwiseZero);
+static_assert(QTypeInfo<std::nullptr_t>::isValueInitializationBitwiseZero);
+
+struct A {};
+struct B { B() {} };
+struct C { ~C() {} };
+struct D { D(int) {} };
+struct E { E() {} ~E() {} };
+struct F { int i; };
+struct G { G() : i(0) {} int i; };
+struct H { constexpr H() : i(0) {} int i; };
+struct I { I() : i(42) {} int i; };
+struct J { constexpr J() : i(42) {} int i; };
+struct K { K() : i(0) {} ~K() {} int i; };
+
+static_assert(!QTypeInfo<A>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<B>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<C>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<D>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<E>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<F>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<G>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<H>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<I>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<J>::isValueInitializationBitwiseZero);
+static_assert(!QTypeInfo<K>::isValueInitializationBitwiseZero);
+
+} // namespace TriviallyConstructibleTests
+
+// Value-initializing these trivially constructible types cannot be achieved by
+// memset(0) into their storage. For instance, on Itanium, a pointer to a data
+// member needs to be value-initialized by setting it to -1.
+
+// Fits into QVariant
+struct TrivialTypeNotZeroInitableSmall {
+ int TrivialTypeNotZeroInitableSmall::*pdm;
+};
+
+static_assert(std::is_trivially_default_constructible_v<TrivialTypeNotZeroInitableSmall>);
+static_assert(!QTypeInfo<TrivialTypeNotZeroInitableSmall>::isValueInitializationBitwiseZero);
+static_assert(sizeof(TrivialTypeNotZeroInitableSmall) < sizeof(QVariant)); // also checked more thoroughly below
+
+// Does not fit into QVariant internal storage
+struct TrivialTypeNotZeroInitableBig {
+ int a;
+ double b;
+ char c;
+ int array[42];
+ void (TrivialTypeNotZeroInitableBig::*pmf)();
+ int TrivialTypeNotZeroInitableBig::*pdm;
+};
+
+static_assert(std::is_trivially_default_constructible_v<TrivialTypeNotZeroInitableBig>);
+static_assert(!QTypeInfo<TrivialTypeNotZeroInitableSmall>::isValueInitializationBitwiseZero);
+static_assert(sizeof(TrivialTypeNotZeroInitableBig) > sizeof(QVariant)); // also checked more thoroughly below
+
+void tst_QMetaType::defaultConstructTrivial_QTBUG_109594()
+{
+ // MSVC miscompiles value-initialization of pointers to data members,
+ // https://developercommunity.visualstudio.com/t/Pointer-to-data-member-is-not-initialize/10238905
+ {
+ QMetaType mt = QMetaType::fromType<TrivialTypeNotZeroInitableSmall>();
+ QVERIFY(mt.isDefaultConstructible());
+ auto ptr = static_cast<TrivialTypeNotZeroInitableSmall *>(mt.create());
+ const auto cleanup = qScopeGuard([=] {
+ mt.destroy(ptr);
+ });
+#ifdef Q_CC_MSVC_ONLY
+ QEXPECT_FAIL("", "MSVC compiler bug", Continue);
+#endif
+ QCOMPARE(ptr->pdm, nullptr);
+
+ QVariant v(mt);
+ QVERIFY(QVariant::Private::canUseInternalSpace(mt.iface()));
+ auto obj = v.value<TrivialTypeNotZeroInitableSmall>();
+#ifdef Q_CC_MSVC_ONLY
+ QEXPECT_FAIL("", "MSVC compiler bug", Continue);
+#endif
+ QCOMPARE(obj.pdm, nullptr);
+ }
+
+ {
+ QMetaType mt = QMetaType::fromType<TrivialTypeNotZeroInitableBig>();
+ QVERIFY(mt.isDefaultConstructible());
+ auto ptr = static_cast<TrivialTypeNotZeroInitableBig *>(mt.create());
+ const auto cleanup = qScopeGuard([=] {
+ mt.destroy(ptr);
+ });
+ QCOMPARE(ptr->a, 0);
+ QCOMPARE(ptr->b, 0.0);
+ QCOMPARE(ptr->c, '\0');
+ QCOMPARE(ptr->pmf, nullptr);
+ for (int i : ptr->array)
+ QCOMPARE(i, 0);
+#ifdef Q_CC_MSVC_ONLY
+ QEXPECT_FAIL("", "MSVC compiler bug", Continue);
+#endif
+ QCOMPARE(ptr->pdm, nullptr);
+
+ QVariant v(mt);
+ QVERIFY(!QVariant::Private::canUseInternalSpace(mt.iface()));
+ auto obj = v.value<TrivialTypeNotZeroInitableBig>();
+ QCOMPARE(obj.a, 0);
+ QCOMPARE(obj.b, 0.0);
+ QCOMPARE(obj.c, '\0');
+ QCOMPARE(obj.pmf, nullptr);
+ for (int i : obj.array)
+ QCOMPARE(i, 0);
+#ifdef Q_CC_MSVC_ONLY
+ QEXPECT_FAIL("", "MSVC compiler bug", Continue);
+#endif
+ QCOMPARE(obj.pdm, nullptr);
+ }
+}
+
void tst_QMetaType::typedConstruct()
{
auto testMetaObjectWriteOnGadget = [](QVariant &gadget, const QList<GadgetPropertyType> &properties)
{
- auto metaObject = QMetaType::metaObjectForType(gadget.userType());
+ auto metaObject = QMetaType(gadget.userType()).metaObject();
QVERIFY(metaObject != nullptr);
QCOMPARE(metaObject->methodCount(), 0);
QCOMPARE(metaObject->propertyCount(), properties.size());
@@ -1186,7 +1488,7 @@ void tst_QMetaType::typedConstruct()
auto testMetaObjectReadOnGadget = [](QVariant gadget, const QList<GadgetPropertyType> &properties)
{
- auto metaObject = QMetaType::metaObjectForType(gadget.userType());
+ auto metaObject = QMetaType(gadget.userType()).metaObject();
QVERIFY(metaObject != nullptr);
QCOMPARE(metaObject->methodCount(), 0);
QCOMPARE(metaObject->propertyCount(), properties.size());
@@ -1194,7 +1496,7 @@ void tst_QMetaType::typedConstruct()
auto prop = metaObject->property(i);
QCOMPARE(properties[i].name, prop.name());
QCOMPARE(properties[i].type, prop.typeName());
- if (!QMetaType::typeFlags(prop.userType()).testFlag(QMetaType::IsGadget))
+ if (!QMetaType(prop.userType()).flags().testFlag(QMetaType::IsGadget))
QCOMPARE(properties[i].testData, prop.readOnGadget(gadget.constData()));
}
};
@@ -1206,7 +1508,7 @@ void tst_QMetaType::typedConstruct()
};
registerGadget("DynamicGadget1", dynamicGadget1);
- QVariant testGadget1(QMetaType(QMetaType::type("DynamicGadget1")));
+ QVariant testGadget1(QMetaType::fromName("DynamicGadget1"));
testMetaObjectWriteOnGadget(testGadget1, dynamicGadget1);
testMetaObjectReadOnGadget(testGadget1, dynamicGadget1);
@@ -1218,10 +1520,10 @@ void tst_QMetaType::typedConstruct()
{"DynamicGadget1", "dynamicGadget1_prop", testGadget1}
};
registerGadget("DynamicGadget2", dynamicGadget2);
- QVariant testGadget2(QMetaType(QMetaType::type("DynamicGadget2")));
+ QVariant testGadget2(QMetaType::fromName("DynamicGadget2"));
testMetaObjectWriteOnGadget(testGadget2, dynamicGadget2);
testMetaObjectReadOnGadget(testGadget2, dynamicGadget2);
- auto g2mo = QMetaType::metaObjectForType(testGadget2.userType());
+ auto g2mo = QMetaType(testGadget2.userType()).metaObject();
auto dynamicGadget1_prop = g2mo->property(g2mo->indexOfProperty("dynamicGadget1_prop"));
testMetaObjectReadOnGadget(dynamicGadget1_prop.readOnGadget(testGadget2.constData()), dynamicGadget1);
@@ -1253,7 +1555,7 @@ void tst_QMetaType::typedConstruct()
s_managedTypes[podTypeId] = qMakePair(dynamicGadgetProperties, std::shared_ptr<QMetaObject>{});
// Test POD
- QCOMPARE(podTypeId, QMetaType::type(podTypeName));
+ QCOMPARE(podTypeId, QMetaType::fromName(podTypeName).id());
QVariant podVariant{QMetaType(podTypeId)};
QCOMPARE(myPodTesData, static_cast<const GenericPODType *>(reinterpret_cast<const BaseGenericType *>(podVariant.constData()))->podData);
@@ -1267,27 +1569,32 @@ template<int ID>
static void testConstructCopyHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
- Type *expected = TestValueFactory<ID>::create();
+ auto expected = std::unique_ptr<Type>(TestValueFactory<ID>::create());
QMetaType info(ID);
- int size = QMetaType::sizeOf(ID);
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ const int size = QMetaType::sizeOf(ID);
QCOMPARE(info.sizeOf(), size);
void *storage1 = qMallocAligned(size, alignof(Type));
- void *actual1 = QMetaType::construct(ID, storage1, expected);
- void *storage2 = qMallocAligned(size, alignof(Type));
- void *actual2 = info.construct(storage2, expected);
+ void *actual1 = QMetaType::construct(ID, storage1, expected.get());
+ auto cleanup1 = qScopeGuard([storage1, actual1]() {
+ QMetaType::destruct(ID, actual1);
+ qFreeAligned(storage1);
+ });
QCOMPARE(actual1, storage1);
- QCOMPARE(actual2, storage2);
QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(QMetaType::construct(ID, nullptr, nullptr), nullptr);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ void *storage2 = qMallocAligned(info.sizeOf(), alignof(Type));
+ void *actual2 = info.construct(storage2, expected.get());
+ auto cleanup2 = qScopeGuard([&info, storage2, actual2]() {
+ info.destruct(actual2);
+ qFreeAligned(storage2);
+ });
+ QCOMPARE(actual2, storage2);
QCOMPARE(*static_cast<Type *>(actual2), *expected);
- QMetaType::destruct(ID, actual1);
- qFreeAligned(storage1);
- info.destruct(actual2);
- qFreeAligned(storage2);
-
- QVERIFY(QMetaType::construct(ID, 0, expected) == 0);
- QVERIFY(info.construct(0, expected) == 0);
-
- delete expected;
+ QCOMPARE(info.construct(nullptr, expected.get()), nullptr);
}
template<>
@@ -1295,14 +1602,28 @@ void testConstructCopyHelper<QMetaType::Void>()
{
typedef MetaEnumToType<QMetaType::Void>::Type Type;
Type *expected = TestValueFactory<QMetaType::Void>::create();
- /* int size = */QMetaType::sizeOf(QMetaType::Void);
- void *storage = 0;
- void *actual = QMetaType::construct(QMetaType::Void, storage, expected);
- QCOMPARE(actual, storage);
- QMetaType::destruct(QMetaType::Void, actual);
- qFreeAligned(storage);
-
- QVERIFY(QMetaType::construct(QMetaType::Void, 0, expected) == 0);
+ QCOMPARE(expected, nullptr); // we do not need to delete it
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
+ void *storage1 = nullptr;
+ void *actual1 = QMetaType::construct(QMetaType::Void, storage1, expected);
+ auto cleanup1 = qScopeGuard([storage1, actual1]() {
+ QMetaType::destruct(QMetaType::Void, actual1);
+ qFreeAligned(storage1);
+ });
+ QCOMPARE(actual1, storage1);
+ QCOMPARE(QMetaType::construct(QMetaType::Void, nullptr, nullptr), nullptr);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+ QMetaType info(QMetaType::Void);
+ void *storage2 = nullptr;
+ void *actual2 = info.construct(storage2, expected);
+ auto cleanup2 = qScopeGuard([&info, storage2, actual2]() {
+ info.destruct(actual2);
+ qFreeAligned(storage2);
+ });
+ QCOMPARE(actual2, storage2);
+ QCOMPARE(info.construct(nullptr, expected), nullptr);
}
void tst_QMetaType::constructCopy_data()
@@ -1319,7 +1640,7 @@ void tst_QMetaType::constructCopy()
switch (type) {
case QMetaType::UnknownType:
return []() {
- char buf[1], buf2[1];
+ char buf[1], buf2[1] = {};
QCOMPARE(QMetaType().construct(&buf, &buf2), nullptr);
};
#define RETURN_CONSTRUCT_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \
@@ -1351,7 +1672,7 @@ void tst_QMetaType::selfCompare_data()
QTest::newRow("unknown-type") << int(QMetaType::UnknownType) << orderingFor(QMetaType::UnknownType);
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
- QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << int(QMetaType::MetaTypeName) << orderingFor(QMetaType::MetaTypeName);
+ QTest::newRow(QMetaType(QMetaType::MetaTypeName).name()) << int(QMetaType::MetaTypeName) << orderingFor(QMetaType::MetaTypeName);
FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
}
@@ -1393,42 +1714,49 @@ Q_DECLARE_METATYPE(CustomString) //this line is useless
void tst_QMetaType::typedefs()
{
- QCOMPARE(QMetaType::type("long long"), int(QMetaType::LongLong));
- QCOMPARE(QMetaType::type("unsigned long long"), int(QMetaType::ULongLong));
- QCOMPARE(QMetaType::type("qint8"), int(QMetaType::SChar));
- QCOMPARE(QMetaType::type("quint8"), int(QMetaType::UChar));
- QCOMPARE(QMetaType::type("qint16"), int(QMetaType::Short));
- QCOMPARE(QMetaType::type("quint16"), int(QMetaType::UShort));
- QCOMPARE(QMetaType::type("qint32"), int(QMetaType::Int));
- QCOMPARE(QMetaType::type("quint32"), int(QMetaType::UInt));
- QCOMPARE(QMetaType::type("qint64"), int(QMetaType::LongLong));
- QCOMPARE(QMetaType::type("quint64"), int(QMetaType::ULongLong));
+ QCOMPARE(QMetaType::fromName("long long").id(), int(QMetaType::LongLong));
+ QCOMPARE(QMetaType::fromName("unsigned long long").id(), int(QMetaType::ULongLong));
+ QCOMPARE(QMetaType::fromName("qint8").id(), int(QMetaType::SChar));
+ QCOMPARE(QMetaType::fromName("quint8").id(), int(QMetaType::UChar));
+ QCOMPARE(QMetaType::fromName("qint16").id(), int(QMetaType::Short));
+ QCOMPARE(QMetaType::fromName("quint16").id(), int(QMetaType::UShort));
+ QCOMPARE(QMetaType::fromName("qint32").id(), int(QMetaType::Int));
+ QCOMPARE(QMetaType::fromName("quint32").id(), int(QMetaType::UInt));
+ QCOMPARE(QMetaType::fromName("qint64").id(), int(QMetaType::LongLong));
+ QCOMPARE(QMetaType::fromName("quint64").id(), int(QMetaType::ULongLong));
// make sure the qreal typeId is the type id of the type it's defined to
- QCOMPARE(QMetaType::type("qreal"), ::qMetaTypeId<qreal>());
+ QCOMPARE(QMetaType::fromName("qreal").id(), ::qMetaTypeId<qreal>());
qRegisterMetaType<CustomString>("CustomString");
- QCOMPARE(QMetaType::type("CustomString"), ::qMetaTypeId<CustomString>());
+ QCOMPARE(QMetaType::fromName("CustomString").id(), ::qMetaTypeId<CustomString>());
typedef Whity<double> WhityDouble;
qRegisterMetaType<WhityDouble>("WhityDouble");
- QCOMPARE(QMetaType::type("WhityDouble"), ::qMetaTypeId<WhityDouble>());
+ QCOMPARE(QMetaType::fromName("WhityDouble").id(), ::qMetaTypeId<WhityDouble>());
}
+struct RegisterTypeType {};
+
void tst_QMetaType::registerType()
{
// Built-in
QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString));
QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString));
+ qRegisterMetaType(QMetaType::fromType<QString>());
// Custom
int fooId = qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo");
QVERIFY(fooId >= int(QMetaType::User));
QCOMPARE(qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo"), fooId);
+ qRegisterMetaType(QMetaType::fromType<TestSpace::Foo>());
int movableId = qRegisterMetaType<CustomMovable>("CustomMovable");
QVERIFY(movableId >= int(QMetaType::User));
QCOMPARE(qRegisterMetaType<CustomMovable>("CustomMovable"), movableId);
+ qRegisterMetaType(QMetaType::fromType<CustomMovable>());
+
+ // Aliases are deprecated
// Alias to built-in
typedef QString MyString;
@@ -1436,7 +1764,7 @@ void tst_QMetaType::registerType()
QCOMPARE(qRegisterMetaType<MyString>("MyString"), int(QMetaType::QString));
QCOMPARE(qRegisterMetaType<MyString>("MyString"), int(QMetaType::QString));
- QCOMPARE(QMetaType::type("MyString"), int(QMetaType::QString));
+ QCOMPARE(QMetaType::fromType<MyString>().id(), int(QMetaType::QString));
// Alias to custom type
typedef CustomMovable MyMovable;
@@ -1445,12 +1773,29 @@ void tst_QMetaType::registerType()
QCOMPARE(qRegisterMetaType<MyMovable>("MyMovable"), movableId);
QCOMPARE(qRegisterMetaType<MyMovable>("MyMovable"), movableId);
- QCOMPARE(QMetaType::type("MyMovable"), movableId);
+ QCOMPARE(QMetaType::fromType<MyMovable>().id(), movableId);
QCOMPARE(qRegisterMetaType<MyFoo>("MyFoo"), fooId);
QCOMPARE(qRegisterMetaType<MyFoo>("MyFoo"), fooId);
- QCOMPARE(QMetaType::type("MyFoo"), fooId);
+ QCOMPARE(QMetaType::fromType<MyFoo>().id(), fooId);
+
+ // this portion of the test can only be run once
+ static bool typeWasRegistered = false;
+ if (!typeWasRegistered) {
+ QMetaType mt = QMetaType::fromType<RegisterTypeType>();
+ QVERIFY(mt.isValid());
+ QCOMPARE_NE(mt.name(), nullptr);
+
+ QVERIFY(!mt.isRegistered());
+ QVERIFY(!QMetaType::fromName(mt.name()).isValid());
+
+ QCOMPARE_GT(qRegisterMetaType(mt), 0);
+ typeWasRegistered = true;
+
+ QVERIFY(mt.isRegistered());
+ QVERIFY(QMetaType::fromName(mt.name()).isValid());
+ }
}
class IsRegisteredDummyType { };
@@ -1471,7 +1816,7 @@ void tst_QMetaType::isRegistered_data()
// unknown types
QTest::newRow("-1") << -1 << false;
QTest::newRow("-42") << -42 << false;
- QTest::newRow("IsRegisteredDummyType + 1") << (dummyTypeId + 1) << false;
+ QTest::newRow("IsRegisteredDummyType + 1000") << (dummyTypeId + 1000) << false;
QTest::newRow("QMetaType::UnknownType") << int(QMetaType::UnknownType) << false;
}
@@ -1494,25 +1839,77 @@ Q_DECLARE_METATYPE(isEnumTest_Enum1)
void tst_QMetaType::isEnum()
{
int type0 = qRegisterMetaType<int>("int");
- QVERIFY((QMetaType::typeFlags(type0) & QMetaType::IsEnumeration) == 0);
+ QVERIFY((QMetaType(type0).flags() & QMetaType::IsEnumeration) == 0);
int type1 = qRegisterMetaType<isEnumTest_Enum0>("isEnumTest_Enum0");
- QVERIFY((QMetaType::typeFlags(type1) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
+ QVERIFY((QMetaType(type1).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
int type2 = qRegisterMetaType<isEnumTest_Struct0>("isEnumTest_Struct0");
- QVERIFY((QMetaType::typeFlags(type2) & QMetaType::IsEnumeration) == 0);
+ QVERIFY((QMetaType(type2).flags() & QMetaType::IsEnumeration) == 0);
int type3 = qRegisterMetaType<isEnumTest_Enum0 *>("isEnumTest_Enum0 *");
- QVERIFY((QMetaType::typeFlags(type3) & QMetaType::IsEnumeration) == 0);
+ QVERIFY((QMetaType(type3).flags() & QMetaType::IsEnumeration) == 0);
int type4 = qRegisterMetaType<isEnumTest_Struct0::A>("isEnumTest_Struct0::A");
- QVERIFY((QMetaType::typeFlags(type4) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
+ QVERIFY((QMetaType(type4).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
int type5 = ::qMetaTypeId<isEnumTest_Struct1>();
- QVERIFY((QMetaType::typeFlags(type5) & QMetaType::IsEnumeration) == 0);
+ QVERIFY((QMetaType(type5).flags() & QMetaType::IsEnumeration) == 0);
int type6 = ::qMetaTypeId<isEnumTest_Enum1>();
- QVERIFY((QMetaType::typeFlags(type6) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
+ QVERIFY((QMetaType(type6).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration);
+}
+
+enum E1 : unsigned char {};
+enum E2 : qlonglong {};
+enum class E3 : unsigned short {};
+
+namespace myflags {
+
+ Q_NAMESPACE
+
+ enum Flag1 : int { A, B };
+ enum Flag2 : short { X, Y };
+
+ Q_DECLARE_FLAGS(Flags1, myflags::Flag1);
+ Q_FLAG_NS(Flags1)
+ Q_DECLARE_FLAGS(Flags2, myflags::Flag2);
+ Q_FLAG_NS(Flags2)
+
+}
+
+template <typename T>
+using getUnderlyingTypeNormalized = std::conditional_t<
+ std::is_signed_v<std::underlying_type_t<T>>,
+ typename QIntegerForSize<sizeof(T)>::Signed,
+ typename QIntegerForSize<sizeof(T)>::Unsigned
+>;
+
+void tst_QMetaType::underlyingType_data()
+{
+ QTest::addColumn<QMetaType>("source");
+ QTest::addColumn<QMetaType>("underlying");
+
+ QTest::newRow("invalid") << QMetaType() << QMetaType();
+ QTest::newRow("plain") << QMetaType::fromType<isEnumTest_Enum1>()
+ << QMetaType::fromType<getUnderlyingTypeNormalized<isEnumTest_Enum1>>();
+ QTest::newRow("uchar") << QMetaType::fromType<E1>()
+ << QMetaType::fromType<getUnderlyingTypeNormalized<E1>>();
+ QTest::newRow("long") << QMetaType::fromType<E2>()
+ << QMetaType::fromType<getUnderlyingTypeNormalized<E2>>();
+ QTest::newRow("class_ushort") << QMetaType::fromType<E3>()
+ << QMetaType::fromType<getUnderlyingTypeNormalized<E3>>();
+ QTest::newRow("flags_int") << QMetaType::fromType<myflags::Flags1>()
+ << QMetaType::fromType<int>();
+ QTest::newRow("flags_short") << QMetaType::fromType<myflags::Flags2>()
+ << QMetaType::fromType<int>(); // sic, not short!
+}
+
+void tst_QMetaType::underlyingType()
+{
+ QFETCH(QMetaType, source);
+ QFETCH(QMetaType, underlying);
+ QCOMPARE(source.underlyingType(), underlying);
}
void tst_QMetaType::isRegisteredStaticLess_data()
@@ -1524,9 +1921,16 @@ void tst_QMetaType::isRegisteredStaticLess()
{
QFETCH(int, typeId);
QFETCH(bool, registered);
+ ignoreInvalidMetaTypeWarning(typeId);
QCOMPARE(QMetaType(typeId).isRegistered(), registered);
}
+struct NotARegisteredType {};
+void tst_QMetaType::isNotRegistered()
+{
+ QVERIFY(!QMetaType::fromType<NotARegisteredType>().isRegistered());
+}
+
typedef QHash<int, uint> IntUIntHash;
Q_DECLARE_METATYPE(IntUIntHash)
typedef QMap<int, uint> IntUIntMap;
@@ -1792,7 +2196,7 @@ void tst_QMetaType::automaticTemplateRegistration_1()
sp.data()->setObjectName("Test name"); \
QVariant v = QVariant::fromValue(sp); \
QCOMPARE(v.typeName(), #SMARTPOINTER "<" #ELEMENT_TYPE ">"); \
- QVERIFY(QMetaType::typeFlags(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \
+ QVERIFY(QMetaType(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \
SMARTPOINTER < QObject > extractedPtr = FROMVARIANTFUNCTION<QObject>(v); \
QCOMPARE(extractedPtr.data()->objectName(), sp.data()->objectName()); \
}
@@ -1810,7 +2214,7 @@ void tst_QMetaType::automaticTemplateRegistration_1()
sp.data()->setObjectName("Test name"); \
QVariant v = QVariant::fromValue(sp); \
QCOMPARE(v.typeName(), #SMARTPOINTER "<" #ELEMENT_TYPE ">"); \
- QVERIFY(QMetaType::typeFlags(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \
+ QVERIFY(QMetaType(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \
SMARTPOINTER < QObject > extractedPtr = FROMVARIANTFUNCTION<QObject>(v); \
QCOMPARE(extractedPtr.data()->objectName(), sp.data()->objectName()); \
}
@@ -1830,7 +2234,7 @@ void tst_QMetaType::automaticTemplateRegistration_1()
sp.toStrongRef()->setObjectName("Test name"); \
QVariant v = QVariant::fromValue(sp); \
QCOMPARE(v.typeName(), "QWeakPointer<" #ELEMENT_TYPE ">"); \
- QVERIFY(QMetaType::typeFlags(::qMetaTypeId<QWeakPointer < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \
+ QVERIFY(QMetaType(::qMetaTypeId<QWeakPointer < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \
}
TEST_WEAK_SMARTPOINTER(QObject, WeakPointerToQObject)
@@ -1883,38 +2287,39 @@ void tst_QMetaType::saveAndLoadBuiltin()
QFETCH(int, type);
QFETCH(bool, isStreamable);
- void *value = QMetaType::create(type);
+ QMetaType metaType(type);
+ void *value = metaType.create();
QByteArray ba;
QDataStream stream(&ba, QIODevice::ReadWrite);
- QCOMPARE(QMetaType::save(stream, type, value), isStreamable);
+ QCOMPARE(metaType.save(stream, value), isStreamable);
QCOMPARE(stream.status(), QDataStream::Ok);
if (isStreamable) {
- QVERIFY(QMetaType(type).hasRegisteredDataStreamOperators());
- QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false?
+ QVERIFY(metaType.hasRegisteredDataStreamOperators());
+ QVERIFY(metaType.load(stream, value)); // Hmmm, shouldn't it return false?
// std::nullptr_t is nullary: it doesn't actually read anything
if (type != QMetaType::Nullptr)
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
} else {
- QVERIFY(!QMetaType(type).hasRegisteredDataStreamOperators());
+ QVERIFY(!metaType.hasRegisteredDataStreamOperators());
}
stream.device()->seek(0);
stream.resetStatus();
- QCOMPARE(QMetaType::load(stream, type, value), isStreamable);
+ QCOMPARE(metaType.load(stream, value), isStreamable);
QCOMPARE(stream.status(), QDataStream::Ok);
if (isStreamable) {
- QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false?
+ QVERIFY(metaType.load(stream, value)); // Hmmm, shouldn't it return false?
// std::nullptr_t is nullary: it doesn't actually read anything
if (type != QMetaType::Nullptr)
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
}
- QMetaType::destroy(type, value);
+ metaType.destroy(value);
}
struct CustomStreamableType
@@ -1943,25 +2348,27 @@ void tst_QMetaType::saveAndLoadCustom()
t.a = 123;
int id = ::qMetaTypeId<CustomStreamableType>();
+ QMetaType metaType(id);
+
QByteArray ba;
QDataStream stream(&ba, QIODevice::ReadWrite);
- QVERIFY(QMetaType::save(stream, id, &t));
+ QVERIFY(metaType.save(stream, &t));
QCOMPARE(stream.status(), QDataStream::Ok);
CustomStreamableType t2;
t2.a = -1;
- QVERIFY(QMetaType::load(stream, id, &t2)); // Hmmm, shouldn't it return false?
+ QVERIFY(metaType.load(stream, &t2)); // Hmmm, shouldn't it return false?
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
QCOMPARE(t2.a, -1);
stream.device()->seek(0);
stream.resetStatus();
- QVERIFY(QMetaType::load(stream, id, &t2));
+ QVERIFY(metaType.load(stream, &t2));
QCOMPARE(stream.status(), QDataStream::Ok);
QCOMPARE(t2.a, t.a);
- QVERIFY(QMetaType::load(stream, id, &t2)); // Hmmm, shouldn't it return false?
+ QVERIFY(metaType.load(stream, &t2)); // Hmmm, shouldn't it return false?
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
}
@@ -1999,6 +2406,7 @@ void tst_QMetaType::metaObject_data()
QTest::newRow("unknown-type") << int(QMetaType::UnknownType) << static_cast<const QMetaObject *>(0) << false << false << false;
QTest::newRow("QObject") << int(QMetaType::QObjectStar) << &QObject::staticMetaObject << false << false << true;
QTest::newRow("QFile*") << ::qMetaTypeId<QFile*>() << &QFile::staticMetaObject << false << false << true;
+ QTest::newRow("MyObject") << ::qMetaTypeId<MyObject>() << &MyObject::staticMetaObject << false << false << false;
QTest::newRow("MyObject*") << ::qMetaTypeId<MyObject*>() << &MyObject::staticMetaObject << false << false << true;
QTest::newRow("int") << int(QMetaType::Int) << static_cast<const QMetaObject *>(0) << false << false << false;
QTest::newRow("QEasingCurve") << ::qMetaTypeId<QEasingCurve>() << &QEasingCurve::staticMetaObject << true << false << false;
@@ -2021,7 +2429,6 @@ void tst_QMetaType::metaObject()
QFETCH(bool, isGadgetPtr);
QFETCH(bool, isQObjectPtr);
- QCOMPARE(QMetaType::metaObjectForType(type), result);
QMetaType mt(type);
QCOMPARE(mt.metaObject(), result);
QCOMPARE(!!(mt.flags() & QMetaType::IsGadget), isGadget);
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h
index 588e52c4f6..1694e49491 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h
@@ -1,5 +1,5 @@
// Copyright (C) 2022 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include "tst_qmetatype_common.h"
@@ -45,7 +45,9 @@ private:
private slots:
void defined();
+#if QT_CONFIG(thread)
void threadSafety();
+#endif
void namespaces();
void id();
void qMetaTypeId();
@@ -69,12 +71,13 @@ private slots:
void alignOf();
void flags_data();
void flags();
- void flagsStaticLess_data();
- void flagsStaticLess();
+ void flags2_data();
+ void flags2();
void flagsBinaryCompatibility6_0_data();
void flagsBinaryCompatibility6_0();
void construct_data();
void construct();
+ void defaultConstructTrivial_QTBUG_109594();
void typedConstruct();
void constructCopy_data();
void constructCopy();
@@ -86,7 +89,10 @@ private slots:
void isRegistered();
void isRegisteredStaticLess_data();
void isRegisteredStaticLess();
+ void isNotRegistered();
void isEnum();
+ void underlyingType_data();
+ void underlyingType();
void automaticTemplateRegistration_1();
void automaticTemplateRegistration_2(); // defined in tst_qmetatype3.cpp
void saveAndLoadBuiltin_data();
@@ -107,9 +113,22 @@ private slots:
void fromType();
void operatorEq_data();
void operatorEq();
+ void operatorEq2_data();
+ void operatorEq2();
+ void operatorEqAcrossLibs_data();
+ void operatorEqAcrossLibs();
void typesWithInaccessibleDTors();
void voidIsNotUnknown();
void typeNameNormalization();
+ void typeNameInQtPrivate();
+
+ // Tests for deprecated APIs
+#if QT_DEPRECATED_SINCE(6, 0)
+ void testDeprecatedGetters_data() { type_data(); }
+ void testDeprecatedGetters();
+ void testDeprecatedLoadSave_data() { saveAndLoadBuiltin_data(); }
+ void testDeprecatedLoadSave();
+#endif
};
template <typename T>
@@ -257,7 +276,7 @@ Q_DECLARE_METATYPE(CustomMovable);
const QVariant v = QVariant::fromValue(t); \
QByteArray tn = createTypeName(#CONTAINER "<", #__VA_ARGS__); \
const int expectedType = ::qMetaTypeId<CONTAINER< __VA_ARGS__ > >(); \
- const int type = QMetaType::type(tn); \
+ const int type = QMetaType::fromName(tn).id(); \
QCOMPARE(type, expectedType); \
QCOMPARE((QMetaType::fromType<CONTAINER< __VA_ARGS__ >>().id()), expectedType); \
}
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
index 95cc0e7ed8..68bcb53056 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp
@@ -1,8 +1,8 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "tst_qmetatype.h"
-#include "tst_qvariant_common.h"
+#include "tst_qmetatype_libs.h"
#include <QtCore/private/qmetaobjectbuilder_p.h>
@@ -30,6 +30,30 @@ struct ConvertFunctor
}
};
+template<typename T>
+struct OptionalWrapper
+{
+ std::optional<T> operator()(const T& t) const
+ {
+ if (!CustomConvertibleType::s_ok)
+ return std::nullopt;
+
+ return t;
+ }
+};
+
+template<typename From>
+struct ConvertFunctorWithOptional
+{
+ std::optional<CustomConvertibleType> operator()(const From& f) const
+ {
+ if (!CustomConvertibleType::s_ok)
+ return std::nullopt;
+
+ return CustomConvertibleType(QVariant::fromValue(f));
+ }
+};
+
template<typename From, typename To>
bool hasRegisteredConverterFunction()
{
@@ -81,6 +105,7 @@ void customTypeNotYetConvertible()
testCustomTypeNotYetConvertible<QLineF, CustomConvertibleType>();
testCustomTypeNotYetConvertible<QChar, CustomConvertibleType>();
testCustomTypeNotYetConvertible<CustomConvertibleType, CustomConvertibleType2>();
+ testCustomTypeNotYetConvertible<CustomConvertibleType, std::optional<CustomConvertibleType>>();
}
void registerCustomTypeConversions()
@@ -99,20 +124,22 @@ void registerCustomTypeConversions()
QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QLine>(&CustomConvertibleType::convert<QLine>)));
QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QLineF>(&CustomConvertibleType::convertOk<QLineF>)));
QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QChar>(&CustomConvertibleType::convert<QChar>)));
- QVERIFY((QMetaType::registerConverter<QString, CustomConvertibleType>(ConvertFunctor<QString>())));
+ QVERIFY((QMetaType::registerConverter<QString, CustomConvertibleType>(ConvertFunctorWithOptional<QString>())));
QVERIFY((QMetaType::registerConverter<bool, CustomConvertibleType>(ConvertFunctor<bool>())));
- QVERIFY((QMetaType::registerConverter<int, CustomConvertibleType>(ConvertFunctor<int>())));
+ QVERIFY((QMetaType::registerConverter<int, CustomConvertibleType>(ConvertFunctorWithOptional<int>())));
QVERIFY((QMetaType::registerConverter<double, CustomConvertibleType>(ConvertFunctor<double>())));
- QVERIFY((QMetaType::registerConverter<float, CustomConvertibleType>(ConvertFunctor<float>())));
+ QVERIFY((QMetaType::registerConverter<float, CustomConvertibleType>(ConvertFunctorWithOptional<float>())));
QVERIFY((QMetaType::registerConverter<QRect, CustomConvertibleType>(ConvertFunctor<QRect>())));
- QVERIFY((QMetaType::registerConverter<QRectF, CustomConvertibleType>(ConvertFunctor<QRectF>())));
+ QVERIFY((QMetaType::registerConverter<QRectF, CustomConvertibleType>(ConvertFunctorWithOptional<QRectF>())));
QVERIFY((QMetaType::registerConverter<QPoint, CustomConvertibleType>(ConvertFunctor<QPoint>())));
- QVERIFY((QMetaType::registerConverter<QPointF, CustomConvertibleType>(ConvertFunctor<QPointF>())));
+ QVERIFY((QMetaType::registerConverter<QPointF, CustomConvertibleType>(ConvertFunctorWithOptional<QPointF>())));
QVERIFY((QMetaType::registerConverter<QSize, CustomConvertibleType>(ConvertFunctor<QSize>())));
- QVERIFY((QMetaType::registerConverter<QSizeF, CustomConvertibleType>(ConvertFunctor<QSizeF>())));
+ QVERIFY((QMetaType::registerConverter<QSizeF, CustomConvertibleType>(ConvertFunctorWithOptional<QSizeF>())));
QVERIFY((QMetaType::registerConverter<QLine, CustomConvertibleType>(ConvertFunctor<QLine>())));
- QVERIFY((QMetaType::registerConverter<QLineF, CustomConvertibleType>(ConvertFunctor<QLineF>())));
+ QVERIFY((QMetaType::registerConverter<QLineF, CustomConvertibleType>(ConvertFunctorWithOptional<QLineF>())));
QVERIFY((QMetaType::registerConverter<QChar, CustomConvertibleType>(ConvertFunctor<QChar>())));
+ QVERIFY((QMetaType::registerConverter<CustomConvertibleType, std::optional<CustomConvertibleType>>(OptionalWrapper<CustomConvertibleType>())));
+
QVERIFY((QMetaType::registerConverter<CustomConvertibleType, CustomConvertibleType2>()));
QTest::ignoreMessage(QtWarningMsg, "Type conversion already registered from type CustomConvertibleType to type CustomConvertibleType2");
QVERIFY((!QMetaType::registerConverter<CustomConvertibleType, CustomConvertibleType2>()));
@@ -171,7 +198,7 @@ void tst_QMetaType::convertCustomType()
QCOMPARE(v.toString(), ok ? testQString : QString());
QCOMPARE(v.value<QString>(), ok ? testQString : QString());
QVERIFY(CustomConvertibleType::s_value.canConvert<CustomConvertibleType>());
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toString()), testQString);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toString()), ok ? testQString : QString());
QFETCH(bool, testBool);
CustomConvertibleType::s_value = testBool;
@@ -183,7 +210,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testInt;
QCOMPARE(v.toInt(), ok ? testInt : 0);
QCOMPARE(v.value<int>(), ok ? testInt : 0);
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toInt()), testInt);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toInt()), ok ? testInt : 0);
QFETCH(double, testDouble);
CustomConvertibleType::s_value = testDouble;
@@ -195,7 +222,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testFloat;
QCOMPARE(v.toFloat(), ok ? testFloat : 0.0);
QCOMPARE(v.value<float>(), ok ? testFloat : 0.0);
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toFloat()), testFloat);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toFloat()), ok ? testFloat : 0);
QFETCH(QRect, testQRect);
CustomConvertibleType::s_value = testQRect;
@@ -207,7 +234,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testQRectF;
QCOMPARE(v.toRectF(), ok ? testQRectF : QRectF());
QCOMPARE(v.value<QRectF>(), ok ? testQRectF : QRectF());
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toRectF()), testQRectF);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toRectF()), ok ? testQRectF : QRectF());
QFETCH(QPoint, testQPoint);
CustomConvertibleType::s_value = testQPoint;
@@ -219,7 +246,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testQPointF;
QCOMPARE(v.toPointF(), ok ? testQPointF : QPointF());
QCOMPARE(v.value<QPointF>(), ok ? testQPointF : QPointF());
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toPointF()), testQPointF);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toPointF()), ok ? testQPointF : QPointF());
QFETCH(QSize, testQSize);
CustomConvertibleType::s_value = testQSize;
@@ -231,7 +258,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testQSizeF;
QCOMPARE(v.toSizeF(), ok ? testQSizeF : QSizeF());
QCOMPARE(v.value<QSizeF>(), ok ? testQSizeF : QSizeF());
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toSizeF()), testQSizeF);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toSizeF()), ok ? testQSizeF : QSizeF());
QFETCH(QLine, testQLine);
CustomConvertibleType::s_value = testQLine;
@@ -243,7 +270,7 @@ void tst_QMetaType::convertCustomType()
CustomConvertibleType::s_value = testQLineF;
QCOMPARE(v.toLineF(), ok ? testQLineF : QLineF());
QCOMPARE(v.value<QLineF>(), ok ? testQLineF : QLineF());
- QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toLineF()), testQLineF);
+ QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toLineF()), ok ? testQLineF : QLineF());
QFETCH(QChar, testQChar);
CustomConvertibleType::s_value = testQChar;
@@ -252,9 +279,21 @@ void tst_QMetaType::convertCustomType()
QFETCH(CustomConvertibleType, testCustom);
v = QVariant::fromValue(testCustom);
- QVERIFY(v.canConvert(::qMetaTypeId<CustomConvertibleType2>()));
+ QVERIFY(v.canConvert(QMetaType(::qMetaTypeId<CustomConvertibleType2>())));
QCOMPARE(v.value<CustomConvertibleType2>().m_foo, testCustom.m_foo);
+ // Check that converters that actually convert to std::optional<T> are not
+ // taken to indicate success or failure of the conversion. In these cases,
+ // the conversion must always succeed, even if the converter has returned a
+ // nullopt.
+ v = QVariant::fromValue(testCustom);
+ QVERIFY(v.canConvert(QMetaType::fromType<std::optional<CustomConvertibleType>>()));
+ QVERIFY(v.convert(QMetaType::fromType<std::optional<CustomConvertibleType>>()));
+ QCOMPARE(v.value<std::optional<CustomConvertibleType>>().has_value(), ok);
+ if (ok) {
+ QCOMPARE(v.value<std::optional<CustomConvertibleType>>().value().m_foo, testCustom.m_foo);
+ }
+
QFETCH(DerivedGadgetType, testDerived);
v = QVariant::fromValue(testDerived);
QCOMPARE(v.metaType(), QMetaType::fromType<DerivedGadgetType>());
@@ -412,6 +451,124 @@ void tst_QMetaType::operatorEq()
QCOMPARE(typeB == typeA, eq);
QCOMPARE(typeA != typeB, !eq);
QCOMPARE(typeB != typeA, !eq);
+
+#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
+ // for built-in types or locally-defined types, this must also hold true
+ if (eq)
+ QCOMPARE(typeA.iface(), typeB.iface());
+#endif
+}
+
+void tst_QMetaType::operatorEq2_data()
+{
+ create_data();
+}
+
+void tst_QMetaType::operatorEq2()
+{
+ QFETCH(int, type);
+ QMetaType fromType1, fromType2;
+ QMetaType fromId1(type), fromId2(type);
+
+ switch (type) {
+ case QMetaType::UnknownType:
+ break;
+#define GET_METATYPE_FROM_TYPE(MetaTypeName, MetaTypeId, RealType) \
+ case QMetaType::MetaTypeName: \
+ fromType1 = QMetaType::fromType<RealType>(); \
+ fromType2 = QMetaType::fromType<RealType>(); \
+ break;
+FOR_EACH_CORE_METATYPE(GET_METATYPE_FROM_TYPE)
+#undef GET_METATYPE_FROM_TYPE
+ }
+
+ // sanity check
+ QCOMPARE(fromId1.id(), type);
+ QCOMPARE(fromId2.id(), type);
+
+ // confirm that they're all equal
+ QCOMPARE(fromId1, fromId2);
+ QCOMPARE(fromType1, fromType2);
+ QCOMPARE(fromType1, fromId1);
+ QCOMPARE(fromType2, fromId2);
+
+#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
+ // for built-in types (other than void), this must be true
+ if (type != QMetaType::Void) {
+ QCOMPARE(fromType1.iface(), fromId1.iface());
+ QCOMPARE(fromType2.iface(), fromId1.iface());
+ }
+#endif
+}
+
+#define DECLARE_LIB_FUNCTION(TYPE, ID) \
+ Q_DECL_IMPORT QMetaType metatype_ ## TYPE();
+namespace Lib1 { FOR_EACH_METATYPE_LIBS(DECLARE_LIB_FUNCTION) }
+namespace Lib2 { FOR_EACH_METATYPE_LIBS(DECLARE_LIB_FUNCTION) }
+#undef DECLARE_LIB_FUNCTION
+
+using LibMetatypeFunction = QMetaType (*)();
+void tst_QMetaType::operatorEqAcrossLibs_data()
+{
+ QTest::addColumn<int>("builtinTypeId");
+ QTest::addColumn<QMetaType>("localType");
+ QTest::addColumn<LibMetatypeFunction>("lib1Function");
+ QTest::addColumn<LibMetatypeFunction>("lib2Function");
+
+#define ADD_ROW(TYPE, ID) \
+ QTest::addRow(QT_STRINGIFY(TYPE)) << int(ID) \
+ << QMetaType::fromType<TYPE>() \
+ << &Lib1::metatype_ ## TYPE \
+ << &Lib2::metatype_ ## TYPE;
+FOR_EACH_METATYPE_LIBS(ADD_ROW)
+#undef ADD_ROW
+}
+
+void tst_QMetaType::operatorEqAcrossLibs()
+{
+ QFETCH(int, builtinTypeId);
+ QFETCH(QMetaType, localType);
+ QFETCH(LibMetatypeFunction, lib1Function);
+ QFETCH(LibMetatypeFunction, lib2Function);
+
+ QMetaType lib1Type = lib1Function();
+ QMetaType lib2Type = lib2Function();
+
+ const QtPrivate::QMetaTypeInterface *localIface = localType.iface();
+ const QtPrivate::QMetaTypeInterface *lib1Iface = lib1Type.iface();
+ const QtPrivate::QMetaTypeInterface *lib2Iface = lib2Type.iface();
+
+ // DO THIS FIRST:
+ // if this isn't a built-in type, then the QMetaTypeInterface::typeId is
+ // initially set to 0
+ QCOMPARE(lib1Type, lib2Type);
+
+ int actualTypeId = localType.id();
+ bool builtinTypeExpected = builtinTypeId != QMetaType::UnknownType;
+ bool builtinTypeActually = actualTypeId < QMetaType::User;
+
+ qDebug() << "QMetaType for type" << QByteArray(localType.name())
+ << "(type ID" << (actualTypeId >= 0x1000 ? Qt::hex : Qt::dec) << actualTypeId << ')'
+ << (builtinTypeActually ? "IS" : "is NOT") << "a built-in type;"
+ << "local interface:" << static_cast<const void *>(localIface)
+ << "lib1 interface:" << static_cast<const void *>(lib1Iface)
+ << "lib2 interface:" << static_cast<const void *>(lib2Iface);
+
+ QCOMPARE(builtinTypeActually, builtinTypeExpected);
+ QCOMPARE(lib1Type.id(), actualTypeId);
+ QCOMPARE(lib2Type.id(), actualTypeId);
+ QCOMPARE(QByteArray(lib1Type.name()), QByteArray(localType.name()));
+ QCOMPARE(QByteArray(lib2Type.name()), QByteArray(localType.name()));
+ QCOMPARE(lib1Type, localType);
+ QCOMPARE(lib2Type, localType);
+
+#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
+ if (actualTypeId < QMetaType::FirstGuiType && actualTypeId != QMetaType::Void) {
+ // for built-in QtCore types, we expect the interfaces to be the same too
+ QCOMPARE(lib1Iface, localIface);
+ QCOMPARE(lib2Iface, localIface);
+ }
+#endif
}
class WithPrivateDTor {
@@ -483,6 +640,83 @@ void tst_QMetaType::typeNameNormalization()
}
}
+QT_BEGIN_NAMESPACE
+namespace QtPrivate { struct tst_QMetaType_TestType {}; }
+QT_END_NAMESPACE
+
+void tst_QMetaType::typeNameInQtPrivate()
+{
+ using T = QT_PREPEND_NAMESPACE(QtPrivate::tst_QMetaType_TestType);
+
+ // some compilers (GCC) are known to suppress the namespace prefix if the
+ // type and the function where it is expanded on are on the same namespace
+ static constexpr char expectedName[] = "QtPrivate::tst_QMetaType_TestType";
+ QMetaType mt = QMetaType::fromType<T>();
+ QCOMPARE(mt.name(), expectedName);
+}
+
+#if QT_DEPRECATED_SINCE(6, 0)
+void tst_QMetaType::testDeprecatedGetters()
+{
+ QFETCH(int, aType);
+ QFETCH(QByteArray, aTypeName);
+
+ if (aType >= QMetaType::FirstWidgetsType)
+ QSKIP("The test doesn't link against QtWidgets.");
+
+ // QMetaType::type("name") -> QMetaType::fromName("name").id()
+ QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::type(aTypeName),
+ QMetaType::fromName(aTypeName).id());)
+ // QMetaType::typeName(int) -> QMetaType(int).name()
+ QT_IGNORE_DEPRECATIONS(QCOMPARE(QLatin1String(QMetaType::typeName(aType)),
+ QLatin1String(QMetaType(aType).name()));)
+ // QMetaType::typeFlags(int) -> QMetaType(int).flags()
+ QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::typeFlags(aType),
+ QMetaType(aType).flags());)
+ // QMetaType::metaObjectForType(int) -> QMetaType(int).metaObject()
+ QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::metaObjectForType(aType),
+ QMetaType(aType).metaObject());)
+}
+
+void tst_QMetaType::testDeprecatedLoadSave()
+{
+ QFETCH(int, type);
+ QFETCH(bool, isStreamable);
+
+ if (!isStreamable)
+ return;
+
+ QMetaType metaType(type);
+ void *value = metaType.create();
+ auto cleanup = qScopeGuard([&metaType, value]() {
+ metaType.destroy(value);
+ });
+
+ QByteArray ba;
+ QDataStream stream(&ba, QIODevice::ReadWrite);
+
+ // Write using deprecated API
+ QT_IGNORE_DEPRECATIONS(QVERIFY(QMetaType::save(stream, type, value));)
+ QCOMPARE(stream.status(), QDataStream::Ok);
+
+ // Read using non-deprecated API
+ stream.device()->seek(0);
+ QVERIFY(metaType.load(stream, value));
+ QCOMPARE(stream.status(), QDataStream::Ok);
+
+ // Write using non-deprecated API
+ stream.device()->seek(0);
+ ba.clear();
+ QVERIFY(metaType.save(stream, value));
+ QCOMPARE(stream.status(), QDataStream::Ok);
+
+ // Read using deprecated API
+ stream.device()->seek(0);
+ QT_IGNORE_DEPRECATIONS(QVERIFY(QMetaType::load(stream, type, value));)
+ QCOMPARE(stream.status(), QDataStream::Ok);
+}
+#endif // QT_DEPRECATED_SINCE(6, 0)
+
// Compile-time test, it should be possible to register function pointer types
class Undefined;
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp
index 95577e16da..779044589c 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "tst_qmetatype.h"
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h
index 3324cb5b32..ea59b5cd02 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h
@@ -1,5 +1,5 @@
// Copyright (C) 2022 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
// Used by both tst_qmetatype and tst_qsettings
@@ -129,6 +129,9 @@ template<> struct TestValueFactory<QMetaType::UChar> {
template<> struct TestValueFactory<QMetaType::Float> {
static float *create() { return new float(FLT_MIN); }
};
+template<> struct TestValueFactory<QMetaType::Float16> {
+ static auto create() { return new qfloat16(std::numeric_limits<qfloat16>::min()); }
+};
template<> struct TestValueFactory<QMetaType::QObjectStar> {
static QObject * *create() { return new QObject *(0); }
};
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h
new file mode 100644
index 0000000000..673fc9083c
--- /dev/null
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h
@@ -0,0 +1,24 @@
+// Copyright (C) 2022 Intel Corporation
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#ifndef TST_QMETATYPE_LIBS_H
+#define TST_QMETATYPE_LIBS_H
+
+#include <qmetatype.h>
+
+#include <stdlib.h> // for div_t
+
+// void: builtin metatype, special
+// int: builtin metatype, primitive type
+// QString: builtin metatype, class
+// QCollator: not builtin, class, Q_CORE_EXPORT
+// div_t: not builtin, class, no export
+#define FOR_EACH_METATYPE_LIBS(F) \
+ F(void, QMetaType::Void) \
+ F(int, QMetaType::Int) \
+ F(QString, QMetaType::QString) \
+ F(QCollator, QMetaType::UnknownType) \
+ F(div_t, QMetaType::UnknownType) \
+ /**/
+
+#endif // TST_QMETATYPE_LIBS_H
diff --git a/tests/auto/corelib/kernel/qmimedata/CMakeLists.txt b/tests/auto/corelib/kernel/qmimedata/CMakeLists.txt
index ec5505d3e0..ba1d5e12cd 100644
--- a/tests/auto/corelib/kernel/qmimedata/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qmimedata/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qmimedata.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qmimedata Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qmimedata LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qmimedata
SOURCES
tst_qmimedata.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::Gui
)
diff --git a/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp b/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp
index 16d5c5308e..c9c8734353 100644
--- a/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp
+++ b/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -71,13 +71,20 @@ void tst_QMimeData::data() const
// set text, verify
mimeData.setData("text/plain", "pirates");
QCOMPARE(mimeData.data("text/plain"), QByteArray("pirates"));
- QCOMPARE(mimeData.data("text/html").length(), 0);
+ QCOMPARE(mimeData.data("text/html").size(), 0);
+ QCOMPARE(mimeData.data("text/markdown").size(), 0);
// html time
mimeData.setData("text/html", "ninjas");
QCOMPARE(mimeData.data("text/html"), QByteArray("ninjas"));
QCOMPARE(mimeData.data("text/plain"), QByteArray("pirates")); // make sure text not damaged
QCOMPARE(mimeData.data("text/html"), mimeData.html().toLatin1());
+
+ // markdown time
+ mimeData.setData("text/markdown", "vikings");
+ QCOMPARE(mimeData.data("text/markdown"), QByteArray("vikings"));
+ QCOMPARE(mimeData.data("text/html"), QByteArray("ninjas"));
+ QCOMPARE(mimeData.data("text/plain"), QByteArray("pirates"));
}
void tst_QMimeData::formats() const
@@ -92,6 +99,10 @@ void tst_QMimeData::formats() const
mimeData.setData("text/html", "ninjas");
QCOMPARE(mimeData.formats(), QStringList() << "text/plain" << "text/html");
+ // set markdown, verify
+ mimeData.setData("text/markdown", "vikings");
+ QCOMPARE(mimeData.formats(), QStringList() << "text/plain" << "text/html" << "text/markdown");
+
// clear, verify
mimeData.clear();
QCOMPARE(mimeData.formats(), QStringList());
@@ -299,7 +310,8 @@ void tst_QMimeData::setUrls() const
QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n"));
// test and verify that setData doesn't corrupt url content
- foreach (const QString &format, mimeData.formats()) {
+ const auto allFormats = mimeData.formats();
+ for (const QString &format : allFormats) {
QVariant before = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray));
mimeData.setData(format, mimeData.data(format));
QVariant after = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray));
diff --git a/tests/auto/corelib/kernel/qobject/CMakeLists.txt b/tests/auto/corelib/kernel/qobject/CMakeLists.txt
index a231ef985f..46b8e2d238 100644
--- a/tests/auto/corelib/kernel/qobject/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qobject/CMakeLists.txt
@@ -1,14 +1,19 @@
-# Generated from qobject.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qobject Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qobject LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qobject
SOURCES
tst_qobject.cpp
- DEFINES
- QT_DISABLE_DEPRECATED_BEFORE=0
LIBRARIES
Qt::CorePrivate
Qt::Network
@@ -18,4 +23,4 @@ qt_internal_add_test(tst_qobject
## Scopes:
#####################################################################
add_subdirectory(signalbug)
-add_dependencies(tst_qobject signalbug_helper) # special case
+add_dependencies(tst_qobject signalbug_helper)
diff --git a/tests/auto/corelib/kernel/qobject/signalbug/CMakeLists.txt b/tests/auto/corelib/kernel/qobject/signalbug/CMakeLists.txt
index b479ada78e..aefa1554b6 100644
--- a/tests/auto/corelib/kernel/qobject/signalbug/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qobject/signalbug/CMakeLists.txt
@@ -1,4 +1,5 @@
-# Generated from signalbug.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## signalbug_helper Binary:
diff --git a/tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp b/tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp
index 044f3ef230..bb8d7984f4 100644
--- a/tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp
+++ b/tests/auto/corelib/kernel/qobject/signalbug/signalbug.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "signalbug.h"
diff --git a/tests/auto/corelib/kernel/qobject/signalbug/signalbug.h b/tests/auto/corelib/kernel/qobject/signalbug/signalbug.h
index c1786e2cbc..ac124c2a1c 100644
--- a/tests/auto/corelib/kernel/qobject/signalbug/signalbug.h
+++ b/tests/auto/corelib/kernel/qobject/signalbug/signalbug.h
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#ifndef SIGNAL_BUG_H
#define SIGNAL_BUG_H
diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
index 2e0895dbb4..6c387fde96 100644
--- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
+++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
@@ -1,7 +1,7 @@
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2020 Olivier Goffart <ogoffart@woboq.com>
// Copyright (C) 2021 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
// This test actually wants to practice narrowing conditions, so never define this.
#ifdef QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
@@ -39,6 +39,8 @@
#include <math.h>
+using namespace Qt::StringLiterals;
+
class tst_QObject : public QObject
{
Q_OBJECT
@@ -58,6 +60,7 @@ private slots:
void connectNotify_connectSlotsByName();
void connectDisconnectNotify_shadowing();
void connectReferenceToIncompleteTypes();
+ void connectAutoQueuedIncomplete();
void emitInDefinedOrder();
void customTypes();
void streamCustomTypes();
@@ -79,7 +82,9 @@ private slots:
void signalBlocking();
void blockingQueuedConnection();
void childEvents();
+ void parentEvents();
void installEventFilter();
+ void installEventFilterOrder();
void deleteSelfInSlot();
void disconnectSelfInSlotAndDeleteAfterEmit();
void dumpObjectInfo();
@@ -147,6 +152,8 @@ private slots:
void singleShotConnection();
void objectNameBinding();
void emitToDestroyedClass();
+ void declarativeData();
+ void asyncCallbackHelper();
};
struct QObjectCreatedOnShutdown
@@ -504,6 +511,12 @@ void tst_QObject::qobject_castTemplate()
QVERIFY(!::qobject_cast<ReceiverObject*>(o.data()));
}
+class DerivedObj : public QObject {
+ Q_OBJECT
+public:
+ using QObject::QObject;
+};
+
void tst_QObject::findChildren()
{
QObject o;
@@ -516,6 +529,10 @@ void tst_QObject::findChildren()
QTimer t1(&o);
QTimer t121(&o12);
QTimer emptyname(&o);
+ QObject oo;
+ QObject o21(&oo);
+ QObject o22(&oo);
+ QObject o23(&oo);
Q_SET_OBJECT_NAME(o);
Q_SET_OBJECT_NAME(o1);
@@ -526,6 +543,13 @@ void tst_QObject::findChildren()
Q_SET_OBJECT_NAME(t1);
Q_SET_OBJECT_NAME(t121);
emptyname.setObjectName("");
+ Q_SET_OBJECT_NAME(oo);
+ const QUtf8StringView utf8_name = u8"utf8 ⁎ obj";
+ o21.setObjectName(utf8_name);
+ const QStringView utf16_name = u"utf16 ⁎ obj";
+ o22.setObjectName(utf16_name);
+ constexpr QLatin1StringView L1_name("L1 ⁎ obj");
+ o23.setObjectName(L1_name);
QObject *op = nullptr;
@@ -556,6 +580,27 @@ void tst_QObject::findChildren()
op = o.findChild<QObject*>("o1");
QCOMPARE(op, &o1);
+ op = oo.findChild<QObject*>(utf8_name);
+ QCOMPARE(op, &o21);
+ op = oo.findChild<QObject*>(utf8_name.chopped(1));
+ QCOMPARE(op, nullptr);
+ const QUtf8StringView utf8_name_with_trailing_data = u8"utf8 ⁎ obj_data";
+ op = oo.findChild<QObject*>(utf8_name_with_trailing_data.chopped(5));
+ QCOMPARE(op, &o21);
+ op = oo.findChild<QObject*>(utf16_name);
+ QCOMPARE(op, &o22);
+ op = oo.findChild<QObject*>(utf16_name.chopped(1));
+ QCOMPARE(op, nullptr);
+ const QStringView utf16_name_with_trailing_data = u"utf16 ⁎ obj_data";
+ op = oo.findChild<QObject*>(utf16_name_with_trailing_data.chopped(5));
+ QCOMPARE(op, &o22);
+ op = oo.findChild<QObject*>(L1_name);
+ QCOMPARE(op, &o23);
+ op = oo.findChild<QObject*>(L1_name.chopped(1));
+ QCOMPARE(op, nullptr);
+ op = oo.findChild<QObject*>((L1_name + "_data"_L1).chopped(5));
+ QCOMPARE(op, &o23);
+
QList<QObject*> l;
QList<QTimer*> tl;
@@ -731,7 +776,20 @@ void tst_QObject::findChildren()
l = o.findChildren<QObject*>(QRegularExpression("^harry$"), Qt::FindDirectChildrenOnly);
QCOMPARE(l.size(), 0);
+ DerivedObj dr1(&o111);
+ DerivedObj dr2(&o111);
+ Q_SET_OBJECT_NAME(dr1);
+ Q_SET_OBJECT_NAME(dr2);
+
// empty and null string check
+ op = o.findChild<QObject*>(Qt::FindDirectChildrenOnly);
+ QCOMPARE(op, &o1);
+ op = o.findChild<QTimer*>(Qt::FindDirectChildrenOnly);
+ QCOMPARE(op, &t1);
+ op = o.findChild<DerivedObj*>(Qt::FindDirectChildrenOnly);
+ QCOMPARE(op, nullptr);
+ op = o.findChild<DerivedObj*>(Qt::FindChildrenRecursively);
+ QCOMPARE(op, &dr1);
op = o.findChild<QObject*>(QString(), Qt::FindDirectChildrenOnly);
QCOMPARE(op, &o1);
op = o.findChild<QObject*>("", Qt::FindDirectChildrenOnly);
@@ -892,6 +950,42 @@ void tst_QObject::connectReferenceToIncompleteTypes() {
QVERIFY(connection);
}
+struct Incomplete2;
+class QObjectWithIncomplete2 : public QObject {
+ Q_OBJECT
+
+public:
+ QObjectWithIncomplete2(QObject *parent=nullptr) : QObject(parent) {}
+signals:
+ void signalWithIncomplete(Incomplete2 *ptr);
+public slots:
+ void slotWithIncomplete(Incomplete2 *) { calledSlot = true; }
+ void run() { Q_EMIT signalWithIncomplete(nullptr); }
+public:
+ bool calledSlot = false;
+};
+
+void tst_QObject::connectAutoQueuedIncomplete()
+{
+ auto objectWithIncomplete1 = new QObjectWithIncomplete2();
+ auto objectWithIncomplete2 = new QObjectWithIncomplete2();
+ auto t = new QThread(this);
+ auto cleanup = qScopeGuard([&](){
+ t->quit();
+ QVERIFY(t->wait());
+ delete objectWithIncomplete1;
+ delete objectWithIncomplete2;
+ });
+
+ t->start();
+ objectWithIncomplete2->moveToThread(t);
+
+ connect(objectWithIncomplete2, &QObjectWithIncomplete2::signalWithIncomplete,
+ objectWithIncomplete1, &QObjectWithIncomplete2::slotWithIncomplete);
+ QMetaObject::invokeMethod(objectWithIncomplete2, "run", Qt::QueuedConnection);
+ QTRY_VERIFY(objectWithIncomplete1->calledSlot);
+}
+
static void connectDisconnectNotifyTestSlot() {}
void tst_QObject::connectDisconnectNotifyPMF()
@@ -959,7 +1053,7 @@ void tst_QObject::disconnectNotify_receiverDestroyed()
QVERIFY(QObject::connect((SenderObject *)&s, SIGNAL(signal1()),
(ReceiverObject *)&r, SLOT(slot1())));
}
- QCOMPARE(s.disconnectedSignals.count(), 1);
+ QCOMPARE(s.disconnectedSignals.size(), 1);
QCOMPARE(s.disconnectedSignals.at(0), QMetaMethod::fromSignal(&SenderObject::signal1));
s.disconnectedSignals.clear();
@@ -970,7 +1064,7 @@ void tst_QObject::disconnectNotify_receiverDestroyed()
(ReceiverObject *)&r, SLOT(slot3())));
}
- QCOMPARE(s.disconnectedSignals.count(), 1);
+ QCOMPARE(s.disconnectedSignals.size(), 1);
QCOMPARE(s.disconnectedSignals.at(0), QMetaMethod::fromSignal(&SenderObject::signal3));
s.disconnectedSignals.clear();
@@ -980,7 +1074,7 @@ void tst_QObject::disconnectNotify_receiverDestroyed()
QVERIFY(QObject::connect((SenderObject *)&s, SIGNAL(destroyed()), (ReceiverObject *)&r, SLOT(slot3())));
}
- QCOMPARE(s.disconnectedSignals.count(), 1);
+ QCOMPARE(s.disconnectedSignals.size(), 1);
QCOMPARE(s.disconnectedSignals.at(0), QMetaMethod::fromSignal(&QObject::destroyed));
}
@@ -995,10 +1089,10 @@ void tst_QObject::disconnectNotify_metaObjConnection()
QVERIFY(c);
QVERIFY(QObject::disconnect(c));
- QCOMPARE(s.disconnectedSignals.count(), 1);
+ QCOMPARE(s.disconnectedSignals.size(), 1);
QCOMPARE(s.disconnectedSignals.at(0), QMetaMethod::fromSignal(&SenderObject::signal1));
- QCOMPARE(s.disconnectedSignals.count(), 1);
+ QCOMPARE(s.disconnectedSignals.size(), 1);
}
}
@@ -1480,8 +1574,7 @@ void tst_QObject::customTypes()
QCOMPARE(checker.received.value(), t1.value());
checker.received = t0;
- int idx = qRegisterMetaType<CustomType>("CustomType");
- QCOMPARE(QMetaType::type("CustomType"), idx);
+ qRegisterMetaType<CustomType>();
checker.disconnect();
connect(&checker, SIGNAL(signal1(CustomType)), &checker, SLOT(slot1(CustomType)),
@@ -1494,11 +1587,6 @@ void tst_QObject::customTypes()
QCoreApplication::processEvents();
QCOMPARE(checker.received.value(), t2.value());
QCOMPARE(instanceCount, 4);
-
- QVERIFY(QMetaType::isRegistered(idx));
- QCOMPARE(qRegisterMetaType<CustomType>("CustomType"), idx);
- QCOMPARE(QMetaType::type("CustomType"), idx);
- QVERIFY(QMetaType::isRegistered(idx));
}
QCOMPARE(instanceCount, 3);
}
@@ -1507,13 +1595,15 @@ void tst_QObject::streamCustomTypes()
{
QByteArray ba;
- int idx = qRegisterMetaType<CustomType>("CustomType");
+ qRegisterMetaType<CustomType>();
+
+ QMetaType metaType = QMetaType::fromType<CustomType>();
{
CustomType t1(1, 2, 3);
QCOMPARE(instanceCount, 1);
QDataStream stream(&ba, (QIODevice::OpenMode)QIODevice::WriteOnly);
- QMetaType::save(stream, idx, &t1);
+ metaType.save(stream, &t1);
}
QCOMPARE(instanceCount, 0);
@@ -1522,7 +1612,7 @@ void tst_QObject::streamCustomTypes()
CustomType t2;
QCOMPARE(instanceCount, 1);
QDataStream stream(&ba, (QIODevice::OpenMode)QIODevice::ReadOnly);
- QMetaType::load(stream, idx, &t2);
+ metaType.load(stream, &t2);
QCOMPARE(instanceCount, 1);
QCOMPARE(t2.i1, 1);
QCOMPARE(t2.i2, 2);
@@ -1781,13 +1871,15 @@ void tst_QObject::moveToThread()
QObject *child = new QObject(object);
QCOMPARE(object->thread(), currentThread);
QCOMPARE(child->thread(), currentThread);
- object->moveToThread(0);
+ QVERIFY(object->moveToThread(nullptr));
QCOMPARE(object->thread(), (QThread *)0);
QCOMPARE(child->thread(), (QThread *)0);
- object->moveToThread(currentThread);
+ QVERIFY(object->moveToThread(currentThread));
QCOMPARE(object->thread(), currentThread);
QCOMPARE(child->thread(), currentThread);
- object->moveToThread(0);
+ QTest::ignoreMessage(QtWarningMsg, "QObject::moveToThread: Cannot move objects with a parent");
+ QVERIFY(!child->moveToThread(nullptr));
+ QVERIFY(object->moveToThread(nullptr));
QCOMPARE(object->thread(), (QThread *)0);
QCOMPARE(child->thread(), (QThread *)0);
// can delete an object with no thread anywhere
@@ -1958,7 +2050,7 @@ void tst_QObject::property()
const int idx = mo->indexOfProperty("variant");
QVERIFY(idx != -1);
- QCOMPARE(QMetaType::Type(mo->property(idx).type()), QMetaType::QVariant);
+ QCOMPARE(mo->property(idx).userType(), QMetaType::QVariant);
QCOMPARE(object.property("variant"), QVariant());
QVariant variant1(42);
QVariant variant2("string");
@@ -1977,7 +2069,7 @@ void tst_QObject::property()
QVERIFY(!property.isEnumType());
QCOMPARE(property.typeName(), "CustomType*");
qRegisterMetaType<CustomType*>();
- QCOMPARE(property.type(), QVariant::UserType);
+ QCOMPARE_GE(property.typeId(), QMetaType::User);
QCOMPARE(property.userType(), qMetaTypeId<CustomType*>());
CustomType *customPointer = nullptr;
@@ -1992,7 +2084,7 @@ void tst_QObject::property()
property = mo->property(mo->indexOfProperty("custom"));
QVERIFY(property.isWritable());
QCOMPARE(property.typeName(), "CustomType*");
- QCOMPARE(property.type(), QVariant::UserType);
+ QCOMPARE_GE(property.typeId(), QMetaType::User);
QCOMPARE(property.userType(), qMetaTypeId<CustomType*>());
QVERIFY(object.setProperty("custom", customVariant));
@@ -2022,13 +2114,13 @@ void tst_QObject::property()
QCOMPARE(object.property("priority").toInt(), 0);
// now it's registered, so it works as expected
- int priorityMetaTypeId = qRegisterMetaType<PropertyObject::Priority>("PropertyObject::Priority");
+ int priorityMetaTypeId = qRegisterMetaType<PropertyObject::Priority>();
QVERIFY(mo->indexOfProperty("priority") != -1);
property = mo->property(mo->indexOfProperty("priority"));
QVERIFY(property.isEnumType());
QCOMPARE(property.typeName(), "PropertyObject::Priority");
- QCOMPARE(property.type(), QVariant::UserType);
+ QCOMPARE_GE(property.typeId(), QMetaType::User);
QCOMPARE(property.userType(), priorityMetaTypeId);
var = object.property("priority");
@@ -2051,7 +2143,7 @@ void tst_QObject::property()
object.setProperty("priority", var);
QCOMPARE(qvariant_cast<PropertyObject::Priority>(object.property("priority")), PropertyObject::High);
- qRegisterMetaType<CustomString>("CustomString");
+ qRegisterMetaType<CustomString>();
QVERIFY(mo->indexOfProperty("customString") != -1);
QCOMPARE(object.property("customString").toString(), QString());
object.setCustomString("String1");
@@ -2124,18 +2216,18 @@ void tst_QObject::metamethod()
QVERIFY(!(m.attributes() & QMetaMethod::Compatibility));
m = mobj->method(mobj->indexOfMethod("invoke1()"));
- QCOMPARE(m.parameterNames().count(), 0);
- QCOMPARE(m.parameterTypes().count(), 0);
+ QCOMPARE(m.parameterNames().size(), 0);
+ QCOMPARE(m.parameterTypes().size(), 0);
m = mobj->method(mobj->indexOfMethod("invoke2(int)"));
- QCOMPARE(m.parameterNames().count(), 1);
- QCOMPARE(m.parameterTypes().count(), 1);
+ QCOMPARE(m.parameterNames().size(), 1);
+ QCOMPARE(m.parameterTypes().size(), 1);
QCOMPARE(m.parameterTypes().at(0), QByteArray("int"));
QVERIFY(m.parameterNames().at(0).isEmpty());
m = mobj->method(mobj->indexOfMethod("invoke3(int,int)"));
- QCOMPARE(m.parameterNames().count(), 2);
- QCOMPARE(m.parameterTypes().count(), 2);
+ QCOMPARE(m.parameterNames().size(), 2);
+ QCOMPARE(m.parameterTypes().size(), 2);
QCOMPARE(m.parameterTypes().at(0), QByteArray("int"));
QCOMPARE(m.parameterNames().at(0), QByteArray("hinz"));
QCOMPARE(m.parameterTypes().at(1), QByteArray("int"));
@@ -2890,7 +2982,7 @@ void tst_QObject::floatProperty()
QVERIFY(idx > 0);
QMetaProperty prop = obj.metaObject()->property(idx);
QVERIFY(prop.isValid());
- QCOMPARE(int(prop.type()), QMetaType::type("float"));
+ QCOMPARE(prop.typeId(), QMetaType::fromType<float>().id());
QVERIFY(!prop.write(&obj, QVariant("Hello")));
QVERIFY(prop.write(&obj, QVariant::fromValue(128.0f)));
QVariant v = prop.read(&obj);
@@ -2905,7 +2997,7 @@ void tst_QObject::qrealProperty()
QVERIFY(idx > 0);
QMetaProperty prop = obj.metaObject()->property(idx);
QVERIFY(prop.isValid());
- QCOMPARE(int(prop.type()), QMetaType::type("qreal"));
+ QCOMPARE(prop.typeId(), QMetaType::fromType<qreal>().id());
QVERIFY(!prop.write(&obj, QVariant("Hello")));
QVERIFY(prop.write(&obj, QVariant::fromValue(128.0f)));
@@ -2950,31 +3042,31 @@ void tst_QObject::dynamicProperties()
// set a dynamic property
QVERIFY(!obj.setProperty("myuserproperty", "Hello"));
- QCOMPARE(obj.changedDynamicProperties.count(), 1);
+ QCOMPARE(obj.changedDynamicProperties.size(), 1);
QCOMPARE(obj.changedDynamicProperties.first(), QByteArray("myuserproperty"));
//check if there is no redundant DynamicPropertyChange events
QVERIFY(!obj.setProperty("myuserproperty", "Hello"));
- QCOMPARE(obj.changedDynamicProperties.count(), 1);
+ QCOMPARE(obj.changedDynamicProperties.size(), 1);
- QCOMPARE(obj.property("myuserproperty").type(), QVariant::String);
+ QCOMPARE(obj.property("myuserproperty").typeId(), QMetaType::QString);
QCOMPARE(obj.property("myuserproperty").toString(), QString("Hello"));
- QCOMPARE(obj.dynamicPropertyNames().count(), 1);
+ QCOMPARE(obj.dynamicPropertyNames().size(), 1);
QCOMPARE(obj.dynamicPropertyNames().first(), QByteArray("myuserproperty"));
// change type of the dynamic property
obj.changedDynamicProperties.clear();
QVERIFY(!obj.setProperty("myuserproperty", QByteArray("Hello")));
- QCOMPARE(obj.changedDynamicProperties.count(), 1);
+ QCOMPARE(obj.changedDynamicProperties.size(), 1);
QCOMPARE(obj.changedDynamicProperties.first(), QByteArray("myuserproperty"));
- QCOMPARE(obj.property("myuserproperty").type(), QVariant::ByteArray);
+ QCOMPARE(obj.property("myuserproperty").typeId(), QMetaType::QByteArray);
QCOMPARE(obj.property("myuserproperty").toString(), QByteArray("Hello"));
// unset the property
obj.changedDynamicProperties.clear();
QVERIFY(!obj.setProperty("myuserproperty", QVariant()));
- QCOMPARE(obj.changedDynamicProperties.count(), 1);
+ QCOMPARE(obj.changedDynamicProperties.size(), 1);
QCOMPARE(obj.changedDynamicProperties.first(), QByteArray("myuserproperty"));
obj.changedDynamicProperties.clear();
@@ -3050,6 +3142,8 @@ void tst_QObject::blockingQueuedConnection()
}
}
+static int s_eventSpyCounter = -1;
+
class EventSpy : public QObject
{
Q_OBJECT
@@ -3069,14 +3163,17 @@ public:
void clear()
{
events.clear();
+ thisCounter = -1;
}
bool eventFilter(QObject *object, QEvent *event) override
{
events.append(qMakePair(object, event->type()));
+ thisCounter = ++s_eventSpyCounter;
return false;
}
+ int thisCounter = -1;
private:
EventList events;
};
@@ -3165,6 +3262,78 @@ void tst_QObject::childEvents()
}
}
+void tst_QObject::parentEvents()
+{
+#ifdef QT_BUILD_INTERNAL
+ EventSpy::EventList expected;
+
+ {
+ // Parent events not enabled
+ QObject parent;
+ QObject child;
+
+ EventSpy spy;
+ child.installEventFilter(&spy);
+
+ QCoreApplication::postEvent(&child, new QEvent(QEvent::Type(QEvent::User + 1)));
+
+ child.setParent(&parent);
+
+ QCoreApplication::postEvent(&child, new QEvent(QEvent::Type(QEvent::User + 2)));
+
+ expected =
+ EventSpy::EventList();
+ QCOMPARE(spy.eventList(), expected);
+ spy.clear();
+
+ QCoreApplication::processEvents();
+
+ expected =
+ EventSpy::EventList()
+ << qMakePair(&child, QEvent::Type(QEvent::User + 1))
+ << qMakePair(&child, QEvent::Type(QEvent::User + 2));
+ QCOMPARE(spy.eventList(), expected);
+ }
+
+ {
+ // Parent events enabled
+ QObject parent;
+ QObject child;
+ auto *childPrivate = QObjectPrivate::get(&child);
+ childPrivate->receiveParentEvents = true;
+
+ EventSpy spy;
+ child.installEventFilter(&spy);
+
+ QCoreApplication::postEvent(&child, new QEvent(QEvent::Type(QEvent::User + 1)));
+
+ child.setParent(&parent);
+ child.setParent(nullptr);
+
+ QCoreApplication::postEvent(&child, new QEvent(QEvent::Type(QEvent::User + 2)));
+
+ expected =
+ EventSpy::EventList()
+ << qMakePair(&child, QEvent::ParentAboutToChange)
+ << qMakePair(&child, QEvent::ParentChange)
+ << qMakePair(&child, QEvent::ParentAboutToChange)
+ << qMakePair(&child, QEvent::ParentChange);
+ QCOMPARE(spy.eventList(), expected);
+ spy.clear();
+
+ QCoreApplication::processEvents();
+
+ expected =
+ EventSpy::EventList()
+ << qMakePair(&child, QEvent::Type(QEvent::User + 1))
+ << qMakePair(&child, QEvent::Type(QEvent::User + 2));
+ QCOMPARE(spy.eventList(), expected);
+ }
+#else
+ QSKIP("Needs QT_BUILD_INTERNAL");
+#endif
+}
+
void tst_QObject::installEventFilter()
{
QEvent event(QEvent::User);
@@ -3206,6 +3375,70 @@ void tst_QObject::installEventFilter()
QVERIFY(spy.eventList().isEmpty());
}
+#define CHECK_FAIL(message) \
+do {\
+ if (QTest::currentTestFailed())\
+ QFAIL("failed one line above on " message);\
+} while (false)
+
+void tst_QObject::installEventFilterOrder()
+{
+ // installEventFilter() adds new objects to d_func()->extraData->eventFilters, which
+ // affects the order of calling each object's eventFilter() when processing the events.
+
+ QObject object;
+ EventSpy spy1, spy2, spy3;
+
+ auto clearSignalSpies = [&] {
+ for (auto *s : {&spy1, &spy2, &spy3})
+ s->clear();
+ s_eventSpyCounter = -1;
+ };
+
+ const EventSpy::EventList expected = { { &object, QEvent::Type(QEvent::User + 1) } };
+
+ // Call Order: from first to last
+ auto checkCallOrder = [&expected](const QList<EventSpy *> &spies) {
+ for (int i = 0; i < spies.size(); ++i) {
+ EventSpy *spy = spies.at(i);
+ QVERIFY2(spy->eventList() == expected,
+ QString("The spy %1 wasn't triggered exactly once.").arg(i).toLatin1());
+ QCOMPARE(spy->thisCounter, i);
+ }
+ };
+
+ // Install event filters and check the order of invocations:
+ // The last installed = the first called.
+ object.installEventFilter(&spy1);
+ object.installEventFilter(&spy2);
+ object.installEventFilter(&spy3);
+ clearSignalSpies();
+ QCoreApplication::postEvent(&object, new QEvent(QEvent::Type(QEvent::User + 1)));
+ QCoreApplication::processEvents();
+ checkCallOrder({ &spy3, &spy2, &spy1 });
+ CHECK_FAIL("checkCallOrder() - 1st round");
+
+ // Install event filter for `spy1` again, which reorders spy1 in `eventFilters`
+ // (the list doesn't have duplicates).
+ object.installEventFilter(&spy1);
+ clearSignalSpies();
+ QCoreApplication::postEvent(&object, new QEvent(QEvent::Type(QEvent::User + 1)));
+ QCoreApplication::processEvents();
+ checkCallOrder({ &spy1, &spy3, &spy2 });
+ CHECK_FAIL("checkCallOrder() - 2nd round");
+
+ // Remove event filter for `spy3`, ensure it's not called anymore and the
+ // existing filters order is preserved.
+ object.removeEventFilter(&spy3);
+ clearSignalSpies();
+ QCoreApplication::postEvent(&object, new QEvent(QEvent::Type(QEvent::User + 1)));
+ QCoreApplication::processEvents();
+ checkCallOrder({ &spy1, &spy2 });
+ CHECK_FAIL("checkCallOrder() - 3rd round");
+ QVERIFY(spy3.eventList().isEmpty());
+ QCOMPARE(spy3.thisCounter, -1);
+}
+
class EmitThread : public QThread
{ Q_OBJECT
public:
@@ -4701,8 +4934,7 @@ void tst_QObject::customTypesPointer()
checker.disconnect();
- int idx = qRegisterMetaType<CustomType>("CustomType");
- QCOMPARE(QMetaType::type("CustomType"), idx);
+ qRegisterMetaType<CustomType>();
connect(&checker, &QCustomTypeChecker::signal1, &checker, &QCustomTypeChecker::slot1,
Qt::QueuedConnection);
@@ -4715,11 +4947,6 @@ void tst_QObject::customTypesPointer()
QCOMPARE(checker.received.value(), t2.value());
QCOMPARE(instanceCount, 4);
- QVERIFY(QMetaType::isRegistered(idx));
- QCOMPARE(qRegisterMetaType<CustomType>("CustomType"), idx);
- QCOMPARE(QMetaType::type("CustomType"), idx);
- QVERIFY(QMetaType::isRegistered(idx));
-
// Test auto registered type (QList<CustomType>)
QList<CustomType> list;
QCOMPARE(instanceCount, 4);
@@ -5954,15 +6181,15 @@ class ConnectToPrivateSlotPrivate;
class ConnectToPrivateSlot :public QObject {
Q_OBJECT
+ Q_DECLARE_PRIVATE(ConnectToPrivateSlot)
public:
ConnectToPrivateSlot();
void test(SenderObject *obj1) ;
- Q_DECLARE_PRIVATE(ConnectToPrivateSlot)
};
class ConnectToPrivateSlotPrivate : public QObjectPrivate {
-public:
Q_DECLARE_PUBLIC(ConnectToPrivateSlot)
+public:
int receivedCount;
QVariant receivedValue;
@@ -5974,6 +6201,8 @@ public:
receivedCount++;
receivedValue = v;
};
+
+ void testFromPrivate(SenderObject *obj);
};
ConnectToPrivateSlot::ConnectToPrivateSlot(): QObject(*new ConnectToPrivateSlotPrivate) {}
@@ -6000,6 +6229,14 @@ void ConnectToPrivateSlot::test(SenderObject* obj1) {
QVERIFY(!QObjectPrivate::disconnect(obj1, &SenderObject::signal2, d, &ConnectToPrivateSlotPrivate::thisIsAPrivateSlot));
}
+// Compile test to verify that we can use QObjectPrivate::connect in
+// the code of the private class, even if Q_DECLARE_PUBLIC is used in the
+// private section of the private class.
+void ConnectToPrivateSlotPrivate::testFromPrivate(SenderObject *obj)
+{
+ QVERIFY(QObjectPrivate::connect(obj, &SenderObject::signal1, this, &ConnectToPrivateSlotPrivate::thisIsAPrivateSlot));
+}
+
void tst_QObject::connectPrivateSlots()
{
SenderObject sender;
@@ -6036,6 +6273,7 @@ void tst_QObject::connectFunctorArgDifference()
connect(&timer, &QTimer::timeout, [=](){});
connect(&timer, &QTimer::objectNameChanged, [=](const QString &){});
+ connect(&timer, &QTimer::objectNameChanged, this, [](){});
connect(qApp, &QCoreApplication::aboutToQuit, [=](){});
connect(&timer, &QTimer::objectNameChanged, [=](){});
@@ -6263,11 +6501,46 @@ void tst_QObject::connectFunctorWithContextUnique()
QVERIFY(QObject::connect(&sender, &SenderObject::signal1, &receiver, &ReceiverObject::slot1));
receiver.count_slot1 = 0;
- QTest::ignoreMessage(QtWarningMsg, "QObject::connect(SenderObject, ReceiverObject): unique connections require a pointer to member function of a QObject subclass");
+ QVERIFY(QObject::connect(&sender, &SenderObject::signal2, &receiver, &ReceiverObject::slot2));
+ receiver.count_slot2 = 0;
+
+ const auto oredType = Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection);
+
+ // Will assert in debug builds, so only test in release builds
+#if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
+ auto ignoreMsg = [] {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QObject::connect(SenderObject, ReceiverObject): unique connections "
+ "require a pointer to member function of a QObject subclass");
+ };
+
+ ignoreMsg();
QVERIFY(!QObject::connect(&sender, &SenderObject::signal1, &receiver, [&](){ receiver.slot1(); }, Qt::UniqueConnection));
+ ignoreMsg();
+ QVERIFY(!QObject::connect(
+ &sender, &SenderObject::signal2, &receiver, [&]() { receiver.slot2(); }, oredType));
+#endif
+
sender.emitSignal1();
QCOMPARE(receiver.count_slot1, 1);
+
+ sender.emitSignal2();
+ QCOMPARE(receiver.count_slot2, 1);
+
+ // Check connecting to PMF doesn't hit the assert
+
+ QVERIFY(QObject::connect(&sender, &SenderObject::signal3, &receiver, &ReceiverObject::slot3,
+ Qt::UniqueConnection));
+ receiver.count_slot3 = 0;
+ sender.emitSignal3();
+ QCOMPARE(receiver.count_slot3, 1);
+
+ QVERIFY(QObject::connect(&sender, &SenderObject::signal4, &receiver, &ReceiverObject::slot4,
+ oredType));
+ receiver.count_slot4 = 0;
+ sender.emitSignal4();
+ QCOMPARE(receiver.count_slot4, 1);
}
class MyFunctor
@@ -6774,7 +7047,11 @@ struct QmlReceiver : public QtPrivate::QSlotObjectBase
, magic(0)
{}
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
static void impl(int which, QSlotObjectBase *this_, QObject *, void **metaArgs, bool *ret)
+#else
+ static void impl(QSlotObjectBase *this_, QObject *, void **metaArgs, int which, bool *ret)
+#endif
{
switch (which) {
case Destroy: delete static_cast<QmlReceiver*>(this_); return;
@@ -8224,5 +8501,333 @@ void tst_QObject::emitToDestroyedClass()
static_assert(QtPrivate::HasQ_OBJECT_Macro<tst_QObject>::Value);
static_assert(!QtPrivate::HasQ_OBJECT_Macro<SiblingDeleter>::Value);
+Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr)
+Q_DECLARE_SMART_POINTER_METATYPE(std::unique_ptr)
+
+
+// QTBUG-103741: OK to use smart pointers to const QObject in signals/slots
+class SenderWithSharedPointerConstQObject : public QObject
+{
+ Q_OBJECT
+
+signals:
+ void aSignal1(const QSharedPointer<const QObject> &);
+ void aSignal2(const QWeakPointer<const QObject> &);
+ void aSignal3(const QPointer<const QObject> &);
+ void aSignal4(const std::shared_ptr<const QObject> &);
+ void aSignal5(const std::unique_ptr<const QObject> &);
+};
+
+#ifdef QT_BUILD_INTERNAL
+/*
+ Since QObjectPrivate stores the declarativeData pointer in a union with the pointer
+ to the currently destroyed child, calls to the QtDeclarative handlers need to be
+ correctly guarded. QTBUG-105286
+*/
+namespace QtDeclarative {
+static QAbstractDeclarativeData *theData;
+
+static void destroyed(QAbstractDeclarativeData *data, QObject *)
+{
+ QCOMPARE(data, theData);
+}
+static void signalEmitted(QAbstractDeclarativeData *data, QObject *, int, void **)
+{
+ QCOMPARE(data, theData);
+}
+// we can't use QCOMPARE in the next two functions, as they don't return void
+static int receivers(QAbstractDeclarativeData *data, const QObject *, int)
+{
+ QTest::qCompare(data, theData, "data", "theData", __FILE__, __LINE__);
+ return 0;
+}
+static bool isSignalConnected(QAbstractDeclarativeData *data, const QObject *, int)
+{
+ QTest::qCompare(data, theData, "data", "theData", __FILE__, __LINE__);
+ return true;
+}
+
+class Object : public QObject
+{
+ Q_OBJECT
+public:
+ using QObject::QObject;
+ ~Object()
+ {
+ if (Object *p = static_cast<Object *>(parent()))
+ p->emitSignal();
+ }
+
+ void emitSignal()
+ {
+ emit theSignal();
+ }
+
+signals:
+ void theSignal();
+};
+
+}
+#endif
+
+void tst_QObject::declarativeData()
+{
+#ifdef QT_BUILD_INTERNAL
+ QScopedValueRollback destroyed(QAbstractDeclarativeData::destroyed,
+ QtDeclarative::destroyed);
+ QScopedValueRollback signalEmitted(QAbstractDeclarativeData::signalEmitted,
+ QtDeclarative::signalEmitted);
+ QScopedValueRollback receivers(QAbstractDeclarativeData::receivers,
+ QtDeclarative::receivers);
+ QScopedValueRollback isSignalConnected(QAbstractDeclarativeData::isSignalConnected,
+ QtDeclarative::isSignalConnected);
+
+ QtDeclarative::Object p;
+ QObjectPrivate *priv = QObjectPrivate::get(&p);
+ priv->declarativeData = QtDeclarative::theData = new QAbstractDeclarativeData;
+
+ connect(&p, &QtDeclarative::Object::theSignal, &p, []{
+ });
+
+ QtDeclarative::Object *child = new QtDeclarative::Object;
+ child->setParent(&p);
+#endif
+}
+
+/*
+ Compile-time test for the helpers in qobjectdefs_impl.h.
+*/
+class AsyncCaller : public QObject
+{
+ Q_OBJECT
+public:
+ ~AsyncCaller()
+ {
+ if (slotObject)
+ slotObject->destroyIfLastRef();
+ }
+ void callback0() {}
+ void callback1(const QString &) {}
+ void callbackInt(int) {}
+ int returnInt() const { return 0; }
+
+ static int staticCallback0() { return 0; }
+ static void staticCallback1(const QString &) {}
+
+ using Prototype0 = int(*)();
+ using Prototype1 = void(*)(QString);
+
+ template<typename Functor>
+ bool callMe0(const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *, Functor &&func)
+ {
+ if (slotObject) {
+ slotObject->destroyIfLastRef();
+ slotObject = nullptr;
+ }
+ QtPrivate::AssertCompatibleFunctions<Prototype0, Functor>();
+ slotObject = QtPrivate::makeCallableObject<Prototype0>(std::forward<Functor>(func));
+ return true;
+ }
+
+ template<typename Functor>
+ bool callMe0(Functor &&func)
+ {
+ return callMe0(nullptr, std::forward<Functor>(func));
+ }
+
+ template<typename Functor>
+ bool callMe1(const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *, Functor &&func)
+ {
+ if (slotObject) {
+ slotObject->destroyIfLastRef();
+ slotObject = nullptr;
+ }
+ QtPrivate::AssertCompatibleFunctions<Prototype1, Functor>();
+ slotObject = QtPrivate::makeCallableObject<Prototype1>(std::forward<Functor>(func));
+ return true;
+ }
+
+ template<typename Functor>
+ bool callMe1(Functor &&func)
+ {
+ return callMe1(nullptr, std::forward<Functor>(func));
+ }
+
+ QtPrivate::QSlotObjectBase *slotObject = nullptr;
+};
+
+static void freeFunction0() {}
+static void freeFunction1(QString) {}
+static void freeFunctionVariant(QVariant) {}
+
+template<typename Prototype, typename Functor>
+inline constexpr bool compiles(Functor &&) {
+ return QtPrivate::AreFunctionsCompatible<Prototype, Functor>::value;
+}
+
+void tst_QObject::asyncCallbackHelper()
+{
+ int result = 0;
+ QString arg1 = "Parameter";
+ void *argv[] = { &result, &arg1 };
+
+ auto lambda0 = []{};
+ auto lambda1 = [](const QString &) {};
+ auto lambda2 = [](const QString &, int) {};
+ const auto constLambda = [](const QString &) {};
+ auto moveOnlyLambda = [u = std::unique_ptr<int>()]{};
+ auto moveOnlyLambda1 = [u = std::unique_ptr<int>()](const QString &){};
+
+ SlotFunctor functor0;
+ SlotFunctorString functor1;
+
+ // no parameters provided or needed
+ static_assert(compiles<AsyncCaller::Prototype0>(&AsyncCaller::callback0));
+ static_assert(compiles<AsyncCaller::Prototype0>(&AsyncCaller::staticCallback0));
+ static_assert(compiles<AsyncCaller::Prototype0>(lambda0));
+ static_assert(compiles<AsyncCaller::Prototype0>(std::move(moveOnlyLambda)));
+ static_assert(compiles<AsyncCaller::Prototype0>(freeFunction0));
+ static_assert(compiles<AsyncCaller::Prototype0>(functor0));
+
+ // more parameters than needed
+ static_assert(compiles<AsyncCaller::Prototype1>(&AsyncCaller::callback0));
+ static_assert(compiles<AsyncCaller::Prototype1>(&AsyncCaller::staticCallback0));
+ static_assert(compiles<AsyncCaller::Prototype1>(lambda0));
+ static_assert(compiles<AsyncCaller::Prototype1>(freeFunction0));
+ static_assert(compiles<AsyncCaller::Prototype1>(functor0));
+
+ // matching parameter
+ static_assert(compiles<AsyncCaller::Prototype1>(&AsyncCaller::callback1));
+ static_assert(compiles<AsyncCaller::Prototype1>(&AsyncCaller::staticCallback1));
+ static_assert(compiles<AsyncCaller::Prototype1>(lambda1));
+ static_assert(compiles<AsyncCaller::Prototype1>(std::move(moveOnlyLambda1)));
+ static_assert(compiles<AsyncCaller::Prototype1>(constLambda));
+ static_assert(compiles<AsyncCaller::Prototype1>(freeFunction1));
+ static_assert(compiles<AsyncCaller::Prototype1>(functor1));
+
+ // not enough parameters
+ static_assert(!compiles<AsyncCaller::Prototype0>(&AsyncCaller::callback1));
+ static_assert(!compiles<AsyncCaller::Prototype0>(&AsyncCaller::staticCallback1));
+ static_assert(!compiles<AsyncCaller::Prototype0>(lambda1));
+ static_assert(!compiles<AsyncCaller::Prototype0>(constLambda));
+ static_assert(!compiles<AsyncCaller::Prototype0>(lambda2));
+ static_assert(!compiles<AsyncCaller::Prototype0>(freeFunction1));
+ static_assert(!compiles<AsyncCaller::Prototype0>(functor1));
+
+ // wrong parameter type
+ static_assert(!compiles<AsyncCaller::Prototype1>(&AsyncCaller::callbackInt));
+
+ // old-style slot name
+ static_assert(!compiles<AsyncCaller::Prototype0>("callback1"));
+
+ // slot with return value is ok, we just don't pass
+ // the return value through to anything.
+ static_assert(compiles<AsyncCaller::Prototype0>(&AsyncCaller::returnInt));
+
+ static_assert(compiles<AsyncCaller::Prototype1>(freeFunctionVariant));
+
+ std::function<int()> stdFunction0(&AsyncCaller::staticCallback0);
+ std::function<void(QString)> stdFunction1(&AsyncCaller::staticCallback1);
+ static_assert(compiles<AsyncCaller::Prototype0>(stdFunction0));
+ static_assert(compiles<AsyncCaller::Prototype1>(stdFunction1));
+
+ AsyncCaller caller;
+ // with context
+ QVERIFY(caller.callMe0(&caller, &AsyncCaller::callback0));
+ QVERIFY(caller.callMe0(&caller, &AsyncCaller::returnInt));
+ QVERIFY(caller.callMe0(&caller, &AsyncCaller::staticCallback0));
+ QVERIFY(caller.callMe0(&caller, lambda0));
+ QVERIFY(caller.callMe0(&caller, freeFunction0));
+ QVERIFY(caller.callMe0(&caller, std::move(moveOnlyLambda)));
+ QVERIFY(caller.callMe0(&caller, stdFunction0));
+
+ QVERIFY(caller.callMe1(&caller, &AsyncCaller::callback1));
+ QVERIFY(caller.callMe1(&caller, &AsyncCaller::staticCallback1));
+ QVERIFY(caller.callMe1(&caller, lambda1));
+ QVERIFY(caller.callMe1(&caller, freeFunction1));
+ QVERIFY(caller.callMe1(&caller, constLambda));
+ QVERIFY(caller.callMe1(&caller, stdFunction1));
+
+ // without context
+ QVERIFY(caller.callMe0(&AsyncCaller::staticCallback0));
+ QVERIFY(caller.callMe0(lambda0));
+ QVERIFY(caller.callMe0(freeFunction0));
+ QVERIFY(caller.callMe0(stdFunction0));
+
+ QVERIFY(caller.callMe1(&AsyncCaller::staticCallback1));
+ QVERIFY(caller.callMe1(lambda1));
+ QVERIFY(caller.callMe1(constLambda));
+ QVERIFY(caller.callMe1(std::move(moveOnlyLambda1)));
+ QVERIFY(caller.callMe1(freeFunction1));
+ QVERIFY(caller.callMe1(stdFunction1));
+
+ static const char *expectedPayload = "Hello World!";
+ {
+ struct MoveOnlyFunctor {
+ MoveOnlyFunctor() = default;
+ MoveOnlyFunctor(MoveOnlyFunctor &&) = default;
+ MoveOnlyFunctor(const MoveOnlyFunctor &) = delete;
+ ~MoveOnlyFunctor() = default;
+
+ int operator()() const {
+ qDebug().noquote() << payload;
+ return int(payload.length());
+ }
+ QString payload = expectedPayload;
+ } moveOnlyFunctor;
+ QVERIFY(caller.callMe0(std::move(moveOnlyFunctor)));
+ }
+ QTest::ignoreMessage(QtDebugMsg, expectedPayload);
+ caller.slotObject->call(nullptr, argv);
+ QCOMPARE(result, QLatin1String(expectedPayload).length());
+
+ // mutable lambda; same behavior as mutableFunctor - we copy the functor
+ // in the QCallableObject, so the original is not modified
+ int status = 0;
+ auto mutableLambda1 = [&status, calls = 0]() mutable { status = ++calls; };
+
+ mutableLambda1();
+ QCOMPARE(status, 1);
+ QVERIFY(caller.callMe0(mutableLambda1)); // this copies the lambda with count == 1
+ caller.slotObject->call(nullptr, argv); // this doesn't change mutableLambda1, but the copy
+ QCOMPARE(status, 2);
+ mutableLambda1();
+ QCOMPARE(status, 2); // and we are still at two
+
+ auto mutableLambda2 = [calls = 0]() mutable { return ++calls; };
+ QCOMPARE(mutableLambda2(), 1);
+ QVERIFY(caller.callMe0(mutableLambda2)); // this copies the lambda
+ caller.slotObject->call(nullptr, argv); // this call doesn't change mutableLambda2
+ QCOMPARE(mutableLambda2(), 2); // so we are still at 2
+
+ {
+ int called = -1;
+ struct MutableFunctor {
+ void operator()() { called = 0; }
+ int &called;
+ };
+ struct ConstFunctor
+ {
+ void operator()() const { called = 1; }
+ int &called;
+ };
+
+ MutableFunctor mf{called};
+ QMetaObject::invokeMethod(this, mf);
+ QCOMPARE(called, 0);
+ ConstFunctor cf{called};
+ QMetaObject::invokeMethod(this, cf);
+ QCOMPARE(called, 1);
+ QMetaObject::invokeMethod(this, [&called, u = std::unique_ptr<int>()]{ called = 2; });
+ QCOMPARE(called, 2);
+ QMetaObject::invokeMethod(this, [&called, count = 0]() mutable {
+ if (!count)
+ called = 3;
+ ++count;
+ });
+ QCOMPARE(called, 3);
+ }
+}
+
QTEST_MAIN(tst_QObject)
#include "tst_qobject.moc"
diff --git a/tests/auto/corelib/kernel/qpermission/.gitignore b/tests/auto/corelib/kernel/qpermission/.gitignore
new file mode 100644
index 0000000000..56a6bf0795
--- /dev/null
+++ b/tests/auto/corelib/kernel/qpermission/.gitignore
@@ -0,0 +1 @@
+tst_qpermission
diff --git a/tests/auto/corelib/kernel/qpermission/CMakeLists.txt b/tests/auto/corelib/kernel/qpermission/CMakeLists.txt
new file mode 100644
index 0000000000..1af0331186
--- /dev/null
+++ b/tests/auto/corelib/kernel/qpermission/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qpermission LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if (NOT QT_FEATURE_permissions)
+ return()
+endif()
+
+qt_internal_add_test(tst_qpermission
+ SOURCES
+ tst_qpermission.cpp
+ LIBRARIES
+ Qt::Core
+)
diff --git a/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp b/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp
new file mode 100644
index 0000000000..dbf1d2dd84
--- /dev/null
+++ b/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp
@@ -0,0 +1,284 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QPermission>
+
+#include <QTest>
+
+struct DummyPermission // a minimal QPermission-compatible type
+{
+ using QtPermissionHelper = void;
+ int state = 0;
+};
+Q_DECLARE_METATYPE(DummyPermission)
+
+class tst_QPermission : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+ void converting_Dummy() const { return converting_impl<DummyPermission>(); }
+ void converting_Location() const { return converting_impl<QLocationPermission>(); }
+ void converting_Calendar() const { return converting_impl<QCalendarPermission>(); }
+ void converting_Contacts() const { return converting_impl<QContactsPermission>(); }
+ void converting_Camera() const { return converting_impl<QCameraPermission>(); }
+ void converting_Microphone() const { return converting_impl<QMicrophonePermission>(); }
+ void converting_Bluetooth() const { return converting_impl<QBluetoothPermission>(); }
+
+ void conversionMaintainsState() const;
+
+ void functorWithoutContext();
+ void functorWithContextInThread();
+ void receiverInThread();
+ void destroyedContextObject();
+private:
+ template <typename T>
+ void converting_impl() const;
+};
+
+template <typename T>
+void tst_QPermission::converting_impl() const
+{
+ T concrete;
+ const T cconcrete = concrete;
+ const auto metaType = QMetaType::fromType<T>();
+
+ // construction is implicit:
+ // from rvalue:
+ {
+ QPermission p = T();
+ QCOMPARE_EQ(p.type(), metaType);
+ }
+ // from mutable lvalue:
+ {
+ QPermission p = concrete;
+ QCOMPARE_EQ(p.type(), metaType);
+ }
+ // from const lvalue:
+ {
+ QPermission p = cconcrete;
+ QCOMPARE_EQ(p.type(), metaType);
+ }
+
+ // value<>() compiles:
+ {
+ const QPermission p = concrete;
+ auto v = p.value<T>();
+ static_assert(std::is_same_v<decltype(v), std::optional<T>>);
+ QCOMPARE_NE(v, std::nullopt);
+ }
+}
+
+void tst_QPermission::conversionMaintainsState() const
+{
+ DummyPermission dummy{42}, dummy_default;
+ QCOMPARE_NE(dummy.state, dummy_default.state);
+
+ QLocationPermission loc, loc_default;
+ QCOMPARE_EQ(loc_default.accuracy(), QLocationPermission::Accuracy::Approximate);
+ QCOMPARE_EQ(loc_default.availability(), QLocationPermission::Availability::WhenInUse);
+
+ loc.setAccuracy(QLocationPermission::Accuracy::Precise);
+ loc.setAvailability(QLocationPermission::Availability::Always);
+
+ QCOMPARE_EQ(loc.accuracy(), QLocationPermission::Accuracy::Precise);
+ QCOMPARE_EQ(loc.availability(), QLocationPermission::Availability::Always);
+
+ QCalendarPermission cal, cal_default;
+ QCOMPARE_EQ(cal_default.accessMode(), QCalendarPermission::AccessMode::ReadOnly);
+
+ cal.setAccessMode(QCalendarPermission::AccessMode::ReadWrite);
+
+ QCOMPARE_EQ(cal.accessMode(), QCalendarPermission::AccessMode::ReadWrite);
+
+ QContactsPermission con, con_default;
+ QCOMPARE_EQ(con_default.accessMode(), QContactsPermission::AccessMode::ReadOnly);
+
+ con.setAccessMode(QContactsPermission::AccessMode::ReadWrite);
+
+ QCOMPARE_EQ(con.accessMode(), QContactsPermission::AccessMode::ReadWrite);
+
+ //
+ // QCameraPermission, QMicrophonePermission, QBluetoothPermission don't have
+ // state at the time of writing
+ //
+
+ QPermission p; // maintain state between the blocks below to test reset behavior
+
+ {
+ p = dummy;
+ auto v = p.value<DummyPermission>();
+ QCOMPARE_NE(v, std::nullopt);
+ auto &r = *v;
+ QCOMPARE_EQ(r.state, dummy.state);
+ // check mismatched returns nullopt:
+ QCOMPARE_EQ(p.value<QCalendarPermission>(), std::nullopt);
+ }
+
+ {
+ p = loc;
+ auto v = p.value<QLocationPermission>();
+ QCOMPARE_NE(v, std::nullopt);
+ auto &r = *v;
+ QCOMPARE_EQ(r.accuracy(), loc.accuracy());
+ QCOMPARE_EQ(r.availability(), loc.availability());
+ // check mismatched returns nullopt:
+ QCOMPARE_EQ(p.value<DummyPermission>(), std::nullopt);
+ }
+
+ {
+ p = con;
+ auto v = p.value<QContactsPermission>();
+ QCOMPARE_NE(v, std::nullopt);
+ auto &r = *v;
+ QCOMPARE_EQ(r.accessMode(), con.accessMode());
+ // check mismatched returns nullopt:
+ QCOMPARE_EQ(p.value<QLocationPermission>(), std::nullopt);
+ }
+
+ {
+ p = cal;
+ auto v = p.value<QCalendarPermission>();
+ QCOMPARE_NE(v, std::nullopt);
+ auto &r = *v;
+ QCOMPARE_EQ(r.accessMode(), cal.accessMode());
+ // check mismatched returns nullopt:
+ QCOMPARE_EQ(p.value<QContactsPermission>(), std::nullopt);
+ }
+}
+
+template <typename Func,
+ typename T = std::void_t<decltype(qApp->requestPermission(std::declval<DummyPermission>(),
+ std::declval<Func>()))>
+ >
+void wrapRequestPermission(const QPermission &p, Func &&f)
+{
+ qApp->requestPermission(p, std::forward<Func>(f));
+}
+
+template <typename Functor>
+using CompatibleTest = decltype(wrapRequestPermission(std::declval<QPermission>(), std::declval<Functor>()));
+
+
+// Compile test for context-less functor overloads
+void tst_QPermission::functorWithoutContext()
+{
+ int argc = 0;
+ char *argv = nullptr;
+ QCoreApplication app(argc, &argv);
+
+ DummyPermission dummy;
+#ifdef Q_OS_DARWIN
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*Could not find permission plugin for DummyPermission.*"));
+#endif
+
+ qApp->requestPermission(dummy, [](const QPermission &permission){
+ QVERIFY(permission.value<DummyPermission>());
+ });
+ wrapRequestPermission(dummy, [](const QPermission &permission){
+ QVERIFY(permission.value<DummyPermission>());
+ });
+
+ auto compatible = [](const QPermission &) {};
+ using Compatible = decltype(compatible);
+ auto incompatible = [](const QString &) {};
+ using Incompatible = decltype(incompatible);
+
+ static_assert(qxp::is_detected_v<CompatibleTest, Compatible>);
+ static_assert(!qxp::is_detected_v<CompatibleTest, Incompatible>);
+}
+
+void tst_QPermission::functorWithContextInThread()
+{
+ int argc = 0;
+ char *argv = nullptr;
+ QCoreApplication app(argc, &argv);
+ QThread::currentThread()->setObjectName("main thread");
+ QThread receiverThread;
+ receiverThread.setObjectName("receiverThread");
+ QObject receiver;
+ receiver.moveToThread(&receiverThread);
+ receiverThread.start();
+ auto guard = qScopeGuard([&receiverThread]{
+ receiverThread.quit();
+ QVERIFY(receiverThread.wait(1000));
+ });
+
+ DummyPermission dummy;
+#ifdef Q_OS_DARWIN
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*Could not find permission plugin for DummyPermission.*"));
+#endif
+ QThread *permissionReceiverThread = nullptr;
+ qApp->requestPermission(dummy, &receiver, [&](const QPermission &permission){
+ auto dummy = permission.value<DummyPermission>();
+ QVERIFY(dummy);
+ permissionReceiverThread = QThread::currentThread();
+ });
+ QTRY_COMPARE(permissionReceiverThread, &receiverThread);
+}
+
+void tst_QPermission::receiverInThread()
+{
+ int argc = 0;
+ char *argv = nullptr;
+ QCoreApplication app(argc, &argv);
+ QThread::currentThread()->setObjectName("main thread");
+ QThread receiverThread;
+ receiverThread.setObjectName("receiverThread");
+ class Receiver : public QObject
+ {
+ public:
+ using QObject::QObject;
+ void handlePermission(const QPermission &permission)
+ {
+ auto dummy = permission.value<DummyPermission>();
+ QVERIFY(dummy);
+ permissionReceiverThread = QThread::currentThread();
+ }
+
+ QThread *permissionReceiverThread = nullptr;
+ } receiver;
+ receiver.moveToThread(&receiverThread);
+ receiverThread.start();
+ auto guard = qScopeGuard([&receiverThread]{
+ receiverThread.quit();
+ QVERIFY(receiverThread.wait(1000));
+ });
+
+ DummyPermission dummy;
+#ifdef Q_OS_DARWIN
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*Could not find permission plugin for DummyPermission.*"));
+#endif
+
+ qApp->requestPermission(dummy, &receiver, &Receiver::handlePermission);
+ QTRY_COMPARE(receiver.permissionReceiverThread, &receiverThread);
+
+ // compile tests: none of these work and the error output isn't horrible
+ // qApp->requestPermission(dummy, &receiver, "&tst_QPermission::receiverInThread");
+ // qApp->requestPermission(dummy, &receiver, &tst_QPermission::receiverInThread);
+ // qApp->requestPermission(dummy, &receiver, &QObject::destroyed);
+}
+
+void tst_QPermission::destroyedContextObject()
+{
+ int argc = 0;
+ char *argv = nullptr;
+ QCoreApplication app(argc, &argv);
+
+ QObject *context = new QObject;
+
+ DummyPermission dummy;
+#ifdef Q_OS_DARWIN
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*Could not find permission plugin for DummyPermission.*"));
+#endif
+ bool permissionReceived = false;
+ qApp->requestPermission(dummy, context, [&]{
+ permissionReceived = true;
+ });
+ QVERIFY2(!permissionReceived, "Permission received synchronously");
+ delete context;
+ QTest::qWait(100);
+ QVERIFY(!permissionReceived);
+}
+
+QTEST_APPLESS_MAIN(tst_QPermission)
+#include "tst_qpermission.moc"
diff --git a/tests/auto/corelib/kernel/qpointer/CMakeLists.txt b/tests/auto/corelib/kernel/qpointer/CMakeLists.txt
index 55a5e50ee7..b1570b8cef 100644
--- a/tests/auto/corelib/kernel/qpointer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qpointer/CMakeLists.txt
@@ -1,13 +1,24 @@
-# Generated from qpointer.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qpointer Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qpointer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if (NOT QT_FEATURE_thread)
+ return()
+endif()
+
qt_internal_add_test(tst_qpointer
SOURCES
tst_qpointer.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::Gui
)
@@ -15,6 +26,6 @@ qt_internal_add_test(tst_qpointer
#####################################################################
qt_internal_extend_target(tst_qpointer CONDITION TARGET Qt::Widgets
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::Widgets
)
diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
index 9886ab84f2..7365fee819 100644
--- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
+++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QRunnable>
@@ -19,6 +19,8 @@ public:
private slots:
void constructors();
+ void ctad();
+ void conversion();
void destructor();
void assignment_operators();
void equality_operators();
@@ -34,14 +36,114 @@ private slots:
void constQPointer();
};
+// check that nullptr QPointer construction is Q_CONSTINIT:
+[[maybe_unused]] Q_CONSTINIT static QPointer<QFile> s_file1;
+[[maybe_unused]] Q_CONSTINIT static QPointer<QFile> s_file2 = {};
+[[maybe_unused]] Q_CONSTINIT static QPointer<QFile> s_file3 = nullptr;
+[[maybe_unused]] Q_CONSTINIT static QPointer<QFile> s_file4 = 0; // legacy nullptr
+
void tst_QPointer::constructors()
{
+ struct Derived : QObject {};
+ Derived derived;
+
QPointer<QObject> p1;
QPointer<QObject> p2(this);
QPointer<QObject> p3(p2);
+ QPointer<QObject> p4 = &derived;
QCOMPARE(p1, QPointer<QObject>(0));
QCOMPARE(p2, QPointer<QObject>(this));
QCOMPARE(p3, QPointer<QObject>(this));
+ QCOMPARE(p4, &derived);
+}
+
+void tst_QPointer::ctad()
+{
+
+ {
+ QObject o;
+ QPointer po = &o;
+ static_assert(std::is_same_v<decltype(po), QPointer<QObject>>);
+ QPointer poc = po;
+ static_assert(std::is_same_v<decltype(poc), QPointer<QObject>>);
+ QPointer pom = std::move(po);
+ static_assert(std::is_same_v<decltype(pom), QPointer<QObject>>);
+ }
+ {
+ const QObject co;
+ QPointer pco = &co;
+ static_assert(std::is_same_v<decltype(pco), QPointer<const QObject>>);
+ QPointer pcoc = pco;
+ static_assert(std::is_same_v<decltype(pcoc), QPointer<const QObject>>);
+ QPointer pcom = std::move(pco);
+ static_assert(std::is_same_v<decltype(pcom), QPointer<const QObject>>);
+ }
+ {
+ QFile f;
+ QPointer pf = &f;
+ static_assert(std::is_same_v<decltype(pf), QPointer<QFile>>);
+ QPointer pfc = pf;
+ static_assert(std::is_same_v<decltype(pfc), QPointer<QFile>>);
+ QPointer pfm = std::move(pf);
+ static_assert(std::is_same_v<decltype(pfm), QPointer<QFile>>);
+ }
+ {
+ const QFile cf;
+ QPointer pcf = &cf;
+ static_assert(std::is_same_v<decltype(pcf), QPointer<const QFile>>);
+ QPointer pcfc = pcf;
+ static_assert(std::is_same_v<decltype(pcfc), QPointer<const QFile>>);
+ QPointer pcfm = std::move(pcf);
+ static_assert(std::is_same_v<decltype(pcfm), QPointer<const QFile>>);
+ }
+}
+
+void tst_QPointer::conversion()
+{
+ // copy-conversion:
+ {
+ QFile file;
+ QPointer<QFile> pf = &file;
+ QCOMPARE_EQ(pf, &file);
+ QPointer<const QIODevice> pio = pf;
+ QCOMPARE_EQ(pio, &file);
+ QCOMPARE_EQ(pio.get(), &file);
+ QCOMPARE_EQ(pio, pf);
+ QCOMPARE_EQ(pio.get(), pf.get());
+
+ // reset
+ pio = nullptr;
+ QCOMPARE_EQ(pio, nullptr);
+ QCOMPARE_EQ(pio.get(), nullptr);
+
+ // copy-assignment
+ QCOMPARE_EQ(pf, &file);
+ pio = pf;
+ QCOMPARE_EQ(pio, &file);
+ QCOMPARE_EQ(pio.get(), &file);
+ QCOMPARE_EQ(pio, pf);
+ QCOMPARE_EQ(pio.get(), pf.get());
+ }
+ // move-conversion:
+ {
+ QFile file;
+ QPointer<QFile> pf = &file;
+ QCOMPARE_EQ(pf, &file);
+ QPointer<const QIODevice> pio = std::move(pf);
+ QCOMPARE_EQ(pf, nullptr);
+ QCOMPARE_EQ(pio, &file);
+ QCOMPARE_EQ(pio.get(), &file);
+
+ // reset
+ pio = nullptr;
+ QCOMPARE_EQ(pio, nullptr);
+ QCOMPARE_EQ(pio.get(), nullptr);
+
+ // move-assignment
+ pio = QPointer<QFile>(&file);
+ QCOMPARE_EQ(pio, &file);
+ QCOMPARE_EQ(pio.get(), &file);
+ }
}
void tst_QPointer::destructor()
diff --git a/tests/auto/corelib/kernel/qproperty/CMakeLists.txt b/tests/auto/corelib/kernel/qproperty/CMakeLists.txt
index 55a5d71752..177465d2ee 100644
--- a/tests/auto/corelib/kernel/qproperty/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qproperty/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qproperty.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qproperty Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qproperty LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qproperty
SOURCES
tst_qproperty.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
)
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
index 92de7725e4..cc7edb8bf2 100644
--- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
+++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
@@ -1,16 +1,17 @@
// Copyright (C) 2020 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QObject>
#include <QSignalSpy>
#include <qtest.h>
#include <qproperty.h>
#include <private/qproperty_p.h>
+#include <private/qobject_p.h>
-#if __has_include(<source_location>) && __cplusplus >= 202002L && !defined(Q_CLANG_QDOC)
+#if __has_include(<source_location>) && __cplusplus >= 202002L && !defined(Q_QDOC)
#include <source_location>
#define QT_SOURCE_LOCATION_NAMESPACE std
-#elif __has_include(<experimental/source_location>) && !defined(Q_CLANG_QDOC)
+#elif __has_include(<experimental/source_location>) && !defined(Q_QDOC)
#include <experimental/source_location>
#define QT_SOURCE_LOCATION_NAMESPACE std::experimental
#endif
@@ -28,6 +29,7 @@ class tst_QProperty : public QObject
{
Q_OBJECT
private slots:
+ void inheritQUntypedPropertyData();
void functorBinding();
void basicDependencies();
void multipleDependencies();
@@ -66,6 +68,7 @@ private slots:
void quntypedBindableApi();
void readonlyConstQBindable();
void qobjectBindableManualNotify();
+ void qobjectBindableReallocatedBindingStorage();
void qobjectBindableSignalTakingNewValue();
void testNewStuff();
@@ -74,16 +77,22 @@ private slots:
void metaProperty();
void modifyObserverListWhileIterating();
+ void noDoubleCapture();
void compatPropertyNoDobuleNotification();
void compatPropertySignals();
void noFakeDependencies();
+#if QT_CONFIG(thread)
void threadSafety();
+ void threadSafety2();
+#endif // QT_CONFIG(thread)
void bindablePropertyWithInitialization();
void noDoubleNotification();
void groupedNotifications();
void groupedNotificationConsistency();
+ void bindingGroupMovingBindingData();
+ void bindingGroupBindingDeleted();
void uninstalledBindingDoesNotEvaluate();
void notify();
@@ -94,8 +103,68 @@ private slots:
void qpropertyAlias();
void scheduleNotify();
+
+ void notifyAfterAllDepsGone();
+
+ void propertyAdaptorBinding();
+ void propertyUpdateViaSignaledProperty();
+
+ void derefFromObserver();
};
+namespace {
+template <class T>
+constexpr auto isDerivedFromQUntypedPropertyData = std::is_base_of_v<QUntypedPropertyData, T>;
+
+template <typename Property>
+constexpr auto isDerivedFromQUntypedPropertyDataFunc(const Property &property)
+{
+ Q_UNUSED(property);
+ return isDerivedFromQUntypedPropertyData<Property>;
+}
+
+template <typename Property>
+constexpr auto isDerivedFromQUntypedPropertyDataFunc(Property *property)
+{
+ Q_UNUSED(property);
+ return isDerivedFromQUntypedPropertyData<Property>;
+}
+} // namespace
+
+void tst_QProperty::inheritQUntypedPropertyData()
+{
+ class propertyPublic : public QUntypedPropertyData
+ {
+ };
+ class propertyPrivate : private QUntypedPropertyData
+ {
+ };
+
+ // Compile time test
+ static_assert(isDerivedFromQUntypedPropertyData<propertyPublic>);
+ static_assert(isDerivedFromQUntypedPropertyData<propertyPrivate>);
+ static_assert(isDerivedFromQUntypedPropertyData<QPropertyData<int>>);
+ static_assert(isDerivedFromQUntypedPropertyData<QProperty<int>>);
+
+ // Run time test
+ propertyPublic _propertyPublic;
+ propertyPrivate _propertyPrivate;
+ QPropertyData<int> qpropertyData;
+ QProperty<int> qproperty;
+ std::unique_ptr<propertyPublic> _propertyPublicPtr{ new propertyPublic };
+ std::unique_ptr<propertyPrivate> _propertyPrivatePtr{ new propertyPrivate };
+ std::unique_ptr<QPropertyData<int>> qpropertyDataPtr{ new QPropertyData<int> };
+ std::unique_ptr<QProperty<int>> qpropertyPtr{ new QProperty<int> };
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPublic));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPrivate));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyData));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qproperty));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPublicPtr.get()));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPrivatePtr.get()));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyDataPtr.get()));
+ QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyPtr.get()));
+}
+
void tst_QProperty::functorBinding()
{
QProperty<int> property([]() { return 42; });
@@ -248,6 +317,7 @@ void tst_QProperty::bindingAfterUse()
void tst_QProperty::bindingFunctionDtorCalled()
{
+ DtorCounter::counter = 0;
DtorCounter dc;
{
QProperty<int> prop;
@@ -386,7 +456,7 @@ void tst_QProperty::changeHandler()
}
testProperty = 3;
- QCOMPARE(recordedValues.count(), 2);
+ QCOMPARE(recordedValues.size(), 2);
QCOMPARE(recordedValues.at(0), 1);
QCOMPARE(recordedValues.at(1), 2);
}
@@ -429,7 +499,7 @@ void tst_QProperty::subscribe()
}
testProperty = 3;
- QCOMPARE(recordedValues.count(), 3);
+ QCOMPARE(recordedValues.size(), 3);
QCOMPARE(recordedValues.at(0), 42);
QCOMPARE(recordedValues.at(1), 1);
QCOMPARE(recordedValues.at(2), 2);
@@ -864,7 +934,7 @@ void tst_QProperty::notifiedProperty()
check();
instance.property.setValue(42);
- QCOMPARE(instance.recordedValues.count(), 1);
+ QCOMPARE(instance.recordedValues.size(), 1);
QCOMPARE(instance.recordedValues.at(0), 42);
instance.recordedValues.clear();
check();
@@ -894,7 +964,7 @@ void tst_QProperty::notifiedProperty()
subscribedCount = 0;
QCOMPARE(instance.property.value(), 100);
- QCOMPARE(instance.recordedValues.count(), 1);
+ QCOMPARE(instance.recordedValues.size(), 1);
QCOMPARE(instance.recordedValues.at(0), 100);
instance.recordedValues.clear();
check();
@@ -902,7 +972,7 @@ void tst_QProperty::notifiedProperty()
injectedValue = 200;
QCOMPARE(instance.property.value(), 200);
- QCOMPARE(instance.recordedValues.count(), 1);
+ QCOMPARE(instance.recordedValues.size(), 1);
QCOMPARE(instance.recordedValues.at(0), 200);
instance.recordedValues.clear();
check();
@@ -911,7 +981,7 @@ void tst_QProperty::notifiedProperty()
injectedValue = 400;
QCOMPARE(instance.property.value(), 400);
- QCOMPARE(instance.recordedValues.count(), 1);
+ QCOMPARE(instance.recordedValues.size(), 1);
QCOMPARE(instance.recordedValues.at(0), 400);
instance.recordedValues.clear();
check();
@@ -1150,12 +1220,12 @@ void tst_QProperty::qobjectBindableManualNotify()
object.fooData.setValueBypassingBindings(42);
// there is no change.
QCOMPARE(fooChangeCount, 0);
- QCOMPARE(fooChangedSpy.count(), 0);
+ QCOMPARE(fooChangedSpy.size(), 0);
// Once we notify manually
object.fooData.notify();
// observers are notified and the signal arrives.
QCOMPARE(fooChangeCount, 1);
- QCOMPARE(fooChangedSpy.count(), 1);
+ QCOMPARE(fooChangedSpy.size(), 1);
// If we set a binding
int i = 1;
@@ -1164,20 +1234,37 @@ void tst_QProperty::qobjectBindableManualNotify()
QCOMPARE(object.foo(), 1);
// and the change and signal count are incremented.
QCOMPARE(fooChangeCount, 2);
- QCOMPARE(fooChangedSpy.count(), 2);
+ QCOMPARE(fooChangedSpy.size(), 2);
// Changing a non-property won't trigger any notification.
i = 2;
QCOMPARE(fooChangeCount, 2);
- QCOMPARE(fooChangedSpy.count(), 2);
+ QCOMPARE(fooChangedSpy.size(), 2);
// Manually triggering the notification
object.fooData.notify();
// increments the change count
QCOMPARE(fooChangeCount, 3);
- QCOMPARE(fooChangedSpy.count(), 3);
+ QCOMPARE(fooChangedSpy.size(), 3);
// but doesn't actually cause a binding reevaluation.
QCOMPARE(object.foo(), 1);
}
+
+struct ReallocObject : QObject {
+ ReallocObject()
+ { v.setBinding([this] { return x.value() + y.value() + z.value(); }); }
+ Q_OBJECT_BINDABLE_PROPERTY(ReallocObject, int, v)
+ Q_OBJECT_BINDABLE_PROPERTY(ReallocObject, int, x)
+ Q_OBJECT_BINDABLE_PROPERTY(ReallocObject, int, y)
+ Q_OBJECT_BINDABLE_PROPERTY(ReallocObject, int, z)
+};
+
+void tst_QProperty::qobjectBindableReallocatedBindingStorage()
+{
+ ReallocObject object;
+ object.x = 1;
+ QCOMPARE(object.v.value(), 1);
+}
+
void tst_QProperty::qobjectBindableSignalTakingNewValue()
{
// Given an object of type MyQObject,
@@ -1465,6 +1552,22 @@ void tst_QProperty::modifyObserverListWhileIterating()
}
}
+void tst_QProperty::noDoubleCapture()
+{
+ QProperty<long long> size;
+ size = 3;
+ QProperty<int> max;
+ max.setBinding([&size]() -> int {
+ // each loop run attempts to capture size
+ for (int i = 0; i < size; ++i) {}
+ return size.value();
+ });
+ auto bindingPriv = QPropertyBindingPrivate::get(max.binding());
+ QCOMPARE(bindingPriv->dependencyObserverCount, 1U);
+ size = 4; // should not crash
+ QCOMPARE(max.value(), 4);
+}
+
class CompatPropertyTester : public QObject
{
Q_OBJECT
@@ -1556,7 +1659,7 @@ void tst_QProperty::compatPropertySignals()
tester.setProp2(10);
QCOMPARE(prop2Observer.value(), 10);
- QCOMPARE(prop2Spy.count(), 1);
+ QCOMPARE(prop2Spy.size(), 1);
QList<QVariant> arguments = prop2Spy.takeFirst();
QCOMPARE(arguments.size(), 1);
QCOMPARE(arguments.at(0).metaType().id(), QMetaType::Int);
@@ -1572,7 +1675,7 @@ void tst_QProperty::compatPropertySignals()
tester.setProp3(5);
QCOMPARE(prop3Observer.value(), 5);
- QCOMPARE(prop3Spy.count(), 1);
+ QCOMPARE(prop3Spy.size(), 1);
// Compat property with signal, default value, and custom setter. Signal has parameter.
QProperty<int> prop4Observer;
@@ -1584,7 +1687,7 @@ void tst_QProperty::compatPropertySignals()
tester.setProp4(10);
QCOMPARE(prop4Observer.value(), 10);
- QCOMPARE(prop4Spy.count(), 1);
+ QCOMPARE(prop4Spy.size(), 1);
arguments = prop4Spy.takeFirst();
QCOMPARE(arguments.size(), 1);
QCOMPARE(arguments.at(0).metaType().id(), QMetaType::Int);
@@ -1593,7 +1696,7 @@ void tst_QProperty::compatPropertySignals()
tester.setProp4(42);
QCOMPARE(prop4Observer.value(), 42);
- QCOMPARE(prop4Spy.count(), 1);
+ QCOMPARE(prop4Spy.size(), 1);
arguments = prop4Spy.takeFirst();
QCOMPARE(arguments.size(), 1);
QCOMPARE(arguments.at(0).metaType().id(), QMetaType::Int);
@@ -1602,7 +1705,7 @@ void tst_QProperty::compatPropertySignals()
tester.setProp4(0);
QCOMPARE(prop4Observer.value(), 42);
- QCOMPARE(prop4Spy.count(), 1);
+ QCOMPARE(prop4Spy.size(), 1);
arguments = prop4Spy.takeFirst();
QCOMPARE(arguments.size(), 1);
QCOMPARE(arguments.at(0).metaType().id(), QMetaType::Int);
@@ -1658,6 +1761,202 @@ void tst_QProperty::noFakeDependencies()
QCOMPARE(old, bindingFunctionCalled);
}
+class PropertyAdaptorTester : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged)
+ Q_PROPERTY(int foo1 READ foo WRITE setFoo)
+
+signals:
+ void dummySignal1();
+ void dummySignal2();
+ void dummySignal3();
+ void dummySignal4();
+ void dummySignal5();
+ void dummySignal6();
+ void dummySignal7();
+ void dummySignal8();
+ void fooChanged(int newFoo);
+
+public slots:
+ void fooHasChanged() { fooChangedCount++; }
+
+public:
+ int foo() const { return fooData; }
+ void setFoo(int i)
+ {
+ if (i != fooData) {
+ fooData = i;
+ fooChanged(fooData);
+ }
+ }
+
+public:
+ int fooData = 0;
+ int fooChangedCount = 0;
+};
+
+void tst_QProperty::propertyAdaptorBinding()
+{
+ QProperty<int> source { 5 };
+ QProperty<int> dest1 { 99 };
+ QProperty<int> dest2 { 98 };
+
+ // Check binding of non BINDABLE property
+ PropertyAdaptorTester object;
+ // set up a dummy connection (needed to verify that the QBindable avoids an out-of-bounds read)
+ QObject::connect(&object, &PropertyAdaptorTester::dummySignal1, [](){});
+ QBindable<int> binding(&object, "foo");
+ QObject::connect(&object, &PropertyAdaptorTester::fooChanged, &object,
+ &PropertyAdaptorTester::fooHasChanged);
+ binding.setBinding([&]() { return source + 1; });
+ QCOMPARE(object.foo(), 6);
+ QCOMPARE(object.fooChangedCount, 1);
+
+ struct MyBindable : QBindable<int> {
+ using QBindable<int>::QBindable;
+ QtPrivate::QPropertyAdaptorSlotObject* data() {
+ return static_cast<QtPrivate::QPropertyAdaptorSlotObject*>(QUntypedBindable::data);
+ }
+ } dataBinding(&object, "foo");
+ QPropertyBindingDataPointer data{&dataBinding.data()->bindingData()};
+
+ QCOMPARE(data.observerCount(), 0);
+ dest1.setBinding(binding.makeBinding());
+ QCOMPARE(data.observerCount(), 1);
+ dest2.setBinding([=]() { return binding.value() + 1; });
+ binding = {};
+ QCOMPARE(data.observerCount(), 2);
+
+ // Check addNotifer
+ {
+ int local_foo = 0;
+ auto notifier = QBindable<int>(&object, "foo").addNotifier([&]() { local_foo++; });
+ QCOMPARE(data.observerCount(), 3);
+ QCOMPARE(object.foo(), 6);
+ QCOMPARE(dest1.value(), 6);
+ QCOMPARE(dest2.value(), 7);
+ QCOMPARE(local_foo, 0);
+ QCOMPARE(object.fooChangedCount, 1);
+
+ source = 7;
+ QCOMPARE(object.foo(), 8);
+ QCOMPARE(dest1.value(), 8);
+ QCOMPARE(dest2.value(), 9);
+ QCOMPARE(local_foo, 1);
+ QCOMPARE(object.fooChangedCount, 2);
+ }
+
+ QCOMPARE(data.observerCount(), 2);
+
+ // Check a new QBindable object can override the existing binding
+ QBindable<int>(&object, "foo").setValue(10);
+ QCOMPARE(object.foo(), 10);
+ QCOMPARE(dest1.value(), 10);
+ QCOMPARE(dest2.value(), 11);
+ QCOMPARE(object.fooChangedCount, 3);
+ source.setValue(99);
+ QCOMPARE(object.foo(), 10);
+ QCOMPARE(dest1.value(), 10);
+ QCOMPARE(dest2.value(), 11);
+ QCOMPARE(object.fooChangedCount, 3);
+ object.setFoo(12);
+ QCOMPARE(object.foo(), 12);
+ QCOMPARE(dest1.value(), 12);
+ QCOMPARE(dest2.value(), 13);
+ QCOMPARE(object.fooChangedCount, 4);
+
+ // Check binding multiple notifiers
+ QProperty<int> source2 { 20 };
+ source.setValue(21);
+ binding = QBindable<int>(&object, "foo");
+ binding.setBinding([&]() { return source + source2; });
+ QCOMPARE(object.foo(), 41);
+ QCOMPARE(dest1.value(), 41);
+ QCOMPARE(object.fooChangedCount, 5);
+ source.setValue(22);
+ QCOMPARE(object.foo(), 42);
+ QCOMPARE(dest1.value(), 42);
+ QCOMPARE(object.fooChangedCount, 6);
+ source2.setValue(21);
+ QCOMPARE(object.foo(), 43);
+ QCOMPARE(dest1.value(), 43);
+ QCOMPARE(object.fooChangedCount, 7);
+
+ // Check update group
+ {
+ const QScopedPropertyUpdateGroup guard;
+ source.setValue(23);
+ source2.setValue(22);
+ QCOMPARE(object.foo(), 43);
+ QCOMPARE(dest1.value(), 43);
+ QCOMPARE(object.fooChangedCount, 7);
+ }
+ QCOMPARE(object.foo(), 45);
+ QCOMPARE(dest1.value(), 45);
+ QCOMPARE(object.fooChangedCount, 8);
+
+ PropertyAdaptorTester object2;
+ PropertyAdaptorTester object3;
+
+ // Check multiple observers
+ QBindable<int> binding2(&object2, "foo");
+ QBindable<int> binding3(&object3, "foo");
+ binding.setBinding([=]() { return binding2.value(); });
+ binding3.setBinding([=]() { return binding.value(); });
+ QCOMPARE(object.foo(), 0);
+ QCOMPARE(object2.foo(), 0);
+ QCOMPARE(object3.foo(), 0);
+ QCOMPARE(dest1.value(), 0);
+ object2.setFoo(1);
+ QCOMPARE(object.foo(), 1);
+ QCOMPARE(object2.foo(), 1);
+ QCOMPARE(object3.foo(), 1);
+ QCOMPARE(dest1.value(), 1);
+
+ // Check interoperation with BINDABLE properties
+ MyQObject bindableObject;
+ bindableObject.fooData.setBinding([]() { return 5; });
+ QVERIFY(bindableObject.fooData.hasBinding());
+ QVERIFY(!bindableObject.barData.hasBinding());
+ QVERIFY(QBindable<int>(&bindableObject, "foo").hasBinding());
+ QBindable<int> bindableBar(&bindableObject, "bar");
+ QVERIFY(!bindableBar.hasBinding());
+ bindableBar.setBinding([]() { return 6; });
+ QVERIFY(bindableBar.hasBinding());
+ QVERIFY(bindableObject.barData.hasBinding());
+
+ // Check bad arguments
+#ifndef QT_NO_DEBUG
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QUntypedBindable: Property is not valid");
+#endif
+ QVERIFY(!QBindable<int>(&object, QMetaProperty{}).isValid());
+#ifndef QT_NO_DEBUG
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QUntypedBindable: Property foo1 has no notify signal");
+#endif
+ QVERIFY(!QBindable<int>(&object, "foo1").isValid());
+#ifndef QT_NO_DEBUG
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QUntypedBindable: Property foo of type int does not match requested type bool");
+#endif
+ QVERIFY(!QBindable<bool>(&object, "foo").isValid());
+#ifndef QT_NO_DEBUG
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg,
+ "QUntypedBindable: Property foo does not belong to this object");
+#endif
+ QObject qobj;
+ QVERIFY(!QBindable<int>(
+ &qobj,
+ object.metaObject()->property(object.metaObject()->indexOfProperty("foo")))
+ .isValid());
+#ifndef QT_NO_DEBUG
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QUntypedBindable: No property named fizz");
+ QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QUntypedBindable: Property is not valid");
+#endif
+ QVERIFY(!QBindable<int>(&object, "fizz").isValid());
+}
+
+#if QT_CONFIG(thread)
struct ThreadSafetyTester : public QObject
{
Q_OBJECT
@@ -1694,7 +1993,7 @@ void tst_QProperty::threadSafety()
auto child1 = new ThreadSafetyTester(obj1);
obj1->moveToThread(&workerThread);
const auto mainThreadBindingStatus = QtPrivate::getBindingStatus({});
- QCOMPARE(qGetBindingStorage(child1)->status({}), mainThreadBindingStatus);
+ QCOMPARE(qGetBindingStorage(child1)->status({}), nullptr);
workerThread.start();
bool correctStatus = false;
@@ -1743,6 +2042,42 @@ void tst_QProperty::threadSafety()
QCOMPARE(obj3->objectName(), "moved again");
}
+class QPropertyUsingThread : public QThread
+{
+public:
+ QPropertyUsingThread(QObject **dest, QThread *destThread) : dest(dest), destThread(destThread) {}
+ void run() override
+ {
+ scopedObj1.reset(new ThreadSafetyTester());
+ scopedObj1->setObjectName("test");
+ QObject *child = new ThreadSafetyTester(scopedObj1.get());
+ child->setObjectName("child");
+ exec();
+ scopedObj1->moveToThread(destThread);
+ *dest = scopedObj1.release();
+ }
+ std::unique_ptr<ThreadSafetyTester> scopedObj1;
+ QObject **dest;
+ QThread *destThread;
+};
+
+void tst_QProperty::threadSafety2()
+{
+ std::unique_ptr<QObject> movedObj;
+ {
+ QObject *tmp = nullptr;
+ QPropertyUsingThread workerThread(&tmp, QThread::currentThread());
+ workerThread.start();
+ workerThread.quit();
+ workerThread.wait();
+ movedObj.reset(tmp);
+ }
+
+ QCOMPARE(movedObj->objectName(), "test");
+ QCOMPARE(movedObj->children().first()->objectName(), "child");
+}
+#endif // QT_CONFIG(thread)
+
struct CustomType
{
CustomType() = default;
@@ -1854,27 +2189,29 @@ void tst_QProperty::groupedNotifications()
QCOMPARE(nNotifications, 1);
expected = 2;
- Qt::beginPropertyUpdateGroup();
- a = 1;
- QCOMPARE(b.value(), 0);
- QCOMPARE(c.value(), 0);
- QCOMPARE(d.value(), 0);
- QCOMPARE(nNotifications, 1);
- Qt::endPropertyUpdateGroup();
+ {
+ const QScopedPropertyUpdateGroup guard;
+ a = 1;
+ QCOMPARE(b.value(), 0);
+ QCOMPARE(c.value(), 0);
+ QCOMPARE(d.value(), 0);
+ QCOMPARE(nNotifications, 1);
+ }
QCOMPARE(b.value(), 1);
QCOMPARE(c.value(), 1);
QCOMPARE(e.value(), 2);
QCOMPARE(nNotifications, 2);
expected = 7;
- Qt::beginPropertyUpdateGroup();
- a = 2;
- d = 3;
- QCOMPARE(b.value(), 1);
- QCOMPARE(c.value(), 1);
- QCOMPARE(d.value(), 3);
- QCOMPARE(nNotifications, 2);
- Qt::endPropertyUpdateGroup();
+ {
+ const QScopedPropertyUpdateGroup guard;
+ a = 2;
+ d = 3;
+ QCOMPARE(b.value(), 1);
+ QCOMPARE(c.value(), 1);
+ QCOMPARE(d.value(), 3);
+ QCOMPARE(nNotifications, 2);
+ }
QCOMPARE(b.value(), 2);
QCOMPARE(c.value(), 2);
QCOMPARE(e.value(), 7);
@@ -1897,13 +2234,71 @@ void tst_QProperty::groupedNotificationConsistency()
j = 1;
QVERIFY(!areEqual); // value changed runs before j = 1
- Qt::beginPropertyUpdateGroup();
- i = 2;
- j = 2;
- Qt::endPropertyUpdateGroup();
+ {
+ const QScopedPropertyUpdateGroup guard;
+ i = 2;
+ j = 2;
+ }
QVERIFY(areEqual); // value changed runs after everything has been evaluated
}
+void tst_QProperty::bindingGroupMovingBindingData()
+{
+ auto tester = std::make_unique<ClassWithNotifiedProperty>();
+ auto testerPriv = QObjectPrivate::get(tester.get());
+
+ auto dummyNotifier = tester->property.addNotifier([](){});
+ auto bindingData = testerPriv->bindingStorage.bindingData(&tester->property);
+ QVERIFY(bindingData); // we have a notifier, so there should be binding data
+
+ Qt::beginPropertyUpdateGroup();
+ auto cleanup = qScopeGuard([](){ Qt::endPropertyUpdateGroup(); });
+ tester->property = 42;
+ QCOMPARE(testerPriv->bindingStorage.bindingData(&tester->property), bindingData);
+ auto proxyData = QPropertyBindingDataPointer::proxyData(bindingData);
+ // as we've modified the property, we now should have a proxy for the delayed notification
+ QVERIFY(proxyData);
+ // trigger binding data reallocation
+ std::array<QUntypedPropertyData, 10> propertyDataArray;
+ for (auto&& data: propertyDataArray)
+ testerPriv->bindingStorage.bindingData(&data, true);
+ // binding data has moved
+ QVERIFY(testerPriv->bindingStorage.bindingData(&tester->property) != bindingData);
+ bindingData = testerPriv->bindingStorage.bindingData(&tester->property);
+ // the proxy data has been updated
+ QCOMPARE(proxyData->originalBindingData, bindingData);
+
+ tester.reset();
+ // the property data is gone, proxyData should have been informed
+ QCOMPARE(proxyData->originalBindingData, nullptr);
+ QVERIFY(proxyData);
+}
+
+void tst_QProperty::bindingGroupBindingDeleted()
+{
+ auto deleter = std::make_unique<ClassWithNotifiedProperty>();
+ auto toBeDeleted = std::make_unique<ClassWithNotifiedProperty>();
+
+ bool calledHandler = false;
+ deleter->property.setBinding([&](){
+ int newValue = toBeDeleted->property;
+ if (newValue == 42)
+ toBeDeleted.reset();
+ return newValue;
+ });
+ auto handler = toBeDeleted->property.onValueChanged([&]() { calledHandler = true; } );
+ {
+ Qt::beginPropertyUpdateGroup();
+ auto cleanup = qScopeGuard([](){ Qt::endPropertyUpdateGroup(); });
+ QVERIFY(toBeDeleted);
+ toBeDeleted->property = 42;
+ // ASAN should not complain here
+ }
+ QVERIFY(!toBeDeleted);
+ // the change notification is sent, even if the binding is deleted during evaluation
+ QVERIFY(calledHandler);
+}
+
void tst_QProperty::uninstalledBindingDoesNotEvaluate()
{
QProperty<int> i;
@@ -1955,7 +2350,7 @@ void tst_QProperty::notify()
testProperty = 4;
QCOMPARE(value, 3);
- QCOMPARE(recordedValues.count(), 2);
+ QCOMPARE(recordedValues.size(), 2);
QCOMPARE(recordedValues.at(0), 1);
QCOMPARE(recordedValues.at(1), 2);
}
@@ -1978,17 +2373,24 @@ void tst_QProperty::selfBindingShouldNotCrash()
void tst_QProperty::qpropertyAlias()
{
+#if QT_DEPRECATED_SINCE(6, 6)
+ QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
std::unique_ptr<QProperty<int>> i {new QProperty<int>};
QPropertyAlias<int> alias(i.get());
QVERIFY(alias.isValid());
alias.setValue(42);
QCOMPARE(i->value(), 42);
QProperty<int> j;
+ bool notifierCalled = false;
+ auto myNotifier = alias.addNotifier([&](){notifierCalled = true;});
i->setBinding([&]() -> int { return j; });
+ QVERIFY(notifierCalled);
j.setValue(42);
QCOMPARE(alias.value(), 42);
i.reset();
QVERIFY(!alias.isValid());
+ QT_WARNING_POP
+#endif
}
void tst_QProperty::scheduleNotify()
@@ -2006,6 +2408,173 @@ void tst_QProperty::scheduleNotify()
QCOMPARE(p.value(), 0);
}
+void tst_QProperty::notifyAfterAllDepsGone()
+{
+ bool b = true;
+ QProperty<int> iprop;
+ QProperty<int> jprop(42);
+ iprop.setBinding([&](){
+ if (b)
+ return jprop.value();
+ return 13;
+ });
+ int changeCounter = 0;
+ auto keepAlive = iprop.onValueChanged([&](){ changeCounter++; });
+ QCOMPARE(iprop.value(), 42);
+ jprop = 44;
+ QCOMPARE(iprop.value(), 44);
+ QCOMPARE(changeCounter, 1);
+ b = false;
+ jprop = 43;
+ QCOMPARE(iprop.value(), 13);
+ QCOMPARE(changeCounter, 2);
+}
+
+class TestObject : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int signaled READ signaled WRITE setSignaled NOTIFY signaledChanged FINAL)
+ Q_PROPERTY(int bindable1 READ bindable1 WRITE setBindable1 BINDABLE bindable1Bindable NOTIFY bindable1Changed FINAL)
+ Q_PROPERTY(int bindable2 READ bindable2 WRITE setBindable2 BINDABLE bindable2Bindable NOTIFY bindable2Changed FINAL)
+
+public:
+ int signaled() const
+ {
+ return m_signaled;
+ }
+
+ void setSignaled(int newSignaled)
+ {
+ if (m_signaled == newSignaled)
+ return;
+ m_signaled = newSignaled;
+ emit signaledChanged();
+ }
+
+ int bindable1() const
+ {
+ return m_bindable1;
+ }
+
+ void setBindable1(int newBindable1)
+ {
+ if (m_bindable1 == newBindable1)
+ return;
+ m_bindable1 = newBindable1;
+ emit bindable1Changed();
+ }
+
+ QBindable<int> bindable1Bindable()
+ {
+ return QBindable<int>(&m_bindable1);
+ }
+
+ int bindable2() const
+ {
+ return m_bindable2;
+ }
+
+ void setBindable2(int newBindable2)
+ {
+ if (m_bindable2 == newBindable2)
+ return;
+ m_bindable2 = newBindable2;
+ emit bindable2Changed();
+ }
+
+ QBindable<int> bindable2Bindable()
+ {
+ return QBindable<int>(&m_bindable2);
+ }
+
+signals:
+ void signaledChanged();
+ void bindable1Changed();
+ void bindable2Changed();
+
+private:
+ int m_signaled = 0;
+ Q_OBJECT_COMPAT_PROPERTY(TestObject, int, m_bindable1, &TestObject::setBindable1, &TestObject::bindable1Changed);
+ Q_OBJECT_COMPAT_PROPERTY(TestObject, int, m_bindable2, &TestObject::setBindable2, &TestObject::bindable2Changed);
+};
+
+void tst_QProperty::propertyUpdateViaSignaledProperty()
+{
+ TestObject o;
+ QProperty<int> rootTrigger;
+ QProperty<int> signalTrigger;
+
+ o.bindable1Bindable().setBinding([&]() {
+ return rootTrigger.value();
+ });
+
+ QObject::connect(&o, &TestObject::bindable1Changed, &o, [&]() {
+ // Signaled changes only once, doesn't actually depend on bindable1.
+ // In reality, there could be some complicated calculation behind this that changes
+ // on certain checkpoints, but not on every iteration.
+ o.setSignaled(40);
+ });
+
+ o.bindable2Bindable().setBinding([&]() {
+ return signalTrigger.value() - o.bindable1();
+ });
+
+ QObject::connect(&o, &TestObject::signaledChanged, &o, [&]() {
+ signalTrigger.setValue(o.signaled());
+ });
+
+ rootTrigger.setValue(2);
+ QCOMPARE(o.bindable1(), 2);
+ QCOMPARE(o.bindable2(), 38);
+ rootTrigger.setValue(3);
+ QCOMPARE(o.bindable1(), 3);
+ QCOMPARE(o.bindable2(), 37);
+ rootTrigger.setValue(4);
+ QCOMPARE(o.bindable1(), 4);
+ QCOMPARE(o.bindable2(), 36);
+}
+
+void tst_QProperty::derefFromObserver()
+{
+ int triggered = 0;
+ QProperty<int> source(11);
+
+ DtorCounter::counter = 0;
+ DtorCounter dc;
+
+ QProperty<int> target([&triggered, &source, dc]() mutable {
+ dc.shouldIncrement = true;
+ return ++triggered + source.value();
+ });
+ QCOMPARE(triggered, 1);
+
+ {
+ auto propObserver = std::make_unique<QPropertyObserver>();
+ QPropertyObserverPointer propObserverPtr { propObserver.get() };
+ propObserverPtr.setBindingToNotify(QPropertyBindingPrivate::get(target.binding()));
+
+ QBindingObserverPtr bindingPtr(propObserver.get());
+
+ QCOMPARE(triggered, 1);
+ source = 25;
+ QCOMPARE(triggered, 2);
+ QCOMPARE(target, 27);
+
+ target.setBinding([]() { return 8; });
+ QCOMPARE(target, 8);
+
+ // The QBindingObserverPtr still holds on to the binding.
+ QCOMPARE(dc.counter, 0);
+ }
+
+ // The binding is actually gone now.
+ QCOMPARE(dc.counter, 1);
+
+ source = 26;
+ QCOMPARE(triggered, 2);
+ QCOMPARE(target, 8);
+}
+
QTEST_MAIN(tst_QProperty);
#undef QT_SOURCE_LOCATION_NAMESPACE
diff --git a/tests/auto/corelib/kernel/qsharedmemory/CMakeLists.txt b/tests/auto/corelib/kernel/qsharedmemory/CMakeLists.txt
deleted file mode 100644
index 5553dfd56b..0000000000
--- a/tests/auto/corelib/kernel/qsharedmemory/CMakeLists.txt
+++ /dev/null
@@ -1,24 +0,0 @@
-if(QT_FEATURE_sharedmemory)
- #####################################################################
- ## tst_qsharedmemory Test:
- #####################################################################
-
- qt_internal_add_test(tst_qsharedmemory
- SOURCES
- tst_qsharedmemory.cpp
- PUBLIC_LIBRARIES
- Qt::CorePrivate
- )
-
- ## Scopes:
- #####################################################################
-
- qt_internal_extend_target(tst_qsharedmemory CONDITION LINUX
- PUBLIC_LIBRARIES
- rt
- )
- add_subdirectory(producerconsumer)
- if(QT_FEATURE_process)
- add_dependencies(tst_qsharedmemory producerconsumer_helper)
- endif()
-endif()
diff --git a/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/CMakeLists.txt b/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/CMakeLists.txt
deleted file mode 100644
index 610e2d507f..0000000000
--- a/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-# Generated from producerconsumer.pro.
-
-#####################################################################
-## producerconsumer_helper Binary:
-#####################################################################
-
-qt_internal_add_test_helper(producerconsumer_helper
- SOURCES
- main.cpp
- PUBLIC_LIBRARIES
- Qt::Test
-)
diff --git a/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/main.cpp b/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/main.cpp
deleted file mode 100644
index 968b45aae1..0000000000
--- a/tests/auto/corelib/kernel/qsharedmemory/producerconsumer/main.cpp
+++ /dev/null
@@ -1,173 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-
-#include <QSharedMemory>
-#include <QStringList>
-#include <QDebug>
-#include <QTest>
-#include <stdio.h>
-
-void set(QSharedMemory &sm, int pos, char value)
-{
- ((char*)sm.data())[pos] = value;
-}
-
-QChar get(QSharedMemory &sm, int i)
-{
- return QChar::fromLatin1(((char*)sm.data())[i]);
-}
-
-int readonly_segfault()
-{
- QSharedMemory sharedMemory;
- sharedMemory.setKey("readonly_segfault");
- sharedMemory.create(1024, QSharedMemory::ReadOnly);
- sharedMemory.lock();
- set(sharedMemory, 0, 'a');
- sharedMemory.unlock();
- return EXIT_SUCCESS;
-}
-
-int producer()
-{
- QSharedMemory producer;
- producer.setKey("market");
-
- int size = 1024;
- if (!producer.create(size)) {
- if (producer.error() == QSharedMemory::AlreadyExists) {
- if (!producer.attach()) {
- qWarning() << "Could not attach to" << producer.key();
- return EXIT_FAILURE;
- }
- } else {
- qWarning() << "Could not create" << producer.key();
- return EXIT_FAILURE;
- }
- }
- // tell parent we're ready
- //qDebug("producer created and attached");
- puts("");
- fflush(stdout);
-
- if (!producer.lock()) {
- qWarning() << "Could not lock" << producer.key();
- return EXIT_FAILURE;
- }
- set(producer, 0, 'Q');
- if (!producer.unlock()) {
- qWarning() << "Could not lock" << producer.key();
- return EXIT_FAILURE;
- }
-
- int i = 0;
- while (i < 5) {
- if (!producer.lock()) {
- qWarning() << "Could not lock" << producer.key();
- return EXIT_FAILURE;
- }
- if (get(producer, 0) == 'Q') {
- if (!producer.unlock()) {
- qWarning() << "Could not unlock" << producer.key();
- return EXIT_FAILURE;
- }
- QTest::qSleep(1);
- continue;
- }
- //qDebug() << "producer:" << i);
- ++i;
- set(producer, 0, 'Q');
- if (!producer.unlock()) {
- qWarning() << "Could not unlock" << producer.key();
- return EXIT_FAILURE;
- }
- QTest::qSleep(1);
- }
- if (!producer.lock()) {
- qWarning() << "Could not lock" << producer.key();
- return EXIT_FAILURE;
- }
- set(producer, 0, 'E');
- if (!producer.unlock()) {
- qWarning() << "Could not unlock" << producer.key();
- return EXIT_FAILURE;
- }
-
- //qDebug("producer done");
-
- // Sleep for a bit to let all consumers exit
- getchar();
- return EXIT_SUCCESS;
-}
-
-int consumer()
-{
- QSharedMemory consumer;
- consumer.setKey("market");
-
- //qDebug("consumer starting");
- int tries = 0;
- while (!consumer.attach()) {
- if (tries == 5000) {
- qWarning() << "consumer exiting, waiting too long";
- return EXIT_FAILURE;
- }
- ++tries;
- QTest::qSleep(1);
- }
- //qDebug("consumer attached");
-
-
- int i = 0;
- while (true) {
- if (!consumer.lock()) {
- qWarning() << "Could not lock" << consumer.key();
- return EXIT_FAILURE;
- }
- if (get(consumer, 0) == 'Q') {
- set(consumer, 0, ++i);
- //qDebug() << "consumer sets" << i;
- }
- if (get(consumer, 0) == 'E') {
- if (!consumer.unlock()) {
- qWarning() << "Could not unlock" << consumer.key();
- return EXIT_FAILURE;
- }
- break;
- }
- if (!consumer.unlock()) {
- qWarning() << "Could not unlock" << consumer.key();
- return EXIT_FAILURE;
- }
- QTest::qSleep(10);
- }
-
- //qDebug("consumer detaching");
- if (!consumer.detach()) {
- qWarning() << "Could not detach" << consumer.key();
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
-}
-
-int main(int argc, char *argv[])
-{
- QCoreApplication app(argc, argv);
-
- QStringList arguments = app.arguments();
- if (app.arguments().count() != 2) {
- qWarning("Please call the helper with the function to call as argument");
- return EXIT_FAILURE;
- }
- QString function = arguments.at(1);
- if (function == QLatin1String("readonly_segfault"))
- return readonly_segfault();
- else if (function == QLatin1String("producer"))
- return producer();
- else if (function == QLatin1String("consumer"))
- return consumer();
- else
- qWarning() << "Unknown function" << arguments.at(1);
-
- return EXIT_SUCCESS;
-}
diff --git a/tests/auto/corelib/kernel/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/corelib/kernel/qsharedmemory/tst_qsharedmemory.cpp
deleted file mode 100644
index 63a33d73a0..0000000000
--- a/tests/auto/corelib/kernel/qsharedmemory/tst_qsharedmemory.cpp
+++ /dev/null
@@ -1,811 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-
-#include <QDebug>
-#include <QFile>
-#if QT_CONFIG(process)
-# include <QProcess>
-#endif
-#include <QSharedMemory>
-#include <QTest>
-#include <QThread>
-#include <QElapsedTimer>
-
-#define EXISTING_SHARE "existing"
-#define EXISTING_SIZE 1024
-
-Q_DECLARE_METATYPE(QSharedMemory::SharedMemoryError)
-Q_DECLARE_METATYPE(QSharedMemory::AccessMode)
-
-class tst_QSharedMemory : public QObject
-{
- Q_OBJECT
-
-public:
- tst_QSharedMemory();
- virtual ~tst_QSharedMemory();
-
-public Q_SLOTS:
- void init();
- void cleanup();
-
-
-private slots:
- // basics
- void constructor();
- void key_data();
- void key();
- void create_data();
- void create();
- void attach_data();
- void attach();
- void lock();
-
- // custom edge cases
-#ifndef Q_OS_HPUX
- void removeWhileAttached();
-#endif
- void emptyMemory();
-#if !defined(Q_OS_WIN)
- void readOnly();
-#endif
-
- // basics all together
-#ifndef Q_OS_HPUX
- void simpleProducerConsumer_data();
- void simpleProducerConsumer();
- void simpleDoubleProducerConsumer();
-#endif
-
- // with threads
- void simpleThreadedProducerConsumer_data();
- void simpleThreadedProducerConsumer();
-
- // with processes
- void simpleProcessProducerConsumer_data();
- void simpleProcessProducerConsumer();
-
- // extreme cases
- void useTooMuchMemory();
-#if !defined(Q_OS_HPUX)
- void attachTooMuch();
-#endif
-
- // unique keys
- void uniqueKey_data();
- void uniqueKey();
-
-protected:
- int remove(const QString &key);
-
- QString rememberKey(const QString &key)
- {
- if (key == EXISTING_SHARE)
- return key;
- if (!keys.contains(key)) {
- keys.append(key);
- remove(key);
- }
- return key;
- }
-
- QStringList keys;
- QList<QSharedMemory*> jail;
- QSharedMemory *existingSharedMemory;
-
-private:
- const QString m_helperBinary;
-};
-
-tst_QSharedMemory::tst_QSharedMemory()
- : existingSharedMemory(0)
- , m_helperBinary("./producerconsumer_helper")
-{
-}
-
-tst_QSharedMemory::~tst_QSharedMemory()
-{
-}
-
-void tst_QSharedMemory::init()
-{
- existingSharedMemory = new QSharedMemory(EXISTING_SHARE);
- if (!existingSharedMemory->create(EXISTING_SIZE)) {
- QCOMPARE(existingSharedMemory->error(), QSharedMemory::AlreadyExists);
- }
-}
-
-void tst_QSharedMemory::cleanup()
-{
- delete existingSharedMemory;
- qDeleteAll(jail.begin(), jail.end());
- jail.clear();
-
- keys.append(EXISTING_SHARE);
- for (int i = 0; i < keys.count(); ++i) {
- QSharedMemory sm(keys.at(i));
- if (!sm.create(1024)) {
- //if (sm.error() != QSharedMemory::KeyError)
- // qWarning() << "test cleanup: remove failed:" << keys.at(i) << sm.error() << sm.errorString();
- sm.attach();
- sm.detach();
- remove(keys.at(i));
- }
- }
-}
-
-#ifndef Q_OS_WIN
-#include <private/qsharedmemory_p.h>
-#include <sys/types.h>
-#ifndef QT_POSIX_IPC
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#else
-#include <sys/mman.h>
-#endif // QT_POSIX_IPC
-#include <errno.h>
-#endif
-
-int tst_QSharedMemory::remove(const QString &key)
-{
-#ifdef Q_OS_WIN
- Q_UNUSED(key);
- return 0;
-#else
- // On unix the shared memory might exists from a previously failed test
- // or segfault, remove it it does
- if (key.isEmpty())
- return -1;
-
- QString fileName = QSharedMemoryPrivate::makePlatformSafeKey(key);
-
-#ifndef QT_POSIX_IPC
- // ftok requires that an actual file exists somewhere
- if (!QFile::exists(fileName)) {
- //qDebug() << "exits failed";
- return -2;
- }
-
- int unix_key = ftok(fileName.toLatin1().constData(), 'Q');
- if (-1 == unix_key) {
- qDebug() << "ftok failed";
- return -3;
- }
-
- int id = shmget(unix_key, 0, 0600);
- if (-1 == id) {
- qDebug() << "shmget failed" << strerror(errno);
- return -4;
- }
-
- struct shmid_ds shmid_ds;
- if (-1 == shmctl(id, IPC_RMID, &shmid_ds)) {
- qDebug() << "shmctl failed";
- return -5;
- }
-#else
- if (shm_unlink(QFile::encodeName(fileName).constData()) == -1) {
- if (errno != ENOENT) {
- qDebug() << "shm_unlink failed";
- return -5;
- }
- }
-#endif // QT_POSIX_IPC
-
- return QFile::remove(fileName);
-#endif // Q_OS_WIN
-}
-
-/*!
- Tests the default values
- */
-void tst_QSharedMemory::constructor()
-{
- QSharedMemory sm;
- QCOMPARE(sm.key(), QString());
- QVERIFY(!sm.isAttached());
- QVERIFY(!sm.data());
- QCOMPARE(sm.size(), 0);
- QCOMPARE(sm.error(), QSharedMemory::NoError);
- QCOMPARE(sm.errorString(), QString());
-}
-
-void tst_QSharedMemory::key_data()
-{
- QTest::addColumn<QString>("constructorKey");
- QTest::addColumn<QString>("setKey");
- QTest::addColumn<QString>("setNativeKey");
-
- QTest::newRow("null, null, null") << QString() << QString() << QString();
- QTest::newRow("one, null, null") << QString("one") << QString() << QString();
- QTest::newRow("null, one, null") << QString() << QString("one") << QString();
- QTest::newRow("null, null, one") << QString() << QString() << QString("one");
- QTest::newRow("one, two, null") << QString("one") << QString("two") << QString();
- QTest::newRow("one, null, two") << QString("one") << QString() << QString("two");
- QTest::newRow("null, one, two") << QString() << QString("one") << QString("two");
- QTest::newRow("one, two, three") << QString("one") << QString("two") << QString("three");
- QTest::newRow("invalid") << QString("o/e") << QString("t/o") << QString("|x");
-}
-
-/*!
- Basic key testing
- */
-void tst_QSharedMemory::key()
-{
- QFETCH(QString, constructorKey);
- QFETCH(QString, setKey);
- QFETCH(QString, setNativeKey);
-
- QSharedMemory sm(constructorKey);
- QCOMPARE(sm.key(), constructorKey);
- QCOMPARE(sm.nativeKey().isEmpty(), constructorKey.isEmpty());
- sm.setKey(setKey);
- QCOMPARE(sm.key(), setKey);
- QCOMPARE(sm.nativeKey().isEmpty(), setKey.isEmpty());
- sm.setNativeKey(setNativeKey);
- QVERIFY(sm.key().isNull());
- QCOMPARE(sm.nativeKey(), setNativeKey);
- QCOMPARE(sm.isAttached(), false);
-
- QCOMPARE(sm.error(), QSharedMemory::NoError);
- QCOMPARE(sm.errorString(), QString());
- QVERIFY(!sm.data());
- QCOMPARE(sm.size(), 0);
-
- QCOMPARE(sm.detach(), false);
-}
-
-void tst_QSharedMemory::create_data()
-{
- QTest::addColumn<QString>("key");
- QTest::addColumn<int>("size");
- QTest::addColumn<bool>("canCreate");
- QTest::addColumn<QSharedMemory::SharedMemoryError>("error");
-
- QTest::newRow("null key") << QString() << 1024
- << false << QSharedMemory::KeyError;
- QTest::newRow("-1 size") << QString("negsize") << -1
- << false << QSharedMemory::InvalidSize;
- QTest::newRow("nor size") << QString("norsize") << 1024
- << true << QSharedMemory::NoError;
- QTest::newRow("already exists") << QString(EXISTING_SHARE) << EXISTING_SIZE
- << false << QSharedMemory::AlreadyExists;
-}
-
-/*!
- Basic create testing
- */
-void tst_QSharedMemory::create()
-{
- QFETCH(QString, key);
- QFETCH(int, size);
- QFETCH(bool, canCreate);
- QFETCH(QSharedMemory::SharedMemoryError, error);
-
- QSharedMemory sm(rememberKey(key));
- QCOMPARE(sm.create(size), canCreate);
- if (sm.error() != error)
- qDebug() << sm.errorString();
- QCOMPARE(sm.key(), key);
- if (canCreate) {
- QCOMPARE(sm.errorString(), QString());
- QVERIFY(sm.data() != 0);
- QVERIFY(sm.size() != 0);
- } else {
- QVERIFY(!sm.data());
- QVERIFY(sm.errorString() != QString());
- }
-}
-
-void tst_QSharedMemory::attach_data()
-{
- QTest::addColumn<QString>("key");
- QTest::addColumn<bool>("exists");
- QTest::addColumn<QSharedMemory::SharedMemoryError>("error");
-
- QTest::newRow("null key") << QString() << false << QSharedMemory::KeyError;
- QTest::newRow("doesn't exists") << QString("doesntexists") << false << QSharedMemory::NotFound;
-
- // HPUX doesn't allow for multiple attaches per process.
-#ifndef Q_OS_HPUX
- QTest::newRow("already exists") << QString(EXISTING_SHARE) << true << QSharedMemory::NoError;
-#endif
-}
-
-/*!
- Basic attach/detach testing
- */
-void tst_QSharedMemory::attach()
-{
- QFETCH(QString, key);
- QFETCH(bool, exists);
- QFETCH(QSharedMemory::SharedMemoryError, error);
-
- QSharedMemory sm(key);
- QCOMPARE(sm.attach(), exists);
- QCOMPARE(sm.isAttached(), exists);
- QCOMPARE(sm.error(), error);
- QCOMPARE(sm.key(), key);
- if (exists) {
- QVERIFY(sm.data() != 0);
- QVERIFY(sm.size() != 0);
- QCOMPARE(sm.errorString(), QString());
- QVERIFY(sm.detach());
- // Make sure detach doesn't screw up something and we can't re-attach.
- QVERIFY(sm.attach());
- QVERIFY(sm.data() != 0);
- QVERIFY(sm.size() != 0);
- QVERIFY(sm.detach());
- QCOMPARE(sm.size(), 0);
- QVERIFY(!sm.data());
- } else {
- QVERIFY(!sm.data());
- QCOMPARE(sm.size(), 0);
- QVERIFY(sm.errorString() != QString());
- QVERIFY(!sm.detach());
- }
-}
-
-void tst_QSharedMemory::lock()
-{
- QSharedMemory shm;
- QVERIFY(!shm.lock());
- QCOMPARE(shm.error(), QSharedMemory::LockError);
-
- shm.setKey(rememberKey(QLatin1String("qsharedmemory")));
-
- QVERIFY(!shm.lock());
- QCOMPARE(shm.error(), QSharedMemory::LockError);
-
- QVERIFY(shm.create(100));
- QVERIFY(shm.lock());
- QTest::ignoreMessage(QtWarningMsg, "QSharedMemory::lock: already locked");
- QVERIFY(shm.lock());
- // we didn't unlock(), so ignore the warning from auto-detach in destructor
- QTest::ignoreMessage(QtWarningMsg, "QSharedMemory::lock: already locked");
-}
-
-/*!
- Other shared memory are allowed to be attached after we remove,
- but new shared memory are not allowed to attach after a remove.
- */
-// HPUX doesn't allow for multiple attaches per process.
-#ifndef Q_OS_HPUX
-void tst_QSharedMemory::removeWhileAttached()
-{
- rememberKey("one");
-
- // attach 1
- QSharedMemory *smOne = new QSharedMemory(QLatin1String("one"));
- QVERIFY(smOne->create(1024));
- QVERIFY(smOne->isAttached());
-
- // attach 2
- QSharedMemory *smTwo = new QSharedMemory(QLatin1String("one"));
- QVERIFY(smTwo->attach());
- QVERIFY(smTwo->isAttached());
-
- // detach 1 and remove, remove one first to catch another error.
- delete smOne;
- delete smTwo;
-
-#ifdef QT_POSIX_IPC
- // POSIX IPC doesn't guarantee that the shared memory is removed
- remove("one");
-#endif
-
- // three shouldn't be able to attach
- QSharedMemory smThree(QLatin1String("one"));
- QVERIFY(!smThree.attach());
- QCOMPARE(smThree.error(), QSharedMemory::NotFound);
-}
-#endif
-
-/*!
- The memory should be set to 0 after created.
- */
-void tst_QSharedMemory::emptyMemory()
-{
- QSharedMemory sm(rememberKey(QLatin1String("voidland")));
- int size = 1024;
- QVERIFY(sm.create(size, QSharedMemory::ReadOnly));
- char *get = (char*)sm.data();
- char null = 0;
- for (int i = 0; i < size; ++i)
- QCOMPARE(get[i], null);
-}
-
-/*!
- Verify that attach with ReadOnly is actually read only
- by writing to data and causing a segfault.
-*/
-// This test opens a crash dialog on Windows.
-#if !defined(Q_OS_WIN)
-void tst_QSharedMemory::readOnly()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#elif defined(Q_OS_MACOS)
- QSKIP("QTBUG-59936: Times out on macOS", SkipAll);
-#elif defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
- QSKIP("ASan prevents the crash this test is looking for.", SkipAll);
-#else
- rememberKey("readonly_segfault");
- // ### on windows disable the popup somehow
- QProcess p;
- p.start(m_helperBinary, QStringList("readonly_segfault"));
- p.setProcessChannelMode(QProcess::ForwardedChannels);
- p.waitForFinished();
- QCOMPARE(p.error(), QProcess::Crashed);
-#endif
-}
-#endif
-
-/*!
- Keep making shared memory until the kernel stops us.
- */
-void tst_QSharedMemory::useTooMuchMemory()
-{
-#ifdef Q_OS_LINUX
- bool success = true;
- int count = 0;
- while (success) {
- QString key = QLatin1String("maxmemorytest_") + QString::number(count++);
- QSharedMemory *sm = new QSharedMemory(rememberKey(key));
- QVERIFY(sm);
- jail.append(sm);
- int size = 32768 * 1024;
- success = sm->create(size);
- if (!success && sm->error() == QSharedMemory::AlreadyExists) {
- // left over from a crash, clean it up
- sm->attach();
- sm->detach();
- success = sm->create(size);
- }
-
- if (!success) {
- QVERIFY(!sm->isAttached());
- QCOMPARE(sm->key(), key);
- QCOMPARE(sm->size(), 0);
- QVERIFY(!sm->data());
- if (sm->error() != QSharedMemory::OutOfResources)
- qDebug() << sm->error() << sm->errorString();
- // ### Linux won't return OutOfResources if there are not enough semaphores to use.
- QVERIFY(sm->error() == QSharedMemory::OutOfResources
- || sm->error() == QSharedMemory::LockError);
- QVERIFY(sm->errorString() != QString());
- QVERIFY(!sm->attach());
- QVERIFY(!sm->detach());
- } else {
- QVERIFY(sm->isAttached());
- }
- }
-#endif
-}
-
-/*!
- Create one shared memory (government) and see how many other shared memories (wars) we can
- attach before the system runs out of resources.
- */
-// HPUX doesn't allow for multiple attaches per process.
-#if !defined(Q_OS_HPUX)
-void tst_QSharedMemory::attachTooMuch()
-{
- QSKIP("disabled");
-
- QSharedMemory government(rememberKey("government"));
- QVERIFY(government.create(1024));
- while (true) {
- QSharedMemory *war = new QSharedMemory(government.key());
- QVERIFY(war);
- jail.append(war);
- if (!war->attach()) {
- QVERIFY(!war->isAttached());
- QCOMPARE(war->key(), government.key());
- QCOMPARE(war->size(), 0);
- QVERIFY(!war->data());
- QCOMPARE(war->error(), QSharedMemory::OutOfResources);
- QVERIFY(war->errorString() != QString());
- QVERIFY(!war->detach());
- break;
- } else {
- QVERIFY(war->isAttached());
- }
- }
-}
-#endif
-
-// HPUX doesn't allow for multiple attaches per process.
-#ifndef Q_OS_HPUX
-void tst_QSharedMemory::simpleProducerConsumer_data()
-{
- QTest::addColumn<QSharedMemory::AccessMode>("mode");
-
- QTest::newRow("readonly") << QSharedMemory::ReadOnly;
- QTest::newRow("readwrite") << QSharedMemory::ReadWrite;
-}
-
-/*!
- The basic consumer producer that rounds out the basic testing.
- If this fails then any muli-threading/process might fail (but be
- harder to debug)
-
- This doesn't require nor test any locking system.
- */
-void tst_QSharedMemory::simpleProducerConsumer()
-{
- QFETCH(QSharedMemory::AccessMode, mode);
-
- rememberKey(QLatin1String("market"));
- QSharedMemory producer(QLatin1String("market"));
- QSharedMemory consumer(QLatin1String("market"));
- int size = 512;
- QVERIFY(producer.create(size));
- QVERIFY(consumer.attach(mode));
-
- char *put = (char*)producer.data();
- char *get = (char*)consumer.data();
- // On Windows CE you always have ReadWrite access. Thus
- // ViewMapOfFile returns the same pointer
- QVERIFY(put != get);
- for (int i = 0; i < size; ++i) {
- put[i] = 'Q';
- QCOMPARE(get[i], 'Q');
- }
- QVERIFY(consumer.detach());
-}
-#endif
-
-// HPUX doesn't allow for multiple attaches per process.
-#ifndef Q_OS_HPUX
-void tst_QSharedMemory::simpleDoubleProducerConsumer()
-{
- rememberKey(QLatin1String("market"));
- QSharedMemory producer(QLatin1String("market"));
- int size = 512;
- QVERIFY(producer.create(size));
- QVERIFY(producer.detach());
-#ifdef QT_POSIX_IPC
- // POSIX IPC doesn't guarantee that the shared memory is removed
- remove("market");
-#endif
- QVERIFY(producer.create(size));
-
- {
- QSharedMemory consumer(QLatin1String("market"));
- QVERIFY(consumer.attach());
- }
-}
-#endif
-
-class Consumer : public QThread
-{
-
-public:
- void run() override
- {
- QSharedMemory consumer(QLatin1String("market"));
- while (!consumer.attach()) {
- if (consumer.error() != QSharedMemory::NotFound)
- qDebug() << "consumer: failed to connect" << consumer.error() << consumer.errorString();
- QVERIFY(consumer.error() == QSharedMemory::NotFound || consumer.error() == QSharedMemory::KeyError);
- QTest::qWait(1);
- }
-
- char *memory = (char*)consumer.data();
-
- int i = 0;
- while (true) {
- if (!consumer.lock())
- break;
- if (memory[0] == 'Q')
- memory[0] = ++i;
- if (memory[0] == 'E') {
- memory[1]++;
- QVERIFY(consumer.unlock());
- break;
- }
- QVERIFY(consumer.unlock());
- QTest::qWait(1);
- }
-
- QVERIFY(consumer.detach());
- }
-};
-
-class Producer : public QThread
-{
-
-public:
- Producer() : producer(QLatin1String("market"))
- {
- int size = 1024;
- if (!producer.create(size)) {
- // left over from a crash...
- if (producer.error() == QSharedMemory::AlreadyExists) {
- producer.attach();
- producer.detach();
- QVERIFY(producer.create(size));
- }
- }
- }
-
- void run() override
- {
-
- char *memory = (char*)producer.data();
- memory[1] = '0';
- QElapsedTimer timer;
- timer.start();
- int i = 0;
- while (i < 5 && timer.elapsed() < 5000) {
- QVERIFY(producer.lock());
- if (memory[0] == 'Q') {
- QVERIFY(producer.unlock());
- QTest::qWait(1);
- continue;
- }
- ++i;
- memory[0] = 'Q';
- QVERIFY(producer.unlock());
- QTest::qWait(1);
- }
-
- // tell everyone to quit
- QVERIFY(producer.lock());
- memory[0] = 'E';
- QVERIFY(producer.unlock());
-
- }
-
- QSharedMemory producer;
-private:
-
-};
-
-void tst_QSharedMemory::simpleThreadedProducerConsumer_data()
-{
- QTest::addColumn<bool>("producerIsThread");
- QTest::addColumn<int>("threads");
- for (int i = 0; i < 5; ++i) {
- QTest::newRow("1 consumer, producer is thread") << true << 1;
- QTest::newRow("1 consumer, producer is this") << false << 1;
- QTest::newRow("5 consumers, producer is thread") << true << 5;
- QTest::newRow("5 consumers, producer is this") << false << 5;
- }
-}
-
-/*!
- The basic producer/consumer, but this time using threads.
- */
-void tst_QSharedMemory::simpleThreadedProducerConsumer()
-{
- QFETCH(bool, producerIsThread);
- QFETCH(int, threads);
- rememberKey(QLatin1String("market"));
-
-#if defined Q_OS_HPUX && defined __ia64
- QSKIP("This test locks up on gravlaks.troll.no");
-#endif
-
- Producer p;
- QVERIFY(p.producer.isAttached());
- if (producerIsThread)
- p.start();
-
- QList<Consumer*> consumers;
- for (int i = 0; i < threads; ++i) {
- consumers.append(new Consumer());
- consumers.last()->start();
- }
-
- if (!producerIsThread)
- p.run();
-
- p.wait(5000);
- while (!consumers.isEmpty()) {
- Consumer *c = consumers.first();
- QVERIFY(c->isFinished() || c->wait(5000));
- delete consumers.takeFirst();
- }
-}
-
-void tst_QSharedMemory::simpleProcessProducerConsumer_data()
-{
-#if QT_CONFIG(process)
- QTest::addColumn<int>("processes");
- int tries = 5;
- for (int i = 0; i < tries; ++i) {
- QTest::newRow("1 process") << 1;
- QTest::newRow("5 processes") << 5;
- }
-#endif
-}
-
-/*!
- Create external processes that produce and consume.
- */
-void tst_QSharedMemory::simpleProcessProducerConsumer()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#else
- QFETCH(int, processes);
-
- QSKIP("This test is unstable: QTBUG-25655");
-
- rememberKey("market");
-
- QProcess producer;
- producer.start(m_helperBinary, QStringList("producer"));
- QVERIFY2(producer.waitForStarted(), "Could not start helper binary");
- QVERIFY2(producer.waitForReadyRead(), "Helper process failed to create shared memory segment: " +
- producer.readAllStandardError());
-
- QList<QProcess*> consumers;
- unsigned int failedProcesses = 0;
- const QStringList consumerArguments = QStringList("consumer");
- for (int i = 0; i < processes; ++i) {
- QProcess *p = new QProcess;
- p->setProcessChannelMode(QProcess::ForwardedChannels);
- p->start(m_helperBinary, consumerArguments);
- if (p->waitForStarted(2000))
- consumers.append(p);
- else
- ++failedProcesses;
- }
-
- bool consumerFailed = false;
-
- while (!consumers.isEmpty()) {
- QVERIFY(consumers.first()->waitForFinished(3000));
- if (consumers.first()->state() == QProcess::Running ||
- consumers.first()->exitStatus() != QProcess::NormalExit ||
- consumers.first()->exitCode() != 0) {
- consumerFailed = true;
- }
- delete consumers.takeFirst();
- }
- QCOMPARE(consumerFailed, false);
- QCOMPARE(failedProcesses, (unsigned int)(0));
-
- // tell the producer to exit now
- producer.write("", 1);
- producer.waitForBytesWritten();
- QVERIFY(producer.waitForFinished(5000));
-#endif
-}
-
-void tst_QSharedMemory::uniqueKey_data()
-{
- QTest::addColumn<QString>("key1");
- QTest::addColumn<QString>("key2");
-
- QTest::newRow("null == null") << QString() << QString();
- QTest::newRow("key == key") << QString("key") << QString("key");
- QTest::newRow("key1 == key1") << QString("key1") << QString("key1");
- QTest::newRow("key != key1") << QString("key") << QString("key1");
- QTest::newRow("ke1y != key1") << QString("ke1y") << QString("key1");
- QTest::newRow("key1 != key2") << QString("key1") << QString("key2");
- QTest::newRow("Noël -> Nol") << QString::fromUtf8("N\xc3\xabl") << QString("Nol");
-}
-
-void tst_QSharedMemory::uniqueKey()
-{
- QFETCH(QString, key1);
- QFETCH(QString, key2);
-
- QSharedMemory sm1(key1);
- QSharedMemory sm2(key2);
-
- bool setEqual = (key1 == key2);
- bool keyEqual = (sm1.key() == sm2.key());
- bool nativeEqual = (sm1.nativeKey() == sm2.nativeKey());
-
- QCOMPARE(keyEqual, setEqual);
- QCOMPARE(nativeEqual, setEqual);
-}
-
-QTEST_MAIN(tst_QSharedMemory)
-#include "tst_qsharedmemory.moc"
-
diff --git a/tests/auto/corelib/kernel/qsignalblocker/CMakeLists.txt b/tests/auto/corelib/kernel/qsignalblocker/CMakeLists.txt
index ff5a20bcad..0effacb018 100644
--- a/tests/auto/corelib/kernel/qsignalblocker/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qsignalblocker/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qsignalblocker.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qsignalblocker Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qsignalblocker LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qsignalblocker
SOURCES
tst_qsignalblocker.cpp
diff --git a/tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp b/tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp
index 3345707a74..bd1f149a3f 100644
--- a/tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp
+++ b/tests/auto/corelib/kernel/qsignalblocker/tst_qsignalblocker.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@woboq.com>
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -11,6 +11,7 @@ class tst_QSignalBlocker : public QObject
private slots:
void signalBlocking();
void moveAssignment();
+ void dismiss();
};
void tst_QSignalBlocker::signalBlocking()
@@ -133,5 +134,15 @@ void tst_QSignalBlocker::moveAssignment()
QVERIFY(!o2.signalsBlocked());
}
+void tst_QSignalBlocker::dismiss()
+{
+ QObject obj;
+ {
+ QSignalBlocker blocker(obj);
+ blocker.dismiss();
+ }
+ QVERIFY(obj.signalsBlocked());
+}
+
QTEST_MAIN(tst_QSignalBlocker)
#include "tst_qsignalblocker.moc"
diff --git a/tests/auto/corelib/kernel/qsignalmapper/CMakeLists.txt b/tests/auto/corelib/kernel/qsignalmapper/CMakeLists.txt
index 064b6722f8..b5de408ea8 100644
--- a/tests/auto/corelib/kernel/qsignalmapper/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qsignalmapper/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qsignalmapper.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qsignalmapper Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qsignalmapper LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qsignalmapper
SOURCES
tst_qsignalmapper.cpp
diff --git a/tests/auto/corelib/kernel/qsignalmapper/tst_qsignalmapper.cpp b/tests/auto/corelib/kernel/qsignalmapper/tst_qsignalmapper.cpp
index aee5751e47..de54cd5adc 100644
--- a/tests/auto/corelib/kernel/qsignalmapper/tst_qsignalmapper.cpp
+++ b/tests/auto/corelib/kernel/qsignalmapper/tst_qsignalmapper.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/BLACKLIST b/tests/auto/corelib/kernel/qsocketnotifier/BLACKLIST
deleted file mode 100644
index 664ad32582..0000000000
--- a/tests/auto/corelib/kernel/qsocketnotifier/BLACKLIST
+++ /dev/null
@@ -1,3 +0,0 @@
-[unexpectedDisconnection]
-windows ci
-
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/CMakeLists.txt b/tests/auto/corelib/kernel/qsocketnotifier/CMakeLists.txt
index 3790e46eaa..2a802b5f4d 100644
--- a/tests/auto/corelib/kernel/qsocketnotifier/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qsocketnotifier/CMakeLists.txt
@@ -1,4 +1,11 @@
-# Generated from qsocketnotifier.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qsocketnotifier LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
if(NOT QT_FEATURE_private_tests)
return()
@@ -11,19 +18,16 @@ endif()
qt_internal_add_test(tst_qsocketnotifier
SOURCES
tst_qsocketnotifier.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Network
Qt::NetworkPrivate
)
-#### Keys ignored in scope 1:.:.:qsocketnotifier.pro:<TRUE>:
-# _REQUIREMENTS = "qtConfig(private_tests)"
-
## Scopes:
#####################################################################
qt_internal_extend_target(tst_qsocketnotifier CONDITION WIN32
- PUBLIC_LIBRARIES
+ LIBRARIES
ws2_32
)
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
index 59ef20c6e4..17bba73dab 100644
--- a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
+++ b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/QTest>
#include <QtTest/QSignalSpy>
@@ -24,6 +24,7 @@
# undef min
#endif // Q_CC_MSVC
+using namespace std::chrono_literals;
class tst_QSocketNotifier : public QObject
{
@@ -131,11 +132,11 @@ public slots:
++sequence;
if (sequence == 1) {
// read from both ends
- (void) readEnd1->read(data1, sizeof(data1));
- (void) readEnd2->read(data2, sizeof(data2));
+ QCOMPARE(readEnd1->read(data1, sizeof(data1)), 1);
+ QCOMPARE(readEnd2->read(data2, sizeof(data2)), 1);
emit finished();
} else if (sequence == 2) {
- // we should never get here
+ // check that we can't read now because we've read our byte
QCOMPARE(readEnd2->read(data2, sizeof(data2)), qint64(-2));
QVERIFY(readEnd2->isValid());
}
@@ -151,7 +152,7 @@ void tst_QSocketNotifier::unexpectedDisconnection()
Given two sockets and two QSocketNotifiers registered on each
their socket. If both sockets receive data, and the first slot
invoked by one of the socket notifiers empties both sockets, the
- other notifier will also emit activated(). This results in
+ other notifier will also emit activated(). This was causing an
unexpected disconnection in QAbstractSocket.
The use case is that somebody calls one of the
@@ -187,8 +188,9 @@ void tst_QSocketNotifier::unexpectedDisconnection()
writeEnd1->waitForBytesWritten();
writeEnd2->waitForBytesWritten();
- writeEnd1->flush();
- writeEnd2->flush();
+ // ensure both read ends are ready for reading, before the event loop
+ QVERIFY(readEnd1.waitForRead(5s));
+ QVERIFY(readEnd2.waitForRead(5s));
UnexpectedDisconnectTester tester(&readEnd1, &readEnd2);
@@ -309,8 +311,8 @@ void tst_QSocketNotifier::posixSockets()
passive->waitForBytesWritten(5000);
QTestEventLoop::instance().enterLoop(3);
- QCOMPARE(readSpy.count(), 1);
- QCOMPARE(errorSpy.count(), 0);
+ QCOMPARE(readSpy.size(), 1);
+ QCOMPARE(errorSpy.size(), 0);
char buffer[100];
int r = qt_safe_read(posixSocket, buffer, 100);
@@ -324,9 +326,9 @@ void tst_QSocketNotifier::posixSockets()
qt_safe_write(posixSocket, "goodbye", 8);
QTestEventLoop::instance().enterLoop(3);
- QCOMPARE(readSpy.count(), 1);
- QCOMPARE(writeSpy.count(), 1);
- QCOMPARE(errorSpy.count(), 0);
+ QCOMPARE(readSpy.size(), 1);
+ QCOMPARE(writeSpy.size(), 1);
+ QCOMPARE(errorSpy.size(), 0);
// Write notifier may have fired before the read notifier inside
// QTcpSocket, give QTcpSocket a chance to see the incoming data
@@ -374,7 +376,7 @@ void tst_QSocketNotifier::asyncMultipleDatagram()
&tst_QSocketNotifier::async_readDatagramSlot);
// activate socket notifiers
- QTestEventLoop::instance().enterLoopMSecs(100);
+ QTestEventLoop::instance().enterLoop(100ms);
m_asyncSender->writeDatagram("1", makeNonAny(m_asyncReceiver->localAddress()), port);
m_asyncSender->writeDatagram("2", makeNonAny(m_asyncReceiver->localAddress()), port);
@@ -387,7 +389,7 @@ void tst_QSocketNotifier::asyncMultipleDatagram()
QTestEventLoop::instance().enterLoop(1);
QVERIFY(!QTestEventLoop::instance().timeout());
- QCOMPARE(spy.count(), 2);
+ QCOMPARE(spy.size(), 2);
delete m_asyncSender;
delete m_asyncReceiver;
diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/CMakeLists.txt b/tests/auto/corelib/kernel/qsystemsemaphore/CMakeLists.txt
deleted file mode 100644
index d4f1d69593..0000000000
--- a/tests/auto/corelib/kernel/qsystemsemaphore/CMakeLists.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-#####################################################################
-## tst_qsystemsemaphore Test:
-#####################################################################
-
-qt_internal_add_test(tst_qsystemsemaphore
- SOURCES
- tst_qsystemsemaphore.cpp
-)
-
-add_subdirectory(acquirerelease)
-if(QT_FEATURE_process)
- add_dependencies(tst_qsystemsemaphore acquirerelease_helper)
-endif()
diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/CMakeLists.txt b/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/CMakeLists.txt
deleted file mode 100644
index f34076350e..0000000000
--- a/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-# Generated from acquirerelease.pro.
-
-#####################################################################
-## acquirerelease_helper Binary:
-#####################################################################
-
-qt_internal_add_test_helper(acquirerelease_helper
- SOURCES
- main.cpp
- PUBLIC_LIBRARIES
- Qt::Test
-)
diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/main.cpp b/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/main.cpp
deleted file mode 100644
index 979649d662..0000000000
--- a/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/main.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-
-#include <QCoreApplication>
-#include <QDebug>
-#include <QStringList>
-#include <QSystemSemaphore>
-
-int acquire(int count = 1)
-{
- QSystemSemaphore sem("store");
-
- for (int i = 0; i < count; ++i) {
- if (!sem.acquire()) {
- qWarning() << "Could not acquire" << sem.key();
- return EXIT_FAILURE;
- }
- }
- qDebug("done aquiring");
- return EXIT_SUCCESS;
-}
-
-int release()
-{
- QSystemSemaphore sem("store");
- if (!sem.release()) {
- qWarning() << "Could not release" << sem.key();
- return EXIT_FAILURE;
- }
- qDebug("done releasing");
- return EXIT_SUCCESS;
-}
-
-int acquirerelease()
-{
- QSystemSemaphore sem("store");
- if (!sem.acquire()) {
- qWarning() << "Could not acquire" << sem.key();
- return EXIT_FAILURE;
- }
- if (!sem.release()) {
- qWarning() << "Could not release" << sem.key();
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
-}
-
-int main(int argc, char *argv[])
-{
- QCoreApplication app(argc, argv);
-
- QStringList arguments = app.arguments();
- // binary name is not used here
- arguments.takeFirst();
- if (arguments.count() < 1) {
- qWarning("Please call the helper with the function to call as argument");
- return EXIT_FAILURE;
- }
- QString function = arguments.takeFirst();
- if (function == QLatin1String("acquire")) {
- int count = 1;
- bool ok = true;
- if (arguments.count())
- count = arguments.takeFirst().toInt(&ok);
- if (!ok)
- count = 1;
- return acquire(count);
- } else if (function == QLatin1String("release")) {
- return release();
- } else if (function == QLatin1String("acquirerelease")) {
- return acquirerelease();
- } else {
- qWarning() << "Unknown function" << function;
- }
- return EXIT_SUCCESS;
-}
diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/tst_qsystemsemaphore.cpp b/tests/auto/corelib/kernel/qsystemsemaphore/tst_qsystemsemaphore.cpp
deleted file mode 100644
index e2f557b698..0000000000
--- a/tests/auto/corelib/kernel/qsystemsemaphore/tst_qsystemsemaphore.cpp
+++ /dev/null
@@ -1,271 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-
-#include <QTest>
-#if QT_CONFIG(process)
-#include <QProcess>
-#endif
-
-#include <QtCore/QList>
-#include <QtCore/QSystemSemaphore>
-#include <QtCore/QTemporaryDir>
-
-#define EXISTING_SHARE "existing"
-#define HELPERWAITTIME 10000
-
-class tst_QSystemSemaphore : public QObject
-{
- Q_OBJECT
-
-public:
- tst_QSystemSemaphore();
-
-public Q_SLOTS:
- void init();
- void cleanup();
-
-private slots:
- void key_data();
- void key();
-
- void basicacquire();
- void complexacquire();
- void release();
-
- void basicProcesses();
-
- void processes_data();
- void processes();
-
-#if !defined(Q_OS_WIN) && !defined(QT_POSIX_IPC)
- void undo();
-#endif
- void initialValue();
-
-private:
- QSystemSemaphore *existingLock;
-
- const QString m_helperBinary;
-};
-
-tst_QSystemSemaphore::tst_QSystemSemaphore()
- : m_helperBinary("./acquirerelease_helper")
-{
-}
-
-void tst_QSystemSemaphore::init()
-{
- existingLock = new QSystemSemaphore(EXISTING_SHARE, 1, QSystemSemaphore::Create);
-}
-
-void tst_QSystemSemaphore::cleanup()
-{
- delete existingLock;
-}
-
-void tst_QSystemSemaphore::key_data()
-{
- QTest::addColumn<QString>("constructorKey");
- QTest::addColumn<QString>("setKey");
-
- QTest::newRow("null, null") << QString() << QString();
- QTest::newRow("null, one") << QString() << QString("one");
- QTest::newRow("one, two") << QString("one") << QString("two");
-}
-
-/*!
- Basic key testing
- */
-void tst_QSystemSemaphore::key()
-{
- QFETCH(QString, constructorKey);
- QFETCH(QString, setKey);
-
- QSystemSemaphore sem(constructorKey);
- QCOMPARE(sem.key(), constructorKey);
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QCOMPARE(sem.errorString(), QString());
-
- sem.setKey(setKey);
- QCOMPARE(sem.key(), setKey);
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QCOMPARE(sem.errorString(), QString());
-}
-
-void tst_QSystemSemaphore::basicacquire()
-{
- QSystemSemaphore sem("QSystemSemaphore_basicacquire", 1, QSystemSemaphore::Create);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QCOMPARE(sem.errorString(), QString());
-}
-
-void tst_QSystemSemaphore::complexacquire()
-{
- QSystemSemaphore sem("QSystemSemaphore_complexacquire", 2, QSystemSemaphore::Create);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QCOMPARE(sem.errorString(), QString());
-}
-
-void tst_QSystemSemaphore::release()
-{
- QSystemSemaphore sem("QSystemSemaphore_release", 0, QSystemSemaphore::Create);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.acquire());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QVERIFY(sem.release());
- QCOMPARE(sem.error(), QSystemSemaphore::NoError);
- QCOMPARE(sem.errorString(), QString());
-}
-
-void tst_QSystemSemaphore::basicProcesses()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#else
- QSystemSemaphore sem("store", 0, QSystemSemaphore::Create);
-
- QProcess acquire;
- acquire.setProcessChannelMode(QProcess::ForwardedChannels);
-
- QProcess release;
- release.setProcessChannelMode(QProcess::ForwardedChannels);
-
- acquire.start(m_helperBinary, QStringList("acquire"));
- QVERIFY2(acquire.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- QCOMPARE(acquire.state(), QProcess::Running);
- acquire.kill();
- release.start(m_helperBinary, QStringList("release"));
- QVERIFY2(release.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- release.waitForFinished(HELPERWAITTIME);
- QCOMPARE(acquire.state(), QProcess::NotRunning);
-#endif
-}
-
-void tst_QSystemSemaphore::processes_data()
-{
- QTest::addColumn<int>("processes");
- for (int i = 0; i < 5; ++i) {
- QTest::newRow("1 process") << 1;
- QTest::newRow("3 process") << 3;
- QTest::newRow("10 process") << 10;
- }
-}
-
-void tst_QSystemSemaphore::processes()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#else
- QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
-
- QFETCH(int, processes);
- QList<QString> scripts(processes, "acquirerelease");
-
- QList<QProcess*> consumers;
- for (int i = 0; i < scripts.count(); ++i) {
- QProcess *p = new QProcess;
- p->setProcessChannelMode(QProcess::ForwardedChannels);
- consumers.append(p);
- p->start(m_helperBinary, QStringList(scripts.at(i)));
- }
-
- while (!consumers.isEmpty()) {
- consumers.first()->waitForFinished();
- QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit);
- QCOMPARE(consumers.first()->exitCode(), 0);
- delete consumers.takeFirst();
- }
-#endif
-}
-
-// This test only checks a system v unix behavior.
-#if !defined(Q_OS_WIN) && !defined(QT_POSIX_IPC)
-void tst_QSystemSemaphore::undo()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#else
- QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
-
- QStringList acquireArguments = QStringList("acquire");
- QProcess acquire;
- acquire.setProcessChannelMode(QProcess::ForwardedChannels);
- acquire.start(m_helperBinary, acquireArguments);
- QVERIFY2(acquire.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- QVERIFY(acquire.state()== QProcess::NotRunning);
-
- // At process exit the kernel should auto undo
-
- acquire.start(m_helperBinary, acquireArguments);
- QVERIFY2(acquire.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- QVERIFY(acquire.state()== QProcess::NotRunning);
-#endif
-}
-#endif
-
-void tst_QSystemSemaphore::initialValue()
-{
-#if !QT_CONFIG(process)
- QSKIP("No qprocess support", SkipAll);
-#else
- QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
-
- QStringList acquireArguments = QStringList("acquire");
- QStringList releaseArguments = QStringList("release");
- QProcess acquire;
- acquire.setProcessChannelMode(QProcess::ForwardedChannels);
-
- QProcess release;
- release.setProcessChannelMode(QProcess::ForwardedChannels);
-
- acquire.start(m_helperBinary, acquireArguments);
- QVERIFY2(acquire.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- QVERIFY(acquire.state()== QProcess::NotRunning);
-
- acquire.start(m_helperBinary, acquireArguments << QLatin1String("2"));
- QVERIFY2(acquire.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- QVERIFY(acquire.state()== QProcess::Running);
- acquire.kill();
-
- release.start(m_helperBinary, releaseArguments);
- QVERIFY2(release.waitForStarted(), "Could not start helper binary");
- acquire.waitForFinished(HELPERWAITTIME);
- release.waitForFinished(HELPERWAITTIME);
- QVERIFY(acquire.state()== QProcess::NotRunning);
-#endif
-}
-
-QTEST_MAIN(tst_QSystemSemaphore)
-#include "tst_qsystemsemaphore.moc"
-
diff --git a/tests/auto/corelib/kernel/qtimer/BLACKLIST b/tests/auto/corelib/kernel/qtimer/BLACKLIST
deleted file mode 100644
index e69de29bb2..0000000000
--- a/tests/auto/corelib/kernel/qtimer/BLACKLIST
+++ /dev/null
diff --git a/tests/auto/corelib/kernel/qtimer/CMakeLists.txt b/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
index 39a40b6983..6bb3b15850 100644
--- a/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qtimer/CMakeLists.txt
@@ -1,15 +1,34 @@
-# Generated from qtimer.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
-#####################################################################
-## tst_qtimer Test:
-#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qtimer LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
-qt_internal_add_test(tst_qtimer
- SOURCES
- tst_qtimer.cpp
- PUBLIC_LIBRARIES
- Qt::CorePrivate
-)
+if (NOT QT_FEATURE_thread)
+ return()
+endif()
+
+function(addTimerTest test)
+ qt_internal_add_test(${test}
+ SOURCES
+ tst_qtimer.cpp
+ LIBRARIES
+ Qt::CorePrivate
+ Qt::TestPrivate
+ )
+endfunction()
+
+addTimerTest(tst_qtimer)
+
+if(QT_FEATURE_glib AND UNIX)
+ addTimerTest(tst_qtimer_no_glib)
+ qt_internal_extend_target(tst_qtimer_no_glib
+ DEFINES
+ DISABLE_GLIB
+ tst_QTimer=tst_QTimer_no_glib # Class name in the unittest
+ )
+endif()
-## Scopes:
-#####################################################################
diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
index c91aba4790..40190ca465 100644
--- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
+++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp
@@ -1,16 +1,27 @@
// Copyright (C) 2020 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+/* WARNING: this source-code is reused by another test.
+
+ As Qt built with GUI support may use a different backend for its event loops
+ and other timer-related matters, it is important to test it in that form, as
+ well as in its GUI-less form. So this source file is reused by a build config
+ in the GUI module. Similarly, testing with and without glib is supported,
+ where relevant (see DISABLE_GLIB below).
+*/
#ifdef QT_GUI_LIB
+// When compiled as tests/auto/gui/kernel/qguitimer/'s source-code:
# include <QtGui/QGuiApplication>
#else
+// When compiled as tests/auto/corelib/kernel/qtimer/'s source-code:
# include <QtCore/QCoreApplication>
#endif
#include <QtCore/private/qglobal_p.h>
#include <QTest>
#include <QSignalSpy>
+#include <QtTest/private/qpropertytesthelper_p.h>
#include <qtimer.h>
#include <qthread.h>
@@ -21,6 +32,15 @@
#include <unistd.h>
#endif
+#ifdef DISABLE_GLIB
+static bool glibDisabled = []() {
+ qputenv("QT_NO_GLIB", "1");
+ return true;
+}();
+#endif
+
+using namespace std::chrono_literals;
+
class tst_QTimer : public QObject
{
Q_OBJECT
@@ -32,6 +52,12 @@ private slots:
void zeroTimer();
void singleShotTimeout();
void timeout();
+ void singleShotNormalizes_data();
+ void singleShotNormalizes();
+ void sequentialTimers_data();
+ void sequentialTimers();
+ void singleShotSequentialTimers_data();
+ void singleShotSequentialTimers();
void remainingTime();
void remainingTimeInitial_data();
void remainingTimeInitial();
@@ -56,6 +82,7 @@ private slots:
void singleShotToFunctors();
void singleShot_chrono();
void singleShot_static();
+ void crossThreadSingleShotToFunctor_data();
void crossThreadSingleShotToFunctor();
void timerOrder();
void timerOrder_data();
@@ -68,6 +95,10 @@ private slots:
void bindToTimer();
void bindTimer();
+ void automatedBindingTests();
+
+ void negativeInterval();
+ void testTimerId();
};
void tst_QTimer::zeroTimer()
@@ -84,7 +115,7 @@ void tst_QTimer::zeroTimer()
// Pass timeout to work round glib issue, see QTBUG-84291.
QCoreApplication::processEvents(QEventLoop::AllEvents, INT_MAX);
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
}
void tst_QTimer::singleShotTimeout()
@@ -98,9 +129,9 @@ void tst_QTimer::singleShotTimeout()
timer.start(100);
QVERIFY(timeoutSpy.wait(500));
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
QTest::qWait(500);
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
}
#define TIMEOUT_TIMEOUT 200
@@ -111,12 +142,147 @@ void tst_QTimer::timeout()
QSignalSpy timeoutSpy(&timer, &QTimer::timeout);
timer.start(100);
- QCOMPARE(timeoutSpy.count(), 0);
+ QCOMPARE(timeoutSpy.size(), 0);
- QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.count() > 0, TIMEOUT_TIMEOUT);
- int oldCount = timeoutSpy.count();
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > 0, TIMEOUT_TIMEOUT);
+ int oldCount = timeoutSpy.size();
- QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.count() > oldCount, TIMEOUT_TIMEOUT);
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > oldCount, TIMEOUT_TIMEOUT);
+}
+
+void tst_QTimer::singleShotNormalizes_data()
+{
+ QTest::addColumn<QByteArray>("slotName");
+
+ QTest::newRow("normalized") << QByteArray(SLOT(exitLoop()));
+
+ QTest::newRow("space-before") << QByteArray(SLOT( exitLoop()));
+ QTest::newRow("space-after") << QByteArray(SLOT(exitLoop ()));
+ QTest::newRow("space-around") << QByteArray(SLOT( exitLoop ()));
+ QTest::newRow("spaces-before") << QByteArray(SLOT( exitLoop()));
+ QTest::newRow("spaces-after") << QByteArray(SLOT(exitLoop ()));
+ QTest::newRow("spaces-around") << QByteArray(SLOT( exitLoop ()));
+
+ QTest::newRow("space-in-parens") << QByteArray(SLOT(exitLoop( )));
+ QTest::newRow("spaces-in-parens") << QByteArray(SLOT(exitLoop( )));
+ QTest::newRow("space-after-parens") << QByteArray(SLOT(exitLoop() ));
+ QTest::newRow("spaces-after-parens") << QByteArray(SLOT(exitLoop() ));
+}
+
+void tst_QTimer::singleShotNormalizes()
+{
+ static constexpr auto TestTimeout = 250ms;
+ QFETCH(QByteArray, slotName);
+ QEventLoop loop;
+
+ // control test: regular connection
+ {
+ QTimer timer;
+ QVERIFY(QObject::connect(&timer, SIGNAL(timeout()), &QTestEventLoop::instance(), slotName));
+ timer.setSingleShot(true);
+ timer.start(1);
+ QTestEventLoop::instance().enterLoop(TestTimeout);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+ }
+
+ // non-zero time
+ QTimer::singleShot(1, &QTestEventLoop::instance(), slotName);
+ QTestEventLoop::instance().enterLoop(TestTimeout);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ QTimer::singleShot(1ms, &QTestEventLoop::instance(), slotName);
+ QTestEventLoop::instance().enterLoop(TestTimeout);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ // zero time
+ QTimer::singleShot(0, &QTestEventLoop::instance(), slotName);
+ QTestEventLoop::instance().enterLoop(TestTimeout);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ QTimer::singleShot(0ms, &QTestEventLoop::instance(), slotName);
+ QTestEventLoop::instance().enterLoop(TestTimeout);
+ QVERIFY(!QTestEventLoop::instance().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()
@@ -240,19 +406,14 @@ void tst_QTimer::remainingTimeDuringActivation()
namespace {
-#if __has_include(<chrono>)
template <typename T>
std::chrono::milliseconds to_ms(T t)
{ return std::chrono::duration_cast<std::chrono::milliseconds>(t); }
-#endif
} // unnamed namespace
void tst_QTimer::basic_chrono()
{
-#if !__has_include(<chrono>)
- QSKIP("This test requires C++11 <chrono> support");
-#else
// duplicates zeroTimer, singleShotTimeout, interval and remainingTime
using namespace std::chrono;
QTimer timer;
@@ -264,24 +425,24 @@ void tst_QTimer::basic_chrono()
QCoreApplication::processEvents();
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
timeoutSpy.clear();
timer.start(milliseconds(100));
- QCOMPARE(timeoutSpy.count(), 0);
+ QCOMPARE(timeoutSpy.size(), 0);
QVERIFY(timeoutSpy.wait(TIMEOUT_TIMEOUT));
- QVERIFY(timeoutSpy.count() > 0);
- int oldCount = timeoutSpy.count();
+ QVERIFY(timeoutSpy.size() > 0);
+ int oldCount = timeoutSpy.size();
QVERIFY(timeoutSpy.wait(TIMEOUT_TIMEOUT));
- QVERIFY(timeoutSpy.count() > oldCount);
+ QVERIFY(timeoutSpy.size() > oldCount);
timeoutSpy.clear();
timer.start(to_ms(microseconds(200000)));
QCOMPARE(timer.intervalAsDuration().count(), milliseconds::rep(200));
QTest::qWait(50);
- QCOMPARE(timeoutSpy.count(), 0);
+ QCOMPARE(timeoutSpy.size(), 0);
milliseconds rt = timer.remainingTimeAsDuration();
QVERIFY2(rt.count() >= 50 && rt.count() <= 200, qPrintable(QString::number(rt.count())));
@@ -290,10 +451,9 @@ void tst_QTimer::basic_chrono()
timer.setSingleShot(true);
timer.start(milliseconds(100));
QVERIFY(timeoutSpy.wait(TIMEOUT_TIMEOUT));
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
QTest::qWait(500);
- QCOMPARE(timeoutSpy.count(), 1);
-#endif
+ QCOMPARE(timeoutSpy.size(), 1);
}
void tst_QTimer::livelock_data()
@@ -518,6 +678,7 @@ void tst_QTimer::moveToThread()
#endif
QTimer ti1;
QTimer ti2;
+ ti1.setSingleShot(true);
ti1.start(MOVETOTHREAD_TIMEOUT);
ti2.start(MOVETOTHREAD_TIMEOUT);
QVERIFY((ti1.timerId() & 0xffffff) != (ti2.timerId() & 0xffffff));
@@ -656,12 +817,10 @@ void tst_QTimer::timerFiresOnlyOncePerProcessEvents()
class TimerIdPersistsAfterThreadExitThread : public QThread
{
public:
- QTimer *timer;
- int timerId, returnValue;
+ QTimer *timer = nullptr;
+ Qt::TimerId timerId = Qt::TimerId::Invalid;
+ int returnValue = -1;
- TimerIdPersistsAfterThreadExitThread()
- : QThread(), timer(0), timerId(-1), returnValue(-1)
- { }
~TimerIdPersistsAfterThreadExitThread()
{
delete timer;
@@ -673,11 +832,15 @@ public:
timer = new QTimer;
connect(timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer->start(100);
- timerId = timer->timerId();
+ timerId = timer->id();
returnValue = eventLoop.exec();
}
};
+namespace {
+int operator&(Qt::TimerId id, int i) { return qToUnderlying(id) & i; }
+}
+
void tst_QTimer::timerIdPersistsAfterThreadExit()
{
TimerIdPersistsAfterThreadExitThread thread;
@@ -703,6 +866,19 @@ void tst_QTimer::cancelLongTimer()
QVERIFY(!timer.isActive());
}
+void tst_QTimer::testTimerId()
+{
+ QTimer timer;
+ timer.start(100ms);
+ QVERIFY(timer.isActive());
+ QCOMPARE_GT(timer.timerId(), 0);
+ QCOMPARE_GT(timer.id(), Qt::TimerId::Invalid);
+ timer.stop();
+ QVERIFY(!timer.isActive());
+ QCOMPARE(timer.timerId(), -1);
+ QCOMPARE(timer.id(), Qt::TimerId::Invalid);
+}
+
class TimeoutCounter : public QObject
{
Q_OBJECT
@@ -877,7 +1053,7 @@ void tst_QTimer::singleShotToFunctors()
thread.wait();
struct MoveOnly : CountedStruct {
- Q_DISABLE_COPY(MoveOnly);
+ Q_DISABLE_COPY(MoveOnly)
MoveOnly(MoveOnly &&o) : CountedStruct(std::move(o)) {};
MoveOnly(int *c) : CountedStruct(c) {}
};
@@ -890,9 +1066,6 @@ void tst_QTimer::singleShotToFunctors()
void tst_QTimer::singleShot_chrono()
{
-#if !__has_include(<chrono>)
- QSKIP("This test requires C++11 <chrono> support");
-#else
// duplicates singleShotStaticFunctionZeroTimeout and singleShotToFunctors
using namespace std::chrono;
{
@@ -929,7 +1102,6 @@ void tst_QTimer::singleShot_chrono()
QTRY_COMPARE(count, 3);
_e.reset();
-#endif
}
class DontBlockEvents : public QObject
@@ -1011,30 +1183,57 @@ void tst_QTimer::postedEventsShouldNotStarveTimers()
timer.start();
SlotRepeater slotRepeater;
slotRepeater.repeatThisSlot();
- QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.count() > 5, 100);
+ QTRY_VERIFY_WITH_TIMEOUT(timeoutSpy.size() > 5, 100);
}
struct DummyFunctor {
- void operator()() {}
+ static QThread *callThread;
+ void operator()() {
+ callThread = QThread::currentThread();
+ callThread->quit();
+ }
};
+QThread *DummyFunctor::callThread = nullptr;
+
+void tst_QTimer::crossThreadSingleShotToFunctor_data()
+{
+ QTest::addColumn<int>("timeout");
+
+ QTest::addRow("zero-timer") << 0;
+ QTest::addRow("1ms") << 1;
+}
void tst_QTimer::crossThreadSingleShotToFunctor()
{
- // We're testing for crashes here, so the test simply running to
- // completion is considered a success
- QThread t;
- t.start();
+ QFETCH(int, timeout);
+ // We're also testing for crashes here, so the test simply running to
+ // completion is part of the success
+ DummyFunctor::callThread = nullptr;
- QObject* o = new QObject();
+ QThread t;
+ std::unique_ptr<QObject> o(new QObject());
o->moveToThread(&t);
- for (int i = 0; i < 10000; i++) {
- QTimer::singleShot(0, o, DummyFunctor());
- }
+ QTimer::singleShot(timeout, o.get(), DummyFunctor());
+
+ // wait enough time for the timer to have timed out before the timer
+ // could be start in the receiver's thread.
+ QTest::qWait(10 + timeout * 10);
+ t.start();
+ t.wait();
+ QCOMPARE(DummyFunctor::callThread, &t);
+
+ // continue with a stress test - the calling thread is busy, the
+ // timer should still fire and no crashes.
+ DummyFunctor::callThread = nullptr;
+ t.start();
+ for (int i = 0; i < 10000; i++)
+ QTimer::singleShot(timeout, o.get(), DummyFunctor());
- t.quit();
t.wait();
- delete o;
+ o.reset();
+
+ QCOMPARE(DummyFunctor::callThread, &t);
}
void tst_QTimer::callOnTimeout()
@@ -1054,7 +1253,7 @@ void tst_QTimer::callOnTimeout()
QTest::qWait(100);
QCOMPARE(count, 2);
- QCOMPARE(timeoutSpy.count(), 1);
+ QCOMPARE(timeoutSpy.size(), 1);
// Test that connection is bound to context lifetime
QVERIFY(connection);
@@ -1107,6 +1306,23 @@ void tst_QTimer::bindToTimer()
timer.stop();
QVERIFY(!active);
+
+ auto ignoreMsg = [] {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QObject::startTimer: Timers cannot have negative intervals");
+ };
+
+ // also test that using negative interval updates the binding correctly
+ timer.start(100);
+ QVERIFY(active);
+ ignoreMsg();
+ timer.setInterval(-100);
+ QVERIFY(!active);
+ timer.start(100);
+ QVERIFY(active);
+ ignoreMsg();
+ timer.start(-100);
+ QVERIFY(!active);
}
void tst_QTimer::bindTimer()
@@ -1151,6 +1367,73 @@ void tst_QTimer::bindTimer()
QCOMPARE(timer.timerType(), Qt::VeryCoarseTimer);
}
+void tst_QTimer::automatedBindingTests()
+{
+ QTimer timer;
+
+ QVERIFY(!timer.isSingleShot());
+ QTestPrivate::testReadWritePropertyBasics(timer, true, false, "singleShot");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QTimer::singleShot");
+ return;
+ }
+
+ QCOMPARE_NE(timer.interval(), 10);
+ QTestPrivate::testReadWritePropertyBasics(timer, 10, 20, "interval");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QTimer::interval");
+ return;
+ }
+
+ QCOMPARE_NE(timer.timerType(), Qt::PreciseTimer);
+ QTestPrivate::testReadWritePropertyBasics(timer, Qt::PreciseTimer, Qt::CoarseTimer,
+ "timerType");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QTimer::timerType");
+ return;
+ }
+
+ timer.start(1000);
+ QVERIFY(timer.isActive());
+ QTestPrivate::testReadOnlyPropertyBasics(timer, true, false, "active",
+ [&timer]() { timer.stop(); });
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QTimer::active");
+ return;
+ }
+}
+
+void tst_QTimer::negativeInterval()
+{
+ auto ignoreMsg = [] {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QObject::startTimer: Timers cannot have negative intervals");
+ };
+
+ QTimer timer;
+
+ // Starting with a negative interval does not change active state.
+ ignoreMsg();
+ timer.start(-100ms);
+ QVERIFY(!timer.isActive());
+
+ // Updating the interval to a negative value stops the timer and changes
+ // the active state.
+ timer.start(100ms);
+ QVERIFY(timer.isActive());
+ ignoreMsg();
+ timer.setInterval(-100);
+ QVERIFY(!timer.isActive());
+
+ // Starting with a negative interval when already started leads to stop
+ // and inactive state.
+ timer.start(100);
+ QVERIFY(timer.isActive());
+ ignoreMsg();
+ timer.start(-100ms);
+ QVERIFY(!timer.isActive());
+}
+
class OrderHelper : public QObject
{
Q_OBJECT
@@ -1224,14 +1507,10 @@ void tst_QTimer::timerOrder_data()
void tst_QTimer::timerOrderBackgroundThread()
{
-#if !QT_CONFIG(cxx11_future)
- QSKIP("This test requires QThread::create");
-#else
auto *thread = QThread::create([this]() { timerOrder(); });
thread->start();
QVERIFY(thread->wait());
delete thread;
-#endif
}
struct StaticSingleShotUser
diff --git a/tests/auto/corelib/kernel/qtranslator/CMakeLists.txt b/tests/auto/corelib/kernel/qtranslator/CMakeLists.txt
index e3e69537bf..92b6edb17c 100644
--- a/tests/auto/corelib/kernel/qtranslator/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qtranslator/CMakeLists.txt
@@ -1,9 +1,20 @@
-# Generated from qtranslator.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qtranslator Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qtranslator LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if (NOT QT_FEATURE_thread)
+ return()
+endif()
+
qt_internal_add_test(tst_qtranslator
SOURCES
tst_qtranslator.cpp
@@ -24,7 +35,6 @@ qt_internal_add_resource(tst_qtranslator "qtranslator"
${qtranslator_resource_files}
)
-
## Scopes:
#####################################################################
@@ -44,6 +54,3 @@ if(ANDROID)
${android_testdata_resource_files}
)
endif()
-
-#### Keys ignored in scope 3:.:.:qtranslator.pro:else:
-# TESTDATA = "dependencies_la.qm" "hellotr_empty.qm" "hellotr_la.qm" "msgfmt_from_po.qm"
diff --git a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
index 372a7a9db1..c76500ea11 100644
--- a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
+++ b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2021 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QWaitCondition>
@@ -95,7 +95,7 @@ void tst_QTranslator::load()
{
QFile file(filepath);
- file.open(QFile::ReadOnly);
+ QVERIFY(file.open(QFile::ReadOnly));
QByteArray data = file.readAll();
QTranslator tor;
QVERIFY(tor.load((const uchar *)data.constData(), data.length()));
@@ -119,7 +119,7 @@ void tst_QTranslator::load()
void tst_QTranslator::loadLocale()
{
QLocale locale;
- auto localeName = locale.uiLanguages().value(0).replace('-', '_');
+ auto localeName = locale.uiLanguages(QLocale::TagSeparator::Underscore).value(0);
if (localeName.isEmpty())
QSKIP("This test requires at least one available UI language.");
@@ -329,7 +329,7 @@ void tst_QTranslator::dependencies()
{
QTranslator tor( 0 );
QFile file("dependencies_la.qm");
- file.open(QFile::ReadOnly);
+ QVERIFY(file.open(QFile::ReadOnly));
QByteArray data = file.readAll();
QVERIFY(tor.load((const uchar *)data.constData(), data.length()));
QVERIFY(!tor.isEmpty());
diff --git a/tests/auto/corelib/kernel/qvariant/CMakeLists.txt b/tests/auto/corelib/kernel/qvariant/CMakeLists.txt
index 85a90b440f..eae9c0d30e 100644
--- a/tests/auto/corelib/kernel/qvariant/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qvariant/CMakeLists.txt
@@ -1,9 +1,16 @@
-# Generated from qvariant.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qvariant Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qvariant LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
# Resources:
set(qvariant_resource_files
"stream/qt4.9/"
@@ -13,13 +20,12 @@ set(qvariant_resource_files
qt_internal_add_test(tst_qvariant
SOURCES
tst_qvariant.cpp
- DEFINES
- QT_DISABLE_DEPRECATED_BEFORE=0
INCLUDE_DIRECTORIES
../../../other/qvariant_common
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
Qt::Gui
+ Qt::TestPrivate
TESTDATA ${qvariant_resource_files}
BUILTIN_TESTDATA
)
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
index 520515df61..23d41cafb2 100644
--- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
+++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
@@ -1,49 +1,96 @@
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
// Copyright (C) 2016 Intel Corporation.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <qvariant.h>
+
+// don't assume <type_traits>
+template <typename T, typename U>
+constexpr inline bool my_is_same_v = false;
+template <typename T>
+constexpr inline bool my_is_same_v<T, T> = true;
+
+#define CHECK_IMPL(func, arg, Variant, cvref, R) \
+ static_assert(my_is_same_v<decltype( func < arg >(std::declval< Variant cvref >())), R cvref >)
+
+#define CHECK_GET_IF(Variant, cvref) \
+ CHECK_IMPL(get_if, int, Variant, cvref *, int)
+
+#define CHECK_GET(Variant, cvref) \
+ CHECK_IMPL(get, int, Variant, cvref, int)
+
+CHECK_GET_IF(QVariant, /* unadorned */);
+CHECK_GET_IF(QVariant, const);
+
+CHECK_GET(QVariant, &);
+CHECK_GET(QVariant, const &);
+CHECK_GET(QVariant, &&);
+CHECK_GET(QVariant, const &&);
+
+// check for a type derived from QVariant:
+
+struct MyVariant : QVariant
+{
+ using QVariant::QVariant;
+};
+
+CHECK_GET_IF(MyVariant, /* unadorned */);
+CHECK_GET_IF(MyVariant, const);
+
+CHECK_GET(MyVariant, &);
+CHECK_GET(MyVariant, const &);
+CHECK_GET(MyVariant, &&);
+CHECK_GET(MyVariant, const &&);
+
+#undef CHECK_GET_IF
+#undef CHECK_GET
+#undef CHECK_IMPL
#include <QTest>
-#include <QQueue>
-#include <QStack>
-#include <QSet>
-#include <qvariant.h>
-#include <QtCore/private/qvariant_p.h>
-#include <qbitarray.h>
-#include <qbytearraylist.h>
-#include <qdatetime.h>
-#include <qmap.h>
-#include <QHash>
-#include <qiodevice.h>
-#include <qurl.h>
-#include <qlocale.h>
-#include <qdebug.h>
-#include <qjsondocument.h>
-#include <quuid.h>
-
-#include <limits.h>
-#include <float.h>
-#include <cmath>
-#include <variant>
-#include <QRegularExpression>
-#include <QDir>
+// Please stick to alphabetic order.
+#include <QAssociativeIterable>
+#include <QBitArray>
#include <QBuffer>
+#include <QByteArrayList>
+#include <QDateTime>
+#include <QDebug>
+#include <QDir>
+#include <QEasingCurve>
+#include <QMap>
+#include <QIODevice>
+#include <QHash>
#include <QJsonArray>
+#include <QJsonDocument>
#include <QJsonObject>
-#include <QEasingCurve>
-#include <QSequentialIterable>
-#include <QAssociativeIterable>
+#include <QLocale>
+#include <QQueue>
+#include <QRegularExpression>
#include <QScopeGuard>
-#include "qnumeric.h"
+#include <QSequentialIterable>
+#include <QSet>
+#include <QStack>
+#include <QTimeZone>
+#include <QtNumeric>
+#include <QUrl>
+#include <QUuid>
+#include <private/qcomparisontesthelper_p.h>
#include <private/qlocale_p.h>
#include <private/qmetatype_p.h>
#include "tst_qvariant_common.h"
+#include <limits>
+#include <float.h>
+#include <cmath>
+#include <variant>
#include <unordered_map>
+using namespace Qt::StringLiterals;
+
class CustomNonQObject;
+struct NonDefaultConstructible;
template<typename T, typename = void>
struct QVariantFromValueCompiles
@@ -60,15 +107,43 @@ struct QVariantFromValueCompiles<T, std::void_t<decltype (QVariant::fromValue(st
static_assert(QVariantFromValueCompiles<int>::value);
static_assert(!QVariantFromValueCompiles<QObject>::value);
+enum EnumTest_Enum0 { EnumTest_Enum0_value = 42, EnumTest_Enum0_negValue = -8 };
+Q_DECLARE_METATYPE(EnumTest_Enum0)
+enum EnumTest_Enum1 : qint64 { EnumTest_Enum1_value = 42, EnumTest_Enum1_bigValue = (Q_INT64_C(1) << 33) + 50 };
+Q_DECLARE_METATYPE(EnumTest_Enum1)
+
+enum EnumTest_Enum3 : qint64 { EnumTest_Enum3_value = -47, EnumTest_Enum3_bigValue = (Q_INT64_C(1) << 56) + 5 };
+Q_DECLARE_METATYPE(EnumTest_Enum3)
+enum EnumTest_Enum4 : quint64 { EnumTest_Enum4_value = 47, EnumTest_Enum4_bigValue = (Q_INT64_C(1) << 52) + 45 };
+Q_DECLARE_METATYPE(EnumTest_Enum4)
+enum EnumTest_Enum5 : uint { EnumTest_Enum5_value = 47 };
+Q_DECLARE_METATYPE(EnumTest_Enum5)
+enum EnumTest_Enum6 : uchar { EnumTest_Enum6_value = 47 };
+Q_DECLARE_METATYPE(EnumTest_Enum6)
+enum class EnumTest_Enum7 { EnumTest_Enum7_value = 47, ensureSignedEnum7 = -1 };
+Q_DECLARE_METATYPE(EnumTest_Enum7)
+enum EnumTest_Enum8 : short { EnumTest_Enum8_value = 47 };
+Q_DECLARE_METATYPE(EnumTest_Enum8)
+
+template <typename T> int qToUnderlying(QFlags<T> f)
+{
+ return f.toInt();
+}
+
class tst_QVariant : public QObject
{
Q_OBJECT
+ static void runTestFunction()
+ {
+ QFETCH(QFunctionPointer, testFunction);
+ testFunction();
+ }
+
public:
tst_QVariant(QObject *parent = nullptr)
: QObject(parent), customNonQObjectPointer(0)
{
-
}
@@ -100,6 +175,12 @@ private slots:
void canConvert_data();
void canConvert();
+
+ void canConvertAndConvert_ReturnFalse_WhenConvertingBetweenPointerAndValue_data();
+ void canConvertAndConvert_ReturnFalse_WhenConvertingBetweenPointerAndValue();
+
+ void canConvertAndConvert_ReturnFalse_WhenConvertingQObjectBetweenPointerAndValue();
+
void convert();
void toSize_data();
@@ -191,9 +272,11 @@ private slots:
void operator_eq_eq_data();
void operator_eq_eq();
+#if QT_DEPRECATED_SINCE(6, 0)
void typeName_data();
void typeName();
void typeToName();
+#endif
void streamInvalidVariant();
@@ -208,6 +291,9 @@ private slots:
void variantHash();
void convertToQUint8() const;
+ void compareCompiles() const;
+ void compareNumerics_data() const;
+ void compareNumerics() const;
void comparePointers() const;
void voidStar() const;
void dataStar() const;
@@ -221,6 +307,7 @@ private slots:
void loadBrokenUserType();
void invalidDate() const;
+ void compareCustomTypes_data() const;
void compareCustomTypes() const;
void timeToDateTime() const;
void copyingUserTypes() const;
@@ -248,8 +335,10 @@ private slots:
void forwardDeclare();
void debugStream_data();
void debugStream();
+#if QT_DEPRECATED_SINCE(6, 0)
void debugStreamType_data();
void debugStreamType();
+#endif
void loadQt4Stream_data();
void loadQt4Stream();
@@ -265,11 +354,18 @@ private slots:
void implicitConstruction();
+ void iterateSequentialContainerElements_data();
+ void iterateSequentialContainerElements() { runTestFunction(); }
+ void iterateAssociativeContainerElements_data();
+ void iterateAssociativeContainerElements() { runTestFunction(); }
void iterateContainerElements();
- void pairElements();
+ void pairElements_data();
+ void pairElements() { runTestFunction(); }
- void enums();
- void metaEnums();
+ void enums_data();
+ void enums() { runTestFunction(); }
+ void metaEnums_data();
+ void metaEnums() { runTestFunction(); }
void nullConvert();
@@ -284,10 +380,41 @@ private slots:
void preferDirectConversionOverInterfaces();
void mutableView();
+ void canViewAndView_ReturnFalseAndDefault_WhenConvertingBetweenPointerAndValue();
+
void moveOperations();
void equalsWithoutMetaObject();
+ void constructFromIncompatibleMetaType_data();
+ void constructFromIncompatibleMetaType();
+ void constructFromQtLT65MetaType();
+ void copyNonDefaultConstructible();
+
+ void inplaceConstruct();
+ void emplace();
+
+ void getIf_int() { getIf_impl(42); }
+ void getIf_QString() { getIf_impl(u"string"_s); };
+ void getIf_NonDefaultConstructible();
+ void getIfSpecial();
+
+ void get_int() { get_impl(42); }
+ void get_QString() { get_impl(u"string"_s); }
+ void get_NonDefaultConstructible();
+
private:
+ using StdVariant = std::variant<std::monostate,
+ // list here all the types with which we instantiate getIf_impl:
+ int,
+ QString,
+ NonDefaultConstructible
+ >;
+ template <typename T>
+ void getIf_impl(T t) const;
+ template <typename T>
+ void get_impl(T t) const;
+ template<typename T>
+ void canViewAndView_ReturnFalseAndDefault_WhenConvertingBetweenPointerAndValue_impl(const QByteArray &typeName);
void dataStream_data(QDataStream::Version version);
void loadQVariantFromDataStream(QDataStream::Version version);
void saveQVariantFromDataStream(QDataStream::Version version);
@@ -313,22 +440,32 @@ void tst_QVariant::constructor()
QVariant varll2(varll);
QCOMPARE(varll2, varll);
- QVariant var3(QVariant::String);
+ QVariant var3 {QMetaType::fromType<QString>()};
QCOMPARE(var3.typeName(), "QString");
QVERIFY(var3.isNull());
QVERIFY(var3.isValid());
- QVariant var4(QVariant::Invalid);
- QCOMPARE(var4.type(), QVariant::Invalid);
+ QVariant var3a = QVariant::fromMetaType(QMetaType::fromType<QString>());
+ QCOMPARE(var3a.typeName(), "QString");
+ QVERIFY(var3a.isNull());
+ QVERIFY(var3a.isValid());
+
+ QVariant var4 {QMetaType()};
+ QCOMPARE(var4.typeId(), QMetaType::UnknownType);
QVERIFY(var4.isNull());
QVERIFY(!var4.isValid());
+ QVariant var4a = QVariant::fromMetaType(QMetaType());
+ QCOMPARE(var4a.typeId(), QMetaType::UnknownType);
+ QVERIFY(var4a.isNull());
+ QVERIFY(!var4a.isValid());
+
QVariant var5(QLatin1String("hallo"));
- QCOMPARE(var5.type(), QVariant::String);
+ QCOMPARE(var5.typeId(), QMetaType::QString);
QCOMPARE(var5.typeName(), "QString");
QVariant var6(qlonglong(0));
- QCOMPARE(var6.type(), QVariant::LongLong);
+ QCOMPARE(var6.typeId(), QMetaType::LongLong);
QCOMPARE(var6.typeName(), "qlonglong");
QVariant var7 = 5;
@@ -357,10 +494,18 @@ void tst_QVariant::constructor_invalid()
QFETCH(uint, typeId);
{
QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type"));
- QVariant variant(static_cast<QVariant::Type>(typeId));
+ QVariant variant {QMetaType(typeId)};
+ QVERIFY(!variant.isValid());
+ QVERIFY(variant.isNull());
+ QCOMPARE(variant.typeId(), int(QMetaType::UnknownType));
+ QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
+ }
+ {
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type"));
+ QVariant variant = QVariant::fromMetaType(QMetaType(typeId));
QVERIFY(!variant.isValid());
QVERIFY(variant.isNull());
- QCOMPARE(int(variant.type()), int(QMetaType::UnknownType));
+ QCOMPARE(variant.typeId(), int(QMetaType::UnknownType));
QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
}
{
@@ -374,9 +519,9 @@ void tst_QVariant::constructor_invalid()
void tst_QVariant::copy_constructor()
{
- QVariant var7(QVariant::Int);
+ QVariant var7 {QMetaType::fromType<int>()};
QVariant var8(var7);
- QCOMPARE(var8.type(), QVariant::Int);
+ QCOMPARE(var8.typeId(), QMetaType::Int);
QVERIFY(var8.isNull());
}
@@ -396,7 +541,7 @@ void tst_QVariant::isNull()
QVariant var3( QString( "blah" ) );
QVERIFY( !var3.isNull() );
- var3 = QVariant(QVariant::String);
+ var3 = QVariant(QMetaType::fromType<QString>());
QVERIFY( var3.isNull() );
QVariant var4( 0 );
@@ -409,12 +554,12 @@ void tst_QVariant::isNull()
QVERIFY( !var6.isNull() );
var6 = QVariant();
QVERIFY( var6.isNull() );
- var6.convert( QVariant::String );
+ var6.convert(QMetaType::fromType<QString>());
QVERIFY( var6.isNull() );
QVariant varLL( (qlonglong)0 );
QVERIFY( !varLL.isNull() );
- QVariant var8(QMetaType(QMetaType::Nullptr), nullptr);
+ QVariant var8(QMetaType::fromType<std::nullptr_t>(), nullptr);
QVERIFY(var8.isNull());
var8 = QVariant::fromValue<std::nullptr_t>(nullptr);
QVERIFY(var8.isNull());
@@ -423,12 +568,12 @@ void tst_QVariant::isNull()
var9 = QVariant::fromValue<QJsonValue>(QJsonValue(QJsonValue::Null));
QVERIFY(!var9.isNull());
- QVariant var10(QMetaType(QMetaType::VoidStar), nullptr);
+ QVariant var10(QMetaType::fromType<void*>(), nullptr);
QVERIFY(var10.isNull());
var10 = QVariant::fromValue<void*>(nullptr);
QVERIFY(var10.isNull());
- QVariant var11(QMetaType(QMetaType::QObjectStar), nullptr);
+ QVariant var11(QMetaType::fromType<QObject*>(), nullptr);
QVERIFY(var11.isNull());
var11 = QVariant::fromValue<QObject*>(nullptr);
QVERIFY(var11.isNull());
@@ -443,9 +588,9 @@ void tst_QVariant::swap()
{
QVariant v1 = 1, v2 = 2.0;
v1.swap(v2);
- QCOMPARE(v1.type(),QVariant::Double);
+ QCOMPARE(v1.typeId(), QMetaType::Double);
QCOMPARE(v1.toDouble(),2.0);
- QCOMPARE(v2.type(),QVariant::Int);
+ QCOMPARE(v2.typeId(), QMetaType::Int);
QCOMPARE(v2.toInt(),1);
}
@@ -519,9 +664,6 @@ void tst_QVariant::canConvert_data()
var = QVariant((uint)1);
QTest::newRow("UInt")
<< var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
- var = QVariant((int)1);
- QTest::newRow("Int")
- << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
var = QVariant((qulonglong)1);
QTest::newRow("ULongLong")
<< var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
@@ -555,13 +697,122 @@ void tst_QVariant::canConvert()
{
TST_QVARIANT_CANCONVERT_FETCH_DATA
+ // This test links against QtGui but not QtWidgets, so QSizePolicy isn't real for it.
+ QTest::ignoreMessage(QtWarningMsg, // QSizePolicy's id is 0x2000, a.k.a. 8192
+ "Trying to construct an instance of an invalid type, type id: 8192");
TST_QVARIANT_CANCONVERT_COMPARE_DATA
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
// Invalid type ids
+ QTest::ignoreMessage(QtWarningMsg,
+ "Trying to construct an instance of an invalid type, type id: -1");
QCOMPARE(val.canConvert(-1), false);
+ QTest::ignoreMessage(QtWarningMsg,
+ "Trying to construct an instance of an invalid type, type id: -23");
QCOMPARE(val.canConvert(-23), false);
+ QTest::ignoreMessage(QtWarningMsg,
+ "Trying to construct an instance of an invalid type, type id: -23876");
QCOMPARE(val.canConvert(-23876), false);
+ QTest::ignoreMessage(QtWarningMsg,
+ "Trying to construct an instance of an invalid type, type id: 23876");
QCOMPARE(val.canConvert(23876), false);
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
+}
+
+namespace {
+
+// Used for testing canConvert/convert of QObject derived types
+struct QObjectDerived : QObject
+{
+ Q_OBJECT
+};
+
+// Adds a test table row for checking value <-> pointer conversion
+// If type is a pointer, the target type is value type and vice versa.
+template<typename T>
+void addRowForPointerValueConversion()
+{
+ using ValueType = std::remove_pointer_t<T>;
+ if constexpr (!std::is_same_v<ValueType, std::nullptr_t>) {
+
+ static ValueType instance{}; // static since we may need a pointer to a valid object
+
+ QVariant variant;
+ if constexpr (std::is_pointer_v<T>)
+ variant = QVariant::fromValue(&instance);
+ else
+ variant = QVariant::fromValue(instance);
+
+ // Toggle pointer/value type
+ using TargetType = std::conditional_t<std::is_pointer_v<T>, ValueType, T *>;
+
+ const QMetaType fromType = QMetaType::fromType<T>();
+ const QMetaType toType = QMetaType::fromType<TargetType>();
+
+ QTest::addRow("%s->%s", fromType.name(), toType.name())
+ << variant << QMetaType::fromType<TargetType>();
+ }
+}
+
+} // namespace
+
+void tst_QVariant::canConvertAndConvert_ReturnFalse_WhenConvertingBetweenPointerAndValue_data()
+{
+ QTest::addColumn<QVariant>("variant");
+ QTest::addColumn<QMetaType>("targetType");
+
+#define ADD_ROW(typeName, typeNameId, realType) \
+ addRowForPointerValueConversion<realType>(); \
+ addRowForPointerValueConversion<realType *>();
+
+ // Add rows for static primitive types
+ QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_ROW)
+
+ // Add rows for static core types
+ QT_FOR_EACH_STATIC_CORE_CLASS(ADD_ROW)
+#undef ADD_ROW
+
+}
+
+void tst_QVariant::canConvertAndConvert_ReturnFalse_WhenConvertingBetweenPointerAndValue()
+{
+ QFETCH(QVariant, variant);
+ QFETCH(QMetaType, targetType);
+
+ QVERIFY(!variant.canConvert(targetType));
+
+ QVERIFY(!variant.convert(targetType));
+
+ // As per the documentation, when QVariant::convert fails, the
+ // QVariant is cleared and changed to the requested type.
+ QVERIFY(variant.isNull());
+ QCOMPARE(variant.metaType(), targetType);
+}
+
+void tst_QVariant::canConvertAndConvert_ReturnFalse_WhenConvertingQObjectBetweenPointerAndValue()
+{
+ // Types derived from QObject are non-copyable and require their own test.
+ // We only test pointer -> value conversion, because constructing a QVariant
+ // from a non-copyable object will just set the QVariant to null.
+
+ QObjectDerived object;
+ QVariant variant = QVariant::fromValue(&object);
+
+ constexpr QMetaType targetType = QMetaType::fromType<QObjectDerived>();
+ QVERIFY(!variant.canConvert(targetType));
+
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ QRegularExpression(".*does not support destruction and copy construction"));
+
+ QVERIFY(!variant.convert(targetType));
+
+ // When the QVariant::convert fails, the QVariant is cleared, and since the target type is
+ // invalid for QVariant, the QVariant's type is also cleared to an unknown type.
+ QVERIFY(variant.isNull());
+ QCOMPARE(variant.metaType(), QMetaType());
}
void tst_QVariant::convert()
@@ -573,7 +824,6 @@ void tst_QVariant::convert()
QCOMPARE(var.toInt(), 0);
}
-
void tst_QVariant::toInt_data()
{
QTest::addColumn<QVariant>("value");
@@ -617,12 +867,19 @@ void tst_QVariant::toInt_data()
QTest::newRow("undefined-QJsonValue") << QVariant(QJsonValue(QJsonValue::Undefined)) << 0 << false;
}
+#if QT_DEPRECATED_SINCE(6, 0)
+# define EXEC_DEPRECATED_CALL(x) QT_IGNORE_DEPRECATIONS(x)
+#else
+# define EXEC_DEPRECATED_CALL(x)
+#endif
+
void tst_QVariant::toInt()
{
QFETCH( QVariant, value );
QFETCH( int, result );
QFETCH( bool, valueOK );
- QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.isValid() == value.canConvert( QVariant::Int ) );)
+ QVERIFY( value.isValid() == value.canConvert(QMetaType::fromType<int>()) );
bool ok;
int i = value.toInt( &ok );
QCOMPARE( i, result );
@@ -671,7 +928,8 @@ void tst_QVariant::toUInt()
QFETCH( uint, result );
QFETCH( bool, valueOK );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::UInt ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::UInt ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<uint>()) );
bool ok;
uint i = value.toUInt( &ok );
@@ -695,7 +953,8 @@ void tst_QVariant::toSize()
QFETCH( QVariant, value );
QFETCH( QSize, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Size ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Size ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QSize>()) );
QSize i = value.toSize();
QCOMPARE( i, result );
@@ -716,7 +975,8 @@ void tst_QVariant::toSizeF()
QFETCH( QVariant, value );
QFETCH( QSizeF, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::SizeF ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::SizeF ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QSizeF>()) );
QSizeF i = value.toSizeF();
QCOMPARE( i, result );
@@ -737,7 +997,8 @@ void tst_QVariant::toLine()
QFETCH( QVariant, value );
QFETCH( QLine, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Line ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Line ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QLine>()) );
QLine i = value.toLine();
QCOMPARE( i, result );
@@ -758,7 +1019,8 @@ void tst_QVariant::toLineF()
QFETCH( QVariant, value );
QFETCH( QLineF, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::LineF ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::LineF ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QLineF>()) );
QLineF i = value.toLineF();
QCOMPARE( i, result );
@@ -780,7 +1042,8 @@ void tst_QVariant::toPoint()
QFETCH( QVariant, value );
QFETCH( QPoint, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Point ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Point ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QPoint>()) );
QPoint i = value.toPoint();
QCOMPARE( i, result );
}
@@ -801,7 +1064,8 @@ void tst_QVariant::toRect()
QFETCH( QVariant, value );
QFETCH( QRect, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Rect ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Rect ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QRect>()) );
QRect i = value.toRect();
QCOMPARE( i, result );
}
@@ -819,7 +1083,8 @@ void tst_QVariant::toChar()
QFETCH( QVariant, value );
QFETCH( QChar, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Char ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Char ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QChar>()) );
QChar i = value.toChar();
QCOMPARE( i, result );
@@ -857,7 +1122,8 @@ void tst_QVariant::toBool()
QFETCH( QVariant, value );
QFETCH( bool, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Bool ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Bool ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<bool>()) );
bool i = value.toBool();
QCOMPARE( i, result );
@@ -876,7 +1142,8 @@ void tst_QVariant::toPointF()
QFETCH( QVariant, value );
QFETCH( QPointF, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::PointF ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::PointF ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QPointF>()) );
QPointF d = value.toPointF();
QCOMPARE( d, result );
}
@@ -896,7 +1163,8 @@ void tst_QVariant::toRectF()
QFETCH( QVariant, value );
QFETCH( QRectF, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::RectF ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::RectF ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QRectF>()) );
QRectF d = value.toRectF();
QCOMPARE( d, result );
}
@@ -923,7 +1191,8 @@ void tst_QVariant::toDouble()
QFETCH( double, result );
QFETCH( bool, valueOK );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Double ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Double ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<double>()) );
bool ok;
double d = value.toDouble( &ok );
QCOMPARE( d, result );
@@ -952,7 +1221,8 @@ void tst_QVariant::toFloat()
QFETCH(float, result);
QFETCH(bool, valueOK);
QVERIFY(value.isValid());
- QVERIFY(value.canConvert(QMetaType::Float));
+ EXEC_DEPRECATED_CALL(QVERIFY(value.canConvert(QMetaType::Float));)
+ QVERIFY(value.canConvert(QMetaType::fromType<float>()));
bool ok;
float d = value.toFloat(&ok);
QCOMPARE(d, result);
@@ -1003,7 +1273,8 @@ void tst_QVariant::toLongLong()
QFETCH( qlonglong, result );
QFETCH( bool, valueOK );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::LongLong ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::LongLong ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<qlonglong>()) );
bool ok;
qlonglong ll = value.toLongLong( &ok );
QCOMPARE( ll, result );
@@ -1058,7 +1329,8 @@ void tst_QVariant::toULongLong()
QFETCH( qulonglong, result );
QFETCH( bool, valueOK );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::ULongLong ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::ULongLong ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<qulonglong>()) );
bool ok;
qulonglong ll = value.toULongLong( &ok );
QCOMPARE( ll, result );
@@ -1098,12 +1370,13 @@ void tst_QVariant::toByteArray()
QFETCH( QVariant, value );
QFETCH( QByteArray, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::ByteArray ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::ByteArray ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QByteArray>()) );
QByteArray ba = value.toByteArray();
QCOMPARE( ba.isNull(), result.isNull() );
QCOMPARE( ba, result );
- QVERIFY( value.convert( QVariant::ByteArray ) );
+ QVERIFY( value.convert(QMetaType::fromType<QByteArray>()) );
QCOMPARE( value.toByteArray(), result );
}
@@ -1140,12 +1413,13 @@ void tst_QVariant::toString()
QFETCH( QVariant, value );
QFETCH( QString, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::String ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::String ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QString>()) );
QString str = value.toString();
QCOMPARE( str.isNull(), result.isNull() );
QCOMPARE( str, result );
- QVERIFY( value.convert( QVariant::String ) );
+ QVERIFY( value.convert(QMetaType::fromType<QString>()) );
QCOMPARE( value.toString(), result );
}
@@ -1164,7 +1438,8 @@ void tst_QVariant::toDate()
QFETCH( QVariant, value );
QFETCH( QDate, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Date ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Date ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QDate>()) );
QCOMPARE( value.toDate(), result );
}
@@ -1184,7 +1459,8 @@ void tst_QVariant::toTime()
QFETCH( QVariant, value );
QFETCH( QTime, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::Time ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::Time ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QTime>()) );
QCOMPARE( value.toTime(), result );
}
@@ -1197,8 +1473,9 @@ void tst_QVariant::toDateTime_data()
<< QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 10, 10 ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 0, 0, 0 ) );
QTest::newRow( "qstring" ) << QVariant( QString( "2002-10-10T12:34:56" ) ) << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ) );
- QTest::newRow( "qstring-utc" ) << QVariant( QString( "2002-10-10T12:34:56Z" ) )
- << QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56 ), Qt::UTC );
+ QTest::newRow("qstring-utc")
+ << QVariant(QString("2002-10-10T12:34:56Z"))
+ << QDateTime(QDate(2002, 10, 10), QTime(12, 34, 56), QTimeZone::UTC);
QTest::newRow( "qstring-with-ms" ) << QVariant( QString( "2002-10-10T12:34:56.789" ) )
<< QDateTime( QDate( 2002, 10, 10 ), QTime( 12, 34, 56, 789 ) );
}
@@ -1208,10 +1485,13 @@ void tst_QVariant::toDateTime()
QFETCH( QVariant, value );
QFETCH( QDateTime, result );
QVERIFY( value.isValid() );
- QVERIFY( value.canConvert( QVariant::DateTime ) );
+ EXEC_DEPRECATED_CALL(QVERIFY( value.canConvert( QVariant::DateTime ) );)
+ QVERIFY( value.canConvert(QMetaType::fromType<QDateTime>()) );
QCOMPARE( value.toDateTime(), result );
}
+#undef EXEC_DEPRECATED_CALL
+
void tst_QVariant::toLocale()
{
QVariant variant;
@@ -1267,16 +1547,16 @@ void tst_QVariant::writeToReadFromDataStream_data()
}
QTest::newRow( "invalid" ) << QVariant() << true;
- QTest::newRow( "bitarray_invalid" ) << QVariant(QVariant::BitArray) << true;
+ QTest::newRow( "bitarray_invalid" ) << QVariant(QMetaType::fromType<QBitArray>()) << true;
QTest::newRow( "bitarray_empty" ) << QVariant( QBitArray() ) << false;
QBitArray bitarray( 3 );
bitarray[0] = 0;
bitarray[1] = 1;
bitarray[2] = 0;
QTest::newRow( "bitarray_valid" ) << QVariant( bitarray ) << false;
- QTest::newRow( "bytearray_invalid" ) << QVariant(QVariant::ByteArray) << true;
+ QTest::newRow( "bytearray_invalid" ) << QVariant(QMetaType::fromType<QByteArray>()) << true;
QTest::newRow( "bytearray_empty" ) << QVariant( QByteArray() ) << false;
- QTest::newRow( "int_invalid") << QVariant(QVariant::Int) << true;
+ QTest::newRow( "int_invalid") << QVariant(QMetaType::fromType<int>()) << true;
QByteArray bytearray(5, ' ');
bytearray[0] = 'T';
bytearray[1] = 'e';
@@ -1284,10 +1564,10 @@ void tst_QVariant::writeToReadFromDataStream_data()
bytearray[3] = 't';
bytearray[4] = '\0';
QTest::newRow( "bytearray_valid" ) << QVariant( bytearray ) << false;
- QTest::newRow( "date_invalid" ) << QVariant(QVariant::Date) << true;
+ QTest::newRow( "date_invalid" ) << QVariant(QMetaType::fromType<QDate>()) << true;
QTest::newRow( "date_empty" ) << QVariant( QDate() ) << false;
QTest::newRow( "date_valid" ) << QVariant( QDate( 2002, 07, 06 ) ) << false;
- QTest::newRow( "datetime_invalid" ) << QVariant(QVariant::DateTime) << true;
+ QTest::newRow( "datetime_invalid" ) << QVariant(QMetaType::fromType<QDateTime>()) << true;
QTest::newRow( "datetime_empty" ) << QVariant( QDateTime() ) << false;
QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false;
QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false;
@@ -1299,22 +1579,22 @@ void tst_QVariant::writeToReadFromDataStream_data()
vMap.insert( "double", QVariant( 3.45 ) );
vMap.insert( "float", QVariant( 3.45f ) );
QTest::newRow( "map_valid" ) << QVariant( vMap ) << false;
- QTest::newRow( "point_invalid" ) << QVariant(QVariant::Point) << true;
+ QTest::newRow( "point_invalid" ) << QVariant(QMetaType::fromType<QPoint>()) << true;
QTest::newRow( "point_empty" ) << QVariant::fromValue( QPoint() ) << false;
QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false;
- QTest::newRow( "rect_invalid" ) << QVariant(QVariant::Rect) << true;
+ QTest::newRow( "rect_invalid" ) << QVariant(QMetaType::fromType<QRect>()) << true;
QTest::newRow( "rect_empty" ) << QVariant( QRect() ) << false;
QTest::newRow( "rect_valid" ) << QVariant( QRect( 10, 10, 20, 20 ) ) << false;
- QTest::newRow( "size_invalid" ) << QVariant(QVariant::Size) << true;
+ QTest::newRow( "size_invalid" ) << QVariant(QMetaType::fromType<QSize>()) << true;
QTest::newRow( "size_empty" ) << QVariant( QSize( 0, 0 ) ) << false;
QTest::newRow( "size_valid" ) << QVariant( QSize( 10, 10 ) ) << false;
- QTest::newRow( "string_invalid" ) << QVariant(QVariant::String) << true;
+ QTest::newRow( "string_invalid" ) << QVariant(QMetaType::fromType<QString>()) << true;
QTest::newRow( "string_empty" ) << QVariant( QString() ) << false;
QTest::newRow( "string_valid" ) << QVariant( QString( "Test" ) ) << false;
QStringList stringlist;
stringlist << "One" << "Two" << "Three";
QTest::newRow( "stringlist_valid" ) << QVariant( stringlist ) << false;
- QTest::newRow( "time_invalid" ) << QVariant(QVariant::Time) << true;
+ QTest::newRow( "time_invalid" ) << QVariant(QMetaType::fromType<QTime>()) << true;
QTest::newRow( "time_empty" ) << QVariant( QTime() ) << false;
QTest::newRow( "time_valid" ) << QVariant( QTime( 14, 0, 0 ) ) << false;
QTest::newRow( "uint_valid" ) << QVariant( (uint)123 ) << false;
@@ -1324,27 +1604,27 @@ void tst_QVariant::writeToReadFromDataStream_data()
QTest::newRow( "regularexpression_empty" ) << QVariant(QRegularExpression()) << false;
// types known to QMetaType, but not part of QVariant::Type
- QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType(QMetaType::Long), nullptr) << true;
+ QTest::newRow("QMetaType::Long invalid") << QVariant(QMetaType::fromType<long>(), nullptr) << true;
long longInt = -1l;
- QTest::newRow("QMetaType::Long") << QVariant(QMetaType(QMetaType::Long), &longInt) << false;
- QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType(QMetaType::Short), nullptr) << true;
+ QTest::newRow("QMetaType::Long") << QVariant(QMetaType::fromType<long>(), &longInt) << false;
+ QTest::newRow("QMetaType::Short invalid") << QVariant(QMetaType::fromType<short>(), nullptr) << true;
short shortInt = 1;
- QTest::newRow("QMetaType::Short") << QVariant(QMetaType(QMetaType::Short), &shortInt) << false;
- QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType(QMetaType::Char), nullptr) << true;
+ QTest::newRow("QMetaType::Short") << QVariant(QMetaType::fromType<short>(), &shortInt) << false;
+ QTest::newRow("QMetaType::Char invalid") << QVariant(QMetaType::fromType<QChar>(), nullptr) << true;
char ch = 'c';
- QTest::newRow("QMetaType::Char") << QVariant(QMetaType(QMetaType::Char), &ch) << false;
- QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType(QMetaType::ULong), nullptr) << true;
+ QTest::newRow("QMetaType::Char") << QVariant(QMetaType::fromType<char>(), &ch) << false;
+ QTest::newRow("QMetaType::ULong invalid") << QVariant(QMetaType::fromType<ulong>(), nullptr) << true;
ulong ulongInt = 1ul;
- QTest::newRow("QMetaType::ULong") << QVariant(QMetaType(QMetaType::ULong), &ulongInt) << false;
- QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType(QMetaType::UShort), nullptr) << true;
+ QTest::newRow("QMetaType::ULong") << QVariant(QMetaType::fromType<ulong>(), &ulongInt) << false;
+ QTest::newRow("QMetaType::UShort invalid") << QVariant(QMetaType::fromType<ushort>(), nullptr) << true;
ushort ushortInt = 1u;
- QTest::newRow("QMetaType::UShort") << QVariant(QMetaType(QMetaType::UShort), &ushortInt) << false;
- QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType(QMetaType::UChar), nullptr) << true;
+ QTest::newRow("QMetaType::UShort") << QVariant(QMetaType::fromType<ushort>(), &ushortInt) << false;
+ QTest::newRow("QMetaType::UChar invalid") << QVariant(QMetaType::fromType<uchar>(), nullptr) << true;
uchar uch = 0xf0;
- QTest::newRow("QMetaType::UChar") << QVariant(QMetaType(QMetaType::UChar), &uch) << false;
- QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType(QMetaType::Float), nullptr) << true;
+ QTest::newRow("QMetaType::UChar") << QVariant(QMetaType::fromType<uchar>(), &uch) << false;
+ QTest::newRow("QMetaType::Float invalid") << QVariant(QMetaType::fromType<float>(), nullptr) << true;
float f = 1.234f;
- QTest::newRow("QMetaType::Float") << QVariant(QMetaType(QMetaType::Float), &f) << false;
+ QTest::newRow("QMetaType::Float") << QVariant(QMetaType::fromType<float>(), &f) << false;
CustomStreamableClass custom = {123};
QTest::newRow("Custom type") << QVariant::fromValue(custom) << false;
}
@@ -1366,10 +1646,11 @@ void tst_QVariant::writeToReadFromDataStream()
// Since only a few won't match since the serial numbers are different
// I won't bother adding another bool in the data test.
const int writeType = writeVariant.userType();
- if (writeType == qMetaTypeId<CustomStreamableClass>())
- QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant), qvariant_cast<CustomStreamableClass>(writeVariant));
- else if ( writeType != QVariant::Invalid && writeType != QVariant::Bitmap && writeType != QVariant::Pixmap
- && writeType != QVariant::Image) {
+ if (writeType == qMetaTypeId<CustomStreamableClass>()) {
+ QCOMPARE(qvariant_cast<CustomStreamableClass>(readVariant),
+ qvariant_cast<CustomStreamableClass>(writeVariant));
+ } else if ( writeType != QMetaType::UnknownType && writeType != QMetaType::QBitmap
+ && writeType != QMetaType::QPixmap && writeType != QMetaType::QImage) {
switch (writeType) {
default:
QCOMPARE( readVariant, writeVariant );
@@ -1444,7 +1725,7 @@ void tst_QVariant::checkDataStream()
// constructed. However, it might be worth considering changing that behavior
// in the future.
// QCOMPARE(in.status(), QDataStream::ReadCorruptData);
- QCOMPARE(v.type(), QVariant::Invalid);
+ QCOMPARE(v.typeId(), QMetaType::UnknownType);
}
void tst_QVariant::operator_eq_eq_data()
@@ -1461,7 +1742,7 @@ void tst_QVariant::operator_eq_eq_data()
// Int
QTest::newRow( "int1int1" ) << i1 << i1 << true;
QTest::newRow( "int1int0" ) << i1 << i0 << false;
- QTest::newRow( "nullint" ) << i0 << QVariant(QVariant::Int) << true;
+ QTest::newRow( "nullint" ) << i0 << QVariant(QMetaType::fromType<int>()) << true;
// LongLong and ULongLong
QVariant ll1( (qlonglong)1 );
@@ -1479,6 +1760,10 @@ void tst_QVariant::operator_eq_eq_data()
QVariant mIntString(QByteArray("-42"));
QVariant mIntQString(QString("-42"));
+ QVariant mIntZero(0);
+ QVariant mIntStringZero(QByteArray("0"));
+ QVariant mIntQStringZero(QString("0"));
+
QVariant mUInt(42u);
QVariant mUIntString(QByteArray("42"));
QVariant mUIntQString(QString("42"));
@@ -1519,6 +1804,9 @@ void tst_QVariant::operator_eq_eq_data()
QVariant mBoolString(QByteArray("false"));
QVariant mBoolQString(QString("false"));
+ QVariant mTextString(QByteArray("foobar"));
+ QVariant mTextQString(QString("foobar"));
+
QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true;
QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true;
QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << false;
@@ -1526,6 +1814,21 @@ void tst_QVariant::operator_eq_eq_data()
QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true;
QTest::newRow( "mIntQString_mInt" ) << mIntQString << mInt << true;
+ QTest::newRow( "mIntZero_mIntStringZero" ) << mIntZero << mIntStringZero << false;
+ QTest::newRow( "mIntStringZero_mIntZero" ) << mIntStringZero << mIntZero << false;
+ QTest::newRow( "mIntZero_mIntQStringZero" ) << mIntZero << mIntQStringZero << true;
+ QTest::newRow( "mIntQStringZero_mIntZero" ) << mIntQStringZero << mIntZero << true;
+
+ QTest::newRow( "mInt_mTextString" ) << mInt << mTextString << false;
+ QTest::newRow( "mTextString_mInt" ) << mTextString << mInt << false;
+ QTest::newRow( "mInt_mTextQString" ) << mInt << mTextQString << false;
+ QTest::newRow( "mTextQString_mInt" ) << mTextQString << mInt << false;
+
+ QTest::newRow( "mIntZero_mTextString" ) << mIntZero << mTextString << false;
+ QTest::newRow( "mTextString_mIntZero" ) << mTextString << mIntZero << false;
+ QTest::newRow( "mIntZero_mTextQString" ) << mIntZero << mTextQString << false;
+ QTest::newRow( "mTextQString_mIntZero" ) << mTextQString << mIntZero << false;
+
QTest::newRow( "mUInt_mUIntString" ) << mUInt << mUIntString << false;
QTest::newRow( "mUIntString_mUInt" ) << mUIntString << mUInt << false;
QTest::newRow( "mUInt_mUIntQString" ) << mUInt << mUIntQString << true;
@@ -1536,6 +1839,11 @@ void tst_QVariant::operator_eq_eq_data()
QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true;
QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true;
+ QTest::newRow( "mDouble_mTextString" ) << mDouble << mTextString << false;
+ QTest::newRow( "mTextString_mDouble" ) << mTextString << mDouble << false;
+ QTest::newRow( "mDouble_mTextQString" ) << mDouble << mTextQString << false;
+ QTest::newRow( "mTextQString_mDouble" ) << mTextQString << mDouble << false;
+
QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << false;
QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << false;
QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true;
@@ -1702,9 +2010,11 @@ void tst_QVariant::operator_eq_eq()
QFETCH( QVariant, left );
QFETCH( QVariant, right );
QFETCH( bool, equal );
- QCOMPARE( left == right, equal );
+ QT_TEST_EQUALITY_OPS(left, right, equal);
}
+#if QT_DEPRECATED_SINCE(6, 0)
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
void tst_QVariant::typeName_data()
{
QTest::addColumn<int>("type");
@@ -1777,17 +2087,23 @@ void tst_QVariant::typeToName()
// assumes that QVariant::Type contains consecutive values
int max = QVariant::LastGuiType;
- for ( int t = 1; t <= max; t++ ) {
+ for (int t = 1; t <= max; ++t) {
+ if (!QMetaType::isRegistered(t)) {
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ "^Trying to construct an instance of an invalid type"));
+ }
const char *n = QVariant::typeToName( (QVariant::Type)t );
if (n)
QCOMPARE( int(QVariant::nameToType( n )), t );
-
}
+
QCOMPARE(QVariant::typeToName(QVariant::Int), "int");
// not documented but we return 0 if the type is out of range
// by testing this we catch cases where QVariant is extended
// but type_map is not updated accordingly
- QCOMPARE( QVariant::typeToName( QVariant::Type(max+1) ), (char*)0 );
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ "^Trying to construct an instance of an invalid type"));
+ QCOMPARE(QVariant::typeToName(QVariant::Type(max + 1)), (const char *)nullptr);
// invalid type names
QVERIFY( QVariant::nameToType( 0 ) == QVariant::Invalid );
QVERIFY( QVariant::nameToType( "" ) == QVariant::Invalid );
@@ -1801,6 +2117,8 @@ void tst_QVariant::typeToName()
QCOMPARE(QVariant::nameToType("Q_LLONG"), QVariant::Invalid);
QCOMPARE(QVariant::nameToType("Q_ULLONG"), QVariant::Invalid);
}
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 0)
void tst_QVariant::streamInvalidVariant()
{
@@ -1811,7 +2129,7 @@ void tst_QVariant::streamInvalidVariant()
QVariant writeVariant;
QVariant readVariant;
- QVERIFY( writeVariant.type() == QVariant::Invalid );
+ QVERIFY( writeVariant.typeId() == QMetaType::UnknownType );
QByteArray data;
QDataStream writeStream( &data, QIODevice::WriteOnly );
@@ -1823,7 +2141,7 @@ void tst_QVariant::streamInvalidVariant()
QVERIFY( readX == writeX );
// Two invalid QVariant's aren't necessarily the same, so == will
// return false if one is invalid, so check the type() instead
- QVERIFY( readVariant.type() == QVariant::Invalid );
+ QVERIFY( readVariant.typeId() == QMetaType::UnknownType );
QVERIFY( readY == writeY );
}
@@ -1869,7 +2187,9 @@ void tst_QVariant::userType()
QVariant userVar;
userVar.setValue(data);
- QCOMPARE(userVar.type(), QVariant::UserType);
+ QVERIFY(QMetaType::fromName("MyType").isValid());
+ QCOMPARE(QMetaType::fromName("MyType"), QMetaType::fromType<MyType>());
+ QVERIFY(userVar.typeId() > QMetaType::User);
QCOMPARE(userVar.userType(), qMetaTypeId<MyType>());
QCOMPARE(userVar.typeName(), "MyType");
QVERIFY(!userVar.isNull());
@@ -1898,7 +2218,7 @@ void tst_QVariant::userType()
QVariant userVar;
userVar.setValue(&data);
- QCOMPARE(userVar.type(), QVariant::UserType);
+ QVERIFY(userVar.typeId() > QMetaType::User);
QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>());
QCOMPARE(userVar.typeName(), "MyType*");
QVERIFY(!userVar.isNull());
@@ -1992,7 +2312,15 @@ void tst_QVariant::podUserType()
pod.a = 10;
pod.b = 20;
+ // one of these two must register the type
+ // (QVariant::fromValue calls QMetaType::fromType)
QVariant pod_as_variant = QVariant::fromValue(pod);
+ QMetaType mt = QMetaType::fromType<MyTypePOD>();
+ QCOMPARE(pod_as_variant.metaType(), mt);
+ QCOMPARE(pod_as_variant.metaType().name(), mt.name());
+ QCOMPARE(QMetaType::fromName(mt.name()), mt);
+ QCOMPARE_NE(pod_as_variant.typeId(), 0);
+
MyTypePOD pod2 = qvariant_cast<MyTypePOD>(pod_as_variant);
QCOMPARE(pod.a, pod2.a);
@@ -2010,37 +2338,37 @@ void tst_QVariant::basicUserType()
QVariant v;
{
int i = 7;
- v = QVariant(QMetaType(QMetaType::Int), &i);
+ v = QVariant(QMetaType::fromType<int>(), &i);
}
- QCOMPARE(v.type(), QVariant::Int);
+ QCOMPARE(v.typeId(), QMetaType::Int);
QCOMPARE(v.toInt(), 7);
{
QString s("foo");
- v = QVariant(QMetaType(QMetaType::QString), &s);
+ v = QVariant(QMetaType::fromType<QString>(), &s);
}
- QCOMPARE(v.type(), QVariant::String);
+ QCOMPARE(v.typeId(), QMetaType::QString);
QCOMPARE(v.toString(), QString("foo"));
{
double d = 4.4;
- v = QVariant(QMetaType(QMetaType::Double), &d);
+ v = QVariant(QMetaType::fromType<double>(), &d);
}
- QCOMPARE(v.type(), QVariant::Double);
+ QCOMPARE(v.typeId(), QMetaType::Double);
QCOMPARE(v.toDouble(), 4.4);
{
float f = 4.5f;
- v = QVariant(QMetaType(QMetaType::Float), &f);
+ v = QVariant(QMetaType::fromType<float>(), &f);
}
QCOMPARE(v.userType(), int(QMetaType::Float));
QCOMPARE(v.toDouble(), 4.5);
{
QByteArray ba("bar");
- v = QVariant(QMetaType(QMetaType::QByteArray), &ba);
+ v = QVariant(QMetaType::fromType<QByteArray>(), &ba);
}
- QCOMPARE(v.type(), QVariant::ByteArray);
+ QCOMPARE(v.typeId(), QMetaType::QByteArray);
QCOMPARE(v.toByteArray(), QByteArray("bar"));
}
@@ -2205,7 +2533,7 @@ void tst_QVariant::saveLoadCustomTypes()
QVariant v = QVariant(tp, &i);
QCOMPARE(v.userType(), tp.id());
- QCOMPARE(v.type(), QVariant::UserType);
+ QVERIFY(v.typeId() > QMetaType::User);
{
QDataStream stream(&data, QIODevice::WriteOnly);
stream << v;
@@ -2218,7 +2546,7 @@ void tst_QVariant::saveLoadCustomTypes()
stream >> v;
}
- QCOMPARE(int(v.userType()), QMetaType::type("Blah"));
+ QCOMPARE(int(v.userType()), QMetaType::fromName("Blah").id());
int value = *(int*)v.constData();
QCOMPARE(value, 42);
}
@@ -2317,7 +2645,7 @@ void tst_QVariant::qvariant_cast_QObject_data()
QTest::addColumn<bool>("isNull");
QObject *obj = new QObject;
obj->setObjectName(QString::fromLatin1("Hello"));
- QTest::newRow("from QObject") << QVariant(QMetaType(QMetaType::QObjectStar), &obj) << true << false;
+ QTest::newRow("from QObject") << QVariant(QMetaType::fromType<QObject*>(), &obj) << true << false;
QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true << false;
QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false << false;
QTest::newRow("from int") << QVariant((int) 123) << false << false;
@@ -2352,18 +2680,18 @@ void tst_QVariant::qvariant_cast_QObject()
if (!isNull)
QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
QVERIFY(data.canConvert<QObject*>());
- QVERIFY(data.canConvert(QMetaType::QObjectStar));
- QVERIFY(data.canConvert(::qMetaTypeId<QObject*>()));
+ QVERIFY(data.canConvert(QMetaType::fromType<QObject*>()));
+ QVERIFY(data.canConvert(QMetaType(::qMetaTypeId<QObject*>())));
QCOMPARE(data.value<QObject*>() == nullptr, isNull);
- QVERIFY(data.convert(QMetaType::QObjectStar));
+ QVERIFY(data.convert(QMetaType::fromType<QObject*>()));
QCOMPARE(data.userType(), int(QMetaType::QObjectStar));
} else {
QVERIFY(!data.canConvert<QObject*>());
- QVERIFY(!data.canConvert(QMetaType::QObjectStar));
- QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>()));
+ QVERIFY(!data.canConvert(QMetaType::fromType<QObject*>()));
+ QVERIFY(!data.canConvert(QMetaType(::qMetaTypeId<QObject*>())));
QVERIFY(!data.value<QObject*>());
QVERIFY(data.userType() != QMetaType::QObjectStar);
- QVERIFY(!data.convert(QMetaType::QObjectStar));
+ QVERIFY(!data.convert(QMetaType::fromType<QObject*>()));
}
}
@@ -2404,7 +2732,7 @@ void tst_QVariant::qvariant_cast_QObject_derived()
QObject *object = new CustomQObjectDerivedNoMetaType(this);
QVariant data = QVariant::fromValue(object);
QVERIFY(data.canConvert<CustomQObjectDerivedNoMetaType*>());
- QVERIFY(data.convert(qMetaTypeId<CustomQObjectDerivedNoMetaType*>()));
+ QVERIFY(data.convert(QMetaType(qMetaTypeId<CustomQObjectDerivedNoMetaType*>())));
QCOMPARE(data.value<CustomQObjectDerivedNoMetaType*>(), object);
QCOMPARE(data.isNull(), false);
}
@@ -2484,7 +2812,7 @@ void tst_QVariant::qvariant_cast_QObject_wrapper()
QObjectWrapper wrapper(object);
QVariant v = QVariant::fromValue(wrapper);
QCOMPARE(v.value<QObject*>(), object);
- v.convert(qMetaTypeId<QObject*>());
+ v.convert(QMetaType(qMetaTypeId<QObject*>()));
QCOMPARE(v.value<QObject*>(), object);
MyNS::SequentialContainer<int> sc;
@@ -2655,18 +2983,289 @@ void tst_QVariant::convertToQUint8() const
}
}
-void tst_QVariant::comparePointers() const
+void tst_QVariant::compareCompiles() const
{
- class MyClass
- {
+ QTestPrivate::testEqualityOperatorsCompile<QVariant>();
+}
+
+void tst_QVariant::compareNumerics_data() const
+{
+ QTest::addColumn<QVariant>("v1");
+ QTest::addColumn<QVariant>("v2");
+ QTest::addColumn<QPartialOrdering>("result");
+
+ QTest::addRow("invalid-invalid")
+ << QVariant() << QVariant() << QPartialOrdering::Unordered;
+
+ static const auto asString = [](const QVariant &v) {
+ if (v.isNull())
+ return QStringLiteral("null");
+ if (v.metaType().flags() & QMetaType::IsEnumeration)
+ return v.metaType().flags() & QMetaType::IsUnsignedEnumeration ?
+ QString::number(v.toULongLong()) :
+ QString::number(v.toLongLong());
+ switch (v.typeId()) {
+ case QMetaType::Char16:
+ return QString::number(qvariant_cast<char16_t>(v));
+ case QMetaType::Char32:
+ return QString::number(qvariant_cast<char32_t>(v));
+ case QMetaType::Char:
+ case QMetaType::UChar:
+ return QString::number(v.toUInt());
+ case QMetaType::SChar:
+ return QString::number(v.toInt());
+ }
+ return v.toString();
};
- MyClass myClass;
+ auto addCompareToInvalid = [](auto value) {
+ QVariant v = QVariant::fromValue(value);
+ QTest::addRow("invalid-%s(%s)", v.typeName(), qPrintable(asString(v)))
+ << QVariant() << v << QPartialOrdering::Unordered;
+ QTest::addRow("%s(%s)-invalid", v.typeName(), qPrintable(asString(v)))
+ << v << QVariant() << QPartialOrdering::Unordered;
+ };
+ addCompareToInvalid(false);
+ addCompareToInvalid(true);
+ addCompareToInvalid(char(0));
+ addCompareToInvalid(qint8(0));
+ addCompareToInvalid(quint8(0));
+ addCompareToInvalid(short(0));
+ addCompareToInvalid(ushort(0));
+ addCompareToInvalid(int(0));
+ addCompareToInvalid(uint(0));
+ addCompareToInvalid(long(0));
+ addCompareToInvalid(ulong(0));
+ addCompareToInvalid(qint64(0));
+ addCompareToInvalid(quint64(0));
+ addCompareToInvalid(0.f);
+ addCompareToInvalid(0.0);
+ addCompareToInvalid(QCborSimpleType{});
+
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_CLANG("-Wsign-compare")
+QT_WARNING_DISABLE_GCC("-Wsign-compare")
+QT_WARNING_DISABLE_MSVC(4018) // '<': signed/unsigned mismatch
+ static const auto addComparePairWithResult = [](auto value1, auto value2, QPartialOrdering order) {
+ QVariant v1 = QVariant::fromValue(value1);
+ QVariant v2 = QVariant::fromValue(value2);
+ QTest::addRow("%s(%s)-%s(%s)", v1.typeName(), qPrintable(asString(v1)),
+ v2.typeName(), qPrintable(asString(v2)))
+ << v1 << v2 << order;
+ };
- QVariant v = QVariant::fromValue<void *>(&myClass);
- QVariant v2 = QVariant::fromValue<void *>(&myClass);
+ static const auto addComparePair = [](auto value1, auto value2) {
+ QPartialOrdering order = QPartialOrdering::Unordered;
+ if (value1 == value2)
+ order = QPartialOrdering::Equivalent;
+ else if (value1 < value2)
+ order = QPartialOrdering::Less;
+ else if (value1 > value2)
+ order = QPartialOrdering::Greater;
+ addComparePairWithResult(value1, value2, order);
+ };
+QT_WARNING_POP
+
+ // homogeneous first
+ static const auto addList = [](auto list) {
+ for (auto v1 : list)
+ for (auto v2 : list)
+ addComparePair(v1, v2);
+ };
- QCOMPARE(v, v2);
+ auto addSingleType = [](auto zero) {
+ using T = decltype(zero);
+ T one = T(zero + 1);
+ T min = std::numeric_limits<T>::min();
+ T max = std::numeric_limits<T>::max();
+ T mid = max / 2 + 1;
+ if (min != zero)
+ addList(std::array{zero, one, min, mid, max});
+ else
+ addList(std::array{zero, one, mid, max});
+ };
+ addList(std::array{ false, true });
+ addList(std::array{ QCborSimpleType{}, QCborSimpleType::False, QCborSimpleType(0xff) });
+ addSingleType(char(0));
+ addSingleType(char16_t(0));
+ addSingleType(char32_t(0));
+ addSingleType(qint8(0));
+ addSingleType(quint8(0));
+ addSingleType(qint16(0));
+ addSingleType(quint16(0));
+ addSingleType(qint32(0));
+ addSingleType(quint32(0));
+ addSingleType(qint64(0));
+ addSingleType(quint64(0));
+ addSingleType(0.f);
+ addSingleType(0.0);
+ addList(std::array{ EnumTest_Enum0{}, EnumTest_Enum0_value, EnumTest_Enum0_negValue });
+ addList(std::array{ EnumTest_Enum1{}, EnumTest_Enum1_value, EnumTest_Enum1_bigValue });
+ addList(std::array{ EnumTest_Enum7{}, EnumTest_Enum7::EnumTest_Enum7_value, EnumTest_Enum7::ensureSignedEnum7 });
+ addList(std::array{ Qt::AlignRight|Qt::AlignHCenter, Qt::AlignCenter|Qt::AlignVCenter });
+
+ // heterogeneous
+ addComparePair(char(0), qint8(-127));
+ addComparePair(char(127), qint8(127));
+ addComparePair(char(127), quint8(127));
+ addComparePair(qint8(-1), quint8(255));
+ addComparePair(char16_t(256), qint8(-1));
+ addComparePair(char16_t(256), short(-1));
+ addComparePair(char16_t(256), int(-1));
+ addComparePair(char32_t(256), int(-1));
+ addComparePair(0U, -1);
+ addComparePair(~0U, -1);
+ addComparePair(Q_UINT64_C(0), -1);
+ addComparePair(~Q_UINT64_C(0), -1);
+ addComparePair(Q_UINT64_C(0), Q_INT64_C(-1));
+ addComparePair(~Q_UINT64_C(0), Q_INT64_C(-1));
+ addComparePair(INT_MAX, uint(INT_MAX));
+ addComparePair(INT_MAX, qint64(INT_MAX) + 1);
+ addComparePair(INT_MAX, UINT_MAX);
+ addComparePair(INT_MAX, qint64(UINT_MAX));
+ addComparePair(INT_MAX, qint64(UINT_MAX) + 1);
+ addComparePair(INT_MAX, quint64(UINT_MAX));
+ addComparePair(INT_MAX, quint64(UINT_MAX) + 1);
+ addComparePair(INT_MAX, LONG_MIN);
+ addComparePair(INT_MAX, LONG_MAX);
+ addComparePair(INT_MAX, LLONG_MIN);
+ addComparePair(INT_MAX, LLONG_MAX);
+ addComparePair(INT_MIN, uint(INT_MIN));
+ addComparePair(INT_MIN, uint(INT_MIN) + 1);
+ addComparePair(INT_MIN + 1, uint(INT_MIN));
+ addComparePair(INT_MIN + 1, uint(INT_MIN) + 1);
+ addComparePair(INT_MIN, qint64(INT_MIN) - 1);
+ addComparePair(INT_MIN + 1, qint64(INT_MIN) + 1);
+ addComparePair(INT_MIN + 1, qint64(INT_MIN) - 1);
+ addComparePair(INT_MIN, UINT_MAX);
+ addComparePair(INT_MIN, qint64(UINT_MAX));
+ addComparePair(INT_MIN, qint64(UINT_MAX) + 1);
+ addComparePair(INT_MIN, quint64(UINT_MAX));
+ addComparePair(INT_MIN, quint64(UINT_MAX) + 1);
+ addComparePair(UINT_MAX, qint64(UINT_MAX) + 1);
+ addComparePair(UINT_MAX, quint64(UINT_MAX) + 1);
+ addComparePair(UINT_MAX, qint64(INT_MIN) - 1);
+ addComparePair(UINT_MAX, quint64(INT_MIN) + 1);
+ addComparePair(LLONG_MAX, quint64(LLONG_MAX));
+ addComparePair(LLONG_MAX, quint64(LLONG_MAX) + 1);
+ addComparePair(LLONG_MIN, quint64(LLONG_MAX));
+ addComparePair(LLONG_MIN, quint64(LLONG_MAX) + 1);
+ addComparePair(LLONG_MIN, quint64(LLONG_MIN) + 1);
+ addComparePair(LLONG_MIN + 1, quint64(LLONG_MIN) + 1);
+ addComparePair(LLONG_MIN, LLONG_MAX - 1);
+ // addComparePair(LLONG_MIN, LLONG_MAX); // already added by addSingleType()
+
+ // floating point
+ addComparePair(0.f, 0);
+ addComparePair(0.f, 0U);
+ addComparePair(0.f, Q_INT64_C(0));
+ addComparePair(0.f, Q_UINT64_C(0));
+ addComparePair(0.f, 0.);
+ addComparePair(0.f, 1.);
+ addComparePair(float(1 << 24), 1 << 24);
+ addComparePair(float(1 << 24) - 1, (1 << 24) - 1);
+ addComparePair(-float(1 << 24), 1 << 24);
+ addComparePair(-float(1 << 24) + 1, -(1 << 24) + 1);
+ addComparePair(HUGE_VALF, qInf());
+ addComparePair(HUGE_VALF, -qInf());
+ addComparePair(qQNaN(), std::numeric_limits<float>::quiet_NaN());
+ if (sizeof(qreal) == sizeof(double)) {
+ addComparePair(std::numeric_limits<float>::min(), std::numeric_limits<double>::min());
+ addComparePair(std::numeric_limits<float>::min(), std::numeric_limits<double>::max());
+ addComparePair(std::numeric_limits<float>::max(), std::numeric_limits<double>::min());
+ addComparePair(std::numeric_limits<float>::max(), std::numeric_limits<double>::max());
+ addComparePair(double(Q_INT64_C(1) << 53), Q_INT64_C(1) << 53);
+ addComparePair(double(Q_INT64_C(1) << 53) - 1, (Q_INT64_C(1) << 53) - 1);
+ addComparePair(-double(Q_INT64_C(1) << 53), Q_INT64_C(1) << 53);
+ addComparePair(-double(Q_INT64_C(1) << 53) + 1, (Q_INT64_C(1) << 53) + 1);
+ }
+
+ // enums vs integers
+ addComparePair(EnumTest_Enum0_value, 0);
+ addComparePair(EnumTest_Enum0_value, 0U);
+ addComparePair(EnumTest_Enum0_value, 0LL);
+ addComparePair(EnumTest_Enum0_value, 0ULL);
+ addComparePair(EnumTest_Enum0_value, int(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_value, qint64(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_value, quint64(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_negValue, int(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_negValue, qint64(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_negValue, quint64(EnumTest_Enum0_value));
+ addComparePair(EnumTest_Enum0_negValue, int(EnumTest_Enum0_negValue));
+ addComparePair(EnumTest_Enum0_negValue, qint64(EnumTest_Enum0_negValue));
+ addComparePair(EnumTest_Enum0_negValue, quint64(EnumTest_Enum0_negValue));
+
+ addComparePair(EnumTest_Enum1_value, 0);
+ addComparePair(EnumTest_Enum1_value, 0U);
+ addComparePair(EnumTest_Enum1_value, 0LL);
+ addComparePair(EnumTest_Enum1_value, 0ULL);
+ addComparePair(EnumTest_Enum1_value, int(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_value, qint64(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_value, quint64(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_bigValue, int(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_bigValue, qint64(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_bigValue, quint64(EnumTest_Enum1_value));
+ addComparePair(EnumTest_Enum1_bigValue, int(EnumTest_Enum1_bigValue));
+ addComparePair(EnumTest_Enum1_bigValue, qint64(EnumTest_Enum1_bigValue));
+ addComparePair(EnumTest_Enum1_bigValue, quint64(EnumTest_Enum1_bigValue));
+
+ addComparePair(EnumTest_Enum3_value, 0);
+ addComparePair(EnumTest_Enum3_value, 0U);
+ addComparePair(EnumTest_Enum3_value, 0LL);
+ addComparePair(EnumTest_Enum3_value, 0ULL);
+ addComparePair(EnumTest_Enum3_value, int(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_value, qint64(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_value, quint64(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_bigValue, int(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_bigValue, qint64(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_bigValue, quint64(EnumTest_Enum3_value));
+ addComparePair(EnumTest_Enum3_bigValue, int(EnumTest_Enum3_bigValue));
+ addComparePair(EnumTest_Enum3_bigValue, qint64(EnumTest_Enum3_bigValue));
+ addComparePair(EnumTest_Enum3_bigValue, quint64(EnumTest_Enum3_bigValue));
+
+ // enums of different types always compare as unordered
+ addComparePairWithResult(EnumTest_Enum0_value, EnumTest_Enum1_value, QPartialOrdering::Unordered);
+}
+
+void tst_QVariant::compareNumerics() const
+{
+ QFETCH(QVariant, v1);
+ QFETCH(QVariant, v2);
+ QFETCH(QPartialOrdering, result);
+ QCOMPARE(QVariant::compare(v1, v2), result);
+
+ QEXPECT_FAIL("invalid-invalid", "needs fixing", Abort);
+ QT_TEST_EQUALITY_OPS(v1, v2, is_eq(result));
+}
+
+void tst_QVariant::comparePointers() const
+{
+ class NonQObjectClass {};
+ const std::array<NonQObjectClass, 2> arr{ NonQObjectClass{}, NonQObjectClass{} };
+
+ const QVariant nonObjV1 = QVariant::fromValue<const void*>(&arr[0]);
+ const QVariant nonObjV2 = QVariant::fromValue<const void*>(&arr[1]);
+
+ Qt::partial_ordering expectedOrdering = Qt::partial_ordering::equivalent;
+ QCOMPARE(QVariant::compare(nonObjV1, nonObjV1), expectedOrdering);
+ QT_TEST_EQUALITY_OPS(nonObjV1, nonObjV1, is_eq(expectedOrdering));
+
+ expectedOrdering = Qt::partial_ordering::less;
+ QCOMPARE(QVariant::compare(nonObjV1, nonObjV2), expectedOrdering);
+ QT_TEST_EQUALITY_OPS(nonObjV1, nonObjV2, is_eq(expectedOrdering));
+
+ class QObjectClass : public QObject
+ {
+ public:
+ QObjectClass(QObject *parent = nullptr) : QObject(parent) {}
+ };
+ const QObjectClass c1;
+ const QObjectClass c2;
+
+ const QVariant objV1 = QVariant::fromValue(&c1);
+ const QVariant objV2 = QVariant::fromValue(&c2);
+ QT_TEST_EQUALITY_OPS(objV1, objV1, true);
+ QT_TEST_EQUALITY_OPS(objV1, objV2, false);
}
struct Data {};
@@ -2783,7 +3382,7 @@ void tst_QVariant::variantToDateTimeWithoutWarnings() const
{
QVariant v1(QLatin1String("xyz"));
- v1.convert(QVariant::DateTime);
+ v1.convert(QMetaType::fromType<QDateTime>());
QVariant v2(QLatin1String("xyz"));
QDateTime dt1(v2.toDateTime());
@@ -2797,7 +3396,7 @@ void tst_QVariant::invalidDateTime() const
{
QVariant variant(QString::fromLatin1("Invalid date time string"));
QVERIFY(!variant.toDateTime().isValid());
- QVERIFY(!variant.convert(QVariant::DateTime));
+ QVERIFY(!variant.convert(QMetaType::fromType<QDateTime>()));
}
struct MyClass
@@ -2838,22 +3437,22 @@ void tst_QVariant::invalidDate() const
{
QString foo("Hello");
QVariant variant(foo);
- QVERIFY(!variant.convert(QVariant::Date));
+ QVERIFY(!variant.convert(QMetaType::fromType<QDate>()));
variant = foo;
- QVERIFY(!variant.convert(QVariant::DateTime));
+ QVERIFY(!variant.convert(QMetaType::fromType<QDateTime>()));
variant = foo;
- QVERIFY(!variant.convert(QVariant::Time));
+ QVERIFY(!variant.convert(QMetaType::fromType<QTime>()));
variant = foo;
- QVERIFY(!variant.convert(QVariant::Int));
+ QVERIFY(!variant.convert(QMetaType::fromType<int>()));
variant = foo;
- QVERIFY(!variant.convert(QVariant::Double));
+ QVERIFY(!variant.convert(QMetaType::fromType<double>()));
variant = foo;
- QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float)));
+ QVERIFY(!variant.convert(QMetaType::fromType<float>()));
}
struct WontCompare
@@ -2865,35 +3464,49 @@ Q_DECLARE_METATYPE(WontCompare);
struct WillCompare
{
int x;
+
+ friend bool operator==(const WillCompare &a, const WillCompare &b)
+ { return a.x == b.x; }
+ friend bool operator<(const WillCompare &a, const WillCompare &b)
+ { return a.x < b.x; }
};
-bool operator==(const WillCompare &a, const WillCompare &b) { return a.x == b.x; }
Q_DECLARE_METATYPE(WillCompare);
-void tst_QVariant::compareCustomTypes() const
+void tst_QVariant::compareCustomTypes_data() const
{
- {
- WontCompare f1{0};
- const QVariant variant1(QVariant::fromValue(f1));
+ QTest::addColumn<QVariant>("v1");
+ QTest::addColumn<QVariant>("v2");
+ QTest::addColumn<Qt::partial_ordering>("expectedOrdering");
- WontCompare f2{1};
- const QVariant variant2(QVariant::fromValue(f2));
+ QTest::newRow("same_uncomparable")
+ << QVariant::fromValue(WontCompare{0})
+ << QVariant::fromValue(WontCompare{0})
+ << Qt::partial_ordering::unordered;
- /* No comparison operator exists. */
- QVERIFY(variant1 != variant2);
- QVERIFY(variant1 != variant1);
- QVERIFY(variant2 != variant2);
- }
- {
- WillCompare f1{0};
- const QVariant variant1(QVariant::fromValue(f1));
+ QTest::newRow("same_comparable")
+ << QVariant::fromValue(WillCompare{0})
+ << QVariant::fromValue(WillCompare{0})
+ << Qt::partial_ordering::equivalent;
- WillCompare f2 {1};
- const QVariant variant2(QVariant::fromValue(f2));
+ QTest::newRow("different_comparable")
+ << QVariant::fromValue(WillCompare{1})
+ << QVariant::fromValue(WillCompare{0})
+ << Qt::partial_ordering::greater;
- QVERIFY(variant1 != variant2);
- QCOMPARE(variant1, variant1);
- QCOMPARE(variant2, variant2);
- }
+ QTest::newRow("qdatetime_vs_comparable")
+ << QVariant::fromValue(QDateTime::currentDateTimeUtc())
+ << QVariant::fromValue(WillCompare{0})
+ << Qt::partial_ordering::unordered;
+}
+
+void tst_QVariant::compareCustomTypes() const
+{
+ QFETCH(const QVariant, v1);
+ QFETCH(const QVariant, v2);
+ QFETCH(const Qt::partial_ordering, expectedOrdering);
+
+ QCOMPARE(QVariant::compare(v1, v2), expectedOrdering);
+ QT_TEST_EQUALITY_OPS(v1, v2, is_eq(expectedOrdering));
}
void tst_QVariant::timeToDateTime() const
{
@@ -3037,7 +3650,7 @@ void tst_QVariant::convertByteArrayToBool() const
QFETCH(QByteArray, output);
const QVariant variant(input);
- QCOMPARE(variant.type(), QVariant::Bool);
+ QCOMPARE(variant.typeId(), QMetaType::Bool);
QCOMPARE(variant.toBool(), input);
QVERIFY(variant.canConvert<bool>());
@@ -3063,57 +3676,57 @@ void tst_QVariant::convertIterables() const
{
QStringList list;
list.append("Hello");
- QCOMPARE(QVariant::fromValue(list).value<QVariantList>().count(), list.count());
+ QCOMPARE(QVariant::fromValue(list).value<QVariantList>().size(), list.size());
}
{
QByteArrayList list;
list.append("Hello");
- QCOMPARE(QVariant::fromValue(list).value<QVariantList>().count(), list.count());
+ QCOMPARE(QVariant::fromValue(list).value<QVariantList>().size(), list.size());
}
{
QVariantList list;
list.append("World");
- QCOMPARE(QVariant::fromValue(list).value<QVariantList>().count(), list.count());
+ QCOMPARE(QVariant::fromValue(list).value<QVariantList>().size(), list.size());
}
{
QMap<QString, int> map;
map.insert("3", 4);
- QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
- QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().size(), map.size());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().size(), map.size());
map.insert("4", 5);
- QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
- QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().size(), map.size());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().size(), map.size());
}
{
QVariantMap map;
map.insert("3", 4);
- QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
- QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().size(), map.size());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().size(), map.size());
map.insert("4", 5);
- QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
- QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().size(), map.size());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().size(), map.size());
}
{
QHash<QString, int> hash;
hash.insert("3", 4);
- QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
- QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().size(), hash.size());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().size(), hash.size());
hash.insert("4", 5);
- QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
- QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().size(), hash.size());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().size(), hash.size());
}
{
QVariantHash hash;
hash.insert("3", 4);
- QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
- QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().size(), hash.size());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().size(), hash.size());
hash.insert("4", 5);
- QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
- QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().size(), hash.size());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().size(), hash.size());
}
}
@@ -3205,13 +3818,13 @@ void tst_QVariant::fpStringRoundtrip() const
QFETCH(QVariant, number);
QVariant converted = number;
- QVERIFY(converted.convert(QVariant::String));
- QVERIFY(converted.convert(number.type()));
+ QVERIFY(converted.convert(QMetaType::fromType<QString>()));
+ QVERIFY(converted.convert(QMetaType(number.typeId())));
QCOMPARE(converted, number);
converted = number;
- QVERIFY(converted.convert(QVariant::ByteArray));
- QVERIFY(converted.convert(number.type()));
+ QVERIFY(converted.convert(QMetaType::fromType<QByteArray>()));
+ QVERIFY(converted.convert(QMetaType(number.typeId())));
QCOMPARE(converted, number);
}
@@ -3249,7 +3862,7 @@ void tst_QVariant::numericalConvert()
}
switch (v.userType())
{
- case QVariant::Double:
+ case QMetaType::Double:
QCOMPARE(v.toString() , QString::number(num, 'g', QLocale::FloatingPointShortest));
break;
case QMetaType::Float:
@@ -3314,10 +3927,8 @@ template<class T> void playWithVariant(const T &orig, bool isNull, const QString
QCOMPARE(v.toBool(), toBool);
QCOMPARE(qvariant_cast<T>(v), orig);
- if (qMetaTypeId<T>() != qMetaTypeId<QVariant>()) {
+ if (qMetaTypeId<T>() != qMetaTypeId<QVariant>())
QCOMPARE(v.userType(), qMetaTypeId<T>());
- QCOMPARE(QVariant::typeToName(QVariant::Type(v.userType())), QMetaType::typeName(qMetaTypeId<T>()));
- }
}
#define PLAY_WITH_VARIANT(Orig, IsNull, ToString, ToDouble, ToBool) \
@@ -3595,11 +4206,11 @@ void tst_QVariant::movabilityTest()
// prepare destination memory space to which variant will be moved
QVariant buffer[1];
- QCOMPARE(buffer[0].type(), QVariant::Invalid);
+ QCOMPARE(buffer[0].typeId(), QMetaType::UnknownType);
buffer[0].~QVariant();
memcpy(static_cast<void *>(buffer), static_cast<void *>(&variant), sizeof(QVariant));
- QCOMPARE(buffer[0].type(), QVariant::UserType);
+ QVERIFY(buffer[0].typeId() > QMetaType::User);
QCOMPARE(buffer[0].userType(), qMetaTypeId<MyNotMovable>());
MyNotMovable tmp(buffer[0].value<MyNotMovable>());
@@ -3611,24 +4222,24 @@ void tst_QVariant::movabilityTest()
void tst_QVariant::variantInVariant()
{
QVariant var1 = 5;
- QCOMPARE(var1.type(), QVariant::Int);
+ QCOMPARE(var1.typeId(), QMetaType::Int);
QVariant var2 = var1;
QCOMPARE(var2, var1);
- QCOMPARE(var2.type(), QVariant::Int);
+ QCOMPARE(var2.typeId(), QMetaType::Int);
QVariant var3 = QVariant::fromValue(var1);
QCOMPARE(var3, var1);
- QCOMPARE(var3.type(), QVariant::Int);
+ QCOMPARE(var3.typeId(), QMetaType::Int);
QVariant var4 = qvariant_cast<QVariant>(var1);
QCOMPARE(var4, var1);
- QCOMPARE(var4.type(), QVariant::Int);
+ QCOMPARE(var4.typeId(), QMetaType::Int);
QVariant var5;
var5 = var1;
QCOMPARE(var5, var1);
- QCOMPARE(var5.type(), QVariant::Int);
+ QCOMPARE(var5.typeId(), QMetaType::Int);
QVariant var6;
var6.setValue(var1);
QCOMPARE(var6, var1);
- QCOMPARE(var6.type(), QVariant::Int);
+ QCOMPARE(var6.typeId(), QMetaType::Int);
QCOMPARE(QVariant::fromValue(var1), QVariant::fromValue(var2));
QCOMPARE(qvariant_cast<QVariant>(var3), QVariant::fromValue(var4));
@@ -3636,7 +4247,7 @@ void tst_QVariant::variantInVariant()
QString str("hello");
QVariant var8 = qvariant_cast<QVariant>(QVariant::fromValue(QVariant::fromValue(str)));
- QCOMPARE((int)var8.type(), (int)QVariant::String);
+ QCOMPARE(var8.typeId(), QMetaType::QString);
QCOMPARE(qvariant_cast<QString>(QVariant(qvariant_cast<QVariant>(var8))), str);
QVariant var9(QMetaType::fromType<QVariant>(), &var1);
@@ -3739,11 +4350,11 @@ void tst_QVariant::modelIndexConversion()
{
QVariant modelIndexVariant = QModelIndex();
QVERIFY(modelIndexVariant.canConvert<QPersistentModelIndex>());
- QVERIFY(modelIndexVariant.convert(QMetaType::QPersistentModelIndex));
- QCOMPARE(modelIndexVariant.type(), QVariant::PersistentModelIndex);
- QVERIFY(modelIndexVariant.canConvert(QMetaType::QModelIndex));
- QVERIFY(modelIndexVariant.convert(QMetaType::QModelIndex));
- QCOMPARE(modelIndexVariant.type(), QVariant::ModelIndex);
+ QVERIFY(modelIndexVariant.convert(QMetaType::fromType<QPersistentModelIndex>()));
+ QCOMPARE(modelIndexVariant.typeId(), QMetaType::QPersistentModelIndex);
+ QVERIFY(modelIndexVariant.canConvert(QMetaType::fromType<QModelIndex>()));
+ QVERIFY(modelIndexVariant.convert(QMetaType::fromType<QModelIndex>()));
+ QCOMPARE(modelIndexVariant.typeId(), QMetaType::QModelIndex);
}
class Forward;
@@ -3816,7 +4427,8 @@ void tst_QVariant::dataStream_data(QDataStream::Version version)
path = path.prepend(":/stream/").append("/");
QDir dir(path);
uint i = 0;
- foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) {
+ const auto entries = dir.entryInfoList(QStringList{u"*.bin"_s});
+ for (const QFileInfo &fileInfo : entries) {
QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
i += 1;
}
@@ -3837,15 +4449,15 @@ void tst_QVariant::loadQVariantFromDataStream(QDataStream::Version version)
QVariant loadedVariant;
stream >> typeName >> loadedVariant;
- const int id = QMetaType::type(typeName.toLatin1());
+ const int id = QMetaType::fromName(typeName.toLatin1()).id();
if (id == QMetaType::Void) {
// Void type is not supported by QVariant
return;
}
- QVariant constructedVariant(static_cast<QVariant::Type>(id));
+ QVariant constructedVariant {QMetaType(id)};
QCOMPARE(constructedVariant.userType(), id);
- QCOMPARE(QMetaType::typeName(loadedVariant.userType()), typeName.toLatin1().constData());
+ QCOMPARE(QMetaType(loadedVariant.userType()).name(), typeName.toLatin1().constData());
QCOMPARE(loadedVariant.userType(), constructedVariant.userType());
}
@@ -3860,7 +4472,7 @@ void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
QString typeName;
dataFileStream >> typeName;
QByteArray data = file.readAll();
- const int id = QMetaType::type(typeName.toLatin1());
+ const int id = QMetaType::fromName(typeName.toLatin1()).id();
if (id == QMetaType::Void) {
// Void type is not supported by QVariant
return;
@@ -3871,7 +4483,7 @@ void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version)
QDataStream stream(&buffer);
stream.setVersion(version);
- QVariant constructedVariant(static_cast<QVariant::Type>(id));
+ QVariant constructedVariant {QMetaType(id)};
QCOMPARE(constructedVariant.userType(), id);
stream << constructedVariant;
@@ -3889,12 +4501,13 @@ void tst_QVariant::debugStream_data()
QTest::addColumn<QVariant>("variant");
QTest::addColumn<int>("typeId");
for (int id = 0; id < QMetaType::LastCoreType + 1; ++id) {
- const char *tagName = QMetaType::typeName(id);
- if (!tagName)
- continue;
- if (id != QMetaType::Void) {
- QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
+ if (id && !QMetaType::isRegistered(id)) {
+ QTest::ignoreMessage(QtWarningMsg, QRegularExpression(
+ "^Trying to construct an instance of an invalid type"));
}
+ const char *tagName = QMetaType(id).name();
+ if (tagName && id != QMetaType::Void)
+ QTest::newRow(tagName) << QVariant(QMetaType(id)) << id;
}
QTest::newRow("QBitArray(111)") << QVariant(QBitArray(3, true)) << qMetaTypeId<QBitArray>();
QTest::newRow("CustomStreamableClass") << QVariant(QMetaType::fromType<CustomStreamableClass>(), 0) << qMetaTypeId<CustomStreamableClass>();
@@ -3913,6 +4526,7 @@ void tst_QVariant::debugStream()
QVERIFY(msgHandler.testPassed());
}
+#if QT_DEPRECATED_SINCE(6, 0)
struct MessageHandlerType : public MessageHandler
{
MessageHandlerType(const int typeId)
@@ -3923,9 +4537,11 @@ struct MessageHandlerType : public MessageHandler
// Format itself is not important, but basic data as a type name should be included in the output
ok = msg.startsWith("QVariant::");
QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
+QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
ok &= (currentId == QMetaType::UnknownType
? msg.contains("Invalid")
: msg.contains(QMetaType::typeName(currentId)));
+QT_WARNING_POP
QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
}
};
@@ -3941,9 +4557,10 @@ void tst_QVariant::debugStreamType()
QFETCH(int, typeId);
MessageHandlerType msgHandler(typeId);
- qDebug() << QVariant::Type(typeId);
+ QT_IGNORE_DEPRECATIONS(qDebug() << QVariant::Type(typeId);)
QVERIFY(msgHandler.testPassed());
}
+#endif // QT_DEPRECATED_SINCE(6, 0)
void tst_QVariant::implicitConstruction()
{
@@ -4250,7 +4867,7 @@ void sortIterable(QSequentialIterable *iterable)
}
template<typename Container>
-void testSequentialIteration()
+static void testSequentialIteration()
{
int numSeen = 0;
Container sequence;
@@ -4275,16 +4892,6 @@ void testSequentialIteration()
QCOMPARE(numSeen, (int)std::distance(sequence.begin(), sequence.end()));
QCOMPARE(containerIter, containerEnd);
- containerIter = sequence.begin();
- numSeen = 0;
- Q_FOREACH (const QVariant &v, listIter) {
- QVERIFY(ContainerAPI<Container>::compare(v, *containerIter));
- QVERIFY(ContainerAPI<Container>::compare(v, varList.at(numSeen)));
- ++containerIter;
- ++numSeen;
- }
- QCOMPARE(numSeen, (int)std::distance(sequence.begin(), sequence.end()));
-
numSeen = 0;
containerIter = sequence.begin();
for (QVariant v : listIter) {
@@ -4299,7 +4906,7 @@ void testSequentialIteration()
int numSeen = 0;
auto varList = listVariant.value<QVariantList>();
auto varIter = varList.begin();
- for (const QVariant &v : qAsConst(listIter)) {
+ for (const QVariant &v : std::as_const(listIter)) {
QVERIFY(ContainerAPI<Container>::compare(v, *varIter));
++varIter;
++numSeen;
@@ -4379,7 +4986,7 @@ void testSequentialIteration()
}
template<typename Container>
-void testAssociativeIteration()
+static void testAssociativeIteration()
{
using Key = typename Container::key_type;
using Mapped = typename Container::mapped_type;
@@ -4450,35 +5057,53 @@ void testAssociativeIteration()
QCOMPARE(f, iter.constEnd());
}
-void tst_QVariant::iterateContainerElements()
-{
- testSequentialIteration<QQueue<int>>();
- testSequentialIteration<QQueue<QVariant>>();
- testSequentialIteration<QQueue<QString>>();
- testSequentialIteration<QList<int>>();
- testSequentialIteration<QList<QVariant>>();
- testSequentialIteration<QList<QString>>();
- testSequentialIteration<QList<QByteArray>>();
- testSequentialIteration<QStack<int>>();
- testSequentialIteration<QStack<QVariant>>();
- testSequentialIteration<QStack<QString>>();
- testSequentialIteration<std::vector<int>>();
- testSequentialIteration<std::vector<QVariant>>();
- testSequentialIteration<std::vector<QString>>();
- testSequentialIteration<std::list<int>>();
- testSequentialIteration<std::list<QVariant>>();
- testSequentialIteration<std::list<QString>>();
- testSequentialIteration<QStringList>();
- testSequentialIteration<QByteArrayList>();
- testSequentialIteration<QString>();
- testSequentialIteration<QByteArray>();
+void tst_QVariant::iterateSequentialContainerElements_data()
+{
+ QTest::addColumn<QFunctionPointer>("testFunction");
+#define ADD(T) QTest::newRow(#T) << &testSequentialIteration<T>
+ ADD(QQueue<int>);
+ ADD(QQueue<QVariant>);
+ ADD(QQueue<QString>);
+ ADD(QList<int>);
+ ADD(QList<QVariant>);
+ ADD(QList<QString>);
+ ADD(QList<QByteArray>);
+ ADD(QStack<int>);
+ ADD(QStack<QVariant>);
+ ADD(QStack<QString>);
+ ADD(std::vector<int>);
+ ADD(std::vector<QVariant>);
+ ADD(std::vector<QString>);
+ ADD(std::list<int>);
+ ADD(std::list<QVariant>);
+ ADD(std::list<QString>);
+ ADD(QStringList);
+ ADD(QByteArrayList);
+ ADD(QString);
+ ADD(QByteArray);
#ifdef TEST_FORWARD_LIST
- testSequentialIteration<std::forward_list<int>>();
- testSequentialIteration<std::forward_list<QVariant>>();
- testSequentialIteration<std::forward_list<QString>>();
+ ADD(std::forward_list<int>);
+ ADD(std::forward_list<QVariant>);
+ ADD(std::forward_list<QString>);
#endif
+#undef ADD
+}
+
+void tst_QVariant::iterateAssociativeContainerElements_data()
+{
+ QTest::addColumn<QFunctionPointer>("testFunction");
+#define ADD(C, K, V) QTest::newRow(#C #K #V) << &testAssociativeIteration<C<K, V>>;
+ ADD(QHash, int, bool);
+ ADD(QHash, int, int);
+ ADD(QMap, int, bool);
+ ADD(std::map, int, bool);
+ ADD(std::unordered_map, int, bool);
+#undef ADD
+}
+void tst_QVariant::iterateContainerElements()
+{
{
QVariantList ints;
ints << 1 << 2 << 3;
@@ -4501,12 +5126,6 @@ void tst_QVariant::iterateContainerElements()
QCOMPARE(ints, intsCopy);
}
- testAssociativeIteration<QHash<int, bool>>();
- testAssociativeIteration<QHash<int, int>>();
- testAssociativeIteration<QMap<int, bool>>();
- testAssociativeIteration<std::map<int, bool>>();
- testAssociativeIteration<std::unordered_map<int, bool>>();
-
{
QMap<int, QString> mapping;
mapping.insert(1, "one");
@@ -4549,56 +5168,61 @@ void tst_QVariant::iterateContainerElements()
}
}
-void tst_QVariant::pairElements()
+template <typename Pair> static void testVariantPairElements()
{
- typedef QPair<QVariant, QVariant> QVariantPair;
+ QFETCH(std::function<void(void *)>, makeValue);
+ Pair p;
+ makeValue(&p);
+ QVariant v = QVariant::fromValue(p);
-#define TEST_PAIR_ELEMENT_ACCESS(PAIR, T1, T2, VALUE1, VALUE2) \
- { \
- PAIR<T1, T2> p(VALUE1, VALUE2); \
- QVariant v = QVariant::fromValue(p); \
- \
- QVERIFY(v.canConvert<QVariantPair>()); \
- QVariantPair pi = v.value<QVariantPair>(); \
- QCOMPARE(pi.first, QVariant::fromValue(VALUE1)); \
- QCOMPARE(pi.second, QVariant::fromValue(VALUE2)); \
- }
-
- TEST_PAIR_ELEMENT_ACCESS(QPair, int, int, 4, 5)
- TEST_PAIR_ELEMENT_ACCESS(std::pair, int, int, 4, 5)
- TEST_PAIR_ELEMENT_ACCESS(QPair, QString, QString, QStringLiteral("one"), QStringLiteral("two"))
- TEST_PAIR_ELEMENT_ACCESS(std::pair, QString, QString, QStringLiteral("one"), QStringLiteral("two"))
- TEST_PAIR_ELEMENT_ACCESS(QPair, QVariant, QVariant, 4, 5)
- TEST_PAIR_ELEMENT_ACCESS(std::pair, QVariant, QVariant, 4, 5)
- TEST_PAIR_ELEMENT_ACCESS(QPair, QVariant, int, 41, 15)
- TEST_PAIR_ELEMENT_ACCESS(std::pair, QVariant, int, 34, 65)
- TEST_PAIR_ELEMENT_ACCESS(QPair, int, QVariant, 24, 25)
- TEST_PAIR_ELEMENT_ACCESS(std::pair, int, QVariant, 44, 15)
+ QVERIFY(v.canConvert<QVariantPair>());
+ QVariantPair pi = v.value<QVariantPair>();
+ QCOMPARE(pi.first, QVariant::fromValue(p.first));
+ QCOMPARE(pi.second, QVariant::fromValue(p.second));
}
-enum EnumTest_Enum0 { EnumTest_Enum0_value = 42, EnumTest_Enum0_negValue = -8 };
-Q_DECLARE_METATYPE(EnumTest_Enum0)
-enum EnumTest_Enum1 : qint64 { EnumTest_Enum1_value = 42, EnumTest_Enum1_bigValue = (Q_INT64_C(1) << 33) + 50 };
-Q_DECLARE_METATYPE(EnumTest_Enum1)
+void tst_QVariant::pairElements_data()
+{
+ QTest::addColumn<QFunctionPointer>("testFunction");
+ QTest::addColumn<std::function<void(void *)>>("makeValue");
-enum EnumTest_Enum3 : qint64 { EnumTest_Enum3_value = -47, EnumTest_Enum3_bigValue = (Q_INT64_C(1) << 56) + 5 };
-Q_DECLARE_METATYPE(EnumTest_Enum3)
-enum EnumTest_Enum4 : quint64 { EnumTest_Enum4_value = 47, EnumTest_Enum4_bigValue = (Q_INT64_C(1) << 52) + 45 };
-Q_DECLARE_METATYPE(EnumTest_Enum4)
-enum EnumTest_Enum5 : uint { EnumTest_Enum5_value = 47 };
-Q_DECLARE_METATYPE(EnumTest_Enum5)
-enum EnumTest_Enum6 : uchar { EnumTest_Enum6_value = 47 };
-Q_DECLARE_METATYPE(EnumTest_Enum6)
-enum class EnumTest_Enum7 { EnumTest_Enum7_value = 47, ensureSignedEnum7 = -1 };
-Q_DECLARE_METATYPE(EnumTest_Enum7)
-enum EnumTest_Enum8 : short { EnumTest_Enum8_value = 47 };
-Q_DECLARE_METATYPE(EnumTest_Enum8)
+ static auto makeString = [](auto &&value) -> QString {
+ using T = std::decay_t<decltype(value)>;
+ if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>) {
+ return QString::number(value);
+ } else if constexpr (std::is_same_v<T, QVariant>) {
+ return value.toString();
+ } else {
+ return value;
+ }
+ };
+ auto addRow = [](auto &&first, auto &&second) {
+ using Pair = std::pair<std::decay_t<decltype(first)>, std::decay_t<decltype(second)>>;
+ std::function<void(void *)> makeValue = [=](void *pair) {
+ *static_cast<Pair *>(pair) = Pair{first, second};
+ };
-template<typename Enum> void testVariant(Enum value, bool *ok)
+ QTest::addRow("%s", qPrintable(makeString(first) + u',' + makeString(second)))
+ << &testVariantPairElements<Pair> << makeValue;
+ };
+
+ addRow(4, 5);
+ addRow(QStringLiteral("one"), QStringLiteral("two"));
+ addRow(QVariant(4), QVariant(5));
+ addRow(QVariant(41), 65);
+ addRow(41, QVariant(15));
+}
+
+template <auto value> static void testVariantEnum()
{
- *ok = false;
- QVariant var = QVariant::fromValue(value);
+ using Enum = decltype(value);
+ auto canLosslesslyConvert = [=](auto zero) {
+ return sizeof(value) <= sizeof(zero) ||
+ value == Enum(decltype(zero)(qToUnderlying(value)));
+ };
+ bool losslessConvertToInt = canLosslesslyConvert(int{});
+ QVariant var = QVariant::fromValue(value);
QCOMPARE(var.userType(), qMetaTypeId<Enum>());
QVERIFY(var.canConvert<Enum>());
@@ -4609,7 +5233,6 @@ template<typename Enum> void testVariant(Enum value, bool *ok)
QVERIFY(var.canConvert<qint64>());
QVERIFY(var.canConvert<quint64>());
-
QCOMPARE(var.value<Enum>(), value);
QCOMPARE(var.value<int>(), static_cast<int>(value));
QCOMPARE(var.value<uint>(), static_cast<uint>(value));
@@ -4619,11 +5242,18 @@ template<typename Enum> void testVariant(Enum value, bool *ok)
QCOMPARE(var.value<quint64>(), static_cast<quint64>(value));
QVariant var2 = var;
- QVERIFY(var2.convert(QMetaType::Int));
+ QVERIFY(var2.convert(QMetaType::fromType<int>()));
QCOMPARE(var2.value<int>(), static_cast<int>(value));
+ QVariant strVar = QString::number(qToUnderlying(value));
+ QVariant baVar = QByteArray::number(qToUnderlying(value));
+ QCOMPARE(strVar.value<Enum>(), value);
+ QCOMPARE(baVar.value<Enum>(), value);
+ QCOMPARE(var.value<QString>(), strVar);
+ QCOMPARE(var.value<QByteArray>(), baVar);
+
// unary + to silence gcc warning
- if ((+static_cast<qint64>(value) <= INT_MAX) && (+static_cast<qint64>(value) >= INT_MIN)) {
+ if (losslessConvertToInt) {
int intValue = static_cast<int>(value);
QVariant intVar = intValue;
QVERIFY(intVar.canConvert<Enum>());
@@ -4633,72 +5263,126 @@ template<typename Enum> void testVariant(Enum value, bool *ok)
QVERIFY(QVariant(longValue).canConvert<Enum>());
QCOMPARE(QVariant(longValue).value<Enum>(), value);
- *ok = true;
-}
-
-void tst_QVariant::enums()
-{
- bool ok = false;
- testVariant(EnumTest_Enum0_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum0_negValue, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum1_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum1_bigValue, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum3::EnumTest_Enum3_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum3::EnumTest_Enum3_bigValue, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum4::EnumTest_Enum4_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum4::EnumTest_Enum4_bigValue, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum5::EnumTest_Enum5_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum6::EnumTest_Enum6_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum7::EnumTest_Enum7_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum8::EnumTest_Enum8_value, &ok);
- QVERIFY(ok);
- testVariant(EnumTest_Enum3::EnumTest_Enum3_value, &ok);
- QVERIFY(ok);
-}
-
-template<typename Enum> void testVariantMeta(Enum value, bool *ok, const char *string)
-{
- testVariant<Enum>(value, ok);
- QVERIFY(ok);
- *ok = false;
+ auto value2 = Enum(qToUnderlying(value) + 1);
+ var2 = QVariant::fromValue(value2);
+ QCOMPARE_EQ(var, var);
+ QCOMPARE_NE(var, var2);
+ QCOMPARE(QVariant::compare(var, var), QPartialOrdering::Equivalent);
+ QCOMPARE(QVariant::compare(var, var2), QPartialOrdering::Less);
+ QCOMPARE(QVariant::compare(var2, var), QPartialOrdering::Greater);
+
+ QCOMPARE_EQ(var, static_cast<qint64>(value));
+ QCOMPARE_EQ(var, static_cast<quint64>(value));
+ QCOMPARE_EQ(static_cast<qint64>(value), var);
+ QCOMPARE_EQ(static_cast<quint64>(value), var);
+ QCOMPARE_NE(var2, static_cast<qint64>(value));
+ QCOMPARE_NE(var2, static_cast<quint64>(value));
+ QCOMPARE_NE(static_cast<qint64>(value), var2);
+ QCOMPARE_NE(static_cast<quint64>(value), var2);
+
+ if (losslessConvertToInt) {
+ QCOMPARE_EQ(var, int(value));
+ QCOMPARE_EQ(int(value), var);
+ QCOMPARE_NE(var2, int(value));
+ QCOMPARE_NE(int(value), var2);
+ }
+ if (canLosslesslyConvert(uint{})) {
+ QCOMPARE_EQ(var, uint(value));
+ QCOMPARE_EQ(uint(value), var);
+ QCOMPARE_NE(var2, uint(value));
+ QCOMPARE_NE(uint(value), var2);
+ }
+ if (canLosslesslyConvert(short{})) {
+ QCOMPARE_EQ(var, short(value));
+ QCOMPARE_EQ(short(value), var);
+ QCOMPARE_NE(var2, short(value));
+ QCOMPARE_NE(short(value), var2);
+ }
+ if (canLosslesslyConvert(ushort{})) {
+ QCOMPARE_EQ(var, ushort(value));
+ QCOMPARE_EQ(ushort(value), var);
+ QCOMPARE_NE(var2, ushort(value));
+ QCOMPARE_NE(ushort(value), var2);
+ }
+ if (canLosslesslyConvert(char{})) {
+ QCOMPARE_EQ(var, char(value));
+ QCOMPARE_EQ(char(value), var);
+ QCOMPARE_NE(var2, char(value));
+ QCOMPARE_NE(char(value), var2);
+ }
+ if (canLosslesslyConvert(uchar{})) {
+ QCOMPARE_EQ(var, uchar(value));
+ QCOMPARE_EQ(uchar(value), var);
+ QCOMPARE_NE(var2, uchar(value));
+ QCOMPARE_NE(uchar(value), var2);
+ }
+ if (canLosslesslyConvert(qint8{})) {
+ QCOMPARE_EQ(var, qint8(value));
+ QCOMPARE_EQ(qint8(value), var);
+ QCOMPARE_NE(var2, qint8(value));
+ QCOMPARE_NE(qint8(value), var2);
+ }
+
+ // string compares too (of the values in decimal)
+ QCOMPARE_EQ(var, QString::number(qToUnderlying(value)));
+ QCOMPARE_EQ(QString::number(qToUnderlying(value)), var);
+ QCOMPARE_NE(var, QString::number(qToUnderlying(value2)));
+ QCOMPARE_NE(QString::number(qToUnderlying(value2)), var);
+}
+
+void tst_QVariant::enums_data()
+{
+ QTest::addColumn<QFunctionPointer>("testFunction");
+
+#define ADD(V) QTest::newRow(#V) << &testVariantEnum<V>
+ ADD(EnumTest_Enum0_value);
+ ADD(EnumTest_Enum0_negValue);
+ ADD(EnumTest_Enum1_value);
+ ADD(EnumTest_Enum1_bigValue);
+ ADD(EnumTest_Enum3::EnumTest_Enum3_value);
+ ADD(EnumTest_Enum3::EnumTest_Enum3_bigValue);
+ ADD(EnumTest_Enum4::EnumTest_Enum4_value);
+ ADD(EnumTest_Enum4::EnumTest_Enum4_bigValue);
+ ADD(EnumTest_Enum5::EnumTest_Enum5_value);
+ ADD(EnumTest_Enum6::EnumTest_Enum6_value);
+ ADD(EnumTest_Enum7::EnumTest_Enum7_value);
+ ADD(EnumTest_Enum8::EnumTest_Enum8_value);
+ ADD(EnumTest_Enum3::EnumTest_Enum3_value);
+#undef ADD
+}
+
+// ### C++20: this would be easier if QFlags were a structural type
+template <typename Enum, auto Value> static void testVariantMetaEnum()
+{
+ Enum value(Value);
+ QFETCH(QString, string);
QVariant var = QVariant::fromValue(value);
QVERIFY(var.canConvert<QString>());
QVERIFY(var.canConvert<QByteArray>());
- QCOMPARE(var.value<QString>(), QString::fromLatin1(string));
- QCOMPARE(var.value<QByteArray>(), QByteArray(string));
+ QCOMPARE(var.value<QString>(), string);
+ QCOMPARE(var.value<QByteArray>(), string.toLatin1());
- QVariant strVar = QString::fromLatin1(string);
+ QVariant strVar = string;
QVERIFY(strVar.canConvert<Enum>());
// unary + to silence gcc warning
if ((+static_cast<qint64>(value) > INT_MAX) || (+static_cast<qint64>(value) < INT_MIN)) {
QEXPECT_FAIL("", "QMetaEnum api uses 'int' as return type QTBUG-27451", Abort);
- *ok = true;
}
QCOMPARE(strVar.value<Enum>(), value);
- strVar = QByteArray(string);
+ strVar = string.toLatin1();
QVERIFY(strVar.canConvert<Enum>());
QCOMPARE(strVar.value<Enum>(), value);
- *ok = true;
}
-void tst_QVariant::metaEnums()
+void tst_QVariant::metaEnums_data()
{
- bool ok = false;
+ QTest::addColumn<QFunctionPointer>("testFunction");
+ QTest::addColumn<QString>("string");
+
#define METAENUMS_TEST(Value) \
- testVariantMeta(Value, &ok, #Value); QVERIFY(ok)
+ QTest::newRow(#Value) << &testVariantMetaEnum<decltype(Value), Value> << #Value;
METAENUMS_TEST(MetaEnumTest_Enum0_value);
METAENUMS_TEST(MetaEnumTest_Enum1_value);
@@ -4711,17 +5395,26 @@ void tst_QVariant::metaEnums()
METAENUMS_TEST(MetaEnumTest_Enum5_value);
METAENUMS_TEST(MetaEnumTest_Enum6_value);
METAENUMS_TEST(MetaEnumTest_Enum8_value);
+ { using namespace Qt; METAENUMS_TEST(RichText); }
+#undef METAENUMS_TEST
+
+ QTest::newRow("AlignBottom")
+ << &testVariantMetaEnum<Qt::Alignment, Qt::AlignBottom> << "AlignBottom";
+
+ constexpr auto AlignHCenterBottom = Qt::AlignmentFlag((Qt::AlignHCenter | Qt::AlignBottom).toInt());
+ QTest::newRow("AlignHCenter|AlignBottom")
+ << &testVariantMetaEnum<Qt::Alignment, AlignHCenterBottom> << "AlignHCenter|AlignBottom";
}
void tst_QVariant::nullConvert()
{
// null variant with no initialized value
- QVariant nullVar(QVariant::String);
+ QVariant nullVar {QMetaType::fromType<QString>()};
QVERIFY(nullVar.isValid());
QVERIFY(nullVar.isNull());
// We can not convert a variant with no value
- QVERIFY(!nullVar.convert(QVariant::Url));
- QCOMPARE(nullVar.type(), QVariant::Url);
+ QVERIFY(!nullVar.convert(QMetaType::fromType<QUrl>()));
+ QCOMPARE(nullVar.typeId(), QMetaType::QUrl);
QVERIFY(nullVar.isNull());
}
@@ -4857,36 +5550,81 @@ void tst_QVariant::shouldDeleteVariantDataWorksForAssociative()
void tst_QVariant::fromStdVariant()
{
+#define CHECK_EQUAL(lhs, rhs, type) do { \
+ QCOMPARE(lhs.typeId(), rhs.typeId()); \
+ if (lhs.isNull()) { \
+ QVERIFY(rhs.isNull()); \
+ } else { \
+ QVERIFY(!rhs.isNull()); \
+ QCOMPARE(get< type >(lhs), get< type >(rhs)); \
+ } \
+ } while (false)
+
{
typedef std::variant<int, bool> intorbool_t;
intorbool_t stdvar = 5;
QVariant qvar = QVariant::fromStdVariant(stdvar);
QVERIFY(!qvar.isNull());
- QCOMPARE(qvar.type(), QVariant::Int);
+ QCOMPARE(qvar.typeId(), QMetaType::Int);
QCOMPARE(qvar.value<int>(), std::get<int>(stdvar));
+ {
+ const auto qv2 = QVariant::fromStdVariant(std::move(stdvar));
+ CHECK_EQUAL(qv2, qvar, int);
+ }
+
stdvar = true;
qvar = QVariant::fromStdVariant(stdvar);
QVERIFY(!qvar.isNull());
- QCOMPARE(qvar.type(), QVariant::Bool);
+ QCOMPARE(qvar.typeId(), QMetaType::Bool);
QCOMPARE(qvar.value<bool>(), std::get<bool>(stdvar));
+ {
+ const auto qv2 = QVariant::fromStdVariant(std::move(stdvar));
+ CHECK_EQUAL(qv2, qvar, bool);
+ }
}
{
std::variant<std::monostate, int> stdvar;
QVariant qvar = QVariant::fromStdVariant(stdvar);
QVERIFY(!qvar.isValid());
+ {
+ const auto qv2 = QVariant::fromStdVariant(std::move(stdvar));
+ CHECK_EQUAL(qv2, qvar, int); // fake type, they're empty
+ }
stdvar = -4;
qvar = QVariant::fromStdVariant(stdvar);
QVERIFY(!qvar.isNull());
- QCOMPARE(qvar.type(), QVariant::Int);
+ QCOMPARE(qvar.typeId(), QMetaType::Int);
QCOMPARE(qvar.value<int>(), std::get<int>(stdvar));
+ {
+ const auto qv2 = QVariant::fromStdVariant(std::move(stdvar));
+ CHECK_EQUAL(qv2, qvar, int);
+ }
}
{
std::variant<int, bool, QChar> stdvar = QChar::fromLatin1(' ');
QVariant qvar = QVariant::fromStdVariant(stdvar);
QVERIFY(!qvar.isNull());
- QCOMPARE(qvar.type(), QVariant::Char);
+ QCOMPARE(qvar.typeId(), QMetaType::QChar);
QCOMPARE(qvar.value<QChar>(), std::get<QChar>(stdvar));
+ {
+ const auto qv2 = QVariant::fromStdVariant(std::move(stdvar));
+ CHECK_EQUAL(qv2, qvar, QChar);
+ }
+ }
+ // rvalue fromStdVariant() actually moves:
+ {
+ const auto foo = u"foo"_s;
+ std::variant<QString, QByteArray> stdvar = foo;
+ QVariant qvar = QVariant::fromStdVariant(std::move(stdvar));
+ const auto ps = get_if<QString>(&stdvar);
+ QVERIFY(ps);
+ QVERIFY(ps->isNull()); // QString was moved from
+ QVERIFY(!qvar.isNull());
+ QCOMPARE(qvar.typeId(), QMetaType::QString);
+ QCOMPARE(get<QString>(qvar), foo);
}
+
+#undef CHECK_EQUAL
}
void tst_QVariant::qt4UuidDataStream()
@@ -5038,6 +5776,48 @@ void tst_QVariant::mutableView()
QCOMPARE(extracted.text, nullptr);
}
+template<typename T>
+void tst_QVariant::canViewAndView_ReturnFalseAndDefault_WhenConvertingBetweenPointerAndValue_impl(
+ const QByteArray &typeName)
+{
+ T instance{};
+
+ // Value -> Pointer
+ QVariant value = QVariant::fromValue(instance);
+ QVERIFY2(!value.canView<T *>(), typeName);
+ QCOMPARE(value.view<T *>(), nullptr); // Expect default constructed pointer
+
+ // Pointer -> Value
+ QVariant pointer = QVariant::fromValue(&instance);
+ QVERIFY2(!pointer.canView<T>(), typeName);
+ QCOMPARE(pointer.view<T>(), T{}); // Expect default constructed. Note: Weak test since instance
+ // is default constructed, but we detect data corruption
+}
+
+void tst_QVariant::canViewAndView_ReturnFalseAndDefault_WhenConvertingBetweenPointerAndValue()
+{
+#define ADD_TEST_IMPL(typeName, typeNameId, realType) \
+ canViewAndView_ReturnFalseAndDefault_WhenConvertingBetweenPointerAndValue_impl<realType>( \
+ #typeName);
+
+ // Add tests for static primitive types
+ QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_TEST_IMPL)
+
+ // Add tests for static core types
+ QT_FOR_EACH_STATIC_CORE_CLASS(ADD_TEST_IMPL)
+#undef ADD_TEST_IMPL
+}
+
+struct MoveTester
+{
+ bool wasMoved = false;
+ MoveTester() = default;
+ MoveTester(const MoveTester &) {}; // non-trivial on purpose
+ MoveTester(MoveTester &&other) { other.wasMoved = true; }
+ MoveTester& operator=(const MoveTester &) = default;
+ MoveTester& operator=(MoveTester &&other) {other.wasMoved = true; return *this;}
+};
+
void tst_QVariant::moveOperations()
{
{
@@ -5059,6 +5839,30 @@ void tst_QVariant::moveOperations()
v = QVariant::fromValue(list);
v2 = std::move(v);
QVERIFY(v2.value<std::list<int>>() == list);
+
+ {
+ MoveTester tester;
+ QVariant::fromValue(tester);
+ QVERIFY(!tester.wasMoved);
+ QVariant::fromValue(std::move(tester));
+ QVERIFY(tester.wasMoved);
+ }
+ {
+ const MoveTester tester;
+ QVariant::fromValue(std::move(tester));
+ QVERIFY(!tester.wasMoved); // we don't want to move from const variables
+ }
+ {
+ QVariant var(std::in_place_type<MoveTester>);
+ const auto p = get_if<MoveTester>(&var);
+ QVERIFY(p);
+ auto &tester = *p;
+ QVERIFY(!tester.wasMoved);
+ [[maybe_unused]] auto copy = var.value<MoveTester>();
+ QVERIFY(!tester.wasMoved);
+ [[maybe_unused]] auto moved = std::move(var).value<MoveTester>();
+ QVERIFY(tester.wasMoved);
+ }
}
class NoMetaObject : public QObject {};
@@ -5107,5 +5911,423 @@ void tst_QVariant::equalsWithoutMetaObject()
QVERIFY(qobjectVariant != noMetaObjectVariant);
}
+struct NonDefaultConstructible
+{
+ NonDefaultConstructible(int i) :i(i) {}
+ int i;
+ friend bool operator==(NonDefaultConstructible l, NonDefaultConstructible r)
+ { return l.i == r.i; }
+};
+
+template <> char *QTest::toString<NonDefaultConstructible>(const NonDefaultConstructible &ndc)
+{
+ return qstrdup('{' + QByteArray::number(ndc.i) + '}');
+}
+
+struct Indestructible
+{
+ Indestructible() {}
+ Indestructible(const Indestructible &) {}
+ Indestructible &operator=(const Indestructible &) { return *this; }
+private:
+ ~Indestructible() {}
+};
+
+struct NotCopyable
+{
+ NotCopyable() = default;
+ NotCopyable(const NotCopyable&) = delete;
+ NotCopyable &operator=(const NotCopyable &) = delete;
+};
+
+void tst_QVariant::constructFromIncompatibleMetaType_data()
+{
+ QTest::addColumn<QMetaType>("type");
+ auto addRow = [](QMetaType meta) {
+ QTest::newRow(meta.name()) << meta;
+ };
+ addRow(QMetaType::fromType<void>());
+ addRow(QMetaType::fromType<NonDefaultConstructible>());
+ addRow(QMetaType::fromType<QObject>());
+ addRow(QMetaType::fromType<Indestructible>());
+ addRow(QMetaType::fromType<NotCopyable>());
+}
+
+void tst_QVariant::constructFromIncompatibleMetaType()
+{
+ QFETCH(QMetaType, type);
+ const auto anticipate = [type]() {
+ // In that case, we run into a different condition (size == 0), and do not warn
+ if (type == QMetaType::fromType<NonDefaultConstructible>()) {
+ QTest::ignoreMessage(QtWarningMsg,
+ "QVariant: Cannot create type 'NonDefaultConstructible' without a "
+ "default constructor");
+ } else if (type != QMetaType::fromType<void>()) {
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "QVariant: Provided metatype for '" + QByteArray(type.name()) +
+ "' does not support destruction and copy construction");
+ }
+ };
+ anticipate();
+ QVariant var(type, nullptr);
+ QVERIFY(!var.isValid());
+ QVERIFY(!var.metaType().isValid());
+
+ anticipate();
+ QVariant regular(1.0);
+ QVERIFY(!var.canView(type));
+ QVERIFY(!var.canConvert(type));
+ QVERIFY(!QVariant(regular).convert(type));
+}
+
+void tst_QVariant::constructFromQtLT65MetaType()
+{
+ auto qsizeIface = QtPrivate::qMetaTypeInterfaceForType<QSize>();
+
+ QtPrivate::QMetaTypeInterface qsize64Iface = {
+ /*revision*/0,
+ 8,
+ 8,
+ QMetaType::NeedsConstruction | QMetaType::NeedsDestruction,
+ 0,
+ qsizeIface->metaObjectFn,
+ "FakeQSize",
+ qsizeIface->defaultCtr,
+ qsizeIface->copyCtr,
+ qsizeIface->moveCtr,
+ /*dtor =*/ nullptr,
+ qsizeIface->equals,
+ qsizeIface->lessThan,
+ qsizeIface->debugStream,
+ qsizeIface->dataStreamOut,
+ qsizeIface->dataStreamIn,
+ /*legacyregop =*/ nullptr
+ };
+ QVariant var{ QMetaType(&qsize64Iface) };
+ QVERIFY(var.isValid());
+}
+
+void tst_QVariant::copyNonDefaultConstructible()
+{
+ NonDefaultConstructible ndc(42);
+ QVariant var = QVariant::fromValue(ndc);
+ QVERIFY(var.isDetached());
+ QCOMPARE(var.metaType(), QMetaType::fromType<NonDefaultConstructible>());
+ QVERIFY(var.constData() != &ndc);
+
+ // qvariant_cast<T> and QVariant::value<T> don't compile
+ QCOMPARE(get<NonDefaultConstructible>(std::as_const(var)), ndc);
+
+ QVariant var2 = var;
+ var2.detach(); // force another copy
+ QVERIFY(var2.isDetached());
+ QVERIFY(var2.constData() != var.constData());
+ QCOMPARE(get<NonDefaultConstructible>(std::as_const(var2)),
+ get<NonDefaultConstructible>(std::as_const(var)));
+ QCOMPARE(var2, var);
+}
+
+void tst_QVariant::inplaceConstruct()
+{
+ {
+ NonDefaultConstructible ndc(42);
+ QVariant var(std::in_place_type<NonDefaultConstructible>, 42);
+ QVERIFY(get_if<NonDefaultConstructible>(&var));
+ QCOMPARE(get<NonDefaultConstructible>(var), ndc);
+ }
+
+ {
+ std::vector<int> vec {1, 2, 3, 4};
+ QVariant var(std::in_place_type<std::vector<int>>, {1, 2, 3, 4});
+ QVERIFY(get_if<std::vector<int>>(&var));
+ QCOMPARE(get<std::vector<int>>(var), vec);
+ }
+}
+
+struct LargerThanInternalQVariantStorage {
+ char data[6 * sizeof(void *)];
+};
+
+struct alignas(256) LargerThanInternalQVariantStorageOveraligned {
+ char data[6 * sizeof(void *)];
+};
+
+struct alignas(128) SmallerAlignmentEvenLargerSize {
+ char data[17 * sizeof(void *)];
+};
+
+void tst_QVariant::emplace()
+{
+ {
+ // can emplace non default constructible + can emplace on null variant
+ NonDefaultConstructible ndc(42);
+ QVariant var;
+ var.emplace<NonDefaultConstructible>(42);
+ QVERIFY(get_if<NonDefaultConstructible>(&var));
+ QCOMPARE(get<NonDefaultConstructible>(var), ndc);
+ }
+ {
+ // can emplace using ctor taking initializer_list
+ QVariant var;
+ var.emplace<std::vector<int>>({0, 1, 2, 3, 4});
+ auto vecPtr = get_if<std::vector<int>>(&var);
+ QVERIFY(vecPtr);
+ QCOMPARE(vecPtr->size(), 5U);
+ for (int i = 0; i < 5; ++i)
+ QCOMPARE(vecPtr->at(size_t(i)), i);
+ }
+ // prequisites for the test
+ QCOMPARE_LE(sizeof(std::vector<int>), sizeof(std::string));
+ QCOMPARE(alignof(std::vector<int>), alignof(std::string));
+ {
+ // emplace can reuse storage
+ auto var = QVariant::fromValue(std::string{});
+ QVERIFY(var.data_ptr().is_shared);
+ auto data = var.constData();
+ std::vector<int> &vec = var.emplace<std::vector<int>>(3, 42);
+ /* alignment is the same, so the pointer is exactly the same;
+ no offset change */
+ auto expected = std::vector<int>{42, 42, 42};
+ QCOMPARE(get_if<std::vector<int>>(&var), &vec);
+ QCOMPARE(get<std::vector<int>>(var), expected);
+ QCOMPARE(var.constData(), data);
+ }
+ {
+ // emplace can't reuse storage if the variant is shared
+ auto var = QVariant::fromValue(std::string{});
+ [[maybe_unused]] QVariant causesSharing = var;
+ QVERIFY(var.data_ptr().is_shared);
+ auto data = var.constData();
+ var.emplace<std::vector<int>>(3, 42);
+ auto expected = std::vector<int>{42, 42, 42};
+ QVERIFY(get_if<std::vector<int>>(&var));
+ QCOMPARE(get<std::vector<int>>(var), expected);
+ QCOMPARE_NE(var.constData(), data);
+ }
+ {
+ // emplace puts element into the correct place - non-shared
+ QVERIFY(QVariant::Private::canUseInternalSpace(QMetaType::fromType<QString>().iface()));
+ QVariant var;
+ var.emplace<QString>(QChar('x'));
+ QVERIFY(!var.data_ptr().is_shared);
+ }
+ {
+ // emplace puts element into the correct place - shared
+ QVERIFY(!QVariant::Private::canUseInternalSpace(QMetaType::fromType<std::string>().iface()));
+ QVariant var;
+ var.emplace<std::string>(42, 'x');
+ QVERIFY(var.data_ptr().is_shared);
+ }
+ {
+ // emplace does not reuse the storage if alignment is too large
+ auto iface = QMetaType::fromType<LargerThanInternalQVariantStorage>().iface();
+ QVERIFY(!QVariant::Private::canUseInternalSpace(iface));
+ auto var = QVariant::fromValue(LargerThanInternalQVariantStorage{});
+ auto data = var.constData();
+ var.emplace<LargerThanInternalQVariantStorageOveraligned>();
+ QCOMPARE_NE(var.constData(), data);
+ }
+ {
+ // emplace does reuse the storage if new alignment and size are together small enough
+ auto iface = QMetaType::fromType<LargerThanInternalQVariantStorageOveraligned>().iface();
+ QVERIFY(!QVariant::Private::canUseInternalSpace(iface));
+ auto var = QVariant::fromValue(LargerThanInternalQVariantStorageOveraligned{});
+ auto data = var.constData();
+ var.emplace<SmallerAlignmentEvenLargerSize>();
+ // no exact match below - the alignment is after all different
+ QCOMPARE_LE(quintptr(var.constData()), quintptr(data));
+ QCOMPARE_LE(quintptr(var.constData()),
+ quintptr(data) + sizeof(LargerThanInternalQVariantStorageOveraligned));
+ }
+}
+
+void tst_QVariant::getIf_NonDefaultConstructible()
+{
+ getIf_impl(NonDefaultConstructible{42});
+}
+
+void tst_QVariant::getIfSpecial()
+{
+ QVariant v{QString{}}; // used to be a null QVariant in Qt 5
+ QCOMPARE_NE(get_if<QString>(&v), nullptr); // not anymore...
+}
+
+void tst_QVariant::get_NonDefaultConstructible()
+{
+ get_impl(NonDefaultConstructible{42});
+}
+
+template <typename T>
+T mutate(const T &t) { return t + t; }
+template <>
+NonDefaultConstructible mutate(const NonDefaultConstructible &t)
+{
+ return NonDefaultConstructible{t.i + t.i};
+}
+
+template <typename T>
+QVariant make_null_QVariant_of_type()
+{
+ return QVariant(QMetaType::fromType<T>());
+}
+
+template <typename T>
+void tst_QVariant::getIf_impl(T t) const
+{
+ QVariant v = QVariant::fromValue(t);
+
+ QVariant null;
+ QVERIFY(null.isNull());
+
+ [[maybe_unused]]
+ QVariant nulT;
+ if constexpr (std::is_default_constructible_v<T>) {
+ // typed null QVariants don't work with non-default-constuctable types
+ nulT = make_null_QVariant_of_type<T>();
+ QVERIFY(nulT.isNull());
+ }
+
+ QVariant date = QDate(2023, 3, 3);
+ static_assert(!std::is_same_v<T, QDate>);
+
+ // for behavioral comparison:
+ StdVariant stdn = {}, stdv = t;
+
+ // returns nullptr on type mismatch:
+ {
+ // const
+ QCOMPARE_EQ(get_if<T>(&std::as_const(stdn)), nullptr);
+ QCOMPARE_EQ(get_if<T>(&std::as_const(date)), nullptr);
+ // mutable
+ QCOMPARE_EQ(get_if<T>(&stdn), nullptr);
+ QCOMPARE_EQ(get_if<T>(&date), nullptr);
+ }
+
+ // returns nullptr on null variant (QVariant only):
+ {
+ QCOMPARE_EQ(get_if<T>(&std::as_const(null)), nullptr);
+ QCOMPARE_EQ(get_if<T>(&null), nullptr);
+ if constexpr (std::is_default_constructible_v<T>) {
+ // const access return nullptr
+ QCOMPARE_EQ(get_if<T>(&std::as_const(nulT)), nullptr);
+ // but mutable access makes typed null QVariants non-null (like data())
+ QCOMPARE_NE(get_if<T>(&nulT), nullptr);
+ QVERIFY(!nulT.isNull());
+ nulT = make_null_QVariant_of_type<T>(); // reset to null state
+ }
+ }
+
+ // const access:
+ {
+ auto ps = get_if<T>(&std::as_const(stdv));
+ static_assert(std::is_same_v<decltype(ps), const T*>);
+ QCOMPARE_NE(ps, nullptr);
+ QCOMPARE_EQ(*ps, t);
+
+ auto pv = get_if<T>(&std::as_const(v));
+ static_assert(std::is_same_v<decltype(ps), const T*>);
+ QCOMPARE_NE(pv, nullptr);
+ QCOMPARE_EQ(*pv, t);
+ }
+
+ // mutable access:
+ {
+ T t2 = mutate(t);
+
+ auto ps = get_if<T>(&stdv);
+ static_assert(std::is_same_v<decltype(ps), T*>);
+ QCOMPARE_NE(ps, nullptr);
+ QCOMPARE_EQ(*ps, t);
+ *ps = t2;
+ auto ps2 = get_if<T>(&stdv);
+ QCOMPARE_NE(ps2, nullptr);
+ QCOMPARE_EQ(*ps2, t2);
+
+ auto pv = get_if<T>(&v);
+ static_assert(std::is_same_v<decltype(pv), T*>);
+ QCOMPARE_NE(pv, nullptr);
+ QCOMPARE_EQ(*pv, t);
+ *pv = t2;
+ auto pv2 = get_if<T>(&v);
+ QCOMPARE_NE(pv2, nullptr);
+ QCOMPARE_EQ(*pv2, t2);
+
+ // typed null QVariants become non-null (data() behavior):
+ if constexpr (std::is_default_constructible_v<T>) {
+ QVERIFY(nulT.isNull());
+ auto pn = get_if<T>(&nulT);
+ QVERIFY(!nulT.isNull());
+ static_assert(std::is_same_v<decltype(pn), T*>);
+ QCOMPARE_NE(pn, nullptr);
+ QCOMPARE_EQ(*pn, T{});
+ *pn = t2;
+ auto pn2 = get_if<T>(&nulT);
+ QCOMPARE_NE(pn2, nullptr);
+ QCOMPARE_EQ(*pn2, t2);
+ }
+ }
+}
+
+template <typename T>
+void tst_QVariant::get_impl(T t) const
+{
+ QVariant v = QVariant::fromValue(t);
+
+ // for behavioral comparison:
+ StdVariant stdv = t;
+
+ #define FOR_EACH_CVREF(op) \
+ op(/*unadorned*/, &&) \
+ op(&, &) \
+ op(&&, &&) \
+ op(const, const &&) \
+ op(const &, const &) \
+ op(const &&, const &&) \
+ /* end */
+
+
+ #define CHECK_RETURN_TYPE_OF(Variant, cvref_in, cvref_out) \
+ static_assert(std::is_same_v< \
+ decltype(get<T>(std::declval<Variant cvref_in >())), \
+ T cvref_out \
+ >); \
+ /* end */
+ #define CHECK_RETURN_TYPE(cvref_in, cvref_out) \
+ CHECK_RETURN_TYPE_OF(StdVariant, cvref_in, cvref_out) \
+ CHECK_RETURN_TYPE_OF(QVariant, cvref_in, cvref_out) \
+ /* end */
+ FOR_EACH_CVREF(CHECK_RETURN_TYPE)
+ #undef CHECK_RETURN_TYPE
+
+ #undef FOR_EACH_CVREF
+
+ // const access:
+ {
+ auto &&rs = get<T>(std::as_const(stdv));
+ QCOMPARE_EQ(rs, t);
+
+ auto &&rv = get<T>(std::as_const(v));
+ QCOMPARE_EQ(rv, t);
+ }
+
+ // mutable access:
+ {
+ T t2 = mutate(t);
+
+ auto &&rs = get<T>(stdv);
+ QCOMPARE_EQ(rs, t);
+ rs = t2;
+ auto &&rs2 = get<T>(stdv);
+ QCOMPARE_EQ(rs2, t2);
+
+ auto &&rv = get<T>(v);
+ QCOMPARE_EQ(rv, t);
+ rv = t2;
+ auto &&rv2 = get<T>(v);
+ QCOMPARE_EQ(rv2, t2);
+ }
+}
+
QTEST_MAIN(tst_QVariant)
#include "tst_qvariant.moc"
diff --git a/tests/auto/corelib/kernel/qwineventnotifier/CMakeLists.txt b/tests/auto/corelib/kernel/qwineventnotifier/CMakeLists.txt
index b9bcb36771..81b2c9a58c 100644
--- a/tests/auto/corelib/kernel/qwineventnotifier/CMakeLists.txt
+++ b/tests/auto/corelib/kernel/qwineventnotifier/CMakeLists.txt
@@ -1,12 +1,19 @@
-# Generated from qwineventnotifier.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qwineventnotifier Test:
#####################################################################
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qwineventnotifier LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
qt_internal_add_test(tst_qwineventnotifier
SOURCES
tst_qwineventnotifier.cpp
- PUBLIC_LIBRARIES
+ LIBRARIES
Qt::CorePrivate
)
diff --git a/tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp b/tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp
index 74122b6c83..bf08f85fb5 100644
--- a/tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp
+++ b/tests/auto/corelib/kernel/qwineventnotifier/tst_qwineventnotifier.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QTestEventLoop>
diff --git a/tests/auto/corelib/kernel/qwinregistrykey/.gitignore b/tests/auto/corelib/kernel/qwinregistrykey/.gitignore
new file mode 100644
index 0000000000..3d888e2868
--- /dev/null
+++ b/tests/auto/corelib/kernel/qwinregistrykey/.gitignore
@@ -0,0 +1 @@
+tst_qwinregistrykey
diff --git a/tests/auto/corelib/kernel/qwinregistrykey/CMakeLists.txt b/tests/auto/corelib/kernel/qwinregistrykey/CMakeLists.txt
new file mode 100644
index 0000000000..aeda6f0033
--- /dev/null
+++ b/tests/auto/corelib/kernel/qwinregistrykey/CMakeLists.txt
@@ -0,0 +1,15 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qwinregistrykey LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qwinregistrykey
+ SOURCES
+ tst_qwinregistrykey.cpp
+ LIBRARIES
+ Qt::CorePrivate
+)
diff --git a/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp
new file mode 100644
index 0000000000..a5dca8a3d5
--- /dev/null
+++ b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp
@@ -0,0 +1,242 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
+#include <QObject>
+#include <QPair>
+#include <QScopeGuard>
+#include <private/qwinregistry_p.h>
+#include <qt_windows.h>
+
+using namespace Qt::StringLiterals;
+
+static constexpr const wchar_t TEST_KEY[] = LR"(SOFTWARE\tst_qwinregistrykey)";
+
+static const QPair<QStringView, QString> TEST_STRING = qMakePair(u"string", u"string"_s);
+static const QPair<QStringView, QString> TEST_STRING_NULL = qMakePair(u"string_null", QString());
+static const QPair<QStringView, QStringList> TEST_STRINGLIST = qMakePair(u"stringlist", QStringList{ u"element1"_s, u"element2"_s, u"element3"_s });
+static const QPair<QStringView, QStringList> TEST_STRINGLIST_NULL = qMakePair(u"stringlist_null", QStringList());
+static const QPair<QStringView, quint32> TEST_DWORD = qMakePair(u"dword", 123);
+static const QPair<QStringView, quint64> TEST_QWORD = qMakePair(u"qword", 456);
+static const QPair<QStringView, QByteArray> TEST_BINARY = qMakePair(u"binary", "binary\0"_ba);
+static const QPair<QStringView, QVariant> TEST_NOT_EXIST = qMakePair(u"not_exist", QVariant());
+static const QPair<QStringView, QVariant> TEST_DEFAULT = qMakePair(u"", u"default"_s);
+
+[[nodiscard]] static inline bool write(const HKEY key, const QStringView name, const QVariant &value)
+{
+ DWORD type = REG_NONE;
+ QByteArray buf = {};
+
+ switch (value.typeId()) {
+ case QMetaType::QStringList: {
+ // If none of the elements contains '\0', we can use REG_MULTI_SZ, the
+ // native registry string list type. Otherwise we use REG_BINARY.
+ type = REG_MULTI_SZ;
+ const QStringList list = value.toStringList();
+ for (auto it = list.constBegin(); it != list.constEnd(); ++it) {
+ if ((*it).length() == 0 || it->contains(QChar::Null)) {
+ type = REG_BINARY;
+ break;
+ }
+ }
+
+ if (type == REG_BINARY) {
+ const QString str = value.toString();
+ buf = QByteArray(reinterpret_cast<const char *>(str.data()), str.length() * 2);
+ } else {
+ for (auto it = list.constBegin(); it != list.constEnd(); ++it) {
+ const QString &str = *it;
+ buf += QByteArray(reinterpret_cast<const char *>(str.utf16()), (str.length() + 1) * 2);
+ }
+ // According to Microsoft Docs, REG_MULTI_SZ requires double '\0'.
+ buf.append((char)0);
+ buf.append((char)0);
+ }
+ break;
+ }
+
+ case QMetaType::Int:
+ case QMetaType::UInt: {
+ type = REG_DWORD;
+ quint32 num = value.toUInt();
+ buf = QByteArray(reinterpret_cast<const char *>(&num), sizeof(quint32));
+ break;
+ }
+
+ case QMetaType::LongLong:
+ case QMetaType::ULongLong: {
+ type = REG_QWORD;
+ quint64 num = value.toULongLong();
+ buf = QByteArray(reinterpret_cast<const char *>(&num), sizeof(quint64));
+ break;
+ }
+
+ case QMetaType::QByteArray:
+ default: {
+ // If the string does not contain '\0', we can use REG_SZ, the native registry
+ // string type. Otherwise we use REG_BINARY.
+ const QString str = value.toString();
+ type = str.contains(QChar::Null) ? REG_BINARY : REG_SZ;
+ int length = str.length();
+ if (type == REG_SZ)
+ ++length;
+ buf = QByteArray(reinterpret_cast<const char *>(str.utf16()), sizeof(wchar_t) * length);
+ break;
+ }
+ }
+
+ const LONG ret = RegSetValueExW(key, reinterpret_cast<const wchar_t *>(name.utf16()),
+ 0, type, reinterpret_cast<LPBYTE>(buf.data()), buf.size());
+ return ret == ERROR_SUCCESS;
+}
+
+class tst_qwinregistrykey : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void initTestCase();
+ void cleanupTestCase();
+ void qwinregistrykey();
+
+private:
+ bool m_available = false;
+};
+
+void tst_qwinregistrykey::initTestCase()
+{
+ HKEY key = nullptr;
+ const LONG ret = RegCreateKeyExW(HKEY_CURRENT_USER, TEST_KEY, 0, nullptr, 0,
+ KEY_READ | KEY_WRITE, nullptr, &key, nullptr);
+ if (ret != ERROR_SUCCESS)
+ return;
+ const auto cleanup = qScopeGuard([key](){ RegCloseKey(key); });
+ if (!write(key, TEST_STRING.first, TEST_STRING.second))
+ return;
+ if (!write(key, TEST_STRING_NULL.first, TEST_STRING_NULL.second))
+ return;
+ if (!write(key, TEST_STRINGLIST.first, TEST_STRINGLIST.second))
+ return;
+ if (!write(key, TEST_STRINGLIST_NULL.first, TEST_STRINGLIST_NULL.second))
+ return;
+ if (!write(key, TEST_DWORD.first, TEST_DWORD.second))
+ return;
+ if (!write(key, TEST_QWORD.first, TEST_QWORD.second))
+ return;
+ if (!write(key, TEST_BINARY.first, TEST_BINARY.second))
+ return;
+ if (!write(key, TEST_DEFAULT.first, TEST_DEFAULT.second))
+ return;
+ m_available = true;
+}
+
+void tst_qwinregistrykey::cleanupTestCase()
+{
+ HKEY key = nullptr;
+ const LONG ret = RegOpenKeyExW(HKEY_CURRENT_USER, TEST_KEY, 0, KEY_READ | KEY_WRITE, &key);
+ if (ret != ERROR_SUCCESS)
+ return;
+ #define C_STR(View) reinterpret_cast<const wchar_t *>(View.utf16())
+ RegDeleteValueW(key, C_STR(TEST_STRING.first));
+ RegDeleteValueW(key, C_STR(TEST_STRING_NULL.first));
+ RegDeleteValueW(key, C_STR(TEST_STRINGLIST.first));
+ RegDeleteValueW(key, C_STR(TEST_STRINGLIST_NULL.first));
+ RegDeleteValueW(key, C_STR(TEST_DWORD.first));
+ RegDeleteValueW(key, C_STR(TEST_QWORD.first));
+ RegDeleteValueW(key, C_STR(TEST_BINARY.first));
+ RegDeleteValueW(key, C_STR(TEST_DEFAULT.first));
+ #undef C_STR
+ RegDeleteKeyW(HKEY_CURRENT_USER, TEST_KEY);
+ RegCloseKey(key);
+}
+
+void tst_qwinregistrykey::qwinregistrykey()
+{
+ if (!m_available)
+ QSKIP("The test data is not ready.");
+
+ QWinRegistryKey registry(HKEY_CURRENT_USER, TEST_KEY);
+
+ QVERIFY(registry.isValid());
+
+ QVERIFY(registry.handle() != nullptr);
+
+ {
+ const auto value = registry.value<QString>(TEST_STRING.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QString()), TEST_STRING.second);
+ }
+
+ {
+ const auto value = registry.value<QString>(TEST_STRING_NULL.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QString()), TEST_STRING_NULL.second);
+
+ }
+
+ {
+ const auto value = registry.value<QStringList>(TEST_STRINGLIST.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QStringList()), TEST_STRINGLIST.second);
+ }
+
+ {
+ const auto value = registry.value<QStringList>(TEST_STRINGLIST_NULL.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QStringList()), TEST_STRINGLIST_NULL.second);
+ }
+
+ {
+ const auto value = registry.value<quint32>(TEST_DWORD.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(0), TEST_DWORD.second);
+ }
+
+ {
+ const auto value = registry.value<quint64>(TEST_QWORD.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(0), TEST_QWORD.second);
+ }
+
+ {
+ const auto value = registry.value<QByteArray>(TEST_BINARY.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QByteArray()), TEST_BINARY.second);
+ }
+
+ {
+ const auto value = registry.value<QVariant>(TEST_NOT_EXIST.first);
+ QVERIFY(!value.has_value());
+ QCOMPARE(value.value_or(QVariant()), QVariant());
+ }
+
+ {
+ const auto value = registry.value<QString>(TEST_DEFAULT.first);
+ QVERIFY(value.has_value());
+ QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second);
+ }
+
+ {
+ const QString value = registry.stringValue(TEST_STRING.first);
+ QVERIFY(!value.isEmpty());
+ QCOMPARE(value, TEST_STRING.second);
+ }
+
+ QVERIFY(registry.stringValue(TEST_NOT_EXIST.first).isEmpty());
+
+ {
+ const auto value = registry.dwordValue(TEST_DWORD.first);
+ QVERIFY(value.second);
+ QCOMPARE(value.first, TEST_DWORD.second);
+ }
+
+ {
+ const auto value = registry.dwordValue(TEST_NOT_EXIST.first);
+ QVERIFY(!value.second);
+ QCOMPARE(value.first, DWORD(0));
+ }
+}
+
+QTEST_MAIN(tst_qwinregistrykey)
+
+#include "tst_qwinregistrykey.moc"