summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/plugin/tsrc/tst_cntfilteringdbms/tst_cntfilteringdbms.cpp
blob: 8dd01ebd871b9f248589ef915b2a0a25f9d3dbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#define QT_STATICPLUGIN
#include "cntsymbianfilterdbms.h"

#include <cntdb.h>

#include <qtcontacts.h>
#include <QContactDetailFilter.h>
#include <QtTest/QtTest>
//#include <QMetaType>

QTM_USE_NAMESPACE

typedef QList<QContactLocalId> QContactIds;

Q_DECLARE_METATYPE(QContactFilter *)
Q_DECLARE_METATYPE(CntAbstractContactFilter::FilterSupport)
Q_DECLARE_METATYPE(QContactIds)

class tst_cntfilteringdbms : public QObject
{
Q_OBJECT

public:
    tst_cntfilteringdbms();
    virtual ~tst_cntfilteringdbms();

private slots:  // Init & cleanup
	void initTestCase();
	void cleanupTestCase();

private slots: // tests
    void filterContacts();
    void filterContacts_data() {addFilterData();}
    
private:
    QContactLocalId addContact(QString firstName, QString lastName, QString phoneNumber);
    void addFilterData();
    void addNewRow(QString defName, QString fieldName, int flags, CntAbstractContactFilter::FilterSupport filterSupport, QString filterCriterion, QList<QContactLocalId> matchingContacts);
    
private:
    QScopedPointer<QContactManager> m_contactManager;
    QScopedPointer<CContactDatabase> m_contactDatabase;
    QScopedPointer<CntSymbianFilter> m_dbmsFilter;
    QList<QContactFilter> m_contactFilters;
};

tst_cntfilteringdbms::tst_cntfilteringdbms()
{
    TRAPD(error, m_contactDatabase.reset(CContactDatabase::OpenL()));
    QVERIFY(error == KErrNone);
    m_dbmsFilter.reset(new CntSymbianFilter(*m_contactDatabase));
    m_contactManager.reset(new QContactManager("symbian"));
}

tst_cntfilteringdbms::~tst_cntfilteringdbms()
{
 
}

void tst_cntfilteringdbms::initTestCase()
{
    // Remove all contacts
    QList<QContactLocalId> contacts = m_contactManager->contactIds();
    m_contactManager->removeContacts(contacts, 0);
}

void tst_cntfilteringdbms::cleanupTestCase()
{
}

void tst_cntfilteringdbms::filterContacts()
{
    QFETCH(QContactFilter *, filter);
    QFETCH(CntAbstractContactFilter::FilterSupport, filterSupport);
    QFETCH(QContactIds, matchingContacts);
    
    QCOMPARE(m_dbmsFilter->filterSupportLevel(*filter), filterSupport);   
    
    QList<QContactSortOrder> sortOrders;
    QStringList definitionRestrictions;
    QList<QContactLocalId> contacts = m_contactManager->contactIds(*filter);
    
    qDebug() << contacts;
    
    QCOMPARE(contacts.count(), matchingContacts.count());
    foreach(QContactLocalId id, matchingContacts) {
        QVERIFY(contacts.contains(id));
    }    
}

QContactLocalId tst_cntfilteringdbms::addContact(QString firstName, QString lastName, QString phoneNumber)
{
    QContact c;
    
    QContactName n;
    n.setFirstName(firstName);
    n.setLastName(lastName);
    c.saveDetail(&n);
    
    QContactPhoneNumber nb;
    nb.setNumber(phoneNumber);
    c.saveDetail(&nb);
    
    m_contactManager->saveContact(&c);
    
    return c.localId();
}

void tst_cntfilteringdbms::addFilterData()
{
    QTest::addColumn<QContactFilter *>("filter");
    QTest::addColumn<CntAbstractContactFilter::FilterSupport>("filterSupport");
    QTest::addColumn<QContactIds>("matchingContacts");
    
    QContactLocalId abc = addContact("abc", "def", "123");
    QContactLocalId bcd = addContact("bcd", "efg", "1234567");
    QContactLocalId cde = addContact("cde", "fgh", "1234567890");
    QContactLocalId Abc = addContact("Abc", "Def", "+3581234567890");
    QContactLocalId Bcd = addContact("Bcd", "Efg", "0987654321");
    QContactLocalId Cde = addContact("Cde", "Fgh", "1111111111");
    
    qDebug() << "abc =" << abc;
    qDebug() << "bcd =" << bcd;
    qDebug() << "cde =" << cde;
    qDebug() << "Abc =" << Abc;
    qDebug() << "Bcd =" << Bcd;
    qDebug() << "Cde =" << Cde;
    
    QList<QContactLocalId> allContacts;
    allContacts << abc << bcd << cde << Abc << Bcd << Cde;
    
    addNewRow(QContactPhoneNumber::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "1234567", QContactIds() << bcd);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "1234567890", QContactIds() << cde);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchContains, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc << bcd << cde << Abc);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchStartsWith, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc << bcd << cde);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchEndsWith, CntAbstractContactFilter::NotSupported, "567", QContactIds() << bcd );
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchEndsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "4567890", QContactIds() << cde << Abc );
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchFixedString, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchPhoneNumber, CntAbstractContactFilter::Supported, "1234567890", QContactIds() << cde << Abc);
    addNewRow(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "123", QContactIds() << abc);
    
    addNewRow(QContactName::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchExactly, CntAbstractContactFilter::SupportedPreFilterOnly, "Abc", QContactIds() << abc << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchExactly | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Abc", QContactIds() << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchContains, CntAbstractContactFilter::Supported, "Ab", QContactIds() << abc << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchContains | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchStartsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << abc << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchStartsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Ab", QContactIds() << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchEndsWith, CntAbstractContactFilter::SupportedPreFilterOnly, "Cde", QContactIds() << cde << Cde);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchEndsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "Cde", QContactIds() << Cde);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchFixedString, CntAbstractContactFilter::SupportedPreFilterOnly, "abc", QContactIds() << abc << Abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchFixedString | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "abc", QContactIds() << abc);
    addNewRow(QContactName::DefinitionName, QContactName::FieldFirstName, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "223", QContactIds() << bcd << Bcd);
    
    addNewRow(QContactDisplayLabel::DefinitionName, QString(), QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "foobar", allContacts);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchExactly, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchExactly | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchContains, CntAbstractContactFilter::NotSupported, "d e", QContactIds() << bcd << Bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchContains | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "d e", QContactIds() << bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchStartsWith, CntAbstractContactFilter::Supported, "B", QContactIds() << bcd << Bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchStartsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::SupportedPreFilterOnly, "B", QContactIds() << Bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchEndsWith, CntAbstractContactFilter::NotSupported, "d Efg", QContactIds() << bcd << Bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchEndsWith | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "d Efg", QContactIds() << Bcd);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchFixedString, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc << Abc);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchFixedString | QContactFilter::MatchCaseSensitive, CntAbstractContactFilter::NotSupported, "abc def", QContactIds() << abc);
    addNewRow(QContactDisplayLabel::DefinitionName, QContactDisplayLabel::FieldLabel, QContactFilter::MatchKeypadCollation, CntAbstractContactFilter::NotSupported, "223 334", QContactIds() << bcd << Bcd);
}



void tst_cntfilteringdbms::addNewRow(QString defName, QString fieldName, int flags, CntAbstractContactFilter::FilterSupport filterSupport, QString filterCriterion, QList<QContactLocalId> matchingContacts)
{
    QString s;
    foreach(QContactLocalId id, matchingContacts)
        s.append(QString("%1 ").arg(id));
    
    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);
    
    QContactDetailFilter *f = new QContactDetailFilter();
    f->setDetailDefinitionName(defName, fieldName);
    f->setMatchFlags(QContactFilter::MatchFlags(flags));
    f->setValue(QVariant(filterCriterion));
    
    QTest::newRow(title.toAscii().constData()) << (QContactFilter*) f << filterSupport << matchingContacts;
}

QTEST_MAIN(tst_cntfilteringdbms);
#include "tst_cntfilteringdbms.moc"