|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include "../../../src/gui/dialogs/qsidebar_p.h" |
|
45 #include "../../../src/gui/dialogs/qfilesystemmodel_p.h" |
|
46 |
|
47 //TESTED_CLASS= |
|
48 //TESTED_FILES= |
|
49 |
|
50 class tst_QSidebar : public QObject { |
|
51 Q_OBJECT |
|
52 |
|
53 public: |
|
54 tst_QSidebar(); |
|
55 virtual ~tst_QSidebar(); |
|
56 |
|
57 public Q_SLOTS: |
|
58 void init(); |
|
59 void cleanup(); |
|
60 |
|
61 private slots: |
|
62 void setUrls(); |
|
63 void selectUrls(); |
|
64 void addUrls(); |
|
65 |
|
66 void goToUrl(); |
|
67 }; |
|
68 |
|
69 tst_QSidebar::tst_QSidebar() |
|
70 { |
|
71 } |
|
72 |
|
73 tst_QSidebar::~tst_QSidebar() |
|
74 { |
|
75 } |
|
76 |
|
77 void tst_QSidebar::init() |
|
78 { |
|
79 } |
|
80 |
|
81 void tst_QSidebar::cleanup() |
|
82 { |
|
83 } |
|
84 |
|
85 void tst_QSidebar::setUrls() |
|
86 { |
|
87 QList<QUrl> urls; |
|
88 QFileSystemModel fsmodel; |
|
89 QSidebar qsidebar; |
|
90 qsidebar.init(&fsmodel, urls); |
|
91 QAbstractItemModel *model = qsidebar.model(); |
|
92 |
|
93 urls << QUrl::fromLocalFile(QDir::rootPath()) |
|
94 << QUrl::fromLocalFile(QDir::temp().absolutePath()); |
|
95 |
|
96 QCOMPARE(model->rowCount(), 0); |
|
97 qsidebar.setUrls(urls); |
|
98 QCOMPARE(qsidebar.urls(), urls); |
|
99 QCOMPARE(model->rowCount(), urls.count()); |
|
100 qsidebar.setUrls(urls); |
|
101 QCOMPARE(model->rowCount(), urls.count()); |
|
102 } |
|
103 |
|
104 void tst_QSidebar::selectUrls() |
|
105 { |
|
106 QList<QUrl> urls; |
|
107 urls << QUrl::fromLocalFile(QDir::rootPath()) |
|
108 << QUrl::fromLocalFile(QDir::temp().absolutePath()); |
|
109 QFileSystemModel fsmodel; |
|
110 QSidebar qsidebar; |
|
111 qsidebar.init(&fsmodel, urls); |
|
112 |
|
113 QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(const QUrl &))); |
|
114 qsidebar.selectUrl(urls.at(0)); |
|
115 QCOMPARE(spy.count(), 0); |
|
116 } |
|
117 |
|
118 void tst_QSidebar::addUrls() |
|
119 { |
|
120 QList<QUrl> emptyUrls; |
|
121 QFileSystemModel fsmodel; |
|
122 QSidebar qsidebar; |
|
123 qsidebar.init(&fsmodel, emptyUrls); |
|
124 QAbstractItemModel *model = qsidebar.model(); |
|
125 #if defined(Q_OS_SYMBIAN) |
|
126 // On Symbian, QDir::rootPath() and QDir::home() are same. |
|
127 QDir testDir = QDir::currentPath(); |
|
128 #else |
|
129 QDir testDir = QDir::home(); |
|
130 #endif |
|
131 |
|
132 // default |
|
133 QCOMPARE(model->rowCount(), 0); |
|
134 |
|
135 QList<QUrl> urls; |
|
136 urls << QUrl::fromLocalFile(QDir::rootPath()) |
|
137 << QUrl::fromLocalFile(QDir::temp().absolutePath()); |
|
138 |
|
139 // test < 0 |
|
140 qsidebar.addUrls(urls, -1); |
|
141 QCOMPARE(model->rowCount(), 2); |
|
142 |
|
143 // test = 0 |
|
144 qsidebar.setUrls(emptyUrls); |
|
145 qsidebar.addUrls(urls, 0); |
|
146 QCOMPARE(model->rowCount(), 2); |
|
147 |
|
148 // test > 0 |
|
149 qsidebar.setUrls(emptyUrls); |
|
150 qsidebar.addUrls(urls, 100); |
|
151 QCOMPARE(model->rowCount(), 2); |
|
152 |
|
153 // test inserting with already existing rows |
|
154 QList<QUrl> moreUrls; |
|
155 moreUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
156 qsidebar.addUrls(moreUrls, -1); |
|
157 QCOMPARE(model->rowCount(), 3); |
|
158 |
|
159 // make sure invalid urls are still added |
|
160 QList<QUrl> badUrls; |
|
161 badUrls << QUrl::fromLocalFile(testDir.absolutePath() + "/I used to exist"); |
|
162 qsidebar.addUrls(badUrls, 0); |
|
163 QCOMPARE(model->rowCount(), 4); |
|
164 |
|
165 // check that every item has text and an icon including the above invalid one |
|
166 for (int i = 0; i < model->rowCount(); ++i) { |
|
167 QVERIFY(!model->index(i, 0).data().toString().isEmpty()); |
|
168 QIcon icon = qvariant_cast<QIcon>(model->index(i, 0).data(Qt::DecorationRole)); |
|
169 QVERIFY(!icon.isNull()); |
|
170 } |
|
171 |
|
172 // test moving up the list |
|
173 qsidebar.setUrls(emptyUrls); |
|
174 qsidebar.addUrls(urls, 100); |
|
175 qsidebar.addUrls(moreUrls, 100); |
|
176 QCOMPARE(model->rowCount(), 3); |
|
177 qsidebar.addUrls(moreUrls, 1); |
|
178 QCOMPARE(qsidebar.urls()[1], moreUrls[0]); |
|
179 |
|
180 // test appending with -1 |
|
181 qsidebar.setUrls(emptyUrls); |
|
182 qsidebar.addUrls(urls, -1); |
|
183 qsidebar.addUrls(moreUrls, -1); |
|
184 QCOMPARE(qsidebar.urls()[0], urls[0]); |
|
185 |
|
186 QList<QUrl> doubleUrls; |
|
187 //tow exact same paths, we have only one entry |
|
188 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
189 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
190 qsidebar.setUrls(emptyUrls); |
|
191 qsidebar.addUrls(doubleUrls, 1); |
|
192 QCOMPARE(qsidebar.urls().size(), 1); |
|
193 |
|
194 // Two paths that are effectively pointing to the same location |
|
195 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
196 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath() + "/."); |
|
197 qsidebar.setUrls(emptyUrls); |
|
198 qsidebar.addUrls(doubleUrls, 1); |
|
199 QCOMPARE(qsidebar.urls().size(), 1); |
|
200 |
|
201 #if defined(Q_OS_WIN) |
|
202 //Windows is case insensitive so no duplicate entries in that case |
|
203 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
204 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath().toUpper()); |
|
205 qsidebar.setUrls(emptyUrls); |
|
206 qsidebar.addUrls(doubleUrls, 1); |
|
207 QCOMPARE(qsidebar.urls().size(), 1); |
|
208 #else |
|
209 //Two different paths we should have two entries |
|
210 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath()); |
|
211 doubleUrls << QUrl::fromLocalFile(testDir.absolutePath().toUpper()); |
|
212 qsidebar.setUrls(emptyUrls); |
|
213 qsidebar.addUrls(doubleUrls, 1); |
|
214 QCOMPARE(qsidebar.urls().size(), 2); |
|
215 #endif |
|
216 } |
|
217 |
|
218 void tst_QSidebar::goToUrl() |
|
219 { |
|
220 QList<QUrl> urls; |
|
221 urls << QUrl::fromLocalFile(QDir::rootPath()) |
|
222 << QUrl::fromLocalFile(QDir::temp().absolutePath()); |
|
223 QFileSystemModel fsmodel; |
|
224 QSidebar qsidebar; |
|
225 qsidebar.init(&fsmodel, urls); |
|
226 qsidebar.show(); |
|
227 |
|
228 QSignalSpy spy(&qsidebar, SIGNAL(goToUrl(const QUrl &))); |
|
229 QTest::mousePress(qsidebar.viewport(), Qt::LeftButton, 0, qsidebar.visualRect(qsidebar.model()->index(0, 0)).center()); |
|
230 QCOMPARE(spy.count(), 1); |
|
231 QCOMPARE((spy.value(0)).at(0).toUrl(), urls.first()); |
|
232 } |
|
233 |
|
234 QTEST_MAIN(tst_QSidebar) |
|
235 #include "tst_qsidebar.moc" |
|
236 |