on xenial help sign in
Add tests for launcher backend
0bac488Michael Terry
plugins/Unity/Launcher/backend/launcherbackend.cpp+29−13
@@ -17,15 +17,19 @@
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
18 */
19
19
20+#include "AccountsService.h"
20#include "launcherbackend.h"
21#include "launcherbackend.h"
21
22
22#include <QDir>
23#include <QDir>
23#include <QFileInfo>
24#include <QFileInfo>
24
25
25−LauncherBackend::LauncherBackend(QObject *parent):
26+LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
26− QObject(parent)
27+ QObject(parent),
28+ m_accounts(nullptr)
27{
29{
30+ if (useStorage) {
28− m_accounts = new AccountsService(this);
31+ m_accounts = new AccountsService(this);
32+ }
29 setUser(qgetenv("USER"));
33 setUser(qgetenv("USER"));
30}
34}
31
35
@@ -55,11 +59,12 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
55{
59{
56 // Are we dropping any pinned apps?
60 // Are we dropping any pinned apps?
57 auto needToSync = false;
61 auto needToSync = false;
62+ auto pinnedItems = QStringList();
58 for (int i = 0; i < m_storedApps.size(); i++) {
63 for (int i = 0; i < m_storedApps.size(); i++) {
59 if (m_storedApps[i].pinned) {
64 if (m_storedApps[i].pinned) {
60 auto found = false;
65 auto found = false;
61 for (int j = 0; j < appIds.size(); j++) {
66 for (int j = 0; j < appIds.size(); j++) {
62− auto fullAppId = desktopFile(appIds[j]);
67+ auto fullAppId = resolveAppId(appIds[j]);
63 if (m_storedApps[i].settings->fileName() == fullAppId) {
68 if (m_storedApps[i].settings->fileName() == fullAppId) {
64 found = true;
69 found = true;
65 break;
70 break;
@@ -67,15 +72,15 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
67 }
72 }
68 if (!found) {
73 if (!found) {
69 needToSync = true;
74 needToSync = true;
70− break;
71 }
75 }
76+ pinnedItems.append(m_storedApps[i].settings->fileName());
72 }
77 }
73 }
78 }
74
79
75 clearItems();
80 clearItems();
76
81
77 for (auto appId: appIds) {
82 for (auto appId: appIds) {
78− loadDesktopFile(appId);
83+ loadDesktopFile(appId, pinnedItems.contains(resolveAppId(appId)));
79 }
84 }
80
85
81 if (needToSync) {
86 if (needToSync) {
@@ -85,8 +90,12 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
85
90
86QString LauncherBackend::desktopFile(const QString &appId) const
91QString LauncherBackend::desktopFile(const QString &appId) const
87{
92{
88− auto fileInfo = QFileInfo(QDir("/usr/share/applications"), appId);
93+ auto index = findItem(appId);
89− return fileInfo.canonicalFilePath();
94+ if (index < 0) {
95+ return "";
96+ } else {
97+ return resolveAppId(appId);
98+ }
90}
99}
91
100
92QString LauncherBackend::displayName(const QString &appId) const
101QString LauncherBackend::displayName(const QString &appId) const
@@ -174,11 +183,17 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
174 Q_UNUSED(quickListId)
183 Q_UNUSED(quickListId)
175}
184}
176
185
186+QString LauncherBackend::resolveAppId(const QString &appId) const
187+{
188+ QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
189+ return fileInfo.absoluteFilePath();
190+}
191+
177void LauncherBackend::syncFromAccounts()
192void LauncherBackend::syncFromAccounts()
178{
193{
179 auto appIds = QStringList();
194 auto appIds = QStringList();
180
195
181− if (m_user != "") {
196+ if (m_user != "" && m_accounts != nullptr) {
182 appIds = m_accounts->getUserProperty(m_user, "launcher-items").toStringList();
197 appIds = m_accounts->getUserProperty(m_user, "launcher-items").toStringList();
183 }
198 }
184
199
@@ -214,23 +229,24 @@ void LauncherBackend::syncToAccounts()
214 }
229 }
215 }
230 }
216
231
217− if (m_user != "") {
232+ if (m_user != "" && m_accounts != nullptr) {
218 m_accounts->setUserProperty(m_user, "launcher-items", QVariant(pinnedItems));
233 m_accounts->setUserProperty(m_user, "launcher-items", QVariant(pinnedItems));
219 }
234 }
220}
235}
221
236
222−bool LauncherBackend::loadDesktopFile(const QString &appId)
237+bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
223{
238{
224 auto item = LauncherBackendItem();
239 auto item = LauncherBackendItem();
225− auto fullAppId = desktopFile(appId);
240+ auto fullAppId = resolveAppId(appId);
226 item.settings = new QSettings(fullAppId, QSettings::IniFormat);
241 item.settings = new QSettings(fullAppId, QSettings::IniFormat);
242+ item.pinned = isPinned;
227 m_storedApps.append(item);
243 m_storedApps.append(item);
228 return true; // QSettings doesn't really indicate a failure mode right now
244 return true; // QSettings doesn't really indicate a failure mode right now
229}
245}
230
246
231int LauncherBackend::findItem(const QString &appId) const
247int LauncherBackend::findItem(const QString &appId) const
232{
248{
233− auto fullAppId = desktopFile(appId);
249+ auto fullAppId = resolveAppId(appId);
234 for (int i = 0; i < m_storedApps.size(); i++) {
250 for (int i = 0; i < m_storedApps.size(); i++) {
235 if (m_storedApps[i].settings->fileName() == fullAppId) {
251 if (m_storedApps[i].settings->fileName() == fullAppId) {
236 return i;
252 return i;
plugins/Unity/Launcher/backend/launcherbackend.h+4−3
@@ -19,13 +19,14 @@
19#ifndef LAUNCHERBACKEND_H
19#ifndef LAUNCHERBACKEND_H
20#define LAUNCHERBACKEND_H
20#define LAUNCHERBACKEND_H
21
21
22−#include "AccountsService.h"
23#include "common/quicklistentry.h"
22#include "common/quicklistentry.h"
24
23
25#include <QObject>
24#include <QObject>
26#include <QSettings>
25#include <QSettings>
27#include <QStringList>
26#include <QStringList>
28
27
28+class AccountsService;
29+
29/**
30/**
30 * @brief An interface that provides all the data needed by the launcher.
31 * @brief An interface that provides all the data needed by the launcher.
31 */
32 */
@@ -36,7 +37,7 @@ class LauncherBackend : public QObject
36
37
37
38
38public:
39public:
39− LauncherBackend(QObject *parent = 0);
40+ LauncherBackend(bool useStorage = true, QObject *parent = 0);
40 virtual ~LauncherBackend();
41 virtual ~LauncherBackend();
41
42
42 /**
43 /**
@@ -132,7 +133,7 @@ Q_SIGNALS:
132
133
133private:
134private:
134 QString resolveAppId(const QString &appId) const;
135 QString resolveAppId(const QString &appId) const;
135− bool loadDesktopFile(const QString &appId);
136+ bool loadDesktopFile(const QString &appId, bool isPinned);
136 int findItem(const QString &appId) const;
137 int findItem(const QString &appId) const;
137 void syncFromAccounts();
138 void syncFromAccounts();
138 void syncToAccounts();
139 void syncToAccounts();
plugins/Unity/Launcher/launchermodel.cpp+1−1
@@ -23,7 +23,7 @@
23
23
24LauncherModel::LauncherModel(QObject *parent):
24LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
25 LauncherModelInterface(parent),
26− m_backend(new LauncherBackend(this))
26+ m_backend(new LauncherBackend(true, this))
27{
27{
28 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
28 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
29 LauncherItem *item = new LauncherItem(entry,
29 LauncherItem *item = new LauncherItem(entry,
plugins/Unity/Launcher/launchermodel.h+1−1
@@ -17,7 +17,7 @@
17 * Michael Zanetti <michael.zanetti@canonical.com>
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
18 */
19
19
20−#ifndef LAUNCHERMODELH
20+#ifndef LAUNCHERMODEL_H
21#define LAUNCHERMODEL_H
21#define LAUNCHERMODEL_H
22
22
23// unity-api
23// unity-api
tests/plugins/Unity/CMakeLists.txt+1
@@ -1,4 +1,5 @@
1add_subdirectory(Indicators)
1add_subdirectory(Indicators)
2+add_subdirectory(Launcher)
2
3
3pkg_check_modules(UNITYCORE REQUIRED unity-core-6.0)
4pkg_check_modules(UNITYCORE REQUIRED unity-core-6.0)
4pkg_check_modules(LIBUNITYPROTO REQUIRED unity-protocol-private)
5pkg_check_modules(LIBUNITYPROTO REQUIRED unity-protocol-private)
tests/plugins/Unity/Launcher/CMakeLists.txtnew file+22
@@ -0,0 +1,22 @@
1+include_directories(
2+ ${CMAKE_CURRENT_BINARY_DIR}
3+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService
4+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher
5+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend
6+ )
7+
8+add_definitions(-DSM_BUSNAME=sessionBus)
9+add_definitions(-DSRCDIR="${CMAKE_CURRENT_SOURCE_DIR}")
10+
11+set(testCommand ${CMAKE_CURRENT_BINARY_DIR}/launcherbackendtestExec
12+ -o ${CMAKE_BINARY_DIR}/launcherbackendtest.xml,xunitxml
13+ -o -,txt)
14+add_test(NAME launcherbackendtest COMMAND ${testCommand})
15+add_custom_target(launcherbackendtest ${testCommand})
16+add_executable(launcherbackendtestExec
17+ launcherbackendtest.cpp
18+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService/AccountsService.cpp
19+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/backend/launcherbackend.cpp
20+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Launcher/common/quicklistentry.cpp
21+ )
22+qt5_use_modules(launcherbackendtestExec Test Core DBus)
tests/plugins/Unity/Launcher/abs-icon.desktopnew file+3
@@ -0,0 +1,3 @@
1+[Desktop Entry]
2+Name=Absolute Icon
3+Icon=/path/to/icon.png
tests/plugins/Unity/Launcher/launcherbackendtest.cppnew file+100
@@ -0,0 +1,100 @@
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+ * Nick Dedekind <nick.dedekind@canonical.com>
18+ */
19+
20+#include "launcherbackend.h"
21+
22+#include <QtTest>
23+#include <QDebug>
24+
25+class LauncherBackendTest : public QObject
26+{
27+ Q_OBJECT
28+
29+private Q_SLOTS:
30+ void testFileNames()
31+ {
32+ LauncherBackend backend(false);
33+
34+ auto apps = QStringList() << "one.desktop" << "two" << "/full";
35+ backend.setStoredApplications(apps);
36+
37+ QCOMPARE(backend.desktopFile("one.desktop"), QString("/usr/share/applications/one.desktop"));
38+ QCOMPARE(backend.desktopFile("two"), QString("/usr/share/applications/two"));
39+ QCOMPARE(backend.desktopFile("/full"), QString("/full"));
40+ QCOMPARE(backend.storedApplications(), QStringList() << "/usr/share/applications/one.desktop" << "/usr/share/applications/two" << "/full");
41+
42+ QCOMPARE(backend.desktopFile("full"), QString(""));
43+
44+ QCOMPARE(backend.desktopFile("/usr/share/applications/one.desktop"), QString("/usr/share/applications/one.desktop"));
45+ }
46+
47+ void testDesktopReading()
48+ {
49+ LauncherBackend backend(false);
50+
51+ auto apps = QStringList() << SRCDIR "/rel-icon.desktop" << SRCDIR "/abs-icon.desktop" << SRCDIR "/no-icon.desktop" << SRCDIR "/no-name.desktop" << SRCDIR "/no-exist.desktop";
52+ backend.setStoredApplications(apps);
53+
54+ QCOMPARE(backend.displayName(SRCDIR "/rel-icon.desktop"), QString("Relative Icon"));
55+ QCOMPARE(backend.icon(SRCDIR "/rel-icon.desktop"), QString("image://gicon/rel-icon"));
56+
57+ QCOMPARE(backend.displayName(SRCDIR "/abs-icon.desktop"), QString("Absolute Icon"));
58+ QCOMPARE(backend.icon(SRCDIR "/abs-icon.desktop"), QString("/path/to/icon.png"));
59+
60+ QCOMPARE(backend.displayName(SRCDIR "/no-icon.desktop"), QString("No Icon"));
61+ QCOMPARE(backend.icon(SRCDIR "/no-icon.desktop"), QString(""));
62+
63+ QCOMPARE(backend.displayName(SRCDIR "/no-name.desktop"), QString(""));
64+ QCOMPARE(backend.icon(SRCDIR "/no-name.desktop"), QString("image://gicon/no-name"));
65+
66+ QCOMPARE(backend.displayName(SRCDIR "/no-exist.desktop"), QString(""));
67+ QCOMPARE(backend.icon(SRCDIR "/no-exist.desktop"), QString(""));
68+ }
69+
70+ void testPinning()
71+ {
72+ LauncherBackend backend(false);
73+
74+ // Confirm that default entries are all pinned
75+ auto defaultApps = backend.storedApplications();
76+ QVERIFY(defaultApps.size() > 0);
77+ for (auto app: defaultApps) {
78+ QCOMPARE(backend.isPinned(app), true);
79+ }
80+
81+ backend.setStoredApplications(QStringList() << "one" << "two");
82+ QCOMPARE(backend.isPinned("one"), false);
83+ QCOMPARE(backend.isPinned("two"), false);
84+
85+ backend.setPinned("two", true);
86+ QCOMPARE(backend.isPinned("two"), true);
87+
88+ backend.setStoredApplications(QStringList() << "one" << "two" << "three");
89+ QCOMPARE(backend.isPinned("two"), true);
90+ QCOMPARE(backend.isPinned("three"), false);
91+
92+ backend.setPinned("three", true);
93+ backend.setStoredApplications(QStringList() << "one" << "two");
94+ QCOMPARE(backend.isPinned("two"), true);
95+ QCOMPARE(backend.isPinned("three"), false); // doesn't exist anymore!
96+ }
97+};
98+
99+QTEST_GUILESS_MAIN(LauncherBackendTest)
100+#include "launcherbackendtest.moc"
tests/plugins/Unity/Launcher/no-icon.desktopnew file+2
@@ -0,0 +1,2 @@
1+[Desktop Entry]
2+Name=No Icon
tests/plugins/Unity/Launcher/no-name.desktopnew file+2
@@ -0,0 +1,2 @@
1+[Desktop Entry]
2+Icon=no-name
tests/plugins/Unity/Launcher/rel-icon.desktopnew file+3
@@ -0,0 +1,3 @@
1+[Desktop Entry]
2+Name=Relative Icon
3+Icon=rel-icon