diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2012-06-09 22:57:35 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-06-20 15:45:07 +0200 |
commit | 668efc29fd85bbae2395a4eca8d0ad71ad6ee3d1 (patch) | |
tree | ffccf50b396428156cafb3c6a51ddb87d2359f28 /tests/auto | |
parent | 534bcc96676ac02fc327f9e43d3785c7424b0750 (diff) |
Add some internal API for extracting a QSharedPointer<T> from QVariant.
The T must be derived from QObject, or it will fail to compile.
This will allow scripting or other 'wrapping' and runtime environments like
QtDeclarative to handle QSharedPointers to types derived from QObject
properly. A QSharedPointer<T> can be inserted into a QVariant, and
where T derives from QObject, a QSharedPointer<QObject> can be
extracted from the QVariant, and its properties are then accessible.
Change-Id: I68d6d89aceceb019267bd7301baa2047f9c09b90
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp | 37 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 71 |
2 files changed, 108 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index 3540df17ac..fd20a1cf5d 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -63,6 +63,8 @@ private slots: void disconnect(); void castDuringDestruction(); void threadSafety(); + + void qvariantCast(); }; void tst_QPointer::constructors() @@ -348,6 +350,41 @@ void tst_QPointer::threadSafety() owner.wait(); } +void tst_QPointer::qvariantCast() +{ + QPointer<QFile> tracking = new QFile; + tracking->setObjectName("A test name"); + QVariant v = QVariant::fromValue(tracking); + + { + QPointer<QObject> other = qPointerFromVariant<QObject>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QPointer<QIODevice> other = qPointerFromVariant<QIODevice>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QPointer<QFile> other = qPointerFromVariant<QFile>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QPointer<QThread> other = qPointerFromVariant<QThread>(v); + QVERIFY(!other); + } + { + QPointer<QFile> toBeDeleted = new QFile; + QVariant deletedVariant = QVariant::fromValue(toBeDeleted); + delete toBeDeleted; + QPointer<QObject> deleted = qPointerFromVariant<QObject>(deletedVariant); + QVERIFY(!deleted); + } + + // Intentionally does not compile. +// QPointer<int> sop = qPointerFromVariant<int>(v); +} + + QTEST_MAIN(tst_QPointer) #include "tst_qpointer.moc" diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 70caad856b..6896b328d0 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -105,6 +105,8 @@ private slots: void invalidConstructs_data(); void invalidConstructs(); + void qvariantCast(); + public slots: void cleanup() { safetyCheck(); } @@ -1853,6 +1855,75 @@ void tst_QSharedPointer::invalidConstructs() } } +void tst_QSharedPointer::qvariantCast() +{ + QSharedPointer<QFile> sp = QSharedPointer<QFile>::create(); + sp->setObjectName("A test name"); + QVariant v = QVariant::fromValue(sp); + + { + QSharedPointer<QObject> other = qSharedPointerFromVariant<QObject>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QIODevice> other = qSharedPointerFromVariant<QIODevice>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QFile> other = qSharedPointerFromVariant<QFile>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QThread> other = qSharedPointerFromVariant<QThread>(v); + QVERIFY(!other); + } + // Intentionally does not compile. +// QSharedPointer<int> sop = qSharedPointerFromVariant<int>(v); + + v = QVariant::fromValue(sp.toWeakRef()); + + { + QWeakPointer<QObject> other = qWeakPointerFromVariant<QObject>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QIODevice> other = qWeakPointerFromVariant<QIODevice>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QFile> other = qWeakPointerFromVariant<QFile>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QThread> other = qWeakPointerFromVariant<QThread>(v); + QVERIFY(!other); + } + + // Intentionally does not compile. +// QWeakPointer<int> sop = qWeakPointerFromVariant<int>(v); + + QWeakPointer<QFile> tracking = new QFile; + tracking.data()->setObjectName("A test name"); + v = QVariant::fromValue(tracking); + + { + QWeakPointer<QObject> other = qWeakPointerFromVariant<QObject>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QIODevice> other = qWeakPointerFromVariant<QIODevice>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QFile> other = qWeakPointerFromVariant<QFile>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QThread> other = qWeakPointerFromVariant<QThread>(v); + QVERIFY(!other); + } +} + namespace ReentrancyWhileDestructing { struct IB { |