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