~ci-train-bot/history-service/history-service-ubuntu-yakkety-landing-052

« back to all changes in this revision

Viewing changes to tests/historyprivate/ContactMatcherTest.cpp

Request contact information for all known participants on history-daemon initialization, and use this cached information on the models.
Approved by: Tiago Salem Herrmann, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    void testMatchExistingContact();
44
44
    void testContactAdded();
45
45
    void testContactRemoved();
 
46
    void testSynchronousContactInfoRequest();
 
47
    void testWatchIdentifier();
 
48
 
 
49
protected:
 
50
    QContact createContact(const QString &firstName, const QString &lastName, const QStringList &phoneNumbers = QStringList(), const QStringList &extendedDetails = QStringList());
46
51
 
47
52
private:
48
53
    QContactManager *mContactManager;
57
62
    ContactMatcher::instance(mContactManager);
58
63
 
59
64
    // create two contacts to test
60
 
    QContactPhoneNumber phoneNumber;
61
 
    phoneNumber.setNumber("123456789");
62
 
    QVERIFY(mPhoneContact.saveDetail(&phoneNumber));
63
 
    QContactPhoneNumber phoneNumber2;
64
 
    phoneNumber2.setNumber("7654321");
65
 
    QVERIFY(mPhoneContact.saveDetail(&phoneNumber2));
66
 
    QContactName name;
67
 
    name.setFirstName("Phone");
68
 
    name.setLastName("Contact");
69
 
    QVERIFY(mPhoneContact.saveDetail(&name));
70
 
    QVERIFY(mContactManager->saveContact(&mPhoneContact));
71
 
 
72
 
    QContactExtendedDetail extendedDetail;
73
 
    extendedDetail.setName("x-mock-im");
74
 
    extendedDetail.setData("123456789");
75
 
    QVERIFY(mExtendedContact.saveDetail(&extendedDetail));
76
 
    name.setFirstName("Extended");
77
 
    name.setLastName("Generic Contact");
78
 
    QVERIFY(mExtendedContact.saveDetail(&name));
79
 
    QVERIFY(mContactManager->saveContact(&mExtendedContact));
 
65
    mPhoneContact = createContact("Phone", "Contact", QStringList() << "123456789" << "7654321");
 
66
    mExtendedContact = createContact("Extended", "Generic Contact", QStringList(), QStringList() << "123456789");
80
67
}
81
68
 
82
69
void ContactMatcherTest::init()
140
127
    QVERIFY(!info.contains(History::FieldContactId));
141
128
 
142
129
    // now add a contact that matches this item
143
 
    QContact contact;
144
 
    QContactPhoneNumber phoneNumber;
145
 
    phoneNumber.setNumber(identifier);
146
 
    QVERIFY(contact.saveDetail(&phoneNumber));
147
 
    QVERIFY(mContactManager->saveContact(&contact));
148
 
 
 
130
    QContact contact = createContact("Added", "Contact", QStringList() << identifier);
149
131
    QTRY_COMPARE(contactInfoSpy.count(), 1);
150
132
    QCOMPARE(contactInfoSpy.first()[0].toString(), accountId);
151
133
    QCOMPARE(contactInfoSpy.first()[1].toString(), identifier);
161
143
    QCOMPARE(info[History::FieldIdentifier].toString(), identifier);
162
144
 
163
145
    // now add a contact that matches this item
164
 
    QContact contact;
165
 
    QContactPhoneNumber phoneNumber;
166
 
    phoneNumber.setNumber(identifier);
167
 
    QVERIFY(contact.saveDetail(&phoneNumber));
168
 
    QVERIFY(mContactManager->saveContact(&contact));
 
146
    QContact contact = createContact("Removed", "Contact", QStringList() << identifier);
169
147
    QTRY_COMPARE(contactInfoSpy.count(), 1);
170
148
 
171
149
    // now that the contact info is filled, remove the contact
177
155
    QVERIFY(!contactInfoSpy.first()[2].toMap().contains(History::FieldContactId));
178
156
}
179
157
 
 
158
void ContactMatcherTest::testSynchronousContactInfoRequest()
 
159
{
 
160
    QString identifier("77777777");
 
161
    QString accountId("mock/ofono/account0");
 
162
 
 
163
    // now add a contact that matches this item
 
164
    QContact contact = createContact("Synchronous", "Contact", QStringList() << identifier);
 
165
 
 
166
    // now that the contact info is filled, remove the contact
 
167
    QVariantMap info = ContactMatcher::instance()->contactInfo(accountId, identifier, true);
 
168
    QCOMPARE(info[History::FieldIdentifier].toString(), identifier);
 
169
    QCOMPARE(info[History::FieldAccountId].toString(), accountId);
 
170
    QVERIFY(!info[History::FieldContactId].toString().isEmpty());
 
171
 
 
172
    // and remove this contact to not interfere in the other tests
 
173
    QVERIFY(mContactManager->removeContact(contact.id()));
 
174
}
 
175
 
 
176
void ContactMatcherTest::testWatchIdentifier()
 
177
{
 
178
    QString identifier("88888888");
 
179
    QString accountId("mock/ofono/account0");
 
180
 
 
181
    ContactMatcher::instance()->watchIdentifier(accountId, identifier);
 
182
 
 
183
    // now add a contact and make sure we get the contactInfoChanged signal
 
184
    QSignalSpy contactInfoSpy(ContactMatcher::instance(), SIGNAL(contactInfoChanged(QString,QString,QVariantMap)));
 
185
    QContact contact = createContact("Contact", "Watched", QStringList() << identifier);
 
186
    QTRY_COMPARE(contactInfoSpy.count(), 1);
 
187
    QCOMPARE(contactInfoSpy.first()[0].toString(), accountId);
 
188
    QCOMPARE(contactInfoSpy.first()[1].toString(), identifier);
 
189
    QVariantMap info = contactInfoSpy.first()[2].toMap();
 
190
    QCOMPARE(info[History::FieldContactId].toString(), contact.id().toString());
 
191
 
 
192
    QVERIFY(mContactManager->removeContact(contact.id()));
 
193
}
 
194
 
 
195
QContact ContactMatcherTest::createContact(const QString &firstName, const QString &lastName, const QStringList &phoneNumbers, const QStringList &extendedDetails)
 
196
{
 
197
    QContact contact;
 
198
 
 
199
    QContactName name;
 
200
    name.setFirstName(firstName);
 
201
    name.setLastName(lastName);
 
202
 
 
203
    if (!contact.saveDetail(&name)) {
 
204
        return contact;
 
205
    }
 
206
 
 
207
    Q_FOREACH(const QString &number, phoneNumbers) {
 
208
        QContactPhoneNumber phoneNumber;
 
209
        phoneNumber.setNumber(number);
 
210
        if (!contact.saveDetail(&phoneNumber)) {
 
211
            return contact;
 
212
        }
 
213
    }
 
214
 
 
215
    Q_FOREACH(const QString &extended, extendedDetails) {
 
216
        QContactExtendedDetail extendedDetail;
 
217
        extendedDetail.setName("x-mock-im");
 
218
        extendedDetail.setData(extended);
 
219
        if (!contact.saveDetail(&extendedDetail)) {
 
220
            return contact;
 
221
        }
 
222
    }
 
223
 
 
224
    mContactManager->saveContact(&contact);
 
225
    return contact;
 
226
}
 
227
 
180
228
QTEST_MAIN(ContactMatcherTest)
181
229
#include "ContactMatcherTest.moc"