1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <qtest.h> |
30 | #include <QLibraryInfo> |
31 | #include <QDir> |
32 | #if QT_CONFIG(process) |
33 | #include <QProcess> |
34 | #endif |
35 | #include <QDebug> |
36 | #include <QQmlError> |
37 | #include <cstdlib> |
38 | |
39 | class tst_qmlmin : public QObject |
40 | { |
41 | Q_OBJECT |
42 | public: |
43 | tst_qmlmin(); |
44 | |
45 | private slots: |
46 | void initTestCase(); |
47 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
48 | void qmlMinify_data(); |
49 | void qmlMinify(); |
50 | #endif |
51 | |
52 | private: |
53 | QString qmlminPath; |
54 | QStringList excludedDirs; |
55 | QStringList invalidFiles; |
56 | |
57 | QStringList findFiles(const QDir &); |
58 | bool isInvalidFile(const QFileInfo &fileName) const; |
59 | }; |
60 | |
61 | tst_qmlmin::tst_qmlmin() |
62 | { |
63 | // this test can be slow, double the timeout from the default 5 minutes to 10 minutes |
64 | const int timeout = 10*60*1000; // 10 minutes |
65 | qputenv(varName: "QTEST_FUNCTION_TIMEOUT" , value: QByteArray::number(timeout)); |
66 | } |
67 | |
68 | void tst_qmlmin::initTestCase() |
69 | { |
70 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
71 | qmlminPath = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/qmlmin" ); |
72 | #ifdef Q_OS_WIN |
73 | qmlminPath += QLatin1String(".exe" ); |
74 | #endif |
75 | if (!QFileInfo(qmlminPath).exists()) { |
76 | QString message = QString::fromLatin1(str: "qmlmin executable not found (looked for %0)" ) |
77 | .arg(a: qmlminPath); |
78 | QFAIL(qPrintable(message)); |
79 | } |
80 | |
81 | // Add directories you want excluded here |
82 | |
83 | // These snippets are not expected to run on their own. |
84 | excludedDirs << "doc/src/snippets/qml/visualdatamodel_rootindex" ; |
85 | excludedDirs << "doc/src/snippets/qml/qtbinding" ; |
86 | excludedDirs << "doc/src/snippets/qml/imports" ; |
87 | excludedDirs << "doc/src/snippets/qtquick1/visualdatamodel_rootindex" ; |
88 | excludedDirs << "doc/src/snippets/qtquick1/qtbinding" ; |
89 | excludedDirs << "doc/src/snippets/qtquick1/imports" ; |
90 | excludedDirs << "tests/manual/v4" ; |
91 | excludedDirs << "tests/auto/qml/ecmascripttests" ; |
92 | excludedDirs << "tests/auto/qml/qmllint" ; |
93 | |
94 | // Add invalid files (i.e. files with syntax errors) |
95 | invalidFiles << "tests/auto/quick/qquickloader/data/InvalidSourceComponent.qml" ; |
96 | invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.2.qml" ; |
97 | invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.3.qml" ; |
98 | invalidFiles << "tests/auto/qml/qqmllanguage/data/signal.5.qml" ; |
99 | invalidFiles << "tests/auto/qml/qqmllanguage/data/property.4.qml" ; |
100 | invalidFiles << "tests/auto/qml/qqmllanguage/data/empty.qml" ; |
101 | invalidFiles << "tests/auto/qml/qqmllanguage/data/missingObject.qml" ; |
102 | invalidFiles << "tests/auto/qml/qqmllanguage/data/insertedSemicolon.1.qml" ; |
103 | invalidFiles << "tests/auto/qml/qqmllanguage/data/nonexistantProperty.5.qml" ; |
104 | invalidFiles << "tests/auto/qml/qqmllanguage/data/invalidRoot.1.qml" ; |
105 | invalidFiles << "tests/auto/qml/qqmllanguage/data/invalidQmlEnumValue.1.qml" ; |
106 | invalidFiles << "tests/auto/qml/qqmllanguage/data/invalidQmlEnumValue.2.qml" ; |
107 | invalidFiles << "tests/auto/qml/qquickfolderlistmodel/data/dummy.qml" ; |
108 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/qtbug_22843.js" ; |
109 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/qtbug_22843.library.js" ; |
110 | invalidFiles << "tests/auto/qml/qquickworkerscript/data/script_error_onLoad.js" ; |
111 | invalidFiles << "tests/auto/qml/parserstress/tests/ecma_3/Unicode/regress-352044-02-n.js" ; |
112 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/incrDecrSemicolon_error1.qml" ; |
113 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedFileQualifier.js" ; |
114 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedFileQualifier.2.js" ; |
115 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedImport.js" ; |
116 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedModule.js" ; |
117 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedFile.js" ; |
118 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedModuleQualifier.js" ; |
119 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedModuleQualifier.2.js" ; |
120 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/malformedModuleVersion.js" ; |
121 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/missingFileQualifier.js" ; |
122 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/missingModuleQualifier.js" ; |
123 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/jsimportfail/missingModuleVersion.js" ; |
124 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.1.qml" ; |
125 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.2.qml" ; |
126 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.3.qml" ; |
127 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.4.qml" ; |
128 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.5.qml" ; |
129 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/stringParsing_error.6.qml" ; |
130 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.1.qml" ; |
131 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.2.qml" ; |
132 | invalidFiles << "tests/auto/qml/parserstress/tests/ecma_3/FunExpr/fe-001.js" ; |
133 | invalidFiles << "tests/auto/qml/qjsengine/script/com/trolltech/syntaxerror/__init__.js" ; |
134 | invalidFiles << "tests/auto/qml/debugger/qqmlpreview/data/broken.qml" ; |
135 | invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.2.qml" ; |
136 | invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.3.qml" ; |
137 | invalidFiles << "tests/auto/qml/qqmllanguage/data/requiredProperties.2.qml" ; |
138 | // generatorFunction.qml is not invalid per se, but the minifier cannot handle yield statements |
139 | invalidFiles << "tests/auto/qml/qqmlecmascript/data/generatorFunction.qml" ; |
140 | // minifer can't handle template strings properly |
141 | invalidFiles << "tests/auto/qml/qmlformat/data/verbatimString.qml" ; |
142 | invalidFiles << "tests/auto/qml/qmlformat/data/verbatimString.formatted.qml" ; |
143 | #endif |
144 | } |
145 | |
146 | QStringList tst_qmlmin::findFiles(const QDir &d) |
147 | { |
148 | for (int ii = 0; ii < excludedDirs.count(); ++ii) { |
149 | QString s = excludedDirs.at(i: ii); |
150 | if (d.absolutePath().endsWith(s)) |
151 | return QStringList(); |
152 | } |
153 | |
154 | QStringList rv; |
155 | |
156 | QStringList files = d.entryList(nameFilters: QStringList() << QLatin1String("*.qml" ) << QLatin1String("*.js" ), |
157 | filters: QDir::Files); |
158 | foreach (const QString &file, files) { |
159 | rv << d.absoluteFilePath(fileName: file); |
160 | } |
161 | |
162 | QStringList dirs = d.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot | |
163 | QDir::NoSymLinks); |
164 | foreach (const QString &dir, dirs) { |
165 | QDir sub = d; |
166 | sub.cd(dirName: dir); |
167 | rv << findFiles(d: sub); |
168 | } |
169 | |
170 | return rv; |
171 | } |
172 | |
173 | bool tst_qmlmin::isInvalidFile(const QFileInfo &fileName) const |
174 | { |
175 | foreach (const QString &invalidFile, invalidFiles) { |
176 | if (fileName.absoluteFilePath().endsWith(s: invalidFile)) |
177 | return true; |
178 | } |
179 | return false; |
180 | } |
181 | |
182 | /* |
183 | This test runs all the examples in the Qt QML UI source tree and ensures |
184 | that they start and exit cleanly. |
185 | |
186 | Examples are any .qml files under the examples/ directory that start |
187 | with a lower case letter. |
188 | */ |
189 | |
190 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
191 | void tst_qmlmin::qmlMinify_data() |
192 | { |
193 | QTest::addColumn<QString>(name: "file" ); |
194 | |
195 | QString examples = QLatin1String(SRCDIR) + "/../../../../examples/" ; |
196 | QString tests = QLatin1String(SRCDIR) + "/../../../../tests/" ; |
197 | |
198 | QStringList files; |
199 | files << findFiles(d: QDir(examples)); |
200 | files << findFiles(d: QDir(tests)); |
201 | |
202 | foreach (const QString &file, files) |
203 | QTest::newRow(qPrintable(file)) << file; |
204 | } |
205 | #endif |
206 | |
207 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
208 | void tst_qmlmin::qmlMinify() |
209 | { |
210 | QFETCH(QString, file); |
211 | |
212 | QProcess qmlminify; |
213 | |
214 | // Restrict line width to 100 characters |
215 | qmlminify.start(program: qmlminPath, arguments: QStringList() << QLatin1String("--verify-only" ) << QLatin1String("-w100" ) << file); |
216 | qmlminify.waitForFinished(); |
217 | |
218 | QCOMPARE(qmlminify.error(), QProcess::UnknownError); |
219 | QCOMPARE(qmlminify.exitStatus(), QProcess::NormalExit); |
220 | |
221 | if (isInvalidFile(fileName: file)) |
222 | QCOMPARE(qmlminify.exitCode(), EXIT_FAILURE); // cannot minify files with syntax errors |
223 | else |
224 | QCOMPARE(qmlminify.exitCode(), 0); |
225 | } |
226 | #endif |
227 | |
228 | QTEST_MAIN(tst_qmlmin) |
229 | |
230 | #include "tst_qmlmin.moc" |
231 | |