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)
382 return QTestResult::compare
383 (success, message.toLocal8Bit().constData(),
384 QTest::toString(val1.toLatin1().constData()),
385 QTest::toString(val2.toLatin1().constData()),
387 qtestFixUrl(location).toLatin1().constData(), line);
390 void QuickTestResult::skip
391 (const QString &message, const QUrl &location, int line)
393 QTestResult::addSkip(message.toLatin1().constData(),
394 qtestFixUrl(location).toLatin1().constData(), line);
395 QTestResult::setSkipCurrentTest(true);
398 bool QuickTestResult::expectFail
399 (const QString &tag, const QString &comment, const QUrl &location, int line)
401 return QTestResult::expectFail
402 (tag.toLatin1().constData(),
403 QTest::toString(comment.toLatin1().constData()),
404 QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
407 bool QuickTestResult::expectFailContinue
408 (const QString &tag, const QString &comment, const QUrl &location, int line)
410 return QTestResult::expectFail
411 (tag.toLatin1().constData(),
412 QTest::toString(comment.toLatin1().constData()),
413 QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
416 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
418 QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
421 void QuickTestResult::ignoreWarning(const QString &message)
423 QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
426 void QuickTestResult::wait(int ms)
431 void QuickTestResult::sleep(int ms)
436 void QuickTestResult::startMeasurement()
438 Q_D(QuickTestResult);
439 delete d->benchmarkData;
440 d->benchmarkData = new QBenchmarkTestMethodData();
441 QBenchmarkTestMethodData::current = d->benchmarkData;
442 d->iterCount = (QBenchmarkGlobalData::current->measurer->needsWarmupIteration()) ? -1 : 0;
446 void QuickTestResult::beginDataRun()
448 QBenchmarkTestMethodData::current->beginDataRun();
451 void QuickTestResult::endDataRun()
453 Q_D(QuickTestResult);
454 QBenchmarkTestMethodData::current->endDataRun();
455 if (d->iterCount > -1) // iteration -1 is the warmup iteration.
456 d->results.append(QBenchmarkTestMethodData::current->result);
458 if (QBenchmarkGlobalData::current->verboseOutput) {
459 if (d->iterCount == -1) {
460 qDebug() << "warmup stage result :" << QBenchmarkTestMethodData::current->result.value;
462 qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
467 bool QuickTestResult::measurementAccepted()
469 return QBenchmarkTestMethodData::current->resultsAccepted();
472 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
474 const int count = container.count();
476 return QBenchmarkResult();
479 return container.at(0);
481 QList<QBenchmarkResult> containerCopy = container;
482 qSort(containerCopy);
484 const int middle = count / 2;
486 // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
487 return containerCopy.at(middle);
490 bool QuickTestResult::needsMoreMeasurements()
492 Q_D(QuickTestResult);
494 if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
496 if (QBenchmarkTestMethodData::current->resultsAccepted())
497 QTestLog::addBenchmarkResult(qMedian(d->results));
501 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
503 QBenchmarkTestMethodData::current->result = QBenchmarkResult();
504 QBenchmarkTestMethodData::current->resultAccepted = false;
505 QBenchmarkGlobalData::current->context.tag = tag;
506 QBenchmarkGlobalData::current->context.slotName = functionName();
508 Q_D(QuickTestResult);
509 delete d->benchmarkIter;
510 d->benchmarkIter = new QTest::QBenchmarkIterationController
511 (QTest::QBenchmarkIterationController::RunMode(runMode));
514 bool QuickTestResult::isBenchmarkDone() const
516 Q_D(const QuickTestResult);
517 if (d->benchmarkIter)
518 return d->benchmarkIter->isDone();
523 void QuickTestResult::nextBenchmark()
525 Q_D(QuickTestResult);
526 if (d->benchmarkIter)
527 d->benchmarkIter->next();
530 void QuickTestResult::stopBenchmark()
532 Q_D(QuickTestResult);
533 delete d->benchmarkIter;
534 d->benchmarkIter = 0;
538 void qtest_qParseArgs(int argc, char *argv[], bool qml);
541 void QuickTestResult::parseArgs(int argc, char *argv[])
543 if (!QBenchmarkGlobalData::current)
544 QBenchmarkGlobalData::current = &globalBenchmarkData;
545 QTest::qtest_qParseArgs(argc, argv, true);
548 void QuickTestResult::setProgramName(const char *name)
551 QTestResult::reset();
552 } else if (!name && loggingStarted) {
553 QTestResult::setCurrentTestObject(globalProgramName);
554 QTestLog::stopLogging();
555 QTestResult::setCurrentTestObject(0);
557 globalProgramName = name;
558 QTestResult::setCurrentTestObject(globalProgramName);
561 void QuickTestResult::setCurrentAppname(const char *appname)
563 QTestResult::setCurrentAppname(appname);
566 int QuickTestResult::exitCode()
568 #if defined(QTEST_NOEXITCODE)
571 // make sure our exit code is never going above 127
572 // since that could wrap and indicate 0 test fails
573 return qMin(QTestLog::failCount(), 127);