@@ -22,9 +22,12 @@
2222#include "launchermodelas.h"
2323#include "AccountsServiceDBusAdaptor.h"
2424
25+#include <QDBusInterface>
26+#include <QDBusMetaType>
27+#include <QDBusReply>
28+#include <QSignalSpy>
2529#include <QtTest>
2630
27−QHash<QString, QList<QVariantMap>> mockProperties;
2831
2932class LauncherModelASTest : public QObject
3033{
@@ -32,9 +35,15 @@ class LauncherModelASTest : public QObject
3235
3336private:
3437 LauncherModel *launcherModel;
38+ QStringList users;
39+ QHash<QString, QList<QVariantMap>> mockProperties;
3540
3641private Q_SLOTS:
3742
43+ void initTestCase() {
44+ qDBusRegisterMetaType<QList<QVariantMap>>();
45+ }
46+
3847 void init() {
3948 // Prepare some fake list
4049 QList<QVariantMap> list;
@@ -50,7 +59,63 @@ private Q_SLOTS:
5059 item["id"] = "appId2";
5160 item["name"] = "Item 2";
5261 list.append(item);
53− mockProperties["user1"] = list;
62+ setASLauncherItems("user1", list);
63+ }
64+
65+ void cleanup() {
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"),
71+ user);
72+ QVERIFY(removeReply.isValid());
73+ QCOMPARE(removeReply.value(), true);
74+ }
75+
76+ users.clear();
77+ mockProperties.clear();
78+ }
79+
80+ void addASUser(const QString &user)
81+ {
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"),
86+ user);
87+ QVERIFY(addReply.isValid());
88+ QCOMPARE(addReply.value(), true);
89+
90+ users << user;
91+ mockProperties[user] = QList<QVariantMap>();
92+ }
93+
94+ void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
95+ {
96+ if (!users.contains(user)) {
97+ addASUser(user);
98+ }
99+
100+ QDBusInterface userInterface(QStringLiteral("org.freedesktop.Accounts"),
101+ QString("/%1").arg(user),
102+ QStringLiteral("org.freedesktop.DBus.Properties"));
103+
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"),
108+ wrapped);
109+ QVERIFY(setReply.isValid());
110+
111+ // Record what we set for comparison's sake
112+ mockProperties[user] = items;
113+ }
114+
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());
54119 }
55120
56121 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
@@ -81,7 +146,7 @@ private Q_SLOTS:
81146 // We didn't set a user yet. model should be empty
82147 QCOMPARE(model->rowCount(), 0);
83148
84− model->setUser("user1");
149+ setUser(model, "user1");
85150
86151 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
87152 model->deleteLater();
@@ -102,7 +167,7 @@ private Q_SLOTS:
102167 QFETCH(bool, inSync);
103168
104169 LauncherModel* model = new LauncherModel(this);
105− model->setUser(modelUser);
170+ setUser(model, modelUser);
106171
107172 int oldCount = mockProperties[modelUser].count();
108173 QCOMPARE(model->rowCount(), oldCount);
@@ -117,17 +182,17 @@ private Q_SLOTS:
117182 newEntry.insert("countVisible", false);
118183 newEntry.insert("pinned", true);
119184 newList.append(newEntry);
120− model->m_accounts->simulatePropertyChange(changedUser, "LauncherItems", QVariant::fromValue(newList));
185+ setASLauncherItems(changedUser, newList);
121186
122− QCOMPARE(isInSync(model, mockProperties[modelUser]), true);
123− QCOMPARE(isInSync(model, mockProperties[changedUser]), inSync);
187+ QTRY_COMPARE(isInSync(model, mockProperties[modelUser]), true);
188+ QTRY_COMPARE(isInSync(model, mockProperties[changedUser]), inSync);
124189
125190 model->deleteLater();
126191 }
127192
128193 void testUpdateCount() {
129194 LauncherModel* model = new LauncherModel(this);
130− model->setUser("user1");
195+ setUser(model, "user1");
131196
132197 QCOMPARE(model->get(0)->countVisible(), false);
133198 QCOMPARE(model->get(0)->count(), 0);
@@ -137,34 +202,34 @@ private Q_SLOTS:
137202 entry["countVisible"] = true;
138203 entry["count"] = 55;
139204 newList[0] = entry;
140− model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
205+ setASLauncherItems("user1", newList);
141206
142− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
207+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
143208
144209 model->deleteLater();
145210 }
146211
147212 void testOnlyPinned() {
148213 LauncherModel *model = new LauncherModel(this);
149− model->setUser("user1");
214+ setUser(model, "user1");
150215 model->setOnlyPinned(true);
151216
152− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
217+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
153218
154219 // Let's unpin one item
155220 QList<QVariantMap> newList = mockProperties["user1"];
156221 QVariantMap entry = newList.at(0);
157222 entry["pinned"] = false;
158223 newList[0] = entry;
159− model->m_accounts->simulatePropertyChange("user1", "LauncherItems", QVariant::fromValue(newList));
160− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
224+ setASLauncherItems("user1", newList);
225+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
161226
162227 // Now toggle onlyPinned and make sure the model keeps up
163228 model->setOnlyPinned(false);
164− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
229+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
165230
166231 model->setOnlyPinned(true);
167− QCOMPARE(isInSync(model, mockProperties["user1"]), true);
232+ QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
168233 }
169234};
170235