@@ -22,9 +22,12 @@
22#include "launchermodelas.h"
22#include "launchermodelas.h"
23#include "AccountsServiceDBusAdaptor.h"
23#include "AccountsServiceDBusAdaptor.h"
25+#include <QDBusInterface>
26+#include <QDBusMetaType>
25#include <QtTest>
29#include <QtTest>
27−QHash<QString, QList<QVariantMap>> mockProperties;
29class LauncherModelASTest : public QObject
32class LauncherModelASTest : public QObject
@@ -32,9 +35,15 @@ class LauncherModelASTest : public QObject
34 LauncherModel *launcherModel;
37 LauncherModel *launcherModel;
39+ QHash<QString, QList<QVariantMap>> mockProperties;
36private Q_SLOTS:
41private Q_SLOTS:
43+ void initTestCase() {
44+ qDBusRegisterMetaType<QList<QVariantMap>>();
38 void init() {
47 void init() {
39 // Prepare some fake list
48 // Prepare some fake list
40 QList<QVariantMap> list;
49 QList<QVariantMap> list;
@@ -50,7 +59,63 @@ private Q_SLOTS:
50 item["id"] = "appId2";
59 item["id"] = "appId2";
51 item["name"] = "Item 2";
60 item["name"] = "Item 2";
52 list.append(item);
61 list.append(item);
53− mockProperties["user1"] = list;
62+ setASLauncherItems("user1", list);
66+ Q_FOREACH(QString user, users) {
67+ QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
68+ QStringLiteral("/org/freedesktop/Accounts"),
69+ QStringLiteral("org.freedesktop.Accounts"));
70+ QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
72+ QVERIFY(removeReply.isValid());
73+ QCOMPARE(removeReply.value(), true);
77+ mockProperties.clear();
80+ void addASUser(const QString &user)
82+ QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
83+ QStringLiteral("/org/freedesktop/Accounts"),
84+ QStringLiteral("org.freedesktop.Accounts"));
85+ QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
87+ QVERIFY(addReply.isValid());
88+ QCOMPARE(addReply.value(), true);
91+ mockProperties[user] = QList<QVariantMap>();
94+ void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
96+ if (!users.contains(user)) {
100+ QDBusInterface userInterface(QStringLiteral("org.freedesktop.Accounts"),
101+ QString("/%1").arg(user),
102+ QStringLiteral("org.freedesktop.DBus.Properties"));
104+ QVariant wrapped = QVariant::fromValue(QDBusVariant(QVariant::fromValue(items)));
105+ QDBusReply<void> setReply = userInterface.call(QStringLiteral("Set"),
106+ QStringLiteral("com.canonical.unity.AccountsService"),
107+ QStringLiteral("LauncherItems"),
109+ QVERIFY(setReply.isValid());
111+ // Record what we set for comparison's sake
112+ mockProperties[user] = items;
115+ void setUser(LauncherModel *model, const QString &user) {
116+ QSignalSpy spy(model, &QAbstractItemModel::rowsInserted);
117+ model->setUser(user);
118+ QTRY_COMPARE(spy.count(), mockProperties[user].count());
56 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
121 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
@@ -81,7 +146,7 @@ private Q_SLOTS:
81 // We didn't set a user yet. model should be empty
146 // We didn't set a user yet. model should be empty
82 QCOMPARE(model->rowCount(), 0);
147 QCOMPARE(model->rowCount(), 0);
84− model->setUser("user1");
149+ setUser(model, "user1");
86 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
151 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
87 model->deleteLater();
152 model->deleteLater();
@@ -102,7 +167,7 @@ private Q_SLOTS:
102 QFETCH(bool, inSync);
167 QFETCH(bool, inSync);
104 LauncherModel* model = new LauncherModel(this);
169 LauncherModel* model = new LauncherModel(this);
105− model->setUser(modelUser);
170+ setUser(model, modelUser);
107 int oldCount = mockProperties[modelUser].count();
172 int oldCount = mockProperties[modelUser].count();
108 QCOMPARE(model->rowCount(), oldCount);
173 QCOMPARE(model->rowCount(), oldCount);
@@ -117,17 +182,17 @@ private Q_SLOTS:
117 newEntry.insert("countVisible", false);
182 newEntry.insert("countVisible", false);
118 newEntry.insert("pinned", true);
183 newEntry.insert("pinned", true);
119 newList.append(newEntry);
184 newList.append(newEntry);
120− model->m_accounts->simulatePropertyChange(changedUser, "LauncherItems", QVariant::fromValue(newList));
185+ setASLauncherItems(changedUser, newList);
122− QCOMPARE(isInSync(model, mockProperties[modelUser]), true);
187+ QTRY_COMPARE(isInSync(model, mockProperties[modelUser]), true);
123− QCOMPARE(isInSync(model, mockProperties[changedUser]), inSync);
188+ QTRY_COMPARE(isInSync(model, mockProperties[changedUser]), inSync);
125 model->deleteLater();
190 model->deleteLater();
128 void testUpdateCount() {
193 void testUpdateCount() {
129 LauncherModel* model = new LauncherModel(this);
194 LauncherModel* model = new LauncherModel(this);
130− model->setUser("user1");
195+ setUser(model, "user1");
132 QCOMPARE(model->get(0)->countVisible(), false);
197 QCOMPARE(model->get(0)->countVisible(), false);
133 QCOMPARE(model->get(0)->count(), 0);
198 QCOMPARE(model->get(0)->count(), 0);
@@ -137,34 +202,34 @@ private Q_SLOTS:
137 entry["countVisible"] = true;
202 entry["countVisible"] = true;
138 entry["count"] = 55;
203 entry["count"] = 55;
139 newList[0] = entry;
204 newList[0] = entry;
140− model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
205+ setASLauncherItems("user1", newList);
142− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
207+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
144 model->deleteLater();
209 model->deleteLater();
147 void testOnlyPinned() {
212 void testOnlyPinned() {
148 LauncherModel *model = new LauncherModel(this);
213 LauncherModel *model = new LauncherModel(this);
149− model->setUser("user1");
214+ setUser(model, "user1");
150 model->setOnlyPinned(true);
215 model->setOnlyPinned(true);
152− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
217+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
154 // Let's unpin one item
219 // Let's unpin one item
155 QList<QVariantMap> newList = mockProperties["user1"];
220 QList<QVariantMap> newList = mockProperties["user1"];
156 QVariantMap entry = newList.at(0);
221 QVariantMap entry = newList.at(0);
157 entry["pinned"] = false;
222 entry["pinned"] = false;
158 newList[0] = entry;
223 newList[0] = entry;
159− model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
224+ setASLauncherItems("user1", newList);
160− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
225+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
162 // Now toggle onlyPinned and make sure the model keeps up
227 // Now toggle onlyPinned and make sure the model keeps up
163 model->setOnlyPinned(false);
228 model->setOnlyPinned(false);
164− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
229+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
166 model->setOnlyPinned(true);
231 model->setOnlyPinned(true);
167− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
232+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);