tests/auto/qguard/tst_qguard.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 // NOTE: This is identical to the QPointer autotest
       
    43 
       
    44 #include <QtTest/QtTest>
       
    45 
       
    46 #include <QApplication>
       
    47 #include <QDebug>
       
    48 #include <QWidget>
       
    49 #include <private/qguard_p.h>
       
    50 
       
    51 class tst_QGuard : public QObject
       
    52 {
       
    53     Q_OBJECT
       
    54 public:
       
    55     tst_QGuard();
       
    56     ~tst_QGuard();
       
    57 
       
    58     inline tst_QGuard *me() const
       
    59     { return const_cast<tst_QGuard *>(this); }
       
    60 
       
    61 public slots:
       
    62     void initTestCase();
       
    63     void cleanupTestCase();
       
    64     void init();
       
    65     void cleanup();
       
    66 private slots:
       
    67     void constructors();
       
    68     void destructor();
       
    69     void assignment_operators();
       
    70     void equality_operators();
       
    71     void isNull();
       
    72     void dereference_operators();
       
    73     void disconnect();
       
    74     void castDuringDestruction();
       
    75     void data() const;
       
    76     void dataSignature() const;
       
    77 };
       
    78 
       
    79 tst_QGuard::tst_QGuard()
       
    80 { }
       
    81 
       
    82 tst_QGuard::~tst_QGuard()
       
    83 { }
       
    84 
       
    85 void tst_QGuard::initTestCase()
       
    86 { }
       
    87 
       
    88 void tst_QGuard::cleanupTestCase()
       
    89 { }
       
    90 
       
    91 void tst_QGuard::init()
       
    92 { }
       
    93 
       
    94 void tst_QGuard::cleanup()
       
    95 { }
       
    96 
       
    97 void tst_QGuard::constructors()
       
    98 {
       
    99     QGuard<QObject> p1;
       
   100     QGuard<QObject> p2(this);
       
   101     QGuard<QObject> p3(p2);
       
   102     QCOMPARE(p1, QGuard<QObject>(0));
       
   103     QCOMPARE(p2, QGuard<QObject>(this));
       
   104     QCOMPARE(p3, QGuard<QObject>(this));
       
   105 }
       
   106 
       
   107 void tst_QGuard::destructor()
       
   108 {
       
   109     QObject *object = new QObject;
       
   110     QGuard<QObject> p = object;
       
   111     QCOMPARE(p, QGuard<QObject>(object));
       
   112     delete object;
       
   113     QCOMPARE(p, QGuard<QObject>(0));
       
   114 }
       
   115 
       
   116 void tst_QGuard::assignment_operators()
       
   117 {
       
   118     QGuard<QObject> p1;
       
   119     QGuard<QObject> p2;
       
   120 
       
   121     p1 = this;
       
   122     p2 = p1;
       
   123 
       
   124     QCOMPARE(p1, QGuard<QObject>(this));
       
   125     QCOMPARE(p2, QGuard<QObject>(this));
       
   126     QCOMPARE(p1, QGuard<QObject>(p2));
       
   127 
       
   128     p1 = 0;
       
   129     p2 = p1;
       
   130     QCOMPARE(p1, QGuard<QObject>(0));
       
   131     QCOMPARE(p2, QGuard<QObject>(0));
       
   132     QCOMPARE(p1, QGuard<QObject>(p2));
       
   133 
       
   134     QObject *object = new QObject;
       
   135 
       
   136     p1 = object;
       
   137     p2 = p1;
       
   138     QCOMPARE(p1, QGuard<QObject>(object));
       
   139     QCOMPARE(p2, QGuard<QObject>(object));
       
   140     QCOMPARE(p1, QGuard<QObject>(p2));
       
   141 
       
   142     delete object;
       
   143     QCOMPARE(p1, QGuard<QObject>(0));
       
   144     QCOMPARE(p2, QGuard<QObject>(0));
       
   145     QCOMPARE(p1, QGuard<QObject>(p2));
       
   146 }
       
   147 
       
   148 void tst_QGuard::equality_operators()
       
   149 {
       
   150     QGuard<QObject> p1;
       
   151     QGuard<QObject> p2;
       
   152 
       
   153     QVERIFY(p1 == p2);
       
   154 
       
   155     QObject *object = 0;
       
   156     QWidget *widget = 0;
       
   157 
       
   158     p1 = object;
       
   159     QVERIFY(p1 == p2);
       
   160     QVERIFY(p1 == object);
       
   161     p2 = object;
       
   162     QVERIFY(p2 == p1);
       
   163     QVERIFY(p2 == object);
       
   164 
       
   165     p1 = this;
       
   166     QVERIFY(p1 != p2);
       
   167     p2 = p1;
       
   168     QVERIFY(p1 == p2);
       
   169 
       
   170     // compare to zero
       
   171     p1 = 0;
       
   172     QVERIFY(p1 == 0);
       
   173     QVERIFY(0 == p1);
       
   174     QVERIFY(p2 != 0);
       
   175     QVERIFY(0 != p2);
       
   176     QVERIFY(p1 == object);
       
   177     QVERIFY(object == p1);
       
   178     QVERIFY(p2 != object);
       
   179     QVERIFY(object != p2);
       
   180     QVERIFY(p1 == widget);
       
   181     QVERIFY(widget == p1);
       
   182     QVERIFY(p2 != widget);
       
   183     QVERIFY(widget != p2);
       
   184 }
       
   185 
       
   186 void tst_QGuard::isNull()
       
   187 {
       
   188     QGuard<QObject> p1;
       
   189     QVERIFY(p1.isNull());
       
   190     p1 = this;
       
   191     QVERIFY(!p1.isNull());
       
   192     p1 = 0;
       
   193     QVERIFY(p1.isNull());
       
   194 }
       
   195 
       
   196 void tst_QGuard::dereference_operators()
       
   197 {
       
   198     QGuard<tst_QGuard> p1 = this;
       
   199 
       
   200     QObject *object = p1->me();
       
   201     QVERIFY(object == this);
       
   202 
       
   203     QObject &ref = *p1;
       
   204     QVERIFY(&ref == this);
       
   205 
       
   206     object = static_cast<QObject *>(p1);
       
   207     QVERIFY(object == this);
       
   208 }
       
   209 
       
   210 void tst_QGuard::disconnect()
       
   211 {
       
   212     QGuard<QObject> p1 = new QObject;
       
   213     QVERIFY(!p1.isNull());
       
   214     p1->disconnect();
       
   215     QVERIFY(!p1.isNull());
       
   216     delete static_cast<QObject *>(p1);
       
   217     QVERIFY(p1.isNull());
       
   218 }
       
   219 
       
   220 class ChildObject : public QObject
       
   221 {
       
   222     QGuard<QObject> guardedPointer;
       
   223 
       
   224 public:
       
   225     ChildObject(QObject *parent)
       
   226         : QObject(parent), guardedPointer(parent)
       
   227     { }
       
   228     ~ChildObject();
       
   229 };
       
   230 
       
   231 ChildObject::~ChildObject()
       
   232 {
       
   233     QCOMPARE(static_cast<QObject *>(guardedPointer), static_cast<QObject *>(0));
       
   234     QCOMPARE(qobject_cast<QObject *>(guardedPointer), static_cast<QObject *>(0));
       
   235 }
       
   236 
       
   237 class ChildWidget : public QWidget
       
   238 {
       
   239     QGuard<QWidget> guardedPointer;
       
   240 
       
   241 public:
       
   242     ChildWidget(QWidget *parent)
       
   243         : QWidget(parent), guardedPointer(parent)
       
   244     { }
       
   245     ~ChildWidget();
       
   246 };
       
   247 
       
   248 ChildWidget::~ChildWidget()
       
   249 {
       
   250     QCOMPARE(static_cast<QWidget *>(guardedPointer), static_cast<QWidget *>(0));
       
   251     QCOMPARE(qobject_cast<QWidget *>(guardedPointer), static_cast<QWidget *>(0));
       
   252 }
       
   253 
       
   254 class DerivedChild;
       
   255 
       
   256 class DerivedParent : public QObject
       
   257 {
       
   258     Q_OBJECT
       
   259 
       
   260     DerivedChild *derivedChild;
       
   261 
       
   262 public:
       
   263     DerivedParent();
       
   264     ~DerivedParent();
       
   265 };
       
   266 
       
   267 class DerivedChild : public QObject
       
   268 {
       
   269     Q_OBJECT
       
   270 
       
   271     DerivedParent *parentPointer;
       
   272     QGuard<DerivedParent> guardedParentPointer;
       
   273 
       
   274 public:
       
   275     DerivedChild(DerivedParent *parent)
       
   276         : QObject(parent), parentPointer(parent), guardedParentPointer(parent)
       
   277     { }
       
   278     ~DerivedChild();
       
   279 };
       
   280 
       
   281 DerivedParent::DerivedParent()
       
   282     : QObject()
       
   283 {
       
   284     derivedChild = new DerivedChild(this);
       
   285 }
       
   286 
       
   287 DerivedParent::~DerivedParent()
       
   288 {
       
   289     delete derivedChild;
       
   290 }
       
   291 
       
   292 DerivedChild::~DerivedChild()
       
   293 {
       
   294     QCOMPARE(static_cast<DerivedParent *>(guardedParentPointer), parentPointer);
       
   295     QCOMPARE(qobject_cast<DerivedParent *>(guardedParentPointer), parentPointer);
       
   296 }
       
   297 
       
   298 void tst_QGuard::castDuringDestruction()
       
   299 {
       
   300     {
       
   301         QObject *parentObject = new QObject();
       
   302         (void) new ChildObject(parentObject);
       
   303         delete parentObject;
       
   304     }
       
   305 
       
   306     {
       
   307         QWidget *parentWidget = new QWidget();
       
   308         (void) new ChildWidget(parentWidget);
       
   309         delete parentWidget;
       
   310     }
       
   311 
       
   312     {
       
   313         delete new DerivedParent();
       
   314     }
       
   315 }
       
   316 
       
   317 void tst_QGuard::data() const
       
   318 {
       
   319     /* Check value of a default constructed object. */
       
   320     {
       
   321         QGuard<QObject> p;
       
   322         QCOMPARE(p.data(), static_cast<QObject *>(0));
       
   323     }
       
   324 
       
   325     /* Check value of a default constructed object. */
       
   326     {
       
   327         QObject *const object = new QObject();
       
   328         QGuard<QObject> p(object);
       
   329         QCOMPARE(p.data(), object);
       
   330     }
       
   331 }
       
   332 
       
   333 void tst_QGuard::dataSignature() const
       
   334 {
       
   335     /* data() should be const. */
       
   336     {
       
   337         const QGuard<QObject> p;
       
   338         p.data();
       
   339     }
       
   340 
       
   341     /* The return type should be T. */
       
   342     {
       
   343         const QGuard<QWidget> p;
       
   344         /* If the types differs, the QCOMPARE will fail to instansiate. */
       
   345         QCOMPARE(p.data(), static_cast<QWidget *>(0));
       
   346     }
       
   347 }
       
   348 
       
   349 QTEST_MAIN(tst_QGuard)
       
   350 #include "tst_qguard.moc"