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 <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
55 #include <QtDeclarative/qjsvalue.h>
56 #include <QtDeclarative/qjsengine.h>
57 #include <QtOpenGL/qgl.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>
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
75 static QJSEngine *getScriptEngine(QDeclarativeEngine *engine);
76 static void setAnimationSlowDownFactor(qreal factor);
77 static void enableDebugging();
80 class QTestRootObject : public QObject
83 Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
85 QTestRootObject(QObject *parent = 0)
86 : QObject(parent), hasQuit(false), m_windowShown(false) {}
90 bool windowShown() const { return m_windowShown; }
91 void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
94 void windowShownChanged();
97 void quit() { hasQuit = true; }
103 static inline QString stripQuotes(const QString &s)
105 if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
106 return s.mid(1, s.length() - 2);
111 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
113 QApplication* app = 0;
114 if (!QCoreApplication::instance()) {
115 app = new QApplication(argc, argv);
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.
124 bool qtQuick2 = true;
127 while (index < argc) {
128 if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
129 imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
131 } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
132 testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
134 } else if (strcmp(argv[index], "-opengl") == 0) {
136 } else if (strcmp(argv[index], "-qtquick1") == 0) {
139 } else if (outargc != index) {
140 argv[outargc++] = argv[index++];
149 // Determine where to look for the test data.
150 if (testPath.isEmpty() && sourceDir)
151 testPath = QString::fromLocal8Bit(sourceDir);
152 if (testPath.isEmpty())
153 testPath = QLatin1String(".");
155 // Scan the test data directory recursively, looking for "tst_*.qml" files.
157 filters += QLatin1String("tst_*.qml");
159 QDirIterator iter(testPath, filters, QDir::Files,
160 QDirIterator::Subdirectories |
161 QDirIterator::FollowSymlinks);
162 while (iter.hasNext())
163 files += iter.next();
166 // Bail out if we didn't find any test cases.
167 if (files.isEmpty()) {
168 qWarning() << argv[0] << ": could not find any test cases under"
173 // Parse the command-line arguments.
174 QuickTestResult::parseArgs(argc, argv);
175 QuickTestResult::setProgramName(name);
177 // Scan through all of the "tst_*.qml" files and run each of them
178 // in turn with a QDeclarativeView.
179 #ifdef QUICK_TEST_SCENEGRAPH
181 foreach (QString file, files) {
186 QTestRootObject rootobj;
187 QEventLoop eventLoop;
188 QObject::connect(view.engine(), SIGNAL(quit()),
189 &rootobj, SLOT(quit()));
190 QObject::connect(view.engine(), SIGNAL(quit()),
191 &eventLoop, SLOT(quit()));
192 view.rootContext()->setContextProperty
193 (QLatin1String("qtest"), &rootobj);
194 foreach (QString path, imports)
195 view.engine()->addImportPath(path);
196 QString path = fi.absoluteFilePath();
197 if (path.startsWith(QLatin1String(":/")))
198 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
200 view.setSource(QUrl::fromLocalFile(path));
201 if (QTest::printAvailableFunctions)
203 if (view.status() == QSGView::Error) {
204 // Error compiling the test - flag failure in the log and continue.
205 QList<QDeclarativeError> errors = view.errors();
206 QuickTestResult results;
207 results.setTestCaseName(fi.baseName());
208 results.startLogging();
209 results.setFunctionName(QLatin1String("compile"));
210 results.setFunctionType(QuickTestResult::Func);
211 results.fail(errors.at(0).description(),
212 errors.at(0).url().toString(),
213 errors.at(0).line());
214 results.finishTestFunction();
215 results.setFunctionName(QString());
216 results.setFunctionType(QuickTestResult::NoWhere);
217 results.stopLogging();
220 if (!rootobj.hasQuit) {
221 // If the test already quit, then it was performed
222 // synchronously during setSource(). Otherwise it is
223 // an asynchronous test and we need to show the window
224 // and wait for the quit indication.
226 QTest::qWaitForWindowShown(&view);
227 rootobj.setWindowShown(true);
228 if (!rootobj.hasQuit)
235 foreach (QString file, files) {
239 QDeclarativeView view;
240 QTestRootObject rootobj;
241 QEventLoop eventLoop;
242 QObject::connect(view.engine(), SIGNAL(quit()),
243 &rootobj, SLOT(quit()));
244 QObject::connect(view.engine(), SIGNAL(quit()),
245 &eventLoop, SLOT(quit()));
247 view.setViewport((*createViewport)());
248 view.rootContext()->setContextProperty
249 (QLatin1String("qtest"), &rootobj);
250 foreach (QString path, imports)
251 view.engine()->addImportPath(path);
252 QString path = fi.absoluteFilePath();
253 if (path.startsWith(QLatin1String(":/")))
254 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
256 view.setSource(QUrl::fromLocalFile(path));
257 if (QTest::printAvailableFunctions)
259 if (view.status() == QDeclarativeView::Error) {
260 // Error compiling the test - flag failure in the log and continue.
261 QList<QDeclarativeError> errors = view.errors();
262 QuickTestResult results;
263 results.setTestCaseName(fi.baseName());
264 results.startLogging();
265 results.setFunctionName(QLatin1String("compile"));
266 results.setFunctionType(QuickTestResult::Func);
267 results.fail(errors.at(0).description(),
268 errors.at(0).url().toString(),
269 errors.at(0).line());
270 results.finishTestFunction();
271 results.setFunctionName(QString());
272 results.setFunctionType(QuickTestResult::NoWhere);
273 results.stopLogging();
276 if (!rootobj.hasQuit) {
277 // If the test already quit, then it was performed
278 // synchronously during setSource(). Otherwise it is
279 // an asynchronous test and we need to show the window
280 // and wait for the quit indication.
282 QTest::qWaitForWindowShown(&view);
283 rootobj.setWindowShown(true);
284 if (!rootobj.hasQuit)
290 // Flush the current logging stream.
291 QuickTestResult::setProgramName(0);
294 // Return the number of failures as the exit code.
295 return QuickTestResult::exitCode();
300 #include "quicktest.moc"