qtmobility/plugins/contacts/symbian/tsrc/tst_qcontactmanagersymbian/tst_qcontactmanagersymbian.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
--- a/qtmobility/plugins/contacts/symbian/tsrc/tst_qcontactmanagersymbian/tst_qcontactmanagersymbian.cpp	Fri Jun 11 14:26:25 2010 +0300
+++ b/qtmobility/plugins/contacts/symbian/tsrc/tst_qcontactmanagersymbian/tst_qcontactmanagersymbian.cpp	Wed Jun 23 19:08:38 2010 +0300
@@ -68,7 +68,9 @@
     QTRY_COMPARE(spyContactsRemoved.count(), contactsRemoved); \
     QTRY_COMPARE(spyRelationshipsAdded.count(), relationshipsAdded); \
     QTRY_COMPARE(spyRelationshipsRemoved.count(), relationshipsRemoved); \
-    QTRY_COMPARE(spySelfContactIdChanged.count(), selfContactIdChanged); \
+    QTRY_COMPARE(spySelfContactIdChanged.count(), selfContactIdChanged);
+
+#define QTRY_COMPARE_SIGNAL_COUNTS2() \
     QTRY_COMPARE(spyContactsAdded2.count(), contactsAdded); \
     QTRY_COMPARE(spyContactsChanged2.count(), contactsChanged); \
     QTRY_COMPARE(spyContactsRemoved2.count(), contactsRemoved); \
@@ -101,6 +103,7 @@
 
 private slots:
     void signalEmission();
+    void signalEmissionWithContactModel();
     void filtering();
     void avatarImage();
     void avatarImage_data();
@@ -110,6 +113,8 @@
     void displayLabel_data();
     void displayLabel();
     void timestamp();
+    void onlineAccount_data();
+    void onlineAccount();
     void invalidContactItems();
 
 private:
@@ -152,7 +157,8 @@
 {
     // If the following is commented out => the generated contacts are left into
     // the database
-    QVERIFY(m_cm->removeContact(m_contactId.localId()));
+    QList<QContactLocalId> ids = m_cm->contactIds();
+    QVERIFY(m_cm->removeContacts(ids, 0));
 }
 
 void tst_QContactManagerSymbian::signalEmission()
@@ -191,6 +197,7 @@
     QVERIFY(m_cm->saveContact(&group));
     contactsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // change the group
     QContactName name = group.detail(QContactName::DefinitionName);
@@ -199,27 +206,32 @@
     QVERIFY(m_cm->saveContact(&group));
     contactsChanged++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // remove the group
     QVERIFY(m_cm->removeContact(group.localId()));
     contactsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // Add two contacts
     QContact contact1 = createContact(QContactType::TypeContact, "James", "Hunt");
     QVERIFY(m_cm->saveContact(&contact1));
     contactsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QContact contact2 = createContact(QContactType::TypeContact, "Jochen", "Mass");
     QVERIFY(m_cm->saveContact(&contact2));
     contactsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // Add group 2
     QContact group2 = createContact(QContactType::TypeGroup, "McLaren", "");
     QVERIFY(m_cm->saveContact(&group2));
     contactsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // Add a relationship
     QContactRelationship r;
@@ -229,6 +241,7 @@
     QVERIFY(m_cm->saveRelationship(&r));
     relationshipsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // Create one more contact manager instance
     QScopedPointer<QContactManager> cm3(QContactManager::fromUri("qtcontacts:symbian"));
@@ -243,11 +256,13 @@
     QVERIFY(m_cm->saveRelationship(&r2));
     relationshipsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
 
     // Remove relationship 1
     QVERIFY(m_cm->removeRelationship(r));
     relationshipsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QTRY_COMPARE(spyRelationshipsAdded3.count(), 1);
     QTRY_COMPARE(spyRelationshipsRemoved3.count(), 1);
 
@@ -255,6 +270,7 @@
     QVERIFY(m_cm->removeRelationship(r2));
     relationshipsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QTRY_COMPARE(spyRelationshipsAdded3.count(), 1);
     QTRY_COMPARE(spyRelationshipsRemoved3.count(), 2);
 
@@ -262,25 +278,172 @@
     QVERIFY(m_cm->removeContact(contact1.localId()));
     contactsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QVERIFY(m_cm->removeContact(contact2.localId()));
     contactsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QVERIFY(m_cm->removeContact(group2.localId()));
     contactsRemoved++;
     QTRY_COMPARE_SIGNAL_COUNTS();
-
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    
+    // create a group + a contact and create a relationship3 
+    QContact group3 = createContact(QContactType::TypeGroup, "Outlaws", "");
+    QVERIFY(m_cm->saveContact(&group3));
+    contactsAdded++;
+    QContact contact3 = createContact(QContactType::TypeContact, "Jesse", "James");
+    QVERIFY(m_cm->saveContact(&contact3));
+    contactsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    QContactRelationship r3;
+    r3.setFirst(group3.id());
+    r3.setSecond(contact3.id());
+    r3.setRelationshipType(QContactRelationship::HasMember);
+    QVERIFY(m_cm->saveRelationship(&r3));
+    relationshipsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    
+    // Remove contact from relationship3
+    QVERIFY(m_cm->removeContact(contact3.localId()));
+    contactsRemoved++;
+    relationshipsRemoved++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    
+    // Restore contact and relationship3
+    contact3.setId(QContactId());
+    QVERIFY(m_cm->saveContact(&contact3));
+    contactsAdded++;
+    r3.setSecond(contact3.id());
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    QVERIFY(m_cm->saveRelationship(&r3));
+    relationshipsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    
+    // Remove group from relationship3
+    QVERIFY(m_cm->removeContact(group3.localId()));
+    contactsRemoved++;
+    relationshipsRemoved++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+    
     // Self contact
     QContact memyself = createContact(QContactType::TypeContact, "Kimi", "Raikkonen");
     QVERIFY(m_cm->saveContact(&memyself));
     contactsAdded++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
     QVERIFY(m_cm->setSelfContactId(memyself.localId()));
     selfContactIdChanged++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+
+    // Modify self contact
+    QContactName myname = memyself.detail(QContactName::DefinitionName);
+    myname.setFirstName("Iceman");
+    memyself.saveDetail(&myname);
+    QVERIFY(m_cm->saveContact(&memyself));
+    contactsChanged++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+
+    // Remove self contact
     QVERIFY(m_cm->removeContact(memyself.localId()));
     contactsRemoved++;
     selfContactIdChanged++;
     QTRY_COMPARE_SIGNAL_COUNTS();
+    QTRY_COMPARE_SIGNAL_COUNTS2();
+}
+
+void tst_QContactManagerSymbian::signalEmissionWithContactModel()
+{
+    // Wait a moment to make sure there are no pending database observer events
+    QTest::qWait(500);
+
+    // counters to keep track of the expected signal counts
+    int contactsAdded(0);
+    int contactsChanged(0);
+    int contactsRemoved(0);
+    int relationshipsAdded(0);
+    int relationshipsRemoved(0);
+    int selfContactIdChanged(0);
+
+    // Signal spys for verifying signal emissions
+    qRegisterMetaType<QContactLocalId>("QContactLocalId");
+    qRegisterMetaType<QList<QContactLocalId> >("QList<QContactLocalId>");
+    QSignalSpy spyContactsAdded(m_cm, SIGNAL(contactsAdded(QList<QContactLocalId>)));
+    QSignalSpy spyContactsChanged(m_cm, SIGNAL(contactsChanged(QList<QContactLocalId>)));
+    QSignalSpy spyContactsRemoved(m_cm, SIGNAL(contactsRemoved(QList<QContactLocalId>)));
+    QSignalSpy spyRelationshipsAdded(m_cm, SIGNAL(relationshipsAdded(QList<QContactLocalId>)));
+    QSignalSpy spyRelationshipsRemoved(m_cm, SIGNAL(relationshipsRemoved(QList<QContactLocalId>)));
+    QSignalSpy spySelfContactIdChanged(m_cm, SIGNAL(selfContactIdChanged(QContactLocalId, QContactLocalId)));
+
+    // Add a contact via Qt API and remove it via CntModel API
+    QContact contact1 = createContact(QContactType::TypeContact, "Kimi", "Räikkönen");
+    QVERIFY(m_cm->saveContact(&contact1));
+    contactsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    m_contactDatabase->DeleteContactL(contact1.localId());
+    contactsRemoved++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+
+    // Add a contact, a group and a relationship between those via
+    // CntModel API and verify signal emissions
+    CContactCard* contactCard = CContactCard::NewL();
+    CleanupStack::PushL(contactCard);
+    m_contactDatabase->AddNewContactL(*contactCard);
+    contactsAdded++;
+    CContactItem *groupItem = m_contactDatabase->CreateContactGroupLC();
+    contactsAdded++;
+    m_contactDatabase->AddContactToGroupL(contactCard->Id(), groupItem->Id());
+    relationshipsAdded++;
+    CleanupStack::PopAndDestroy(groupItem);
+    CleanupStack::PopAndDestroy(contactCard);
+    QTRY_COMPARE_SIGNAL_COUNTS();
+
+    // Add a contact, a group and a relationship between those via Qt API
+    // and then remove the contact via contact database API
+    QContact contact2 = createContact(QContactType::TypeContact, "Kimi", "Räikkönen");
+    QVERIFY(m_cm->saveContact(&contact2));
+    contactsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QContact group = createContact(QContactType::TypeGroup, "Reb Bull", "");
+    QVERIFY(m_cm->saveContact(&group));
+    contactsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    QContactRelationship r;
+    r.setFirst(group.id());
+    r.setSecond(contact2.id());
+    r.setRelationshipType(QContactRelationship::HasMember);
+    QVERIFY(m_cm->saveRelationship(&r));
+    relationshipsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    m_contactDatabase->DeleteContactL(contact2.localId());
+    contactsRemoved++;
+    relationshipsRemoved++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    
+    // restore contact and relationship
+    contact2.setId(QContactId());
+    QVERIFY(m_cm->saveContact(&contact2));
+    contactsAdded++;
+    r.setSecond(contact2.id());
+    QVERIFY(m_cm->saveRelationship(&r));
+    relationshipsAdded++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+    
+    // Remove group from relationship
+    m_contactDatabase->DeleteContactL(group.localId());
+    contactsRemoved++;
+    relationshipsRemoved++;
+    QTRY_COMPARE_SIGNAL_COUNTS();
+	
+   // TODO: Create a group which has members already
 }
 
 /*
@@ -453,35 +616,44 @@
 void tst_QContactManagerSymbian::displayLabel_data()
 {
     // Expected display label
+    QTest::addColumn<QString>("contactType");
     QTest::addColumn<QString>("displayLabel");
     // A string list containing the detail fields in format <detail definition name>:<field name>:<value>
     // For example first name: Name:First:James
     // Note: With the current implementation the value must not contain a ':' character
     QTest::addColumn<QStringList>("details");
 
+    QString typeContact = QContactType::TypeContact;
+    QString typeGroup = QContactType::TypeGroup;
+
     QTest::newRow("first name")
+        << typeContact
         << "James"
         << (QStringList()
             << "Name:FirstName:James");
 
     QTest::newRow("last name")
+        << typeContact
         << "Hunt"
         << (QStringList()
             << "Name:LastName:Hunt");
 
     QTest::newRow("first and last name") // fail
+        << typeContact
         << "James Hunt"
         << (QStringList()
             << "Name:FirstName:James"
             << "Name:LastName:Hunt");
 
     QTest::newRow("multi-part first name and last name") // fail
+        << typeContact
         << "James Simon Wallis Hunt"
         << (QStringList()
             << "Name:FirstName:James Simon Wallis"
             << "Name:LastName:Hunt");
 
     QTest::newRow("all names")
+        << typeContact
         << "James Hunt"
         << (QStringList()
             << "Name:FirstName:James"
@@ -491,18 +663,21 @@
             << "Name:Prefix:Pre");
 
     QTest::newRow("first name, organization")
+        << typeContact
         << "James"
         << (QStringList()
             << "Name:FirstName:James"
             << "Organization:Name:McLaren");
 
     QTest::newRow("last name, organization")
+        << typeContact
         << "Hunt"
         << (QStringList()
             << "Name:LastName:Hunt"
             << "Organization:Name:McLaren");
 
     QTest::newRow("first name, last name, organization")
+        << typeContact
         << "James Hunt"
         << (QStringList()
             << "Name:FirstName:James"
@@ -510,9 +685,38 @@
             << "Organization:Name:McLaren");
 
     QTest::newRow("organization")
+        << typeContact
         << "McLaren"
         << (QStringList()
             << "Organization:Name:McLaren");
+
+    QTest::newRow("nick name")
+        << typeContact
+        << ""
+        << (QStringList()
+            << "Nickname:Nickname:The Shunt");
+
+    QTest::newRow("phone number")
+        << typeContact
+        << ""
+        << (QStringList()
+            << "PhoneNumber:PhoneNumber:+44759999999");
+
+    QTest::newRow("no details")
+        << typeContact
+        << ""
+        << QStringList();
+
+    QTest::newRow("group, custom label")
+        << typeGroup
+        << "McLaren"
+        << (QStringList()
+            << "Name:CustomLabel:McLaren");
+
+    QTest::newRow("group, no details")
+        << typeGroup
+        << ""
+        << QStringList();
 }
 
 /*
@@ -521,12 +725,17 @@
  */
 void tst_QContactManagerSymbian::displayLabel()
 {
-    qDebug() << QTest::currentDataTag();
+    //qDebug() << QTest::currentDataTag();
+    QFETCH(QString, contactType);
     QFETCH(QString, displayLabel);
     QFETCH(QStringList, details);
 
+    QContact contact;
+    if (contactType == QContactType::TypeGroup) {
+        contact.setType(QContactType::TypeGroup);
+    }
+
     // Parse details and add them to the contact
-    QContact contact;
     foreach(const QString& detail, details) {
         // the expected format is <detail definition name>:<field name>:<value>
         QStringList detailParts = detail.split(QChar(':'), QString::KeepEmptyParts, Qt::CaseSensitive);
@@ -556,6 +765,12 @@
 
 void tst_QContactManagerSymbian::timestamp()
 {
+    if (!m_cm->detailDefinitions().keys().contains(QContactTimestamp::DefinitionName)) {
+        // S60 3.1 does not support timestamps because cntmodel API for
+        // that is not exported.
+        QSKIP("Backend does not support timestamp!", SkipSingle);
+    }
+    
     // Save a contact
     QContact contact = createContact(QContactType::TypeContact, "Jame", "Hunt");
     QVERIFY(m_cm->saveContact(&contact));
@@ -582,6 +797,51 @@
     QVERIFY(m_cm->removeContact(contact.localId()));
 }
 
+void tst_QContactManagerSymbian::onlineAccount_data()
+{
+    QTest::addColumn<QString>("accountUri");
+    QTest::addColumn<QStringList>("subTypes");
+
+    QTest::newRow("SubTypeSip")
+        << QString("+44728888888")
+        << (QStringList()
+            << QContactOnlineAccount::SubTypeSip);
+    
+    QTest::newRow("SubTypeSipVoip")
+        << QString("+44727777777")
+        << (QStringList()
+            << QContactOnlineAccount::SubTypeSipVoip);
+
+    QTest::newRow("SubTypeVideoShare")
+        << QString("+44726666666")
+        << (QStringList()
+            << QContactOnlineAccount::SubTypeVideoShare);
+}
+
+void tst_QContactManagerSymbian::onlineAccount()
+{
+    QFETCH(QString, accountUri);
+    QFETCH(QStringList, subTypes);
+
+    // Save a contact
+    QContact contact = createContact(QContactType::TypeContact, "James", "Hunt");
+    QVERIFY(m_cm->saveContact(&contact));
+    QContactOnlineAccount onlineAccount;
+    onlineAccount.setAccountUri(accountUri);
+    onlineAccount.setSubTypes(subTypes);
+    contact.saveDetail(&onlineAccount);
+    QVERIFY(m_cm->saveContact(&contact));
+
+    // verify by reading the saved contact and comparing online account data
+    QContact retrievedContact = m_cm->contact(contact.localId());
+    QContactOnlineAccount retrievedOnlineAccount = contact.detail(QContactOnlineAccount::DefinitionName);
+    QCOMPARE(retrievedOnlineAccount.accountUri(), accountUri);
+    QCOMPARE(retrievedOnlineAccount.subTypes(), subTypes);
+
+    // Remove contact
+    QVERIFY(m_cm->removeContact(contact.localId()));
+}
+
 /*
  * Special contact handling test cases that cannot be covered in QtMobility
  * system level test cases.