~ubuntu-branches/ubuntu/oneiric/gwenview/oneiric-proposed

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
/*
Gwenview: an image viewer
Copyright 2009 Aurélien Gâteau <agateau@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) 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
#include "placetreemodeltest.moc"

// Qt
#include <QDir>
#include <QFile>

// KDE
#include <kdebug.h>
#include <kstandarddirs.h>
#include <qtest_kde.h>

// Local
#include "../lib/placetreemodel.h"

#define KEEP_TEMP_DIR

QTEST_KDEMAIN(PlaceTreeModelTest, GUI)

using namespace Gwenview;

const char* BOOKMARKS_XML =
"<?xml version='1.0' encoding='UTF-8'?>"
"<!DOCTYPE xbel>"
"<xbel xmlns:bookmark='http://www.freedesktop.org/standards/desktop-bookmarks' xmlns:mime='http://www.freedesktop.org/standards/shared-mime-info' xmlns:kdepriv='http://www.kde.org/kdepriv' dbusName='kfilePlaces' >"
" <bookmark href='%1' >"
"  <title>url1</title>"
"  <info>"
"   <metadata owner='http://freedesktop.org' >"
"    <bookmark:icon name='user-home' />"
"   </metadata>"
"   <metadata owner='http://www.kde.org' >"
"    <ID>1214343736/0</ID>"
"    <isSystemItem>true</isSystemItem>"
"   </metadata>"
"  </info>"
" </bookmark>"
" <bookmark href='%2' >"
"  <title>url2</title>"
"  <info>"
"   <metadata owner='http://freedesktop.org' >"
"    <bookmark:icon name='user-home' />"
"   </metadata>"
"   <metadata owner='http://www.kde.org' >"
"    <ID>1214343736/1</ID>"
"    <isSystemItem>true</isSystemItem>"
"   </metadata>"
"  </info>"
" </bookmark>"
"</xbel>";


void PlaceTreeModelTest::initTestCase() {
	Q_ASSERT(mTempDir.exists());
	QDir dir(mTempDir.name());

	Q_ASSERT(dir.mkdir("url1"));
	mUrl1 = KUrl::fromPath(dir.filePath("url1"));

	Q_ASSERT(dir.mkdir("url2"));
	mUrl2 = KUrl::fromPath(dir.filePath("url2"));

	mUrl1Dirs << "aaa" << "zzz" << "bbb";
	Q_FOREACH(const QString& dirName, mUrl1Dirs) {
		dir.mkdir("url1/" + dirName);
	}

	QFile bookmark(KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml"));
	Q_ASSERT(bookmark.open(QIODevice::WriteOnly));

	QString xml = QString(BOOKMARKS_XML)
		.arg(mUrl1.toLocalFile())
		.arg(mUrl2.toLocalFile())
		;
	bookmark.write(xml.toUtf8());

#ifdef KEEP_TEMP_DIR
	mTempDir.setAutoRemove(false);
	kDebug() << "mTempDir:" << mTempDir.name();
#endif
}


void PlaceTreeModelTest::testListPlaces() {
	PlaceTreeModel model(0);
	QCOMPARE(model.rowCount(), 2);

	QModelIndex index;
	index = model.index(0, 0);
	QCOMPARE(model.urlForIndex(index), mUrl1);
	index = model.index(1, 0);
	QCOMPARE(model.urlForIndex(index), mUrl2);
}


void PlaceTreeModelTest::testListUrl1() {
	PlaceTreeModel model(0);

	QModelIndex index = model.index(0, 0);
	QCOMPARE(model.urlForIndex(index), mUrl1);

	// We should not have fetched content yet
	QCOMPARE(model.rowCount(index), 0);
	QVERIFY(model.canFetchMore(index));

	while (model.canFetchMore(index)) {
		model.fetchMore(index);
	}
	QTest::qWait(1000);
	QCOMPARE(model.rowCount(index), mUrl1Dirs.length());

	QStringList dirs = mUrl1Dirs;
	dirs.sort();

	for (int row = 0; row < dirs.count(); ++row) {
		QModelIndex subIndex = model.index(row, 0, index);
		QVERIFY(subIndex.isValid());

		QString dirName = model.data(subIndex).toString();
		QCOMPARE(dirName, dirs.value(row));
	}
}