From c4febbf6f88395e8738ecf1c8de3e36333014bde Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Tue, 14 Jan 2014 17:07:22 -0800 Subject: [PATCH] [libcontacts] Add test for SeasideImport --- tests/tests.pro | 2 +- tests/tests.xml.in | 3 + tests/tst_seasideimport/tst_seasideimport.cpp | 505 ++++++++++++++++++ tests/tst_seasideimport/tst_seasideimport.pro | 8 + 4 files changed, 517 insertions(+), 1 deletion(-) create mode 100644 tests/tst_seasideimport/tst_seasideimport.cpp create mode 100644 tests/tst_seasideimport/tst_seasideimport.pro diff --git a/tests/tests.pro b/tests/tests.pro index 8680be2..1f97396 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -1,7 +1,7 @@ include(../package.pri) TEMPLATE = subdirs -SUBDIRS = tst_synchronizelists +SUBDIRS = tst_synchronizelists tst_seasideimport tests_xml.target = tests.xml tests_xml.depends = $$PWD/tests.xml.in diff --git a/tests/tests.xml.in b/tests/tests.xml.in index a5faa91..66a0251 100644 --- a/tests/tests.xml.in +++ b/tests/tests.xml.in @@ -7,6 +7,9 @@ /opt/tests/@PACKAGENAME@/tst_synchronizelists + + /opt/tests/@PACKAGENAME@/tst_seasideimport + diff --git a/tests/tst_seasideimport/tst_seasideimport.cpp b/tests/tst_seasideimport/tst_seasideimport.cpp new file mode 100644 index 0000000..f52ee6f --- /dev/null +++ b/tests/tst_seasideimport/tst_seasideimport.cpp @@ -0,0 +1,505 @@ +/* + * Copyright (C) 2014 Jolla Mobile + * + * You may use this file under the terms of the BSD license as follows: + * + * "Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Nemo Mobile nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." + */ + +#include "seasideimport.h" + +#include +#include +#include +#include + +#include + +#include +#include + +#ifdef USING_QTPIM +QTVERSIT_USE_NAMESPACE +#endif + +class tst_SeasideImport : public QObject +{ + Q_OBJECT + +public: + tst_SeasideImport(); + + static QList processVCard(const char *vCardData); + +private slots: + void name(); + void formattedName(); + void nickname(); + void unidentified(); + + void nonmergedName(); + void nonmergedNickname(); + void nonmergedUid(); + + void mergedName(); + void mergedNickname(); + void mergedUid(); +}; + + +tst_SeasideImport::tst_SeasideImport() +{ +} + +QList tst_SeasideImport::processVCard(const char *vCardData) +{ + const QByteArray ba(vCardData); + QVersitReader reader(ba); + if (reader.startReading() && reader.waitForFinished()) { + return SeasideImport::buildImportContacts(reader.results()); + } + + return QList(); +} + +void tst_SeasideImport::name() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:Springfield;Jebediah;;;\r\n" +"FN:\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Jebediah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); +} + +void tst_SeasideImport::formattedName() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:;;;;\r\n" +"FN:Jebediah Springfield\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Jebediah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); +} + +void tst_SeasideImport::nickname() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:;;;;\r\n" +"NICKNAME:Jebediah Springfield\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Jebediah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); +} + +void tst_SeasideImport::unidentified() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:;;;;\r\n" +"FN:\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("555-1234")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); +} + +void tst_SeasideImport::nonmergedName() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:Springfield;Jebediah;;;\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"N:Springfield;Obadiah;;;\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 2); + { + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Jebediah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + } + { + const QContact &c(contacts.at(1)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Obadiah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + } +} + +void tst_SeasideImport::nonmergedNickname() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"FN:Jebediah Springfield\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"NICKNAME:Obadiah Springfield\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 2); + { + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Jebediah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + } + { + const QContact &c(contacts.at(1)); + + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Obadiah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + } +} + +void tst_SeasideImport::nonmergedUid() +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + QSKIP("UID matches should be ignored where QContactName details differ"); +#else + QSKIP("UID matches should be ignored where QContactName details differ", SkipAll); +#endif + + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:Springfield;Jebediah;;;\r\n" +"TEL;VOICE:555-1234\r\n" +"UID:uid-1\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"N:Springfield;Obadiah;;;\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"UID:uid-1\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 2); + { + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Jebediah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + + const QList guids(c.details()); + QCOMPARE(guids.count(), 1); + + const QContactGuid guid(guids.at(0)); + QCOMPARE(guid.guid(), QString::fromLatin1("uid-1")); + } + { + const QContact &c(contacts.at(1)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Obadiah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 1); + + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + + const QList guids(c.details()); + QCOMPARE(guids.count(), 0); + } +} + +void tst_SeasideImport::mergedName() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"N:Springfield;Jebediah;;;\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"N:Springfield;Jebediah;;;\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.firstName(), QString::fromLatin1("Jebediah")); + QCOMPARE(name.lastName(), QString::fromLatin1("Springfield")); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 0); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 2); + { + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + } + { + const QContactPhoneNumber phone(phoneNumbers.at(1)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + } +} + +void tst_SeasideImport::mergedNickname() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"FN:Jebediah Springfield\r\n" +"TEL;VOICE:555-1234\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"NICKNAME:Jebediah Springfield\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Jebediah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 2); + { + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + } + { + const QContactPhoneNumber phone(phoneNumbers.at(1)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + } +} + +void tst_SeasideImport::mergedUid() +{ + const char *vCardData = +"BEGIN:VCARD\r\n" +"FN:Jebediah Springfield\r\n" +"TEL;VOICE:555-1234\r\n" +"UID:uid-1\r\n" +"END:VCARD\r\n" +"BEGIN:VCARD\r\n" +"FN:Obadiah Springfield\r\n" +"TEL;TYPE=CELL:555-6789\r\n" +"UID:uid-1\r\n" +"END:VCARD\r\n"; + + const QList contacts(processVCard(vCardData)); + QCOMPARE(contacts.count(), 1); + + const QContact &c(contacts.at(0)); + + const QContactName name(c.detail()); + QCOMPARE(name.isEmpty(), true); + + const QList nicknames(c.details()); + QCOMPARE(nicknames.count(), 1); + + const QContactNickname nick(nicknames.at(0)); + QCOMPARE(nick.nickname(), QString::fromLatin1("Jebediah Springfield")); + + const QList phoneNumbers(c.details()); + QCOMPARE(phoneNumbers.count(), 2); + { + const QContactPhoneNumber phone(phoneNumbers.at(0)); + QCOMPARE(phone.number(), QString::fromLatin1("555-1234")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeVoice)); + } + { + const QContactPhoneNumber phone(phoneNumbers.at(1)); + QCOMPARE(phone.number(), QString::fromLatin1("555-6789")); + QCOMPARE(phone.subTypes(), QList() << static_cast(QContactPhoneNumber::SubTypeMobile)); + } + + const QList guids(c.details()); + QCOMPARE(guids.count(), 1); + + const QContactGuid guid(guids.at(0)); + QCOMPARE(guid.guid(), QString::fromLatin1("uid-1")); +} + +#include "tst_seasideimport.moc" +QTEST_APPLESS_MAIN(tst_SeasideImport) diff --git a/tests/tst_seasideimport/tst_seasideimport.pro b/tests/tst_seasideimport/tst_seasideimport.pro new file mode 100644 index 0000000..3212e69 --- /dev/null +++ b/tests/tst_seasideimport/tst_seasideimport.pro @@ -0,0 +1,8 @@ +include(../../config.pri) +include(../common.pri) +TARGET = tst_seasideimport + +SOURCES += tst_seasideimport.cpp + +equals(QT_MAJOR_VERSION, 4): LIBS += ../../src/libcontactcache.so +equals(QT_MAJOR_VERSION, 5): LIBS += ../../src/libcontactcache-qt5.so