qtmobility/tests/auto/ut_qtcontacts_add_async/ut_qtcontacts_add_async.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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_add_async.h"
       
    43 
       
    44 #include <QContactManager>
       
    45 #include <QContactName>
       
    46 #include <QContactDetailFilter>
       
    47 #include <QtTest/QtTest>
       
    48 #include <QDebug>
       
    49 
       
    50 // Note that we try to avoid using any names that might already be in the database:
       
    51 const char* TESTNAME_FIRST = "ut_qtcontacts_add_firstname";
       
    52 const char* TESTNAME_LAST = "ut_qtcontacts_add_firstlast";
       
    53 
       
    54 ut_qtcontacts_add::ut_qtcontacts_add()
       
    55 : getExistingContactFinishedCallback(0),
       
    56   waiting(false)
       
    57 {
       
    58 }
       
    59 
       
    60 ut_qtcontacts_add::~ut_qtcontacts_add()
       
    61 {
       
    62 
       
    63 }
       
    64 
       
    65 void ut_qtcontacts_add::initTestCase()
       
    66 {
       
    67 }
       
    68 
       
    69 void ut_qtcontacts_add::cleanupTestCase()
       
    70 {
       
    71 }
       
    72 
       
    73 void ut_qtcontacts_add::init()
       
    74 {
       
    75 }
       
    76 
       
    77 void ut_qtcontacts_add::cleanup()
       
    78 {
       
    79   waiting = false;
       
    80 }
       
    81 
       
    82 bool ut_qtcontacts_add::waitForStop()
       
    83 {
       
    84     waiting = true;
       
    85 
       
    86     const int max_secs = 100000;
       
    87 
       
    88     // wait for signal
       
    89     int i = 0;
       
    90     while(waiting && i++ < max_secs) {
       
    91         // Allow the mainloop to run:
       
    92         QTest::qWait(10);
       
    93     }
       
    94 
       
    95     return !waiting;
       
    96 }
       
    97 
       
    98 QContactManager* ut_qtcontacts_add::getContactManager()
       
    99 {
       
   100     static QContactManager manager("tracker");
       
   101     return &manager;
       
   102 }
       
   103 
       
   104 void ut_qtcontacts_add::onContactFetchRequestProgress()
       
   105 {
       
   106     //qDebug() << "onContactFetchRequestProgress";
       
   107     if (!contactFetchRequest.isFinished())
       
   108         return;
       
   109 
       
   110     //Store the contact so the callback can use it.
       
   111     if(!(contactFetchRequest.contacts().isEmpty())) {
       
   112         contact = contactFetchRequest.contacts()[0];
       
   113         QVERIFY(contact.localId() != 0);
       
   114         contactFetchRequest.cancel(); //Stop any more slot calls.
       
   115     }
       
   116 
       
   117     //qDebug() << "debug: fetched localId=" << contact.localId();
       
   118 
       
   119     //Avoid more slot calls, though this is unlikely because it has finished.
       
   120     contactFetchRequest.cancel();
       
   121 
       
   122     //Call the callback method that was specified to getExistingContact():
       
   123     if(getExistingContactFinishedCallback) {
       
   124         FinishedCallbackFunc func = getExistingContactFinishedCallback;
       
   125         getExistingContactFinishedCallback = 0;
       
   126         (this->*func)();
       
   127     }
       
   128 }
       
   129 
       
   130 
       
   131 void ut_qtcontacts_add::getExistingContact(FinishedCallbackFunc finishedCallback)
       
   132 {
       
   133     QContactManager* manager = getContactManager();
       
   134     Q_ASSERT(manager);
       
   135 
       
   136     // Stop pending fetch requests
       
   137     if (contactFetchRequest.isActive())
       
   138         contactFetchRequest.cancel();
       
   139 
       
   140     // Initialize the result.
       
   141     contact = QContact();
       
   142 
       
   143     //TODO: How can we AND on both the first and last name?
       
   144     getExistingContactFinishedCallback = finishedCallback; //Call this when the contact has been retrieved.
       
   145     connect(&contactFetchRequest, SIGNAL(resultsAvailable()),
       
   146         SLOT(onContactFetchRequestProgress()));
       
   147 
       
   148     QContactDetailFilter nameFilter;
       
   149     nameFilter.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldFirst);
       
   150     nameFilter.setValue(QLatin1String(TESTNAME_FIRST));
       
   151     nameFilter.setMatchFlags(QContactFilter::MatchExactly);
       
   152     contactFetchRequest.setManager(manager);
       
   153     contactFetchRequest.setFilter(nameFilter);
       
   154 
       
   155     //qDebug() << "debug: start request";
       
   156     contactFetchRequest.start();
       
   157 }
       
   158 
       
   159 //This is our actual test function:
       
   160 void ut_qtcontacts_add::ut_testAddContact()
       
   161 {
       
   162     //qDebug() << "debug: ut_testAddContact";
       
   163     //Make sure that the contact is not already in the database.
       
   164     getExistingContact(&ut_qtcontacts_add::onContactFoundThenRemoveAndTest);
       
   165 
       
   166     //Block (allowing the mainloop to run) until we have finished.
       
   167     waitForStop();
       
   168 }
       
   169 
       
   170 void ut_qtcontacts_add::onContactFoundThenRemoveAndTest()
       
   171 {
       
   172     //qDebug() << "debug: Removing the existing contact, if it exists.";
       
   173     QContactManager* manager = getContactManager();
       
   174     Q_ASSERT(manager);
       
   175 
       
   176     //TODO: Find and use an async API that tells us when it has finished.
       
   177     manager->removeContact(contact.localId());
       
   178 
       
   179     // Run the Qt UI's main loop, showing the editor while it is running:
       
   180     QTimer::singleShot(2000, this, SLOT(onTimeoutAddContact()));
       
   181 }
       
   182 
       
   183 void ut_qtcontacts_add::onTimeoutAddContact()
       
   184 {
       
   185     //qDebug() << "debug: Trying to add contact.";
       
   186 
       
   187     // Offer a UI to edit a prefilled contact.
       
   188     QContactName name;
       
   189     name.setFirstName(QLatin1String(TESTNAME_FIRST));
       
   190     name.setLastName(QLatin1String(TESTNAME_LAST));
       
   191     //TODO: Find and use an async API that tells us when it has finished.
       
   192     contact.saveDetail(&name);
       
   193     //const bool saved = contact.saveDetail(&name);
       
   194     //Q_ASSERT(saved); //This won't necessarily be useful because our implementation doesn't support sync methods.
       
   195 
       
   196     //Save the contact.
       
   197     //But note that our QContactManager backend does not set localId when returning.
       
   198     QContactManager* manager = getContactManager();
       
   199     Q_ASSERT(manager);
       
   200 
       
   201 
       
   202     manager->saveContact(&contact);
       
   203     //This works too:
       
   204     //QContact copy(contact);
       
   205     //manager->saveContact(&copy);
       
   206 
       
   207     //Check that it was really saved:
       
   208     //qDebug() << "debug: checking that the contact was saved.";
       
   209     getExistingContact(&ut_qtcontacts_add::onContactFoundThenCheck);
       
   210 }
       
   211 
       
   212 void ut_qtcontacts_add::onContactFoundThenCheck()
       
   213 {
       
   214     //Check that it was really saved:
       
   215     // The ContactManager::saveContact() documentation suggests that localeId=0 is for non-saved contacts.
       
   216     //QEXPECT_FAIL("", "QContactManager::saveContact() saves the contact (see it by running the contacts UI), but returns false and doesn't set error(). Find out why.", Continue);
       
   217     QVERIFY(contact.localId() != 0);
       
   218 
       
   219     //Check that the correct details were saved:
       
   220     const QContactName name = contact.detail<QContactName>();
       
   221     QVERIFY(name.firstName() == QLatin1String(TESTNAME_FIRST));
       
   222     QVERIFY(name.lastName() == QLatin1String(TESTNAME_LAST));
       
   223 
       
   224     //Try to restore original conditions:
       
   225     getExistingContact(&ut_qtcontacts_add::onContactFoundThenRemoveAndStop);
       
   226 }
       
   227 
       
   228 void ut_qtcontacts_add::onContactFoundThenRemoveAndStop()
       
   229 {
       
   230     //qDebug() << "debug: ut_qtcontacts_add::onContactFoundThenRemoveAndStop";
       
   231     QContactManager* manager = getContactManager();
       
   232     Q_ASSERT(manager);
       
   233     manager->removeContact(contact.localId());
       
   234 
       
   235     // Allow the actual test function to return:
       
   236     waiting = false;
       
   237 }
       
   238 
       
   239 
       
   240 
       
   241 QTEST_MAIN(ut_qtcontacts_add)