|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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_qvcard30writer.h" |
|
43 #ifdef QT_BUILD_INTERNAL |
|
44 #include "qvcard30writer_p.h" |
|
45 #endif |
|
46 #include "qversitdocument.h" |
|
47 #include "qversitproperty.h" |
|
48 #include <QtTest/QtTest> |
|
49 #include <QByteArray> |
|
50 #include <QVariant> |
|
51 |
|
52 // This says "NOKIA" in Katakana encoded with UTF-8 |
|
53 const QString KATAKANA_NOKIA(QString::fromUtf8("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2")); |
|
54 |
|
55 QTM_USE_NAMESPACE |
|
56 |
|
57 Q_DECLARE_METATYPE(QVersitProperty) |
|
58 #ifdef QT_BUILD_INTERNAL |
|
59 void tst_QVCard30Writer::init() |
|
60 { |
|
61 mWriter = new QVCard30Writer; |
|
62 mWriter->setCodec(QTextCodec::codecForName("UTF-8")); |
|
63 } |
|
64 |
|
65 void tst_QVCard30Writer::cleanup() |
|
66 { |
|
67 delete mWriter; |
|
68 } |
|
69 |
|
70 void tst_QVCard30Writer::testEncodeVersitProperty() |
|
71 { |
|
72 QFETCH(QVersitProperty, property); |
|
73 QFETCH(QByteArray, expectedResult); |
|
74 QByteArray encodedProperty; |
|
75 QBuffer buffer(&encodedProperty); |
|
76 mWriter->setDevice(&buffer); |
|
77 buffer.open(QIODevice::WriteOnly); |
|
78 mWriter->encodeVersitProperty(property); |
|
79 QCOMPARE(encodedProperty, expectedResult); |
|
80 } |
|
81 |
|
82 |
|
83 void tst_QVCard30Writer::testEncodeVersitProperty_data() |
|
84 { |
|
85 QTest::addColumn<QVersitProperty>("property"); |
|
86 QTest::addColumn<QByteArray>("expectedResult"); |
|
87 |
|
88 QVersitProperty property; |
|
89 QByteArray expectedResult; |
|
90 |
|
91 // No parameters |
|
92 expectedResult = "FN:John Citizen\r\n"; |
|
93 property.setName(QString::fromAscii("FN")); |
|
94 property.setValue(QString::fromAscii("John Citizen")); |
|
95 QTest::newRow("No parameters") << property << expectedResult; |
|
96 |
|
97 // With parameter(s) |
|
98 expectedResult = "TEL;TYPE=HOME:123\r\n"; |
|
99 property.setName(QString::fromAscii("TEL")); |
|
100 property.setValue(QString::fromAscii("123")); |
|
101 property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME")); |
|
102 QTest::newRow("With parameters, plain value") << property << expectedResult; |
|
103 |
|
104 // normal FN property is backslash escaped |
|
105 property.clear(); |
|
106 property.setName(QLatin1String("FN")); |
|
107 property.setValue(QLatin1String(";,:\\")); |
|
108 // semicolons, commas and backslashes are escaped (not colons, as per RFC2426) |
|
109 expectedResult = "FN:\\;\\,:\\\\\r\n"; |
|
110 QTest::newRow("FN property") << property << expectedResult; |
|
111 |
|
112 // Structured N |
|
113 property.setName(QLatin1String("N")); |
|
114 property.setValue(QStringList() |
|
115 << QLatin1String("La;st") // needs to be backslash escaped |
|
116 << QLatin1String("Fi,rst") |
|
117 << QLatin1String("Mi:ddle") |
|
118 << QLatin1String("Pr\\efix") // needs to be QP encoded |
|
119 << QLatin1String("Suffix")); |
|
120 property.setValueType(QVersitProperty::CompoundType); |
|
121 expectedResult = "N:La\\;st;Fi\\,rst;Mi:ddle;Pr\\\\efix;Suffix\r\n"; |
|
122 QTest::newRow("N property") << property << expectedResult; |
|
123 |
|
124 // Structured CATEGORIES |
|
125 property.setName(QLatin1String("CATEGORIES")); |
|
126 property.setValue(QStringList() |
|
127 << QLatin1String("re;d") |
|
128 << QLatin1String("gr,een") |
|
129 << QLatin1String("bl:ue") |
|
130 << QLatin1String("ye\\llow")); |
|
131 property.setValueType(QVersitProperty::ListType); |
|
132 expectedResult = "CATEGORIES:re\\;d,gr\\,een,bl:ue,ye\\\\llow\r\n"; |
|
133 QTest::newRow("CATEGORIES property") << property << expectedResult; |
|
134 |
|
135 // Convert X-NICKNAME to NICKNAME |
|
136 expectedResult = "NICKNAME:Jack\r\n"; |
|
137 property.setParameters(QMultiHash<QString,QString>()); |
|
138 property.setName(QString::fromAscii("X-NICKNAME")); |
|
139 property.setValue(QString::fromAscii("Jack")); |
|
140 QTest::newRow("NICKNAME property") << property << expectedResult; |
|
141 |
|
142 // Convert X-IMPP to IMPP; |
|
143 expectedResult = "IMPP:msn:msn-address\r\n"; |
|
144 property.setParameters(QMultiHash<QString,QString>()); |
|
145 property.setName(QString::fromAscii("X-IMPP")); |
|
146 property.setValue(QString::fromAscii("msn:msn-address")); |
|
147 QTest::newRow("IMPP property") << property << expectedResult; |
|
148 |
|
149 // AGENT property |
|
150 expectedResult = "AGENT:BEGIN:VCARD\\nVERSION:3.0\\nFN:Secret Agent\\nEND:VCARD\\n\r\n"; |
|
151 property.setName(QString::fromAscii("AGENT")); |
|
152 property.setValue(QString()); |
|
153 QVersitDocument document; |
|
154 QVersitProperty embeddedProperty; |
|
155 embeddedProperty.setName(QString(QString::fromAscii("FN"))); |
|
156 embeddedProperty.setValue(QString::fromAscii("Secret Agent")); |
|
157 document.addProperty(embeddedProperty); |
|
158 property.setValue(QVariant::fromValue(document)); |
|
159 QTest::newRow("AGENT property") << property << expectedResult; |
|
160 |
|
161 // Value is base64 encoded. |
|
162 QByteArray value("value"); |
|
163 expectedResult = "Springfield.HOUSE.PHOTO;ENCODING=b:" + value.toBase64() + "\r\n"; |
|
164 QStringList groups(QString::fromAscii("Springfield")); |
|
165 groups.append(QString::fromAscii("HOUSE")); |
|
166 property.setGroups(groups); |
|
167 property.setParameters(QMultiHash<QString,QString>()); |
|
168 property.setName(QString::fromAscii("PHOTO")); |
|
169 property.setValue(value); |
|
170 QTest::newRow("base64 encoded") << property << expectedResult; |
|
171 |
|
172 // Characters other than ASCII: |
|
173 expectedResult = "ORG:" + KATAKANA_NOKIA.toUtf8() + "\r\n"; |
|
174 property = QVersitProperty(); |
|
175 property.setName(QLatin1String("ORG")); |
|
176 property.setValue(KATAKANA_NOKIA); |
|
177 QTest::newRow("non-ASCII") << property << expectedResult; |
|
178 |
|
179 // No CHARSET and QUOTED-PRINTABLE parameters |
|
180 expectedResult = "EMAIL:john@" + KATAKANA_NOKIA.toUtf8() + ".com\r\n"; |
|
181 property = QVersitProperty(); |
|
182 property.setName(QLatin1String("EMAIL")); |
|
183 property.setValue(QString::fromAscii("john@%1.com").arg(KATAKANA_NOKIA)); |
|
184 QTest::newRow("special chars") << property << expectedResult; |
|
185 } |
|
186 |
|
187 void tst_QVCard30Writer::testEncodeParameters() |
|
188 { |
|
189 QByteArray encodedParameters; |
|
190 QBuffer buffer(&encodedParameters); |
|
191 mWriter->setDevice(&buffer); |
|
192 buffer.open(QIODevice::WriteOnly); |
|
193 |
|
194 QString typeParameterName(QString::fromAscii("TYPE")); |
|
195 QString encodingParameterName(QString::fromAscii("ENCODING")); |
|
196 |
|
197 // No parameters |
|
198 QMultiHash<QString,QString> parameters; |
|
199 mWriter->encodeParameters(parameters); |
|
200 QCOMPARE(encodedParameters, QByteArray("")); |
|
201 |
|
202 // One TYPE parameter |
|
203 parameters.insert(typeParameterName,QString::fromAscii("HOME")); |
|
204 mWriter->writeCrlf(); // so it doesn't start folding |
|
205 buffer.close(); |
|
206 encodedParameters.clear(); |
|
207 buffer.open(QIODevice::WriteOnly); |
|
208 mWriter->encodeParameters(parameters); |
|
209 QCOMPARE(encodedParameters, QByteArray(";TYPE=HOME")); |
|
210 |
|
211 // Two TYPE parameters |
|
212 parameters.insert(typeParameterName,QString::fromAscii("VOICE")); |
|
213 mWriter->writeCrlf(); // so it doesn't start folding |
|
214 buffer.close(); |
|
215 encodedParameters.clear(); |
|
216 buffer.open(QIODevice::WriteOnly); |
|
217 mWriter->encodeParameters(parameters); |
|
218 QCOMPARE(encodedParameters, QByteArray(";TYPE=VOICE,HOME")); |
|
219 |
|
220 // One ENCODING parameter |
|
221 parameters.clear(); |
|
222 parameters.insert(encodingParameterName,QString::fromAscii("8BIT")); |
|
223 mWriter->writeCrlf(); // so it doesn't start folding |
|
224 buffer.close(); |
|
225 encodedParameters.clear(); |
|
226 buffer.open(QIODevice::WriteOnly); |
|
227 mWriter->encodeParameters(parameters); |
|
228 QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT")); |
|
229 |
|
230 // Two parameters |
|
231 parameters.insert(QString::fromAscii("X-PARAM"),QString::fromAscii("VALUE")); |
|
232 mWriter->writeCrlf(); // so it doesn't start folding |
|
233 buffer.close(); |
|
234 encodedParameters.clear(); |
|
235 buffer.open(QIODevice::WriteOnly); |
|
236 mWriter->encodeParameters(parameters); |
|
237 QCOMPARE(encodedParameters, QByteArray(";X-PARAM=VALUE;ENCODING=8BIT")); |
|
238 |
|
239 // Parameter with characters that require backslash escaping |
|
240 parameters.clear(); |
|
241 parameters.insert(QString::fromAscii("X-P;ARAM"),QString::fromAscii("VA,LUE")); |
|
242 mWriter->writeCrlf(); // so it doesn't start folding |
|
243 buffer.close(); |
|
244 encodedParameters.clear(); |
|
245 buffer.open(QIODevice::WriteOnly); |
|
246 mWriter->encodeParameters(parameters); |
|
247 QCOMPARE(encodedParameters, QByteArray(";X-P\\;ARAM=VA\\,LUE")); |
|
248 } |
|
249 |
|
250 void tst_QVCard30Writer::testBackSlashEscape() |
|
251 { |
|
252 // Empty string |
|
253 QString input; |
|
254 QVCard30Writer::backSlashEscape(input); |
|
255 QCOMPARE(input,QString()); |
|
256 |
|
257 // Nothing to escape in the string |
|
258 input = QString::fromAscii("Nothing to escape"); |
|
259 QVCard30Writer::backSlashEscape(input); |
|
260 QCOMPARE(input,QString::fromAscii("Nothing to escape")); |
|
261 |
|
262 // Line break in the beginning |
|
263 input = QString::fromAscii("\r\n input"); |
|
264 QVCard30Writer::backSlashEscape(input); |
|
265 QCOMPARE(input,QString::fromAscii("\\n input")); |
|
266 |
|
267 // Line break in the end |
|
268 input = QString::fromAscii("input\r\n"); |
|
269 QVCard30Writer::backSlashEscape(input); |
|
270 QCOMPARE(input,QString::fromAscii("input\\n")); |
|
271 |
|
272 // Semicolon in the beginning |
|
273 input = QString::fromAscii(";input"); |
|
274 QVCard30Writer::backSlashEscape(input); |
|
275 QCOMPARE(input,QString::fromAscii("\\;input")); |
|
276 |
|
277 // Semicolon in the end |
|
278 input = QString::fromAscii("input;"); |
|
279 QVCard30Writer::backSlashEscape(input); |
|
280 QCOMPARE(input,QString::fromAscii("input\\;")); |
|
281 |
|
282 // Comma in the beginning |
|
283 input = QString::fromAscii(",input"); |
|
284 QVCard30Writer::backSlashEscape(input); |
|
285 QCOMPARE(input,QString::fromAscii("\\,input")); |
|
286 |
|
287 // Comma in the end |
|
288 input = QString::fromAscii("input,"); |
|
289 QVCard30Writer::backSlashEscape(input); |
|
290 QCOMPARE(input,QString::fromAscii("input\\,")); |
|
291 |
|
292 // Backslash in the beginning |
|
293 input = QString::fromAscii("\\input"); |
|
294 QVCard30Writer::backSlashEscape(input); |
|
295 QCOMPARE(input,QString::fromAscii("\\\\input")); |
|
296 |
|
297 // Backslash in the end |
|
298 input = QString::fromAscii("input\\"); |
|
299 QVCard30Writer::backSlashEscape(input); |
|
300 QCOMPARE(input,QString::fromAscii("input\\\\")); |
|
301 |
|
302 // Line break, semicolon, backslash and comma in the middle of the string |
|
303 input = QString::fromAscii("Escape these \r\n ; , \\ "); |
|
304 QVCard30Writer::backSlashEscape(input); |
|
305 QCOMPARE(input, QString::fromAscii("Escape these \\n \\; \\, \\\\ ")); |
|
306 } |
|
307 #endif |
|
308 QTEST_MAIN(tst_QVCard30Writer) |
|
309 |