Merge branch 'master' of git://gitorious.org/qt/qtdeclarative into merge-master
[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     return QTestResult::compare
383         (success, message.toLocal8Bit().constData(),
384          QTest::toString(val1.toLatin1().constData()),
385          QTest::toString(val2.toLatin1().constData()),
386          "", "",
387          qtestFixUrl(location).toLatin1().constData(), line);
388 }
389
390 void QuickTestResult::skip
391     (const QString &message, const QUrl &location, int line)
392 {
393     QTestResult::addSkip(message.toLatin1().constData(),
394                          qtestFixUrl(location).toLatin1().constData(), line);
395     QTestResult::setSkipCurrentTest(true);
396 }
397
398 bool QuickTestResult::expectFail
399     (const QString &tag, const QString &comment, const QUrl &location, int line)
400 {
401     return QTestResult::expectFail
402         (tag.toLatin1().constData(),
403          QTest::toString(comment.toLatin1().constData()),
404          QTest::Abort, qtestFixUrl(location).toLatin1().constData(), line);
405 }
406
407 bool QuickTestResult::expectFailContinue
408     (const QString &tag, const QString &comment, const QUrl &location, int line)
409 {
410     return QTestResult::expectFail
411         (tag.toLatin1().constData(),
412          QTest::toString(comment.toLatin1().constData()),
413          QTest::Continue, qtestFixUrl(location).toLatin1().constData(), line);
414 }
415
416 void QuickTestResult::warn(const QString &message, const QUrl &location, int line)
417 {
418     QTestLog::warn(message.toLatin1().constData(), qtestFixUrl(location).toLatin1().constData(), line);
419 }
420
421 void QuickTestResult::ignoreWarning(const QString &message)
422 {
423     QTestLog::ignoreMessage(QtWarningMsg, message.toLatin1().constData());
424 }
425
426 void QuickTestResult::wait(int ms)
427 {
428     QTest::qWait(ms);
429 }
430
431 void QuickTestResult::sleep(int ms)
432 {
433     QTest::qSleep(ms);
434 }
435
436 void QuickTestResult::startMeasurement()
437 {
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;
443     d->results.clear();
444 }
445
446 void QuickTestResult::beginDataRun()
447 {
448     QBenchmarkTestMethodData::current->beginDataRun();
449 }
450
451 void QuickTestResult::endDataRun()
452 {
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);
457
458     if (QBenchmarkGlobalData::current->verboseOutput) {
459         if (d->iterCount == -1) {
460             qDebug() << "warmup stage result      :" << QBenchmarkTestMethodData::current->result.value;
461         } else {
462             qDebug() << "accumulation stage result:" << QBenchmarkTestMethodData::current->result.value;
463         }
464     }
465 }
466
467 bool QuickTestResult::measurementAccepted()
468 {
469     return QBenchmarkTestMethodData::current->resultsAccepted();
470 }
471
472 static QBenchmarkResult qMedian(const QList<QBenchmarkResult> &container)
473 {
474     const int count = container.count();
475     if (count == 0)
476         return QBenchmarkResult();
477
478     if (count == 1)
479         return container.at(0);
480
481     QList<QBenchmarkResult> containerCopy = container;
482     qSort(containerCopy);
483
484     const int middle = count / 2;
485
486     // ### handle even-sized containers here by doing an aritmetic mean of the two middle items.
487     return containerCopy.at(middle);
488 }
489
490 bool QuickTestResult::needsMoreMeasurements()
491 {
492     Q_D(QuickTestResult);
493     ++(d->iterCount);
494     if (d->iterCount < QBenchmarkGlobalData::current->adjustMedianIterationCount())
495         return true;
496     if (QBenchmarkTestMethodData::current->resultsAccepted())
497         QTestLog::addBenchmarkResult(qMedian(d->results));
498     return false;
499 }
500
501 void QuickTestResult::startBenchmark(RunMode runMode, const QString &tag)
502 {
503     QBenchmarkTestMethodData::current->result = QBenchmarkResult();
504     QBenchmarkTestMethodData::current->resultAccepted = false;
505     QBenchmarkGlobalData::current->context.tag = tag;
506     QBenchmarkGlobalData::current->context.slotName = functionName();
507
508     Q_D(QuickTestResult);
509     delete d->benchmarkIter;
510     d->benchmarkIter = new QTest::QBenchmarkIterationController
511         (QTest::QBenchmarkIterationController::RunMode(runMode));
512 }
513
514 bool QuickTestResult::isBenchmarkDone() const
515 {
516     Q_D(const QuickTestResult);
517     if (d->benchmarkIter)
518         return d->benchmarkIter->isDone();
519     else
520         return true;
521 }
522
523 void QuickTestResult::nextBenchmark()
524 {
525     Q_D(QuickTestResult);
526     if (d->benchmarkIter)
527         d->benchmarkIter->next();
528 }
529
530 void QuickTestResult::stopBenchmark()
531 {
532     Q_D(QuickTestResult);
533     delete d->benchmarkIter;
534     d->benchmarkIter = 0;
535 }
536
537 namespace QTest {
538     void qtest_qParseArgs(int argc, char *argv[], bool qml);
539 };
540
541 void QuickTestResult::parseArgs(int argc, char *argv[])
542 {
543     if (!QBenchmarkGlobalData::current)
544         QBenchmarkGlobalData::current = &globalBenchmarkData;
545     QTest::qtest_qParseArgs(argc, argv, true);
546 }
547
548 void QuickTestResult::setProgramName(const char *name)
549 {
550     if (name) {
551         QTestResult::reset();
552     } else if (!name && loggingStarted) {
553         QTestResult::setCurrentTestObject(globalProgramName);
554         QTestLog::stopLogging();
555         QTestResult::setCurrentTestObject(0);
556     }
557     globalProgramName = name;
558     QTestResult::setCurrentTestObject(globalProgramName);
559 }
560
561 void QuickTestResult::setCurrentAppname(const char *appname)
562 {
563     QTestResult::setCurrentAppname(appname);
564 }
565
566 int QuickTestResult::exitCode()
567 {
568 #if defined(QTEST_NOEXITCODE)
569     return 0;
570 #else
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);
574 #endif
575 }
576
577 QT_END_NAMESPACE