2
* Copyright (C) 2013 Canonical Ltd.
4
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
6
* This file is part of online-accounts-ui
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.
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.
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/>.
21
#include "application-manager.h"
24
#include <Accounts/Account>
25
#include <Accounts/Manager>
28
#include <QQmlComponent>
29
#include <QQmlContext>
34
#define TEST_DIR "/tmp/tst_application_manager"
36
using namespace OnlineAccountsUi;
38
class ApplicationManagerTest: public QObject
43
ApplicationManagerTest();
47
void testNonExistingApp();
48
void testApplicationInfo_data();
49
void testApplicationInfo();
50
void testAclAdd_data();
52
void testAclRemove_data();
56
void clearApplicationsDir();
58
void writeAccountsFile(const QString &name, const QString &contents);
65
ApplicationManagerTest::ApplicationManagerTest():
68
m_accountsDir(TEST_DIR "/accounts")
73
void ApplicationManagerTest::clearApplicationsDir()
75
QDir applicationsDir = m_accountsDir;
76
if (applicationsDir.cd("applications")) {
77
applicationsDir.removeRecursively();
78
applicationsDir.mkpath(".");
82
void ApplicationManagerTest::clearTestDir()
84
m_testDir.removeRecursively();
85
m_testDir.mkpath(".");
88
void ApplicationManagerTest::writeAccountsFile(const QString &name,
89
const QString &contents)
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".
95
QFileInfo fileInfo(name);
96
QString subDirName = fileInfo.suffix() + "s";
97
m_accountsDir.mkpath(subDirName);
98
QDir subDir = m_accountsDir;
99
subDir.cd(subDirName);
101
QFile file(subDir.filePath(name));
102
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
103
qWarning() << "Could not write file" << name;
107
file.write(contents.toUtf8());
110
void ApplicationManagerTest::initTestCase()
112
qputenv("ACCOUNTS", TEST_DIR);
113
qputenv("XDG_DATA_HOME", TEST_DIR);
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"
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"
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"
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"
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"
153
writeAccountsFile("bad.provider",
154
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
155
"<provider id=\"bad\">\n"
156
" <name>Bad provider</name>\n"
160
void ApplicationManagerTest::testNonExistingApp()
162
ApplicationManager manager;
164
QVariantMap info = manager.applicationInfo("com.ubuntu.test_MyApp",
165
"com.ubuntu.test_MyApp_0.1");
166
QVERIFY(info.isEmpty());
169
acl << "one" << "two";
170
QStringList newAcl = manager.addApplicationToAcl(acl, "com.ubuntu.test_MyApp");
171
QCOMPARE(newAcl, acl);
173
newAcl = manager.removeApplicationFromAcl(acl, "com.ubuntu.test_MyApp");
174
QCOMPARE(newAcl, acl);
177
void ApplicationManagerTest::testApplicationInfo_data()
179
QTest::addColumn<QString>("applicationId");
180
QTest::addColumn<QString>("contents");
181
QTest::addColumn<QString>("profile");
182
QTest::addColumn<QStringList>("services");
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"
191
"com.ubuntu.test_MyApp_0.2" <<
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"
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"
204
"com.ubuntu.test_MyApp2_0.2" <<
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"
213
" <service-type id=\"tstemail\">\n"
214
" <description>Send email</description>\n"
216
" </service-types>\n"
217
" <profile>com.ubuntu.test_MyApp3_0.2</profile>\n"
219
"com.ubuntu.test_MyApp3_0.2" <<
220
(QStringList() << "cool-mail" << "bad-mail");
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"
228
" <service id=\"cool-mail\">\n"
229
" <description>Send email</description>\n"
231
" <service id=\"cool-sharing\">\n"
232
" <description>Share stuff</description>\n"
235
" <profile>com.ubuntu.test_MyApp4_0.2</profile>\n"
237
"com.ubuntu.test_MyApp4_0.2" <<
238
(QStringList() << "cool-mail" << "cool-sharing");
241
void ApplicationManagerTest::testApplicationInfo()
243
clearApplicationsDir();
245
QFETCH(QString, applicationId);
246
QFETCH(QString, contents);
247
QFETCH(QString, profile);
248
QFETCH(QStringList, services);
250
writeAccountsFile(applicationId + ".application", contents);
252
ApplicationManager manager;
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());
260
void ApplicationManagerTest::testAclAdd_data()
262
QTest::addColumn<QString>("applicationId");
263
QTest::addColumn<QStringList>("oldAcl");
264
QTest::addColumn<QStringList>("newAcl");
266
QTest::newRow("add to empty") <<
267
"com.ubuntu.test_One" <<
269
(QStringList() << "com.ubuntu.test_One_0.1");
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");
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");
282
void ApplicationManagerTest::testAclAdd()
284
clearApplicationsDir();
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"
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"
299
QFETCH(QString, applicationId);
300
QFETCH(QStringList, oldAcl);
301
QFETCH(QStringList, newAcl);
303
ApplicationManager manager;
305
QStringList acl = manager.addApplicationToAcl(oldAcl, applicationId);
306
QCOMPARE(acl.toSet(), newAcl.toSet());
309
void ApplicationManagerTest::testAclRemove_data()
311
QTest::addColumn<QString>("applicationId");
312
QTest::addColumn<QStringList>("oldAcl");
313
QTest::addColumn<QStringList>("newAcl");
315
QTest::newRow("remove from empty") <<
316
"com.ubuntu.test_One" <<
320
QTest::newRow("remove from one") <<
321
"com.ubuntu.test_One" <<
322
(QStringList() << "com.ubuntu.test_One_0.1") <<
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");
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");
336
void ApplicationManagerTest::testAclRemove()
338
clearApplicationsDir();
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"
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"
353
QFETCH(QString, applicationId);
354
QFETCH(QStringList, oldAcl);
355
QFETCH(QStringList, newAcl);
357
ApplicationManager manager;
359
QStringList acl = manager.removeApplicationFromAcl(oldAcl, applicationId);
360
QCOMPARE(acl.toSet(), newAcl.toSet());
363
QTEST_MAIN(ApplicationManagerTest);
365
#include "tst_application_manager.moc"