~ubuntu-branches/ubuntu/vivid/address-book-service/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/unittest/contact-collection-test.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ubuntu daily release, Renato Araujo Oliveira Filho
  • Date: 2014-11-12 15:30:49 UTC
  • mfrom: (1.1.42)
  • Revision ID: package-import@ubuntu.com-20141112153049-7pxp091gk6vfxwfq
Tags: 0.1.1+15.04.20141112-0ubuntu1
[ Ubuntu daily release ]
* New rebuild forced

[ Renato Araujo Oliveira Filho ]
* Avoid create sources with duplicated Id; Remove address book sources
  based on source name instead of source id. (LP: #1387763)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This file is part of contact-service-app.
 
5
 *
 
6
 * contact-service-app is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * contact-service-app is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <QObject>
 
20
#include <QtTest>
 
21
#include <QDebug>
 
22
#include <QtContacts>
 
23
 
 
24
#include "config.h"
 
25
 
 
26
using namespace QtContacts;
 
27
 
 
28
class ContactCollectionTest : public QObject
 
29
{
 
30
    Q_OBJECT
 
31
private:
 
32
    QContactManager *m_manager;
 
33
 
 
34
private Q_SLOTS:
 
35
    void initTestCase()
 
36
    {
 
37
        QCoreApplication::setLibraryPaths(QStringList() << QT_PLUGINS_BINARY_DIR);
 
38
        // wait for address-book-service
 
39
        QTest::qWait(1000);
 
40
    }
 
41
 
 
42
    void init()
 
43
    {
 
44
        m_manager = new QContactManager("galera");
 
45
    }
 
46
 
 
47
    void cleanup()
 
48
    {
 
49
        delete m_manager;
 
50
    }
 
51
 
 
52
    /*
 
53
     * Test create a new collection
 
54
     */
 
55
    void testCreateAddressBook()
 
56
    {
 
57
        QContact c;
 
58
        c.setType(QContactType::TypeGroup);
 
59
        QContactDisplayLabel label;
 
60
        QString uniqueName = QString("source@%1").arg(QUuid::createUuid().toString().remove("{").remove("}"));
 
61
        label.setLabel(uniqueName);
 
62
        c.saveDetail(&label);
 
63
 
 
64
        bool saveResult = m_manager->saveContact(&c);
 
65
        QVERIFY(saveResult);
 
66
 
 
67
        QContactDetailFilter filter;
 
68
        filter.setDetailType(QContactDetail::TypeType, QContactType::FieldType);
 
69
        filter.setValue(QContactType::TypeGroup);
 
70
 
 
71
        QList<QContact> contacts = m_manager->contacts(filter);
 
72
        Q_FOREACH(const QContact &contact, contacts) {
 
73
            if ((contact.detail<QContactDisplayLabel>().label() == uniqueName) &&
 
74
                (contact.id() == c.id())) {
 
75
                return;
 
76
            }
 
77
        }
 
78
        QFAIL("New collection not found");
 
79
    }
 
80
 
 
81
    /*
 
82
     * Test remove a collection
 
83
     */
 
84
    void testRemoveAddressBook()
 
85
    {
 
86
        // create a source
 
87
        QContact c;
 
88
        c.setType(QContactType::TypeGroup);
 
89
        QContactDisplayLabel label;
 
90
        QString uniqueName = QString("source@%1").arg(QUuid::createUuid().toString().remove("{").remove("}"));
 
91
        label.setLabel(uniqueName);
 
92
        c.saveDetail(&label);
 
93
 
 
94
        bool saveResult = m_manager->saveContact(&c);
 
95
        QVERIFY(saveResult);
 
96
 
 
97
        // try to remove new source
 
98
        // WORKAROUND: Since qcontacts does not cotains a API to remove address book we use the contact label as id
 
99
        // for addressbook. This Id must contain a "@" to be handled as address book name.
 
100
        QContactId addressBookId = QContactId::fromString(QString("qtcontacts:galera::%1").arg(uniqueName));
 
101
        bool removeResult = m_manager->removeContact(addressBookId);
 
102
        QVERIFY(removeResult);
 
103
 
 
104
        // check if the source was removed
 
105
        QContactDetailFilter filter;
 
106
        filter.setDetailType(QContactDetail::TypeType, QContactType::FieldType);
 
107
        filter.setValue(QContactType::TypeGroup);
 
108
        QList<QContact> contacts = m_manager->contacts(filter);
 
109
        Q_FOREACH(const QContact &contact, contacts) {
 
110
            if (contact.id() == c.id()) {
 
111
                QFAIL("Collection not removed");
 
112
            }
 
113
        }
 
114
    }
 
115
 
 
116
    /*
 
117
     * Test query for collections using the contact group type
 
118
     */
 
119
    void testQueryAddressBook()
 
120
    {
 
121
        // filter all contact groups/addressbook
 
122
        QContactDetailFilter filter;
 
123
        filter.setDetailType(QContactDetail::TypeType, QContactType::FieldType);
 
124
        filter.setValue(QContactType::TypeGroup);
 
125
 
 
126
        // check result for the default source
 
127
        QList<QContact> contacts = m_manager->contacts(filter);
 
128
        Q_FOREACH(const QContact &c, contacts) {
 
129
            QCOMPARE(c.type(), QContactType::TypeGroup);
 
130
            if (c.id().toString() == QStringLiteral("qtcontacts:galera::system-address-book")) {
 
131
                QContactDisplayLabel label = c.detail(QContactDisplayLabel::Type);
 
132
                QCOMPARE(label.label(), QStringLiteral("Personal"));
 
133
                return;
 
134
            }
 
135
        }
 
136
        QFAIL("Fail to query for collections");
 
137
    }
 
138
};
 
139
 
 
140
QTEST_MAIN(ContactCollectionTest)
 
141
 
 
142
#include "contact-collection-test.moc"