1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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
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>
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
76 static QScriptEngine *getScriptEngine(QDeclarativeEngine *engine);
77 static void setAnimationSlowDownFactor(qreal factor);
78 static void enableDebugging();
81 class QTestRootObject : public QObject
84 Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
86 QTestRootObject(QObject *parent = 0)
87 : QObject(parent), hasQuit(false), m_windowShown(false) {}
91 bool windowShown() const { return m_windowShown; }
92 void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
95 void windowShownChanged();
98 void quit() { hasQuit = true; }
104 static inline QString stripQuotes(const QString &s)
106 if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
107 return s.mid(1, s.length() - 2);
112 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
114 QApplication app(argc, argv);
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.
122 bool qtQuick2 = true;
125 while (index < argc) {
126 if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
127 imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
129 } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
130 testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
132 } else if (strcmp(argv[index], "-opengl") == 0) {
134 } else if (strcmp(argv[index], "-qtquick1") == 0) {
137 } else if (outargc != index) {
138 argv[outargc++] = argv[index++];
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(".");
153 // Scan the test data directory recursively, looking for "tst_*.qml" files.
155 filters += QLatin1String("tst_*.qml");
157 QDirIterator iter(testPath, filters, QDir::Files,
158 QDirIterator::Subdirectories |
159 QDirIterator::FollowSymlinks);
160 while (iter.hasNext())
161 files += iter.next();
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"
171 // Parse the command-line arguments.
172 QuickTestResult::parseArgs(argc, argv);
173 QuickTestResult::setProgramName(name);
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
179 foreach (QString file, files) {
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 foreach (QString path, imports)
193 view.engine()->addImportPath(path);
194 QString path = fi.absoluteFilePath();
195 if (path.startsWith(QLatin1String(":/")))
196 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
198 view.setSource(QUrl::fromLocalFile(path));
199 if (QTest::printAvailableFunctions)
201 if (view.status() == QSGView::Error) {
202 // Error compiling the test - flag failure in the log and continue.
203 QList<QDeclarativeError> errors = view.errors();
204 QuickTestResult results;
205 results.setTestCaseName(fi.baseName());
206 results.startLogging();
207 results.setFunctionName(QLatin1String("compile"));
208 results.setFunctionType(QuickTestResult::Func);
209 results.fail(errors.at(0).description(),
210 errors.at(0).url().toString(),
211 errors.at(0).line());
212 results.finishTestFunction();
213 results.setFunctionName(QString());
214 results.setFunctionType(QuickTestResult::NoWhere);
215 results.stopLogging();
218 if (!rootobj.hasQuit) {
219 // If the test already quit, then it was performed
220 // synchronously during setSource(). Otherwise it is
221 // an asynchronous test and we need to show the window
222 // and wait for the quit indication.
224 QTest::qWaitForWindowShown(&view);
225 rootobj.setWindowShown(true);
226 if (!rootobj.hasQuit)
233 foreach (QString file, files) {
237 QDeclarativeView view;
238 QTestRootObject rootobj;
239 QEventLoop eventLoop;
240 QObject::connect(view.engine(), SIGNAL(quit()),
241 &rootobj, SLOT(quit()));
242 QObject::connect(view.engine(), SIGNAL(quit()),
243 &eventLoop, SLOT(quit()));
245 view.setViewport((*createViewport)());
246 view.rootContext()->setContextProperty
247 (QLatin1String("qtest"), &rootobj);
248 foreach (QString path, imports)
249 view.engine()->addImportPath(path);
250 QString path = fi.absoluteFilePath();
251 if (path.startsWith(QLatin1String(":/")))
252 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
254 view.setSource(QUrl::fromLocalFile(path));
255 if (QTest::printAvailableFunctions)
257 if (view.status() == QDeclarativeView::Error) {
258 // Error compiling the test - flag failure in the log and continue.
259 QList<QDeclarativeError> errors = view.errors();
260 QuickTestResult results;
261 results.setTestCaseName(fi.baseName());
262 results.startLogging();
263 results.setFunctionName(QLatin1String("compile"));
264 results.setFunctionType(QuickTestResult::Func);
265 results.fail(errors.at(0).description(),
266 errors.at(0).url().toString(),
267 errors.at(0).line());
268 results.finishTestFunction();
269 results.setFunctionName(QString());
270 results.setFunctionType(QuickTestResult::NoWhere);
271 results.stopLogging();
274 if (!rootobj.hasQuit) {
275 // If the test already quit, then it was performed
276 // synchronously during setSource(). Otherwise it is
277 // an asynchronous test and we need to show the window
278 // and wait for the quit indication.
280 QTest::qWaitForWindowShown(&view);
281 rootobj.setWindowShown(true);
282 if (!rootobj.hasQuit)
288 // Flush the current logging stream.
289 QuickTestResult::setProgramName(0);
291 // Return the number of failures as the exit code.
292 return QuickTestResult::exitCode();
297 #include "quicktest.moc"