File indexing completed on 2025-01-12 12:29:32
0001 /* 0002 SPDX-FileCopyrightText: 2022 Méven Car <meven.car@kdemail.net> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 #include "krecentdocumenttest.h" 0007 0008 #include <KRecentDocument> 0009 0010 #include <QDomDocument> 0011 #include <QStandardPaths> 0012 #include <QTemporaryFile> 0013 #include <QTest> 0014 0015 void KRecentDocumentTest::initTestCase() 0016 { 0017 QStandardPaths::setTestModeEnabled(true); 0018 m_xbelPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/recently-used.xbel"); 0019 0020 cleanup(); 0021 0022 // must be outside of /tmp 0023 QFile tempFile(QDir::currentPath() + "/temp File"); 0024 if (!tempFile.open(QIODevice::WriteOnly)) { 0025 qFatal("Can't create %s", qPrintable(tempFile.fileName())); 0026 } 0027 m_testFile = tempFile.fileName(); 0028 qDebug() << "test file" << m_testFile; 0029 } 0030 0031 void KRecentDocumentTest::cleanupTestCase() 0032 { 0033 QFile(m_testFile).remove(); 0034 } 0035 0036 void KRecentDocumentTest::cleanup() 0037 { 0038 QFile(m_xbelPath).remove(); 0039 0040 QString recentDocDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/RecentDocuments/"); 0041 QDir(recentDocDir).removeRecursively(); 0042 } 0043 0044 void KRecentDocumentTest::testXbelBookmark() 0045 { 0046 QTemporaryFile tempFile; 0047 QVERIFY(tempFile.open()); 0048 0049 const auto url = QUrl::fromLocalFile(m_testFile); 0050 qDebug() << "url=" << url; 0051 KRecentDocument::add(url, QStringLiteral("my-application")); 0052 KRecentDocument::add(url, QStringLiteral("my-application-2")); 0053 KRecentDocument::add(url, QStringLiteral("my-application")); 0054 0055 auto xbelFile = QFile(m_xbelPath); 0056 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0057 auto xbelContent = xbelFile.readAll(); 0058 xbelFile.close(); 0059 0060 auto expected = R"foo(<?xml version="1.0" encoding="UTF-8"?> 0061 <xbel version="1.0" xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks" xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info">)foo"; 0062 // check basic formatting 0063 QVERIFY(xbelContent.startsWith(expected)); 0064 0065 QDomDocument reader; 0066 QVERIFY(reader.setContent(xbelContent)); 0067 0068 auto bookmarks = reader.elementsByTagName("bookmark"); 0069 // check there is only one <bookmark> element and matches expected href 0070 QCOMPARE(bookmarks.length(), 1); 0071 QVERIFY(bookmarks.at(0).attributes().contains("href")); 0072 QCOMPARE(url.toString(QUrl::FullyEncoded), bookmarks.at(0).toElement().attribute("href")); 0073 0074 const auto apps = reader.elementsByTagName("bookmark:application"); 0075 QCOMPARE(apps.length(), 2); 0076 0077 for (int i = 0; i < apps.count(); ++i) { 0078 const auto applicationElement = apps.at(i).toElement(); 0079 if (applicationElement.attribute(QStringLiteral("name")) == QStringLiteral("my-application")) { 0080 QCOMPARE(applicationElement.attribute("count"), QStringLiteral("2")); 0081 } else { 0082 QCOMPARE(applicationElement.attribute("count"), QStringLiteral("1")); 0083 } 0084 QCOMPARE(applicationElement.attribute(QStringLiteral("exec")), QStringLiteral("krecentdocumenttest %u")); 0085 } 0086 0087 auto urls = KRecentDocument::recentUrls(); 0088 if (urls.length() != 1) { 0089 qWarning() << urls; 0090 } 0091 0092 QCOMPARE(urls.length(), 1); 0093 QCOMPARE(urls.at(0), url); 0094 0095 QFile tempJpegFile(QDir::currentPath() + "/tempFile.jpg"); 0096 QVERIFY(tempJpegFile.open(QIODevice::WriteOnly)); 0097 const auto imgFileUrl = QUrl::fromLocalFile(tempJpegFile.fileName()); 0098 KRecentDocument::add(imgFileUrl, QStringLiteral("my-image-viewer")); 0099 0100 urls = KRecentDocument::recentUrls(); 0101 0102 QCOMPARE(urls.length(), 2); 0103 QCOMPARE(urls.at(0), url); 0104 QCOMPARE(urls.at(1), imgFileUrl); 0105 0106 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0107 xbelContent = xbelFile.readAll(); 0108 xbelFile.close(); 0109 QVERIFY(reader.setContent(xbelContent)); 0110 auto bookmarksGroups = reader.elementsByTagName("bookmark:groups"); 0111 QCOMPARE(bookmarksGroups.length(), 1); 0112 QCOMPARE(bookmarksGroups.at(0).toElement().text(), QStringLiteral("Graphics")); 0113 0114 QFile temparchiveFile(QDir::currentPath() + "/tempFile.zip"); 0115 QVERIFY(temparchiveFile.open(QIODevice::WriteOnly)); 0116 const auto archiveFileUrl = QUrl::fromLocalFile(temparchiveFile.fileName()); 0117 KRecentDocument::add(archiveFileUrl, QStringLiteral("my-archive-viewer"), QList{KRecentDocument::RecentDocumentGroup::Archive}); 0118 0119 urls = KRecentDocument::recentUrls(); 0120 0121 QCOMPARE(urls.length(), 3); 0122 QCOMPARE(urls.at(0), url); 0123 QCOMPARE(urls.at(1), imgFileUrl); 0124 QCOMPARE(urls.at(2), archiveFileUrl); 0125 0126 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0127 xbelContent = xbelFile.readAll(); 0128 QVERIFY(reader.setContent(xbelContent)); 0129 bookmarksGroups = reader.elementsByTagName("bookmark:groups"); 0130 QCOMPARE(bookmarksGroups.length(), 2); 0131 QCOMPARE(bookmarksGroups.at(1).toElement().text(), QStringLiteral("Archive")); 0132 } 0133 0134 QTEST_MAIN(KRecentDocumentTest) 0135 0136 #include "moc_krecentdocumenttest.cpp"