File: kpeoplevcardtest.cpp

package info (click to toggle)
kpeoplevcard 0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 140 kB
  • sloc: cpp: 376; makefile: 3
file content (200 lines) | stat: -rw-r--r-- 6,614 bytes parent folder | download | duplicates (2)
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
/*
    Copyright (C) 2015 Aleix Pol Gonzalez <aleixpol@kde.org>

    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) any later version.

    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
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <QDir>
#include <QObject>
#include <QTest>
#include <QStandardPaths>
#include <QSignalSpy>
#include <KContacts/VCardConverter>
#include <../src/kpeoplevcard.h>

class KPeopleVCardTest : public QObject
{
Q_OBJECT
public:
    KPeopleVCardTest()
        : m_source(new VCardDataSource(this, {}))
    {}

    QByteArray createContact(const KContacts::Addressee& addressee)
    {
        KContacts::VCardConverter conv;
        return conv.exportVCard(addressee, KContacts::VCardConverter::v3_0);
    }

    void writeContact(const KContacts::Addressee& addressee, const QString& path)
    {
        QFile f(path);
        bool b = f.open(QIODevice::WriteOnly);
        Q_ASSERT(b);

        f.write(createContact(addressee));
    }

private Q_SLOTS:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);
        m_vcardsDir = QDir(KPeopleVCard::contactsVCardPath());

        QDir().temp().mkpath(KPeopleVCard::contactsVCardPath());

        QVERIFY(m_vcardsDir.exists());

        foreach(const QFileInfo & entry, m_vcardsDir.entryInfoList(QDir::Files)) {
            QFile::remove(entry.absoluteFilePath());
        }

        QDir(KPeopleVCard::contactsVCardWritePath()).removeRecursively();
        QDir().temp().mkpath(KPeopleVCard::contactsVCardPath() + "/subdir");
        QCOMPARE(m_vcardsDir.count(), uint(3)); //. and .. and subdir/

        m_backend = dynamic_cast<KPeopleVCard*>(m_source->createAllContactsMonitor());
    }

    void emptyTest()
    {
        QVERIFY(m_backend->contacts().isEmpty());
    }

    void crudTest()
    {
//      ENSURE EMPTY
        QVERIFY(m_backend->contacts().isEmpty());

        const QString name = QStringLiteral("aaa"), name2 = QStringLiteral("bbb");
        KContacts::Addressee addr;
        addr.setName(name);
        const QString path = m_vcardsDir.absoluteFilePath("X");
        const QString pathInSubdir = m_vcardsDir.absoluteFilePath("subdir/a");
        // CREATE
        {
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactAdded);
            writeContact(addr, path);
            QVERIFY(spy.wait());

            // write the same contact into a subdir
            writeContact(addr, pathInSubdir);
            QVERIFY(spy.wait());
        }

        // READ
        {
            KPeople::AbstractContact::Ptr ptr = m_backend->contacts().first();
            QCOMPARE(ptr->customProperty(KPeople::AbstractContact::NameProperty).toString(), name);

            // the contact in subdir
            ptr = m_backend->contacts().last();
            QCOMPARE(ptr->customProperty(KPeople::AbstractContact::NameProperty).toString(), name);
        }

        // UPDATE
        {
            addr.setName(name2);
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactChanged);
            writeContact(addr, path);
            QVERIFY(spy.wait());

            writeContact(addr, pathInSubdir);
            QVERIFY(spy.wait());
        }

        // READ
        {
            KPeople::AbstractContact::Ptr ptr = m_backend->contacts().first();
            QCOMPARE(ptr->customProperty(KPeople::AbstractContact::NameProperty).toString(), name2);

            ptr = m_backend->contacts().last();
            QCOMPARE(ptr->customProperty(KPeople::AbstractContact::NameProperty).toString(), name2);
        }

        // REMOVE
        {
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactRemoved);
            QFile::remove(path);
            QVERIFY(spy.wait());

            QFile::remove(pathInSubdir);
            QVERIFY(spy.wait());
        }

//      ENSURE EMPTY
        QVERIFY(m_backend->contacts().isEmpty());
    }

    void editableInterface() {
        KContacts::Addressee addr;
        addr.setName(QStringLiteral("Potato Person"));
        QString uri;
        KPeople::AbstractContact::Ptr ptr;

        //CREATE
        {
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactAdded);
            QVERIFY(m_source->addContact({ {"vcard", createContact(addr) } }));
            QVERIFY(spy.wait());

            uri = spy.constFirst().constFirst().toString();
            ptr = spy.constFirst().constLast().value<KPeople::AbstractContact::Ptr>();

            QVERIFY(m_backend->contacts().contains(uri));
            QCOMPARE(QDir(KPeopleVCard::contactsVCardWritePath()).count(), 3); //. .. and the potato person
        }


        //READ
        {
            QCOMPARE(ptr->customProperty(KPeople::AbstractContact::NameProperty), addr.name());
        }

        //UPDATE
        {
            KPeople::AbstractEditableContact* editable = dynamic_cast<KPeople::AbstractEditableContact*>(ptr.data());
            QVERIFY(editable);
            QCOMPARE(QDir(KPeopleVCard::contactsVCardWritePath()).count(), 3); //. .. and the potato person
            QVERIFY(m_backend->contacts().contains(uri));

            addr.setName("Tomato Person");
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactChanged);
            QVERIFY(editable->setCustomProperty("vcard", createContact(addr)));
            QVERIFY(spy.wait());
            QCOMPARE(spy.constFirst().constFirst(), uri);
        }

        {
            QSignalSpy spy(m_backend, &KPeople::AllContactsMonitor::contactRemoved);
            QVERIFY(m_source->deleteContact(uri));
            QVERIFY(spy.wait());

            QCOMPARE(spy.constFirst().constFirst().toString(), uri);
        }
        QCOMPARE(QDir(KPeopleVCard::contactsVCardWritePath()).count(), 2); //. .. and the potato person
        QVERIFY(!m_backend->contacts().contains(uri));
    }

private:
    KPeopleVCard* m_backend;
    VCardDataSource* m_source;
    QDir m_vcardsDir;
};

QTEST_GUILESS_MAIN(KPeopleVCardTest)

#include "kpeoplevcardtest.moc"