on xenial help sign in
Add tests for launcher backend
commit0bac48847cd24420b959c042950b4993cbf16647 authorMichael Terry <michael.terry@canonical.com> dateTue 20 Aug 2013, 13:57 (13 years ago) parent98a09ce
11 files changed+168−18
plugins/Unity/Launcher/backend/launcherbackend.cpp+29−13 plugins/Unity/Launcher/backend/launcherbackend.h+4−3 plugins/Unity/Launcher/launchermodel.cpp+1−1 plugins/Unity/Launcher/launchermodel.h+1−1 tests/plugins/Unity/CMakeLists.txt+1 tests/plugins/Unity/Launcher/CMakeLists.txt+22 tests/plugins/Unity/Launcher/abs-icon.desktop+3 tests/plugins/Unity/Launcher/launcherbackendtest.cpp+100 tests/plugins/Unity/Launcher/no-icon.desktop+2 tests/plugins/Unity/Launcher/no-name.desktop+2 tests/plugins/Unity/Launcher/rel-icon.desktop+3
plugins/Unity/Launcher/backend/launcherbackend.cpp+29−13
@@ -17,15 +17,19 @@
1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20+#include "AccountsService.h"
2021#include "launcherbackend.h"
2122
2223#include <QDir>
2324#include <QFileInfo>
2425
25−LauncherBackend::LauncherBackend(QObject *parent):
26− QObject(parent)
26+LauncherBackend::LauncherBackend(bool useStorage, QObject *parent):
27+ QObject(parent),
28+ m_accounts(nullptr)
2729{
28− m_accounts = new AccountsService(this);
30+ if (useStorage) {
31+ m_accounts = new AccountsService(this);
32+ }
2933 setUser(qgetenv("USER"));
3034}
3135
@@ -55,11 +59,12 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
5559{
5660 // Are we dropping any pinned apps?
5761 auto needToSync = false;
62+ auto pinnedItems = QStringList();
5863 for (int i = 0; i < m_storedApps.size(); i++) {
5964 if (m_storedApps[i].pinned) {
6065 auto found = false;
6166 for (int j = 0; j < appIds.size(); j++) {
62− auto fullAppId = desktopFile(appIds[j]);
67+ auto fullAppId = resolveAppId(appIds[j]);
6368 if (m_storedApps[i].settings->fileName() == fullAppId) {
6469 found = true;
6570 break;
@@ -67,15 +72,15 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
6772 }
6873 if (!found) {
6974 needToSync = true;
70− break;
7175 }
76+ pinnedItems.append(m_storedApps[i].settings->fileName());
7277 }
7378 }
7479
7580 clearItems();
7681
7782 for (auto appId: appIds) {
78− loadDesktopFile(appId);
83+ loadDesktopFile(appId, pinnedItems.contains(resolveAppId(appId)));
7984 }
8085
8186 if (needToSync) {
@@ -85,8 +90,12 @@ void LauncherBackend::setStoredApplications(const QStringList &appIds)
8590
8691QString LauncherBackend::desktopFile(const QString &appId) const
8792{
88− auto fileInfo = QFileInfo(QDir("/usr/share/applications"), appId);
89− return fileInfo.canonicalFilePath();
93+ auto index = findItem(appId);
94+ if (index < 0) {
95+ return "";
96+ } else {
97+ return resolveAppId(appId);
98+ }
9099}
91100
92101QString LauncherBackend::displayName(const QString &appId) const
@@ -174,11 +183,17 @@ void LauncherBackend::triggerQuickListAction(const QString &appId, const QString
174183 Q_UNUSED(quickListId)
175184}
176185
186+QString LauncherBackend::resolveAppId(const QString &appId) const
187+{
188+ QFileInfo fileInfo(QDir("/usr/share/applications"), appId);
189+ return fileInfo.absoluteFilePath();
190+}
191+
177192void LauncherBackend::syncFromAccounts()
178193{
179194 auto appIds = QStringList();
180195
181− if (m_user != "") {
196+ if (m_user != "" && m_accounts != nullptr) {
182197 appIds = m_accounts->getUserProperty(m_user, "launcher-items").toStringList();
183198 }
184199
@@ -214,23 +229,24 @@ void LauncherBackend::syncToAccounts()
214229 }
215230 }
216231
217− if (m_user != "") {
232+ if (m_user != "" && m_accounts != nullptr) {
218233 m_accounts->setUserProperty(m_user, "launcher-items", QVariant(pinnedItems));
219234 }
220235}
221236
222−bool LauncherBackend::loadDesktopFile(const QString &appId)
237+bool LauncherBackend::loadDesktopFile(const QString &appId, bool isPinned)
223238{
224239 auto item = LauncherBackendItem();
225− auto fullAppId = desktopFile(appId);
240+ auto fullAppId = resolveAppId(appId);
226241 item.settings = new QSettings(fullAppId, QSettings::IniFormat);
242+ item.pinned = isPinned;
227243 m_storedApps.append(item);
228244 return true; // QSettings doesn't really indicate a failure mode right now
229245}
230246
231247int LauncherBackend::findItem(const QString &appId) const
232248{
233− auto fullAppId = desktopFile(appId);
249+ auto fullAppId = resolveAppId(appId);
234250 for (int i = 0; i < m_storedApps.size(); i++) {
235251 if (m_storedApps[i].settings->fileName() == fullAppId) {
236252 return i;
plugins/Unity/Launcher/backend/launcherbackend.h+4−3
@@ -19,13 +19,14 @@
1919#ifndef LAUNCHERBACKEND_H
2020#define LAUNCHERBACKEND_H
2121
22−#include "AccountsService.h"
2322#include "common/quicklistentry.h"
2423
2524#include <QObject>
2625#include <QSettings>
2726#include <QStringList>
2827
28+class AccountsService;
29+
2930/**
3031 * @brief An interface that provides all the data needed by the launcher.
3132 */
@@ -36,7 +37,7 @@ class LauncherBackend : public QObject
3637
3738
3839public:
39− LauncherBackend(QObject *parent = 0);
40+ LauncherBackend(bool useStorage = true, QObject *parent = 0);
4041 virtual ~LauncherBackend();
4142
4243 /**
@@ -132,7 +133,7 @@ Q_SIGNALS:
132133
133134private:
134135 QString resolveAppId(const QString &appId) const;
135− bool loadDesktopFile(const QString &appId);
136+ bool loadDesktopFile(const QString &appId, bool isPinned);
136137 int findItem(const QString &appId) const;
137138 void syncFromAccounts();
138139 void syncToAccounts();
plugins/Unity/Launcher/launchermodel.cpp+1−1
@@ -23,7 +23,7 @@
2323
2424LauncherModel::LauncherModel(QObject *parent):
2525 LauncherModelInterface(parent),
26− m_backend(new LauncherBackend(this))
26+ m_backend(new LauncherBackend(true, this))
2727{
2828 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
2929 LauncherItem *item = new LauncherItem(entry,
plugins/Unity/Launcher/launchermodel.h+1−1
@@ -17,7 +17,7 @@
1717 * Michael Zanetti <michael.zanetti@canonical.com>
1818 */
1919
20−#ifndef LAUNCHERMODELH
20+#ifndef LAUNCHERMODEL_H
2121#define LAUNCHERMODEL_H
2222
2323// unity-api
tests/plugins/Unity/CMakeLists.txt+1
@@ -1,4 +1,5 @@
11add_subdirectory(Indicators)
2+add_subdirectory(Launcher)
23
34pkg_check_modules(UNITYCORE REQUIRED unity-core-6.0)
45pkg_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