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_qversitwriter.h" |
|
43 #include "qversitwriter.h" |
|
44 #include "qversitdocument.h" |
|
45 #include "qversitproperty.h" |
|
46 #include <QtTest/QtTest> |
|
47 #include <QByteArray> |
|
48 |
|
49 // Copied from tst_qcontactmanager.cpp |
|
50 // Waits until __expr is true and fails if it doesn't happen within 5s. |
|
51 #ifndef QTRY_VERIFY |
|
52 #define QTRY_VERIFY(__expr) \ |
|
53 do { \ |
|
54 const int __step = 50; \ |
|
55 const int __timeout = 5000; \ |
|
56 if (!(__expr)) { \ |
|
57 QTest::qWait(0); \ |
|
58 } \ |
|
59 for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \ |
|
60 QTest::qWait(__step); \ |
|
61 } \ |
|
62 QVERIFY(__expr); \ |
|
63 } while(0) |
|
64 #endif |
|
65 |
|
66 QTM_USE_NAMESPACE |
|
67 |
|
68 void UT_QVersitWriter::init() |
|
69 { |
|
70 mOutputDevice = new QBuffer; |
|
71 mWriter = new QVersitWriter; |
|
72 mSignalCatcher = new SignalCatcher; |
|
73 qRegisterMetaType<QVersitWriter::State>("QVersitWriter::State"); |
|
74 connect(mWriter, SIGNAL(stateChanged(QVersitWriter::State)), |
|
75 mSignalCatcher, SLOT(stateChanged(QVersitWriter::State))); |
|
76 } |
|
77 |
|
78 void UT_QVersitWriter::cleanup() |
|
79 { |
|
80 delete mWriter; |
|
81 delete mOutputDevice; |
|
82 delete mSignalCatcher; |
|
83 } |
|
84 |
|
85 void UT_QVersitWriter::testDevice() |
|
86 { |
|
87 // No device |
|
88 QVERIFY(mWriter->device() == NULL); |
|
89 |
|
90 // Device has been set |
|
91 mWriter->setDevice(mOutputDevice); |
|
92 QVERIFY(mWriter->device() == mOutputDevice); |
|
93 } |
|
94 |
|
95 void UT_QVersitWriter::testDefaultCodec() |
|
96 { |
|
97 QVERIFY(mWriter->defaultCodec() == 0); |
|
98 mWriter->setDefaultCodec(QTextCodec::codecForName("UTF-16BE")); |
|
99 QVERIFY(mWriter->defaultCodec() == QTextCodec::codecForName("UTF-16BE")); |
|
100 } |
|
101 |
|
102 void UT_QVersitWriter::testFold() |
|
103 { |
|
104 // 87 characters long |
|
105 QString longString(QLatin1String( |
|
106 "4567890123456789012345678901234567890123456789012345678901234567890123456" |
|
107 "234567890123456789012345678901234567890123456789012345678901234567890123456" |
|
108 "234567890123456789012")); |
|
109 QByteArray expected( |
|
110 "BEGIN:VCARD\r\n" |
|
111 "VERSION:2.1\r\n" |
|
112 "FN:4567890123456789012345678901234567890123456789012345678901234567890123456\r\n" |
|
113 " 234567890123456789012345678901234567890123456789012345678901234567890123456\r\n" |
|
114 " 234567890123456789012\r\n" |
|
115 "END:VCARD\r\n"); |
|
116 QVersitDocument document; |
|
117 QVersitProperty property; |
|
118 property.setName(QLatin1String("FN")); |
|
119 property.setValue(longString); |
|
120 document.addProperty(property); |
|
121 document.setType(QVersitDocument::VCard21Type); |
|
122 QList<QVersitDocument> list; |
|
123 list.append(document); |
|
124 mWriter->setDevice(mOutputDevice); |
|
125 mOutputDevice->open(QBuffer::ReadWrite); |
|
126 QVERIFY(mWriter->startWriting(list)); |
|
127 QVERIFY(mWriter->waitForFinished()); |
|
128 QCOMPARE(mWriter->state(), QVersitWriter::FinishedState); |
|
129 QCOMPARE(mWriter->error(), QVersitWriter::NoError); |
|
130 mOutputDevice->seek(0); |
|
131 QByteArray result(mOutputDevice->readAll()); |
|
132 QCOMPARE(result, expected); |
|
133 } |
|
134 |
|
135 void UT_QVersitWriter::testWriting21() |
|
136 { |
|
137 // vCard 2.1 |
|
138 QByteArray vCard21( |
|
139 "BEGIN:VCARD\r\n\ |
|
140 VERSION:2.1\r\n\ |
|
141 FN:John\r\n\ |
|
142 END:VCARD\r\n"); |
|
143 QVersitDocument document; |
|
144 QVersitProperty property; |
|
145 property.setName(QString(QString::fromAscii("FN"))); |
|
146 property.setValue(QString::fromAscii("John")); |
|
147 document.addProperty(property); |
|
148 document.setType(QVersitDocument::VCard21Type); |
|
149 QList<QVersitDocument> list; |
|
150 list.append(document); |
|
151 |
|
152 // Device not set |
|
153 QCOMPARE(mWriter->state(), QVersitWriter::InactiveState); |
|
154 QCOMPARE(mWriter->error(), QVersitWriter::NoError); |
|
155 QVERIFY(!mWriter->startWriting(list)); |
|
156 QCOMPARE(mWriter->state(), QVersitWriter::InactiveState); |
|
157 QCOMPARE(mWriter->error(), QVersitWriter::IOError); |
|
158 QVERIFY(!mWriter->waitForFinished()); |
|
159 |
|
160 // Device not opened |
|
161 mWriter->setDevice(mOutputDevice); |
|
162 QVERIFY(!mWriter->startWriting(list)); |
|
163 QCOMPARE(mWriter->state(), QVersitWriter::InactiveState); |
|
164 QCOMPARE(mWriter->error(), QVersitWriter::IOError); |
|
165 |
|
166 // Now open the device and it should work. |
|
167 mOutputDevice->open(QBuffer::ReadWrite); |
|
168 QVERIFY(mWriter->startWriting(list)); |
|
169 QVERIFY(mWriter->waitForFinished()); |
|
170 QCOMPARE(mWriter->state(), QVersitWriter::FinishedState); |
|
171 QCOMPARE(mWriter->error(), QVersitWriter::NoError); |
|
172 mOutputDevice->seek(0); |
|
173 QByteArray result(mOutputDevice->readAll()); |
|
174 QCOMPARE(result, vCard21); |
|
175 |
|
176 // Try some other codec |
|
177 delete mOutputDevice; |
|
178 mOutputDevice = new QBuffer; |
|
179 mOutputDevice->open(QBuffer::ReadWrite); |
|
180 mWriter->setDevice(mOutputDevice); |
|
181 QTextCodec* utf16(QTextCodec::codecForName("UTF-16")); |
|
182 mWriter->setDefaultCodec(utf16); |
|
183 QVERIFY(mWriter->startWriting(list)); |
|
184 QVERIFY(mWriter->waitForFinished()); |
|
185 QCOMPARE(mWriter->state(), QVersitWriter::FinishedState); |
|
186 QCOMPARE(mWriter->error(), QVersitWriter::NoError); |
|
187 mOutputDevice->seek(0); |
|
188 result = mOutputDevice->readAll(); |
|
189 QByteArray expected(utf16->fromUnicode(QLatin1String(vCard21.data()))); |
|
190 QString out; |
|
191 for (int i = 0; i < result.length(); i++) { |
|
192 QString t; |
|
193 out += t.sprintf("%02X ", (unsigned char)result.at(i)); |
|
194 } |
|
195 QCOMPARE(result, expected); |
|
196 } |
|
197 |
|
198 void UT_QVersitWriter::testWriting30() |
|
199 { |
|
200 // vCard 3.0 |
|
201 QByteArray vCard30( |
|
202 "BEGIN:VCARD\r\n\ |
|
203 VERSION:3.0\r\n\ |
|
204 FN:John\r\n\ |
|
205 END:VCARD\r\n"); |
|
206 |
|
207 QVersitDocument document; |
|
208 QVersitProperty property; |
|
209 property.setName(QString(QString::fromAscii("FN"))); |
|
210 property.setValue(QString::fromAscii("John")); |
|
211 document.addProperty(property); |
|
212 document.setType(QVersitDocument::VCard30Type); |
|
213 QList<QVersitDocument> list; |
|
214 list.append(document); |
|
215 |
|
216 // Basic 3.0 test |
|
217 mOutputDevice->open(QBuffer::ReadWrite); |
|
218 mWriter->setDevice(mOutputDevice); |
|
219 QVERIFY(mWriter->startWriting(list)); |
|
220 QVERIFY(mWriter->waitForFinished()); |
|
221 QCOMPARE(mWriter->state(), QVersitWriter::FinishedState); |
|
222 QCOMPARE(mWriter->error(), QVersitWriter::NoError); |
|
223 mOutputDevice->seek(0); |
|
224 QByteArray result(mOutputDevice->readAll()); |
|
225 QCOMPARE(result, vCard30); |
|
226 |
|
227 // Asynchronous writing |
|
228 mOutputDevice->reset(); |
|
229 mSignalCatcher->mReceived.clear(); |
|
230 QVERIFY(mWriter->startWriting(list)); |
|
231 QTRY_VERIFY(mSignalCatcher->mReceived.count() >= 2); |
|
232 QCOMPARE(mSignalCatcher->mReceived.at(0), QVersitWriter::ActiveState); |
|
233 QCOMPARE(mSignalCatcher->mReceived.at(1), QVersitWriter::FinishedState); |
|
234 |
|
235 // Cancelling |
|
236 delete mOutputDevice; |
|
237 mOutputDevice = new QBuffer; |
|
238 mOutputDevice->open(QBuffer::ReadWrite); |
|
239 mSignalCatcher->mReceived.clear(); |
|
240 mWriter->setDevice(mOutputDevice); |
|
241 mWriter->startWriting(list); |
|
242 mWriter->cancel(); |
|
243 mWriter->waitForFinished(); |
|
244 QTRY_VERIFY(mSignalCatcher->mReceived.count() >= 2); |
|
245 QCOMPARE(mSignalCatcher->mReceived.at(0), QVersitWriter::ActiveState); |
|
246 QVersitWriter::State state(mSignalCatcher->mReceived.at(1)); |
|
247 // It's possible that it finishes before it cancels. |
|
248 QVERIFY(state == QVersitWriter::CanceledState |
|
249 || state == QVersitWriter::FinishedState); |
|
250 } |
|
251 |
|
252 QTEST_MAIN(UT_QVersitWriter) |
|
253 |
|