Changeset View
Changeset View
Standalone View
Standalone View
libtaskmanager/autotests/tasktoolstest.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2016 Eike Hein <hein@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | | ||||
21 | #include <QObject> | ||||
22 | | ||||
23 | #include <KConfigGroup> | ||||
24 | #include <KDesktopFile> | ||||
25 | #include <KIconTheme> | ||||
26 | #include <KService> | ||||
27 | #include <KSharedConfig> | ||||
28 | #include <KSycoca> | ||||
29 | | ||||
30 | #include <QDir> | ||||
31 | #include <QIcon> | ||||
32 | #include <QStandardPaths> | ||||
33 | #include <QTemporaryDir> | ||||
34 | #include <QTest> | ||||
35 | | ||||
36 | #include "tasktools.h" | ||||
37 | | ||||
38 | // Taken from tst_qstandardpaths. | ||||
39 | #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_BLACKBERRY) && !defined(Q_OS_ANDROID) | ||||
40 | #define Q_XDG_PLATFORM | ||||
41 | #endif | ||||
42 | | ||||
43 | using namespace TaskManager; | ||||
44 | | ||||
45 | class TaskToolsTest : public QObject | ||||
46 | { | ||||
47 | Q_OBJECT | ||||
48 | | ||||
49 | private Q_SLOTS: | ||||
50 | void initTestCase(); | ||||
51 | void cleanupTestCase(); | ||||
52 | | ||||
53 | void shouldFindApp(); | ||||
54 | void shouldFindDefaultApp(); | ||||
55 | void shouldCompareLauncherUrls(); | ||||
56 | | ||||
57 | private: | ||||
58 | QString appLinkPath(); | ||||
59 | void fillReferenceAppData(); | ||||
60 | void createAppLink(); | ||||
61 | void createIcon(); | ||||
62 | | ||||
63 | AppData m_referenceAppData; | ||||
64 | QTemporaryDir m_tempDir; | ||||
65 | }; | ||||
66 | | ||||
67 | void TaskToolsTest::initTestCase() | ||||
68 | { | ||||
69 | QStandardPaths::enableTestMode(true); | ||||
70 | | ||||
71 | QVERIFY(m_tempDir.isValid()); | ||||
72 | QVERIFY(QDir().mkpath(m_tempDir.path() + QLatin1String("/config"))); | ||||
73 | QVERIFY(QDir().mkpath(m_tempDir.path() + QLatin1String("/cache"))); | ||||
74 | QVERIFY(QDir().mkpath(m_tempDir.path() + QLatin1String("/data/applications"))); | ||||
75 | | ||||
76 | #ifdef Q_XDG_PLATFORM | ||||
77 | qputenv("XDG_CONFIG_HOME", QFile::encodeName(m_tempDir.path() + QLatin1String("/config"))); | ||||
78 | qputenv("XDG_CACHE_HOME", QFile::encodeName(m_tempDir.path() + QLatin1String("/cache"))); | ||||
79 | qputenv("XDG_DATA_DIRS", QFile::encodeName(m_tempDir.path() + QLatin1String("/data"))); | ||||
80 | #else | ||||
81 | QSKIP("This test requires XDG."); | ||||
82 | #endif | ||||
83 | | ||||
84 | createIcon(); | ||||
85 | fillReferenceAppData(); | ||||
86 | createAppLink(); | ||||
87 | | ||||
88 | QFile::remove(KSycoca::absoluteFilePath()); | ||||
89 | KSycoca::self()->ensureCacheValid(); | ||||
90 | QVERIFY(QFile::exists(KSycoca::absoluteFilePath())); | ||||
91 | } | ||||
92 | | ||||
93 | void TaskToolsTest::cleanupTestCase() | ||||
94 | { | ||||
95 | QFile::remove(KSycoca::absoluteFilePath()); | ||||
96 | } | ||||
97 | | ||||
98 | void TaskToolsTest::shouldFindApp() | ||||
99 | { | ||||
100 | // FIXME Test icon. | ||||
101 | | ||||
102 | const AppData &data = appDataFromUrl(QUrl::fromLocalFile(appLinkPath())); | ||||
103 | | ||||
104 | QCOMPARE(data.id, m_referenceAppData.id); | ||||
105 | QCOMPARE(data.name, m_referenceAppData.name); | ||||
106 | QCOMPARE(data.genericName, m_referenceAppData.genericName); | ||||
107 | QCOMPARE(data.url, m_referenceAppData.url); | ||||
108 | } | ||||
109 | | ||||
110 | void TaskToolsTest::shouldFindDefaultApp() | ||||
111 | { | ||||
112 | // FIXME Test other recognized default app types. | ||||
113 | | ||||
114 | KConfigGroup config(KSharedConfig::openConfig(), "General"); | ||||
115 | config.writePathEntry("BrowserApplication", QLatin1String("konqueror")); | ||||
116 | | ||||
117 | QVERIFY(defaultApplication(QUrl("wrong://url")).isEmpty()); | ||||
118 | QCOMPARE(defaultApplication(QUrl("preferred://browser")), QLatin1String("konqueror")); | ||||
119 | } | ||||
120 | | ||||
121 | void TaskToolsTest::shouldCompareLauncherUrls() | ||||
122 | { | ||||
123 | QUrl a(QLatin1String("file:///usr/share/applications/org.kde.dolphin.desktop")); | ||||
124 | QUrl b(QLatin1String("file:///usr/share/applications/org.kde.konsole.desktop")); | ||||
125 | QUrl c(QLatin1String("file:///usr/share/applications/org.kde.dolphin.desktop?iconData=foo")); | ||||
126 | QUrl d(QLatin1String("file:///usr/share/applications/org.kde.konsole.desktop?iconData=bar")); | ||||
127 | | ||||
128 | QVERIFY(launcherUrlsMatch(QUrl(a), QUrl(a))); | ||||
129 | QVERIFY(launcherUrlsMatch(QUrl(a), QUrl(a), Strict)); | ||||
130 | QVERIFY(launcherUrlsMatch(QUrl(a), QUrl(a), IgnoreQueryItems));; | ||||
131 | | ||||
132 | QVERIFY(!launcherUrlsMatch(QUrl(a), QUrl(b))); | ||||
133 | QVERIFY(!launcherUrlsMatch(QUrl(a), QUrl(b), Strict)); | ||||
134 | QVERIFY(!launcherUrlsMatch(QUrl(a), QUrl(b), IgnoreQueryItems)); | ||||
135 | | ||||
136 | QVERIFY(!launcherUrlsMatch(QUrl(b), QUrl(c), Strict)); | ||||
137 | QVERIFY(!launcherUrlsMatch(QUrl(c), QUrl(d), Strict)); | ||||
138 | | ||||
139 | QVERIFY(launcherUrlsMatch(QUrl(a), QUrl(c), IgnoreQueryItems)); | ||||
140 | QVERIFY(!launcherUrlsMatch(QUrl(c), QUrl(d), IgnoreQueryItems)); | ||||
141 | } | ||||
142 | | ||||
143 | QString TaskToolsTest::appLinkPath() | ||||
144 | { | ||||
145 | return QString(m_tempDir.path() + QLatin1String("/data/applications/org.kde.konversation.desktop")); | ||||
146 | } | ||||
147 | | ||||
148 | void TaskToolsTest::fillReferenceAppData() | ||||
149 | { | ||||
150 | // FIXME Add icon. | ||||
151 | | ||||
152 | m_referenceAppData.id = QLatin1String("org.kde.konversation"); | ||||
153 | m_referenceAppData.name = QLatin1String("Konversation"); | ||||
154 | m_referenceAppData.genericName = QLatin1String("IRC Client"); | ||||
155 | m_referenceAppData.url = QUrl::fromLocalFile(appLinkPath()); | ||||
156 | } | ||||
157 | | ||||
158 | void TaskToolsTest::createAppLink() | ||||
159 | { | ||||
160 | KDesktopFile file(appLinkPath()); | ||||
161 | KConfigGroup group = file.desktopGroup(); | ||||
162 | group.writeEntry(QLatin1String("Type"), QString("Application")); | ||||
163 | group.writeEntry(QLatin1String("Name"), m_referenceAppData.name); | ||||
164 | group.writeEntry(QLatin1String("GenericName"), m_referenceAppData.genericName); | ||||
165 | group.writeEntry(QLatin1String("Icon"), QString("konversation")); | ||||
166 | group.writeEntry(QLatin1String("Exec"), QString("konversation")); | ||||
167 | file.sync(); | ||||
168 | | ||||
169 | QVERIFY(file.hasApplicationType()); | ||||
170 | | ||||
171 | QVERIFY(QFile::exists(appLinkPath())); | ||||
172 | QVERIFY(KDesktopFile::isDesktopFile(appLinkPath())); | ||||
173 | } | ||||
174 | | ||||
175 | void TaskToolsTest::createIcon() | ||||
176 | { | ||||
177 | // FIXME KIconLoaderPrivate::initIconThemes: Error: standard icon theme "oxygen" not found! | ||||
178 | | ||||
179 | QString iconDir = m_tempDir.path() + QLatin1String("/data/icons/"); | ||||
180 | | ||||
181 | QVERIFY(QDir().mkpath(iconDir)); | ||||
182 | | ||||
183 | QIcon::setThemeSearchPaths(QStringList() << m_tempDir.path() + QLatin1String("/data/icons/")); | ||||
184 | QCOMPARE(QIcon::themeSearchPaths(), QStringList() << iconDir); | ||||
185 | | ||||
186 | iconDir = iconDir + QLatin1String("/") + KIconTheme::defaultThemeName(); | ||||
187 | | ||||
188 | QVERIFY(QDir().mkpath(iconDir)); | ||||
189 | | ||||
190 | const QString &themeFile = iconDir + QLatin1String("/index.theme"); | ||||
191 | | ||||
192 | KConfig config(themeFile); | ||||
193 | KConfigGroup group(config.group(QLatin1String("Icon Theme"))); | ||||
194 | group.writeEntry(QLatin1String("Name"), KIconTheme::defaultThemeName()); | ||||
195 | group.writeEntry(QLatin1String("Inherits"), QString("hicolor")); | ||||
196 | config.sync(); | ||||
197 | | ||||
198 | QVERIFY(QFile::exists(themeFile)); | ||||
199 | | ||||
200 | iconDir = iconDir + QLatin1String("/64x64/apps"); | ||||
201 | | ||||
202 | QVERIFY(QDir().mkpath(iconDir)); | ||||
203 | | ||||
204 | const QString &iconPath = iconDir + QLatin1String("/konversation.png"); | ||||
205 | | ||||
206 | QImage image(64, 64, QImage::Format_Mono); | ||||
207 | image.save(iconPath); | ||||
208 | | ||||
209 | QVERIFY(QFile::exists(iconPath)); | ||||
210 | } | ||||
211 | | ||||
212 | QTEST_MAIN(TaskToolsTest) | ||||
213 | | ||||
214 | #include "tasktoolstest.moc" |