qtmobility/plugins/contacts/qtcontacts-tracker/tests/ut_qtcontacts_sparql/ut_qtcontacts_sparql.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "ut_qtcontacts_sparql.h"
       
    43 
       
    44 #include <QContactManager>
       
    45 #include <QContactName>
       
    46 #include <QContactPhoneNumber>
       
    47 #include <QContactFetchRequest>
       
    48 
       
    49 #include <QtTracker/RDFService>
       
    50 #include <QtTracker/Tracker>
       
    51 
       
    52 #include <QtTracker/ontologies/nco.h>
       
    53 
       
    54 using namespace SopranoLive;
       
    55 
       
    56 ut_qtcontacts_sparql::ut_qtcontacts_sparql()
       
    57 {
       
    58     const QString uuid(QUuid::createUuid());
       
    59 
       
    60     mFirstName = "ut_qtcontacts_sparql_firstname_" + uuid;
       
    61     mLastName = "ut_qtcontacts_sparql_lastname_" + uuid;
       
    62     mPhoneNumber = "ut_qtcontacts_sparql_phone_" + uuid;
       
    63     mPhoneNumber = mPhoneNumber.replace('-', '_');
       
    64 
       
    65     mNameFilter.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst);
       
    66     mNameFilter.setMatchFlags(QContactFilter::MatchExactly);
       
    67     mNameFilter.setValue(mFirstName);
       
    68 }
       
    69 
       
    70 void ut_qtcontacts_sparql::setupTestContact(QContact &contact)
       
    71 {
       
    72     // attach a name to the contact
       
    73     QContactName name;
       
    74 
       
    75     name.setFirstName(mFirstName);
       
    76     name.setLastName(mLastName);
       
    77 
       
    78     QVERIFY(contact.saveDetail(&name));
       
    79 
       
    80     // attach a phone number to the contact
       
    81     QContactPhoneNumber phone;
       
    82 
       
    83     phone.setSubTypes(QContactPhoneNumber::SubTypeMobile);
       
    84     phone.setContexts(QContactPhoneNumber::ContextHome);
       
    85     phone.setNumber(mPhoneNumber);
       
    86 
       
    87     QVERIFY(contact.saveDetail(&phone));
       
    88 }
       
    89 
       
    90 void ut_qtcontacts_sparql::checkDatabaseEmpty()
       
    91 {
       
    92     // try to sparql our testing contact
       
    93     QContactFetchRequest request;
       
    94 
       
    95     request.setManager(mContactManager);
       
    96     request.setFilter(mNameFilter);
       
    97     request.start();
       
    98 
       
    99     // wait for the request to finish
       
   100     waitForRequest(request);
       
   101     CHECK_CURRENT_TEST_FAILED;
       
   102 
       
   103     // verify that there really is no test contact yet
       
   104     QVERIFY(request.isFinished());
       
   105     QVERIFY(request.contacts().isEmpty());
       
   106 }
       
   107 
       
   108 template <class PhoneNumberType>
       
   109 void ut_qtcontacts_sparql::checkOntology(QContactLocalId contactId, int expectedCount)
       
   110 {
       
   111     // fetch contact from tracker
       
   112     const QUrl id(QString("contact:%1").arg(contactId));
       
   113     Live<nco::PersonContact> contact = ::tracker()->liveNode(id);
       
   114     QCOMPARE(contact->getContactUID(), QString::number(contactId));
       
   115 
       
   116     // check first name property of the contact
       
   117     const QStringList givenNames(contact->getNameGivens());
       
   118     QCOMPARE(givenNames.count(), 1);
       
   119     QCOMPARE(givenNames[0], mFirstName);
       
   120 
       
   121     // check last name property of the contact
       
   122     const QStringList familyNames(contact->getNameFamilys());
       
   123     QCOMPARE(familyNames.count(), 1);
       
   124     QCOMPARE(familyNames[0], mLastName);
       
   125 
       
   126     // check if the contact has the expected number of phone numbers
       
   127     const QList< Live<PhoneNumberType> > hasPhoneNumbers(contact->getHasPhoneNumbers());
       
   128     QCOMPARE(hasPhoneNumbers.count(), expectedCount);
       
   129 
       
   130     // check if the contact's actual phone number
       
   131     const QStringList phoneNumbers(hasPhoneNumbers[0]->getPhoneNumbers());
       
   132     QCOMPARE(phoneNumbers.count(), 1);
       
   133     QCOMPARE(phoneNumbers[0], mPhoneNumber);
       
   134 }
       
   135 
       
   136 void ut_qtcontacts_sparql::testSaveContact()
       
   137 {
       
   138     // check that we start with a clean database
       
   139     checkDatabaseEmpty();
       
   140     CHECK_CURRENT_TEST_FAILED;
       
   141 
       
   142     // create a named contact and save it
       
   143     QContact contact;
       
   144 
       
   145     setupTestContact(contact);
       
   146     CHECK_CURRENT_TEST_FAILED;
       
   147 
       
   148     saveContact(contact);
       
   149     CHECK_CURRENT_TEST_FAILED;
       
   150 
       
   151     // now use RDF queries to check what landed within the database
       
   152     checkOntology<nco::CellPhoneNumber>(contact.localId());
       
   153     CHECK_CURRENT_TEST_FAILED;
       
   154 }
       
   155 
       
   156 // this test basically checks for NB#158859
       
   157 void ut_qtcontacts_sparql::testModifyContact()
       
   158 {
       
   159     // check that we start with a clean database
       
   160     checkDatabaseEmpty();
       
   161     CHECK_CURRENT_TEST_FAILED;
       
   162 
       
   163     // create a named contact and save it
       
   164     QContact contact;
       
   165 
       
   166     setupTestContact(contact);
       
   167     CHECK_CURRENT_TEST_FAILED;
       
   168 
       
   169     saveContact(contact);
       
   170     CHECK_CURRENT_TEST_FAILED;
       
   171 
       
   172     // check that the contact's phone number has the expected subtype
       
   173     QList<QContactDetail> details(contact.details(QContactPhoneNumber::DefinitionName));
       
   174     QCOMPARE(details.count(), 1);
       
   175 
       
   176     QContactPhoneNumber phoneNumber(details[0]);
       
   177     QCOMPARE(phoneNumber.subTypes(), QStringList(QContactPhoneNumber::SubTypeMobile));
       
   178 
       
   179     // modify the phone number's subtype and save the modified contact
       
   180     phoneNumber.setSubTypes(QContactPhoneNumber::SubTypeFacsimile);
       
   181     QVERIFY(contact.saveDetail(&phoneNumber));
       
   182 
       
   183     saveContact(contact);
       
   184     CHECK_CURRENT_TEST_FAILED;
       
   185 
       
   186     // check that the saved contact's phone number has the expected subtype
       
   187     details = contact.details(QContactPhoneNumber::DefinitionName);
       
   188     QCOMPARE(details.count(), 1);
       
   189 
       
   190     phoneNumber = details[0];
       
   191     QCOMPARE(phoneNumber.subTypes(), QStringList(QContactPhoneNumber::SubTypeFacsimile));
       
   192 
       
   193     // now use RDF queries to check what landed within the database
       
   194     checkOntology<nco::CellPhoneNumber>(contact.localId());
       
   195     CHECK_CURRENT_TEST_FAILED;
       
   196 
       
   197     // we should have got a fax number assigned...
       
   198     checkOntology<nco::FaxNumber>(contact.localId());
       
   199     CHECK_CURRENT_TEST_FAILED;
       
   200 
       
   201     checkOntology<nco::PhoneNumber>(contact.localId());
       
   202     CHECK_CURRENT_TEST_FAILED;
       
   203 
       
   204     // ...and the cell phone numbers shall be gone
       
   205     checkOntology<nco::CellPhoneNumber>(contact.localId(), 0);
       
   206     CHECK_CURRENT_TEST_FAILED;
       
   207 }
       
   208 
       
   209 QTEST_MAIN(ut_qtcontacts_sparql)