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
39 QHash<QString, QList<QVariantMap>> mockProperties;
44 qDBusRegisterMetaType<QList<QVariantMap>>();
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;
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);
62 item["id"] = "appId2";
63 item["name"] = "Item 2";
65 setASLauncherItems("user1", list);
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"),
75 QVERIFY(removeReply.isValid());
76 QCOMPARE(removeReply.value(), true);
80 mockProperties.clear();
83 void addASUser(const QString &user)
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"),
90 QVERIFY(addReply.isValid());
91 QCOMPARE(addReply.value(), true);
94 mockProperties[user] = QList<QVariantMap>();
97 void setASLauncherItems(const QString &user, const QList<QVariantMap> &items)
99 if (!users.contains(user)) {
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"),
112 QVERIFY(setReply.isValid());
114 // Record what we set for comparison's sake
115 mockProperties[user] = items;
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());
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()) {
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();
145 void testLoadASOnStartup() {
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();
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;
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();
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;
208 setASLauncherItems("user1", newList);
210 QTRY_COMPARE(isInSync(model, mockProperties["user1"]), true);
212 model->deleteLater();
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;
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);
239QTEST_GUILESS_MAIN(LauncherModelASTest)
240#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