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