qtmobility/plugins/contacts/symbian/tsrc/tst_cntfilteringdbms/tst_cntfilteringdbms.cpp
changeset 4 90517678cc4f
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 #define QT_STATICPLUGIN
       
    43 #include "cntsymbianfilterdbms.h"
       
    44 
       
    45 #include <cntdb.h>
       
    46 
       
    47 #include <qtcontacts.h>
       
    48 #include <QContactDetailFilter.h>
       
    49 #include <QtTest/QtTest>
       
    50 //#include <QMetaType>
       
    51 
       
    52 QTM_USE_NAMESPACE
       
    53 
       
    54 typedef QList<QContactLocalId> QContactIds;
       
    55 
       
    56 Q_DECLARE_METATYPE(QContactFilter *)
       
    57 Q_DECLARE_METATYPE(CntAbstractContactFilter::FilterSupport)
       
    58 Q_DECLARE_METATYPE(QContactIds)
       
    59 
       
    60 class tst_cntfilteringdbms : public QObject
       
    61 {
       
    62 Q_OBJECT
       
    63 
       
    64 public:
       
    65     tst_cntfilteringdbms();
       
    66     virtual ~tst_cntfilteringdbms();
       
    67 
       
    68 private slots:  // Init & cleanup
       
    69 	void initTestCase();
       
    70 	void cleanupTestCase();
       
    71 
       
    72 private slots: // tests
       
    73     void filterContacts();
       
    74     void filterContacts_data() {addFilterData();}
       
    75     
       
    76 private:
       
    77     QContactLocalId addContact(QString firstName, QString lastName, QString phoneNumber);
       
    78     void addFilterData();
       
    79     void addNewRow(QString defName, QString fieldName, int flags, CntAbstractContactFilter::FilterSupport filterSupport, QString filterCriterion, QList<QContactLocalId> matchingContacts);
       
    80     
       
    81 private:
       
    82     QScopedPointer<QContactManager> m_contactManager;
       
    83     QScopedPointer<CContactDatabase> m_contactDatabase;
       
    84     QScopedPointer<CntSymbianFilter> m_dbmsFilter;
       
    85     QList<QContactFilter> m_contactFilters;
       
    86 };
       
    87 
       
    88 tst_cntfilteringdbms::tst_cntfilteringdbms()
       
    89 {
       
    90     TRAPD(error, m_contactDatabase.reset(CContactDatabase::OpenL()));
       
    91     QVERIFY(error == KErrNone);
       
    92     m_dbmsFilter.reset(new CntSymbianFilter(*m_contactDatabase));
       
    93     m_contactManager.reset(new QContactManager("symbian"));
       
    94 }
       
    95 
       
    96 tst_cntfilteringdbms::~tst_cntfilteringdbms()
       
    97 {
       
    98  
       
    99 }
       
   100 
       
   101 void tst_cntfilteringdbms::initTestCase()
       
   102 {
       
   103     // Remove all contacts
       
   104     QList<QContactLocalId> contacts = m_contactManager->contactIds();
       
   105     m_contactManager->removeContacts(contacts, 0);
       
   106 }
       
   107 
       
   108 void tst_cntfilteringdbms::cleanupTestCase()
       
   109 {
       
   110 }
       
   111 
       
   112 void tst_cntfilteringdbms::filterContacts()
       
   113 {
       
   114     QFETCH(QContactFilter *, filter);
       
   115     QFETCH(CntAbstractContactFilter::FilterSupport, filterSupport);
       
   116     QFETCH(QContactIds, matchingContacts);
       
   117     
       
   118     QCOMPARE(m_dbmsFilter->filterSupportLevel(*filter), filterSupport);   
       
   119     
       
   120     QList<QContactSortOrder> sortOrders;
       
   121     QStringList definitionRestrictions;
       
   122     QList<QContactLocalId> contacts = m_contactManager->contactIds(*filter);
       
   123     
       
   124     qDebug() << contacts;
       
   125     
       
   126     QCOMPARE(contacts.count(), matchingContacts.count());
       
   127     foreach(QContactLocalId id, matchingContacts) {
       
   128         QVERIFY(contacts.contains(id));
       
   129     }    
       
   130 }
       
   131 
       
   132 QContactLocalId tst_cntfilteringdbms::addContact(QString firstName, QString lastName, QString phoneNumber)
       
   133 {
       
   134     QContact c;
       
   135     
       
   136     QContactName n;
       
   137     n.setFirstName(firstName);
       
   138     n.setLastName(lastName);
       
   139     c.saveDetail(&n);
       
   140     
       
   141     QContactPhoneNumber nb;
       
   142     nb.setNumber(phoneNumber);
       
   143     c.saveDetail(&nb);
       
   144     
       
   145     m_contactManager->saveContact(&c);
       
   146     
       
   147     return c.localId();
       
   148 }
       
   149 
       
   150 void tst_cntfilteringdbms::addFilterData()
       
   151 {
       
   152     QTest::addColumn<QContactFilter *>("filter");
       
   153     QTest::addColumn<CntAbstractContactFilter::FilterSupport>("filterSupport");
       
   154     QTest::addColumn<QContactIds>("matchingContacts");
       
   155     
       
   156     QContactLocalId abc = addContact("abc", "def", "123");
       
   157     QContactLocalId bcd = addContact("bcd", "efg", "1234567");
       
   158     QContactLocalId cde = addContact("cde", "fgh", "1234567890");
       
   159     QContactLocalId Abc = addContact("Abc", "Def", "+3581234567890");
       
   160     QContactLocalId Bcd = addContact("Bcd", "Efg", "0987654321");
       
   161     QContactLocalId Cde = addContact("Cde", "Fgh", "1111111111");
       
   162     
       
   163     qDebug() << "abc =" << abc;
       
   164     qDebug() << "bcd =" << bcd;
       
   165     qDebug() << "cde =" << cde;
       
   166     qDebug() << "Abc =" << Abc;
       
   167     qDebug() << "Bcd =" << Bcd;
       
   168     qDebug() << "Cde =" << Cde;
       
   169     
       
   170     QList<QContactLocalId> allContacts;
       
   171     allContacts << abc << bcd << cde << Abc << Bcd << Cde;
       
   172     
       
   173     addNewRow(QContactPhoneNumber::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
       
   174     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "123", QContactIds() << abc);
       
   175     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "1234567", QContactIds() << bcd);
       
   176     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "1234567890", QContactIds() << cde);
       
   177     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchContains, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc << bcd << cde << Abc);
       
   178     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchStartsWith, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc << bcd << cde);
       
   179     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchEndsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "567", QContactIds() << bcd );
       
   180     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchEndsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "4567890", QContactIds() << cde << Abc );
       
   181     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchFixedString, CntAbstractContactFilter::SupportedPreFilterOnly, "123", QContactIds() << abc);
       
   182     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchPhoneNumber, CntAbstractContactFilter::Supported, "1234567890", QContactIds() << cde << Abc);
       
   183     addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc << bcd << cde << Abc);
       
   184     
       
   185     addNewRow(QContactName::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
       
   186     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "Abc", QContactIds() << abc << Abc);
       
   187     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchExactly | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Abc", QContactIds() << Abc);
       
   188     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchContains, CntAbstractContactFilter::Supported, "Ab", QContactIds() << abc << Abc);
       
   189     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchContains | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << Abc);
       
   190     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchStartsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << abc << Abc);
       
   191     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchStartsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << Abc);
       
   192     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchEndsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "Cde", QContactIds() << cde << Cde);
       
   193     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchEndsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Cde", QContactIds() << Cde);
       
   194     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchFixedString, CntAbstractContactFilter::SupportedPreFilterOnly, "abc", QContactIds() << abc << Abc);
       
   195     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchFixedString | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "abc", QContactIds() << abc);
       
   196     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchPhoneNumber, CntAbstractContactFilter::NotSupported, "abc", QContactIds() << abc << Abc); // not sure if this valid
       
   197     addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "abc", QContactIds() << abc << Abc);
       
   198     
       
   199     addNewRow(QContactDisplayLabel::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
       
   200     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc);
       
   201     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchExactly | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc);
       
   202     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchContains, CntAbstractContactFilter::NotSupported, "d e", QContactIds() << bcd << Bcd);
       
   203     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchContains | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "d e", QContactIds() << bcd);
       
   204     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchStartsWith, CntAbstractContactFilter::Supported, "B", QContactIds() << bcd << Bcd);
       
   205     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchStartsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "B", QContactIds() << Bcd);
       
   206     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchEndsWith, CntAbstractContactFilter::NotSupported, "d Efg", QContactIds() << bcd << Bcd);
       
   207     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchEndsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "d Efg", QContactIds() << Bcd);
       
   208     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchFixedString, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc);
       
   209     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchFixedString | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc);
       
   210     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchPhoneNumber, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc); // not sure if this is valid
       
   211     addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc);
       
   212 }
       
   213 
       
   214 
       
   215 
       
   216 void tst_cntfilteringdbms::addNewRow(QString defName, QString fieldName, int flags, CntAbstractContactFilter::FilterSupport filterSupport, QString filterCriterion, QList<QContactLocalId> matchingContacts)
       
   217 {
       
   218     QString s;
       
   219     foreach(QContactLocalId id, matchingContacts)
       
   220         s.append(QString("%1 ").arg(id));
       
   221     
       
   222     QString title = QString("Detail filter : defName=%1 fieldName=%2 matchFlags=0x%3 filterCriterion=%4 matchingContacts=%5").arg(defName).arg(fieldName).arg(flags,0,16).arg(filterCriterion).arg(s);
       
   223     
       
   224     QContactDetailFilter *f = new QContactDetailFilter();
       
   225     f->setDetailDefinitionName(defName, fieldName);
       
   226     f->setMatchFlags(QContactFilter::MatchFlags(flags));
       
   227     f->setValue(QVariant(filterCriterion));
       
   228     
       
   229     QTest::newRow(title.toAscii().constData()) << (QContactFilter*) f << filterSupport << matchingContacts;
       
   230 }
       
   231 
       
   232 QTEST_MAIN(tst_cntfilteringdbms);
       
   233 #include "tst_cntfilteringdbms.moc"
       
   234