author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 <QtGui/QtGui> |
|
45 |
#include <qeventloop.h> |
|
46 |
#include <qlist.h> |
|
47 |
||
48 |
#include <qlistwidget.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
#include <private/qlistwidget_p.h> |
0 | 50 |
|
51 |
//TESTED_CLASS= |
|
52 |
//TESTED_FILES= |
|
53 |
||
54 |
class tst_QListWidget : public QObject |
|
55 |
{ |
|
56 |
Q_OBJECT |
|
57 |
||
58 |
public: |
|
59 |
tst_QListWidget(); |
|
60 |
~tst_QListWidget(); |
|
61 |
||
62 |
enum ModelChanged { |
|
63 |
RowsAboutToBeInserted, |
|
64 |
RowsInserted, |
|
65 |
RowsAboutToBeRemoved, |
|
66 |
RowsRemoved, |
|
67 |
ColumnsAboutToBeInserted, |
|
68 |
ColumnsInserted, |
|
69 |
ColumnsAboutToBeRemoved, |
|
70 |
ColumnsRemoved |
|
71 |
}; |
|
72 |
||
73 |
public slots: |
|
74 |
void initTestCase(); |
|
75 |
void cleanupTestCase(); |
|
76 |
void init(); |
|
77 |
void cleanup(); |
|
78 |
||
79 |
private slots: |
|
80 |
void addItem(); |
|
81 |
void addItem2(); |
|
82 |
void addItems(); |
|
83 |
void openPersistentEditor(); |
|
84 |
void closePersistentEditor(); |
|
85 |
void count(); |
|
86 |
void currentItem(); |
|
87 |
void setCurrentItem_data(); |
|
88 |
void setCurrentItem(); |
|
89 |
void currentRow(); |
|
90 |
void setCurrentRow_data(); |
|
91 |
void setCurrentRow(); |
|
92 |
void editItem_data(); |
|
93 |
void editItem(); |
|
94 |
void findItems(); |
|
95 |
void insertItem_data(); |
|
96 |
void insertItem(); |
|
97 |
void insertItems_data(); |
|
98 |
void insertItems(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
void moveItemsPriv_data(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
void moveItemsPriv(); |
0 | 101 |
|
102 |
void itemAssignment(); |
|
103 |
void item_data(); |
|
104 |
void item(); |
|
105 |
void takeItem_data(); |
|
106 |
void takeItem(); |
|
107 |
void setItemHidden(); |
|
108 |
void selectedItems_data(); |
|
109 |
void selectedItems(); |
|
110 |
void removeItems_data(); |
|
111 |
void removeItems(); |
|
112 |
void itemStreaming_data(); |
|
113 |
void itemStreaming(); |
|
114 |
void sortItems_data(); |
|
115 |
void sortItems(); |
|
116 |
void sortHiddenItems(); |
|
117 |
void sortHiddenItems_data(); |
|
118 |
void closeEditor(); |
|
119 |
void setData_data(); |
|
120 |
void setData(); |
|
121 |
void insertItemsWithSorting_data(); |
|
122 |
void insertItemsWithSorting(); |
|
123 |
void changeDataWithSorting_data(); |
|
124 |
void changeDataWithSorting(); |
|
125 |
void itemData(); |
|
126 |
void itemWidget(); |
|
127 |
#ifndef Q_WS_MAC |
|
128 |
void fastScroll(); |
|
129 |
#endif |
|
130 |
void insertUnchanged(); |
|
131 |
void setSortingEnabled(); |
|
132 |
void task199503_crashWhenCleared(); |
|
133 |
void task217070_scrollbarsAdjusted(); |
|
134 |
void task258949_keypressHangup(); |
|
135 |
||
136 |
||
137 |
protected slots: |
|
138 |
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last) |
|
139 |
{ modelChanged(RowsAboutToBeInserted, parent, first, last); } |
|
140 |
void rowsInserted(const QModelIndex &parent, int first, int last) |
|
141 |
{ modelChanged(RowsInserted, parent, first, last); } |
|
142 |
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last) |
|
143 |
{ modelChanged(RowsAboutToBeRemoved, parent, first, last); } |
|
144 |
void rowsRemoved(const QModelIndex &parent, int first, int last) |
|
145 |
{ modelChanged(RowsRemoved, parent, first, last); } |
|
146 |
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last) |
|
147 |
{ modelChanged(ColumnsAboutToBeInserted, parent, first, last); } |
|
148 |
void columnsInserted(const QModelIndex &parent, int first, int last) |
|
149 |
{ modelChanged(ColumnsInserted, parent, first, last); } |
|
150 |
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last) |
|
151 |
{ modelChanged(ColumnsAboutToBeRemoved, parent, first, last); } |
|
152 |
void columnsRemoved(const QModelIndex &parent, int first, int last) |
|
153 |
{ modelChanged(ColumnsRemoved, parent, first, last); } |
|
154 |
||
155 |
void modelChanged(ModelChanged change, const QModelIndex &parent, int first, int last); |
|
156 |
||
157 |
private: |
|
158 |
QListWidget *testWidget; |
|
159 |
QVector<QModelIndex> rcParent; |
|
160 |
QVector<int> rcFirst; |
|
161 |
QVector<int> rcLast; |
|
162 |
||
163 |
void populate(); |
|
164 |
void checkDefaultValues(); |
|
165 |
}; |
|
166 |
||
167 |
||
168 |
typedef QList<int> IntList; |
|
169 |
Q_DECLARE_METATYPE(IntList) |
|
170 |
Q_DECLARE_METATYPE(QVariantList) |
|
171 |
||
172 |
tst_QListWidget::tst_QListWidget(): testWidget(0), rcParent(8), rcFirst(8,0), rcLast(8,0) |
|
173 |
{ |
|
174 |
} |
|
175 |
||
176 |
tst_QListWidget::~tst_QListWidget() |
|
177 |
{ |
|
178 |
} |
|
179 |
||
180 |
void tst_QListWidget::initTestCase() |
|
181 |
{ |
|
182 |
testWidget = new QListWidget(); |
|
183 |
testWidget->show(); |
|
184 |
||
185 |
connect(testWidget->model(), SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), |
|
186 |
this, SLOT(rowsAboutToBeInserted(QModelIndex, int, int))); |
|
187 |
connect(testWidget->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), |
|
188 |
this, SLOT(rowsInserted(QModelIndex, int, int))); |
|
189 |
connect(testWidget->model(), SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), |
|
190 |
this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int))); |
|
191 |
connect(testWidget->model(), SIGNAL(rowsRemoved(QModelIndex, int, int)), |
|
192 |
this, SLOT(rowsRemoved(QModelIndex, int, int))); |
|
193 |
||
194 |
connect(testWidget->model(), SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)), |
|
195 |
this, SLOT(columnsAboutToBeInserted(QModelIndex, int, int))); |
|
196 |
connect(testWidget->model(), SIGNAL(columnsInserted(QModelIndex, int, int)), |
|
197 |
this, SLOT(columnsInserted(QModelIndex, int, int))); |
|
198 |
connect(testWidget->model(), SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)), |
|
199 |
this, SLOT(columnsAboutToBeRemoved(QModelIndex, int, int))); |
|
200 |
connect(testWidget->model(), SIGNAL(columnsRemoved(QModelIndex, int, int)), |
|
201 |
this, SLOT(columnsRemoved(QModelIndex, int, int))); |
|
202 |
||
203 |
checkDefaultValues(); |
|
204 |
} |
|
205 |
||
206 |
void tst_QListWidget::cleanupTestCase() |
|
207 |
{ |
|
208 |
delete testWidget; |
|
209 |
} |
|
210 |
||
211 |
void tst_QListWidget::init() |
|
212 |
{ |
|
213 |
testWidget->clear(); |
|
214 |
||
215 |
if (testWidget->viewport()->children().count() > 0) { |
|
216 |
QEventLoop eventLoop; |
|
217 |
for (int i=0; i < testWidget->viewport()->children().count(); ++i) |
|
218 |
connect(testWidget->viewport()->children().at(i), SIGNAL(destroyed()), &eventLoop, SLOT(quit())); |
|
219 |
QTimer::singleShot(100, &eventLoop, SLOT(quit())); |
|
220 |
eventLoop.exec(); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
void tst_QListWidget::checkDefaultValues() |
|
225 |
{ |
|
226 |
QCOMPARE(testWidget->currentItem(), (QListWidgetItem *)0); |
|
227 |
QCOMPARE(testWidget->currentRow(), -1); |
|
228 |
QCOMPARE(testWidget->count(), 0); |
|
229 |
} |
|
230 |
||
231 |
void tst_QListWidget::cleanup() |
|
232 |
{ |
|
233 |
} |
|
234 |
||
235 |
void tst_QListWidget::populate() |
|
236 |
{ |
|
237 |
addItem(); |
|
238 |
addItem2(); |
|
239 |
addItems(); |
|
240 |
setItemHidden(); |
|
241 |
||
242 |
testWidget->setCurrentIndex(testWidget->model()->index(0,0)); |
|
243 |
||
244 |
// setCurrentItem(); |
|
245 |
// setCurrentRow(); |
|
246 |
} |
|
247 |
||
248 |
void tst_QListWidget::addItem() |
|
249 |
{ |
|
250 |
int count = testWidget->count(); |
|
251 |
QString label = QString("%1").arg(count); |
|
252 |
testWidget->addItem(label); |
|
253 |
QCOMPARE(testWidget->count(), ++count); |
|
254 |
QCOMPARE(testWidget->item(testWidget->count()-1)->text(), label); |
|
255 |
} |
|
256 |
||
257 |
void tst_QListWidget::addItem2() |
|
258 |
{ |
|
259 |
int count = testWidget->count(); |
|
260 |
||
261 |
// Boundry Checking |
|
262 |
testWidget->addItem(0); |
|
263 |
QCOMPARE(testWidget->count(), count); |
|
264 |
||
265 |
QListWidgetItem *item = new QListWidgetItem(QString("%1").arg(count)); |
|
266 |
item->setFlags(item->flags() | Qt::ItemIsEditable); |
|
267 |
testWidget->addItem(item); |
|
268 |
QCOMPARE(testWidget->count(), ++count); |
|
269 |
QCOMPARE(testWidget->item(testWidget->count()-1), item); |
|
270 |
QCOMPARE(testWidget->isItemHidden(item), false); |
|
271 |
} |
|
272 |
||
273 |
void tst_QListWidget::addItems() |
|
274 |
{ |
|
275 |
int count = testWidget->count(); |
|
276 |
||
277 |
// Boundry Checking |
|
278 |
testWidget->addItems(QStringList()); |
|
279 |
QCOMPARE(testWidget->count(), count); |
|
280 |
||
281 |
QStringList stringList; |
|
282 |
QString label = QString("%1").arg(count); |
|
283 |
stringList << QString("%1").arg(testWidget->count() + 1) |
|
284 |
<< QString("%1").arg(testWidget->count() + 2) |
|
285 |
<< QString("%1").arg(testWidget->count() + 3) |
|
286 |
<< label; |
|
287 |
testWidget->addItems(stringList); |
|
288 |
QCOMPARE(testWidget->count(), count + stringList.count()); |
|
289 |
QCOMPARE(testWidget->item(testWidget->count()-1)->text(), label); |
|
290 |
} |
|
291 |
||
292 |
||
293 |
void tst_QListWidget::openPersistentEditor() |
|
294 |
{ |
|
295 |
// Boundry checking |
|
296 |
testWidget->openPersistentEditor(0); |
|
297 |
QListWidgetItem *item = new QListWidgetItem(QString("%1").arg(testWidget->count())); |
|
298 |
testWidget->openPersistentEditor(item); |
|
299 |
||
300 |
int childCount = testWidget->viewport()->children().count(); |
|
301 |
testWidget->addItem(item); |
|
302 |
testWidget->openPersistentEditor(item); |
|
303 |
QCOMPARE(childCount + 1, testWidget->viewport()->children().count()); |
|
304 |
} |
|
305 |
||
306 |
void tst_QListWidget::closePersistentEditor() |
|
307 |
{ |
|
308 |
#if defined(Q_OS_SYMBIAN) |
|
309 |
//give the Symbian app start event queue time to clear |
|
310 |
QTest::qWait(1000); |
|
311 |
#endif |
|
312 |
||
313 |
// Boundry checking |
|
314 |
int childCount = testWidget->viewport()->children().count(); |
|
315 |
testWidget->closePersistentEditor(0); |
|
316 |
QListWidgetItem *item = new QListWidgetItem(QString("%1").arg(testWidget->count())); |
|
317 |
testWidget->closePersistentEditor(item); |
|
318 |
QCOMPARE(childCount, testWidget->viewport()->children().count()); |
|
319 |
||
320 |
// Create something |
|
321 |
testWidget->addItem(item); |
|
322 |
testWidget->openPersistentEditor(item); |
|
323 |
||
324 |
// actual test |
|
325 |
childCount = testWidget->viewport()->children().count(); |
|
326 |
testWidget->closePersistentEditor(item); |
|
327 |
// Spin the event loop and hopefully it will die. |
|
328 |
QEventLoop eventLoop; |
|
329 |
for (int i=0; i < childCount; ++i) |
|
330 |
connect(testWidget->viewport()->children().at(i), SIGNAL(destroyed()), &eventLoop, SLOT(quit())); |
|
331 |
QTimer::singleShot(100, &eventLoop, SLOT(quit())); |
|
332 |
eventLoop.exec(); |
|
333 |
QCOMPARE(testWidget->viewport()->children().count(), childCount - 1); |
|
334 |
} |
|
335 |
||
336 |
void tst_QListWidget::setItemHidden() |
|
337 |
{ |
|
338 |
// Boundry checking |
|
339 |
testWidget->setItemHidden(0, true); |
|
340 |
testWidget->setItemHidden(0, false); |
|
341 |
||
342 |
int totalHidden = 0; |
|
343 |
for (int i = 0; i < testWidget->model()->rowCount(); ++i) |
|
344 |
if (testWidget->isItemHidden(testWidget->item(i))) |
|
345 |
totalHidden++; |
|
346 |
||
347 |
QListWidgetItem *item = new QListWidgetItem(QString("%1").arg(testWidget->count())); |
|
348 |
testWidget->addItem(item); |
|
349 |
||
350 |
// Check that nothing else changed |
|
351 |
int newTotal = 0; |
|
352 |
for (int i = 0; i < testWidget->model()->rowCount(); ++i) |
|
353 |
if (testWidget->isItemHidden(testWidget->item(i))) |
|
354 |
newTotal++; |
|
355 |
QCOMPARE(newTotal, totalHidden); |
|
356 |
||
357 |
testWidget->setItemHidden(item, true); |
|
358 |
QCOMPARE(testWidget->isItemHidden(item), true); |
|
359 |
||
360 |
// Check that nothing else changed |
|
361 |
newTotal = 0; |
|
362 |
for (int i = 0; i < testWidget->model()->rowCount(); ++i) |
|
363 |
if (testWidget->isItemHidden(testWidget->item(i))) |
|
364 |
newTotal++; |
|
365 |
QCOMPARE(newTotal, totalHidden + 1); |
|
366 |
||
367 |
testWidget->setItemHidden(item, false); |
|
368 |
QCOMPARE(testWidget->isItemHidden(item), false); |
|
369 |
||
370 |
// Check that nothing else changed |
|
371 |
newTotal = 0; |
|
372 |
for (int i = 0; i < testWidget->model()->rowCount(); ++i) |
|
373 |
if (testWidget->isItemHidden(testWidget->item(i))) |
|
374 |
newTotal++; |
|
375 |
QCOMPARE(newTotal, totalHidden); |
|
376 |
||
377 |
testWidget->setItemHidden(item, true); |
|
378 |
} |
|
379 |
||
380 |
void tst_QListWidget::setCurrentItem_data() |
|
381 |
{ |
|
382 |
QTest::addColumn<int>("fill"); |
|
383 |
QTest::newRow("HasItems: 0") << 0; |
|
384 |
QTest::newRow("HasItems: 1") << 1; |
|
385 |
QTest::newRow("HasItems: 2") << 2; |
|
386 |
QTest::newRow("HasItems: 3") << 3; |
|
387 |
} |
|
388 |
||
389 |
void tst_QListWidget::setCurrentItem() |
|
390 |
{ |
|
391 |
QFETCH(int, fill); |
|
392 |
for (int i = 0; i < fill; ++i) |
|
393 |
testWidget->addItem(QString("%1").arg(i)); |
|
394 |
||
395 |
// Boundry checking |
|
396 |
testWidget->setCurrentItem((QListWidgetItem *)0); |
|
397 |
QCOMPARE((QListWidgetItem *)0, testWidget->currentItem()); |
|
398 |
QListWidgetItem item; |
|
399 |
testWidget->setCurrentItem(&item); |
|
400 |
QCOMPARE((QListWidgetItem *)0, testWidget->currentItem()); |
|
401 |
||
402 |
// Make sure that currentItem changes to what is passed into setCurrentItem |
|
403 |
for (int i = 0; i < testWidget->count(); ++i) { |
|
404 |
testWidget->setCurrentItem(testWidget->item(i)); |
|
405 |
for (int j = 0; j < testWidget->count(); ++j) { |
|
406 |
testWidget->setCurrentItem(testWidget->item(j)); |
|
407 |
QCOMPARE(testWidget->item(j), testWidget->currentItem()); |
|
408 |
} |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
void tst_QListWidget::setCurrentRow_data() |
|
413 |
{ |
|
414 |
QTest::addColumn<int>("fill"); |
|
415 |
QTest::newRow("HasItems: 0") << 0; |
|
416 |
QTest::newRow("HasItems: 1") << 1; |
|
417 |
QTest::newRow("HasItems: 2") << 2; |
|
418 |
QTest::newRow("HasItems: 3") << 3; |
|
419 |
} |
|
420 |
||
421 |
void tst_QListWidget::setCurrentRow() |
|
422 |
{ |
|
423 |
QFETCH(int, fill); |
|
424 |
for (int i = 0; i < fill; ++i) |
|
425 |
testWidget->addItem(QString("%1").arg(i)); |
|
426 |
||
427 |
// Boundry checking |
|
428 |
testWidget->setCurrentRow(-1); |
|
429 |
QCOMPARE(-1, testWidget->currentRow()); |
|
430 |
testWidget->setCurrentRow(testWidget->count()); |
|
431 |
QCOMPARE(-1, testWidget->currentRow()); |
|
432 |
||
433 |
// Make sure that currentRow changes to what is passed into setCurrentRow |
|
434 |
for (int i = 0; i < testWidget->count(); ++i) { |
|
435 |
testWidget->setCurrentRow(i); |
|
436 |
for (int j = 0; j < testWidget->count(); ++j) { |
|
437 |
testWidget->setCurrentRow(j); |
|
438 |
QCOMPARE(j, testWidget->currentRow()); |
|
439 |
} |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
void tst_QListWidget::count() |
|
444 |
{ |
|
445 |
populate(); |
|
446 |
||
447 |
// actual test |
|
448 |
QCOMPARE(testWidget->model()->rowCount(), testWidget->count()); |
|
449 |
} |
|
450 |
||
451 |
void tst_QListWidget::currentItem() |
|
452 |
{ |
|
453 |
populate(); |
|
454 |
||
455 |
// actual test |
|
456 |
QModelIndex currentIndex = testWidget->selectionModel()->currentIndex(); |
|
457 |
if (currentIndex.isValid()) |
|
458 |
QVERIFY(testWidget->currentItem() == testWidget->item(currentIndex.row())); |
|
459 |
else |
|
460 |
QVERIFY(testWidget->currentItem() == (QListWidgetItem*)0); |
|
461 |
} |
|
462 |
||
463 |
void tst_QListWidget::currentRow() |
|
464 |
{ |
|
465 |
populate(); |
|
466 |
||
467 |
// actual test |
|
468 |
QModelIndex currentIndex = testWidget->selectionModel()->currentIndex(); |
|
469 |
if (currentIndex.isValid()) |
|
470 |
QCOMPARE(testWidget->currentRow(), currentIndex.row()); |
|
471 |
else |
|
472 |
QCOMPARE(testWidget->currentRow(), -1); |
|
473 |
} |
|
474 |
||
475 |
void tst_QListWidget::editItem_data() |
|
476 |
{ |
|
477 |
QTest::addColumn<bool>("editable"); |
|
478 |
QTest::newRow("editable") << true; |
|
479 |
QTest::newRow("not editable") << false; |
|
480 |
} |
|
481 |
||
482 |
void tst_QListWidget::editItem() |
|
483 |
{ |
|
484 |
// Boundry checking |
|
485 |
testWidget->editItem(0); |
|
486 |
QListWidgetItem *item = new QListWidgetItem(QString("%1").arg(testWidget->count())); |
|
487 |
testWidget->editItem(item); |
|
488 |
||
489 |
QFETCH(bool, editable); |
|
490 |
if (editable) |
|
491 |
item->setFlags(item->flags() | Qt::ItemIsEditable); |
|
492 |
testWidget->addItem(item); |
|
493 |
||
494 |
int childCount = testWidget->viewport()->children().count(); |
|
495 |
QWidget *existsAlready = testWidget->indexWidget(testWidget->model()->index(testWidget->row(item), 0)); |
|
496 |
testWidget->editItem(item); |
|
497 |
Qt::ItemFlags flags = item->flags(); |
|
498 |
||
499 |
// There doesn't seem to be a way to detect if the item has already been edited... |
|
500 |
if (!existsAlready && flags & Qt::ItemIsEditable && flags & Qt::ItemIsEnabled) { |
|
501 |
QList<QObject *> children = testWidget->viewport()->children(); |
|
502 |
QVERIFY(children.count() > childCount); |
|
503 |
bool found = false; |
|
504 |
for (int i = 0; i < children.size(); ++i) { |
|
505 |
if (children.at(i)->inherits("QExpandingLineEdit")) |
|
506 |
found = true; |
|
507 |
} |
|
508 |
QVERIFY(found); |
|
509 |
} else { |
|
510 |
QCOMPARE(testWidget->viewport()->children().count(), childCount); |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
void tst_QListWidget::findItems() |
|
515 |
{ |
|
516 |
// This really just tests that the items that are returned are converted from index's to items correctly. |
|
517 |
||
518 |
// Boundry checking |
|
519 |
QCOMPARE(testWidget->findItems("GirlsCanWearJeansAndCutTheirHairShort", Qt::MatchExactly).count(), 0); |
|
520 |
||
521 |
populate(); |
|
522 |
||
523 |
for (int i=0; i < testWidget->count(); ++i) |
|
524 |
QCOMPARE(testWidget->findItems( (testWidget->item(i)->text()), Qt::MatchExactly).count(), 1); |
|
525 |
} |
|
526 |
||
527 |
||
528 |
void tst_QListWidget::insertItem_data() |
|
529 |
{ |
|
530 |
QTest::addColumn<QStringList>("initialItems"); |
|
531 |
QTest::addColumn<int>("insertIndex"); |
|
532 |
QTest::addColumn<QString>("itemLabel"); |
|
533 |
QTest::addColumn<int>("expectedIndex"); |
|
534 |
||
535 |
QStringList initialItems; |
|
536 |
initialItems << "foo" << "bar"; |
|
537 |
||
538 |
QTest::newRow("Insert less then 0") << initialItems << -1 << "inserted" << 0; |
|
539 |
QTest::newRow("Insert at 0") << initialItems << 0 << "inserted" << 0; |
|
540 |
QTest::newRow("Insert beyond count") << initialItems << initialItems.count()+1 << "inserted" << initialItems.count(); |
|
541 |
QTest::newRow("Insert at count") << initialItems << initialItems.count() << "inserted" << initialItems.count(); |
|
542 |
QTest::newRow("Insert in the middle") << initialItems << 1 << "inserted" << 1; |
|
543 |
} |
|
544 |
||
545 |
void tst_QListWidget::insertItem() |
|
546 |
{ |
|
547 |
QFETCH(QStringList, initialItems); |
|
548 |
QFETCH(int, insertIndex); |
|
549 |
QFETCH(QString, itemLabel); |
|
550 |
QFETCH(int, expectedIndex); |
|
551 |
||
552 |
testWidget->insertItems(0, initialItems); |
|
553 |
QCOMPARE(testWidget->count(), initialItems.count()); |
|
554 |
||
555 |
testWidget->insertItem(insertIndex, itemLabel); |
|
556 |
||
557 |
QCOMPARE(rcFirst[RowsAboutToBeInserted], expectedIndex); |
|
558 |
QCOMPARE(rcLast[RowsAboutToBeInserted], expectedIndex); |
|
559 |
QCOMPARE(rcFirst[RowsInserted], expectedIndex); |
|
560 |
QCOMPARE(rcLast[RowsInserted], expectedIndex); |
|
561 |
||
562 |
QCOMPARE(testWidget->count(), initialItems.count() + 1); |
|
563 |
QCOMPARE(testWidget->item(expectedIndex)->text(), itemLabel); |
|
564 |
} |
|
565 |
||
566 |
void tst_QListWidget::insertItems_data() |
|
567 |
{ |
|
568 |
QTest::addColumn<int>("rowCount"); |
|
569 |
QTest::addColumn<int>("insertType"); |
|
570 |
||
571 |
QTest::newRow("Insert 1 item using constructor") << 1 << 0; |
|
572 |
QTest::newRow("Insert 10 items using constructor") << 10 << 0; |
|
573 |
QTest::newRow("Insert 100 items using constructor") << 100 << 0; |
|
574 |
||
575 |
QTest::newRow("Insert 1 item with insertItem") << 1 << 1; |
|
576 |
QTest::newRow("Insert 10 items with insertItem") << 10 << 1; |
|
577 |
QTest::newRow("Insert 100 items with insertItem") << 100 << 1; |
|
578 |
||
579 |
QTest::newRow("Insert/Create 1 item using insertItem") << 1 << 2; |
|
580 |
QTest::newRow("Insert/Create 10 items using insertItem") << 10 << 2; |
|
581 |
QTest::newRow("Insert/Create 100 items using insertItem") << 100 << 2; |
|
582 |
||
583 |
QTest::newRow("Insert 0 items with insertItems") << 0 << 3; |
|
584 |
QTest::newRow("Insert 1 item with insertItems") << 1 << 3; |
|
585 |
QTest::newRow("Insert 10 items with insertItems") << 10 << 3; |
|
586 |
QTest::newRow("Insert 100 items with insertItems") << 100 << 3; |
|
587 |
} |
|
588 |
||
589 |
void tst_QListWidget::insertItems() |
|
590 |
{ |
|
591 |
QFETCH(int, rowCount); |
|
592 |
QFETCH(int, insertType); |
|
593 |
||
594 |
if (insertType == 3) { |
|
595 |
QStringList strings; |
|
596 |
for (int i=0; i<rowCount; ++i) |
|
597 |
strings << QString::number(i); |
|
598 |
testWidget->insertItems(0, strings); |
|
599 |
} else { |
|
600 |
for (int r = 0; r < rowCount; ++r) { |
|
601 |
if (insertType == 0) { |
|
602 |
// insert with QListWidgetItem constructor |
|
603 |
new QListWidgetItem(QString::number(r), testWidget); |
|
604 |
} else if (insertType == 1) { |
|
605 |
// insert actual item |
|
606 |
testWidget->insertItem(r, new QListWidgetItem(QString::number(r))); |
|
607 |
} else if (insertType == 2) { |
|
608 |
// insert/creating with string |
|
609 |
testWidget->insertItem(r, QString::number(r)); |
|
610 |
} else if (insertType == 3) { |
|
611 |
QStringList strings; |
|
612 |
for (int i=0; i<rowCount; ++i) |
|
613 |
strings << QString::number(i); |
|
614 |
testWidget->insertItems(0, strings); |
|
615 |
break; |
|
616 |
} else { |
|
617 |
QVERIFY(0); |
|
618 |
} |
|
619 |
} |
|
620 |
} |
|
621 |
// compare the results |
|
622 |
QCOMPARE(testWidget->count(), rowCount); |
|
623 |
||
624 |
// check if the text |
|
625 |
for (int r = 0; r < rowCount; ++r) |
|
626 |
QCOMPARE(testWidget->item(r)->text(), QString::number(r)); |
|
627 |
||
628 |
// make sure all items have view set correctly |
|
629 |
for (int i=0; i<testWidget->count(); ++i) |
|
630 |
QCOMPARE(testWidget->item(i)->listWidget(), testWidget); |
|
631 |
} |
|
632 |
||
633 |
void tst_QListWidget::itemAssignment() |
|
634 |
{ |
|
635 |
QListWidgetItem itemInWidget("inWidget", testWidget); |
|
636 |
itemInWidget.setFlags(itemInWidget.flags() | Qt::ItemIsTristate); |
|
637 |
QListWidgetItem itemOutsideWidget("outsideWidget"); |
|
638 |
||
639 |
QVERIFY(itemInWidget.listWidget()); |
|
640 |
QCOMPARE(itemInWidget.text(), QString("inWidget")); |
|
641 |
QVERIFY(itemInWidget.flags() & Qt::ItemIsTristate); |
|
642 |
||
643 |
QVERIFY(!itemOutsideWidget.listWidget()); |
|
644 |
QCOMPARE(itemOutsideWidget.text(), QString("outsideWidget")); |
|
645 |
QVERIFY(!(itemOutsideWidget.flags() & Qt::ItemIsTristate)); |
|
646 |
||
647 |
itemOutsideWidget = itemInWidget; |
|
648 |
QVERIFY(!itemOutsideWidget.listWidget()); |
|
649 |
QCOMPARE(itemOutsideWidget.text(), QString("inWidget")); |
|
650 |
QVERIFY(itemOutsideWidget.flags() & Qt::ItemIsTristate); |
|
651 |
} |
|
652 |
||
653 |
void tst_QListWidget::item_data() |
|
654 |
{ |
|
655 |
QTest::addColumn<int>("row"); |
|
656 |
QTest::addColumn<bool>("outOfBounds"); |
|
657 |
||
658 |
QTest::newRow("First item, row: 0") << 0 << false; |
|
659 |
QTest::newRow("Middle item, row: 1") << 1 << false; |
|
660 |
QTest::newRow("Last item, row: 2") << 2 << false; |
|
661 |
QTest::newRow("Out of bounds, row: -1") << -1 << true; |
|
662 |
QTest::newRow("Out of bounds, row: 3") << 3 << true; |
|
663 |
} |
|
664 |
||
665 |
void tst_QListWidget::item() |
|
666 |
{ |
|
667 |
QFETCH(int, row); |
|
668 |
QFETCH(bool, outOfBounds); |
|
669 |
||
670 |
(new QListWidgetItem(testWidget))->setText("item0"); |
|
671 |
(new QListWidgetItem(testWidget))->setText("item1"); |
|
672 |
(new QListWidgetItem(testWidget))->setText("item2"); |
|
673 |
||
674 |
QCOMPARE(testWidget->count(), 3); |
|
675 |
||
676 |
QListWidgetItem *item = testWidget->item(row); |
|
677 |
if (outOfBounds) { |
|
678 |
QCOMPARE(item, static_cast<QListWidgetItem*>(0)); |
|
679 |
QCOMPARE(testWidget->count(), 3); |
|
680 |
} else { |
|
681 |
QCOMPARE(item->text(), QString("item%1").arg(row)); |
|
682 |
QCOMPARE(testWidget->count(), 3); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
void tst_QListWidget::takeItem_data() |
|
687 |
{ |
|
688 |
QTest::addColumn<int>("row"); |
|
689 |
QTest::addColumn<bool>("outOfBounds"); |
|
690 |
||
691 |
QTest::newRow("First item, row: 0") << 0 << false; |
|
692 |
QTest::newRow("Middle item, row: 1") << 1 << false; |
|
693 |
QTest::newRow("Last item, row: 2") << 2 << false; |
|
694 |
QTest::newRow("Out of bounds, row: -1") << -1 << true; |
|
695 |
QTest::newRow("Out of bounds, row: 3") << 3 << true; |
|
696 |
} |
|
697 |
||
698 |
void tst_QListWidget::takeItem() |
|
699 |
{ |
|
700 |
QFETCH(int, row); |
|
701 |
QFETCH(bool, outOfBounds); |
|
702 |
||
703 |
(new QListWidgetItem(testWidget))->setText("item0"); |
|
704 |
(new QListWidgetItem(testWidget))->setText("item1"); |
|
705 |
(new QListWidgetItem(testWidget))->setText("item2"); |
|
706 |
||
707 |
QCOMPARE(testWidget->count(), 3); |
|
708 |
||
709 |
QListWidgetItem *item = testWidget->takeItem(row); |
|
710 |
if (outOfBounds) { |
|
711 |
QCOMPARE(item, static_cast<QListWidgetItem*>(0)); |
|
712 |
QCOMPARE(testWidget->count(), 3); |
|
713 |
} else { |
|
714 |
QCOMPARE(item->text(), QString("item%1").arg(row)); |
|
715 |
QCOMPARE(testWidget->count(), 2); |
|
716 |
} |
|
717 |
||
718 |
delete item; |
|
719 |
} |
|
720 |
||
721 |
void tst_QListWidget::selectedItems_data() |
|
722 |
{ |
|
723 |
QTest::addColumn<int>("itemCount"); |
|
724 |
QTest::addColumn<IntList>("hiddenRows"); |
|
725 |
QTest::addColumn<IntList>("selectedRows"); |
|
726 |
QTest::addColumn<IntList>("expectedRows"); |
|
727 |
||
728 |
||
729 |
QTest::newRow("none hidden, none selected") |
|
730 |
<< 3 |
|
731 |
<< IntList() |
|
732 |
<< IntList() |
|
733 |
<< IntList(); |
|
734 |
||
735 |
QTest::newRow("none hidden, all selected") |
|
736 |
<< 3 |
|
737 |
<< IntList() |
|
738 |
<< (IntList() << 0 << 1 << 2) |
|
739 |
<< (IntList() << 0 << 1 << 2); |
|
740 |
||
741 |
QTest::newRow("first hidden, all selected") |
|
742 |
<< 3 |
|
743 |
<< (IntList() << 0) |
|
744 |
<< (IntList() << 0 << 1 << 2) |
|
745 |
<< (IntList() << 0 << 1 << 2); |
|
746 |
||
747 |
QTest::newRow("last hidden, all selected") |
|
748 |
<< 3 |
|
749 |
<< (IntList() << 2) |
|
750 |
<< (IntList() << 0 << 1 << 2) |
|
751 |
<< (IntList() << 0 << 1 << 2); |
|
752 |
||
753 |
QTest::newRow("middle hidden, all selected") |
|
754 |
<< 3 |
|
755 |
<< (IntList() << 1) |
|
756 |
<< (IntList() << 0 << 1 << 2) |
|
757 |
<< (IntList() << 0 << 1 << 2); |
|
758 |
||
759 |
QTest::newRow("all hidden, all selected") |
|
760 |
<< 3 |
|
761 |
<< (IntList() << 0 << 1 << 2) |
|
762 |
<< (IntList() << 0 << 1 << 2) |
|
763 |
<< (IntList() << 0 << 1 << 2); |
|
764 |
} |
|
765 |
||
766 |
void tst_QListWidget::selectedItems() |
|
767 |
{ |
|
768 |
QFETCH(int, itemCount); |
|
769 |
QFETCH(IntList, hiddenRows); |
|
770 |
QFETCH(IntList, selectedRows); |
|
771 |
QFETCH(IntList, expectedRows); |
|
772 |
||
773 |
QVERIFY(testWidget->count() == 0); |
|
774 |
||
775 |
//insert items |
|
776 |
for (int i=0; i<itemCount; ++i) |
|
777 |
new QListWidgetItem(QString("Item%1").arg(i), testWidget); |
|
778 |
||
779 |
//test the selection |
|
780 |
testWidget->setSelectionMode(QListWidget::SingleSelection); |
|
781 |
for (int i=0; i<itemCount; ++i) { |
|
782 |
QListWidgetItem *item = testWidget->item(i); |
|
783 |
testWidget->setItemSelected(item, true); |
|
784 |
QVERIFY(item->isSelected()); |
|
785 |
QCOMPARE(testWidget->selectedItems().count(), 1); |
|
786 |
} |
|
787 |
//let's clear the selection |
|
788 |
testWidget->clearSelection(); |
|
789 |
//... and set the selection mode to allow more than 1 item to be selected |
|
790 |
testWidget->setSelectionMode(QAbstractItemView::MultiSelection); |
|
791 |
||
792 |
//verify items are inserted |
|
793 |
QCOMPARE(testWidget->count(), itemCount); |
|
794 |
// hide items |
|
795 |
foreach (int row, hiddenRows) |
|
796 |
testWidget->setItemHidden(testWidget->item(row), true); |
|
797 |
// select items |
|
798 |
foreach (int row, selectedRows) |
|
799 |
testWidget->setItemSelected(testWidget->item(row), true); |
|
800 |
||
801 |
// check that the correct number of items and the expected items are there |
|
802 |
QList<QListWidgetItem *> selectedItems = testWidget->selectedItems(); |
|
803 |
QCOMPARE(selectedItems.count(), expectedRows.count()); |
|
804 |
foreach (int row, expectedRows) |
|
805 |
QVERIFY(selectedItems.contains(testWidget->item(row))); |
|
806 |
||
807 |
//check that isSelected agrees with selectedItems |
|
808 |
for (int i=0; i<itemCount; ++i) { |
|
809 |
QListWidgetItem *item = testWidget->item(i); |
|
810 |
if (testWidget->isItemSelected(item)) |
|
811 |
QVERIFY(selectedItems.contains(item)); |
|
812 |
} |
|
813 |
} |
|
814 |
||
815 |
void tst_QListWidget::removeItems_data() |
|
816 |
{ |
|
817 |
QTest::addColumn<int>("rowCount"); |
|
818 |
QTest::addColumn<int>("removeRows"); |
|
819 |
QTest::addColumn<int>("row"); |
|
820 |
QTest::addColumn<int>("expectedRowCount"); |
|
821 |
||
822 |
QTest::newRow("Empty") << 0 << 1 << 0 << 0; |
|
823 |
QTest::newRow("1:1") << 1 << 1 << 0 << 0; |
|
824 |
QTest::newRow("3:1") << 3 << 1 << 0 << 2; |
|
825 |
QTest::newRow("3:2") << 3 << 2 << 0 << 1; |
|
826 |
QTest::newRow("100:10") << 100 << 10 << 0 << 90; |
|
827 |
} |
|
828 |
||
829 |
void tst_QListWidget::removeItems() |
|
830 |
{ |
|
831 |
QFETCH(int, rowCount); |
|
832 |
QFETCH(int, removeRows); |
|
833 |
QFETCH(int, row); |
|
834 |
QFETCH(int, expectedRowCount); |
|
835 |
||
836 |
//insert items |
|
837 |
for (int r = 0; r < rowCount; ++r) |
|
838 |
new QListWidgetItem(QString::number(r), testWidget); |
|
839 |
||
840 |
// remove and compare the results |
|
841 |
for (int r = 0; r < removeRows; ++r) |
|
842 |
delete testWidget->item(row); |
|
843 |
QCOMPARE(testWidget->count(), expectedRowCount); |
|
844 |
||
845 |
// check if the correct items were removed |
|
846 |
for (int r = 0; r < expectedRowCount; ++r) |
|
847 |
if (r < row) |
|
848 |
QCOMPARE(testWidget->item(r)->text(), QString::number(r)); |
|
849 |
else |
|
850 |
QCOMPARE(testWidget->item(r)->text(), QString::number(r + removeRows)); |
|
851 |
||
852 |
||
853 |
} |
|
854 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
855 |
void tst_QListWidget::moveItemsPriv_data() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
856 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
857 |
QTest::addColumn<int>("rowCount"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
858 |
QTest::addColumn<int>("srcRow"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
859 |
QTest::addColumn<int>("dstRow"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
860 |
QTest::addColumn<bool>("shouldHaveSignaled"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
861 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
862 |
QTest::newRow("Empty") << 0 << 0 << 0 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
863 |
QTest::newRow("Overflow src") << 5 << 5 << 2 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
864 |
QTest::newRow("Underflow src") << 5 << -1 << 2 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
865 |
QTest::newRow("Overflow dst") << 5 << 2 << 6 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
866 |
QTest::newRow("Underflow dst") << 5 << 2 << -1 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
867 |
QTest::newRow("Same place") << 5 << 2 << 2 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
868 |
QTest::newRow("Up") << 5 << 4 << 2 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
869 |
QTest::newRow("Down") << 5 << 2 << 4 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
870 |
QTest::newRow("QTBUG-6532 assert") << 5 << 0 << 1 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
871 |
QTest::newRow("QTBUG-6565 to the end") << 5 << 3 << 5 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
872 |
QTest::newRow("Same place 2") << 2 << 0 << 1 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
873 |
QTest::newRow("swap") << 2 << 0 << 2 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
874 |
QTest::newRow("swap2") << 4 << 1 << 3 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
875 |
QTest::newRow("swap3") << 4 << 3 << 2 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
876 |
QTest::newRow("swap4") << 2 << 1 << 0 << true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
877 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
878 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
879 |
void tst_QListWidget::moveItemsPriv() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
880 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
881 |
QFETCH(int, rowCount); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
882 |
QFETCH(int, srcRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
883 |
QFETCH(int, dstRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
884 |
QFETCH(bool, shouldHaveSignaled); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
885 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
886 |
for (int r = 0; r < rowCount; ++r) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
887 |
new QListWidgetItem(QString::number(r), testWidget); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
888 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
889 |
QListModel *model = qobject_cast<QListModel *>(testWidget->model()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
890 |
QVERIFY(model); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
891 |
QSignalSpy beginMoveSpy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
892 |
QSignalSpy movedSpy(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
893 |
model->move(srcRow, dstRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
894 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
895 |
if (shouldHaveSignaled) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
896 |
if (srcRow < dstRow) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
897 |
QCOMPARE(testWidget->item(dstRow - 1)->text(), QString::number(srcRow)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
898 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
899 |
QCOMPARE(testWidget->item(dstRow)->text(), QString::number(srcRow)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
900 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
901 |
QCOMPARE(beginMoveSpy.count(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
902 |
const QList<QVariant> &beginMoveArgs = beginMoveSpy.takeFirst(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
903 |
QCOMPARE(beginMoveArgs.at(1).toInt(), srcRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
904 |
QCOMPARE(beginMoveArgs.at(2).toInt(), srcRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
905 |
QCOMPARE(beginMoveArgs.at(4).toInt(), dstRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
906 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
907 |
QCOMPARE(movedSpy.count(), 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
908 |
const QList<QVariant> &movedArgs = movedSpy.takeFirst(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
909 |
QCOMPARE(movedArgs.at(1).toInt(), srcRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
910 |
QCOMPARE(movedArgs.at(2).toInt(), srcRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
911 |
QCOMPARE(movedArgs.at(4).toInt(), dstRow); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
912 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
913 |
QCOMPARE(beginMoveSpy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
914 |
QCOMPARE(movedSpy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
915 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
916 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
917 |
|
0 | 918 |
void tst_QListWidget::itemStreaming_data() |
919 |
{ |
|
920 |
QTest::addColumn<QString>("text"); |
|
921 |
QTest::addColumn<QString>("toolTip"); |
|
922 |
||
923 |
QTest::newRow("Data") << "item text" << "tool tip text"; |
|
924 |
} |
|
925 |
||
926 |
void tst_QListWidget::itemStreaming() |
|
927 |
{ |
|
928 |
QFETCH(QString, text); |
|
929 |
QFETCH(QString, toolTip); |
|
930 |
||
931 |
QListWidgetItem item; |
|
932 |
QCOMPARE(item.text(), QString()); |
|
933 |
QCOMPARE(item.toolTip(), QString()); |
|
934 |
||
935 |
item.setText(text); |
|
936 |
item.setToolTip(toolTip); |
|
937 |
QCOMPARE(item.text(), text); |
|
938 |
QCOMPARE(item.toolTip(), toolTip); |
|
939 |
||
940 |
QByteArray buffer; |
|
941 |
QDataStream out(&buffer, QIODevice::WriteOnly); |
|
942 |
out << item; |
|
943 |
||
944 |
QListWidgetItem item2; |
|
945 |
QCOMPARE(item2.text(), QString()); |
|
946 |
QCOMPARE(item2.toolTip(), QString()); |
|
947 |
||
948 |
QVERIFY(!buffer.isEmpty()); |
|
949 |
||
950 |
QDataStream in(&buffer, QIODevice::ReadOnly); |
|
951 |
in >> item2; |
|
952 |
QCOMPARE(item2.text(), text); |
|
953 |
QCOMPARE(item2.toolTip(), toolTip); |
|
954 |
} |
|
955 |
||
956 |
void tst_QListWidget::sortItems_data() |
|
957 |
{ |
|
958 |
QTest::addColumn<int>("order"); |
|
959 |
QTest::addColumn<QVariantList>("initialList"); |
|
960 |
QTest::addColumn<QVariantList>("expectedList"); |
|
961 |
QTest::addColumn<IntList>("expectedRows"); |
|
962 |
||
963 |
QTest::newRow("ascending strings") |
|
964 |
<< static_cast<int>(Qt::AscendingOrder) |
|
965 |
<< (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) |
|
966 |
<< (QVariantList() << QString("a") << QString("b") << QString("c") << QString("d")) |
|
967 |
<< (IntList() << 2 << 3 << 0 << 1); |
|
968 |
||
969 |
QTest::newRow("descending strings") |
|
970 |
<< static_cast<int>(Qt::DescendingOrder) |
|
971 |
<< (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) |
|
972 |
<< (QVariantList() << QString("d") << QString("c") << QString("b") << QString("a")) |
|
973 |
<< (IntList() << 1 << 0 << 3 << 2); |
|
974 |
||
975 |
QTest::newRow("ascending numbers") |
|
976 |
<< static_cast<int>(Qt::AscendingOrder) |
|
977 |
<< (QVariantList() << 1 << 11 << 2 << 22) |
|
978 |
<< (QVariantList() << 1 << 2 << 11 << 22) |
|
979 |
<< (IntList() << 0 << 2 << 1 << 3); |
|
980 |
||
981 |
QTest::newRow("descending numbers") |
|
982 |
<< static_cast<int>(Qt::DescendingOrder) |
|
983 |
<< (QVariantList() << 1 << 11 << 2 << 22) |
|
984 |
<< (QVariantList() << 22 << 11 << 2 << 1) |
|
985 |
<< (IntList() << 3 << 1 << 2 << 0); |
|
986 |
} |
|
987 |
||
988 |
void tst_QListWidget::sortItems() |
|
989 |
{ |
|
990 |
QFETCH(int, order); |
|
991 |
QFETCH(QVariantList, initialList); |
|
992 |
QFETCH(QVariantList, expectedList); |
|
993 |
QFETCH(IntList, expectedRows); |
|
994 |
||
995 |
foreach (const QVariant &data, initialList) { |
|
996 |
QListWidgetItem *item = new QListWidgetItem(testWidget); |
|
997 |
item->setData(Qt::DisplayRole, data); |
|
998 |
} |
|
999 |
||
1000 |
QAbstractItemModel *model = testWidget->model(); |
|
1001 |
QList<QPersistentModelIndex> persistent; |
|
1002 |
for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
1003 |
persistent << model->index(j, 0, QModelIndex()); |
|
1004 |
||
1005 |
testWidget->sortItems(static_cast<Qt::SortOrder>(order)); |
|
1006 |
||
1007 |
QCOMPARE(testWidget->count(), expectedList.count()); |
|
1008 |
for (int i = 0; i < testWidget->count(); ++i) |
|
1009 |
QCOMPARE(testWidget->item(i)->text(), expectedList.at(i).toString()); |
|
1010 |
||
1011 |
for (int k = 0; k < testWidget->count(); ++k) |
|
1012 |
QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
1013 |
} |
|
1014 |
||
1015 |
void tst_QListWidget::sortHiddenItems_data() |
|
1016 |
{ |
|
1017 |
QTest::addColumn<int>("order"); |
|
1018 |
QTest::addColumn<QStringList>("initialList"); |
|
1019 |
QTest::addColumn<QStringList>("expectedList"); |
|
1020 |
QTest::addColumn<IntList>("expectedRows"); |
|
1021 |
QTest::addColumn<IntList>("expectedVisibility"); |
|
1022 |
||
1023 |
QStringList initial, expected; |
|
1024 |
IntList rowOrder; |
|
1025 |
IntList visible; |
|
1026 |
for (int i = 0; i < 20; ++i) { |
|
1027 |
initial << QString(QChar(0x41 + i)); |
|
1028 |
expected << QString(QChar(0x54 - i)); |
|
1029 |
rowOrder << 19 - i; |
|
1030 |
visible << (i % 2); |
|
1031 |
||
1032 |
} |
|
1033 |
QTest::newRow("descending order, 20 items") |
|
1034 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1035 |
<< initial |
|
1036 |
<< expected |
|
1037 |
<< rowOrder |
|
1038 |
<< visible; |
|
1039 |
||
1040 |
QTest::newRow("ascending order") |
|
1041 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1042 |
<< (QStringList() << "c" << "d" << "a" << "b") |
|
1043 |
<< (QStringList() << "a" << "b" << "c" << "d") |
|
1044 |
<< (IntList() << 2 << 3 << 0 << 1) |
|
1045 |
<< (IntList() << 1 << 0 << 1 << 0); |
|
1046 |
||
1047 |
QTest::newRow("descending order") |
|
1048 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1049 |
<< (QStringList() << "c" << "d" << "a" << "b") |
|
1050 |
<< (QStringList() << "d" << "c" << "b" << "a") |
|
1051 |
<< (IntList() << 1 << 0 << 3 << 2) |
|
1052 |
<< (IntList() << 0 << 1 << 0 << 1); |
|
1053 |
} |
|
1054 |
||
1055 |
void tst_QListWidget::sortHiddenItems() |
|
1056 |
{ |
|
1057 |
QFETCH(int, order); |
|
1058 |
QFETCH(QStringList, initialList); |
|
1059 |
QFETCH(QStringList, expectedList); |
|
1060 |
QFETCH(IntList, expectedRows); |
|
1061 |
QFETCH(IntList, expectedVisibility); |
|
1062 |
||
1063 |
// init() won't clear hidden items... |
|
1064 |
QListWidget *tw = new QListWidget(); |
|
1065 |
tw->addItems(initialList); |
|
1066 |
||
1067 |
QAbstractItemModel *model = tw->model(); |
|
1068 |
QList<QPersistentModelIndex> persistent; |
|
1069 |
for (int j = 0; j < model->rowCount(QModelIndex()); ++j) { |
|
1070 |
persistent << model->index(j, 0, QModelIndex()); |
|
1071 |
tw->setRowHidden(j, j & 1); // every odd is hidden |
|
1072 |
} |
|
1073 |
||
1074 |
tw->setSortingEnabled(true); |
|
1075 |
tw->sortItems(static_cast<Qt::SortOrder>(order)); |
|
1076 |
||
1077 |
QCOMPARE(tw->count(), expectedList.count()); |
|
1078 |
for (int i = 0; i < tw->count(); ++i) { |
|
1079 |
QCOMPARE(tw->item(i)->text(), expectedList.at(i)); |
|
1080 |
QCOMPARE(tw->item(i)->isHidden(), !expectedVisibility.at(i)); |
|
1081 |
} |
|
1082 |
||
1083 |
for (int k = 0; k < tw->count(); ++k) |
|
1084 |
QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
1085 |
||
1086 |
delete tw; |
|
1087 |
} |
|
1088 |
||
1089 |
void tst_QListWidget::modelChanged(ModelChanged change, const QModelIndex &parent, |
|
1090 |
int first, int last) |
|
1091 |
{ |
|
1092 |
rcParent[change] = parent; |
|
1093 |
rcFirst[change] = first; |
|
1094 |
rcLast[change] = last; |
|
1095 |
} |
|
1096 |
||
1097 |
class TestListWidget : public QListWidget { |
|
1098 |
public: |
|
1099 |
TestListWidget() : QListWidget() |
|
1100 |
{ |
|
1101 |
||
1102 |
} |
|
1103 |
State getState() {return QListWidget::state();} |
|
1104 |
||
1105 |
void closeEditor(QWidget *w, QAbstractItemDelegate::EndEditHint hint) { |
|
1106 |
QListWidget::closeEditor(w, hint); |
|
1107 |
} |
|
1108 |
||
1109 |
bool isEditingState(QListWidgetItem *item) { |
|
1110 |
Q_UNUSED(item); |
|
1111 |
return (QListWidget::state() == QListWidget::EditingState ? true : false); |
|
1112 |
} |
|
1113 |
}; |
|
1114 |
||
1115 |
void tst_QListWidget::closeEditor() |
|
1116 |
{ |
|
1117 |
TestListWidget w; |
|
1118 |
QStringList labels = (QStringList() << "a" << "b" << "c" << "d"); |
|
1119 |
w.addItems(labels); |
|
1120 |
QListWidgetItem *item = w.item(0); |
|
1121 |
item->setFlags(item->flags() | Qt::ItemIsEditable); |
|
1122 |
QVERIFY(item); |
|
1123 |
w.editItem(item); |
|
1124 |
||
1125 |
QVERIFY(w.isEditingState(item)); |
|
1126 |
||
1127 |
w.reset(); |
|
1128 |
||
1129 |
QVERIFY(!w.isEditingState(item)); |
|
1130 |
} |
|
1131 |
||
1132 |
void tst_QListWidget::setData_data() |
|
1133 |
{ |
|
1134 |
QTest::addColumn<QStringList>("initialItems"); |
|
1135 |
QTest::addColumn<int>("itemIndex"); |
|
1136 |
QTest::addColumn<IntList>("roles"); |
|
1137 |
QTest::addColumn<QVariantList>("values"); |
|
1138 |
QTest::addColumn<int>("expectedSignalCount"); |
|
1139 |
||
1140 |
QStringList initialItems; |
|
1141 |
IntList roles; |
|
1142 |
QVariantList values; |
|
1143 |
||
1144 |
{ |
|
1145 |
initialItems.clear(); roles.clear(); values.clear(); |
|
1146 |
initialItems << "foo"; |
|
1147 |
roles << Qt::DisplayRole; |
|
1148 |
values << "xxx"; |
|
1149 |
QTest::newRow("changing a role should emit") |
|
1150 |
<< initialItems << 0 << roles << values << 1; |
|
1151 |
} |
|
1152 |
{ |
|
1153 |
initialItems.clear(); roles.clear(); values.clear(); |
|
1154 |
initialItems << "foo"; |
|
1155 |
roles << Qt::DisplayRole; |
|
1156 |
values << "foo"; |
|
1157 |
QTest::newRow("setting the same value should not emit") |
|
1158 |
<< initialItems << 0 << roles << values << 0; |
|
1159 |
} |
|
1160 |
{ |
|
1161 |
initialItems.clear(); roles.clear(); values.clear(); |
|
1162 |
initialItems << "foo"; |
|
1163 |
roles << Qt::DisplayRole << Qt::DisplayRole; |
|
1164 |
values << "bar" << "bar"; |
|
1165 |
QTest::newRow("setting the same value twice should only emit once") |
|
1166 |
<< initialItems << 0 << roles << values << 1; |
|
1167 |
} |
|
1168 |
{ |
|
1169 |
initialItems.clear(); roles.clear(); values.clear(); |
|
1170 |
initialItems << "foo"; |
|
1171 |
roles << Qt::DisplayRole << Qt::ToolTipRole << Qt::WhatsThisRole; |
|
1172 |
values << "bar" << "bartooltip" << "barwhatsthis"; |
|
1173 |
QTest::newRow("changing three roles should emit three times") |
|
1174 |
<< initialItems << 0 << roles << values << 3; |
|
1175 |
} |
|
1176 |
} |
|
1177 |
||
1178 |
void tst_QListWidget::setData() |
|
1179 |
{ |
|
1180 |
QFETCH(QStringList, initialItems); |
|
1181 |
QFETCH(int, itemIndex); |
|
1182 |
QFETCH(IntList, roles); |
|
1183 |
QFETCH(QVariantList, values); |
|
1184 |
QFETCH(int, expectedSignalCount); |
|
1185 |
qRegisterMetaType<QListWidgetItem *>("QListWidgetItem*"); |
|
1186 |
qRegisterMetaType<QModelIndex>("QModelIndex"); |
|
1187 |
||
1188 |
QVERIFY(roles.count() == values.count()); |
|
1189 |
||
1190 |
for (int manipulateModel=0; manipulateModel<2; ++manipulateModel) { |
|
1191 |
testWidget->clear(); |
|
1192 |
testWidget->insertItems(0, initialItems); |
|
1193 |
QCOMPARE(testWidget->count(), initialItems.count()); |
|
1194 |
||
1195 |
QSignalSpy itemChanged(testWidget, SIGNAL(itemChanged(QListWidgetItem *))); |
|
1196 |
QSignalSpy dataChanged(testWidget->model(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &))); |
|
1197 |
||
1198 |
for (int i=0; i < roles.count(); ++i) { |
|
1199 |
if (manipulateModel) |
|
1200 |
testWidget->model()->setData( |
|
1201 |
testWidget->model()->index(itemIndex, 0, testWidget->rootIndex()), |
|
1202 |
values.at(i), |
|
1203 |
roles.at(i)); |
|
1204 |
else |
|
1205 |
testWidget->item(itemIndex)->setData(roles.at(i), values.at(i)); |
|
1206 |
} |
|
1207 |
||
1208 |
// make sure the data is actually set |
|
1209 |
for (int i=0; i < roles.count(); ++i) |
|
1210 |
QCOMPARE(testWidget->item(itemIndex)->data(roles.at(i)), values.at(i)); |
|
1211 |
||
1212 |
// make sure we get the right number of emits |
|
1213 |
QCOMPARE(itemChanged.count(), expectedSignalCount); |
|
1214 |
QCOMPARE(dataChanged.count(), expectedSignalCount); |
|
1215 |
} |
|
1216 |
} |
|
1217 |
||
1218 |
void tst_QListWidget::insertItemsWithSorting_data() |
|
1219 |
{ |
|
1220 |
QTest::addColumn<int>("sortOrder"); |
|
1221 |
QTest::addColumn<QStringList>("initialItems"); |
|
1222 |
QTest::addColumn<QStringList>("insertItems"); |
|
1223 |
QTest::addColumn<QStringList>("expectedItems"); |
|
1224 |
QTest::addColumn<IntList>("expectedRows"); |
|
1225 |
||
1226 |
QTest::newRow("() + (a) = (a)") |
|
1227 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1228 |
<< QStringList() |
|
1229 |
<< (QStringList() << "a") |
|
1230 |
<< (QStringList() << "a") |
|
1231 |
<< IntList(); |
|
1232 |
QTest::newRow("() + (c, b, a) = (a, b, c)") |
|
1233 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1234 |
<< QStringList() |
|
1235 |
<< (QStringList() << "c" << "b" << "a") |
|
1236 |
<< (QStringList() << "a" << "b" << "c") |
|
1237 |
<< IntList(); |
|
1238 |
QTest::newRow("() + (a, b, c) = (c, b, a)") |
|
1239 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1240 |
<< QStringList() |
|
1241 |
<< (QStringList() << "a" << "b" << "c") |
|
1242 |
<< (QStringList() << "c" << "b" << "a") |
|
1243 |
<< IntList(); |
|
1244 |
QTest::newRow("(a) + (b) = (a, b)") |
|
1245 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1246 |
<< QStringList("a") |
|
1247 |
<< (QStringList() << "b") |
|
1248 |
<< (QStringList() << "a" << "b") |
|
1249 |
<< (IntList() << 0); |
|
1250 |
QTest::newRow("(a) + (b) = (b, a)") |
|
1251 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1252 |
<< QStringList("a") |
|
1253 |
<< (QStringList() << "b") |
|
1254 |
<< (QStringList() << "b" << "a") |
|
1255 |
<< (IntList() << 1); |
|
1256 |
QTest::newRow("(a, c, b) + (d) = (a, b, c, d)") |
|
1257 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1258 |
<< (QStringList() << "a" << "c" << "b") |
|
1259 |
<< (QStringList() << "d") |
|
1260 |
<< (QStringList() << "a" << "b" << "c" << "d") |
|
1261 |
<< (IntList() << 0 << 1 << 2); |
|
1262 |
QTest::newRow("(b, c, a) + (d) = (d, c, b, a)") |
|
1263 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1264 |
<< (QStringList() << "b" << "c" << "a") |
|
1265 |
<< (QStringList() << "d") |
|
1266 |
<< (QStringList() << "d" << "c" << "b" << "a") |
|
1267 |
<< (IntList() << 1 << 2 << 3); |
|
1268 |
{ |
|
1269 |
IntList ascendingRows; |
|
1270 |
IntList reverseRows; |
|
1271 |
QStringList ascendingItems; |
|
1272 |
QStringList reverseItems; |
|
1273 |
for (int i = 'a'; i <= 'z'; ++i) { |
|
1274 |
ascendingItems << QString("%0").arg(QLatin1Char(i)); |
|
1275 |
reverseItems << QString("%0").arg(QLatin1Char('z' - i + 'a')); |
|
1276 |
ascendingRows << i - 'a'; |
|
1277 |
reverseRows << 'z' - i + 'a'; |
|
1278 |
} |
|
1279 |
QTest::newRow("() + (sorted items) = (sorted items)") |
|
1280 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1281 |
<< QStringList() |
|
1282 |
<< ascendingItems |
|
1283 |
<< ascendingItems |
|
1284 |
<< IntList(); |
|
1285 |
QTest::newRow("(sorted items) + () = (sorted items)") |
|
1286 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1287 |
<< ascendingItems |
|
1288 |
<< QStringList() |
|
1289 |
<< ascendingItems |
|
1290 |
<< ascendingRows; |
|
1291 |
QTest::newRow("() + (ascending items) = (reverse items)") |
|
1292 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1293 |
<< QStringList() |
|
1294 |
<< ascendingItems |
|
1295 |
<< reverseItems |
|
1296 |
<< IntList(); |
|
1297 |
QTest::newRow("(reverse items) + () = (ascending items)") |
|
1298 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1299 |
<< reverseItems |
|
1300 |
<< QStringList() |
|
1301 |
<< ascendingItems |
|
1302 |
<< ascendingRows; |
|
1303 |
QTest::newRow("(reverse items) + () = (reverse items)") |
|
1304 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1305 |
<< reverseItems |
|
1306 |
<< QStringList() |
|
1307 |
<< reverseItems |
|
1308 |
<< ascendingRows; |
|
1309 |
} |
|
1310 |
} |
|
1311 |
||
1312 |
void tst_QListWidget::insertItemsWithSorting() |
|
1313 |
{ |
|
1314 |
QFETCH(int, sortOrder); |
|
1315 |
QFETCH(QStringList, initialItems); |
|
1316 |
QFETCH(QStringList, insertItems); |
|
1317 |
QFETCH(QStringList, expectedItems); |
|
1318 |
QFETCH(IntList, expectedRows); |
|
1319 |
||
1320 |
for (int method = 0; method < 5; ++method) { |
|
1321 |
QListWidget w; |
|
1322 |
w.setSortingEnabled(true); |
|
1323 |
w.sortItems(static_cast<Qt::SortOrder>(sortOrder)); |
|
1324 |
w.addItems(initialItems); |
|
1325 |
||
1326 |
QAbstractItemModel *model = w.model(); |
|
1327 |
QList<QPersistentModelIndex> persistent; |
|
1328 |
for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
1329 |
persistent << model->index(j, 0, QModelIndex()); |
|
1330 |
||
1331 |
switch (method) { |
|
1332 |
case 0: |
|
1333 |
// insert using item constructor |
|
1334 |
for (int i = 0; i < insertItems.size(); ++i) |
|
1335 |
new QListWidgetItem(insertItems.at(i), &w); |
|
1336 |
break; |
|
1337 |
case 1: |
|
1338 |
// insert using insertItems() |
|
1339 |
w.insertItems(0, insertItems); |
|
1340 |
break; |
|
1341 |
case 2: |
|
1342 |
// insert using insertItem() |
|
1343 |
for (int i = 0; i < insertItems.size(); ++i) |
|
1344 |
w.insertItem(0, insertItems.at(i)); |
|
1345 |
break; |
|
1346 |
case 3: |
|
1347 |
// insert using addItems() |
|
1348 |
w.addItems(insertItems); |
|
1349 |
break; |
|
1350 |
case 4: |
|
1351 |
// insert using addItem() |
|
1352 |
for (int i = 0; i < insertItems.size(); ++i) |
|
1353 |
w.addItem(insertItems.at(i)); |
|
1354 |
break; |
|
1355 |
} |
|
1356 |
QCOMPARE(w.count(), expectedItems.count()); |
|
1357 |
for (int i = 0; i < w.count(); ++i) |
|
1358 |
QCOMPARE(w.item(i)->text(), expectedItems.at(i)); |
|
1359 |
||
1360 |
for (int k = 0; k < persistent.count(); ++k) |
|
1361 |
QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
1362 |
} |
|
1363 |
} |
|
1364 |
||
1365 |
void tst_QListWidget::changeDataWithSorting_data() |
|
1366 |
{ |
|
1367 |
QTest::addColumn<int>("sortOrder"); |
|
1368 |
QTest::addColumn<QStringList>("initialItems"); |
|
1369 |
QTest::addColumn<int>("itemIndex"); |
|
1370 |
QTest::addColumn<QString>("newValue"); |
|
1371 |
QTest::addColumn<QStringList>("expectedItems"); |
|
1372 |
QTest::addColumn<IntList>("expectedRows"); |
|
1373 |
QTest::addColumn<bool>("reorderingExpected"); |
|
1374 |
||
1375 |
QTest::newRow("change a to b in (a)") |
|
1376 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1377 |
<< (QStringList() << "a") |
|
1378 |
<< 0 << "b" |
|
1379 |
<< (QStringList() << "b") |
|
1380 |
<< (IntList() << 0) |
|
1381 |
<< false; |
|
1382 |
QTest::newRow("change a to b in (a, c)") |
|
1383 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1384 |
<< (QStringList() << "a" << "c") |
|
1385 |
<< 0 << "b" |
|
1386 |
<< (QStringList() << "b" << "c") |
|
1387 |
<< (IntList() << 0 << 1) |
|
1388 |
<< false; |
|
1389 |
QTest::newRow("change a to c in (a, b)") |
|
1390 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1391 |
<< (QStringList() << "a" << "b") |
|
1392 |
<< 0 << "c" |
|
1393 |
<< (QStringList() << "b" << "c") |
|
1394 |
<< (IntList() << 1 << 0) |
|
1395 |
<< true; |
|
1396 |
QTest::newRow("change c to a in (c, b)") |
|
1397 |
<< static_cast<int>(Qt::DescendingOrder) |
|
1398 |
<< (QStringList() << "c" << "b") |
|
1399 |
<< 0 << "a" |
|
1400 |
<< (QStringList() << "b" << "a") |
|
1401 |
<< (IntList() << 1 << 0) |
|
1402 |
<< true; |
|
1403 |
QTest::newRow("change e to i in (a, c, e, g)") |
|
1404 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1405 |
<< (QStringList() << "a" << "c" << "e" << "g") |
|
1406 |
<< 2 << "i" |
|
1407 |
<< (QStringList() << "a" << "c" << "g" << "i") |
|
1408 |
<< (IntList() << 0 << 1 << 3 << 2) |
|
1409 |
<< true; |
|
1410 |
QTest::newRow("change e to a in (c, e, g, i)") |
|
1411 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1412 |
<< (QStringList() << "c" << "e" << "g" << "i") |
|
1413 |
<< 1 << "a" |
|
1414 |
<< (QStringList() << "a" << "c" << "g" << "i") |
|
1415 |
<< (IntList() << 1 << 0 << 2 << 3) |
|
1416 |
<< true; |
|
1417 |
QTest::newRow("change e to f in (c, e, g, i)") |
|
1418 |
<< static_cast<int>(Qt::AscendingOrder) |
|
1419 |
<< (QStringList() << "c" << "e" << "g" << "i") |
|
1420 |
<< 1 << "f" |
|
1421 |
<< (QStringList() << "c" << "f" << "g" << "i") |
|
1422 |
<< (IntList() << 0 << 1 << 2 << 3) |
|
1423 |
<< false; |
|
1424 |
} |
|
1425 |
||
1426 |
void tst_QListWidget::itemData() |
|
1427 |
{ |
|
1428 |
QListWidget widget; |
|
1429 |
QListWidgetItem item(&widget); |
|
1430 |
item.setFlags(item.flags() | Qt::ItemIsEditable); |
|
1431 |
item.setData(Qt::DisplayRole, QString("0")); |
|
1432 |
item.setData(Qt::CheckStateRole, Qt::PartiallyChecked); |
|
1433 |
item.setData(Qt::UserRole + 0, QString("1")); |
|
1434 |
item.setData(Qt::UserRole + 1, QString("2")); |
|
1435 |
item.setData(Qt::UserRole + 2, QString("3")); |
|
1436 |
item.setData(Qt::UserRole + 3, QString("4")); |
|
1437 |
QMap<int, QVariant> flags = widget.model()->itemData(widget.model()->index(0, 0)); |
|
1438 |
QCOMPARE(flags.count(), 6); |
|
1439 |
QCOMPARE(flags[Qt::UserRole + 0].toString(), QString("1")); |
|
1440 |
} |
|
1441 |
||
1442 |
void tst_QListWidget::changeDataWithSorting() |
|
1443 |
{ |
|
1444 |
QFETCH(int, sortOrder); |
|
1445 |
QFETCH(QStringList, initialItems); |
|
1446 |
QFETCH(int, itemIndex); |
|
1447 |
QFETCH(QString, newValue); |
|
1448 |
QFETCH(QStringList, expectedItems); |
|
1449 |
QFETCH(IntList, expectedRows); |
|
1450 |
QFETCH(bool, reorderingExpected); |
|
1451 |
||
1452 |
QListWidget w; |
|
1453 |
w.setSortingEnabled(true); |
|
1454 |
w.sortItems(static_cast<Qt::SortOrder>(sortOrder)); |
|
1455 |
w.addItems(initialItems); |
|
1456 |
||
1457 |
QAbstractItemModel *model = w.model(); |
|
1458 |
QList<QPersistentModelIndex> persistent; |
|
1459 |
for (int j = 0; j < model->rowCount(QModelIndex()); ++j) |
|
1460 |
persistent << model->index(j, 0, QModelIndex()); |
|
1461 |
||
1462 |
QSignalSpy dataChangedSpy(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &))); |
|
1463 |
QSignalSpy layoutChangedSpy(model, SIGNAL(layoutChanged())); |
|
1464 |
||
1465 |
QListWidgetItem *item = w.item(itemIndex); |
|
1466 |
item->setText(newValue); |
|
1467 |
for (int i = 0; i < expectedItems.count(); ++i) { |
|
1468 |
QCOMPARE(w.item(i)->text(), expectedItems.at(i)); |
|
1469 |
for (int j = 0; j < persistent.count(); ++j) { |
|
1470 |
if (persistent.at(j).row() == i) // the same toplevel row |
|
1471 |
QCOMPARE(persistent.at(j).internalPointer(), (void *)w.item(i)); |
|
1472 |
} |
|
1473 |
} |
|
1474 |
||
1475 |
for (int k = 0; k < persistent.count(); ++k) |
|
1476 |
QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); |
|
1477 |
||
1478 |
QCOMPARE(dataChangedSpy.count(), 1); |
|
1479 |
QCOMPARE(layoutChangedSpy.count(), reorderingExpected ? 1 : 0); |
|
1480 |
} |
|
1481 |
||
1482 |
void tst_QListWidget::itemWidget() |
|
1483 |
{ |
|
1484 |
QListWidget list; |
|
1485 |
QWidget widget; |
|
1486 |
||
1487 |
QListWidgetItem *item = new QListWidgetItem(&list); |
|
1488 |
||
1489 |
||
1490 |
QCOMPARE(list.itemWidget(item), static_cast<QWidget*>(0)); |
|
1491 |
list.setItemWidget(item, &widget); |
|
1492 |
QCOMPARE(list.itemWidget(item), &widget); |
|
1493 |
list.removeItemWidget(item); |
|
1494 |
QCOMPARE(list.itemWidget(item), static_cast<QWidget*>(0)); |
|
1495 |
} |
|
1496 |
||
1497 |
#ifndef Q_WS_MAC |
|
1498 |
class MyListWidget : public QListWidget |
|
1499 |
{ |
|
1500 |
public: |
|
1501 |
void paintEvent(QPaintEvent *e) { |
|
1502 |
painted += e->region(); |
|
1503 |
QListWidget::paintEvent(e); |
|
1504 |
} |
|
1505 |
||
1506 |
QRegion painted; |
|
1507 |
}; |
|
1508 |
||
1509 |
void tst_QListWidget::fastScroll() |
|
1510 |
{ |
|
1511 |
if (qstrcmp(QApplication::style()->metaObject()->className(), "QS60Style") == 0) { |
|
1512 |
QSKIP("S60 style doesn't support fast scrolling", SkipAll); |
|
1513 |
} |
|
1514 |
||
1515 |
MyListWidget widget; |
|
1516 |
for (int i = 0; i < 50; ++i) |
|
1517 |
widget.addItem(QString("Item %1").arg(i)); |
|
1518 |
||
1519 |
widget.show(); |
|
1520 |
// Make sure the widget gets the first full repaint. On |
|
1521 |
// some WMs, we'll get two (first inactive exposure, then |
|
1522 |
// active exposure. |
|
1523 |
#ifdef Q_WS_X11 |
|
1524 |
qt_x11_wait_for_window_manager(&widget); |
|
1525 |
#endif |
|
1526 |
QApplication::processEvents(); |
|
1527 |
QTest::qWait(500); |
|
1528 |
||
1529 |
QSize itemSize = widget.visualItemRect(widget.item(0)).size(); |
|
1530 |
QVERIFY(!itemSize.isEmpty()); |
|
1531 |
||
1532 |
QScrollBar *sbar = widget.verticalScrollBar(); |
|
1533 |
widget.painted = QRegion(); |
|
1534 |
sbar->setValue(sbar->value() + sbar->singleStep()); |
|
1535 |
QApplication::processEvents(); |
|
1536 |
||
1537 |
// only one item should be repainted, the rest should be scrolled in memory |
|
1538 |
QCOMPARE(widget.painted.boundingRect().size(), itemSize); |
|
1539 |
} |
|
1540 |
#endif // Q_WS_MAC |
|
1541 |
||
1542 |
void tst_QListWidget::insertUnchanged() |
|
1543 |
{ |
|
1544 |
QListWidget w; |
|
1545 |
QSignalSpy itemChangedSpy(&w, SIGNAL(itemChanged(QListWidgetItem*))); |
|
1546 |
QListWidgetItem item("foo", &w); |
|
1547 |
QCOMPARE(itemChangedSpy.count(), 0); |
|
1548 |
} |
|
1549 |
||
1550 |
void tst_QListWidget::setSortingEnabled() |
|
1551 |
{ |
|
1552 |
QListWidget w; |
|
1553 |
QListWidgetItem *item1 = new QListWidgetItem(&w); |
|
1554 |
QListWidgetItem *item2 = new QListWidgetItem(&w); |
|
1555 |
||
1556 |
w.setSortingEnabled(true); |
|
1557 |
QCOMPARE(w.isSortingEnabled(), true); |
|
1558 |
QCOMPARE(w.item(0), item1); |
|
1559 |
QCOMPARE(w.item(1), item2); |
|
1560 |
} |
|
1561 |
||
1562 |
void tst_QListWidget::task199503_crashWhenCleared() |
|
1563 |
{ |
|
1564 |
//we test here for a crash that would occur if you clear the items in the currentItemChanged signal |
|
1565 |
QListWidget w; |
|
1566 |
w.addItems( QStringList() << "item1" << "item2" << "item3"); |
|
1567 |
w.setCurrentRow(0); |
|
1568 |
w.connect(&w, SIGNAL(currentItemChanged(QListWidgetItem *,QListWidgetItem*)), SLOT(clear())); |
|
1569 |
w.setCurrentRow(1); |
|
1570 |
} |
|
1571 |
||
1572 |
void tst_QListWidget::task217070_scrollbarsAdjusted() |
|
1573 |
{ |
|
1574 |
//This task was mailing for style using SH_ScrollView_FrameOnlyAroundContents such as QMotifStyle |
|
1575 |
QListWidget v; |
|
1576 |
for (int i = 0; i<200;i++) |
|
1577 |
v.addItem(QString::number(i)); |
|
1578 |
v.show(); |
|
1579 |
v.setViewMode(QListView::IconMode); |
|
1580 |
v.setResizeMode(QListView::Adjust); |
|
1581 |
v.setUniformItemSizes(true); |
|
1582 |
v.resize(160,100); |
|
1583 |
QTest::qWait(50); |
|
1584 |
for(int f=150; f>90 ; f--) { |
|
1585 |
v.resize(f,100); |
|
1586 |
QTest::qWait(30); |
|
1587 |
QVERIFY(v.verticalScrollBar()->isVisible()); |
|
1588 |
//the vertical scrollbar must not be visible. |
|
1589 |
QVERIFY(!v.horizontalScrollBar()->isVisible()); |
|
1590 |
} |
|
1591 |
} |
|
1592 |
||
1593 |
void tst_QListWidget::task258949_keypressHangup() |
|
1594 |
{ |
|
1595 |
QListWidget lw; |
|
1596 |
for (int y = 0; y < 5; y++) { |
|
1597 |
QListWidgetItem *lwi = new QListWidgetItem(&lw); |
|
1598 |
lwi->setText(y ? "1" : "0"); |
|
1599 |
if (y) |
|
1600 |
lwi->setFlags(Qt::ItemIsSelectable); |
|
1601 |
} |
|
1602 |
||
1603 |
lw.show(); |
|
1604 |
lw.setCurrentIndex(lw.model()->index(0,0)); |
|
1605 |
QCOMPARE(lw.currentIndex(), lw.model()->index(0,0)); |
|
1606 |
QTest::qWait(30); |
|
1607 |
QTest::keyPress(&lw, '1'); //this used to freeze |
|
1608 |
QTest::qWait(30); |
|
1609 |
QCOMPARE(lw.currentIndex(), lw.model()->index(0,0)); |
|
1610 |
} |
|
1611 |
||
1612 |
QTEST_MAIN(tst_QListWidget) |
|
1613 |
#include "tst_qlistwidget.moc" |