Debian Codegraph @

storageplugins/hcalendar/unittest/CalendarTest.cpp

raw
1 /*
2 * This file is part of buteo-sync-plugins package
3 *
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * Copyright (C) 2014 Jolla Ltd.
6 *
7 * Contributors: Sateesh Kavuri <[email protected]>
8 * Valério Valério <[email protected]>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * version 2.1 as published by the Free Software Foundation.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26 #include "CalendarTest.h"
27
28 #include <buteosyncfw5/StorageItem.h>
29 #include <QtTest>
30
31 void CalendarTest::initTestCase()
32 {
33 iCalendarStorage = new CalendarStorage("hcalendar");
34 QMap<QString, QString> props;
35 props[NOTEBOOKNAME] = "testnotebook";
36
37 QVERIFY( iCalendarStorage->init( props ) );
38 }
39
40 void CalendarTest::cleanupTestCase()
41 {
42 QVERIFY( iCalendarStorage->uninit() );
43 delete iCalendarStorage;
44 }
45
46 void CalendarTest::testSuite()
47 {
48
49 const QByteArray originalData( "BEGIN:VCALENDAR\r\n" \
50 "VERSION:1.0\r\n" \
51 "BEGIN:VEVENT\r\n" \
52 "UID:D5G8OBE64EFnl46ib91EM2\r\n" \
53 "SUMMARY:Test\r\n" \
54 "DTSTART:20090909T080000\r\n" \
55 "DTEND:20090909T080000\r\n" \
56 "CLASS:PRIVATE\r\n" \
57 "LOCATION:Hell\r\n" \
58 "PRIORITY:2\r\n" \
59 "END:VEVENT\r\n" \
60 "END:VCALENDAR\r\n" );
61
62 const QByteArray modifiedData( "BEGIN:VCALENDAR\r\n" \
63 "VERSION:1.0\r\n" \
64 "BEGIN:VEVENT\r\n" \
65 "UID:D5G8OBE64EFnl46ib9\r\n" \
66 "SUMMARY:Test2\r\n" \
67 "DTSTART:20090909T090000\r\n" \
68 "DTEND:20090909T090000\r\n" \
69 "CLASS:PRIVATE\r\n" \
70 "LOCATION:Heaven\r\n" \
71 "PRIORITY:2\r\n" \
72 "END:VEVENT\r\n" \
73 "END:VCALENDAR\r\n" );
74
75 runTestSuite( originalData, modifiedData);
76
77 }
78
79 void CalendarTest::runTestSuite( const QByteArray& aOriginalData, const QByteArray& aModifiedData)
80 {
81 QByteArray data;
82
83 qDebug() << "Starting suite...";
84
85 // Get timestamp of 1 seconds in the past. Storages have timestamp accuracy of 1 seconds and
86 // the current second is NOT included in getNewItems(), getModifiedItems() and getDeletedItems()
87 QDateTime t1 = QDateTime::currentDateTime();
88 qDebug() << "Marking time t1:" << t1;
89
90 QTest::qSleep( 2000 );
91
92 // ** Test New Item
93 Buteo::StorageItem* item = iCalendarStorage->newItem();
94
95 QVERIFY( item );
96 QCOMPARE( item->getId(), QString( "" ) );
97
98
99 // ** Test Add Item
100
101 QVERIFY( item->write( 0, aOriginalData ) );
102 QVERIFY( item->getSize() == aOriginalData.size() );
103 QVERIFY( item->read( 0, item->getSize(), data ) );
104 QVERIFY( data == aOriginalData );
105
106 qDebug() << "Adding new item...";
107 Buteo::StoragePlugin::OperationStatus status = iCalendarStorage->addItem( *item );
108
109 QVERIFY( status == Buteo::StoragePlugin::STATUS_OK );
110 QVERIFY( !item->getId().isEmpty() );
111
112 QString id = item->getId();
113
114 QTest::qSleep( 2000 );
115 QDateTime t2 = QDateTime::currentDateTime();
116 qDebug() << "Marking time t2:" << t2;
117 QTest::qSleep( 2000 );
118
119
120 // ** Check that item is now found from all items
121 qDebug() << "Checking that the item is now found from getItems()...";
122
123 QList<QString> items;
124 QVERIFY( iCalendarStorage->getAllItemIds( items ) );
125
126 bool found = false;
127 for( int i = 0; i < items.count(); ++i ) {
128
129 if( items[i] == id ) {
130 found = true;
131 break;
132 }
133
134 }
135
136 QVERIFY( found );
137
138 items.clear();
139
140 // ** Check that item is now found from new items at t1
141 qDebug() << "Checking that the item is found from getNewItems(t1)...";
142 QVERIFY( iCalendarStorage->getNewItemIds( items, t1 ) );
143
144 found = false;
145 for( int i = 0; i < items.count(); ++i ) {
146
147 if( items[i] == id ) {
148 found = true;
149 break;
150 }
151
152 }
153
154 items.clear();
155
156 QVERIFY( found );
157
158 // ** Check that item is not found from new items at t2
159 qDebug() << "Checking that the item is NOT found from getNewItems(t2)...";
160
161 // @todo: There's no mechanism in storage plugin API for error handling,
162 // therefore we can't check if this call failed or not
163 QVERIFY( iCalendarStorage->getNewItemIds( items, t2 ) );
164
165 found = false;
166 for( int i = 0; i < items.count(); ++i ) {
167
168 if( items[i] == id ) {
169 found = true;
170 break;
171 }
172
173 }
174
175 items.clear();
176
177 QVERIFY( !found );
178
179 // ** Test Modify Item
180
181 QVERIFY( item->write( 0, aModifiedData ) );
182 QVERIFY( item->getSize() == aModifiedData.size() );
183 QVERIFY( item->read( 0, item->getSize(), data ) );
184 QVERIFY( data == aModifiedData );
185
186 qDebug() << "Modifying item...";
187 status = iCalendarStorage->modifyItem( *item );
188
189 QVERIFY( status == Buteo::StoragePlugin::STATUS_OK );
190 QVERIFY( item->getId() == id );
191
192 delete item;
193
194 QTest::qSleep( 2000 );
195 QDateTime t3 = QDateTime::currentDateTime();
196 qDebug() << "Marking time t3:" << t3;
197 QTest::qSleep( 2000 );
198
199 // ** Check that item is still found from new items at t1
200 qDebug() << "Checking that the item is found from getNewItems(t1)...";
201 QVERIFY( iCalendarStorage->getNewItemIds( items, t1 ) );
202
203 found = false;
204 for( int i = 0; i < items.count(); ++i ) {
205
206 if( items[i] == id ) {
207 found = true;
208 break;
209 }
210
211 }
212
213 items.clear();
214
215 QVERIFY( found );
216
217 // ** Check that item is not found from modified items at t1
218 qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)...";
219 QVERIFY( iCalendarStorage->getModifiedItemIds( items, t1 ) );
220
221 found = false;
222 for( int i = 0; i < items.count(); ++i ) {
223
224 if( items[i] == id ) {
225 found = true;
226 break;
227 }
228
229 }
230
231 items.clear();
232
233 QVERIFY( !found );
234
235 // ** Check that item is now found from modified items at t2
236 qDebug() << "Checking that the item is found from getModifiedItems(t2)...";
237 QVERIFY( iCalendarStorage->getModifiedItemIds( items, t2 ) );
238
239 found = false;
240 for( int i = 0; i < items.count(); ++i ) {
241
242 if( items[i] == id ) {
243 found = true;
244 break;
245 }
246
247 }
248
249 items.clear();
250
251 QVERIFY( found );
252
253 // ** Check that item is not found from modified items at t3
254 qDebug() << "Checking that the item is NOT found from getModifiedItems(t3)...";
255 QVERIFY( iCalendarStorage->getModifiedItemIds( items, t3 ) );
256
257 found = false;
258 for( int i = 0; i < items.count(); ++i ) {
259
260 if( items[i] == id ) {
261 found = true;
262 break;
263 }
264
265 }
266
267 items.clear();
268
269 QVERIFY( !found );
270
271 // ** Test Delete Item
272 qDebug() << "Deleting item...";
273 QVERIFY( iCalendarStorage->deleteItem( id ) == Buteo::StoragePlugin::STATUS_OK );
274
275 // ** Check that item is no longer found from new items at t1
276 qDebug() << "Checking that the item is NOT found from getNewItems(t1)...";
277 QVERIFY( iCalendarStorage->getNewItemIds( items, t1 ) );
278
279 found = false;
280 for( int i = 0; i < items.count(); ++i ) {
281
282 if( items[i] == id ) {
283 found = true;
284 break;
285 }
286
287 }
288
289 items.clear();
290
291 QVERIFY( !found );
292
293
294 // ** Check that item is not found from modified items at t1
295 qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)...";
296 QVERIFY( iCalendarStorage->getModifiedItemIds( items, t1 ) );
297
298 found = false;
299 for( int i = 0; i < items.count(); ++i ) {
300
301 if( items[i] == id ) {
302 found = true;
303 break;
304 }
305
306 }
307
308 items.clear();
309
310 QVERIFY( !found );
311
312
313 // ** Check that item is not found from deleted items at t1
314 qDebug() << "Checking that the item is NOT found from getDeletedItems(t1)...";
315 QVERIFY( iCalendarStorage->getDeletedItemIds( items, t1 ) );
316
317 found = false;
318 for( int i = 0; i < items.count(); ++i ) {
319
320 if( items[i] == id ) {
321 found = true;
322 break;
323 }
324
325 }
326
327 items.clear();
328
329 QVERIFY( !found );
330
331 // ** Check that item is no longer found from modified items at t2
332 qDebug() << "Checking that the item is NOT found from getModifiedItems(t2)...";
333 QVERIFY( iCalendarStorage->getModifiedItemIds( items, t2 ) );
334
335 found = false;
336 for( int i = 0; i < items.count(); ++i ) {
337
338 if( items[i] == id ) {
339 found = true;
340 break;
341 }
342
343 }
344
345 items.clear();
346
347 QVERIFY( !found );
348
349
350 // ** Check that item is now found from deleted items at t2
351 qDebug() << "Checking that the item is now found from getDeletedItems(t2)...";
352 QVERIFY( iCalendarStorage->getDeletedItemIds( items, t2 ) );
353
354 found = false;
355 for( int i = 0; i < items.count(); ++i ) {
356
357 if( items[i] == id ) {
358 found = true;
359 break;
360 }
361
362 }
363
364 items.clear();
365
366 QVERIFY( found );
367
368 // ** Check that item is now found from deleted items at t3
369 qDebug() << "Checking that the item is found from getDeletedItems(t3)...";
370 QVERIFY( iCalendarStorage->getDeletedItemIds( items, t3 ) );
371
372 found = false;
373 for( int i = 0; i < items.count(); ++i ) {
374
375 if( items[i] == id ) {
376 found = true;
377 break;
378 }
379
380 }
381
382 items.clear();
383
384 QVERIFY( found );
385
386 // ** Check that item is no longer found from all items
387 qDebug() << "Checking that the item is NOT found from getItems()...";
388 QVERIFY( iCalendarStorage->getAllItemIds( items ) );
389
390 found = false;
391 for( int i = 0; i < items.count(); ++i ) {
392
393 if( items[i] == id ) {
394 found = true;
395 break;
396 }
397
398 }
399
400 items.clear();
401
402 QVERIFY( !found );
403 }
404
405 QTEST_MAIN(CalendarTest)