Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativefolderlistmodel / tst_qdeclarativefolderlistmodel.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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>
49 #include <QDebug>
50
51 #ifdef Q_OS_SYMBIAN
52 // In Symbian OS test data is located in applications private dir
53 #define SRCDIR "."
54 #endif
55
56 // From qdeclarastivefolderlistmodel.h
57 const int FileNameRole = Qt::UserRole+1;
58 const int FilePathRole = Qt::UserRole+2;
59 enum SortField { Unsorted, Name, Time, Size, Type };
60
61 class tst_qdeclarativefolderlistmodel : public QObject
62 {
63     Q_OBJECT
64 public:
65     tst_qdeclarativefolderlistmodel() : removeStart(0), removeEnd(0) {}
66
67 public slots:
68     void removed(const QModelIndex &, int start, int end) {
69         removeStart = start;
70         removeEnd = end;
71     }
72
73 private slots:
74     void basicProperties();
75     void refresh();
76
77 private:
78     void checkNoErrors(const QDeclarativeComponent& component);
79     QDeclarativeEngine engine;
80
81     int removeStart;
82     int removeEnd;
83 };
84
85 void tst_qdeclarativefolderlistmodel::checkNoErrors(const QDeclarativeComponent& component)
86 {
87     // Wait until the component is ready
88     QTRY_VERIFY(component.isReady() || component.isError());
89
90     if (component.isError()) {
91         QList<QDeclarativeError> errors = component.errors();
92         for (int ii = 0; ii < errors.count(); ++ii) {
93             const QDeclarativeError &error = errors.at(ii);
94             QByteArray errorStr = QByteArray::number(error.line()) + ":" +
95                                   QByteArray::number(error.column()) + ":" +
96                                   error.description().toUtf8();
97             qWarning() << errorStr;
98         }
99     }
100     QVERIFY(!component.isError());
101 }
102
103 void tst_qdeclarativefolderlistmodel::basicProperties()
104 {
105     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml"));
106     checkNoErrors(component);
107
108     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
109     QVERIFY(flm != 0);
110
111     flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data"));
112     QTRY_COMPARE(flm->property("count").toInt(),2); // wait for refresh
113     QCOMPARE(flm->property("folder").toUrl(), QUrl::fromLocalFile(SRCDIR "/data"));
114     QCOMPARE(flm->property("parentFolder").toUrl(), QUrl::fromLocalFile(SRCDIR));
115     QCOMPARE(flm->property("sortField").toInt(), int(Name));
116     QCOMPARE(flm->property("nameFilters").toStringList(), QStringList() << "*.qml");
117     QCOMPARE(flm->property("sortReversed").toBool(), false);
118     QCOMPARE(flm->property("showDirs").toBool(), true);
119     QCOMPARE(flm->property("showDotAndDotDot").toBool(), false);
120     QCOMPARE(flm->property("showOnlyReadable").toBool(), false);
121     QCOMPARE(flm->data(flm->index(0),FileNameRole).toString(), QLatin1String("basic.qml"));
122     QCOMPARE(flm->data(flm->index(1),FileNameRole).toString(), QLatin1String("dummy.qml"));    
123     
124     flm->setProperty("folder",QUrl::fromLocalFile(""));
125     QCOMPARE(flm->property("folder").toUrl(), QUrl::fromLocalFile(""));
126 }
127
128 void tst_qdeclarativefolderlistmodel::refresh()
129 {
130     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml"));
131     checkNoErrors(component);
132
133     QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
134     QVERIFY(flm != 0);
135
136     flm->setProperty("folder",QUrl::fromLocalFile(SRCDIR "/data"));
137     QTRY_COMPARE(flm->property("count").toInt(),2); // wait for refresh
138
139     int count = flm->rowCount();
140
141     connect(flm, SIGNAL(rowsRemoved(const QModelIndex&,int,int)),
142             this, SLOT(removed(const QModelIndex&,int,int)));
143
144     flm->setProperty("sortReversed", true);
145
146     QCOMPARE(removeStart, 0);
147     QCOMPARE(removeEnd, count-1);
148 }
149
150 QTEST_MAIN(tst_qdeclarativefolderlistmodel)
151
152 #include "tst_qdeclarativefolderlistmodel.moc"