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