source: trunk/src/testlib/qtestcase.cpp@ 318

Last change on this file since 318 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 62.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "QtTest/qtestcase.h"
43#include "QtTest/qtestassert.h"
44
45#include <QtCore/qbytearray.h>
46#include <QtCore/qmetaobject.h>
47#include <QtCore/qobject.h>
48#include <QtCore/qstringlist.h>
49#include <QtCore/qvector.h>
50#include <QtCore/qvarlengtharray.h>
51#include <QtCore/qcoreapplication.h>
52#include <QtCore/qfile.h>
53#include <QtCore/qfileinfo.h>
54#include <QtCore/qdir.h>
55#include <QtCore/qprocess.h>
56#include <QtCore/qdebug.h>
57
58#include "QtTest/private/qtestlog_p.h"
59#include "QtTest/private/qtesttable_p.h"
60#include "QtTest/qtestdata.h"
61#include "QtTest/private/qtestresult_p.h"
62#include "QtTest/private/qsignaldumper_p.h"
63#include "QtTest/private/qbenchmark_p.h"
64#include "3rdparty/cycle_p.h"
65
66#include <stdarg.h>
67#include <stdio.h>
68#include <stdlib.h>
69
70#ifdef Q_OS_WIN
71#include <windows.h> // for Sleep
72#endif
73#ifdef Q_OS_UNIX
74#include <time.h>
75#endif
76
77#ifdef Q_WS_MAC
78#include <Carbon/Carbon.h> // for SetFrontProcess
79#ifdef QT_MAC_USE_COCOA
80#include <IOKit/pwr_mgt/IOPMLib.h>
81#else
82#include <Security/AuthSession.h>
83#endif
84#undef verify
85#endif
86
87QT_BEGIN_NAMESPACE
88
89/*!
90 \namespace QTest
91 \inmodule QtTest
92
93 \brief The QTest namespace contains all the functions and
94 declarations that are related to the QTestLib tool.
95
96 Please refer to the \l{QTestLib Manual} documentation for information on
97 how to write unit tests.
98*/
99
100/*! \macro QVERIFY(condition)
101
102 \relates QTest
103
104 The QVERIFY() macro checks whether the \a condition is true or not. If it is
105 true, execution continues. If not, a failure is recorded in the test log
106 and the test won't be executed further.
107
108 \bold {Note:} This macro can only be used in a test function that is invoked
109 by the test framework.
110
111 Example:
112 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 0
113
114 \sa QCOMPARE()
115*/
116
117/*! \macro QVERIFY2(condition, message)
118
119 \relates QTest
120
121 The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs
122 a verbose \a message when \a condition is false. The \a message is a plain
123 C string.
124
125 Example:
126 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 1
127
128 \sa QVERIFY(), QCOMPARE()
129*/
130
131/*! \macro QCOMPARE(actual, expected)
132
133 \relates QTest
134
135 The QCOMPARE macro compares an \a actual value to an \a expected value using
136 the equals operator. If \a actual and \a expected are identical, execution
137 continues. If not, a failure is recorded in the test log and the test
138 won't be executed further.
139
140 In the case of comparing floats and doubles, qFuzzyCompare() is used for
141 comparing. This means that comparing to 0 will likely fail. One solution
142 to this is to compare to 1, and add 1 to the produced output.
143
144 QCOMPARE tries to output the contents of the values if the comparison fails,
145 so it is visible from the test log why the comparison failed.
146
147 QCOMPARE is very strict on the data types. Both \a actual and \a expected
148 have to be of the same type, otherwise the test won't compile. This prohibits
149 unspecified behavior from being introduced; that is behavior that usually
150 occurs when the compiler implicitly casts the argument.
151
152 If you use QCOMPARE() to compare two QStringList objects, it will start
153 comparing the objects from the end of the lists.
154
155 For your own classes, you can use \l QTest::toString() to format values for
156 outputting into the test log.
157
158 \note This macro can only be used in a test function that is invoked
159 by the test framework.
160
161 Example:
162 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 2
163
164 \sa QVERIFY(), QTest::toString()
165*/
166
167/*! \macro QFETCH(type, name)
168
169 \relates QTest
170
171 The fetch macro creates a local variable named \a name with the type \a type
172 on the stack. \a name has to match the element name from the test's data.
173 If no such element exists, the test will assert.
174
175 Assuming a test has the following data:
176
177 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 3
178
179 The test data has two elements, a QString called \c aString and an integer
180 called \c expected. To fetch these values in the actual test:
181
182 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 4
183
184 \c aString and \c expected are variables on the stack that are initialized with
185 the current test data.
186
187 \bold {Note:} This macro can only be used in a test function that is invoked
188 by the test framework. The test function must have a _data function.
189*/
190
191/*! \macro QWARN(message)
192
193 \relates QTest
194 \threadsafe
195
196 Appends \a message as a warning to the test log. This macro can be used anywhere
197 in your tests.
198*/
199
200/*! \macro QFAIL(message)
201
202 \relates QTest
203
204 This macro can be used to force a test failure. The test stops
205 executing and the failure \a message is appended to the test log.
206
207 \bold {Note:} This macro can only be used in a test function that is invoked
208 by the test framework.
209
210 Example:
211
212 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 5
213*/
214
215/*! \macro QTEST(actual, testElement)
216
217 \relates QTest
218
219 QTEST() is a convenience macro for \l QCOMPARE() that compares
220 the value \a actual with the element \a testElement from the test's data.
221 If there is no such element, the test asserts.
222
223 Apart from that, QTEST() behaves exactly as \l QCOMPARE().
224
225 Instead of writing:
226
227 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 6
228
229 you can write:
230
231 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 7
232
233 \sa QCOMPARE()
234*/
235
236/*! \macro QSKIP(description, mode)
237
238 \relates QTest
239
240 The QSKIP() macro stops execution of the test without adding a failure to the
241 test log. You can use it to skip tests that wouldn't make sense in the current
242 configuration. The text \a description is appended to the test log and should
243 contain an explanation why the test couldn't be executed. \a mode is a QTest::SkipMode
244 and describes whether to proceed with the rest of the test data or not.
245
246 \bold {Note:} This macro can only be used in a test function that is invoked
247 by the test framework.
248
249 Example:
250 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 8
251
252 \sa QTest::SkipMode
253*/
254
255/*! \macro QEXPECT_FAIL(dataIndex, comment, mode)
256
257 \relates QTest
258
259 The QEXPECT_FAIL() macro marks the next \l QCOMPARE() or \l QVERIFY() as an
260 expected failure. Instead of adding a failure to the test log, an expected
261 failure will be reported.
262
263 If a \l QVERIFY() or \l QCOMPARE() is marked as an expected failure,
264 but passes instead, an unexpected pass (XPASS) is written to the test log.
265
266 The parameter \a dataIndex describes for which entry in the test data the
267 failure is expected. Pass an empty string (\c{""}) if the failure
268 is expected for all entries or if no test data exists.
269
270 \a comment will be appended to the test log for the expected failure.
271
272 \a mode is a \l QTest::TestFailMode and sets whether the test should
273 continue to execute or not.
274
275 \bold {Note:} This macro can only be used in a test function that is invoked
276 by the test framework.
277
278 Example 1:
279 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 9
280
281 In the example above, an expected fail will be written into the test output
282 if the variable \c i is not 42. If the variable \c i is 42, an unexpected pass
283 is written instead. The QEXPECT_FAIL() has no influence on the second QCOMPARE()
284 statement in the example.
285
286 Example 2:
287 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 10
288
289 The above testfunction will not continue executing for the test data
290 entry \c{data27}.
291
292 \sa QTest::TestFailMode, QVERIFY(), QCOMPARE()
293*/
294
295/*! \macro QTEST_MAIN(TestClass)
296
297 \relates QTest
298
299 Implements a main() function that instantiates a QApplication object and
300 the \a TestClass, and executes all tests in the order they were defined.
301 Use this macro to build stand-alone executables.
302
303 Example:
304 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 11
305
306 \sa QTEST_APPLESS_MAIN(), QTest::qExec()
307*/
308
309/*! \macro QTEST_APPLESS_MAIN(TestClass)
310
311 \relates QTest
312
313 Implements a main() function that executes all tests in \a TestClass.
314
315 Behaves like \l QTEST_MAIN(), but doesn't instantiate a QApplication
316 object. Use this macro for really simple stand-alone non-GUI tests.
317
318 \sa QTEST_MAIN()
319*/
320
321/*! \macro QTEST_NOOP_MAIN()
322
323 \relates QTest
324
325 Implements a main() function with a test class that does absolutely nothing.
326 Use this macro to create a test that produces valid test output but just
327 doesn't execute any test, for example in conditional compilations:
328
329 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 12
330
331 \sa QTEST_MAIN()
332*/
333
334/*!
335 \macro QBENCHMARK
336
337 \relates QTest
338
339 This macro is used to measure the performance of code within a test.
340 The code to be benchmarked is contained within a code block following
341 this macro.
342
343 For example:
344
345 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 0
346
347 \sa {QTestLib Manual#Creating a Benchmark}{Creating a Benchmark},
348 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
349*/
350
351/*! \enum QTest::SkipMode
352
353 This enum describes the modes for skipping tests during execution
354 of the test data.
355
356 \value SkipSingle Skips the current entry in the test table; continues
357 execution of all the other entries in the table.
358
359 \value SkipAll Skips all the entries in the test table; the test won't
360 be executed further.
361
362 \sa QSKIP()
363*/
364
365/*! \enum QTest::TestFailMode
366
367 This enum describes the modes for handling an expected failure of the
368 \l QVERIFY() or \l QCOMPARE() macros.
369
370 \value Abort Aborts the execution of the test. Use this mode when it
371 doesn't make sense to execute the test any further after the
372 expected failure.
373
374 \value Continue Continues execution of the test after the expected failure.
375
376 \sa QEXPECT_FAIL()
377*/
378
379/*! \enum QTest::KeyAction
380
381 This enum describes possible actions for key handling.
382
383 \value Press The key is pressed.
384 \value Release The key is released.
385 \value Click The key is clicked (pressed and released).
386*/
387
388/*! \enum QTest::MouseAction
389
390 This enum describes possible actions for mouse handling.
391
392 \value MousePress A mouse button is pressed.
393 \value MouseRelease A mouse button is released.
394 \value MouseClick A mouse button is clicked (pressed and released).
395 \value MouseDClick A mouse button is double clicked (pressed and released twice).
396 \value MouseMove The mouse pointer has moved.
397*/
398
399/*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
400
401 \overload
402
403 Simulates clicking of \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
404
405 Example:
406 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 13
407
408 The example above simulates clicking \c a on \c myWidget without
409 any keyboard modifiers and without delay of the test.
410
411 \sa QTest::keyClicks()
412*/
413
414/*! \fn void QTest::keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
415
416 Simulates clicking of \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
417
418 Examples:
419 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 14
420
421 The first example above simulates clicking the \c escape key on \c
422 myWidget without any keyboard modifiers and without delay. The
423 second example simulates clicking \c shift-escape on \c myWidget
424 with a following 200 ms delay of the test.
425
426 \sa QTest::keyClicks()
427*/
428
429/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
430
431 Sends a Qt key event to \a widget with the given \a key and an associated \a action. Optionally, a keyboard \a modifier can be specified, as well as a \a delay (in milliseconds) of the test before sending the event.
432*/
433
434/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
435
436 \overload
437
438 Sends a Qt key event to \a widget with the given key \a ascii and an associated \a action. Optionally, a keyboard \a modifier can be specified, as well as a \a delay (in milliseconds) of the test before sending the event.
439
440*/
441
442/*! \fn void QTest::keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
443
444 Simulates pressing a \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
445
446 \bold {Note:} At some point you should release the key using \l keyRelease().
447
448 \sa QTest::keyRelease(), QTest::keyClick()
449*/
450
451/*! \fn void QTest::keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
452
453 \overload
454
455 Simulates pressing a \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
456
457 \bold {Note:} At some point you should release the key using \l keyRelease().
458
459 \sa QTest::keyRelease(), QTest::keyClick()
460*/
461
462/*! \fn void QTest::keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
463
464 Simulates releasing a \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
465
466 \sa QTest::keyPress(), QTest::keyClick()
467*/
468
469/*! \fn void QTest::keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
470
471 \overload
472
473 Simulates releasing a \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds.
474
475 \sa QTest::keyClick()
476*/
477
478
479/*! \fn void QTest::keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
480
481 Simulates clicking a \a sequence of keys on a \a
482 widget. Optionally, a keyboard \a modifier can be specified as
483 well as a \a delay (in milliseconds) of the test before each key
484 click.
485
486 Example:
487 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 15
488
489 The example above simulates clicking the sequence of keys
490 representing "hello world" on \c myWidget without any keyboard
491 modifiers and without delay of the test.
492
493 \sa QTest::keyClick()
494*/
495
496/*! \fn void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
497
498 Simulates pressing a mouse \a button with an optional \a modifier
499 on a \a widget. The position is defined by \a pos; the default
500 position is the center of the widget. If \a delay is specified,
501 the test will wait for the specified amount of milliseconds before
502 the press.
503
504 \sa QTest::mouseRelease(), QTest::mouseClick()
505*/
506
507/*! \fn void QTest::mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
508
509 Simulates releasing a mouse \a button with an optional \a modifier
510 on a \a widget. The position of the release is defined by \a pos;
511 the default position is the center of the widget. If \a delay is
512 specified, the test will wait for the specified amount of
513 milliseconds before releasing the button.
514
515 \sa QTest::mousePress(), QTest::mouseClick()
516*/
517
518/*! \fn void QTest::mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
519
520 Simulates clicking a mouse \a button with an optional \a modifier
521 on a \a widget. The position of the click is defined by \a pos;
522 the default position is the center of the widget. If \a delay is
523 specified, the test will wait for the specified amount of
524 milliseconds before pressing and before releasing the button.
525
526 \sa QTest::mousePress(), QTest::mouseRelease()
527*/
528
529/*! \fn void QTest::mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
530
531 Simulates double clicking a mouse \a button with an optional \a
532 modifier on a \a widget. The position of the click is defined by
533 \a pos; the default position is the center of the widget. If \a
534 delay is specified, the test will wait for the specified amount of
535 milliseconds before each press and release.