| 1 | /* |
| 2 | * Copyright (C) 2014 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui |
| 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 <online-accounts-common/debug.h> |
| 22 | #include <online-accounts-common/globals.h> |
| 23 | #include "mock/application-manager-mock.h" |
| 24 | #include "mock/request-mock.h" |
| 25 | #include "mock/ui-server-mock.h" |
| 26 | #include "provider-request.h" |
| 27 | |
| 28 | #include <QDebug> |
| 29 | #include <QJsonDocument> |
| 30 | #include <QJsonObject> |
| 31 | #include <QQmlContext> |
| 32 | #include <QQmlEngine> |
| 33 | #include <QQuickView> |
| 34 | #include <QSignalSpy> |
| 35 | #include <QTest> |
| 36 | |
| 37 | using namespace OnlineAccountsUi; |
| 38 | |
| 39 | namespace QTest { |
| 40 | template<> |
| 41 | char *toString(const QVariantMap &p) |
| 42 | { |
| 43 | QJsonDocument doc(QJsonObject::fromVariantMap(p)); |
| 44 | QByteArray ba = doc.toJson(QJsonDocument::Compact); |
| 45 | return qstrdup(ba.data()); |
| 46 | } |
| 47 | } // QTest namespace |
| 48 | |
| 49 | class TestRequest: public ProviderRequest |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | |
| 53 | public: |
| 54 | TestRequest(const QVariantMap ¶meters, const QString &profile, |
| 55 | QObject *parent = 0): |
| 56 | ProviderRequest("interface", 0, profile, parameters, parent) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class ProviderRequestTest: public QObject |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | |
| 65 | public: |
| 66 | ProviderRequestTest(); |
| 67 | |
| 68 | private Q_SLOTS: |
| 69 | void initTestCase(); |
| 70 | void testParameters_data(); |
| 71 | void testParameters(); |
| 72 | |
| 73 | private: |
| 74 | UiServer m_uiServer; |
| 75 | }; |
| 76 | |
| 77 | ProviderRequestTest::ProviderRequestTest(): |
| 78 | QObject(), |
| 79 | m_uiServer("fake") |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | void ProviderRequestTest::initTestCase() |
| 84 | { |
| 85 | qputenv("ACCOUNTS", TMP_DIR "/"); |
| 86 | qputenv("AG_APPLICATIONS", TEST_DATA_DIR); |
| 87 | qputenv("AG_SERVICES", TEST_DATA_DIR); |
| 88 | qputenv("AG_SERVICE_TYPES", TEST_DATA_DIR); |
| 89 | qputenv("AG_PROVIDERS", TEST_DATA_DIR); |
| 90 | qputenv("XDG_DATA_HOME", TEST_DATA_DIR); |
| 91 | } |
| 92 | |
| 93 | void ProviderRequestTest::testParameters_data() |
| 94 | { |
| 95 | QTest::addColumn<QVariantMap>("parameters"); |
| 96 | QTest::addColumn<QString>("profile"); |
| 97 | QTest::addColumn<QString>("providerId"); |
| 98 | QTest::addColumn<QVariantMap>("providerInfo"); |
| 99 | QTest::addColumn<QVariantMap>("applicationInfo"); |
| 100 | QTest::addColumn<QString>("errorName"); |
| 101 | |
| 102 | QTest::newRow("empty") << |
| 103 | QVariantMap() << |
| 104 | QString() << |
| 105 | QString() << |
| 106 | QVariantMap() << |
| 107 | QVariantMap() << |
| 108 | OAU_ERROR_INVALID_APPLICATION; |
| 109 | |
| 110 | QVariantMap parameters; |
| 111 | QVariantMap providerInfo; |
| 112 | QVariantMap applicationInfo; |
| 113 | |
| 114 | parameters.insert(OAU_KEY_APPLICATION, "Gallery"); |
| 115 | parameters.insert(OAU_KEY_PROVIDER, "my provider"); |
| 116 | providerInfo.insert("some", "value"); |
| 117 | applicationInfo.insert("one", "two"); |
| 118 | QTest::newRow("confined app") << |
| 119 | parameters << |
| 120 | "my-app" << |
| 121 | "my provider" << |
| 122 | providerInfo << |
| 123 | applicationInfo << |
| 124 | ""; |
| 125 | parameters.clear(); |
| 126 | providerInfo.clear(); |
| 127 | applicationInfo.clear(); |
| 128 | |
| 129 | parameters.insert(OAU_KEY_APPLICATION, "Gallery"); |
| 130 | parameters.insert(OAU_KEY_SERVICE_ID, "coolmail"); |
| 131 | providerInfo.insert("name", "cool"); |
| 132 | applicationInfo.insert("one", "two"); |
| 133 | QTest::newRow("by service ID") << |
| 134 | parameters << |
| 135 | "my-app" << |
| 136 | "cool" << |
| 137 | providerInfo << |
| 138 | applicationInfo << |
| 139 | ""; |
| 140 | parameters.clear(); |
| 141 | providerInfo.clear(); |
| 142 | applicationInfo.clear(); |
| 143 | } |
| 144 | |
| 145 | void ProviderRequestTest::testParameters() |
| 146 | { |
| 147 | QFETCH(QVariantMap, parameters); |
| 148 | QFETCH(QString, profile); |
| 149 | QFETCH(QString, providerId); |
| 150 | QFETCH(QVariantMap, providerInfo); |
| 151 | QFETCH(QVariantMap, applicationInfo); |
| 152 | QFETCH(QString, errorName); |
| 153 | |
| 154 | TestRequest request(parameters, profile); |
| 155 | RequestPrivate *mockedRequest = RequestPrivate::mocked(&request); |
| 156 | QSignalSpy setWindowCalled(mockedRequest, |
| 157 | SIGNAL(setWindowCalled(QWindow*))); |
| 158 | QSignalSpy failCalled(mockedRequest, |
| 159 | SIGNAL(failCalled(const QString&, const QString&))); |
| 160 | |
| 161 | ApplicationManager *appManager = ApplicationManager::instance(); |
| 162 | ApplicationManagerPrivate *mockedAppManager = |
| 163 | ApplicationManagerPrivate::mocked(appManager); |
| 164 | mockedAppManager->setApplicationInfo(parameters[OAU_KEY_APPLICATION].toString(), |
| 165 | applicationInfo); |
| 166 | mockedAppManager->setProviderInfo(providerId, providerInfo); |
| 167 | QSignalSpy applicationInfoCalled(mockedAppManager, |
| 168 | SIGNAL(applicationInfoCalled(QString,QString))); |
| 169 | |
| 170 | request.start(); |
| 171 | |
| 172 | if (errorName.isEmpty()) { |
| 173 | QTRY_COMPARE(setWindowCalled.count(), 1); |
| 174 | QQuickView *view = static_cast<QQuickView*>(setWindowCalled.at(0).at(0).value<QWindow*>()); |
| 175 | QQmlContext *context = view->engine()->rootContext(); |
| 176 | QObject *request = context->contextProperty("request").value<QObject*>(); |
| 177 | |
| 178 | QCOMPARE(applicationInfoCalled.count(), 1); |
| 179 | QCOMPARE(applicationInfoCalled.at(0).at(1).toString(), profile); |
| 180 | |
| 181 | QCOMPARE(request->property("provider").toMap(), providerInfo); |
| 182 | QCOMPARE(request->property("application").toMap(), applicationInfo); |
| 183 | } else { |
| 184 | QTRY_COMPARE(failCalled.count(), 1); |
| 185 | QCOMPARE(failCalled.at(0).at(0).toString(), errorName); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | QTEST_MAIN(ProviderRequestTest); |
| 190 | |
| 191 | #include "tst_provider_request.moc" |