| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of OnlineAccountsClient. |
| 7 | * |
| 8 | * OnlineAccountsClient is free software: you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public License as |
| 10 | * published by the Free Software Foundation, either version 3 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * OnlineAccountsClient is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public License |
| 19 | * along with OnlineAccountsClient. If not, see |
| 20 | * <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include <online-accounts-common/globals.h> |
| 24 | |
| 25 | #include <LomiriOnlineAccountsClient/Setup> |
| 26 | #include <QDBusConnection> |
| 27 | #include <QDBusContext> |
| 28 | #include <QDebug> |
| 29 | #include <QFile> |
| 30 | #include <QProcessEnvironment> |
| 31 | #include <QSignalSpy> |
| 32 | #include <QTest> |
| 33 | |
| 34 | using namespace OnlineAccountsClient; |
| 35 | |
| 36 | class Service: public QObject, QDBusContext |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | Q_CLASSINFO("D-Bus Interface", |
| 40 | "com.lomiri.OnlineAccountsUi") |
| 41 | |
| 42 | public: |
| 43 | Service(): QObject() {} |
| 44 | QVariantMap options() const { return m_options; } |
| 45 | |
| 46 | void setError(const QString &name) { m_errorName = name; } |
| 47 | |
| 48 | public Q_SLOTS: |
| 49 | QVariantMap requestAccess(const QVariantMap &options) { |
| 50 | m_options = options; |
| 51 | if (!m_errorName.isEmpty()) { |
| 52 | sendErrorReply(m_errorName, "Some error"); |
| 53 | m_errorName = QString(); |
| 54 | } |
| 55 | return QVariantMap(); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | QVariantMap m_options; |
| 60 | QString m_errorName; |
| 61 | }; |
| 62 | |
| 63 | class SetupTest: public QObject |
| 64 | { |
| 65 | Q_OBJECT |
| 66 | |
| 67 | public: |
| 68 | SetupTest(); |
| 69 | QVariantMap options() const { return m_service.options(); } |
| 70 | |
| 71 | private Q_SLOTS: |
| 72 | void initTestCase(); |
| 73 | void testProperties(); |
| 74 | void testExec(); |
| 75 | void testExecWithProvider(); |
| 76 | void testExecWithServiceType(); |
| 77 | void testExecWithApplication(); |
| 78 | void testError(); |
| 79 | void testWindowId(); |
| 80 | |
| 81 | private: |
| 82 | Service m_service; |
| 83 | }; |
| 84 | |
| 85 | SetupTest::SetupTest(): |
| 86 | QObject(0) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void SetupTest::initTestCase() |
| 91 | { |
| 92 | QDBusConnection connection = QDBusConnection::sessionBus(); |
| 93 | connection.registerObject(OAU_OBJECT_PATH, &m_service, |
| 94 | QDBusConnection::ExportAllContents); |
| 95 | connection.registerService(OAU_SERVICE_NAME); |
| 96 | } |
| 97 | |
| 98 | void SetupTest::testProperties() |
| 99 | { |
| 100 | Setup setup; |
| 101 | |
| 102 | QCOMPARE(setup.applicationId(), QString()); |
| 103 | QCOMPARE(setup.providerId(), QString()); |
| 104 | QCOMPARE(setup.serviceTypeId(), QString()); |
| 105 | |
| 106 | setup.setApplicationId("hi!"); |
| 107 | QCOMPARE(setup.applicationId(), QString("hi!")); |
| 108 | |
| 109 | setup.setProviderId("ciao"); |
| 110 | QCOMPARE(setup.providerId(), QString("ciao")); |
| 111 | |
| 112 | setup.setServiceTypeId("hello"); |
| 113 | QCOMPARE(setup.serviceTypeId(), QString("hello")); |
| 114 | } |
| 115 | |
| 116 | void SetupTest::testExec() |
| 117 | { |
| 118 | Setup setup; |
| 119 | |
| 120 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 121 | setup.exec(); |
| 122 | |
| 123 | QVERIFY(finished.wait(10000)); |
| 124 | QCOMPARE(options().contains(OAU_KEY_APPLICATION), false); |
| 125 | QCOMPARE(options().contains(OAU_KEY_PROVIDER), false); |
| 126 | QCOMPARE(options().contains(OAU_KEY_SERVICE_TYPE), false); |
| 127 | QCOMPARE(options().contains(OAU_KEY_WINDOW_ID), false); |
| 128 | } |
| 129 | |
| 130 | void SetupTest::testExecWithProvider() |
| 131 | { |
| 132 | Setup setup; |
| 133 | setup.setProviderId("lethal-provider"); |
| 134 | |
| 135 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 136 | setup.exec(); |
| 137 | |
| 138 | QVERIFY(finished.wait(10000)); |
| 139 | QCOMPARE(options().value(OAU_KEY_PROVIDER).toString(), |
| 140 | QStringLiteral("lethal-provider")); |
| 141 | } |
| 142 | |
| 143 | void SetupTest::testExecWithServiceType() |
| 144 | { |
| 145 | Setup setup; |
| 146 | setup.setServiceTypeId("e-mail"); |
| 147 | |
| 148 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 149 | setup.exec(); |
| 150 | |
| 151 | QVERIFY(finished.wait(10000)); |
| 152 | QCOMPARE(options().value(OAU_KEY_SERVICE_TYPE).toString(), |
| 153 | QStringLiteral("e-mail")); |
| 154 | } |
| 155 | |
| 156 | void SetupTest::testExecWithApplication() |
| 157 | { |
| 158 | Setup setup; |
| 159 | setup.setApplicationId("MyApp"); |
| 160 | |
| 161 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 162 | setup.exec(); |
| 163 | |
| 164 | QVERIFY(finished.wait(10000)); |
| 165 | QCOMPARE(options().value(OAU_KEY_APPLICATION).toString(), |
| 166 | QStringLiteral("MyApp")); |
| 167 | } |
| 168 | |
| 169 | void SetupTest::testWindowId() |
| 170 | { |
| 171 | Setup setup; |
| 172 | |
| 173 | QWindow window; |
| 174 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 175 | setup.exec(); |
| 176 | |
| 177 | QVERIFY(finished.wait()); |
| 178 | QCOMPARE(options().value(OAU_KEY_WINDOW_ID).toUInt(), |
| 179 | uint(window.winId())); |
| 180 | } |
| 181 | |
| 182 | void SetupTest::testError() |
| 183 | { |
| 184 | m_service.setError("com.lomiri.SomeError"); |
| 185 | |
| 186 | Setup setup; |
| 187 | |
| 188 | QSignalSpy finished(&setup, SIGNAL(finished(QVariantMap))); |
| 189 | setup.exec(); |
| 190 | |
| 191 | QVERIFY(finished.wait(10000)); |
| 192 | QCOMPARE(finished.count(), 1); |
| 193 | QCOMPARE(finished.at(0).at(0).toMap().value("errorName").toString(), |
| 194 | QString("com.lomiri.SomeError")); |
| 195 | } |
| 196 | |
| 197 | QTEST_MAIN(SetupTest); |
| 198 | |
| 199 | #include "tst_client.moc" |