searchplugintest.cpp 58 KB
Newer Older
Christian Mollekopf's avatar
Christian Mollekopf committed
1
/*
2
 * This file is part of the KDE Akonadi Search Project
Christian Mollekopf's avatar
Christian Mollekopf committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Copyright (C) 2014  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
Christian Mollekopf's avatar
Christian Mollekopf committed
20
21
22
23
 *
 */

#include <QTest>
24
#include <AkonadiCore/Collection>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
25
26
#include <KContacts/Addressee>
#include <KContacts/ContactGroup>
Christian Mollekopf's avatar
Christian Mollekopf committed
27
28
29
#include <QDir>

#include "searchplugin.h"
Vishesh Handa's avatar
Vishesh Handa committed
30
31
#include <../agent/emailindexer.h>
#include <../agent/contactindexer.h>
32
#include <../agent/calendarindexer.h>
Vishesh Handa's avatar
Vishesh Handa committed
33
34
35
#include <../agent/akonotesindexer.h>
#include <../search/email/emailsearchstore.h>
#include <../search/contact/contactsearchstore.h>
36
#include <../search/calendar/calendarsearchstore.h>
Vishesh Handa's avatar
Vishesh Handa committed
37
#include <../search/note/notesearchstore.h>
38
39
#include <AkonadiCore/searchquery.h>
#include <Akonadi/KMime/MessageFlags>
Christian Mollekopf's avatar
Christian Mollekopf committed
40

41
42
#include <QElapsedTimer>

Laurent Montel's avatar
Laurent Montel committed
43
Q_DECLARE_METATYPE(QSet<qint64>)
44
Q_DECLARE_METATYPE(QVector<qint64>)
Christian Mollekopf's avatar
Christian Mollekopf committed
45
46
47
48
49
50
51
52

class SearchPluginTest : public QObject
{
    Q_OBJECT
private:
    QString emailDir;
    QString emailContactsDir;
    QString contactsDir;
Laurent Montel's avatar
Laurent Montel committed
53
    QString noteDir;
54
    QString calendarDir;
Christian Mollekopf's avatar
Christian Mollekopf committed
55

56
57
58
    void resultSearch()
    {
        QFETCH(QString, query);
Laurent Montel's avatar
Laurent Montel committed
59
        QFETCH(QVector<qint64>, collections);
60
61
62
        QFETCH(QStringList, mimeTypes);
        QFETCH(QSet<qint64>, expectedResult);

Vishesh Handa's avatar
Vishesh Handa committed
63
        qDebug() << "starting search";
64
65
        QElapsedTimer t;
        t.start();
66
67
        SearchPlugin plugin;
        const QSet<qint64> result = plugin.search(query, collections, mimeTypes);
68
        qDebug() << "result:" << result << "(in" << t.elapsed() << "ms)";
69
70
        QCOMPARE(result, expectedResult);
    }
Christian Mollekopf's avatar
Christian Mollekopf committed
71
72

private Q_SLOTS:
73
    void initTestCase()
Laurent Montel's avatar
Laurent Montel committed
74
    {
75
76
77
78
79
        emailDir = QDir::tempPath() + QLatin1String("/searchplugintest/email/");
        emailContactsDir = QDir::tempPath() + QLatin1String("/searchplugintest/emailcontacts/");
        contactsDir = QDir::tempPath() + QLatin1String("/searchplugintest/contacts/");
        noteDir = QDir::tempPath() + QLatin1String("/searchplugintest/notes/");
        calendarDir = QDir::tempPath() + QLatin1String("/searchplugintest/calendar/");
Christian Mollekopf's avatar
Christian Mollekopf committed
80
81

        QDir dir;
82
        QVERIFY(QDir(QDir::tempPath() + QStringLiteral("/searchplugintest")).removeRecursively());
Christian Mollekopf's avatar
Christian Mollekopf committed
83
84
85
        QVERIFY(dir.mkpath(emailDir));
        QVERIFY(dir.mkpath(emailContactsDir));
        QVERIFY(dir.mkpath(contactsDir));
Laurent Montel's avatar
Laurent Montel committed
86
        QVERIFY(dir.mkpath(noteDir));
87
        QVERIFY(dir.mkpath(calendarDir));
Christian Mollekopf's avatar
Christian Mollekopf committed
88

Vishesh Handa's avatar
Vishesh Handa committed
89
90
91
92
        qDebug() << "indexing sample data";
        qDebug() << emailDir;
        qDebug() << emailContactsDir;
        qDebug() << noteDir;
93
        qDebug() << calendarDir;
Christian Mollekopf's avatar
Christian Mollekopf committed
94
95
96

        EmailIndexer emailIndexer(emailDir, emailContactsDir);
        ContactIndexer contactIndexer(contactsDir);
Laurent Montel's avatar
Laurent Montel committed
97
        AkonotesIndexer noteIndexer(noteDir);
98
        CalendarIndexer calendarIndexer(calendarDir);
Christian Mollekopf's avatar
Christian Mollekopf committed
99
100
101
102

        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("subject1");
103
            msg->contentType()->setMimeType("text/plain");
104
            msg->contentType()->setCharset("utf-8");
105
            msg->setBody("body1 mälmöö");
Laurent Montel's avatar
Laurent Montel committed
106
107
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
108
            msg->date()->setDateTime(QDateTime(QDate(2013, 11, 10), QTime(12, 0, 0)));
109
            msg->assemble();
110

Laurent Montel's avatar
Laurent Montel committed
111
            Akonadi::Item item(QStringLiteral("message/rfc822"));
Christian Mollekopf's avatar
Christian Mollekopf committed
112
            item.setId(1);
113
            item.setSize(1000);
Christian Mollekopf's avatar
Christian Mollekopf committed
114
115
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(1));
Laurent Montel's avatar
Laurent Montel committed
116
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Replied << Akonadi::MessageFlags::Encrypted);
Christian Mollekopf's avatar
Christian Mollekopf committed
117
118
119
120
121
            emailIndexer.index(item);
        }
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("subject2");
122
123
124

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
125
126
127
            b->contentType()->setMimeType("text/plain");
            b->setBody("body2");
            msg->addContent(b, true);
128

Laurent Montel's avatar
Laurent Montel committed
129
130
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
131
            msg->date()->setDateTime(QDateTime(QDate(2013, 11, 10), QTime(13, 0, 0)));
Laurent Montel's avatar
Laurent Montel committed
132
            msg->organization()->from7BitString("kde");
133
            msg->assemble();
134

Laurent Montel's avatar
Laurent Montel committed
135
            Akonadi::Item item(QStringLiteral("message/rfc822"));
Christian Mollekopf's avatar
Christian Mollekopf committed
136
            item.setId(2);
137
            item.setSize(1002);
Christian Mollekopf's avatar
Christian Mollekopf committed
138
139
140
141
142
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(2));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            emailIndexer.index(item);
        }
Laurent Montel's avatar
Laurent Montel committed
143
144
145
146
147
148
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("subject3");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
149
150
151
            b->contentType()->setMimeType("text/plain");
            b->setBody("body3");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
152

Laurent Montel's avatar
Laurent Montel committed
153
154
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
155
            msg->date()->setDateTime(QDateTime(QDate(2014, 11, 10), QTime(13, 0, 0)));
Laurent Montel's avatar
Laurent Montel committed
156
            msg->organization()->from7BitString("kde5");
Laurent Montel's avatar
Laurent Montel committed
157
158
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
159
            Akonadi::Item item(QStringLiteral("message/rfc822"));
Laurent Montel's avatar
Laurent Montel committed
160
161
162
163
164
165
166
167
168
169
170
171
172
            item.setId(3);
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(2));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            emailIndexer.index(item);
        }
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("subject4");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
173
174
175
            b->contentType()->setMimeType("text/plain");
            b->setBody("body4");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
176

Laurent Montel's avatar
Laurent Montel committed
177
178
179
180
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
            msg->cc()->addAddress("cc@test.com", QStringLiteral("Jane Doe"));
            msg->bcc()->addAddress("bcc@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
181
            msg->date()->setDateTime(QDateTime(QDate(2014, 11, 11), QTime(13, 0, 0)));
182
            msg->replyTo()->from7BitString("test@kde.org");
183
            KMime::Headers::Generic *header = new KMime::Headers::Generic("Resent-From");
Volker Krause's avatar
Volker Krause committed
184
            header->fromUnicodeString(QStringLiteral("resent@kde.org"), "utf-8");
Laurent Montel's avatar
Laurent Montel committed
185
            msg->setHeader(header);
186
            header = new KMime::Headers::Generic("List-Id");
Volker Krause's avatar
Volker Krause committed
187
            header->fromUnicodeString(QStringLiteral("KDE PIM <kde-pim.kde.org>"), "utf-8");
Laurent Montel's avatar
Laurent Montel committed
188
            msg->setHeader(header);
Laurent Montel's avatar
Laurent Montel committed
189

Laurent Montel's avatar
Laurent Montel committed
190
191
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
192
            Akonadi::Item item(QStringLiteral("message/rfc822"));
Laurent Montel's avatar
Laurent Montel committed
193
194
195
196
197
198
199
            item.setId(4);
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(2));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            emailIndexer.index(item);
        }
200
201
202
203
204
205
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("all tags");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
206
207
208
            b->contentType()->setMimeType("text/plain");
            b->setBody("tags");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
209

Laurent Montel's avatar
Laurent Montel committed
210
211
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
212
            msg->date()->setDateTime(QDateTime(QDate(2014, 11, 11), QTime(13, 0, 0)));
213
214
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
215
            Akonadi::Item item(QStringLiteral("message/rfc822"));
216
217
218
219
220
            item.setId(5);
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(2));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Seen
Laurent Montel's avatar
Laurent Montel committed
221
222
223
224
225
226
                                                 << Akonadi::MessageFlags::Deleted
                                                 << Akonadi::MessageFlags::Answered
                                                 << Akonadi::MessageFlags::Flagged
                                                 << Akonadi::MessageFlags::HasAttachment
                                                 << Akonadi::MessageFlags::HasInvitation
                                                 << Akonadi::MessageFlags::Sent
227
                          //<< Akonadi::MessageFlags::Queued    //can't have Sent and Queued at the same time
Laurent Montel's avatar
Laurent Montel committed
228
229
230
231
                                                 << Akonadi::MessageFlags::Replied
                                                 << Akonadi::MessageFlags::Forwarded
                                                 << Akonadi::MessageFlags::ToAct
                                                 << Akonadi::MessageFlags::Watched
232
                          //<< Akonadi::MessageFlags::Ignored   // can't have Watched and Ignored at the same time
Laurent Montel's avatar
Laurent Montel committed
233
                                                 << Akonadi::MessageFlags::Encrypted
234
                          /*<< Akonadi::MessageFlags::Spam*/
Laurent Montel's avatar
Laurent Montel committed
235
                                                 << Akonadi::MessageFlags::Ham);
Laurent Montel's avatar
Laurent Montel committed
236
            //Spam is exclude from indexer. So we can't add it.
237
238
            emailIndexer.index(item);
        }
239
240
241
242
243
244
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("Change in qt/qtx11extras[stable]: remove QtWidgets dependency");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
245
246
247
            b->contentType()->setMimeType("text/plain");
            b->setBody("body5");
            msg->addContent(b, true);
248

Laurent Montel's avatar
Laurent Montel committed
249
250
            msg->from()->addAddress("john@test.com", QStringLiteral("John Doe"));
            msg->to()->addAddress("jane@test.com", QStringLiteral("Jane Doe"));
Laurent Montel's avatar
Laurent Montel committed
251
            msg->date()->setDateTime(QDateTime(QDate(2014, 11, 11), QTime(13, 0, 0)));
252
253
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
254
            Akonadi::Item item(QStringLiteral("message/rfc822"));
255
256
257
258
259
260
261
            item.setId(6);
            item.setSize(50);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(2));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            emailIndexer.index(item);
        }
262
263
        emailIndexer.commit();

Laurent Montel's avatar
Laurent Montel committed
264
        //Contact item
Christian Mollekopf's avatar
Christian Mollekopf committed
265
        {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
266
            KContacts::Addressee addressee;
Laurent Montel's avatar
Laurent Montel committed
267
268
269
270
271
            addressee.setUid(QStringLiteral("uid1"));
            addressee.setName(QStringLiteral("John Doe"));
            addressee.setFormattedName(QStringLiteral("John Doe"));
            addressee.setNickName(QStringLiteral("JD"));
            addressee.setEmails(QStringList() << QStringLiteral("john@test.com"));
Laurent Montel's avatar
Laurent Montel committed
272
273
274
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01)));
#else
275
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01).startOfDay()));
Laurent Montel's avatar
Laurent Montel committed
276
#endif
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
277
            Akonadi::Item item(KContacts::Addressee::mimeType());
Laurent Montel's avatar
Laurent Montel committed
278
            item.setId(100);
Christian Mollekopf's avatar
Christian Mollekopf committed
279
280
281
282
283
            item.setPayload(addressee);
            item.setParentCollection(Akonadi::Collection(3));
            contactIndexer.index(item);
        }
        {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
284
            KContacts::Addressee addressee;
Laurent Montel's avatar
Laurent Montel committed
285
286
287
            addressee.setUid(QStringLiteral("uid2"));
            addressee.setName(QStringLiteral("Jane Doe"));
            addressee.setEmails(QStringList() << QStringLiteral("jane@test.com"));
Laurent Montel's avatar
Laurent Montel committed
288
289
290
291
292
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01)));
#else
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01).startOfDay()));
#endif
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
293
            Akonadi::Item item(KContacts::Addressee::mimeType());
Laurent Montel's avatar
Laurent Montel committed
294
            item.setId(101);
Christian Mollekopf's avatar
Christian Mollekopf committed
295
296
297
298
            item.setPayload(addressee);
            item.setParentCollection(Akonadi::Collection(3));
            contactIndexer.index(item);
        }
299
        {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
300
            KContacts::Addressee addressee;
Laurent Montel's avatar
Laurent Montel committed
301
302
303
            addressee.setUid(QStringLiteral("uid2"));
            addressee.setName(QStringLiteral("Jane Doe"));
            addressee.setEmails(QStringList() << QStringLiteral("JANE@TEST.COM"));
Laurent Montel's avatar
Laurent Montel committed
304
305
306
307
308
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01)));
#else
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01).startOfDay()));
#endif
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
309
            Akonadi::Item item(KContacts::Addressee::mimeType());
310
311
312
313
314
            item.setId(102);
            item.setPayload(addressee);
            item.setParentCollection(Akonadi::Collection(3));
            contactIndexer.index(item);
        }
315
316
317
318
319
        {
            KContacts::Addressee addressee;
            addressee.setUid(QStringLiteral("abcd-efgh-1234-5678"));
            addressee.setName(QStringLiteral("Dan Vrátil"));
            addressee.setEmails({ QStringLiteral("dan@test.com") });
Laurent Montel's avatar
Laurent Montel committed
320
321
322
323
324
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01)));
#else
            addressee.setBirthday(QDateTime(QDate(2000, 01, 01).startOfDay()));
#endif
325
326
327
328
329
330
            Akonadi::Item item(KContacts::Addressee::mimeType());
            item.setId(105);
            item.setPayload(addressee);
            item.setParentCollection(Akonadi::Collection(3));
            contactIndexer.index(item);
        }
331
        {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
332
            KContacts::ContactGroup group;
Laurent Montel's avatar
Laurent Montel committed
333
            group.setName(QStringLiteral("group1"));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
334
            Akonadi::Item item(KContacts::ContactGroup::mimeType());
335
336
337
338
339
            item.setId(103);
            item.setPayload(group);
            item.setParentCollection(Akonadi::Collection(3));
            contactIndexer.index(item);
        }
340
        {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
341
            KContacts::ContactGroup group;
Laurent Montel's avatar
Laurent Montel committed
342
            group.setName(QStringLiteral("group3"));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
343
            Akonadi::Item item(KContacts::ContactGroup::mimeType());
344
345
346
347
348
            item.setId(104);
            item.setPayload(group);
            item.setParentCollection(Akonadi::Collection(4));
            contactIndexer.index(item);
        }
349
        contactIndexer.commit();
Laurent Montel's avatar
Laurent Montel committed
350
351

        //Note item
Laurent Montel's avatar
Laurent Montel committed
352
353
354
355
356
357
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("note");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
358
359
360
            b->contentType()->setMimeType("text/plain");
            b->setBody("body note");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
361
362
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
363
            Akonadi::Item item(QStringLiteral("text/x-vnd.akonadi.note"));
Laurent Montel's avatar
Laurent Montel committed
364
365
366
367
368
369
370
371
372
373
374
375
376
            item.setId(1000);
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(5));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            noteIndexer.index(item);
        }
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("note2");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
377
378
379
            b->contentType()->setMimeType("text/plain");
            b->setBody("note");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
380
381
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
382
            Akonadi::Item item(QStringLiteral("text/x-vnd.akonadi.note"));
Laurent Montel's avatar
Laurent Montel committed
383
384
385
386
387
388
389
390
391
392
393
394
395
            item.setId(1001);
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(5));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            noteIndexer.index(item);
        }
        {
            KMime::Message::Ptr msg(new KMime::Message);
            msg->subject()->from7BitString("note3");

            //Multipart message
            KMime::Content *b = new KMime::Content;
Laurent Montel's avatar
Laurent Montel committed
396
397
398
            b->contentType()->setMimeType("text/plain");
            b->setBody("note3");
            msg->addContent(b, true);
Laurent Montel's avatar
Laurent Montel committed
399
400
            msg->assemble();

Laurent Montel's avatar
Laurent Montel committed
401
            Akonadi::Item item(QStringLiteral("text/x-vnd.akonadi.note"));
Laurent Montel's avatar
Laurent Montel committed
402
            item.setId(1002);
Laurent Montel's avatar
Laurent Montel committed
403
404
405
406
407
408
            item.setSize(1002);
            item.setPayload(msg);
            item.setParentCollection(Akonadi::Collection(5));
            item.setFlags(Akonadi::Item::Flags() << Akonadi::MessageFlags::Flagged << Akonadi::MessageFlags::Replied);
            noteIndexer.index(item);
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
409

410
411
        // Calendar item
        {
412
413
            KCalendarCore::Event::Ptr event(new KCalendarCore::Event);
            KCalendarCore::Attendee attendee(QStringLiteral("attendee1"), QStringLiteral("attendee1@example.com"), false, KCalendarCore::Attendee::NeedsAction);
Laurent Montel's avatar
Laurent Montel committed
414
            event->setOrganizer(QStringLiteral("organizer@example.com"));
415
            event->addAttendee(attendee);
416
            attendee = KCalendarCore::Attendee(QStringLiteral("attendee2"), QStringLiteral("attendee2@example.com"), false, KCalendarCore::Attendee::Accepted);
417
            event->addAttendee(attendee);
418
            attendee = KCalendarCore::Attendee(QStringLiteral("attendee3"), QStringLiteral("attendee3@example.com"), false, KCalendarCore::Attendee::Declined);
419
            event->addAttendee(attendee);
420
            attendee = KCalendarCore::Attendee(QStringLiteral("attendee4"), QStringLiteral("attendee4@example.com"), false, KCalendarCore::Attendee::Tentative);
421
            event->addAttendee(attendee);
422
            attendee = KCalendarCore::Attendee(QStringLiteral("attendee5"), QStringLiteral("attendee5@example.com"), false, KCalendarCore::Attendee::Delegated);
423
424
            event->addAttendee(attendee);

Laurent Montel's avatar
Laurent Montel committed
425
426
            event->setSummary(QStringLiteral("title"));
            event->setLocation(QStringLiteral("here"));
427

428
            Akonadi::Item item(KCalendarCore::Event::eventMimeType());
429
            item.setId(2001);
430
            item.setPayload<KCalendarCore::Event::Ptr>(event);
431
432
433
            item.setParentCollection(Akonadi::Collection(6));
            calendarIndexer.index(item);
        }
434
        calendarIndexer.commit();
Christian Mollekopf's avatar
Christian Mollekopf committed
435

436
        Akonadi::Search::EmailSearchStore *emailSearchStore = new Akonadi::Search::EmailSearchStore();
Christian Mollekopf's avatar
Christian Mollekopf committed
437
        emailSearchStore->setDbPath(emailDir);
438
        Akonadi::Search::ContactSearchStore *contactSearchStore = new Akonadi::Search::ContactSearchStore();
Christian Mollekopf's avatar
Christian Mollekopf committed
439
        contactSearchStore->setDbPath(contactsDir);
440
        Akonadi::Search::NoteSearchStore *noteSearchStore = new Akonadi::Search::NoteSearchStore();
Laurent Montel's avatar
Laurent Montel committed
441
        noteSearchStore->setDbPath(noteDir);
442
        Akonadi::Search::CalendarSearchStore *calendarSearchStore = new Akonadi::Search::CalendarSearchStore();
443
        calendarSearchStore->setDbPath(calendarDir);
Laurent Montel's avatar
Laurent Montel committed
444

445
        Akonadi::Search::SearchStore::overrideSearchStores(QList<Akonadi::Search::SearchStore *>() << emailSearchStore << contactSearchStore << noteSearchStore << calendarSearchStore);
Christian Mollekopf's avatar
Christian Mollekopf committed
446
    }
447

448
449
450
451
452
    void cleanupTestCase()
    {
        QVERIFY(QDir(QDir::tempPath() + QStringLiteral("/searchplugintest")).removeRecursively());
    }

Laurent Montel's avatar
Laurent Montel committed
453
454
    void testCalendarSearch_data()
    {
455
        QTest::addColumn<QString>("query");
Laurent Montel's avatar
Laurent Montel committed
456
        QTest::addColumn<QVector<qint64> >("collections");
457
458
        QTest::addColumn<QStringList>("mimeTypes");
        QTest::addColumn<QSet<qint64> >("expectedResult");
459
        const QStringList calendarMimeTypes = QStringList() << KCalendarCore::Event::eventMimeType();
460
461
        {
            Akonadi::SearchQuery query;
462
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::Organizer, QStringLiteral("organizer@example.com"), Akonadi::SearchTerm::CondEqual));
463

Laurent Montel's avatar
Laurent Montel committed
464
            QVector<qint64> collections = QVector<qint64>() << 6;
465
466
467
468
469
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find organizer") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
470
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::Organizer, QStringLiteral("organizer2@example.com"), Akonadi::SearchTerm::CondEqual));
471

Laurent Montel's avatar
Laurent Montel committed
472
            QVector<qint64> collections = QVector<qint64>() << 6;
473
474
475
476
477
            QSet<qint64> result;
            QTest::newRow("find no organizer") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
478
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee1@example.com0"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
479
            QVector<qint64> collections = QVector<qint64>() << 6;
480
481
482
483
484
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events needsAction") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
485
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee2@example.com1"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
486
            QVector<qint64> collections = QVector<qint64>() << 6;
487
488
489
490
491
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events accepted") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
492
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee3@example.com2"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
493
            QVector<qint64> collections = QVector<qint64>() << 6;
494
495
496
497
498
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events declined") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
499
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee4@example.com3"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
500
            QVector<qint64> collections = QVector<qint64>() << 6;
501
502
503
504
505
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events tentative") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
506
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee5@example.com4"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
507
            QVector<qint64> collections = QVector<qint64>() << 6;
508
509
510
511
512
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events delegated") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
513
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::PartStatus, QStringLiteral("attendee5@example.com5"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
514
            QVector<qint64> collections = QVector<qint64>() << 6;
515
516
517
518
519
            QSet<qint64> result;
            QTest::newRow("unknown partstatus") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
520
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::Summary, QStringLiteral("title"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
521
            QVector<qint64> collections = QVector<qint64>() << 6;
522
523
524
525
526
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find event summary") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
527
            query.addTerm(Akonadi::IncidenceSearchTerm(Akonadi::IncidenceSearchTerm::Location, QStringLiteral("here"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
528
            QVector<qint64> collections = QVector<qint64>() << 6;
529
530
531
532
533
            QSet<qint64> result = QSet<qint64>() << 2001;
            QTest::newRow("find events location") << QString::fromLatin1(query.toJSON()) << collections << calendarMimeTypes << result;
        }
    }

Laurent Montel's avatar
Laurent Montel committed
534
535
    void testCalendarSearch()
    {
536
537
538
        resultSearch();
    }

539
#if 1
Laurent Montel's avatar
Laurent Montel committed
540
541
    void testNoteSearch_data()
    {
Laurent Montel's avatar
Laurent Montel committed
542
        QTest::addColumn<QString>("query");
543
        QTest::addColumn<QVector<qint64> >("collections");
Laurent Montel's avatar
Laurent Montel committed
544
545
        QTest::addColumn<QStringList>("mimeTypes");
        QTest::addColumn<QSet<qint64> >("expectedResult");
Laurent Montel's avatar
Laurent Montel committed
546
        const QStringList notesMimeTypes = QStringList() << QStringLiteral("text/x-vnd.akonadi.note");
Laurent Montel's avatar
Laurent Montel committed
547
548
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
549
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("note"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
550

Laurent Montel's avatar
Laurent Montel committed
551
            QVector<qint64> collections = QVector<qint64>() << 5;
Laurent Montel's avatar
Laurent Montel committed
552
            QSet<qint64> result = QSet<qint64>() << 1000;
553
            QTest::newRow("find note subject equal") << QString::fromLatin1(query.toJSON()) << collections << notesMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
554
555
556
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
557
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("note1"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
558

Laurent Montel's avatar
Laurent Montel committed
559
            QVector<qint64> collections = QVector<qint64>() << 5;
Laurent Montel's avatar
Laurent Montel committed
560
            QSet<qint64> result;
561
            QTest::newRow("find note subject equal") << QString::fromLatin1(query.toJSON()) << collections << notesMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
562
563
564
        }
        {
            Akonadi::SearchQuery query(Akonadi::SearchTerm::RelOr);
Laurent Montel's avatar
Laurent Montel committed
565
566
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("note"), Akonadi::SearchTerm::CondEqual));
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("note"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
567

Laurent Montel's avatar
Laurent Montel committed
568
            QVector<qint64> collections = QVector<qint64>() << 5;
Laurent Montel's avatar
Laurent Montel committed
569
            QSet<qint64> result = QSet<qint64>() << 1000 << 1001;
570
            QTest::newRow("find note subject equal or body equal") << QString::fromLatin1(query.toJSON()) << collections << notesMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
571
572
573
        }
        {
            Akonadi::SearchQuery query(Akonadi::SearchTerm::RelAnd);
Laurent Montel's avatar
Laurent Montel committed
574
575
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("note3"), Akonadi::SearchTerm::CondEqual));
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("note3"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
576

Laurent Montel's avatar
Laurent Montel committed
577
            QVector<qint64> collections = QVector<qint64>() << 5;
Laurent Montel's avatar
Laurent Montel committed
578
            QSet<qint64> result = QSet<qint64>() << 1002;
579
            QTest::newRow("find note subject equal and body equal") << QString::fromLatin1(query.toJSON()) << collections << notesMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
580
581
582
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
583
            Akonadi::EmailSearchTerm term(Akonadi::EmailSearchTerm::Subject, QStringLiteral("note3"), Akonadi::SearchTerm::CondEqual);
Laurent Montel's avatar
Laurent Montel committed
584
585
            term.setIsNegated(true);
            query.addTerm(term);
Laurent Montel's avatar
Laurent Montel committed
586
            QVector<qint64> collections = QVector<qint64>() << 5;
Laurent Montel's avatar
Laurent Montel committed
587
            QSet<qint64> result = QSet<qint64>() << 1000 << 1001;
588
            QTest::newRow("find not subject equal note3") << QString::fromLatin1(query.toJSON()) << collections << notesMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
589
590
        }
    }
Laurent Montel's avatar
Laurent Montel committed
591

Laurent Montel's avatar
Laurent Montel committed
592
593
    void testNoteSearch()
    {
594
        resultSearch();
Laurent Montel's avatar
Laurent Montel committed
595
596
    }

Laurent Montel's avatar
Laurent Montel committed
597
598
    void testContactSearch()
    {
599
        resultSearch();
Laurent Montel's avatar
Laurent Montel committed
600
601
    }

Laurent Montel's avatar
Laurent Montel committed
602
603
    void testContactSearch_data()
    {
Laurent Montel's avatar
Laurent Montel committed
604
        QTest::addColumn<QString>("query");
Laurent Montel's avatar
Laurent Montel committed
605
        QTest::addColumn<QVector<qint64> >("collections");
Laurent Montel's avatar
Laurent Montel committed
606
607
        QTest::addColumn<QStringList>("mimeTypes");
        QTest::addColumn<QSet<qint64> >("expectedResult");
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
608
609
        const QStringList contactMimeTypes = QStringList() << KContacts::Addressee::mimeType();
        const QStringList contactGroupMimeTypes = QStringList() << KContacts::ContactGroup::mimeType();
Laurent Montel's avatar
Laurent Montel committed
610
#if 1
Laurent Montel's avatar
Laurent Montel committed
611
612
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
613
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("John"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
614

Laurent Montel's avatar
Laurent Montel committed
615
            QVector<qint64> collections;
Laurent Montel's avatar
Laurent Montel committed
616
            QSet<qint64> result = QSet<qint64>() << 100;
617
            QTest::newRow("contact by name") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
618
619
620
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
621
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("John"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
622

Laurent Montel's avatar
Laurent Montel committed
623
            QVector<qint64> collections = QVector<qint64>() << 4;
Laurent Montel's avatar
Laurent Montel committed
624
            QSet<qint64> result;
625
            QTest::newRow("contact collectionfilter") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
626
627
628
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
629
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("john"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
630

Laurent Montel's avatar
Laurent Montel committed
631
            QVector<qint64> collections = QVector<qint64>() << 3;
Laurent Montel's avatar
Laurent Montel committed
632
            QSet<qint64> result = QSet<qint64>() << 100;
633
            QTest::newRow("contact by lowercase name") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
634
635
636
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
637
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Nickname, QStringLiteral("JD"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
638

Laurent Montel's avatar
Laurent Montel committed
639
            QVector<qint64> collections = QVector<qint64>() << 3;
Laurent Montel's avatar
Laurent Montel committed
640
            QSet<qint64> result = QSet<qint64>() << 100;
641
            QTest::newRow("contact by nickname") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
642
643
644
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
645
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Uid, QStringLiteral("uid1"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
646

Laurent Montel's avatar
Laurent Montel committed
647
            QVector<qint64> collections = QVector<qint64>() << 3;
Laurent Montel's avatar
Laurent Montel committed
648
            QSet<qint64> result = QSet<qint64>() << 100;
649
            QTest::newRow("contact by uid") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
650
        }
651
652
653
654
655
656
657
658
        {
            Akonadi::SearchQuery query;
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Uid, QStringLiteral("abcd-efgh-1234-5678"), Akonadi::SearchTerm::CondEqual));

            QVector<qint64> collections = { 3 };
            QSet<qint64> result = { 105 };
            QTest::newRow("contact by uid 2") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
        }
659
660
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
661
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Email, QStringLiteral("JANE@TEST.COM"), Akonadi::SearchTerm::CondEqual));
662

Laurent Montel's avatar
Laurent Montel committed
663
            QVector<qint64> collections = QVector<qint64>() << 3;
664
            QSet<qint64> result = QSet<qint64>() << 101 << 102;
665
            QTest::newRow("contact by email") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
666
        }
Laurent Montel's avatar
Laurent Montel committed
667
668
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
669
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("Doe"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
670

671
            QVector<qint64> collections;
Laurent Montel's avatar
Laurent Montel committed
672
673
674
675
676
            QSet<qint64> result = QSet<qint64>() << 100 << 101 << 102;
            QTest::newRow("contact by name (Doe)") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
677
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("Do"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
678

679
            QVector<qint64> collections;
Laurent Montel's avatar
Laurent Montel committed
680
681
682
            QSet<qint64> result = QSet<qint64>() << 100 << 101 << 102;
            QTest::newRow("contact by name (Do)") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
        }
Laurent Montel's avatar
Laurent Montel committed
683
#endif
684
685
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
686
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("group1"), Akonadi::SearchTerm::CondContains));
687

688
            QVector<qint64> collections;
689
690
691
            QSet<qint64> result = QSet<qint64>() << 103;
            QTest::newRow("contact group by name (group1)") << QString::fromLatin1(query.toJSON()) << collections << contactGroupMimeTypes << result;
        }
692
693
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
694
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("group2"), Akonadi::SearchTerm::CondContains));
695

696
            QVector<qint64> collections;
697
698
699
            QSet<qint64> result;
            QTest::newRow("contact group by name (group2)") << QString::fromLatin1(query.toJSON()) << collections << contactGroupMimeTypes << result;
        }
700
701
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
702
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("group3"), Akonadi::SearchTerm::CondContains));
703

Laurent Montel's avatar
Laurent Montel committed
704
            QVector<qint64> collections = QVector<qint64>() << 4;
705
706
707
708
709
            QSet<qint64> result = QSet<qint64>() << 104;
            QTest::newRow("contact group by name (group3 in collection 4)") << QString::fromLatin1(query.toJSON()) << collections << contactGroupMimeTypes << result;
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
710
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("group3"), Akonadi::SearchTerm::CondContains));
711

Laurent Montel's avatar
Laurent Montel committed
712
            QVector<qint64> collections = QVector<qint64>() << 3;
713
714
            QSet<qint64> result;
            QTest::newRow("contact group by name (group3 in collection 3)") << QString::fromLatin1(query.toJSON()) << collections << contactGroupMimeTypes << result;
715
716
        }

Laurent Montel's avatar
Laurent Montel committed
717
718
719
#if 0 //Doesn't work for the moment
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
720
            query.addTerm(Akonadi::ContactSearchTerm(Akonadi::ContactSearchTerm::Name, QStringLiteral("oe"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
721

722
            QVector<qint64> collections;
Laurent Montel's avatar
Laurent Montel committed
723
724
725
726
            QSet<qint64> result = QSet<qint64>() << 100 << 101 << 102;
            QTest::newRow("contact by name (oe)") << QString::fromLatin1(query.toJSON()) << collections << contactMimeTypes << result;
        }
#endif
Laurent Montel's avatar
Laurent Montel committed
727
    }
Laurent Montel's avatar
Laurent Montel committed
728

729
#endif
Laurent Montel's avatar
Laurent Montel committed
730
731
    void testEmailSearch_data()
    {
Christian Mollekopf's avatar
Christian Mollekopf committed
732
        QTest::addColumn<QString>("query");
733
        QTest::addColumn<QVector<qint64> >("collections");
Christian Mollekopf's avatar
Christian Mollekopf committed
734
735
        QTest::addColumn<QStringList>("mimeTypes");
        QTest::addColumn<QSet<qint64> >("expectedResult");
Laurent Montel's avatar
Laurent Montel committed
736
        const QStringList emailMimeTypes = QStringList() << QStringLiteral("message/rfc822");
737
        const QVector<qint64> allEmailCollections = QVector<qint64>() << 1 << 2;
738
#if 1
739
740
741
742
743
744
        {
            Akonadi::SearchQuery query;
            QVector<qint64> collections = QVector<qint64>() << 1;
            QSet<qint64> result = { 1 };
            QTest::newRow("all emails in collection") << QString::fromLatin1(query.toJSON()) << collections << emailMimeTypes << result;
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
745
746
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
747
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject1"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
748
            QVector<qint64> collections = QVector<qint64>() << 1;
Laurent Montel's avatar
Laurent Montel committed
749
            QSet<qint64> result = QSet<qint64>() << 1;
750
            QTest::newRow("find subject equal") << QString::fromLatin1(query.toJSON()) << collections << emailMimeTypes << result;
Christian Mollekopf's avatar
Christian Mollekopf committed
751
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
752
753
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
754
            Akonadi::EmailSearchTerm term(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject1"), Akonadi::SearchTerm::CondEqual);
Christian Mollekopf's avatar
Christian Mollekopf committed
755
756
            term.setIsNegated(true);
            query.addTerm(term);
Laurent Montel's avatar
Laurent Montel committed
757
            QVector<qint64> collections = QVector<qint64>() << 2;
Laurent Montel's avatar
Laurent Montel committed
758
            QSet<qint64> result = QSet<qint64>() << 2 << 3 << 4 << 5 << 6;
759
            QTest::newRow("find subject equal negated") << QString::fromLatin1(query.toJSON()) << collections << emailMimeTypes << result;
Christian Mollekopf's avatar
Christian Mollekopf committed
760
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
761
762
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
763
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
764
            QSet<qint64> result = QSet<qint64>() << 1 << 2 << 3 << 4;
765
            QTest::newRow("find subject contains") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
Christian Mollekopf's avatar
Christian Mollekopf committed
766
        }
767
768
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
769
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("body"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
770
            QVector<qint64> collections = QVector<qint64>() << 1 << 2 << 3 << 4;
Laurent Montel's avatar
Laurent Montel committed
771
            QSet<qint64> result = QSet<qint64>() << 1 << 2 << 3 << 4 << 6;
772
            QTest::newRow("find body contains") << QString::fromLatin1(query.toJSON()) << collections << emailMimeTypes << result;
773
774
775
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
776
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("mälmöö"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
777
            QSet<qint64> result = QSet<qint64>() << 1;
778
            QTest::newRow("find utf8 body contains") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
779
        }
780
781
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
782
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Headers, QStringLiteral("From:"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
783
            QSet<qint64> result = QSet<qint64>() << 1 << 2 << 3 << 4 << 5 << 6;
784
            QTest::newRow("find header contains") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
785
786
787
        }
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
788
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Message, QStringLiteral("body"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
789
            QSet<qint64> result = QSet<qint64>() << 1 << 2 << 3 << 4 << 6;
790
            QTest::newRow("find message contains") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
791
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
792
793
        {
            Akonadi::SearchQuery query(Akonadi::SearchTerm::RelOr);
Laurent Montel's avatar
Laurent Montel committed
794
795
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject1"), Akonadi::SearchTerm::CondEqual));
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject2"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
796
            QSet<qint64> result = QSet<qint64>() << 1 << 2;
797
            QTest::newRow("or term") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
Christian Mollekopf's avatar
Christian Mollekopf committed
798
        }
Laurent Montel's avatar
Laurent Montel committed
799
800
        {
            Akonadi::SearchQuery query(Akonadi::SearchTerm::RelAnd);
Laurent Montel's avatar
Laurent Montel committed
801
802
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject1"), Akonadi::SearchTerm::CondEqual));
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("body1"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
803
            QSet<qint64> result = QSet<qint64>() << 1;
804
            QTest::newRow("and term") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
805
806
807
        }
        {
            Akonadi::SearchQuery query(Akonadi::SearchTerm::RelAnd);
Laurent Montel's avatar
Laurent Montel committed
808
809
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject1"), Akonadi::SearchTerm::CondEqual));
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Body, QStringLiteral("body2"), Akonadi::SearchTerm::CondEqual));
Laurent Montel's avatar
Laurent Montel committed
810
            QSet<qint64> result;
811
            QTest::newRow("and term equal") << QString::fromLatin1(query.toJSON()) << allEmailCollections << emailMimeTypes << result;
Laurent Montel's avatar
Laurent Montel committed
812
        }
Christian Mollekopf's avatar
Christian Mollekopf committed
813
814
        {
            Akonadi::SearchQuery query;
Laurent Montel's avatar
Laurent Montel committed
815
            query.addTerm(Akonadi::EmailSearchTerm(Akonadi::EmailSearchTerm::Subject, QStringLiteral("subject"), Akonadi::SearchTerm::CondContains));
Laurent Montel's avatar
Laurent Montel committed
816
            QVector<qint64> collections = QVector<qint64>() << 1;