Doc: Enabling Qt QML linking to Qt Quick.
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 <QtQml/qqml.h>
47 #include <QtQml/qqmlengine.h>
48 #include <QtQml/qqmlcontext.h>
49 #include <QtQuick/qquickview.h>
50 #include <QtQml/qjsvalue.h>
51 #include <QtQml/qjsengine.h>
52 #include <QtGui/qopengl.h>
53 #include <QtCore/qurl.h>
54 #include <QtCore/qfileinfo.h>
55 #include <QtCore/qdir.h>
56 #include <QtCore/qdiriterator.h>
57 #include <QtCore/qfile.h>
58 #include <QtCore/qdebug.h>
59 #include <QtCore/qeventloop.h>
60 #include <QtCore/qtextstream.h>
61 #include <QtGui/qtextdocument.h>
62 #include <stdio.h>
63 #include <QtGui/QGuiApplication>
64 #include <QtCore/QTranslator>
65 #include <QtTest/QSignalSpy>
66
67 QT_BEGIN_NAMESPACE
68
69 class QTestRootObject : public QObject
70 {
71     Q_OBJECT
72     Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
73     Q_PROPERTY(bool hasTestCase READ hasTestCase WRITE setHasTestCase NOTIFY hasTestCaseChanged)
74 public:
75     QTestRootObject(QObject *parent = 0)
76         : QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false)  {}
77
78     bool hasQuit:1;
79     bool hasTestCase() const { return m_hasTestCase; }
80     void setHasTestCase(bool value) { m_hasTestCase = value; emit hasTestCaseChanged(); }
81
82     bool windowShown() const { return m_windowShown; }
83     void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
84
85 Q_SIGNALS:
86     void windowShownChanged();
87     void hasTestCaseChanged();
88
89 private Q_SLOTS:
90     void quit() { hasQuit = true; }
91
92 private:
93     bool m_windowShown : 1;
94     bool m_hasTestCase :1;
95 };
96
97 static inline QString stripQuotes(const QString &s)
98 {
99     if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
100         return s.mid(1, s.length() - 2);
101     else
102         return s;
103 }
104
105 void handleCompileErrors(const QFileInfo &fi, QQuickView *view)
106 {
107     // Error compiling the test - flag failure in the log and continue.
108     const QList<QQmlError> errors = view->errors();
109     QuickTestResult results;
110     results.setTestCaseName(fi.baseName());
111     results.startLogging();
112     results.setFunctionName(QLatin1String("compile"));
113     // Verbose warning output of all messages and relevant parameters
114     QString message;
115     QTextStream str(&message);
116     str << "\n  " << QDir::toNativeSeparators(fi.absoluteFilePath()) << " produced "
117         << errors.size() << " error(s):\n";
118     foreach (const QQmlError &e, errors) {
119         str << "    ";
120         if (e.url().isLocalFile()) {
121             str << QDir::toNativeSeparators(e.url().toLocalFile());
122         } else {
123             str << e.url().toString();
124         }
125         if (e.line() > 0)
126             str << ':' << e.line() << ',' << e.column();
127         str << ": " << e.description() << '\n';
128     }
129     str << "  Working directory: " << QDir::toNativeSeparators(QDir::current().absolutePath()) << '\n';
130     if (QQmlEngine *engine = view->engine()) {
131         str << "  View: " << view->metaObject()->className() << ", import paths:\n";
132         foreach (const QString &i, engine->importPathList())
133             str << "    '" << QDir::toNativeSeparators(i) << "'\n";
134         const QStringList pluginPaths = engine->pluginPathList();
135         str << "  Plugin paths:\n";
136         foreach (const QString &p, pluginPaths)
137             str << "    '" << QDir::toNativeSeparators(p) << "'\n";
138     }
139     qWarning("%s", qPrintable(message));
140     // Fail with error 0.
141     results.fail(errors.at(0).description(),
142                  errors.at(0).url(), errors.at(0).line());
143     results.finishTestData();
144     results.finishTestDataCleanup();
145     results.finishTestFunction();
146     results.setFunctionName(QString());
147     results.stopLogging();
148 }
149
150 bool qWaitForSignal(QObject *obj, const char* signal, int timeout = 5000)
151 {
152     QSignalSpy spy(obj, signal);
153     QElapsedTimer timer;
154     timer.start();
155
156     while (!spy.size()) {
157         int remaining = timeout - int(timer.elapsed());
158         if (remaining <= 0)
159             break;
160         QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
161         QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
162         QTest::qSleep(10);
163     }
164
165     return spy.size();
166 }
167
168 int quick_test_main(int argc, char **argv, const char *name, const char *sourceDir)
169 {
170     QGuiApplication* app = 0;
171     if (!QCoreApplication::instance()) {
172         app = new QGuiApplication(argc, argv);
173     }
174
175     // Look for QML-specific command-line options.
176     //      -import dir         Specify an import directory.
177     //      -input dir          Specify the input directory for test cases.
178     //      -translation file   Specify the translation file.
179     QStringList imports;
180     QString testPath;
181     QString translationFile;
182     int outargc = 1;
183     int index = 1;
184     while (index < argc) {
185         if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
186             imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
187             index += 2;
188         } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
189             testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
190             index += 2;
191         } else if (strcmp(argv[index], "-opengl") == 0) {
192             ++index;
193         } else if (strcmp(argv[index], "-translation") == 0 && (index + 1) < argc) {
194             translationFile = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
195             index += 2;
196         } else if (outargc != index) {
197             argv[outargc++] = argv[index++];
198         } else {
199             ++outargc;
200             ++index;
201         }
202     }
203     argv[outargc] = 0;
204     argc = outargc;
205
206     // Parse the command-line arguments.
207
208     // Setting currentAppname and currentTestObjectName (via setProgramName) are needed
209     // for the code coverage analysis. Must be done before parseArgs is called.
210     QuickTestResult::setCurrentAppname(argv[0]);
211     QuickTestResult::setProgramName(name);
212
213     QuickTestResult::parseArgs(argc, argv);
214
215 #ifndef QT_NO_TRANSLATION
216     QTranslator translator;
217     if (!translationFile.isEmpty()) {
218         if (translator.load(translationFile)) {
219             app->installTranslator(&translator);
220         } else {
221             qWarning("Could not load the translation file '%s'.", qPrintable(translationFile));
222         }
223     }
224 #endif
225
226     // Determine where to look for the test data.
227     if (testPath.isEmpty() && sourceDir) {
228         const QString s = QString::fromLocal8Bit(sourceDir);
229         if (QFile::exists(s))
230             testPath = s;
231     }
232     if (testPath.isEmpty()) {
233         QDir current = QDir::current();
234 #ifdef Q_OS_WIN
235         // Skip release/debug subfolders
236         if (!current.dirName().compare(QLatin1String("Release"), Qt::CaseInsensitive)
237             || !current.dirName().compare(QLatin1String("Debug"), Qt::CaseInsensitive))
238             current.cdUp();
239 #endif // Q_OS_WIN
240         testPath = current.absolutePath();
241     }
242     QStringList files;
243
244     const QFileInfo testPathInfo(testPath);
245     if (testPathInfo.isFile()) {
246         if (!testPath.endsWith(QStringLiteral(".qml"))) {
247             qWarning("'%s' does not have the suffix '.qml'.", qPrintable(testPath));
248             return 1;
249         }
250         files << testPath;
251     } else if (testPathInfo.isDir()) {
252         // Scan the test data directory recursively, looking for "tst_*.qml" files.
253         const QStringList filters(QStringLiteral("tst_*.qml"));
254         QDirIterator iter(testPathInfo.absoluteFilePath(), filters, QDir::Files,
255                           QDirIterator::Subdirectories |
256                           QDirIterator::FollowSymlinks);
257         while (iter.hasNext())
258             files += iter.next();
259         files.sort();
260         if (files.isEmpty()) {
261             qWarning("The directory '%s' does not contain any test files matching '%s'",
262                      qPrintable(testPath), qPrintable(filters.front()));
263             return 1;
264         }
265     } else {
266         qWarning("'%s' does not exist under '%s'.",
267                  qPrintable(testPath), qPrintable(QDir::currentPath()));
268         return 1;
269     }
270
271     // Scan through all of the "tst_*.qml" files and run each of them
272     // in turn with a QQuickView.
273     QQuickView *view = new QQuickView;
274     view->setFlags(Qt::Window | Qt::WindowSystemMenuHint
275                          | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint
276                          | Qt::WindowCloseButtonHint);
277     QTestRootObject rootobj;
278     QEventLoop eventLoop;
279     QObject::connect(view->engine(), SIGNAL(quit()),
280                      &rootobj, SLOT(quit()));
281     QObject::connect(view->engine(), SIGNAL(quit()),
282                      &eventLoop, SLOT(quit()));
283     view->rootContext()->setContextProperty
284         (QLatin1String("qtest"), &rootobj);
285     foreach (const QString &path, imports)
286         view->engine()->addImportPath(path);
287     foreach (const QString &file, files) {
288         const QFileInfo fi(file);
289         if (!fi.exists())
290             continue;
291
292         view->setObjectName(fi.baseName());
293         view->setTitle(view->objectName());
294         rootobj.setHasTestCase(false);
295         rootobj.setWindowShown(false);
296         rootobj.hasQuit = false;
297         QString path = fi.absoluteFilePath();
298         if (path.startsWith(QLatin1String(":/")))
299             view->setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
300         else
301             view->setSource(QUrl::fromLocalFile(path));
302
303         if (QTest::printAvailableFunctions)
304             continue;
305         if (view->status() == QQuickView::Error) {
306             handleCompileErrors(fi, view);
307             continue;
308         }
309         if (!rootobj.hasQuit) {
310             // If the test already quit, then it was performed
311             // synchronously during setSource().  Otherwise it is
312             // an asynchronous test and we need to show the window
313             // and wait for the first frame to be rendered
314             // and then wait for quit indication.
315             view->setFramePosition(QPoint(50, 50));
316             if (view->size().isEmpty()) { // Avoid hangs with empty windows.
317                 qWarning().nospace()
318                     << "Test '" << QDir::toNativeSeparators(path) << "' has invalid size "
319                     << view->size() << ", resizing.";
320                 view->resize(200, 200);
321             }
322             view->show();
323             if (qWaitForSignal(view, SIGNAL(frameSwapped())))
324                 rootobj.setWindowShown(true);
325             if (!rootobj.hasQuit && rootobj.hasTestCase())
326                 eventLoop.exec();
327             // view->hide(); Causes a crash in Qt3D due to deletion of the GL context, see QTBUG-27696
328         }
329     }
330
331     // Flush the current logging stream.
332     QuickTestResult::setProgramName(0);
333     delete view;
334     delete app;
335
336     // Return the number of failures as the exit code.
337     return QuickTestResult::exitCode();
338 }
339
340 QT_END_NAMESPACE
341
342 #include "quicktest.moc"