CodeCoverage: Fixes regression.
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestresult.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
58
59 QT_BEGIN_NAMESPACE
60
61 static const char *globalProgramName = 0;
62 static bool loggingStarted = false;
63 static QBenchmarkGlobalData globalBenchmarkData;
64
65 class QuickTestResultPrivate
66 {
67 public:
68     QuickTestResultPrivate()
69         : table(0)
70         , benchmarkIter(0)
71         , benchmarkData(0)
72         , iterCount(0)
73     {
74     }
75     ~QuickTestResultPrivate()
76     {
77         delete table;
78         delete benchmarkIter;
79         delete benchmarkData;
80     }
81
82     QByteArray intern(const QString &str);
83
84     QString testCaseName;
85     QString functionName;
86     QSet<QByteArray> internedStrings;
87     QTestTable *table;
88     QTest::QBenchmarkIterationController *benchmarkIter;
89     QBenchmarkTestMethodData *benchmarkData;
90     int iterCount;
91     QList<QBenchmarkResult> results;
92 };
93
94 QByteArray QuickTestResultPrivate::intern(const QString &str)
95 {
96     QByteArray bstr = str.toUtf8();
97     return *(internedStrings.insert(bstr));
98 }
99
100 QuickTestResult::QuickTestResult(QObject *parent)
101     : QObject(parent), d_ptr(new QuickTestResultPrivate)
102 {
103     if (!QBenchmarkGlobalData::current)
104         QBenchmarkGlobalData::current = &globalBenchmarkData;
105 }
106
107 QuickTestResult::~QuickTestResult()
108 {
109 }
110
111 /*!
112     \qmlproperty string TestResult::testCaseName
113
114     This property defines the name of current TestCase element
115     that is running test cases.
116
117     \sa functionName
118 */
119 QString QuickTestResult::testCaseName() const
120 {
121     Q_D(const QuickTestResult);
122     return d->testCaseName;
123 }
124
125 void QuickTestResult::setTestCaseName(const QString &name)
126 {
127     Q_D(QuickTestResult);
128     d->testCaseName = name;
129     emit testCaseNameChanged();
130 }
131
132 /*!
133     \qmlproperty string TestResult::functionName
134
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.
138
139     \sa testCaseName
140 */
141 QString QuickTestResult::functionName() const
142 {
143     Q_D(const QuickTestResult);
144     return d->functionName;
145 }
146
147 void QuickTestResult::setFunctionName(const QString &name)
148 {
149     Q_D(QuickTestResult);
150     if (!name.isEmpty()) {
151         if (d->testCaseName.isEmpty()) {
152             QTestResult::setCurrentTestFunction
153                 (d->intern(name).constData());
154         } else {
155             QString fullName = d->testCaseName + QLatin1String("::") + name;
156             QTestResult::setCurrentTestFunction
157                 (d->intern(fullName).constData());
158         }
159     } else {
160         QTestResult::setCurrentTestFunction(0);
161     }
162     d->functionName = name;
163     emit functionNameChanged();
164 }
165
166 /*!
167     \qmlproperty string TestResult::dataTag
168
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.
171 */
172 QString QuickTestResult::dataTag() const
173 {
174     const char *tag = QTestResult::currentDataTag();
175     if (tag)
176         return QString::fromUtf8(tag);
177     else
178         return QString();
179 }
180
181 void QuickTestResult::setDataTag(const QString &tag)
182 {
183     if (!tag.isEmpty()) {
184         QTestData *data = &(QTest::newRow(tag.toUtf8().constData()));
185         QTestResult::setCurrentTestData(data);
186         emit dataTagChanged();
187     } else {
188         QTestResult::setCurrentTestData(0);
189     }
190 }
191
192 /*!
193     \qmlproperty bool TestResult::failed
194
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.
199
200     \sa skipped
201 */
202 bool QuickTestResult::isFailed() const
203 {
204     return QTestResult::currentTestFailed();
205 }
206
207 /*!
208     \qmlproperty bool TestResult::skipped
209
210     This property returns true if the current test function was
211     marked as skipped; false otherwise.
212
213     \sa failed
214 */
215 bool QuickTestResult::isSkipped() const
216 {
217     return QTestResult::skipCurrentTest();
218 }
219
220 void QuickTestResult::setSkipped(bool skip)
221 {
222     QTestResult::setSkipCurrentTest(skip);
223     emit skippedChanged();
224 }
225
226 /*!
227     \qmlproperty int TestResult::passCount
228
229     This property returns the number of tests that have passed.
230
231     \sa failCount, skipCount
232 */
233 int QuickTestResult::passCount() const
234 {
235     return QTestLog::passCount();
236 }
237
238 /*!
239     \qmlproperty int TestResult::failCount
240
241     This property returns the number of tests that have failed.
242
243     \sa passCount, skipCount
244 */
245 int QuickTestResult::failCount() const
246 {
247     return QTestLog::failCount();
248 }
249
250 /*!
251     \qmlproperty int TestResult::skipCount
252
253     This property returns the number of tests that have been skipped.
254
255     \sa passCount, failCount
256 */
257 int QuickTestResult::skipCount() const
258 {
259     return QTestLog::skipCount();
260 }
261
262 /*!
263     \qmlproperty list<string> TestResult::functionsToRun
264
265     This property returns the list of function names to be run.
266 */
267 QStringList QuickTestResult::functionsToRun() const
268 {
269     return QTest::testFunctions;
270 }
271
272 /*!
273     \qmlmethod TestResult::reset()
274
275     Resets all pass/fail/skip counters and prepare for testing.
276 */
277 void QuickTestResult::reset()
278 {
279     if (!globalProgramName)     // Only if run via qmlviewer.
280         QTestResult::reset();
281 }
282
283 /*!
284     \qmlmethod TestResult::startLogging()
285
286     Starts logging to the test output stream and writes the
287     test header.
288
289     \sa stopLogging()
290 */
291 void QuickTestResult::startLogging()
292 {
293     // The program name is used for logging headers and footers if it
294     // is set.  Otherwise the test case name is used.
295     if (loggingStarted)
296         return;
297     QTestLog::startLogging();
298     loggingStarted = true;
299 }
300
301 /*!
302     \qmlmethod TestResult::stopLogging()
303
304     Writes the test footer to the test output stream and then stops logging.
305
306     \sa startLogging()
307 */
308 void QuickTestResult::stopLogging()
309 {
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();
315 }
316
317 void QuickTestResult::initTestTable()
318 {
319     Q_D(QuickTestResult);
320     delete d->table;
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");
325 }
326
327 void QuickTestResult::clearTestTable()
328 {
329     Q_D(QuickTestResult);
330     delete d->table;
331     d->table = 0;
332 }
333
334 void QuickTestResult::finishTestData()
335 {
336     QTestResult::finishedCurrentTestData();
337 }
338
339 void QuickTestResult::finishTestDataCleanup()
340 {
341     QTestResult::finishedCurrentTestDataCleanup();
342 }
343
344 void QuickTestResult::finishTestFunction()
345 {
346     QTestResult::finishedCurrentTestFunction();
347 }
348
349 static QString qtestFixUrl(const QUrl &location)
350 {
351     if (location.isLocalFile()) // Use QUrl's logic for Windows drive letters.
352         return QDir::toNativeSeparators(location.toLocalFile());
353     return location.toString();
354 }
355
356 void QuickTestResult::fail
357     (const QString &message, const QUrl &location, int line)
358 {
359     QTestResult::addFailure(message.toLatin1().constData(),
360                             qtestFixUrl(location).toLatin1().constData(), line);
361 }
362
363 bool QuickTestResult::verify
364     (bool success, const QString &message, const QUrl &location, int line)
365 {
366     if (!success && message.isEmpty()) {
367         return QTestResult::verify
368             (success, "verify()", "",
369              qtestFixUrl(location).toLatin1().constData(), line);
370     } else {
371         return QTestResult::verify
372             (success, message.toLatin1().constData(), "",
373              qtestFixUrl(location).toLatin1().constData(), line);
374     }
375 }
376
377 bool QuickTestResult::compare
378     (bool success, const QString &message,
379      const QString &val1, const QString &val2,
380      const QUrl &location, int line)
381 {
382     if (success) {
383         return QTestResult::compare
384             (success, message.toLocal8Bit().constData(),
385              qtestFixUrl(location).toLatin1().constData(), line);
386     } else {
387         return QTestResult::compare
388             (success, message.toLocal8Bit().constData(),
389              QTest::toString(val1.toLatin1().constData()),
390              QTest::toString(val2.toLatin1().constData()),
391              "", "",
392              qtestFixUrl(location).toLatin1().constData(), line);
393     }
394 }
395
396 void QuickTestResult::skip
397     (const QString &message, const QUrl &location, int line)
398 {
399     QTestResult::addSkip(message.toLatin1().constData(),
400                          qtestFixUrl(location).toLatin1().constData(), line);
401     QTestResult::setSkipCurrentTest(true);
402 }
403
404 bool QuickTestResult::expectFail
405     (const QString &tag, const QString &comment, const QUrl &location, int line)
406 {
407     return QTestResult::expectFail
408         (tag.toLatin1().constData(),
409          QTest::toString(comment.toLatin1().constData()),
410          QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
411 }
412
413 bool QuickTestResult::expectFailContinue
414     (const QString &tag, const QString &comment, const QUrl &location, int line)
415 {
416     return QTestResult::expectFail
417         (tag.toLatin1().constData(),
418          QTest::toString(comment.toLatin1().constData()),
419          QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
420 }
421
422 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
423 {
424     QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
425 }
426
427 void QuickTestResult::ignoreWarning(const QString &message)
428 {
429     QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
430 }
431
432 void QuickTestResult::wait(int ms)
433 {
434     QTest::qWait(ms);
435 }
436
437 void QuickTestResult::sleep(int ms)
438 {
439     QTest::qSleep(ms);
440 }
441
442 void QuickTestResult::startMeasurement()
443 {
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;
449     d->results.clear();
450 }
451
452 void QuickTestResult::beginDataRun()
453 {
454     QBenchmarkTestMethodData::current->beginDataRun();
455 }
456
457 void QuickTestResult::endDataRun()
458 {
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);
463
464     if (QBenchmarkGlobalData::current->verboseOutput) {
465         if (d->iterCount == -1) {
466             qDebug() << "warmup stage result      :" << QBenchmarkTestMethodData::current->result.value;
467         } else {
468             qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
469         }
470     }
471 }
472
473 bool QuickTestResult::measurementAccepted()
474 {
475     return QBenchmarkTestMethodData::current->resultsAccepted();
476 }
477
478 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
479 {
480     const int count = container.count();
481     if (count == 0)
482         return QBenchmarkResult();
483
484     if (count == 1)
485         return container.at(0);
486
487     QList<QBenchmarkResult> containerCopy = container;
488     qSort(containerCopy);
489
490     const int middle = count / 2;
491
492     // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
493     return containerCopy.at(middle);
494 }
495
496 bool QuickTestResult::needsMoreMeasurements()
497 {
498     Q_D(QuickTestResult);
499     ++(d->iterCount);
500     if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
501         return true;
502     if (QBenchmarkTestMethodData::current->resultsAccepted())
503         QTestLog::addBenchmarkResult(qMedian(d->results));
504     return false;
505 }
506
507 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
508 {
509     QBenchmarkTestMethodData::current->result = QBenchmarkResult();
510     QBenchmarkTestMethodData::current->resultAccepted = false;
511     QBenchmarkGlobalData::current->context.tag = tag;
512     QBenchmarkGlobalData::current->context.slotName = functionName();
513
514     Q_D(QuickTestResult);
515     delete d->benchmarkIter;
516     d->benchmarkIter = new QTest::QBenchmarkIterationController
517         (QTest::QBenchmarkIterationController::RunMode(runMode));
518 }
519
520 bool QuickTestResult::isBenchmarkDone() const
521 {
522     Q_D(const QuickTestResult);
523     if (d->benchmarkIter)
524         return d->benchmarkIter->isDone();
525     else
526         return true;
527 }
528
529 void QuickTestResult::nextBenchmark()
530 {
531     Q_D(QuickTestResult);
532     if (d->benchmarkIter)
533         d->benchmarkIter->next();
534 }
535
536 void QuickTestResult::stopBenchmark()
537 {
538     Q_D(QuickTestResult);
539     delete d->benchmarkIter;
540     d->benchmarkIter = 0;
541 }
542
543 namespace QTest {
544     void qtest_qParseArgs(int argc, char *argv[], bool qml);
545 };
546
547 void QuickTestResult::parseArgs(int argc, char *argv[])
548 {
549     if (!QBenchmarkGlobalData::current)
550         QBenchmarkGlobalData::current = &globalBenchmarkData;
551     QTest::qtest_qParseArgs(argc, argv, true);
552 }
553
554 void QuickTestResult::setProgramName(const char *name)
555 {
556     if (name) {
557         QTestResult::reset();
558     } else if (!name && loggingStarted) {
559         QTestResult::setCurrentTestObject(globalProgramName);
560         QTestLog::stopLogging();
561         QTestResult::setCurrentTestObject(0);
562     }
563     globalProgramName = name;
564     QTestResult::setCurrentTestObject(globalProgramName);
565 }
566
567 void QuickTestResult::setCurrentAppname(const char *appname)
568 {
569     QTestResult::setCurrentAppname(appname);
570 }
571
572 int QuickTestResult::exitCode()
573 {
574 #if defined(QTEST_NOEXITCODE)
575     return 0;
576 #else
577     // make sure our exit code is never going above 127
578     // since that could wrap and indicate 0 test fails
579     return qMin(QTestLog::failCount(), 127);
580 #endif
581 }
582
583 QT_END_NAMESPACE