File indexing completed on 2024-06-16 12:53:48

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2005 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include <kbookmark.h>
0009 #include <kbookmarkmanager.h>
0010 
0011 #include <QDir>
0012 #include <QMimeData>
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 
0017 class KBookmarkTest : public QObject
0018 {
0019     Q_OBJECT
0020 private Q_SLOTS:
0021     void initTestCase();
0022     void cleanupTestCase();
0023     void testMimeDataOneBookmark();
0024     void testMimeDataBookmarkList();
0025     void testFileCreatedExternally();
0026     void testBookmarkManager();
0027 };
0028 
0029 static const QString placesFile()
0030 {
0031     const QString datadir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0032     QDir().mkpath(datadir);
0033     return datadir + "/user-places.xbel";
0034 }
0035 
0036 void KBookmarkTest::initTestCase()
0037 {
0038     QStandardPaths::setTestModeEnabled(true);
0039     QFile::remove(placesFile());
0040 }
0041 
0042 void KBookmarkTest::cleanupTestCase()
0043 {
0044     QFile::remove(placesFile());
0045 }
0046 
0047 static void compareBookmarks(const KBookmark &initialBookmark, const KBookmark &decodedBookmark)
0048 {
0049     QCOMPARE(decodedBookmark.url(), initialBookmark.url());
0050     QCOMPARE(decodedBookmark.icon(), initialBookmark.icon());
0051     QCOMPARE(decodedBookmark.text(), initialBookmark.text());
0052     QCOMPARE(decodedBookmark.description(), initialBookmark.description());
0053     QDomNamedNodeMap decodedAttribs = decodedBookmark.internalElement().attributes();
0054     QDomNamedNodeMap initialAttribs = initialBookmark.internalElement().attributes();
0055     QCOMPARE(decodedAttribs.count(), initialAttribs.count());
0056     for (int i = 0; i < decodedAttribs.length(); ++i) {
0057         QDomAttr decodedAttr = decodedAttribs.item(i).toAttr();
0058         QDomAttr initialAttr = initialAttribs.item(i).toAttr();
0059         QCOMPARE(decodedAttr.name(), initialAttr.name());
0060         QCOMPARE(decodedAttr.value(), initialAttr.value());
0061     }
0062 }
0063 
0064 void KBookmarkTest::testMimeDataOneBookmark()
0065 {
0066     QMimeData *mimeData = new QMimeData;
0067 
0068     KBookmark bookmark = KBookmark::standaloneBookmark(QStringLiteral("KDE"), QUrl(QStringLiteral("http://www.kde.org")), QStringLiteral("icon"));
0069     bookmark.setDescription(QStringLiteral("Comment"));
0070     QVERIFY(!bookmark.isNull());
0071     bookmark.populateMimeData(mimeData);
0072 
0073     QDomDocument doc;
0074     QVERIFY(mimeData->hasUrls());
0075     QVERIFY(KBookmark::List::canDecode(mimeData));
0076     const KBookmark::List decodedBookmarks = KBookmark::List::fromMimeData(mimeData, doc);
0077     QVERIFY(!decodedBookmarks.isEmpty());
0078     QCOMPARE(decodedBookmarks.count(), 1);
0079     QVERIFY(!decodedBookmarks[0].isNull());
0080     compareBookmarks(bookmark, decodedBookmarks[0]);
0081 
0082     // Do like keditbookmarks's paste code: (CreateCommand::execute)
0083     // Crashed before passing "doc" to fromMimeData (#160679)
0084     QDomElement clonedElem = decodedBookmarks[0].internalElement().cloneNode().toElement();
0085 
0086     delete mimeData;
0087 }
0088 
0089 void KBookmarkTest::testMimeDataBookmarkList()
0090 {
0091     QMimeData *mimeData = new QMimeData;
0092 
0093     KBookmark bookmark1 = KBookmark::standaloneBookmark(QStringLiteral("KDE"), QUrl(QStringLiteral("http://www.kde.org")), QStringLiteral("icon"));
0094     bookmark1.setDescription(QStringLiteral("KDE comment"));
0095     QVERIFY(!bookmark1.isNull());
0096     KBookmark bookmark2 = KBookmark::standaloneBookmark(QStringLiteral("KOffice"), QUrl(QStringLiteral("http://www.koffice.org")), QStringLiteral("koicon"));
0097     bookmark2.setDescription(QStringLiteral("KOffice comment"));
0098     QVERIFY(!bookmark2.isNull());
0099     bookmark2.setMetaDataItem(QStringLiteral("key"), QStringLiteral("value"));
0100 
0101     KBookmark::List initialBookmarks;
0102     initialBookmarks.append(bookmark1);
0103     initialBookmarks.append(bookmark2);
0104     initialBookmarks.populateMimeData(mimeData);
0105 
0106     QDomDocument doc;
0107     QVERIFY(mimeData->hasUrls());
0108     QVERIFY(KBookmark::List::canDecode(mimeData));
0109     const KBookmark::List decodedBookmarks = KBookmark::List::fromMimeData(mimeData, doc);
0110     QCOMPARE(decodedBookmarks.count(), 2);
0111     QVERIFY(!decodedBookmarks[0].isNull());
0112     QVERIFY(!decodedBookmarks[1].isNull());
0113     compareBookmarks(bookmark1, decodedBookmarks[0]);
0114     compareBookmarks(bookmark2, decodedBookmarks[1]);
0115 
0116     delete mimeData;
0117 }
0118 
0119 void KBookmarkTest::testFileCreatedExternally()
0120 {
0121     KBookmarkManager *sharedBookmarkManager = KBookmarkManager::managerForExternalFile(placesFile());
0122     QVERIFY(sharedBookmarkManager->root().first().isNull());
0123 
0124     QFile file(placesFile());
0125     QVERIFY(file.open(QIODevice::WriteOnly));
0126     file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xbel version=\"1.0\"><bookmark href=\"file:///external\"><title>external</title></bookmark></xbel>");
0127     file.close();
0128 
0129     QTRY_VERIFY(!sharedBookmarkManager->root().first().isNull());
0130     KBookmark bk = sharedBookmarkManager->root().first();
0131     QCOMPARE(bk.url().toString(), QString("file:///external"));
0132     QCOMPARE(bk.fullText(), QString("external"));
0133 }
0134 
0135 void KBookmarkTest::testBookmarkManager()
0136 {
0137     // like kfileplacesmodel.cpp used to do
0138     const QString placesModelFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kfileplaces/bookmarks.xml";
0139     KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(placesModelFile, QStringLiteral("kfilePlaces"));
0140 
0141     // like kfileplacessharedbookmarks.cpp does
0142     KBookmarkManager *sharedBookmarkManager = KBookmarkManager::managerForExternalFile(placesFile());
0143 
0144     // like kfilebookmarkhandler.cpp does
0145     QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kfile/bookmarks.xml"));
0146     if (file.isEmpty()) {
0147         file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kfile/bookmarks.xml";
0148     }
0149     KBookmarkManager *manager = KBookmarkManager::managerForFile(file, QStringLiteral("kfile"));
0150 
0151     // This is just to check an old crash in the global-static destructor, not doing anything with
0152     // these managers yet.
0153     Q_UNUSED(bookmarkManager);
0154     Q_UNUSED(sharedBookmarkManager);
0155     Q_UNUSED(manager);
0156 }
0157 
0158 QTEST_MAIN(KBookmarkTest)
0159 
0160 #include "kbookmarktest.moc"