1/****************************************************************************
2**
3** Copyright (C) 2016 Research In Motion
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 <qndefmessage.h>
33#include <qndefnfctextrecord.h>
34#include <qndefnfcurirecord.h>
35#include <qndefnfcsmartposterrecord.h>
36
37QT_USE_NAMESPACE
38
39static const quint8 payload[] = {
40 0xC1, 0x02, 0x00, 0x00, 0x03, 0x0D, 0x53, 0x70, 0x91, 0x01, 0x08, 0x55, 0x01, 0x72, 0x69, 0x6D, 0x2E, 0x63, 0x6F, 0x6D,
41 0x11, 0x01, 0x0D, 0x54, 0x02, 0x65, 0x6E, 0x49, 0x63, 0x6F, 0x6E, 0x20, 0x54, 0x69, 0x74, 0x6C, 0x65, 0x11, 0x01, 0x09,
42 0x74, 0x74, 0x65, 0x78, 0x74, 0x2F, 0x68, 0x74, 0x6D, 0x6C, 0x11, 0x01, 0x04, 0x73, 0x00, 0x00, 0x04, 0x00, 0x11, 0x03,
43 0x01, 0x61, 0x63, 0x74, 0x01, 0x42, 0x09, 0x00, 0x00, 0x02, 0xC5, 0x69, 0x6D, 0x61, 0x67, 0x65, 0x2F, 0x70, 0x6E, 0x67,
44 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x1E,
45 0x00, 0x00, 0x00, 0x1E, 0x08, 0x02, 0x00, 0x00, 0x00, 0xB4, 0x52, 0x39, 0xF5, 0x00, 0x00, 0x02, 0x8C, 0x49, 0x44, 0x41,
46 0x54, 0x78, 0x9C, 0x62, 0x60, 0x68, 0x60, 0xA0, 0x11, 0x02, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0x99, 0xB9, 0x0D, 0x0C,
47 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00,
48 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x62, 0x60, 0x60, 0xE8, 0xA6, 0x11, 0x02,
49 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00,
50 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x82, 0x1A, 0x1D, 0x1B, 0xBB, 0x75, 0xE6, 0xCC,
51 0x8B, 0x10, 0x34, 0x7F, 0xFE, 0xE5, 0xF2, 0xF2, 0x83, 0x82, 0x82, 0x93, 0x81, 0xE2, 0xF9, 0xF9, 0x7B, 0x81, 0x22, 0x1D,
52 0x1D, 0x27, 0xE1, 0x1A, 0x3A, 0x3B, 0x4F, 0x02, 0x45, 0x94, 0x94, 0x66, 0x41, 0xB8, 0x69, 0x69, 0x3B, 0xF7, 0xEF, 0x7F,
53 0x74, 0xF7, 0xEE, 0x87, 0xD5, 0xAB, 0x6F, 0x1A, 0x1B, 0x2F, 0x42, 0x36, 0x1A, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x82, 0x1A,
54 0x0D, 0x34, 0xEE, 0x3F, 0x06, 0x00, 0x2A, 0x05, 0x6A, 0x83, 0xB3, 0x21, 0x2A, 0xDF, 0xBF, 0xFF, 0x01, 0xE4, 0xBA, 0xB8,
55 0xAC, 0x02, 0xB2, 0xD7, 0xAF, 0xBF, 0x8D, 0xA9, 0x05, 0x6E, 0x34, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC2, 0x67, 0x34, 0xD0,
56 0x2D, 0x70, 0xA3, 0xE1, 0x0E, 0x87, 0x1B, 0x1D, 0x1A, 0xBA, 0x09, 0xAE, 0xF2, 0xDD, 0xBB, 0xEF, 0x10, 0x06, 0xC4, 0x4A,
57 0x08, 0x02, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x42, 0x31, 0x1A, 0xA8, 0x02, 0xE8, 0x53, 0x7B, 0xFB, 0x15, 0x67, 0xCE, 0xBC,
58 0x80, 0x28, 0x85, 0x33, 0x80, 0xD6, 0xA0, 0x19, 0x0D, 0x91, 0x82, 0x68, 0x01, 0x8A, 0x4B, 0x4B, 0x4F, 0x5F, 0xB5, 0xEA,
59 0x06, 0xB2, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x42, 0x37, 0x1A, 0xC2, 0x05, 0xAA, 0x80, 0x98, 0x78, 0xE5, 0xCA,
60 0x1B, 0xB8, 0xD3, 0x20, 0x46, 0x40, 0x8C, 0x8E, 0x8E, 0xDE, 0x02, 0x11, 0x04, 0xBA, 0x1D, 0x57, 0x34, 0x02, 0x00, 0x00,
61 0x00, 0xFF, 0xFF, 0xC2, 0x6E, 0x34, 0x30, 0xC8, 0x90, 0x8D, 0xBE, 0x7F, 0xFF, 0x03, 0x3C, 0x4C, 0x20, 0x46, 0xD7, 0xD5,
62 0x1D, 0x81, 0x28, 0xC0, 0x93, 0x42, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC2, 0x6E, 0x34, 0xD0, 0x14, 0x88, 0xCE, 0x0B,
63 0x17, 0x5E, 0x01, 0xC9, 0xFE, 0xFE, 0x33, 0xF0, 0x30, 0x81, 0x18, 0xDD, 0xD5, 0x75, 0x0A, 0x59, 0x3D, 0x30, 0x39, 0x41,
64 0x10, 0xB2, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x42, 0x8F, 0x46, 0x60, 0x08, 0x42, 0xDC, 0x08, 0x01, 0x90, 0x68,
65 0xAC, 0xAF, 0x3F, 0xBA, 0x7B, 0xF7, 0x03, 0x48, 0x98, 0x40, 0x8C, 0x9E, 0x3A, 0xF5, 0x3C, 0x72, 0x04, 0xC0, 0xA3, 0x04,
66 0x98, 0x2E, 0xE1, 0x46, 0x03, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC2, 0x97, 0x42, 0x80, 0x6E, 0x87, 0x18, 0x0D, 0x4C, 0xE6,
67 0xC0, 0xF4, 0x0B, 0x61, 0x10, 0x6F, 0x34, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC2, 0x62, 0x34, 0x50, 0x33, 0x50, 0x0F, 0xD0,
68 0x2C, 0xA0, 0x38, 0xDC, 0x68, 0xA0, 0x67, 0x21, 0x7E, 0x42, 0x0E, 0x10, 0x78, 0x58, 0x03, 0x23, 0x13, 0xA2, 0x12, 0xD9,
69 0x68, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xC2, 0x1E, 0xD6, 0x70, 0x04, 0x37, 0x1A, 0xCE, 0x86, 0x80, 0xB4, 0xB4, 0x5D,
70 0xC8, 0xC9, 0x06, 0x88, 0x80, 0x81, 0x86, 0x66, 0x34, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x22, 0xC1, 0x68, 0x60, 0xA6, 0x87,
71 0x1B, 0x0D, 0x4C, 0x9D, 0x90, 0x6C, 0x02, 0x4C, 0xCB, 0xB8, 0x8C, 0x06, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x22, 0xC1, 0x68,
72 0x79, 0xF9, 0x99, 0xC8, 0x46, 0xC3, 0x53, 0x11, 0x30, 0xBB, 0x03, 0x15, 0x9C, 0x3F, 0xFF, 0x12, 0xCD, 0x68, 0x00, 0x00,
73 0x00, 0x00, 0xFF, 0xFF, 0x22, 0xC1, 0x68, 0xE4, 0xE8, 0x02, 0x1A, 0x0D, 0x0C, 0x7D, 0x48, 0xB8, 0x23, 0x03, 0x64, 0xA3,
74 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x22, 0xCD, 0x68, 0x20, 0x03, 0x6E, 0x34, 0x90, 0x0B, 0x2C, 0x12, 0x80, 0x11, 0x0E,
75 0x37, 0x17, 0x18, 0x38, 0xC8, 0x49, 0x1B, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0x42, 0x79, 0x0D, 0xCC, 0xBA, 0x40, 0x84,
76 0x91, 0x5F, 0xBA, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0x61, 0xB5, 0x0B, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1,
77 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA2, 0xA1, 0xD1, 0x00,
78 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xA3, 0xA1, 0xD1, 0x00, 0x27, 0x58, 0x04, 0x5A, 0x28, 0x87, 0x48, 0xD1, 0x00, 0x00, 0x00,
79 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
80};
81
82class tst_QNdefNfcSmartPosterRecord : public QObject
83{
84 Q_OBJECT
85
86public:
87 tst_QNdefNfcSmartPosterRecord();
88 ~tst_QNdefNfcSmartPosterRecord();
89
90private:
91 QNdefNfcTextRecord getTextRecord(const QString& locale);
92 QString getTitle(const QString& locale);
93 void checkLocale(const QNdefNfcSmartPosterRecord& record, const QStringList& localeList);
94
95 QMap<QString, QString> _textRecords;
96
97private slots:
98 void tst_emptyRecord();
99 void tst_titles();
100 void tst_uri();
101 void tst_action();
102 void tst_icon();
103 void tst_size();
104 void tst_typeInfo();
105 void tst_construct();
106 void tst_downcast();
107};
108
109tst_QNdefNfcSmartPosterRecord::tst_QNdefNfcSmartPosterRecord()
110{
111 _textRecords["en"] = "test";
112 _textRecords["fr"] = "essai";
113 _textRecords["es"] = "prueba";
114 _textRecords["de"] = "pruefung";
115 _textRecords["nl"] = "proef";
116 _textRecords["en-US"] = "trial";
117}
118
119tst_QNdefNfcSmartPosterRecord::~tst_QNdefNfcSmartPosterRecord()
120{
121}
122
123QNdefNfcTextRecord tst_QNdefNfcSmartPosterRecord::getTextRecord(const QString& locale)
124{
125 QNdefNfcTextRecord record;
126 record.setLocale(locale);
127 record.setEncoding(QNdefNfcTextRecord::Utf8);
128 record.setText(_textRecords[locale]);
129 return record;
130}
131
132QString tst_QNdefNfcSmartPosterRecord::getTitle(const QString& locale)
133{
134 return _textRecords[locale];
135}
136
137void tst_QNdefNfcSmartPosterRecord::checkLocale(const QNdefNfcSmartPosterRecord& record, const QStringList& localeList)
138{
139 QList<QString> locales = _textRecords.keys();
140
141 for (int i = 0; i < locales.size(); i++) {
142 if (localeList.contains(str: locales[i])) {
143 QVERIFY(record.hasTitle(locales[i]));
144 }
145
146 else {
147 QVERIFY(!record.hasTitle(locales[i]));
148 }
149 }
150
151 if (localeList.empty()) {
152 QVERIFY(record.hasTitle());
153 }
154}
155
156void tst_QNdefNfcSmartPosterRecord::tst_emptyRecord()
157{
158 QNdefNfcSmartPosterRecord record;
159
160 QCOMPARE(record.typeNameFormat(), QNdefRecord::NfcRtd);
161 QCOMPARE(record.type(), QByteArray("Sp"));
162 QVERIFY(record.id().isEmpty());
163 QVERIFY(record.payload().isEmpty());
164
165 QVERIFY(!record.hasTitle());
166 QVERIFY(!record.hasAction());
167 QVERIFY(!record.hasIcon());
168 QVERIFY(!record.hasSize());
169 QVERIFY(!record.hasTypeInfo());
170
171 QCOMPARE(record.titleCount(), 0);
172 QCOMPARE(record.titleRecord(0), QNdefNfcTextRecord());
173 QCOMPARE(record.title(), QString());
174
175 QList<QNdefNfcTextRecord> emptyTitleList;
176 QCOMPARE(record.titleRecords(), emptyTitleList);
177
178 QCOMPARE(record.uri(), QUrl());
179 QCOMPARE(record.uriRecord(), QNdefNfcUriRecord());
180 QCOMPARE(record.action(), QNdefNfcSmartPosterRecord::UnspecifiedAction);
181 QCOMPARE(record.iconCount(), 0);
182 QCOMPARE(record.iconRecord(0), QNdefNfcIconRecord());
183 QCOMPARE(record.icon(), QByteArray());
184
185 QList<QNdefNfcIconRecord> emptyIconList;
186 QCOMPARE(record.iconRecords(), emptyIconList);
187
188 quint32 size = 0;
189 QCOMPARE(record.size(), size);
190 QCOMPARE(record.typeInfo(), QByteArray());
191
192 QVERIFY(record.isRecordType<QNdefNfcSmartPosterRecord>());
193 QVERIFY(!record.isRecordType<QNdefNfcTextRecord>());
194 QVERIFY(!record.isRecordType<QNdefNfcUriRecord>());
195
196 QCOMPARE(record, QNdefNfcSmartPosterRecord());
197 QVERIFY(!(record != QNdefNfcSmartPosterRecord()));
198}
199
200void tst_QNdefNfcSmartPosterRecord::tst_titles()
201{
202 QNdefNfcSmartPosterRecord record;
203
204 // Set "en" locale
205 QNdefNfcTextRecord enRecord = getTextRecord(locale: "en");
206 QVERIFY(record.addTitle(enRecord));
207
208 QCOMPARE(record.titleCount(), 1);
209 {
210 QStringList locales;
211 locales << "en";
212 checkLocale(record, localeList: locales);
213 }
214
215 // Get "en" locale
216 QCOMPARE(record.titleRecord(0), enRecord);
217 QCOMPARE(record.title(), getTitle("en"));
218 QCOMPARE(record.title("fr"), QString());
219
220 QList<QNdefNfcTextRecord> titleRecords = record.titleRecords();
221 QCOMPARE(titleRecords.size(), 1);
222 QCOMPARE(titleRecords[0], enRecord);
223
224 QVERIFY(!record.addTitle(enRecord));
225 QCOMPARE(record.titleCount(), 1);
226
227 // Add "de" locale
228 QNdefNfcTextRecord deRecord = getTextRecord(locale: "de");
229 QVERIFY(record.addTitle(deRecord));
230
231 QCOMPARE(record.titleCount(), 2);
232 {
233 QStringList locales;
234 locales << "en" << "de";
235 checkLocale(record, localeList: locales);
236 }
237
238 // Add "fr" locale
239 QNdefNfcTextRecord frRecord = getTextRecord(locale: "fr");
240 QVERIFY(record.addTitle(frRecord));
241
242 QCOMPARE(record.titleCount(), 3);
243 {
244 QStringList locales;
245 locales << "en" << "de" << "fr";
246 checkLocale(record, localeList: locales);
247 }
248
249 QVERIFY(record.removeTitle(frRecord));
250
251 QCOMPARE(record.titleCount(), 2);
252 {
253 QStringList locales;
254 locales << "en" << "de";
255 checkLocale(record, localeList: locales);
256 }
257
258 QVERIFY(!record.removeTitle(frRecord));
259 QCOMPARE(record.titleCount(), 2);
260
261 QVERIFY(record.removeTitle("en"));
262 QCOMPARE(record.titleCount(), 1);
263 {
264 QStringList locales;
265 locales << "de";
266 checkLocale(record, localeList: locales);
267 }
268
269 titleRecords = record.titleRecords();
270 QCOMPARE(titleRecords.size(), 1);
271 QCOMPARE(titleRecords[0], deRecord);
272
273 QCOMPARE(record.title(), getTitle("de"));
274
275 QVERIFY(record.removeTitle(deRecord));
276 QCOMPARE(record.titleCount(), 0);
277
278 // Test setTitles
279 QNdefNfcTextRecord nlRecord = getTextRecord(locale: "nl");
280 QNdefNfcTextRecord esRecord = getTextRecord(locale: "es");
281
282 QVERIFY(record.addTitle(enRecord));
283 QVERIFY(record.addTitle(deRecord));
284 QVERIFY(record.addTitle(frRecord));
285 QCOMPARE(record.titleCount(), 3);
286
287 QList<QNdefNfcTextRecord> titles;
288 titles << nlRecord << esRecord;
289 record.setTitles(titles);
290
291 QCOMPARE(record.titleCount(), 2);
292 QCOMPARE(record.title("en"), QString());
293 QCOMPARE(record.title("de"), QString());
294 QCOMPARE(record.title("fr"), QString());
295
296 QCOMPARE(record.title("nl"), getTitle("nl"));
297 QCOMPARE(record.title("es"), getTitle("es"));
298}
299
300void tst_QNdefNfcSmartPosterRecord::tst_uri()
301{
302 QNdefNfcSmartPosterRecord record;
303 QString qtString = "http://www.qt-project.org";
304 QString bbString = "http://www.blackberry.com";
305
306 QCOMPARE(record.uri(), QUrl());
307 QCOMPARE(record.uriRecord(), QNdefNfcUriRecord());
308
309 QUrl qtUrl(qtString);
310 record.setUri(qtUrl);
311 QCOMPARE(record.uri(), qtUrl);
312
313 QNdefNfcUriRecord qtRecord;
314 qtRecord.setUri(qtString);
315 QCOMPARE(record.uriRecord(), qtRecord);
316
317 QNdefNfcUriRecord bbRecord;
318 bbRecord.setUri(bbString);
319 record.setUri(bbRecord);
320 QCOMPARE(record.uri(), QUrl(bbString));
321 QCOMPARE(record.uriRecord(), bbRecord);
322}
323
324void tst_QNdefNfcSmartPosterRecord::tst_action()
325{
326 QNdefNfcSmartPosterRecord record;
327
328 record.setAction(QNdefNfcSmartPosterRecord::DoAction);
329 QVERIFY(record.hasAction());
330 QCOMPARE(record.action(), QNdefNfcSmartPosterRecord::DoAction);
331
332 record.setAction(QNdefNfcSmartPosterRecord::SaveAction);
333 QVERIFY(record.hasAction());
334 QCOMPARE(record.action(), QNdefNfcSmartPosterRecord::SaveAction);
335
336 record.setAction(QNdefNfcSmartPosterRecord::UnspecifiedAction);
337 QVERIFY(record.hasAction());
338 QCOMPARE(record.action(), QNdefNfcSmartPosterRecord::UnspecifiedAction);
339}
340
341void tst_QNdefNfcSmartPosterRecord::tst_icon()
342{
343 QNdefNfcSmartPosterRecord record;
344
345 QNdefNfcIconRecord icon;
346 record.addIcon(icon);
347 QVERIFY(record.hasIcon());
348 QCOMPARE(record.iconCount(), 1);
349 QCOMPARE(record.iconRecord(0), icon);
350 QCOMPARE(record.icon(), QByteArray());
351
352 QList<QNdefNfcIconRecord> iconRecords = record.iconRecords();
353 QCOMPARE(iconRecords.size(), 1);
354 QCOMPARE(iconRecords[0], icon);
355
356 QVERIFY(record.removeIcon(QByteArray()));
357 QVERIFY(!record.hasIcon());
358 QCOMPARE(record.iconCount(), 0);
359
360 QByteArray mimeType = "test/data";
361 record.addIcon(type: mimeType, data: "icondata");
362 QVERIFY(record.hasIcon());
363 QVERIFY(record.hasIcon(mimeType));
364 QVERIFY(!record.hasIcon(QByteArray("mime")));
365
366 QCOMPARE(record.iconCount(), 1);
367 QCOMPARE(record.icon(), QByteArray("icondata"));
368 QCOMPARE(record.icon(mimeType), QByteArray("icondata"));
369
370 QNdefNfcIconRecord icon2;
371 icon2.setData("iconrecorddata");
372 icon2.setType("test/icon");
373 record.addIcon(icon: icon2);
374 QVERIFY(record.hasIcon(QByteArray("test/icon")));
375
376 QCOMPARE(record.iconCount(), 2);
377 QCOMPARE(record.icon("test/icon"), QByteArray("iconrecorddata"));
378
379 iconRecords = record.iconRecords();
380 QCOMPARE(iconRecords.size(), 2);
381
382 QNdefNfcIconRecord icon3;
383 icon3.setData("icondata");
384 icon3.setType("test/data");
385
386 QCOMPARE(iconRecords[0], icon3);
387 QCOMPARE(iconRecords[1], icon2);
388
389 QVERIFY(record.removeIcon(mimeType));
390 QCOMPARE(record.iconCount(), 1);
391 iconRecords = record.iconRecords();
392 QCOMPARE(iconRecords.size(), 1);
393 QCOMPARE(iconRecords[0], icon2);
394
395 QList<QNdefNfcIconRecord> iconList;
396
397 QNdefNfcIconRecord testIcon;
398 testIcon.setData("testicondata");
399 testIcon.setType("test/data");
400
401 iconList << testIcon;
402 record.setIcons(iconList);
403
404 QCOMPARE(record.iconCount(), 1);
405 iconRecords = record.iconRecords();
406 QCOMPARE(iconRecords.size(), 1);
407
408 QCOMPARE(iconRecords[0], testIcon);
409}
410
411void tst_QNdefNfcSmartPosterRecord::tst_size()
412{
413 QNdefNfcSmartPosterRecord record;
414
415 quint32 size = 1024;
416 record.setSize(size);
417 QCOMPARE(record.size(), size);
418 QVERIFY(record.hasSize());
419
420 size = 0;
421 record.setSize(size);
422 QCOMPARE(record.size(), size);
423 QVERIFY(record.hasSize());
424}
425
426void tst_QNdefNfcSmartPosterRecord::tst_typeInfo()
427{
428 QNdefNfcSmartPosterRecord record;
429
430 QByteArray typeInfo("typeinfo");
431 record.setTypeInfo(typeInfo);
432 QCOMPARE(record.typeInfo(), typeInfo);
433 QVERIFY(record.hasTypeInfo());
434
435 typeInfo = "moreinfo";
436 record.setTypeInfo(typeInfo);
437 QCOMPARE(record.typeInfo(), typeInfo);
438 QVERIFY(record.hasTypeInfo());
439}
440
441void tst_QNdefNfcSmartPosterRecord::tst_construct()
442{
443 QByteArray data((char *)payload, sizeof(payload));
444 QNdefMessage ndefMessage = QNdefMessage::fromByteArray(message: data);
445 QCOMPARE(ndefMessage.size(), 1);
446 QNdefRecord record = ndefMessage[0];
447
448 QVERIFY(record.isRecordType<QNdefNfcSmartPosterRecord>());
449
450 // Construct a SP record from a standard QNdefRecord
451 QNdefNfcSmartPosterRecord sprecord(record);
452
453 QCOMPARE(sprecord.typeNameFormat(), record.typeNameFormat());
454 QCOMPARE(sprecord.type(), record.type());
455 QCOMPARE(sprecord.payload(), record.payload());
456
457 QVERIFY(sprecord.hasTitle("en"));
458 QCOMPARE(sprecord.title("en"), QString("Icon Title"));
459
460 QCOMPARE(sprecord.titleCount(), 1);
461 QList<QNdefNfcTextRecord> titleRecords = sprecord.titleRecords();
462
463 QCOMPARE(titleRecords[0].text(), QString("Icon Title"));
464 QCOMPARE(titleRecords[0].locale(), QString("en"));
465 QCOMPARE(sprecord.uri(), QUrl("http://www.rim.com"));
466
467 QVERIFY(sprecord.hasAction());
468 QCOMPARE(sprecord.action(), QNdefNfcSmartPosterRecord::SaveAction);
469
470 QByteArray mimeType = "image/png";
471 QVERIFY(sprecord.hasIcon());
472 QVERIFY(sprecord.hasIcon(mimeType));
473 QCOMPARE(sprecord.iconCount(), 1);
474
475 QList<QNdefNfcIconRecord> iconRecords = sprecord.iconRecords();
476 QCOMPARE(iconRecords.size(), 1);
477 QCOMPARE(iconRecords[0].type(), mimeType);
478 QVERIFY(!iconRecords[0].data().isEmpty());
479 QVERIFY(iconRecords.value(1).type().isEmpty());
480 QVERIFY(iconRecords.value(1).data().isEmpty());
481
482 QVERIFY(sprecord.hasSize());
483 quint32 size = 1024;
484 QCOMPARE(sprecord.size(), size);
485
486 QVERIFY(sprecord.hasTypeInfo());
487 QCOMPARE(sprecord.typeInfo(), QByteArray("text/html"));
488
489 QVERIFY(record == sprecord);
490 QVERIFY(!(record != sprecord));
491}
492
493void tst_QNdefNfcSmartPosterRecord::tst_downcast()
494{
495 QByteArray data((char *)payload, sizeof(payload));
496 QNdefMessage ndefMessage = QNdefMessage::fromByteArray(message: data);
497 QCOMPARE(ndefMessage.size(), 1);
498 QNdefRecord record = ndefMessage[0];
499
500 QVERIFY(record.isRecordType<QNdefNfcSmartPosterRecord>());
501
502 // Trying to downcast a QNdefRecord to a QNdefNfcSmartPosterRecord causes
503 // an exception because a QNdefRecord does not contain the shared private data.
504 // So first need to create a real QNdefNfcSmartPosterRecord and refer to it via its base
505 QNdefNfcSmartPosterRecord sprecord(record);
506 QNdefRecord *pRecord = (QNdefRecord *)&sprecord;
507
508 QVERIFY(pRecord->isRecordType<QNdefNfcSmartPosterRecord>());
509
510 QCOMPARE(pRecord->typeNameFormat(), sprecord.typeNameFormat());
511 QCOMPARE(pRecord->type(), sprecord.type());
512 QCOMPARE(pRecord->payload(), sprecord.payload());
513
514 QCOMPARE(pRecord->typeNameFormat(), record.typeNameFormat());
515 QCOMPARE(pRecord->type(), record.type());
516 QCOMPARE(pRecord->payload(), record.payload());
517
518 // Add another title
519 QNdefNfcTextRecord esRecord = getTextRecord(locale: "es");
520 QVERIFY(sprecord.addTitle(esRecord));
521
522 QCOMPARE(sprecord.titleCount(), 2);
523 {
524 QStringList locales;
525 locales << "en" << "es";
526 checkLocale(record: sprecord, localeList: locales);
527 }
528
529 // Now make sure the base and SP record's payloads are the same
530 QByteArray basePayload = pRecord->payload();
531 QByteArray spPayload = sprecord.payload();
532
533 // Check length is longer on the base
534 QVERIFY(basePayload.length() > record.payload().length());
535
536 // Check the payloads are the same
537 QCOMPARE(basePayload, spPayload);
538}
539
540QTEST_MAIN(tst_QNdefNfcSmartPosterRecord)
541
542#include "tst_qndefnfcsmartposterrecord.moc"
543

source code of qtconnectivity/tests/auto/qndefnfcsmartposterrecord/tst_qndefnfcsmartposterrecord.cpp