on 41df433 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
302 lines 12 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"
27#include <QtTest>
29// This is a mock, specifically to test the LauncherModel
30class MockApp: public unity::shell::application::ApplicationInfoInterface
32 Q_OBJECT
33public:
34 MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
35 QString appId() const { return m_appId; }
36 QString name() const { return "mock"; }
37 QString comment() const { return "this is a mock"; }
38 QUrl icon() const { return QUrl(); }
39 ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
40 ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
41 bool focused() const { return m_focused; }
42 QString splashTitle() const override { return QString(); }
43 QUrl splashImage() const override { return QUrl(); }
44 bool splashShowHeader() const override { return true; }
45 QColor splashColor() const override { return QColor(0,0,0,0); }
46 QColor splashColorHeader() const override { return QColor(0,0,0,0); }
47 QColor splashColorFooter() const override { return QColor(0,0,0,0); }
49 // Methods used for mocking (not in the interface)
50 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
51private:
52 QString m_appId;
53 bool m_focused;
54};
56// This is a mock, specifically to test the LauncherModel
57class MockAppManager: public unity::shell::application::ApplicationManagerInterface
59 Q_OBJECT
60public:
61 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
62 int rowCount(const QModelIndex &) const { return m_list.count(); }
63 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
64 QString focusedApplicationId() const {
65 Q_FOREACH(MockApp *app, m_list) {
66 if (app->focused()) return app->appId();
67 }
68 return QString();
69 }
70 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
71 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
72 Q_FOREACH(MockApp* app, m_list) {
73 if (app->appId() == appId) {
74 return app;
75 }
76 }
77 return nullptr;
78 }
79 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
80 bool stopApplication(const QString &) { return false; }
81 bool focusApplication(const QString &appId) {
82 Q_FOREACH(MockApp* app, m_list) {
83 app->setFocused(app->appId() == appId);
84 }
85 Q_EMIT focusedApplicationIdChanged();
86 return true;
87 }
89 void unfocusCurrentApplication() { }
91 void addApplication(MockApp *app) {
92 beginInsertRows(QModelIndex(), count(), count());
93 m_list.append(app);
94 endInsertRows();
95 }
96 void removeApplication(int index) {
97 beginRemoveRows(QModelIndex(), index, index);
98 m_list.takeAt(index)->deleteLater();
99 endRemoveRows();
100 }
101 bool requestFocusApplication(const QString &appId) { Q_UNUSED(appId); return true; }
102 bool suspended() const { return false; }
103 void setSuspended(bool) {}
105private:
106 QList<MockApp*> m_list;
107};
109class LauncherModelTest : public QObject
111 Q_OBJECT
113private:
114 LauncherModel *launcherModel;
115 MockAppManager *appManager;
117private Q_SLOTS:
119 void initTestCase() {
120 launcherModel = new LauncherModel(this);
121 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
123 appManager = new MockAppManager(this);
124 launcherModel->setApplicationManager(appManager);
125 }
127 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
128 void init() {
129 appManager->addApplication(new MockApp("abs-icon"));
130 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
132 appManager->addApplication(new MockApp("no-icon"));
133 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
134 }
136 // Removing apps from appmanager and launcher as pinned ones would stick
137 void cleanup() {
138 while (appManager->count() > 0) {
139 appManager->removeApplication(0);
140 }
141 while (launcherModel->rowCount(QModelIndex()) > 0) {
142 launcherModel->requestRemove(launcherModel->get(0)->appId());
143 }
144 }
146 void testMove() {
147 QCOMPARE(launcherModel->get(0)->pinned(), false);
148 QCOMPARE(launcherModel->get(1)->pinned(), false);
150 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
151 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
152 launcherModel->move(1, 0);
154 QCOMPARE(item0BeforeMove, launcherModel->get(1));
155 QCOMPARE(item1BeforeMove, launcherModel->get(0));
157 // moved item must be pinned now
158 QCOMPARE(item0BeforeMove->pinned(), false);
159 QCOMPARE(item1BeforeMove->pinned(), true);
160 }
162 void testPinning() {
163 QSignalSpy spy(launcherModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
164 QCOMPARE(launcherModel->get(0)->pinned(), false);
165 QCOMPARE(launcherModel->get(1)->pinned(), false);
166 launcherModel->pin(launcherModel->get(0)->appId());
167 QCOMPARE(launcherModel->get(0)->pinned(), true);
168 QCOMPARE(launcherModel->get(1)->pinned(), false);
169 QCOMPARE(spy.count(), 1);
170 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
172 launcherModel->requestRemove(launcherModel->get(0)->appId());
173 QCOMPARE(launcherModel->get(0)->pinned(), false);
174 QCOMPARE(launcherModel->get(1)->pinned(), false);
175 QCOMPARE(spy.count(), 2);
176 QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
177 }
179 void testRemove_data() {
181 QTest::addColumn<bool>("pinned");
182 QTest::addColumn<bool>("running");
184 QTest::newRow("non-pinned, running") << false << true;
185 QTest::newRow("pinned, running") << true << false;
186 QTest::newRow("pinned, non-running") << true << false;
187 }
189 void testRemove() {
190 QFETCH(bool, pinned);
191 QFETCH(bool, running);
193 // In the beginning we always have two items
194 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
196 // pin one if required
197 if (pinned) {
198 launcherModel->pin(launcherModel->get(1)->appId());
199 }
201 // stop it if required
202 if (!running) {
203 appManager->removeApplication(1);
204 }
206 // Now remove it
207 launcherModel->requestRemove(launcherModel->get(1)->appId());
209 if (running) {
210 // both apps are running, both apps must still be here
211 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
213 // Item must be unpinned now
214 QCOMPARE(launcherModel->get(1)->pinned(), false);
216 } else if (pinned) {
217 // Item 1 must go away, item 0 is here to stay
218 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
219 }
221 // done our checks. now stop the app if was still running
222 if (running) {
223 appManager->removeApplication(1);
224 }
226 // It needs to go away in any case now
227 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
228 }
230 void testQuickListPinningRemoving() {
231 // we start with 2 unpinned items
232 QCOMPARE(launcherModel->get(0)->pinned(), false);
233 QCOMPARE(launcherModel->get(1)->pinned(), false);
234 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
236 // find the Pin item in the quicklist
237 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
238 int pinActionIndex = -1;
239 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
240 if (model->get(i).actionId() == "pin_item") {
241 pinActionIndex = i;
242 break;
243 }
244 }
245 QVERIFY(pinActionIndex >= 0);
247 // trigger pin item quicklist action => Item must be pinned now.
248 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
249 QCOMPARE(launcherModel->get(0)->pinned(), true);
250 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
252 // quicklist needs to transform to remove item. trigger it and check it item goes away
253 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
254 QCOMPARE(launcherModel->get(0)->pinned(), false);
256 // still needs to be here as the app is still here
257 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
258 // close the app
259 appManager->removeApplication(0);
260 // Now it needs to go away
261 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
262 }
264 void testApplicationFocused() {
265 // all apps unfocused at beginning...
266 QCOMPARE(launcherModel->get(0)->focused(), false);
267 QCOMPARE(launcherModel->get(1)->focused(), false);
269 appManager->focusApplication("abs-icon");
270 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
271 QCOMPARE(launcherModel->get(0)->focused(), true);
272 QCOMPARE(launcherModel->get(1)->focused(), false);
274 appManager->focusApplication("no-icon");
275 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
276 QCOMPARE(launcherModel->get(0)->focused(), false);
277 QCOMPARE(launcherModel->get(1)->focused(), true);
278 }
280 void testClosingApps() {
281 // At the start there are 2 items. Let's pin one.
282 launcherModel->pin("abs-icon");
283 while (appManager->count() > 0) {
284 appManager->removeApplication(0);
285 }
286 // The pinned one needs to stay, the other needs to disappear
287 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
288 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
289 }
291 void testGetUrlForAppId() {
292 QCOMPARE(launcherModel->getUrlForAppId(QString()), QString());
293 QCOMPARE(launcherModel->getUrlForAppId(""), QString());
294 QCOMPARE(launcherModel->getUrlForAppId("no-name"), QString("application:///no-name.desktop"));
295 QCOMPARE(launcherModel->getUrlForAppId("com.test.good"), QString("appid://com.test.good/first-listed-app/current-user-version"));
296 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application"), QString("appid://com.test.good/application/current-user-version"));
297 QCOMPARE(launcherModel->getUrlForAppId("com.test.good_application_1.2.3"), QString("appid://com.test.good/application/current-user-version"));
298 }
299};
301QTEST_GUILESS_MAIN(LauncherModelTest)
302#include "launchermodeltest.moc"