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 | } |
64 | |
65 | void tst_qmlmin::initTestCase() |
66 | { |
67 | qmlminPath = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QLatin1String("/qmlmin" ); |
68 | #ifdef Q_OS_WIN |
69 | qmlminPath += QLatin1String(".exe" ); |
70 | #endif |
71 | if (!QFileInfo(qmlminPath).exists()) { |
72 | QString message = QString::fromLatin1(str: "qmlmin executable not found (looked for %0)" ) |
73 | .arg(a: qmlminPath); |
74 | QFAIL(qPrintable(message)); |
75 | } |
76 | |
77 | // Add directories you want excluded here |
78 | // excludedDirs << "exclude/this/dir"; |
79 | |
80 | // Add invalid files (i.e. files with syntax errors) |
81 | // invalidFiles << "exclude/this/file.txt"; |
82 | } |
83 | |
84 | QStringList tst_qmlmin::findFiles(const QDir &d) |
85 | { |
86 | for (int ii = 0; ii < excludedDirs.count(); ++ii) { |
87 | QString s = excludedDirs.at(i: ii); |
88 | if (d.absolutePath().endsWith(s)) |
89 | return QStringList(); |
90 | } |
91 | |
92 | QStringList rv; |
93 | |
94 | QStringList files = d.entryList(nameFilters: QStringList() << QLatin1String("*.qml" ) << QLatin1String("*.js" ), |
95 | filters: QDir::Files); |
96 | foreach (const QString &file, files) { |
97 | rv << d.absoluteFilePath(fileName: file); |
98 | } |
99 | |
100 | QStringList dirs = d.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot | |
101 | QDir::NoSymLinks); |
102 | foreach (const QString &dir, dirs) { |
103 | QDir sub = d; |
104 | sub.cd(dirName: dir); |
105 | rv << findFiles(d: sub); |
106 | } |
107 | |
108 | return rv; |
109 | } |
110 | |
111 | bool tst_qmlmin::isInvalidFile(const QFileInfo &fileName) const |
112 | { |
113 | foreach (const QString &invalidFile, invalidFiles) { |
114 | if (fileName.absoluteFilePath().endsWith(s: invalidFile)) |
115 | return true; |
116 | } |
117 | return false; |
118 | } |
119 | |
120 | /* |
121 | This test runs all the examples in the Qt QML UI source tree and ensures |
122 | that they start and exit cleanly. |
123 | |
124 | Examples are any .qml files under the examples/ directory that start |
125 | with a lower case letter. |
126 | */ |
127 | |
128 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
129 | void tst_qmlmin::qmlMinify_data() |
130 | { |
131 | QTest::addColumn<QString>(name: "file" ); |
132 | |
133 | QString examples = QLatin1String(SRCDIR) + "/../../../../examples/" ; |
134 | QString tests = QLatin1String(SRCDIR) + "/../../../../tests/" ; |
135 | |
136 | QStringList files; |
137 | files << findFiles(d: QDir(examples)); |
138 | files << findFiles(d: QDir(tests)); |
139 | |
140 | foreach (const QString &file, files) |
141 | QTest::newRow(qPrintable(file)) << file; |
142 | } |
143 | #endif |
144 | |
145 | #if QT_CONFIG(process) && !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled |
146 | void tst_qmlmin::qmlMinify() |
147 | { |
148 | QFETCH(QString, file); |
149 | |
150 | QProcess qmlminify; |
151 | |
152 | // Restrict line width to 100 characters |
153 | qmlminify.start(program: qmlminPath, arguments: QStringList() << QLatin1String("--verify-only" ) << QLatin1String("-w100" ) << file); |
154 | qmlminify.waitForFinished(); |
155 | |
156 | QCOMPARE(qmlminify.error(), QProcess::UnknownError); |
157 | QCOMPARE(qmlminify.exitStatus(), QProcess::NormalExit); |
158 | |
159 | if (isInvalidFile(fileName: file)) |
160 | QCOMPARE(qmlminify.exitCode(), EXIT_FAILURE); // cannot minify files with syntax errors |
161 | else |
162 | QCOMPARE(qmlminify.exitCode(), 0); |
163 | } |
164 | #endif |
165 | |
166 | QTEST_MAIN(tst_qmlmin) |
167 | |
168 | #include "tst_qmlmin.moc" |
169 | |