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

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

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

File size: 70.9 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtTest module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]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
[561]74#include <errno.h>
75#include <signal.h>
[2]76#include <time.h>
77#endif
78
79#ifdef Q_WS_MAC
80#include <Carbon/Carbon.h> // for SetFrontProcess
81#ifdef QT_MAC_USE_COCOA
82#include <IOKit/pwr_mgt/IOPMLib.h>
83#else
84#include <Security/AuthSession.h>
85#endif
86#undef verify
87#endif
88
89QT_BEGIN_NAMESPACE
90
91/*!
92 \namespace QTest
93 \inmodule QtTest
94
95 \brief The QTest namespace contains all the functions and
96 declarations that are related to the QTestLib tool.
97
98 Please refer to the \l{QTestLib Manual} documentation for information on
99 how to write unit tests.
100*/
101
102/*! \macro QVERIFY(condition)
103
104 \relates QTest
105
106 The QVERIFY() macro checks whether the \a condition is true or not. If it is
107 true, execution continues. If not, a failure is recorded in the test log
108 and the test won't be executed further.
109
110 \bold {Note:} This macro can only be used in a test function that is invoked
111 by the test framework.
112
113 Example:
114 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 0
115
116 \sa QCOMPARE()
117*/
118
119/*! \macro QVERIFY2(condition, message)
120
121 \relates QTest
122
123 The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs
124 a verbose \a message when \a condition is false. The \a message is a plain
125 C string.
126
127 Example:
128 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 1
129
130 \sa QVERIFY(), QCOMPARE()
131*/
132
133/*! \macro QCOMPARE(actual, expected)
134
135 \relates QTest
136
137 The QCOMPARE macro compares an \a actual value to an \a expected value using
138 the equals operator. If \a actual and \a expected are identical, execution
139 continues. If not, a failure is recorded in the test log and the test
140 won't be executed further.
141
142 In the case of comparing floats and doubles, qFuzzyCompare() is used for
143 comparing. This means that comparing to 0 will likely fail. One solution
144 to this is to compare to 1, and add 1 to the produced output.
145
146 QCOMPARE tries to output the contents of the values if the comparison fails,
147 so it is visible from the test log why the comparison failed.
148
149 QCOMPARE is very strict on the data types. Both \a actual and \a expected
150 have to be of the same type, otherwise the test won't compile. This prohibits
151 unspecified behavior from being introduced; that is behavior that usually
152 occurs when the compiler implicitly casts the argument.
153
154 If you use QCOMPARE() to compare two QStringList objects, it will start
155 comparing the objects from the end of the lists.
156
157 For your own classes, you can use \l QTest::toString() to format values for
158 outputting into the test log.
159
160 \note This macro can only be used in a test function that is invoked
161 by the test framework.
162
163 Example:
164 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 2
165
166 \sa QVERIFY(), QTest::toString()
167*/
168
169/*! \macro QFETCH(type, name)
170
171 \relates QTest
172
173 The fetch macro creates a local variable named \a name with the type \a type
174 on the stack. \a name has to match the element name from the test's data.
175 If no such element exists, the test will assert.
176
177 Assuming a test has the following data:
178
179 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 3
180
181 The test data has two elements, a QString called \c aString and an integer
182 called \c expected. To fetch these values in the actual test:
183
184 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 4
185
186 \c aString and \c expected are variables on the stack that are initialized with
187 the current test data.
188
189 \bold {Note:} This macro can only be used in a test function that is invoked
190 by the test framework. The test function must have a _data function.
191*/
192
193/*! \macro QWARN(message)
194
195 \relates QTest
196 \threadsafe
197
198 Appends \a message as a warning to the test log. This macro can be used anywhere
199 in your tests.
200*/
201
202/*! \macro QFAIL(message)
203
204 \relates QTest
205
206 This macro can be used to force a test failure. The test stops
207 executing and the failure \a message is appended to the test log.
208
209 \bold {Note:} This macro can only be used in a test function that is invoked
210 by the test framework.
211
212 Example:
213
214 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 5
215*/
216
217/*! \macro QTEST(actual, testElement)
218
219 \relates QTest
220
221 QTEST() is a convenience macro for \l QCOMPARE() that compares
222 the value \a actual with the element \a testElement from the test's data.
223 If there is no such element, the test asserts.
224
225 Apart from that, QTEST() behaves exactly as \l QCOMPARE().
226
227 Instead of writing:
228
229 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 6
230
231 you can write:
232
233 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 7
234
235 \sa QCOMPARE()
236*/
237
238/*! \macro QSKIP(description, mode)
239
240 \relates QTest
241
242 The QSKIP() macro stops execution of the test without adding a failure to the
243 test log. You can use it to skip tests that wouldn't make sense in the current
244 configuration. The text \a description is appended to the test log and should
245 contain an explanation why the test couldn't be executed. \a mode is a QTest::SkipMode
246 and describes whether to proceed with the rest of the test data or not.
247
248 \bold {Note:} This macro can only be used in a test function that is invoked
249 by the test framework.
250
251 Example:
252 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 8
253
254 \sa QTest::SkipMode
255*/
256
257/*! \macro QEXPECT_FAIL(dataIndex, comment, mode)
258
259 \relates QTest
260
261 The QEXPECT_FAIL() macro marks the next \l QCOMPARE() or \l QVERIFY() as an
262 expected failure. Instead of adding a failure to the test log, an expected
263 failure will be reported.
264
265 If a \l QVERIFY() or \l QCOMPARE() is marked as an expected failure,
266 but passes instead, an unexpected pass (XPASS) is written to the test log.
267
268 The parameter \a dataIndex describes for which entry in the test data the
269 failure is expected. Pass an empty string (\c{""}) if the failure
270 is expected for all entries or if no test data exists.
271
272 \a comment will be appended to the test log for the expected failure.
273
274 \a mode is a \l QTest::TestFailMode and sets whether the test should
275 continue to execute or not.
276
277 \bold {Note:} This macro can only be used in a test function that is invoked
278 by the test framework.
279
280 Example 1:
281 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 9
282
283 In the example above, an expected fail will be written into the test output
284 if the variable \c i is not 42. If the variable \c i is 42, an unexpected pass
285 is written instead. The QEXPECT_FAIL() has no influence on the second QCOMPARE()
286 statement in the example.
287
288 Example 2:
289 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 10
290
291 The above testfunction will not continue executing for the test data
292 entry \c{data27}.
293
294 \sa QTest::TestFailMode, QVERIFY(), QCOMPARE()
295*/
296
297/*! \macro QTEST_MAIN(TestClass)
298
299 \relates QTest
300
[846]301 Implements a main() function that instantiates an application object and
[2]302 the \a TestClass, and executes all tests in the order they were defined.
303 Use this macro to build stand-alone executables.
304
[846]305 If \c QT_GUI_LIB is defined, the application object will be a QApplication,
306 otherwise it will be a QCoreApplication. If qmake is used and the configuration
307 includes \c{QT += gui}, then \c QT_GUI_LIB will be defined automatically.
308
[561]309 \bold {Note:} On platforms that have keypad navigation enabled by default (eg: Symbian),
310 this macro will forcfully disable it to simplify the usage of key events when writing
311 autotests. If you wish to write a test case that uses keypad navigation, you should
312 enable it either in the \c {initTestCase()} or \c {init()} functions of your test case.
313
[2]314 Example:
315 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 11
316
[561]317 \sa QTEST_APPLESS_MAIN(), QTest::qExec(), QApplication::setNavigationMode()
[2]318*/
319
320/*! \macro QTEST_APPLESS_MAIN(TestClass)
321
322 \relates QTest
323
324 Implements a main() function that executes all tests in \a TestClass.
325
326 Behaves like \l QTEST_MAIN(), but doesn't instantiate a QApplication
327 object. Use this macro for really simple stand-alone non-GUI tests.
328
329 \sa QTEST_MAIN()
330*/
331
332/*! \macro QTEST_NOOP_MAIN()
333
334 \relates QTest
335
336 Implements a main() function with a test class that does absolutely nothing.
337 Use this macro to create a test that produces valid test output but just
338 doesn't execute any test, for example in conditional compilations:
339
340 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 12
341
342 \sa QTEST_MAIN()
343*/
344
345/*!
346 \macro QBENCHMARK
347
348 \relates QTest
349
350 This macro is used to measure the performance of code within a test.
351 The code to be benchmarked is contained within a code block following
352 this macro.
353
354 For example:
355
356 \snippet examples/qtestlib/tutorial5/benchmarking.cpp 0
357
358 \sa {QTestLib Manual#Creating a Benchmark}{Creating a Benchmark},
359 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
360*/
361
[561]362/*!
363 \macro QBENCHMARK_ONCE
364 \since 4.6
365
366 \relates QTest
367
368 \brief The QBENCHMARK_ONCE macro is for measuring performance of a
369 code block by running it once.
370
371 This macro is used to measure the performance of code within a test.
372 The code to be benchmarked is contained within a code block following
373 this macro.
374
375 Unlike QBENCHMARK, the contents of the contained code block is only run
376 once. The elapsed time will be reported as "0" if it's to short to
377 be measured by the selected backend. (Use)
378
379 \sa {QTestLib Manual#Creating a Benchmark}{Creating a Benchmark},
380 {Chapter 5: Writing a Benchmark}{Writing a Benchmark}
381*/
382
383
384
[2]385/*! \enum QTest::SkipMode
386
387 This enum describes the modes for skipping tests during execution
388 of the test data.
389
390 \value SkipSingle Skips the current entry in the test table; continues
391 execution of all the other entries in the table.
392
393 \value SkipAll Skips all the entries in the test table; the test won't
394 be executed further.
395
396 \sa QSKIP()
397*/
398
399/*! \enum QTest::TestFailMode
400
401 This enum describes the modes for handling an expected failure of the
402 \l QVERIFY() or \l QCOMPARE() macros.
403
404 \value Abort Aborts the execution of the test. Use this mode when it
405 doesn't make sense to execute the test any further after the
406 expected failure.
407
408 \value Continue Continues execution of the test after the expected failure.
409
410 \sa QEXPECT_FAIL()
411*/
412
413/*! \enum QTest::KeyAction
414
415 This enum describes possible actions for key handling.
416
417 \value Press The key is pressed.
418 \value Release The key is released.
419 \value Click The key is clicked (pressed and released).
420*/
421
422/*! \enum QTest::MouseAction
423
424 This enum describes possible actions for mouse handling.
425
426 \value MousePress A mouse button is pressed.
427 \value MouseRelease A mouse button is released.
428 \value MouseClick A mouse button is clicked (pressed and released).
429 \value MouseDClick A mouse button is double clicked (pressed and released twice).
430 \value MouseMove The mouse pointer has moved.
431*/
432
433/*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
434
435 \overload
436
[561]437 Simulates clicking of \a key with an optional \a modifier on a \a widget.
438 If \a delay is larger than 0, the test will wait for \a delay milliseconds.
[2]439
440 Example:
441 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 13
442
443 The example above simulates clicking \c a on \c myWidget without
444 any keyboard modifiers and without delay of the test.
445
446 \sa QTest::keyClicks()
447*/
448
449/*! \fn void QTest::keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
450
[561]451 Simulates clicking of \a key with an optional \a modifier on a \a widget.
452 If \a delay is larger than 0, the test will wait for \a delay milliseconds.
[2]453
454 Examples:
455 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 14
456
457 The first example above simulates clicking the \c escape key on \c
458 myWidget without any keyboard modifiers and without delay. The
459 second example simulates clicking \c shift-escape on \c myWidget
460 with a following 200 ms delay of the test.
461
462 \sa QTest::keyClicks()
463*/
464
465/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
466
[561]467 Sends a Qt key event to \a widget with the given \a key and an associated \a action.
468 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
469 (in milliseconds) of the test before sending the event.
[2]470*/
471
472/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
473
474 \overload
475
[561]476 Sends a Qt key event to \a widget with the given key \a ascii and an associated \a action.
477 Optionally, a keyboard \a modifier can be specified, as well as a \a delay
478 (in milliseconds) of the test before sending the event.
[2]479
480*/
481
482/*! \fn void QTest::keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
483
[561]484 Simulates pressing a \a key with an optional \a modifier on a \a widget. If \a delay
485 is larger than 0, the test will wait for \a delay milliseconds.
[2]486
487 \bold {Note:} At some point you should release the key using \l keyRelease().
488
489 \sa QTest::keyRelease(), QTest::keyClick()
490*/
491
492/*! \fn void QTest::keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
493
494 \overload
495
[561]496 Simulates pressing a \a key with an optional \a modifier on a \a widget.
497 If \a delay is larger than 0, the test will wait for \a delay milliseconds.
[2]498
499 \bold {Note:} At some point you should release the key using \l keyRelease().
500
501 \sa QTest::keyRelease(), QTest::keyClick()
502*/
503
504/*! \fn void QTest::keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
505
[561]506 Simulates releasing a \a key with an optional \a modifier on a \a widget.
507 If \a delay is larger than 0, the test will wait for \a delay milliseconds.
[2]508
509 \sa QTest::keyPress(), QTest::keyClick()
510*/
511
512/*! \fn void QTest::keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
513
514 \overload
515
[561]516 Simulates releasing a \a key with an optional \a modifier on a \a widget.
517 If \a delay is larger than 0, the test will wait for \a delay milliseconds.
[2]518
519 \sa QTest::keyClick()
520*/
521
522
523/*! \fn void QTest::keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
524
525 Simulates clicking a \a sequence of keys on a \a
526 widget. Optionally, a keyboard \a modifier can be specified as
527 well as a \a delay (in milliseconds) of the test before each key
528 click.
529
530 Example:
531 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 15
532
533 The example above simulates clicking the sequence of keys
534 representing "hello world" on \c myWidget without any keyboard
535 modifiers and without delay of the test.
536
537 \sa QTest::keyClick()
538*/
539
540/*! \fn void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
541
542 Simulates pressing a mouse \a button with an optional \a modifier
543 on a \a widget. The position is defined by \a pos; the default
544 position is the center of the widget. If \a delay is specified,
545 the test will wait for the specified amount of milliseconds before
546 the press.
547
548 \sa QTest::mouseRelease(), QTest::mouseClick()
549*/
550
551/*! \fn void QTest::mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
552
553 Simulates releasing a mouse \a button with an optional \a modifier
554 on a \a widget. The position of the release is defined by \a pos;
555 the default position is the center of the widget. If \a delay is
556 specified, the test will wait for the specified amount of
557 milliseconds before releasing the button.
558
559 \sa QTest::mousePress(), QTest::mouseClick()
560*/
561
562/*! \fn void QTest::mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
563
564 Simulates clicking a mouse \a button with an optional \a modifier
565 on a \a widget. The position of the click is defined by \a pos;
566 the default position is the center of the widget. If \a delay is
567 specified, the test will wait for the specified amount of
568 milliseconds before pressing and before releasing the button.
569
570 \sa QTest::mousePress(), QTest::mouseRelease()
571*/
572
573/*! \fn void QTest::mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1)
574
575 Simulates double clicking a mouse \a button with an optional \a
576 modifier on a \a widget. The position of the click is defined by
577 \a pos; the default position is the center of the widget. If \a
578 delay is specified, the test will wait for the specified amount of
579 milliseconds before each press and release.
580
581 \sa QTest::mouseClick()
582*/
583
584/*! \fn void QTest::mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1)
585
586 Moves the mouse pointer to a \a widget. If \a pos is not
587 specified, the mouse pointer moves to the center of the widget. If
588 a \a delay (in milliseconds) is given, the test will wait before
589 moving the mouse pointer.
590*/
591
592/*!
593 \fn char *QTest::toString(const T &value)
594
595 Returns a textual representation of \a value. This function is used by
596 \l QCOMPARE() to output verbose information in case of a test failure.
597
598 You can add specializations of this function to your test to enable
599 verbose output.
600
601 \bold {Note:} The caller of toString() must delete the returned data
602 using \c{delete[]}. Your implementation should return a string
603 created with \c{new[]} or qstrdup().
604
605 Example:
606
607 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 16
608
609 The example above defines a toString() specialization for a class
610 called \c MyPoint. Whenever a comparison of two instances of \c
611 MyPoint fails, \l QCOMPARE() will call this function to output the
612 contents of \c MyPoint to the test log.
613
614 \sa QCOMPARE()
615*/
616
617/*!
618 \fn char *QTest::toString(const QLatin1String &string)
619 \overload
620
621 Returns a textual representation of the given \a string.
622*/
623
624/*!
625 \fn char *QTest::toString(const QString &string)
626 \overload
627
628 Returns a textual representation of the given \a string.
629*/
630
631/*!
632 \fn char *QTest::toString(const QByteArray &ba)
633 \overload
634
635 Returns a textual representation of the byte array \a ba.
636
637 \sa QTest::toHexRepresentation()
638*/
639
640/*!
641 \fn char *QTest::toString(const QTime &time)
642 \overload
643
644 Returns a textual representation of the given \a time.
645*/
646
647/*!
648 \fn char *QTest::toString(const QDate &date)
649 \overload
650
651 Returns a textual representation of the given \a date.
652*/
653
654/*!
655 \fn char *QTest::toString(const QDateTime &dateTime)
656 \overload
657
658 Returns a textual representation of the date and time specified by
659 \a dateTime.
660*/
661
662/*!
663 \fn char *QTest::toString(const QChar &character)
664 \overload
665
666 Returns a textual representation of the given \a character.
667*/
668
669/*!
670 \fn char *QTest::toString(const QPoint &point)
671 \overload
672
673 Returns a textual representation of the given \a point.
674*/
675
676/*!
677 \fn char *QTest::toString(const QSize &size)
678 \overload
679
680 Returns a textual representation of the given \a size.
681*/
682
683/*!
684 \fn char *QTest::toString(const QRect &rectangle)
685 \overload
686
687 Returns a textual representation of the given \a rectangle.
688*/
689
690/*!
691 \fn char *QTest::toString(const QUrl &url)
692 \since 4.4
693 \overload
694
695 Returns a textual representation of the given \a url.
696*/
697
698/*!
699 \fn char *QTest::toString(const QPointF &point)
700 \overload
701
702 Returns a textual representation of the given \a point.
703*/
704
705/*!
706 \fn char *QTest::toString(const QSizeF &size)
707 \overload
708
709 Returns a textual representation of the given \a size.
710*/
711
712/*!
713 \fn char *QTest::toString(const QRectF &rectangle)
714 \overload
715
716 Returns a textual representation of the given \a rectangle.
717*/
718
[561]719/*!
720 \fn char *QTest::toString(const QVariant &variant)
721 \overload
722
723 Returns a textual representation of the given \a variant.
724*/
725
[2]726/*! \fn void QTest::qWait(int ms)
727
728 Waits for \a ms milliseconds. While waiting, events will be processed and
729 your test will stay responsive to user interface events or network communication.
730
731 Example:
732 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 17
733
734 The code above will wait until the network server is responding for a
735 maximum of about 12.5 seconds.
736
737 \sa QTest::qSleep()
738*/
739
[561]740/*! \fn bool QTest::qWaitForWindowShown(QWidget *window)
741 \since 4.6
742
743 Waits until the \a window is shown in the screen. This is mainly useful for
744 asynchronous systems like X11, where a window will be mapped to screen some
745 time after being asked to show itself on the screen. Returns true.
746
747 Example:
748 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 24
749*/
750
751/*!
752 \class QTest::QTouchEventSequence
753 \inmodule QtTest
754 \since 4.6
755
756 \brief The QTouchEventSequence class is used to simulate a sequence of touch events.
757
758 To simulate a sequence of touch events on a specific device for a widget, call
759 QTest::touchEvent to create a QTouchEventSequence instance. Add touch events to
760 the sequence by calling press(), move(), release() and stationary(), and let the
761 instance run out of scope to commit the sequence to the event system.
[846]762
763 Example:
764 \snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 25
[561]765*/
766
767/*!
768 \fn QTest::QTouchEventSequence::~QTouchEventSequence()
769
770 Commits this sequence of touch events and frees allocated resources.
771*/
772
773/*!
774 \fn QTouchEventSequence &QTest::QTouchEventSequence::press(int touchId, const QPoint &pt, QWidget *widget)
775
776 Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns
777 a reference to this QTouchEventSequence.
778
779 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
780 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence.
781
782 Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId.
783*/
784
785/*!
786 \fn QTouchEventSequence &QTest::QTouchEventSequence::move(int touchId, const QPoint &pt, QWidget *widget)
787
788 Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns
789 a reference to this QTouchEventSequence.
790
791 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
792 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence.
793
794 Simulates that the user moved the finger identified by \a touchId.
795*/
796
797/*!
798 \fn QTouchEventSequence &QTest::QTouchEventSequence::release(int touchId, const QPoint &pt, QWidget *widget)
799
800 Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns
801 a reference to this QTouchEventSequence.
802
803 The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then
804 \a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence.
805
806 Simulates that the user lifted the finger identified by \a touchId.
807*/
808
809/*!
810 \fn QTouchEventSequence &QTest::QTouchEventSequence::stationary(int touchId)
811
812 Adds a stationary event for touchpoint \a touchId to this sequence and returns
813 a reference to this QTouchEventSequence.
814
815 Simulates that the user did not move the finger identified by \a touchId.
816*/
817
818/*!
819 \fn QTouchEventSequence QTest::touchEvent(QWidget *widget, QTouchEvent::DeviceType deviceType)
820
821 Creates and returns a QTouchEventSequence for the device \a deviceType to
822 simulate events for \a widget.
823
824 When adding touch events to the sequence, \a widget will also be used to translate
825 the position provided to screen coordinates, unless another widget is provided in the
826 respective calls to press(), move() etc.
827
828 The touch events are committed to the event system when the destructor of the
829 QTouchEventSequence is called (ie when the object returned runs out of scope).
830*/
831
[2]832namespace QTest
833{
834 static QObject *currentTestObject = 0;
835
[561]836 static struct TestFunction {
[2]837 TestFunction():function(0), data(0) {}
838 ~TestFunction() { delete [] data; }
839 int function;
840 char *data;
[561]841 } *testFuncs;
[2]842
843 /**
844 * Contains the count of test functions that was supplied
845 * on the command line, if any. Hence, if lastTestFuncIdx is
846 * more than zero, those functions should be run instead of
847 * all appearing in the test case.
848 */
849 static int lastTestFuncIdx = -1;
850
851 static int keyDelay = -1;
852 static int mouseDelay = -1;
853 static int eventDelay = -1;
854 static int keyVerbose = -1;
[846]855#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
856 static bool noCrashHandler = false;
857#endif
[2]858
[561]859void filter_unprintable(char *str)
860{
861 char *idx = str;
862 while (*idx) {
863 if (((*idx < 0x20 && *idx != '\n' && *idx != '\t') || *idx > 0x7e))
864 *idx = '?';
865 ++idx;
866 }
867}
868
[2]869/*! \internal
870 */
871int qt_snprintf(char *str, int size, const char *format, ...)
872{
873 va_list ap;
874 int res = 0;
875
876 va_start(ap, format);
877 qvsnprintf(str, size, format, ap);
878 va_end(ap);
879 str[size - 1] = '\0';
880
[561]881 filter_unprintable(str);
882
[2]883 return res;
884}
885
[846]886/*! \internal
887 Invoke a method of the object without generating warning if the method does not exist
888 */
889static void invokeMethod(QObject *obj, const char *methodName)
890{
891 const QMetaObject *metaObject = obj->metaObject();
892 int funcIndex = metaObject->indexOfMethod(methodName);
893 if (funcIndex >= 0) {
894 QMetaMethod method = metaObject->method(funcIndex);
895 method.invoke(obj, Qt::DirectConnection);
896 }
897}
898
[2]899bool Q_TESTLIB_EXPORT defaultKeyVerbose()
900{
901 if (keyVerbose == -1) {
902 keyVerbose = qgetenv("QTEST_KEYEVENT_VERBOSE").constData() ? 1 : 0;
903 }
904 return keyVerbose == 1;
905}
906
907int defaultEventDelay()
908{
909 if (eventDelay == -1) {
910 if (qgetenv("QTEST_EVENT_DELAY").constData())
911 eventDelay = atoi(qgetenv("QTEST_EVENT_DELAY"));
912 else
913 eventDelay = 0;
914 }
915 return eventDelay;
916}
917
918int Q_TESTLIB_EXPORT defaultMouseDelay()
919{
920 if (mouseDelay == -1) {
921 if (qgetenv("QTEST_MOUSEEVENT_DELAY").constData())
922 mouseDelay = atoi((qgetenv("QTEST_MOUSEEVENT_DELAY")));
923 else
924 mouseDelay = defaultEventDelay();
925 }
926 return mouseDelay;
927}
928
929int Q_TESTLIB_EXPORT defaultKeyDelay()
930{
931 if (keyDelay == -1) {
932 if (qgetenv("QTEST_KEYEVENT_DELAY").constData())
933 keyDelay = atoi(qgetenv("QTEST_KEYEVENT_DELAY").constData());
934 else
935 keyDelay = defaultEventDelay();
936 }
937 return keyDelay;
938}
939
940static bool isValidSlot(const QMetaMethod &sl)
941{
942 if (sl.access() != QMetaMethod::Private || !sl.parameterTypes().isEmpty()
943 || qstrlen(sl.typeName()) || sl.methodType() != QMetaMethod::Slot)
944 return false;
945 const char *sig = sl.signature();
946 int len = qstrlen(sig);
947 if (len < 2)
948 return false;
949 if (sig[len - 2] != '(' || sig[len - 1] != ')')
950 return false;
951 if (len > 7 && strcmp(sig + (len - 7), "_data()") == 0)
952 return false;
953 if (strcmp(sig, "initTestCase()") == 0 || strcmp(sig, "cleanupTestCase()") == 0
954 || strcmp(sig, "cleanup()") == 0 || strcmp(sig, "init()") == 0)
955 return false;
956 return true;
957}
958
959static void qPrintTestSlots()
960{
961 for (int i = 0; i < QTest::currentTestObject->metaObject()->methodCount(); ++i) {
962 QMetaMethod sl = QTest::currentTestObject->metaObject()->method(i);
963 if (isValidSlot(sl))
964 printf("%s\n", sl.signature());
965 }
966}
967
968static int qToInt(char *str)
969{
970 char *pEnd;
971 int l = (int)strtol(str, &pEnd, 10);
972 if (*pEnd != 0) {
973 printf("Invalid numeric parameter: '%s'\n", str);
974 exit(1);
975 }
976 return l;
977}
978
979static void qParseArgs(int argc, char *argv[])
980{
981 lastTestFuncIdx = -1;
982
983 const char *testOptions =
984 " options:\n"
985 " -functions : Returns a list of current testfunctions\n"
[561]986 " -xunitxml : Outputs results as XML XUnit document\n"
[2]987 " -xml : Outputs results as XML document\n"
988 " -lightxml : Outputs results as stream of XML tags\n"
[769]989 " -flush : Flushes the results\n"
[2]990 " -o filename: Writes all output into a file\n"
991 " -silent : Only outputs warnings and failures\n"
992 " -v1 : Print enter messages for each testfunction\n"
993 " -v2 : Also print out each QVERIFY/QCOMPARE/QTEST\n"
994 " -vs : Print every signal emitted\n"
995 " -eventdelay ms : Set default delay for mouse and keyboard simulation to ms milliseconds\n"
996 " -keydelay ms : Set default delay for keyboard simulation to ms milliseconds\n"
997 " -mousedelay ms : Set default delay for mouse simulation to ms milliseconds\n"
998 " -keyevent-verbose : Turn on verbose messages for keyboard simulation\n"
999 " -maxwarnings n : Sets the maximum amount of messages to output.\n"
1000 " 0 means unlimited, default: 2000\n"
[846]1001#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
1002 " -nocrashhandler : Disables the crash handler\n"
1003#endif
[2]1004 "\n"
1005 " Benchmark related options:\n"
1006#ifdef QTESTLIB_USE_VALGRIND
1007 " -callgrind : Use callgrind to time benchmarks\n"
1008#endif
1009#ifdef HAVE_TICK_COUNTER
1010 " -tickcounter : Use CPU tick counters to time benchmarks\n"
1011#endif
1012 " -eventcounter : Counts events received during benchmarks\n"
1013 " -minimumvalue n : Sets the minimum acceptable measurement value\n"
1014 " -iterations n : Sets the number of accumulation iterations.\n"
1015 " -median n : Sets the number of median iterations.\n"
1016 " -vb : Print out verbose benchmarking information.\n"
1017 "\n"
1018 " -help : This help\n";
1019
1020 for (int i = 1; i < argc; ++i) {
1021 if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0
1022 || strcmp(argv[i], "/?") == 0) {
1023 printf(" Usage: %s [options] [testfunction[:testdata]]...\n"
1024 " By default, all testfunctions will be run.\n\n"
1025 "%s", argv[0], testOptions);
1026 exit(0);
1027 } else if (strcmp(argv[i], "-functions") == 0) {
1028 qPrintTestSlots();
1029 exit(0);
[561]1030 } else if(strcmp(argv[i], "-xunitxml") == 0){
1031 QTestLog::setLogMode(QTestLog::XunitXML);
[2]1032 } else if (strcmp(argv[i], "-xml") == 0) {
1033 QTestLog::setLogMode(QTestLog::XML);
1034 } else if (strcmp(argv[i], "-lightxml") == 0) {
1035 QTestLog::setLogMode(QTestLog::LightXML);
[561]1036 }else if(strcmp(argv[i], "-flush") == 0){
1037 QTestLog::setFlushMode(QTestLog::FLushOn);
[2]1038 } else if (strcmp(argv[i], "-silent") == 0) {
1039 QTestLog::setVerboseLevel(-1);
1040 } else if (strcmp(argv[i], "-v1") == 0) {
1041 QTestLog::setVerboseLevel(1);
1042 } else if (strcmp(argv[i], "-v2") == 0) {
1043 QTestLog::setVerboseLevel(2);
1044 } else if (strcmp(argv[i], "-vs") == 0) {
1045 QSignalDumper::startDump();
1046 } else if (strcmp(argv[i], "-o") == 0) {
1047 if (i + 1 >= argc) {
1048 printf("-o needs an extra parameter specifying the filename\n");
1049 exit(1);
1050 } else {
1051 QTestLog::redirectOutput(argv[++i]);
1052 }
1053 } else if (strcmp(argv[i], "-eventdelay") == 0) {
1054 if (i + 1 >= argc) {
1055 printf("-eventdelay needs an extra parameter to indicate the delay(ms)\n");
1056 exit(1);
1057 } else {
1058 QTest::eventDelay = qToInt(argv[++i]);
1059 }
1060 } else if (strcmp(argv[i], "-keydelay") == 0) {
1061 if (i + 1 >= argc) {
1062 printf("-keydelay needs an extra parameter to indicate the delay(ms)\n");
1063 exit(1);
1064 } else {
1065 QTest::keyDelay = qToInt(argv[++i]);
1066 }
1067 } else if (strcmp(argv[i], "-mousedelay") == 0) {
1068 if (i + 1 >= argc) {