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 "ut_qvcard30writer.h" |
|
43 #include "qvcard30writer_p.h" |
|
44 #include "qversitdocument.h" |
|
45 #include "qversitproperty.h" |
|
46 #include <QtTest/QtTest> |
|
47 #include <QByteArray> |
|
48 #include <QVariant> |
|
49 |
|
50 // This says "NOKIA" in Katakana encoded with UTF-8 |
|
51 const QString KATAKANA_NOKIA(QString::fromUtf8("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2")); |
|
52 |
|
53 QTM_USE_NAMESPACE |
|
54 |
|
55 void UT_QVCard30Writer::init() |
|
56 { |
|
57 mWriter = new QVCard30Writer; |
|
58 mWriter->setCodec(QTextCodec::codecForName("UTF-8")); |
|
59 } |
|
60 |
|
61 void UT_QVCard30Writer::cleanup() |
|
62 { |
|
63 delete mWriter; |
|
64 } |
|
65 |
|
66 void UT_QVCard30Writer::testEncodeVersitProperty() |
|
67 { |
|
68 QByteArray encodedProperty; |
|
69 QBuffer buffer(&encodedProperty); |
|
70 mWriter->setDevice(&buffer); |
|
71 buffer.open(QIODevice::WriteOnly); |
|
72 |
|
73 // No parameters |
|
74 QByteArray expectedResult = "FN:John Citizen\r\n"; |
|
75 QVersitProperty property; |
|
76 property.setName(QString::fromAscii("FN")); |
|
77 property.setValue(QString::fromAscii("John Citizen")); |
|
78 mWriter->encodeVersitProperty(property); |
|
79 QCOMPARE(encodedProperty, expectedResult); |
|
80 |
|
81 // With parameter(s) |
|
82 mWriter->writeCrlf(); // so it doesn't start folding |
|
83 buffer.close(); |
|
84 encodedProperty.clear(); |
|
85 buffer.open(QIODevice::WriteOnly); |
|
86 expectedResult = "TEL;TYPE=HOME:123\r\n"; |
|
87 property.setName(QString::fromAscii("TEL")); |
|
88 property.setValue(QString::fromAscii("123")); |
|
89 property.insertParameter(QString::fromAscii("TYPE"),QString::fromAscii("HOME")); |
|
90 mWriter->encodeVersitProperty(property); |
|
91 QCOMPARE(encodedProperty, expectedResult); |
|
92 |
|
93 // Convert X-NICKNAME to NICKNAME |
|
94 mWriter->writeCrlf(); // so it doesn't start folding |
|
95 buffer.close(); |
|
96 encodedProperty.clear(); |
|
97 buffer.open(QIODevice::WriteOnly); |
|
98 expectedResult = "NICKNAME:Jack\r\n"; |
|
99 property.setParameters(QMultiHash<QString,QString>()); |
|
100 property.setName(QString::fromAscii("X-NICKNAME")); |
|
101 property.setValue(QString::fromAscii("Jack")); |
|
102 mWriter->encodeVersitProperty(property); |
|
103 QCOMPARE(encodedProperty, expectedResult); |
|
104 |
|
105 // Convert X-IMPP to IMPP |
|
106 mWriter->writeCrlf(); // so it doesn't start folding |
|
107 buffer.close(); |
|
108 encodedProperty.clear(); |
|
109 buffer.open(QIODevice::WriteOnly); |
|
110 expectedResult = "IMPP:msn:msn-address\r\n"; |
|
111 property.setParameters(QMultiHash<QString,QString>()); |
|
112 property.setName(QString::fromAscii("X-IMPP")); |
|
113 property.setValue(QString::fromAscii("msn:msn-address")); |
|
114 mWriter->encodeVersitProperty(property); |
|
115 QCOMPARE(encodedProperty, expectedResult); |
|
116 |
|
117 // AGENT property |
|
118 mWriter->writeCrlf(); // so it doesn't start folding |
|
119 buffer.close(); |
|
120 encodedProperty.clear(); |
|
121 buffer.open(QIODevice::WriteOnly); |
|
122 expectedResult = "AGENT:BEGIN:VCARD\\nVERSION:3.0\\nFN:Secret Agent\\nEND:VCARD\\n\r\n"; |
|
123 property.setName(QString::fromAscii("AGENT")); |
|
124 property.setValue(QString()); |
|
125 QVersitDocument document; |
|
126 QVersitProperty embeddedProperty; |
|
127 embeddedProperty.setName(QString(QString::fromAscii("FN"))); |
|
128 embeddedProperty.setValue(QString::fromAscii("Secret Agent")); |
|
129 document.addProperty(embeddedProperty); |
|
130 property.setValue(QVariant::fromValue(document)); |
|
131 mWriter->encodeVersitProperty(property); |
|
132 QCOMPARE(encodedProperty, expectedResult); |
|
133 |
|
134 // Value is base64 encoded. |
|
135 mWriter->writeCrlf(); // so it doesn't start folding |
|
136 buffer.close(); |
|
137 encodedProperty.clear(); |
|
138 buffer.open(QIODevice::WriteOnly); |
|
139 QByteArray value("value"); |
|
140 expectedResult = "Springfield.HOUSE.PHOTO;ENCODING=B:" + value.toBase64() + "\r\n"; |
|
141 QStringList groups(QString::fromAscii("Springfield")); |
|
142 groups.append(QString::fromAscii("HOUSE")); |
|
143 property.setGroups(groups); |
|
144 property.setParameters(QMultiHash<QString,QString>()); |
|
145 property.setName(QString::fromAscii("PHOTO")); |
|
146 property.setValue(value); |
|
147 mWriter->encodeVersitProperty(property); |
|
148 QCOMPARE(encodedProperty, expectedResult); |
|
149 |
|
150 // Characters other than ASCII: |
|
151 mWriter->writeCrlf(); // so it doesn't start folding |
|
152 buffer.close(); |
|
153 encodedProperty.clear(); |
|
154 buffer.open(QIODevice::WriteOnly); |
|
155 expectedResult = "ORG:" + KATAKANA_NOKIA.toUtf8() + "\r\n"; |
|
156 property = QVersitProperty(); |
|
157 property.setName(QLatin1String("ORG")); |
|
158 property.setValue(KATAKANA_NOKIA); |
|
159 mWriter->encodeVersitProperty(property); |
|
160 QCOMPARE(encodedProperty, expectedResult); |
|
161 |
|
162 // No CHARSET and QUOTED-PRINTABLE parameters |
|
163 mWriter->writeCrlf(); // so it doesn't start folding |
|
164 buffer.close(); |
|
165 encodedProperty.clear(); |
|
166 buffer.open(QIODevice::WriteOnly); |
|
167 expectedResult = "EMAIL:john@" + KATAKANA_NOKIA.toUtf8() + ".com\r\n"; |
|
168 property = QVersitProperty(); |
|
169 property.setName(QLatin1String("EMAIL")); |
|
170 property.setValue(QString::fromAscii("john@%1.com").arg(KATAKANA_NOKIA)); |
|
171 mWriter->encodeVersitProperty(property); |
|
172 QCOMPARE(encodedProperty, expectedResult); |
|
173 } |
|
174 |
|
175 void UT_QVCard30Writer::testEncodeParameters() |
|
176 { |
|
177 QByteArray encodedParameters; |
|
178 QBuffer buffer(&encodedParameters); |
|
179 mWriter->setDevice(&buffer); |
|
180 buffer.open(QIODevice::WriteOnly); |
|
181 |
|
182 QString typeParameterName(QString::fromAscii("TYPE")); |
|
183 QString encodingParameterName(QString::fromAscii("ENCODING")); |
|
184 |
|
185 // No parameters |
|
186 QMultiHash<QString,QString> parameters; |
|
187 mWriter->encodeParameters(parameters); |
|
188 QCOMPARE(encodedParameters, QByteArray("")); |
|
189 |
|
190 // One TYPE parameter |
|
191 parameters.insert(typeParameterName,QString::fromAscii("HOME")); |
|
192 mWriter->writeCrlf(); // so it doesn't start folding |
|
193 buffer.close(); |
|
194 encodedParameters.clear(); |
|
195 buffer.open(QIODevice::WriteOnly); |
|
196 mWriter->encodeParameters(parameters); |
|
197 QCOMPARE(encodedParameters, QByteArray(";TYPE=HOME")); |
|
198 |
|
199 // Two TYPE parameters |
|
200 parameters.insert(typeParameterName,QString::fromAscii("VOICE")); |
|
201 mWriter->writeCrlf(); // so it doesn't start folding |
|
202 buffer.close(); |
|
203 encodedParameters.clear(); |
|
204 buffer.open(QIODevice::WriteOnly); |
|
205 mWriter->encodeParameters(parameters); |
|
206 QCOMPARE(encodedParameters, QByteArray(";TYPE=VOICE,HOME")); |
|
207 |
|
208 // One ENCODING parameter |
|
209 parameters.clear(); |
|
210 parameters.insert(encodingParameterName,QString::fromAscii("8BIT")); |
|
211 mWriter->writeCrlf(); // so it doesn't start folding |
|
212 buffer.close(); |
|
213 encodedParameters.clear(); |
|
214 buffer.open(QIODevice::WriteOnly); |
|
215 mWriter->encodeParameters(parameters); |
|
216 QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT")); |
|
217 |
|
218 // Two parameters |
|
219 parameters.insert(QString::fromAscii("X-PARAM"),QString::fromAscii("VALUE")); |
|
220 mWriter->writeCrlf(); // so it doesn't start folding |
|
221 buffer.close(); |
|
222 encodedParameters.clear(); |
|
223 buffer.open(QIODevice::WriteOnly); |
|
224 mWriter->encodeParameters(parameters); |
|
225 QCOMPARE(encodedParameters, QByteArray(";X-PARAM=VALUE;ENCODING=8BIT")); |
|
226 |
|
227 // Parameter with characters that require backslash escaping |
|
228 parameters.clear(); |
|
229 parameters.insert(QString::fromAscii("X-P;ARAM"),QString::fromAscii("VA,LUE")); |
|
230 mWriter->writeCrlf(); // so it doesn't start folding |
|
231 buffer.close(); |
|
232 encodedParameters.clear(); |
|
233 buffer.open(QIODevice::WriteOnly); |
|
234 mWriter->encodeParameters(parameters); |
|
235 QCOMPARE(encodedParameters, QByteArray(";X-P\\;ARAM=VA\\,LUE")); |
|
236 } |
|
237 |
|
238 QTEST_MAIN(UT_QVCard30Writer) |
|
239 |
|