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