qmltest: Count passes, fails and skips consistently.
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktest.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 "quicktest.h"
43 #include "quicktestresult_p.h"
44 #include <QtTest/qtestsystem.h>
45 #include "qtestoptions_p.h"
46 #include <QApplication>
47 #include <QtDeclarative/qdeclarative.h>
48 #include <QtDeclarative/qdeclarativeengine.h>
49 #include <QtDeclarative/qdeclarativecontext.h>
50 #if defined(QML_VERSION) && QML_VERSION >= 0x020000
51 #include <QtQuick/qquickview.h>
52 #define QUICK_TEST_SCENEGRAPH 1
53 #endif
54 #include <QtDeclarative/qjsvalue.h>
55 #include <QtDeclarative/qjsengine.h>
56 #include <QtGui/qopengl.h>
57 #include <QtCore/qurl.h>
58 #include <QtCore/qfileinfo.h>
59 #include <QtCore/qdir.h>
60 #include <QtCore/qdiriterator.h>
61 #include <QtCore/qfile.h>
62 #include <QtCore/qdebug.h>
63 #include <QtCore/qeventloop.h>
64 #include <QtCore/qtextstream.h>
65 #include <QtGui/qtextdocument.h>
66 #include <stdio.h>
67 #include <QtGui/QGuiApplication>
68 #include <QtCore/QTranslator>
69 QT_BEGIN_NAMESPACE
70
71 static void installCoverageTool(const char * appname, const char * testname)
72 {
73 #ifdef __COVERAGESCANNER__
74     // Install Coverage Tool
75     __coveragescanner_install(appname);
76     __coveragescanner_testname(testname);
77     __coveragescanner_clear();
78 #else
79     Q_UNUSED(appname);
80     Q_UNUSED(testname);
81 #endif
82 }
83
84 static void saveCoverageTool(const char * appname, bool testfailed)
85 {
86 #ifdef __COVERAGESCANNER__
87     // install again to make sure the filename is correct.
88     // without this, a plugin or similar may have changed the filename.
89     __coveragescanner_install(appname);
90     __coveragescanner_teststate(testfailed ? "FAILED" : "PASSED");
91     __coveragescanner_save();
92     __coveragescanner_testname("");
93     __coveragescanner_clear();
94 #else
95     Q_UNUSED(appname);
96     Q_UNUSED(testfailed);
97 #endif
98 }
99
100
101 class QTestRootObject : public QObject
102 {
103     Q_OBJECT
104     Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
105     Q_PROPERTY(bool hasTestCase READ hasTestCase WRITE setHasTestCase NOTIFY hasTestCaseChanged)
106 public:
107     QTestRootObject(QObject *parent = 0)
108         : QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false)  {}
109
110     bool hasQuit:1;
111     bool hasTestCase() const { return m_hasTestCase; }
112     void setHasTestCase(bool value) { m_hasTestCase = value; emit hasTestCaseChanged(); }
113
114     bool windowShown() const { return m_windowShown; }
115     void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
116
117 Q_SIGNALS:
118     void windowShownChanged();
119     void hasTestCaseChanged();
120
121 private Q_SLOTS:
122     void quit() { hasQuit = true; }
123
124 private:
125     bool m_windowShown : 1;
126     bool m_hasTestCase :1;
127 };
128
129 static inline QString stripQuotes(const QString &s)
130 {
131     if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
132         return s.mid(1, s.length() - 2);
133     else
134         return s;
135 }
136
137 template <class View> void handleCompileErrors(const QFileInfo &fi, const View &view)
138 {
139     // Error compiling the test - flag failure in the log and continue.
140     const QList<QDeclarativeError> errors = view.errors();
141     QuickTestResult results;
142     results.setTestCaseName(fi.baseName());
143     results.startLogging();
144     results.setFunctionName(QLatin1String("compile"));
145     // Verbose warning output of all messages and relevant parameters
146     QString message;
147     QTextStream str(&message);
148     str << "\n  " << QDir::toNativeSeparators(fi.absoluteFilePath()) << " produced "
149         << errors.size() << " error(s):\n";
150     foreach (const QDeclarativeError &e, errors) {
151         str << "    ";
152         if (e.url().isLocalFile()) {
153             str << e.url().toLocalFile();
154         } else {
155             str << e.url().toString();
156         }
157         if (e.line() > 0)
158             str << ':' << e.line() << ',' << e.column();
159         str << ": " << e.description() << '\n';
160     }
161     str << "  Working directory: " << QDir::toNativeSeparators(QDir::current().absolutePath()) << '\n';
162     if (QDeclarativeEngine *engine = view.engine()) {
163         str << "  View: " << view.metaObject()->className() << ", import paths:\n";
164         foreach (const QString &i, engine->importPathList())
165             str << "    '" << QDir::toNativeSeparators(i) << "'\n";
166         const QStringList pluginPaths = engine->pluginPathList();
167         str << "  Plugin paths:\n";
168         foreach (const QString &p, pluginPaths)
169             str << "    '" << QDir::toNativeSeparators(p) << "'\n";
170     }
171     qWarning("%s", qPrintable(message));
172     // Fail with error 0.
173     results.fail(errors.at(0).description(),
174                  errors.at(0).url(), errors.at(0).line());
175     results.finishTestData();
176     results.finishTestDataCleanup();
177     results.finishTestFunction();
178     results.setFunctionName(QString());
179     results.stopLogging();
180 }
181
182 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
183 {
184     QGuiApplication* app = 0;
185     if (!QCoreApplication::instance()) {
186         app = new QGuiApplication(argc, argv);
187     }
188
189     // Look for QML-specific command-line options.
190     //      -import dir         Specify an import directory.
191     //      -input dir          Specify the input directory for test cases.
192     //      -qtquick1           Run with QtQuick 1 rather than QtQuick 2.
193     //      -translation file   Specify the translation file.
194     QStringList imports;
195     QString testPath;
196     QString translationFile;
197     bool qtQuick2 = true;
198     int outargc = 1;
199     int index = 1;
200     while (index < argc) {
201         if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
202             imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
203             index += 2;
204         } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
205             testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
206             index += 2;
207         } else if (strcmp(argv[index], "-opengl") == 0) {
208             ++index;
209         } else if (strcmp(argv[index], "-qtquick1") == 0) {
210             qtQuick2 = false;
211             ++index;
212         } else if (strcmp(argv[index], "-translation") == 0 && (index + 1) < argc) {
213             translationFile = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
214             index += 2;
215         } else if (outargc != index) {
216             argv[outargc++] = argv[index++];
217         } else {
218             ++outargc;
219             ++index;
220         }
221     }
222     argv[outargc] = 0;
223     argc = outargc;
224
225     // Parse the command-line arguments.
226     QuickTestResult::parseArgs(argc, argv);
227     QuickTestResult::setProgramName(name);
228
229     installCoverageTool(argv[0], name);
230
231     QTranslator translator;
232     if (!translationFile.isEmpty()) {
233         if (translator.load(translationFile)) {
234             app->installTranslator(&translator);
235         } else {
236             qWarning("Could not load the translation file '%s'.", qPrintable(translationFile));
237         }
238     }
239
240     // Determine where to look for the test data.
241     if (testPath.isEmpty() && sourceDir)
242         testPath = QString::fromLocal8Bit(sourceDir);
243     if (testPath.isEmpty()) {
244         QDir current = QDir::current();
245 #ifdef Q_OS_WIN
246         // Skip release/debug subfolders
247         if (!current.dirName().compare(QLatin1String("Release"), Qt::CaseInsensitive)
248             || !current.dirName().compare(QLatin1String("Debug"), Qt::CaseInsensitive))
249             current.cdUp();
250 #endif // Q_OS_WIN
251         testPath = current.absolutePath();
252     }
253     QStringList files;
254
255     const QFileInfo testPathInfo(testPath);
256     if (testPathInfo.isFile()) {
257         if (!testPath.endsWith(QStringLiteral(".qml"))) {
258             qWarning("'%s' does not have the suffix '.qml'.", qPrintable(testPath));
259             return 1;
260         }
261         files << testPath;
262     } else if (testPathInfo.isDir()) {
263         // Scan the test data directory recursively, looking for "tst_*.qml" files.
264         const QStringList filters(QStringLiteral("tst_*.qml"));
265         QDirIterator iter(testPathInfo.absoluteFilePath(), filters, QDir::Files,
266                           QDirIterator::Subdirectories |
267                           QDirIterator::FollowSymlinks);
268         while (iter.hasNext())
269             files += iter.next();
270         files.sort();
271         if (files.isEmpty()) {
272             qWarning("The directory '%s' does not contain any test files matching '%s'",
273                      qPrintable(testPath), qPrintable(filters.front()));
274             return 1;
275         }
276     } else {
277         qWarning("'%s' does not exist under '%s'.",
278                  qPrintable(testPath), qPrintable(QDir::currentPath()));
279         return 1;
280     }
281
282     // Scan through all of the "tst_*.qml" files and run each of them
283     // in turn with a QDeclarativeView.
284 #ifdef QUICK_TEST_SCENEGRAPH
285     if (qtQuick2) {
286         QQuickView view;
287         QTestRootObject rootobj;
288         QEventLoop eventLoop;
289         QObject::connect(view.engine(), SIGNAL(quit()),
290                          &rootobj, SLOT(quit()));
291         QObject::connect(view.engine(), SIGNAL(quit()),
292                          &eventLoop, SLOT(quit()));
293         view.rootContext()->setContextProperty
294             (QLatin1String("qtest"), &rootobj);
295         foreach (const QString &path, imports)
296             view.engine()->addImportPath(path);
297
298         foreach (QString file, files) {
299             QFileInfo fi(file);
300             if (!fi.exists())
301                 continue;
302
303             rootobj.setHasTestCase(false);
304             rootobj.setWindowShown(false);
305             rootobj.hasQuit = false;
306             QString path = fi.absoluteFilePath();
307             if (path.startsWith(QLatin1String(":/")))
308                 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
309             else
310                 view.setSource(QUrl::fromLocalFile(path));
311
312             if (QTest::printAvailableFunctions)
313                 continue;
314             if (view.status() == QQuickView::Error) {
315                 handleCompileErrors(fi, view);
316                 continue;
317             }
318             if (!rootobj.hasQuit) {
319                 // If the test already quit, then it was performed
320                 // synchronously during setSource().  Otherwise it is
321                 // an asynchronous test and we need to show the window
322                 // and wait for the quit indication.
323                 view.show();
324                 QTest::qWaitForWindowShown(&view);
325                 rootobj.setWindowShown(true);
326                 if (!rootobj.hasQuit && rootobj.hasTestCase())
327                     eventLoop.exec();
328             }
329         }
330     } else
331 #endif
332     {
333         qWarning("No suitable QtQuick1 implementation is available!");
334         return 1;
335     }
336
337     // Flush the current logging stream.
338     QuickTestResult::setProgramName(0);
339
340     saveCoverageTool(argv[0], QuickTestResult::exitCode() != 0);
341
342     //Sometimes delete app cause crash here with some qpa plugins,
343     //so we comment the follow line out to make them happy.
344     //delete app;
345
346     // Return the number of failures as the exit code.
347     return QuickTestResult::exitCode();
348 }
349
350 QT_END_NAMESPACE
351
352 #include "quicktest.moc"