@@ -24,6 +24,7 @@
2424#include "launcheritem.h"
2525#include "launchermodel.h"
2626#include "dbusinterface.h"
27+#include "gsettings.h"
2728
2829#include <QtTest>
2930#include <QDBusInterface>
@@ -81,7 +82,15 @@ public:
8182 return nullptr;
8283 }
8384 unity::shell::application::ApplicationInfoInterface *startApplication(const QString &, const QStringList &) { return nullptr; }
84− bool stopApplication(const QString &) { return false; }
85+ bool stopApplication(const QString &appId) {
86+ Q_FOREACH(MockApp* app, m_list) {
87+ if (app->appId() == appId) {
88+ removeApplication(m_list.indexOf(app));
89+ return true;
90+ }
91+ }
92+ return false;
93+ }
8594 bool focusApplication(const QString &appId) {
8695 Q_FOREACH(MockApp* app, m_list) {
8796 app->setFocused(app->appId() == appId);
@@ -135,6 +144,8 @@ private Q_SLOTS:
135144
136145 appManager->addApplication(new MockApp("no-icon"));
137146 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
147+
148+ launcherModel->m_settings->setStoredApplications(QStringList());
138149 }
139150
140151 // Removing apps from appmanager and launcher as pinned ones would stick
@@ -170,14 +181,15 @@ private Q_SLOTS:
170181 launcherModel->pin(launcherModel->get(0)->appId());
171182 QCOMPARE(launcherModel->get(0)->pinned(), true);
172183 QCOMPARE(launcherModel->get(1)->pinned(), false);
173− QCOMPARE(spy.count(), 1);
184+ QCOMPARE(spy.count() > 0, true);
174185 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
175186
187+ spy.clear();
176188 launcherModel->requestRemove(launcherModel->get(0)->appId());
177189 QCOMPARE(launcherModel->get(0)->pinned(), false);
178190 QCOMPARE(launcherModel->get(1)->pinned(), false);
179− QCOMPARE(spy.count(), 2);
180− QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
191+ QCOMPARE(spy.count(), 1);
192+ QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
181193 }
182194
183195 void testRemove_data() {
@@ -186,7 +198,7 @@ private Q_SLOTS:
186198 QTest::addColumn<bool>("running");
187199
188200 QTest::newRow("non-pinned, running") << false << true;
189− QTest::newRow("pinned, running") << true << false;
201+ QTest::newRow("pinned, running") << true << true;
190202 QTest::newRow("pinned, non-running") << true << false;
191203 }
192204
@@ -199,23 +211,24 @@ private Q_SLOTS:
199211
200212 // pin one if required
201213 if (pinned) {
202− launcherModel->pin(launcherModel->get(1)->appId());
214+ launcherModel->pin("abs-icon");
203215 }
204216
205217 // stop it if required
206218 if (!running) {
207− appManager->removeApplication(1);
219+ appManager->stopApplication("abs-icon");
208220 }
209221
210222 // Now remove it
211− launcherModel->requestRemove(launcherModel->get(1)->appId());
223+ launcherModel->requestRemove("abs-icon");
212224
213225 if (running) {
214226 // both apps are running, both apps must still be here
215227 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
216228
217229 // Item must be unpinned now
218− QCOMPARE(launcherModel->get(1)->pinned(), false);
230+ int index = launcherModel->findApplication("abs-icon");
231+ QCOMPARE(launcherModel->get(index)->pinned(), false);
219232
220233 } else if (pinned) {
221234 // Item 1 must go away, item 0 is here to stay
@@ -224,7 +237,7 @@ private Q_SLOTS:
224237
225238 // done our checks. now stop the app if was still running
226239 if (running) {
227− appManager->removeApplication(1);
240+ appManager->stopApplication("abs-icon");
228241 }
229242
230243 // It needs to go away in any case now
@@ -369,12 +382,81 @@ private Q_SLOTS:
369382 QCOMPARE(launcherModel->get(index)->count(), 55);
370383 }
371384
372− void testRefresh() {
385+ void testRefreshAfterDeletedDesktopFiles_data() {
386+ QTest::addColumn<bool>("deleted");
387+ QTest::newRow("have .desktop files") << false;
388+ QTest::newRow("deleted .desktop files") << true;
389+ }
390+
391+ void testRefreshAfterDeletedDesktopFiles() {
392+ QFETCH(bool, deleted);
393+
394+ // pin both apps
395+ launcherModel->pin("abs-icon");
396+ launcherModel->pin("no-icon");
397+ // close both apps
398+ appManager->removeApplication(0);
399+ appManager->removeApplication(0);
400+
401+ // "delete" the .desktop files
402+ QString oldCurrent = QDir::currentPath();
403+ if (deleted) {
404+ // In testing mode, the launcher searches the current dir for the sample .desktop file
405+ // We can make that fail by changing the current dir
406+ QDir::setCurrent("..");
407+ }
408+
409+ // Call refresh
373410 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
374411 QDBusReply<void> reply = interface.call("Refresh");
375412
376413 // Make sure the call to Refresh returned without error.
377414 QCOMPARE(reply.isValid(), true);
415+
416+ QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
417+
418+ // Restoring current dir
419+ QDir::setCurrent(oldCurrent);
420+ }
421+
422+ void testSettings_data() {
423+ QTest::addColumn<QStringList>("storedList");
424+
425+ QTest::newRow("2 stored") << (QStringList() << "abs-icon" << "no-icon");
426+ QTest::newRow("1 stored") << (QStringList() << "abs-icon");
427+ }
428+
429+ void testSettings() {
430+ GSettings *settings = launcherModel->m_settings;
431+
432+ // Nothing pinned at startup
433+ QCOMPARE(settings->storedApplications().count(), 0);
434+
435+ // pin both apps
436+ launcherModel->pin("abs-icon");
437+ launcherModel->pin("no-icon");
438+
439+ // Now settings should have 2 apps
440+ QCOMPARE(settings->storedApplications().count(), 2);
441+
442+ // close both apps
443+ appManager->removeApplication(0);
444+ appManager->removeApplication(0);
445+
446+ // Now settings should have 2 apps
447+ QCOMPARE(settings->storedApplications().count(), 2);
448+
449+ // Now remove 1 app through the backend, make sure one is still there
450+ settings->setStoredApplications(QStringList() << "abs-icon");
451+ QCOMPARE(settings->storedApplications().count(), 1);
452+
453+ // Check if it disappeared from the frontend too
454+ QCOMPARE(launcherModel->rowCount(), 1);
455+
456+ // Add them back:
457+ settings->setStoredApplications(QStringList() << "abs-icon" << "no-icon");
458+ QCOMPARE(launcherModel->rowCount(), 2);
459+
378460 }
379461};
380462