File indexing completed on 2024-04-14 03:52:45
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 #include "kconfiggroup.h" 0008 #include "ksharedconfig.h" 0009 0010 #include <KRecentDocument> 0011 0012 #include <QDomDocument> 0013 #include <QStandardPaths> 0014 #include <QTemporaryFile> 0015 #include <QTest> 0016 0017 void KRecentDocumentTest::initTestCase() 0018 { 0019 QStandardPaths::setTestModeEnabled(true); 0020 m_xbelPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/recently-used.xbel"); 0021 0022 cleanup(); 0023 0024 // must be outside of /tmp 0025 QFile tempFile(QDir::currentPath() + "/temp File"); 0026 if (!tempFile.open(QIODevice::WriteOnly)) { 0027 qFatal("Can't create %s", qPrintable(tempFile.fileName())); 0028 } 0029 m_testFile = tempFile.fileName(); 0030 //qDebug() << "test file" << m_testFile; 0031 } 0032 0033 void KRecentDocumentTest::cleanupTestCase() 0034 { 0035 QFile(m_testFile).remove(); 0036 } 0037 0038 void KRecentDocumentTest::cleanup() 0039 { 0040 QFile(m_xbelPath).remove(); 0041 0042 QString recentDocDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/RecentDocuments/"); 0043 QDir(recentDocDir).removeRecursively(); 0044 } 0045 0046 void KRecentDocumentTest::testXbelBookmark() 0047 { 0048 const auto url = QUrl::fromLocalFile(m_testFile); 0049 // qDebug() << "url=" << url; 0050 KRecentDocument::add(url, QStringLiteral("my-application")); 0051 KRecentDocument::add(url, QStringLiteral("my-application-2")); 0052 KRecentDocument::add(url, QStringLiteral("my-application")); 0053 0054 auto xbelFile = QFile(m_xbelPath); 0055 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0056 auto xbelContent = xbelFile.readAll(); 0057 xbelFile.close(); 0058 0059 QDomDocument reader; 0060 QVERIFY(reader.setContent(xbelContent)); 0061 0062 auto bookmarks = reader.elementsByTagName("bookmark"); 0063 // check there is only one <bookmark> element and matches expected href 0064 QCOMPARE(bookmarks.length(), 1); 0065 QVERIFY(bookmarks.at(0).attributes().contains("href")); 0066 QCOMPARE(url.toString(QUrl::FullyEncoded), bookmarks.at(0).toElement().attribute("href")); 0067 0068 const auto apps = reader.elementsByTagName("bookmark:application"); 0069 QCOMPARE(apps.length(), 2); 0070 0071 for (int i = 0; i < apps.count(); ++i) { 0072 const auto applicationElement = apps.at(i).toElement(); 0073 if (applicationElement.attribute(QStringLiteral("name")) == QStringLiteral("my-application")) { 0074 QCOMPARE(applicationElement.attribute("count"), QStringLiteral("2")); 0075 } else { 0076 QCOMPARE(applicationElement.attribute("count"), QStringLiteral("1")); 0077 } 0078 QCOMPARE(applicationElement.attribute(QStringLiteral("exec")), QStringLiteral("krecentdocumenttest %f")); 0079 } 0080 0081 auto urls = KRecentDocument::recentUrls(); 0082 if (urls.length() != 1) { 0083 qWarning() << urls; 0084 } 0085 0086 QCOMPARE(urls.length(), 1); 0087 QCOMPARE(urls.at(0), url); 0088 0089 QFile tempJpegFile(QDir::currentPath() + "/tempFile.jpg"); 0090 QVERIFY(tempJpegFile.open(QIODevice::WriteOnly)); 0091 const auto imgFileUrl = QUrl::fromLocalFile(tempJpegFile.fileName()); 0092 KRecentDocument::add(imgFileUrl, QStringLiteral("my-image-viewer")); 0093 0094 urls = KRecentDocument::recentUrls(); 0095 0096 QCOMPARE(urls.length(), 2); 0097 QCOMPARE(urls.at(0), url); 0098 QCOMPARE(urls.at(1), imgFileUrl); 0099 0100 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0101 xbelContent = xbelFile.readAll(); 0102 xbelFile.close(); 0103 QVERIFY(reader.setContent(xbelContent)); 0104 auto bookmarksGroups = reader.elementsByTagName("bookmark:groups"); 0105 QCOMPARE(bookmarksGroups.length(), 1); 0106 QCOMPARE(bookmarksGroups.at(0).toElement().text(), QStringLiteral("Graphics")); 0107 0108 QFile temparchiveFile(QDir::currentPath() + "/tempFile.zip"); 0109 QVERIFY(temparchiveFile.open(QIODevice::WriteOnly)); 0110 const auto archiveFileUrl = QUrl::fromLocalFile(temparchiveFile.fileName()); 0111 KRecentDocument::add(archiveFileUrl, QStringLiteral("my-archive-viewer"), QList{KRecentDocument::RecentDocumentGroup::Archive}); 0112 0113 urls = KRecentDocument::recentUrls(); 0114 0115 QCOMPARE(urls.length(), 3); 0116 QCOMPARE(urls.at(0), url); 0117 QCOMPARE(urls.at(1), imgFileUrl); 0118 QCOMPARE(urls.at(2), archiveFileUrl); 0119 0120 QVERIFY(xbelFile.open(QIODevice::OpenModeFlag::ReadOnly)); 0121 xbelContent = xbelFile.readAll(); 0122 QVERIFY(reader.setContent(xbelContent)); 0123 bookmarksGroups = reader.elementsByTagName("bookmark:groups"); 0124 QCOMPARE(bookmarksGroups.length(), 2); 0125 QCOMPARE(bookmarksGroups.at(1).toElement().text(), QStringLiteral("Archive")); 0126 } 0127 0128 void KRecentDocumentTest::testXbelBookmarkMaxEntries() 0129 { 0130 KConfigGroup config = KSharedConfig::openConfig()->group(QStringLiteral("RecentDocuments")); 0131 config.writeEntry(QStringLiteral("UseRecent"), true); 0132 config.writeEntry(QStringLiteral("MaxEntries"), 3); 0133 0134 auto tempFiles = QList<QFile*>(); 0135 for (int i = 0; i < 15; ++i) 0136 { 0137 QFile *tempFile = new QFile(QDir::currentPath() + "/temp File" + QString::number(i)); 0138 QVERIFY(tempFile->open(QIODevice::WriteOnly)); 0139 tempFile->close(); 0140 tempFiles << tempFile; 0141 0142 KRecentDocument::add(QUrl::fromLocalFile(tempFile->fileName()), QStringLiteral("my-application")); 0143 } 0144 0145 const auto recentUrls = KRecentDocument::recentUrls(); 0146 QCOMPARE(recentUrls.length(), 3); 0147 0148 for (int i = 0; i < 3; ++i) { 0149 QCOMPARE(recentUrls.at(i).fileName(), "temp File" + QString::number(i + 12)); 0150 } 0151 0152 for (auto &f: tempFiles) { 0153 f->remove(); 0154 } 0155 } 0156 0157 QTEST_MAIN(KRecentDocumentTest) 0158 0159 #include "moc_krecentdocumenttest.cpp"