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