/* * Copyright (C) 2013 Canonical Ltd. * * Contact: Alberto Mardegan * * This file is part of liblomiri-online-accounts-plugin * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published * by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranties of * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include #define TEST_DIR TMP_DIR "/tst_application_manager" using namespace OnlineAccountsUi; class ApplicationManagerTest: public QObject { Q_OBJECT public: ApplicationManagerTest(); private Q_SLOTS: void initTestCase(); void testNonExistingApp(); void testApplicationInfo_data(); void testApplicationInfo(); void testAclAdd_data(); void testAclAdd(); void testAclRemove_data(); void testAclRemove(); void testApplicationFromProfile_data(); void testApplicationFromProfile(); void testProviderInfo_data(); void testProviderInfo(); void testUsefulProviders_data(); void testUsefulProviders(); private: void clearApplicationsDir(); void clearProvidersDir(); void clearTestDir(); void writeAccountsFile(const QString &name, const QString &contents); private: QDir m_testDir; QDir m_accountsDir; }; ApplicationManagerTest::ApplicationManagerTest(): QObject(0), m_testDir(TEST_DIR), m_accountsDir(TEST_DIR "/accounts") { } void ApplicationManagerTest::clearApplicationsDir() { QDir applicationsDir = m_accountsDir; if (applicationsDir.cd("applications")) { applicationsDir.removeRecursively(); applicationsDir.mkpath("."); } } void ApplicationManagerTest::clearProvidersDir() { QDir providersDir = m_accountsDir; if (providersDir.cd("providers")) { providersDir.removeRecursively(); providersDir.mkpath("."); } } void ApplicationManagerTest::clearTestDir() { m_testDir.removeRecursively(); m_testDir.mkpath("."); } void ApplicationManagerTest::writeAccountsFile(const QString &name, const QString &contents) { /* Create files into the appropriate directory: * .provider files go in "providers", .application in "applications" and so * on: so we just need to take the file extension and add one "s". */ QFileInfo fileInfo(name); QString subDirName = fileInfo.suffix() + "s"; m_accountsDir.mkpath(subDirName); QDir subDir = m_accountsDir; subDir.cd(subDirName); QFile file(subDir.filePath(name)); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qWarning() << "Could not write file" << name; return; } file.write(contents.toUtf8()); } void ApplicationManagerTest::initTestCase() { qputenv("ACCOUNTS", TEST_DIR); qputenv("XDG_DATA_HOME", TEST_DIR); qputenv("XDG_DATA_DIRS", TEST_DIR); clearTestDir(); /* Create a few service files */ writeAccountsFile("cool-mail.service", "\n" "\n" " tstemail\n" " Cool Mail\n" " cool\n" ""); writeAccountsFile("cool-sharing.service", "\n" "\n" " tstsharing\n" " Cool Sharing\n" " cool\n" ""); writeAccountsFile("bad-mail.service", "\n" "\n" " tstemail\n" " Bad Mail\n" " bad\n" ""); writeAccountsFile("bad-sharing.service", "\n" "\n" " tstsharing\n" " Bad Sharing\n" " bad\n" ""); /* And the relative providers */ writeAccountsFile("cool.provider", "\n" "\n" " Cool provider\n" ""); writeAccountsFile("bad.provider", "\n" "\n" " Bad provider\n" ""); } void ApplicationManagerTest::testNonExistingApp() { ApplicationManager manager; QVariantMap info = manager.applicationInfo("com.lomiri.test_MyApp", "com.lomiri.test_MyApp_0.1"); QVERIFY(info.isEmpty()); QStringList acl; acl << "one" << "two"; QStringList newAcl = manager.addApplicationToAcl(acl, "com.lomiri.test_MyApp"); QCOMPARE(newAcl, acl); newAcl = manager.removeApplicationFromAcl(acl, "com.lomiri.test_MyApp"); QCOMPARE(newAcl, acl); } void ApplicationManagerTest::testApplicationInfo_data() { QTest::addColumn("applicationId"); QTest::addColumn("contents"); QTest::addColumn("inputProfile"); QTest::addColumn("expectedProfile"); QTest::addColumn("services"); QTest::newRow("no-services") << "com.lomiri.test_MyApp" << "\n" "\n" " My application\n" " com.lomiri.test_MyApp_0.2\n" "" << "com.lomiri.test_MyApp_0.2" << "com.lomiri.test_MyApp_0.2" << QStringList(); QTest::newRow("no-valid-services") << "com.lomiri.test_MyApp2" << "\n" "\n" " My application 2\n" " \n" " Do something\n" " \n" " com.lomiri.test_MyApp2_0.2\n" "" << "com.lomiri.test_MyApp2_0.2" << "com.lomiri.test_MyApp2_0.2" << QStringList(); QTest::newRow("email-services") << "com.lomiri.test_MyApp3" << "\n" "\n" " My application 3\n" " \n" " \n" " Send email\n" " \n" " \n" " com.lomiri.test_MyApp3_0.2\n" "" << "com.lomiri.test_MyApp3_0.2" << "com.lomiri.test_MyApp3_0.2" << (QStringList() << "cool-mail" << "bad-mail"); QTest::newRow("cool-services") << "com.lomiri.test_MyApp4" << "\n" "\n" " My application 4\n" " \n" " \n" " Send email\n" " \n" " \n" " Share stuff\n" " \n" " \n" " com.lomiri.test_MyApp4_0.2\n" "" << "com.lomiri.test_MyApp4_0.2" << "com.lomiri.test_MyApp4_0.2" << (QStringList() << "cool-mail" << "cool-sharing"); QTest::newRow("unconfined app") << "com.lomiri.test_UnconfinedApp" << "\n" "\n" " My application 5\n" " \n" " \n" " Send email\n" " \n" " \n" " com.lomiri.test_UnconfinedApp_0.2\n" "" << "unconfined" << "com.lomiri.test_UnconfinedApp_0.2" << (QStringList() << "cool-mail"); QTest::newRow("unconfined app, no profile") << "com.lomiri.test_UnconfinedApp2" << "\n" "\n" " My application 6\n" " \n" " \n" " Send email\n" " \n" " \n" "" << "unconfined" << "unconfined" << (QStringList() << "cool-mail"); } void ApplicationManagerTest::testApplicationInfo() { clearApplicationsDir(); QFETCH(QString, applicationId); QFETCH(QString, contents); QFETCH(QString, inputProfile); QFETCH(QString, expectedProfile); QFETCH(QStringList, services); writeAccountsFile(applicationId + ".application", contents); ApplicationManager manager; QVariantMap info = manager.applicationInfo(applicationId, inputProfile); QCOMPARE(info.value("id").toString(), applicationId); QCOMPARE(info.value("profile").toString(), expectedProfile); QStringList info_services = info.value("services").toStringList(); QCOMPARE(QSet(info_services.constBegin(), info_services.constEnd()), QSet(services.constBegin(), services.constEnd())); } void ApplicationManagerTest::testAclAdd_data() { QTest::addColumn("applicationId"); QTest::addColumn("oldAcl"); QTest::addColumn("newAcl"); QTest::newRow("add to empty") << "com.lomiri.test_One" << QStringList() << (QStringList() << "com.lomiri.test_One_0.1"); QTest::newRow("add to one") << "com.lomiri.test_One" << (QStringList() << "some app") << (QStringList() << "some app" << "com.lomiri.test_One_0.1"); QTest::newRow("add existing") << "com.lomiri.test_One" << (QStringList() << "com.lomiri.test_One_0.1" << "some app") << (QStringList() << "com.lomiri.test_One_0.1" << "some app"); } void ApplicationManagerTest::testAclAdd() { clearApplicationsDir(); writeAccountsFile("com.lomiri.test_One.application", "\n" "\n" " My application\n" " com.lomiri.test_One_0.1\n" ""); writeAccountsFile("com.lomiri.test_Two.application", "\n" "\n" " My application\n" " com.lomiri.test_Two_0.2\n" ""); QFETCH(QString, applicationId); QFETCH(QStringList, oldAcl); QFETCH(QStringList, newAcl); ApplicationManager manager; QStringList acl = manager.addApplicationToAcl(oldAcl, applicationId); QCOMPARE(QSet(acl.constBegin(), acl.constEnd()), QSet(newAcl.constBegin(), newAcl.constEnd())); } void ApplicationManagerTest::testAclRemove_data() { QTest::addColumn("applicationId"); QTest::addColumn("oldAcl"); QTest::addColumn("newAcl"); QTest::newRow("remove from empty") << "com.lomiri.test_One" << QStringList() << QStringList(); QTest::newRow("remove from one") << "com.lomiri.test_One" << (QStringList() << "com.lomiri.test_One_0.1") << QStringList(); QTest::newRow("remove from two") << "com.lomiri.test_One" << (QStringList() << "com.lomiri.test_One_0.1" << "some app") << (QStringList() << "some app"); QTest::newRow("remove from missing") << "com.lomiri.test_One" << (QStringList() << "some other app" << "some app") << (QStringList() << "some other app" << "some app"); QTest::newRow("remove multiple versions") << "com.lomiri.test_One" << (QStringList() << "some other app" << "com.lomiri.test_One_0.1" << "com.lomiri.test_One_0.3" << "some app" << "com.lomiri.test_One_0.2") << (QStringList() << "some other app" << "some app"); } void ApplicationManagerTest::testAclRemove() { clearApplicationsDir(); writeAccountsFile("com.lomiri.test_One.application", "\n" "\n" " My application\n" " com.lomiri.test_One_0.1\n" ""); writeAccountsFile("com.lomiri.test_Two.application", "\n" "\n" " My application\n" " com.lomiri.test_Two_0.2\n" ""); QFETCH(QString, applicationId); QFETCH(QStringList, oldAcl); QFETCH(QStringList, newAcl); ApplicationManager manager; QStringList acl = manager.removeApplicationFromAcl(oldAcl, applicationId); QCOMPARE(QSet(acl.constBegin(), acl.constEnd()), QSet(newAcl.constBegin(), newAcl.constEnd())); } void ApplicationManagerTest::testApplicationFromProfile_data() { QTest::addColumn("applicationId"); QTest::addColumn("contents"); QTest::addColumn("profile"); QTest::addColumn("isValid"); QTest::newRow("id-same-as-profile") << "com.lomiri.test_MyApp_0.2" << "\n" "\n" " My application\n" " com.lomiri.test_MyApp_0.2\n" "" << "com.lomiri.test_MyApp_0.2" << true; QTest::newRow("id-with-no-version") << "com.lomiri.test_MyApp2" << "\n" "\n" " My application 2\n" " com.lomiri.test_MyApp2_0.2\n" "" << "com.lomiri.test_MyApp2_0.2" << true; QTest::newRow("id-is-just-package") << "com.lomiri.test" << "\n" "\n" " My application 3\n" " com.lomiri.test_MyApp3_0.2\n" "" << "com.lomiri.test_MyApp3_0.2" << true; QTest::newRow("not found") << "com.lomiri.test_MyApp5" << "\n" "\n" " My application 4\n" " com.lomiri.test_MyApp4_0.2\n" "" << "com.lomiri.test_MyApp4_0.2" << false; } void ApplicationManagerTest::testApplicationFromProfile() { clearApplicationsDir(); QFETCH(QString, applicationId); QFETCH(QString, contents); QFETCH(QString, profile); QFETCH(bool, isValid); writeAccountsFile(applicationId + ".application", contents); ApplicationManager manager; Accounts::Application app = manager.applicationFromProfile(profile); if (!isValid) { QVERIFY(!app.isValid()); } else { QVERIFY(app.isValid()); QCOMPARE(app.name(), applicationId); } } void ApplicationManagerTest::testProviderInfo_data() { QTest::addColumn("providerId"); QTest::addColumn("contents"); QTest::addColumn("profile"); QTest::addColumn("packageDir"); QTest::addColumn("isSingleAccount"); QTest::newRow("no profile") << "com.lomiri.test_MyPlugin" << "\n" "\n" " My Plugin\n" "" << QString() << QString() << false; QTest::newRow("single account") << "com.lomiri.test_MyPlugin" << "\n" "\n" " My Plugin\n" " true\n" "" << QString() << QString() << true; QTest::newRow("no package-dir") << "com.lomiri.test_MyPlugin2" << "\n" "\n" " My Plugin\n" " com.lomiri.test_MyPlugin2_0.2\n" " false\n" "" << "com.lomiri.test_MyPlugin2_0.2" << QString() << false; QTest::newRow("with package-dir") << "com.lomiri.test_MyPlugin3" << "\n" "\n" " My Plugin\n" " com.lomiri.test_MyPlugin3_0.2\n" " /opt/click.ubuntu.com/something\n" "" << "com.lomiri.test_MyPlugin3_0.2" << "/opt/click.ubuntu.com/something" << false; } void ApplicationManagerTest::testProviderInfo() { clearProvidersDir(); QFETCH(QString, providerId); QFETCH(QString, contents); QFETCH(QString, profile); QFETCH(QString, packageDir); QFETCH(bool, isSingleAccount); writeAccountsFile(providerId + ".provider", contents); ApplicationManager manager; QVariantMap info = manager.providerInfo(providerId); QCOMPARE(info.value("id").toString(), providerId); QCOMPARE(info.value("profile").toString(), profile); QCOMPARE(info.value("package-dir").toString(), packageDir); QCOMPARE(info.value("isSingleAccount").toBool(), isSingleAccount); } void ApplicationManagerTest::testUsefulProviders_data() { QTest::addColumn("applicationIds"); QTest::addColumn("contents"); QTest::addColumn("expectedProviders"); QStringList applicationIds; QStringList contents; applicationIds << "com.lomiri.test_MyApp"; contents << "\n" "\n" " My application\n" ""; QTest::newRow("no-services") << applicationIds << contents << QStringList(); applicationIds.clear(); contents.clear(); applicationIds << "com.lomiri.test_MyApp2"; contents << "\n" "\n" " My application 2\n" " \n" " Do something\n" " \n" ""; QTest::newRow("no-valid-services") << applicationIds << contents << QStringList(); applicationIds.clear(); contents.clear(); applicationIds << "com.lomiri.test_MyApp3"; contents << "\n" "\n" " My application 3\n" " \n" " \n" " Send email\n" " \n" " \n" ""; QTest::newRow("email-services") << applicationIds << contents << (QStringList() << "cool" << "bad"); applicationIds.clear(); contents.clear(); applicationIds << "com.lomiri.test_MyApp4"; contents << "\n" "\n" " My application 4\n" " \n" " \n" " Send email\n" " \n" " \n" " Share stuff\n" " \n" " \n" ""; QTest::newRow("cool-services") << applicationIds << contents << (QStringList() << "cool"); applicationIds.clear(); contents.clear(); applicationIds << "com.lomiri.test_MyApp5" << "com.lomiri.test_MyApp6"; contents << "\n" "\n" " My application 5\n" " \n" " \n" " Send email\n" " \n" " \n" "" << "\n" "\n" " My application 6\n" " \n" " \n" " Share stuff\n" " \n" " \n" ""; QTest::newRow("two apps") << applicationIds << contents << (QStringList() << "cool" << "bad"); applicationIds.clear(); contents.clear(); } void ApplicationManagerTest::testUsefulProviders() { clearApplicationsDir(); QFETCH(QStringList, applicationIds); QFETCH(QStringList, contents); QFETCH(QStringList, expectedProviders); for (int i = 0; i < applicationIds.count(); i++) { writeAccountsFile(applicationIds[i] + ".application", contents[i]); } ApplicationManager manager; QStringList providers = manager.usefulProviders(); QCOMPARE(QSet(providers.constBegin(), providers.constEnd()), QSet(expectedProviders.constBegin(), expectedProviders.constEnd())); } QTEST_GUILESS_MAIN(ApplicationManagerTest); #include "tst_application_manager.moc"