on 16fa310 help sign in
unity8/tests/plugins/Greeter/Unity/Launcher/launchermodelastest.cpp
237 lines 8.5 kB history raw
1/*
2 * Copyright 2014-2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17// unity-api
18#include <unity/shell/launcher/LauncherModelInterface.h>
19#include <unity/shell/application/ApplicationInfoInterface.h>
21#include "launcheritem.h"
22#include "launchermodelas.h"
23#include "AccountsServiceDBusAdaptor.h"
25#include <QDBusInterface>
26#include <QDBusMetaType>
27#include <QDBusReply>
28#include <QSignalSpy>
29#include <QtTest>
32class LauncherModelASTest : public QObject
34 Q_OBJECT
36private:
37 LauncherModel *launcherModel;
38 QStringList users;
39 QHash<QString, QList<QVariantMap>> mockProperties;
41private Q_SLOTS:
43 void initTestCase() {
44 qDBusRegisterMetaType<QList<QVariantMap>>();
45 }
47 void init() {
48 // Prepare some fake list
49 QList<QVariantMap> list;
50 QVariantMap item;
51 item.insert("id", "appId1");
52 item.insert("name", "Item 1");
53 item.insert("icon", "fake.svg");
54 item.insert("count", 0);
55 item.insert("progress", 0);
56 item.insert("countVisible", false);
57 item.insert("pinned", true);
58 list.append(item);
59 item["id"] = "appId2";
60 item["name"] = "Item 2";
61 list.append(item);
62 setASLauncherItems("user1", list);
63 }
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 }
76 users.clear();
77 mockProperties.clear();
78 }
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);
90 users << user;
91 mockProperties[user] = QList<QVariantMap>();
92 }
94 void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
95 {
96 if (!users.contains(user)) {
97 addASUser(user);
98 }
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"),
108 wrapped);
109 QVERIFY(setReply.isValid());
111 // Record what we set for comparison's sake
112 mockProperties[user] = items;
113 }
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());
119 }
121 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
122 QList<QVariantMap> list;
123 // Strip unpinned if onlyPinned is set
124 Q_FOREACH (const QVariantMap &map, properties) {
125 if (!model->onlyPinned() || map.value("pinned").toBool()) {
126 list << map;
127 }
128 }
130 bool inSync = model->rowCount() == list.count();
131 for (int i = 0; inSync && i < model->rowCount(); i++) {
132 inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
133 inSync &= model->get(i)->name() == list.at(i).value("name").toString();
134 inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
135 inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
136 inSync &= model->get(i)->progress() == list.at(i).value("progress").toInt();
137 inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
138 }
139 return inSync;
140 }
142 void testLoadASOnStartup() {
143 // Load up the model
144 LauncherModel* model = new LauncherModel(this);
146 // We didn't set a user yet. model should be empty
147 QCOMPARE(model->rowCount(), 0);
149 setUser(model, "user1");
151 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
152 model->deleteLater();
153 }
155 void testASChangedUpdatesModel_data() {
156 QTest::addColumn<QString>("modelUser");
157 QTest::addColumn<QString>("changedUser");
158 QTest::addColumn<bool>("inSync");
160 QTest::newRow("this user changed") << "user1" << "user1" << true;
161 QTest::newRow("other user changed") << "user1" << "user2" << false;
162 }
164 void testASChangedUpdatesModel() {
165 QFETCH(QString, modelUser);
166 QFETCH(QString, changedUser);
167 QFETCH(bool, inSync);
169 LauncherModel* model = new LauncherModel(this);
170 setUser(model, modelUser);
172 int oldCount = mockProperties[modelUser].count();
173 QCOMPARE(model->rowCount(), oldCount);
175 QList<QVariantMap> newList;
176 QVariantMap newEntry;
177 newEntry.insert("id", "newappId");
178 newEntry.insert("name", "New app");
179 newEntry.insert("icon", "some-icon.svg");
180 newEntry.insert("count", 0);
181 newEntry.insert("progress", 42);
182 newEntry.insert("countVisible", false);
183 newEntry.insert("pinned", true);
184 newList.append(newEntry);
185 setASLauncherItems(changedUser, newList);
187 QTRY_COMPARE(isInSync(model, mockProperties[modelUser]), true);
188 QTRY_COMPARE(isInSync(model, mockProperties[changedUser]), inSync);
190 model->deleteLater();
191 }
193 void testUpdateCount() {
194 LauncherModel* model = new LauncherModel(this);
195 setUser(model, "user1");
197 QCOMPARE(model->get(0)->countVisible(), false);
198 QCOMPARE(model->get(0)->count(), 0);
200 QList<QVariantMap> newList = mockProperties["user1"];
201 QVariantMap entry = newList.at(0);
202 entry["countVisible"] = true;
203 entry["count"] = 55;
204 newList[0] = entry;
205 setASLauncherItems("user1", newList);
207 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
209 model->deleteLater();
210 }
212 void testOnlyPinned() {
213 LauncherModel *model = new LauncherModel(this);
214 setUser(model, "user1");
215 model->setOnlyPinned(true);
217 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
219 // Let's unpin one item
220 QList<QVariantMap> newList = mockProperties["user1"];
221 QVariantMap entry = newList.at(0);
222 entry["pinned"] = false;
223 newList[0] = entry;
224 setASLauncherItems("user1", newList);
225 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
227 // Now toggle onlyPinned and make sure the model keeps up
228 model->setOnlyPinned(false);
229 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
231 model->setOnlyPinned(true);
232 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
233 }
234};
236QTEST_GUILESS_MAIN(LauncherModelASTest)
237#include "launchermodelastest.moc"