on 690519c help sign in
unity8/tests/plugins/Unity/Launcher/launcherbackendtest.cpp
217 lines 8.0 kB history raw
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 * Michael Terry <michael.terry@canonical.com>
18 */
20#include "launcherbackend.h"
22#include <QtTest>
23#include <QDBusConnection>
24#include <QDBusMessage>
25#include <QDBusVariant>
28class LauncherBackendTest : public QObject
30 Q_OBJECT
32private Q_SLOTS:
34 void initTestCase()
35 {
36 // As we want to test absolute file paths in the .desktop file, we need to modify the
37 // sample file and replace the path in there with the current build dir.
38 QSettings settings(QDir::currentPath() + "/click-icon.desktop", QSettings::IniFormat);
39 settings.setValue("Desktop Entry/Path", QDir::currentPath());
40 }
42 void testFileNames()
43 {
44 LauncherBackend backend;
46 backend.setStoredApplications(QStringList() << "rel-icon" << "abs-icon" << "invalid");
47 QCOMPARE(backend.storedApplications(), QStringList() << "rel-icon" << "abs-icon");
48 }
50 void testIcon_data() {
51 QTest::addColumn<QString>("appId");
52 QTest::addColumn<QString>("expectedIcon");
54 // Needs to expand a relative icon to the absolute path
55 QTest::newRow("relative icon path") << "rel-icon" << QDir::currentPath() + "/rel-icon.svg";
57 // In case an icon is not found on disk, it needs to fallback on image://theme/ for it
58 QTest::newRow("fallback on theme") << "abs-icon" << "image://theme//path/to/icon.png";
60 // Click packages have a relative icon path but an absolute path as a separate entry
61 QTest::newRow("click package icon") << "click-icon" << QDir::currentPath() + "/click-icon.svg";
62 }
64 void testIcon() {
65 QFETCH(QString, appId);
66 QFETCH(QString, expectedIcon);
68 LauncherBackend backend;
69 backend.setStoredApplications(QStringList() << appId);
71 QCOMPARE(backend.icon(appId), expectedIcon);
72 }
74 void testCount_data() {
75 QTest::addColumn<QString>("appId");
76 QTest::addColumn<bool>("setCount");
77 QTest::addColumn<int>("inCount");
78 QTest::addColumn<bool>("countVisible");
79 QTest::addColumn<int>("expectedCount");
81 /* Get baseline data on things working */
82 QTest::newRow("Baseline") << "rel-icon" << false << 0 << false << -1;
84 /* Valid count, but not visible */
85 QTest::newRow("Not visible") << "rel-icon" << true << 42 << false << -1;
87 /* Turn it on */
88 QTest::newRow("Visible Count") << "rel-icon" << true << 42 << true << 42;
90 /* Invalide app to load */
91 QTest::newRow("Invalid App ID") << "this-app-doesnt-exist" << true << 42 << true << -1;
92 }
94 void testCount() {
95 QFETCH(QString, appId);
96 QFETCH(bool, setCount);
97 QFETCH(int, inCount);
98 QFETCH(bool, countVisible);
99 QFETCH(int, expectedCount);
101 LauncherBackend backend;
103 if (setCount)
104 backend.setCount(appId, inCount);
105 backend.setCountVisible(appId, countVisible);
107 QCOMPARE(backend.count(appId), expectedCount);
108 }
110 void testDbusName_data() {
111 QTest::addColumn<QString>("decoded");
112 QTest::addColumn<QString>("encoded");
114 /* Passthrough test */
115 QTest::newRow("Passthrough") << "fine" << "fine";
117 /* Number as first characeter */
118 QTest::newRow("Number first") << "31337" << "_331337";
120 /* Underscore test */
121 QTest::newRow("Underscore test") << "this_is_c_style_namespacing" << "this_5Fis_5Fc_5Fstyle_5Fnamespacing";
123 /* Hyphen test */
124 QTest::newRow("Hyphen test") << "typical-application" << "typical_2Dapplication";
126 /* Japanese test */
127 QTest::newRow("日本語 test") << "日本語" << "_E6_97_A5_E6_9C_AC_E8_AA_9E";
128 }
130 void testDbusName() {
131 QFETCH(QString, decoded);
132 QFETCH(QString, encoded);
134 QString encodeOut = LauncherBackend::encodeAppId(decoded);
135 QCOMPARE(encoded, encodeOut);
137 QString decodeOut = LauncherBackend::decodeAppId(encoded);
138 QCOMPARE(decoded, decodeOut);
139 }
141 void testDbusIface_data() {
142 QTest::addColumn<QString>("appId");
143 QTest::addColumn<bool>("setCount");
144 QTest::addColumn<int>("inCount");
145 QTest::addColumn<bool>("countVisible");
146 QTest::addColumn<int>("expectedCount");
148 /* Get baseline data on things working */
149 QTest::newRow("Baseline") << "rel-icon" << false << 0 << false << -1;
151 /* Turn it on */
152 QTest::newRow("Visible Count") << "rel-icon" << true << 42 << true << 42;
154 /* Invalide app to load */
155 QTest::newRow("Invalid App ID") << "this-app-doesnt-exist" << true << 42 << true << -1;
156 }
158 void testDbusIface() {
159 QFETCH(QString, appId);
160 QFETCH(bool, setCount);
161 QFETCH(int, inCount);
162 QFETCH(bool, countVisible);
163 QFETCH(int, expectedCount);
165 QDBusConnection con = QDBusConnection::sessionBus();
166 QDBusMessage message;
167 QDBusMessage reply;
169 LauncherBackend backend;
171 if (setCount) {
172 message = QDBusMessage::createMethodCall("com.canonical.unity.launcher",
173 "/com/canonical/unity/launcher/" + LauncherBackend::encodeAppId(appId),
174 "org.freedesktop.DBus.Properties",
175 "Set");
176 QVariantList cargs;
177 cargs.append(QString("com.canonical.unity.Launcher.Item"));
178 cargs.append(QString("count"));
179 cargs.append(QVariant::fromValue(QDBusVariant(inCount)));
181 message.setArguments(cargs);
182 reply = con.call(message);
183 QCOMPARE(reply.type(), QDBusMessage::ReplyMessage);
184 }
186 /* Set countVisible */
187 message = QDBusMessage::createMethodCall("com.canonical.unity.launcher",
188 "/com/canonical/unity/launcher/" + LauncherBackend::encodeAppId(appId),
189 "org.freedesktop.DBus.Properties",
190 "Set");
191 QVariantList cvargs;
192 cvargs.append(QString("com.canonical.unity.Launcher.Item"));
193 cvargs.append(QString("countVisible"));
194 cvargs.append(QVariant::fromValue(QDBusVariant(countVisible)));
196 message.setArguments(cvargs);
197 reply = con.call(message);
198 QCOMPARE(reply.type(), QDBusMessage::ReplyMessage);
200 /* Get value */
201 message = QDBusMessage::createMethodCall("com.canonical.unity.launcher",
202 "/com/canonical/unity/launcher/" + LauncherBackend::encodeAppId(appId),
203 "org.freedesktop.DBus.Properties",
204 "Get");
205 QVariantList getargs;
206 getargs.append(QString("com.canonical.unity.Launcher.Item"));
207 getargs.append(QString("count"));
209 message.setArguments(getargs);
210 reply = con.call(message);
211 QCOMPARE(reply.type(), QDBusMessage::ReplyMessage);
212 QCOMPARE(reply.arguments()[0].value<QDBusVariant>().variant().toInt(), expectedCount);
213 }
214};
216QTEST_GUILESS_MAIN(LauncherBackendTest)
217#include "launcherbackendtest.moc"