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