@@ -19,39 +19,107 @@
20// unity-api
20// unity-api
21#include <unity/shell/launcher/LauncherModelInterface.h>
21#include <unity/shell/launcher/LauncherModelInterface.h>
22+#include <unity/shell/application/ApplicationInfoInterface.h>
23#include "launcheritem.h"
24#include "launcheritem.h"
24#include "launchermodel.h"
25#include "launchermodel.h"
26#include <QtTest>
27#include <QtTest>
29+class MockApp: public unity::shell::application::ApplicationInfoInterface
33+ MockApp(const QString &appId, QObject *parent = 0): ApplicationInfoInterface(appId, parent), m_appId(appId), m_focused(false) { }
34+ QString appId() const { return m_appId; }
35+ QString name() const { return "mock"; }
36+ QString comment() const { return "this is a mock"; }
37+ QUrl icon() const { return QUrl(); }
38+ ApplicationInfoInterface::Stage stage() const { return ApplicationInfoInterface::MainStage; }
39+ ApplicationInfoInterface::State state() const { return ApplicationInfoInterface::Running; }
40+ bool focused() const { return m_focused; }
42+ // Methods used for mocking (not in the interface)
43+ void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
49+class MockAppManager: public unity::shell::application::ApplicationManagerInterface
53+ MockAppManager(QObject *parent = 0): ApplicationManagerInterface(parent) {}
54+ int rowCount(const QModelIndex &) const { return m_list.count(); }
55+ QVariant data(const QModelIndex &, int ) const { return QVariant(); }
56+ QString focusedApplicationId() const {
57+ Q_FOREACH(MockApp *app, m_list) {
58+ if (app->focused()) return app->appId();
62+ unity::shell::application::ApplicationInfoInterface *get(int index) const { return m_list.at(index); }
63+ unity::shell::application::ApplicationInfoInterface *findApplication(const QString &) const { return nullptr; }
64+ unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
65+ bool stopApplication(const QString &) { return false; }
66+ bool focusApplication(const QString &appId) {
67+ Q_FOREACH(MockApp* app, m_list) {
68+ app->setFocused(app->appId() == appId);
70+ Q_EMIT focusedApplicationIdChanged();
74+ void unfocusCurrentApplication() { }
76+ void addApplication(MockApp *app) {
77+ beginInsertRows(QModelIndex(), count(), count());
81+ void removeApplication(int index) {
82+ beginRemoveRows(QModelIndex(), index, index);
83+ m_list.takeAt(index)->deleteLater();
88+ QList<MockApp*> m_list;
28class LauncherModelTest : public QObject
91class LauncherModelTest : public QObject
33 LauncherModel *launcherModel;
96 LauncherModel *launcherModel;
97+ MockAppManager *appManager;
35private Q_SLOTS:
99private Q_SLOTS:
37 void initTestCase() {
101 void initTestCase() {
38 launcherModel = new LauncherModel(this);
102 launcherModel = new LauncherModel(this);
39 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
103 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
105+ appManager = new MockAppManager(this);
106+ launcherModel->setApplicationManager(appManager);
109+ // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
42 void init() {
110 void init() {
43−/* qDebug() << "init";
111+ appManager->addApplication(new MockApp("abs-icon"));
44− launcherModel->applicationFocused("abs-icon");
45 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
112 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
47− launcherModel->applicationFocused("no-icon.desktop");
114+ appManager->addApplication(new MockApp("no-icon"));
48 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
115 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
50− launcherModel->applicationFocused(QString());
118+ // Removing apps from appmanager and launcher as pinned ones would stick
54 void cleanup() {
119 void cleanup() {
120+ while (appManager->count() > 0) {
121+ appManager->removeApplication(0);
55 while (launcherModel->rowCount(QModelIndex()) > 0) {
123 while (launcherModel->rowCount(QModelIndex()) > 0) {
56 launcherModel->requestRemove(launcherModel->get(0)->appId());
124 launcherModel->requestRemove(launcherModel->get(0)->appId());
@@ -115,32 +183,30 @@ private Q_SLOTS:
117 void testApplicationFocused() {
185 void testApplicationFocused() {
118−/* // all apps unfocused at beginning...
186+ // all apps unfocused at beginning...
119 QCOMPARE(launcherModel->get(0)->focused(), false);
187 QCOMPARE(launcherModel->get(0)->focused(), false);
120 QCOMPARE(launcherModel->get(1)->focused(), false);
188 QCOMPARE(launcherModel->get(1)->focused(), false);
122− launcherModel->applicationFocused("abs-icon");
190+ appManager->focusApplication("abs-icon");
123 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
191 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
124 QCOMPARE(launcherModel->get(0)->focused(), true);
192 QCOMPARE(launcherModel->get(0)->focused(), true);
125 QCOMPARE(launcherModel->get(1)->focused(), false);
193 QCOMPARE(launcherModel->get(1)->focused(), false);
127− launcherModel->applicationFocused("no-icon");
195+ appManager->focusApplication("no-icon");
128 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
196 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
129 QCOMPARE(launcherModel->get(0)->focused(), false);
197 QCOMPARE(launcherModel->get(0)->focused(), false);
130 QCOMPARE(launcherModel->get(1)->focused(), true);
198 QCOMPARE(launcherModel->get(1)->focused(), true);
201+ void testClosingApps() {
202+ // At the start there are 2 items. Let's pin one.
132− launcherModel->applicationFocused("rel-icon");
203+ launcherModel->pin("abs-icon");
133− QCOMPARE(launcherModel->rowCount(QModelIndex()), 3);
204+ while (appManager->count() > 0) {
134− QCOMPARE(launcherModel->get(0)->focused(), false);
205+ appManager->removeApplication(0);
135− QCOMPARE(launcherModel->get(1)->focused(), false);
206+ }
136− QCOMPARE(launcherModel->get(2)->focused(), true);
207+ // The pinned one needs to stay, the other needs to disappear
138− launcherModel->applicationFocused(QString());
139− QCOMPARE(launcherModel->rowCount(QModelIndex()), 3);
208+ QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
140− QCOMPARE(launcherModel->get(0)->focused(), false);
209+ QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
141− QCOMPARE(launcherModel->get(1)->focused(), false);
142− QCOMPARE(launcherModel->get(2)->focused(), false);