Changed qml tests to work from install directory
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qmlmin / tst_qmlmin.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 <qtest.h>
43 #include <QLibraryInfo>
44 #include <QDir>
45 #include <QProcess>
46 #include <QDebug>
47 #include <QQmlError>
48 #include <cstdlib>
49
50 class tst_qmlmin : public QObject
51 {
52     Q_OBJECT
53 public:
54     tst_qmlmin();
55
56 private slots:
57     void initTestCase();
58     void qmlMinify_data();
59     void qmlMinify();
60
61 private:
62     QString qmlminPath;
63     QStringList excludedDirs;
64     QStringList invalidFiles;
65
66     QStringList findFiles(const QDir &);
67     bool isInvalidFile(const QFileInfo &fileName) const;
68 };
69
70 tst_qmlmin::tst_qmlmin()
71 {
72 }
73
74 void tst_qmlmin::initTestCase()
75 {
76     qmlminPath = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/qmlmin");
77 #ifdef Q_OS_WIN
78     qmlminPath += QLatin1String(".exe");
79 #endif
80     if (!QFileInfo(qmlminPath).exists()) {
81         QString message = QString::fromLatin1("qmlmin executable not found (looked for %0)")
82                 .arg(qmlminPath);
83         QFAIL(qPrintable(message));
84     }
85
86     // Add directories you want excluded here
87
88     // These snippets are not expected to run on their own.
89     excludedDirs << "doc/src/snippets/qml/visualdatamodel_rootindex";
90     excludedDirs << "doc/src/snippets/qml/qtbinding";
91     excludedDirs << "doc/src/snippets/qml/imports";
92     excludedDirs << "doc/src/snippets/qtquick1/visualdatamodel_rootindex";
93     excludedDirs << "doc/src/snippets/qtquick1/qtbinding";
94     excludedDirs << "doc/src/snippets/qtquick1/imports";
95
96     // Add invalid files (i.e. files with syntax errors)
97     invalidFiles << "tests/auto/quick/qquickloader/data/InvalidSourceComponent.qml";
98     invalidFiles << "tests/auto/qml/qqmllanguage/data/dynamicObjectProperties.2.qml";
99     invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.2.qml";
100     invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.3.qml";
101     invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.5.qml";
102     invalidFiles << "tests/auto/qml/qqmllanguage/data/property.4.qml";
103     invalidFiles << "tests/auto/qml/qqmllanguage/data/empty.qml";
104     invalidFiles << "tests/auto/qml/qqmllanguage/data/missingObject.qml";
105     invalidFiles << "tests/auto/qml/qqmllanguage/data/insertedSemicolon.1.qml";
106     invalidFiles << "tests/auto/qml/qqmllanguage/data/nonexistantProperty.5.qml";
107     invalidFiles << "tests/auto/qml/qqmllanguage/data/invalidRoot.1.qml";
108     invalidFiles << "tests/auto/qml/qquickfolderlistmodel/data/dummy.qml";
109     invalidFiles << "tests/auto/qml/qqmlecmascript/data/qtbug_22843.js";
110     invalidFiles << "tests/auto/qml/qqmlecmascript/data/qtbug_22843.library.js";
111     invalidFiles << "tests/auto/qml/qquickworkerscript/data/script_error_onLoad.js";
112     invalidFiles << "tests/auto/qml/parserstress/tests/ecma_3/Unicode/regress-352044-02-n.js";
113 }
114
115 QStringList tst_qmlmin::findFiles(const QDir &d)
116 {
117     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
118         QString s = excludedDirs.at(ii);
119         if (d.absolutePath().endsWith(s))
120             return QStringList();
121     }
122
123     QStringList rv;
124
125     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"),
126                                     QDir::Files);
127     foreach (const QString &file, files) {
128         rv << d.absoluteFilePath(file);
129     }
130
131     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
132                                    QDir::NoSymLinks);
133     foreach (const QString &dir, dirs) {
134         QDir sub = d;
135         sub.cd(dir);
136         rv << findFiles(sub);
137     }
138
139     return rv;
140 }
141
142 bool tst_qmlmin::isInvalidFile(const QFileInfo &fileName) const
143 {
144     foreach (const QString &invalidFile, invalidFiles) {
145         if (fileName.absoluteFilePath().endsWith(invalidFile))
146             return true;
147     }
148     return false;
149 }
150
151 /*
152 This test runs all the examples in the QtQml UI source tree and ensures
153 that they start and exit cleanly.
154
155 Examples are any .qml files under the examples/ directory that start
156 with a lower case letter.
157 */
158
159 void tst_qmlmin::qmlMinify_data()
160 {
161     QTest::addColumn<QString>("file");
162
163     QString examples = QLatin1String(SRCDIR) + "/../../../../examples/";
164     QString tests = QLatin1String(SRCDIR) + "/../../../../tests/";
165
166     QStringList files;
167     files << findFiles(QDir(examples));
168     files << findFiles(QDir(tests));
169
170     foreach (const QString &file, files)
171         QTest::newRow(qPrintable(file)) << file;
172 }
173
174 void tst_qmlmin::qmlMinify()
175 {
176     QFETCH(QString, file);
177
178 #if defined(QTEST_CROSS_COMPILED)
179     QSKIP("sources not available when cross compiled");
180 #endif
181
182     QProcess qmlminify;
183     qmlminify.start(qmlminPath, QStringList() << QLatin1String("--verify-only") << file);
184     qmlminify.waitForFinished();
185
186     QCOMPARE(qmlminify.error(), QProcess::UnknownError);
187     QCOMPARE(qmlminify.exitStatus(), QProcess::NormalExit);
188
189     if (isInvalidFile(file))
190         QCOMPARE(qmlminify.exitCode(), EXIT_FAILURE); // cannot minify files with syntax errors
191     else
192         QCOMPARE(qmlminify.exitCode(), 0);
193 }
194
195 QTEST_MAIN(tst_qmlmin)
196
197 #include "tst_qmlmin.moc"