Remove the rest of qtquick 1.0 code from qmltest
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktest.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
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 <QtQml/qqml.h>
47 #include <QtQml/qqmlengine.h>
48 #include <QtQml/qqmlcontext.h>
49 #include <QtQuick/qquickview.h>
50 #include <QtQml/qjsvalue.h>
51 #include <QtQml/qjsengine.h>
52 #include <QtGui/qopengl.h>
53 #include <QtCore/qurl.h>
54 #include <QtCore/qfileinfo.h>
55 #include <QtCore/qdir.h>
56 #include <QtCore/qdiriterator.h>
57 #include <QtCore/qfile.h>
58 #include <QtCore/qdebug.h>
59 #include <QtCore/qeventloop.h>
60 #include <QtCore/qtextstream.h>
61 #include <QtGui/qtextdocument.h>
62 #include <stdio.h>
63 #include <QtGui/QGuiApplication>
64 #include <QtCore/QTranslator>
65 QT_BEGIN_NAMESPACE
66
67 class QTestRootObject : public QObject
68 {
69     Q_OBJECT
70     Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged)
71     Q_PROPERTY(bool hasTestCase READ hasTestCase WRITE setHasTestCase NOTIFY hasTestCaseChanged)
72 public:
73     QTestRootObject(QObject *parent = 0)
74         : QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false)  {}
75
76     bool hasQuit:1;
77     bool hasTestCase() const { return m_hasTestCase; }
78     void setHasTestCase(bool value) { m_hasTestCase = value; emit hasTestCaseChanged(); }
79
80     bool windowShown() const { return m_windowShown; }
81     void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); }
82
83 Q_SIGNALS:
84     void windowShownChanged();
85     void hasTestCaseChanged();
86
87 private Q_SLOTS:
88     void quit() { hasQuit = true; }
89
90 private:
91     bool m_windowShown : 1;
92     bool m_hasTestCase :1;
93 };
94
95 static inline QString stripQuotes(const QString &s)
96 {
97     if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"')))
98         return s.mid(1, s.length() - 2);
99     else
100         return s;
101 }
102
103 template <class View> void handleCompileErrors(const QFileInfo &fi, const View &view)
104 {
105     // Error compiling the test - flag failure in the log and continue.
106     const QList<QQmlError> errors = view.errors();
107     QuickTestResult results;
108     results.setTestCaseName(fi.baseName());
109     results.startLogging();
110     results.setFunctionName(QLatin1String("compile"));
111     // Verbose warning output of all messages and relevant parameters
112     QString message;
113     QTextStream str(&message);
114     str << "\n  " << QDir::toNativeSeparators(fi.absoluteFilePath()) << " produced "
115         << errors.size() << " error(s):\n";
116     foreach (const QQmlError &e, errors) {
117         str << "    ";
118         if (e.url().isLocalFile()) {
119             str << e.url().toLocalFile();
120         } else {
121             str << e.url().toString();
122         }
123         if (e.line() > 0)
124             str << ':' << e.line() << ',' << e.column();
125         str << ": " << e.description() << '\n';
126     }
127     str << "  Working directory: " << QDir::toNativeSeparators(QDir::current().absolutePath()) << '\n';
128     if (QQmlEngine *engine = view.engine()) {
129         str << "  View: " << view.metaObject()->className() << ", import paths:\n";
130         foreach (const QString &i, engine->importPathList())
131             str << "    '" << QDir::toNativeSeparators(i) << "'\n";
132         const QStringList pluginPaths = engine->pluginPathList();
133         str << "  Plugin paths:\n";
134         foreach (const QString &p, pluginPaths)
135             str << "    '" << QDir::toNativeSeparators(p) << "'\n";
136     }
137     qWarning("%s", qPrintable(message));
138     // Fail with error 0.
139     results.fail(errors.at(0).description(),
140                  errors.at(0).url(), errors.at(0).line());
141     results.finishTestData();
142     results.finishTestDataCleanup();
143     results.finishTestFunction();
144     results.setFunctionName(QString());
145     results.stopLogging();
146 }
147
148 int quick_test_main(int argc, char **argv, const char *name, quick_test_viewport_create createViewport, const char *sourceDir)
149 {
150     Q_UNUSED(createViewport);
151     QGuiApplication* app = 0;
152     if (!QCoreApplication::instance()) {
153         app = new QGuiApplication(argc, argv);
154     }
155
156     // Look for QML-specific command-line options.
157     //      -import dir         Specify an import directory.
158     //      -input dir          Specify the input directory for test cases.
159     //      -translation file   Specify the translation file.
160     QStringList imports;
161     QString testPath;
162     QString translationFile;
163     int outargc = 1;
164     int index = 1;
165     while (index < argc) {
166         if (strcmp(argv[index], "-import") == 0 && (index + 1) < argc) {
167             imports += stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
168             index += 2;
169         } else if (strcmp(argv[index], "-input") == 0 && (index + 1) < argc) {
170             testPath = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
171             index += 2;
172         } else if (strcmp(argv[index], "-opengl") == 0) {
173             ++index;
174         } else if (strcmp(argv[index], "-translation") == 0 && (index + 1) < argc) {
175             translationFile = stripQuotes(QString::fromLocal8Bit(argv[index + 1]));
176             index += 2;
177         } else if (outargc != index) {
178             argv[outargc++] = argv[index++];
179         } else {
180             ++outargc;
181             ++index;
182         }
183     }
184     argv[outargc] = 0;
185     argc = outargc;
186
187     // Parse the command-line arguments.
188
189     // Setting currentAppname and currentTestObjectName (via setProgramName) are needed
190     // for the code coverage analysis. Must be done before parseArgs is called.
191     QuickTestResult::setCurrentAppname(argv[0]);
192     QuickTestResult::setProgramName(name);
193
194     QuickTestResult::parseArgs(argc, argv);
195
196     QTranslator translator;
197     if (!translationFile.isEmpty()) {
198         if (translator.load(translationFile)) {
199             app->installTranslator(&translator);
200         } else {
201             qWarning("Could not load the translation file '%s'.", qPrintable(translationFile));
202         }
203     }
204
205     // Determine where to look for the test data.
206     if (testPath.isEmpty() && sourceDir)
207         testPath = QString::fromLocal8Bit(sourceDir);
208     if (testPath.isEmpty()) {
209         QDir current = QDir::current();
210 #ifdef Q_OS_WIN
211         // Skip release/debug subfolders
212         if (!current.dirName().compare(QLatin1String("Release"), Qt::CaseInsensitive)
213             || !current.dirName().compare(QLatin1String("Debug"), Qt::CaseInsensitive))
214             current.cdUp();
215 #endif // Q_OS_WIN
216         testPath = current.absolutePath();
217     }
218     QStringList files;
219
220     const QFileInfo testPathInfo(testPath);
221     if (testPathInfo.isFile()) {
222         if (!testPath.endsWith(QStringLiteral(".qml"))) {
223             qWarning("'%s' does not have the suffix '.qml'.", qPrintable(testPath));
224             return 1;
225         }
226         files << testPath;
227     } else if (testPathInfo.isDir()) {
228         // Scan the test data directory recursively, looking for "tst_*.qml" files.
229         const QStringList filters(QStringLiteral("tst_*.qml"));
230         QDirIterator iter(testPathInfo.absoluteFilePath(), filters, QDir::Files,
231                           QDirIterator::Subdirectories |
232                           QDirIterator::FollowSymlinks);
233         while (iter.hasNext())
234             files += iter.next();
235         files.sort();
236         if (files.isEmpty()) {
237             qWarning("The directory '%s' does not contain any test files matching '%s'",
238                      qPrintable(testPath), qPrintable(filters.front()));
239             return 1;
240         }
241     } else {
242         qWarning("'%s' does not exist under '%s'.",
243                  qPrintable(testPath), qPrintable(QDir::currentPath()));
244         return 1;
245     }
246
247     // Scan through all of the "tst_*.qml" files and run each of them
248     // in turn with a QQuickView.
249     QQuickView view;
250     QTestRootObject rootobj;
251     QEventLoop eventLoop;
252     QObject::connect(view.engine(), SIGNAL(quit()),
253                      &rootobj, SLOT(quit()));
254     QObject::connect(view.engine(), SIGNAL(quit()),
255                      &eventLoop, SLOT(quit()));
256     view.rootContext()->setContextProperty
257         (QLatin1String("qtest"), &rootobj);
258     foreach (const QString &path, imports)
259         view.engine()->addImportPath(path);
260
261     foreach (QString file, files) {
262         QFileInfo fi(file);
263         if (!fi.exists())
264             continue;
265
266         rootobj.setHasTestCase(false);
267         rootobj.setWindowShown(false);
268         rootobj.hasQuit = false;
269         QString path = fi.absoluteFilePath();
270         if (path.startsWith(QLatin1String(":/")))
271             view.setSource(QUrl(QLatin1String("qrc:") + path.mid(2)));
272         else
273             view.setSource(QUrl::fromLocalFile(path));
274
275         if (QTest::printAvailableFunctions)
276             continue;
277         if (view.status() == QQuickView::Error) {
278             handleCompileErrors(fi, view);
279             continue;
280         }
281         if (!rootobj.hasQuit) {
282             // If the test already quit, then it was performed
283             // synchronously during setSource().  Otherwise it is
284             // an asynchronous test and we need to show the window
285             // and wait for the quit indication.
286             view.show();
287             QTest::qWaitForWindowShown(&view);
288             rootobj.setWindowShown(true);
289             if (!rootobj.hasQuit && rootobj.hasTestCase())
290                 eventLoop.exec();
291         }
292     }
293
294     // Flush the current logging stream.
295     QuickTestResult::setProgramName(0);
296
297     //Sometimes delete app cause crash here with some qpa plugins,
298     //so we comment the follow line out to make them happy.
299     //delete app;
300
301     // Return the number of failures as the exit code.
302     return QuickTestResult::exitCode();
303 }
304
305 QT_END_NAMESPACE
306
307 #include "quicktest.moc"