@@ -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);
@@ -137,6 +146,8 @@ private Q_SLOTS:
137146
138147 appManager->addApplication(new MockApp("no-icon"));
139148 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
149+
150+ launcherModel->m_settings->setStoredApplications(QStringList());
140151 }
141152
142153 // Removing apps from appmanager and launcher as pinned ones would stick
@@ -172,14 +183,15 @@ private Q_SLOTS:
172183 launcherModel->pin(launcherModel->get(0)->appId());
173184 QCOMPARE(launcherModel->get(0)->pinned(), true);
174185 QCOMPARE(launcherModel->get(1)->pinned(), false);
175− QCOMPARE(spy.count(), 1);
186+ QCOMPARE(spy.count() > 0, true);
176187 QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
177188
189+ spy.clear();
178190 launcherModel->requestRemove(launcherModel->get(0)->appId());
179191 QCOMPARE(launcherModel->get(0)->pinned(), false);
180192 QCOMPARE(launcherModel->get(1)->pinned(), false);
181− QCOMPARE(spy.count(), 2);
182− QCOMPARE(spy.at(1).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
193+ QCOMPARE(spy.count(), 1);
194+ QCOMPARE(spy.at(0).at(2).value<QVector<int>>().first(), (int)LauncherModelInterface::RolePinned);
183195 }
184196
185197 void testRemove_data() {
@@ -188,7 +200,7 @@ private Q_SLOTS:
188200 QTest::addColumn<bool>("running");
189201
190202 QTest::newRow("non-pinned, running") << false << true;
191− QTest::newRow("pinned, running") << true << false;
203+ QTest::newRow("pinned, running") << true << true;
192204 QTest::newRow("pinned, non-running") << true << false;
193205 }
194206
@@ -201,23 +213,24 @@ private Q_SLOTS:
201213
202214 // pin one if required
203215 if (pinned) {
204− launcherModel->pin(launcherModel->get(1)->appId());
216+ launcherModel->pin("abs-icon");
205217 }
206218
207219 // stop it if required
208220 if (!running) {
209− appManager->removeApplication(1);
221+ appManager->stopApplication("abs-icon");
210222 }
211223
212224 // Now remove it
213− launcherModel->requestRemove(launcherModel->get(1)->appId());
225+ launcherModel->requestRemove("abs-icon");
214226
215227 if (running) {
216228 // both apps are running, both apps must still be here
217229 QCOMPARE(launcherModel->rowCount(QModelIndex()), 2);
218230
219231 // Item must be unpinned now
220− QCOMPARE(launcherModel->get(1)->pinned(), false);
232+ int index = launcherModel->findApplication("abs-icon");
233+ QCOMPARE(launcherModel->get(index)->pinned(), false);
221234
222235 } else if (pinned) {
223236 // Item 1 must go away, item 0 is here to stay
@@ -226,7 +239,7 @@ private Q_SLOTS:
226239
227240 // done our checks. now stop the app if was still running
228241 if (running) {
229− appManager->removeApplication(1);
242+ appManager->stopApplication("abs-icon");
230243 }
231244
232245 // It needs to go away in any case now
@@ -371,12 +384,75 @@ private Q_SLOTS:
371384 QCOMPARE(launcherModel->get(index)->count(), 55);
372385 }
373386
374− void testRefresh() {
387+ void testRefreshAfterDeletedDesktopFiles_data() {
388+ QTest::addColumn<bool>("deleted");
389+ QTest::newRow("have .desktop files") << false;
390+ QTest::newRow("deleted .desktop files") << true;
391+ }
392+
393+ void testRefreshAfterDeletedDesktopFiles() {
394+ QFETCH(bool, deleted);
395+
396+ // pin both apps
397+ launcherModel->pin("abs-icon");
398+ launcherModel->pin("no-icon");
399+ // close both apps
400+ appManager->removeApplication(0);
401+ appManager->removeApplication(0);
402+
403+ // "delete" the .desktop files
404+ QString oldCurrent = QDir::currentPath();
405+ if (deleted) {
406+ // In testing mode, the launcher searches the current dir for the sample .desktop file
407+ // We can make that fail by changing the current dir
408+ QDir::setCurrent("..");
409+ }
410+
411+ // Call refresh
375412 QDBusInterface interface("com.canonical.Unity.Launcher", "/com/canonical/Unity/Launcher", "com.canonical.Unity.Launcher");
376413 QDBusReply<void> reply = interface.call("Refresh");
377414
378415 // Make sure the call to Refresh returned without error.
379416 QCOMPARE(reply.isValid(), true);
417+
418+ QCOMPARE(launcherModel->rowCount(), deleted ? 0 : 2);
419+
420+ // Restoring current dir
421+ QDir::setCurrent(oldCurrent);
422+ }
423+
424+ void testSettings() {
425+ GSettings *settings = launcherModel->m_settings;
426+
427+ // Nothing pinned at startup
428+ QCOMPARE(settings->storedApplications().count(), 0);
429+
430+ // pin both apps
431+ launcherModel->pin("abs-icon");
432+ launcherModel->pin("no-icon");
433+
434+ // Now settings should have 2 apps
435+ QCOMPARE(settings->storedApplications().count(), 2);
436+
437+ // close both apps
438+ appManager->removeApplication(0);
439+ appManager->removeApplication(0);
440+
441+ // Now settings should have 2 apps
442+ QCOMPARE(settings->storedApplications().count(), 2);
443+
444+ // Now remove 1 app through the backend, make sure one is still there
445+ settings->simulateDConfChanged(QStringList() << "abs-icon");
446+ QCOMPARE(settings->storedApplications().count(), 1);
447+
448+ // Check if it disappeared from the frontend too
449+ QCOMPARE(launcherModel->rowCount(), 1);
450+
451+ // Add them back but in reverse order
452+ settings->simulateDConfChanged(QStringList() << "no-icon" << "abs-icon");
453+ QCOMPARE(launcherModel->rowCount(), 2);
454+ QCOMPARE(launcherModel->get(0)->appId(), QString("no-icon"));
455+ QCOMPARE(launcherModel->get(1)->appId(), QString("abs-icon"));
380456 }
381457};
382458