2 * Copyright 2014-2015 Canonical Ltd.
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.
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.
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/>.
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>
33class LauncherModelASTest : public QObject
38 LauncherModel *launcherModel;
40 QHash<QString, QList<QVariantMap>> mockProperties;
45 qDBusRegisterMetaType<QList<QVariantMap>>();
49 // Launcher will look in current dir in testing mode for data
50 QDir::setCurrent(testDataDir() + "/" TEST_DIR);
52 // Prepare some fake list
53 QList<QVariantMap> list;
55 item.insert("id", "appId1");
56 item.insert("name", "Item 1");
57 item.insert("icon", "fake.svg");
58 item.insert("count", 0);
59 item.insert("progress", 0);
60 item.insert("countVisible", false);
61 item.insert("pinned", true);
63 item["id"] = "appId2";
64 item["name"] = "Item 2";
66 setASLauncherItems("user1", list);
70 Q_FOREACH(QString user, users) {
71 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
72 QStringLiteral("/org/freedesktop/Accounts"),
73 QStringLiteral("org.freedesktop.Accounts"));
74 QDBusReply<bool> removeReply = accountsInterface.call(QStringLiteral("RemoveUser"),
76 QVERIFY(removeReply.isValid());
77 QCOMPARE(removeReply.value(), true);
81 mockProperties.clear();
84 void addASUser(const QString &user)
86 QDBusInterface accountsInterface(QStringLiteral("org.freedesktop.Accounts"),
87 QStringLiteral("/org/freedesktop/Accounts"),
88 QStringLiteral("org.freedesktop.Accounts"));
89 QDBusReply<bool> addReply = accountsInterface.call(QStringLiteral("AddUser"),
91 QVERIFY(addReply.isValid());
92 QCOMPARE(addReply.value(), true);
95 mockProperties[user] = QList<QVariantMap>();
98 void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
100 if (!users.contains(user)) {
104 QDBusInterface userInterface(QStringLiteral("org.freedesktop.Accounts"),
105 QString("/%1").arg(user),
106 QStringLiteral("org.freedesktop.DBus.Properties"));
108 QVariant wrapped = QVariant::fromValue(QDBusVariant(QVariant::fromValue(items)));
109 QDBusReply<void> setReply = userInterface.call(QStringLiteral("Set"),
110 QStringLiteral("com.canonical.unity.AccountsService"),
111 QStringLiteral("LauncherItems"),
113 QVERIFY(setReply.isValid());
115 // Record what we set for comparison's sake
116 mockProperties[user] = items;
119 void setUser(LauncherModel *model, const QString &user) {
120 QSignalSpy spy(model, &QAbstractItemModel::rowsInserted);
121 model->setUser(user);
122 QTRY_COMPARE(spy.count(), mockProperties[user].count());
125 bool isInSync(LauncherModel *model, const QList<QVariantMap> &properties) {
126 QList<QVariantMap> list;
127 // Strip unpinned if onlyPinned is set
128 Q_FOREACH (const QVariantMap &map, properties) {
129 if (!model->onlyPinned() || map.value("pinned").toBool()) {
134 bool inSync = model->rowCount() == list.count();
135 for (int i = 0; inSync && i < model->rowCount(); i++) {
136 inSync &= model->get(i)->appId() == list.at(i).value("id").toString();
137 inSync &= model->get(i)->name() == list.at(i).value("name").toString();
138 inSync &= model->get(i)->icon() == list.at(i).value("icon").toString();
139 inSync &= model->get(i)->count() == list.at(i).value("count").toInt();
140 inSync &= model->get(i)->progress() == list.at(i).value("progress").toInt();
141 inSync &= model->get(i)->countVisible() == list.at(i).value("countVisible").toBool();
146 void testLoadASOnStartup() {
148 LauncherModel* model = new LauncherModel(this);
150 // We didn't set a user yet. model should be empty
151 QCOMPARE(model->rowCount(), 0);
153 setUser(model, "user1");
155 QCOMPARE(isInSync(model, mockProperties["user1"]), true);
156 model->deleteLater();
159 void testASChangedUpdatesModel_data() {
160 QTest::addColumn<QString>("modelUser");
161 QTest::addColumn<QString>("changedUser");
162 QTest::addColumn<bool>("inSync");
164 QTest::newRow("this user changed") << "user1" << "user1" << true;
165 QTest::newRow("other user changed") << "user1" << "user2" << false;
168 void testASChangedUpdatesModel() {
169 QFETCH(QString, modelUser);
170 QFETCH(QString, changedUser);
171 QFETCH(bool, inSync);
173 LauncherModel* model = new LauncherModel(this);
174 setUser(model, modelUser);
176 int oldCount = mockProperties[modelUser].count();
177 QCOMPARE(model->rowCount(), oldCount);
179 QList<QVariantMap> newList;
180 QVariantMap newEntry;
181 newEntry.insert("id", "newappId");
182 newEntry.insert("name", "New app");
183 newEntry.insert("icon", "some-icon.svg");
184 newEntry.insert("count", 0);
185 newEntry.insert("progress", 42);
186 newEntry.insert("countVisible", false);
187 newEntry.insert("pinned", true);
188 newList.append(newEntry);
189 setASLauncherItems(changedUser, newList);
191 QTRY_COMPARE(isInSync(model, mockProperties[modelUser]), true);
192 QTRY_COMPARE(isInSync(model, mockProperties[changedUser]), inSync);
194 model->deleteLater();
197 void testUpdateCount() {
198 LauncherModel* model = new LauncherModel(this);
199 setUser(model, "user1");
201 QCOMPARE(model->get(0)->countVisible(), false);
202 QCOMPARE(model->get(0)->count(), 0);
204 QList<QVariantMap> newList = mockProperties["user1"];
205 QVariantMap entry = newList.at(0);
206 entry["countVisible"] = true;
209 setASLauncherItems("user1", newList);
211 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
213 model->deleteLater();
216 void testOnlyPinned() {
217 LauncherModel *model = new LauncherModel(this);
218 setUser(model, "user1");
219 model->setOnlyPinned(true);
221 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
223 // Let's unpin one item
224 QList<QVariantMap> newList = mockProperties["user1"];
225 QVariantMap entry = newList.at(0);
226 entry["pinned"] = false;
228 setASLauncherItems("user1", newList);
229 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
231 // Now toggle onlyPinned and make sure the model keeps up
232 model->setOnlyPinned(false);
233 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
235 model->setOnlyPinned(true);
236 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
240QTEST_GUILESS_MAIN(LauncherModelASTest)
241#include "launchermodelastest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list