| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of liblomiri-online-accounts-plugin |
| 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 <LomiriOnlineAccountsPlugin/application-manager.h> |
| 22 | |
| 23 | #include <Accounts/Account> |
| 24 | #include <Accounts/Manager> |
| 25 | #include <QDebug> |
| 26 | #include <QDir> |
| 27 | #include <QQmlComponent> |
| 28 | #include <QQmlContext> |
| 29 | #include <QQmlEngine> |
| 30 | #include <QSignalSpy> |
| 31 | #include <QTest> |
| 32 | |
| 33 | #define TEST_DIR TMP_DIR "/tst_application_manager" |
| 34 | |
| 35 | using namespace OnlineAccountsUi; |
| 36 | |
| 37 | class ApplicationManagerTest: public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | ApplicationManagerTest(); |
| 43 | |
| 44 | private Q_SLOTS: |
| 45 | void initTestCase(); |
| 46 | void testNonExistingApp(); |
| 47 | void testApplicationInfo_data(); |
| 48 | void testApplicationInfo(); |
| 49 | void testAclAdd_data(); |
| 50 | void testAclAdd(); |
| 51 | void testAclRemove_data(); |
| 52 | void testAclRemove(); |
| 53 | void testApplicationFromProfile_data(); |
| 54 | void testApplicationFromProfile(); |
| 55 | void testProviderInfo_data(); |
| 56 | void testProviderInfo(); |
| 57 | void testUsefulProviders_data(); |
| 58 | void testUsefulProviders(); |
| 59 | |
| 60 | private: |
| 61 | void clearApplicationsDir(); |
| 62 | void clearProvidersDir(); |
| 63 | void clearTestDir(); |
| 64 | void writeAccountsFile(const QString &name, const QString &contents); |
| 65 | |
| 66 | private: |
| 67 | QDir m_testDir; |
| 68 | QDir m_accountsDir; |
| 69 | }; |
| 70 | |
| 71 | ApplicationManagerTest::ApplicationManagerTest(): |
| 72 | QObject(0), |
| 73 | m_testDir(TEST_DIR), |
| 74 | m_accountsDir(TEST_DIR "/accounts") |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void ApplicationManagerTest::clearApplicationsDir() |
| 79 | { |
| 80 | QDir applicationsDir = m_accountsDir; |
| 81 | if (applicationsDir.cd("applications")) { |
| 82 | applicationsDir.removeRecursively(); |
| 83 | applicationsDir.mkpath("."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void ApplicationManagerTest::clearProvidersDir() |
| 88 | { |
| 89 | QDir providersDir = m_accountsDir; |
| 90 | if (providersDir.cd("providers")) { |
| 91 | providersDir.removeRecursively(); |
| 92 | providersDir.mkpath("."); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void ApplicationManagerTest::clearTestDir() |
| 97 | { |
| 98 | m_testDir.removeRecursively(); |
| 99 | m_testDir.mkpath("."); |
| 100 | } |
| 101 | |
| 102 | void ApplicationManagerTest::writeAccountsFile(const QString &name, |
| 103 | const QString &contents) |
| 104 | { |
| 105 | /* Create files into the appropriate directory: |
| 106 | * .provider files go in "providers", .application in "applications" and so |
| 107 | * on: so we just need to take the file extension and add one "s". |
| 108 | */ |
| 109 | QFileInfo fileInfo(name); |
| 110 | QString subDirName = fileInfo.suffix() + "s"; |
| 111 | m_accountsDir.mkpath(subDirName); |
| 112 | QDir subDir = m_accountsDir; |
| 113 | subDir.cd(subDirName); |
| 114 | |
| 115 | QFile file(subDir.filePath(name)); |
| 116 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 117 | qWarning() << "Could not write file" << name; |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | file.write(contents.toUtf8()); |
| 122 | } |
| 123 | |
| 124 | void ApplicationManagerTest::initTestCase() |
| 125 | { |
| 126 | qputenv("ACCOUNTS", TEST_DIR); |
| 127 | qputenv("XDG_DATA_HOME", TEST_DIR); |
| 128 | qputenv("XDG_DATA_DIRS", TEST_DIR); |
| 129 | |
| 130 | clearTestDir(); |
| 131 | |
| 132 | /* Create a few service files */ |
| 133 | writeAccountsFile("cool-mail.service", |
| 134 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 135 | "<service id=\"cool-mail\">\n" |
| 136 | " <type>tstemail</type>\n" |
| 137 | " <name>Cool Mail</name>\n" |
| 138 | " <provider>cool</provider>\n" |
| 139 | "</service>"); |
| 140 | writeAccountsFile("cool-sharing.service", |
| 141 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 142 | "<service id=\"cool-sharing\">\n" |
| 143 | " <type>tstsharing</type>\n" |
| 144 | " <name>Cool Sharing</name>\n" |
| 145 | " <provider>cool</provider>\n" |
| 146 | "</service>"); |
| 147 | writeAccountsFile("bad-mail.service", |
| 148 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 149 | "<service id=\"bad-mail\">\n" |
| 150 | " <type>tstemail</type>\n" |
| 151 | " <name>Bad Mail</name>\n" |
| 152 | " <provider>bad</provider>\n" |
| 153 | "</service>"); |
| 154 | writeAccountsFile("bad-sharing.service", |
| 155 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 156 | "<service id=\"bad-sharing\">\n" |
| 157 | " <type>tstsharing</type>\n" |
| 158 | " <name>Bad Sharing</name>\n" |
| 159 | " <provider>bad</provider>\n" |
| 160 | "</service>"); |
| 161 | |
| 162 | /* And the relative providers */ |
| 163 | writeAccountsFile("cool.provider", |
| 164 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 165 | "<provider id=\"cool\">\n" |
| 166 | " <name>Cool provider</name>\n" |
| 167 | "</provider>"); |
| 168 | writeAccountsFile("bad.provider", |
| 169 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 170 | "<provider id=\"bad\">\n" |
| 171 | " <name>Bad provider</name>\n" |
| 172 | "</provider>"); |
| 173 | } |
| 174 | |
| 175 | void ApplicationManagerTest::testNonExistingApp() |
| 176 | { |
| 177 | ApplicationManager manager; |
| 178 | |
| 179 | QVariantMap info = manager.applicationInfo("com.lomiri.test_MyApp", |
| 180 | "com.lomiri.test_MyApp_0.1"); |
| 181 | QVERIFY(info.isEmpty()); |
| 182 | |
| 183 | QStringList acl; |
| 184 | acl << "one" << "two"; |
| 185 | QStringList newAcl = manager.addApplicationToAcl(acl, "com.lomiri.test_MyApp"); |
| 186 | QCOMPARE(newAcl, acl); |
| 187 | |
| 188 | newAcl = manager.removeApplicationFromAcl(acl, "com.lomiri.test_MyApp"); |
| 189 | QCOMPARE(newAcl, acl); |
| 190 | } |
| 191 | |
| 192 | void ApplicationManagerTest::testApplicationInfo_data() |
| 193 | { |
| 194 | QTest::addColumn<QString>("applicationId"); |
| 195 | QTest::addColumn<QString>("contents"); |
| 196 | QTest::addColumn<QString>("inputProfile"); |
| 197 | QTest::addColumn<QString>("expectedProfile"); |
| 198 | QTest::addColumn<QStringList>("services"); |
| 199 | |
| 200 | QTest::newRow("no-services") << |
| 201 | "com.lomiri.test_MyApp" << |
| 202 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 203 | "<application id=\"com.lomiri.test_MyApp\">\n" |
| 204 | " <description>My application</description>\n" |
| 205 | " <profile>com.lomiri.test_MyApp_0.2</profile>\n" |
| 206 | "</application>" << |
| 207 | "com.lomiri.test_MyApp_0.2" << |
| 208 | "com.lomiri.test_MyApp_0.2" << |
| 209 | QStringList(); |
| 210 | |
| 211 | QTest::newRow("no-valid-services") << |
| 212 | "com.lomiri.test_MyApp2" << |
| 213 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 214 | "<application id=\"com.lomiri.test_MyApp2\">\n" |
| 215 | " <description>My application 2</description>\n" |
| 216 | " <service-types>\n" |
| 217 | " <service-type id=\"some type\">Do something</service-type>\n" |
| 218 | " </service-types>\n" |
| 219 | " <profile>com.lomiri.test_MyApp2_0.2</profile>\n" |
| 220 | "</application>" << |
| 221 | "com.lomiri.test_MyApp2_0.2" << |
| 222 | "com.lomiri.test_MyApp2_0.2" << |
| 223 | QStringList(); |
| 224 | |
| 225 | QTest::newRow("email-services") << |
| 226 | "com.lomiri.test_MyApp3" << |
| 227 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 228 | "<application id=\"com.lomiri.test_MyApp3\">\n" |
| 229 | " <description>My application 3</description>\n" |
| 230 | " <service-types>\n" |
| 231 | " <service-type id=\"tstemail\">\n" |
| 232 | " <description>Send email</description>\n" |
| 233 | " </service-type>\n" |
| 234 | " </service-types>\n" |
| 235 | " <profile>com.lomiri.test_MyApp3_0.2</profile>\n" |
| 236 | "</application>" << |
| 237 | "com.lomiri.test_MyApp3_0.2" << |
| 238 | "com.lomiri.test_MyApp3_0.2" << |
| 239 | (QStringList() << "cool-mail" << "bad-mail"); |
| 240 | |
| 241 | QTest::newRow("cool-services") << |
| 242 | "com.lomiri.test_MyApp4" << |
| 243 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 244 | "<application id=\"com.lomiri.test_MyApp4\">\n" |
| 245 | " <description>My application 4</description>\n" |
| 246 | " <services>\n" |
| 247 | " <service id=\"cool-mail\">\n" |
| 248 | " <description>Send email</description>\n" |
| 249 | " </service>\n" |
| 250 | " <service id=\"cool-sharing\">\n" |
| 251 | " <description>Share stuff</description>\n" |
| 252 | " </service>\n" |
| 253 | " </services>\n" |
| 254 | " <profile>com.lomiri.test_MyApp4_0.2</profile>\n" |
| 255 | "</application>" << |
| 256 | "com.lomiri.test_MyApp4_0.2" << |
| 257 | "com.lomiri.test_MyApp4_0.2" << |
| 258 | (QStringList() << "cool-mail" << "cool-sharing"); |
| 259 | |
| 260 | QTest::newRow("unconfined app") << |
| 261 | "com.lomiri.test_UnconfinedApp" << |
| 262 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 263 | "<application id=\"com.lomiri.test_UnconfinedApp\">\n" |
| 264 | " <description>My application 5</description>\n" |
| 265 | " <services>\n" |
| 266 | " <service id=\"cool-mail\">\n" |
| 267 | " <description>Send email</description>\n" |
| 268 | " </service>\n" |
| 269 | " </services>\n" |
| 270 | " <profile>com.lomiri.test_UnconfinedApp_0.2</profile>\n" |
| 271 | "</application>" << |
| 272 | "unconfined" << |
| 273 | "com.lomiri.test_UnconfinedApp_0.2" << |
| 274 | (QStringList() << "cool-mail"); |
| 275 | |
| 276 | QTest::newRow("unconfined app, no profile") << |
| 277 | "com.lomiri.test_UnconfinedApp2" << |
| 278 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 279 | "<application id=\"com.lomiri.test_UnconfinedApp2\">\n" |
| 280 | " <description>My application 6</description>\n" |
| 281 | " <services>\n" |
| 282 | " <service id=\"cool-mail\">\n" |
| 283 | " <description>Send email</description>\n" |
| 284 | " </service>\n" |
| 285 | " </services>\n" |
| 286 | "</application>" << |
| 287 | "unconfined" << |
| 288 | "unconfined" << |
| 289 | (QStringList() << "cool-mail"); |
| 290 | } |
| 291 | |
| 292 | void ApplicationManagerTest::testApplicationInfo() |
| 293 | { |
| 294 | clearApplicationsDir(); |
| 295 | |
| 296 | QFETCH(QString, applicationId); |
| 297 | QFETCH(QString, contents); |
| 298 | QFETCH(QString, inputProfile); |
| 299 | QFETCH(QString, expectedProfile); |
| 300 | QFETCH(QStringList, services); |
| 301 | |
| 302 | writeAccountsFile(applicationId + ".application", contents); |
| 303 | |
| 304 | ApplicationManager manager; |
| 305 | |
| 306 | QVariantMap info = manager.applicationInfo(applicationId, inputProfile); |
| 307 | QCOMPARE(info.value("id").toString(), applicationId); |
| 308 | QCOMPARE(info.value("profile").toString(), expectedProfile); |
| 309 | QStringList info_services = info.value("services").toStringList(); |
| 310 | QCOMPARE(QSet<QString>(info_services.constBegin(), info_services.constEnd()), QSet<QString>(services.constBegin(), services.constEnd())); |
| 311 | } |
| 312 | |
| 313 | void ApplicationManagerTest::testAclAdd_data() |
| 314 | { |
| 315 | QTest::addColumn<QString>("applicationId"); |
| 316 | QTest::addColumn<QStringList>("oldAcl"); |
| 317 | QTest::addColumn<QStringList>("newAcl"); |
| 318 | |
| 319 | QTest::newRow("add to empty") << |
| 320 | "com.lomiri.test_One" << |
| 321 | QStringList() << |
| 322 | (QStringList() << "com.lomiri.test_One_0.1"); |
| 323 | |
| 324 | QTest::newRow("add to one") << |
| 325 | "com.lomiri.test_One" << |
| 326 | (QStringList() << "some app") << |
| 327 | (QStringList() << "some app" << "com.lomiri.test_One_0.1"); |
| 328 | |
| 329 | QTest::newRow("add existing") << |
| 330 | "com.lomiri.test_One" << |
| 331 | (QStringList() << "com.lomiri.test_One_0.1" << "some app") << |
| 332 | (QStringList() << "com.lomiri.test_One_0.1" << "some app"); |
| 333 | } |
| 334 | |
| 335 | void ApplicationManagerTest::testAclAdd() |
| 336 | { |
| 337 | clearApplicationsDir(); |
| 338 | |
| 339 | writeAccountsFile("com.lomiri.test_One.application", |
| 340 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 341 | "<application id=\"com.lomiri.test_One\">\n" |
| 342 | " <description>My application</description>\n" |
| 343 | " <profile>com.lomiri.test_One_0.1</profile>\n" |
| 344 | "</application>"); |
| 345 | writeAccountsFile("com.lomiri.test_Two.application", |
| 346 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 347 | "<application id=\"com.lomiri.test_Two\">\n" |
| 348 | " <description>My application</description>\n" |
| 349 | " <profile>com.lomiri.test_Two_0.2</profile>\n" |
| 350 | "</application>"); |
| 351 | |
| 352 | QFETCH(QString, applicationId); |
| 353 | QFETCH(QStringList, oldAcl); |
| 354 | QFETCH(QStringList, newAcl); |
| 355 | |
| 356 | ApplicationManager manager; |
| 357 | |
| 358 | QStringList acl = manager.addApplicationToAcl(oldAcl, applicationId); |
| 359 | QCOMPARE(QSet<QString>(acl.constBegin(), acl.constEnd()), QSet<QString>(newAcl.constBegin(), newAcl.constEnd())); |
| 360 | } |
| 361 | |
| 362 | void ApplicationManagerTest::testAclRemove_data() |
| 363 | { |
| 364 | QTest::addColumn<QString>("applicationId"); |
| 365 | QTest::addColumn<QStringList>("oldAcl"); |
| 366 | QTest::addColumn<QStringList>("newAcl"); |
| 367 | |
| 368 | QTest::newRow("remove from empty") << |
| 369 | "com.lomiri.test_One" << |
| 370 | QStringList() << |
| 371 | QStringList(); |
| 372 | |
| 373 | QTest::newRow("remove from one") << |
| 374 | "com.lomiri.test_One" << |
| 375 | (QStringList() << "com.lomiri.test_One_0.1") << |
| 376 | QStringList(); |
| 377 | |
| 378 | QTest::newRow("remove from two") << |
| 379 | "com.lomiri.test_One" << |
| 380 | (QStringList() << "com.lomiri.test_One_0.1" << "some app") << |
| 381 | (QStringList() << "some app"); |
| 382 | |
| 383 | QTest::newRow("remove from missing") << |
| 384 | "com.lomiri.test_One" << |
| 385 | (QStringList() << "some other app" << "some app") << |
| 386 | (QStringList() << "some other app" << "some app"); |
| 387 | |
| 388 | QTest::newRow("remove multiple versions") << |
| 389 | "com.lomiri.test_One" << |
| 390 | (QStringList() << "some other app" << "com.lomiri.test_One_0.1" << |
| 391 | "com.lomiri.test_One_0.3" << "some app" << |
| 392 | "com.lomiri.test_One_0.2") << |
| 393 | (QStringList() << "some other app" << "some app"); |
| 394 | } |
| 395 | |
| 396 | void ApplicationManagerTest::testAclRemove() |
| 397 | { |
| 398 | clearApplicationsDir(); |
| 399 | |
| 400 | writeAccountsFile("com.lomiri.test_One.application", |
| 401 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 402 | "<application id=\"com.lomiri.test_One\">\n" |
| 403 | " <description>My application</description>\n" |
| 404 | " <profile>com.lomiri.test_One_0.1</profile>\n" |
| 405 | "</application>"); |
| 406 | writeAccountsFile("com.lomiri.test_Two.application", |
| 407 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 408 | "<application id=\"com.lomiri.test_Two\">\n" |
| 409 | " <description>My application</description>\n" |
| 410 | " <profile>com.lomiri.test_Two_0.2</profile>\n" |
| 411 | "</application>"); |
| 412 | |
| 413 | QFETCH(QString, applicationId); |
| 414 | QFETCH(QStringList, oldAcl); |
| 415 | QFETCH(QStringList, newAcl); |
| 416 | |
| 417 | ApplicationManager manager; |
| 418 | |
| 419 | QStringList acl = manager.removeApplicationFromAcl(oldAcl, applicationId); |
| 420 | QCOMPARE(QSet<QString>(acl.constBegin(), acl.constEnd()), QSet<QString>(newAcl.constBegin(), newAcl.constEnd())); |
| 421 | } |
| 422 | |
| 423 | void ApplicationManagerTest::testApplicationFromProfile_data() |
| 424 | { |
| 425 | QTest::addColumn<QString>("applicationId"); |
| 426 | QTest::addColumn<QString>("contents"); |
| 427 | QTest::addColumn<QString>("profile"); |
| 428 | QTest::addColumn<bool>("isValid"); |
| 429 | |
| 430 | QTest::newRow("id-same-as-profile") << |
| 431 | "com.lomiri.test_MyApp_0.2" << |
| 432 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 433 | "<application id=\"com.lomiri.test_MyApp\">\n" |
| 434 | " <description>My application</description>\n" |
| 435 | " <profile>com.lomiri.test_MyApp_0.2</profile>\n" |
| 436 | "</application>" << |
| 437 | "com.lomiri.test_MyApp_0.2" << |
| 438 | true; |
| 439 | |
| 440 | QTest::newRow("id-with-no-version") << |
| 441 | "com.lomiri.test_MyApp2" << |
| 442 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 443 | "<application id=\"com.lomiri.test_MyApp2\">\n" |
| 444 | " <description>My application 2</description>\n" |
| 445 | " <profile>com.lomiri.test_MyApp2_0.2</profile>\n" |
| 446 | "</application>" << |
| 447 | "com.lomiri.test_MyApp2_0.2" << |
| 448 | true; |
| 449 | |
| 450 | QTest::newRow("id-is-just-package") << |
| 451 | "com.lomiri.test" << |
| 452 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 453 | "<application id=\"com.lomiri.test_MyApp3\">\n" |
| 454 | " <description>My application 3</description>\n" |
| 455 | " <profile>com.lomiri.test_MyApp3_0.2</profile>\n" |
| 456 | "</application>" << |
| 457 | "com.lomiri.test_MyApp3_0.2" << |
| 458 | true; |
| 459 | |
| 460 | QTest::newRow("not found") << |
| 461 | "com.lomiri.test_MyApp5" << |
| 462 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 463 | "<application id=\"com.lomiri.test_MyApp4\">\n" |
| 464 | " <description>My application 4</description>\n" |
| 465 | " <profile>com.lomiri.test_MyApp4_0.2</profile>\n" |
| 466 | "</application>" << |
| 467 | "com.lomiri.test_MyApp4_0.2" << |
| 468 | false; |
| 469 | } |
| 470 | |
| 471 | void ApplicationManagerTest::testApplicationFromProfile() |
| 472 | { |
| 473 | clearApplicationsDir(); |
| 474 | |
| 475 | QFETCH(QString, applicationId); |
| 476 | QFETCH(QString, contents); |
| 477 | QFETCH(QString, profile); |
| 478 | QFETCH(bool, isValid); |
| 479 | |
| 480 | writeAccountsFile(applicationId + ".application", contents); |
| 481 | |
| 482 | ApplicationManager manager; |
| 483 | |
| 484 | Accounts::Application app = manager.applicationFromProfile(profile); |
| 485 | if (!isValid) { |
| 486 | QVERIFY(!app.isValid()); |
| 487 | } else { |
| 488 | QVERIFY(app.isValid()); |
| 489 | QCOMPARE(app.name(), applicationId); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void ApplicationManagerTest::testProviderInfo_data() |
| 494 | { |
| 495 | QTest::addColumn<QString>("providerId"); |
| 496 | QTest::addColumn<QString>("contents"); |
| 497 | QTest::addColumn<QString>("profile"); |
| 498 | QTest::addColumn<QString>("packageDir"); |
| 499 | QTest::addColumn<bool>("isSingleAccount"); |
| 500 | |
| 501 | QTest::newRow("no profile") << |
| 502 | "com.lomiri.test_MyPlugin" << |
| 503 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 504 | "<provider id=\"com.lomiri.test_MyPlugin\">\n" |
| 505 | " <name>My Plugin</name>\n" |
| 506 | "</provider>" << |
| 507 | QString() << |
| 508 | QString() << |
| 509 | false; |
| 510 | |
| 511 | QTest::newRow("single account") << |
| 512 | "com.lomiri.test_MyPlugin" << |
| 513 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 514 | "<provider id=\"com.lomiri.test_MyPlugin\">\n" |
| 515 | " <name>My Plugin</name>\n" |
| 516 | " <single-account>true</single-account>\n" |
| 517 | "</provider>" << |
| 518 | QString() << |
| 519 | QString() << |
| 520 | true; |
| 521 | |
| 522 | QTest::newRow("no package-dir") << |
| 523 | "com.lomiri.test_MyPlugin2" << |
| 524 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 525 | "<provider id=\"com.lomiri.test_MyPlugin2\">\n" |
| 526 | " <name>My Plugin</name>\n" |
| 527 | " <profile>com.lomiri.test_MyPlugin2_0.2</profile>\n" |
| 528 | " <single-account>false</single-account>\n" |
| 529 | "</provider>" << |
| 530 | "com.lomiri.test_MyPlugin2_0.2" << |
| 531 | QString() << |
| 532 | false; |
| 533 | |
| 534 | QTest::newRow("with package-dir") << |
| 535 | "com.lomiri.test_MyPlugin3" << |
| 536 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 537 | "<provider id=\"com.lomiri.test_MyPlugin3\">\n" |
| 538 | " <name>My Plugin</name>\n" |
| 539 | " <profile>com.lomiri.test_MyPlugin3_0.2</profile>\n" |
| 540 | " <package-dir>/opt/click.ubuntu.com/something</package-dir>\n" |
| 541 | "</provider>" << |
| 542 | "com.lomiri.test_MyPlugin3_0.2" << |
| 543 | "/opt/click.ubuntu.com/something" << |
| 544 | false; |
| 545 | } |
| 546 | |
| 547 | void ApplicationManagerTest::testProviderInfo() |
| 548 | { |
| 549 | clearProvidersDir(); |
| 550 | |
| 551 | QFETCH(QString, providerId); |
| 552 | QFETCH(QString, contents); |
| 553 | QFETCH(QString, profile); |
| 554 | QFETCH(QString, packageDir); |
| 555 | QFETCH(bool, isSingleAccount); |
| 556 | |
| 557 | writeAccountsFile(providerId + ".provider", contents); |
| 558 | |
| 559 | ApplicationManager manager; |
| 560 | |
| 561 | QVariantMap info = manager.providerInfo(providerId); |
| 562 | QCOMPARE(info.value("id").toString(), providerId); |
| 563 | QCOMPARE(info.value("profile").toString(), profile); |
| 564 | QCOMPARE(info.value("package-dir").toString(), packageDir); |
| 565 | QCOMPARE(info.value("isSingleAccount").toBool(), isSingleAccount); |
| 566 | } |
| 567 | |
| 568 | void ApplicationManagerTest::testUsefulProviders_data() |
| 569 | { |
| 570 | QTest::addColumn<QStringList>("applicationIds"); |
| 571 | QTest::addColumn<QStringList>("contents"); |
| 572 | QTest::addColumn<QStringList>("expectedProviders"); |
| 573 | |
| 574 | QStringList applicationIds; |
| 575 | QStringList contents; |
| 576 | applicationIds << "com.lomiri.test_MyApp"; |
| 577 | contents << |
| 578 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 579 | "<application id=\"com.lomiri.test_MyApp\">\n" |
| 580 | " <description>My application</description>\n" |
| 581 | "</application>"; |
| 582 | QTest::newRow("no-services") << |
| 583 | applicationIds << contents << |
| 584 | QStringList(); |
| 585 | applicationIds.clear(); |
| 586 | contents.clear(); |
| 587 | |
| 588 | applicationIds << |
| 589 | "com.lomiri.test_MyApp2"; |
| 590 | contents << |
| 591 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 592 | "<application id=\"com.lomiri.test_MyApp2\">\n" |
| 593 | " <description>My application 2</description>\n" |
| 594 | " <service-types>\n" |
| 595 | " <service-type id=\"some type\">Do something</service-type>\n" |
| 596 | " </service-types>\n" |
| 597 | "</application>"; |
| 598 | QTest::newRow("no-valid-services") << |
| 599 | applicationIds << contents << |
| 600 | QStringList(); |
| 601 | applicationIds.clear(); |
| 602 | contents.clear(); |
| 603 | |
| 604 | applicationIds << |
| 605 | "com.lomiri.test_MyApp3"; |
| 606 | contents << |
| 607 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 608 | "<application id=\"com.lomiri.test_MyApp3\">\n" |
| 609 | " <description>My application 3</description>\n" |
| 610 | " <service-types>\n" |
| 611 | " <service-type id=\"tstemail\">\n" |
| 612 | " <description>Send email</description>\n" |
| 613 | " </service-type>\n" |
| 614 | " </service-types>\n" |
| 615 | "</application>"; |
| 616 | QTest::newRow("email-services") << |
| 617 | applicationIds << contents << |
| 618 | (QStringList() << "cool" << "bad"); |
| 619 | applicationIds.clear(); |
| 620 | contents.clear(); |
| 621 | |
| 622 | applicationIds << |
| 623 | "com.lomiri.test_MyApp4"; |
| 624 | contents << |
| 625 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 626 | "<application id=\"com.lomiri.test_MyApp4\">\n" |
| 627 | " <description>My application 4</description>\n" |
| 628 | " <services>\n" |
| 629 | " <service id=\"cool-mail\">\n" |
| 630 | " <description>Send email</description>\n" |
| 631 | " </service>\n" |
| 632 | " <service id=\"cool-sharing\">\n" |
| 633 | " <description>Share stuff</description>\n" |
| 634 | " </service>\n" |
| 635 | " </services>\n" |
| 636 | "</application>"; |
| 637 | QTest::newRow("cool-services") << |
| 638 | applicationIds << contents << |
| 639 | (QStringList() << "cool"); |
| 640 | applicationIds.clear(); |
| 641 | contents.clear(); |
| 642 | |
| 643 | applicationIds << |
| 644 | "com.lomiri.test_MyApp5" << |
| 645 | "com.lomiri.test_MyApp6"; |
| 646 | contents << |
| 647 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 648 | "<application id=\"com.lomiri.test_MyApp5\">\n" |
| 649 | " <description>My application 5</description>\n" |
| 650 | " <services>\n" |
| 651 | " <service id=\"cool-mail\">\n" |
| 652 | " <description>Send email</description>\n" |
| 653 | " </service>\n" |
| 654 | " </services>\n" |
| 655 | "</application>" << |
| 656 | "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 657 | "<application id=\"com.lomiri.test_MyApp6\">\n" |
| 658 | " <description>My application 6</description>\n" |
| 659 | " <services>\n" |
| 660 | " <service id=\"bad-sharing\">\n" |
| 661 | " <description>Share stuff</description>\n" |
| 662 | " </service>\n" |
| 663 | " </services>\n" |
| 664 | "</application>"; |
| 665 | QTest::newRow("two apps") << |
| 666 | applicationIds << contents << |
| 667 | (QStringList() << "cool" << "bad"); |
| 668 | applicationIds.clear(); |
| 669 | contents.clear(); |
| 670 | } |
| 671 | |
| 672 | void ApplicationManagerTest::testUsefulProviders() |
| 673 | { |
| 674 | clearApplicationsDir(); |
| 675 | |
| 676 | QFETCH(QStringList, applicationIds); |
| 677 | QFETCH(QStringList, contents); |
| 678 | QFETCH(QStringList, expectedProviders); |
| 679 | |
| 680 | for (int i = 0; i < applicationIds.count(); i++) { |
| 681 | writeAccountsFile(applicationIds[i] + ".application", contents[i]); |
| 682 | } |
| 683 | |
| 684 | ApplicationManager manager; |
| 685 | |
| 686 | QStringList providers = manager.usefulProviders(); |
| 687 | QCOMPARE(QSet<QString>(providers.constBegin(), providers.constEnd()), QSet<QString>(expectedProviders.constBegin(), expectedProviders.constEnd())); |
| 688 | } |
| 689 | |
| 690 | QTEST_GUILESS_MAIN(ApplicationManagerTest); |
| 691 | |
| 692 | #include "tst_application_manager.moc" |