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 <QtTest/QtTest> |
30 | |
31 | #include <QMimeData> |
32 | |
33 | class tst_QMimeData : public QObject |
34 | { |
35 | Q_OBJECT |
36 | private slots: |
37 | void clear() const; |
38 | void colorData() const; |
39 | void data() const; |
40 | void formats() const; |
41 | void hasColor() const; |
42 | void hasFormat() const; |
43 | void hasHtml() const; |
44 | void hasImage() const; |
45 | // hasText() covered by setText() |
46 | // hasUrls() covered by setUrls() |
47 | // html() covered by setHtml() |
48 | void imageData() const; |
49 | void removeFormat() const; |
50 | // setColorData() covered by hasColor() |
51 | // setData() covered in a few different tests |
52 | void setHtml() const; |
53 | // setImageData() covered in a few tests |
54 | void setText() const; |
55 | void setUrls() const; |
56 | // text() covered in setText() |
57 | // urls() covered by setUrls() |
58 | }; |
59 | |
60 | void tst_QMimeData::clear() const |
61 | { |
62 | QMimeData mimeData; |
63 | |
64 | // set, clear, verify empty |
65 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
66 | QVERIFY(mimeData.hasText()); |
67 | mimeData.clear(); |
68 | QVERIFY(!mimeData.hasText()); |
69 | |
70 | // repopulate, verify not empty |
71 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
72 | QVERIFY(mimeData.hasText()); |
73 | } |
74 | |
75 | void tst_QMimeData::colorData() const |
76 | { |
77 | QMimeData mimeData; |
78 | QColor red = Qt::red; |
79 | QColor blue = Qt::blue; |
80 | |
81 | // set, verify |
82 | mimeData.setColorData(red); |
83 | QVERIFY(mimeData.hasColor()); |
84 | QCOMPARE(qvariant_cast<QColor>(mimeData.colorData()), red); |
85 | |
86 | // change, verify |
87 | mimeData.setColorData(QColor(Qt::blue)); |
88 | QVERIFY(mimeData.hasColor()); |
89 | QCOMPARE(qvariant_cast<QColor>(mimeData.colorData()), blue); |
90 | } |
91 | |
92 | void tst_QMimeData::data() const |
93 | { |
94 | QMimeData mimeData; |
95 | |
96 | // set text, verify |
97 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
98 | QCOMPARE(mimeData.data("text/plain" ), QByteArray("pirates" )); |
99 | QCOMPARE(mimeData.data("text/html" ).length(), 0); |
100 | |
101 | // html time |
102 | mimeData.setData(mimetype: "text/html" , data: "ninjas" ); |
103 | QCOMPARE(mimeData.data("text/html" ), QByteArray("ninjas" )); |
104 | QCOMPARE(mimeData.data("text/plain" ), QByteArray("pirates" )); // make sure text not damaged |
105 | QCOMPARE(mimeData.data("text/html" ), mimeData.html().toLatin1()); |
106 | } |
107 | |
108 | void tst_QMimeData::formats() const |
109 | { |
110 | QMimeData mimeData; |
111 | |
112 | // set text, verify |
113 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
114 | QCOMPARE(mimeData.formats(), QStringList() << "text/plain" ); |
115 | |
116 | // set html, verify |
117 | mimeData.setData(mimetype: "text/html" , data: "ninjas" ); |
118 | QCOMPARE(mimeData.formats(), QStringList() << "text/plain" << "text/html" ); |
119 | |
120 | // clear, verify |
121 | mimeData.clear(); |
122 | QCOMPARE(mimeData.formats(), QStringList()); |
123 | |
124 | // set an odd format, verify |
125 | mimeData.setData(mimetype: "foo/bar" , data: "somevalue" ); |
126 | QCOMPARE(mimeData.formats(), QStringList() << "foo/bar" ); |
127 | } |
128 | |
129 | void tst_QMimeData::hasColor() const |
130 | { |
131 | QMimeData mimeData; |
132 | |
133 | // initial state |
134 | QVERIFY(!mimeData.hasColor()); |
135 | |
136 | // set, verify |
137 | mimeData.setColorData(QColor(Qt::red)); |
138 | QVERIFY(mimeData.hasColor()); |
139 | |
140 | // clear, verify |
141 | mimeData.clear(); |
142 | QVERIFY(!mimeData.hasColor()); |
143 | |
144 | // set something else, verify |
145 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
146 | QVERIFY(!mimeData.hasColor()); |
147 | } |
148 | |
149 | void tst_QMimeData::hasFormat() const |
150 | { |
151 | QMimeData mimeData; |
152 | |
153 | // initial state |
154 | QVERIFY(!mimeData.hasFormat("text/plain" )); |
155 | |
156 | // add, verify |
157 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
158 | QVERIFY(mimeData.hasFormat("text/plain" )); |
159 | QVERIFY(!mimeData.hasFormat("text/html" )); |
160 | |
161 | // clear, verify |
162 | mimeData.clear(); |
163 | QVERIFY(!mimeData.hasFormat("text/plain" )); |
164 | QVERIFY(!mimeData.hasFormat("text/html" )); |
165 | } |
166 | |
167 | void tst_QMimeData::hasHtml() const |
168 | { |
169 | QMimeData mimeData; |
170 | |
171 | // initial state |
172 | QVERIFY(!mimeData.hasHtml()); |
173 | |
174 | // add plain, verify false |
175 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
176 | QVERIFY(!mimeData.hasHtml()); |
177 | |
178 | // add html, verify |
179 | mimeData.setData(mimetype: "text/html" , data: "ninjas" ); |
180 | QVERIFY(mimeData.hasHtml()); |
181 | |
182 | // clear, verify |
183 | mimeData.clear(); |
184 | QVERIFY(!mimeData.hasHtml()); |
185 | |
186 | // readd, verify |
187 | mimeData.setData(mimetype: "text/html" , data: "ninjas" ); |
188 | QVERIFY(mimeData.hasHtml()); |
189 | } |
190 | |
191 | void tst_QMimeData::hasImage() const |
192 | { |
193 | QMimeData mimeData; |
194 | |
195 | // initial state |
196 | QVERIFY(!mimeData.hasImage()); |
197 | |
198 | // add text, verify false |
199 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
200 | QVERIFY(!mimeData.hasImage()); |
201 | |
202 | // add image |
203 | mimeData.setImageData(QImage()); |
204 | QVERIFY(mimeData.hasImage()); |
205 | |
206 | // clear, verify |
207 | mimeData.clear(); |
208 | QVERIFY(!mimeData.hasImage()); |
209 | } |
210 | |
211 | void tst_QMimeData::imageData() const |
212 | { |
213 | QMimeData mimeData; |
214 | |
215 | // initial state |
216 | QCOMPARE(mimeData.imageData(), QVariant()); |
217 | |
218 | // set, test |
219 | mimeData.setImageData(QImage()); |
220 | QVERIFY(mimeData.hasImage()); |
221 | QCOMPARE(mimeData.imageData(), QVariant(QImage())); |
222 | |
223 | // clear, verify |
224 | mimeData.clear(); |
225 | QCOMPARE(mimeData.imageData(), QVariant()); |
226 | } |
227 | |
228 | void tst_QMimeData::removeFormat() const |
229 | { |
230 | QMimeData mimeData; |
231 | |
232 | // add, verify |
233 | mimeData.setData(mimetype: "text/plain" , data: "pirates" ); |
234 | QVERIFY(mimeData.hasFormat("text/plain" )); |
235 | |
236 | // add another, verify |
237 | mimeData.setData(mimetype: "text/html" , data: "ninjas" ); |
238 | QVERIFY(mimeData.hasFormat("text/html" )); |
239 | |
240 | // remove, verify |
241 | mimeData.removeFormat(mimetype: "text/plain" ); |
242 | QVERIFY(!mimeData.hasFormat("text/plain" )); |
243 | QVERIFY(mimeData.hasFormat("text/html" )); |
244 | |
245 | // remove, verify |
246 | mimeData.removeFormat(mimetype: "text/html" ); |
247 | QVERIFY(!mimeData.hasFormat("text/plain" )); |
248 | QVERIFY(!mimeData.hasFormat("text/html" )); |
249 | } |
250 | |
251 | void tst_QMimeData::setHtml() const |
252 | { |
253 | QMimeData mimeData; |
254 | |
255 | // initial state |
256 | QVERIFY(!mimeData.hasHtml()); |
257 | |
258 | // add html, verify |
259 | mimeData.setHtml("ninjas" ); |
260 | QVERIFY(mimeData.hasHtml()); |
261 | QCOMPARE(mimeData.html(), QLatin1String("ninjas" )); |
262 | |
263 | // reset html |
264 | mimeData.setHtml("pirates" ); |
265 | QVERIFY(mimeData.hasHtml()); |
266 | QCOMPARE(mimeData.html(), QLatin1String("pirates" )); |
267 | } |
268 | |
269 | void tst_QMimeData::setText() const |
270 | { |
271 | QMimeData mimeData; |
272 | |
273 | // verify initial state |
274 | QCOMPARE(mimeData.text(), QLatin1String("" )); |
275 | QVERIFY(!mimeData.hasText()); |
276 | |
277 | // set, verify |
278 | mimeData.setText("pirates" ); |
279 | QVERIFY(mimeData.hasText()); |
280 | QCOMPARE(mimeData.text(), QLatin1String("pirates" )); |
281 | QCOMPARE(mimeData.text().toLatin1(), mimeData.data("text/plain" )); |
282 | |
283 | // reset, verify |
284 | mimeData.setText("ninjas" ); |
285 | QVERIFY(mimeData.hasText()); |
286 | QCOMPARE(mimeData.text(), QLatin1String("ninjas" )); |
287 | QCOMPARE(mimeData.text().toLatin1(), mimeData.data("text/plain" )); |
288 | |
289 | // clear, verify |
290 | mimeData.clear(); |
291 | QCOMPARE(mimeData.text(), QLatin1String("" )); |
292 | QVERIFY(!mimeData.hasText()); |
293 | } |
294 | |
295 | // Publish retrieveData for verifying content validity |
296 | class TstMetaData : public QMimeData |
297 | { |
298 | public: |
299 | using QMimeData::retrieveData; |
300 | }; |
301 | |
302 | void tst_QMimeData::setUrls() const |
303 | { |
304 | TstMetaData mimeData; |
305 | QList<QUrl> shortUrlList; |
306 | QList<QUrl> longUrlList; |
307 | |
308 | // set up |
309 | shortUrlList += QUrl("http://qt-project.org" ); |
310 | longUrlList = shortUrlList; |
311 | longUrlList += QUrl("http://www.google.com" ); |
312 | |
313 | // verify initial state |
314 | QCOMPARE(mimeData.hasUrls(), false); |
315 | |
316 | // set a few, verify |
317 | mimeData.setUrls(shortUrlList); |
318 | QCOMPARE(mimeData.urls(), shortUrlList); |
319 | QCOMPARE(mimeData.text(), QString("http://qt-project.org" )); |
320 | |
321 | // change them, verify |
322 | mimeData.setUrls(longUrlList); |
323 | QCOMPARE(mimeData.urls(), longUrlList); |
324 | QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n" )); |
325 | |
326 | // test and verify that setData doesn't corrupt url content |
327 | foreach (const QString &format, mimeData.formats()) { |
328 | QVariant before = mimeData.retrieveData(mimetype: format, preferredType: QVariant::ByteArray); |
329 | mimeData.setData(mimetype: format, data: mimeData.data(mimetype: format)); |
330 | QVariant after = mimeData.retrieveData(mimetype: format, preferredType: QVariant::ByteArray); |
331 | QCOMPARE(after, before); |
332 | } |
333 | |
334 | // clear, verify |
335 | mimeData.clear(); |
336 | QCOMPARE(mimeData.hasUrls(), false); |
337 | QCOMPARE(mimeData.hasText(), false); |
338 | } |
339 | |
340 | QTEST_APPLESS_MAIN(tst_QMimeData) |
341 | #include "tst_qmimedata.moc" |
342 | |