~canonical-platform-qa/ubuntu-system-settings-online-accounts/launch_fixture

« back to all changes in this revision

Viewing changes to tests/online-accounts-ui/tst_application_manager.cpp

  • Committer: CI bot
  • Author(s): Alberto Mardegan
  • Date: 2014-05-30 13:30:01 UTC
  • mfrom: (107.1.13 master)
  • Revision ID: ps-jenkins@lists.canonical.com-20140530133001-an9lfy1dc1pfkifd
Release development branch

Features landing with this branch:
- Updating the ACL when applications are enabled/disabled in System Settings
- Write profile information in the XML files installed by click hooks
- Run tests with Python 3 autopilot.
- Merge signon-ui into online-accounts-ui 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical Ltd.
 
3
 *
 
4
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
5
 *
 
6
 * This file is part of online-accounts-ui
 
7
 *
 
8
 * This program is free software: you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License version 3, as published
 
10
 * by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
 * PURPOSE.  See the GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along
 
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "application-manager.h"
 
22
#include "debug.h"
 
23
 
 
24
#include <Accounts/Account>
 
25
#include <Accounts/Manager>
 
26
#include <QDebug>
 
27
#include <QDir>
 
28
#include <QQmlComponent>
 
29
#include <QQmlContext>
 
30
#include <QQmlEngine>
 
31
#include <QSignalSpy>
 
32
#include <QTest>
 
33
 
 
34
#define TEST_DIR "/tmp/tst_application_manager"
 
35
 
 
36
using namespace OnlineAccountsUi;
 
37
 
 
38
class ApplicationManagerTest: public QObject
 
39
{
 
40
    Q_OBJECT
 
41
 
 
42
public:
 
43
    ApplicationManagerTest();
 
44
 
 
45
private Q_SLOTS:
 
46
    void initTestCase();
 
47
    void testNonExistingApp();
 
48
    void testApplicationInfo_data();
 
49
    void testApplicationInfo();
 
50
    void testAclAdd_data();
 
51
    void testAclAdd();
 
52
    void testAclRemove_data();
 
53
    void testAclRemove();
 
54
 
 
55
private:
 
56
    void clearApplicationsDir();
 
57
    void clearTestDir();
 
58
    void writeAccountsFile(const QString &name, const QString &contents);
 
59
 
 
60
private:
 
61
    QDir m_testDir;
 
62
    QDir m_accountsDir;
 
63
};
 
64
 
 
65
ApplicationManagerTest::ApplicationManagerTest():
 
66
    QObject(0),
 
67
    m_testDir(TEST_DIR),
 
68
    m_accountsDir(TEST_DIR "/accounts")
 
69
{
 
70
    setLoggingLevel(2);
 
71
}
 
72
 
 
73
void ApplicationManagerTest::clearApplicationsDir()
 
74
{
 
75
    QDir applicationsDir = m_accountsDir;
 
76
    if (applicationsDir.cd("applications")) {
 
77
        applicationsDir.removeRecursively();
 
78
        applicationsDir.mkpath(".");
 
79
    }
 
80
}
 
81
 
 
82
void ApplicationManagerTest::clearTestDir()
 
83
{
 
84
    m_testDir.removeRecursively();
 
85
    m_testDir.mkpath(".");
 
86
}
 
87
 
 
88
void ApplicationManagerTest::writeAccountsFile(const QString &name,
 
89
                                               const QString &contents)
 
90
{
 
91
    /* Create files into the appropriate directory:
 
92
     * .provider files go in "providers", .application in "applications" and so
 
93
     * on: so we just need to take the file extension and add one "s".
 
94
     */
 
95
    QFileInfo fileInfo(name);
 
96
    QString subDirName = fileInfo.suffix() + "s";
 
97
    m_accountsDir.mkpath(subDirName);
 
98
    QDir subDir = m_accountsDir;
 
99
    subDir.cd(subDirName);
 
100
 
 
101
    QFile file(subDir.filePath(name));
 
102
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
103
        qWarning() << "Could not write file" << name;
 
104
        return;
 
105
    }
 
106
 
 
107
    file.write(contents.toUtf8());
 
108
}
 
109
 
 
110
void ApplicationManagerTest::initTestCase()
 
111
{
 
112
    qputenv("ACCOUNTS", TEST_DIR);
 
113
    qputenv("XDG_DATA_HOME", TEST_DIR);
 
114
 
 
115
    clearTestDir();
 
116
 
 
117
    /* Create a few service files */
 
118
    writeAccountsFile("cool-mail.service",
 
119
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
120
        "<service id=\"cool-mail\">\n"
 
121
        "  <type>tstemail</type>\n"
 
122
        "  <name>Cool Mail</name>\n"
 
123
        "  <provider>cool</provider>\n"
 
124
        "</service>");
 
125
    writeAccountsFile("cool-sharing.service",
 
126
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
127
        "<service id=\"cool-sharing\">\n"
 
128
        "  <type>tstsharing</type>\n"
 
129
        "  <name>Cool Sharing</name>\n"
 
130
        "  <provider>cool</provider>\n"
 
131
        "</service>");
 
132
    writeAccountsFile("bad-mail.service",
 
133
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
134
        "<service id=\"bad-mail\">\n"
 
135
        "  <type>tstemail</type>\n"
 
136
        "  <name>Bad Mail</name>\n"
 
137
        "  <provider>bad</provider>\n"
 
138
        "</service>");
 
139
    writeAccountsFile("bad-sharing.service",
 
140
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
141
        "<service id=\"bad-sharing\">\n"
 
142
        "  <type>tstsharing</type>\n"
 
143
        "  <name>Bad Sharing</name>\n"
 
144
        "  <provider>bad</provider>\n"
 
145
        "</service>");
 
146
 
 
147
    /* And the relative providers */
 
148
    writeAccountsFile("cool.provider",
 
149
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
150
        "<provider id=\"cool\">\n"
 
151
        "  <name>Cool provider</name>\n"
 
152
        "</provider>");
 
153
    writeAccountsFile("bad.provider",
 
154
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
155
        "<provider id=\"bad\">\n"
 
156
        "  <name>Bad provider</name>\n"
 
157
        "</provider>");
 
158
}
 
159
 
 
160
void ApplicationManagerTest::testNonExistingApp()
 
161
{
 
162
    ApplicationManager manager;
 
163
 
 
164
    QVariantMap info = manager.applicationInfo("com.ubuntu.test_MyApp",
 
165
                                               "com.ubuntu.test_MyApp_0.1");
 
166
    QVERIFY(info.isEmpty());
 
167
 
 
168
    QStringList acl;
 
169
    acl << "one" << "two";
 
170
    QStringList newAcl = manager.addApplicationToAcl(acl, "com.ubuntu.test_MyApp");
 
171
    QCOMPARE(newAcl, acl);
 
172
 
 
173
    newAcl = manager.removeApplicationFromAcl(acl, "com.ubuntu.test_MyApp");
 
174
    QCOMPARE(newAcl, acl);
 
175
}
 
176
 
 
177
void ApplicationManagerTest::testApplicationInfo_data()
 
178
{
 
179
    QTest::addColumn<QString>("applicationId");
 
180
    QTest::addColumn<QString>("contents");
 
181
    QTest::addColumn<QString>("profile");
 
182
    QTest::addColumn<QStringList>("services");
 
183
 
 
184
    QTest::newRow("no-services") <<
 
185
        "com.ubuntu.test_MyApp" <<
 
186
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
187
        "<application id=\"com.ubuntu.test_MyApp\">\n"
 
188
        "  <description>My application</description>\n"
 
189
        "  <profile>com.ubuntu.test_MyApp_0.2</profile>\n"
 
190
        "</application>" <<
 
191
        "com.ubuntu.test_MyApp_0.2" <<
 
192
        QStringList();
 
193
 
 
194
    QTest::newRow("no-valid-services") <<
 
195
        "com.ubuntu.test_MyApp2" <<
 
196
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
197
        "<application id=\"com.ubuntu.test_MyApp2\">\n"
 
198
        "  <description>My application 2</description>\n"
 
199
        "  <service-types>\n"
 
200
        "    <service-type id=\"some type\">Do something</service-type>\n"
 
201
        "  </service-types>\n"
 
202
        "  <profile>com.ubuntu.test_MyApp2_0.2</profile>\n"
 
203
        "</application>" <<
 
204
        "com.ubuntu.test_MyApp2_0.2" <<
 
205
        QStringList();
 
206
 
 
207
    QTest::newRow("email-services") <<
 
208
        "com.ubuntu.test_MyApp3" <<
 
209
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
210
        "<application id=\"com.ubuntu.test_MyApp3\">\n"
 
211
        "  <description>My application 3</description>\n"
 
212
        "  <service-types>\n"
 
213
        "    <service-type id=\"tstemail\">\n"
 
214
        "      <description>Send email</description>\n"
 
215
        "    </service-type>\n"
 
216
        "  </service-types>\n"
 
217
        "  <profile>com.ubuntu.test_MyApp3_0.2</profile>\n"
 
218
        "</application>" <<
 
219
        "com.ubuntu.test_MyApp3_0.2" <<
 
220
        (QStringList() << "cool-mail" << "bad-mail");
 
221
 
 
222
    QTest::newRow("cool-services") <<
 
223
        "com.ubuntu.test_MyApp4" <<
 
224
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
225
        "<application id=\"com.ubuntu.test_MyApp4\">\n"
 
226
        "  <description>My application 4</description>\n"
 
227
        "  <services>\n"
 
228
        "    <service id=\"cool-mail\">\n"
 
229
        "      <description>Send email</description>\n"
 
230
        "    </service>\n"
 
231
        "    <service id=\"cool-sharing\">\n"
 
232
        "      <description>Share stuff</description>\n"
 
233
        "    </service>\n"
 
234
        "  </services>\n"
 
235
        "  <profile>com.ubuntu.test_MyApp4_0.2</profile>\n"
 
236
        "</application>" <<
 
237
        "com.ubuntu.test_MyApp4_0.2" <<
 
238
        (QStringList() << "cool-mail" << "cool-sharing");
 
239
}
 
240
 
 
241
void ApplicationManagerTest::testApplicationInfo()
 
242
{
 
243
    clearApplicationsDir();
 
244
 
 
245
    QFETCH(QString, applicationId);
 
246
    QFETCH(QString, contents);
 
247
    QFETCH(QString, profile);
 
248
    QFETCH(QStringList, services);
 
249
 
 
250
    writeAccountsFile(applicationId + ".application", contents);
 
251
 
 
252
    ApplicationManager manager;
 
253
 
 
254
    QVariantMap info = manager.applicationInfo(applicationId, profile);
 
255
    QCOMPARE(info.value("id").toString(), applicationId);
 
256
    QCOMPARE(info.value("profile").toString(), profile);
 
257
    QCOMPARE(info.value("services").toStringList().toSet(), services.toSet());
 
258
}
 
259
 
 
260
void ApplicationManagerTest::testAclAdd_data()
 
261
{
 
262
    QTest::addColumn<QString>("applicationId");
 
263
    QTest::addColumn<QStringList>("oldAcl");
 
264
    QTest::addColumn<QStringList>("newAcl");
 
265
 
 
266
    QTest::newRow("add to empty") <<
 
267
        "com.ubuntu.test_One" <<
 
268
        QStringList() <<
 
269
        (QStringList() << "com.ubuntu.test_One_0.1");
 
270
 
 
271
    QTest::newRow("add to one") <<
 
272
        "com.ubuntu.test_One" <<
 
273
        (QStringList() << "some app") <<
 
274
        (QStringList() << "some app" << "com.ubuntu.test_One_0.1");
 
275
 
 
276
    QTest::newRow("add existing") <<
 
277
        "com.ubuntu.test_One" <<
 
278
        (QStringList() << "com.ubuntu.test_One_0.1" << "some app") <<
 
279
        (QStringList() << "com.ubuntu.test_One_0.1" << "some app");
 
280
}
 
281
 
 
282
void ApplicationManagerTest::testAclAdd()
 
283
{
 
284
    clearApplicationsDir();
 
285
 
 
286
    writeAccountsFile("com.ubuntu.test_One.application",
 
287
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
288
        "<application id=\"com.ubuntu.test_One\">\n"
 
289
        "  <description>My application</description>\n"
 
290
        "  <profile>com.ubuntu.test_One_0.1</profile>\n"
 
291
        "</application>");
 
292
    writeAccountsFile("com.ubuntu.test_Two.application",
 
293
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
294
        "<application id=\"com.ubuntu.test_Two\">\n"
 
295
        "  <description>My application</description>\n"
 
296
        "  <profile>com.ubuntu.test_Two_0.2</profile>\n"
 
297
        "</application>");
 
298
 
 
299
    QFETCH(QString, applicationId);
 
300
    QFETCH(QStringList, oldAcl);
 
301
    QFETCH(QStringList, newAcl);
 
302
 
 
303
    ApplicationManager manager;
 
304
 
 
305
    QStringList acl = manager.addApplicationToAcl(oldAcl, applicationId);
 
306
    QCOMPARE(acl.toSet(), newAcl.toSet());
 
307
}
 
308
 
 
309
void ApplicationManagerTest::testAclRemove_data()
 
310
{
 
311
    QTest::addColumn<QString>("applicationId");
 
312
    QTest::addColumn<QStringList>("oldAcl");
 
313
    QTest::addColumn<QStringList>("newAcl");
 
314
 
 
315
    QTest::newRow("remove from empty") <<
 
316
        "com.ubuntu.test_One" <<
 
317
        QStringList() <<
 
318
        QStringList();
 
319
 
 
320
    QTest::newRow("remove from one") <<
 
321
        "com.ubuntu.test_One" <<
 
322
        (QStringList() << "com.ubuntu.test_One_0.1") <<
 
323
        QStringList();
 
324
 
 
325
    QTest::newRow("remove from two") <<
 
326
        "com.ubuntu.test_One" <<
 
327
        (QStringList() << "com.ubuntu.test_One_0.1" << "some app") <<
 
328
        (QStringList() << "some app");
 
329
 
 
330
    QTest::newRow("remove from missing") <<
 
331
        "com.ubuntu.test_One" <<
 
332
        (QStringList() << "some other app" << "some app") <<
 
333
        (QStringList() << "some other app" << "some app");
 
334
}
 
335
 
 
336
void ApplicationManagerTest::testAclRemove()
 
337
{
 
338
    clearApplicationsDir();
 
339
 
 
340
    writeAccountsFile("com.ubuntu.test_One.application",
 
341
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
342
        "<application id=\"com.ubuntu.test_One\">\n"
 
343
        "  <description>My application</description>\n"
 
344
        "  <profile>com.ubuntu.test_One_0.1</profile>\n"
 
345
        "</application>");
 
346
    writeAccountsFile("com.ubuntu.test_Two.application",
 
347
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
 
348
        "<application id=\"com.ubuntu.test_Two\">\n"
 
349
        "  <description>My application</description>\n"
 
350
        "  <profile>com.ubuntu.test_Two_0.2</profile>\n"
 
351
        "</application>");
 
352
 
 
353
    QFETCH(QString, applicationId);
 
354
    QFETCH(QStringList, oldAcl);
 
355
    QFETCH(QStringList, newAcl);
 
356
 
 
357
    ApplicationManager manager;
 
358
 
 
359
    QStringList acl = manager.removeApplicationFromAcl(oldAcl, applicationId);
 
360
    QCOMPARE(acl.toSet(), newAcl.toSet());
 
361
}
 
362
 
 
363
QTEST_MAIN(ApplicationManagerTest);
 
364
 
 
365
#include "tst_application_manager.moc"