|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "tst_qvcard21writer.h" |
|
43 #include "qvcard21writer_p.h" |
|
44 #include "qversitproperty.h" |
|
45 #include "qversitdocument.h" |
|
46 #include <QtTest/QtTest> |
|
47 #include <QByteArray> |
|
48 #include <QVariant> |
|
49 |
|
50 // This says "NOKIA" in Katakana |
|
51 const QString KATAKANA_NOKIA(QString::fromUtf8("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2")); |
|
52 |
|
53 QTM_USE_NAMESPACE |
|
54 |
|
55 Q_DECLARE_METATYPE(QVersitProperty) |
|
56 |
|
57 void tst_QVCard21Writer::init() |
|
58 { |
|
59 mWriter = new QVCard21Writer; |
|
60 mWriter->setCodec(QTextCodec::codecForName("ISO_8859-1")); |
|
61 } |
|
62 |
|
63 void tst_QVCard21Writer::cleanup() |
|
64 { |
|
65 delete mWriter; |
|
66 } |
|
67 |
|
68 void tst_QVCard21Writer::testEncodeVersitProperty() |
|
69 { |
|
70 QFETCH(QVersitProperty, property); |
|
71 QFETCH(QByteArray, expectedResult); |
|
72 QFETCH(QByteArray, codec); |
|
73 QTextCodec* textCodec = QTextCodec::codecForName(codec); |
|
74 QByteArray encodedProperty; |
|
75 QBuffer buffer(&encodedProperty); |
|
76 mWriter->setDevice(&buffer); |
|
77 mWriter->setCodec(textCodec); |
|
78 buffer.open(QIODevice::WriteOnly); |
|
79 |
|
80 mWriter->encodeVersitProperty(property); |
|
81 QCOMPARE(encodedProperty, expectedResult); |
|
82 } |
|
83 |
|
84 void tst_QVCard21Writer::testEncodeVersitProperty_data() |
|
85 { |
|
86 QTest::addColumn<QVersitProperty>("property"); |
|
87 QTest::addColumn<QByteArray>("expectedResult"); |
|
88 QTest::addColumn<QByteArray>("codec"); |
|
89 |
|
90 QVersitProperty property; |
|
91 QByteArray expectedResult; |
|
92 QByteArray codec("ISO-8859_1"); |
|
93 |
|
94 // normal case |
|
95 property.setName(QString::fromAscii("FN")); |
|
96 property.setValue(QString::fromAscii("John Citizen")); |
|
97 property.setValueType(QVersitProperty::PlainType); |
|
98 expectedResult = "FN:John Citizen\r\n"; |
|
99 QTest::newRow("No parameters") << property << expectedResult << codec; |
|
100 |
|
101 // Structured N - escaping should happen for semicolons, not for commas |
|
102 property.setName(QLatin1String("N")); |
|
103 property.setValue(QStringList() |
|
104 << QLatin1String("La;st") // needs to be backslash escaped |
|
105 << QLatin1String("Fi,rst") |
|
106 << QLatin1String("Mi:ddle") |
|
107 << QLatin1String("Pr\\efix") // needs to be QP encoded |
|
108 << QLatin1String("Suffix")); |
|
109 property.setValueType(QVersitProperty::CompoundType); |
|
110 expectedResult = "N;ENCODING=QUOTED-PRINTABLE:La\\;st;Fi,rst;Mi:ddle;Pr=5Cefix;Suffix\r\n"; |
|
111 QTest::newRow("N property") << property << expectedResult << codec; |
|
112 |
|
113 // Structured CATEGORIES - escaping should happen for commas, not semicolons |
|
114 property.setName(QLatin1String("CATEGORIES")); |
|
115 property.setValue(QStringList() |
|
116 << QLatin1String("re;d") |
|
117 << QLatin1String("gr,een") |
|
118 << QLatin1String("bl:ue")); |
|
119 property.setValueType(QVersitProperty::ListType); |
|
120 expectedResult = "CATEGORIES:re;d,gr\\,een,bl:ue\r\n"; |
|
121 QTest::newRow("CATEGORIES property") << property << expectedResult << codec; |
|
122 |
|
123 // With parameter(s). No special characters in the value. |
|
124 // -> No need to Quoted-Printable encode the value. |
|
125 expectedResult = "TEL;HOME:123\r\n"; |
|
126 property.setName(QString::fromAscii("TEL")); |
|
127 property.setValue(QString::fromAscii("123")); |
|
128 property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME")); |
|
129 QTest::newRow("With parameters, plain value") << property << expectedResult << codec; |
|
130 |
|
131 expectedResult = "EMAIL;HOME;ENCODING=QUOTED-PRINTABLE:john.citizen=40example.com\r\n"; |
|
132 property.setName(QString::fromAscii("EMAIL")); |
|
133 property.setValue(QString::fromAscii("john.citizen@example.com")); |
|
134 QTest::newRow("With parameters, special value") << property << expectedResult << codec; |
|
135 |
|
136 // AGENT property with parameter |
|
137 expectedResult = |
|
138 "AGENT;X-PARAMETER=VALUE:\r\n\ |
|
139 BEGIN:VCARD\r\n\ |
|
140 VERSION:2.1\r\n\ |
|
141 FN:Secret Agent\r\n\ |
|
142 END:VCARD\r\n\ |
|
143 \r\n"; |
|
144 property.setParameters(QMultiHash<QString,QString>()); |
|
145 property.setName(QString::fromAscii("AGENT")); |
|
146 property.setValue(QString()); |
|
147 property.insertParameter(QString::fromAscii("X-PARAMETER"),QString::fromAscii("VALUE")); |
|
148 QVersitDocument document; |
|
149 QVersitProperty embeddedProperty; |
|
150 embeddedProperty.setName(QString(QString::fromAscii("FN"))); |
|
151 embeddedProperty.setValue(QString::fromAscii("Secret Agent")); |
|
152 document.addProperty(embeddedProperty); |
|
153 property.setValue(QVariant::fromValue(document)); |
|
154 QTest::newRow("AGENT property") << property << expectedResult << codec; |
|
155 |
|
156 // Value is base64 encoded. |
|
157 // Check that the extra folding and the line break are added |
|
158 QByteArray value("value"); |
|
159 expectedResult = "Springfield.HOUSE.PHOTO;ENCODING=BASE64:\r\n " + value.toBase64() + "\r\n\r\n"; |
|
160 QStringList groups(QString::fromAscii("Springfield")); |
|
161 groups.append(QString::fromAscii("HOUSE")); |
|
162 property.setGroups(groups); |
|
163 property.setParameters(QMultiHash<QString,QString>()); |
|
164 property.setName(QString::fromAscii("PHOTO")); |
|
165 property.setValue(value); |
|
166 QTest::newRow("base64 encoded") << property << expectedResult << codec; |
|
167 |
|
168 // Characters other than ASCII: |
|
169 expectedResult = "ORG;CHARSET=UTF-8:" + KATAKANA_NOKIA.toUtf8() + "\r\n"; |
|
170 property = QVersitProperty(); |
|
171 property.setName(QLatin1String("ORG")); |
|
172 property.setValue(KATAKANA_NOKIA); |
|
173 QTest::newRow("non-ASCII") << property << expectedResult << codec; |
|
174 |
|
175 // In Shift-JIS codec. |
|
176 QTextCodec* jisCodec = QTextCodec::codecForName("Shift-JIS"); |
|
177 expectedResult = jisCodec->fromUnicode( |
|
178 QLatin1String("ORG:") + KATAKANA_NOKIA + QLatin1String("\r\n")); |
|
179 property = QVersitProperty(); |
|
180 property.setName(QLatin1String("ORG")); |
|
181 property.setValue(KATAKANA_NOKIA); |
|
182 QTest::newRow("JIS codec") << property << expectedResult << QByteArray("Shift-JIS"); |
|
183 |
|
184 // CHARSET and QUOTED-PRINTABLE |
|
185 expectedResult = "EMAIL;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:john=40" |
|
186 + KATAKANA_NOKIA.toUtf8() + ".com\r\n"; |
|
187 property = QVersitProperty(); |
|
188 property.setName(QLatin1String("EMAIL")); |
|
189 property.setValue(QString::fromAscii("john@%1.com").arg(KATAKANA_NOKIA)); |
|
190 QTest::newRow("Charset and QP") << property << expectedResult << codec; |
|
191 } |
|
192 |
|
193 void tst_QVCard21Writer::testEncodeParameters() |
|
194 { |
|
195 QByteArray encodedParameters; |
|
196 QBuffer buffer(&encodedParameters); |
|
197 mWriter->setDevice(&buffer); |
|
198 buffer.open(QIODevice::WriteOnly); |
|
199 |
|
200 QString typeParameterName(QString::fromAscii("TYPE")); |
|
201 QString encodingParameterName(QString::fromAscii("ENCODING")); |
|
202 |
|
203 // No parameters |
|
204 QMultiHash<QString,QString> parameters; |
|
205 mWriter->encodeParameters(parameters); |
|
206 QCOMPARE(encodedParameters, QByteArray("")); |
|
207 |
|
208 // One TYPE parameter |
|
209 mWriter->writeCrlf(); // so it doesn't start folding |
|
210 buffer.close(); |
|
211 encodedParameters.clear(); |
|
212 buffer.open(QIODevice::WriteOnly); |
|
213 parameters.insert(typeParameterName,QString::fromAscii("HOME")); |
|
214 mWriter->encodeParameters(parameters); |
|
215 QCOMPARE(encodedParameters, QByteArray(";HOME")); |
|
216 |
|
217 // Two TYPE parameters |
|
218 mWriter->writeCrlf(); // so it doesn't start folding |
|
219 buffer.close(); |
|
220 encodedParameters.clear(); |
|
221 buffer.open(QIODevice::WriteOnly); |
|
222 parameters.insert(typeParameterName,QString::fromAscii("VOICE")); |
|
223 mWriter->encodeParameters(parameters); |
|
224 QCOMPARE(encodedParameters, QByteArray(";VOICE;HOME")); |
|
225 |
|
226 // One ENCODING parameter |
|
227 mWriter->writeCrlf(); // so it doesn't start folding |
|
228 buffer.close(); |
|
229 encodedParameters.clear(); |
|
230 buffer.open(QIODevice::WriteOnly); |
|
231 parameters.clear(); |
|
232 parameters.insert(encodingParameterName,QString::fromAscii("8BIT")); |
|
233 mWriter->encodeParameters(parameters); |
|
234 QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT")); |
|
235 |
|
236 // Two parameters |
|
237 mWriter->writeCrlf(); // so it doesn't start folding |
|
238 buffer.close(); |
|
239 encodedParameters.clear(); |
|
240 buffer.open(QIODevice::WriteOnly); |
|
241 parameters.insert(QString::fromAscii("X-PARAM"),QString::fromAscii("VALUE")); |
|
242 mWriter->encodeParameters(parameters); |
|
243 QCOMPARE(encodedParameters, QByteArray(";X-PARAM=VALUE;ENCODING=8BIT")); |
|
244 } |
|
245 |
|
246 void tst_QVCard21Writer::testEncodeGroupsAndName() |
|
247 { |
|
248 QVersitProperty property; |
|
249 QByteArray result; |
|
250 QBuffer buffer(&result); |
|
251 mWriter->setDevice(&buffer); |
|
252 buffer.open(QIODevice::WriteOnly); |
|
253 |
|
254 // No groups |
|
255 |
|
256 property.setName(QString::fromAscii("name")); |
|
257 QByteArray expected("NAME"); |
|
258 mWriter->encodeGroupsAndName(property); |
|
259 QCOMPARE(result, expected); |
|
260 |
|
261 // One group |
|
262 mWriter->writeCrlf(); // so it doesn't start folding |
|
263 buffer.close(); |
|
264 result.clear(); |
|
265 buffer.open(QIODevice::WriteOnly); |
|
266 property.setGroups(QStringList(QString::fromAscii("group"))); |
|
267 expected = "group.NAME"; |
|
268 mWriter->encodeGroupsAndName(property); |
|
269 QCOMPARE(result, expected); |
|
270 |
|
271 // Two groups |
|
272 mWriter->writeCrlf(); // so it doesn't start folding |
|
273 buffer.close(); |
|
274 result.clear(); |
|
275 buffer.open(QIODevice::WriteOnly); |
|
276 QStringList groups(QString::fromAscii("group1")); |
|
277 groups.append(QString::fromAscii("group2")); |
|
278 property.setGroups(groups); |
|
279 expected = "group1.group2.NAME"; |
|
280 mWriter->encodeGroupsAndName(property); |
|
281 QCOMPARE(result, expected); |
|
282 } |
|
283 |
|
284 |
|
285 void tst_QVCard21Writer::testQuotedPrintableEncode() |
|
286 { |
|
287 QByteArray encodedBytes; |
|
288 |
|
289 // Nothing to encode |
|
290 QString nothingToEncode(QLatin1String("nothing to encode")); |
|
291 QVERIFY(!mWriter->quotedPrintableEncode(nothingToEncode)); |
|
292 |
|
293 // Special characters |
|
294 QString inputOutput(QLatin1String("\n")); |
|
295 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
296 QCOMPARE(inputOutput, QLatin1String("=0A")); |
|
297 inputOutput = QLatin1String("\r"); |
|
298 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
299 QCOMPARE(inputOutput, QLatin1String("=0D")); |
|
300 inputOutput = QLatin1String("!"); |
|
301 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
302 QCOMPARE(inputOutput, QLatin1String("=21")); |
|
303 inputOutput = QLatin1String("\""); |
|
304 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
305 QCOMPARE(inputOutput, QLatin1String("=22")); |
|
306 inputOutput = QLatin1String("#"); |
|
307 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
308 QCOMPARE(inputOutput, QLatin1String("=23")); |
|
309 inputOutput = QLatin1String("$"); |
|
310 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
311 QCOMPARE(inputOutput, QLatin1String("=24")); |
|
312 inputOutput = QLatin1String("="); |
|
313 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
314 QCOMPARE(inputOutput, QLatin1String("=3D")); |
|
315 inputOutput = QLatin1String("@"); |
|
316 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
317 QCOMPARE(inputOutput, QLatin1String("=40")); |
|
318 inputOutput = QLatin1String("["); |
|
319 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
320 QCOMPARE(inputOutput, QLatin1String("=5B")); |
|
321 inputOutput = QLatin1String("\\"); |
|
322 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
323 QCOMPARE(inputOutput, QLatin1String("=5C")); |
|
324 inputOutput = QLatin1String("]"); |
|
325 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
326 QCOMPARE(inputOutput, QLatin1String("=5D")); |
|
327 inputOutput = QLatin1String("^"); |
|
328 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
329 QCOMPARE(inputOutput, QLatin1String("=5E")); |
|
330 inputOutput = QLatin1String("`"); |
|
331 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
332 QCOMPARE(inputOutput, QLatin1String("=60")); |
|
333 inputOutput = QLatin1String("{"); |
|
334 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
335 QCOMPARE(inputOutput, QLatin1String("=7B")); |
|
336 inputOutput = QLatin1String("|"); |
|
337 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
338 QCOMPARE(inputOutput, QLatin1String("=7C")); |
|
339 inputOutput = QLatin1String("}"); |
|
340 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
341 QCOMPARE(inputOutput, QLatin1String("=7D")); |
|
342 inputOutput = QLatin1String("~"); |
|
343 QVERIFY(mWriter->quotedPrintableEncode(inputOutput)); |
|
344 QCOMPARE(inputOutput, QLatin1String("=7E")); |
|
345 } |
|
346 |
|
347 QTEST_MAIN(tst_QVCard21Writer) |
|
348 |