123 void dumpContacts(QContactManager *cm); |
123 void dumpContacts(QContactManager *cm); |
124 bool isSuperset(const QContact& ca, const QContact& cb); |
124 bool isSuperset(const QContact& ca, const QContact& cb); |
125 QList<QContactDetail> removeAllDefaultDetails(const QList<QContactDetail>& details); |
125 QList<QContactDetail> removeAllDefaultDetails(const QList<QContactDetail>& details); |
126 void addManagers(); // add standard managers to the data |
126 void addManagers(); // add standard managers to the data |
127 QContact createContact(QContactDetailDefinition nameDef, QString firstName, QString lastName, QString phoneNumber); |
127 QContact createContact(QContactDetailDefinition nameDef, QString firstName, QString lastName, QString phoneNumber); |
128 void setContactName(QContactDetailDefinition nameDef, QContactName& contactName, const QString &name) const; |
128 void saveContactName(QContact *contact, QContactDetailDefinition nameDef, QContactName *contactName, const QString &name) const; |
129 |
129 |
130 QContactManagerDataHolder managerDataHolder; |
130 QContactManagerDataHolder managerDataHolder; |
131 |
131 |
132 public slots: |
132 public slots: |
133 void init(); |
133 void init(); |
431 } |
431 } |
432 |
432 |
433 return contact; |
433 return contact; |
434 } |
434 } |
435 |
435 |
436 void tst_QContactManager::setContactName(QContactDetailDefinition nameDef, QContactName& contactName, const QString &name) const |
436 void tst_QContactManager::saveContactName(QContact *contact, QContactDetailDefinition nameDef, QContactName *contactName, const QString &name) const |
437 { |
437 { |
438 // check which name fields are supported in the following order: |
438 // check which name fields are supported in the following order: |
439 // 1. custom label, 2. first name, 3. last name |
439 // 1. custom label, 2. first name, 3. last name |
440 if(nameDef.fields().contains(QContactName::FieldCustomLabel)) { |
440 if(nameDef.fields().contains(QContactName::FieldCustomLabel)) { |
441 contactName.setCustomLabel(name); |
441 contactName->setCustomLabel(name); |
442 } else if(nameDef.fields().contains(QContactName::FieldFirstName)) { |
442 } else if(nameDef.fields().contains(QContactName::FieldFirstName)) { |
443 contactName.setFirstName(name); |
443 contactName->setFirstName(name); |
444 } else if(nameDef.fields().contains(QContactName::FieldLastName)) { |
444 } else if(nameDef.fields().contains(QContactName::FieldLastName)) { |
445 contactName.setLastName(name); |
445 contactName->setLastName(name); |
446 } else { |
446 } else { |
447 // Assume that at least one of the above name fields is supported by the backend |
447 // Assume that at least one of the above name fields is supported by the backend |
448 QVERIFY(false); |
448 QVERIFY(false); |
449 } |
449 } |
|
450 contact->saveDetail(contactName); |
450 } |
451 } |
451 |
452 |
452 void tst_QContactManager::metadata() |
453 void tst_QContactManager::metadata() |
453 { |
454 { |
454 // ensure that the backend is publishing its metadata (name / parameters / uri) correctly |
455 // ensure that the backend is publishing its metadata (name / parameters / uri) correctly |
706 QCOMPARE(cm->contactIds().count(), currCount+1); |
707 QCOMPARE(cm->contactIds().count(), currCount+1); |
707 |
708 |
708 QContact added = cm->contact(alice.id().localId()); |
709 QContact added = cm->contact(alice.id().localId()); |
709 QVERIFY(added.id() != QContactId()); |
710 QVERIFY(added.id() != QContactId()); |
710 QVERIFY(added.id() == alice.id()); |
711 QVERIFY(added.id() == alice.id()); |
711 |
712 |
712 if (!isSuperset(added, alice)) { |
713 if (!isSuperset(added, alice)) { |
713 dumpContacts(cm.data()); |
714 dumpContacts(cm.data()); |
714 dumpContactDifferences(added, alice); |
715 dumpContactDifferences(added, alice); |
715 QCOMPARE(added, alice); |
716 QCOMPARE(added, alice); |
716 } |
717 } |
717 |
718 |
718 // now try adding a contact that does not exist in the database with non-zero id |
719 // now try adding a contact that does not exist in the database with non-zero id |
719 QContact nonexistent = createContact(nameDef, "nonexistent", "contact", ""); |
720 if (cm->managerName() == "symbiansim") { |
720 QVERIFY(cm->saveContact(&nonexistent)); // should work |
721 // TODO: symbiansim backend fails this test currently. Will be fixed later. |
721 QVERIFY(cm->removeContact(nonexistent.id().localId())); // now nonexistent has an id which does not exist |
722 QWARN("This manager has a known issue with saving a non-zero id contact. Skipping this test step."); |
722 QVERIFY(!cm->saveContact(&nonexistent)); // hence, should fail |
723 } else { |
723 QCOMPARE(cm->error(), QContactManager::DoesNotExistError); |
724 QContact nonexistent = createContact(nameDef, "nonexistent", "contact", ""); |
724 nonexistent.setId(QContactId()); |
725 QVERIFY(cm->saveContact(&nonexistent)); // should work |
725 QVERIFY(cm->saveContact(&nonexistent)); // after setting id to zero, should save |
726 QVERIFY(cm->removeContact(nonexistent.id().localId())); // now nonexistent has an id which does not exist |
726 QVERIFY(cm->removeContact(nonexistent.id().localId())); |
727 QVERIFY(!cm->saveContact(&nonexistent)); // hence, should fail |
|
728 QCOMPARE(cm->error(), QContactManager::DoesNotExistError); |
|
729 nonexistent.setId(QContactId()); |
|
730 QVERIFY(cm->saveContact(&nonexistent)); // after setting id to zero, should save |
|
731 QVERIFY(cm->removeContact(nonexistent.id().localId())); |
|
732 } |
727 |
733 |
728 // now try adding a "megacontact" |
734 // now try adding a "megacontact" |
729 // - get list of all definitions supported by the manager |
735 // - get list of all definitions supported by the manager |
730 // - add one detail of each definition to a contact |
736 // - add one detail of each definition to a contact |
731 // - save the contact |
737 // - save the contact |
735 QMap<QString, QContactDetailDefinition> defmap = cm->detailDefinitions(); |
741 QMap<QString, QContactDetailDefinition> defmap = cm->detailDefinitions(); |
736 QList<QContactDetailDefinition> defs = defmap.values(); |
742 QList<QContactDetailDefinition> defs = defmap.values(); |
737 foreach (const QContactDetailDefinition def, defs) { |
743 foreach (const QContactDetailDefinition def, defs) { |
738 |
744 |
739 // Leave these warnings here - might need an API for this |
745 // Leave these warnings here - might need an API for this |
740 if (def.accessConstraint() == QContactDetailDefinition::ReadOnly) { |
746 // XXX FIXME: access constraint reporting as moved to the detail itself |
741 continue; |
747 //if (def.accessConstraint() == QContactDetailDefinition::ReadOnly) { |
742 } |
748 // continue; |
|
749 //} |
743 |
750 |
744 // otherwise, create a new detail of the given type and save it to the contact |
751 // otherwise, create a new detail of the given type and save it to the contact |
745 QContactDetail det(def.name()); |
752 QContactDetail det(def.name()); |
746 QMap<QString, QContactDetailFieldDefinition> fieldmap = def.fields(); |
753 QMap<QString, QContactDetailFieldDefinition> fieldmap = def.fields(); |
747 QStringList fieldKeys = fieldmap.keys(); |
754 QStringList fieldKeys = fieldmap.keys(); |
748 foreach (const QString& fieldKey, fieldKeys) { |
755 foreach (const QString& fieldKey, fieldKeys) { |
749 // get the field, and check to see that it's not constrained. |
756 // get the field, and check to see that it's not constrained. |
750 QContactDetailFieldDefinition currentField = fieldmap.value(fieldKey); |
757 QContactDetailFieldDefinition currentField = fieldmap.value(fieldKey); |
|
758 |
|
759 // Special case: phone number. |
|
760 if (def.name() == QContactPhoneNumber::DefinitionName && |
|
761 fieldKey == QContactPhoneNumber::FieldNumber) { |
|
762 det.setValue(fieldKey, "+3581234567890"); |
|
763 continue; |
|
764 } |
751 |
765 |
752 // Attempt to create a worthy value |
766 // Attempt to create a worthy value |
753 if (!currentField.allowableValues().isEmpty()) { |
767 if (!currentField.allowableValues().isEmpty()) { |
754 // we want to save a value that will be accepted. |
768 // we want to save a value that will be accepted. |
755 if (currentField.dataType() == QVariant::StringList) |
769 if (currentField.dataType() == QVariant::StringList) |
806 QEXPECT_FAIL("mgr='wince'", "Address Display Label mismatch", Continue); |
820 QEXPECT_FAIL("mgr='wince'", "Address Display Label mismatch", Continue); |
807 QCOMPARE(megacontact, retrievedMegacontact); |
821 QCOMPARE(megacontact, retrievedMegacontact); |
808 } |
822 } |
809 |
823 |
810 // now a contact with many details of a particular definition |
824 // now a contact with many details of a particular definition |
811 // this will fail on some backends; how do we query for this capability? |
825 // if the detail is not unique it should then support minumum of two of the same kind |
|
826 const int nrOfdetails = 2; |
812 QContact veryContactable = createContact(nameDef, "Very", "Contactable", ""); |
827 QContact veryContactable = createContact(nameDef, "Very", "Contactable", ""); |
813 for (int i = 0; i < 50; i++) { |
828 for (int i = 0; i < nrOfdetails; i++) { |
814 QString phnStr = QString::number(i); |
829 QString phnStr = QString::number(i); |
815 QContactPhoneNumber vcphn; |
830 QContactPhoneNumber vcphn; |
816 vcphn.setNumber(phnStr); |
831 vcphn.setNumber(phnStr); |
817 QVERIFY(veryContactable.saveDetail(&vcphn)); |
832 QVERIFY(veryContactable.saveDetail(&vcphn)); |
818 } |
833 } |
819 |
834 |
820 // check that all the numbers were added successfully, and that it can be saved. |
835 // check that all the numbers were added successfully |
821 QVERIFY(veryContactable.details(QContactPhoneNumber::DefinitionName).size() == 50); |
836 QVERIFY(veryContactable.details(QContactPhoneNumber::DefinitionName).size() == nrOfdetails); |
822 QVERIFY(cm->saveContact(&veryContactable)); |
837 |
823 QContact retrievedContactable = cm->contact(veryContactable.id().localId()); |
838 // check if it can be saved |
824 if (retrievedContactable != veryContactable) { |
839 QContactDetailDefinition def = cm->detailDefinition(QContactPhoneNumber::DefinitionName); |
825 dumpContactDifferences(veryContactable, retrievedContactable); |
840 if (def.isUnique()) { |
826 QEXPECT_FAIL("mgr='wince'", "Number of phones supported mismatch", Continue); |
841 QVERIFY(!cm->saveContact(&veryContactable)); |
827 QCOMPARE(veryContactable, retrievedContactable); |
842 } |
|
843 else { |
|
844 QVERIFY(cm->saveContact(&veryContactable)); |
|
845 |
|
846 // verify save |
|
847 QContact retrievedContactable = cm->contact(veryContactable.id().localId()); |
|
848 if (retrievedContactable != veryContactable) { |
|
849 dumpContactDifferences(veryContactable, retrievedContactable); |
|
850 QEXPECT_FAIL("mgr='wince'", "Number of phones supported mismatch", Continue); |
|
851 QCOMPARE(veryContactable, retrievedContactable); |
|
852 } |
828 } |
853 } |
829 } |
854 } |
830 |
855 |
831 void tst_QContactManager::update() |
856 void tst_QContactManager::update() |
832 { |
857 { |
839 QVERIFY(cm->saveContact(&alice)); |
864 QVERIFY(cm->saveContact(&alice)); |
840 QVERIFY(cm->error() == QContactManager::NoError); |
865 QVERIFY(cm->error() == QContactManager::NoError); |
841 |
866 |
842 /* Update name */ |
867 /* Update name */ |
843 QContactName name = alice.detail(QContactName::DefinitionName); |
868 QContactName name = alice.detail(QContactName::DefinitionName); |
844 setContactName(nameDef, name, "updated"); |
869 saveContactName(&alice, nameDef, &name, "updated"); |
845 alice.saveDetail(&name); |
870 QVERIFY(cm->saveContact(&alice)); |
|
871 QVERIFY(cm->error() == QContactManager::NoError); |
|
872 saveContactName(&alice, nameDef, &name, "updated2"); |
846 QVERIFY(cm->saveContact(&alice)); |
873 QVERIFY(cm->saveContact(&alice)); |
847 QVERIFY(cm->error() == QContactManager::NoError); |
874 QVERIFY(cm->error() == QContactManager::NoError); |
848 QContact updated = cm->contact(alice.localId()); |
875 QContact updated = cm->contact(alice.localId()); |
849 QContactName updatedName = updated.detail(QContactName::DefinitionName); |
876 QContactName updatedName = updated.detail(QContactName::DefinitionName); |
850 QCOMPARE(updatedName, name); |
877 QCOMPARE(updatedName, name); |
892 { |
919 { |
893 QFETCH(QString, uri); |
920 QFETCH(QString, uri); |
894 QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
921 QScopedPointer<QContactManager> cm(QContactManager::fromUri(uri)); |
895 |
922 |
896 /* First test null pointer operations */ |
923 /* First test null pointer operations */ |
897 QVERIFY(!cm->saveContacts(0, 0)); |
924 QVERIFY(!cm->saveContacts(NULL, NULL)); |
898 QVERIFY(cm->error() == QContactManager::BadArgumentError); |
925 QVERIFY(cm->error() == QContactManager::BadArgumentError); |
899 |
926 |
900 QVERIFY(!cm->removeContacts(0, 0)); |
927 QVERIFY(!cm->removeContacts(QList<QContactLocalId>(), NULL)); |
901 QVERIFY(cm->error() == QContactManager::BadArgumentError); |
928 QVERIFY(cm->error() == QContactManager::BadArgumentError); |
|
929 |
|
930 // Get supported name field |
|
931 QString nameField = QContactName::FieldFirstName; |
|
932 QContactDetailDefinition def = cm->detailDefinition(QContactName::DefinitionName); |
|
933 if (!def.fields().contains(QContactName::FieldFirstName)) { |
|
934 if(def.fields().contains(QContactName::FieldCustomLabel)) |
|
935 nameField = QLatin1String(QContactName::FieldCustomLabel); |
|
936 else |
|
937 QSKIP("This backend does not support the required name field!", SkipSingle); |
|
938 } |
902 |
939 |
903 /* Now add 3 contacts, all valid */ |
940 /* Now add 3 contacts, all valid */ |
904 QContact a; |
941 QContact a; |
905 QContactName na; |
942 QContactName na; |
906 na.setFirstName("XXXXXX Albert"); |
943 na.setValue(nameField, "XXXXXX Albert"); |
907 a.saveDetail(&na); |
944 a.saveDetail(&na); |
908 |
945 |
909 QContact b; |
946 QContact b; |
910 QContactName nb; |
947 QContactName nb; |
911 nb.setFirstName("XXXXXX Bob"); |
948 nb.setValue(nameField, "XXXXXX Bob"); |
912 b.saveDetail(&nb); |
949 b.saveDetail(&nb); |
913 |
950 |
914 QContact c; |
951 QContact c; |
915 QContactName nc; |
952 QContactName nc; |
916 nc.setFirstName("XXXXXX Carol"); |
953 nc.setValue(nameField, "XXXXXX Carol"); |
917 c.saveDetail(&nc); |
954 c.saveDetail(&nc); |
918 |
955 |
919 QList<QContact> contacts; |
956 QList<QContact> contacts; |
920 contacts << a << b << c; |
957 contacts << a << b << c; |
921 |
958 |
974 QVERIFY(b.details<QContactPhoneNumber>().at(0).number() == "234567"); |
1011 QVERIFY(b.details<QContactPhoneNumber>().at(0).number() == "234567"); |
975 QVERIFY(c.details<QContactPhoneNumber>().at(0).number() == "34567"); |
1012 QVERIFY(c.details<QContactPhoneNumber>().at(0).number() == "34567"); |
976 |
1013 |
977 /* Now delete them all */ |
1014 /* Now delete them all */ |
978 QList<QContactLocalId> ids; |
1015 QList<QContactLocalId> ids; |
979 QContactLocalId removedIdForLater = b.id().localId(); |
|
980 ids << a.id().localId() << b.id().localId() << c.id().localId(); |
1016 ids << a.id().localId() << b.id().localId() << c.id().localId(); |
981 QVERIFY(cm->removeContacts(&ids, &errorMap)); |
1017 QVERIFY(cm->removeContacts(ids, &errorMap)); |
982 QVERIFY(errorMap.count() == 0); |
1018 QVERIFY(errorMap.count() == 0); |
983 QVERIFY(cm->error() == QContactManager::NoError); |
1019 QVERIFY(cm->error() == QContactManager::NoError); |
984 |
|
985 /* Make sure all the ids are now 0 */ |
|
986 QVERIFY(ids.count() == 3); |
|
987 QVERIFY(ids.at(0) == 0); |
|
988 QVERIFY(ids.at(1) == 0); |
|
989 QVERIFY(ids.at(2) == 0); |
|
990 |
1020 |
991 /* Make sure the contacts really don't exist any more */ |
1021 /* Make sure the contacts really don't exist any more */ |
992 QVERIFY(cm->contact(a.id().localId()).id() == QContactId()); |
1022 QVERIFY(cm->contact(a.id().localId()).id() == QContactId()); |
993 QVERIFY(cm->contact(a.id().localId()).isEmpty()); |
1023 QVERIFY(cm->contact(a.id().localId()).isEmpty()); |
994 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1024 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1000 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1030 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1001 |
1031 |
1002 /* Now try removing with all invalid ids (e.g. the ones we just removed) */ |
1032 /* Now try removing with all invalid ids (e.g. the ones we just removed) */ |
1003 ids.clear(); |
1033 ids.clear(); |
1004 ids << a.id().localId() << b.id().localId() << c.id().localId(); |
1034 ids << a.id().localId() << b.id().localId() << c.id().localId(); |
1005 QVERIFY(!cm->removeContacts(&ids, &errorMap)); |
1035 QVERIFY(!cm->removeContacts(ids, &errorMap)); |
1006 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1036 QVERIFY(cm->error() == QContactManager::DoesNotExistError); |
1007 QVERIFY(errorMap.count() == 3); |
1037 QVERIFY(errorMap.count() == 3); |
1008 QVERIFY(errorMap.values().at(0) == QContactManager::DoesNotExistError); |
1038 QVERIFY(errorMap.values().at(0) == QContactManager::DoesNotExistError); |
1009 QVERIFY(errorMap.values().at(1) == QContactManager::DoesNotExistError); |
1039 QVERIFY(errorMap.values().at(1) == QContactManager::DoesNotExistError); |
1010 QVERIFY(errorMap.values().at(2) == QContactManager::DoesNotExistError); |
1040 QVERIFY(errorMap.values().at(2) == QContactManager::DoesNotExistError); |
1052 /* Fix up B and re save it */ |
1082 /* Fix up B and re save it */ |
1053 QVERIFY(contacts[1].removeDetail(&bad)); |
1083 QVERIFY(contacts[1].removeDetail(&bad)); |
1054 QVERIFY(cm->saveContacts(&contacts, &errorMap)); |
1084 QVERIFY(cm->saveContacts(&contacts, &errorMap)); |
1055 QVERIFY(errorMap.count() == 0); |
1085 QVERIFY(errorMap.count() == 0); |
1056 QVERIFY(cm->error() == QContactManager::NoError); |
1086 QVERIFY(cm->error() == QContactManager::NoError); |
|
1087 |
|
1088 // Save and remove a fourth contact. Store the id. |
|
1089 a.setId(QContactId()); |
|
1090 QVERIFY(cm->saveContact(&a)); |
|
1091 QContactLocalId removedId = a.localId(); |
|
1092 QVERIFY(cm->removeContact(removedId)); |
1057 |
1093 |
1058 /* Now delete 3 items, but with one bad argument */ |
1094 /* Now delete 3 items, but with one bad argument */ |
1059 ids.clear(); |
1095 ids.clear(); |
1060 ids << contacts.at(0).id().localId(); |
1096 ids << contacts.at(0).id().localId(); |
1061 ids << removedIdForLater; |
1097 ids << removedId; |
1062 ids << contacts.at(2).id().localId(); |
1098 ids << contacts.at(2).id().localId(); |
1063 |
1099 |
1064 QVERIFY(!cm->removeContacts(&ids, &errorMap)); |
1100 QVERIFY(!cm->removeContacts(ids, &errorMap)); |
1065 QVERIFY(cm->error() != QContactManager::NoError); |
1101 QVERIFY(cm->error() != QContactManager::NoError); |
1066 |
1102 |
1067 /* Again, the backend has the choice of either removing the successful ones, or not */ |
1103 /* Again, the backend has the choice of either removing the successful ones, or not */ |
1068 QVERIFY(errorMap.count() > 0); |
1104 QVERIFY(errorMap.count() > 0); |
1069 QVERIFY(errorMap.count() <= 3); |
1105 QVERIFY(errorMap.count() <= 3); |
1149 |
1185 |
1150 QList<QContact> list; |
1186 QList<QContact> list; |
1151 list << foo; |
1187 list << foo; |
1152 |
1188 |
1153 QVERIFY(!manager.saveContacts(&list, &errorMap)); |
1189 QVERIFY(!manager.saveContacts(&list, &errorMap)); |
1154 QVERIFY(errorMap.count() == 1); |
1190 QVERIFY(errorMap.count() == 0); |
1155 QVERIFY(errorMap.value(0) == QContactManager::NotSupportedError); |
|
1156 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1191 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1157 |
1192 |
1158 QVERIFY(!manager.removeContacts(0, &errorMap)); |
1193 QVERIFY(!manager.removeContacts(QList<QContactLocalId>(), &errorMap)); |
1159 QVERIFY(errorMap.count() == 0); |
1194 QVERIFY(errorMap.count() == 0); |
1160 QVERIFY(manager.error() == QContactManager::BadArgumentError); |
1195 QVERIFY(manager.error() == QContactManager::BadArgumentError); |
1161 |
1196 |
1162 QList<QContactLocalId> idlist; |
1197 QList<QContactLocalId> idlist; |
1163 idlist << foo.id().localId(); |
1198 idlist << foo.id().localId(); |
1164 QVERIFY(!manager.removeContacts(&idlist, &errorMap)); |
1199 QVERIFY(!manager.removeContacts(idlist, &errorMap)); |
1165 QVERIFY(errorMap.count() == 1); |
1200 QVERIFY(errorMap.count() == 0); |
1166 QVERIFY(errorMap.value(0) == QContactManager::NotSupportedError); |
|
1167 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1201 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1168 |
1202 |
1169 /* Detail definitions */ |
1203 /* Detail definitions */ |
1170 QVERIFY(manager.detailDefinitions().count() == 0); |
1204 QVERIFY(manager.detailDefinitions().count() == 0); |
1171 QVERIFY(manager.error() == QContactManager::NotSupportedError || manager.error() == QContactManager::InvalidContactTypeError); |
1205 QVERIFY(manager.error() == QContactManager::NotSupportedError || manager.error() == QContactManager::InvalidContactTypeError); |
1214 invalidRelList << invalidRel; |
1248 invalidRelList << invalidRel; |
1215 QVERIFY(!manager.saveRelationship(&invalidRel)); |
1249 QVERIFY(!manager.saveRelationship(&invalidRel)); |
1216 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1250 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1217 QVERIFY(manager.relationships().isEmpty()); |
1251 QVERIFY(manager.relationships().isEmpty()); |
1218 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1252 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1219 manager.saveRelationships(&invalidRelList); |
1253 manager.saveRelationships(&invalidRelList, NULL); |
1220 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1254 QVERIFY(manager.error() == QContactManager::NotSupportedError); |
1221 manager.removeRelationships(invalidRelList); |
1255 manager.removeRelationships(invalidRelList, NULL); |
1222 QVERIFY(manager.error() == QContactManager::NotSupportedError || manager.error() == QContactManager::DoesNotExistError); |
1256 QVERIFY(manager.error() == QContactManager::NotSupportedError || manager.error() == QContactManager::DoesNotExistError); |
1223 |
1257 |
1224 /* Capabilities */ |
1258 /* Capabilities */ |
1225 QVERIFY(manager.supportedDataTypes().count() == 0); |
1259 QVERIFY(manager.supportedDataTypes().count() == 0); |
1226 QVERIFY(!manager.hasFeature(QContactManager::ActionPreferences)); |
1260 QVERIFY(!manager.hasFeature(QContactManager::ActionPreferences)); |
1687 QList<QContactLocalId> sigids; |
1721 QList<QContactLocalId> sigids; |
1688 int addSigCount = 0; // the expected signal counts. |
1722 int addSigCount = 0; // the expected signal counts. |
1689 int modSigCount = 0; |
1723 int modSigCount = 0; |
1690 int remSigCount = 0; |
1724 int remSigCount = 0; |
1691 |
1725 |
|
1726 QContactDetailDefinition nameDef = m1->detailDefinition(QContactName::DefinitionName, QContactType::TypeContact); |
|
1727 |
1692 // verify add emits signal added |
1728 // verify add emits signal added |
1693 QContactName nc; |
1729 QContactName nc; |
1694 nc.setFirstName("John"); |
1730 saveContactName(&c, nameDef, &nc, "John"); |
1695 c.saveDetail(&nc); |
1731 QVERIFY(m1->saveContact(&c)); |
1696 m1->saveContact(&c); |
|
1697 addSigCount += 1; |
1732 addSigCount += 1; |
1698 QTRY_COMPARE(spyCA.count(), addSigCount); |
1733 QTRY_COMPARE(spyCA.count(), addSigCount); |
1699 args = spyCA.takeFirst(); |
1734 args = spyCA.takeFirst(); |
1700 addSigCount -= 1; |
1735 addSigCount -= 1; |
1701 QVERIFY(args.count() == 1); |
1736 QVERIFY(args.count() == 1); |
1702 temp = QContactLocalId(args.at(0).value<quint32>()); |
1737 temp = QContactLocalId(args.at(0).value<quint32>()); |
1703 |
1738 |
1704 // verify save modified emits signal changed |
1739 // verify save modified emits signal changed |
1705 nc.setLastName("Citizen"); |
1740 saveContactName(&c, nameDef, &nc, "Citizen"); |
1706 c.saveDetail(&nc); |
1741 QVERIFY(m1->saveContact(&c)); |
1707 m1->saveContact(&c); |
|
1708 modSigCount += 1; |
1742 modSigCount += 1; |
1709 QTRY_COMPARE(spyCM.count(), modSigCount); |
1743 QTRY_COMPARE(spyCM.count(), modSigCount); |
1710 args = spyCM.takeFirst(); |
1744 args = spyCM.takeFirst(); |
1711 modSigCount -= 1; |
1745 modSigCount -= 1; |
1712 QVERIFY(args.count() == 1); |
1746 QVERIFY(args.count() == 1); |
1722 QCOMPARE(temp, QContactLocalId(args.at(0).value<quint32>())); |
1756 QCOMPARE(temp, QContactLocalId(args.at(0).value<quint32>())); |
1723 |
1757 |
1724 // verify multiple adds works as advertised |
1758 // verify multiple adds works as advertised |
1725 QContact c2, c3; |
1759 QContact c2, c3; |
1726 QContactName nc2, nc3; |
1760 QContactName nc2, nc3; |
1727 nc2.setFirstName("Mark"); |
1761 saveContactName(&c2, nameDef, &nc2, "Mark"); |
1728 nc3.setFirstName("Garry"); |
1762 saveContactName(&c3, nameDef, &nc3, "Garry"); |
1729 c2.saveDetail(&nc2); |
1763 #if defined(Q_OS_SYMBIAN) |
1730 c3.saveDetail(&nc3); |
1764 // TODO: symbiansim backend fails this test currently. Commented out for |
1731 QVERIFY(!m1->saveContact(&c)); // saving contact with nonexistent id fails |
1765 // now. Will be fixed later. |
|
1766 if(!uri.contains("symbiansim")) { |
|
1767 QVERIFY(!m1->saveContact(&c)); // saving contact with nonexistent id fails |
|
1768 } |
|
1769 #endif |
1732 QVERIFY(m1->saveContact(&c2)); |
1770 QVERIFY(m1->saveContact(&c2)); |
1733 addSigCount += 1; |
1771 addSigCount += 1; |
1734 QVERIFY(m1->saveContact(&c3)); |
1772 QVERIFY(m1->saveContact(&c3)); |
1735 addSigCount += 1; |
1773 addSigCount += 1; |
1736 QTRY_COMPARE(spyCM.count(), modSigCount); |
1774 QTRY_COMPARE(spyCM.count(), modSigCount); |
1737 QTRY_COMPARE(spyCA.count(), addSigCount); |
1775 QTRY_COMPARE(spyCA.count(), addSigCount); |
1738 |
1776 |
1739 // verify multiple modifies works as advertised |
1777 // verify multiple modifies works as advertised |
1740 nc2.setLastName("M."); |
1778 saveContactName(&c2, nameDef, &nc2, "M."); |
1741 c2.saveDetail(&nc2); |
|
1742 QVERIFY(m1->saveContact(&c2)); |
1779 QVERIFY(m1->saveContact(&c2)); |
1743 modSigCount += 1; |
1780 modSigCount += 1; |
1744 nc2.setPrefix("Mr."); |
1781 saveContactName(&c2, nameDef, &nc2, "Mark"); |
1745 nc3.setLastName("G."); |
1782 saveContactName(&c3, nameDef, &nc3, "G."); |
1746 c2.saveDetail(&nc2); |
|
1747 c3.saveDetail(&nc3); |
|
1748 QVERIFY(m1->saveContact(&c2)); |
1783 QVERIFY(m1->saveContact(&c2)); |
1749 modSigCount += 1; |
1784 modSigCount += 1; |
1750 QVERIFY(m1->saveContact(&c3)); |
1785 QVERIFY(m1->saveContact(&c3)); |
1751 modSigCount += 1; |
1786 modSigCount += 1; |
1752 QTRY_COMPARE(spyCM.count(), modSigCount); |
1787 QTRY_COMPARE(spyCM.count(), modSigCount); |
1784 QTRY_WAIT( while(spyCA.size() > 0) {sigids += spyCA.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1819 QTRY_WAIT( while(spyCA.size() > 0) {sigids += spyCA.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1785 QTRY_COMPARE(spyCM.count(), 0); |
1820 QTRY_COMPARE(spyCM.count(), 0); |
1786 QTRY_COMPARE(spyCR.count(), 0); |
1821 QTRY_COMPARE(spyCR.count(), 0); |
1787 |
1822 |
1788 /* Batch modifies */ |
1823 /* Batch modifies */ |
1789 QContactDetailDefinition nameDef = m1->detailDefinition(QContactName::DefinitionName, QContactType::TypeContact); |
|
1790 QContactName modifiedName = c.detail(QContactName::DefinitionName); |
1824 QContactName modifiedName = c.detail(QContactName::DefinitionName); |
1791 setContactName(nameDef, modifiedName, "This is modified number 1"); |
1825 saveContactName(&c, nameDef, &modifiedName, "This is modified number 1"); |
1792 c.saveDetail(&modifiedName); |
|
1793 modifiedName = c2.detail(QContactName::DefinitionName); |
1826 modifiedName = c2.detail(QContactName::DefinitionName); |
1794 setContactName(nameDef, modifiedName, "This is modified number 2"); |
1827 saveContactName(&c2, nameDef, &modifiedName, "This is modified number 2"); |
1795 c2.saveDetail(&modifiedName); |
|
1796 modifiedName = c3.detail(QContactName::DefinitionName); |
1828 modifiedName = c3.detail(QContactName::DefinitionName); |
1797 setContactName(nameDef, modifiedName, "This is modified number 3"); |
1829 saveContactName(&c3, nameDef, &modifiedName, "This is modified number 3"); |
1798 c3.saveDetail(&modifiedName); |
|
1799 |
1830 |
1800 batchAdd.clear(); |
1831 batchAdd.clear(); |
1801 batchAdd << c << c2 << c3; |
1832 batchAdd << c << c2 << c3; |
1802 QVERIFY(m1->saveContacts(&batchAdd, &errorMap)); |
1833 QVERIFY(m1->saveContacts(&batchAdd, &errorMap)); |
1803 |
1834 |
1804 sigids.clear(); |
1835 sigids.clear(); |
1805 QTRY_WAIT( while(spyCM.size() > 0) {sigids += spyCM.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1836 QTRY_WAIT( while(spyCM.size() > 0) {sigids += spyCM.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1806 |
1837 |
1807 /* Batch removes */ |
1838 /* Batch removes */ |
1808 batchRemove << c.id().localId() << c2.id().localId() << c3.id().localId(); |
1839 batchRemove << c.id().localId() << c2.id().localId() << c3.id().localId(); |
1809 QVERIFY(m1->removeContacts(&batchRemove, &errorMap)); |
1840 QVERIFY(m1->removeContacts(batchRemove, &errorMap)); |
1810 |
1841 |
1811 sigids.clear(); |
1842 sigids.clear(); |
1812 QTRY_WAIT( while(spyCR.size() > 0) {sigids += spyCR.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1843 QTRY_WAIT( while(spyCR.size() > 0) {sigids += spyCR.takeFirst().at(0).value<QList<QContactLocalId> >(); }, sigids.contains(c.localId()) && sigids.contains(c2.localId()) && sigids.contains(c3.localId())); |
1813 |
1844 |
1814 QTRY_COMPARE(spyCA.count(), 0); |
1845 QTRY_COMPARE(spyCA.count(), 0); |
1816 |
1847 |
1817 /* Now some cross manager testing */ |
1848 /* Now some cross manager testing */ |
1818 if (!m1->hasFeature(QContactManager::Anonymous)) { |
1849 if (!m1->hasFeature(QContactManager::Anonymous)) { |
1819 // verify that signals are emitted for modifications made to other managers (same id). |
1850 // verify that signals are emitted for modifications made to other managers (same id). |
1820 QContactName ncs = c.detail(QContactName::DefinitionName); |
1851 QContactName ncs = c.detail(QContactName::DefinitionName); |
1821 ncs.setSuffix("Test"); |
1852 saveContactName(&c, nameDef, &ncs, "Test"); |
1822 c.saveDetail(&ncs); |
|
1823 c.setId(QContactId()); // reset id so save can succeed. |
1853 c.setId(QContactId()); // reset id so save can succeed. |
1824 m2->saveContact(&c); |
1854 QVERIFY(m2->saveContact(&c)); |
1825 ncs.setPrefix("Test2"); |
1855 saveContactName(&c, nameDef, &ncs, "Test2"); |
1826 c.saveDetail(&ncs); |
1856 QVERIFY(m2->saveContact(&c)); |
1827 m2->saveContact(&c); |
|
1828 QTRY_COMPARE(spyCA.count(), 1); // check that we received the update signals. |
1857 QTRY_COMPARE(spyCA.count(), 1); // check that we received the update signals. |
1829 QTRY_COMPARE(spyCM.count(), 1); // check that we received the update signals. |
1858 QTRY_COMPARE(spyCM.count(), 1); // check that we received the update signals. |
1830 m2->removeContact(c.localId()); |
1859 m2->removeContact(c.localId()); |
1831 QTRY_COMPARE(spyCR.count(), 1); // check that we received the remove signal. |
1860 QTRY_COMPARE(spyCR.count(), 1); // check that we received the remove signal. |
1832 } |
1861 } |
2137 QSKIP("Manager does not support action preferences", SkipSingle); |
2165 QSKIP("Manager does not support action preferences", SkipSingle); |
2138 } |
2166 } |
2139 |
2167 |
2140 // create a sample contact |
2168 // create a sample contact |
2141 QContactAvatar a; |
2169 QContactAvatar a; |
2142 a.setAvatar("test.png"); |
2170 a.setImageUrl(QUrl("test.png")); |
2143 QContactPhoneNumber p1; |
2171 QContactPhoneNumber p1; |
2144 p1.setNumber("12345"); |
2172 p1.setNumber("12345"); |
2145 QContactPhoneNumber p2; |
2173 QContactPhoneNumber p2; |
2146 p2.setNumber("34567"); |
2174 p2.setNumber("34567"); |
2147 QContactPhoneNumber p3; |
2175 QContactPhoneNumber p3; |
2148 p3.setNumber("56789"); |
2176 p3.setNumber("56789"); |
2149 QContactUrl u; |
2177 QContactUrl u; |
2150 u.setUrl("http://test.nokia.com"); |
2178 u.setUrl("http://test.nokia.com"); |
2151 QContactName n; |
2179 QContactName n; |
2152 setContactName(cm->detailDefinition(QContactName::DefinitionName, QContactType::TypeContact), n, "TestContact"); |
|
2153 |
|
2154 QContact c; |
2180 QContact c; |
|
2181 saveContactName(&c, cm->detailDefinition(QContactName::DefinitionName, QContactType::TypeContact), &n, "TestContact"); |
2155 c.saveDetail(&a); |
2182 c.saveDetail(&a); |
2156 c.saveDetail(&p1); |
2183 c.saveDetail(&p1); |
2157 c.saveDetail(&p2); |
2184 c.saveDetail(&p2); |
2158 c.saveDetail(&p3); |
2185 c.saveDetail(&p3); |
2159 c.saveDetail(&u); |
2186 c.saveDetail(&u); |
2160 c.saveDetail(&n); |
|
2161 |
2187 |
2162 // set a preference for dialing a particular saved phonenumber. |
2188 // set a preference for dialing a particular saved phonenumber. |
2163 c.setPreferredDetail("Dial", p2); |
2189 c.setPreferredDetail("Dial", p2); |
2164 |
2190 |
2165 QVERIFY(cm->saveContact(&c)); // save the contact |
2191 QVERIFY(cm->saveContact(&c)); // save the contact |
2174 |
2200 |
2175 void tst_QContactManager::changeSet() |
2201 void tst_QContactManager::changeSet() |
2176 { |
2202 { |
2177 QContactLocalId id(1); |
2203 QContactLocalId id(1); |
2178 |
2204 |
2179 QContactChangeSet cs; |
2205 QContactChangeSet changeSet; |
2180 QVERIFY(cs.addedContacts().isEmpty()); |
2206 QVERIFY(changeSet.addedContacts().isEmpty()); |
2181 QVERIFY(cs.changedContacts().isEmpty()); |
2207 QVERIFY(changeSet.changedContacts().isEmpty()); |
2182 QVERIFY(cs.removedContacts().isEmpty()); |
2208 QVERIFY(changeSet.removedContacts().isEmpty()); |
2183 |
2209 |
2184 cs.addedContacts().insert(id); |
2210 changeSet.insertAddedContact(id); |
2185 QVERIFY(!cs.addedContacts().isEmpty()); |
2211 QVERIFY(!changeSet.addedContacts().isEmpty()); |
2186 QVERIFY(cs.changedContacts().isEmpty()); |
2212 QVERIFY(changeSet.changedContacts().isEmpty()); |
2187 QVERIFY(cs.removedContacts().isEmpty()); |
2213 QVERIFY(changeSet.removedContacts().isEmpty()); |
2188 QVERIFY(cs.addedContacts().contains(id)); |
2214 QVERIFY(changeSet.addedContacts().contains(id)); |
2189 |
2215 |
2190 cs.changedContacts().insert(id); |
2216 changeSet.insertChangedContact(id); |
2191 cs.changedContacts().insert(id); |
2217 changeSet.insertChangedContact(id); |
2192 QVERIFY(cs.changedContacts().size() == 1); // set, should only be added once. |
2218 QVERIFY(changeSet.changedContacts().size() == 1); // set, should only be added once. |
2193 QVERIFY(!cs.addedContacts().isEmpty()); |
2219 QVERIFY(!changeSet.addedContacts().isEmpty()); |
2194 QVERIFY(!cs.changedContacts().isEmpty()); |
2220 QVERIFY(!changeSet.changedContacts().isEmpty()); |
2195 QVERIFY(cs.removedContacts().isEmpty()); |
2221 QVERIFY(changeSet.removedContacts().isEmpty()); |
2196 QVERIFY(cs.changedContacts().contains(id)); |
2222 QVERIFY(changeSet.changedContacts().contains(id)); |
2197 |
2223 |
2198 QVERIFY(cs.dataChanged() == false); |
2224 QVERIFY(changeSet.dataChanged() == false); |
2199 QContactChangeSet cs2; |
2225 QContactChangeSet changeSet2; |
2200 cs2 = cs; |
2226 changeSet2 = changeSet; |
2201 QVERIFY(cs.addedContacts() == cs2.addedContacts()); |
2227 QVERIFY(changeSet.addedContacts() == changeSet2.addedContacts()); |
2202 cs.emitSignals(0); |
2228 changeSet.emitSignals(0); |
2203 |
2229 |
2204 cs2.clear(); |
2230 changeSet2.clearAll(); |
2205 QVERIFY(cs.addedContacts() != cs2.addedContacts()); |
2231 QVERIFY(changeSet.addedContacts() != changeSet2.addedContacts()); |
2206 |
2232 |
2207 QContactChangeSet cs3(cs2); |
2233 QContactChangeSet changeSet3(changeSet2); |
2208 QVERIFY(cs.addedContacts() != cs3.addedContacts()); |
2234 QVERIFY(changeSet.addedContacts() != changeSet3.addedContacts()); |
2209 QVERIFY(cs2.addedContacts() == cs3.addedContacts()); |
2235 QVERIFY(changeSet2.addedContacts() == changeSet3.addedContacts()); |
2210 |
2236 |
2211 cs.setDataChanged(true); |
2237 changeSet.setDataChanged(true); |
2212 QVERIFY(cs.dataChanged() == true); |
2238 QVERIFY(changeSet.dataChanged() == true); |
2213 QVERIFY(cs.dataChanged() != cs2.dataChanged()); |
2239 QVERIFY(changeSet.dataChanged() != changeSet2.dataChanged()); |
2214 QVERIFY(cs.dataChanged() != cs3.dataChanged()); |
2240 QVERIFY(changeSet.dataChanged() != changeSet3.dataChanged()); |
2215 cs.emitSignals(0); |
2241 changeSet.emitSignals(0); |
2216 |
2242 |
2217 cs.addedRelationshipsContacts().insert(id); |
2243 changeSet.addedRelationshipsContacts().insert(id); |
2218 cs.emitSignals(0); |
2244 changeSet.emitSignals(0); |
2219 cs.removedRelationshipsContacts().insert(id); |
2245 changeSet.removedRelationshipsContacts().insert(id); |
2220 cs.emitSignals(0); |
2246 changeSet.emitSignals(0); |
2221 |
2247 |
2222 cs.oldAndNewSelfContactId() = QPair<QContactLocalId, QContactLocalId>(QContactLocalId(0), id); |
2248 changeSet.setOldAndNewSelfContactId(QPair<QContactLocalId, QContactLocalId>(QContactLocalId(0), id)); |
2223 cs2 = cs; |
2249 changeSet2 = changeSet; |
2224 QVERIFY(cs2.addedRelationshipsContacts() == cs.addedRelationshipsContacts()); |
2250 QVERIFY(changeSet2.addedRelationshipsContacts() == changeSet.addedRelationshipsContacts()); |
2225 QVERIFY(cs2.removedRelationshipsContacts() == cs.removedRelationshipsContacts()); |
2251 QVERIFY(changeSet2.removedRelationshipsContacts() == changeSet.removedRelationshipsContacts()); |
2226 QVERIFY(cs2.oldAndNewSelfContactId() == cs.oldAndNewSelfContactId()); |
2252 QVERIFY(changeSet2.oldAndNewSelfContactId() == changeSet.oldAndNewSelfContactId()); |
2227 cs.emitSignals(0); |
2253 changeSet.emitSignals(0); |
2228 cs.oldAndNewSelfContactId() = QPair<QContactLocalId, QContactLocalId>(id, QContactLocalId(0)); |
2254 changeSet.setOldAndNewSelfContactId(QPair<QContactLocalId, QContactLocalId>(id, QContactLocalId(0))); |
2229 QVERIFY(cs2.oldAndNewSelfContactId() != cs.oldAndNewSelfContactId()); |
2255 QVERIFY(changeSet2.oldAndNewSelfContactId() != changeSet.oldAndNewSelfContactId()); |
2230 cs.setDataChanged(true); |
2256 changeSet.setDataChanged(true); |
2231 cs.emitSignals(0); |
2257 changeSet.emitSignals(0); |
2232 } |
2258 } |
2233 |
2259 |
2234 void tst_QContactManager::selfContactId() |
2260 void tst_QContactManager::selfContactId() |
2235 { |
2261 { |
2236 QFETCH(QString, uri); |
2262 QFETCH(QString, uri); |
2511 // test save and remove |
2537 // test save and remove |
2512 QVERIFY(!cm->saveRelationship(&r1)); |
2538 QVERIFY(!cm->saveRelationship(&r1)); |
2513 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2539 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2514 QVERIFY(!cm->removeRelationship(r1)); |
2540 QVERIFY(!cm->removeRelationship(r1)); |
2515 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2541 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2516 QVERIFY(cm->saveRelationships(&batchList).isEmpty()); |
2542 cm->saveRelationships(&batchList, NULL); |
2517 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
|
2518 QVERIFY(cm->removeRelationships(batchList).isEmpty()); |
|
2519 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2543 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2520 |
2544 |
2521 // test retrieval |
2545 // test retrieval |
2522 QList<QContactRelationship> retrieveList; |
2546 QList<QContactRelationship> retrieveList; |
2523 retrieveList = cm->relationships(source.id(), QContactRelationshipFilter::First); |
2547 retrieveList = cm->relationships(source.id(), QContactRelationship::First); |
2524 QVERIFY(retrieveList.isEmpty()); |
2548 QVERIFY(retrieveList.isEmpty()); |
2525 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2549 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2526 retrieveList = cm->relationships(source.id(), QContactRelationshipFilter::Second); |
2550 retrieveList = cm->relationships(source.id(), QContactRelationship::Second); |
2527 QVERIFY(retrieveList.isEmpty()); |
2551 QVERIFY(retrieveList.isEmpty()); |
2528 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2552 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2529 retrieveList = cm->relationships(source.id()); // Either |
2553 retrieveList = cm->relationships(source.id(), QContactRelationship::Either); // Either |
2530 QVERIFY(retrieveList.isEmpty()); |
2554 QVERIFY(retrieveList.isEmpty()); |
2531 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2555 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2532 |
2556 |
2533 |
2557 |
2534 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationshipFilter::First); |
2558 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationship::First); |
2535 QVERIFY(retrieveList.isEmpty()); |
2559 QVERIFY(retrieveList.isEmpty()); |
2536 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2560 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2537 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationshipFilter::Second); |
2561 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationship::Second); |
2538 QVERIFY(retrieveList.isEmpty()); |
2562 QVERIFY(retrieveList.isEmpty()); |
2539 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2563 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2540 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationshipFilter::Either); |
2564 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id(), QContactRelationship::Either); |
2541 QVERIFY(retrieveList.isEmpty()); |
2565 QVERIFY(retrieveList.isEmpty()); |
2542 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2566 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2543 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id()); |
2567 retrieveList = cm->relationships(QContactRelationship::HasManager, source.id()); |
2544 QVERIFY(retrieveList.isEmpty()); |
2568 QVERIFY(retrieveList.isEmpty()); |
2545 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2569 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2548 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2572 QVERIFY(cm->error() == QContactManager::NotSupportedError); |
2549 return; |
2573 return; |
2550 } |
2574 } |
2551 |
2575 |
2552 // Get supported relationship types |
2576 // Get supported relationship types |
2553 QStringList availableRelationshipTypes = cm->supportedRelationshipTypes(); |
2577 QStringList availableRelationshipTypes; |
|
2578 if (cm->isRelationshipTypeSupported(QContactRelationship::HasMember)) |
|
2579 availableRelationshipTypes << QContactRelationship::HasMember; |
|
2580 if (cm->isRelationshipTypeSupported(QContactRelationship::HasAssistant)) |
|
2581 availableRelationshipTypes << QContactRelationship::HasAssistant; |
|
2582 if (cm->isRelationshipTypeSupported(QContactRelationship::HasManager)) |
|
2583 availableRelationshipTypes << QContactRelationship::HasManager; |
|
2584 if (cm->isRelationshipTypeSupported(QContactRelationship::HasSpouse)) |
|
2585 availableRelationshipTypes << QContactRelationship::HasSpouse; |
|
2586 if (cm->isRelationshipTypeSupported(QContactRelationship::IsSameAs)) |
|
2587 availableRelationshipTypes << QContactRelationship::IsSameAs; |
|
2588 |
2554 |
2589 |
2555 // Check arbitary relationship support |
2590 // Check arbitary relationship support |
2556 if (cm->hasFeature(QContactManager::ArbitraryRelationshipTypes)) { |
2591 if (cm->hasFeature(QContactManager::ArbitraryRelationshipTypes)) { |
2557 // add some arbitary type for testing |
2592 // add some arbitary type for testing |
2558 if (availableRelationshipTypes.count()) |
2593 if (availableRelationshipTypes.count()) |
2608 QCOMPARE(cm->relationships(availableRelationshipTypes.at(0)).count(), (managerRelationshipsCount + 1)); |
2643 QCOMPARE(cm->relationships(availableRelationshipTypes.at(0)).count(), (managerRelationshipsCount + 1)); |
2609 QVERIFY(cm->relationships(availableRelationshipTypes.at(0), source.id()).count() == 1); |
2644 QVERIFY(cm->relationships(availableRelationshipTypes.at(0), source.id()).count() == 1); |
2610 |
2645 |
2611 // remove the dest1 contact, relationship should be removed. |
2646 // remove the dest1 contact, relationship should be removed. |
2612 cm->removeContact(dest1.localId()); |
2647 cm->removeContact(dest1.localId()); |
2613 QCOMPARE(cm->relationships(availableRelationshipTypes.at(0), dest1Uri, QContactRelationshipFilter::Second).count(), 0); |
2648 QCOMPARE(cm->relationships(availableRelationshipTypes.at(0), dest1Uri, QContactRelationship::Second).count(), 0); |
2614 |
2649 |
2615 // modify and save the relationship |
2650 // modify and save the relationship |
2616 customRelationshipOne.setSecond(dest2Uri); |
2651 customRelationshipOne.setSecond(dest2Uri); |
2617 QVERIFY(cm->saveRelationship(&customRelationshipOne)); |
2652 QVERIFY(cm->saveRelationship(&customRelationshipOne)); |
2618 |
2653 |
2652 dest3 = cm->contact(dest3.localId()); |
2687 dest3 = cm->contact(dest3.localId()); |
2653 dest2 = cm->contact(dest2.localId()); |
2688 dest2 = cm->contact(dest2.localId()); |
2654 source = cm->contact(source.localId()); |
2689 source = cm->contact(source.localId()); |
2655 |
2690 |
2656 // and test again. |
2691 // and test again. |
2657 QVERIFY(source.relatedContacts(QString(), QContactRelationshipFilter::First).isEmpty()); // source is always the first, so this should be empty. |
2692 QVERIFY(source.relatedContacts(QString(), QContactRelationship::First).isEmpty()); // source is always the first, so this should be empty. |
2658 QVERIFY(source.relatedContacts(QString(), QContactRelationshipFilter::Second).contains(dest2.id())); |
2693 QVERIFY(source.relatedContacts(QString(), QContactRelationship::Second).contains(dest2.id())); |
2659 QVERIFY(source.relatedContacts(QString(), QContactRelationshipFilter::Either).contains(dest2.id())); |
2694 QVERIFY(source.relatedContacts(QString(), QContactRelationship::Either).contains(dest2.id())); |
2660 QVERIFY(source.relatedContacts(QString(), QContactRelationshipFilter::Second).contains(dest3.id())); |
2695 QVERIFY(source.relatedContacts(QString(), QContactRelationship::Second).contains(dest3.id())); |
2661 QVERIFY(source.relatedContacts(QString(), QContactRelationshipFilter::Either).contains(dest3.id())); |
2696 QVERIFY(source.relatedContacts(QString(), QContactRelationship::Either).contains(dest3.id())); |
2662 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::Second).contains(dest2.id())); |
2697 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::Second).contains(dest2.id())); |
2663 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::First).isEmpty()); |
2698 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::First).isEmpty()); |
2664 |
2699 |
2665 QVERIFY(dest2.relatedContacts().contains(source.id())); |
2700 QVERIFY(dest2.relatedContacts().contains(source.id())); |
2666 QVERIFY(dest2.relationships().contains(customRelationshipOne)); |
2701 QVERIFY(dest2.relationships().contains(customRelationshipOne)); |
2667 QVERIFY(!dest2.relationships().contains(customRelationshipTwo)); |
2702 QVERIFY(!dest2.relationships().contains(customRelationshipTwo)); |
2668 QVERIFY(dest2.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipOne)); |
2703 QVERIFY(dest2.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipOne)); |
2669 QVERIFY(!dest2.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipTwo)); |
2704 QVERIFY(!dest2.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipTwo)); |
2670 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0)).contains(source.id())); |
2705 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0)).contains(source.id())); |
2671 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::First).contains(source.id())); |
2706 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::First).contains(source.id())); |
2672 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::Second).isEmpty()); |
2707 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::Second).isEmpty()); |
2673 QVERIFY(!dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::Second).contains(source.id())); |
2708 QVERIFY(!dest2.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::Second).contains(source.id())); |
2674 |
2709 |
2675 QVERIFY(dest3.relatedContacts().contains(source.id())); |
2710 QVERIFY(dest3.relatedContacts().contains(source.id())); |
2676 QVERIFY(!dest3.relationships().contains(customRelationshipOne)); |
2711 QVERIFY(!dest3.relationships().contains(customRelationshipOne)); |
2677 QVERIFY(dest3.relationships().contains(customRelationshipTwo)); |
2712 QVERIFY(dest3.relationships().contains(customRelationshipTwo)); |
2678 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipOne)); |
2713 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipOne)); |
2687 QVERIFY(it->second() == dest2.id() || it->second() == dest3.id()); |
2722 QVERIFY(it->second() == dest2.id() || it->second() == dest3.id()); |
2688 it++; |
2723 it++; |
2689 } |
2724 } |
2690 |
2725 |
2691 if (availableRelationshipTypes.count() > 1) { |
2726 if (availableRelationshipTypes.count() > 1) { |
2692 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(1), QContactRelationshipFilter::Second).contains(dest3.id())); |
2727 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(1), QContactRelationship::Second).contains(dest3.id())); |
2693 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(1), QContactRelationshipFilter::First).isEmpty()); |
2728 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(1), QContactRelationship::First).isEmpty()); |
2694 |
2729 |
2695 QVERIFY(dest2.relationships(availableRelationshipTypes.at(1)).isEmpty()); |
2730 QVERIFY(dest2.relationships(availableRelationshipTypes.at(1)).isEmpty()); |
2696 |
2731 |
2697 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipTwo)); |
2732 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(0)).contains(customRelationshipTwo)); |
2698 QVERIFY(dest3.relationships(availableRelationshipTypes.at(1)).contains(customRelationshipTwo)); |
2733 QVERIFY(dest3.relationships(availableRelationshipTypes.at(1)).contains(customRelationshipTwo)); |
2699 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(1)).contains(customRelationshipOne)); |
2734 QVERIFY(!dest3.relationships(availableRelationshipTypes.at(1)).contains(customRelationshipOne)); |
2700 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1)).contains(source.id())); |
2735 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1)).contains(source.id())); |
2701 QVERIFY(!dest3.relatedContacts(availableRelationshipTypes.at(0)).contains(source.id())); |
2736 QVERIFY(!dest3.relatedContacts(availableRelationshipTypes.at(0)).contains(source.id())); |
2702 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1)).contains(source.id())); // role = either |
2737 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1)).contains(source.id())); // role = either |
2703 QVERIFY(!dest3.relatedContacts(availableRelationshipTypes.at(1), QContactRelationshipFilter::Second).contains(source.id())); |
2738 QVERIFY(!dest3.relatedContacts(availableRelationshipTypes.at(1), QContactRelationship::Second).contains(source.id())); |
2704 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1), QContactRelationshipFilter::First).contains(source.id())); |
2739 QVERIFY(dest3.relatedContacts(availableRelationshipTypes.at(1), QContactRelationship::First).contains(source.id())); |
2705 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(1)).isEmpty()); |
2740 QVERIFY(dest2.relatedContacts(availableRelationshipTypes.at(1)).isEmpty()); |
2706 } |
2741 } |
2707 else { |
2742 else { |
2708 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationshipFilter::Second).contains(dest3.id())); |
2743 QVERIFY(source.relatedContacts(availableRelationshipTypes.at(0), QContactRelationship::Second).contains(dest3.id())); |
2709 } |
2744 } |
2710 |
2745 |
2711 // Cleanup a bit |
2746 // Cleanup a bit |
2712 QVERIFY(cm->removeRelationship(customRelationshipOne)); |
2747 QVERIFY(cm->removeRelationship(customRelationshipOne)); |
2713 QVERIFY(cm->removeRelationship(customRelationshipTwo)); |
2748 QVERIFY(cm->removeRelationship(customRelationshipTwo)); |
2714 |
2749 |
2715 // test batch API and ordering in contacts |
2750 // test batch API and ordering in contacts |
2716 QList<QContactRelationship> currentRelationships = cm->relationships(source.id(), QContactRelationshipFilter::First); |
2751 QList<QContactRelationship> currentRelationships = cm->relationships(source.id(), QContactRelationship::First); |
2717 QList<QContactRelationship> batchList; |
2752 QList<QContactRelationship> batchList; |
2718 QContactRelationship br1, br2, br3; |
2753 QContactRelationship br1, br2, br3; |
2719 br1.setFirst(source.id()); |
2754 br1.setFirst(source.id()); |
2720 br1.setSecond(dest2.id()); |
2755 br1.setSecond(dest2.id()); |
2721 br1.setRelationshipType(availableRelationshipTypes.at(0)); |
2756 br1.setRelationshipType(availableRelationshipTypes.at(0)); |
2735 br3.setRelationshipType(availableRelationshipTypes.at(0)); |
2770 br3.setRelationshipType(availableRelationshipTypes.at(0)); |
2736 } |
2771 } |
2737 batchList << br1 << br2 << br3; |
2772 batchList << br1 << br2 << br3; |
2738 |
2773 |
2739 // ensure that the batch save works properly |
2774 // ensure that the batch save works properly |
2740 cm->saveRelationships(&batchList); |
2775 cm->saveRelationships(&batchList, NULL); |
2741 QVERIFY(cm->error() == QContactManager::NoError); |
2776 QCOMPARE(cm->error(), QContactManager::NoError); |
2742 QList<QContactRelationship> batchRetrieve = cm->relationships(source.id(), QContactRelationshipFilter::First); |
2777 QList<QContactRelationship> batchRetrieve = cm->relationships(source.id(), QContactRelationship::First); |
2743 QVERIFY(batchRetrieve.contains(br1)); |
2778 QVERIFY(batchRetrieve.contains(br1)); |
2744 QVERIFY(batchRetrieve.contains(br2)); |
2779 QVERIFY(batchRetrieve.contains(br2)); |
2745 QVERIFY(batchRetrieve.contains(br3)); |
2780 QVERIFY(batchRetrieve.contains(br3)); |
2746 |
2781 |
2747 // Check relationship ordering support |
|
2748 if (cm->hasFeature(QContactManager::RelationshipOrdering)) |
|
2749 { |
|
2750 // test relationship ordering in the contact |
|
2751 source = cm->contact(source.localId()); |
|
2752 QList<QContactRelationship> cachedRelationships = source.relationships(); |
|
2753 QList<QContactRelationship> orderedRelationships = source.relationshipOrder(); |
|
2754 QCOMPARE(cachedRelationships, orderedRelationships); // initially, should be the same |
|
2755 QVERIFY(orderedRelationships.contains(br1)); |
|
2756 QVERIFY(orderedRelationships.contains(br2)); |
|
2757 QVERIFY(orderedRelationships.contains(br3)); |
|
2758 |
|
2759 // ensure that the reordering works as required. |
|
2760 QContactRelationship temp1 = orderedRelationships.takeAt(0); // now fiddle with the order |
|
2761 QContactRelationship temp2 = orderedRelationships.at(0); // this is the new first relationship |
|
2762 orderedRelationships.insert(2, temp1); // and save the old first back at position 3. |
|
2763 source.setRelationshipOrder(orderedRelationships); // set the new relationship order |
|
2764 cm->saveContact(&source); // save the contact to persist the new order |
|
2765 source = cm->contact(source.localId()); // reload the contact |
|
2766 QCOMPARE(source.relationshipOrder(), orderedRelationships); // ensure that it was persisted. |
|
2767 |
|
2768 // now lets try a negative reordering test: adding relationships which don't exist in the database. |
|
2769 QContactRelationship invalidRel; |
|
2770 invalidRel.setFirst(source.id()); |
|
2771 invalidRel.setSecond(dest2.id()); |
|
2772 invalidRel.setRelationshipType("test-nokia-invalid-relationship-type"); |
|
2773 orderedRelationships << invalidRel; |
|
2774 source.setRelationshipOrder(orderedRelationships); |
|
2775 QVERIFY(!cm->saveContact(&source)); |
|
2776 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
|
2777 orderedRelationships.removeOne(br3); |
|
2778 source.setRelationshipOrder(orderedRelationships); |
|
2779 QVERIFY(!cm->saveContact(&source)); |
|
2780 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
|
2781 source.setRelationshipOrder(QList<QContactRelationship>()); |
|
2782 QVERIFY(!cm->saveContact(&source)); |
|
2783 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
|
2784 } |
|
2785 |
|
2786 // remove a single relationship |
2782 // remove a single relationship |
2787 QVERIFY(cm->removeRelationship(br3)); |
2783 QVERIFY(cm->removeRelationship(br3)); |
2788 batchRetrieve = cm->relationships(source.id(), QContactRelationshipFilter::First); |
2784 batchRetrieve = cm->relationships(source.id(), QContactRelationship::First); |
2789 QVERIFY(batchRetrieve.contains(br1)); |
2785 QVERIFY(batchRetrieve.contains(br1)); |
2790 QVERIFY(batchRetrieve.contains(br2)); |
2786 QVERIFY(batchRetrieve.contains(br2)); |
2791 QVERIFY(!batchRetrieve.contains(br3)); // has already been removed. |
2787 QVERIFY(!batchRetrieve.contains(br3)); // has already been removed. |
2792 |
2788 |
2793 // now ensure that the batch remove works and we get returned to the original state. |
2789 // now ensure that the batch remove works and we get returned to the original state. |
2794 batchList.removeOne(br3); |
2790 batchList.removeOne(br3); |
2795 cm->removeRelationships(batchList); |
2791 cm->removeRelationships(batchList, NULL); |
2796 QVERIFY(cm->error() == QContactManager::NoError); |
2792 QVERIFY(cm->error() == QContactManager::NoError); |
2797 QCOMPARE(cm->relationships(source.id(), QContactRelationshipFilter::First), currentRelationships); |
2793 QCOMPARE(cm->relationships(source.id(), QContactRelationship::First), currentRelationships); |
2798 |
2794 |
2799 // attempt to save relationships between an existing source but non-existent destination |
2795 // attempt to save relationships between an existing source but non-existent destination |
2800 QContactId nonexistentDest; |
2796 QContactId nonexistentDest; |
2801 quint32 idSeed = 0x5544; |
2797 quint32 idSeed = 0x5544; |
2802 QContactLocalId nonexistentLocalId = QContactLocalId(idSeed); |
2798 QContactLocalId nonexistentLocalId = QContactLocalId(idSeed); |
2853 relationshipsCount = cm->relationships().count(); |
2849 relationshipsCount = cm->relationships().count(); |
2854 QVERIFY(!cm->removeRelationship(maliciousRel)); // does not exist; fail remove. |
2850 QVERIFY(!cm->removeRelationship(maliciousRel)); // does not exist; fail remove. |
2855 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2851 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2856 QCOMPARE(cm->relationships().count(), relationshipsCount); // should be unchanged. |
2852 QCOMPARE(cm->relationships().count(), relationshipsCount); // should be unchanged. |
2857 |
2853 |
|
2854 // now we want to ensure that a relationship is removed if one of the contacts is removed. |
|
2855 customRelationshipOne.setFirst(source.id()); |
|
2856 customRelationshipOne.setSecond(dest2.id()); |
|
2857 customRelationshipOne.setRelationshipType(availableRelationshipTypes.at(0)); |
|
2858 QVERIFY(cm->saveRelationship(&customRelationshipOne)); |
|
2859 source = cm->contact(source.localId()); |
|
2860 dest2 = cm->contact(dest2.localId()); |
|
2861 QVERIFY(cm->removeContact(dest2.localId())); // remove dest2, the relationship should be removed |
|
2862 QVERIFY(cm->relationships(availableRelationshipTypes.at(0), dest2.id(), QContactRelationship::Second).isEmpty()); |
|
2863 source = cm->contact(source.localId()); |
|
2864 QVERIFY(!source.relatedContacts().contains(dest2.id())); // and it shouldn't appear in cache. |
|
2865 |
2858 // now clean up and remove our dests. |
2866 // now clean up and remove our dests. |
2859 QVERIFY(cm->removeContact(source.localId())); |
2867 QVERIFY(cm->removeContact(source.localId())); |
2860 QVERIFY(cm->removeContact(dest2.localId())); |
|
2861 QVERIFY(cm->removeContact(dest3.localId())); |
2868 QVERIFY(cm->removeContact(dest3.localId())); |
2862 |
2869 |
2863 // attempt to save relationships with nonexistent contacts |
2870 // attempt to save relationships with nonexistent contacts |
2864 QVERIFY(!cm->saveRelationship(&br1)); |
2871 QVERIFY(!cm->saveRelationship(&br1)); |
2865 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
2872 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
2866 cm->saveRelationships(&batchList); |
2873 cm->saveRelationships(&batchList, NULL); |
2867 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
2874 QVERIFY(cm->error() == QContactManager::InvalidRelationshipError); |
2868 QVERIFY(!cm->removeRelationship(br1)); |
2875 QVERIFY(!cm->removeRelationship(br1)); |
2869 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2876 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2870 cm->removeRelationships(batchList); |
2877 cm->removeRelationships(batchList, NULL); |
2871 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2878 QVERIFY(cm->error() == QContactManager::DoesNotExistError || cm->error() == QContactManager::InvalidRelationshipError); |
2872 } |
2879 } |
2873 |
2880 |
2874 void tst_QContactManager::contactType() |
2881 void tst_QContactManager::contactType() |
2875 { |
2882 { |