on ceacebb help sign in
unity8/tests/plugins/Greeter/Unity/Launcher/launchermodelastest.cpp
240 lines 8.6 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>
31#include <paths.h>
33class LauncherModelASTest : public QObject
35 Q_OBJECT
37private:
38 QStringList users;
39 QHash<QString, QList<QVariantMap>> mockProperties;
41private Q_SLOTS:
43 void initTestCase() {
44 qDBusRegisterMetaType<QList<QVariantMap>>();
45 }
47 void init() {
48 // Launcher will look in current dir in testing mode for data
49 QDir::setCurrent(testDataDir() + "/" TEST_DIR);
51 // Prepare some fake list
52 QList<QVariantMap> list;
53 QVariantMap item;
54 item.insert("id", "appId1");
55 item.insert("name", "Item 1");
56 item.insert("icon", "fake.svg");
57 item.insert("count", 0);
58 item.insert("progress", 0);
59 item.insert("countVisible", false);
60 item.insert("pinned", true);
61 list.append(item);
62 item["id"] = "appId2";
63 item["name"] = "Item 2";
64 list.append(item);
65 setASLauncherItems("user1", list);
66 }
68 void cleanup() {
69 Q_FOREACH(QString user, users) {
70 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
71 QStringLiteral("/org/freedesktop/Accounts"),
72 QStringLiteral("org.freedesktop.Accounts"));
73 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
74 user);
75 QVERIFY(removeReply.isValid());
76 QCOMPARE(removeReply.value(), true);
77 }
79 users.clear();
80 mockProperties.clear();
81 }
83 void addASUser(const QString &user)
84 {
85 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
86 QStringLiteral("/org/freedesktop/Accounts"),
87 QStringLiteral("org.freedesktop.Accounts"));
88 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
89 user);
90 QVERIFY(addReply.isValid());
91 QCOMPARE(addReply.value(), true);
93 users << user;
94 mockProperties[user] = QList<QVariantMap>();
95 }
97 void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
98 {
99 if (!users.contains(user)) {
100 addASUser(user);
101 }
103 QDBusInterface userInterface(QStringLiteral("org.freedesktop.Accounts"),
104 QString("/%1").arg(user),
105 QStringLiteral("org.freedesktop.DBus.Properties"));
107 QVariant wrapped = QVariant::fromValue(QDBusVariant(QVariant::fromValue(items)));
108 QDBusReply<void> setReply = userInterface.call(QStringLiteral("Set"),
109 QStringLiteral("com.canonical.unity.AccountsService"),
110 QStringLiteral("LauncherItems"),
111 wrapped);
112 QVERIFY(setReply.isValid());
114 // Record what we set for comparison's sake
115 mockProperties[user] = items;
116 }
118 void setUser(LauncherModel *model, const QString &user) {
119 QSignalSpy spy(model, &QAbstractItemModel::rowsInserted);
120 model->setUser(user);
121 QTRY_COMPARE(spy.count(), mockProperties[user].count());
122 }
124 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
125 QList<QVariantMap> list;
126 // Strip unpinned if onlyPinned is set
127 Q_FOREACH (const QVariantMap &map, properties) {
128 if (!model->onlyPinned() || map.value("pinned").toBool()) {
129 list << map;
130 }
131 }
133 bool inSync = model->rowCount() == list.count();
134 for (int i = 0; inSync && i < model->rowCount(); i++) {
135 inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
136 inSync &= model->get(i)->name() == list.at(i).value("name").toString();
137 inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
138 inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
139 inSync &= model->get(i)->progress() == list.at(i).value("progress").toInt();
140 inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
141 }
142 return inSync;
143 }
145 void testLoadASOnStartup() {
146 // Load up the model
147 LauncherModel* model = new LauncherModel(this);
149 // We didn't set a user yet. model should be empty
150 QCOMPARE(model->rowCount(), 0);
152 setUser(model, "user1");
154 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
155 model->deleteLater();
156 }
158 void testASChangedUpdatesModel_data() {
159 QTest::addColumn<QString>("modelUser");
160 QTest::addColumn<QString>("changedUser");
161 QTest::addColumn<bool>("inSync");
163 QTest::newRow("this user changed") << "user1" << "user1" << true;
164 QTest::newRow("other user changed") << "user1" << "user2" << false;
165 }
167 void testASChangedUpdatesModel() {
168 QFETCH(QString, modelUser);
169 QFETCH(QString, changedUser);
170 QFETCH(bool, inSync);
172 LauncherModel* model = new LauncherModel(this);
173 setUser(model, modelUser);
175 int oldCount = mockProperties[modelUser].count();
176 QCOMPARE(model->rowCount(), oldCount);
178 QList<QVariantMap> newList;
179 QVariantMap newEntry;
180 newEntry.insert("id", "newappId");
181 newEntry.insert("name", "New app");
182 newEntry.insert("icon", "some-icon.svg");
183 newEntry.insert("count", 0);
184 newEntry.insert("progress", 42);
185 newEntry.insert("countVisible", false);
186 newEntry.insert("pinned", true);
187 newList.append(newEntry);
188 setASLauncherItems(changedUser, newList);
190 QTRY_COMPARE(isInSync(model, mockProperties[modelUser]), true);
191 QTRY_COMPARE(isInSync(model, mockProperties[changedUser]), inSync);
193 model->deleteLater();
194 }
196 void testUpdateCount() {
197 LauncherModel* model = new LauncherModel(this);
198 setUser(model, "user1");
200 QCOMPARE(model->get(0)->countVisible(), false);
201 QCOMPARE(model->get(0)->count(), 0);
203 QList<QVariantMap> newList = mockProperties["user1"];
204 QVariantMap entry = newList.at(0);
205 entry["countVisible"] = true;
206 entry["count"] = 55;
207 newList[0] = entry;
208 setASLauncherItems("user1", newList);
210 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
212 model->deleteLater();
213 }
215 void testOnlyPinned() {
216 LauncherModel *model = new LauncherModel(this);
217 setUser(model, "user1");
218 model->setOnlyPinned(true);
220 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
222 // Let's unpin one item
223 QList<QVariantMap> newList = mockProperties["user1"];
224 QVariantMap entry = newList.at(0);
225 entry["pinned"] = false;
226 newList[0] = entry;
227 setASLauncherItems("user1", newList);
228 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
230 // Now toggle onlyPinned and make sure the model keeps up
231 model->setOnlyPinned(false);
232 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
234 model->setOnlyPinned(true);
235 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
236 }
237};
239QTEST_GUILESS_MAIN(LauncherModelASTest)
240#include "launchermodelastest.moc"