author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtTest module 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 |
#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 <errno.h> |
|
75 |
#include <signal.h> |
|
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 |
||
89 |
QT_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 |
||
301 |
Implements a main() function that instantiates a QApplication object and |
|
302 |
the \a TestClass, and executes all tests in the order they were defined. |
|
303 |
Use this macro to build stand-alone executables. |
|
304 |
||
305 |
\bold {Note:} On platforms that have keypad navigation enabled by default (eg: Symbian), |
|
306 |
this macro will forcfully disable it to simplify the usage of key events when writing |
|
307 |
autotests. If you wish to write a test case that uses keypad navigation, you should |
|
308 |
enable it either in the \c {initTestCase()} or \c {init()} functions of your test case. |
|
309 |
||
310 |
Example: |
|
311 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 11 |
|
312 |
||
313 |
\sa QTEST_APPLESS_MAIN(), QTest::qExec(), QApplication::setNavigationMode() |
|
314 |
*/ |
|
315 |
||
316 |
/*! \macro QTEST_APPLESS_MAIN(TestClass) |
|
317 |
||
318 |
\relates QTest |
|
319 |
||
320 |
Implements a main() function that executes all tests in \a TestClass. |
|
321 |
||
322 |
Behaves like \l QTEST_MAIN(), but doesn't instantiate a QApplication |
|
323 |
object. Use this macro for really simple stand-alone non-GUI tests. |
|
324 |
||
325 |
\sa QTEST_MAIN() |
|
326 |
*/ |
|
327 |
||
328 |
/*! \macro QTEST_NOOP_MAIN() |
|
329 |
||
330 |
\relates QTest |
|
331 |
||
332 |
Implements a main() function with a test class that does absolutely nothing. |
|
333 |
Use this macro to create a test that produces valid test output but just |
|
334 |
doesn't execute any test, for example in conditional compilations: |
|
335 |
||
336 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 12 |
|
337 |
||
338 |
\sa QTEST_MAIN() |
|
339 |
*/ |
|
340 |
||
341 |
/*! |
|
342 |
\macro QBENCHMARK |
|
343 |
||
344 |
\relates QTest |
|
345 |
||
346 |
This macro is used to measure the performance of code within a test. |
|
347 |
The code to be benchmarked is contained within a code block following |
|
348 |
this macro. |
|
349 |
||
350 |
For example: |
|
351 |
||
352 |
\snippet examples/qtestlib/tutorial5/benchmarking.cpp 0 |
|
353 |
||
354 |
\sa {QTestLib Manual#Creating a Benchmark}{Creating a Benchmark}, |
|
355 |
{Chapter 5: Writing a Benchmark}{Writing a Benchmark} |
|
356 |
*/ |
|
357 |
||
358 |
/*! |
|
359 |
\macro QBENCHMARK_ONCE |
|
360 |
\since 4.6 |
|
361 |
||
362 |
\relates QTest |
|
363 |
||
364 |
\brief The QBENCHMARK_ONCE macro is for measuring performance of a |
|
365 |
code block by running it once. |
|
366 |
||
367 |
This macro is used to measure the performance of code within a test. |
|
368 |
The code to be benchmarked is contained within a code block following |
|
369 |
this macro. |
|
370 |
||
371 |
Unlike QBENCHMARK, the contents of the contained code block is only run |
|
372 |
once. The elapsed time will be reported as "0" if it's to short to |
|
373 |
be measured by the selected backend. (Use) |
|
374 |
||
375 |
\sa {QTestLib Manual#Creating a Benchmark}{Creating a Benchmark}, |
|
376 |
{Chapter 5: Writing a Benchmark}{Writing a Benchmark} |
|
377 |
*/ |
|
378 |
||
379 |
||
380 |
||
381 |
/*! \enum QTest::SkipMode |
|
382 |
||
383 |
This enum describes the modes for skipping tests during execution |
|
384 |
of the test data. |
|
385 |
||
386 |
\value SkipSingle Skips the current entry in the test table; continues |
|
387 |
execution of all the other entries in the table. |
|
388 |
||
389 |
\value SkipAll Skips all the entries in the test table; the test won't |
|
390 |
be executed further. |
|
391 |
||
392 |
\sa QSKIP() |
|
393 |
*/ |
|
394 |
||
395 |
/*! \enum QTest::TestFailMode |
|
396 |
||
397 |
This enum describes the modes for handling an expected failure of the |
|
398 |
\l QVERIFY() or \l QCOMPARE() macros. |
|
399 |
||
400 |
\value Abort Aborts the execution of the test. Use this mode when it |
|
401 |
doesn't make sense to execute the test any further after the |
|
402 |
expected failure. |
|
403 |
||
404 |
\value Continue Continues execution of the test after the expected failure. |
|
405 |
||
406 |
\sa QEXPECT_FAIL() |
|
407 |
*/ |
|
408 |
||
409 |
/*! \enum QTest::KeyAction |
|
410 |
||
411 |
This enum describes possible actions for key handling. |
|
412 |
||
413 |
\value Press The key is pressed. |
|
414 |
\value Release The key is released. |
|
415 |
\value Click The key is clicked (pressed and released). |
|
416 |
*/ |
|
417 |
||
418 |
/*! \enum QTest::MouseAction |
|
419 |
||
420 |
This enum describes possible actions for mouse handling. |
|
421 |
||
422 |
\value MousePress A mouse button is pressed. |
|
423 |
\value MouseRelease A mouse button is released. |
|
424 |
\value MouseClick A mouse button is clicked (pressed and released). |
|
425 |
\value MouseDClick A mouse button is double clicked (pressed and released twice). |
|
426 |
\value MouseMove The mouse pointer has moved. |
|
427 |
*/ |
|
428 |
||
429 |
/*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
430 |
||
431 |
\overload |
|
432 |
||
433 |
Simulates clicking of \a key with an optional \a modifier on a \a widget. |
|
434 |
If \a delay is larger than 0, the test will wait for \a delay milliseconds. |
|
435 |
||
436 |
Example: |
|
437 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 13 |
|
438 |
||
439 |
The example above simulates clicking \c a on \c myWidget without |
|
440 |
any keyboard modifiers and without delay of the test. |
|
441 |
||
442 |
\sa QTest::keyClicks() |
|
443 |
*/ |
|
444 |
||
445 |
/*! \fn void QTest::keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
446 |
||
447 |
Simulates clicking of \a key with an optional \a modifier on a \a widget. |
|
448 |
If \a delay is larger than 0, the test will wait for \a delay milliseconds. |
|
449 |
||
450 |
Examples: |
|
451 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 14 |
|
452 |
||
453 |
The first example above simulates clicking the \c escape key on \c |
|
454 |
myWidget without any keyboard modifiers and without delay. The |
|
455 |
second example simulates clicking \c shift-escape on \c myWidget |
|
456 |
with a following 200 ms delay of the test. |
|
457 |
||
458 |
\sa QTest::keyClicks() |
|
459 |
*/ |
|
460 |
||
461 |
/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
462 |
||
463 |
Sends a Qt key event to \a widget with the given \a key and an associated \a action. |
|
464 |
Optionally, a keyboard \a modifier can be specified, as well as a \a delay |
|
465 |
(in milliseconds) of the test before sending the event. |
|
466 |
*/ |
|
467 |
||
468 |
/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
469 |
||
470 |
\overload |
|
471 |
||
472 |
Sends a Qt key event to \a widget with the given key \a ascii and an associated \a action. |
|
473 |
Optionally, a keyboard \a modifier can be specified, as well as a \a delay |
|
474 |
(in milliseconds) of the test before sending the event. |
|
475 |
||
476 |
*/ |
|
477 |
||
478 |
/*! \fn void QTest::keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
479 |
||
480 |
Simulates pressing a \a key with an optional \a modifier on a \a widget. If \a delay |
|
481 |
is larger than 0, the test will wait for \a delay milliseconds. |
|
482 |
||
483 |
\bold {Note:} At some point you should release the key using \l keyRelease(). |
|
484 |
||
485 |
\sa QTest::keyRelease(), QTest::keyClick() |
|
486 |
*/ |
|
487 |
||
488 |
/*! \fn void QTest::keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
489 |
||
490 |
\overload |
|
491 |
||
492 |
Simulates pressing a \a key with an optional \a modifier on a \a widget. |
|
493 |
If \a delay is larger than 0, the test will wait for \a delay milliseconds. |
|
494 |
||
495 |
\bold {Note:} At some point you should release the key using \l keyRelease(). |
|
496 |
||
497 |
\sa QTest::keyRelease(), QTest::keyClick() |
|
498 |
*/ |
|
499 |
||
500 |
/*! \fn void QTest::keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
501 |
||
502 |
Simulates releasing a \a key with an optional \a modifier on a \a widget. |
|
503 |
If \a delay is larger than 0, the test will wait for \a delay milliseconds. |
|
504 |
||
505 |
\sa QTest::keyPress(), QTest::keyClick() |
|
506 |
*/ |
|
507 |
||
508 |
/*! \fn void QTest::keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
509 |
||
510 |
\overload |
|
511 |
||
512 |
Simulates releasing a \a key with an optional \a modifier on a \a widget. |
|
513 |
If \a delay is larger than 0, the test will wait for \a delay milliseconds. |
|
514 |
||
515 |
\sa QTest::keyClick() |
|
516 |
*/ |
|
517 |
||
518 |
||
519 |
/*! \fn void QTest::keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
|
520 |
||
521 |
Simulates clicking a \a sequence of keys on a \a |
|
522 |
widget. Optionally, a keyboard \a modifier can be specified as |
|
523 |
well as a \a delay (in milliseconds) of the test before each key |
|
524 |
click. |
|
525 |
||
526 |
Example: |
|
527 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 15 |
|
528 |
||
529 |
The example above simulates clicking the sequence of keys |
|
530 |
representing "hello world" on \c myWidget without any keyboard |
|
531 |
modifiers and without delay of the test. |
|
532 |
||
533 |
\sa QTest::keyClick() |
|
534 |
*/ |
|
535 |
||
536 |
/*! \fn void QTest::mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1) |
|
537 |
||
538 |
Simulates pressing a mouse \a button with an optional \a modifier |
|
539 |
on a \a widget. The position is defined by \a pos; the default |
|
540 |
position is the center of the widget. If \a delay is specified, |
|
541 |
the test will wait for the specified amount of milliseconds before |
|
542 |
the press. |
|
543 |
||
544 |
\sa QTest::mouseRelease(), QTest::mouseClick() |
|
545 |
*/ |
|
546 |
||
547 |
/*! \fn void QTest::mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1) |
|
548 |
||
549 |
Simulates releasing a mouse \a button with an optional \a modifier |
|
550 |
on a \a widget. The position of the release is defined by \a pos; |
|
551 |
the default position is the center of the widget. If \a delay is |
|
552 |
specified, the test will wait for the specified amount of |
|
553 |
milliseconds before releasing the button. |
|
554 |
||
555 |
\sa QTest::mousePress(), QTest::mouseClick() |
|
556 |
*/ |
|
557 |
||
558 |
/*! \fn void QTest::mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1) |
|
559 |
||
560 |
Simulates clicking a mouse \a button with an optional \a modifier |
|
561 |
on a \a widget. The position of the click is defined by \a pos; |
|
562 |
the default position is the center of the widget. If \a delay is |
|
563 |
specified, the test will wait for the specified amount of |
|
564 |
milliseconds before pressing and before releasing the button. |
|
565 |
||
566 |
\sa QTest::mousePress(), QTest::mouseRelease() |
|
567 |
*/ |
|
568 |
||
569 |
/*! \fn void QTest::mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay=-1) |
|
570 |
||
571 |
Simulates double clicking a mouse \a button with an optional \a |
|
572 |
modifier on a \a widget. The position of the click is defined by |
|
573 |
\a pos; the default position is the center of the widget. If \a |
|
574 |
delay is specified, the test will wait for the specified amount of |
|
575 |
milliseconds before each press and release. |
|
576 |
||
577 |
\sa QTest::mouseClick() |
|
578 |
*/ |
|
579 |
||
580 |
/*! \fn void QTest::mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay=-1) |
|
581 |
||
582 |
Moves the mouse pointer to a \a widget. If \a pos is not |
|
583 |
specified, the mouse pointer moves to the center of the widget. If |
|
584 |
a \a delay (in milliseconds) is given, the test will wait before |
|
585 |
moving the mouse pointer. |
|
586 |
*/ |
|
587 |
||
588 |
/*! |
|
589 |
\fn char *QTest::toString(const T &value) |
|
590 |
||
591 |
Returns a textual representation of \a value. This function is used by |
|
592 |
\l QCOMPARE() to output verbose information in case of a test failure. |
|
593 |
||
594 |
You can add specializations of this function to your test to enable |
|
595 |
verbose output. |
|
596 |
||
597 |
\bold {Note:} The caller of toString() must delete the returned data |
|
598 |
using \c{delete[]}. Your implementation should return a string |
|
599 |
created with \c{new[]} or qstrdup(). |
|
600 |
||
601 |
Example: |
|
602 |
||
603 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 16 |
|
604 |
||
605 |
The example above defines a toString() specialization for a class |
|
606 |
called \c MyPoint. Whenever a comparison of two instances of \c |
|
607 |
MyPoint fails, \l QCOMPARE() will call this function to output the |
|
608 |
contents of \c MyPoint to the test log. |
|
609 |
||
610 |
\sa QCOMPARE() |
|
611 |
*/ |
|
612 |
||
613 |
/*! |
|
614 |
\fn char *QTest::toString(const QLatin1String &string) |
|
615 |
\overload |
|
616 |
||
617 |
Returns a textual representation of the given \a string. |
|
618 |
*/ |
|
619 |
||
620 |
/*! |
|
621 |
\fn char *QTest::toString(const QString &string) |
|
622 |
\overload |
|
623 |
||
624 |
Returns a textual representation of the given \a string. |
|
625 |
*/ |
|
626 |
||
627 |
/*! |
|
628 |
\fn char *QTest::toString(const QByteArray &ba) |
|
629 |
\overload |
|
630 |
||
631 |
Returns a textual representation of the byte array \a ba. |
|
632 |
||
633 |
\sa QTest::toHexRepresentation() |
|
634 |
*/ |
|
635 |
||
636 |
/*! |
|
637 |
\fn char *QTest::toString(const QTime &time) |
|
638 |
\overload |
|
639 |
||
640 |
Returns a textual representation of the given \a time. |
|
641 |
*/ |
|
642 |
||
643 |
/*! |
|
644 |
\fn char *QTest::toString(const QDate &date) |
|
645 |
\overload |
|
646 |
||
647 |
Returns a textual representation of the given \a date. |
|
648 |
*/ |
|
649 |
||
650 |
/*! |
|
651 |
\fn char *QTest::toString(const QDateTime &dateTime) |
|
652 |
\overload |
|
653 |
||
654 |
Returns a textual representation of the date and time specified by |
|
655 |
\a dateTime. |
|
656 |
*/ |
|
657 |
||
658 |
/*! |
|
659 |
\fn char *QTest::toString(const QChar &character) |
|
660 |
\overload |
|
661 |
||
662 |
Returns a textual representation of the given \a character. |
|
663 |
*/ |
|
664 |
||
665 |
/*! |
|
666 |
\fn char *QTest::toString(const QPoint &point) |
|
667 |
\overload |
|
668 |
||
669 |
Returns a textual representation of the given \a point. |
|
670 |
*/ |
|
671 |
||
672 |
/*! |
|
673 |
\fn char *QTest::toString(const QSize &size) |
|
674 |
\overload |
|
675 |
||
676 |
Returns a textual representation of the given \a size. |
|
677 |
*/ |
|
678 |
||
679 |
/*! |
|
680 |
\fn char *QTest::toString(const QRect &rectangle) |
|
681 |
\overload |
|
682 |
||
683 |
Returns a textual representation of the given \a rectangle. |
|
684 |
*/ |
|
685 |
||
686 |
/*! |
|
687 |
\fn char *QTest::toString(const QUrl &url) |
|
688 |
\since 4.4 |
|
689 |
\overload |
|
690 |
||
691 |
Returns a textual representation of the given \a url. |
|
692 |
*/ |
|
693 |
||
694 |
/*! |
|
695 |
\fn char *QTest::toString(const QPointF &point) |
|
696 |
\overload |
|
697 |
||
698 |
Returns a textual representation of the given \a point. |
|
699 |
*/ |
|
700 |
||
701 |
/*! |
|
702 |
\fn char *QTest::toString(const QSizeF &size) |
|
703 |
\overload |
|
704 |
||
705 |
Returns a textual representation of the given \a size. |
|
706 |
*/ |
|
707 |
||
708 |
/*! |
|
709 |
\fn char *QTest::toString(const QRectF &rectangle) |
|
710 |
\overload |
|
711 |
||
712 |
Returns a textual representation of the given \a rectangle. |
|
713 |
*/ |
|
714 |
||
715 |
/*! |
|
716 |
\fn char *QTest::toString(const QVariant &variant) |
|
717 |
\overload |
|
718 |
||
719 |
Returns a textual representation of the given \a variant. |
|
720 |
*/ |
|
721 |
||
722 |
/*! \fn void QTest::qWait(int ms) |
|
723 |
||
724 |
Waits for \a ms milliseconds. While waiting, events will be processed and |
|
725 |
your test will stay responsive to user interface events or network communication. |
|
726 |
||
727 |
Example: |
|
728 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 17 |
|
729 |
||
730 |
The code above will wait until the network server is responding for a |
|
731 |
maximum of about 12.5 seconds. |
|
732 |
||
733 |
\sa QTest::qSleep() |
|
734 |
*/ |
|
735 |
||
736 |
/*! \fn bool QTest::qWaitForWindowShown(QWidget *window) |
|
737 |
\since 4.6 |
|
738 |
||
739 |
Waits until the \a window is shown in the screen. This is mainly useful for |
|
740 |
asynchronous systems like X11, where a window will be mapped to screen some |
|
741 |
time after being asked to show itself on the screen. Returns true. |
|
742 |
||
743 |
Example: |
|
744 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 24 |
|
745 |
*/ |
|
746 |
||
747 |
/*! |
|
748 |
\class QTest::QTouchEventSequence |
|
749 |
\inmodule QtTest |
|
750 |
\since 4.6 |
|
751 |
||
752 |
\brief The QTouchEventSequence class is used to simulate a sequence of touch events. |
|
753 |
||
754 |
To simulate a sequence of touch events on a specific device for a widget, call |
|
755 |
QTest::touchEvent to create a QTouchEventSequence instance. Add touch events to |
|
756 |
the sequence by calling press(), move(), release() and stationary(), and let the |
|
757 |
instance run out of scope to commit the sequence to the event system. |
|
758 |
*/ |
|
759 |
||
760 |
/*! |
|
761 |
\fn QTest::QTouchEventSequence::~QTouchEventSequence() |
|
762 |
||
763 |
Commits this sequence of touch events and frees allocated resources. |
|
764 |
*/ |
|
765 |
||
766 |
/*! |
|
767 |
\fn QTouchEventSequence &QTest::QTouchEventSequence::press(int touchId, const QPoint &pt, QWidget *widget) |
|
768 |
||
769 |
Adds a press event for touchpoint \a touchId at position \a pt to this sequence and returns |
|
770 |
a reference to this QTouchEventSequence. |
|
771 |
||
772 |
The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then |
|
773 |
\a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence. |
|
774 |
||
775 |
Simulates that the user pressed the touch screen or pad with the finger identified by \a touchId. |
|
776 |
*/ |
|
777 |
||
778 |
/*! |
|
779 |
\fn QTouchEventSequence &QTest::QTouchEventSequence::move(int touchId, const QPoint &pt, QWidget *widget) |
|
780 |
||
781 |
Adds a move event for touchpoint \a touchId at position \a pt to this sequence and returns |
|
782 |
a reference to this QTouchEventSequence. |
|
783 |
||
784 |
The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then |
|
785 |
\a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence. |
|
786 |
||
787 |
Simulates that the user moved the finger identified by \a touchId. |
|
788 |
*/ |
|
789 |
||
790 |
/*! |
|
791 |
\fn QTouchEventSequence &QTest::QTouchEventSequence::release(int touchId, const QPoint &pt, QWidget *widget) |
|
792 |
||
793 |
Adds a release event for touchpoint \a touchId at position \a pt to this sequence and returns |
|
794 |
a reference to this QTouchEventSequence. |
|
795 |
||
796 |
The position \a pt is interpreted as relative to \a widget. If \a widget is the null pointer, then |
|
797 |
\a pt is interpreted as relative to the widget provided when instantiating this QTouchEventSequence. |
|
798 |
||
799 |
Simulates that the user lifted the finger identified by \a touchId. |
|
800 |
*/ |
|
801 |
||
802 |
/*! |
|
803 |
\fn QTouchEventSequence &QTest::QTouchEventSequence::stationary(int touchId) |
|
804 |
||
805 |
Adds a stationary event for touchpoint \a touchId to this sequence and returns |
|
806 |
a reference to this QTouchEventSequence. |
|
807 |
||
808 |
Simulates that the user did not move the finger identified by \a touchId. |
|
809 |
*/ |
|
810 |
||
811 |
/*! |
|
812 |
\fn QTouchEventSequence QTest::touchEvent(QWidget *widget, QTouchEvent::DeviceType deviceType) |
|
813 |
||
814 |
Creates and returns a QTouchEventSequence for the device \a deviceType to |
|
815 |
simulate events for \a widget. |
|
816 |
||
817 |
When adding touch events to the sequence, \a widget will also be used to translate |
|
818 |
the position provided to screen coordinates, unless another widget is provided in the |
|
819 |
respective calls to press(), move() etc. |
|
820 |
||
821 |
The touch events are committed to the event system when the destructor of the |
|
822 |
QTouchEventSequence is called (ie when the object returned runs out of scope). |
|
823 |
*/ |
|
824 |
||
825 |
namespace QTest |
|
826 |
{ |
|
827 |
static QObject *currentTestObject = 0; |
|
828 |
||
829 |
static struct TestFunction { |
|
830 |
TestFunction():function(0), data(0) {} |
|
831 |
~TestFunction() { delete [] data; } |
|
832 |
int function; |
|
833 |
char *data; |
|
834 |
} *testFuncs; |
|
835 |
||
836 |
/** |
|
837 |
* Contains the count of test functions that was supplied |
|
838 |
* on the command line, if any. Hence, if lastTestFuncIdx is |
|
839 |
* more than zero, those functions should be run instead of |
|
840 |
* all appearing in the test case. |
|
841 |
*/ |
|
842 |
static int lastTestFuncIdx = -1; |
|
843 |
||
844 |
static int keyDelay = -1; |
|
845 |
static int mouseDelay = -1; |
|
846 |
static int eventDelay = -1; |
|
847 |
static int keyVerbose = -1; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
848 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
849 |
static bool noCrashHandler = false; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
850 |
#endif |
0 | 851 |
|
852 |
void filter_unprintable(char *str) |
|
853 |
{ |
|
854 |
char *idx = str; |
|
855 |
while (*idx) { |
|
856 |
if (((*idx < 0x20 && *idx != '\n' && *idx != '\t') || *idx > 0x7e)) |
|
857 |
*idx = '?'; |
|
858 |
++idx; |
|
859 |
} |
|
860 |
} |
|
861 |
||
862 |
/*! \internal |
|
863 |
*/ |
|
864 |
int qt_snprintf(char *str, int size, const char *format, ...) |
|
865 |
{ |
|
866 |
va_list ap; |
|
867 |
int res = 0; |
|
868 |
||
869 |
va_start(ap, format); |
|
870 |
qvsnprintf(str, size, format, ap); |
|
871 |
va_end(ap); |
|
872 |
str[size - 1] = '\0'; |
|
873 |
||
874 |
filter_unprintable(str); |
|
875 |
||
876 |
return res; |
|
877 |
} |
|
878 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
879 |
/*! \internal |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
880 |
Invoke a method of the object without generating warning if the method does not exist |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
881 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
882 |
static void invokeMethod(QObject *obj, const char *methodName) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
883 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
884 |
const QMetaObject *metaObject = obj->metaObject(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
885 |
int funcIndex = metaObject->indexOfMethod(methodName); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
886 |
if (funcIndex >= 0) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
887 |
QMetaMethod method = metaObject->method(funcIndex); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
888 |
method.invoke(obj, Qt::DirectConnection); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
889 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
890 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
891 |
|
0 | 892 |
bool Q_TESTLIB_EXPORT defaultKeyVerbose() |
893 |
{ |
|
894 |
if (keyVerbose == -1) { |
|
895 |
keyVerbose = qgetenv("QTEST_KEYEVENT_VERBOSE").constData() ? 1 : 0; |
|
896 |
} |
|
897 |
return keyVerbose == 1; |
|
898 |
} |
|
899 |
||
900 |
int defaultEventDelay() |
|
901 |
{ |
|
902 |
if (eventDelay == -1) { |
|
903 |
if (qgetenv("QTEST_EVENT_DELAY").constData()) |
|
904 |
eventDelay = atoi(qgetenv("QTEST_EVENT_DELAY")); |
|
905 |
else |
|
906 |
eventDelay = 0; |
|
907 |
} |
|
908 |
return eventDelay; |
|
909 |
} |
|
910 |
||
911 |
int Q_TESTLIB_EXPORT defaultMouseDelay() |
|
912 |
{ |
|
913 |
if (mouseDelay == -1) { |
|
914 |
if (qgetenv("QTEST_MOUSEEVENT_DELAY").constData()) |
|
915 |
mouseDelay = atoi((qgetenv("QTEST_MOUSEEVENT_DELAY"))); |
|
916 |
else |
|
917 |
mouseDelay = defaultEventDelay(); |
|
918 |
} |
|
919 |
return mouseDelay; |
|
920 |
} |
|
921 |
||
922 |
int Q_TESTLIB_EXPORT defaultKeyDelay() |
|
923 |
{ |
|
924 |
if (keyDelay == -1) { |
|
925 |
if (qgetenv("QTEST_KEYEVENT_DELAY").constData()) |
|
926 |
keyDelay = atoi(qgetenv("QTEST_KEYEVENT_DELAY").constData()); |
|
927 |
else |
|
928 |
keyDelay = defaultEventDelay(); |
|
929 |
} |
|
930 |
return keyDelay; |
|
931 |
} |
|
932 |
||
933 |
static bool isValidSlot(const QMetaMethod &sl) |
|
934 |
{ |
|
935 |
if (sl.access() != QMetaMethod::Private || !sl.parameterTypes().isEmpty() |
|
936 |
|| qstrlen(sl.typeName()) || sl.methodType() != QMetaMethod::Slot) |
|
937 |
return false; |
|
938 |
const char *sig = sl.signature(); |
|
939 |
int len = qstrlen(sig); |
|
940 |
if (len < 2) |
|
941 |
return false; |
|
942 |
if (sig[len - 2] != '(' || sig[len - 1] != ')') |
|
943 |
return false; |
|
944 |
if (len > 7 && strcmp(sig + (len - 7), "_data()") == 0) |
|
945 |
return false; |
|
946 |
if (strcmp(sig, "initTestCase()") == 0 || strcmp(sig, "cleanupTestCase()") == 0 |
|
947 |
|| strcmp(sig, "cleanup()") == 0 || strcmp(sig, "init()") == 0) |
|
948 |
return false; |
|
949 |
return true; |
|
950 |
} |
|
951 |
||
952 |
static void qPrintTestSlots() |
|
953 |
{ |
|
954 |
for (int i = 0; i < QTest::currentTestObject->metaObject()->methodCount(); ++i) { |
|
955 |
QMetaMethod sl = QTest::currentTestObject->metaObject()->method(i); |
|
956 |
if (isValidSlot(sl)) |
|
957 |
printf("%s\n", sl.signature()); |
|
958 |
} |
|
959 |
} |
|
960 |
||
961 |
static int qToInt(char *str) |
|
962 |
{ |
|
963 |
char *pEnd; |
|
964 |
int l = (int)strtol(str, &pEnd, 10); |
|
965 |
if (*pEnd != 0) { |
|
966 |
printf("Invalid numeric parameter: '%s'\n", str); |
|
967 |
exit(1); |
|
968 |
} |
|
969 |
return l; |
|
970 |
} |
|
971 |
||
972 |
static void qParseArgs(int argc, char *argv[]) |
|
973 |
{ |
|
974 |
lastTestFuncIdx = -1; |
|
975 |
||
976 |
const char *testOptions = |
|
977 |
" options:\n" |
|
978 |
" -functions : Returns a list of current testfunctions\n" |
|
979 |
" -xunitxml : Outputs results as XML XUnit document\n" |
|
980 |
" -xml : Outputs results as XML document\n" |
|
981 |
" -lightxml : Outputs results as stream of XML tags\n" |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
982 |
" -flush : Flushes the results\n" |
0 | 983 |
" -o filename: Writes all output into a file\n" |
984 |
" -silent : Only outputs warnings and failures\n" |
|
985 |
" -v1 : Print enter messages for each testfunction\n" |
|
986 |
" -v2 : Also print out each QVERIFY/QCOMPARE/QTEST\n" |
|
987 |
" -vs : Print every signal emitted\n" |
|
988 |
" -eventdelay ms : Set default delay for mouse and keyboard simulation to ms milliseconds\n" |
|
989 |
" -keydelay ms : Set default delay for keyboard simulation to ms milliseconds\n" |
|
990 |
" -mousedelay ms : Set default delay for mouse simulation to ms milliseconds\n" |
|
991 |
" -keyevent-verbose : Turn on verbose messages for keyboard simulation\n" |
|
992 |
" -maxwarnings n : Sets the maximum amount of messages to output.\n" |
|
993 |
" 0 means unlimited, default: 2000\n" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
994 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
995 |
" -nocrashhandler : Disables the crash handler\n" |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
996 |
#endif |
0 | 997 |
"\n" |
998 |
" Benchmark related options:\n" |
|
999 |
#ifdef QTESTLIB_USE_VALGRIND |
|
1000 |
" -callgrind : Use callgrind to time benchmarks\n" |
|
1001 |
#endif |
|
1002 |
#ifdef HAVE_TICK_COUNTER |
|
1003 |
" -tickcounter : Use CPU tick counters to time benchmarks\n" |
|
1004 |
#endif |
|
1005 |
" -eventcounter : Counts events received during benchmarks\n" |
|
1006 |
" -minimumvalue n : Sets the minimum acceptable measurement value\n" |
|
1007 |
" -iterations n : Sets the number of accumulation iterations.\n" |
|
1008 |
" -median n : Sets the number of median iterations.\n" |
|
1009 |
" -vb : Print out verbose benchmarking information.\n" |
|
1010 |
"\n" |
|
1011 |
" -help : This help\n"; |
|
1012 |
||
1013 |
for (int i = 1; i < argc; ++i) { |
|
1014 |
if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 |
|
1015 |
|| strcmp(argv[i], "/?") == 0) { |
|
1016 |
printf(" Usage: %s [options] [testfunction[:testdata]]...\n" |
|
1017 |
" By default, all testfunctions will be run.\n\n" |
|
1018 |
"%s", argv[0], testOptions); |
|
1019 |
exit(0); |
|
1020 |
} else if (strcmp(argv[i], "-functions") == 0) { |
|
1021 |
qPrintTestSlots(); |
|
1022 |
exit(0); |
|
1023 |
} else if(strcmp(argv[i], "-xunitxml") == 0){ |
|
1024 |
QTestLog::setLogMode(QTestLog::XunitXML); |
|
1025 |
} else if (strcmp(argv[i], "-xml") == 0) { |
|
1026 |
QTestLog::setLogMode(QTestLog::XML); |
|
1027 |
} else if (strcmp(argv[i], "-lightxml") == 0) { |
|
1028 |
QTestLog::setLogMode(QTestLog::LightXML); |
|
1029 |
}else if(strcmp(argv[i], "-flush") == 0){ |
|
1030 |
QTestLog::setFlushMode(QTestLog::FLushOn); |
|
1031 |
} else if (strcmp(argv[i], "-silent") == 0) { |
|
1032 |
QTestLog::setVerboseLevel(-1); |
|
1033 |
} else if (strcmp(argv[i], "-v1") == 0) { |
|
1034 |
QTestLog::setVerboseLevel(1); |
|
1035 |
} else if (strcmp(argv[i], "-v2") == 0) { |
|
1036 |
QTestLog::setVerboseLevel(2); |
|
1037 |
} else if (strcmp(argv[i], "-vs") == 0) { |
|
1038 |
QSignalDumper::startDump(); |
|
1039 |
} else if (strcmp(argv[i], "-o") == 0) { |
|
1040 |
if (i + 1 >= argc) { |
|
1041 |
printf("-o needs an extra parameter specifying the filename\n"); |
|
1042 |
exit(1); |
|
1043 |
} else { |
|
1044 |
QTestLog::redirectOutput(argv[++i]); |
|
1045 |
} |
|
1046 |
} else if (strcmp(argv[i], "-eventdelay") == 0) { |
|
1047 |
if (i + 1 >= argc) { |
|
1048 |
printf("-eventdelay needs an extra parameter to indicate the delay(ms)\n"); |
|
1049 |
exit(1); |
|
1050 |
} else { |
|
1051 |
QTest::eventDelay = qToInt(argv[++i]); |
|
1052 |
} |
|
1053 |
} else if (strcmp(argv[i], "-keydelay") == 0) { |
|
1054 |
if (i + 1 >= argc) { |
|
1055 |
printf("-keydelay needs an extra parameter to indicate the delay(ms)\n"); |
|
1056 |
exit(1); |
|
1057 |
} else { |
|
1058 |
QTest::keyDelay = qToInt(argv[++i]); |
|
1059 |
} |
|
1060 |
} else if (strcmp(argv[i], "-mousedelay") == 0) { |
|
1061 |
if (i + 1 >= argc) { |
|
1062 |
printf("-mousedelay needs an extra parameter to indicate the delay(ms)\n"); |
|
1063 |
exit(1); |
|
1064 |
} else { |
|
1065 |
QTest::mouseDelay = qToInt(argv[++i]); |
|
1066 |
} |
|
1067 |
} else if (strcmp(argv[i], "-maxwarnings") == 0) { |
|
1068 |
if (i + 1 >= argc) { |
|
1069 |
printf("-maxwarnings needs an extra parameter with the amount of warnings\n"); |
|
1070 |
exit(1); |
|
1071 |
} else { |
|
1072 |
QTestLog::setMaxWarnings(qToInt(argv[++i])); |
|
1073 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1074 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1075 |
} else if (strcmp(argv[i], "-nocrashhandler") == 0) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1076 |
QTest::noCrashHandler = true; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1077 |
#endif |
0 | 1078 |
} else if (strcmp(argv[i], "-keyevent-verbose") == 0) { |
1079 |
QTest::keyVerbose = 1; |
|
1080 |
#ifdef QTESTLIB_USE_VALGRIND |
|
1081 |
} else if (strcmp(argv[i], "-callgrind") == 0) { |
|
1082 |
if (QBenchmarkValgrindUtils::haveValgrind()) |
|
1083 |
if (QFileInfo(QDir::currentPath()).isWritable()) { |
|
1084 |
QBenchmarkGlobalData::current->setMode(QBenchmarkGlobalData::CallgrindParentProcess); |
|
1085 |
} else { |
|
1086 |
printf("WARNING: Current directory not writable. Using the walltime measurer.\n"); |
|
1087 |
} |
|
1088 |
else { |
|
1089 |
printf("WARNING: Valgrind not found or too old. Make sure it is installed and in your path. " |
|
1090 |
"Using the walltime measurer.\n"); |
|
1091 |
} |
|
1092 |
} else if (strcmp(argv[i], "-callgrindchild") == 0) { // "private" option |
|
1093 |
QBenchmarkGlobalData::current->setMode(QBenchmarkGlobalData::CallgrindChildProcess); |
|
1094 |
QBenchmarkGlobalData::current->callgrindOutFileBase = |
|
1095 |
QBenchmarkValgrindUtils::outFileBase(); |
|
1096 |
#endif |
|
1097 |
#ifdef HAVE_TICK_COUNTER |
|
1098 |
} else if (strcmp(argv[i], "-tickcounter") == 0) { |
|
1099 |
QBenchmarkGlobalData::current->setMode(QBenchmarkGlobalData::TickCounter); |
|
1100 |
#endif |
|
1101 |
} else if (strcmp(argv[i], "-eventcounter") == 0) { |
|
1102 |
QBenchmarkGlobalData::current->setMode(QBenchmarkGlobalData::EventCounter); |
|
1103 |
} else if (strcmp(argv[i], "-minimumvalue") == 0) { |
|
1104 |
if (i + 1 >= argc) { |
|
1105 |
printf("-minimumvalue needs an extra parameter to indicate the minimum time(ms)\n"); |
|
1106 |
exit(1); |
|
1107 |
} else { |
|
1108 |
QBenchmarkGlobalData::current->walltimeMinimum = qToInt(argv[++i]); |
|
1109 |
} |
|
1110 |
} else if (strcmp(argv[i], "-iterations") == 0) { |
|
1111 |
if (i + 1 >= argc) { |
|
1112 |
printf("-iterations needs an extra parameter to indicate the number of iterations\n"); |
|
1113 |
exit(1); |
|
1114 |
} else { |
|
1115 |
QBenchmarkGlobalData::current->iterationCount = qToInt(argv[++i]); |
|
1116 |
} |
|
1117 |
} else if (strcmp(argv[i], "-median") == 0) { |
|
1118 |
if (i + 1 >= argc) { |
|
1119 |
printf("-median needs an extra parameter to indicate the number of median iterations\n"); |
|
1120 |
exit(1); |
|
1121 |
} else { |
|
1122 |
QBenchmarkGlobalData::current->medianIterationCount = qToInt(argv[++i]); |
|
1123 |
} |
|
1124 |
||
1125 |
} else if (strcmp(argv[i], "-vb") == 0) { |
|
1126 |
QBenchmarkGlobalData::current->verboseOutput = true; |
|
1127 |
} else if (strcmp(argv[i], "-chart") == 0) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
1128 |
fprintf(stderr, "Warning: `-chart' option is not available\n"); |
0 | 1129 |
} else if (strcmp(argv[i], "-qws") == 0) { |
1130 |
// do nothing |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1131 |
} else if (strcmp(argv[i], "-graphicssystem") == 0) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1132 |
// do nothing |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1133 |
if (i + 1 >= argc) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1134 |
printf("-graphicssystem needs an extra parameter specifying the graphics system\n"); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1135 |
exit(1); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1136 |
} else { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1137 |
++i; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1138 |
} |
0 | 1139 |
} else if (argv[i][0] == '-') { |
1140 |
printf("Unknown option: '%s'\n\n%s", argv[i], testOptions); |
|
1141 |
exit(1); |
|
1142 |
} else { |
|
1143 |
int colon = -1; |
|
1144 |
char buf[512], *data=0; |
|
1145 |
int off; |
|
1146 |
for(off = 0; *(argv[i]+off); ++off) { |
|
1147 |
if (*(argv[i]+off) == ':') { |
|
1148 |
colon = off; |
|
1149 |
break; |
|
1150 |
} |
|
1151 |
} |
|
1152 |
if(colon != -1) { |
|
1153 |
data = qstrdup(argv[i]+colon+1); |
|
1154 |
} |
|
1155 |
QTest::qt_snprintf(buf, qMin(512, off + 1), "%s", argv[i]); // copy text before the ':' into buf |
|
1156 |
QTest::qt_snprintf(buf + off, qMin(512 - off, 3), "()"); // append "()" |
|
1157 |
int idx = QTest::currentTestObject->metaObject()->indexOfMethod(buf); |
|
1158 |
if (idx < 0 || !isValidSlot(QTest::currentTestObject->metaObject()->method(idx))) { |
|
1159 |
printf("Unknown testfunction: '%s'\n", buf); |
|
1160 |
printf("Available testfunctions:\n"); |
|
1161 |
qPrintTestSlots(); |
|
1162 |
exit(1); |
|
1163 |
} |
|
1164 |
++QTest::lastTestFuncIdx; |
|
1165 |
if (!QTest::testFuncs) { |
|
1166 |
struct Cleanup { ~Cleanup() { delete[] QTest::testFuncs; } }; |
|
1167 |
static Cleanup cleanup; |
|
1168 |
QTest::testFuncs = new TestFunction[512]; |
|
1169 |
} |
|
1170 |
QTest::testFuncs[QTest::lastTestFuncIdx].function = idx; |
|
1171 |
QTest::testFuncs[QTest::lastTestFuncIdx].data = data; |
|
1172 |
QTEST_ASSERT(QTest::lastTestFuncIdx < 512); |
|
1173 |
} |
|
1174 |
} |
|
1175 |
} |
|
1176 |
||
1177 |
QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container) |
|
1178 |
{ |
|
1179 |
const int count = container.count(); |
|
1180 |
if (count == 0) |
|
1181 |
return QBenchmarkResult(); |
|
1182 |
||
1183 |
if (count == 1) |
|
1184 |
return container.at(0); |
|
1185 |
||
1186 |
QList<QBenchmarkResult> containerCopy = container; |
|
1187 |
qSort(containerCopy); |
|
1188 |
||
1189 |
const int middle = count / 2; |
|
1190 |
||
1191 |
// ### handle even-sized containers here by doing an aritmetic mean of the two middle items. |
|
1192 |
return containerCopy.at(middle); |
|
1193 |
} |
|
1194 |
||
1195 |
struct QTestDataSetter |
|
1196 |
{ |
|
1197 |
QTestDataSetter(QTestData *data) |
|
1198 |
{ |
|
1199 |
QTestResult::setCurrentTestData(data); |
|
1200 |
} |
|
1201 |
~QTestDataSetter() |
|
1202 |
{ |
|
1203 |
QTestResult::setCurrentTestData(0); |
|
1204 |
} |
|
1205 |
}; |
|
1206 |
||
1207 |
static void qInvokeTestMethodDataEntry(char *slot) |
|
1208 |
{ |
|
1209 |
/* Benchmarking: for each median iteration*/ |
|
1210 |
||
1211 |
int i = (QBenchmarkGlobalData::current->measurer->needsWarmupIteration()) ? -1 : 0; |
|
1212 |
||
1213 |
QList<QBenchmarkResult> results; |
|
1214 |
do { |
|
1215 |
QBenchmarkTestMethodData::current->beginDataRun(); |
|
1216 |
||
1217 |
/* Benchmarking: for each accumulation iteration*/ |
|
1218 |
bool invokeOk; |
|
1219 |
do { |
|
1220 |
QTestResult::setCurrentTestLocation(QTestResult::InitFunc); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1221 |
invokeMethod(QTest::currentTestObject, "init()"); |
0 | 1222 |
if (QTestResult::skipCurrentTest()) |
1223 |
break; |
|
1224 |
||
1225 |
QTestResult::setCurrentTestLocation(QTestResult::Func); |
|
1226 |
||
1227 |
QBenchmarkTestMethodData::current->result = QBenchmarkResult(); |
|
1228 |
QBenchmarkTestMethodData::current->resultAccepted = false; |
|
1229 |
||
1230 |
QBenchmarkGlobalData::current->context.tag = |
|
1231 |
QLatin1String( |
|
1232 |
QTestResult::currentDataTag() |
|
1233 |
? QTestResult::currentDataTag() : ""); |
|
1234 |
||
1235 |
invokeOk = QMetaObject::invokeMethod(QTest::currentTestObject, slot, |
|
1236 |
Qt::DirectConnection); |
|
1237 |
if (!invokeOk) |
|
1238 |
QTestResult::addFailure("Unable to execute slot", __FILE__, __LINE__); |
|
1239 |
||
1240 |
QTestResult::setCurrentTestLocation(QTestResult::CleanupFunc); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1241 |
invokeMethod(QTest::currentTestObject, "cleanup()"); |
0 | 1242 |
QTestResult::setCurrentTestLocation(QTestResult::NoWhere); |
1243 |
||
1244 |
// If this test method has a benchmark, repeat until all measurements are |
|
1245 |
// acceptable. |
|
1246 |
// The QBENCHMARK macro increases the number of iterations for each run until |
|
1247 |
// this happens. |
|
1248 |
} while (invokeOk |
|
1249 |
&& QBenchmarkTestMethodData::current->isBenchmark() |
|
1250 |
&& QBenchmarkTestMethodData::current->resultsAccepted() == false); |
|
1251 |
||
1252 |
QBenchmarkTestMethodData::current->endDataRun(); |
|
1253 |
if (i > -1) // iteration -1 is the warmup iteration. |
|
1254 |
results.append(QBenchmarkTestMethodData::current->result); |
|
1255 |
||
1256 |
if (QBenchmarkTestMethodData::current->isBenchmark() && |
|
1257 |
QBenchmarkGlobalData::current->verboseOutput) { |
|
1258 |
if (i == -1) { |
|
1259 |
qDebug() << "warmup stage result :" << QBenchmarkTestMethodData::current->result.value; |
|
1260 |
} else { |
|
1261 |
qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value; |
|
1262 |
} |
|
1263 |
} |
|
1264 |
} while (QBenchmarkTestMethodData::current->isBenchmark() |
|
1265 |
&& (++i < QBenchmarkGlobalData::current->adjustMedianIterationCount())); |
|
1266 |
||
1267 |
if (QBenchmarkTestMethodData::current->isBenchmark() |
|
1268 |
&& QBenchmarkTestMethodData::current->resultsAccepted()) |
|
1269 |
QTestLog::addBenchmarkResult(qMedian(results)); |
|
1270 |
} |
|
1271 |
||
1272 |
/*! |
|
1273 |
\internal |
|
1274 |
||
1275 |
Call init(), slot_data(), slot(), slot(), slot()..., cleanup() |
|
1276 |
If data is set then it is the only test that is performed |
|
1277 |
||
1278 |
If the function was successfully called, true is returned, otherwise |
|
1279 |
false. |
|
1280 |
*/ |
|
1281 |
static bool qInvokeTestMethod(const char *slotName, const char *data=0) |
|
1282 |
{ |
|
1283 |
QTEST_ASSERT(slotName); |
|
1284 |
||
1285 |
QBenchmarkTestMethodData benchmarkData; |
|
1286 |
QBenchmarkTestMethodData::current = &benchmarkData; |
|
1287 |
||
1288 |
QBenchmarkGlobalData::current->context.slotName = QLatin1String(slotName); |
|
1289 |
||
1290 |
char member[512]; |
|
1291 |
QTestTable table; |
|
1292 |
||
1293 |
char *slot = qstrdup(slotName); |
|
1294 |
slot[strlen(slot) - 2] = '\0'; |
|
1295 |
QTestResult::setCurrentTestFunction(slot); |
|
1296 |
||
1297 |
const QTestTable *gTable = QTestTable::globalTestTable(); |
|
1298 |
const int globalDataCount = gTable->dataCount(); |
|
1299 |
int curGlobalDataIndex = 0; |
|
1300 |
||
1301 |
/* For each test function that has a *_data() table/function, do: */ |
|
1302 |
do { |
|
1303 |
if (!gTable->isEmpty()) |
|
1304 |
QTestResult::setCurrentGlobalTestData(gTable->testData(curGlobalDataIndex)); |
|
1305 |
||
1306 |
if (curGlobalDataIndex == 0) { |
|
1307 |
QTestResult::setCurrentTestLocation(QTestResult::DataFunc); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1308 |
QTest::qt_snprintf(member, 512, "%s_data()", slot); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1309 |
invokeMethod(QTest::currentTestObject, member); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1310 |
|
0 | 1311 |
// if we encounter a SkipAll in the _data slot, we skip the whole |
1312 |
// testfunction, no matter how much global data exists |
|
1313 |
if (QTestResult::skipCurrentTest()) { |
|
1314 |
QTestResult::setCurrentGlobalTestData(0); |
|
1315 |
break; |
|
1316 |
} |
|
1317 |
} |
|
1318 |
||
1319 |
bool foundFunction = false; |
|
1320 |
if (!QTestResult::skipCurrentTest()) { |
|
1321 |
int curDataIndex = 0; |
|
1322 |
const int dataCount = table.dataCount(); |
|
1323 |
QTestResult::setSkipCurrentTest(false); |
|
1324 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1325 |
// Data tag requested but none available? |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1326 |
if (data && !dataCount) { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1327 |
// Let empty data tag through. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1328 |
if (!*data) |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1329 |
data = 0; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1330 |
else { |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1331 |
printf("Unknown testdata for function %s: '%s'\n", slotName, data); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1332 |
printf("Function has no testdata.\n"); |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1333 |
return false; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1334 |
} |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1335 |
} |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1336 |
|
0 | 1337 |
/* For each entry in the data table, do: */ |
1338 |
do { |
|
1339 |
if (!data || !qstrcmp(data, table.testData(curDataIndex)->dataTag())) { |
|
1340 |
foundFunction = true; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1341 |
QTestDataSetter s(curDataIndex >= dataCount ? static_cast<QTestData *>(0) |
0 | 1342 |
: table.testData(curDataIndex)); |
1343 |
||
1344 |
qInvokeTestMethodDataEntry(slot); |
|
1345 |
||
1346 |
if (QTestResult::skipCurrentTest()) |
|
1347 |
// check whether SkipAll was requested |
|
1348 |
break; |
|
1349 |
if (data) |
|
1350 |
break; |
|
1351 |
} |
|
1352 |
++curDataIndex; |
|
1353 |
} while (curDataIndex < dataCount); |
|
1354 |
} |
|
1355 |
||
1356 |
if (data && !foundFunction) { |
|
1357 |
printf("Unknown testdata for function %s: '%s'\n", slotName, data); |
|
1358 |
printf("Available testdata:\n"); |
|
1359 |
for(int i = 0; i < table.dataCount(); ++i) |
|
1360 |
printf("%s\n", table.testData(i)->dataTag()); |
|
1361 |
return false; |
|
1362 |
} |
|
1363 |
||
1364 |
QTestResult::setCurrentGlobalTestData(0); |
|
1365 |
++curGlobalDataIndex; |
|
1366 |
} while (curGlobalDataIndex < globalDataCount); |
|
1367 |
||
1368 |
QTestResult::finishedCurrentTestFunction(); |
|
1369 |
QTestResult::setSkipCurrentTest(false); |
|
1370 |
QTestResult::setCurrentTestData(0); |
|
1371 |
delete[] slot; |
|
1372 |
||
1373 |
return true; |
|
1374 |
} |
|
1375 |
||
1376 |
void *fetchData(QTestData *data, const char *tagName, int typeId) |
|
1377 |
{ |
|
1378 |
QTEST_ASSERT(typeId); |
|
1379 |
QTEST_ASSERT_X(data, "QTest::fetchData()", "Test data requested, but no testdata available."); |
|
1380 |
QTEST_ASSERT(data->parent()); |
|
1381 |
||
1382 |
int idx = data->parent()->indexOf(tagName); |
|
1383 |
||
1384 |
if (idx == -1 || idx >= data->dataCount()) { |
|
1385 |
qFatal("QFETCH: Requested testdata '%s' not available, check your _data function.", |
|
1386 |
tagName); |
|
1387 |
} |
|
1388 |
||
1389 |
if (typeId != data->parent()->elementTypeId(idx)) { |
|
1390 |
qFatal("Requested type '%s' does not match available type '%s'.", |
|
1391 |
QMetaType::typeName(typeId), |
|
1392 |
QMetaType::typeName(data->parent()->elementTypeId(idx))); |
|
1393 |
} |
|
1394 |
||
1395 |
return data->data(idx); |
|
1396 |
} |
|
1397 |
||
1398 |
/*! |
|
1399 |
\fn char* QTest::toHexRepresentation(const char *ba, int length) |
|
1400 |
||
1401 |
Returns a pointer to a string that is the string \a ba represented |
|
1402 |
as a space-separated sequence of hex characters. If the input is |
|
1403 |
considered too long, it is truncated. A trucation is indicated in |
|
1404 |
the returned string as an ellipsis at the end. |
|
1405 |
||
1406 |
\a length is the length of the string \a ba. |
|
1407 |
*/ |
|
1408 |
char *toHexRepresentation(const char *ba, int length) |
|
1409 |
{ |
|
1410 |
if(length == 0) |
|
1411 |
return qstrdup(""); |
|
1412 |
||
1413 |
/* We output at maximum about maxLen characters in order to avoid |
|
1414 |
* running out of memory and flooding things when the byte array |
|
1415 |
* is large. |
|
1416 |
* |
|
1417 |
* maxLen can't be for example 200 because QTestLib is sprinkled with fixed |
|
1418 |
* size char arrays. |
|
1419 |
* */ |
|
1420 |
const int maxLen = 50; |
|
1421 |
const int len = qMin(maxLen, length); |
|
1422 |
char *result = 0; |
|
1423 |
||
1424 |
if(length > maxLen) { |
|
1425 |
const int size = len * 3 + 4; |
|
1426 |
result = new char[size]; |
|
1427 |
||
1428 |
char *const forElipsis = result + size - 5; |
|
1429 |
forElipsis[0] = ' '; |
|
1430 |
forElipsis[1] = '.'; |
|
1431 |
forElipsis[2] = '.'; |
|
1432 |
forElipsis[3] = '.'; |
|
1433 |
result[size - 1] = '\0'; |
|
1434 |
} |
|
1435 |
else { |
|
1436 |
const int size = len * 3; |
|
1437 |
result = new char[size]; |
|
1438 |
result[size - 1] = '\0'; |
|
1439 |
} |
|
1440 |
||
1441 |
const char toHex[] = "0123456789ABCDEF"; |
|
1442 |
int i = 0; |
|
1443 |
int o = 0; |
|
1444 |
||
1445 |
while(true) { |
|
1446 |
const char at = ba[i]; |
|
1447 |
||
1448 |
result[o] = toHex[(at >> 4) & 0x0F]; |
|
1449 |
++o; |
|
1450 |
result[o] = toHex[at & 0x0F]; |
|
1451 |
||
1452 |
++i; |
|
1453 |
++o; |
|
1454 |
if(i == len) |
|
1455 |
break; |
|
1456 |
else { |
|
1457 |
result[o] = ' '; |
|
1458 |
++o; |
|
1459 |
} |
|
1460 |
} |
|
1461 |
||
1462 |
return result; |
|
1463 |
} |
|
1464 |
||
1465 |
static void qInvokeTestMethods(QObject *testObject) |
|
1466 |
{ |
|
1467 |
const QMetaObject *metaObject = testObject->metaObject(); |
|
1468 |
QTEST_ASSERT(metaObject); |
|
1469 |
||
1470 |
QTestLog::startLogging(); |
|
1471 |
||
1472 |
QTestResult::setCurrentTestFunction("initTestCase"); |
|
1473 |
QTestResult::setCurrentTestLocation(QTestResult::DataFunc); |
|
1474 |
QTestTable::globalTestTable(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1475 |
invokeMethod(testObject, "initTestCase_data()"); |
0 | 1476 |
|
1477 |
if (!QTestResult::skipCurrentTest() && !QTest::currentTestFailed()) { |
|
1478 |
QTestResult::setCurrentTestLocation(QTestResult::InitFunc); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1479 |
invokeMethod(testObject, "initTestCase()"); |
0 | 1480 |
|
1481 |
// finishedCurrentTestFunction() resets QTestResult::testFailed(), so use a local copy. |
|
1482 |
const bool previousFailed = QTestResult::testFailed(); |
|
1483 |
QTestResult::finishedCurrentTestFunction(); |
|
1484 |
||
1485 |
if(!QTestResult::skipCurrentTest() && !previousFailed) { |
|
1486 |
||
1487 |
if (lastTestFuncIdx >= 0) { |
|
1488 |
for (int i = 0; i <= lastTestFuncIdx; ++i) { |
|
1489 |
if (!qInvokeTestMethod(metaObject->method(testFuncs[i].function).signature(), |
|
1490 |
testFuncs[i].data)) |
|
1491 |
break; |
|
1492 |
} |
|
1493 |
} else { |
|
1494 |
int methodCount = metaObject->methodCount(); |
|
1495 |
for (int i = 0; i < methodCount; ++i) { |
|
1496 |
QMetaMethod slotMethod = metaObject->method(i); |
|
1497 |
if (!isValidSlot(slotMethod)) |
|
1498 |
continue; |
|
1499 |
if (!qInvokeTestMethod(slotMethod.signature())) |
|
1500 |
break; |
|
1501 |
} |
|
1502 |
} |
|
1503 |
} |
|
1504 |
||
1505 |
QTestResult::setSkipCurrentTest(false); |
|
1506 |
QTestResult::setCurrentTestFunction("cleanupTestCase"); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1507 |
invokeMethod(testObject, "cleanupTestCase()"); |
0 | 1508 |
} |
1509 |
QTestResult::finishedCurrentTestFunction(); |
|
1510 |
QTestResult::setCurrentTestFunction(0); |
|
1511 |
QTestTable::clearGlobalTestTable(); |
|
1512 |
||
1513 |
QTestLog::stopLogging(); |
|
1514 |
} |
|
1515 |
||
1516 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
|
1517 |
class FatalSignalHandler |
|
1518 |
{ |
|
1519 |
public: |
|
1520 |
FatalSignalHandler(); |
|
1521 |
~FatalSignalHandler(); |
|
1522 |
||
1523 |
private: |
|
1524 |
static void signal(int); |
|
1525 |
sigset_t handledSignals; |
|
1526 |
}; |
|
1527 |
||
1528 |
void FatalSignalHandler::signal(int signum) |
|
1529 |
{ |
|
1530 |
qFatal("Received signal %d", signum); |
|
1531 |
} |
|
1532 |
||
1533 |
FatalSignalHandler::FatalSignalHandler() |
|
1534 |
{ |
|
1535 |
sigemptyset(&handledSignals); |
|
1536 |
||
1537 |
const int fatalSignals[] = { |
|
1538 |
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGFPE, SIGSEGV, SIGPIPE, SIGTERM, 0 }; |
|
1539 |
||
1540 |
struct sigaction act; |
|
1541 |
memset(&act, 0, sizeof(act)); |
|
1542 |
act.sa_handler = FatalSignalHandler::signal; |
|
1543 |
||
1544 |
// Remove the handler after it is invoked. |
|
1545 |
act.sa_flags = SA_RESETHAND; |
|
1546 |
||
1547 |
// Block all fatal signals in our signal handler so we don't try to close |
|
1548 |
// the testlog twice. |
|
1549 |
sigemptyset(&act.sa_mask); |
|
1550 |
for (int i = 0; fatalSignals[i]; ++i) |
|
1551 |
sigaddset(&act.sa_mask, fatalSignals[i]); |
|
1552 |
||
1553 |
struct sigaction oldact; |
|
1554 |
||
1555 |
for (int i = 0; fatalSignals[i]; ++i) { |
|
1556 |
sigaction(fatalSignals[i], &act, &oldact); |
|
1557 |
#ifndef Q_WS_QWS |
|
1558 |
// Don't overwrite any non-default handlers |
|
1559 |
// however, we need to replace the default QWS handlers |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1560 |
if ( |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1561 |
#ifdef SA_SIGINFO |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1562 |
oldact.sa_flags & SA_SIGINFO || |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1563 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1564 |
oldact.sa_handler != SIG_DFL) { |
0 | 1565 |
sigaction(fatalSignals[i], &oldact, 0); |
1566 |
} else |
|
1567 |
#endif |
|
1568 |
{ |
|
1569 |
sigaddset(&handledSignals, fatalSignals[i]); |
|
1570 |
} |
|
1571 |
} |
|
1572 |
} |
|
1573 |
||
1574 |
||
1575 |
FatalSignalHandler::~FatalSignalHandler() |
|
1576 |
{ |
|
1577 |
// Unregister any of our remaining signal handlers |
|
1578 |
struct sigaction act; |
|
1579 |
memset(&act, 0, sizeof(act)); |
|
1580 |
act.sa_handler = SIG_DFL; |
|
1581 |
||
1582 |
struct sigaction oldact; |
|
1583 |
||
1584 |
for (int i = 1; i < 32; ++i) { |
|
1585 |
if (!sigismember(&handledSignals, i)) |
|
1586 |
continue; |
|
1587 |
sigaction(i, &act, &oldact); |
|
1588 |
||
1589 |
// If someone overwrote it in the mean time, put it back |
|
1590 |
if (oldact.sa_handler != FatalSignalHandler::signal) |
|
1591 |
sigaction(i, &oldact, 0); |
|
1592 |
} |
|
1593 |
} |
|
1594 |
||
1595 |
#endif |
|
1596 |
||
1597 |
||
1598 |
} // namespace |
|
1599 |
||
1600 |
/*! |
|
1601 |
Executes tests declared in \a testObject. In addition, the private slots |
|
1602 |
\c{initTestCase()}, \c{cleanupTestCase()}, \c{init()} and \c{cleanup()} |
|
1603 |
are executed if they exist. See \l{Creating a Test} for more details. |
|
1604 |
||
1605 |
Optionally, the command line arguments \a argc and \a argv can be provided. |
|
1606 |
For a list of recognized arguments, read \l {QTestLib Command Line Arguments}. |
|
1607 |
||
1608 |
For stand-alone tests, the convenience macro \l QTEST_MAIN() can |
|
1609 |
be used to declare a main method that parses the command line arguments |
|
1610 |
and executes the tests. |
|
1611 |
||
1612 |
Returns 0 if all tests passed. Returns a value other than 0 if tests failed |
|
1613 |
or in case of unhandled exceptions. The return value from this function is |
|
1614 |
also the exit code of the test application when the \l QTEST_MAIN() macro |
|
1615 |
is used. |
|
1616 |
||
1617 |
The following example will run all tests in \c MyFirstTestObject and |
|
1618 |
\c{MySecondTestObject}: |
|
1619 |
||
1620 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 18 |
|
1621 |
||
1622 |
Note: This function is not reentrant, only one test can run at a time. A |
|
1623 |
test that was executed with qExec() can't run another test via qExec() and |
|
1624 |
threads are not allowed to call qExec() simultaneously. |
|
1625 |
||
1626 |
If you have programatically created the arguments, as opposed to getting them |
|
1627 |
from the arguments in \c main(), it is likely of interest to use |
|
1628 |
QTest::qExec(QObject *, const QStringList &) since it is Unicode safe. |
|
1629 |
||
1630 |
\sa QTEST_MAIN() |
|
1631 |
*/ |
|
1632 |
||
1633 |
int QTest::qExec(QObject *testObject, int argc, char **argv) |
|
1634 |
{ |
|
1635 |
QBenchmarkGlobalData benchmarkData; |
|
1636 |
QBenchmarkGlobalData::current = &benchmarkData; |
|
1637 |
||
1638 |
#ifdef QTESTLIB_USE_VALGRIND |
|
1639 |
int callgrindChildExitCode = 0; |
|
1640 |
#endif |
|
1641 |
||
1642 |
#ifdef Q_WS_MAC |
|
1643 |
bool macNeedsActivate = qApp && (qstrcmp(qApp->metaObject()->className(), "QApplication") == 0); |
|
1644 |
#ifdef QT_MAC_USE_COCOA |
|
1645 |
IOPMAssertionID powerID; |
|
1646 |
#endif |
|
1647 |
#endif |
|
1648 |
#ifndef QT_NO_EXCEPTIONS |
|
1649 |
try { |
|
1650 |
#endif |
|
1651 |
||
1652 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) |
|
1653 |
SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX); |
|
1654 |
#endif |
|
1655 |
||
1656 |
#ifdef Q_WS_MAC |
|
1657 |
// Starting with Qt 4.4, applications launched from the command line |
|
1658 |
// no longer get focus automatically. Since some tests might depend |
|
1659 |
// on this, call SetFrontProcess here to get the pre 4.4 behavior. |
|
1660 |
if (macNeedsActivate) { |
|
1661 |
ProcessSerialNumber psn = { 0, kCurrentProcess }; |
|
1662 |
SetFrontProcess(&psn); |
|
1663 |
# ifdef QT_MAC_USE_COCOA |
|
1664 |
IOReturn ok = IOPMAssertionCreate(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, &powerID); |
|
1665 |
if (ok != kIOReturnSuccess) |
|
1666 |
macNeedsActivate = false; // no need to release the assertion on exit. |
|
1667 |
# else |
|
1668 |
UpdateSystemActivity(1); // Wake the display. |
|
1669 |
# endif |
|
1670 |
} |
|
1671 |
#endif |
|
1672 |
||
1673 |
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
1674 |
// Delay execution of tests in Symbian emulator. |
|
1675 |
// Needed to allow worst of other higher priority apps and services launched by emulator |
|
1676 |
// to get out of the way before we run our test. Otherwise some of the timing sensitive tests |
|
1677 |
// will not work properly. |
|
1678 |
qSleep(3000); |
|
1679 |
#endif |
|
1680 |
||
1681 |
QTestResult::reset(); |
|
1682 |
||
1683 |
QTEST_ASSERT(testObject); |
|
1684 |
QTEST_ASSERT(!currentTestObject); |
|
1685 |
currentTestObject = testObject; |
|
1686 |
||
1687 |
const QMetaObject *metaObject = testObject->metaObject(); |
|
1688 |
QTEST_ASSERT(metaObject); |
|
1689 |
||
1690 |
QTestResult::setCurrentTestObject(metaObject->className()); |
|
1691 |
qParseArgs(argc, argv); |
|
1692 |
#ifdef QTESTLIB_USE_VALGRIND |
|
1693 |
if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) { |
|
1694 |
const QStringList origAppArgs(QCoreApplication::arguments()); |
|
1695 |
if (!QBenchmarkValgrindUtils::rerunThroughCallgrind(origAppArgs, callgrindChildExitCode)) |
|
1696 |
return -1; |
|
1697 |
||
1698 |
QBenchmarkValgrindUtils::cleanup(); |
|
1699 |
||
1700 |
} else |
|
1701 |
#endif |
|
1702 |
{ |
|
1703 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1704 |
QScopedPointer<FatalSignalHandler> handler; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1705 |
if (!noCrashHandler) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1706 |
handler.reset(new FatalSignalHandler); |
0 | 1707 |
#endif |
1708 |
qInvokeTestMethods(testObject); |
|
1709 |
} |
|
1710 |
||
1711 |
#ifndef QT_NO_EXCEPTIONS |
|
1712 |
} catch (...) { |
|
1713 |
QTestResult::addFailure("Caught unhandled exception", __FILE__, __LINE__); |
|
1714 |
if (QTestResult::currentTestFunction()) { |
|
1715 |
QTestResult::finishedCurrentTestFunction(); |
|
1716 |
QTestResult::setCurrentTestFunction(0); |
|
1717 |
} |
|
1718 |
||
1719 |
QTestLog::stopLogging(); |
|
1720 |
#ifdef QT_MAC_USE_COCOA |
|
1721 |
if (macNeedsActivate) { |
|
1722 |
IOPMAssertionRelease(powerID); |
|
1723 |
} |
|
1724 |
#endif |
|
1725 |
// Rethrow exception to make debugging easier. |
|
1726 |
throw; |
|
1727 |
return 1; |
|
1728 |
} |
|
1729 |
# endif |
|
1730 |
||
1731 |
currentTestObject = 0; |
|
1732 |
#ifdef QT_MAC_USE_COCOA |
|
1733 |
if (macNeedsActivate) { |
|
1734 |
IOPMAssertionRelease(powerID); |
|
1735 |
} |
|
1736 |
#endif |
|
1737 |
||
1738 |
#if defined(QTEST_NOEXITCODE) |
|
1739 |
return 0; |
|
1740 |
#else |
|
1741 |
||
1742 |
#ifdef QTESTLIB_USE_VALGRIND |
|
1743 |
if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) |
|
1744 |
return callgrindChildExitCode; |
|
1745 |
#endif |
|
1746 |
// make sure our exit code is never going above 127 |
|
1747 |
// since that could wrap and indicate 0 test fails |
|
1748 |
return qMin(QTestResult::failCount(), 127); |
|
1749 |
||
1750 |
#endif |
|
1751 |
} |
|
1752 |
||
1753 |
/*! |
|
1754 |
\overload |
|
1755 |
\since 4.4 |
|
1756 |
||
1757 |
Behaves identically to qExec(QObject *, int, char**) but takes a |
|
1758 |
QStringList of \a arguments instead of a \c char** list. |
|
1759 |
*/ |
|
1760 |
int QTest::qExec(QObject *testObject, const QStringList &arguments) |
|
1761 |
{ |
|
1762 |
const int argc = arguments.count(); |
|
1763 |
QVarLengthArray<char *> argv(argc); |
|
1764 |
||
1765 |
QVector<QByteArray> args; |
|
1766 |
args.reserve(argc); |
|
1767 |
||
1768 |
for(int i = 0; i < argc; ++i) |
|
1769 |
{ |
|
1770 |
args.append(arguments.at(i).toLocal8Bit().constData()); |
|
1771 |
argv[i] = args.last().data(); |
|
1772 |
} |
|
1773 |
||
1774 |
return qExec(testObject, argc, argv.data()); |
|
1775 |
} |
|
1776 |
||
1777 |
/*! \internal |
|
1778 |
*/ |
|
1779 |
void QTest::qFail(const char *statementStr, const char *file, int line) |
|
1780 |
{ |
|
1781 |
QTestResult::addFailure(statementStr, file, line); |
|
1782 |
} |
|
1783 |
||
1784 |
/*! \internal |
|
1785 |
*/ |
|
1786 |
bool QTest::qVerify(bool statement, const char *statementStr, const char *description, |
|
1787 |
const char *file, int line) |
|
1788 |
{ |
|
1789 |
return QTestResult::verify(statement, statementStr, description, file, line); |
|
1790 |
} |
|
1791 |
||
1792 |
/*! \fn void QTest::qSkip(const char *message, SkipMode mode, const char *file, int line) |
|
1793 |
\internal |
|
1794 |
*/ |
|
1795 |
void QTest::qSkip(const char *message, QTest::SkipMode mode, |
|
1796 |
const char *file, int line) |
|
1797 |
{ |
|
1798 |
QTestResult::addSkip(message, mode, file, line); |
|
1799 |
if (mode == QTest::SkipAll) |
|
1800 |
QTestResult::setSkipCurrentTest(true); |
|
1801 |
} |
|
1802 |
||
1803 |
/*! \fn bool QTest::qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line) |
|
1804 |
\internal |
|
1805 |
*/ |
|
1806 |
bool QTest::qExpectFail(const char *dataIndex, const char *comment, |
|
1807 |
QTest::TestFailMode mode, const char *file, int line) |
|
1808 |
{ |
|
1809 |
return QTestResult::expectFail(dataIndex, qstrdup(comment), mode, file, line); |
|
1810 |
} |
|
1811 |
||
1812 |
/*! \internal |
|
1813 |
*/ |
|
1814 |
void QTest::qWarn(const char *message) |
|
1815 |
{ |
|
1816 |
QTestLog::warn(message); |
|
1817 |
} |
|
1818 |
||
1819 |
/*! |
|
1820 |
Ignores messages created by qDebug() or qWarning(). If the \a message |
|
1821 |
with the corresponding \a type is outputted, it will be removed from the |
|
1822 |
test log. If the test finished and the \a message was not outputted, |
|
1823 |
a test failure is appended to the test log. |
|
1824 |
||
1825 |
\bold {Note:} Invoking this function will only ignore one message. |
|
1826 |
If the message you want to ignore is outputted twice, you have to |
|
1827 |
call ignoreMessage() twice, too. |
|
1828 |
||
1829 |
Example: |
|
1830 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 19 |
|
1831 |
||
1832 |
The example above tests that QDir::mkdir() outputs the right warning when invoked |
|
1833 |
with an invalid file name. |
|
1834 |
*/ |
|
1835 |
void QTest::ignoreMessage(QtMsgType type, const char *message) |
|
1836 |
{ |
|
1837 |
QTestResult::ignoreMessage(type, message); |
|
1838 |
} |
|
1839 |
||
1840 |
/*! \internal |
|
1841 |
*/ |
|
1842 |
void *QTest::qData(const char *tagName, int typeId) |
|
1843 |
{ |
|
1844 |
return fetchData(QTestResult::currentTestData(), tagName, typeId); |
|
1845 |
} |
|
1846 |
||
1847 |
/*! \internal |
|
1848 |
*/ |
|
1849 |
void *QTest::qGlobalData(const char *tagName, int typeId) |
|
1850 |
{ |
|
1851 |
return fetchData(QTestResult::currentGlobalTestData(), tagName, typeId); |
|
1852 |
} |
|
1853 |
||
1854 |
/*! \internal |
|
1855 |
*/ |
|
1856 |
void *QTest::qElementData(const char *tagName, int metaTypeId) |
|
1857 |
{ |
|
1858 |
QTEST_ASSERT(tagName); |
|
1859 |
QTestData *data = QTestResult::currentTestData(); |
|
1860 |
QTEST_ASSERT(data); |
|
1861 |
QTEST_ASSERT(data->parent()); |
|
1862 |
||
1863 |
int idx = data->parent()->indexOf(tagName); |
|
1864 |
QTEST_ASSERT(idx != -1); |
|
1865 |
QTEST_ASSERT(data->parent()->elementTypeId(idx) == metaTypeId); |
|
1866 |
||
1867 |
return data->data(data->parent()->indexOf(tagName)); |
|
1868 |
} |
|
1869 |
||
1870 |
/*! \internal |
|
1871 |
*/ |
|
1872 |
void QTest::addColumnInternal(int id, const char *name) |
|
1873 |
{ |
|
1874 |
QTestTable *tbl = QTestTable::currentTestTable(); |
|
1875 |
QTEST_ASSERT_X(tbl, "QTest::addColumn()", "Cannot add testdata outside of a _data slot."); |
|
1876 |
||
1877 |
tbl->addColumn(id, name); |
|
1878 |
} |
|
1879 |
||
1880 |
/*! |
|
1881 |
Appends a new row to the current test data. \a dataTag is the name of |
|
1882 |
the testdata that will appear in the test output. Returns a QTestData reference |
|
1883 |
that can be used to stream in data. |
|
1884 |
||
1885 |
Example: |
|
1886 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 20 |
|
1887 |
||
1888 |
\bold {Note:} This macro can only be used in a test's data function |
|
1889 |
that is invoked by the test framework. |
|
1890 |
||
1891 |
See \l {Chapter 2: Data Driven Testing}{Data Driven Testing} for |
|
1892 |
a more extensive example. |
|
1893 |
||
1894 |
\sa addColumn(), QFETCH() |
|
1895 |
*/ |
|
1896 |
QTestData &QTest::newRow(const char *dataTag) |
|
1897 |
{ |
|
1898 |
QTestTable *tbl = QTestTable::currentTestTable(); |
|
1899 |
QTEST_ASSERT_X(tbl, "QTest::addColumn()", "Cannot add testdata outside of a _data slot."); |
|
1900 |
||
1901 |
return *tbl->newData(dataTag); |
|
1902 |
} |
|
1903 |
||
1904 |
/*! \fn void QTest::addColumn(const char *name, T *dummy = 0) |
|
1905 |
||
1906 |
Adds a column with type \c{T} to the current test data. |
|
1907 |
\a name is the name of the column. \a dummy is a workaround |
|
1908 |
for buggy compilers and can be ignored. |
|
1909 |
||
1910 |
To populate the column with values, newRow() can be used. Use |
|
1911 |
\l QFETCH() to fetch the data in the actual test. |
|
1912 |
||
1913 |
Example: |
|
1914 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 21 |
|
1915 |
||
1916 |
To add custom types to the testdata, the type must be registered with |
|
1917 |
QMetaType via \l Q_DECLARE_METATYPE(). |
|
1918 |
||
1919 |
\bold {Note:} This macro can only be used in a test's data function |
|
1920 |
that is invoked by the test framework. |
|
1921 |
||
1922 |
See \l {Chapter 2: Data Driven Testing}{Data Driven Testing} for |
|
1923 |
a more extensive example. |
|
1924 |
||
1925 |
\sa QTest::newRow(), QFETCH(), QMetaType |
|
1926 |
*/ |
|
1927 |
||
1928 |
/*! |
|
1929 |
Returns the name of the test function that is currently executed. |
|
1930 |
||
1931 |
Example: |
|
1932 |
||
1933 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 22 |
|
1934 |
*/ |
|
1935 |
const char *QTest::currentTestFunction() |
|
1936 |
{ |
|
1937 |
return QTestResult::currentTestFunction(); |
|
1938 |
} |
|
1939 |
||
1940 |
/*! |
|
1941 |
Returns the name of the current test data. If the test doesn't |
|
1942 |
have any assigned testdata, the function returns 0. |
|
1943 |
*/ |
|
1944 |
const char *QTest::currentDataTag() |
|
1945 |
{ |
|
1946 |
return QTestResult::currentDataTag(); |
|
1947 |
} |
|
1948 |
||
1949 |
/*! |
|
1950 |
Returns true if the current test function failed, otherwise false. |
|
1951 |
*/ |
|
1952 |
bool QTest::currentTestFailed() |
|
1953 |
{ |
|
1954 |
return QTestResult::currentTestFailed(); |
|
1955 |
} |
|
1956 |
||
1957 |
/*! |
|
1958 |
Sleeps for \a ms milliseconds, blocking execution of the |
|
1959 |
test. qSleep() will not do any event processing and leave your test |
|
1960 |
unresponsive. Network communication might time out while |
|
1961 |
sleeping. Use \l qWait() to do non-blocking sleeping. |
|
1962 |
||
1963 |
\a ms must be greater than 0. |
|
1964 |
||
1965 |
\bold {Note:} The qSleep() function calls either \c nanosleep() on |
|
1966 |
unix or \c Sleep() on windows, so the accuracy of time spent in |
|
1967 |
qSleep() depends on the operating system. |
|
1968 |
||
1969 |
Example: |
|
1970 |
\snippet doc/src/snippets/code/src_qtestlib_qtestcase.cpp 23 |
|
1971 |
||
1972 |
\sa qWait() |
|
1973 |
*/ |
|
1974 |
void QTest::qSleep(int ms) |
|
1975 |
{ |
|
1976 |
QTEST_ASSERT(ms > 0); |
|
1977 |
||
1978 |
#ifdef Q_OS_WIN |
|
1979 |
Sleep(uint(ms)); |
|
1980 |
#else |
|
1981 |
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; |
|
1982 |
nanosleep(&ts, NULL); |
|
1983 |
#endif |
|
1984 |
} |
|
1985 |
||
1986 |
/*! \internal |
|
1987 |
*/ |
|
1988 |
QObject *QTest::testObject() |
|
1989 |
{ |
|
1990 |
return currentTestObject; |
|
1991 |
} |
|
1992 |
||
1993 |
/*! \internal |
|
1994 |
*/ |
|
1995 |
bool QTest::compare_helper(bool success, const char *msg, const char *file, int line) |
|
1996 |
{ |
|
1997 |
return QTestResult::compare(success, msg, file, line); |
|
1998 |
} |
|
1999 |
||
2000 |
/*! \internal |
|
2001 |
*/ |
|
2002 |
bool QTest::compare_helper(bool success, const char *msg, char *val1, char *val2, |
|
2003 |
const char *actual, const char *expected, const char *file, int line) |
|
2004 |
{ |
|
2005 |
return QTestResult::compare(success, msg, val1, val2, actual, expected, file, line); |
|
2006 |
} |
|
2007 |
||
2008 |
/*! \fn bool QTest::qCompare<float>(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2009 |
\internal |
|
2010 |
*/ |
|
2011 |
template <> |
|
2012 |
Q_TESTLIB_EXPORT bool QTest::qCompare<float>(float const &t1, float const &t2, const char *actual, const char *expected, |
|
2013 |
const char *file, int line) |
|
2014 |
{ |
|
2015 |
return qFuzzyCompare(t1, t2) |
|
2016 |
? compare_helper(true, "COMPARE()", file, line) |
|
2017 |
: compare_helper(false, "Compared floats are not the same (fuzzy compare)", |
|
2018 |
toString(t1), toString(t2), actual, expected, file, line); |
|
2019 |
} |
|
2020 |
||
2021 |
/*! \fn bool QTest::qCompare<double>(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2022 |
\internal |
|
2023 |
*/ |
|
2024 |
template <> |
|
2025 |
Q_TESTLIB_EXPORT bool QTest::qCompare<double>(double const &t1, double const &t2, const char *actual, const char *expected, |
|
2026 |
const char *file, int line) |
|
2027 |
{ |
|
2028 |
return qFuzzyCompare(t1, t2) |
|
2029 |
? compare_helper(true, "COMPARE()", file, line) |
|
2030 |
: compare_helper(false, "Compared doubles are not the same (fuzzy compare)", |
|
2031 |
toString(t1), toString(t2), actual, expected, file, line); |
|
2032 |
} |
|
2033 |
||
2034 |
#define COMPARE_IMPL2(TYPE, FORMAT) \ |
|
2035 |
template <> Q_TESTLIB_EXPORT char *QTest::toString<TYPE >(const TYPE &t) \ |
|
2036 |
{ \ |
|
2037 |
char *msg = new char[128]; \ |
|
2038 |
qt_snprintf(msg, 128, #FORMAT, t); \ |
|
2039 |
return msg; \ |
|
2040 |
} |
|
2041 |
||
2042 |
COMPARE_IMPL2(short, %hd) |
|
2043 |
COMPARE_IMPL2(ushort, %hu) |
|
2044 |
COMPARE_IMPL2(int, %d) |
|
2045 |
COMPARE_IMPL2(uint, %u) |
|
2046 |
COMPARE_IMPL2(long, %ld) |
|
2047 |
COMPARE_IMPL2(ulong, %lu) |
|
2048 |
#if defined(Q_OS_WIN) |
|
2049 |
COMPARE_IMPL2(qint64, %I64d) |
|
2050 |
COMPARE_IMPL2(quint64, %I64u) |
|
2051 |
#else |
|
2052 |
COMPARE_IMPL2(qint64, %lld) |
|
2053 |
COMPARE_IMPL2(quint64, %llu) |
|
2054 |
#endif |
|
2055 |
COMPARE_IMPL2(bool, %d) |
|
2056 |
COMPARE_IMPL2(char, %c) |
|
2057 |
COMPARE_IMPL2(float, %g) |
|
2058 |
COMPARE_IMPL2(double, %lg) |
|
2059 |
||
2060 |
/*! \internal |
|
2061 |
*/ |
|
2062 |
char *QTest::toString(const char *str) |
|
2063 |
{ |
|
2064 |
if (!str) |
|
2065 |
return 0; |
|
2066 |
char *msg = new char[strlen(str) + 1]; |
|
2067 |
return qstrcpy(msg, str); |
|
2068 |
} |
|
2069 |
||
2070 |
/*! \internal |
|
2071 |
*/ |
|
2072 |
char *QTest::toString(const void *p) |
|
2073 |
{ |
|
2074 |
char *msg = new char[128]; |
|
2075 |
qt_snprintf(msg, 128, "%p", p); |
|
2076 |
return msg; |
|
2077 |
} |
|
2078 |
||
2079 |
/*! \internal |
|
2080 |
*/ |
|
2081 |
bool QTest::compare_string_helper(const char *t1, const char *t2, const char *actual, |
|
2082 |
const char *expected, const char *file, int line) |
|
2083 |
{ |
|
2084 |
return (qstrcmp(t1, t2) == 0) |
|
2085 |
? compare_helper(true, "COMPARE()", file, line) |
|
2086 |
: compare_helper(false, "Compared strings are not the same", |
|
2087 |
toString(t1), toString(t2), actual, expected, file, line); |
|
2088 |
} |
|
2089 |
||
2090 |
/*! \fn bool QTest::compare_ptr_helper(const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line); |
|
2091 |
\internal |
|
2092 |
*/ |
|
2093 |
||
2094 |
/*! \fn bool QTest::qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int); |
|
2095 |
\internal |
|
2096 |
*/ |
|
2097 |
||
2098 |
||
2099 |
/*! \fn void QTest::mouseEvent(MouseAction action, QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1) |
|
2100 |
\internal |
|
2101 |
*/ |
|
2102 |
||
2103 |
/*! \fn bool QTest::qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2104 |
\internal |
|
2105 |
*/ |
|
2106 |
||
2107 |
/*! \fn bool QTest::qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2108 |
\internal |
|
2109 |
*/ |
|
2110 |
||
2111 |
/*! \fn bool QTest::qCompare(T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2112 |
\internal |
|
2113 |
*/ |
|
2114 |
||
2115 |
/*! \fn bool QTest::qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line) |
|
2116 |
\internal |
|
2117 |
*/ |
|
2118 |
||
2119 |
/*! \fn bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) |
|
2120 |
\internal |
|
2121 |
*/ |
|
2122 |
||
2123 |
/*! \fn bool QTest::qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) |
|
2124 |
\internal |
|
2125 |
*/ |
|
2126 |
||
2127 |
/*! \fn bool QTest::qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line) |
|
2128 |
\internal |
|
2129 |
*/ |
|
2130 |
||
2131 |
/*! \fn bool QTest::qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) |
|
2132 |
\internal |
|
2133 |
*/ |
|
2134 |
||
2135 |
/*! \fn bool QTest::qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) |
|
2136 |
\internal |
|
2137 |
*/ |
|
2138 |
||
2139 |
/*! \fn bool QTest::qCompare(char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) |
|
2140 |
\internal |
|
2141 |
*/ |
|
2142 |
||
2143 |
/*! \fn bool QTest::qCompare(const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) |
|
2144 |
\internal |
|
2145 |
*/ |
|
2146 |
||
2147 |
/*! \fn bool QTest::qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2148 |
\internal |
|
2149 |
*/ |
|
2150 |
||
2151 |
/*! \fn bool QTest::qCompare(QLatin1String const &t1, QString const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2152 |
\internal |
|
2153 |
*/ |
|
2154 |
||
2155 |
/*! \fn bool QTest::qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2156 |
\internal |
|
2157 |
*/ |
|
2158 |
||
2159 |
/*! \fn bool QTest::qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2160 |
\internal |
|
2161 |
*/ |
|
2162 |
||
2163 |
/*! \fn bool QTest::qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2164 |
\internal |
|
2165 |
*/ |
|
2166 |
||
2167 |
/*! \fn bool QTest::qCompare(bool const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line) |
|
2168 |
\internal |
|
2169 |
*/ |
|
2170 |
||
2171 |
/*! \fn bool QTest::qTest(const T& actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line) |
|
2172 |
\internal |
|
2173 |
*/ |
|
2174 |
||
2175 |
/*! \fn void QTest::sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, QString text, Qt::KeyboardModifiers modifier, int delay=-1) |
|
2176 |
\internal |
|
2177 |
*/ |
|
2178 |
||
2179 |
/*! \fn void QTest::sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, char ascii, Qt::KeyboardModifiers modifier, int delay=-1) |
|
2180 |
\internal |
|
2181 |
*/ |
|
2182 |
||
2183 |
/*! \fn void QTest::simulateEvent(QWidget *widget, bool press, int code, Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1) |
|
2184 |
\internal |
|
2185 |
*/ |
|
2186 |
||
2187 |
QT_END_NAMESPACE |