tests/auto/qpointer/tst_qpointer.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <QApplication>
       
    46 #include <QDebug>
       
    47 #include <QPointer>
       
    48 #include <QWidget>
       
    49 
       
    50 class tst_QPointer : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 public:
       
    54     tst_QPointer();
       
    55     ~tst_QPointer();
       
    56 
       
    57     inline tst_QPointer *me() const
       
    58     { return const_cast<tst_QPointer *>(this); }
       
    59 
       
    60 public slots:
       
    61     void initTestCase();
       
    62     void cleanupTestCase();
       
    63     void init();
       
    64     void cleanup();
       
    65 private slots:
       
    66     void constructors();
       
    67     void destructor();
       
    68     void assignment_operators();
       
    69     void equality_operators();
       
    70     void isNull();
       
    71     void dereference_operators();
       
    72     void disconnect();
       
    73     void castDuringDestruction();
       
    74     void data() const;
       
    75     void dataSignature() const;
       
    76 };
       
    77 
       
    78 tst_QPointer::tst_QPointer()
       
    79 { }
       
    80 
       
    81 tst_QPointer::~tst_QPointer()
       
    82 { }
       
    83 
       
    84 void tst_QPointer::initTestCase()
       
    85 { }
       
    86 
       
    87 void tst_QPointer::cleanupTestCase()
       
    88 { }
       
    89 
       
    90 void tst_QPointer::init()
       
    91 { }
       
    92 
       
    93 void tst_QPointer::cleanup()
       
    94 { }
       
    95 
       
    96 void tst_QPointer::constructors()
       
    97 {
       
    98     QPointer<QObject> p1;
       
    99     QPointer<QObject> p2(this);
       
   100     QPointer<QObject> p3(p2);
       
   101     QCOMPARE(p1, QPointer<QObject>(0));
       
   102     QCOMPARE(p2, QPointer<QObject>(this));
       
   103     QCOMPARE(p3, QPointer<QObject>(this));
       
   104 }
       
   105 
       
   106 void tst_QPointer::destructor()
       
   107 {
       
   108     QObject *object = new QObject;
       
   109     QPointer<QObject> p = object;
       
   110     QCOMPARE(p, QPointer<QObject>(object));
       
   111     delete object;
       
   112     QCOMPARE(p, QPointer<QObject>(0));
       
   113 }
       
   114 
       
   115 void tst_QPointer::assignment_operators()
       
   116 {
       
   117     QPointer<QObject> p1;
       
   118     QPointer<QObject> p2;
       
   119 
       
   120     p1 = this;
       
   121     p2 = p1;
       
   122 
       
   123     QCOMPARE(p1, QPointer<QObject>(this));
       
   124     QCOMPARE(p2, QPointer<QObject>(this));
       
   125     QCOMPARE(p1, QPointer<QObject>(p2));
       
   126 
       
   127     p1 = 0;
       
   128     p2 = p1;
       
   129     QCOMPARE(p1, QPointer<QObject>(0));
       
   130     QCOMPARE(p2, QPointer<QObject>(0));
       
   131     QCOMPARE(p1, QPointer<QObject>(p2));
       
   132 
       
   133     QObject *object = new QObject;
       
   134 
       
   135     p1 = object;
       
   136     p2 = p1;
       
   137     QCOMPARE(p1, QPointer<QObject>(object));
       
   138     QCOMPARE(p2, QPointer<QObject>(object));
       
   139     QCOMPARE(p1, QPointer<QObject>(p2));
       
   140 
       
   141     delete object;
       
   142     QCOMPARE(p1, QPointer<QObject>(0));
       
   143     QCOMPARE(p2, QPointer<QObject>(0));
       
   144     QCOMPARE(p1, QPointer<QObject>(p2));
       
   145 }
       
   146 
       
   147 void tst_QPointer::equality_operators()
       
   148 {
       
   149     QPointer<QObject> p1;
       
   150     QPointer<QObject> p2;
       
   151 
       
   152     QVERIFY(p1 == p2);
       
   153 
       
   154     QObject *object = 0;
       
   155     QWidget *widget = 0;
       
   156 
       
   157     p1 = object;
       
   158     QVERIFY(p1 == p2);
       
   159     QVERIFY(p1 == object);
       
   160     p2 = object;
       
   161     QVERIFY(p2 == p1);
       
   162     QVERIFY(p2 == object);
       
   163 
       
   164     p1 = this;
       
   165     QVERIFY(p1 != p2);
       
   166     p2 = p1;
       
   167     QVERIFY(p1 == p2);
       
   168 
       
   169     // compare to zero
       
   170     p1 = 0;
       
   171     QVERIFY(p1 == 0);
       
   172     QVERIFY(0 == p1);
       
   173     QVERIFY(p2 != 0);
       
   174     QVERIFY(0 != p2);
       
   175     QVERIFY(p1 == object);
       
   176     QVERIFY(object == p1);
       
   177     QVERIFY(p2 != object);
       
   178     QVERIFY(object != p2);
       
   179     QVERIFY(p1 == widget);
       
   180     QVERIFY(widget == p1);
       
   181     QVERIFY(p2 != widget);
       
   182     QVERIFY(widget != p2);
       
   183 }
       
   184 
       
   185 void tst_QPointer::isNull()
       
   186 {
       
   187     QPointer<QObject> p1;
       
   188     QVERIFY(p1.isNull());
       
   189     p1 = this;
       
   190     QVERIFY(!p1.isNull());
       
   191     p1 = 0;
       
   192     QVERIFY(p1.isNull());
       
   193 }
       
   194 
       
   195 void tst_QPointer::dereference_operators()
       
   196 {
       
   197     QPointer<tst_QPointer> p1 = this;
       
   198 
       
   199     QObject *object = p1->me();
       
   200     QVERIFY(object == this);
       
   201 
       
   202     QObject &ref = *p1;
       
   203     QVERIFY(&ref == this);
       
   204 
       
   205     object = static_cast<QObject *>(p1);
       
   206     QVERIFY(object == this);
       
   207 }
       
   208 
       
   209 void tst_QPointer::disconnect()
       
   210 {
       
   211     QPointer<QObject> p1 = new QObject;
       
   212     QVERIFY(!p1.isNull());
       
   213     p1->disconnect();
       
   214     QVERIFY(!p1.isNull());
       
   215     delete static_cast<QObject *>(p1);
       
   216     QVERIFY(p1.isNull());
       
   217 }
       
   218 
       
   219 class ChildObject : public QObject
       
   220 {
       
   221     QPointer<QObject> guardedPointer;
       
   222 
       
   223 public:
       
   224     ChildObject(QObject *parent)
       
   225         : QObject(parent), guardedPointer(parent)
       
   226     { }
       
   227     ~ChildObject();
       
   228 };
       
   229 
       
   230 ChildObject::~ChildObject()
       
   231 {
       
   232     QCOMPARE(static_cast<QObject *>(guardedPointer), static_cast<QObject *>(0));
       
   233     QCOMPARE(qobject_cast<QObject *>(guardedPointer), static_cast<QObject *>(0));
       
   234 }
       
   235 
       
   236 class ChildWidget : public QWidget
       
   237 {
       
   238     QPointer<QWidget> guardedPointer;
       
   239 
       
   240 public:
       
   241     ChildWidget(QWidget *parent)
       
   242         : QWidget(parent), guardedPointer(parent)
       
   243     { }
       
   244     ~ChildWidget();
       
   245 };
       
   246 
       
   247 ChildWidget::~ChildWidget()
       
   248 {
       
   249     QCOMPARE(static_cast<QWidget *>(guardedPointer), static_cast<QWidget *>(0));
       
   250     QCOMPARE(qobject_cast<QWidget *>(guardedPointer), static_cast<QWidget *>(0));
       
   251 }
       
   252 
       
   253 class DerivedChild;
       
   254 
       
   255 class DerivedParent : public QObject
       
   256 {
       
   257     Q_OBJECT
       
   258 
       
   259     DerivedChild *derivedChild;
       
   260 
       
   261 public:
       
   262     DerivedParent();
       
   263     ~DerivedParent();
       
   264 };
       
   265 
       
   266 class DerivedChild : public QObject
       
   267 {
       
   268     Q_OBJECT
       
   269 
       
   270     DerivedParent *parentPointer;
       
   271     QPointer<DerivedParent> guardedParentPointer;
       
   272 
       
   273 public:
       
   274     DerivedChild(DerivedParent *parent)
       
   275         : QObject(parent), parentPointer(parent), guardedParentPointer(parent)
       
   276     { }
       
   277     ~DerivedChild();
       
   278 };
       
   279 
       
   280 DerivedParent::DerivedParent()
       
   281     : QObject()
       
   282 {
       
   283     derivedChild = new DerivedChild(this);
       
   284 }
       
   285 
       
   286 DerivedParent::~DerivedParent()
       
   287 {
       
   288     delete derivedChild;
       
   289 }
       
   290 
       
   291 DerivedChild::~DerivedChild()
       
   292 {
       
   293     QCOMPARE(static_cast<DerivedParent *>(guardedParentPointer), parentPointer);
       
   294     QCOMPARE(qobject_cast<DerivedParent *>(guardedParentPointer), parentPointer);
       
   295 }
       
   296 
       
   297 void tst_QPointer::castDuringDestruction()
       
   298 {
       
   299     {
       
   300         QObject *parentObject = new QObject();
       
   301         (void) new ChildObject(parentObject);
       
   302         delete parentObject;
       
   303     }
       
   304 
       
   305     {
       
   306         QWidget *parentWidget = new QWidget();
       
   307         (void) new ChildWidget(parentWidget);
       
   308         delete parentWidget;
       
   309     }
       
   310 
       
   311     {
       
   312         delete new DerivedParent();
       
   313     }
       
   314 }
       
   315 
       
   316 void tst_QPointer::data() const
       
   317 {
       
   318     /* Check value of a default constructed object. */
       
   319     {
       
   320         QPointer<QObject> p;
       
   321         QCOMPARE(p.data(), static_cast<QObject *>(0));
       
   322     }
       
   323 
       
   324     /* Check value of a default constructed object. */
       
   325     {
       
   326         QObject *const object = new QObject();
       
   327         QPointer<QObject> p(object);
       
   328         QCOMPARE(p.data(), object);
       
   329     }
       
   330 }
       
   331 
       
   332 void tst_QPointer::dataSignature() const
       
   333 {
       
   334     /* data() should be const. */
       
   335     {
       
   336         const QPointer<QObject> p;
       
   337         p.data();
       
   338     }
       
   339 
       
   340     /* The return type should be T. */
       
   341     {
       
   342         const QPointer<QWidget> p;
       
   343         /* If the types differs, the QCOMPARE will fail to instansiate. */
       
   344         QCOMPARE(p.data(), static_cast<QWidget *>(0));
       
   345     }
       
   346 }
       
   347 
       
   348 QTEST_MAIN(tst_QPointer)
       
   349 #include "tst_qpointer.moc"