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