Integrate QtQuickTest into Qt
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktest.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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
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/qdeclarativeview.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtDeclarative/qdeclarativecontext.h>
51 #if defined(QML_VERSION) && QML_VERSION >= 0x020000
52 #include <QtDeclarative/qsgview.h>
53 #define QUICK_TEST_SCENEGRAPH 1
54 #endif
55 #include <QtScript/qscriptvalue.h>
56 #include <QtScript/qscriptcontext.h>
57 #include <QtScript/qscriptengine.h>
58 #include <QtOpenGL/qgl.h>
59 #include <QtCore/qurl.h>
60 #include <QtCore/qfileinfo.h>
61 #include <QtCore/qdir.h>
62 #include <QtCore/qdiriterator.h>
63 #include <QtCore/qfile.h>
64 #include <QtCore/qdebug.h>
65 #include <QtCore/qeventloop.h>
66 #include <QtGui/qtextdocument.h>
67 #include <stdio.h>
68
69 QT_BEGIN_NAMESPACE
70
71 // Copied from qdeclarativedebughelper_p.h in Qt, to avoid a dependency
72 // on a private header from Qt.
73 class Q_DECLARATIVE_EXPORT QDeclarativeDebugHelper
74 {
75 public:
76     static QScriptEngine *getScriptEngine(QDeclarativeEngine *engine);
77     static void setAnimationSlowDownFactor(qreal factor);
78     static void enableDebugging();
79 };
80
81 class QTestRootObject : public QObject
82 {
83     Q_OBJECT
84     Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
85 public:
86     QTestRootObject(QObject *parent = 0)
87         : QObject(parent), hasQuit(false), m_windowShown(false) {}
88
89     bool hasQuit;
90
91     bool windowShown() const { return m_windowShown; }
92     void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
93
94 Q_SIGNALS:
95     void windowShownChanged();
96
97 private Q_SLOTS:
98     void quit() { hasQuit = true; }
99
100 private:
101     bool m_windowShown;
102 };
103
104 static inline QString stripQuotes(const QString &s)
105 {
106     if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
107         return s.mid(1, s.length() - 2);
108     else
109         return s;
110 }
111
112 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
113 {
114     QApplication app(argc, argv);
115
116     // Look for QML-specific command-line options.
117     //      -import dir         Specify an import directory.
118     //      -input dir          Specify the input directory for test cases.
119     //      -qtquick1           Run with QtQuick 1 rather than QtQuick 2.
120     QStringList imports;
121     QString testPath;
122     bool qtQuick2 = true;
123     int outargc = 1;
124     int index = 1;
125     while (index < argc) {
126         if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
127             imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
128             index += 2;
129         } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
130             testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
131             index += 2;
132         } else if (strcmp(argv[index], "-opengl") == 0) {
133             ++index;
134         } else if (strcmp(argv[index], "-qtquick1") == 0) {
135             qtQuick2 = false;
136             ++index;
137         } else if (outargc != index) {
138             argv[outargc++] = argv[index++];
139         } else {
140             ++outargc;
141             ++index;
142         }
143     }
144     argv[outargc] = 0;
145     argc = outargc;
146
147     // Determine where to look for the test data.
148     if (testPath.isEmpty() && sourceDir)
149         testPath = QString::fromLocal8Bit(sourceDir);
150     if (testPath.isEmpty())
151         testPath = QLatin1String(".");
152
153     // Scan the test data directory recursively, looking for "tst_*.qml" files.
154     QStringList filters;
155     filters += QLatin1String("tst_*.qml");
156     QStringList files;
157     QDirIterator iter(testPath, filters, QDir::Files,
158                       QDirIterator::Subdirectories |
159                       QDirIterator::FollowSymlinks);
160     while (iter.hasNext())
161         files += iter.next();
162     files.sort();
163
164     // Bail out if we didn't find any test cases.
165     if (files.isEmpty()) {
166         qWarning() << argv[0] << ": could not find any test cases under"
167                    << testPath;
168         return 1;
169     }
170
171     // Parse the command-line arguments.
172     QuickTestResult::parseArgs(argc, argv);
173     QuickTestResult::setProgramName(name);
174
175     // Scan through all of the "tst_*.qml" files and run each of them
176     // in turn with a QDeclarativeView.
177 #ifdef QUICK_TEST_SCENEGRAPH
178     if (qtQuick2) {
179         foreach (QString file, files) {
180             QFileInfo fi(file);
181             if (!fi.exists())
182                 continue;
183             QSGView view;
184             QTestRootObject rootobj;
185             QEventLoop eventLoop;
186             QObject::connect(view.engine(), SIGNAL(quit()),
187                              &rootobj, SLOT(quit()));
188             QObject::connect(view.engine(), SIGNAL(quit()),
189                              &eventLoop, SLOT(quit()));
190             view.rootContext()->setContextProperty
191                 (QLatin1String("qtest"), &rootobj);
192             QScriptEngine *engine;
193             engine = QDeclarativeDebugHelper::getScriptEngine(view.engine());
194             QScriptValue qtObject
195                 = engine->globalObject().property(QLatin1String("Qt"));
196             qtObject.setProperty
197                 (QLatin1String("qtest_wrapper"), QScriptValue(true));
198             qtObject.setProperty
199                 (QLatin1String("qtest_printAvailableFunctions"),
200                  QScriptValue(QTest::printAvailableFunctions));
201             foreach (QString path, imports)
202                 view.engine()->addImportPath(path);
203             QString path = fi.absoluteFilePath();
204             if (path.startsWith(QLatin1String(":/")))
205                 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
206             else
207                 view.setSource(QUrl::fromLocalFile(path));
208             if (QTest::printAvailableFunctions)
209                 continue;
210             if (view.status() == QSGView::Error) {
211                 // Error compiling the test - flag failure in the log and continue.
212                 QList<QDeclarativeError> errors = view.errors();
213                 QuickTestResult results;
214                 results.setTestCaseName(fi.baseName());
215                 results.startLogging();
216                 results.setFunctionName(QLatin1String("compile"));
217                 results.setFunctionType(QuickTestResult::Func);
218                 results.fail(errors.at(0).description(),
219                              errors.at(0).url().toString(),
220                              errors.at(0).line());
221                 results.finishTestFunction();
222                 results.setFunctionName(QString());
223                 results.setFunctionType(QuickTestResult::NoWhere);
224                 results.stopLogging();
225                 continue;
226             }
227             if (!rootobj.hasQuit) {
228                 // If the test already quit, then it was performed
229                 // synchronously during setSource().  Otherwise it is
230                 // an asynchronous test and we need to show the window
231                 // and wait for the quit indication.
232                 view.show();
233                 QTest::qWaitForWindowShown(&view);
234                 rootobj.setWindowShown(true);
235                 if (!rootobj.hasQuit)
236                     eventLoop.exec();
237             }
238         }
239     } else
240 #endif
241     {
242         foreach (QString file, files) {
243             QFileInfo fi(file);
244             if (!fi.exists())
245                 continue;
246             QDeclarativeView view;
247             QTestRootObject rootobj;
248             QEventLoop eventLoop;
249             QObject::connect(view.engine(), SIGNAL(quit()),
250                              &rootobj, SLOT(quit()));
251             QObject::connect(view.engine(), SIGNAL(quit()),
252                              &eventLoop, SLOT(quit()));
253             if (createViewport)
254                 view.setViewport((*createViewport)());
255             view.rootContext()->setContextProperty
256                 (QLatin1String("qtest"), &rootobj);
257             QScriptEngine *engine;
258             engine = QDeclarativeDebugHelper::getScriptEngine(view.engine());
259             QScriptValue qtObject
260                 = engine->globalObject().property(QLatin1String("Qt"));
261             qtObject.setProperty
262                 (QLatin1String("qtest_wrapper"), QScriptValue(true));
263             qtObject.setProperty
264                 (QLatin1String("qtest_printAvailableFunctions"),
265                  QScriptValue(QTest::printAvailableFunctions));
266             foreach (QString path, imports)
267                 view.engine()->addImportPath(path);
268             QString path = fi.absoluteFilePath();
269             if (path.startsWith(QLatin1String(":/")))
270                 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
271             else
272                 view.setSource(QUrl::fromLocalFile(path));
273             if (QTest::printAvailableFunctions)
274                 continue;
275             if (view.status() == QDeclarativeView::Error) {
276                 // Error compiling the test - flag failure in the log and continue.
277                 QList<QDeclarativeError> errors = view.errors();
278                 QuickTestResult results;
279                 results.setTestCaseName(fi.baseName());
280                 results.startLogging();
281                 results.setFunctionName(QLatin1String("compile"));
282                 results.setFunctionType(QuickTestResult::Func);
283                 results.fail(errors.at(0).description(),
284                              errors.at(0).url().toString(),
285                              errors.at(0).line());
286                 results.finishTestFunction();
287                 results.setFunctionName(QString());
288                 results.setFunctionType(QuickTestResult::NoWhere);
289                 results.stopLogging();
290                 continue;
291             }
292             if (!rootobj.hasQuit) {
293                 // If the test already quit, then it was performed
294                 // synchronously during setSource().  Otherwise it is
295                 // an asynchronous test and we need to show the window
296                 // and wait for the quit indication.
297                 view.show();
298                 QTest::qWaitForWindowShown(&view);
299                 rootobj.setWindowShown(true);
300                 if (!rootobj.hasQuit)
301                     eventLoop.exec();
302             }
303         }
304     }
305
306     // Flush the current logging stream.
307     QuickTestResult::setProgramName(0);
308
309     // Return the number of failures as the exit code.
310     return QuickTestResult::exitCode();
311 }
312
313 QT_END_NAMESPACE
314
315 #include "quicktest.moc"