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 QtNfc module 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 <qndefrecord.h> |
32 | #include <qndefnfctextrecord.h> |
33 | #include <qndefnfcurirecord.h> |
34 | #include <qqmlndefrecord.h> |
35 | |
36 | QT_USE_NAMESPACE |
37 | |
38 | Q_DECLARE_METATYPE(QNdefRecord::TypeNameFormat) |
39 | |
40 | class tst_QNdefRecord : public QObject |
41 | { |
42 | Q_OBJECT |
43 | |
44 | public: |
45 | tst_QNdefRecord(); |
46 | ~tst_QNdefRecord(); |
47 | |
48 | private slots: |
49 | void tst_record(); |
50 | |
51 | void tst_textRecord_data(); |
52 | void tst_textRecord(); |
53 | |
54 | void tst_uriRecord_data(); |
55 | void tst_uriRecord(); |
56 | |
57 | void tst_declarative_record_data(); |
58 | void tst_declarative_record(); |
59 | |
60 | void tst_declarativeChangedSignals(); |
61 | }; |
62 | |
63 | tst_QNdefRecord::tst_QNdefRecord() |
64 | { |
65 | } |
66 | |
67 | tst_QNdefRecord::~tst_QNdefRecord() |
68 | { |
69 | } |
70 | |
71 | void tst_QNdefRecord::tst_record() |
72 | { |
73 | // test empty record |
74 | { |
75 | QNdefRecord record; |
76 | |
77 | QVERIFY(record.isEmpty()); |
78 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Empty); |
79 | QVERIFY(record.type().isEmpty()); |
80 | QVERIFY(record.id().isEmpty()); |
81 | QVERIFY(record.payload().isEmpty()); |
82 | |
83 | QVERIFY(!record.isRecordType<QNdefNfcTextRecord>()); |
84 | QVERIFY(!record.isRecordType<QNdefNfcUriRecord>()); |
85 | |
86 | QCOMPARE(record, QNdefRecord()); |
87 | QVERIFY(!(record != QNdefRecord())); |
88 | |
89 | QQmlNdefRecord declRecord; |
90 | QCOMPARE(declRecord.record(), record); |
91 | QCOMPARE(declRecord.type(), QString()); |
92 | QCOMPARE(declRecord.typeNameFormat(), QQmlNdefRecord::Empty); |
93 | } |
94 | |
95 | // test type name format |
96 | { |
97 | QNdefRecord record; |
98 | |
99 | record.setTypeNameFormat(QNdefRecord::Empty); |
100 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Empty); |
101 | |
102 | record.setTypeNameFormat(QNdefRecord::NfcRtd); |
103 | QCOMPARE(record.typeNameFormat(), QNdefRecord::NfcRtd); |
104 | |
105 | record.setTypeNameFormat(QNdefRecord::Mime); |
106 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Mime); |
107 | |
108 | record.setTypeNameFormat(QNdefRecord::Uri); |
109 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Uri); |
110 | |
111 | record.setTypeNameFormat(QNdefRecord::ExternalRtd); |
112 | QCOMPARE(record.typeNameFormat(), QNdefRecord::ExternalRtd); |
113 | |
114 | record.setTypeNameFormat(QNdefRecord::Unknown); |
115 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Unknown); |
116 | |
117 | record.setTypeNameFormat(QNdefRecord::TypeNameFormat(6)); |
118 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Unknown); |
119 | |
120 | record.setTypeNameFormat(QNdefRecord::TypeNameFormat(7)); |
121 | QCOMPARE(record.typeNameFormat(), QNdefRecord::Unknown); |
122 | } |
123 | |
124 | // test type |
125 | { |
126 | QNdefRecord record; |
127 | |
128 | record.setType("test type" ); |
129 | QCOMPARE(record.type(), QByteArray("test type" )); |
130 | } |
131 | |
132 | // test id |
133 | { |
134 | QNdefRecord record; |
135 | |
136 | record.setId("test id" ); |
137 | QCOMPARE(record.id(), QByteArray("test id" )); |
138 | } |
139 | |
140 | // test payload |
141 | { |
142 | QNdefRecord record; |
143 | |
144 | record.setPayload("test payload" ); |
145 | QVERIFY(!record.isEmpty()); |
146 | QVERIFY(!record.payload().isEmpty()); |
147 | QCOMPARE(record.payload(), QByteArray("test payload" )); |
148 | } |
149 | |
150 | // test copy |
151 | { |
152 | QNdefRecord record; |
153 | record.setTypeNameFormat(QNdefRecord::ExternalRtd); |
154 | record.setType("qt-project.org:test-rtd" ); |
155 | record.setId("test id" ); |
156 | record.setPayload("test payload" ); |
157 | |
158 | QNdefRecord ccopy(record); |
159 | |
160 | QCOMPARE(record.typeNameFormat(), ccopy.typeNameFormat()); |
161 | QCOMPARE(record.type(), ccopy.type()); |
162 | QCOMPARE(record.id(), ccopy.id()); |
163 | QCOMPARE(record.payload(), ccopy.payload()); |
164 | |
165 | QVERIFY(record == ccopy); |
166 | QVERIFY(!(record != ccopy)); |
167 | |
168 | QNdefRecord acopy; |
169 | acopy = record; |
170 | |
171 | QCOMPARE(record.typeNameFormat(), acopy.typeNameFormat()); |
172 | QCOMPARE(record.type(), acopy.type()); |
173 | QCOMPARE(record.id(), acopy.id()); |
174 | QCOMPARE(record.payload(), acopy.payload()); |
175 | |
176 | QVERIFY(record == acopy); |
177 | QVERIFY(!(record != acopy)); |
178 | |
179 | QVERIFY(record != QNdefRecord()); |
180 | } |
181 | |
182 | // test comparison |
183 | { |
184 | QNdefRecord record; |
185 | record.setTypeNameFormat(QNdefRecord::ExternalRtd); |
186 | record.setType("qt-project.org:test-rtd" ); |
187 | record.setId("test id" ); |
188 | record.setPayload("test payload" ); |
189 | |
190 | QNdefRecord other; |
191 | other.setTypeNameFormat(QNdefRecord::ExternalRtd); |
192 | other.setType("qt-project.org:test-other-rtd" ); |
193 | other.setId("test other id" ); |
194 | other.setPayload("test other payload" ); |
195 | |
196 | QVERIFY(record != other); |
197 | } |
198 | } |
199 | |
200 | void tst_QNdefRecord::tst_textRecord_data() |
201 | { |
202 | QTest::addColumn<QString>(name: "locale" ); |
203 | QTest::addColumn<QString>(name: "text" ); |
204 | QTest::addColumn<bool>(name: "utf8" ); |
205 | QTest::addColumn<QByteArray>(name: "payload" ); |
206 | |
207 | |
208 | QTest::newRow(dataTag: "en_US utf8" ) << QString::fromLatin1(str: "en_US" ) |
209 | << QString::fromLatin1(str: "Test String" ) |
210 | << true |
211 | << QByteArray::fromHex(hexEncoded: "05656E5F55535465737420537472696E67" ); |
212 | QTest::newRow(dataTag: "en_US utf16" ) << QString::fromLatin1(str: "en_US" ) |
213 | << QString::fromLatin1(str: "Test String" ) |
214 | << false |
215 | << QByteArray::fromHex(hexEncoded: "85656E5F5553FEFF005400650073007400200053007400720069006E0067" ); |
216 | QTest::newRow(dataTag: "ja_JP utf8" ) << QString::fromLatin1(str: "ja_JP" ) |
217 | << QString::fromUtf8(str: "\343\203\206\343\202\271\343\203\210\346" |
218 | "\226\207\345\255\227\345\210\227" ) |
219 | << true |
220 | << QByteArray::fromHex(hexEncoded: "056A615F4A50E38386E382B9E38388E69687E5AD97E58897" ); |
221 | QTest::newRow(dataTag: "ja_JP utf16" ) << QString::fromLatin1(str: "ja_JP" ) |
222 | << QString::fromUtf8(str: "\343\203\206\343\202\271\343\203\210\346" |
223 | "\226\207\345\255\227\345\210\227" ) |
224 | << false |
225 | << QByteArray::fromHex(hexEncoded: "856A615F4A50FEFF30C630B930C865875B575217" ); |
226 | } |
227 | |
228 | void tst_QNdefRecord::tst_textRecord() |
229 | { |
230 | QFETCH(QString, locale); |
231 | QFETCH(QString, text); |
232 | QFETCH(bool, utf8); |
233 | QFETCH(QByteArray, payload); |
234 | |
235 | // test setters |
236 | { |
237 | QNdefNfcTextRecord record; |
238 | record.setLocale(locale); |
239 | record.setText(text); |
240 | record.setEncoding(utf8 ? QNdefNfcTextRecord::Utf8 : QNdefNfcTextRecord::Utf16); |
241 | |
242 | QCOMPARE(record.payload(), payload); |
243 | |
244 | QVERIFY(record != QNdefRecord()); |
245 | |
246 | QQmlNdefRecord declRecord(record); |
247 | QCOMPARE(declRecord.record(), QNdefRecord(record)); |
248 | QCOMPARE(declRecord.type(), QString("T" )); |
249 | QCOMPARE(declRecord.typeNameFormat(), QQmlNdefRecord::NfcRtd); |
250 | } |
251 | |
252 | // test getters |
253 | { |
254 | QNdefNfcTextRecord record; |
255 | record.setPayload(payload); |
256 | |
257 | QCOMPARE(record.locale(), locale); |
258 | QCOMPARE(record.text(), text); |
259 | QCOMPARE(record.encoding() == QNdefNfcTextRecord::Utf8, utf8); |
260 | } |
261 | |
262 | // test copy |
263 | { |
264 | QNdefRecord record; |
265 | record.setTypeNameFormat(QNdefRecord::NfcRtd); |
266 | record.setType("T" ); |
267 | record.setPayload(payload); |
268 | |
269 | QVERIFY(!record.isRecordType<QNdefRecord>()); |
270 | QVERIFY(record.isRecordType<QNdefNfcTextRecord>()); |
271 | QVERIFY(!record.isRecordType<QNdefNfcUriRecord>()); |
272 | |
273 | QNdefNfcTextRecord textRecord(record); |
274 | |
275 | QVERIFY(!textRecord.isEmpty()); |
276 | |
277 | QVERIFY(record == textRecord); |
278 | |
279 | QCOMPARE(textRecord.locale(), locale); |
280 | QCOMPARE(textRecord.text(), text); |
281 | QCOMPARE(textRecord.encoding() == QNdefNfcTextRecord::Utf8, utf8); |
282 | } |
283 | } |
284 | |
285 | void tst_QNdefRecord::tst_uriRecord_data() |
286 | { |
287 | QTest::addColumn<QString>(name: "url" ); |
288 | QTest::addColumn<QByteArray>(name: "payload" ); |
289 | |
290 | |
291 | QTest::newRow(dataTag: "http" ) << QString::fromLatin1(str: "http://qt-project.org/" ) |
292 | << QByteArray::fromHex(hexEncoded: "0371742d70726f6a6563742e6f72672f" ); |
293 | QTest::newRow(dataTag: "tel" ) << QString::fromLatin1(str: "tel:+1234567890" ) |
294 | << QByteArray::fromHex(hexEncoded: "052B31323334353637383930" ); |
295 | QTest::newRow(dataTag: "mailto" ) << QString::fromLatin1(str: "mailto:test@example.com" ) |
296 | << QByteArray::fromHex(hexEncoded: "0674657374406578616D706C652E636F6D" ); |
297 | QTest::newRow(dataTag: "urn" ) << QString::fromLatin1(str: "urn:nfc:ext:qt-project.org:test" ) |
298 | << QByteArray::fromHex(hexEncoded: "136E66633A6578743A71742D70726F6A6563742E6F72673A74657374" ); |
299 | } |
300 | |
301 | void tst_QNdefRecord::tst_uriRecord() |
302 | { |
303 | QFETCH(QString, url); |
304 | QFETCH(QByteArray, payload); |
305 | |
306 | // test setters |
307 | { |
308 | QNdefNfcUriRecord record; |
309 | record.setUri(QUrl(url)); |
310 | |
311 | QCOMPARE(record.payload(), payload); |
312 | |
313 | QVERIFY(record != QNdefRecord()); |
314 | |
315 | QQmlNdefRecord declRecord(record); |
316 | QCOMPARE(declRecord.record(), QNdefRecord(record)); |
317 | QCOMPARE(declRecord.type(), QString("U" )); |
318 | QCOMPARE(declRecord.typeNameFormat(), QQmlNdefRecord::NfcRtd); |
319 | } |
320 | |
321 | // test getters |
322 | { |
323 | QNdefNfcUriRecord record; |
324 | record.setPayload(payload); |
325 | |
326 | QCOMPARE(record.uri(), QUrl(url)); |
327 | } |
328 | |
329 | // test copy |
330 | { |
331 | QNdefRecord record; |
332 | record.setTypeNameFormat(QNdefRecord::NfcRtd); |
333 | record.setType("U" ); |
334 | record.setPayload(payload); |
335 | |
336 | QVERIFY(!record.isRecordType<QNdefRecord>()); |
337 | QVERIFY(!record.isRecordType<QNdefNfcTextRecord>()); |
338 | QVERIFY(record.isRecordType<QNdefNfcUriRecord>()); |
339 | |
340 | QNdefNfcUriRecord uriRecord(record); |
341 | |
342 | QVERIFY(!uriRecord.isEmpty()); |
343 | |
344 | QVERIFY(record == uriRecord); |
345 | |
346 | QCOMPARE(uriRecord.uri(), QUrl(url)); |
347 | } |
348 | } |
349 | |
350 | void tst_QNdefRecord::tst_declarative_record_data() |
351 | { |
352 | QTest::addColumn<QNdefRecord::TypeNameFormat>(name: "typeNameFormat" ); |
353 | QTest::addColumn<QByteArray>(name: "type" ); |
354 | |
355 | QTest::newRow(dataTag: "NfcRtd:U" ) << QNdefRecord::NfcRtd << QByteArray("U" ); |
356 | QTest::newRow(dataTag: "NfcRtd:T" ) << QNdefRecord::NfcRtd << QByteArray("T" ); |
357 | QTest::newRow(dataTag: "Empty:BLAH" ) << QNdefRecord::Empty << QByteArray("BLAH" ); |
358 | QTest::newRow(dataTag: "Empty" ) << QNdefRecord::Empty << QByteArray("" ); |
359 | QTest::newRow(dataTag: "Unknown" ) << QNdefRecord::Unknown << QByteArray("BLAHfoo" ); |
360 | QTest::newRow(dataTag: "Mime" ) << QNdefRecord::Mime << QByteArray("foobar" ); |
361 | QTest::newRow(dataTag: "ExternalRtd" ) << QNdefRecord::ExternalRtd << QByteArray("" ); |
362 | } |
363 | |
364 | void tst_QNdefRecord::tst_declarative_record() |
365 | { |
366 | QFETCH(QNdefRecord::TypeNameFormat, typeNameFormat); |
367 | QFETCH(QByteArray, type); |
368 | |
369 | { |
370 | QNdefRecord record; |
371 | record.setTypeNameFormat(typeNameFormat); |
372 | record.setType(type); |
373 | QCOMPARE(record.typeNameFormat(), typeNameFormat); |
374 | QCOMPARE(record.type(), type); |
375 | |
376 | QQmlNdefRecord declRecord(record); |
377 | QCOMPARE(declRecord.record(), record); |
378 | QCOMPARE(declRecord.record().typeNameFormat(), typeNameFormat); |
379 | QCOMPARE(declRecord.record().type(), type); |
380 | QCOMPARE(declRecord.type(), QString(type)); |
381 | QCOMPARE(declRecord.typeNameFormat(), static_cast<QQmlNdefRecord::TypeNameFormat>(typeNameFormat)); |
382 | |
383 | QQmlNdefRecord declRecord2; |
384 | declRecord2.setRecord(record); |
385 | QCOMPARE(declRecord2.record(), record); |
386 | QCOMPARE(declRecord2.record().typeNameFormat(), typeNameFormat); |
387 | QCOMPARE(declRecord2.record().type(), type); |
388 | QCOMPARE(declRecord2.type(), QString(type)); |
389 | QCOMPARE(declRecord2.typeNameFormat(), static_cast<QQmlNdefRecord::TypeNameFormat>(typeNameFormat)); |
390 | |
391 | QQmlNdefRecord declRecord3; |
392 | declRecord3.setTypeNameFormat((QQmlNdefRecord::TypeNameFormat)typeNameFormat); |
393 | declRecord3.setType(type); |
394 | QCOMPARE(declRecord3.type(), QString(type)); |
395 | QCOMPARE(declRecord3.record().typeNameFormat(), typeNameFormat); |
396 | QCOMPARE(declRecord3.record().type(), type); |
397 | QCOMPARE(declRecord3.typeNameFormat(), static_cast<QQmlNdefRecord::TypeNameFormat>(typeNameFormat)); |
398 | } |
399 | } |
400 | |
401 | void tst_QNdefRecord::tst_declarativeChangedSignals() |
402 | { |
403 | QQmlNdefRecord record; |
404 | QSignalSpy typeSpy(&record, SIGNAL(typeChanged())); |
405 | QSignalSpy tnfSpy(&record, SIGNAL(typeNameFormatChanged())); |
406 | QSignalSpy recordSpy(&record, SIGNAL(recordChanged())); |
407 | |
408 | QCOMPARE(typeSpy.count(), 0); |
409 | QCOMPARE(recordSpy.count(), 0); |
410 | |
411 | record.setType("U" ); |
412 | record.setTypeNameFormat(QQmlNdefRecord::NfcRtd); |
413 | QCOMPARE(typeSpy.count(), 1); |
414 | QCOMPARE(tnfSpy.count(), 1); |
415 | QCOMPARE(recordSpy.count(), 0); |
416 | QCOMPARE(record.type(), QString("U" )); |
417 | QCOMPARE(record.record().type(), QByteArray("U" )); |
418 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::NfcRtd); |
419 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::NfcRtd); |
420 | |
421 | record.setType("U" ); //same value, no signal |
422 | QCOMPARE(typeSpy.count(), 1); |
423 | QCOMPARE(tnfSpy.count(), 1); |
424 | QCOMPARE(recordSpy.count(), 0); |
425 | QCOMPARE(record.type(), QString("U" )); |
426 | QCOMPARE(record.record().type(), QByteArray("U" )); |
427 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::NfcRtd); |
428 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::NfcRtd); |
429 | |
430 | record.setType("blah" ); |
431 | record.setType("blah2" ); |
432 | record.setTypeNameFormat(QQmlNdefRecord::ExternalRtd); |
433 | QCOMPARE(typeSpy.count(), 3); |
434 | QCOMPARE(tnfSpy.count(), 2); |
435 | QCOMPARE(recordSpy.count(), 0); |
436 | QCOMPARE(record.type(), QString("blah2" )); |
437 | QCOMPARE(record.record().type(), QByteArray("blah2" )); |
438 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::ExternalRtd); |
439 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::ExternalRtd); |
440 | |
441 | record.setType("Rubbish" ); |
442 | QCOMPARE(typeSpy.count(), 4); |
443 | QCOMPARE(tnfSpy.count(), 2); |
444 | QCOMPARE(recordSpy.count(), 0); |
445 | QCOMPARE(record.type(), QString("Rubbish" )); |
446 | QCOMPARE(record.record().type(), QByteArray("Rubbish" )); |
447 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::ExternalRtd); |
448 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::ExternalRtd); |
449 | |
450 | record.setType("QQQQ" ); |
451 | record.setTypeNameFormat(QQmlNdefRecord::Mime); |
452 | QCOMPARE(typeSpy.count(), 5); |
453 | QCOMPARE(tnfSpy.count(), 3); |
454 | QCOMPARE(recordSpy.count(), 0); |
455 | QCOMPARE(record.type(), QString("QQQQ" )); |
456 | QCOMPARE(record.record().type(), QByteArray("QQQQ" )); |
457 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::Mime); |
458 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::Mime); |
459 | |
460 | record.setRecord(QNdefRecord()); |
461 | QCOMPARE(typeSpy.count(), 5); //setting record -> no recordChanged signal |
462 | QCOMPARE(tnfSpy.count(), 3); |
463 | QCOMPARE(recordSpy.count(), 1); |
464 | QCOMPARE(record.type(), QString("" )); |
465 | QCOMPARE(record.record().type(), QByteArray()); |
466 | QCOMPARE(record.record().typeNameFormat(), QNdefRecord::Empty); |
467 | QCOMPARE(record.typeNameFormat(), QQmlNdefRecord::Empty); |
468 | } |
469 | |
470 | QTEST_MAIN(tst_QNdefRecord) |
471 | |
472 | #include "tst_qndefrecord.moc" |
473 | |
474 | |