File indexing completed on 2024-05-12 04:19:58
0001 /* 0002 Gwenview: an image viewer 0003 Copyright 2009 Aurélien Gâteau <agateau@kde.org> 0004 0005 This program is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU General Public License 0007 as published by the Free Software Foundation; either version 2 0008 of the License, or (at your option) any later version. 0009 0010 This program is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU General Public License for more details. 0014 0015 You should have received a copy of the GNU General Public License 0016 along with this program; if not, write to the Free Software 0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0018 0019 */ 0020 #include "placetreemodeltest.h" 0021 0022 // Qt 0023 #include <QDebug> 0024 #include <QDir> 0025 #include <QFile> 0026 #include <QStandardPaths> 0027 #include <QTest> 0028 0029 // KF 0030 0031 // Local 0032 #include "../lib/placetreemodel.h" 0033 #include "testutils.h" 0034 0035 // #define KEEP_TEMP_DIR 0036 0037 QTEST_MAIN(PlaceTreeModelTest) 0038 0039 using namespace Gwenview; 0040 0041 const char *BOOKMARKS_XML = 0042 "<?xml version='1.0' encoding='UTF-8'?>" 0043 "<!DOCTYPE xbel>" 0044 "<xbel xmlns:bookmark='http://www.freedesktop.org/standards/desktop-bookmarks' xmlns:mime='http://www.freedesktop.org/standards/shared-mime-info' " 0045 "xmlns:kdepriv='http://www.kde.org/kdepriv' dbusName='kfilePlaces' >" 0046 " <bookmark href='%1' >" 0047 " <title>url1</title>" 0048 " <info>" 0049 " <metadata owner='http://freedesktop.org' >" 0050 " <bookmark:icon name='user-home' />" 0051 " </metadata>" 0052 " <metadata owner='http://www.kde.org' >" 0053 " <ID>1214343736/0</ID>" 0054 " <isSystemItem>true</isSystemItem>" 0055 " </metadata>" 0056 " </info>" 0057 " </bookmark>" 0058 " <bookmark href='%2' >" 0059 " <title>url2</title>" 0060 " <info>" 0061 " <metadata owner='http://freedesktop.org' >" 0062 " <bookmark:icon name='user-home' />" 0063 " </metadata>" 0064 " <metadata owner='http://www.kde.org' >" 0065 " <ID>1214343736/1</ID>" 0066 " <isSystemItem>true</isSystemItem>" 0067 " </metadata>" 0068 " </info>" 0069 " </bookmark>" 0070 "</xbel>"; 0071 0072 void PlaceTreeModelTest::initTestCase() 0073 { 0074 Q_ASSERT(mTempDir.isValid()); 0075 QDir dir(mTempDir.path()); 0076 0077 const bool dir1created = dir.mkdir("url1"); 0078 Q_ASSERT(dir1created); 0079 Q_UNUSED(dir1created); 0080 mUrl1 = QUrl::fromLocalFile(dir.filePath("url1")); 0081 0082 const bool dir2created = dir.mkdir("url2"); 0083 Q_ASSERT(dir2created); 0084 Q_UNUSED(dir2created); 0085 mUrl2 = QUrl::fromLocalFile(dir.filePath("url2")); 0086 0087 mUrl1Dirs << "aaa" 0088 << "zzz" 0089 << "bbb"; 0090 for (const QString &dirName : qAsConst(mUrl1Dirs)) { 0091 dir.mkdir("url1/" + dirName); 0092 } 0093 0094 #ifdef KEEP_TEMP_DIR 0095 mTempDir.setAutoRemove(false); 0096 // qDebug() << "mTempDir:" << mTempDir.name(); 0097 #endif 0098 } 0099 0100 void PlaceTreeModelTest::init() 0101 { 0102 QStandardPaths::setTestModeEnabled(true); 0103 0104 TestUtils::purgeUserConfiguration(); 0105 0106 const QString confDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); 0107 QDir().mkpath(confDir); 0108 QFile bookmark(confDir + "/user-places.xbel"); 0109 const bool bookmarkOpened = bookmark.open(QIODevice::WriteOnly); 0110 Q_ASSERT(bookmarkOpened); 0111 Q_UNUSED(bookmarkOpened); 0112 0113 QString xml = QString(BOOKMARKS_XML).arg(mUrl1.url(), mUrl2.url()); 0114 bookmark.write(xml.toUtf8()); 0115 0116 #ifdef KEEP_TEMP_DIR 0117 mTempDir.setAutoRemove(false); 0118 // qDebug() << "mTempDir:" << mTempDir.name(); 0119 #endif 0120 } 0121 0122 void PlaceTreeModelTest::testListPlaces() 0123 { 0124 PlaceTreeModel model(nullptr); 0125 0126 QCOMPARE(model.rowCount(), 8); 0127 0128 QModelIndex index; 0129 index = model.index(0, 0); 0130 QCOMPARE(model.urlForIndex(index), mUrl1); 0131 index = model.index(1, 0); 0132 QCOMPARE(model.urlForIndex(index), mUrl2); 0133 } 0134 0135 void PlaceTreeModelTest::testListUrl1() 0136 { 0137 PlaceTreeModel model(nullptr); 0138 0139 QModelIndex index = model.index(0, 0); 0140 QCOMPARE(model.urlForIndex(index), mUrl1); 0141 0142 // We should not have fetched content yet 0143 QCOMPARE(model.rowCount(index), 0); 0144 QVERIFY(model.canFetchMore(index)); 0145 0146 while (model.canFetchMore(index)) { 0147 model.fetchMore(index); 0148 } 0149 QTest::qWait(1000); 0150 QCOMPARE(model.rowCount(index), mUrl1Dirs.length()); 0151 0152 QStringList dirs = mUrl1Dirs; 0153 dirs.sort(); 0154 0155 for (int row = 0; row < dirs.count(); ++row) { 0156 QModelIndex subIndex = model.index(row, 0, index); 0157 QVERIFY(subIndex.isValid()); 0158 0159 QString dirName = model.data(subIndex).toString(); 0160 QCOMPARE(dirName, dirs.value(row)); 0161 } 0162 } 0163 0164 #include "moc_placetreemodeltest.cpp"