Debian Codegraph @

tests/online-accounts/qml_module/tst_qml_module.cpp

raw symbols SCIP
1 /*
2 * This file is part of OnlineAccountsModule
3 *
4 * Copyright (C) 2015 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License version 3, as
10 * published 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 Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <LomiriOnlineAccountsDaemon/dbus_constants.h>
22 #include <QDBusArgument>
23 #include <QDBusConnection>
24 #include <QJsonDocument>
25 #include <QJsonObject>
26 #include <QObject>
27 #include <QQmlComponent>
28 #include <QQmlEngine>
29 #include <QRegularExpression>
30 #include <QSignalSpy>
31 #include <QTest>
32 #include <libqtdbusmock/DBusMock.h>
33
34 namespace QTest {
35 template<>
36 char *toString(const QVariantMap &map)
37 {
38 QJsonDocument doc(QJsonObject::fromVariantMap(map));
39 return qstrdup(doc.toJson(QJsonDocument::Compact).data());
40 }
41 } // QTest namespace
42
43 class ModuleTest: public QObject
44 {
45 Q_OBJECT
46
47 public:
48 ModuleTest();
49
50 OrgFreedesktopDBusMockInterface &mocked() {
51 return m_mock.mockInterface(ONLINE_ACCOUNTS_MANAGER_SERVICE_NAME,
52 ONLINE_ACCOUNTS_MANAGER_PATH,
53 ONLINE_ACCOUNTS_MANAGER_INTERFACE,
54 QDBusConnection::SessionBus);
55 }
56
57 void addMockedMethod(const QString &name,
58 const QString &in_sig,
59 const QString &out_sig,
60 const QString &code)
61 {
62 return mocked().AddMethod(ONLINE_ACCOUNTS_MANAGER_INTERFACE,
63 name, in_sig, out_sig, code).waitForFinished();
64 }
65
66 void addGetAccountsMethod(const QString &code) {
67 addMockedMethod("GetAccounts", "a{sv}", "a(ua{sv})aa{sv}", code);
68 }
69
70 void emitAccountChanged(const QString &service,
71 uint accountId,
72 const QVariantMap &changes)
73 {
74 QVariantList args;
75 args << service;
76 QDBusArgument info;
77 info.beginStructure();
78 info << accountId << changes;
79 info.endStructure();
80 args << QVariant::fromValue(info);
81 mocked().EmitSignal(ONLINE_ACCOUNTS_MANAGER_INTERFACE,
82 "AccountChanged", "s(ua{sv})",
83 args);
84 }
85
86 QVariant get(const QAbstractListModel *model, int row, QString roleName)
87 {
88 QHash<int, QByteArray> roleNames = model->roleNames();
89 int role = roleNames.key(roleName.toLatin1(), -1);
90 return model->data(model->index(row), role);
91 }
92
93 private Q_SLOTS:
94 void initTestCase();
95 void cleanup();
96 void testModuleImport();
97 void testModelProperties();
98 void testServices_data();
99 void testServices();
100 void testModelRoles_data();
101 void testModelRoles();
102 void testModelFilters_data();
103 void testModelFilters();
104 void testModelChanges();
105 void testModelRequestAccess_data();
106 void testModelRequestAccess();
107 void testAccountAuthentication_data();
108 void testAccountAuthentication();
109 void testInitialization_data();
110 void testInitialization();
111
112 private:
113 QtDBusTest::DBusTestRunner m_dbus;
114 QtDBusMock::DBusMock m_mock;
115 };
116
117 ModuleTest::ModuleTest():
118 QObject(),
119 m_mock(m_dbus)
120 {
121 m_mock.registerCustomMock(ONLINE_ACCOUNTS_MANAGER_SERVICE_NAME,
122 ONLINE_ACCOUNTS_MANAGER_PATH,
123 ONLINE_ACCOUNTS_MANAGER_INTERFACE,
124 QDBusConnection::SessionBus);
125 m_dbus.startServices();
126 }
127
128 void ModuleTest::initTestCase()
129 {
130 qputenv("QML2_IMPORT_PATH", TEST_QML_IMPORT_PATH);
131 }
132
133 void ModuleTest::cleanup()
134 {
135 /* Iterate the main loop in order to execute the delayed cleanup methods
136 * and avoid memory leaks.
137 */
138 QTest::qWait(10);
139 }
140
141 void ModuleTest::testModuleImport()
142 {
143 QQmlEngine engine;
144 QQmlComponent component(&engine);
145 component.setData("import Lomiri.OnlineAccounts 2.0\n"
146 "AccountModel {}",
147 QUrl());
148 QObject *object = component.create();
149 QVERIFY(object != 0);
150 delete object;
151 }
152
153 void ModuleTest::testModelProperties()
154 {
155 QQmlEngine engine;
156 QQmlComponent component(&engine);
157 component.setData("import Lomiri.OnlineAccounts 2.0\n"
158 "AccountModel {}",
159 QUrl());
160 QObject *object = component.create();
161 QVERIFY(object != 0);
162
163 QVERIFY(object->setProperty("applicationId", "foo"));
164 QCOMPARE(object->property("applicationId").toString(), QString("foo"));
165
166 QVERIFY(object->setProperty("serviceId", "bar"));
167 QCOMPARE(object->property("serviceId").toString(), QString("bar"));
168
169 QCOMPARE(object->property("ready").toBool(), false);
170
171 delete object;
172 }
173
174 void ModuleTest::testServices_data()
175 {
176 QTest::addColumn<QString>("reply");
177 QTest::addColumn<QList<QVariantMap>>("expectedServices");
178
179 QTest::newRow("no services") <<
180 "[]" <<
181 QList<QVariantMap> {};
182
183 QTest::newRow("one service") <<
184 "[{"
185 "'" ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID "': 'app_coolshare',"
186 "'" ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME "': 'Cool Share',"
187 "'" ONLINE_ACCOUNTS_INFO_KEY_ICON_SOURCE "': 'image://theme/coolshare',"
188 "}]" <<
189 QList<QVariantMap> {
190 {
191 { "serviceId", "app_coolshare" },
192 { "displayName", "Cool Share" },
193 { "iconSource", QUrl("image://theme/coolshare")},
194 },
195 };
196 }
197
198 void ModuleTest::testServices()
199 {
200 QFETCH(QString, reply);
201 QFETCH(QList<QVariantMap>, expectedServices);
202
203 addGetAccountsMethod(QString("ret = ([], %1)").arg(reply));
204
205 QQmlEngine engine;
206 QQmlComponent component(&engine);
207 /* We could return the serviceList property directly, but that would be
208 * passing a OnlineAccounts::Service as QJSValue, which might not be a
209 * proof that every property is properly accessible from QML. Therefore, we
210 * explicitly run through the list and copy it as a new object. */
211 component.setData("import Lomiri.OnlineAccounts 2.0\n"
212 "AccountModel {\n"
213 " applicationId: \"foo\"\n"
214 " function getServices() {\n"
215 " var ret = [];\n"
216 " for (var i = 0; i < serviceList.length; i++) {\n"
217 " var s = serviceList[i];\n"
218 " var service = {};\n"
219 " for (var key in s) { service[key] = s[key]; }\n"
220 " ret.push(service);\n"
221 " }\n"
222 " return ret;\n"
223 " }\n"
224 "}",
225 QUrl());
226 QObject *object = component.create();
227 QVERIFY(object != 0);
228 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
229 QVERIFY(model != 0);
230
231 QSignalSpy ready(object, SIGNAL(isReadyChanged()));
232 ready.wait();
233
234 QVariant serviceList;
235 bool ok = QMetaObject::invokeMethod(object, "getServices",
236 Q_RETURN_ARG(QVariant, serviceList));
237 QVERIFY(ok);
238
239 QVariantList serviceVariantList = serviceList.toList();
240 QList<QVariantMap> services;
241 for (const QVariant &v: serviceVariantList) {
242 services.append(v.toMap());
243 }
244
245 QCOMPARE(services, expectedServices);
246 delete object;
247 }
248
249 void ModuleTest::testModelRoles_data()
250 {
251 QTest::addColumn<QString>("accountData");
252 QTest::addColumn<QString>("serviceData");
253 QTest::addColumn<QString>("displayName");
254 QTest::addColumn<QString>("serviceId");
255 QTest::addColumn<int>("authenticationMethod");
256 QTest::addColumn<QVariantMap>("settings");
257
258 QVariantMap settings;
259 QTest::newRow("empty") <<
260 "{}" <<
261 "" <<
262 "" << "" << 0 << settings;
263
264 settings.insert("Server", "www.example.com");
265 settings.insert("Port", "9900");
266 QTest::newRow("complete") <<
267 "{"
268 " '" ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME "': 'Tom',"
269 " '" ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID "': 'cool',"
270 " '" ONLINE_ACCOUNTS_INFO_KEY_AUTH_METHOD "': 2,"
271 " '" ONLINE_ACCOUNTS_INFO_KEY_SETTINGS "Server': 'www.example.com',"
272 " '" ONLINE_ACCOUNTS_INFO_KEY_SETTINGS "Port': 9900,"
273 "}" <<
274 "{"
275 " '" ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID "': 'cool',"
276 "}" <<
277 "Tom" << "cool" << 2 << settings;
278 }
279
280 void ModuleTest::testModelRoles()
281 {
282 QFETCH(QString, accountData);
283 QFETCH(QString, serviceData);
284 QFETCH(QString, displayName);
285 QFETCH(QString, serviceId);
286 QFETCH(int, authenticationMethod);
287 QFETCH(QVariantMap, settings);
288
289 addGetAccountsMethod(QString("ret = ([(1, %1)], [%2])").
290 arg(accountData).arg(serviceData));
291
292 QQmlEngine engine;
293 QQmlComponent component(&engine);
294 component.setData("import Lomiri.OnlineAccounts 2.0\n"
295 "AccountModel { applicationId: \"foo\" }",
296 QUrl());
297 QObject *object = component.create();
298 QVERIFY(object != 0);
299 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
300 QVERIFY(model != 0);
301
302 QCOMPARE(object->property("ready").toBool(), false);
303 QSignalSpy ready(object, SIGNAL(isReadyChanged()));
304
305 ready.wait();
306 QCOMPARE(object->property("ready").toBool(), true);
307
308 QCOMPARE(model->rowCount(), 1);
309 QCOMPARE(model->data(model->index(0), Qt::DisplayRole).toString(),
310 QString("%1 - %2").arg(displayName).arg(serviceId));
311 QCOMPARE(get(model, 0, "displayName").toString(), displayName);
312 QCOMPARE(get(model, 0, "valid").toBool(), true);
313 QCOMPARE(get(model, 0, "accountId").toInt(), 1);
314 QCOMPARE(get(model, 0, "serviceId").toString(), serviceId);
315 QCOMPARE(get(model, 0, "authenticationMethod").toInt(),
316 authenticationMethod);
317 QCOMPARE(get(model, 0, "settings").toMap(), settings);
318 QObject *account = get(model, 0, "account").value<QObject*>();
319 QVERIFY(account != 0);
320 QCOMPARE(account->metaObject()->className(), "OnlineAccountsModule::Account");
321 QCOMPARE(account,
322 model->property("accountList").value<QList<QObject*> >().first());
323
324 QJSValue service = get(model, 0, "service").value<QJSValue>();
325 QVERIFY(service.isObject());
326 QCOMPARE(service.property("serviceId").toString(), serviceId);
327
328 delete object;
329 }
330
331 void ModuleTest::testModelFilters_data()
332 {
333 QTest::addColumn<QString>("reply");
334 QTest::addColumn<QString>("filters");
335 QTest::addColumn<QList<int> >("expectedIds");
336 QTest::addColumn<QStringList>("expectedDisplayNames");
337
338 QTest::newRow("no accounts") <<
339 "[]" <<
340 "" <<
341 QList<int>();
342
343 QTest::newRow("one account, no service filter") <<
344 "[(1, {'displayName': 'Tom', 'serviceId': 'Foo' })]" <<
345 "" <<
346 (QList<int>() << 1);
347
348 QTest::newRow("one account, with service filter") <<
349 "[(1, {'displayName': 'Tom', 'serviceId': 'Foo' })]" <<
350 "serviceId: \"bar\"" <<
351 QList<int>();
352 }
353
354 void ModuleTest::testModelFilters()
355 {
356 QFETCH(QString, reply);
357 QFETCH(QString, filters);
358 QFETCH(QList<int>, expectedIds);
359
360 addGetAccountsMethod(QString("ret = (%1, [])").arg(reply));
361
362 QQmlEngine engine;
363 QQmlComponent component(&engine);
364 component.setData("import Lomiri.OnlineAccounts 2.0\n"
365 "AccountModel {\n"
366 " applicationId: \"foo\";" + filters.toUtf8() + "\n"
367 "}",
368 QUrl());
369 QObject *object = component.create();
370 QVERIFY(object != 0);
371 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
372 QVERIFY(model != 0);
373
374 QTRY_COMPARE(model->property("count").toInt(), expectedIds.count());
375
376 QList<int> ids;
377 QStringList displayNames;
378 for (int i = 0; i < expectedIds.count(); i++) {
379 ids.append(get(model, i, "accountId").toInt());
380 }
381
382 QCOMPARE(ids, expectedIds);
383
384 delete object;
385 }
386
387 void ModuleTest::testModelChanges()
388 {
389 addGetAccountsMethod("ret = ([], [])");
390 QQmlEngine engine;
391 QQmlComponent component(&engine);
392 component.setData("import Lomiri.OnlineAccounts 2.0\n"
393 "AccountModel { applicationId: \"foo\" }",
394 QUrl());
395 QObject *object = component.create();
396 QVERIFY(object != 0);
397 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
398 QVERIFY(model != 0);
399
400 QTRY_COMPARE(object->property("ready").toBool(), true);
401
402 QSignalSpy countChanged(model, SIGNAL(countChanged()));
403 QSignalSpy dataChanged(model, SIGNAL(dataChanged(const QModelIndex&,
404 const QModelIndex&)));
405
406 QVariantMap changes;
407 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_CHANGE_TYPE,
408 uint(ONLINE_ACCOUNTS_INFO_CHANGE_ENABLED));
409 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME, "John");
410 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID, "coolService");
411 emitAccountChanged("coolService", 5, changes);
412
413 QVERIFY(countChanged.wait());
414 QCOMPARE(countChanged.count(), 1);
415 QCOMPARE(dataChanged.count(), 0);
416 QTRY_COMPARE(model->rowCount(), 1);
417 QCOMPARE(model->property("accountList").value<QObjectList>().count(), 1);
418
419 QCOMPARE(get(model, 0, "displayName").toString(), QString("John"));
420 QObject *account =
421 model->property("accountList").value<QObjectList>().first();
422 QSignalSpy accountChanged(account, SIGNAL(accountChanged()));
423 QSignalSpy validChanged(account, SIGNAL(validChanged()));
424
425 QVERIFY(account);
426 QVERIFY(account->property("valid").toBool());
427 QCOMPARE(account->property("displayName").toString(), QString("John"));
428
429 countChanged.clear();
430
431 // Change the display name
432 changes.clear();
433 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_CHANGE_TYPE,
434 uint(ONLINE_ACCOUNTS_INFO_CHANGE_UPDATED));
435 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME, "Bob");
436 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID, "coolService");
437 emitAccountChanged("coolService", 5, changes);
438
439 QVERIFY(dataChanged.wait());
440 QCOMPARE(dataChanged.count(), 1);
441 QCOMPARE(countChanged.count(), 0);
442 QCOMPARE(accountChanged.count(), 1);
443 QCOMPARE(validChanged.count(), 0);
444 QCOMPARE(get(model, 0, "displayName").toString(), QString("Bob"));
445
446 dataChanged.clear();
447 accountChanged.clear();
448
449 // disable the account
450 changes.clear();
451 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_CHANGE_TYPE,
452 uint(ONLINE_ACCOUNTS_INFO_CHANGE_DISABLED));
453 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME, "Bob");
454 changes.insert(ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID, "coolService");
455 emitAccountChanged("coolService", 5, changes);
456
457 QVERIFY(countChanged.wait());
458 QCOMPARE(countChanged.count(), 1);
459 QCOMPARE(accountChanged.count(), 0);
460 QCOMPARE(validChanged.count(), 1);
461 QCOMPARE(model->rowCount(), 0);
462 QCOMPARE(model->property("accountList").value<QObjectList>().count(), 0);
463 QCOMPARE(account->property("valid").toBool(), false);
464
465 delete object;
466 }
467
468 void ModuleTest::testModelRequestAccess_data()
469 {
470 QTest::addColumn<QString>("reply");
471 QTest::addColumn<QVariantMap>("expectedAccessReply");
472 QTest::addColumn<QVariantMap>("expectedAuthenticationData");
473
474 QVariantMap accessReply;
475 QVariantMap authenticationData;
476 accessReply["errorCode"] = 4;
477 accessReply["errorText"] = "('org.foo.bar', 'not foobarized')";
478 QTest::newRow("dbus error") <<
479 "raise dbus.exceptions.DBusException('org.foo.bar', 'not foobarized')" <<
480 accessReply <<
481 authenticationData;
482
483 accessReply.clear();
484 accessReply["accountDisplayName"] = "Bob";
485 authenticationData.clear();
486 authenticationData.insert("AccessToken", "GoOn");
487 QTest::newRow("access granted") <<
488 "ret = ((1, {'displayName': 'Bob', 'serviceId': 'bar'}),{'AccessToken':'GoOn'})" <<
489 accessReply <<
490 authenticationData;
491 }
492
493 void ModuleTest::testModelRequestAccess()
494 {
495 QFETCH(QString, reply);
496 QFETCH(QVariantMap, expectedAccessReply);
497 QFETCH(QVariantMap, expectedAuthenticationData);
498
499 addGetAccountsMethod("ret = ([(1, {'displayName': 'Tom', 'serviceId': 'Foo' })], [])");
500 addMockedMethod("RequestAccess", "sa{sv}", "(ua{sv})a{sv}", reply);
501
502 QQmlEngine engine;
503 QQmlComponent component(&engine);
504 component.setData("import Lomiri.OnlineAccounts 2.0\n"
505 "AccountModel {\n"
506 " applicationId: \"foo\"\n"
507 "}",
508 QUrl());
509 QObject *object = component.create();
510 QVERIFY(object != 0);
511 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
512 QVERIFY(model != 0);
513
514 QSignalSpy accessReplySpy(model,
515 SIGNAL(accessReply(const QVariantMap&,const QVariantMap&)));
516
517
518 QString serviceId = "bar";
519 QVariantMap params;
520 bool ok = QMetaObject::invokeMethod(model, "requestAccess",
521 Q_ARG(QString, serviceId),
522 Q_ARG(QVariantMap, params));
523 QVERIFY(ok);
524
525 QVERIFY(accessReplySpy.wait());
526 QVariantMap accessReply = accessReplySpy.at(0).at(0).toMap();
527 QVariantMap authenticationData = accessReplySpy.at(0).at(1).toMap();
528
529 if (expectedAccessReply.contains("accountDisplayName")) {
530 QObject *account = accessReply["account"].value<QObject*>();
531 QVERIFY(account != 0);
532 QCOMPARE(account->metaObject()->className(),
533 "OnlineAccountsModule::Account");
534 QCOMPARE(account->property("displayName").toString(),
535 expectedAccessReply["accountDisplayName"].toString());
536 } else {
537 QCOMPARE(accessReply, expectedAccessReply);
538 }
539 QCOMPARE(authenticationData, expectedAuthenticationData);
540
541 delete object;
542 }
543
544 void ModuleTest::testAccountAuthentication_data()
545 {
546 QTest::addColumn<int>("authMethod");
547 QTest::addColumn<QString>("reply");
548 QTest::addColumn<QVariantMap>("params");
549 QTest::addColumn<QVariantMap>("expectedAuthenticationData");
550
551 QVariantMap params;
552 QVariantMap authenticationData;
553
554 authenticationData["errorCode"] = 4;
555 authenticationData["errorText"] = "not foobarized";
556 QTest::newRow("random dbus error") <<
557 ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1 <<
558 "raise dbus.exceptions.DBusException('not foobarized', name='org.foo.bar')" <<
559 params <<
560 authenticationData;
561
562 authenticationData["errorCode"] = 1;
563 authenticationData["errorText"] = "Not there";
564 QTest::newRow("no account") <<
565 ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1 <<
566 "raise dbus.exceptions.DBusException('Not there',"
567 "name='" ONLINE_ACCOUNTS_ERROR_NO_ACCOUNT "')" <<
568 params <<
569 authenticationData;
570
571 authenticationData["errorCode"] = 3;
572 authenticationData["errorText"] = "Sorry";
573 QTest::newRow("user canceled") <<
574 ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1 <<
575 "raise dbus.exceptions.DBusException('Sorry',"
576 "name='" ONLINE_ACCOUNTS_ERROR_USER_CANCELED "')" <<
577 params <<
578 authenticationData;
579
580 params["ConsumerKey"] = "aaaa";
581 params["ConsumerSecret"] = "bbb";
582 authenticationData = params;
583 authenticationData["Token"] = "a token";
584 authenticationData["TokenSecret"] = "a token secret";
585 authenticationData["SignatureMethod"] = "PLAIN";
586 QTest::newRow("oauth 1.0") <<
587 ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1 <<
588 "ret = {"
589 " 'ConsumerKey': args[4]['ConsumerKey'],"
590 " 'ConsumerSecret': args[4]['ConsumerSecret'],"
591 " 'Token': 'a token',"
592 " 'TokenSecret': 'a token secret',"
593 " 'SignatureMethod': 'PLAIN',"
594 "}" <<
595 params <<
596 authenticationData;
597
598 params.clear();
599 authenticationData.clear();
600 params["ClientId"] = "aaaa";
601 params["ClientSecret"] = "bbb";
602 params["Scopes"] = (QStringList() << "one" << "two");
603 authenticationData["AccessToken"] = "my token";
604 authenticationData["ExpiresIn"] = 3600;
605 authenticationData["GrantedScopes"] = (QStringList() << "one" << "two");
606 QTest::newRow("oauth 2.0") <<
607 ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH2 <<
608 "ret = {"
609 " 'AccessToken': 'my token',"
610 " 'ExpiresIn': 3600,"
611 " 'GrantedScopes': args[4]['Scopes'],"
612 "}" <<
613 params <<
614 authenticationData;
615
616 params.clear();
617 authenticationData.clear();
618 authenticationData["Username"] = "admin";
619 authenticationData["Password"] = "rootme";
620 QTest::newRow("password") <<
621 ONLINE_ACCOUNTS_AUTH_METHOD_PASSWORD <<
622 "ret = {"
623 " 'Username': 'admin',"
624 " 'Password': 'rootme',"
625 "}" <<
626 params <<
627 authenticationData;
628 }
629
630 void ModuleTest::testAccountAuthentication()
631 {
632 QFETCH(int, authMethod);
633 QFETCH(QString, reply);
634 QFETCH(QVariantMap, params);
635 QFETCH(QVariantMap, expectedAuthenticationData);
636
637 addGetAccountsMethod(QString("ret = ([(1, {"
638 " 'displayName': 'Bob',"
639 " 'serviceId': 'MyService0',"
640 " 'authMethod': %1,"
641 "})], [])").arg(authMethod));
642 addMockedMethod("Authenticate", "usbba{sv}", "a{sv}", reply);
643
644 QQmlEngine engine;
645 QQmlComponent component(&engine);
646 component.setData("import Lomiri.OnlineAccounts 2.0\n"
647 "AccountModel {\n"
648 " applicationId: \"foo\"\n"
649 "}",
650 QUrl());
651 QObject *object = component.create();
652 QVERIFY(object != 0);
653 QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
654 QVERIFY(model != 0);
655
656 QTRY_COMPARE(model->rowCount(), 1);
657
658 QObject *account =
659 model->property("accountList").value<QObjectList>().first();
660 QSignalSpy authenticationReply(account,
661 SIGNAL(authenticationReply(const QVariantMap&)));
662
663 bool ok = QMetaObject::invokeMethod(account, "authenticate",
664 Q_ARG(QVariantMap, params));
665 QVERIFY(ok);
666
667 QVERIFY(authenticationReply.wait());
668 QVariantMap authenticationData = authenticationReply.at(0).at(0).toMap();
669
670 QCOMPARE(authenticationData, expectedAuthenticationData);
671
672 delete object;
673 }
674
675 void ModuleTest::testInitialization_data()
676 {
677 QTest::addColumn<QString>("appId");
678 QTest::addColumn<bool>("errorExpected");
679
680 QTest::newRow("empty APP_ID") <<
681 "" <<
682 true;
683
684 QTest::newRow("invalid APP_ID") <<
685 "" <<
686 true;
687
688 QTest::newRow("valid APP_ID") <<
689 "my.package_app_0.2" <<
690 false;
691 }
692
693 void ModuleTest::testInitialization()
694 {
695 QFETCH(QString, appId);
696 QFETCH(bool, errorExpected);
697
698 qputenv("APP_ID", appId.toUtf8());
699
700 if (errorExpected) {
701 QTest::ignoreMessage(QtWarningMsg,
702 QRegularExpression("Lomiri.OnlineAccounts:.*"));
703 }
704
705 QQmlEngine engine;
706 QQmlComponent component(&engine);
707 component.setData("import Lomiri.OnlineAccounts 2.0\n"
708 "AccountModel {}",
709 QUrl());
710 QObject *object = component.create();
711 QVERIFY(object != 0);
712
713 if (!errorExpected) {
714 /* We just want to check that invoking this method won't cause a crash */
715 QString serviceId = "bar";
716 QVariantMap params;
717 bool ok = QMetaObject::invokeMethod(object, "requestAccess",
718 Q_ARG(QString, serviceId),
719 Q_ARG(QVariantMap, params));
720 QVERIFY(ok);
721 }
722 delete object;
723 }
724
725 QTEST_MAIN(ModuleTest)
726 #include "tst_qml_module.moc"