on 478de30 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
377 lines 15 kB history raw
1/*
2 * Copyright 2013 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 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20// unity-api
21#include <unity/shell/launcher/LauncherModelInterface.h>
22#include <unity/shell/application/ApplicationInfoInterface.h>
24#include "launcheritem.h"
25#include "launchermodel.h"
26#include "dbusinterface.h"
28#include <QtTest>
29#include <QDBusInterface>
30#include <QDBusReply>
31#include <QDomDocument>
33// This is a mock, specifically to test the LauncherModel
34class MockApp: public unity::shell::application::ApplicationInfoInterface
36 Q_OBJECT
37public:
38 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
39 QString appId() const { return m_appId; }
40 QString name() const { return "mock"; }
41 QString comment() const { return "this is a mock"; }
42 QUrl icon() const { return QUrl(); }
43 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
44 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
45 bool focused() const { return m_focused; }
46 QUrl screenshot() const { return QUrl(); }
48 // Methods used for mocking (not in the interface)
49 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
50private:
51 QString m_appId;
52 bool m_focused;
53};
55// This is a mock, specifically to test the LauncherModel
56class MockAppManager: public unity::shell::application::ApplicationManagerInterface
58 Q_OBJECT
59public:
60 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
61 int rowCount(const QModelIndex &) const { return m_list.count(); }
62 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
63 QString focusedApplicationId() const {
64 Q_FOREACH(MockApp *app, m_list) {
65 if (app->focused()) return app->appId();
66 }
67 return QString();
68 }
69 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
70 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
71 Q_FOREACH(MockApp* app, m_list) {
72 if (app->appId() == appId) {
73 return app;
74 }
75 }
76 return nullptr;
77 }
78 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
79 bool stopApplication(const QString &) { return false; }
80 bool focusApplication(const QString &appId) {
81 Q_FOREACH(MockApp* app, m_list) {
82 app->setFocused(app->appId() == appId);
83 }
84 Q_EMIT focusedApplicationIdChanged();
85 return true;
86 }
88 void unfocusCurrentApplication() { }
90 void addApplication(MockApp *app) {
91 beginInsertRows(QModelIndex(), count(), count());
92 m_list.append(app);
93 endInsertRows();
94 }
95 void removeApplication(int index) {
96 beginRemoveRows(QModelIndex(), index, index);
97 m_list.takeAt(index)->deleteLater();
98 endRemoveRows();
99 }
100 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
101 bool suspended() const { return false; }
102 void setSuspended(bool) {}
104private:
105 QList<MockApp*> m_list;
106};
108class LauncherModelTest : public QObject
110 Q_OBJECT
112private:
113 LauncherModel *launcherModel;
114 MockAppManager *appManager;
116private Q_SLOTS:
118 void initTestCase() {
119 launcherModel = new LauncherModel(this);
120 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
122 appManager = new MockAppManager(this);
123 launcherModel->setApplicationManager(appManager);
124 }
126 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
127 void init() {
128 appManager->addApplication(new MockApp("abs-icon"));
129 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
131 appManager->addApplication(new MockApp("no-icon"));
132 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
133 }
135 // Removing apps from appmanager and launcher as pinned ones would stick
136 void cleanup() {
137 while (appManager->count() > 0) {
138 appManager->removeApplication(0);
139 }
140 while (launcherModel->rowCount(QModelIndex()) > 0) {
141 launcherModel->requestRemove(launcherModel->get(0)->appId());
142 }
143 }
145 void testMove() {
146 QCOMPARE(launcherModel->get(0)->pinned(), false);
147 QCOMPARE(launcherModel->get(1)->pinned(), false);
149 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
150 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
151 launcherModel->move(1, 0);
153 QCOMPARE(item0BeforeMove, launcherModel->get(1));
154 QCOMPARE(item1BeforeMove, launcherModel->get(0));
156 // moved item must be pinned now
157 QCOMPARE(item0BeforeMove->pinned(), false);
158 QCOMPARE(item1BeforeMove->pinned(), true);
159 }
161 void testPinning() {
162 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
163 QCOMPARE(launcherModel->get(0)->pinned(), false);
164 QCOMPARE(launcherModel->get(1)->pinned(), false);
165 launcherModel->pin(launcherModel->get(0)->appId());
166 QCOMPARE(launcherModel->get(0)->pinned(), true);
167 QCOMPARE(launcherModel->get(1)->pinned(), false);
168 QCOMPARE(spy.count(), 1);
169 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
171 launcherModel->requestRemove(launcherModel->get(0)->appId());
172 QCOMPARE(launcherModel->get(0)->pinned(), false);
173 QCOMPARE(launcherModel->get(1)->pinned(), false);
174 QCOMPARE(spy.count(), 2);
175 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
176 }
178 void testRemove_data() {
180 QTest::addColumn<bool>("pinned");
181 QTest::addColumn<bool>("running");
183 QTest::newRow("non-pinned, running") << false << true;
184 QTest::newRow("pinned, running") << true << false;
185 QTest::newRow("pinned, non-running") << true << false;
186 }
188 void testRemove() {
189 QFETCH(bool, pinned);
190 QFETCH(bool, running);
192 // In the beginning we always have two items
193 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
195 // pin one if required
196 if (pinned) {
197 launcherModel->pin(launcherModel->get(1)->appId());
198 }
200 // stop it if required
201 if (!running) {
202 appManager->removeApplication(1);
203 }
205 // Now remove it
206 launcherModel->requestRemove(launcherModel->get(1)->appId());
208 if (running) {
209 // both apps are running, both apps must still be here
210 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
212 // Item must be unpinned now
213 QCOMPARE(launcherModel->get(1)->pinned(), false);
215 } else if (pinned) {
216 // Item 1 must go away, item 0 is here to stay
217 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
218 }
220 // done our checks. now stop the app if was still running
221 if (running) {
222 appManager->removeApplication(1);
223 }
225 // It needs to go away in any case now
226 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
227 }
229 void testQuickListPinningRemoving() {
230 // we start with 2 unpinned items
231 QCOMPARE(launcherModel->get(0)->pinned(), false);
232 QCOMPARE(launcherModel->get(1)->pinned(), false);
233 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
235 // find the Pin item in the quicklist
236 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
237 int pinActionIndex = -1;
238 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
239 if (model->get(i).actionId() == "pin_item") {
240 pinActionIndex = i;
241 break;
242 }
243 }
244 QVERIFY(pinActionIndex >= 0);
246 // trigger pin item quicklist action => Item must be pinned now.
247 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
248 QCOMPARE(launcherModel->get(0)->pinned(), true);
249 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
251 // quicklist needs to transform to remove item. trigger it and check it item goes away
252 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
253 QCOMPARE(launcherModel->get(0)->pinned(), false);
255 // still needs to be here as the app is still here
256 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
257 // close the app
258 appManager->removeApplication(0);
259 // Now it needs to go away
260 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
261 }
263 void testApplicationFocused() {
264 // all apps unfocused at beginning...
265 QCOMPARE(launcherModel->get(0)->focused(), false);
266 QCOMPARE(launcherModel->get(1)->focused(), false);
268 appManager->focusApplication("abs-icon");
269 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
270 QCOMPARE(launcherModel->get(0)->focused(), true);
271 QCOMPARE(launcherModel->get(1)->focused(), false);
273 appManager->focusApplication("no-icon");
274 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
275 QCOMPARE(launcherModel->get(0)->focused(), false);
276 QCOMPARE(launcherModel->get(1)->focused(), true);
277 }
279 void testClosingApps() {
280 // At the start there are 2 items. Let's pin one.
281 launcherModel->pin("abs-icon");
282 while (appManager->count() > 0) {
283 appManager->removeApplication(0);
284 }
285 // The pinned one needs to stay, the other needs to disappear
286 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
287 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
288 }
290 void testGetUrlForAppId() {
291 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
292 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
293 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
294 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("application:///com.test.good.desktop"));
295 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
296 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
297 }
299 void testIntrospection() {
300 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "org.freedesktop.DBus.Introspectable");
301 QDBusReply<QString> reply = interface.call("Introspect");
302 QStringList nodes = extractNodes(reply.value());
303 QCOMPARE(nodes.count(), launcherModel->rowCount());
305 appManager->addApplication(new MockApp("foobar"));
306 reply = interface.call("Introspect");
307 nodes = extractNodes(reply.value());
308 QCOMPARE(nodes.contains("foobar"), true);
310 appManager->removeApplication(2);
311 reply = interface.call("Introspect");
312 nodes = extractNodes(reply.value());
313 QCOMPARE(nodes.contains("foobar"), false);
314 }
316 QStringList extractNodes(const QString &introspectionXml) {
317 QXmlStreamReader introspectReply(introspectionXml);
319 QStringList ret;
320 while (!introspectReply.atEnd() && !introspectReply.hasError()) {
321 QXmlStreamReader::TokenType token = introspectReply.readNext();
323 if (token == QXmlStreamReader::StartElement) {
324 if (introspectReply.name() == "node" && introspectReply.attributes().count() > 0) {
325 ret << introspectReply.attributes().value("name").toString();
326 }
327 }
328 }
329 return ret;
330 }
332 void testCountEmblems() {
333 // Call GetAll on abs-icon
334 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher/abs_2Dicon", "org.freedesktop.DBus.Properties");
335 QDBusReply<QVariantMap> reply = interface.call("GetAll");
336 QVariantMap map = reply.value();
338 // Make sure GetAll returns a map with count and countVisible props
339 QCOMPARE(map.contains("count"), true);
340 QCOMPARE(map.contains("countVisible"), true);
342 // Make sure count is intitilized to 0 and non-visible
343 QCOMPARE(map.value("count").toInt(), 0);
344 QCOMPARE(map.value("countVisible").toBool(), false);
346 // Now make it visible and set it to 55 through D-Bus
347 interface.call("Set", "com.canonical.Unity.Launcher.Item", "count", 55);
348 interface.call("Set", "com.canonical.Unity.Launcher.Item", "countVisible", true);
350 // Fetch it again using GetAll
351 reply = interface.call("GetAll");
352 map = reply.value();
354 // Make sure values have changed on the D-Bus interface
355 QCOMPARE(map.value("count").toInt(), 55);
356 QCOMPARE(map.value("countVisible").toBool(), true);
358 // Now the item on the upper side of the API
359 int index = launcherModel->findApplication("abs-icon");
360 QCOMPARE(index >= 0, true);
362 // And make sure values have changed there as well
363 QCOMPARE(launcherModel->get(index)->countVisible(), true);
364 QCOMPARE(launcherModel->get(index)->count(), 55);
365 }
367 void testRefresh() {
368 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
369 QDBusReply<void> reply = interface.call("Refresh");
371 // Make sure the call to Refresh returned without error.
372 QCOMPARE(reply.isValid(), true);
373 }
374};
376QTEST_GUILESS_MAIN(LauncherModelTest)
377#include "launchermodeltest.moc"