| 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 <QDBusConnection> |
| 26 | #include <QDebug> |
| 27 | #include <QQmlComponent> |
| 28 | #include <QQmlContext> |
| 29 | #include <QQmlEngine> |
| 30 | #include <QQmlError> |
| 31 | #include <QSignalSpy> |
| 32 | #include <QTest> |
| 33 | |
| 34 | class Service: public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | Q_CLASSINFO("D-Bus Interface", |
| 38 | "com.lomiri.OnlineAccountsUi") |
| 39 | |
| 40 | public: |
| 41 | Service(): QObject() {} |
| 42 | QVariantMap options() const { return m_options; } |
| 43 | |
| 44 | public Q_SLOTS: |
| 45 | QVariantMap requestAccess(const QVariantMap &options) { |
| 46 | m_options = options; |
| 47 | return QVariantMap(); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | QVariantMap m_options; |
| 52 | }; |
| 53 | |
| 54 | class SetupTest: public QObject |
| 55 | { |
| 56 | Q_OBJECT |
| 57 | |
| 58 | public: |
| 59 | SetupTest(); |
| 60 | QVariantMap options() const { return m_service.options(); } |
| 61 | |
| 62 | private Q_SLOTS: |
| 63 | void initTestCase(); |
| 64 | void testLoadPlugin(); |
| 65 | void testProperties(); |
| 66 | void testVersions_data(); |
| 67 | void testVersions(); |
| 68 | void testExec(); |
| 69 | void testExecWithServiceType(); |
| 70 | |
| 71 | private: |
| 72 | Service m_service; |
| 73 | }; |
| 74 | |
| 75 | SetupTest::SetupTest(): |
| 76 | QObject(0) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void SetupTest::initTestCase() |
| 81 | { |
| 82 | qputenv("QML2_IMPORT_PATH", PLUGIN_PATH); |
| 83 | QDBusConnection connection = QDBusConnection::sessionBus(); |
| 84 | connection.registerObject(OAU_OBJECT_PATH, &m_service, |
| 85 | QDBusConnection::ExportAllContents); |
| 86 | connection.registerService(OAU_SERVICE_NAME); |
| 87 | } |
| 88 | |
| 89 | void SetupTest::testLoadPlugin() |
| 90 | { |
| 91 | QQmlEngine engine; |
| 92 | QQmlComponent component(&engine); |
| 93 | component.setData("import Lomiri.OnlineAccounts.Client 0.1\n" |
| 94 | "Setup {}", |
| 95 | QUrl()); |
| 96 | QObject *object = component.create(); |
| 97 | if (component.isError()) { |
| 98 | Q_FOREACH(const QQmlError &error, component.errors()) { |
| 99 | qDebug() << error; |
| 100 | } |
| 101 | } |
| 102 | QVERIFY(object != 0); |
| 103 | delete object; |
| 104 | } |
| 105 | |
| 106 | void SetupTest::testProperties() |
| 107 | { |
| 108 | QQmlEngine engine; |
| 109 | QQmlComponent component(&engine); |
| 110 | component.setData("import Lomiri.OnlineAccounts.Client 0.2\n" |
| 111 | "Setup { providerId: \"hello\" }", |
| 112 | QUrl()); |
| 113 | QObject *object = component.create(); |
| 114 | QVERIFY(object != 0); |
| 115 | |
| 116 | QCOMPARE(object->property("providerId").toString(), QString("hello")); |
| 117 | QCOMPARE(object->property("serviceTypeId").toString(), QString()); |
| 118 | |
| 119 | object->setProperty("providerId", QString("ciao")); |
| 120 | QCOMPARE(object->property("providerId").toString(), QString("ciao")); |
| 121 | |
| 122 | object->setProperty("serviceTypeId", QString("hi")); |
| 123 | QCOMPARE(object->property("serviceTypeId").toString(), QString("hi")); |
| 124 | |
| 125 | object->setProperty("serviceId", QString("bonjour")); |
| 126 | QCOMPARE(object->property("serviceId").toString(), QString("bonjour")); |
| 127 | |
| 128 | delete object; |
| 129 | } |
| 130 | |
| 131 | void SetupTest::testVersions_data() |
| 132 | { |
| 133 | QTest::addColumn<QString>("qmlCode"); |
| 134 | QTest::addColumn<bool>("mustBuild"); |
| 135 | |
| 136 | QTest::newRow("0.1, regular") << |
| 137 | "import Lomiri.OnlineAccounts.Client 0.1\n" |
| 138 | "Setup {\n" |
| 139 | " providerId: \"hello\"\n" |
| 140 | " serviceTypeId: \"something\"\n" |
| 141 | "}" << |
| 142 | true; |
| 143 | |
| 144 | QTest::newRow("0.1, newer prop") << |
| 145 | "import Lomiri.OnlineAccounts.Client 0.1\n" |
| 146 | "Setup {\n" |
| 147 | " providerId: \"hello\"\n" |
| 148 | " serviceTypeId: \"something\"\n" |
| 149 | " serviceId: \"wishful\"\n" |
| 150 | "}" << |
| 151 | false; |
| 152 | |
| 153 | QTest::newRow("0.2, newer prop") << |
| 154 | "import Lomiri.OnlineAccounts.Client 0.2\n" |
| 155 | "Setup {\n" |
| 156 | " providerId: \"hello\"\n" |
| 157 | " serviceTypeId: \"something\"\n" |
| 158 | " serviceId: \"yes we can\"\n" |
| 159 | "}" << |
| 160 | true; |
| 161 | } |
| 162 | |
| 163 | void SetupTest::testVersions() |
| 164 | { |
| 165 | QFETCH(QString, qmlCode); |
| 166 | QFETCH(bool, mustBuild); |
| 167 | |
| 168 | if (!mustBuild) { |
| 169 | QTest::ignoreMessage(QtWarningMsg, |
| 170 | "QQmlComponent: Component is not ready"); |
| 171 | } |
| 172 | |
| 173 | QQmlEngine engine; |
| 174 | QQmlComponent component(&engine); |
| 175 | component.setData(qmlCode.toUtf8(), QUrl()); |
| 176 | QObject *object = component.create(); |
| 177 | QCOMPARE(object != 0, mustBuild); |
| 178 | |
| 179 | delete object; |
| 180 | } |
| 181 | |
| 182 | void SetupTest::testExec() |
| 183 | { |
| 184 | QQmlEngine engine; |
| 185 | QQmlComponent component(&engine); |
| 186 | component.setData("import Lomiri.OnlineAccounts.Client 0.1\n" |
| 187 | "Setup {}", |
| 188 | QUrl()); |
| 189 | QObject *object = component.create(); |
| 190 | QVERIFY(object != 0); |
| 191 | |
| 192 | QSignalSpy finished(object, SIGNAL(finished(QVariantMap))); |
| 193 | QVERIFY(QMetaObject::invokeMethod(object, "exec")); |
| 194 | |
| 195 | QVERIFY(finished.wait()); |
| 196 | QCOMPARE(options().contains(OAU_KEY_PROVIDER), false); |
| 197 | QCOMPARE(options().contains(OAU_KEY_SERVICE_TYPE), false); |
| 198 | QCOMPARE(options().contains(OAU_KEY_SERVICE_ID), false); |
| 199 | } |
| 200 | |
| 201 | void SetupTest::testExecWithServiceType() |
| 202 | { |
| 203 | QQmlEngine engine; |
| 204 | QQmlComponent component(&engine); |
| 205 | component.setData("import Lomiri.OnlineAccounts.Client 0.1\n" |
| 206 | "Setup { serviceTypeId: \"e-mail\" }", |
| 207 | QUrl()); |
| 208 | QObject *object = component.create(); |
| 209 | QVERIFY(object != 0); |
| 210 | |
| 211 | QSignalSpy finished(object, SIGNAL(finished(QVariantMap))); |
| 212 | QVERIFY(QMetaObject::invokeMethod(object, "exec")); |
| 213 | |
| 214 | QVERIFY(finished.wait()); |
| 215 | QCOMPARE(options().value(OAU_KEY_SERVICE_TYPE).toString(), |
| 216 | QStringLiteral("e-mail")); |
| 217 | } |
| 218 | |
| 219 | QTEST_MAIN(SetupTest); |
| 220 | |
| 221 | #include "tst_qml_client.moc" |