From 8fa0e4d1d202a3d1302469e6827c622423328383 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 10 Sep 2013 12:55:05 -0700 Subject: Add a simple test for QVariant carrying a QSharedPointer Change-Id: Ibe17355c7ed8d4af7fda6261d72cfd64779471a7 Reviewed-by: Stephen Kelly --- .../auto/corelib/kernel/qvariant/tst_qvariant.cpp | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp') diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index a532407ad4..502e2504c9 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -155,6 +155,7 @@ private slots: void qvariant_cast_QObject(); void qvariant_cast_QObject_derived(); void qvariant_cast_QObject_wrapper(); + void qvariant_cast_QSharedPointerQObject(); void toLocale(); @@ -2324,6 +2325,41 @@ void tst_QVariant::qvariant_cast_QObject_wrapper() qRegisterMetaType >("MyNS::SmartPointer"); } +void tst_QVariant::qvariant_cast_QSharedPointerQObject() +{ + // ensure no problems between this form and the auto-registering in QVariant::fromValue + qRegisterMetaType >("QSharedPointer"); + + QObject *rawptr = new QObject; + QSharedPointer strong(rawptr); + QWeakPointer weak(strong); + QPointer qptr(rawptr); + + QVariant v = QVariant::fromValue(strong); + QCOMPARE(v.value >(), strong); + + // clear our QSP; the copy inside the variant should keep the object alive + strong.clear(); + + // check that the object didn't get deleted + QVERIFY(!weak.isNull()); + QVERIFY(!qptr.isNull()); + + strong = qvariant_cast >(v); + QCOMPARE(strong.data(), rawptr); + QVERIFY(strong == weak); + + // now really delete the object and verify + strong.clear(); + v.clear(); + QVERIFY(weak.isNull()); + QVERIFY(qptr.isNull()); + + // compile test: + // QVariant::fromValue has already called this function + qRegisterMetaType >(); +} + void tst_QVariant::convertToQUint8() const { /* qint8. */ -- cgit v1.2.3