doc/src/snippets/code/src_qtestlib_qtestcase.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 documentation 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 void wrapInFunction()
       
    44 {
       
    45 
       
    46 //! [0]
       
    47 QVERIFY(1 + 1 == 2);
       
    48 //! [0]
       
    49 
       
    50 
       
    51 //! [1]
       
    52 QVERIFY2(1 + 1 == 2, "A breach in basic arithmetic occured.");
       
    53 //! [1]
       
    54 
       
    55 
       
    56 //! [2]
       
    57 QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
       
    58 //! [2]
       
    59 
       
    60 
       
    61 //! [3]
       
    62 void TestQString::toInt_data()
       
    63 {
       
    64     QTest::addColumn<QString>("aString");
       
    65     QTest::addColumn<int>("expected");
       
    66 
       
    67     QTest::newRow("positive value") << "42" << 42;
       
    68     QTest::newRow("negative value") << "-42" << -42;
       
    69     QTest::newRow("zero") << "0" << 0;
       
    70 }
       
    71 //! [3]
       
    72 
       
    73 
       
    74 //! [4]
       
    75 void TestQString::toInt()
       
    76 {
       
    77      QFETCH(QString, aString);
       
    78      QFETCH(int, expected);
       
    79 
       
    80      QCOMPARE(aString.toInt(), expected);
       
    81 }
       
    82 //! [4]
       
    83 
       
    84 
       
    85 //! [5]
       
    86 if (sizeof(int) != 4)
       
    87     QFAIL("This test has not been ported to this platform yet.");
       
    88 //! [5]
       
    89 
       
    90 
       
    91 //! [6]
       
    92 QFETCH(QString, myString);
       
    93 QCOMPARE(QString("hello").toUpper(), myString);
       
    94 //! [6]
       
    95 
       
    96 
       
    97 //! [7]
       
    98 QTEST(QString("hello").toUpper(), "myString");
       
    99 //! [7]
       
   100 
       
   101 
       
   102 //! [8]
       
   103 if (!QSqlDatabase::drivers().contains("SQLITE"))
       
   104     QSKIP("This test requires the SQLITE database driver", SkipAll);
       
   105 //! [8]
       
   106 
       
   107 
       
   108 //! [9]
       
   109 QEXPECT_FAIL("", "Will fix in the next release", Continue);
       
   110 QCOMPARE(i, 42);
       
   111 QCOMPARE(j, 43);
       
   112 //! [9]
       
   113 
       
   114 
       
   115 //! [10]
       
   116 QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
       
   117 QCOMPARE(i, 42);
       
   118 //! [10]
       
   119 
       
   120 
       
   121 //! [11]
       
   122 class TestQString: public QObject { ... };
       
   123 QTEST_MAIN(TestQString)
       
   124 //! [11]
       
   125 
       
   126 
       
   127 //! [12]
       
   128 #ifdef Q_WS_X11
       
   129     QTEST_MAIN(MyX11Test)
       
   130 #else
       
   131     // do nothing on non-X11 platforms
       
   132     QTEST_NOOP_MAIN
       
   133 #endif
       
   134 //! [12]
       
   135 
       
   136 
       
   137 //! [13]
       
   138 QTest::keyClick(myWidget, 'a');
       
   139 //! [13]
       
   140 
       
   141 
       
   142 //! [14]
       
   143 QTest::keyClick(myWidget, Qt::Key_Escape);
       
   144 
       
   145 QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
       
   146 //! [14]
       
   147 
       
   148 
       
   149 //! [15]
       
   150 QTest::keyClicks(myWidget, "hello world");
       
   151 //! [15]
       
   152 
       
   153 
       
   154 //! [16]
       
   155 namespace QTest {
       
   156     template<>
       
   157     char *toString(const MyPoint &point)
       
   158     {
       
   159         QByteArray ba = "MyPoint(";
       
   160         ba += QByteArray::number(point.x()) + ", " + QByteArray::number(point.y());
       
   161         ba += ")";
       
   162         return qstrdup(ba.data());
       
   163     }
       
   164 }
       
   165 //! [16]
       
   166 
       
   167 
       
   168 //! [17]
       
   169 int i = 0;
       
   170 while (myNetworkServerNotResponding() && i++ < 50)
       
   171     QTest::qWait(250);
       
   172 //! [17]
       
   173 
       
   174 
       
   175 //! [18]
       
   176 MyFirstTestObject test1;
       
   177 QTest::qExec(&test1);
       
   178 
       
   179 MySecondTestObject test2;
       
   180 QTest::qExec(&test2);
       
   181 //! [18]
       
   182 
       
   183 
       
   184 //! [19]
       
   185 QDir dir;
       
   186 
       
   187 QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
       
   188 dir.mkdir("");
       
   189 //! [19]
       
   190 
       
   191 
       
   192 //! [20]
       
   193 void myTestFunction_data()
       
   194 {
       
   195     QTest::addColumn<QString>("aString");
       
   196     QTest::newRow("just hello") << QString("hello");
       
   197     QTest::newRow("a null string") << QString();
       
   198 }
       
   199 //! [20]
       
   200 
       
   201 
       
   202 //! [21]
       
   203 void myTestFunction_data() {
       
   204     QTest::addColumn<int>("intval");
       
   205     QTest::addColumn<QString>("str");
       
   206     QTest::addColumn<double>("dbl");
       
   207 
       
   208     QTest::newRow("row1") << 1 << "hello" << 1.5;
       
   209 }
       
   210 //! [21]
       
   211 
       
   212 
       
   213 //! [22]
       
   214 void MyTestClass::cleanup()
       
   215 {
       
   216     if (qstrcmp(currentTestFunction(), "myDatabaseTest") == 0) {
       
   217         // clean up all database connections
       
   218         closeAllDatabases();
       
   219     }
       
   220 }
       
   221 //! [22]
       
   222 
       
   223 
       
   224 //! [23]
       
   225 QTest::qSleep(250);
       
   226 //! [23]
       
   227 
       
   228 //! [24]
       
   229 QWidget widget;
       
   230 widget.show();
       
   231 QTest::qWaitForWindowShown(&widget);
       
   232 //! [24]
       
   233 
       
   234 }
       
   235