Get declarative running on new gui/opengl stack.
[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 ** 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 <QtDeclarative/qsgview.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 <QtGui/qtextdocument.h>
66 #include <stdio.h>
67
68 QT_BEGIN_NAMESPACE
69
70 // Copied from qdeclarativedebughelper_p.h in Qt, to avoid a dependency
71 // on a private header from Qt.
72 class Q_DECLARATIVE_EXPORT QDeclarativeDebugHelper
73 {
74 public:
75     static QJSEngine *getScriptEngine(QDeclarativeEngine *engine);
76     static void setAnimationSlowDownFactor(qreal factor);
77     static void enableDebugging();
78 };
79
80 class QTestRootObject : public QObject
81 {
82     Q_OBJECT
83     Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
84 public:
85     QTestRootObject(QObject *parent = 0)
86         : QObject(parent), hasQuit(false), m_windowShown(false) {}
87
88     bool hasQuit;
89
90     bool windowShown() const { return m_windowShown; }
91     void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
92
93 Q_SIGNALS:
94     void windowShownChanged();
95
96 private Q_SLOTS:
97     void quit() { hasQuit = true; }
98
99 private:
100     bool m_windowShown;
101 };
102
103 static inline QString stripQuotes(const QString &s)
104 {
105     if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
106         return s.mid(1, s.length() - 2);
107     else
108         return s;
109 }
110
111 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
112 {
113     QApplication* app = 0;
114     if (!QCoreApplication::instance()) {
115         app = new QApplication(argc, argv);
116     }
117
118     // Look for QML-specific command-line options.
119     //      -import dir         Specify an import directory.
120     //      -input dir          Specify the input directory for test cases.
121     //      -qtquick1           Run with QtQuick 1 rather than QtQuick 2.
122     QStringList imports;
123     QString testPath;
124     bool qtQuick2 = true;
125     int outargc = 1;
126     int index = 1;
127     while (index < argc) {
128         if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
129             imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
130             index += 2;
131         } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
132             testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
133             index += 2;
134         } else if (strcmp(argv[index], "-opengl") == 0) {
135             ++index;
136         } else if (strcmp(argv[index], "-qtquick1") == 0) {
137             qtQuick2 = false;
138             ++index;
139         } else if (outargc != index) {
140             argv[outargc++] = argv[index++];
141         } else {
142             ++outargc;
143             ++index;
144         }
145     }
146     argv[outargc] = 0;
147     argc = outargc;
148
149     // Parse the command-line arguments.
150     QuickTestResult::parseArgs(argc, argv);
151     QuickTestResult::setProgramName(name);
152
153     // Determine where to look for the test data.
154     if (testPath.isEmpty() && sourceDir)
155         testPath = QString::fromLocal8Bit(sourceDir);
156     if (testPath.isEmpty())
157         testPath = QLatin1String(".");
158
159     // Scan the test data directory recursively, looking for "tst_*.qml" files.
160     QStringList filters;
161     filters += QLatin1String("tst_*.qml");
162     QStringList files;
163     QDirIterator iter(testPath, filters, QDir::Files,
164                       QDirIterator::Subdirectories |
165                       QDirIterator::FollowSymlinks);
166     while (iter.hasNext())
167         files += iter.next();
168     files.sort();
169
170     // Bail out if we didn't find any test cases.
171     if (files.isEmpty()) {
172         qWarning() << argv[0] << ": could not find any test cases under"
173                    << testPath;
174         return 1;
175     }
176
177
178     // Scan through all of the "tst_*.qml" files and run each of them
179     // in turn with a QDeclarativeView.
180 #ifdef QUICK_TEST_SCENEGRAPH
181     if (qtQuick2) {
182         foreach (QString file, files) {
183             QFileInfo fi(file);
184             if (!fi.exists())
185                 continue;
186             QSGView view;
187             QTestRootObject rootobj;
188             QEventLoop eventLoop;
189             QObject::connect(view.engine(), SIGNAL(quit()),
190                              &rootobj, SLOT(quit()));
191             QObject::connect(view.engine(), SIGNAL(quit()),
192                              &eventLoop, SLOT(quit()));
193             view.rootContext()->setContextProperty
194                 (QLatin1String("qtest"), &rootobj);
195             foreach (QString path, imports)
196                 view.engine()->addImportPath(path);
197             QString path = fi.absoluteFilePath();
198             if (path.startsWith(QLatin1String(":/")))
199                 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
200             else
201                 view.setSource(QUrl::fromLocalFile(path));
202             if (QTest::printAvailableFunctions)
203                 continue;
204             if (view.status() == QSGView::Error) {
205                 // Error compiling the test - flag failure in the log and continue.
206                 QList<QDeclarativeError> errors = view.errors();
207                 QuickTestResult results;
208                 results.setTestCaseName(fi.baseName());
209                 results.startLogging();
210                 results.setFunctionName(QLatin1String("compile"));
211                 results.setFunctionType(QuickTestResult::Func);
212                 results.fail(errors.at(0).description(),
213                              errors.at(0).url().toString(),
214                              errors.at(0).line());
215                 results.finishTestFunction();
216                 results.setFunctionName(QString());
217                 results.setFunctionType(QuickTestResult::NoWhere);
218                 results.stopLogging();
219                 continue;
220             }
221             if (!rootobj.hasQuit) {
222                 // If the test already quit, then it was performed
223                 // synchronously during setSource().  Otherwise it is
224                 // an asynchronous test and we need to show the window
225                 // and wait for the quit indication.
226                 view.show();
227                 QTest::qWaitForWindowShown(&view);
228                 rootobj.setWindowShown(true);
229                 if (!rootobj.hasQuit)
230                     eventLoop.exec();
231             }
232         }
233     } else
234 #endif
235     {
236         foreach (QString file, files) {
237             QFileInfo fi(file);
238             if (!fi.exists())
239                 continue;
240             QDeclarativeView view;
241             QTestRootObject rootobj;
242             QEventLoop eventLoop;
243             QObject::connect(view.engine(), SIGNAL(quit()),
244                              &rootobj, SLOT(quit()));
245             QObject::connect(view.engine(), SIGNAL(quit()),
246                              &eventLoop, SLOT(quit()));
247             if (createViewport)
248                 view.setViewport((*createViewport)());
249             view.rootContext()->setContextProperty
250                 (QLatin1String("qtest"), &rootobj);
251             foreach (QString path, imports)
252                 view.engine()->addImportPath(path);
253             QString path = fi.absoluteFilePath();
254             if (path.startsWith(QLatin1String(":/")))
255                 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
256             else
257                 view.setSource(QUrl::fromLocalFile(path));
258             if (QTest::printAvailableFunctions)
259                 continue;
260             if (view.status() == QDeclarativeView::Error) {
261                 // Error compiling the test - flag failure in the log and continue.
262                 QList<QDeclarativeError> errors = view.errors();
263                 QuickTestResult results;
264                 results.setTestCaseName(fi.baseName());
265                 results.startLogging();
266                 results.setFunctionName(QLatin1String("compile"));
267                 results.setFunctionType(QuickTestResult::Func);
268                 results.fail(errors.at(0).description(),
269                              errors.at(0).url().toString(),
270                              errors.at(0).line());
271                 results.finishTestFunction();
272                 results.setFunctionName(QString());
273                 results.setFunctionType(QuickTestResult::NoWhere);
274                 results.stopLogging();
275                 continue;
276             }
277             if (!rootobj.hasQuit) {
278                 // If the test already quit, then it was performed
279                 // synchronously during setSource().  Otherwise it is
280                 // an asynchronous test and we need to show the window
281                 // and wait for the quit indication.
282                 view.show();
283                 QTest::qWaitForWindowShown(&view);
284                 rootobj.setWindowShown(true);
285                 if (!rootobj.hasQuit)
286                     eventLoop.exec();
287             }
288         }
289     }
290
291     // Flush the current logging stream.
292     QuickTestResult::setProgramName(0);
293
294     delete app;
295     // Return the number of failures as the exit code.
296     return QuickTestResult::exitCode();
297 }
298
299 QT_END_NAMESPACE
300
301 #include "quicktest.moc"