Debian Codegraph @

storageplugins/hnotes/unittest/NotesTest.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 "NotesTest.h"
27
28 #include <QtTest>
29
30 #include <buteosyncfw5/StorageItem.h>
31
32 void NotesTest::initTestCase()
33 {
34 iNotesStorage = new NotesStorage("hnotes");
35 QMap<QString, QString> props;
36 QVERIFY( iNotesStorage->init( props) );
37 }
38
39 void NotesTest::cleanupTestCase()
40 {
41 QVERIFY( iNotesStorage->uninit() );
42 delete iNotesStorage;
43 }
44
45 void NotesTest::testSuite()
46 {
47 const QByteArray originalData( "I'm a note" );
48
49 const QByteArray modifiedData( "I'm a modified note" );
50
51 runTestSuite( originalData, modifiedData);
52 }
53
54 void NotesTest::runTestSuite(const QByteArray& aOriginalData, const QByteArray& aModifiedData)
55 {
56 QByteArray data;
57
58 qDebug() << "Starting suite...";
59
60 // Get timestamp of 1 seconds in the past. Storages have timestamp accuracy of 1 seconds and
61 // the current second is NOT included in getNewItems(), getModifiedItems() and getDeletedItems()
62 QDateTime t1 = QDateTime::currentDateTime();
63 qDebug() << "Marking time t1:" << t1;
64
65 QTest::qSleep( 2000 );
66
67 // ** Test New Item
68 Buteo::StorageItem* item = iNotesStorage->newItem();
69
70 QVERIFY( item );
71 QCOMPARE( item->getId(), QString( "" ) );
72
73 // ** Test Add Item
74
75 QVERIFY( item->write( 0, aOriginalData ) );
76 QVERIFY( item->getSize() == aOriginalData.size() );
77 QVERIFY( item->read( 0, item->getSize(), data ) );
78 QVERIFY( data == aOriginalData );
79
80 qDebug() << "Adding new item...";
81 Buteo::StoragePlugin::OperationStatus status = iNotesStorage->addItem( *item );
82
83 QVERIFY( status == Buteo::StoragePlugin::STATUS_OK );
84 QVERIFY( !item->getId().isEmpty() );
85
86 QString id = item->getId();
87
88 QTest::qSleep( 2000 );
89 QDateTime t2 = QDateTime::currentDateTime();
90 qDebug() << "Marking time t2:" << t2;
91 QTest::qSleep( 2000 );
92
93 // ** Check that item is now found from all items
94 qDebug() << "Checking that the item is now found from getItems()...";
95
96 QList<QString> items;
97 QVERIFY( iNotesStorage->getAllItemIds( items ) );
98
99 bool found = false;
100 for( int i = 0; i < items.count(); ++i ) {
101
102 if( items[i] == id ) {
103 found = true;
104 break;
105 }
106
107 }
108
109 QVERIFY( found );
110
111 items.clear();
112
113 // ** Check that item is now found from new items at t1
114 qDebug() << "Checking that the item is found from getNewItems(t1)...";
115 QVERIFY( iNotesStorage->getNewItemIds( items, t1 ) );
116
117 found = false;
118 for( int i = 0; i < items.count(); ++i ) {
119
120 if( items[i] == id ) {
121 found = true;
122 break;
123 }
124
125 }
126
127 items.clear();
128
129 QVERIFY( found );
130
131 // ** Check that item is not found from new items at t2
132 qDebug() << "Checking that the item is NOT found from getNewItems(t2)...";
133
134 // @todo: There's no mechanism in storage plugin API for error handling,
135 // therefore we can't check if this call failed or not
136 QVERIFY( iNotesStorage->getNewItemIds( items, t2 ) );
137
138 found = false;
139 for( int i = 0; i < items.count(); ++i ) {
140
141 if( items[i] == id ) {
142 found = true;
143 break;
144 }
145
146 }
147
148 items.clear();
149
150 QVERIFY( !found );
151
152 // ** Test Modify Item
153
154 QVERIFY( item->write( 0, aModifiedData ) );
155 QVERIFY( item->getSize() == aModifiedData.size() );
156 QVERIFY( item->read( 0, item->getSize(), data ) );
157 QVERIFY( data == aModifiedData );
158
159 qDebug() << "Modifying item...";
160 status = iNotesStorage->modifyItem( *item );
161
162 QVERIFY( status == Buteo::StoragePlugin::STATUS_OK );
163 QVERIFY( item->getId() == id );
164
165 delete item;
166
167 QTest::qSleep( 2000 );
168 QDateTime t3 = QDateTime::currentDateTime();
169 qDebug() << "Marking time t3:" << t3;
170 QTest::qSleep( 2000 );
171
172 // ** Check that item is still found from new items at t1
173 qDebug() << "Checking that the item is found from getNewItems(t1)...";
174 QVERIFY( iNotesStorage->getNewItemIds( items, t1 ) );
175
176 found = false;
177 for( int i = 0; i < items.count(); ++i ) {
178
179 if( items[i] == id ) {
180 found = true;
181 break;
182 }
183
184 }
185
186 items.clear();
187
188 QVERIFY( found );
189
190 // ** Check that item is not found from modified items at t1
191 qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)...";
192 QVERIFY( iNotesStorage->getModifiedItemIds( items, t1 ) );
193
194 found = false;
195 for( int i = 0; i < items.count(); ++i ) {
196
197 if( items[i] == id ) {
198 found = true;
199 break;
200 }
201
202 }
203
204 items.clear();
205
206 QVERIFY( !found );
207
208 // ** Check that item is now found from modified items at t2
209 qDebug() << "Checking that the item is found from getModifiedItems(t2)...";
210 QVERIFY( iNotesStorage->getModifiedItemIds( items, t2 ) );
211
212 found = false;
213 for( int i = 0; i < items.count(); ++i ) {
214
215 if( items[i] == id ) {
216 found = true;
217 break;
218 }
219
220 }
221
222 items.clear();
223
224 QVERIFY( found );
225
226 // ** Check that item is not found from modified items at t3
227 qDebug() << "Checking that the item is NOT found from getModifiedItems(t3)...";
228 QVERIFY( iNotesStorage->getModifiedItemIds( items, t3 ) );
229
230 found = false;
231 for( int i = 0; i < items.count(); ++i ) {
232
233 if( items[i] == id ) {
234 found = true;
235 break;
236 }
237
238 }
239
240 items.clear();
241
242 QVERIFY( !found );
243
244 // ** Test Delete Item
245 qDebug() << "Deleting item...";
246 QVERIFY( iNotesStorage->deleteItem( id ) == Buteo::StoragePlugin::STATUS_OK );
247
248 // ** Check that item is no longer found from new items at t1
249 qDebug() << "Checking that the item is NOT found from getNewItems(t1)...";
250 QVERIFY( iNotesStorage->getNewItemIds( items, t1 ) );
251
252 found = false;
253 for( int i = 0; i < items.count(); ++i ) {
254
255 if( items[i] == id ) {
256 found = true;
257 break;
258 }
259
260 }
261
262 items.clear();
263
264 QVERIFY( !found );
265
266
267 // ** Check that item is not found from modified items at t1
268 qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)...";
269 QVERIFY( iNotesStorage->getModifiedItemIds( items, t1 ) );
270
271 found = false;
272 for( int i = 0; i < items.count(); ++i ) {
273
274 if( items[i] == id ) {
275 found = true;
276 break;
277 }
278
279 }
280
281 items.clear();
282
283 QVERIFY( !found );
284
285
286 // ** Check that item is not found from deleted items at t1
287 qDebug() << "Checking that the item is NOT found from getDeletedItems(t1)...";
288 QVERIFY( iNotesStorage->getDeletedItemIds( items, t1 ) );
289
290 found = false;
291 for( int i = 0; i < items.count(); ++i ) {
292
293 if( items[i] == id ) {
294 found = true;
295 break;
296 }
297
298 }
299
300 items.clear();
301
302 QVERIFY( !found );
303
304 // ** Check that item is no longer found from modified items at t2
305 qDebug() << "Checking that the item is NOT found from getModifiedItems(t2)...";
306 QVERIFY( iNotesStorage->getModifiedItemIds( items, t2 ) );
307
308 found = false;
309 for( int i = 0; i < items.count(); ++i ) {
310
311 if( items[i] == id ) {
312 found = true;
313 break;
314 }
315
316 }
317
318 items.clear();
319
320 QVERIFY( !found );
321
322
323 // ** Check that item is now found from deleted items at t2
324 qDebug() << "Checking that the item is now found from getDeletedItems(t2)...";
325 QVERIFY( iNotesStorage->getDeletedItemIds( items, t2 ) );
326
327 found = false;
328 for( int i = 0; i < items.count(); ++i ) {
329
330 if( items[i] == id ) {
331 found = true;
332 break;
333 }
334
335 }
336
337 items.clear();
338
339 QVERIFY( found );
340
341 // ** Check that item is now found from deleted items at t3
342 qDebug() << "Checking that the item is found from getDeletedItems(t3)...";
343 QVERIFY( iNotesStorage->getDeletedItemIds( items, t3 ) );
344
345 found = false;
346 for( int i = 0; i < items.count(); ++i ) {
347
348 if( items[i] == id ) {
349 found = true;
350 break;
351 }
352
353 }
354
355 items.clear();
356
357 QVERIFY( found );
358
359 // ** Check that item is no longer found from all items
360 qDebug() << "Checking that the item is NOT found from getItems()...";
361 QVERIFY( iNotesStorage->getAllItemIds( items ) );
362
363 found = false;
364 for( int i = 0; i < items.count(); ++i ) {
365
366 if( items[i] == id ) {
367 found = true;
368 break;
369 }
370
371 }
372
373 items.clear();
374
375 QVERIFY( !found );
376 }
377
378 QTEST_MAIN(NotesTest)