| 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 "ContactsTest.h" |
| 27 |
|
| 28 |
#include <QVersitReader> |
| 29 |
#include <QVersitContactImporter> |
| 30 |
#include <QtTest/QtTest> |
| 31 |
#include <QDebug> |
| 32 |
#include "SyncMLPluginLogging.h" |
| 33 |
|
| 34 |
#include "ContactsStorage.h" |
| 35 |
#include "SimpleItem.h" |
| 36 |
|
| 37 |
static const QByteArray originalData( |
| 38 |
"BEGIN:VCARD\r\n" |
| 39 |
"VERSION:2.1\r\n" |
| 40 |
"REV:20091214T091343Z\r\n" |
| 41 |
"N:B;A\r\n" |
| 42 |
"SOUND:\r\n" |
| 43 |
"PHOTO:\r\n" |
| 44 |
"URL: \r\n" |
| 45 |
"TITLE:\r\n" |
| 46 |
"NOTE:\r\n" |
| 47 |
"END:VCARD\r\n"); |
| 48 |
|
| 49 |
static const QByteArray modifiedData( |
| 50 |
"BEGIN:VCARD\r\n" |
| 51 |
"VERSION:2.1\r\n" |
| 52 |
"REV:20091214T091343Z\r\n" |
| 53 |
"N:B;A\r\n" |
| 54 |
"SOUND:\r\n" |
| 55 |
"PHOTO:\r\n" |
| 56 |
"URL: \r\n" |
| 57 |
"TITLE:\r\n" |
| 58 |
"NOTE:\r\n" |
| 59 |
"END:VCARD\r\n"); |
| 60 |
|
| 61 |
// allocate all resources needed here |
| 62 |
void ContactsTest::initTestCase() |
| 63 |
{ |
| 64 |
} |
| 65 |
|
| 66 |
// de-allocate all resources used |
| 67 |
void ContactsTest::cleanupTestCase() |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
void ContactsTest::testSuiteSingle() |
| 72 |
{ |
| 73 |
QMap<QString, QString> props; |
| 74 |
props.insert(QLatin1String("Sync Target"), QLatin1String("local")); |
| 75 |
|
| 76 |
ContactStorage storage("hcontacts"); |
| 77 |
QVERIFY(storage.init( props )); |
| 78 |
|
| 79 |
runTestSuite( originalData, modifiedData, storage, false ); |
| 80 |
|
| 81 |
QVERIFY(storage.uninit()); |
| 82 |
} |
| 83 |
|
| 84 |
void ContactsTest::testSuiteBatched() |
| 85 |
{ |
| 86 |
QMap<QString, QString> props; |
| 87 |
props.insert(QLatin1String("Sync Target"), QLatin1String("local")); |
| 88 |
|
| 89 |
ContactStorage storage("hcontacts"); |
| 90 |
QVERIFY(storage.init( props )); |
| 91 |
|
| 92 |
runTestSuite( originalData, modifiedData, storage, true ); |
| 93 |
|
| 94 |
QVERIFY(storage.uninit()); |
| 95 |
} |
| 96 |
|
| 97 |
void ContactsTest::runTestSuite( const QByteArray& aOriginalData, const QByteArray& aModifiedData, |
| 98 |
Buteo::StoragePlugin& aPlugin, bool aBatched ) |
| 99 |
{ |
| 100 |
const int WAIT_TIME = 2000; |
| 101 |
QByteArray data; |
| 102 |
|
| 103 |
qDebug() << "Starting suite..."; |
| 104 |
|
| 105 |
// Get timestamp of 1 seconds in the past. Storages have timestamp accuracy of 1 seconds and |
| 106 |
// the current second is NOT included in getNewItems(), getModifiedItems() and getDeletedItems() |
| 107 |
QDateTime t1 = QDateTime::currentDateTime(); |
| 108 |
qDebug() << "Marking time t1:" << t1; |
| 109 |
|
| 110 |
QTest::qSleep( WAIT_TIME ); |
| 111 |
|
| 112 |
// ** Test New Item |
| 113 |
Buteo::StorageItem* item = aPlugin.newItem(); |
| 114 |
|
| 115 |
QVERIFY( item ); |
| 116 |
QCOMPARE( item->getId(), QString( "" ) ); |
| 117 |
|
| 118 |
// ** Test Add Item |
| 119 |
|
| 120 |
QVERIFY( item->write( 0, aOriginalData ) ); |
| 121 |
QVERIFY( item->getSize() == aOriginalData.size() ); |
| 122 |
QVERIFY( item->read( 0, item->getSize(), data ) ); |
| 123 |
QVERIFY( data == aOriginalData ); |
| 124 |
|
| 125 |
qDebug() << "Adding new item..."; |
| 126 |
Buteo::StoragePlugin::OperationStatus status; |
| 127 |
if( aBatched ) |
| 128 |
{ |
| 129 |
QList<Buteo::StorageItem*> items; |
| 130 |
items.append( item ); |
| 131 |
|
| 132 |
QList<Buteo::StoragePlugin::OperationStatus> results = aPlugin.addItems( items); |
| 133 |
QCOMPARE( results.count(), 1 ); |
| 134 |
status = results.first(); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
status = aPlugin.addItem( *item ); |
| 139 |
} |
| 140 |
|
| 141 |
QVERIFY( status == Buteo::StoragePlugin::STATUS_OK ); |
| 142 |
QVERIFY( !item->getId().isEmpty() ); |
| 143 |
|
| 144 |
QString id = item->getId(); |
| 145 |
|
| 146 |
QTest::qSleep( WAIT_TIME ); |
| 147 |
QDateTime t2 = QDateTime::currentDateTime(); |
| 148 |
qDebug() << "Marking time t2:" << t2; |
| 149 |
QTest::qSleep( WAIT_TIME ); |
| 150 |
|
| 151 |
// ** Check that item is now found from all items |
| 152 |
qDebug() << "Checking that the item is now found from getItems()..."; |
| 153 |
|
| 154 |
QList<QString> items; |
| 155 |
QVERIFY( aPlugin.getAllItemIds( items ) ); |
| 156 |
qDebug() << "id expected" << id; |
| 157 |
QVERIFY( items.contains( id ) ); |
| 158 |
|
| 159 |
// ** Check that item is now found from new items since t1 |
| 160 |
qDebug() << "Checking that the item is found from getNewItems(t1)..."; |
| 161 |
items.clear(); |
| 162 |
QVERIFY( aPlugin.getNewItemIds( items, t1 ) ); |
| 163 |
QVERIFY( items.contains( id ) ); |
| 164 |
|
| 165 |
// ** Check that item is not found from new items since t2 |
| 166 |
qDebug() << "Checking that the item is NOT found from getNewItems(t2)..."; |
| 167 |
items.clear(); |
| 168 |
QVERIFY( aPlugin.getNewItemIds( items, t2 ) ); |
| 169 |
QVERIFY( !items.contains( id ) ); |
| 170 |
|
| 171 |
// ** Test Modify Item |
| 172 |
|
| 173 |
QVERIFY( item->write( 0, aModifiedData ) ); |
| 174 |
QVERIFY( item->getSize() == aModifiedData.size() ); |
| 175 |
QVERIFY( item->read( 0, item->getSize(), data ) ); |
| 176 |
QVERIFY( data == aModifiedData ); |
| 177 |
|
| 178 |
qDebug() << "Modifying item..."; |
| 179 |
if( aBatched ) |
| 180 |
{ |
| 181 |
QList<Buteo::StorageItem*> items; |
| 182 |
items.append( item ); |
| 183 |
|
| 184 |
QList<Buteo::StoragePlugin::OperationStatus> results = aPlugin.modifyItems( items); |
| 185 |
QCOMPARE( results.count(), 1 ); |
| 186 |
status = results.first(); |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
status = aPlugin.modifyItem( *item ); |
| 191 |
} |
| 192 |
|
| 193 |
QDateTime time = QDateTime::currentDateTime(); |
| 194 |
qDebug() << "Item modified at :" << time; |
| 195 |
|
| 196 |
QVERIFY( status == Buteo::StoragePlugin::STATUS_OK ); |
| 197 |
QVERIFY( item->getId() == id ); |
| 198 |
|
| 199 |
delete item; |
| 200 |
|
| 201 |
QTest::qSleep( WAIT_TIME ); |
| 202 |
QDateTime t3 = QDateTime::currentDateTime(); |
| 203 |
qDebug() << "Marking time t3:" << t3; |
| 204 |
QTest::qSleep( WAIT_TIME ); |
| 205 |
|
| 206 |
// ** Check that item is NOT found from new items since t1 |
| 207 |
qDebug() << "Checking that the item is found from getNewItems(t1)..."; |
| 208 |
items.clear(); |
| 209 |
QVERIFY( aPlugin.getNewItemIds( items, t1 ) ); |
| 210 |
QVERIFY( !items.contains( id ) ); |
| 211 |
|
| 212 |
// ** Check that item is found from modified items since t1 |
| 213 |
qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)..."; |
| 214 |
items.clear(); |
| 215 |
QVERIFY( aPlugin.getModifiedItemIds( items, t1 ) ); |
| 216 |
QVERIFY( items.contains( id ) ); |
| 217 |
|
| 218 |
// ** Check that item is now found from modified items since t2 |
| 219 |
qDebug() << "Checking that the item is found from getModifiedItems(t2)..."; |
| 220 |
items.clear(); |
| 221 |
QVERIFY( aPlugin.getModifiedItemIds( items, t2 ) ); |
| 222 |
QVERIFY( items.contains( id ) ); |
| 223 |
|
| 224 |
// ** Check that item is not found from modified items since t3 |
| 225 |
qDebug() << "Checking that the item is NOT found from getModifiedItems(t3)..."; |
| 226 |
items.clear(); |
| 227 |
QVERIFY( aPlugin.getModifiedItemIds( items, t3 ) ); |
| 228 |
QVERIFY( !items.contains( id ) ); |
| 229 |
|
| 230 |
// ** Test Delete Item |
| 231 |
qDebug() << "Deleting item..."; |
| 232 |
if( aBatched ) |
| 233 |
{ |
| 234 |
QList<QString> items; |
| 235 |
items.append( id ); |
| 236 |
|
| 237 |
QList<Buteo::StoragePlugin::OperationStatus> results = aPlugin.deleteItems( items ); |
| 238 |
QCOMPARE( results.count(), 1 ); |
| 239 |
status = results.first(); |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
status = aPlugin.deleteItem( id ); |
| 244 |
} |
| 245 |
|
| 246 |
QVERIFY( status == Buteo::StoragePlugin::STATUS_OK ); |
| 247 |
|
| 248 |
// ** Check that item is no longer found from new items since t1 |
| 249 |
qDebug() << "Checking that the item is NOT found from getNewItems(t1)..."; |
| 250 |
items.clear(); |
| 251 |
QVERIFY( aPlugin.getNewItemIds( items, t1 ) ); |
| 252 |
QVERIFY( !items.contains( id ) ); |
| 253 |
|
| 254 |
// ** Check that item is not found from modified items since t1 |
| 255 |
qDebug() << "Checking that the item is NOT found from getModifiedItems(t1)..."; |
| 256 |
items.clear(); |
| 257 |
QVERIFY( aPlugin.getModifiedItemIds( items, t1 ) ); |
| 258 |
QVERIFY( !items.contains( id ) ); |
| 259 |
|
| 260 |
// ** Check that item is not found from deleted items since t1 |
| 261 |
qDebug() << "Checking that the item is NOT found from getDeletedItems(t1)..."; |
| 262 |
items.clear(); |
| 263 |
QVERIFY( aPlugin.getDeletedItemIds( items, t1 ) ); |
| 264 |
QVERIFY( !items.contains( id ) ); |
| 265 |
|
| 266 |
// ** Check that item is no longer found from modified items since t2 |
| 267 |
qDebug() << "Checking that the item is NOT found from getModifiedItems(t2)..."; |
| 268 |
items.clear(); |
| 269 |
QVERIFY( aPlugin.getModifiedItemIds( items, t2 ) ); |
| 270 |
QVERIFY( !items.contains( id ) ); |
| 271 |
|
| 272 |
// ** Check that item is now found from deleted items since t2 |
| 273 |
qDebug() << "Checking that the item is now found from getDeletedItems(t2)..."; |
| 274 |
items.clear(); |
| 275 |
QVERIFY( aPlugin.getDeletedItemIds( items, t2 ) ); |
| 276 |
QVERIFY( items.contains( id ) ); |
| 277 |
|
| 278 |
// ** Check that item is now found from deleted items since t3 |
| 279 |
qDebug() << "Checking that the item is found from getDeletedItems(t3)..."; |
| 280 |
items.clear(); |
| 281 |
QVERIFY( aPlugin.getDeletedItemIds( items, t3 ) ); |
| 282 |
QVERIFY( items.contains( id ) ); |
| 283 |
// ** Check that item is no longer found from all items |
| 284 |
qDebug() << "Checking that the item is NOT found from getItems()..."; |
| 285 |
items.clear(); |
| 286 |
QVERIFY( aPlugin.getAllItemIds( items ) ); |
| 287 |
QVERIFY( !items.contains( id ) ); |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
void ContactsTest::pf177715() |
| 292 |
{ |
| 293 |
const QString vcardTemplate( |
| 294 |
"BEGIN:VCARD\r\n" |
| 295 |
"VERSION:2.1\r\n" |
| 296 |
"N:%1;%2\r\n" |
| 297 |
"TEL:%3\r\n" |
| 298 |
"END:VCARD\r\n"); |
| 299 |
|
| 300 |
const int numContacts = 1000; |
| 301 |
const int contactsPerBatch = 20; |
| 302 |
|
| 303 |
QStringList vcards; |
| 304 |
|
| 305 |
for( int i = 0; i < numContacts; ++i ) |
| 306 |
{ |
| 307 |
|
| 308 |
QString number = QString::number(i); |
| 309 |
QString vcard = vcardTemplate.arg( number ).arg( number ).arg( number); |
| 310 |
vcards.append( vcard); |
| 311 |
} |
| 312 |
|
| 313 |
qDebug() << "Testing with" << numContacts << "contacts, using batches of" << contactsPerBatch; |
| 314 |
QTime timer; |
| 315 |
|
| 316 |
QStringList availableManagers = QContactManager::availableManagers(); |
| 317 |
QContactManager manager( "tracker" ); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
qDebug() << "Clearing existing contacts..."; |
| 322 |
QList<QContactLocalId> contactIDs = manager.contactIds(); |
| 323 |
for( int i = 0; i < contactIDs.count(); i += contactsPerBatch ) |
| 324 |
{ |
| 325 |
directDelete( contactIDs.mid( i, contactsPerBatch ), manager ); |
| 326 |
} |
| 327 |
|
| 328 |
qDebug() << "Starting direct add..."; |
| 329 |
timer.start(); |
| 330 |
|
| 331 |
for( int i = 0; i < numContacts; i += contactsPerBatch ) |
| 332 |
{ |
| 333 |
directAdd( vcards.mid( i, contactsPerBatch ), manager ); |
| 334 |
} |
| 335 |
|
| 336 |
qDebug() << "Direct add finished, took" << timer.elapsed() << "ms"; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
qDebug() << "Starting direct ids..."; |
| 341 |
timer.start(); |
| 342 |
|
| 343 |
contactIDs = manager.contactIds(); |
| 344 |
|
| 345 |
qDebug() << "Get direct ids finished, took" << timer.elapsed() << "ms"; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
qDebug() << "Starting direct delete..."; |
| 350 |
timer.restart(); |
| 351 |
|
| 352 |
contactIDs = manager.contactIds(); |
| 353 |
for( int i = 0; i < contactIDs.count(); i += contactsPerBatch ) |
| 354 |
{ |
| 355 |
directDelete( contactIDs.mid( i, contactsPerBatch ), manager ); |
| 356 |
} |
| 357 |
qDebug() << "Direct delete finished, took" << timer.elapsed() << "ms"; |
| 358 |
|
| 359 |
qDebug() << "Initiating plugin..."; |
| 360 |
timer.restart(); |
| 361 |
|
| 362 |
QMap<QString, QString> props; |
| 363 |
ContactStorage storage("hcontacts"); |
| 364 |
QVERIFY(storage.init( props )); |
| 365 |
|
| 366 |
qDebug() << "Plugin init finished, took" << timer.elapsed() << "ms"; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
QList<Buteo::StorageItem*> items; |
| 371 |
for( int i = 0; i < numContacts; ++i ) |
| 372 |
{ |
| 373 |
Buteo::StorageItem* item = storage.newItem(); |
| 374 |
QVERIFY( item ); |
| 375 |
QVERIFY( item->write( 0, vcards[i].toAscii() ) ); |
| 376 |
items.append( item ); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
qDebug() << "Starting plugin add..."; |
| 382 |
timer.start(); |
| 383 |
|
| 384 |
for( int i = 0; i < numContacts; i += contactsPerBatch ) |
| 385 |
{ |
| 386 |
QList<Buteo::StoragePlugin::OperationStatus> status = storage.addItems( items.mid(i, contactsPerBatch ) ); |
| 387 |
QVERIFY( status.contains( Buteo::StoragePlugin::STATUS_OK ) == contactsPerBatch ); |
| 388 |
} |
| 389 |
|
| 390 |
qDebug() << "Plugin add finished, took" << timer.elapsed() << "ms"; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
qDeleteAll( items ); |
| 395 |
items.clear(); |
| 396 |
|
| 397 |
|
| 398 |
qDebug() << "Starting plugin ids..."; |
| 399 |
timer.start(); |
| 400 |
|
| 401 |
QList<QString> ids; |
| 402 |
|
| 403 |
QVERIFY( storage.getAllItemIds( ids) ); |
| 404 |
|
| 405 |
qDebug() << "Get plugin ids finished, took" << timer.elapsed() << "ms"; |
| 406 |
|
| 407 |
qDebug() << "Starting plugin delete..."; |
| 408 |
timer.restart(); |
| 409 |
|
| 410 |
contactIDs = manager.contactIds(); |
| 411 |
for( int i = 0; i < contactIDs.count(); i += contactsPerBatch ) |
| 412 |
{ |
| 413 |
QList<Buteo::StoragePlugin::OperationStatus> status = storage.deleteItems( ids.mid( i, contactsPerBatch ) ); |
| 414 |
QVERIFY( status.contains( Buteo::StoragePlugin::STATUS_OK ) == contactsPerBatch ); |
| 415 |
} |
| 416 |
qDebug() << "Plugin delete finished, took" << timer.elapsed() << "ms"; |
| 417 |
|
| 418 |
|
| 419 |
qDebug() << "Uninitiating plugin..."; |
| 420 |
timer.restart(); |
| 421 |
|
| 422 |
QVERIFY(storage.uninit()); |
| 423 |
|
| 424 |
qDebug() << "Plugin uninit finished, took" << timer.elapsed() << "ms"; |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
void ContactsTest::directAdd( const QStringList& aVCards, QContactManager& aManager ) |
| 429 |
{ |
| 430 |
|
| 431 |
QByteArray byteArray; |
| 432 |
//QVersitReader needs LF/CRLF/CR between successive vcard's in the list, |
| 433 |
//CRLF didn't work though. |
| 434 |
QString LF = "\n"; |
| 435 |
|
| 436 |
foreach ( QString vcard, aVCards ) |
| 437 |
{ |
| 438 |
byteArray.append(vcard.toUtf8()); |
| 439 |
byteArray.append(LF.toUtf8()); |
| 440 |
} |
| 441 |
|
| 442 |
QBuffer readBuf(&byteArray); |
| 443 |
readBuf.open(QIODevice::ReadOnly); |
| 444 |
readBuf.seek(0); |
| 445 |
|
| 446 |
QVersitReader versitReader; |
| 447 |
versitReader.setDevice (&readBuf); |
| 448 |
|
| 449 |
if (!versitReader.startReading()) |
| 450 |
qCWarning(lcSyncMLPlugin) << "Error while reading vcard"; |
| 451 |
|
| 452 |
if (!versitReader.waitForFinished()) |
| 453 |
qCWarning(lcSyncMLPlugin) << "Error while finishing reading vcard"; |
| 454 |
|
| 455 |
QList<QVersitDocument> versitDocList = versitReader.results (); |
| 456 |
readBuf.close(); |
| 457 |
|
| 458 |
QVersitContactImporter contactImporter; |
| 459 |
QList<QContact> contactList; |
| 460 |
QVERIFY( contactImporter.importDocuments(versitDocList) ); |
| 461 |
contactList = contactImporter.contacts(); |
| 462 |
|
| 463 |
QMap<int, QContactManager::Error> errorMap; |
| 464 |
|
| 465 |
QVERIFY( aManager.saveContacts(&contactList, &errorMap) ); |
| 466 |
QVERIFY( errorMap.isEmpty() ); |
| 467 |
|
| 468 |
} |
| 469 |
|
| 470 |
void ContactsTest::directDelete( const QList<QContactLocalId>& aContactIds, QContactManager& manager ) |
| 471 |
{ |
| 472 |
|
| 473 |
QMap<int , QContactManager::Error> errorMap; |
| 474 |
|
| 475 |
QVERIFY( manager.removeContacts( aContactIds, &errorMap ) ); |
| 476 |
QVERIFY( errorMap.isEmpty() ); |
| 477 |
} |
| 478 |
|
| 479 |
*/ |
| 480 |
|
| 481 |
QTEST_MAIN(ContactsTest) |
| 482 |
|