1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/* This file is part of the KDE project
Copyright 2007 David Faure <faure@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License or ( at
your option ) version 3 or, at the discretion of KDE e.V. ( which shall
act as a proxy as in section 14 of the GPLv3 ), any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Boston, MA 02110-1301, USA.
*/
#include <kservice.h>
#include <kconfiggroup.h>
#include <kdesktopfile.h>
#include <ksycoca.h>
// Qt
#include <QProcess>
#include <QDir>
#include <QStandardPaths>
#include <QTest>
#include <QSignalSpy>
#include <QLoggingCategory>
#include <mimetypedata.h>
#include <mimetypewriter.h>
class FileTypesTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
extern KSERVICE_EXPORT bool kservice_require_kded;
kservice_require_kded = false;
QLoggingCategory::setFilterRules(QStringLiteral("kf5.kcoreaddons.kdirwatch.debug=true"));
QStandardPaths::setTestModeEnabled(true);
m_mimeTypeCreatedSuccessfully = false;
QStringList appsDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation);
//qDebug() << appsDirs;
m_localApps = appsDirs.first() + QLatin1Char('/');
m_localConfig = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
QVERIFY(QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/mime/packages")));
QFile::remove(m_localConfig.filePath(QStringLiteral("mimeapps.list")));
// Create fake applications for some tests below.
bool mustUpdateKSycoca = false;
fakeApplication = QStringLiteral("fakeapplication.desktop");
if (createDesktopFile(m_localApps + fakeApplication))
mustUpdateKSycoca = true;
fakeApplication2 = QStringLiteral("fakeapplication2.desktop");
if (createDesktopFile(m_localApps + fakeApplication2))
mustUpdateKSycoca = true;
// Cleanup after testMimeTypePatterns if it failed mid-way
const QString packageFileName = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/mime/") + QStringLiteral("packages/text-plain.xml") ;
if (!packageFileName.isEmpty()) {
QFile::remove(packageFileName);
MimeTypeWriter::runUpdateMimeDatabase();
mustUpdateKSycoca = true;
}
QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + QStringLiteral("filetypesrc"));
if ( mustUpdateKSycoca ) {
// Update ksycoca in ~/.kde-unit-test after creating the above
runKBuildSycoca();
}
KService::Ptr fakeApplicationService = KService::serviceByStorageId(fakeApplication);
QVERIFY(fakeApplicationService);
}
void testMimeTypeGroupAutoEmbed()
{
MimeTypeData data(QStringLiteral("text"));
QCOMPARE(data.majorType(), QStringLiteral("text"));
QCOMPARE(data.name(), QStringLiteral("text"));
QVERIFY(data.isMeta());
QCOMPARE(data.autoEmbed(), MimeTypeData::No); // text doesn't autoembed by default
QVERIFY(!data.isDirty());
data.setAutoEmbed(MimeTypeData::Yes);
QCOMPARE(data.autoEmbed(), MimeTypeData::Yes);
QVERIFY(data.isDirty());
QVERIFY(!data.sync()); // save to disk. Should succeed, but return false (no need to run update-mime-database)
QVERIFY(!data.isDirty());
// Check what's on disk by creating another MimeTypeData instance
MimeTypeData data2(QStringLiteral("text"));
QCOMPARE(data2.autoEmbed(), MimeTypeData::Yes);
QVERIFY(!data2.isDirty());
data2.setAutoEmbed(MimeTypeData::No); // revert to default, for next time
QVERIFY(data2.isDirty());
QVERIFY(!data2.sync());
QVERIFY(!data2.isDirty());
// TODO test askSave after cleaning up the code
}
void testMimeTypeAutoEmbed()
{
QMimeDatabase db;
MimeTypeData data(db.mimeTypeForName(QStringLiteral("text/plain")));
QCOMPARE(data.majorType(), QStringLiteral("text"));
QCOMPARE(data.minorType(), QStringLiteral("plain"));
QCOMPARE(data.name(), QStringLiteral("text/plain"));
QVERIFY(!data.isMeta());
QCOMPARE(data.autoEmbed(), MimeTypeData::UseGroupSetting);
QVERIFY(!data.isDirty());
data.setAutoEmbed(MimeTypeData::Yes);
QCOMPARE(data.autoEmbed(), MimeTypeData::Yes);
QVERIFY(data.isDirty());
QVERIFY(!data.sync()); // save to disk. Should succeed, but return false (no need to run update-mime-database)
QVERIFY(!data.isDirty());
// Check what's on disk by creating another MimeTypeData instance
MimeTypeData data2(db.mimeTypeForName(QStringLiteral("text/plain")));
QCOMPARE(data2.autoEmbed(), MimeTypeData::Yes);
QVERIFY(!data2.isDirty());
data2.setAutoEmbed(MimeTypeData::UseGroupSetting); // revert to default, for next time
QVERIFY(data2.isDirty());
QVERIFY(!data2.sync());
QVERIFY(!data2.isDirty());
}
void testMimeTypePatterns()
{
// Given the text/plain mimetype
QMimeDatabase db;
MimeTypeData data(db.mimeTypeForName(QStringLiteral("text/plain")));
QCOMPARE(data.name(), QStringLiteral("text/plain"));
QCOMPARE(data.majorType(), QStringLiteral("text"));
QCOMPARE(data.minorType(), QStringLiteral("plain"));
QVERIFY(!data.isMeta());
QStringList patterns = data.patterns();
QVERIFY(patterns.contains(QStringLiteral("*.txt")));
QVERIFY(!patterns.contains(QStringLiteral("*.toto")));
// When the user changes the patterns
const QStringList origPatterns = patterns;
// The shared mime specification doesn't allow removing patterns from
// user dir to a system dir
// patterns.removeAll(QStringLiteral("*.txt"));
patterns.append(QStringLiteral("*.toto")); // yes, a french guy wrote this, as you can see
patterns.sort(); // for future comparisons
QVERIFY(!data.isDirty());
data.setPatterns(patterns);
QVERIFY(data.isDirty());
bool needUpdateMimeDb = data.sync();
QVERIFY(needUpdateMimeDb);
MimeTypeWriter::runUpdateMimeDatabase();
// Then the GUI and the QMimeDatabase API should show the new patterns
QCOMPARE(data.patterns(), patterns);
data.refresh(); // reload from the xml
QCOMPARE(data.patterns(), patterns);
// Check what's in QMimeDatabase
QStringList newPatterns = db.mimeTypeForName(QStringLiteral("text/plain")).globPatterns();
newPatterns.sort();
QCOMPARE(newPatterns, patterns);
QVERIFY(!data.isDirty());
// And then removing the custom file by hand should revert to the initial state
const QString packageFileName = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/mime/") + QStringLiteral("packages/text-plain.xml") ;
QVERIFY(!packageFileName.isEmpty());
QFile::remove(packageFileName);
MimeTypeWriter::runUpdateMimeDatabase();
// Check what's in QMimeDatabase
newPatterns = db.mimeTypeForName(QStringLiteral("text/plain")).globPatterns();
newPatterns.sort();
QCOMPARE(newPatterns, origPatterns);
}
void testAddService()
{
QMimeDatabase db;
QString mimeTypeName = QStringLiteral("application/rtf"); // use inherited mimetype to test #321706
MimeTypeData data(db.mimeTypeForName(mimeTypeName));
QStringList appServices = data.appServices();
//qDebug() << appServices;
QVERIFY(!appServices.isEmpty());
const QString oldPreferredApp = appServices.first();
QVERIFY(!appServices.contains(fakeApplication)); // already there? hmm can't really test then
QVERIFY(!data.isDirty());
appServices.prepend(fakeApplication);
data.setAppServices(appServices);
QVERIFY(data.isDirty());
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
QVERIFY(!data.isDirty());
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkAddedAssociationsContains(mimeTypeName, fakeApplication);
// Test reordering apps, i.e. move fakeApplication under oldPreferredApp
appServices.removeFirst();
appServices.insert(1, fakeApplication);
data.setAppServices(appServices);
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
QVERIFY(!data.isDirty());
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkAddedAssociationsContains(mimeTypeName, fakeApplication);
// Now test removing (in the same test, since it's inter-dependent)
QVERIFY(appServices.removeAll(fakeApplication) > 0);
data.setAppServices(appServices);
QVERIFY(data.isDirty());
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkRemovedAssociationsContains(mimeTypeName, fakeApplication);
}
void testRemoveTwice()
{
QMimeDatabase db;
// Remove fakeApplication from image/png
QString mimeTypeName = QStringLiteral("image/png");
MimeTypeData data(db.mimeTypeForName(mimeTypeName));
QStringList appServices = data.appServices();
qDebug() << "initial list for" << mimeTypeName << appServices;
QVERIFY(appServices.removeAll(fakeApplication) > 0);
data.setAppServices(appServices);
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkRemovedAssociationsContains(mimeTypeName, fakeApplication);
// Remove fakeApplication2 from image/png; must keep the previous entry in "Removed Associations"
qDebug() << "Removing fakeApplication2";
QVERIFY(appServices.removeAll(fakeApplication2) > 0);
data.setAppServices(appServices);
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkRemovedAssociationsContains(mimeTypeName, fakeApplication);
// Check what's in mimeapps.list
checkRemovedAssociationsContains(mimeTypeName, fakeApplication2);
// And now re-add fakeApplication2...
qDebug() << "Re-adding fakeApplication2";
appServices.prepend(fakeApplication2);
data.setAppServices(appServices);
QVERIFY(!data.sync()); // success, but no need to run update-mime-database
runKBuildSycoca();
// Check what's in ksycoca
checkMimeTypeServices(mimeTypeName, appServices);
// Check what's in mimeapps.list
checkRemovedAssociationsContains(mimeTypeName, fakeApplication);
checkRemovedAssociationsDoesNotContain(mimeTypeName, fakeApplication2);
}
void testCreateMimeType()
{
QMimeDatabase db;
const QString mimeTypeName = QStringLiteral("fake/unit-test-fake-mimetype");
// Clean up after previous runs if necessary
if (MimeTypeWriter::hasDefinitionFile(mimeTypeName))
MimeTypeWriter::removeOwnMimeType(mimeTypeName);
MimeTypeData data(mimeTypeName, true);
data.setComment(QStringLiteral("Fake MimeType"));
QStringList patterns = QStringList() << QStringLiteral("*.pkg.tar.gz");
data.setPatterns(patterns);
QVERIFY(data.isDirty());
QVERIFY(data.sync());
MimeTypeWriter::runUpdateMimeDatabase();
//runKBuildSycoca();
// QMimeDatabase doesn't even try to update the cache if less than
// 5000 ms have passed
QTest::qSleep(5000);
QMimeType mime = db.mimeTypeForName(mimeTypeName);
QVERIFY(mime.isValid());
QCOMPARE(mime.comment(), QStringLiteral("Fake MimeType"));
QCOMPARE(mime.globPatterns(), patterns); // must sort them if more than one
// Testcase for the shaman.xml bug
QCOMPARE(db.mimeTypeForFile(QStringLiteral("/whatever/foo.pkg.tar.gz")).name(), QStringLiteral("fake/unit-test-fake-mimetype"));
m_mimeTypeCreatedSuccessfully = true;
}
void testDeleteMimeType()
{
QMimeDatabase db;
if (!m_mimeTypeCreatedSuccessfully)
QSKIP("This test relies on testCreateMimeType");
const QString mimeTypeName = QStringLiteral("fake/unit-test-fake-mimetype");
QVERIFY(MimeTypeWriter::hasDefinitionFile(mimeTypeName));
MimeTypeWriter::removeOwnMimeType(mimeTypeName);
MimeTypeWriter::runUpdateMimeDatabase();
//runKBuildSycoca();
QMimeType mime = db.mimeTypeForName(mimeTypeName);
QVERIFY(mime.isValid());
}
void testModifyMimeTypeComment() // of a system mimetype. And check that it's re-read correctly.
{
QMimeDatabase db;
QString mimeTypeName = QStringLiteral("image/png");
MimeTypeData data(db.mimeTypeForName(mimeTypeName));
QCOMPARE(data.comment(), QString::fromLatin1("PNG image"));
QString fakeComment = QStringLiteral("PNG image [testing]");
data.setComment(fakeComment);
QVERIFY(data.isDirty());
QVERIFY(data.sync());
MimeTypeWriter::runUpdateMimeDatabase();
//runKBuildSycoca();
QMimeType mime = db.mimeTypeForName(mimeTypeName);
QVERIFY(mime.isValid());
QCOMPARE(mime.comment(), fakeComment);
// Cleanup
QVERIFY(MimeTypeWriter::hasDefinitionFile(mimeTypeName));
MimeTypeWriter::removeOwnMimeType(mimeTypeName);
}
void cleanupTestCase()
{
// If we remove it, then every run of the unit test has to run kbuildsycoca... slow.
//QFile::remove(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + QLatin1Char('/') + "fakeapplication.desktop");
}
private: // helper methods
void checkAddedAssociationsContains(const QString& mimeTypeName, const QString& application)
{
const KConfig config(m_localConfig.filePath(QStringLiteral("mimeapps.list")), KConfig::NoGlobals);
const KConfigGroup group(&config, "Added Associations");
const QStringList addedEntries = group.readXdgListEntry(mimeTypeName);
if (!addedEntries.contains(application)) {
qWarning() << addedEntries << "does not contain" << application;
QVERIFY(addedEntries.contains(application));
}
}
void checkRemovedAssociationsContains(const QString& mimeTypeName, const QString& application)
{
const KConfig config(m_localConfig.filePath(QStringLiteral("mimeapps.list")), KConfig::NoGlobals);
const KConfigGroup group(&config, "Removed Associations");
const QStringList removedEntries = group.readXdgListEntry(mimeTypeName);
if (!removedEntries.contains(application)) {
qWarning() << removedEntries << "does not contain" << application;
QVERIFY(removedEntries.contains(application));
}
}
void checkRemovedAssociationsDoesNotContain(const QString& mimeTypeName, const QString& application)
{
const KConfig config(m_localConfig.filePath(QStringLiteral("mimeapps.list")), KConfig::NoGlobals);
const KConfigGroup group(&config, "Removed Associations");
const QStringList removedEntries = group.readXdgListEntry(mimeTypeName);
if (removedEntries.contains(application)) {
qWarning() << removedEntries << "contains" << application;
QVERIFY(!removedEntries.contains(application));
}
}
void runKBuildSycoca()
{
// Wait for notifyDatabaseChanged DBus signal
// (The real KCM code simply does the refresh in a slot, asynchronously)
QProcess proc;
//proc.setProcessChannelMode(QProcess::ForwardedChannels);
const QString kbuildsycoca = QStandardPaths::findExecutable(QLatin1String(KBUILDSYCOCA_EXENAME));
QVERIFY(!kbuildsycoca.isEmpty());
QStringList args;
args << QStringLiteral("--testmode");
proc.start(kbuildsycoca, args);
QSignalSpy spy(KSycoca::self(), SIGNAL(databaseChanged(QStringList)));
proc.waitForFinished();
qDebug() << "waiting for signal";
QVERIFY(spy.wait(10000));
qDebug() << "got signal";
}
bool createDesktopFile(const QString& path)
{
if (!QFile::exists(path)) {
KDesktopFile file(path);
KConfigGroup group = file.desktopGroup();
group.writeEntry("Name", "FakeApplication");
group.writeEntry("Type", "Application");
group.writeEntry("Exec", "ls");
group.writeEntry("MimeType", "image/png");
return true;
}
return false;
}
void checkMimeTypeServices(const QString& mimeTypeName, const QStringList& expectedServices)
{
QMimeDatabase db;
MimeTypeData data2(db.mimeTypeForName(mimeTypeName));
if (data2.appServices() != expectedServices)
qDebug() << "got" << data2.appServices() << "expected" << expectedServices;
QCOMPARE(data2.appServices(), expectedServices);
}
QString fakeApplication; // storage id of the fake application
QString fakeApplication2; // storage id of the fake application2
QString m_localApps;
QDir m_localConfig;
bool m_mimeTypeCreatedSuccessfully;
};
QTEST_MAIN(FileTypesTest)
#include "filetypestest.moc"
|