1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QSignalSpy>
43 #include "../../../shared/util.h"
44 #include <QtDeclarative/qdeclarativeengine.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtCore/qdir.h>
47 #include <QtCore/qfile.h>
48 #include <QtCore/qabstractitemmodel.h>
51 // From qdeclarastivefolderlistmodel.h
52 const int FileNameRole = Qt::UserRole+1;
53 const int FilePathRole = Qt::UserRole+2;
54 enum SortField { Unsorted, Name, Time, Size, Type };
56 class tst_qdeclarativefolderlistmodel : public QObject
60 tst_qdeclarativefolderlistmodel() : removeStart(0), removeEnd(0) {}
63 void removed(const QModelIndex &, int start, int end) {
69 void basicProperties();
70 void resetFiltering();
74 void checkNoErrors(const QDeclarativeComponent& component);
75 QDeclarativeEngine engine;
81 void tst_qdeclarativefolderlistmodel::checkNoErrors(const QDeclarativeComponent& component)
83 // Wait until the component is ready
84 QTRY_VERIFY(component.isReady() || component.isError());
86 if (component.isError()) {
87 QList<QDeclarativeError> errors = component.errors();
88 for (int ii = 0; ii < errors.count(); ++ii) {
89 const QDeclarativeError &error = errors.at(ii);
90 QByteArray errorStr = QByteArray::number(error.line()) + ":" +
91 QByteArray::number(error.column()) + ":" +
92 error.description().toUtf8();
93 qWarning() << errorStr;
96 QVERIFY(!component.isError());
99 void tst_qdeclarativefolderlistmodel::basicProperties()
101 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml"));
102 checkNoErrors(component);
104 QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
107 flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data"));
108 QTRY_COMPARE(flm->property("count").toInt(),4); // wait for refresh
109 QCOMPARE(flm->property("folder").toUrl(), QUrl::fromLocalFile(SRCDIR "/data"));
110 QCOMPARE(flm->property("parentFolder").toUrl(), QUrl::fromLocalFile(SRCDIR));
111 QCOMPARE(flm->property("sortField").toInt(), int(Name));
112 QCOMPARE(flm->property("nameFilters").toStringList(), QStringList() << "*.qml");
113 QCOMPARE(flm->property("sortReversed").toBool(), false);
114 QCOMPARE(flm->property("showDirs").toBool(), true);
115 QCOMPARE(flm->property("showDotAndDotDot").toBool(), false);
116 QCOMPARE(flm->property("showOnlyReadable").toBool(), false);
117 QCOMPARE(flm->data(flm->index(0),FileNameRole).toString(), QLatin1String("basic.qml"));
118 QCOMPARE(flm->data(flm->index(1),FileNameRole).toString(), QLatin1String("dummy.qml"));
120 flm->setProperty("folder",QUrl::fromLocalFile(""));
121 QCOMPARE(flm->property("folder").toUrl(), QUrl::fromLocalFile(""));
124 void tst_qdeclarativefolderlistmodel::resetFiltering()
127 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/resetFiltering.qml"));
128 checkNoErrors(component);
130 QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
133 connect(flm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
134 this, SLOT(removed(const QModelIndex&,int,int)));
136 flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data/resetfiltering"));
137 QTRY_COMPARE(flm->property("count").toInt(),1); // should just be "test.txt" visible
138 int count = flm->rowCount();
139 QCOMPARE(removeStart, 0);
140 QCOMPARE(removeEnd, count-1);
142 flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data/resetfiltering/innerdir"));
143 QTRY_COMPARE(flm->property("count").toInt(),1); // should just be "test2.txt" visible
144 count = flm->rowCount();
145 QCOMPARE(removeStart, 0);
146 QCOMPARE(removeEnd, count-1);
148 flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data/resetfiltering"));
149 QTRY_COMPARE(flm->property("count").toInt(),1); // should just be "test.txt" visible
150 count = flm->rowCount();
151 QCOMPARE(removeStart, 0);
152 QCOMPARE(removeEnd, count-1);
155 void tst_qdeclarativefolderlistmodel::refresh()
157 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml"));
158 checkNoErrors(component);
160 QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
163 flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data"));
164 QTRY_COMPARE(flm->property("count").toInt(),4); // wait for refresh
166 int count = flm->rowCount();
168 connect(flm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
169 this, SLOT(removed(const QModelIndex&,int,int)));
171 flm->setProperty("sortReversed", true);
173 QCOMPARE(removeStart, 0);
174 QCOMPARE(removeEnd, count-1);
177 QTEST_MAIN(tst_qdeclarativefolderlistmodel)
179 #include "tst_qdeclarativefolderlistmodel.moc"