@@ -19,39 +19,107 @@
1919
2020// unity-api
2121#include <unity/shell/launcher/LauncherModelInterface.h>
22+#include <unity/shell/application/ApplicationInfoInterface.h>
2223
2324#include "launcheritem.h"
2425#include "launchermodel.h"
2526
2627#include <QtTest>
2728
29+class MockApp: public unity::shell::application::ApplicationInfoInterface
30+{
31+ Q_OBJECT
32+public:
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; }
41+
42+ // Methods used for mocking (not in the interface)
43+ void setFocused(bool focused) { m_focused = focused; Q_EMIT focusedChanged(focused); }
44+private:
45+ QString m_appId;
46+ bool m_focused;
47+};
48+
49+class MockAppManager: public unity::shell::application::ApplicationManagerInterface
50+{
51+ Q_OBJECT
52+public:
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();
59+ }
60+ return QString();
61+ }
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);
69+ }
70+ Q_EMIT focusedApplicationIdChanged();
71+ return true;
72+ }
73+
74+ void unfocusCurrentApplication() { }
75+
76+ void addApplication(MockApp *app) {
77+ beginInsertRows(QModelIndex(), count(), count());
78+ m_list.append(app);
79+ endInsertRows();
80+ }
81+ void removeApplication(int index) {
82+ beginRemoveRows(QModelIndex(), index, index);
83+ m_list.takeAt(index)->deleteLater();
84+ endRemoveRows();
85+ }
86+
87+private:
88+ QList<MockApp*> m_list;
89+};
90+
2891class LauncherModelTest : public QObject
2992{
3093 Q_OBJECT
3194
3295private:
3396 LauncherModel *launcherModel;
97+ MockAppManager *appManager;
3498
3599private Q_SLOTS:
36100
37101 void initTestCase() {
38102 launcherModel = new LauncherModel(this);
39103 QCOMPARE(launcherModel->rowCount(QModelIndex()), 0);
104+
105+ appManager = new MockAppManager(this);
106+ launcherModel->setApplicationManager(appManager);
40107 }
41108
109+ // Adding 2 apps to the mock appmanager. Both should appear in the launcher.
42110 void init() {
43−/* qDebug() << "init";
44− launcherModel->applicationFocused("abs-icon");
111+ appManager->addApplication(new MockApp("abs-icon"));
45112 QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
46113
47− launcherModel->applicationFocused("no-icon.desktop");
114+ appManager->addApplication(new MockApp("no-icon"));
48115 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
49−
50− launcherModel->applicationFocused(QString());
51− */
52116 }
53117
118+ // Removing apps from appmanager and launcher as pinned ones would stick
54119 void cleanup() {
120+ while (appManager->count() > 0) {
121+ appManager->removeApplication(0);
122+ }
55123 while (launcherModel->rowCount(QModelIndex()) > 0) {
56124 launcherModel->requestRemove(launcherModel->get(0)->appId());
57125 }
@@ -115,32 +183,30 @@ private Q_SLOTS:
115183 }
116184
117185 void testApplicationFocused() {
118−/* // all apps unfocused at beginning...
186+ // all apps unfocused at beginning...
119187 QCOMPARE(launcherModel->get(0)->focused(), false);
120188 QCOMPARE(launcherModel->get(1)->focused(), false);
121189
122− launcherModel->applicationFocused("abs-icon");
190+ appManager->focusApplication("abs-icon");
123191 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
124192 QCOMPARE(launcherModel->get(0)->focused(), true);
125193 QCOMPARE(launcherModel->get(1)->focused(), false);
126194
127− launcherModel->applicationFocused("no-icon");
195+ appManager->focusApplication("no-icon");
128196 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
129197 QCOMPARE(launcherModel->get(0)->focused(), false);
130198 QCOMPARE(launcherModel->get(1)->focused(), true);
199+ }
131200
132− launcherModel->applicationFocused("rel-icon");
133− QCOMPARE(launcherModel->rowCount(QModelIndex()), 3);
134− QCOMPARE(launcherModel->get(0)->focused(), false);
135− QCOMPARE(launcherModel->get(1)->focused(), false);
136− QCOMPARE(launcherModel->get(2)->focused(), true);
137−
138− launcherModel->applicationFocused(QString());
139− QCOMPARE(launcherModel->rowCount(QModelIndex()), 3);
140− QCOMPARE(launcherModel->get(0)->focused(), false);
141− QCOMPARE(launcherModel->get(1)->focused(), false);
142− QCOMPARE(launcherModel->get(2)->focused(), false);
143− */
201+ void testClosingApps() {
202+ // At the start there are 2 items. Let's pin one.
203+ launcherModel->pin("abs-icon");
204+ while (appManager->count() > 0) {
205+ appManager->removeApplication(0);
206+ }
207+ // The pinned one needs to stay, the other needs to disappear
208+ QCOMPARE(launcherModel->rowCount(QModelIndex()), 1);
209+ QCOMPARE(launcherModel->get(0)->appId(), QLatin1String("abs-icon"));
144210 }
145211};
146212