source: trunk/doc/src/snippets/code/src_qtestlib_qtestcase.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41
42void wrapInFunction()
43{
44
45//! [0]
46QVERIFY(1 + 1 == 2);
47//! [0]
48
49
50//! [1]
51QVERIFY2(1 + 1 == 2, "A breach in basic arithmetic occurred.");
52//! [1]
53
54
55//! [2]
56QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
57//! [2]
58
59
60//! [3]
61void TestQString::toInt_data()
62{
63 QTest::addColumn<QString>("aString");
64 QTest::addColumn<int>("expected");
65
66 QTest::newRow("positive value") << "42" << 42;
67 QTest::newRow("negative value") << "-42" << -42;
68 QTest::newRow("zero") << "0" << 0;
69}
70//! [3]
71
72
73//! [4]
74void TestQString::toInt()
75{
76 QFETCH(QString, aString);
77 QFETCH(int, expected);
78
79 QCOMPARE(aString.toInt(), expected);
80}
81//! [4]
82
83
84//! [5]
85if (sizeof(int) != 4)
86 QFAIL("This test has not been ported to this platform yet.");
87//! [5]
88
89
90//! [6]
91QFETCH(QString, myString);
92QCOMPARE(QString("hello").toUpper(), myString);
93//! [6]
94
95
96//! [7]
97QTEST(QString("hello").toUpper(), "myString");
98//! [7]
99
100
101//! [8]
102if (!QSqlDatabase::drivers().contains("SQLITE"))
103 QSKIP("This test requires the SQLITE database driver", SkipAll);
104//! [8]
105
106
107//! [9]
108QEXPECT_FAIL("", "Will fix in the next release", Continue);
109QCOMPARE(i, 42);
110QCOMPARE(j, 43);
111//! [9]
112
113
114//! [10]
115QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
116QCOMPARE(i, 42);
117//! [10]
118
119
120//! [11]
121class TestQString: public QObject { ... };
122QTEST_MAIN(TestQString)
123//! [11]
124
125
126//! [12]
127#ifdef Q_WS_X11
128 QTEST_MAIN(MyX11Test)
129#else
130 // do nothing on non-X11 platforms
131 QTEST_NOOP_MAIN
132#endif
133//! [12]
134
135
136//! [13]
137QTest::keyClick(myWidget, 'a');
138//! [13]
139
140
141//! [14]
142QTest::keyClick(myWidget, Qt::Key_Escape);
143
144QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
145//! [14]
146
147
148//! [15]
149QTest::keyClicks(myWidget, "hello world");
150//! [15]
151
152
153//! [16]
154namespace QTest {
155 template<>
156 char *toString(const MyPoint &point)
157 {
158 QByteArray ba = "MyPoint(";
159 ba += QByteArray::number(point.x()) + ", " + QByteArray::number(point.y());
160 ba += ")";
161 return qstrdup(ba.data());
162 }
163}
164//! [16]
165
166
167//! [17]
168int i = 0;
169while (myNetworkServerNotResponding() && i++ < 50)
170 QTest::qWait(250);
171//! [17]
172
173
174//! [18]
175MyFirstTestObject test1;
176QTest::qExec(&test1);
177
178MySecondTestObject test2;
179QTest::qExec(&test2);
180//! [18]
181
182
183//! [19]
184QDir dir;
185
186QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
187dir.mkdir("");
188//! [19]
189
190
191//! [20]
192void myTestFunction_data()
193{
194 QTest::addColumn<QString>("aString");
195 QTest::newRow("just hello") << QString("hello");
196 QTest::newRow("a null string") << QString();
197}
198//! [20]
199
200
201//! [21]
202void myTestFunction_data() {
203 QTest::addColumn<int>("intval");
204 QTest::addColumn<QString>("str");
205 QTest::addColumn<double>("dbl");
206
207 QTest::newRow("row1") << 1 << "hello" << 1.5;
208}
209//! [21]
210
211
212//! [22]
213void MyTestClass::cleanup()
214{
215 if (qstrcmp(currentTestFunction(), "myDatabaseTest") == 0) {
216 // clean up all database connections
217 closeAllDatabases();
218 }
219}
220//! [22]
221
222
223//! [23]
224QTest::qSleep(250);
225//! [23]
226
227//! [24]
228QWidget widget;
229widget.show();
230QTest::qWaitForWindowShown(&widget);
231//! [24]
232
233//! [25]
234QWidget widget;
235
236QTest::touchEvent(&widget)
237 .press(0, QPoint(10, 10));
238QTest::touchEvent(&widget)
239 .stationary(0)
240 .press(1, QPoint(40, 10));
241QTest::touchEvent(&widget)
242 .move(0, QPoint(12, 12))
243 .move(1, QPoint(45, 5));
244QTest::touchEvent(&widget)
245 .release(0)
246 .release(1);
247//! [25]
248
249}
250
Note: See TracBrowser for help on using the repository browser.