38de65f826322db739546fe2ca4e4a7817dd438f
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qmlmin / tst_qmlmin.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 <qtest.h>
43 #include <QLibraryInfo>
44 #include <QDir>
45 #include <QProcess>
46 #include <QDebug>
47 #include <QDeclarativeError>
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/declarative/visualdatamodel_rootindex";
90     excludedDirs << "doc/src/snippets/declarative/qtbinding";
91     excludedDirs << "doc/src/snippets/declarative/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/declarative/qquickloader/data/InvalidSourceComponent.qml";
98     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/dynamicObjectProperties.2.qml";
99     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/signal.3.qml";
100     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/property.4.qml";
101     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/empty.qml";
102     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/signal.2.qml";
103     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/missingObject.qml";
104     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/insertedSemicolon.1.qml";
105     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/nonexistantProperty.5.qml";
106     invalidFiles << "tests/auto/declarative/qdeclarativefolderlistmodel/data/dummy.qml";
107     invalidFiles << "tests/auto/declarative/qdeclarativeecmascript/data/blank.js";
108     invalidFiles << "tests/auto/declarative/qdeclarativeworkerscript/data/script_error_onLoad.js";
109     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/test.js";
110     invalidFiles << "tests/auto/declarative/qdeclarativelanguage/data/test2.js";
111 }
112
113 QStringList tst_qmlmin::findFiles(const QDir &d)
114 {
115     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
116         QString s = excludedDirs.at(ii);
117         if (d.absolutePath().endsWith(s))
118             return QStringList();
119     }
120
121     QStringList rv;
122
123     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"),
124                                     QDir::Files);
125     foreach (const QString &file, files) {
126         rv << d.absoluteFilePath(file);
127     }
128
129     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
130                                    QDir::NoSymLinks);
131     foreach (const QString &dir, dirs) {
132         QDir sub = d;
133         sub.cd(dir);
134         rv << findFiles(sub);
135     }
136
137     return rv;
138 }
139
140 bool tst_qmlmin::isInvalidFile(const QFileInfo &fileName) const
141 {
142     foreach (const QString &invalidFile, invalidFiles) {
143         if (fileName.absoluteFilePath().endsWith(invalidFile))
144             return true;
145     }
146     return false;
147 }
148
149 /*
150 This test runs all the examples in the declarative UI source tree and ensures
151 that they start and exit cleanly.
152
153 Examples are any .qml files under the examples/ directory that start
154 with a lower case letter.
155 */
156
157 void tst_qmlmin::qmlMinify_data()
158 {
159     QTest::addColumn<QString>("file");
160
161     QString examples = QLatin1String(SRCDIR) + "/../../../../examples/";
162     QString tests = QLatin1String(SRCDIR) + "/../../../../tests/";
163
164     QStringList files;
165     files << findFiles(QDir(examples));
166     files << findFiles(QDir(tests));
167
168     foreach (const QString &file, files)
169         QTest::newRow(qPrintable(file)) << file;
170 }
171
172 void tst_qmlmin::qmlMinify()
173 {
174     QFETCH(QString, file);
175
176     QProcess qmlminify;
177     qmlminify.start(qmlminPath, QStringList() << QLatin1String("--verify-only") << file);
178     qmlminify.waitForFinished();
179
180     QCOMPARE(qmlminify.error(), QProcess::UnknownError);
181     QCOMPARE(qmlminify.exitStatus(), QProcess::NormalExit);
182
183     if (isInvalidFile(file))
184         QCOMPARE(qmlminify.exitCode(), EXIT_FAILURE); // cannot minify files with syntax errors
185     else
186         QCOMPARE(qmlminify.exitCode(), 0);
187 }
188
189 QTEST_MAIN(tst_qmlmin)
190
191 #include "tst_qmlmin.moc"