2 * Copyright 2013 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/>.
17 * Michael Zanetti <michael.zanetti@canonical.com>
21#include <unity/shell/launcher/LauncherModelInterface.h>
22#include <unity/shell/application/ApplicationInfoInterface.h>
24#include "launcheritem.h"
25#include "launchermodel.h"
29// This is a mock, specifically to test the LauncherModel
30class MockApp: public unity::shell::application::ApplicationInfoInterface
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; }
43 // Methods used for mocking (not in the interface)
44 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
50// This is a mock, specifically to test the LauncherModel
51class MockAppManager: public unity::shell::application::ApplicationManagerInterface
55 MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
56 int rowCount(const QModelIndex &) const { return m_list.count(); }
57 QVariant data(const QModelIndex &, int ) const { return QVariant(); }
58 QString focusedApplicationId() const {
59 Q_FOREACH(MockApp *app, m_list) {
60 if (app->focused()) return app->appId();
64 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
65 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &appId) const {
66 Q_FOREACH(MockApp* app, m_list) {
67 if (app->appId() == appId) {
73 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
74 bool stopApplication(const QString &) { return false; }
75 bool focusApplication(const QString &appId) {
76 Q_FOREACH(MockApp* app, m_list) {
77 app->setFocused(app->appId() == appId);
79 Q_EMIT focusedApplicationIdChanged();
83 void unfocusCurrentApplication() { }
85 void addApplication(MockApp *app) {
86 beginInsertRows(QModelIndex(), count(), count());
90 void removeApplication(int index) {
91 beginRemoveRows(QModelIndex(), index, index);
92 m_list.takeAt(index)->deleteLater();
97 QList<MockApp*> m_list;
100class LauncherModelTest : public QObject
105 LauncherModel *launcherModel;
106 MockAppManager *appManager;
110 void initTestCase() {
111 launcherModel = new LauncherModel(this);
112 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
114 appManager = new MockAppManager(this);
115 launcherModel->setApplicationManager(appManager);
118 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
120 appManager->addApplication(new MockApp("abs-icon"));
121 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
123 appManager->addApplication(new MockApp("no-icon"));
124 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
127 // Removing apps from appmanager and launcher as pinned ones would stick
129 while (appManager->count() > 0) {
130 appManager->removeApplication(0);
132 while (launcherModel->rowCount(QModelIndex()) > 0) {
133 launcherModel->requestRemove(launcherModel->get(0)->appId());
138 QCOMPARE(launcherModel->get(0)->pinned(), false);
139 QCOMPARE(launcherModel->get(1)->pinned(), false);
141 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
142 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
143 launcherModel->move(1, 0);
145 QCOMPARE(item0BeforeMove, launcherModel->get(1));
146 QCOMPARE(item1BeforeMove, launcherModel->get(0));
148 // moved item must be pinned now
149 QCOMPARE(item0BeforeMove->pinned(), false);
150 QCOMPARE(item1BeforeMove->pinned(), true);
154 QCOMPARE(launcherModel->get(0)->pinned(), false);
155 QCOMPARE(launcherModel->get(1)->pinned(), false);
156 launcherModel->pin(launcherModel->get(0)->appId());
157 QCOMPARE(launcherModel->get(0)->pinned(), true);
158 QCOMPARE(launcherModel->get(1)->pinned(), false);
161 void testRemove_data() {
163 QTest::addColumn<bool>("pinned");
164 QTest::addColumn<bool>("running");
166 QTest::newRow("non-pinned, running") << false << true;
167 QTest::newRow("pinned, running") << true << false;
168 QTest::newRow("pinned, non-running") << true << false;
172 QFETCH(bool, pinned);
173 QFETCH(bool, running);
175 // In the beginning we always have two items
176 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
178 // pin one if required
180 launcherModel->pin(launcherModel->get(1)->appId());
183 // stop it if required
185 appManager->removeApplication(1);
189 launcherModel->requestRemove(launcherModel->get(1)->appId());
192 // both apps are running, both apps must still be here
193 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
195 // Item must be unpinned now
196 QCOMPARE(launcherModel->get(1)->pinned(), false);
199 // Item 1 must go away, item 0 is here to stay
200 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
203 // done our checks. now stop the app if was still running
205 appManager->removeApplication(1);
208 // It needs to go away in any case now
209 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
212 void testQuickListPinningRemoving() {
213 // we start with 2 unpinned items
214 QCOMPARE(launcherModel->get(0)->pinned(), false);
215 QCOMPARE(launcherModel->get(1)->pinned(), false);
216 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
218 // find the Pin item in the quicklist
219 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
220 int pinActionIndex = -1;
221 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
222 if (model->get(i).actionId() == "pin_item") {
227 QVERIFY(pinActionIndex >= 0);
229 // trigger pin item quicklist action => Item must be pinned now.
230 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
231 QCOMPARE(launcherModel->get(0)->pinned(), true);
232 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
234 // quicklist needs to transform to remove item. trigger it and check it item goes away
235 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
236 QCOMPARE(launcherModel->get(0)->pinned(), false);
238 // still needs to be here as the app is still here
239 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
241 appManager->removeApplication(0);
242 // Now it needs to go away
243 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
246 void testApplicationFocused() {
247 // all apps unfocused at beginning...
248 QCOMPARE(launcherModel->get(0)->focused(), false);
249 QCOMPARE(launcherModel->get(1)->focused(), false);
251 appManager->focusApplication("abs-icon");
252 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
253 QCOMPARE(launcherModel->get(0)->focused(), true);
254 QCOMPARE(launcherModel->get(1)->focused(), false);
256 appManager->focusApplication("no-icon");
257 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
258 QCOMPARE(launcherModel->get(0)->focused(), false);
259 QCOMPARE(launcherModel->get(1)->focused(), true);
262 void testClosingApps() {
263 // At the start there are 2 items. Let's pin one.
264 launcherModel->pin("abs-icon");
265 while (appManager->count() > 0) {
266 appManager->removeApplication(0);
268 // The pinned one needs to stay, the other needs to disappear
269 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
270 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
274QTEST_GUILESS_MAIN(LauncherModelTest)
275#include "launchermodeltest.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