1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "quicktestresult_p.h"
43 #include <QtTest/qtestcase.h>
44 #include <QtTest/qtestsystem.h>
45 #include <QtTest/private/qtestresult_p.h>
46 #include <QtTest/private/qtesttable_p.h>
47 #include <QtTest/private/qtestlog_p.h>
48 #include "qtestoptions_p.h"
49 #include <QtTest/qbenchmark.h>
50 #include <QtTest/private/qbenchmark_p.h>
51 #include <QtCore/qset.h>
52 #include <QtCore/qmap.h>
53 #include <QtCore/qbytearray.h>
54 #include <QtCore/qcoreapplication.h>
55 #include <QtCore/qdebug.h>
56 #include <QtCore/QUrl>
57 #include <QtCore/QDir>
61 static const char *globalProgramName = 0;
62 static bool loggingStarted = false;
63 static QBenchmarkGlobalData globalBenchmarkData;
65 class QuickTestResultPrivate
68 QuickTestResultPrivate()
75 ~QuickTestResultPrivate()
82 QByteArray intern(const QString &str);
86 QSet<QByteArray> internedStrings;
88 QTest::QBenchmarkIterationController *benchmarkIter;
89 QBenchmarkTestMethodData *benchmarkData;
91 QList<QBenchmarkResult> results;
94 QByteArray QuickTestResultPrivate::intern(const QString &str)
96 QByteArray bstr = str.toUtf8();
97 return *(internedStrings.insert(bstr));
100 QuickTestResult::QuickTestResult(QObject *parent)
101 : QObject(parent), d_ptr(new QuickTestResultPrivate)
103 if (!QBenchmarkGlobalData::current)
104 QBenchmarkGlobalData::current = &globalBenchmarkData;
107 QuickTestResult::~QuickTestResult()
112 \qmlproperty string TestResult::testCaseName
114 This property defines the name of current TestCase element
115 that is running test cases.
119 QString QuickTestResult::testCaseName() const
121 Q_D(const QuickTestResult);
122 return d->testCaseName;
125 void QuickTestResult::setTestCaseName(const QString &name)
127 Q_D(QuickTestResult);
128 d->testCaseName = name;
129 emit testCaseNameChanged();
133 \qmlproperty string TestResult::functionName
135 This property defines the name of current test function
136 within a TestCase element that is running. If this string is
137 empty, then no function is currently running.
141 QString QuickTestResult::functionName() const
143 Q_D(const QuickTestResult);
144 return d->functionName;
147 void QuickTestResult::setFunctionName(const QString &name)
149 Q_D(QuickTestResult);
150 if (!name.isEmpty()) {
151 if (d->testCaseName.isEmpty()) {
152 QTestResult::setCurrentTestFunction
153 (d->intern(name).constData());
155 QString fullName = d->testCaseName + QLatin1String("::") + name;
156 QTestResult::setCurrentTestFunction
157 (d->intern(fullName).constData());
160 QTestResult::setCurrentTestFunction(0);
162 d->functionName = name;
163 emit functionNameChanged();
166 QuickTestResult::FunctionType QuickTestResult::functionType() const
168 return FunctionType(QTestResult::currentTestLocation());
171 void QuickTestResult::setFunctionType(FunctionType type)
173 QTestResult::setCurrentTestLocation(QTestResult::TestLocation(type));
174 emit functionTypeChanged();
178 \qmlproperty string TestResult::dataTag
180 This property defines the tag for the current row in a
181 data-driven test, or an empty string if not a data-driven test.
183 QString QuickTestResult::dataTag() const
185 const char *tag = QTestResult::currentDataTag();
187 return QString::fromUtf8(tag);
192 void QuickTestResult::setDataTag(const QString &tag)
194 if (!tag.isEmpty()) {
195 QTestData *data = &(QTest::newRow(tag.toUtf8().constData()));
196 QTestResult::setCurrentTestData(data);
197 emit dataTagChanged();
199 QTestResult::setCurrentTestData(0);
204 \qmlproperty bool TestResult::failed
206 This property returns true if the current test function has
207 failed; false otherwise. The fail state is reset when
208 functionName is changed or finishTestFunction() is called.
210 \sa skipped, dataFailed
212 bool QuickTestResult::isFailed() const
214 return QTestResult::testFailed();
218 \qmlproperty bool TestResult::dataFailed
220 This property returns true if the current data function has
221 failed; false otherwise. The fail state is reset when
222 functionName is changed or finishTestFunction() is called.
226 bool QuickTestResult::isDataFailed() const
228 return QTestResult::currentTestFailed();
232 \qmlproperty bool TestResult::skipped
234 This property returns true if the current test function was
235 marked as skipped; false otherwise.
239 bool QuickTestResult::isSkipped() const
241 return QTestResult::skipCurrentTest();
244 void QuickTestResult::setSkipped(bool skip)
246 QTestResult::setSkipCurrentTest(skip);
247 emit skippedChanged();
251 \qmlproperty int TestResult::passCount
253 This property returns the number of tests that have passed.
255 \sa failCount, skipCount
257 int QuickTestResult::passCount() const
259 return QTestResult::passCount();
263 \qmlproperty int TestResult::failCount
265 This property returns the number of tests that have failed.
267 \sa passCount, skipCount
269 int QuickTestResult::failCount() const
271 return QTestResult::failCount();
275 \qmlproperty int TestResult::skipCount
277 This property returns the number of tests that have been skipped.
279 \sa passCount, failCount
281 int QuickTestResult::skipCount() const
283 return QTestResult::skipCount();
287 \qmlproperty list<string> TestResult::functionsToRun
289 This property returns the list of function names to be run.
291 QStringList QuickTestResult::functionsToRun() const
293 return QTest::testFunctions;
297 \qmlmethod TestResult::reset()
299 Resets all pass/fail/skip counters and prepare for testing.
301 void QuickTestResult::reset()
303 if (!globalProgramName) // Only if run via qmlviewer.
304 QTestResult::reset();
308 \qmlmethod TestResult::startLogging()
310 Starts logging to the test output stream and writes the
315 void QuickTestResult::startLogging()
317 // The program name is used for logging headers and footers if it
318 // is set. Otherwise the test case name is used.
321 QTestLog::startLogging();
322 loggingStarted = true;
326 \qmlmethod TestResult::stopLogging()
328 Writes the test footer to the test output stream and then stops logging.
332 void QuickTestResult::stopLogging()
334 Q_D(QuickTestResult);
335 if (globalProgramName)
336 return; // Logging will be stopped by setProgramName(0).
337 QTestResult::setCurrentTestObject(d->intern(d->testCaseName).constData());
338 QTestLog::stopLogging();
341 void QuickTestResult::initTestTable()
343 Q_D(QuickTestResult);
345 d->table = new QTestTable;
348 void QuickTestResult::clearTestTable()
350 Q_D(QuickTestResult);
355 void QuickTestResult::finishTestFunction()
357 QTestResult::finishedCurrentTestFunction();
360 static QString qtestFixUrl(const QUrl &location)
362 if (location.isLocalFile()) // Use QUrl's logic for Windows drive letters.
363 return QDir::toNativeSeparators(location.toLocalFile());
364 return location.toString();
367 void QuickTestResult::fail
368 (const QString &message, const QUrl &location, int line)
370 QTestResult::addFailure(message.toLatin1().constData(),
371 qtestFixUrl(location).toLatin1().constData(), line);
374 bool QuickTestResult::verify
375 (bool success, const QString &message, const QUrl &location, int line)
377 if (!success && message.isEmpty()) {
378 return QTestResult::verify
379 (success, "verify()", "",
380 qtestFixUrl(location).toLatin1().constData(), line);
382 return QTestResult::verify
383 (success, message.toLatin1().constData(), "",
384 qtestFixUrl(location).toLatin1().constData(), line);
388 bool QuickTestResult::compare
389 (bool success, const QString &message,
390 const QString &val1, const QString &val2,
391 const QUrl &location, int line)
394 return QTestResult::compare
395 (success, message.toLocal8Bit().constData(),
396 qtestFixUrl(location).toLatin1().constData(), line);
398 return QTestResult::compare
399 (success, message.toLocal8Bit().constData(),
400 QTest::toString(val1.toLatin1().constData()),
401 QTest::toString(val2.toLatin1().constData()),
403 qtestFixUrl(location).toLatin1().constData(), line);
407 void QuickTestResult::skip
408 (const QString &message, const QUrl &location, int line)
410 QTestResult::addSkip(message.toLatin1().constData(),
411 qtestFixUrl(location).toLatin1().constData(), line);
412 QTestResult::setSkipCurrentTest(true);
415 bool QuickTestResult::expectFail
416 (const QString &tag, const QString &comment, const QUrl &location, int line)
418 return QTestResult::expectFail
419 (tag.toLatin1().constData(),
420 QTest::toString(comment.toLatin1().constData()),
421 QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
424 bool QuickTestResult::expectFailContinue
425 (const QString &tag, const QString &comment, const QUrl &location, int line)
427 return QTestResult::expectFail
428 (tag.toLatin1().constData(),
429 QTest::toString(comment.toLatin1().constData()),
430 QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
433 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
435 QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
438 void QuickTestResult::ignoreWarning(const QString &message)
440 QTestResult::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
443 void QuickTestResult::wait(int ms)
448 void QuickTestResult::sleep(int ms)
453 void QuickTestResult::startMeasurement()
455 Q_D(QuickTestResult);
456 delete d->benchmarkData;
457 d->benchmarkData = new QBenchmarkTestMethodData();
458 QBenchmarkTestMethodData::current = d->benchmarkData;
459 d->iterCount = (QBenchmarkGlobalData::current->measurer->needsWarmupIteration()) ? -1 : 0;
463 void QuickTestResult::beginDataRun()
465 QBenchmarkTestMethodData::current->beginDataRun();
468 void QuickTestResult::endDataRun()
470 Q_D(QuickTestResult);
471 QBenchmarkTestMethodData::current->endDataRun();
472 if (d->iterCount > -1) // iteration -1 is the warmup iteration.
473 d->results.append(QBenchmarkTestMethodData::current->result);
475 if (QBenchmarkGlobalData::current->verboseOutput) {
476 if (d->iterCount == -1) {
477 qDebug() << "warmup stage result :" << QBenchmarkTestMethodData::current->result.value;
479 qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
484 bool QuickTestResult::measurementAccepted()
486 return QBenchmarkTestMethodData::current->resultsAccepted();
489 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
491 const int count = container.count();
493 return QBenchmarkResult();
496 return container.at(0);
498 QList<QBenchmarkResult> containerCopy = container;
499 qSort(containerCopy);
501 const int middle = count / 2;
503 // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
504 return containerCopy.at(middle);
507 bool QuickTestResult::needsMoreMeasurements()
509 Q_D(QuickTestResult);
511 if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
513 if (QBenchmarkTestMethodData::current->resultsAccepted())
514 QTestLog::addBenchmarkResult(qMedian(d->results));
518 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
520 QBenchmarkTestMethodData::current->result = QBenchmarkResult();
521 QBenchmarkTestMethodData::current->resultAccepted = false;
522 QBenchmarkGlobalData::current->context.tag = tag;
523 QBenchmarkGlobalData::current->context.slotName = functionName();
525 Q_D(QuickTestResult);
526 delete d->benchmarkIter;
527 d->benchmarkIter = new QTest::QBenchmarkIterationController
528 (QTest::QBenchmarkIterationController::RunMode(runMode));
531 bool QuickTestResult::isBenchmarkDone() const
533 Q_D(const QuickTestResult);
534 if (d->benchmarkIter)
535 return d->benchmarkIter->isDone();
540 void QuickTestResult::nextBenchmark()
542 Q_D(QuickTestResult);
543 if (d->benchmarkIter)
544 d->benchmarkIter->next();
547 void QuickTestResult::stopBenchmark()
549 Q_D(QuickTestResult);
550 delete d->benchmarkIter;
551 d->benchmarkIter = 0;
555 void qtest_qParseArgs(int argc, char *argv[], bool qml);
558 void QuickTestResult::parseArgs(int argc, char *argv[])
560 if (!QBenchmarkGlobalData::current)
561 QBenchmarkGlobalData::current = &globalBenchmarkData;
562 QTest::qtest_qParseArgs(argc, argv, true);
565 void QuickTestResult::setProgramName(const char *name)
568 QTestResult::reset();
569 } else if (!name && loggingStarted) {
570 QTestResult::setCurrentTestObject(globalProgramName);
571 QTestLog::stopLogging();
572 QTestResult::setCurrentTestObject(0);
574 globalProgramName = name;
575 QTestResult::setCurrentTestObject(globalProgramName);
578 int QuickTestResult::exitCode()
580 #if defined(QTEST_NOEXITCODE)
583 // make sure our exit code is never going above 127
584 // since that could wrap and indicate 0 test fails
585 return qMin(QTestResult::failCount(), 127);