on ebf4a8a help sign in
unity8/tests/plugins/Unity/Launcher/launchermodeltest.cpp
216 lines 8.1 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; }
43 // Methods used for mocking (not in the interface)
44 void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
45private:
46 QString m_appId;
47 bool m_focused;
48};
50// This is a mock, specifically to test the LauncherModel
51class MockAppManager: public unity::shell::application::ApplicationManagerInterface
53 Q_OBJECT
54public:
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();
61 }
62 return QString();
63 }
64 unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
65 unity::shell::application::ApplicationInfoInterface *findApplication(const QString &) const { return nullptr; }
66 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
67 bool stopApplication(const QString &) { return false; }
68 bool focusApplication(const QString &appId) {
69 Q_FOREACH(MockApp* app, m_list) {
70 app->setFocused(app->appId() == appId);
71 }
72 Q_EMIT focusedApplicationIdChanged();
73 return true;
74 }
76 void unfocusCurrentApplication() { }
78 void addApplication(MockApp *app) {
79 beginInsertRows(QModelIndex(), count(), count());
80 m_list.append(app);
81 endInsertRows();
82 }
83 void removeApplication(int index) {
84 beginRemoveRows(QModelIndex(), index, index);
85 m_list.takeAt(index)->deleteLater();
86 endRemoveRows();
87 }
89private:
90 QList<MockApp*> m_list;
91};
93class LauncherModelTest : public QObject
95 Q_OBJECT
97private:
98 LauncherModel *launcherModel;
99 MockAppManager *appManager;
101private Q_SLOTS:
103 void initTestCase() {
104 launcherModel = new LauncherModel(this);
105 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
107 appManager = new MockAppManager(this);
108 launcherModel->setApplicationManager(appManager);
109 }
111 // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
112 void init() {
113 appManager->addApplication(new MockApp("abs-icon"));
114 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
116 appManager->addApplication(new MockApp("no-icon"));
117 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
118 }
120 // Removing apps from appmanager and launcher as pinned ones would stick
121 void cleanup() {
122 while (appManager->count() > 0) {
123 appManager->removeApplication(0);
124 }
125 while (launcherModel->rowCount(QModelIndex()) > 0) {
126 launcherModel->requestRemove(launcherModel->get(0)->appId());
127 }
128 }
130 void testMove() {
131 QCOMPARE(launcherModel->get(0)->pinned(), false);
132 QCOMPARE(launcherModel->get(1)->pinned(), false);
134 LauncherItemInterface *item0BeforeMove = launcherModel->get(0);
135 LauncherItemInterface *item1BeforeMove = launcherModel->get(1);
136 launcherModel->move(1, 0);
138 QCOMPARE(item0BeforeMove, launcherModel->get(1));
139 QCOMPARE(item1BeforeMove, launcherModel->get(0));
141 // moved item must be pinned now
142 QCOMPARE(item0BeforeMove->pinned(), false);
143 QCOMPARE(item1BeforeMove->pinned(), true);
144 }
146 void testPinning() {
147 QCOMPARE(launcherModel->get(0)->pinned(), false);
148 QCOMPARE(launcherModel->get(1)->pinned(), false);
149 launcherModel->pin(launcherModel->get(0)->appId());
150 QCOMPARE(launcherModel->get(0)->pinned(), true);
151 QCOMPARE(launcherModel->get(1)->pinned(), false);
152 }
154 void testRemove() {
155 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
156 launcherModel->requestRemove(launcherModel->get(0)->appId());
157 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
158 }
160 void testQuickListPinningRemoving() {
161 // we start with 2 unpinned items
162 QCOMPARE(launcherModel->get(0)->pinned(), false);
163 QCOMPARE(launcherModel->get(1)->pinned(), false);
164 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
166 // find the Pin item in the quicklist
167 QuickListModel *model = qobject_cast<QuickListModel*>(launcherModel->get(0)->quickList());
168 int pinActionIndex = -1;
169 for (int i = 0; i < model->rowCount(QModelIndex()); ++i) {
170 if (model->get(i).actionId() == "pin_item") {
171 pinActionIndex = i;
172 break;
173 }
174 }
175 QVERIFY(pinActionIndex >= 0);
177 // trigger pin item quicklist action => Item must be pinned now.
178 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
179 QCOMPARE(launcherModel->get(0)->pinned(), true);
180 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
182 // quicklist needs to transform to remove item. trigger it and check it item goes away
183 launcherModel->quickListActionInvoked(launcherModel->get(0)->appId(), pinActionIndex);
184 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
185 }
187 void testApplicationFocused() {
188 // all apps unfocused at beginning...
189 QCOMPARE(launcherModel->get(0)->focused(), false);
190 QCOMPARE(launcherModel->get(1)->focused(), false);
192 appManager->focusApplication("abs-icon");
193 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
194 QCOMPARE(launcherModel->get(0)->focused(), true);
195 QCOMPARE(launcherModel->get(1)->focused(), false);
197 appManager->focusApplication("no-icon");
198 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
199 QCOMPARE(launcherModel->get(0)->focused(), false);
200 QCOMPARE(launcherModel->get(1)->focused(), true);
201 }
203 void testClosingApps() {
204 // At the start there are 2 items. Let's pin one.
205 launcherModel->pin("abs-icon");
206 while (appManager->count() > 0) {
207 appManager->removeApplication(0);
208 }
209 // The pinned one needs to stay, the other needs to disappear
210 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
211 QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
212 }
213};
215QTEST_GUILESS_MAIN(LauncherModelTest)
216#include "launchermodeltest.moc"