Warning, file /frameworks/kcontacts/autotests/phonenumbertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the KContacts framework. 0003 SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "phonenumbertest.h" 0009 #include "kcontacts/phonenumber.h" 0010 #include <QTest> 0011 #include <addressee.h> 0012 #include <addresseelist.h> 0013 #include <vcardtool_p.h> 0014 0015 QTEST_MAIN(PhoneNumberTest) 0016 0017 #ifndef Q_OS_WIN 0018 void initLocale() 0019 { 0020 qputenv("LC_ALL", "en_US.utf-8"); 0021 } 0022 0023 Q_CONSTRUCTOR_FUNCTION(initLocale) 0024 #endif 0025 0026 void PhoneNumberTest::emptyTest() 0027 { 0028 KContacts::PhoneNumber number; 0029 0030 QVERIFY(number.isEmpty()); 0031 } 0032 0033 void PhoneNumberTest::storeTest() 0034 { 0035 KContacts::PhoneNumber number; 0036 0037 number.setId(QStringLiteral("My Id")); 0038 number.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0039 number.setNumber(QStringLiteral("2734826345")); 0040 0041 QVERIFY(number.id() == QLatin1String("My Id")); 0042 QVERIFY(number.type() == (KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell)); 0043 QVERIFY(number.number() == QLatin1String("2734826345")); 0044 } 0045 0046 void PhoneNumberTest::equalsTest() 0047 { 0048 KContacts::PhoneNumber number1; 0049 KContacts::PhoneNumber number2; 0050 0051 number1.setId(QStringLiteral("My Id")); 0052 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0053 number1.setNumber(QStringLiteral("2734826345")); 0054 0055 number2.setId(QStringLiteral("My Id")); 0056 number2.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0057 number2.setNumber(QStringLiteral("2734826345")); 0058 0059 QVERIFY(number1 == number2); 0060 } 0061 0062 void PhoneNumberTest::differsTest() 0063 { 0064 KContacts::PhoneNumber number1(QStringLiteral("123"), KContacts::PhoneNumber::Home); 0065 KContacts::PhoneNumber number2(QStringLiteral("123"), KContacts::PhoneNumber::Work); 0066 0067 QVERIFY(number1 != number2); 0068 } 0069 0070 void PhoneNumberTest::assignmentTest() 0071 { 0072 KContacts::PhoneNumber number1; 0073 KContacts::PhoneNumber number2; 0074 0075 number1.setId(QStringLiteral("My Id")); 0076 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0077 number1.setNumber(QStringLiteral("2734826345")); 0078 0079 number1 = number2; 0080 0081 QVERIFY(number1 == number2); 0082 } 0083 0084 void PhoneNumberTest::serializeTest() 0085 { 0086 KContacts::PhoneNumber number1; 0087 KContacts::PhoneNumber number2; 0088 0089 number1.setId(QStringLiteral("My Id")); 0090 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0091 number1.setNumber(QStringLiteral("2734826345")); 0092 0093 QByteArray data; 0094 QDataStream s(&data, QIODevice::WriteOnly); 0095 s << number1; 0096 0097 QDataStream t(&data, QIODevice::ReadOnly); 0098 t >> number2; 0099 0100 QVERIFY(number1 == number2); 0101 } 0102 0103 void PhoneNumberTest::labelTest() 0104 { 0105 QMap<KContacts::PhoneNumber::Type, QString> labels; 0106 0107 const KContacts::PhoneNumber::TypeList types = KContacts::PhoneNumber::typeList(); 0108 0109 // check all types standalone 0110 for (KContacts::PhoneNumber::Type type : types) { 0111 const KContacts::PhoneNumber phone(QStringLiteral("1"), type); 0112 QCOMPARE(phone.type(), type); 0113 0114 // Pref is special cased 0115 if (type != KContacts::PhoneNumber::Pref) { 0116 QCOMPARE(phone.typeLabel(), KContacts::PhoneNumber::typeFlagLabel((KContacts::PhoneNumber::TypeFlag)(int)type)); 0117 labels.insert(type, phone.typeLabel()); 0118 } else { 0119 labels.insert(type, KContacts::PhoneNumber::typeFlagLabel((KContacts::PhoneNumber::TypeFlag)(int)type)); 0120 } 0121 QCOMPARE(KContacts::PhoneNumber::typeLabel(type), phone.typeLabel()); 0122 } 0123 0124 // combine all with Pref 0125 for (KContacts::PhoneNumber::Type type : std::as_const(types)) { 0126 KContacts::PhoneNumber::Type combinedType = type | KContacts::PhoneNumber::Pref; 0127 const KContacts::PhoneNumber phone(QLatin1String("1"), combinedType); 0128 QCOMPARE(phone.type(), combinedType); 0129 QCOMPARE(KContacts::PhoneNumber::typeLabel(combinedType), phone.typeLabel()); 0130 0131 if (type < KContacts::PhoneNumber::Pref) { 0132 const QString expectedCombinedString = QStringLiteral("%1/%2").arg(labels[type]).arg(labels[KContacts::PhoneNumber::Pref]); 0133 QCOMPARE(phone.typeLabel(), expectedCombinedString); 0134 } else if (type > KContacts::PhoneNumber::Pref) { 0135 const QString expectedCombinedString = QStringLiteral("%1/%2").arg(labels[KContacts::PhoneNumber::Pref]).arg(labels[type]); 0136 QCOMPARE(phone.typeLabel(), expectedCombinedString); 0137 } 0138 } 0139 0140 // combine all with Fax 0141 for (KContacts::PhoneNumber::Type type : std::as_const(types)) { 0142 KContacts::PhoneNumber::Type combinedType = type | KContacts::PhoneNumber::Fax; 0143 const KContacts::PhoneNumber phone(QLatin1String("1"), combinedType); 0144 QCOMPARE(phone.type(), combinedType); 0145 QCOMPARE(KContacts::PhoneNumber::typeLabel(combinedType), phone.typeLabel()); 0146 0147 if (type == KContacts::PhoneNumber::Home || type == KContacts::PhoneNumber::Work) { 0148 // special cased 0149 } else if (type < KContacts::PhoneNumber::Fax) { 0150 const QString expectedCombinedString = QStringLiteral("%1/%2").arg(labels[type]).arg(labels[KContacts::PhoneNumber::Fax]); 0151 QCOMPARE(phone.typeLabel(), expectedCombinedString); 0152 } else if (type > KContacts::PhoneNumber::Fax) { 0153 const QString expectedCombinedString = QStringLiteral("%1/%2").arg(labels[KContacts::PhoneNumber::Fax]).arg(labels[type]); 0154 QCOMPARE(phone.typeLabel(), expectedCombinedString); 0155 } 0156 } 0157 0158 // special cases 0159 QCOMPARE(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Pref), QLatin1String("Preferred Number")); 0160 QCOMPARE(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Home // 0161 | KContacts::PhoneNumber::Fax), 0162 QLatin1String("Home Fax")); 0163 QCOMPARE(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Work // 0164 | KContacts::PhoneNumber::Fax), 0165 QLatin1String("Work Fax")); 0166 QCOMPARE(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Home // 0167 | KContacts::PhoneNumber::Fax // 0168 | KContacts::PhoneNumber::Pref), 0169 QLatin1String("Home Fax/Preferred")); 0170 QCOMPARE(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Work // 0171 | KContacts::PhoneNumber::Fax // 0172 | KContacts::PhoneNumber::Pref), 0173 QLatin1String("Work Fax/Preferred")); 0174 } 0175 0176 void PhoneNumberTest::shouldParseVCard21() 0177 { 0178 QByteArray vcarddata( 0179 "BEGIN:VCARD\n" 0180 "VERSION:2.1\n" 0181 "EMAIL:foo@kde.org\n" 0182 "N:;;;;\n" 0183 "TEL;CELL;WORK:+1-919-676-9564\n" 0184 "UID:testuid\n" 0185 "END:VCARD\n" 0186 "\n"); 0187 0188 KContacts::VCardTool vcard; 0189 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata); 0190 QCOMPARE(lst.count(), 1); 0191 const KContacts::Addressee &addr = lst.at(0); 0192 QCOMPARE(addr.phoneNumbers().count(), 1); 0193 KContacts::PhoneNumber number1 = addr.phoneNumbers().at(0); 0194 QCOMPARE(number1.number(), QLatin1String("+1-919-676-9564")); 0195 QVERIFY(number1.supportsSms()); 0196 QVERIFY(!number1.isPreferred()); 0197 } 0198 0199 void PhoneNumberTest::shouldExportVCard21() 0200 { 0201 KContacts::AddresseeList lst; 0202 KContacts::Addressee addr; 0203 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); 0204 addr.setUid(QStringLiteral("testuid")); 0205 0206 KContacts::PhoneNumber number1; 0207 0208 number1.setId(QStringLiteral("My Id")); 0209 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0210 number1.setNumber(QStringLiteral("+1-919-676-9564")); 0211 addr.setPhoneNumbers(KContacts::PhoneNumber::List() << number1); 0212 lst << addr; 0213 KContacts::VCardTool vcard; 0214 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v2_1); 0215 QByteArray expected( 0216 "BEGIN:VCARD\r\n" 0217 "VERSION:2.1\r\n" 0218 "EMAIL:foo@kde.org\r\n" 0219 "N:;;;;\r\n" 0220 "TEL;CELL;WORK:+1-919-676-9564\r\n" 0221 "UID:testuid\r\n" 0222 "END:VCARD\r\n" 0223 "\r\n"); 0224 QCOMPARE(ba, expected); 0225 } 0226 0227 void PhoneNumberTest::shouldExportVCard3() 0228 { 0229 KContacts::AddresseeList lst; 0230 KContacts::Addressee addr; 0231 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); 0232 addr.setUid(QStringLiteral("testuid")); 0233 0234 KContacts::PhoneNumber number1; 0235 0236 number1.setId(QStringLiteral("My Id")); 0237 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0238 number1.setNumber(QStringLiteral("+1-919-676-9564")); 0239 addr.setPhoneNumbers(KContacts::PhoneNumber::List() << number1); 0240 lst << addr; 0241 KContacts::VCardTool vcard; 0242 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0); 0243 QByteArray expected( 0244 "BEGIN:VCARD\r\n" 0245 "VERSION:3.0\r\n" 0246 "EMAIL:foo@kde.org\r\n" 0247 "N:;;;;\r\n" 0248 "TEL;TYPE=CELL,WORK:+1-919-676-9564\r\n" 0249 "UID:testuid\r\n" 0250 "END:VCARD\r\n" 0251 "\r\n"); 0252 QCOMPARE(ba, expected); 0253 } 0254 0255 void PhoneNumberTest::shouldExportVCard4() 0256 { 0257 KContacts::AddresseeList lst; 0258 KContacts::Addressee addr; 0259 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org")); 0260 addr.setUid(QStringLiteral("testuid")); 0261 0262 KContacts::PhoneNumber number1; 0263 0264 number1.setId(QStringLiteral("My Id")); 0265 number1.setType(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Cell); 0266 number1.setNumber(QStringLiteral("+1-919-676-9564")); 0267 addr.setPhoneNumbers(KContacts::PhoneNumber::List() << number1); 0268 lst << addr; 0269 KContacts::VCardTool vcard; 0270 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0); 0271 QByteArray expected( 0272 "BEGIN:VCARD\r\n" 0273 "VERSION:4.0\r\n" 0274 "EMAIL:foo@kde.org\r\n" 0275 "N:;;;;\r\n" 0276 "TEL;TYPE=\"cell,work\":+1-919-676-9564\r\n" 0277 "UID:testuid\r\n" 0278 "END:VCARD\r\n" 0279 "\r\n"); 0280 QCOMPARE(ba, expected); 0281 } 0282 0283 void PhoneNumberTest::shouldParseVcard3() 0284 { 0285 QByteArray vcarddata( 0286 "BEGIN:VCARD\n" 0287 "VERSION:3.0\n" 0288 "N:LastName;FirstName;;;\n" 0289 "TEL;VALUE=uri;PREF=1;TYPE=\"voice,home\":tel:+44-555-555-5555;ext=5555\r\n" 0290 "TEL;VALUE=uri;TYPE=\"voice,cell,text\":tel:+44-555-555-6666\r\n" 0291 "TEL;VALUE=uri;TYPE=\"voice,work\":tel:+44-555-555-7777\r\n" 0292 "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n" 0293 "LANG:fr\n" 0294 "REV:2015-03-14T09:24:45+00:00\n" 0295 "FN:FirstName LastName\n" 0296 "END:VCARD\n"); 0297 0298 KContacts::VCardTool vcard; 0299 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata); 0300 QCOMPARE(lst.count(), 1); 0301 const KContacts::Addressee &addr = lst.at(0); 0302 QCOMPARE(addr.phoneNumbers().count(), 3); 0303 } 0304 0305 void PhoneNumberTest::shouldParseVcard4() 0306 { 0307 QByteArray vcarddata( 0308 "BEGIN:VCARD\n" 0309 "VERSION:4.0\n" 0310 "N:LastName;FirstName;;;\n" 0311 "TEL;VALUE=uri;PREF=1;TYPE=\"voice,home\":tel:+44-555-555-5555;ext=5555\r\n" 0312 "TEL;VALUE=uri;TYPE=\"voice,cell,text\":tel:+44-555-555-6666\r\n" 0313 "TEL;VALUE=uri;TYPE=\"voice,work\":tel:+44-555-555-7777\r\n" 0314 "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n" 0315 "LANG:fr\n" 0316 "REV:2015-03-14T09:24:45+00:00\n" 0317 "FN:FirstName LastName\n" 0318 "END:VCARD\n"); 0319 0320 KContacts::VCardTool vcard; 0321 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata); 0322 QCOMPARE(lst.count(), 1); 0323 const KContacts::Addressee &addr = lst.at(0); 0324 QCOMPARE(addr.phoneNumbers().count(), 3); 0325 } 0326 0327 void PhoneNumberTest::testNormalizeNumber_data() 0328 { 0329 QTest::addColumn<QString>("input"); 0330 QTest::addColumn<QString>("expected"); 0331 0332 QTest::newRow("empty") << QString() << QString(); 0333 QTest::newRow("normalized") << QStringLiteral("+49123456789") << QStringLiteral("+49123456789"); 0334 QTest::newRow("formatted") << QStringLiteral("+49(123) 456 - 789") << QStringLiteral("+49123456789"); 0335 } 0336 0337 void PhoneNumberTest::testNormalizeNumber() 0338 { 0339 QFETCH(QString, input); 0340 QFETCH(QString, expected); 0341 0342 KContacts::PhoneNumber num; 0343 num.setNumber(input); 0344 QCOMPARE(num.normalizedNumber(), expected); 0345 }