diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-08-27 13:42:44 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-08-27 17:09:48 +0200 |
commit | e1614a41c8a837617a9f87d8d9106120154fbd00 (patch) | |
tree | cbaf0360f49109e302b03888ecee37f5d7d63cd2 /tests/auto/qml/qqmlengine | |
parent | 98af4ab983b9b33c6f5cdb71f6f4f4dd7419aa30 (diff) |
Stabilize tst_qqmlengine.cpp
It's conceivable that the memory used for the first CppSingleton is
re-used for the second one, resulting in the same pointer. Use a
different criterion to discern them.
Change-Id: I87c3435121e92806207a17747565e68cfdafd24c
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlengine')
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index 87654deb44..18ae6085d6 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -115,7 +115,10 @@ signals: class CppSingleton : public QObject { Q_OBJECT public: - CppSingleton() {} + static uint instantiations; + uint id = 0; + + CppSingleton() : id(++instantiations) {} static QObject *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) { @@ -125,6 +128,8 @@ public: } }; +uint CppSingleton::instantiations = 0; + class JsSingleton : public QObject { Q_OBJECT public: @@ -574,6 +579,9 @@ void tst_qqmlengine::clearSingletons() QCOMPARE(engine.singletonInstance<ObjectCaller *>(deprecatedCppInstance), &objectCaller2); const CppSingleton *oldCppSingleton = engine.singletonInstance<CppSingleton *>(cppFactory); QVERIFY(oldCppSingleton != nullptr); + const uint oldCppSingletonId = oldCppSingleton->id; + QVERIFY(oldCppSingletonId > 0); + QCOMPARE(CppSingleton::instantiations, oldCppSingletonId); QCOMPARE(engine.singletonInstance<QJSValue>(jsValue).toUInt(), 13u); const JsSingleton *oldJsSingleton = engine.singletonInstance<JsSingleton *>(jsObject); QVERIFY(oldJsSingleton != nullptr); @@ -601,6 +609,7 @@ void tst_qqmlengine::clearSingletons() QCOMPARE(qvariant_cast<QObject *>(singletonUser->property("e")), oldQmlSingleton); engine.clearSingletons(); + QCOMPARE(CppSingleton::instantiations, oldCppSingletonId); QCOMPARE(engine.singletonInstance<ObjectCaller *>(cppInstance), &objectCaller1); @@ -618,9 +627,11 @@ void tst_qqmlengine::clearSingletons() "available because the callback function returns a null pointer."); QCOMPARE(engine.singletonInstance<ObjectCaller *>(deprecatedCppInstance), nullptr); + QCOMPARE(CppSingleton::instantiations, oldCppSingletonId); const CppSingleton *newCppSingleton = engine.singletonInstance<CppSingleton *>(cppFactory); - QVERIFY(newCppSingleton != nullptr); - QVERIFY(newCppSingleton != oldCppSingleton); + QVERIFY(newCppSingleton != nullptr); // The pointer may be the same as the old one + QCOMPARE(CppSingleton::instantiations, oldCppSingletonId + 1); + QCOMPARE(newCppSingleton->id, CppSingleton::instantiations); QCOMPARE(engine.singletonInstance<QJSValue>(jsValue).toUInt(), 13u); const JsSingleton *newJsSingleton = engine.singletonInstance<JsSingleton *>(jsObject); QVERIFY(newJsSingleton != nullptr); |