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();
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 QTestLog::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 QTestLog::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 QTestLog::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;
346 //qmltest does not really need the column for data driven test
347 //add this to avoid warnings.
348 d->table->addColumn(qMetaTypeId<QString>(), "qmltest_dummy_data_column");
351 void QuickTestResult::clearTestTable()
353 Q_D(QuickTestResult);
358 void QuickTestResult::finishTestFunction()
360 QTestResult::finishedCurrentTestFunction();
363 static QString qtestFixUrl(const QUrl &location)
365 if (location.isLocalFile()) // Use QUrl's logic for Windows drive letters.
366 return QDir::toNativeSeparators(location.toLocalFile());
367 return location.toString();
370 void QuickTestResult::fail
371 (const QString &message, const QUrl &location, int line)
373 QTestResult::addFailure(message.toLatin1().constData(),
374 qtestFixUrl(location).toLatin1().constData(), line);
377 bool QuickTestResult::verify
378 (bool success, const QString &message, const QUrl &location, int line)
380 if (!success && message.isEmpty()) {
381 return QTestResult::verify
382 (success, "verify()", "",
383 qtestFixUrl(location).toLatin1().constData(), line);
385 return QTestResult::verify
386 (success, message.toLatin1().constData(), "",
387 qtestFixUrl(location).toLatin1().constData(), line);
391 bool QuickTestResult::compare
392 (bool success, const QString &message,
393 const QString &val1, const QString &val2,
394 const QUrl &location, int line)
397 return QTestResult::compare
398 (success, message.toLocal8Bit().constData(),
399 qtestFixUrl(location).toLatin1().constData(), line);
401 return QTestResult::compare
402 (success, message.toLocal8Bit().constData(),
403 QTest::toString(val1.toLatin1().constData()),
404 QTest::toString(val2.toLatin1().constData()),
406 qtestFixUrl(location).toLatin1().constData(), line);
410 void QuickTestResult::skip
411 (const QString &message, const QUrl &location, int line)
413 QTestResult::addSkip(message.toLatin1().constData(),
414 qtestFixUrl(location).toLatin1().constData(), line);
415 QTestResult::setSkipCurrentTest(true);
418 bool QuickTestResult::expectFail
419 (const QString &tag, const QString &comment, const QUrl &location, int line)
421 return QTestResult::expectFail
422 (tag.toLatin1().constData(),
423 QTest::toString(comment.toLatin1().constData()),
424 QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
427 bool QuickTestResult::expectFailContinue
428 (const QString &tag, const QString &comment, const QUrl &location, int line)
430 return QTestResult::expectFail
431 (tag.toLatin1().constData(),
432 QTest::toString(comment.toLatin1().constData()),
433 QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
436 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
438 QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
441 void QuickTestResult::ignoreWarning(const QString &message)
443 QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
446 void QuickTestResult::wait(int ms)
451 void QuickTestResult::sleep(int ms)
456 void QuickTestResult::startMeasurement()
458 Q_D(QuickTestResult);
459 delete d->benchmarkData;
460 d->benchmarkData = new QBenchmarkTestMethodData();
461 QBenchmarkTestMethodData::current = d->benchmarkData;
462 d->iterCount = (QBenchmarkGlobalData::current->measurer->needsWarmupIteration()) ? -1 : 0;
466 void QuickTestResult::beginDataRun()
468 QBenchmarkTestMethodData::current->beginDataRun();
471 void QuickTestResult::endDataRun()
473 Q_D(QuickTestResult);
474 QBenchmarkTestMethodData::current->endDataRun();
475 if (d->iterCount > -1) // iteration -1 is the warmup iteration.
476 d->results.append(QBenchmarkTestMethodData::current->result);
478 if (QBenchmarkGlobalData::current->verboseOutput) {
479 if (d->iterCount == -1) {
480 qDebug() << "warmup stage result :" << QBenchmarkTestMethodData::current->result.value;
482 qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
487 bool QuickTestResult::measurementAccepted()
489 return QBenchmarkTestMethodData::current->resultsAccepted();
492 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
494 const int count = container.count();
496 return QBenchmarkResult();
499 return container.at(0);
501 QList<QBenchmarkResult> containerCopy = container;
502 qSort(containerCopy);
504 const int middle = count / 2;
506 // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
507 return containerCopy.at(middle);
510 bool QuickTestResult::needsMoreMeasurements()
512 Q_D(QuickTestResult);
514 if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
516 if (QBenchmarkTestMethodData::current->resultsAccepted())
517 QTestLog::addBenchmarkResult(qMedian(d->results));
521 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
523 QBenchmarkTestMethodData::current->result = QBenchmarkResult();
524 QBenchmarkTestMethodData::current->resultAccepted = false;
525 QBenchmarkGlobalData::current->context.tag = tag;
526 QBenchmarkGlobalData::current->context.slotName = functionName();
528 Q_D(QuickTestResult);
529 delete d->benchmarkIter;
530 d->benchmarkIter = new QTest::QBenchmarkIterationController
531 (QTest::QBenchmarkIterationController::RunMode(runMode));
534 bool QuickTestResult::isBenchmarkDone() const
536 Q_D(const QuickTestResult);
537 if (d->benchmarkIter)
538 return d->benchmarkIter->isDone();
543 void QuickTestResult::nextBenchmark()
545 Q_D(QuickTestResult);
546 if (d->benchmarkIter)
547 d->benchmarkIter->next();
550 void QuickTestResult::stopBenchmark()
552 Q_D(QuickTestResult);
553 delete d->benchmarkIter;
554 d->benchmarkIter = 0;
558 void qtest_qParseArgs(int argc, char *argv[], bool qml);
561 void QuickTestResult::parseArgs(int argc, char *argv[])
563 if (!QBenchmarkGlobalData::current)
564 QBenchmarkGlobalData::current = &globalBenchmarkData;
565 QTest::qtest_qParseArgs(argc, argv, true);
568 void QuickTestResult::setProgramName(const char *name)
571 QTestResult::reset();
572 } else if (!name && loggingStarted) {
573 QTestResult::setCurrentTestObject(globalProgramName);
574 QTestLog::stopLogging();
575 QTestResult::setCurrentTestObject(0);
577 globalProgramName = name;
578 QTestResult::setCurrentTestObject(globalProgramName);
581 int QuickTestResult::exitCode()
583 #if defined(QTEST_NOEXITCODE)
586 // make sure our exit code is never going above 127
587 // since that could wrap and indicate 0 test fails
588 return qMin(QTestLog::failCount(), 127);