tests/benchmarks/contacts/tst_bm_contacts.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <QApplication>
       
    42 #include <QtTest/QtTest>
       
    43 //#include "contact.h"
       
    44 //#include <QContactManager>
       
    45 #include <QDebug>
       
    46 #include <QEventLoop>
       
    47 #include <QTimer>
       
    48 
       
    49 #include <qcontactabstractrequest.h>
       
    50 #include <requests/qcontactfetchrequest.h>
       
    51 #include <filters/qcontactlocalidfilter.h>
       
    52 #include <details/qcontactdetails.h>
       
    53 
       
    54 #ifdef Q_OS_SYMBIAN
       
    55 #include <cntfilt.h>
       
    56 #include <cntitem.h>
       
    57 #include <cntdb.h>
       
    58 #include <cntitem.h>
       
    59 #include <cntfield.h>
       
    60 #include <cntfldst.h>
       
    61 #include <cntdef.h>
       
    62 #include <cntdef.hrh> 
       
    63 #endif
       
    64 
       
    65 QTM_USE_NAMESPACE
       
    66 
       
    67 class tst_Contact : public QObject
       
    68 {
       
    69     Q_OBJECT
       
    70 
       
    71 public:
       
    72     enum platform {
       
    73         QContacts,
       
    74         Native
       
    75     };
       
    76 
       
    77     
       
    78 private slots:
       
    79     void initTestCase();
       
    80     void cleanupTestCase();
       
    81 
       
    82     void tst_createTime_data();
       
    83     void tst_createTime();
       
    84 
       
    85     void tst_fetchAllContactIds_data();
       
    86     void tst_fetchAllContactIds();
       
    87     void tst_fetchOneContact_data();
       
    88     void tst_fetchOneContact();
       
    89     void tst_fetchTenContact_data();
       
    90     void tst_fetchTenContact();
       
    91     //void tst_fetchAllContact();
       
    92 
       
    93     void tst_createContact_data();  
       
    94     void tst_createContact();
       
    95     
       
    96     void tst_saveContact_data();
       
    97     void tst_saveContact();    
       
    98 
       
    99     void tst_nameFilter_data();
       
   100     void tst_nameFilter();
       
   101 
       
   102     void tst_removeOneContact_data();
       
   103     void tst_removeOneContact();
       
   104     
       
   105     void tst_removeAllContacts_data();
       
   106     void tst_removeAllContacts();
       
   107 
       
   108 
       
   109 //    void tst_currentLanguage();
       
   110 //    void tst_availableLanguages();
       
   111 //
       
   112 //    void tst_versions_data();
       
   113 //    void tst_versions();
       
   114 //
       
   115 //    void tst_hasFeatures_data();
       
   116 //    void tst_hasFeatures();
       
   117 
       
   118 public slots:
       
   119 
       
   120     void gotContact(QContactFetchRequest*,bool);
       
   121     void stateChanged(QContactAbstractRequest::State newState);
       
   122     void timeout();
       
   123     void resultsAvailable();
       
   124     void resultsAvailableFilter();
       
   125     //void setManager(QContactManager *mgr);
       
   126 
       
   127 private:
       
   128     void createContact(QContactManager *mgr, enum platform p = QContacts);
       
   129     void clearContacts(QContactManager *mgr, enum platform p = QContacts);
       
   130     int countContacts(QContactManager *mgr, enum platform p = QContacts);
       
   131     
       
   132     QString manager;
       
   133     QEventLoop *loop;
       
   134 //    QContactManager *m_qm;
       
   135     QContactManager *m_systemManager;
       
   136     QList<QContactManager *> m_managers;
       
   137 //    enum platform m_platform;
       
   138     int m_num_contacts;
       
   139     QList<QContactLocalId> id_list;
       
   140 
       
   141     int m_run;
       
   142     
       
   143     int m_num_start;
       
   144 
       
   145     QTimer *m_timer;
       
   146     QStringList firstNames;
       
   147     QStringList lastNames;    
       
   148 
       
   149 };
       
   150 
       
   151 Q_DECLARE_METATYPE(QContactManager *);
       
   152 Q_DECLARE_METATYPE(tst_Contact::platform);
       
   153 
       
   154 void tst_Contact::initTestCase()
       
   155 {    
       
   156     qDebug() << "Managers: " << QContactManager::availableManagers();    
       
   157     m_run = 0;
       
   158     m_systemManager = 0x0;
       
   159 
       
   160     QStringList list = QContactManager::availableManagers();       
       
   161     while(!list.empty()){
       
   162       QString mgr = list.takeFirst();
       
   163       if((mgr != "invalid") && (mgr != "testdummy") && (mgr != "maliciousplugin")){
       
   164         m_managers.append(new QContactManager(mgr));
       
   165         if((mgr == "symbian") || (mgr == "tracker") || (mgr == "maemo5")) {
       
   166           qDebug() << "Found system manager: " << mgr;
       
   167           m_systemManager = m_managers.last();
       
   168         }
       
   169         if(!m_systemManager && mgr == "memory"){
       
   170             qDebug() << "Found system manager of last resort: " << mgr;
       
   171             m_systemManager = m_managers.last();
       
   172         }
       
   173       }      
       
   174     }
       
   175     if(m_managers.isEmpty()) {
       
   176       QFAIL("Unable to find valid managers. Please check install");
       
   177     }
       
   178     if(!m_systemManager){
       
   179       QFAIL("Unable to find a default plugin to use, please install plugins or fix test");
       
   180     }
       
   181 
       
   182     // setup an event loop for waiting
       
   183     loop = new QEventLoop;
       
   184 
       
   185     firstNames << "Anahera" << "Anaru" << "Hemi" << "Hine" << "Kiri" << "Maata" << "Mere" << "Moana" << "Paora" << "Petera" << "Piripi" << "Ruiha" << "Tane" << "Whetu";
       
   186     lastNames << "Ati Awa" << "Kai Taho" << "Moriori" << "Muaupoko" << "Nga Rauru" << "Taranaki" << "Opotoki" << "Aotea" << "Taninui" << "Tuhourangi" << "Tainui" << "Waitaha";
       
   187     
       
   188     m_num_start = countContacts(m_systemManager);
       
   189     qDebug() << "Number of Contact: " << m_num_start;
       
   190 
       
   191 
       
   192     for(int i = 0; i < 20; i++){
       
   193       foreach(QContactManager *mgr, m_managers){
       
   194         createContact(mgr);
       
   195       }
       
   196     }
       
   197     
       
   198     int after = countContacts(m_systemManager);
       
   199     if(after - m_num_start != 20){
       
   200         qWarning() << "Failed to create 20 contacts";
       
   201     }
       
   202 
       
   203     m_timer = new QTimer(this);
       
   204     connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
       
   205 
       
   206 }
       
   207 
       
   208 int tst_Contact::countContacts(QContactManager *manager, enum platform platform)
       
   209 {  
       
   210   if(platform == tst_Contact::QContacts) {
       
   211         QList<QContactLocalId> qcl = manager->contactIds();
       
   212         return qcl.count();
       
   213   } 
       
   214   else if(platform == tst_Contact::Native){ 
       
   215 #ifdef Q_OS_SYMBIAN
       
   216     CContactDatabase* contactsDb = CContactDatabase::OpenL();
       
   217     CleanupStack::PushL(contactsDb);
       
   218     
       
   219     int num = contactsDb->CountL();
       
   220     
       
   221     CleanupStack::PopAndDestroy(contactsDb);
       
   222     
       
   223     return num;    
       
   224 #endif
       
   225     }
       
   226 
       
   227     qWarning("No backend support in countContacts()");
       
   228     return 0;
       
   229 
       
   230 }
       
   231 
       
   232 void tst_Contact::cleanupTestCase()
       
   233 {
       
   234   foreach(QContactManager *manager, m_managers) {
       
   235     clearContacts(manager);          
       
   236   }
       
   237   if(countContacts(0x0, Native))
       
   238     clearContacts(0x0, tst_Contact::Native);
       
   239   int num_end = countContacts(0x0, tst_Contact::Native);  
       
   240   if(num_end){
       
   241     qWarning() << "Ended with: " << num_end << " contacts, should be 0";
       
   242   }
       
   243 }
       
   244 
       
   245 void tst_Contact::createContact(QContactManager *manager, enum platform platform)
       
   246 {
       
   247   if(platform == tst_Contact::QContacts) {    
       
   248     QContact *c = new QContact;
       
   249     c->setType(QContactType::TypeContact);
       
   250     QContactName cname;
       
   251     QString name;
       
   252     name = firstNames.takeFirst();
       
   253     firstNames.push_back(name);
       
   254     cname.setFirstName(name);
       
   255     name = lastNames.takeFirst();
       
   256     lastNames.push_back(name);
       
   257     cname.setLastName(name);
       
   258 #ifndef Q_WS_MAEMO_5
       
   259     cname.setPrefix("Mr");
       
   260 #endif
       
   261     c->saveDetail(&cname);
       
   262 
       
   263     if(!manager->saveContact(c)){
       
   264       qDebug() << "Failed to create contact during setup";
       
   265       return;
       
   266     }
       
   267     id_list.append(c->localId());
       
   268     delete c;
       
   269   }
       
   270   else if(platform == tst_Contact::Native){
       
   271 #ifdef Q_OS_SYMBIAN
       
   272     // Create a contact card and add a work phone number. Numeric values are 
       
   273     // stored in a text field (storage type = KStorageTypeText).
       
   274     
       
   275     CContactDatabase* db = CContactDatabase::OpenL();
       
   276     CleanupStack::PushL(db);
       
   277 
       
   278     CContactCard* newCard = CContactCard::NewLC();    
       
   279     
       
   280     QString name;
       
   281     
       
   282     // Create the firstName field and add the data to it
       
   283     name = firstNames.takeFirst();
       
   284     firstNames.push_back(name);        
       
   285     CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   286     TPtrC Firstname(reinterpret_cast<const TUint16*>(name.utf16()));
       
   287     firstName->TextStorage()->SetTextL(Firstname);      
       
   288     newCard->AddFieldL(*firstName);
       
   289     CleanupStack::Pop(firstName);
       
   290 
       
   291     // Create the lastName field and add the data to it
       
   292     name = lastNames.takeFirst();
       
   293     lastNames.push_back(name);        
       
   294     CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
       
   295     TPtrC Lastname(reinterpret_cast<const TUint16*>(name.utf16()));
       
   296     lastName->TextStorage()->SetTextL(Lastname);
       
   297     newCard->AddFieldL(*lastName);
       
   298     CleanupStack::Pop(lastName);
       
   299 
       
   300     CContactItemField* prefix = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPrefixName);
       
   301     _LIT(KPrefix, "Mr");
       
   302     prefix->TextStorage()->SetTextL(KPrefix);
       
   303     newCard->AddFieldL(*prefix);
       
   304     CleanupStack::Pop(prefix);
       
   305 
       
   306     // Add newCard to the database
       
   307     const TContactItemId contactId = db->AddNewContactL(*newCard);
       
   308     db->CloseContactL(contactId);    
       
   309     
       
   310     id_list.append(contactId);
       
   311     CleanupStack::PopAndDestroy(2, db); //newCard, contactsDb
       
   312 #else
       
   313     qWarning("No native test for createContact");
       
   314 #endif 
       
   315   }
       
   316 }
       
   317 
       
   318 void tst_Contact::clearContacts(QContactManager *manager, enum platform platform)
       
   319 {
       
   320   if(platform == tst_Contact::QContacts) {
       
   321     QMap<int, QContactManager::Error> errorMap;
       
   322     manager->removeContacts(id_list, &errorMap);
       
   323     id_list.clear();
       
   324   }
       
   325   else if(platform == tst_Contact::Native){
       
   326 #ifdef Q_OS_SYMBIAN
       
   327     CContactDatabase* contactDb = CContactDatabase::OpenL();
       
   328     CleanupStack::PushL(contactDb);
       
   329     
       
   330     CCntFilter *filter = CCntFilter::NewL();
       
   331     CleanupStack::PushL(filter);
       
   332     //get all contact items (no groups, templates...)
       
   333     filter->SetContactFilterTypeALL(EFalse);
       
   334     filter->SetContactFilterTypeCard(ETrue);
       
   335     TRAPD(err, contactDb->FilterDatabaseL(*filter));
       
   336     if(err != KErrNone){
       
   337       qWarning() << "clearContacts failed to get any contacts error: " << err;
       
   338       CleanupStack::PopAndDestroy(2, contactDb);
       
   339       return;
       
   340     }
       
   341     
       
   342     CContactIdArray *iContacts = 0;
       
   343     TRAP(err, iContacts = CContactIdArray::NewL(filter->iIds));
       
   344     if(err != KErrNone){      
       
   345       CleanupStack::PopAndDestroy(2, contactDb); //idArray, contactsDb
       
   346       return;
       
   347     }
       
   348     CleanupStack::PushL(iContacts);
       
   349     
       
   350     TRAP(err, contactDb->DeleteContactsL(*iContacts));
       
   351     if(err != KErrNone)
       
   352       qWarning() << "DeleteContacts failed with: " << err;
       
   353     
       
   354     CleanupStack::PopAndDestroy(3, contactDb); //idArray, contactsDb
       
   355 #endif
       
   356   }
       
   357 
       
   358 }
       
   359 
       
   360 void tst_Contact::tst_createTime_data()
       
   361 {
       
   362   QTest::addColumn<tst_Contact::platform>("platform");
       
   363   QTest::addColumn<QContactManager *>("manager");
       
   364   
       
   365   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   366   foreach(QContactManager *mgr, m_managers){
       
   367     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   368   }  
       
   369 }
       
   370 
       
   371 void tst_Contact::tst_createTime()
       
   372 {
       
   373   QFETCH(tst_Contact::platform, platform);
       
   374   QFETCH(QContactManager *, manager);
       
   375   
       
   376   if(platform == tst_Contact::QContacts){
       
   377     
       
   378     QContactManager *qm = 0x0;
       
   379 
       
   380     QBENCHMARK {
       
   381         qm = new QContactManager(manager->managerName());
       
   382     }
       
   383     delete qm;
       
   384   }
       
   385   else if(platform == tst_Contact::Native){
       
   386 #ifdef Q_OS_SYMBIAN
       
   387     CContactDatabase* db = 0x0;
       
   388     QBENCHMARK {
       
   389       db = CContactDatabase::OpenL();     
       
   390     }
       
   391     CleanupStack::PushL(db);
       
   392     CleanupStack::PopAndDestroy(db); //db
       
   393 #endif
       
   394   }
       
   395 }
       
   396 
       
   397 void tst_Contact::tst_fetchAllContactIds_data()
       
   398 {
       
   399   QTest::addColumn<tst_Contact::platform>("platform");
       
   400   QTest::addColumn<QContactManager *>("manager");
       
   401   
       
   402   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   403   foreach(QContactManager *mgr, m_managers){
       
   404     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   405   }  
       
   406 }
       
   407 
       
   408 
       
   409 void tst_Contact::tst_fetchAllContactIds()
       
   410 {   
       
   411   QFETCH(tst_Contact::platform, platform);
       
   412   QFETCH(QContactManager *, manager);
       
   413 
       
   414   if(platform == tst_Contact::QContacts) {
       
   415     QList<QContactLocalId> ql;    
       
   416     
       
   417     QBENCHMARK {
       
   418         ql = manager->contactIds();
       
   419     }        
       
   420   }
       
   421   else if(platform == tst_Contact::Native){
       
   422 #ifdef Q_OS_SYMBIAN
       
   423     //open database
       
   424     // Open the default contact database
       
   425     CContactDatabase* contactsDb = CContactDatabase::OpenL();
       
   426     CleanupStack::PushL(contactsDb);    
       
   427 
       
   428     CCntFilter *filter = CCntFilter::NewLC();
       
   429     
       
   430     //get all contact items (no groups, templates...)
       
   431     filter->SetContactFilterTypeALL(EFalse);
       
   432     filter->SetContactFilterTypeCard(ETrue);
       
   433     QBENCHMARK {
       
   434       contactsDb->FilterDatabaseL(*filter);
       
   435       CContactIdArray *iContacts = CContactIdArray::NewLC(filter->iIds);
       
   436       CleanupStack::PopAndDestroy(iContacts);
       
   437     }
       
   438     CleanupStack::PopAndDestroy(2, contactsDb); //iContacts, filter, contactsDb    
       
   439 #endif 
       
   440   }
       
   441 }
       
   442 
       
   443 void tst_Contact::tst_fetchOneContact_data()
       
   444 {
       
   445   QTest::addColumn<tst_Contact::platform>("platform");
       
   446   QTest::addColumn<QContactManager *>("manager");
       
   447   
       
   448   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   449   foreach(QContactManager *mgr, m_managers){
       
   450     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   451   }  
       
   452 }
       
   453 
       
   454 void tst_Contact::tst_fetchOneContact()
       
   455 {
       
   456   QFETCH(tst_Contact::platform, platform);
       
   457   QFETCH(QContactManager *, manager);
       
   458 
       
   459   if(platform == tst_Contact::QContacts){
       
   460     QContact c;    
       
   461 
       
   462     m_run++;
       
   463 
       
   464 #if defined(Q_WS_MAEMO_6)
       
   465     int ret;   
       
   466     QContactFetchRequest* req = new QContactFetchRequest;
       
   467 
       
   468     QList<QContactLocalId> qcl = manager->contactIds();
       
   469     if(qcl.count() < 1)
       
   470         QFAIL("No contacts to pull from tracker");
       
   471     QList<QContactLocalId> one;
       
   472     one += qcl.takeFirst();
       
   473     QContactLocalIdFilter idFil;
       
   474     idFil.setIds(one);
       
   475     req->setFilter(idFil);
       
   476 
       
   477     req->setManager(manager);    
       
   478     //connect(req, SIGNAL(progress(QContactFetchRequest*, bool)), this, SLOT(gotContact(QContactFetchRequest*,bool)));
       
   479     //connect(req, SIGNAL(stateChanged(QContactAbstractRequest::State)), this, SLOT(stateChanged(QContactAbstractRequest::State)));
       
   480     connect(req, SIGNAL(resultsAvailable()), this, SLOT(resultsAvailable()));
       
   481 
       
   482     m_num_contacts = 1;
       
   483     m_timer->start(1000);
       
   484 
       
   485     QBENCHMARK {
       
   486         req->start();
       
   487         ret = loop->exec();
       
   488     }
       
   489     m_timer->stop();
       
   490 
       
   491     //qDebug() << "Got Contact: " << qm->synthesizeDisplayLabel(c);
       
   492     if(ret){
       
   493         QFAIL("Failed to load one contact");
       
   494     }
       
   495     delete req;
       
   496 
       
   497 #elif defined(Q_OS_SYMBIAN)    
       
   498     QList<QContactLocalId> qcl = manager->contactIds();    
       
   499     if(qcl.count() < 1)
       
   500         QFAIL("No contacts to pull from tracker");
       
   501     
       
   502     QBENCHMARK {
       
   503        c = manager->contact(qcl.first());
       
   504     }
       
   505 #endif
       
   506   }
       
   507   else if(platform == tst_Contact::Native){
       
   508 #ifdef Q_OS_SYMBIAN
       
   509     //open database
       
   510     // Open the default contact database
       
   511     CContactDatabase* contactDb = CContactDatabase::OpenL();
       
   512     CleanupStack::PushL(contactDb);       
       
   513     CContactItem *item = 0;
       
   514     TInt r;
       
   515     
       
   516     CCntFilter *filter = CCntFilter::NewL();
       
   517     CleanupStack::PushL(filter);
       
   518 
       
   519     //get all contact items (no groups, templates...)
       
   520     filter->SetContactFilterTypeALL(EFalse);
       
   521     filter->SetContactFilterTypeCard(ETrue);
       
   522     contactDb->FilterDatabaseL(*filter);
       
   523     CContactIdArray *iContacts = CContactIdArray::NewL(filter->iIds);
       
   524     CleanupStack::PushL(iContacts);
       
   525     int cnt = iContacts->Count();
       
   526     int i = 0;
       
   527                
       
   528     QBENCHMARK {      
       
   529       TRAP(r, item = contactDb->ReadContactL((*iContacts)[i++]));
       
   530       if(r == KErrNone)
       
   531         delete item;
       
   532       if(i >= cnt)
       
   533         i = 0;
       
   534     }      
       
   535     
       
   536     if(r != KErrNone){ qWarning() << "Error by OpenContactL: " << r; }
       
   537             
       
   538     CleanupStack::PopAndDestroy(3, contactDb); //contact, filter, IdArray    
       
   539 
       
   540 #endif
       
   541   }                
       
   542 }
       
   543 
       
   544 
       
   545 void tst_Contact::tst_fetchTenContact_data()
       
   546 {
       
   547   QTest::addColumn<tst_Contact::platform>("platform");
       
   548   QTest::addColumn<QContactManager *>("manager");
       
   549   
       
   550   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   551   foreach(QContactManager *mgr, m_managers){
       
   552     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   553   }  
       
   554 }
       
   555 
       
   556 void tst_Contact::tst_fetchTenContact()
       
   557 {
       
   558   QFETCH(tst_Contact::platform, platform);
       
   559   QFETCH(QContactManager *, manager);
       
   560 
       
   561   if(platform == tst_Contact::QContacts){
       
   562     QContact c;
       
   563     m_run++;
       
   564 
       
   565 #if defined(Q_WS_MAEMO_6)
       
   566     int ret;
       
   567 
       
   568     QContactFetchRequest* req = new QContactFetchRequest;
       
   569 
       
   570     QList<QContactLocalId> qcl = manager->contactIds();
       
   571     if(qcl.count() < 10){
       
   572         QFAIL("No enough contacts to get 10");
       
   573     }
       
   574     QList<QContactLocalId> one;
       
   575     for(int i = 0; i<10; i++)
       
   576         one += qcl.takeFirst();
       
   577     m_num_contacts = 10;
       
   578 
       
   579     QContactLocalIdFilter idFil;
       
   580     idFil.setIds(one);
       
   581     req->setFilter(idFil);
       
   582 
       
   583     req->setManager(manager);
       
   584 
       
   585     //    connect(req, SIGNAL(progress(QContactFetchRequest*, bool)), this, SLOT(gotContact(QContactFetchRequest*,bool)));
       
   586     connect(req, SIGNAL(resultsAvailable()), this, SLOT(resultsAvailable()));
       
   587 
       
   588     m_timer->start(1000);
       
   589 
       
   590     QBENCHMARK {
       
   591         req->start();
       
   592         ret = loop->exec();
       
   593     }
       
   594     m_timer->stop();
       
   595 
       
   596     //qDebug() << "Got Contact: " << qm->synthesizeDisplayLabel(c);
       
   597     if(ret){
       
   598         QFAIL("Failed to load one contact");
       
   599     }
       
   600 
       
   601     delete req;
       
   602 
       
   603 #elif defined(Q_OS_SYMBIAN)
       
   604     QList<QContactLocalId> qcl = manager->contactIds();
       
   605     if(qcl.count() < 10){
       
   606         QFAIL("No enough contacts to get 10");
       
   607     }
       
   608     QList<QContactLocalId> one;
       
   609     for(int i = 0; i<10; i++)
       
   610         one += qcl.takeFirst();
       
   611 
       
   612     QContactLocalIdFilter idFil;
       
   613     idFil.setIds(one);
       
   614     
       
   615     QList<QContact> qlc;
       
   616     
       
   617     QBENCHMARK {
       
   618       qlc = manager->contacts(idFil, QList<QContactSortOrder>(), QContactFetchHint());
       
   619     }
       
   620     
       
   621     if(qlc.count() != 10){
       
   622       QFAIL("Did not get 10 contacts back");    
       
   623     }
       
   624     
       
   625 #endif    
       
   626   }
       
   627   else if(platform == tst_Contact::Native){
       
   628 #ifdef Q_OS_SYMBIAN
       
   629     //open database
       
   630     // Open the default contact database
       
   631     CContactDatabase* contactDb = CContactDatabase::OpenL();
       
   632     CleanupStack::PushL(contactDb);
       
   633 
       
   634     CCntFilter *filter = CCntFilter::NewL();
       
   635     CleanupStack::PushL(filter);
       
   636 
       
   637     //get all contact items (no groups, templates...)
       
   638     filter->SetContactFilterTypeALL(EFalse);
       
   639     filter->SetContactFilterTypeCard(ETrue);
       
   640     contactDb->FilterDatabaseL(*filter);
       
   641     CContactIdArray *iContacts = CContactIdArray::NewL(filter->iIds);
       
   642     CleanupStack::PushL(iContacts);
       
   643     if(iContacts->Count() <= 10){
       
   644       QFAIL("There are less than 10 contacts to fetch");
       
   645     }
       
   646     
       
   647     TInt r;
       
   648     int i = 0;
       
   649     
       
   650     CContactItem *item = 0;
       
   651 
       
   652     // there is no multiple fetch in symbian
       
   653     
       
   654     
       
   655     QBENCHMARK {
       
   656       for(i = 0; i < 10; i++){
       
   657         TRAP(r, item = contactDb->ReadContactL((*iContacts)[i]));
       
   658         if(r == KErrNone)
       
   659           delete item;
       
   660       }
       
   661     }
       
   662     
       
   663     CleanupStack::PopAndDestroy(3, contactDb); //iContacts, filer, contactsDb    
       
   664 #endif
       
   665   }
       
   666 }
       
   667 
       
   668 void tst_Contact::timeout()
       
   669 {    
       
   670     qDebug() << "***** Timeout, haven't received the signal/contact within 1sec";
       
   671     loop->exit(1); // timeout, fail test
       
   672 
       
   673 }
       
   674 
       
   675 void tst_Contact::gotContact(QContactFetchRequest *request, bool appendOnly)
       
   676 {
       
   677     Q_UNUSED(appendOnly);
       
   678 
       
   679     // first, check to make sure that the request is still valid.
       
   680     if (request->state() == QContactAbstractRequest::CanceledState) {
       
   681         delete request;
       
   682         QWARN("Contact request canceled");
       
   683         loop->exit(1);
       
   684         return; // ignore these results.
       
   685     }
       
   686 
       
   687     if(request->contacts().count() > 0) {        
       
   688         m_num_contacts -= request->contacts().count();
       
   689         if(m_num_contacts <= 0)
       
   690             loop->exit(0);
       
   691         return; // got one or more
       
   692     }
       
   693 
       
   694     // check to see if the request status is "finished" - clean up.
       
   695     if (request->state() == QContactAbstractRequest::FinishedState) {
       
   696         delete request;        
       
   697     }
       
   698 
       
   699 }
       
   700 
       
   701 void tst_Contact::resultsAvailable()
       
   702 {
       
   703 
       
   704     QContactFetchRequest *req = qobject_cast<QContactFetchRequest *>(sender());
       
   705     if(req){
       
   706         //qDebug() << m_run << " Got resultsAvailable: " << req->contacts().count() << " need: " << m_num_contacts;
       
   707         if(!req->contacts().empty()) {
       
   708             m_num_contacts -= req->contacts().count();
       
   709             if(m_num_contacts <= 0)
       
   710                 loop->exit(0);
       
   711             return; // got one or more
       
   712         }
       
   713     }
       
   714 
       
   715 }
       
   716 
       
   717 void tst_Contact::resultsAvailableFilter()
       
   718 {
       
   719 
       
   720     QContactFetchRequest *req = qobject_cast<QContactFetchRequest *>(sender());
       
   721     if(req){
       
   722         if(!req->contacts().empty()) { // we got enough certainly...don't know how many are coming back with the filter
       
   723             loop->exit(0);
       
   724             return; // got one or more
       
   725         }
       
   726     }
       
   727 }
       
   728 
       
   729 void tst_Contact::stateChanged(QContactAbstractRequest::State /*newState*/)
       
   730 {
       
   731     qDebug() << "Got state change";
       
   732 }
       
   733 
       
   734 void tst_Contact::tst_createContact_data()
       
   735 {
       
   736   QTest::addColumn<tst_Contact::platform>("platform");
       
   737   QTest::addColumn<QContactManager *>("manager");
       
   738   
       
   739   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   740   foreach(QContactManager *mgr, m_managers){
       
   741     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   742   }  
       
   743 }
       
   744 
       
   745 void tst_Contact::tst_createContact()
       
   746 {
       
   747   QFETCH(tst_Contact::platform, platform);
       
   748   QFETCH(QContactManager *, manager);
       
   749 
       
   750   QBENCHMARK {
       
   751         createContact(manager, platform);
       
   752   }
       
   753 }
       
   754 
       
   755 void tst_Contact::tst_saveContact_data()
       
   756 {
       
   757   QTest::addColumn<tst_Contact::platform>("platform");
       
   758   QTest::addColumn<QContactManager *>("manager");
       
   759   
       
   760   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   761   foreach(QContactManager *mgr, m_managers){
       
   762     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   763   }  
       
   764 }
       
   765 
       
   766 void tst_Contact::tst_saveContact()
       
   767 {
       
   768   QFETCH(tst_Contact::platform, platform);
       
   769   QFETCH(QContactManager *, manager);
       
   770 
       
   771   if(platform == tst_Contact::QContacts) {    
       
   772     QContact *c = new QContact;
       
   773     c->setType("Contact");
       
   774     QContactName cname;
       
   775     QString name;
       
   776     name = firstNames.takeFirst();
       
   777     firstNames.push_back(name);
       
   778     cname.setFirstName(name);
       
   779     name = lastNames.takeFirst();
       
   780     lastNames.push_back(name);
       
   781     cname.setLastName(name);
       
   782 #ifndef Q_WS_MAEMO_5
       
   783     cname.setPrefix("Mr");
       
   784 #endif
       
   785     c->saveDetail(&cname);
       
   786 
       
   787     int ret = 0; 
       
   788     
       
   789     QBENCHMARK {
       
   790       ret = manager->saveContact(c);
       
   791     }
       
   792     if(!ret){
       
   793       qDebug() << "Failed to create contact durring setup";
       
   794       return;
       
   795     }
       
   796     id_list.append(c->localId());
       
   797     delete c;
       
   798   }
       
   799   else if(platform == tst_Contact::Native){
       
   800 #ifdef Q_OS_SYMBIAN
       
   801     // Create a contact card and add a work phone number. Numeric values are 
       
   802     // stored in a text field (storage type = KStorageTypeText).
       
   803     
       
   804     CContactDatabase* db = CContactDatabase::OpenL();
       
   805     CleanupStack::PushL(db);
       
   806 
       
   807     CContactCard* newCard = CContactCard::NewLC();    
       
   808     
       
   809     QString name;
       
   810     
       
   811     // Create the firstName field and add the data to it
       
   812     name = firstNames.takeFirst();
       
   813     firstNames.push_back(name);        
       
   814     CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   815     TPtrC Firstname(reinterpret_cast<const TUint16*>(name.utf16()));
       
   816     firstName->TextStorage()->SetTextL(Firstname);      
       
   817     newCard->AddFieldL(*firstName);
       
   818     CleanupStack::Pop(firstName);
       
   819 
       
   820     // Create the lastName field and add the data to it
       
   821     name = lastNames.takeFirst();
       
   822     lastNames.push_back(name);        
       
   823     CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
       
   824     TPtrC Lastname(reinterpret_cast<const TUint16*>(name.utf16()));
       
   825     lastName->TextStorage()->SetTextL(Lastname);
       
   826     newCard->AddFieldL(*lastName);
       
   827     CleanupStack::Pop(lastName);
       
   828 
       
   829     CContactItemField* prefix = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPrefixName);
       
   830     _LIT(KPrefix, "Mr");
       
   831     prefix->TextStorage()->SetTextL(KPrefix);
       
   832     newCard->AddFieldL(*prefix);
       
   833     CleanupStack::Pop(prefix);
       
   834 
       
   835     QBENCHMARK {
       
   836       // Add newCard to the database
       
   837       const TContactItemId contactId = db->AddNewContactL(*newCard);
       
   838       db->CloseContactL(contactId);
       
   839       id_list.append(contactId);      
       
   840     }
       
   841         
       
   842     CleanupStack::PopAndDestroy(2, db); //newCard, contactsDb
       
   843 #else
       
   844     qWarning("No native test defined for this platform and tst_saveContact()");
       
   845 #endif 
       
   846   }
       
   847 }
       
   848 
       
   849 void tst_Contact::tst_nameFilter_data()
       
   850 {
       
   851   QTest::addColumn<tst_Contact::platform>("platform");
       
   852   QTest::addColumn<QContactManager *>("manager");
       
   853   
       
   854   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   855   foreach(QContactManager *mgr, m_managers){
       
   856     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   857   }  
       
   858 }
       
   859 
       
   860 
       
   861 void tst_Contact::tst_nameFilter()
       
   862 { 
       
   863   QFETCH(tst_Contact::platform, platform);
       
   864   QFETCH(QContactManager *, manager);
       
   865  
       
   866   if(platform == tst_Contact::QContacts){
       
   867     QContactFilter fil = QContactName::match(firstNames.first(),""); // pick one first name to find
       
   868     //QContactFilter fil = QContactName::match("sdfsdfsdfjhsjkdfshdkf", ""); // pick one first name to find
       
   869     QContact c;    
       
   870 
       
   871     m_run++;
       
   872 
       
   873 
       
   874 
       
   875 #if defined(Q_WS_MAEMO_6)
       
   876     int ret;
       
   877     QContactFetchRequest* req = new QContactFetchRequest;
       
   878     req->setFilter(fil);
       
   879     req->setManager(manager);
       
   880 
       
   881     connect(req, SIGNAL(resultsAvailable()), this, SLOT(resultsAvailableFilter()));
       
   882 
       
   883     m_timer->start(1000);
       
   884 
       
   885     QBENCHMARK {
       
   886         req->start();
       
   887         ret = loop->exec();
       
   888     }
       
   889     m_timer->stop();
       
   890 
       
   891     //qDebug() << "Got Contact: " << qm->synthesizeDisplayLabel(c);
       
   892     if(ret){
       
   893         QFAIL("Failed to load one contact");
       
   894     }
       
   895 
       
   896 //    QList<QContact> qcl = req->contacts();
       
   897 //    while(!qcl.isEmpty()){
       
   898 //        QContact c = qcl.takeFirst();
       
   899 //        qDebug() << "Contact: " << c.displayLabel();
       
   900 //    }
       
   901     delete req;
       
   902     
       
   903 #elif defined(Q_OS_SYMBIAN)
       
   904     QList<QContact> qlc;
       
   905     
       
   906     QBENCHMARK {
       
   907       qlc = manager->contacts(fil, QList<QContactSortOrder>(), QContactFetchHint());
       
   908     }
       
   909     
       
   910 //    while(!qlc.isEmpty()){
       
   911 //        QContact c = qlc.takeFirst();
       
   912 //        qDebug() << "Contact: " << c.displayLabel();
       
   913 //    }
       
   914 #endif
       
   915   }
       
   916   else if(platform == tst_Contact::Native){
       
   917 #ifdef Q_OS_SYMBIAN
       
   918     //open database
       
   919     // Open the default contact database
       
   920     CContactDatabase* contactDb = CContactDatabase::OpenL();
       
   921     CleanupStack::PushL(contactDb);
       
   922        
       
   923     CContactItem *item = 0x0;
       
   924 
       
   925     CCntFilter *filter = CCntFilter::NewL();
       
   926     CleanupStack::PushL(filter);
       
   927 
       
   928     //get all contact items (no groups, templates...)
       
   929     filter->SetContactFilterTypeALL(EFalse);
       
   930     filter->SetContactFilterTypeCard(ETrue);
       
   931     contactDb->FilterDatabaseL(*filter);
       
   932     CContactIdArray *iContacts;
       
   933     TRAPD(filter_err, iContacts = CContactIdArray::NewL(filter->iIds));
       
   934     if(filter_err != KErrNone){
       
   935       CleanupStack::PopAndDestroy(2, contactDb); // filter, contactDB
       
   936       QFAIL("Failed to fetch a list of contacts");
       
   937     }
       
   938     CleanupStack::PushL(iContacts);
       
   939     
       
   940     TBuf<100> Firstname(0);
       
   941     
       
   942     for(int i = 0; i< iContacts->Count(); i++){
       
   943       TRAPD(err, item = contactDb->ReadContactL((*iContacts)[0]));
       
   944       if(err != KErrNone) {
       
   945         CleanupStack::PopAndDestroy(3, contactDb); // iContacts, filers, db
       
   946         QFAIL("Unable to fetch a sample item from the contacts file");
       
   947       }
       
   948       CleanupStack::PushL(item);
       
   949       
       
   950       int fieldNum = item->CardFields().Find(KUidContactFieldGivenName);
       
   951       if(fieldNum != KErrNotFound){
       
   952         Firstname.Copy((item->CardFields())[fieldNum].TextStorage()->Text());
       
   953         CleanupStack::PopAndDestroy(item); // item
       
   954         break;
       
   955       }
       
   956       CleanupStack::PopAndDestroy(item); // item
       
   957     }
       
   958     if(Firstname.Length() == 0){
       
   959       QFAIL("Unable to find a contact to search for");
       
   960     }
       
   961       
       
   962 
       
   963     //const TPtrC Firstname(reinterpret_cast<const TUint16*>(firstNames.first().utf16()));
       
   964     CContactIdArray* idArray;
       
   965     
       
   966     CContactItemFieldDef* fieldDef = new (ELeave) CContactItemFieldDef();
       
   967     CleanupStack::PushL(fieldDef);
       
   968     
       
   969     fieldDef->AppendL( KUidContactFieldGivenName);
       
   970        
       
   971     QBENCHMARK {      
       
   972       idArray = contactDb->FindLC(Firstname, fieldDef);      
       
   973       if(idArray->Count() > 0) {
       
   974           item = contactDb->ReadContactL((*idArray)[0]);
       
   975           delete item;
       
   976           CleanupStack::PopAndDestroy(idArray);
       
   977       }
       
   978       else {
       
   979           qDebug() << "Number of contacts native: " << countContacts(0x0, Native);
       
   980           qDebug() << "Number of contacts qt/system: " << countContacts(m_systemManager);
       
   981           QFAIL("No contacts returned from CContactDatabase::FindLC");
       
   982       }
       
   983     }   
       
   984     
       
   985     CleanupStack::PopAndDestroy(4, contactDb); //item, idArray, fielddef, lock, contactsDb
       
   986 #endif
       
   987   }
       
   988 }
       
   989 
       
   990 void tst_Contact::tst_removeOneContact_data()
       
   991 {
       
   992   QTest::addColumn<tst_Contact::platform>("platform");
       
   993   QTest::addColumn<QContactManager *>("manager");
       
   994   
       
   995   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
   996   foreach(QContactManager *mgr, m_managers){
       
   997     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
   998   }  
       
   999 }
       
  1000 
       
  1001 void tst_Contact::tst_removeOneContact()
       
  1002 {
       
  1003   QFETCH(tst_Contact::platform, platform);
       
  1004   QFETCH(QContactManager *, manager);
       
  1005   
       
  1006   if(platform == tst_Contact::QContacts){
       
  1007     QList<QContactLocalId> one;
       
  1008     QMap<int, QContactManager::Error> errorMap;
       
  1009 
       
  1010     if(id_list.count() < 1){ // incase we run out of contacts
       
  1011       createContact(manager, platform);
       
  1012     }
       
  1013         
       
  1014     one += id_list.takeFirst();
       
  1015     
       
  1016     QBENCHMARK_ONCE {
       
  1017         manager->removeContacts(one, &errorMap);
       
  1018     }    
       
  1019 
       
  1020   }
       
  1021   else if(platform == tst_Contact::Native){    
       
  1022 #ifdef Q_OS_SYMBIAN
       
  1023     CContactDatabase* contactDb = CContactDatabase::OpenL();
       
  1024     CleanupStack::PushL(contactDb);
       
  1025 
       
  1026     CCntFilter *filter = CCntFilter::NewL();
       
  1027     CleanupStack::PushL(filter);
       
  1028 
       
  1029     //get all contact items (no groups, templates...)
       
  1030     filter->SetContactFilterTypeALL(EFalse);
       
  1031     filter->SetContactFilterTypeCard(ETrue);
       
  1032     contactDb->FilterDatabaseL(*filter);
       
  1033     CContactIdArray *iContacts = CContactIdArray::NewL(filter->iIds);
       
  1034     CleanupStack::PushL(iContacts);
       
  1035     int i = 0;
       
  1036      
       
  1037     QBENCHMARK_ONCE { // Do it once only, to many removals becomes a problem
       
  1038       contactDb->DeleteContactL((*iContacts)[i++]);
       
  1039       if(i >= iContacts->Count()){
       
  1040         break;
       
  1041       }
       
  1042     }
       
  1043     
       
  1044     CleanupStack::PopAndDestroy(3, contactDb); //idArray, contactsDb
       
  1045 #endif
       
  1046   }
       
  1047 }
       
  1048 
       
  1049 void tst_Contact::tst_removeAllContacts_data()
       
  1050 {
       
  1051   QTest::addColumn<tst_Contact::platform>("platform");
       
  1052   QTest::addColumn<QContactManager *>("manager");
       
  1053   
       
  1054   QTest::newRow("Native") << tst_Contact::Native << (QContactManager *)0x0;
       
  1055   foreach(QContactManager *mgr, m_managers){
       
  1056     QTest::newRow(QString("QContact-" + mgr->managerName()).toAscii()) << tst_Contact::QContacts << mgr; 
       
  1057   }  
       
  1058 }
       
  1059 
       
  1060 
       
  1061 void tst_Contact::tst_removeAllContacts()
       
  1062 {
       
  1063   QFETCH(tst_Contact::platform, platform);
       
  1064   QFETCH(QContactManager *, manager);
       
  1065 
       
  1066   int before = countContacts(manager, platform);
       
  1067 
       
  1068   if(before < 20) {
       
  1069     for(int i = before; i < 20; i++){
       
  1070       createContact(manager, platform);
       
  1071     }
       
  1072   }  
       
  1073 
       
  1074   QBENCHMARK {
       
  1075     clearContacts(manager, platform);
       
  1076   }
       
  1077 }
       
  1078 
       
  1079 int main(int argc, char **argv){
       
  1080 
       
  1081     QApplication app(argc, argv);
       
  1082 
       
  1083     tst_Contact test1;
       
  1084     QTest::qExec(&test1, argc, argv);
       
  1085 
       
  1086 }
       
  1087 
       
  1088 #include "tst_bm_contacts.moc"
       
  1089