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