Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativefolderlistmodel / tst_qdeclarativefolderlistmodel.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtCore/qdir.h>
46 #include <QtCore/qfile.h>
47 #include <QtCore/qabstractitemmodel.h>
48 #include <QDebug>
49 #include "../../shared/util.h"
50
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 };
55
56 class tst_qdeclarativefolderlistmodel : public QDeclarativeDataTest
57 {
58     Q_OBJECT
59 public:
60     tst_qdeclarativefolderlistmodel() : removeStart(0), removeEnd(0) {}
61
62 public slots:
63     void removed(const QModelIndex &, int start, int end) {
64         removeStart = start;
65         removeEnd = end;
66     }
67
68 private slots:
69     void basicProperties();
70     void resetFiltering();
71     void refresh();
72
73 private:
74     void checkNoErrors(const QDeclarativeComponent& component);
75     QDeclarativeEngine engine;
76
77     int removeStart;
78     int removeEnd;
79 };
80
81 void tst_qdeclarativefolderlistmodel::checkNoErrors(const QDeclarativeComponent& component)
82 {
83     // Wait until the component is ready
84     QTRY_VERIFY(component.isReady() || component.isError());
85
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;
94         }
95     }
96     QVERIFY(!component.isError());
97 }
98
99 void tst_qdeclarativefolderlistmodel::basicProperties()
100 {
101     QDeclarativeComponent component(&engine, testFileUrl("basic.qml"));
102     checkNoErrors(component);
103
104     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
105     QVERIFY(flm != 0);
106
107     flm->setProperty("folder", dataDirectoryUrl());
108     QTRY_COMPARE(flm->property("count").toInt(),4); // wait for refresh
109     QCOMPARE(flm->property("folder").toUrl(), dataDirectoryUrl());
110     QCOMPARE(flm->property("parentFolder").toUrl(), QUrl::fromLocalFile(QDir(directory()).canonicalPath()));
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"));    
119     
120     flm->setProperty("folder",QUrl::fromLocalFile(""));
121     QCOMPARE(flm->property("folder").toUrl(), QUrl::fromLocalFile(""));
122 }
123
124 void tst_qdeclarativefolderlistmodel::resetFiltering()
125 {
126     // see QTBUG-17837
127     QDeclarativeComponent component(&engine, testFileUrl("resetFiltering.qml"));
128     checkNoErrors(component);
129
130     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
131     QVERIFY(flm != 0);
132
133     connect(flm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
134             this, SLOT(removed(const QModelIndex&,int,int)));
135
136     flm->setProperty("folder", testFileUrl("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);
141
142     flm->setProperty("folder", testFileUrl("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);
147
148     flm->setProperty("folder", testFileUrl("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);
153 }
154
155 void tst_qdeclarativefolderlistmodel::refresh()
156 {
157     QDeclarativeComponent component(&engine, testFileUrl("basic.qml"));
158     checkNoErrors(component);
159
160     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
161     QVERIFY(flm != 0);
162
163     flm->setProperty("folder", dataDirectoryUrl());
164     QTRY_COMPARE(flm->property("count").toInt(),4); // wait for refresh
165
166     int count = flm->rowCount();
167
168     connect(flm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
169             this, SLOT(removed(const QModelIndex&,int,int)));
170
171     flm->setProperty("sortReversed", true);
172
173     QCOMPARE(removeStart, 0);
174     QCOMPARE(removeEnd, count-1);
175 }
176
177 QTEST_MAIN(tst_qdeclarativefolderlistmodel)
178
179 #include "tst_qdeclarativefolderlistmodel.moc"