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 <private/qmimetype_p.h> |
30 | |
31 | #include <qmimetype.h> |
32 | #include <qmimedatabase.h> |
33 | |
34 | #include <QtTest/QtTest> |
35 | |
36 | |
37 | class tst_qmimetype : public QObject |
38 | { |
39 | Q_OBJECT |
40 | |
41 | private slots: |
42 | void initTestCase(); |
43 | |
44 | void isValid(); |
45 | void name(); |
46 | void genericIconName(); |
47 | void iconName(); |
48 | void suffixes(); |
49 | void gadget(); |
50 | }; |
51 | |
52 | // ------------------------------------------------------------------------------------------------ |
53 | |
54 | void tst_qmimetype::initTestCase() |
55 | { |
56 | qputenv(varName: "XDG_DATA_DIRS" , value: "doesnotexist" ); |
57 | } |
58 | |
59 | // ------------------------------------------------------------------------------------------------ |
60 | |
61 | static QString qMimeTypeName() |
62 | { |
63 | static const QString result ("No name of the MIME type" ); |
64 | return result; |
65 | } |
66 | |
67 | static QString qMimeTypeGenericIconName() |
68 | { |
69 | static const QString result ("No file name of an icon image that represents the MIME type" ); |
70 | return result; |
71 | } |
72 | |
73 | static QString qMimeTypeIconName() |
74 | { |
75 | static const QString result ("No file name of an icon image that represents the MIME type" ); |
76 | return result; |
77 | } |
78 | |
79 | static QStringList buildQMimeTypeFilenameExtensions() |
80 | { |
81 | QStringList result; |
82 | result << QString::fromLatin1(str: "*.png" ); |
83 | return result; |
84 | } |
85 | |
86 | static QStringList qMimeTypeGlobPatterns() |
87 | { |
88 | static const QStringList result (buildQMimeTypeFilenameExtensions()); |
89 | return result; |
90 | } |
91 | |
92 | // ------------------------------------------------------------------------------------------------ |
93 | |
94 | QMIMETYPE_BUILDER_FROM_RVALUE_REFS |
95 | |
96 | // ------------------------------------------------------------------------------------------------ |
97 | |
98 | void tst_qmimetype::isValid() |
99 | { |
100 | QMimeType instantiatedQMimeType ( |
101 | buildQMimeType ( |
102 | name: qMimeTypeName(), |
103 | genericIconName: qMimeTypeGenericIconName(), |
104 | iconName: qMimeTypeIconName(), |
105 | globPatterns: qMimeTypeGlobPatterns() |
106 | ) |
107 | ); |
108 | |
109 | QVERIFY(instantiatedQMimeType.isValid()); |
110 | |
111 | QMimeType otherQMimeType (instantiatedQMimeType); |
112 | |
113 | QVERIFY(otherQMimeType.isValid()); |
114 | QCOMPARE(instantiatedQMimeType, otherQMimeType); |
115 | |
116 | QMimeType defaultQMimeType; |
117 | |
118 | QVERIFY(!defaultQMimeType.isValid()); |
119 | } |
120 | |
121 | // ------------------------------------------------------------------------------------------------ |
122 | |
123 | void tst_qmimetype::name() |
124 | { |
125 | QMimeType instantiatedQMimeType ( |
126 | buildQMimeType ( |
127 | name: qMimeTypeName(), |
128 | genericIconName: qMimeTypeGenericIconName(), |
129 | iconName: qMimeTypeIconName(), |
130 | globPatterns: qMimeTypeGlobPatterns() |
131 | ) |
132 | ); |
133 | |
134 | QMimeType otherQMimeType ( |
135 | buildQMimeType ( |
136 | name: QString(), |
137 | genericIconName: qMimeTypeGenericIconName(), |
138 | iconName: qMimeTypeIconName(), |
139 | globPatterns: qMimeTypeGlobPatterns() |
140 | ) |
141 | ); |
142 | |
143 | // Verify that the Name is part of the equality test: |
144 | QCOMPARE(instantiatedQMimeType.name(), qMimeTypeName()); |
145 | |
146 | QVERIFY(instantiatedQMimeType != otherQMimeType); |
147 | QVERIFY(!(instantiatedQMimeType == otherQMimeType)); |
148 | } |
149 | |
150 | // ------------------------------------------------------------------------------------------------ |
151 | |
152 | void tst_qmimetype::genericIconName() |
153 | { |
154 | QMimeType instantiatedQMimeType ( |
155 | buildQMimeType ( |
156 | name: qMimeTypeName(), |
157 | genericIconName: qMimeTypeGenericIconName(), |
158 | iconName: qMimeTypeIconName(), |
159 | globPatterns: qMimeTypeGlobPatterns() |
160 | ) |
161 | ); |
162 | |
163 | QCOMPARE(instantiatedQMimeType.genericIconName(), qMimeTypeGenericIconName()); |
164 | } |
165 | |
166 | // ------------------------------------------------------------------------------------------------ |
167 | |
168 | void tst_qmimetype::iconName() |
169 | { |
170 | QMimeType instantiatedQMimeType ( |
171 | buildQMimeType ( |
172 | name: qMimeTypeName(), |
173 | genericIconName: qMimeTypeGenericIconName(), |
174 | iconName: qMimeTypeIconName(), |
175 | globPatterns: qMimeTypeGlobPatterns() |
176 | ) |
177 | ); |
178 | |
179 | QCOMPARE(instantiatedQMimeType.iconName(), qMimeTypeIconName()); |
180 | } |
181 | |
182 | // ------------------------------------------------------------------------------------------------ |
183 | |
184 | void tst_qmimetype::suffixes() |
185 | { |
186 | QMimeType instantiatedQMimeType ( |
187 | buildQMimeType ( |
188 | name: qMimeTypeName(), |
189 | genericIconName: qMimeTypeGenericIconName(), |
190 | iconName: qMimeTypeIconName(), |
191 | globPatterns: qMimeTypeGlobPatterns() |
192 | ) |
193 | ); |
194 | |
195 | QCOMPARE(instantiatedQMimeType.globPatterns(), qMimeTypeGlobPatterns()); |
196 | QCOMPARE(instantiatedQMimeType.suffixes(), QStringList() << QString::fromLatin1("png" )); |
197 | } |
198 | |
199 | // ------------------------------------------------------------------------------------------------ |
200 | |
201 | void tst_qmimetype::gadget() |
202 | { |
203 | QMimeType instantiatedQMimeType ( |
204 | buildQMimeType ( |
205 | name: qMimeTypeName(), |
206 | genericIconName: qMimeTypeGenericIconName(), |
207 | iconName: qMimeTypeIconName(), |
208 | globPatterns: qMimeTypeGlobPatterns() |
209 | ) |
210 | ); |
211 | |
212 | const QMetaObject *metaObject = &instantiatedQMimeType.staticMetaObject; |
213 | |
214 | QCOMPARE(metaObject->className(), "QMimeType" ); |
215 | QVariantMap properties; |
216 | for (int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++) { |
217 | QMetaProperty property = metaObject->property(index: i); |
218 | properties[property.name()] = property.readOnGadget(gadget: &instantiatedQMimeType); |
219 | } |
220 | |
221 | QCOMPARE(properties["valid" ].toBool(), instantiatedQMimeType.isValid()); |
222 | QCOMPARE(properties["isDefault" ].toBool(), instantiatedQMimeType.isDefault()); |
223 | QCOMPARE(properties["name" ].toString(), instantiatedQMimeType.name()); |
224 | QCOMPARE(properties["comment" ].toString(), instantiatedQMimeType.comment()); |
225 | QCOMPARE(properties["genericIconName" ].toString(), instantiatedQMimeType.genericIconName()); |
226 | QCOMPARE(properties["iconName" ].toString(), instantiatedQMimeType.iconName()); |
227 | QCOMPARE(properties["globPatterns" ].toStringList(), instantiatedQMimeType.globPatterns()); |
228 | QCOMPARE(properties["parentMimeTypes" ].toStringList(), instantiatedQMimeType.parentMimeTypes()); |
229 | QCOMPARE(properties["allAncestors" ].toStringList(), instantiatedQMimeType.allAncestors()); |
230 | QCOMPARE(properties["aliases" ].toStringList(), instantiatedQMimeType.aliases()); |
231 | QCOMPARE(properties["suffixes" ].toStringList(), instantiatedQMimeType.suffixes()); |
232 | QCOMPARE(properties["preferredSuffix" ].toString(), instantiatedQMimeType.preferredSuffix()); |
233 | QCOMPARE(properties["filterString" ].toString(), instantiatedQMimeType.filterString()); |
234 | |
235 | QVERIFY(metaObject->indexOfMethod("inherits(QString)" ) >= 0); |
236 | } |
237 | |
238 | // ------------------------------------------------------------------------------------------------ |
239 | |
240 | QTEST_GUILESS_MAIN(tst_qmimetype) |
241 | #include "tst_qmimetype.moc" |
242 | |