on b5ffd72 help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
278 lines 10 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 QUrl screenshot() const { return QUrl(); }
44 // Methods used for mocking (not in the interface)
45 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
46private:
47 QString m_appId;
48 bool m_focused;
49};
51// This is a mock, specifically to test the LauncherModel
52class MockAppManager: public unity::shell::application::ApplicationManagerInterface
54 Q_OBJECT
55public:
56 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
57 int rowCount(const QModelIndex &) const { return m_list.count(); }
58 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
59 QString focusedApplicationId() const {
60 Q_FOREACH(MockApp *app, m_list) {
61 if (app->focused()) return app->appId();
62 }
63 return QString();
64 }
65 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
66 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
67 Q_FOREACH(MockApp* app, m_list) {
68 if (app->appId() == appId) {
69 return app;
70 }
71 }
72 return nullptr;
73 }
74 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
75 bool stopApplication(const QString &) { return false; }
76 bool focusApplication(const QString &appId) {
77 Q_FOREACH(MockApp* app, m_list) {
78 app->setFocused(app->appId() == appId);
79 }
80 Q_EMIT focusedApplicationIdChanged();
81 return true;
82 }
84 void unfocusCurrentApplication() { }
86 void addApplication(MockApp *app) {
87 beginInsertRows(QModelIndex(), count(), count());
88 m_list.append(app);
89 endInsertRows();
90 }
91 void removeApplication(int index) {
92 beginRemoveRows(QModelIndex(), index, index);
93 m_list.takeAt(index)->deleteLater();
94 endRemoveRows();
95 }
96 void updateScreenshot(const QString &appId) { Q_UNUSED(appId); }
97 void activateApplication(const QString &appId) { Q_UNUSED(appId); }
99private:
100 QList<MockApp*> m_list;
101};
103class LauncherModelTest : public QObject
105 Q_OBJECT
107private:
108 LauncherModel *launcherModel;
109 MockAppManager *appManager;
111private Q_SLOTS:
113 void initTestCase() {
114 launcherModel = new LauncherModel(this);
115 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
117 appManager = new MockAppManager(this);
118 launcherModel->setApplicationManager(appManager);
119 }
121 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
122 void init() {
123 appManager->addApplication(new MockApp("abs-icon"));
124 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
126 appManager->addApplication(new MockApp("no-icon"));
127 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
128 }
130 // Removing apps from appmanager and launcher as pinned ones would stick
131 void cleanup() {
132 while (appManager->count() > 0) {
133 appManager->removeApplication(0);
134 }
135 while (launcherModel->rowCount(QModelIndex()) > 0) {
136 launcherModel->requestRemove(launcherModel->get(0)->appId());
137 }
138 }
140 void testMove() {
141 QCOMPARE(launcherModel->get(0)->pinned(), false);
142 QCOMPARE(launcherModel->get(1)->pinned(), false);
144 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
145 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
146 launcherModel->move(1, 0);
148 QCOMPARE(item0BeforeMove, launcherModel->get(1));
149 QCOMPARE(item1BeforeMove, launcherModel->get(0));
151 // moved item must be pinned now
152 QCOMPARE(item0BeforeMove->pinned(), false);
153 QCOMPARE(item1BeforeMove->pinned(), true);
154 }
156 void testPinning() {
157 QCOMPARE(launcherModel->get(0)->pinned(), false);
158 QCOMPARE(launcherModel->get(1)->pinned(), false);
159 launcherModel->pin(launcherModel->get(0)->appId());
160 QCOMPARE(launcherModel->get(0)->pinned(), true);
161 QCOMPARE(launcherModel->get(1)->pinned(), false);
162 }
164 void testRemove_data() {
166 QTest::addColumn<bool>("pinned");
167 QTest::addColumn<bool>("running");
169 QTest::newRow("non-pinned, running") << false << true;
170 QTest::newRow("pinned, running") << true << false;
171 QTest::newRow("pinned, non-running") << true << false;
172 }
174 void testRemove() {
175 QFETCH(bool, pinned);
176 QFETCH(bool, running);
178 // In the beginning we always have two items
179 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
181 // pin one if required
182 if (pinned) {
183 launcherModel->pin(launcherModel->get(1)->appId());
184 }
186 // stop it if required
187 if (!running) {
188 appManager->removeApplication(1);
189 }
191 // Now remove it
192 launcherModel->requestRemove(launcherModel->get(1)->appId());
194 if (running) {
195 // both apps are running, both apps must still be here
196 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
198 // Item must be unpinned now
199 QCOMPARE(launcherModel->get(1)->pinned(), false);
201 } else if (pinned) {
202 // Item 1 must go away, item 0 is here to stay
203 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
204 }
206 // done our checks. now stop the app if was still running
207 if (running) {
208 appManager->removeApplication(1);
209 }
211 // It needs to go away in any case now
212 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
213 }
215 void testQuickListPinningRemoving() {
216 // we start with 2 unpinned items
217 QCOMPARE(launcherModel->get(0)->pinned(), false);
218 QCOMPARE(launcherModel->get(1)->pinned(), false);
219 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
221 // find the Pin item in the quicklist
222 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
223 int pinActionIndex = -1;
224 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
225 if (model->get(i).actionId() == "pin_item") {
226 pinActionIndex = i;
227 break;
228 }
229 }
230 QVERIFY(pinActionIndex >= 0);
232 // trigger pin item quicklist action => Item must be pinned now.
233 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
234 QCOMPARE(launcherModel->get(0)->pinned(), true);
235 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
237 // quicklist needs to transform to remove item. trigger it and check it item goes away
238 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
239 QCOMPARE(launcherModel->get(0)->pinned(), false);
241 // still needs to be here as the app is still here
242 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
243 // close the app
244 appManager->removeApplication(0);
245 // Now it needs to go away
246 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
247 }
249 void testApplicationFocused() {
250 // all apps unfocused at beginning...
251 QCOMPARE(launcherModel->get(0)->focused(), false);
252 QCOMPARE(launcherModel->get(1)->focused(), false);
254 appManager->focusApplication("abs-icon");
255 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
256 QCOMPARE(launcherModel->get(0)->focused(), true);
257 QCOMPARE(launcherModel->get(1)->focused(), false);
259 appManager->focusApplication("no-icon");
260 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
261 QCOMPARE(launcherModel->get(0)->focused(), false);
262 QCOMPARE(launcherModel->get(1)->focused(), true);
263 }
265 void testClosingApps() {
266 // At the start there are 2 items. Let's pin one.
267 launcherModel->pin("abs-icon");
268 while (appManager->count() > 0) {
269 appManager->removeApplication(0);
270 }
271 // The pinned one needs to stay, the other needs to disappear
272 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
273 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
274 }
275};
277QTEST_GUILESS_MAIN(LauncherModelTest)
278#include "launchermodeltest.moc"