| 1 | /* |
| 2 | * Copyright (C) 2013 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 "access-model.h" |
| 22 | #include <online-accounts-common/debug.h> |
| 23 | |
| 24 | #include <Accounts/Account> |
| 25 | #include <Accounts/Manager> |
| 26 | #include <Accounts/Service> |
| 27 | #include <QDebug> |
| 28 | #include <QDir> |
| 29 | #include <QQmlComponent> |
| 30 | #include <QQmlContext> |
| 31 | #include <QQmlEngine> |
| 32 | #include <QSignalSpy> |
| 33 | #include <QTest> |
| 34 | |
| 35 | using namespace OnlineAccountsUi; |
| 36 | |
| 37 | class AccessModelTest: public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | AccessModelTest(); |
| 43 | |
| 44 | private Q_SLOTS: |
| 45 | void initTestCase(); |
| 46 | void testEmpty(); |
| 47 | void testProxy(); |
| 48 | void testEnabling(); |
| 49 | |
| 50 | private: |
| 51 | void clearDb(); |
| 52 | }; |
| 53 | |
| 54 | AccessModelTest::AccessModelTest(): |
| 55 | QObject(0) |
| 56 | { |
| 57 | setLoggingLevel(2); |
| 58 | } |
| 59 | |
| 60 | void AccessModelTest::clearDb() |
| 61 | { |
| 62 | QDir dbroot(QString::fromLatin1(qgetenv("ACCOUNTS"))); |
| 63 | dbroot.remove("accounts.db"); |
| 64 | } |
| 65 | |
| 66 | void AccessModelTest::initTestCase() |
| 67 | { |
| 68 | qputenv("ACCOUNTS", TMP_DIR "/"); |
| 69 | qputenv("AG_APPLICATIONS", TEST_DATA_DIR); |
| 70 | qputenv("AG_SERVICES", TEST_DATA_DIR); |
| 71 | qputenv("AG_SERVICE_TYPES", TEST_DATA_DIR); |
| 72 | qputenv("AG_PROVIDERS", TEST_DATA_DIR); |
| 73 | qputenv("XDG_DATA_HOME", TEST_DATA_DIR); |
| 74 | |
| 75 | /* Environment may be set up for i.e. SSO.OnlineAccounts import */ |
| 76 | QString newImportPath = QLatin1String(PLUGIN_PATH); |
| 77 | QString oldImportPath = qgetenv("QML2_IMPORT_PATH"); |
| 78 | if (!oldImportPath.isEmpty()) { |
| 79 | newImportPath += QStringLiteral(":") + oldImportPath; |
| 80 | } |
| 81 | qputenv("QML2_IMPORT_PATH", newImportPath.toUtf8()); |
| 82 | |
| 83 | clearDb(); |
| 84 | } |
| 85 | |
| 86 | void AccessModelTest::testEmpty() |
| 87 | { |
| 88 | QQmlEngine engine; |
| 89 | QQmlComponent component(&engine); |
| 90 | component.setData("import SSO.OnlineAccounts 0.1\n" |
| 91 | "AccountServiceModel {\n" |
| 92 | " provider: \"cool\"\n" |
| 93 | " service: \"global\"\n" |
| 94 | "}", |
| 95 | QUrl()); |
| 96 | QAbstractListModel *accountModel = |
| 97 | qobject_cast<QAbstractListModel*>(component.create()); |
| 98 | QVERIFY(accountModel != 0); |
| 99 | |
| 100 | QCOMPARE(accountModel->property("count").toInt(), 0); |
| 101 | |
| 102 | /* Create the access model */ |
| 103 | AccessModel *model = new AccessModel(this); |
| 104 | QVERIFY(model != 0); |
| 105 | |
| 106 | QSignalSpy countChanged(model, SIGNAL(countChanged())); |
| 107 | QSignalSpy rowsInserted(model, SIGNAL(rowsInserted(const QModelIndex&,int,int))); |
| 108 | QSignalSpy rowsRemoved(model, SIGNAL(rowsRemoved(const QModelIndex&,int,int))); |
| 109 | |
| 110 | QCOMPARE(model->applicationId(), QString()); |
| 111 | QVERIFY(model->accountModel() == 0); |
| 112 | QCOMPARE(model->rowCount(), 0); |
| 113 | |
| 114 | model->setAccountModel(accountModel); |
| 115 | QCOMPARE(model->rowCount(), 0); |
| 116 | |
| 117 | QCOMPARE(countChanged.count(), 0); |
| 118 | QCOMPARE(rowsInserted.count(), 0); |
| 119 | QCOMPARE(rowsRemoved.count(), 0); |
| 120 | |
| 121 | delete model; |
| 122 | delete accountModel; |
| 123 | } |
| 124 | |
| 125 | void AccessModelTest::testProxy() |
| 126 | { |
| 127 | QQmlEngine engine; |
| 128 | QQmlComponent component(&engine); |
| 129 | component.setData("import SSO.OnlineAccounts 0.1\n" |
| 130 | "AccountServiceModel {\n" |
| 131 | " provider: \"cool\"\n" |
| 132 | " service: \"global\"\n" |
| 133 | "}", |
| 134 | QUrl()); |
| 135 | QAbstractListModel *accountModel = |
| 136 | qobject_cast<QAbstractListModel*>(component.create()); |
| 137 | QVERIFY(accountModel != 0); |
| 138 | |
| 139 | QCOMPARE(accountModel->property("count").toInt(), 0); |
| 140 | |
| 141 | /* Create the access model */ |
| 142 | AccessModel *model = new AccessModel(this); |
| 143 | QVERIFY(model != 0); |
| 144 | |
| 145 | model->setAccountModel(accountModel); |
| 146 | model->setApplicationId("mailer"); |
| 147 | QCOMPARE(model->rowCount(), 0); |
| 148 | |
| 149 | /* Now add and remove accounts, and see that the accessModel picks them up */ |
| 150 | QSignalSpy rowsInserted(model, |
| 151 | SIGNAL(rowsInserted(const QModelIndex&,int,int))); |
| 152 | QSignalSpy rowsRemoved(model, |
| 153 | SIGNAL(rowsRemoved(const QModelIndex&,int,int))); |
| 154 | |
| 155 | Accounts::Manager *manager = new Accounts::Manager(this); |
| 156 | Accounts::Account *account1 = manager->createAccount("cool"); |
| 157 | QVERIFY(account1 != 0); |
| 158 | account1->setEnabled(true); |
| 159 | account1->setDisplayName("CoolAccount"); |
| 160 | account1->syncAndBlock(); |
| 161 | |
| 162 | rowsInserted.wait(); |
| 163 | QCOMPARE(model->rowCount(), 1); |
| 164 | QCOMPARE(rowsInserted.count(), 1); |
| 165 | QCOMPARE(rowsRemoved.count(), 0); |
| 166 | rowsInserted.clear(); |
| 167 | QCOMPARE(model->get(0, "displayName").toString(), QString("CoolAccount")); |
| 168 | |
| 169 | /* Create a second account */ |
| 170 | Accounts::Account *account2 = manager->createAccount("cool"); |
| 171 | QVERIFY(account2 != 0); |
| 172 | account2->setEnabled(true); |
| 173 | account2->setDisplayName("UncoolAccount"); |
| 174 | account2->syncAndBlock(); |
| 175 | |
| 176 | rowsInserted.wait(); |
| 177 | QCOMPARE(model->rowCount(), 2); |
| 178 | QCOMPARE(rowsInserted.count(), 1); |
| 179 | int row = rowsInserted.at(0).at(1).toInt(); |
| 180 | QCOMPARE(rowsRemoved.count(), 0); |
| 181 | rowsInserted.clear(); |
| 182 | QCOMPARE(model->get(row, "displayName").toString(), QString("UncoolAccount")); |
| 183 | |
| 184 | /* Delete the first account */ |
| 185 | account1->remove(); |
| 186 | account1->syncAndBlock(); |
| 187 | rowsRemoved.wait(); |
| 188 | QCOMPARE(model->rowCount(), 1); |
| 189 | QCOMPARE(rowsInserted.count(), 0); |
| 190 | QCOMPARE(rowsRemoved.count(), 1); |
| 191 | rowsRemoved.clear(); |
| 192 | QCOMPARE(model->get(0, "displayName").toString(), QString("UncoolAccount")); |
| 193 | |
| 194 | /* Delete also the second account */ |
| 195 | account2->remove(); |
| 196 | account2->syncAndBlock(); |
| 197 | rowsRemoved.wait(); |
| 198 | QCOMPARE(model->rowCount(), 0); |
| 199 | QCOMPARE(rowsInserted.count(), 0); |
| 200 | QCOMPARE(rowsRemoved.count(), 1); |
| 201 | rowsRemoved.clear(); |
| 202 | |
| 203 | delete model; |
| 204 | delete accountModel; |
| 205 | } |
| 206 | |
| 207 | void AccessModelTest::testEnabling() |
| 208 | { |
| 209 | QQmlEngine engine; |
| 210 | QQmlComponent component(&engine); |
| 211 | component.setData("import SSO.OnlineAccounts 0.1\n" |
| 212 | "AccountServiceModel {\n" |
| 213 | " provider: \"cool\"\n" |
| 214 | " service: \"global\"\n" |
| 215 | "}", |
| 216 | QUrl()); |
| 217 | QAbstractListModel *accountModel = |
| 218 | qobject_cast<QAbstractListModel*>(component.create()); |
| 219 | QVERIFY(accountModel != 0); |
| 220 | |
| 221 | QCOMPARE(accountModel->property("count").toInt(), 0); |
| 222 | |
| 223 | /* Create the access model */ |
| 224 | AccessModel *model = new AccessModel(this); |
| 225 | QVERIFY(model != 0); |
| 226 | |
| 227 | model->setAccountModel(accountModel); |
| 228 | model->setApplicationId("mailer"); |
| 229 | QCOMPARE(model->rowCount(), 0); |
| 230 | |
| 231 | /* Now add two accounts, but verify that the model should expose only the |
| 232 | * one which has at least one service disabled */ |
| 233 | QSignalSpy rowsInserted(model, |
| 234 | SIGNAL(rowsInserted(const QModelIndex&,int,int))); |
| 235 | QSignalSpy rowsRemoved(model, |
| 236 | SIGNAL(rowsRemoved(const QModelIndex&,int,int))); |
| 237 | |
| 238 | Accounts::Manager *manager = new Accounts::Manager(this); |
| 239 | Accounts::Service coolMail = manager->service("coolmail"); |
| 240 | Accounts::Service coolShare = manager->service("coolshare"); |
| 241 | Accounts::Account *account1 = manager->createAccount("cool"); |
| 242 | QVERIFY(account1 != 0); |
| 243 | account1->setEnabled(true); |
| 244 | account1->setDisplayName("CoolAccount"); |
| 245 | account1->selectService(coolMail); |
| 246 | account1->setEnabled(true); |
| 247 | account1->syncAndBlock(); |
| 248 | |
| 249 | rowsInserted.wait(); |
| 250 | QCOMPARE(model->rowCount(), 1); |
| 251 | QCOMPARE(rowsInserted.count(), 1); |
| 252 | QCOMPARE(rowsRemoved.count(), 0); |
| 253 | rowsInserted.clear(); |
| 254 | QCOMPARE(model->get(0, "displayName").toString(), QString("CoolAccount")); |
| 255 | |
| 256 | /* Create a second account, enable all of its services */ |
| 257 | Accounts::Account *account2 = manager->createAccount("cool"); |
| 258 | QVERIFY(account2 != 0); |
| 259 | account2->setEnabled(true); |
| 260 | account2->setDisplayName("UncoolAccount"); |
| 261 | account2->selectService(coolMail); |
| 262 | account2->setEnabled(true); |
| 263 | account2->selectService(coolShare); |
| 264 | account2->setEnabled(true); |
| 265 | account2->syncAndBlock(); |
| 266 | |
| 267 | /* Verify that a row is *not* added */ |
| 268 | QTest::qWait(50); |
| 269 | QCOMPARE(model->rowCount(), 1); |
| 270 | QCOMPARE(rowsInserted.count(), 0); |
| 271 | QCOMPARE(rowsRemoved.count(), 0); |
| 272 | rowsInserted.clear(); |
| 273 | |
| 274 | delete model; |
| 275 | delete accountModel; |
| 276 | } |
| 277 | |
| 278 | QTEST_MAIN(AccessModelTest); |
| 279 | |
| 280 | #include "tst_access_model.moc" |