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 <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>
67 #include <QtGui/QGuiApplication>
71 class QTestRootObject : public QObject
74 Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
76 QTestRootObject(QObject *parent = 0)
77 : QObject(parent), hasQuit(false), m_windowShown(false) {}
81 bool windowShown() const { return m_windowShown; }
82 void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
85 void windowShownChanged();
88 void quit() { hasQuit = true; }
94 static inline QString stripQuotes(const QString &s)
96 if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
97 return s.mid(1, s.length() - 2);
102 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
104 QGuiApplication* app = 0;
105 if (!QCoreApplication::instance()) {
106 app = new QGuiApplication(argc, argv);
109 // Look for QML-specific command-line options.
110 // -import dir Specify an import directory.
111 // -input dir Specify the input directory for test cases.
112 // -qtquick1 Run with QtQuick 1 rather than QtQuick 2.
115 bool qtQuick2 = true;
118 while (index < argc) {
119 if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
120 imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
122 } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
123 testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
125 } else if (strcmp(argv[index], "-opengl") == 0) {
127 } else if (strcmp(argv[index], "-qtquick1") == 0) {
130 } else if (outargc != index) {
131 argv[outargc++] = argv[index++];
140 // Parse the command-line arguments.
141 QuickTestResult::parseArgs(argc, argv);
142 QuickTestResult::setProgramName(name);
144 // Determine where to look for the test data.
145 if (testPath.isEmpty() && sourceDir)
146 testPath = QString::fromLocal8Bit(sourceDir);
147 if (testPath.isEmpty())
148 testPath = QLatin1String(".");
150 // Scan the test data directory recursively, looking for "tst_*.qml" files.
152 filters += QLatin1String("tst_*.qml");
154 QDirIterator iter(testPath, filters, QDir::Files,
155 QDirIterator::Subdirectories |
156 QDirIterator::FollowSymlinks);
157 while (iter.hasNext())
158 files += iter.next();
161 // Bail out if we didn't find any test cases.
162 if (files.isEmpty()) {
163 qWarning() << argv[0] << ": could not find any test cases under"
169 // Scan through all of the "tst_*.qml" files and run each of them
170 // in turn with a QDeclarativeView.
171 #ifdef QUICK_TEST_SCENEGRAPH
173 foreach (QString file, files) {
178 QTestRootObject rootobj;
179 QEventLoop eventLoop;
180 QObject::connect(view.engine(), SIGNAL(quit()),
181 &rootobj, SLOT(quit()));
182 QObject::connect(view.engine(), SIGNAL(quit()),
183 &eventLoop, SLOT(quit()));
184 view.rootContext()->setContextProperty
185 (QLatin1String("qtest"), &rootobj);
186 foreach (QString path, imports)
187 view.engine()->addImportPath(path);
188 QString path = fi.absoluteFilePath();
189 if (path.startsWith(QLatin1String(":/")))
190 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
192 view.setSource(QUrl::fromLocalFile(path));
193 if (QTest::printAvailableFunctions)
195 if (view.status() == QSGView::Error) {
196 // Error compiling the test - flag failure in the log and continue.
197 QList<QDeclarativeError> errors = view.errors();
198 QuickTestResult results;
199 results.setTestCaseName(fi.baseName());
200 results.startLogging();
201 results.setFunctionName(QLatin1String("compile"));
202 results.setFunctionType(QuickTestResult::Func);
203 results.fail(errors.at(0).description(),
204 errors.at(0).url().toString(),
205 errors.at(0).line());
206 results.finishTestFunction();
207 results.setFunctionName(QString());
208 results.setFunctionType(QuickTestResult::NoWhere);
209 results.stopLogging();
212 if (!rootobj.hasQuit) {
213 // If the test already quit, then it was performed
214 // synchronously during setSource(). Otherwise it is
215 // an asynchronous test and we need to show the window
216 // and wait for the quit indication.
218 //QTest::qWaitForWindowShown(&view);
220 rootobj.setWindowShown(true);
221 if (!rootobj.hasQuit)
228 foreach (QString file, files) {
232 QDeclarativeView view;
233 QTestRootObject rootobj;
234 QEventLoop eventLoop;
235 QObject::connect(view.engine(), SIGNAL(quit()),
236 &rootobj, SLOT(quit()));
237 QObject::connect(view.engine(), SIGNAL(quit()),
238 &eventLoop, SLOT(quit()));
240 view.setViewport((*createViewport)());
241 view.rootContext()->setContextProperty
242 (QLatin1String("qtest"), &rootobj);
243 foreach (QString path, imports)
244 view.engine()->addImportPath(path);
245 QString path = fi.absoluteFilePath();
246 if (path.startsWith(QLatin1String(":/")))
247 view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
249 view.setSource(QUrl::fromLocalFile(path));
250 if (QTest::printAvailableFunctions)
252 if (view.status() == QDeclarativeView::Error) {
253 // Error compiling the test - flag failure in the log and continue.
254 QList<QDeclarativeError> errors = view.errors();
255 QuickTestResult results;
256 results.setTestCaseName(fi.baseName());
257 results.startLogging();
258 results.setFunctionName(QLatin1String("compile"));
259 results.setFunctionType(QuickTestResult::Func);
260 results.fail(errors.at(0).description(),
261 errors.at(0).url().toString(),
262 errors.at(0).line());
263 results.finishTestFunction();
264 results.setFunctionName(QString());
265 results.setFunctionType(QuickTestResult::NoWhere);
266 results.stopLogging();
269 if (!rootobj.hasQuit) {
270 // If the test already quit, then it was performed
271 // synchronously during setSource(). Otherwise it is
272 // an asynchronous test and we need to show the window
273 // and wait for the quit indication.
275 QTest::qWaitForWindowShown(&view);
276 rootobj.setWindowShown(true);
277 if (!rootobj.hasQuit)
283 // Flush the current logging stream.
284 QuickTestResult::setProgramName(0);
287 // Return the number of failures as the exit code.
288 return QuickTestResult::exitCode();
293 #include "quicktest.moc"