1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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();
167 \qmlproperty string TestResult::dataTag
169 This property defines the tag for the current row in a
170 data-driven test, or an empty string if not a data-driven test.
172 QString QuickTestResult::dataTag() const
174 const char *tag = QTestResult::currentDataTag();
176 return QString::fromUtf8(tag);
181 void QuickTestResult::setDataTag(const QString &tag)
183 if (!tag.isEmpty()) {
184 QTestData *data = &(QTest::newRow(tag.toUtf8().constData()));
185 QTestResult::setCurrentTestData(data);
186 emit dataTagChanged();
188 QTestResult::setCurrentTestData(0);
193 \qmlproperty bool TestResult::failed
195 This property returns true if the current test function (or
196 current test data row for a data-driven test) has failed;
197 false otherwise. The fail state is reset when functionName
198 is changed or finishTestDataCleanup() is called.
202 bool QuickTestResult::isFailed() const
204 return QTestResult::currentTestFailed();
208 \qmlproperty bool TestResult::skipped
210 This property returns true if the current test function was
211 marked as skipped; false otherwise.
215 bool QuickTestResult::isSkipped() const
217 return QTestResult::skipCurrentTest();
220 void QuickTestResult::setSkipped(bool skip)
222 QTestResult::setSkipCurrentTest(skip);
223 emit skippedChanged();
227 \qmlproperty int TestResult::passCount
229 This property returns the number of tests that have passed.
231 \sa failCount, skipCount
233 int QuickTestResult::passCount() const
235 return QTestLog::passCount();
239 \qmlproperty int TestResult::failCount
241 This property returns the number of tests that have failed.
243 \sa passCount, skipCount
245 int QuickTestResult::failCount() const
247 return QTestLog::failCount();
251 \qmlproperty int TestResult::skipCount
253 This property returns the number of tests that have been skipped.
255 \sa passCount, failCount
257 int QuickTestResult::skipCount() const
259 return QTestLog::skipCount();
263 \qmlproperty list<string> TestResult::functionsToRun
265 This property returns the list of function names to be run.
267 QStringList QuickTestResult::functionsToRun() const
269 return QTest::testFunctions;
273 \qmlmethod TestResult::reset()
275 Resets all pass/fail/skip counters and prepare for testing.
277 void QuickTestResult::reset()
279 if (!globalProgramName) // Only if run via qmlviewer.
280 QTestResult::reset();
284 \qmlmethod TestResult::startLogging()
286 Starts logging to the test output stream and writes the
291 void QuickTestResult::startLogging()
293 // The program name is used for logging headers and footers if it
294 // is set. Otherwise the test case name is used.
297 QTestLog::startLogging();
298 loggingStarted = true;
302 \qmlmethod TestResult::stopLogging()
304 Writes the test footer to the test output stream and then stops logging.
308 void QuickTestResult::stopLogging()
310 Q_D(QuickTestResult);
311 if (globalProgramName)
312 return; // Logging will be stopped by setProgramName(0).
313 QTestResult::setCurrentTestObject(d->intern(d->testCaseName).constData());
314 QTestLog::stopLogging();
317 void QuickTestResult::initTestTable()
319 Q_D(QuickTestResult);
321 d->table = new QTestTable;
322 //qmltest does not really need the column for data driven test
323 //add this to avoid warnings.
324 d->table->addColumn(qMetaTypeId<QString>(), "qmltest_dummy_data_column");
327 void QuickTestResult::clearTestTable()
329 Q_D(QuickTestResult);
334 void QuickTestResult::finishTestData()
336 QTestResult::finishedCurrentTestData();
339 void QuickTestResult::finishTestDataCleanup()
341 QTestResult::finishedCurrentTestDataCleanup();
344 void QuickTestResult::finishTestFunction()
346 QTestResult::finishedCurrentTestFunction();
349 static QString qtestFixUrl(const QUrl &location)
351 if (location.isLocalFile()) // Use QUrl's logic for Windows drive letters.
352 return QDir::toNativeSeparators(location.toLocalFile());
353 return location.toString();
356 void QuickTestResult::fail
357 (const QString &message, const QUrl &location, int line)
359 QTestResult::addFailure(message.toLatin1().constData(),
360 qtestFixUrl(location).toLatin1().constData(), line);
363 bool QuickTestResult::verify
364 (bool success, const QString &message, const QUrl &location, int line)
366 if (!success && message.isEmpty()) {
367 return QTestResult::verify
368 (success, "verify()", "",
369 qtestFixUrl(location).toLatin1().constData(), line);
371 return QTestResult::verify
372 (success, message.toLatin1().constData(), "",
373 qtestFixUrl(location).toLatin1().constData(), line);
377 bool QuickTestResult::compare
378 (bool success, const QString &message,
379 const QString &val1, const QString &val2,
380 const QUrl &location, int line)
383 return QTestResult::compare
384 (success, message.toLocal8Bit().constData(),
385 qtestFixUrl(location).toLatin1().constData(), line);
387 return QTestResult::compare
388 (success, message.toLocal8Bit().constData(),
389 QTest::toString(val1.toLatin1().constData()),
390 QTest::toString(val2.toLatin1().constData()),
392 qtestFixUrl(location).toLatin1().constData(), line);
396 void QuickTestResult::skip
397 (const QString &message, const QUrl &location, int line)
399 QTestResult::addSkip(message.toLatin1().constData(),
400 qtestFixUrl(location).toLatin1().constData(), line);
401 QTestResult::setSkipCurrentTest(true);
404 bool QuickTestResult::expectFail
405 (const QString &tag, const QString &comment, const QUrl &location, int line)
407 return QTestResult::expectFail
408 (tag.toLatin1().constData(),
409 QTest::toString(comment.toLatin1().constData()),
410 QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
413 bool QuickTestResult::expectFailContinue
414 (const QString &tag, const QString &comment, const QUrl &location, int line)
416 return QTestResult::expectFail
417 (tag.toLatin1().constData(),
418 QTest::toString(comment.toLatin1().constData()),
419 QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
422 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
424 QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
427 void QuickTestResult::ignoreWarning(const QString &message)
429 QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
432 void QuickTestResult::wait(int ms)
437 void QuickTestResult::sleep(int ms)
442 void QuickTestResult::startMeasurement()
444 Q_D(QuickTestResult);
445 delete d->benchmarkData;
446 d->benchmarkData = new QBenchmarkTestMethodData();
447 QBenchmarkTestMethodData::current = d->benchmarkData;
448 d->iterCount = (QBenchmarkGlobalData::current->measurer->needsWarmupIteration()) ? -1 : 0;
452 void QuickTestResult::beginDataRun()
454 QBenchmarkTestMethodData::current->beginDataRun();
457 void QuickTestResult::endDataRun()
459 Q_D(QuickTestResult);
460 QBenchmarkTestMethodData::current->endDataRun();
461 if (d->iterCount > -1) // iteration -1 is the warmup iteration.
462 d->results.append(QBenchmarkTestMethodData::current->result);
464 if (QBenchmarkGlobalData::current->verboseOutput) {
465 if (d->iterCount == -1) {
466 qDebug() << "warmup stage result :" << QBenchmarkTestMethodData::current->result.value;
468 qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
473 bool QuickTestResult::measurementAccepted()
475 return QBenchmarkTestMethodData::current->resultsAccepted();
478 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
480 const int count = container.count();
482 return QBenchmarkResult();
485 return container.at(0);
487 QList<QBenchmarkResult> containerCopy = container;
488 qSort(containerCopy);
490 const int middle = count / 2;
492 // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
493 return containerCopy.at(middle);
496 bool QuickTestResult::needsMoreMeasurements()
498 Q_D(QuickTestResult);
500 if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
502 if (QBenchmarkTestMethodData::current->resultsAccepted())
503 QTestLog::addBenchmarkResult(qMedian(d->results));
507 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
509 QBenchmarkTestMethodData::current->result = QBenchmarkResult();
510 QBenchmarkTestMethodData::current->resultAccepted = false;
511 QBenchmarkGlobalData::current->context.tag = tag;
512 QBenchmarkGlobalData::current->context.slotName = functionName();
514 Q_D(QuickTestResult);
515 delete d->benchmarkIter;
516 d->benchmarkIter = new QTest::QBenchmarkIterationController
517 (QTest::QBenchmarkIterationController::RunMode(runMode));
520 bool QuickTestResult::isBenchmarkDone() const
522 Q_D(const QuickTestResult);
523 if (d->benchmarkIter)
524 return d->benchmarkIter->isDone();
529 void QuickTestResult::nextBenchmark()
531 Q_D(QuickTestResult);
532 if (d->benchmarkIter)
533 d->benchmarkIter->next();
536 void QuickTestResult::stopBenchmark()
538 Q_D(QuickTestResult);
539 delete d->benchmarkIter;
540 d->benchmarkIter = 0;
544 void qtest_qParseArgs(int argc, char *argv[], bool qml);
547 void QuickTestResult::parseArgs(int argc, char *argv[])
549 if (!QBenchmarkGlobalData::current)
550 QBenchmarkGlobalData::current = &globalBenchmarkData;
551 QTest::qtest_qParseArgs(argc, argv, true);
554 void QuickTestResult::setProgramName(const char *name)
557 QTestResult::reset();
558 } else if (!name && loggingStarted) {
559 QTestResult::setCurrentTestObject(globalProgramName);
560 QTestLog::stopLogging();
561 QTestResult::setCurrentTestObject(0);
563 globalProgramName = name;
564 QTestResult::setCurrentTestObject(globalProgramName);
567 int QuickTestResult::exitCode()
569 #if defined(QTEST_NOEXITCODE)
572 // make sure our exit code is never going above 127
573 // since that could wrap and indicate 0 test fails
574 return qMin(QTestLog::failCount(), 127);