tests/auto/qtableview/tst_qtableview.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 <QtGui/QtGui>
       
    44 #include <QtTest/QtTest>
       
    45 #include "../../shared/util.h"
       
    46 #include "private/qapplication_p.h"
       
    47 
       
    48 //TESTED_CLASS=
       
    49 //TESTED_FILES=
       
    50 
       
    51 // Will try to wait for the condition while allowing event processing
       
    52 // for a maximum of 2 seconds.
       
    53 #define WAIT_FOR_CONDITION(expr, expected) \
       
    54     do { \
       
    55         const int step = 100; \
       
    56         for (int i = 0; i < 2000 && expr != expected; i+=step) { \
       
    57             QTest::qWait(step); \
       
    58         } \
       
    59     } while(0)
       
    60 
       
    61 typedef QList<int> IntList;
       
    62 Q_DECLARE_METATYPE(IntList)
       
    63 
       
    64 typedef QList<bool> BoolList;
       
    65 Q_DECLARE_METATYPE(BoolList)
       
    66 
       
    67 class tst_QTableView : public QObject
       
    68 {
       
    69     Q_OBJECT
       
    70 
       
    71 public:
       
    72     tst_QTableView();
       
    73     virtual ~tst_QTableView();
       
    74 
       
    75 public slots:
       
    76     void initTestCase();
       
    77     void cleanupTestCase();
       
    78     void init();
       
    79     void cleanup();
       
    80 
       
    81 private slots:
       
    82     void getSetCheck();
       
    83 
       
    84     void noDelegate();
       
    85     void noModel();
       
    86     void emptyModel();
       
    87 
       
    88     void removeRows_data();
       
    89     void removeRows();
       
    90 
       
    91     void removeColumns_data();
       
    92     void removeColumns();
       
    93 
       
    94     void keyboardNavigation_data();
       
    95     void keyboardNavigation();
       
    96 
       
    97     void headerSections_data();
       
    98     void headerSections();
       
    99 
       
   100     void moveCursor_data();
       
   101     void moveCursor();
       
   102 
       
   103     void moveCursorStrikesBack_data();
       
   104     void moveCursorStrikesBack();
       
   105 
       
   106     void hideRows_data();
       
   107     void hideRows();
       
   108 
       
   109     void hideColumns_data();
       
   110     void hideColumns();
       
   111 
       
   112     void selection_data();
       
   113     void selection();
       
   114 
       
   115     void selectRow_data();
       
   116     void selectRow();
       
   117 
       
   118     void selectColumn_data();
       
   119     void selectColumn();
       
   120 
       
   121     void visualRect_data();
       
   122     void visualRect();
       
   123 
       
   124     void fetchMore();
       
   125     void setHeaders();
       
   126 
       
   127     void resizeRowsToContents_data();
       
   128     void resizeRowsToContents();
       
   129 
       
   130     void resizeColumnsToContents_data();
       
   131     void resizeColumnsToContents();
       
   132 
       
   133     void rowViewportPosition_data();
       
   134     void rowViewportPosition();
       
   135 
       
   136     void rowAt_data();
       
   137     void rowAt();
       
   138 
       
   139     void rowHeight_data();
       
   140     void rowHeight();
       
   141 
       
   142     void columnViewportPosition_data();
       
   143     void columnViewportPosition();
       
   144 
       
   145     void columnAt_data();
       
   146     void columnAt();
       
   147 
       
   148     void columnWidth_data();
       
   149     void columnWidth();
       
   150 
       
   151     void hiddenRow_data();
       
   152     void hiddenRow();
       
   153 
       
   154     void hiddenColumn_data();
       
   155     void hiddenColumn();
       
   156 
       
   157     void sortingEnabled_data();
       
   158     void sortingEnabled();
       
   159 
       
   160     void scrollTo_data();
       
   161     void scrollTo();
       
   162 
       
   163     void indexAt_data();
       
   164     void indexAt();
       
   165 
       
   166     void span_data();
       
   167     void span();
       
   168     void spans();
       
   169     void spans_data();
       
   170     void spansAfterRowInsertion();
       
   171     void spansAfterColumnInsertion();
       
   172     void spansAfterRowRemoval();
       
   173     void spansAfterColumnRemoval();
       
   174 
       
   175     void checkHeaderReset();
       
   176     void checkHeaderMinSize();
       
   177 
       
   178     void resizeToContents();
       
   179 
       
   180     void tabFocus();
       
   181     void bigModel();
       
   182     void selectionSignal();
       
   183 
       
   184     // task-specific tests:
       
   185     void task173773_updateVerticalHeader();
       
   186     void task227953_setRootIndex();
       
   187     void task240266_veryBigColumn();
       
   188     void task248688_autoScrollNavigation();
       
   189     void task259308_scrollVerticalHeaderSwappedSections();
       
   190     void task191545_dragSelectRows();
       
   191 
       
   192     void mouseWheel_data();
       
   193     void mouseWheel();
       
   194 
       
   195     void addColumnWhileEditing();
       
   196     void task234926_setHeaderSorting();
       
   197 };
       
   198 
       
   199 // Testing get/set functions
       
   200 void tst_QTableView::getSetCheck()
       
   201 {
       
   202     QTableView obj1;
       
   203 
       
   204     obj1.setSortingEnabled(false);
       
   205     QCOMPARE(false, obj1.isSortingEnabled());
       
   206     obj1.setSortingEnabled(true);
       
   207     QCOMPARE(true, obj1.isSortingEnabled());
       
   208 
       
   209     obj1.setShowGrid(false);
       
   210     QCOMPARE(false, obj1.showGrid());
       
   211     obj1.setShowGrid(true);
       
   212     QCOMPARE(true, obj1.showGrid());
       
   213 
       
   214     obj1.setGridStyle(Qt::NoPen);
       
   215     QCOMPARE(Qt::NoPen, obj1.gridStyle());
       
   216     obj1.setGridStyle(Qt::SolidLine);
       
   217     QCOMPARE(Qt::SolidLine, obj1.gridStyle());
       
   218 
       
   219     obj1.setRootIndex(QModelIndex());
       
   220     QCOMPARE(QModelIndex(), obj1.rootIndex());
       
   221     QStandardItemModel model(10, 10);
       
   222     obj1.setModel(&model);
       
   223     QModelIndex index = model.index(0, 0);
       
   224     obj1.setRootIndex(index);
       
   225     QCOMPARE(index, obj1.rootIndex());
       
   226 
       
   227     QHeaderView *var1 = new QHeaderView(Qt::Horizontal);
       
   228     obj1.setHorizontalHeader(var1);
       
   229     QCOMPARE(var1, obj1.horizontalHeader());
       
   230     obj1.setHorizontalHeader((QHeaderView *)0);
       
   231     QCOMPARE(var1, obj1.horizontalHeader());
       
   232     delete var1;
       
   233 
       
   234     QHeaderView *var2 = new QHeaderView(Qt::Vertical);
       
   235     obj1.setVerticalHeader(var2);
       
   236     QCOMPARE(var2, obj1.verticalHeader());
       
   237     obj1.setVerticalHeader((QHeaderView *)0);
       
   238     QCOMPARE(var2, obj1.verticalHeader());
       
   239     delete var2;
       
   240 
       
   241     QCOMPARE(obj1.isCornerButtonEnabled(), true);
       
   242     obj1.setCornerButtonEnabled(false);
       
   243     QCOMPARE(obj1.isCornerButtonEnabled(), false);
       
   244 }
       
   245 
       
   246 class QtTestTableModel: public QAbstractTableModel
       
   247 {
       
   248     Q_OBJECT
       
   249 
       
   250 signals:
       
   251     void invalidIndexEncountered() const;
       
   252 
       
   253 public:
       
   254     QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
       
   255         : QAbstractTableModel(parent),
       
   256           row_count(rows),
       
   257           column_count(columns),
       
   258           can_fetch_more(false),
       
   259           fetch_more_count(0),
       
   260           disabled_rows(),
       
   261           disabled_columns() {}
       
   262 
       
   263     int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
       
   264     int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
       
   265     bool isEditable(const QModelIndex &) const { return true; }
       
   266 
       
   267     Qt::ItemFlags flags(const QModelIndex &index) const
       
   268     {
       
   269         Qt::ItemFlags index_flags = QAbstractTableModel::flags(index);
       
   270         if (disabled_rows.contains(index.row())
       
   271             || disabled_columns.contains(index.column()))
       
   272             index_flags &= ~Qt::ItemIsEnabled;
       
   273         return index_flags;
       
   274     }
       
   275 
       
   276     void disableRow(int row)
       
   277     {
       
   278         disabled_rows.insert(row);
       
   279     }
       
   280 
       
   281     void enableRow(int row)
       
   282     {
       
   283         disabled_rows.remove(row);
       
   284     }
       
   285 
       
   286     void disableColumn(int column)
       
   287     {
       
   288         disabled_columns.insert(column);
       
   289     }
       
   290 
       
   291     void enableColumn(int column)
       
   292     {
       
   293         disabled_columns.remove(column);
       
   294     }
       
   295 
       
   296     QVariant data(const QModelIndex &idx, int role) const
       
   297     {
       
   298         if (!idx.isValid() || idx.row() >= row_count || idx.column() >= column_count) {
       
   299             qWarning() << "Invalid modelIndex [%d,%d,%p]" << idx;
       
   300             emit invalidIndexEncountered();
       
   301             return QVariant();
       
   302         }
       
   303 
       
   304         if (role == Qt::DisplayRole || role == Qt::EditRole)
       
   305             return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0);
       
   306 
       
   307         return QVariant();
       
   308     }
       
   309 
       
   310     bool insertRows(int start, int count, const QModelIndex &parent = QModelIndex())
       
   311     {
       
   312         if (start < 0 || start > row_count)
       
   313             return false;
       
   314 
       
   315         beginInsertRows(parent, start, start + count - 1);
       
   316         row_count += count;
       
   317         endInsertRows();
       
   318         return true;
       
   319     }
       
   320 
       
   321     bool removeRows(int start, int count, const QModelIndex &parent = QModelIndex())
       
   322     {
       
   323         if (start < 0 || start >= row_count || row_count < count)
       
   324             return false;
       
   325 
       
   326         beginRemoveRows(parent, start, start + count - 1);
       
   327         row_count -= count;
       
   328         endRemoveRows();
       
   329         return true;
       
   330     }
       
   331 
       
   332     void removeLastRow()
       
   333     {
       
   334         beginRemoveRows(QModelIndex(), row_count - 1, row_count - 1);
       
   335         --row_count;
       
   336         endRemoveRows();
       
   337     }
       
   338 
       
   339     void removeAllRows()
       
   340     {
       
   341         beginRemoveRows(QModelIndex(), 0, row_count - 1);
       
   342         row_count = 0;
       
   343         endRemoveRows();
       
   344     }
       
   345 
       
   346     bool insertColumns(int start, int count, const QModelIndex &parent = QModelIndex())
       
   347     {
       
   348         if (start < 0 || start > column_count)
       
   349             return false;
       
   350 
       
   351         beginInsertColumns(parent, start, start + count - 1);
       
   352         column_count += count;
       
   353         endInsertColumns();
       
   354         return true;
       
   355     }
       
   356 
       
   357     bool removeColumns(int start, int count, const QModelIndex &parent = QModelIndex())
       
   358     {
       
   359         if (start < 0 || start >= column_count || column_count < count)
       
   360             return false;
       
   361 
       
   362         beginRemoveColumns(parent, start, start + count - 1);
       
   363         column_count -= count;
       
   364         endRemoveColumns();
       
   365         return true;
       
   366     }
       
   367 
       
   368     void removeLastColumn()
       
   369     {
       
   370         beginRemoveColumns(QModelIndex(), column_count - 1, column_count - 1);
       
   371         --column_count;
       
   372         endRemoveColumns();
       
   373     }
       
   374 
       
   375     void removeAllColumns()
       
   376     {
       
   377         beginRemoveColumns(QModelIndex(), 0, column_count - 1);
       
   378         column_count = 0;
       
   379         endRemoveColumns();
       
   380     }
       
   381 
       
   382     bool canFetchMore(const QModelIndex &) const
       
   383     {
       
   384         return can_fetch_more;
       
   385     }
       
   386 
       
   387     void fetchMore(const QModelIndex &)
       
   388     {
       
   389         ++fetch_more_count;
       
   390     }
       
   391 
       
   392     void reset()
       
   393     {
       
   394         QAbstractTableModel::reset();
       
   395     }
       
   396 
       
   397     int row_count;
       
   398     int column_count;
       
   399     bool can_fetch_more;
       
   400     int fetch_more_count;
       
   401     QSet<int> disabled_rows;
       
   402     QSet<int> disabled_columns;
       
   403 };
       
   404 
       
   405 class QtTestTableView : public QTableView
       
   406 {
       
   407 Q_OBJECT
       
   408 
       
   409 public:
       
   410     QtTestTableView(QWidget *parent = 0) : QTableView(parent), checkSignalOrder(false), hasCurrentChanged(0), hasSelectionChanged(0) {}
       
   411 
       
   412     void setModel(QAbstractItemModel *model)
       
   413     {
       
   414         QTableView::setModel(model);
       
   415         connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
       
   416                      this, SLOT(currentChanged(QModelIndex,QModelIndex)));
       
   417         connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
       
   418                      this, SLOT(itemSelectionChanged(QItemSelection,QItemSelection)));
       
   419     }
       
   420 
       
   421     enum CursorAction {
       
   422         MoveUp       = QAbstractItemView::MoveUp,
       
   423         MoveDown     = QAbstractItemView::MoveDown,
       
   424         MoveLeft     = QAbstractItemView::MoveLeft,
       
   425         MoveRight    = QAbstractItemView::MoveRight,
       
   426         MoveHome     = QAbstractItemView::MoveHome,
       
   427         MoveEnd      = QAbstractItemView::MoveEnd,
       
   428         MovePageUp   = QAbstractItemView::MovePageUp,
       
   429         MovePageDown = QAbstractItemView::MovePageDown,
       
   430         MoveNext     = QAbstractItemView::MoveNext,
       
   431         MovePrevious = QAbstractItemView::MovePrevious
       
   432     };
       
   433 
       
   434     QModelIndex moveCursor(QtTestTableView::CursorAction cursorAction,
       
   435                            Qt::KeyboardModifiers modifiers)
       
   436     {
       
   437         return QTableView::moveCursor((QAbstractItemView::CursorAction)cursorAction, modifiers);
       
   438     }
       
   439 
       
   440     int columnWidthHint(int column) const
       
   441     {
       
   442         return sizeHintForColumn(column);
       
   443     }
       
   444 
       
   445     int rowHeightHint(int row) const
       
   446     {
       
   447         return sizeHintForRow(row);
       
   448     }
       
   449 
       
   450     bool isIndexHidden(const QModelIndex &index) const
       
   451     {
       
   452         return QTableView::isIndexHidden(index);
       
   453     }
       
   454 
       
   455     void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
       
   456     {
       
   457         QTableView::setSelection(rect, command);
       
   458     }
       
   459 
       
   460     QModelIndexList selectedIndexes() const
       
   461     {
       
   462         return QTableView::selectedIndexes();
       
   463     }
       
   464 
       
   465     bool checkSignalOrder;
       
   466 public slots:
       
   467     void currentChanged(QModelIndex , QModelIndex ) {
       
   468         hasCurrentChanged++;
       
   469         if (checkSignalOrder)
       
   470             QVERIFY(hasCurrentChanged > hasSelectionChanged);
       
   471     }
       
   472 
       
   473     void itemSelectionChanged(QItemSelection , QItemSelection ) {
       
   474         hasSelectionChanged++;
       
   475         if (checkSignalOrder)
       
   476             QVERIFY(hasCurrentChanged >= hasSelectionChanged);
       
   477     }
       
   478 private:
       
   479     int hasCurrentChanged;
       
   480     int hasSelectionChanged;
       
   481 
       
   482 };
       
   483 
       
   484 class QtTestItemDelegate : public QItemDelegate
       
   485 {
       
   486 public:
       
   487     QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const
       
   488     {
       
   489         return hint;
       
   490     }
       
   491 
       
   492     QSize hint;
       
   493 };
       
   494 
       
   495 tst_QTableView::tst_QTableView()
       
   496 {
       
   497 }
       
   498 
       
   499 tst_QTableView::~tst_QTableView()
       
   500 {
       
   501 }
       
   502 
       
   503 void tst_QTableView::initTestCase()
       
   504 {
       
   505 #ifdef Q_OS_WINCE //disable magic for WindowsCE
       
   506     qApp->setAutoMaximizeThreshold(-1);
       
   507 #endif
       
   508 }
       
   509 
       
   510 void tst_QTableView::cleanupTestCase()
       
   511 {
       
   512 }
       
   513 
       
   514 void tst_QTableView::init()
       
   515 {
       
   516 }
       
   517 
       
   518 void tst_QTableView::cleanup()
       
   519 {
       
   520 }
       
   521 
       
   522 void tst_QTableView::noDelegate()
       
   523 {
       
   524     QtTestTableModel model(3, 3);
       
   525     QTableView view;
       
   526     view.setModel(&model);
       
   527     view.setItemDelegate(0);
       
   528     view.show();
       
   529 }
       
   530 
       
   531 void tst_QTableView::noModel()
       
   532 {
       
   533     QTableView view;
       
   534     view.show();
       
   535 }
       
   536 
       
   537 void tst_QTableView::emptyModel()
       
   538 {
       
   539     QtTestTableModel model;
       
   540     QTableView view;
       
   541     QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
       
   542     view.setModel(&model);
       
   543     view.show();
       
   544     QCOMPARE(spy.count(), 0);
       
   545 }
       
   546 
       
   547 void tst_QTableView::removeRows_data()
       
   548 {
       
   549     QTest::addColumn<int>("rowCount");
       
   550     QTest::addColumn<int>("columnCount");
       
   551 
       
   552     QTest::newRow("2x2") << 2 << 2;
       
   553     QTest::newRow("10x10") << 10  << 10;
       
   554 }
       
   555 
       
   556 void tst_QTableView::removeRows()
       
   557 {
       
   558     QFETCH(int, rowCount);
       
   559     QFETCH(int, columnCount);
       
   560 
       
   561     QtTestTableModel model(rowCount, columnCount);
       
   562     QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
       
   563 
       
   564     QTableView view;
       
   565     view.setModel(&model);
       
   566     view.show();
       
   567 
       
   568     model.removeLastRow();
       
   569     QCOMPARE(spy.count(), 0);
       
   570 
       
   571     model.removeAllRows();
       
   572     QCOMPARE(spy.count(), 0);
       
   573 }
       
   574 
       
   575 void tst_QTableView::removeColumns_data()
       
   576 {
       
   577     QTest::addColumn<int>("rowCount");
       
   578     QTest::addColumn<int>("columnCount");
       
   579 
       
   580     QTest::newRow("2x2") << 2 << 2;
       
   581     QTest::newRow("10x10") << 10  << 10;
       
   582 }
       
   583 
       
   584 void tst_QTableView::removeColumns()
       
   585 {
       
   586     QFETCH(int, rowCount);
       
   587     QFETCH(int, columnCount);
       
   588 
       
   589     QtTestTableModel model(rowCount, columnCount);
       
   590     QSignalSpy spy(&model, SIGNAL(invalidIndexEncountered()));
       
   591 
       
   592     QTableView view;
       
   593     view.setModel(&model);
       
   594     view.show();
       
   595 
       
   596     model.removeLastColumn();
       
   597     QCOMPARE(spy.count(), 0);
       
   598 
       
   599     model.removeAllColumns();
       
   600     QCOMPARE(spy.count(), 0);
       
   601 }
       
   602 
       
   603 void tst_QTableView::keyboardNavigation_data()
       
   604 {
       
   605     QTest::addColumn<int>("rowCount");
       
   606     QTest::addColumn<int>("columnCount");
       
   607     QTest::addColumn<bool>("tabKeyNavigation");
       
   608     QTest::addColumn<IntList>("keyPresses");
       
   609 
       
   610     QTest::newRow("16x16 model") << 16  << 16 << true
       
   611                                  << (IntList()
       
   612                                      << Qt::Key_Up
       
   613                                      << Qt::Key_Up
       
   614                                      << Qt::Key_Right
       
   615                                      << Qt::Key_Right
       
   616                                      << Qt::Key_Up
       
   617                                      << Qt::Key_Left
       
   618                                      << Qt::Key_Left
       
   619                                      << Qt::Key_Up
       
   620                                      << Qt::Key_Down
       
   621                                      << Qt::Key_Up
       
   622                                      << Qt::Key_Up
       
   623                                      << Qt::Key_Up
       
   624                                      << Qt::Key_Up
       
   625                                      << Qt::Key_Up
       
   626                                      << Qt::Key_Up
       
   627                                      << Qt::Key_Left
       
   628                                      << Qt::Key_Left
       
   629                                      << Qt::Key_Up
       
   630                                      << Qt::Key_Down
       
   631                                      << Qt::Key_Down
       
   632                                      << Qt::Key_Tab
       
   633                                      << Qt::Key_Backtab);
       
   634 
       
   635 
       
   636     QTest::newRow("no tab") << 8  << 8 <<  false
       
   637                                  << (IntList()
       
   638                                      << Qt::Key_Up
       
   639                                      << Qt::Key_Up
       
   640                                      << Qt::Key_Right
       
   641                                      << Qt::Key_Right
       
   642                                      << Qt::Key_Up
       
   643                                      << Qt::Key_Left
       
   644                                      << Qt::Key_Left
       
   645                                      << Qt::Key_Up
       
   646                                      << Qt::Key_Down
       
   647                                      << Qt::Key_Up
       
   648                                      << Qt::Key_Up
       
   649                                      << Qt::Key_Up
       
   650                                      << Qt::Key_Up
       
   651                                      << Qt::Key_Up
       
   652                                      << Qt::Key_Up
       
   653                                      << Qt::Key_Left
       
   654                                      << Qt::Key_Left
       
   655                                      << Qt::Key_Up
       
   656                                      << Qt::Key_Down
       
   657                                      << Qt::Key_Down
       
   658                                      << Qt::Key_Tab
       
   659                                      << Qt::Key_Backtab);
       
   660 }
       
   661 
       
   662 void tst_QTableView::keyboardNavigation()
       
   663 {
       
   664     QFETCH(int, rowCount);
       
   665     QFETCH(int, columnCount);
       
   666     QFETCH(bool, tabKeyNavigation);
       
   667     QFETCH(IntList, keyPresses);
       
   668 
       
   669     QtTestTableModel model(rowCount, columnCount);
       
   670     QTableView view;
       
   671     view.setModel(&model);
       
   672 
       
   673     view.setTabKeyNavigation(tabKeyNavigation);
       
   674     QModelIndex index = model.index(rowCount - 1, columnCount - 1);
       
   675     view.setCurrentIndex(index);
       
   676 
       
   677     view.show();
       
   678     QTest::qWaitForWindowShown(&view);
       
   679     qApp->setActiveWindow(&view);
       
   680 
       
   681     int row = rowCount - 1;
       
   682     int column = columnCount - 1;
       
   683     for (int i = 0; i < keyPresses.count(); ++i) {
       
   684 
       
   685         Qt::Key key = (Qt::Key)keyPresses.at(i);
       
   686 
       
   687         switch (key) {
       
   688         case Qt::Key_Up:
       
   689             row = qMax(0, row - 1);
       
   690             break;
       
   691         case Qt::Key_Down:
       
   692             row = qMin(rowCount - 1, row + 1);
       
   693             break;
       
   694         case Qt::Key_Backtab:
       
   695             if (!tabKeyNavigation)
       
   696                 break;
       
   697         case Qt::Key_Left:
       
   698             column = qMax(0, column - 1);
       
   699             break;
       
   700         case Qt::Key_Tab:
       
   701             if (!tabKeyNavigation)
       
   702                 break;
       
   703         case Qt::Key_Right:
       
   704             column = qMin(columnCount - 1, column + 1);
       
   705             break;
       
   706         default:
       
   707             break;
       
   708         }
       
   709 
       
   710         QTest::keyClick(&view, key);
       
   711         QApplication::processEvents();
       
   712 
       
   713         QModelIndex index = model.index(row, column);
       
   714         QCOMPARE(view.currentIndex(), index);
       
   715     }
       
   716 }
       
   717 
       
   718 void tst_QTableView::headerSections_data()
       
   719 {
       
   720     QTest::addColumn<int>("rowCount");
       
   721     QTest::addColumn<int>("columnCount");
       
   722     QTest::addColumn<int>("row");
       
   723     QTest::addColumn<int>("column");
       
   724     QTest::addColumn<int>("rowHeight");
       
   725     QTest::addColumn<int>("columnWidth");
       
   726 
       
   727     QTest::newRow("") << 10 << 10 << 5 << 5 << 30 << 30;
       
   728 }
       
   729 
       
   730 void tst_QTableView::headerSections()
       
   731 {
       
   732     QFETCH(int, rowCount);
       
   733     QFETCH(int, columnCount);
       
   734     QFETCH(int, row);
       
   735     QFETCH(int, column);
       
   736     QFETCH(int, rowHeight);
       
   737     QFETCH(int, columnWidth);
       
   738 
       
   739     QtTestTableModel model(rowCount, columnCount);
       
   740 
       
   741     QTableView view;
       
   742     QHeaderView *hheader = view.horizontalHeader();
       
   743     QHeaderView *vheader = view.verticalHeader();
       
   744 
       
   745     view.setModel(&model);
       
   746     view.show();
       
   747 
       
   748     hheader->doItemsLayout();
       
   749     vheader->doItemsLayout();
       
   750 
       
   751     QCOMPARE(hheader->count(), model.columnCount());
       
   752     QCOMPARE(vheader->count(), model.rowCount());
       
   753 
       
   754     view.setRowHeight(row, rowHeight);
       
   755     QCOMPARE(view.rowHeight(row), rowHeight);
       
   756     view.hideRow(row);
       
   757     QCOMPARE(view.rowHeight(row), 0);
       
   758     view.showRow(row);
       
   759     QCOMPARE(view.rowHeight(row), rowHeight);
       
   760 
       
   761     view.setColumnWidth(column, columnWidth);
       
   762     QCOMPARE(view.columnWidth(column), columnWidth);
       
   763     view.hideColumn(column);
       
   764     QCOMPARE(view.columnWidth(column), 0);
       
   765     view.showColumn(column);
       
   766     QCOMPARE(view.columnWidth(column), columnWidth);
       
   767 }
       
   768 
       
   769 typedef QPair<int,int> IntPair;
       
   770 Q_DECLARE_METATYPE(IntPair)
       
   771 
       
   772 void tst_QTableView::moveCursor_data()
       
   773 {
       
   774     QTest::addColumn<int>("rowCount");
       
   775     QTest::addColumn<int>("columnCount");
       
   776     QTest::addColumn<int>("hideRow");
       
   777     QTest::addColumn<int>("hideColumn");
       
   778 
       
   779     QTest::addColumn<int>("startRow");
       
   780     QTest::addColumn<int>("startColumn");
       
   781 
       
   782     QTest::addColumn<int>("cursorMoveAction");
       
   783     QTest::addColumn<int>("modifier");
       
   784 
       
   785     QTest::addColumn<int>("expectedRow");
       
   786     QTest::addColumn<int>("expectedColumn");
       
   787     QTest::addColumn<IntPair>("moveRow");
       
   788     QTest::addColumn<IntPair>("moveColumn");
       
   789 
       
   790     // MoveRight
       
   791     QTest::newRow("MoveRight (0,0)")
       
   792         << 4 << 4 << -1 << -1
       
   793         << 0 << 0
       
   794         << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
       
   795         << 0 << 1 << IntPair(0,0) << IntPair(0,0);
       
   796 
       
   797     QTest::newRow("MoveRight (3,0)")
       
   798         << 4 << 4 << -1 << -1
       
   799         << 3 << 0
       
   800         << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
       
   801         << 3 << 1 << IntPair(0,0) << IntPair(0,0);
       
   802 
       
   803     QTest::newRow("MoveRight (3,3)")
       
   804         << 4 << 4 << -1 << -1
       
   805         << 3 << 3
       
   806         << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
       
   807         << 3 << 3 << IntPair(0,0) << IntPair(0,0); // ###
       
   808 
       
   809     QTest::newRow("MoveRight, hidden column 1 (0,0)")
       
   810         << 4 << 4 << -1 << 1
       
   811         << 0 << 0
       
   812         << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
       
   813         << 0 << 2 << IntPair(0,0) << IntPair(0,0);
       
   814 
       
   815     QTest::newRow("MoveRight, hidden column 3 (0,2)")
       
   816         << 4 << 4 << -1 << 3
       
   817         << 0 << 2
       
   818         << int(QtTestTableView::MoveRight) << int(Qt::NoModifier)
       
   819         << 0 << 2 << IntPair(0,0) << IntPair(0,0); // ###
       
   820 
       
   821     // MoveNext should in addition wrap
       
   822     QTest::newRow("MoveNext (0,0)")
       
   823         << 4 << 4 << -1 << -1
       
   824         << 0 << 0
       
   825         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   826         << 0 << 1 << IntPair(0,0) << IntPair(0,0);
       
   827 
       
   828     QTest::newRow("MoveNext (0,2)")
       
   829         << 4 << 4 << -1 << -1
       
   830         << 0 << 2
       
   831         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   832         << 0 << 3 << IntPair(0,0) << IntPair(0,0);
       
   833 
       
   834     QTest::newRow("MoveNext, wrap (0,3)")
       
   835         << 4 << 4 << -1 << -1
       
   836         << 0 << 3
       
   837         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   838         << 1 << 0 << IntPair(0,0) << IntPair(0,0);
       
   839 
       
   840     QTest::newRow("MoveNext, wrap (3,3)")
       
   841         << 4 << 4 << -1 << -1
       
   842         << 3 << 3
       
   843         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   844         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   845 
       
   846     QTest::newRow("MoveNext, hidden column 1 (0,0)")
       
   847         << 4 << 4 << -1 << 1
       
   848         << 0 << 0
       
   849         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   850         << 0 << 2 << IntPair(0,0) << IntPair(0,0);
       
   851 
       
   852     QTest::newRow("MoveNext, wrap, hidden column 3 (0,2)")
       
   853         << 4 << 4 << -1 << 3
       
   854         << 0 << 2
       
   855         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   856         << 1 << 0 << IntPair(0,0) << IntPair(0,0);
       
   857 
       
   858     QTest::newRow("MoveNext, wrap, hidden column 3 (3,2)")
       
   859         << 4 << 4 << -1 << 3
       
   860         << 3 << 2
       
   861         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   862         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   863 
       
   864     QTest::newRow("MoveNext, wrapy, wrapx, hidden column 3, hidden row 3 (2,2)")
       
   865         << 4 << 4 << 3 << 3
       
   866         << 2 << 2
       
   867         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   868         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   869 
       
   870     QTest::newRow("MoveNext, wrap, hidden column 2, moved column from 3 to 0. (0,2)")
       
   871         << 4 << 4 << -1 << 2
       
   872         << 0 << 2
       
   873         << int(QtTestTableView::MoveNext) << int(Qt::NoModifier)
       
   874         << 1 << 3 << IntPair(0,0) << IntPair(3,0);
       
   875 
       
   876     // MoveLeft
       
   877     QTest::newRow("MoveLeft (0,0)")
       
   878         << 4 << 4 << -1 << -1
       
   879         << 0 << 0
       
   880         << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
       
   881         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   882 
       
   883     QTest::newRow("MoveLeft (0,3)")
       
   884         << 4 << 4 << -1 << -1
       
   885         << 0 << 3
       
   886         << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
       
   887         << 0 << 2 << IntPair(0,0) << IntPair(0,0);
       
   888 
       
   889     QTest::newRow("MoveLeft (1,0)")
       
   890         << 4 << 4 << -1 << -1
       
   891         << 1 << 0
       
   892         << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
       
   893         << 1 << 0 << IntPair(0,0) << IntPair(0,0);
       
   894 
       
   895     QTest::newRow("MoveLeft, hidden column 0 (0,2)")
       
   896         << 4 << 4 << -1 << 1
       
   897         << 0 << 2
       
   898         << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
       
   899         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   900 
       
   901     QTest::newRow("MoveLeft, hidden column 0 (0,1)")
       
   902         << 4 << 4 << -1 << 0
       
   903         << 0 << 1
       
   904         << int(QtTestTableView::MoveLeft) << int(Qt::NoModifier)
       
   905         << 0 << 1 << IntPair(0,0) << IntPair(0,0);
       
   906 
       
   907     // MovePrevious should in addition wrap
       
   908     QTest::newRow("MovePrevious (0,3)")
       
   909         << 4 << 4 << -1 << -1
       
   910         << 0 << 3
       
   911         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   912         << 0 << 2 << IntPair(0,0) << IntPair(0,0);
       
   913 
       
   914     QTest::newRow("MovePrevious (0,1)")
       
   915         << 4 << 4 << -1 << -1
       
   916         << 0 << 1
       
   917         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   918         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   919 
       
   920     QTest::newRow("MovePrevious, wrap (1,0)")
       
   921         << 4 << 4 << -1 << -1
       
   922         << 1 << 0
       
   923         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   924         << 0 << 3 << IntPair(0,0) << IntPair(0,0);
       
   925 
       
   926     QTest::newRow("MovePrevious, wrap, (0,0)")
       
   927         << 4 << 4 << -1 << -1
       
   928         << 0 << 0
       
   929         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   930         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
   931 
       
   932     QTest::newRow("MovePrevious, hidden column 1 (0,2)")
       
   933         << 4 << 4 << -1 << 1
       
   934         << 0 << 2
       
   935         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   936         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
   937 
       
   938     QTest::newRow("MovePrevious, wrap, hidden column 3 (0,2)")
       
   939         << 4 << 4 << -1 << 3
       
   940         << 0 << 2
       
   941         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   942         << 0 << 1 << IntPair(0,0) << IntPair(0,0);
       
   943 
       
   944     QTest::newRow("MovePrevious, wrapy, hidden column 0 (0,1)")
       
   945         << 4 << 4 << -1 << 0
       
   946         << 0 << 1
       
   947         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   948         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
   949 
       
   950     QTest::newRow("MovePrevious, wrap, hidden column 0, hidden row 0 (1,1)")
       
   951         << 4 << 4 << 0 << 0
       
   952         << 1 << 1
       
   953         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   954         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
   955 
       
   956     QTest::newRow("MovePrevious, wrap, hidden column 1, moved column from 0 to 3. (1,2)")
       
   957         << 4 << 4 << -1 << 1
       
   958         << 1 << 2
       
   959         << int(QtTestTableView::MovePrevious) << int(Qt::NoModifier)
       
   960         << 0 << 0 << IntPair(0,0) << IntPair(0,3);
       
   961 
       
   962     // MoveDown
       
   963     QTest::newRow("MoveDown (0,0)")
       
   964         << 4 << 4 << -1 << -1
       
   965         << 0 << 0
       
   966         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   967         << 1 << 0 << IntPair(0,0) << IntPair(0,0);
       
   968 
       
   969     QTest::newRow("MoveDown (3,0)")
       
   970         << 4 << 4 << -1 << -1
       
   971         << 3 << 0
       
   972         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   973         << 3 << 0 << IntPair(0,0) << IntPair(0,0);
       
   974 
       
   975     QTest::newRow("MoveDown (3,3)")
       
   976         << 4 << 4 << -1 << -1
       
   977         << 3 << 3
       
   978         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   979         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
   980 
       
   981     QTest::newRow("MoveDown, hidden row 1 (0,0)")
       
   982         << 4 << 4 << 1 << -1
       
   983         << 0 << 0
       
   984         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   985         << 2 << 0 << IntPair(0,0) << IntPair(0,0);
       
   986 
       
   987     QTest::newRow("MoveDown, hidden row 3 (2,0)")
       
   988         << 4 << 4 << 3 << -1
       
   989         << 2 << 0
       
   990         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   991         << 2 << 0 << IntPair(0,0) << IntPair(0,0);
       
   992 
       
   993     QTest::newRow("MoveDown, hidden row 0 hidden column 0 (0,0)")
       
   994         << 4 << 4 << 0 << 0
       
   995         << 0 << 0
       
   996         << int(QtTestTableView::MoveDown) << int(Qt::NoModifier)
       
   997         << 1 << 1 << IntPair(0,0) << IntPair(0,0);
       
   998 
       
   999     // MoveUp
       
  1000     QTest::newRow("MoveUp (0,0)")
       
  1001         << 4 << 4 << -1 << -1
       
  1002         << 0 << 0
       
  1003         << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
       
  1004         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1005 
       
  1006     QTest::newRow("MoveUp (3, 0)")
       
  1007         << 4 << 4 << -1 << -1
       
  1008         << 3 << 0
       
  1009         << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
       
  1010         << 2 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1011 
       
  1012     QTest::newRow("MoveUp (0,1)")
       
  1013         << 4 << 4 << -1 << -1
       
  1014         << 0 << 1
       
  1015         << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
       
  1016         << 0 << 1 << IntPair(0,0) << IntPair(0,0);
       
  1017 
       
  1018     QTest::newRow("MoveUp, hidden row 1 (2,0)")
       
  1019         << 4 << 4 << 1 << -1
       
  1020         << 2 << 0
       
  1021         << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
       
  1022         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1023 
       
  1024     QTest::newRow("MoveUp, hidden row (1,0)")
       
  1025         << 4 << 4 << 0 << -1
       
  1026         << 1 << 0
       
  1027         << int(QtTestTableView::MoveUp) << int(Qt::NoModifier)
       
  1028         << 1 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1029 
       
  1030     // MoveHome
       
  1031     QTest::newRow("MoveHome (0,0)")
       
  1032         << 4 << 4 << -1 << -1
       
  1033         << 0 << 0
       
  1034         << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
       
  1035         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1036 
       
  1037     QTest::newRow("MoveHome (3,3)")
       
  1038         << 4 << 4 << -1 << -1
       
  1039         << 3 << 3
       
  1040         << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
       
  1041         << 3 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1042 
       
  1043     QTest::newRow("MoveHome, hidden column 0 (3,3)")
       
  1044         << 4 << 4 << -1 << 0
       
  1045         << 3 << 3
       
  1046         << int(QtTestTableView::MoveHome) << int(Qt::NoModifier)
       
  1047         << 3 << 1 << IntPair(0,0) << IntPair(0,0);
       
  1048 
       
  1049     // Use Ctrl modifier
       
  1050     QTest::newRow("MoveHome + Ctrl (0,0)")
       
  1051         << 4 << 4 << -1 << -1
       
  1052         << 0 << 0
       
  1053         << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
       
  1054         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1055 
       
  1056     QTest::newRow("MoveHome + Ctrl (3,3)")
       
  1057         << 4 << 4 << -1 << -1
       
  1058         << 3 << 3
       
  1059         << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
       
  1060         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1061 
       
  1062     QTest::newRow("MoveHome + Ctrl, hidden column 0, hidden row 0 (3,3)")
       
  1063         << 4 << 4 << 0 << 0
       
  1064         << 3 << 3
       
  1065         << int(QtTestTableView::MoveHome) << int(Qt::ControlModifier)
       
  1066         << 1 << 1 << IntPair(0,0) << IntPair(0,0);
       
  1067 
       
  1068     // MoveEnd
       
  1069     QTest::newRow("MoveEnd (0,0)")
       
  1070         << 4 << 4 << -1 << -1
       
  1071         << 0 << 0
       
  1072         << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
       
  1073         << 0 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1074 
       
  1075     QTest::newRow("MoveEnd (3,3)")
       
  1076         << 4 << 4 << -1 << -1
       
  1077         << 3 << 3
       
  1078         << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
       
  1079         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1080 
       
  1081     QTest::newRow("MoveEnd, hidden column (0,0)")
       
  1082         << 4 << 4 << -1 << 3
       
  1083         << 0 << 0
       
  1084         << int(QtTestTableView::MoveEnd) << int(Qt::NoModifier)
       
  1085         << 0<< 2 << IntPair(0,0) << IntPair(0,0);
       
  1086 
       
  1087     // Use Ctrl modifier
       
  1088     QTest::newRow("MoveEnd + Ctrl (0,0)")
       
  1089         << 4 << 4 << -1 << -1
       
  1090         << 0 << 0
       
  1091         << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
       
  1092         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1093 
       
  1094     QTest::newRow("MoveEnd + Ctrl (3,3)")
       
  1095         << 4 << 4 << -1 << -1
       
  1096         << 3 << 3
       
  1097         << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
       
  1098         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1099 
       
  1100     QTest::newRow("MoveEnd + Ctrl, hidden column 3 (0,0)")
       
  1101         << 4 << 4 << -1 << 3
       
  1102         << 0 << 0
       
  1103         << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
       
  1104         << 3 << 2 << IntPair(0,0) << IntPair(0,0);
       
  1105 
       
  1106     QTest::newRow("MoveEnd + Ctrl, hidden column 3, hidden row 3 (0,0)")
       
  1107         << 4 << 4 << 3 << 3
       
  1108         << 0 << 0
       
  1109         << int(QtTestTableView::MoveEnd) << int(Qt::ControlModifier)
       
  1110         << 2 << 2 << IntPair(0,0) << IntPair(0,0);
       
  1111 
       
  1112     QTest::newRow("MovePageUp (0,0)")
       
  1113         << 4 << 4 << -1 << -1
       
  1114         << 0 << 0
       
  1115         << int(QtTestTableView::MovePageUp) << 0
       
  1116         << 0 << 0 << IntPair(0,0) << IntPair(0,0);
       
  1117 
       
  1118     QTest::newRow("MovePageUp (3,3)")
       
  1119         << 4 << 4 << -1 << -1
       
  1120         << 3 << 3
       
  1121         << int(QtTestTableView::MovePageUp) << 0
       
  1122         << 0 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1123 
       
  1124     QTest::newRow("MovePageDown (3, 3)")
       
  1125         << 4 << 4 << -1 << -1
       
  1126         << 3 << 3
       
  1127         << int(QtTestTableView::MovePageDown) << 0
       
  1128         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1129 
       
  1130     QTest::newRow("MovePageDown (0, 3)")
       
  1131         << 4 << 4 << -1 << -1
       
  1132         << 0 << 3
       
  1133         << int(QtTestTableView::MovePageDown) << 0
       
  1134         << 3 << 3 << IntPair(0,0) << IntPair(0,0);
       
  1135 }
       
  1136 
       
  1137 void tst_QTableView::moveCursor()
       
  1138 {
       
  1139     QFETCH(int, rowCount);
       
  1140     QFETCH(int, columnCount);
       
  1141     QFETCH(int, hideRow);
       
  1142     QFETCH(int, hideColumn);
       
  1143     QFETCH(int, startRow);
       
  1144     QFETCH(int, startColumn);
       
  1145     QFETCH(int, cursorMoveAction);
       
  1146     QFETCH(int, modifier);
       
  1147     QFETCH(int, expectedRow);
       
  1148     QFETCH(int, expectedColumn);
       
  1149     QFETCH(IntPair, moveRow);
       
  1150     QFETCH(IntPair, moveColumn);
       
  1151 
       
  1152     QtTestTableModel model(rowCount, columnCount);
       
  1153     QtTestTableView view;
       
  1154 
       
  1155     view.setModel(&model);
       
  1156     view.hideRow(hideRow);
       
  1157     view.hideColumn(hideColumn);
       
  1158     if (moveColumn.first != moveColumn.second)
       
  1159         view.horizontalHeader()->moveSection(moveColumn.first, moveColumn.second);
       
  1160     if (moveRow.first != moveRow.second)
       
  1161         view.verticalHeader()->moveSection(moveRow.first, moveRow.second);
       
  1162 
       
  1163     view.show();
       
  1164 
       
  1165     QModelIndex index = model.index(startRow, startColumn);
       
  1166     view.setCurrentIndex(index);
       
  1167 
       
  1168     QModelIndex newIndex = view.moveCursor((QtTestTableView::CursorAction)cursorMoveAction,
       
  1169                                            (Qt::KeyboardModifiers)modifier);
       
  1170     // expected fails, task 119433
       
  1171     if(newIndex.row() == -1)
       
  1172         return;
       
  1173     QCOMPARE(newIndex.row(), expectedRow);
       
  1174     QCOMPARE(newIndex.column(), expectedColumn);
       
  1175 }
       
  1176 
       
  1177 void tst_QTableView::moveCursorStrikesBack_data()
       
  1178 {
       
  1179     QTest::addColumn<int>("hideRow");
       
  1180     QTest::addColumn<int>("hideColumn");
       
  1181     QTest::addColumn<IntList>("disableRows");
       
  1182     QTest::addColumn<IntList>("disableColumns");
       
  1183     QTest::addColumn<QRect>("span");
       
  1184 
       
  1185     QTest::addColumn<int>("startRow");
       
  1186     QTest::addColumn<int>("startColumn");
       
  1187     QTest::addColumn<IntList>("cursorMoveActions");
       
  1188     QTest::addColumn<int>("expectedRow");
       
  1189     QTest::addColumn<int>("expectedColumn");
       
  1190 
       
  1191     QTest::newRow("Last column disabled. Task QTBUG-3878") << -1 << -1
       
  1192             << IntList()
       
  1193             << (IntList() << 6)
       
  1194             << QRect()
       
  1195             << 0 << 5 << (IntList() << int(QtTestTableView::MoveNext))
       
  1196             << 1 << 0;
       
  1197 
       
  1198     QTest::newRow("Last column disabled. Task QTBUG-3878") << -1 << -1
       
  1199             << IntList()
       
  1200             << (IntList() << 6)
       
  1201             << QRect()
       
  1202             << 1 << 0 << (IntList() << int(QtTestTableView::MovePrevious))
       
  1203             << 0 << 5;
       
  1204 
       
  1205     QTest::newRow("Span, anchor column hidden") << -1 << 1
       
  1206             << IntList()
       
  1207             << IntList()
       
  1208             << QRect(1, 2, 2, 3)
       
  1209             << 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
       
  1210             << 2 << 2;
       
  1211 
       
  1212     QTest::newRow("Span, anchor column disabled") << -1 << -1
       
  1213             << IntList()
       
  1214             << (IntList() << 1)
       
  1215             << QRect(1, 2, 2, 3)
       
  1216             << 2 << 0 << (IntList() << int(QtTestTableView::MoveNext))
       
  1217             << 2 << 2;
       
  1218 
       
  1219     QTest::newRow("Span, anchor row hidden") << 2 << -1
       
  1220             << IntList()
       
  1221             << IntList()
       
  1222             << QRect(1, 2, 2, 3)
       
  1223             << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
       
  1224             << 3 << 2;
       
  1225 
       
  1226     QTest::newRow("Span, anchor row disabled") << -1 << -1
       
  1227             << (IntList() << 2)
       
  1228             << IntList()
       
  1229             << QRect(1, 2, 2, 3)
       
  1230             << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown))
       
  1231             << 3 << 2;
       
  1232 
       
  1233     QTest::newRow("Move through span right") << -1 << -1
       
  1234             << IntList()
       
  1235             << IntList()
       
  1236             << QRect(1, 2, 2, 3)
       
  1237             << 3 << 0 << (IntList() << int(QtTestTableView::MoveRight) << int(QtTestTableView::MoveRight))
       
  1238             << 3 << 3;
       
  1239 
       
  1240     QTest::newRow("Move through span left") << -1 << -1
       
  1241             << IntList()
       
  1242             << IntList()
       
  1243             << QRect(1, 2, 2, 3)
       
  1244             << 3 << 3 << (IntList() << int(QtTestTableView::MoveLeft) << int(QtTestTableView::MoveLeft))
       
  1245             << 3 << 0;
       
  1246 
       
  1247     QTest::newRow("Move through span down") << -1 << -1
       
  1248             << IntList()
       
  1249             << IntList()
       
  1250             << QRect(1, 2, 2, 3)
       
  1251             << 1 << 2 << (IntList() << int(QtTestTableView::MoveDown) << int(QtTestTableView::MoveDown))
       
  1252             << 5 << 2;
       
  1253 
       
  1254     QTest::newRow("Move through span up") << -1 << -1
       
  1255             << IntList()
       
  1256             << IntList()
       
  1257             << QRect(1, 2, 2, 3)
       
  1258             << 5 << 2 << (IntList() << int(QtTestTableView::MoveUp) << int(QtTestTableView::MoveUp))
       
  1259             << 1 << 2;
       
  1260 
       
  1261     IntList fullList;
       
  1262     for (int i = 0; i < 7; ++i)
       
  1263         fullList << i;
       
  1264 
       
  1265     QTest::newRow("All disabled, wrap forward. Timeout => FAIL") << -1 << -1
       
  1266             << fullList
       
  1267             << fullList
       
  1268             << QRect()
       
  1269             << 1 << 0 << (IntList() << int(QtTestTableView::MoveNext))
       
  1270             << 1 << 0;
       
  1271 
       
  1272     QTest::newRow("All disabled, wrap backwards. Timeout => FAIL") << -1 << -1
       
  1273             << fullList
       
  1274             << fullList
       
  1275             << QRect()
       
  1276             << 1 << 0 << (IntList() << int(QtTestTableView::MovePrevious))
       
  1277             << 1 << 0;
       
  1278 }
       
  1279 
       
  1280 void tst_QTableView::moveCursorStrikesBack()
       
  1281 {
       
  1282     QFETCH(int, hideRow);
       
  1283     QFETCH(int, hideColumn);
       
  1284     QFETCH(IntList, disableRows);
       
  1285     QFETCH(IntList, disableColumns);
       
  1286     QFETCH(QRect, span);
       
  1287 
       
  1288     QFETCH(int, startRow);
       
  1289     QFETCH(int, startColumn);
       
  1290     QFETCH(IntList, cursorMoveActions);
       
  1291     QFETCH(int, expectedRow);
       
  1292     QFETCH(int, expectedColumn);
       
  1293 
       
  1294     QtTestTableModel model(7, 7);
       
  1295     QtTestTableView view;
       
  1296     view.setModel(&model);
       
  1297     view.hideRow(hideRow);
       
  1298     view.hideColumn(hideColumn);
       
  1299 
       
  1300     if (span.height() && span.width())
       
  1301         view.setSpan(span.top(), span.left(), span.height(), span.width());
       
  1302     view.show();
       
  1303 
       
  1304     QModelIndex index = model.index(startRow, startColumn);
       
  1305     view.setCurrentIndex(index);
       
  1306 
       
  1307     foreach (int row, disableRows)
       
  1308         model.disableRow(row);
       
  1309     foreach (int column, disableColumns)
       
  1310         model.disableColumn(column);
       
  1311 
       
  1312     int newRow = -1;
       
  1313     int newColumn = -1;
       
  1314     foreach (int cursorMoveAction, cursorMoveActions) {
       
  1315         QModelIndex newIndex = view.moveCursor((QtTestTableView::CursorAction)cursorMoveAction, 0);
       
  1316         view.setCurrentIndex(newIndex);
       
  1317         newRow = newIndex.row();
       
  1318         newColumn = newIndex.column();
       
  1319     }
       
  1320 
       
  1321     // expected fails, task 119433
       
  1322     if(newRow == -1)
       
  1323         return;
       
  1324     QCOMPARE(newRow, expectedRow);
       
  1325     QCOMPARE(newColumn, expectedColumn);
       
  1326 }
       
  1327 
       
  1328 void tst_QTableView::hideRows_data()
       
  1329 {
       
  1330     QTest::addColumn<int>("rowCount");
       
  1331     QTest::addColumn<int>("columnCount");
       
  1332     QTest::addColumn<int>("showRow"); // hide, then show
       
  1333     QTest::addColumn<int>("hideRow"); // hide only
       
  1334     QTest::addColumn<int>("row");
       
  1335     QTest::addColumn<int>("column");
       
  1336     QTest::addColumn<int>("rowSpan");
       
  1337     QTest::addColumn<int>("columnSpan");
       
  1338 
       
  1339     QTest::newRow("show row 0, hide row 3, no span")
       
  1340       << 10 << 10
       
  1341       << 0
       
  1342       << 3
       
  1343       << -1 << -1
       
  1344       << 1 << 1;
       
  1345 
       
  1346     QTest::newRow("show row 0, hide row 3, span")
       
  1347       << 10 << 10
       
  1348       << 0
       
  1349       << 3
       
  1350       << 0 << 0
       
  1351       << 3 << 2;
       
  1352 }
       
  1353 
       
  1354 void tst_QTableView::hideRows()
       
  1355 {
       
  1356     QFETCH(int, rowCount);
       
  1357     QFETCH(int, columnCount);
       
  1358     QFETCH(int, showRow);
       
  1359     QFETCH(int, hideRow);
       
  1360     QFETCH(int, row);
       
  1361     QFETCH(int, column);
       
  1362     QFETCH(int, rowSpan);
       
  1363     QFETCH(int, columnSpan);
       
  1364 
       
  1365     QtTestTableModel model(rowCount, columnCount);
       
  1366     QTableView view;
       
  1367 
       
  1368     view.setModel(&model);
       
  1369     view.setSpan(row, column, rowSpan, columnSpan);
       
  1370 
       
  1371     view.hideRow(showRow);
       
  1372     QVERIFY(view.isRowHidden(showRow));
       
  1373 
       
  1374     view.hideRow(hideRow);
       
  1375     QVERIFY(view.isRowHidden(hideRow));
       
  1376 
       
  1377     view.showRow(showRow);
       
  1378     QVERIFY(!view.isRowHidden(showRow));
       
  1379     QVERIFY(view.isRowHidden(hideRow));
       
  1380 }
       
  1381 
       
  1382 void tst_QTableView::hideColumns_data()
       
  1383 {
       
  1384     QTest::addColumn<int>("rowCount");
       
  1385     QTest::addColumn<int>("columnCount");
       
  1386     QTest::addColumn<int>("showColumn"); // hide, then show
       
  1387     QTest::addColumn<int>("hideColumn"); // hide only
       
  1388     QTest::addColumn<int>("row");
       
  1389     QTest::addColumn<int>("column");
       
  1390     QTest::addColumn<int>("rowSpan");
       
  1391     QTest::addColumn<int>("columnSpan");
       
  1392 
       
  1393     QTest::newRow("show col 0, hide col 3, no span")
       
  1394       << 10 << 10
       
  1395       << 0
       
  1396       << 3
       
  1397       << -1 << -1
       
  1398       << 1 << 1;
       
  1399 
       
  1400     QTest::newRow("show col 0, hide col 3, span")
       
  1401       << 10 << 10
       
  1402       << 0
       
  1403       << 3
       
  1404       << 0 << 0
       
  1405       << 3 << 2;
       
  1406 }
       
  1407 
       
  1408 void tst_QTableView::hideColumns()
       
  1409 {
       
  1410     QFETCH(int, rowCount);
       
  1411     QFETCH(int, columnCount);
       
  1412     QFETCH(int, showColumn);
       
  1413     QFETCH(int, hideColumn);
       
  1414     QFETCH(int, row);
       
  1415     QFETCH(int, column);
       
  1416     QFETCH(int, rowSpan);
       
  1417     QFETCH(int, columnSpan);
       
  1418 
       
  1419     QtTestTableModel model(rowCount, columnCount);
       
  1420 
       
  1421     QTableView view;
       
  1422     view.setModel(&model);
       
  1423     view.setSpan(row, column, rowSpan, columnSpan);
       
  1424 
       
  1425     view.hideColumn(showColumn);
       
  1426     QVERIFY(view.isColumnHidden(showColumn));
       
  1427 
       
  1428     view.hideColumn(hideColumn);
       
  1429     QVERIFY(view.isColumnHidden(hideColumn));
       
  1430 
       
  1431     view.showColumn(showColumn);
       
  1432     QVERIFY(!view.isColumnHidden(showColumn));
       
  1433     QVERIFY(view.isColumnHidden(hideColumn));
       
  1434 }
       
  1435 
       
  1436 void tst_QTableView::selection_data()
       
  1437 {
       
  1438     QTest::addColumn<int>("rowCount");
       
  1439     QTest::addColumn<int>("columnCount");
       
  1440     QTest::addColumn<int>("row");
       
  1441     QTest::addColumn<int>("column");
       
  1442     QTest::addColumn<int>("rowSpan");
       
  1443     QTest::addColumn<int>("columnSpan");
       
  1444     QTest::addColumn<int>("hideRow");
       
  1445     QTest::addColumn<int>("hideColumn");
       
  1446     QTest::addColumn<int>("moveRowFrom");
       
  1447     QTest::addColumn<int>("moveRowTo");
       
  1448     QTest::addColumn<int>("moveColumnFrom");
       
  1449     QTest::addColumn<int>("moveColumnTo");
       
  1450     QTest::addColumn<int>("rowHeight");
       
  1451     QTest::addColumn<int>("columnWidth");
       
  1452     QTest::addColumn<int>("x");
       
  1453     QTest::addColumn<int>("y");
       
  1454     QTest::addColumn<int>("width");
       
  1455     QTest::addColumn<int>("height");
       
  1456     QTest::addColumn<int>("command");
       
  1457     QTest::addColumn<int>("selectedCount"); // ### make this more detailed
       
  1458 
       
  1459     QTest::newRow("no span, no hidden, no moved, 3x3 select")
       
  1460       << 10 << 10                          // dim
       
  1461       << -1 << -1                          // pos
       
  1462       << 1 << 1                            // span
       
  1463       << -1 << -1                          // hide
       
  1464       << -1 << -1                          // move row
       
  1465       << -1 << -1                          // move col
       
  1466       << 40 << 40                          // cell size
       
  1467       << 20 << 20 << 80 << 80              // rect
       
  1468       << int(QItemSelectionModel::Select)  // command
       
  1469       << 9;                                // selected count
       
  1470 
       
  1471     QTest::newRow("row span, no hidden, no moved, 3x3 select")
       
  1472       << 10 << 10                          // dim
       
  1473       << 1 << 1                            // pos
       
  1474       << 2 << 1                            // span
       
  1475       << -1 << -1                          // hide
       
  1476       << -1 << -1                          // move row
       
  1477       << -1 << -1                          // move col
       
  1478       << 40 << 40                          // cell size
       
  1479       << 20 << 20 << 80 << 80              // rect
       
  1480       << int(QItemSelectionModel::Select)  // command
       
  1481       << 8;                                // selected count
       
  1482 
       
  1483     QTest::newRow("col span, no hidden, no moved, 3x3 select")
       
  1484       << 10 << 10                          // dim
       
  1485       << 1 << 1                            // pos
       
  1486       << 1 << 2                            // span
       
  1487       << -1 << -1                          // hide
       
  1488       << -1 << -1                          // move row
       
  1489       << -1 << -1                          // move col
       
  1490       << 40 << 40                          // cell size
       
  1491       << 20 << 20 << 80 << 80              // rect
       
  1492       << int(QItemSelectionModel::Select)  // command
       
  1493       << 8;                                // selected count
       
  1494 
       
  1495     QTest::newRow("no span, row hidden, no moved, 3x3 select")
       
  1496       << 10 << 10                          // dim
       
  1497       << -1 << -1                          // pos
       
  1498       << 1 << 1                            // span
       
  1499       << 1 << -1                           // hide
       
  1500       << -1 << -1                          // move row
       
  1501       << -1 << -1                          // move col
       
  1502       << 40 << 40                          // cell size
       
  1503       << 20 << 20 << 80 << 80              // rect
       
  1504       << int(QItemSelectionModel::Select)  // command
       
  1505       << 9;                                // selected count
       
  1506 
       
  1507     QTest::newRow("no span, col hidden, no moved, 3x3 select")
       
  1508       << 10 << 10                          // dim
       
  1509       << -1 << -1                          // pos
       
  1510       << 1 << 1                            // span
       
  1511       << -1 << 1                           // hide
       
  1512       << -1 << -1                          // move row
       
  1513       << -1 << -1                          // move col
       
  1514       << 40 << 40                          // cell size
       
  1515       << 20 << 20 << 80 << 80              // rect
       
  1516       << int(QItemSelectionModel::Select)  // command
       
  1517       << 9;                                // selected count
       
  1518 
       
  1519     QTest::newRow("no span, no hidden, row moved, 3x3 select")
       
  1520       << 10 << 10                          // dim
       
  1521       << -1 << -1                          // pos
       
  1522       << 1 << 1                            // span
       
  1523       << -1 << -1                          // hide
       
  1524       << 1 << 3                            // move row
       
  1525       << -1 << -1                          // move col
       
  1526       << 40 << 40                          // cell size
       
  1527       << 20 << 20 << 80 << 80              // rect
       
  1528       << int(QItemSelectionModel::Select)  // command
       
  1529       << 9;                                // selected count
       
  1530 
       
  1531     QTest::newRow("no span, no hidden, col moved, 3x3 select")
       
  1532       << 10 << 10                          // dim
       
  1533       << -1 << -1                          // pos
       
  1534       << 1 << 1                            // span
       
  1535       << -1 << -1                          // hide
       
  1536       << -1 << -1                          // move row
       
  1537       << 1 << 3                            // move col
       
  1538       << 40 << 40                          // cell size
       
  1539       << 20 << 20 << 80 << 80              // rect
       
  1540       << int(QItemSelectionModel::Select)  // command
       
  1541       << 9;                                // selected count
       
  1542 }
       
  1543 
       
  1544 void tst_QTableView::selection()
       
  1545 {
       
  1546     QFETCH(int, rowCount);
       
  1547     QFETCH(int, columnCount);
       
  1548     QFETCH(int, row);
       
  1549     QFETCH(int, column);
       
  1550     QFETCH(int, rowSpan);
       
  1551     QFETCH(int, columnSpan);
       
  1552     QFETCH(int, hideRow);
       
  1553     QFETCH(int, hideColumn);
       
  1554     QFETCH(int, moveRowFrom);
       
  1555     QFETCH(int, moveRowTo);
       
  1556     QFETCH(int, moveColumnFrom);
       
  1557     QFETCH(int, moveColumnTo);
       
  1558     QFETCH(int, rowHeight);
       
  1559     QFETCH(int, columnWidth);
       
  1560     QFETCH(int, x);
       
  1561     QFETCH(int, y);
       
  1562     QFETCH(int, width);
       
  1563     QFETCH(int, height);
       
  1564     QFETCH(int, command);
       
  1565     QFETCH(int, selectedCount);
       
  1566 
       
  1567     QtTestTableModel model(rowCount, columnCount);
       
  1568 
       
  1569     QtTestTableView view;
       
  1570     view.show();
       
  1571     view.setModel(&model);
       
  1572 
       
  1573     view.setSpan(row, column, rowSpan, columnSpan);
       
  1574 
       
  1575     view.hideRow(hideRow);
       
  1576     view.hideColumn(hideColumn);
       
  1577 
       
  1578     view.verticalHeader()->moveSection(moveRowFrom, moveRowTo);
       
  1579     view.horizontalHeader()->moveSection(moveColumnFrom, moveColumnTo);
       
  1580 
       
  1581     for (int r = 0; r < rowCount; ++r)
       
  1582         view.setRowHeight(r, rowHeight);
       
  1583     for (int c = 0; c < columnCount; ++c)
       
  1584         view.setColumnWidth(c, columnWidth);
       
  1585 
       
  1586     view.setSelection(QRect(x, y, width, height),
       
  1587 		      QItemSelectionModel::SelectionFlags(command));
       
  1588 
       
  1589     QCOMPARE(view.selectedIndexes().count(), selectedCount);
       
  1590 }
       
  1591 
       
  1592 void tst_QTableView::selectRow_data()
       
  1593 {
       
  1594     QTest::addColumn<int>("rowCount");
       
  1595     QTest::addColumn<int>("columnCount");
       
  1596     QTest::addColumn<int>("row");
       
  1597     QTest::addColumn<int>("mode");
       
  1598     QTest::addColumn<int>("behavior");
       
  1599     QTest::addColumn<int>("selectedItems");
       
  1600 
       
  1601     QTest::newRow("SingleSelection and SelectItems")
       
  1602         << 10 << 10
       
  1603         << 0
       
  1604         << (int)QAbstractItemView::SingleSelection
       
  1605         << (int)QAbstractItemView::SelectItems
       
  1606         << 0;
       
  1607 
       
  1608     QTest::newRow("SingleSelection and SelectRows")
       
  1609         << 10 << 10
       
  1610         << 0
       
  1611         << (int)QAbstractItemView::SingleSelection
       
  1612         << (int)QAbstractItemView::SelectRows
       
  1613         << 10;
       
  1614 
       
  1615     QTest::newRow("SingleSelection and SelectColumns")
       
  1616         << 10 << 10
       
  1617         << 0
       
  1618         << (int)QAbstractItemView::SingleSelection
       
  1619         << (int)QAbstractItemView::SelectColumns
       
  1620         << 0;
       
  1621 
       
  1622     QTest::newRow("MultiSelection and SelectItems")
       
  1623         << 10 << 10
       
  1624         << 0
       
  1625         << (int)QAbstractItemView::MultiSelection
       
  1626         << (int)QAbstractItemView::SelectItems
       
  1627         << 10;
       
  1628 
       
  1629     QTest::newRow("MultiSelection and SelectRows")
       
  1630         << 10 << 10
       
  1631         << 0
       
  1632         << (int)QAbstractItemView::MultiSelection
       
  1633         << (int)QAbstractItemView::SelectRows
       
  1634         << 10;
       
  1635 
       
  1636     QTest::newRow("MultiSelection and SelectColumns")
       
  1637         << 10 << 10
       
  1638         << 0
       
  1639         << (int)QAbstractItemView::MultiSelection
       
  1640         << (int)QAbstractItemView::SelectColumns
       
  1641         << 0;
       
  1642 
       
  1643     QTest::newRow("ExtendedSelection and SelectItems")
       
  1644         << 10 << 10
       
  1645         << 0
       
  1646         << (int)QAbstractItemView::ExtendedSelection
       
  1647         << (int)QAbstractItemView::SelectItems
       
  1648         << 10;
       
  1649 
       
  1650     QTest::newRow("ExtendedSelection and SelectRows")
       
  1651         << 10 << 10
       
  1652         << 0
       
  1653         << (int)QAbstractItemView::ExtendedSelection
       
  1654         << (int)QAbstractItemView::SelectRows
       
  1655         << 10;
       
  1656 
       
  1657     QTest::newRow("ExtendedSelection and SelectColumns")
       
  1658         << 10 << 10
       
  1659         << 0
       
  1660         << (int)QAbstractItemView::ExtendedSelection
       
  1661         << (int)QAbstractItemView::SelectColumns
       
  1662         << 0;
       
  1663 
       
  1664     QTest::newRow("ContiguousSelection and SelectItems")
       
  1665         << 10 << 10
       
  1666         << 0
       
  1667         << (int)QAbstractItemView::ContiguousSelection
       
  1668         << (int)QAbstractItemView::SelectItems
       
  1669         << 10;
       
  1670 
       
  1671     QTest::newRow("ContiguousSelection and SelectRows")
       
  1672         << 10 << 10
       
  1673         << 0
       
  1674         << (int)QAbstractItemView::ContiguousSelection
       
  1675         << (int)QAbstractItemView::SelectRows
       
  1676         << 10;
       
  1677 
       
  1678     QTest::newRow("ContiguousSelection and SelectColumns")
       
  1679         << 10 << 10
       
  1680         << 0
       
  1681         << (int)QAbstractItemView::ContiguousSelection
       
  1682         << (int)QAbstractItemView::SelectColumns
       
  1683         << 0;
       
  1684 }
       
  1685 
       
  1686 void tst_QTableView::selectRow()
       
  1687 {
       
  1688     QFETCH(int, rowCount);
       
  1689     QFETCH(int, columnCount);
       
  1690     QFETCH(int, row);
       
  1691     QFETCH(int, mode);
       
  1692     QFETCH(int, behavior);
       
  1693     QFETCH(int, selectedItems);
       
  1694 
       
  1695     QtTestTableModel model(rowCount, columnCount);
       
  1696     QTableView view;
       
  1697 
       
  1698     view.setModel(&model);
       
  1699     view.setSelectionMode((QAbstractItemView::SelectionMode)mode);
       
  1700     view.setSelectionBehavior((QAbstractItemView::SelectionBehavior)behavior);
       
  1701 
       
  1702     QCOMPARE(view.selectionModel()->selectedIndexes().count(), 0);
       
  1703 
       
  1704     view.selectRow(row);
       
  1705 
       
  1706     //test we have 10 items selected
       
  1707     QCOMPARE(view.selectionModel()->selectedIndexes().count(), selectedItems);
       
  1708     //test that all 10 items are in the same row
       
  1709     for (int i = 0; selectedItems > 0 && i < rowCount; ++i)
       
  1710         QCOMPARE(view.selectionModel()->selectedIndexes().at(i).row(), row);
       
  1711 }
       
  1712 
       
  1713 void tst_QTableView::selectColumn_data()
       
  1714 {
       
  1715     QTest::addColumn<int>("rowCount");
       
  1716     QTest::addColumn<int>("columnCount");
       
  1717     QTest::addColumn<int>("column");
       
  1718     QTest::addColumn<int>("mode");
       
  1719     QTest::addColumn<int>("behavior");
       
  1720     QTest::addColumn<int>("selectedItems");
       
  1721 
       
  1722         QTest::newRow("SingleSelection and SelectItems")
       
  1723             << 10 << 10
       
  1724             << 0
       
  1725             << (int)QAbstractItemView::SingleSelection
       
  1726             << (int)QAbstractItemView::SelectItems
       
  1727             << 0;
       
  1728 
       
  1729         QTest::newRow("SingleSelection and SelectRows")
       
  1730             << 10 << 10
       
  1731             << 0
       
  1732             << (int)QAbstractItemView::SingleSelection
       
  1733             << (int)QAbstractItemView::SelectRows
       
  1734             << 0;
       
  1735 
       
  1736         QTest::newRow("SingleSelection and SelectColumns")
       
  1737             << 10 << 10
       
  1738             << 0
       
  1739             << (int)QAbstractItemView::SingleSelection
       
  1740             << (int)QAbstractItemView::SelectColumns
       
  1741             << 10;
       
  1742 
       
  1743         QTest::newRow("MultiSelection and SelectItems")
       
  1744             << 10 << 10
       
  1745             << 0
       
  1746             << (int)QAbstractItemView::MultiSelection
       
  1747             << (int)QAbstractItemView::SelectItems
       
  1748             << 10;
       
  1749 
       
  1750         QTest::newRow("MultiSelection and SelectRows")
       
  1751             << 10 << 10
       
  1752             << 0
       
  1753             << (int)QAbstractItemView::MultiSelection
       
  1754             << (int)QAbstractItemView::SelectRows
       
  1755             << 0;
       
  1756 
       
  1757         QTest::newRow("MultiSelection and SelectColumns")
       
  1758             << 10 << 10
       
  1759             << 0
       
  1760             << (int)QAbstractItemView::MultiSelection
       
  1761             << (int)QAbstractItemView::SelectColumns
       
  1762             << 10;
       
  1763 
       
  1764         QTest::newRow("ExtendedSelection and SelectItems")
       
  1765             << 10 << 10
       
  1766             << 0
       
  1767             << (int)QAbstractItemView::ExtendedSelection
       
  1768             << (int)QAbstractItemView::SelectItems
       
  1769             << 10;
       
  1770 
       
  1771         QTest::newRow("ExtendedSelection and SelectRows")
       
  1772             << 10 << 10
       
  1773             << 0
       
  1774             << (int)QAbstractItemView::ExtendedSelection
       
  1775             << (int)QAbstractItemView::SelectRows
       
  1776             << 0;
       
  1777 
       
  1778         QTest::newRow("ExtendedSelection and SelectColumns")
       
  1779             << 10 << 10
       
  1780             << 0
       
  1781             << (int)QAbstractItemView::ExtendedSelection
       
  1782             << (int)QAbstractItemView::SelectColumns
       
  1783             << 10;
       
  1784 
       
  1785         QTest::newRow("ContiguousSelection and SelectItems")
       
  1786             << 10 << 10
       
  1787             << 0
       
  1788             << (int)QAbstractItemView::ContiguousSelection
       
  1789             << (int)QAbstractItemView::SelectItems
       
  1790             << 10;
       
  1791 
       
  1792         QTest::newRow("ContiguousSelection and SelectRows")
       
  1793             << 10 << 10
       
  1794             << 0
       
  1795             << (int)QAbstractItemView::ContiguousSelection
       
  1796             << (int)QAbstractItemView::SelectRows
       
  1797             << 0;
       
  1798 
       
  1799         QTest::newRow("ContiguousSelection and SelectColumns")
       
  1800             << 10 << 10
       
  1801             << 0
       
  1802             << (int)QAbstractItemView::ContiguousSelection
       
  1803             << (int)QAbstractItemView::SelectColumns
       
  1804             << 10;
       
  1805 }
       
  1806 
       
  1807 void tst_QTableView::selectColumn()
       
  1808 {
       
  1809     QFETCH(int, rowCount);
       
  1810     QFETCH(int, columnCount);
       
  1811     QFETCH(int, column);
       
  1812     QFETCH(int, mode);
       
  1813     QFETCH(int, behavior);
       
  1814     QFETCH(int, selectedItems);
       
  1815 
       
  1816     QtTestTableModel model(rowCount, columnCount);
       
  1817 
       
  1818     QTableView view;
       
  1819     view.setModel(&model);
       
  1820     view.setSelectionMode((QAbstractItemView::SelectionMode)mode);
       
  1821     view.setSelectionBehavior((QAbstractItemView::SelectionBehavior)behavior);
       
  1822 
       
  1823     QCOMPARE(view.selectionModel()->selectedIndexes().count(), 0);
       
  1824 
       
  1825     view.selectColumn(column);
       
  1826 
       
  1827     QCOMPARE(view.selectionModel()->selectedIndexes().count(), selectedItems);
       
  1828     for (int i = 0; selectedItems > 0 && i < columnCount; ++i)
       
  1829         QCOMPARE(view.selectionModel()->selectedIndexes().at(i).column(), column);
       
  1830 }
       
  1831 
       
  1832 Q_DECLARE_METATYPE(QRect)
       
  1833 void tst_QTableView::visualRect_data()
       
  1834 {
       
  1835     QTest::addColumn<int>("rowCount");
       
  1836     QTest::addColumn<int>("columnCount");
       
  1837     QTest::addColumn<int>("hideRow");
       
  1838     QTest::addColumn<int>("hideColumn");
       
  1839     QTest::addColumn<int>("row");
       
  1840     QTest::addColumn<int>("column");
       
  1841     QTest::addColumn<int>("rowHeight");
       
  1842     QTest::addColumn<int>("columnWidth");
       
  1843     QTest::addColumn<QRect>("expectedRect");
       
  1844 
       
  1845     QTest::newRow("(0,0)")
       
  1846         << 10 << 10
       
  1847         << -1 << -1
       
  1848         << 0 << 0
       
  1849         << 20 << 30
       
  1850         << QRect(0, 0, 29, 19);
       
  1851 
       
  1852     QTest::newRow("(0,0) hidden row")
       
  1853         << 10 << 10
       
  1854         << 0 << -1
       
  1855         << 0 << 0
       
  1856         << 20 << 30
       
  1857         << QRect();
       
  1858 
       
  1859     QTest::newRow("(0,0) hidden column")
       
  1860         << 10 << 10
       
  1861         << -1 << 0
       
  1862         << 0 << 0
       
  1863         << 20 << 30
       
  1864         << QRect();
       
  1865 
       
  1866     QTest::newRow("(0,0) hidden row and column")
       
  1867         << 10 << 10
       
  1868         << 0 << 0
       
  1869         << 0 << 0
       
  1870         << 20 << 30
       
  1871         << QRect();
       
  1872 
       
  1873     QTest::newRow("(0,0) out of bounds")
       
  1874         << 10 << 10
       
  1875         << -1 << -1
       
  1876         << 20 << 20
       
  1877         << 20 << 30
       
  1878         << QRect();
       
  1879 
       
  1880     QTest::newRow("(5,5), hidden row")
       
  1881         << 10 << 10
       
  1882         << 5 << -1
       
  1883         << 5 << 5
       
  1884         << 20 << 30
       
  1885         << QRect();
       
  1886 
       
  1887     QTest::newRow("(9,9)")
       
  1888         << 10 << 10
       
  1889         << -1 << -1
       
  1890         << 9 << 9
       
  1891         << 20 << 30
       
  1892         << QRect(30*9, 20*9, 29, 19);
       
  1893 }
       
  1894 
       
  1895 void tst_QTableView::visualRect()
       
  1896 {
       
  1897     QFETCH(int, rowCount);
       
  1898     QFETCH(int, columnCount);
       
  1899     QFETCH(int, hideRow);
       
  1900     QFETCH(int, hideColumn);
       
  1901     QFETCH(int, row);
       
  1902     QFETCH(int, column);
       
  1903     QFETCH(int, rowHeight);
       
  1904     QFETCH(int, columnWidth);
       
  1905     QFETCH(QRect, expectedRect);
       
  1906 
       
  1907     QtTestTableModel model(rowCount, columnCount);
       
  1908 
       
  1909     QTableView view;
       
  1910     view.setModel(&model);
       
  1911     // Make sure that it has 1 pixel between each cell.
       
  1912     view.setGridStyle(Qt::SolidLine);
       
  1913     for (int i = 0; i < view.verticalHeader()->count(); ++i)
       
  1914         view.verticalHeader()->resizeSection(i, rowHeight);
       
  1915     for (int i = 0; i < view.horizontalHeader()->count(); ++i)
       
  1916         view.horizontalHeader()->resizeSection(i, columnWidth);
       
  1917 
       
  1918     view.hideRow(hideRow);
       
  1919     view.hideColumn(hideColumn);
       
  1920 
       
  1921     QRect rect = view.visualRect(model.index(row, column));
       
  1922     QCOMPARE(rect, expectedRect);
       
  1923 }
       
  1924 
       
  1925 void tst_QTableView::fetchMore()
       
  1926 {
       
  1927     QtTestTableModel model(64, 64);
       
  1928 
       
  1929     model.can_fetch_more = true;
       
  1930 
       
  1931     QTableView view;
       
  1932     view.setModel(&model);
       
  1933     view.show();
       
  1934 
       
  1935     QCOMPARE(model.fetch_more_count, 0);
       
  1936     view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum());
       
  1937     QVERIFY(model.fetch_more_count > 0);
       
  1938 
       
  1939     model.fetch_more_count = 0; //reset
       
  1940     view.scrollToTop();
       
  1941     QCOMPARE(model.fetch_more_count, 0);
       
  1942 
       
  1943     view.scrollToBottom();
       
  1944     QVERIFY(model.fetch_more_count > 0);
       
  1945 
       
  1946     model.fetch_more_count = 0; //reset
       
  1947     view.scrollToTop();
       
  1948     view.setCurrentIndex(model.index(0, 0));
       
  1949     QCOMPARE(model.fetch_more_count, 0);
       
  1950 
       
  1951     for (int i = 0; i < 64; ++i)
       
  1952         QTest::keyClick(&view, Qt::Key_Down);
       
  1953     QCOMPARE(view.currentIndex(), model.index(63, 0));
       
  1954     QVERIFY(model.fetch_more_count > 0);
       
  1955 }
       
  1956 
       
  1957 void tst_QTableView::setHeaders()
       
  1958 {
       
  1959     QTableView view;
       
  1960 
       
  1961     // Make sure we don't delete ourselves
       
  1962     view.setVerticalHeader(view.verticalHeader());
       
  1963     view.verticalHeader()->count();
       
  1964     view.setHorizontalHeader(view.horizontalHeader());
       
  1965     view.horizontalHeader()->count();
       
  1966 
       
  1967     // Try passing around a header without it being deleted
       
  1968     QTableView view2;
       
  1969     view2.setVerticalHeader(view.verticalHeader());
       
  1970     view2.setHorizontalHeader(view.horizontalHeader());
       
  1971     view.setHorizontalHeader(new QHeaderView(Qt::Horizontal));
       
  1972     view.setVerticalHeader(new QHeaderView(Qt::Vertical));
       
  1973     view2.verticalHeader()->count();
       
  1974     view2.horizontalHeader()->count();
       
  1975 
       
  1976 }
       
  1977 
       
  1978 void tst_QTableView::resizeRowsToContents_data()
       
  1979 {
       
  1980     QTest::addColumn<int>("rowCount");
       
  1981     QTest::addColumn<int>("columnCount");
       
  1982     QTest::addColumn<bool>("showGrid");
       
  1983     QTest::addColumn<int>("cellWidth");
       
  1984     QTest::addColumn<int>("cellHeight");
       
  1985     QTest::addColumn<int>("rowHeight");
       
  1986     QTest::addColumn<int>("columnWidth");
       
  1987 
       
  1988     QTest::newRow("10x10 grid shown 40x40")
       
  1989         << 10 << 10 << false << 40 << 40 << 40 << 40;
       
  1990 
       
  1991     QTest::newRow("10x10 grid not shown 40x40")
       
  1992         << 10 << 10 << true << 40 << 40 << 41 << 41;
       
  1993 }
       
  1994 
       
  1995 void tst_QTableView::resizeRowsToContents()
       
  1996 {
       
  1997     QFETCH(int, rowCount);
       
  1998     QFETCH(int, columnCount);
       
  1999     QFETCH(bool, showGrid);
       
  2000     QFETCH(int, cellWidth);
       
  2001     QFETCH(int, cellHeight);
       
  2002     QFETCH(int, rowHeight);
       
  2003     QFETCH(int, columnWidth);
       
  2004     Q_UNUSED(columnWidth);
       
  2005 
       
  2006     QtTestTableModel model(rowCount, columnCount);
       
  2007     QtTestTableView view;
       
  2008     QtTestItemDelegate delegate;
       
  2009 
       
  2010     view.setModel(&model);
       
  2011     view.setItemDelegate(&delegate);
       
  2012     view.setShowGrid(showGrid); // the grid will add to the row height
       
  2013 
       
  2014     delegate.hint = QSize(cellWidth, cellHeight);
       
  2015 
       
  2016     QSignalSpy resizedSpy(view.verticalHeader(), SIGNAL(sectionResized(int, int, int)));
       
  2017     view.resizeRowsToContents();
       
  2018 
       
  2019     QCOMPARE(resizedSpy.count(), model.rowCount());
       
  2020     for (int r = 0; r < model.rowCount(); ++r)
       
  2021         QCOMPARE(view.rowHeight(r), rowHeight);
       
  2022 }
       
  2023 
       
  2024 void tst_QTableView::resizeColumnsToContents_data()
       
  2025 {
       
  2026     QTest::addColumn<int>("rowCount");
       
  2027     QTest::addColumn<int>("columnCount");
       
  2028     QTest::addColumn<bool>("showGrid");
       
  2029     QTest::addColumn<int>("cellWidth");
       
  2030     QTest::addColumn<int>("cellHeight");
       
  2031     QTest::addColumn<int>("rowHeight");
       
  2032     QTest::addColumn<int>("columnWidth");
       
  2033 
       
  2034     QTest::newRow("10x10 grid shown 40x40")
       
  2035         << 10 << 10 << false << 40 << 40 << 40 << 40;
       
  2036 
       
  2037     QTest::newRow("10x10 grid not shown 40x40")
       
  2038         << 10 << 10 << true << 40 << 40 << 41 << 41;
       
  2039 }
       
  2040 
       
  2041 void tst_QTableView::resizeColumnsToContents()
       
  2042 {
       
  2043     QFETCH(int, rowCount);
       
  2044     QFETCH(int, columnCount);
       
  2045     QFETCH(bool, showGrid);
       
  2046     QFETCH(int, cellWidth);
       
  2047     QFETCH(int, cellHeight);
       
  2048     QFETCH(int, rowHeight);
       
  2049     QFETCH(int, columnWidth);
       
  2050     Q_UNUSED(rowHeight);
       
  2051 
       
  2052     QtTestTableModel model(rowCount, columnCount);
       
  2053     QtTestTableView view;
       
  2054     QtTestItemDelegate delegate;
       
  2055 
       
  2056     view.setModel(&model);
       
  2057     view.setItemDelegate(&delegate);
       
  2058     view.setShowGrid(showGrid); // the grid will add to the row height
       
  2059 
       
  2060     delegate.hint = QSize(cellWidth, cellHeight);
       
  2061 
       
  2062     QSignalSpy resizedSpy(view.horizontalHeader(), SIGNAL(sectionResized(int, int, int)));
       
  2063     view.resizeColumnsToContents();
       
  2064 
       
  2065     QCOMPARE(resizedSpy.count(), model.columnCount());
       
  2066     for (int c = 0; c < model.columnCount(); ++c)
       
  2067         QCOMPARE(view.columnWidth(c), columnWidth);
       
  2068 }
       
  2069 
       
  2070 void tst_QTableView::rowViewportPosition_data()
       
  2071 {
       
  2072     QTest::addColumn<int>("rowCount");
       
  2073     QTest::addColumn<int>("rowHeight");
       
  2074     QTest::addColumn<int>("row");
       
  2075     QTest::addColumn<int>("verticalScrollMode");
       
  2076     QTest::addColumn<int>("verticalScrollValue");
       
  2077     QTest::addColumn<int>("rowViewportPosition");
       
  2078 
       
  2079     QTest::newRow("row 0, scroll per item 0")
       
  2080         << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerItem) << 0 << 0;
       
  2081 
       
  2082     QTest::newRow("row 1, scroll per item, 0")
       
  2083         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 0 << 1 * 40;
       
  2084 
       
  2085     QTest::newRow("row 1, scroll per item, 1")
       
  2086         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 1 << 0;
       
  2087 
       
  2088     QTest::newRow("row 5, scroll per item, 0")
       
  2089         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 0 << 5 * 40;
       
  2090 
       
  2091     QTest::newRow("row 5, scroll per item, 5")
       
  2092         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 5 << 0;
       
  2093 
       
  2094     QTest::newRow("row 9, scroll per item, 0")
       
  2095         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 0 << 9 * 40;
       
  2096 
       
  2097     QTest::newRow("row 9, scroll per item, 5")
       
  2098         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 5 << 4 * 40;
       
  2099 
       
  2100     QTest::newRow("row 0, scroll per pixel 0")
       
  2101         << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerPixel) << 0 << 0;
       
  2102 
       
  2103     QTest::newRow("row 1, scroll per pixel, 0")
       
  2104         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 0 << 1 * 40;
       
  2105 
       
  2106     QTest::newRow("row 1, scroll per pixel, 1")
       
  2107         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 1 * 40 << 0;
       
  2108 
       
  2109     QTest::newRow("row 5, scroll per pixel, 0")
       
  2110         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 0 << 5 * 40;
       
  2111 
       
  2112     QTest::newRow("row 5, scroll per pixel, 5")
       
  2113         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 0;
       
  2114 
       
  2115     QTest::newRow("row 9, scroll per pixel, 0")
       
  2116         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 0 << 9 * 40;
       
  2117 
       
  2118     QTest::newRow("row 9, scroll per pixel, 5")
       
  2119         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 4 * 40;
       
  2120 }
       
  2121 
       
  2122 void tst_QTableView::rowViewportPosition()
       
  2123 {
       
  2124     QFETCH(int, rowCount);
       
  2125     QFETCH(int, rowHeight);
       
  2126     QFETCH(int, row);
       
  2127     QFETCH(int, verticalScrollMode);
       
  2128     QFETCH(int, verticalScrollValue);
       
  2129     QFETCH(int, rowViewportPosition);
       
  2130 
       
  2131     QtTestTableModel model(rowCount, 1);
       
  2132     QtTestTableView view;
       
  2133     view.resize(100, 2 * rowHeight);
       
  2134     view.show();
       
  2135 
       
  2136     view.setModel(&model);
       
  2137     for (int r = 0; r < rowCount; ++r)
       
  2138         view.setRowHeight(r, rowHeight);
       
  2139 
       
  2140     view.setVerticalScrollMode((QAbstractItemView::ScrollMode)verticalScrollMode);
       
  2141     view.verticalScrollBar()->setValue(verticalScrollValue);
       
  2142 
       
  2143     QCOMPARE(view.rowViewportPosition(row), rowViewportPosition);
       
  2144 }
       
  2145 
       
  2146 void tst_QTableView::rowAt_data()
       
  2147 {
       
  2148     QTest::addColumn<int>("rowCount");
       
  2149     QTest::addColumn<int>("rowHeight");
       
  2150     QTest::addColumn<IntList>("hiddenRows");
       
  2151     QTest::addColumn<int>("coordinate");
       
  2152     QTest::addColumn<int>("row");
       
  2153 
       
  2154     QTest::newRow("row at 100") << 5 << 40 << IntList() << 100 << 2;
       
  2155     QTest::newRow("row at 180") << 5 << 40 << IntList() << 180 << 4;
       
  2156     QTest::newRow("row at 20")  << 5 << 40 << IntList() <<  20 << 0;
       
  2157 
       
  2158     // ### expand the dataset to include hidden rows
       
  2159 }
       
  2160 
       
  2161 void tst_QTableView::rowAt()
       
  2162 {
       
  2163     QFETCH(int, rowCount);
       
  2164     QFETCH(int, rowHeight);
       
  2165     QFETCH(IntList, hiddenRows);
       
  2166     QFETCH(int, coordinate);
       
  2167     QFETCH(int, row);
       
  2168 
       
  2169     QtTestTableModel model(rowCount, 1);
       
  2170     QtTestTableView view;
       
  2171     view.resize(100, 2 * rowHeight);
       
  2172 
       
  2173     view.setModel(&model);
       
  2174 
       
  2175     for (int r = 0; r < rowCount; ++r)
       
  2176         view.setRowHeight(r, rowHeight);
       
  2177 
       
  2178     for (int i = 0; i < hiddenRows.count(); ++i)
       
  2179         view.hideRow(hiddenRows.at(i));
       
  2180 
       
  2181     QCOMPARE(view.rowAt(coordinate), row);
       
  2182 }
       
  2183 
       
  2184 void tst_QTableView::rowHeight_data()
       
  2185 {
       
  2186     QTest::addColumn<int>("rowCount");
       
  2187     QTest::addColumn<IntList>("rowHeights");
       
  2188     QTest::addColumn<BoolList>("hiddenRows");
       
  2189 
       
  2190     QTest::newRow("increasing")
       
  2191       << 5
       
  2192       << (IntList() << 20 << 30 << 40 << 50 << 60)
       
  2193       << (BoolList() << false << false << false << false << false);
       
  2194 
       
  2195     QTest::newRow("decreasing")
       
  2196       << 5
       
  2197       << (IntList() << 60 << 50 << 40 << 30 << 20)
       
  2198       << (BoolList() << false << false << false << false << false);
       
  2199 
       
  2200     QTest::newRow("random")
       
  2201       << 5
       
  2202       << (IntList() << 87 << 34 << 68 << 91 << 27)
       
  2203       << (BoolList() << false << false << false << false << false);
       
  2204 
       
  2205     // ### expand the dataset to include hidden rows
       
  2206 }
       
  2207 
       
  2208 void tst_QTableView::rowHeight()
       
  2209 {
       
  2210     QFETCH(int, rowCount);
       
  2211     QFETCH(IntList, rowHeights);
       
  2212     QFETCH(BoolList, hiddenRows);
       
  2213 
       
  2214     QtTestTableModel model(rowCount, 1);
       
  2215     QtTestTableView view;
       
  2216 
       
  2217     view.setModel(&model);
       
  2218 
       
  2219     for (int r = 0; r < rowCount; ++r) {
       
  2220         view.setRowHeight(r, rowHeights.at(r));
       
  2221         view.setRowHidden(r, hiddenRows.at(r));
       
  2222     }
       
  2223 
       
  2224     for (int r = 0; r < rowCount; ++r) {
       
  2225         if (hiddenRows.at(r))
       
  2226             QCOMPARE(view.rowHeight(r), 0);
       
  2227         else
       
  2228             QCOMPARE(view.rowHeight(r), rowHeights.at(r));
       
  2229     }
       
  2230 }
       
  2231 
       
  2232 void tst_QTableView::columnViewportPosition_data()
       
  2233 {
       
  2234     QTest::addColumn<int>("columnCount");
       
  2235     QTest::addColumn<int>("columnWidth");
       
  2236     QTest::addColumn<int>("column");
       
  2237     QTest::addColumn<int>("horizontalScrollMode");
       
  2238     QTest::addColumn<int>("horizontalScrollValue");
       
  2239     QTest::addColumn<int>("columnViewportPosition");
       
  2240 
       
  2241     QTest::newRow("column 0, scroll per item 0")
       
  2242         << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerItem) << 0 << 0;
       
  2243 
       
  2244     QTest::newRow("column 1, scroll per item, 0")
       
  2245         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 0 << 1 * 40;
       
  2246 
       
  2247     QTest::newRow("column 1, scroll per item, 1")
       
  2248         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerItem) << 1 << 0;
       
  2249 
       
  2250     QTest::newRow("column 5, scroll per item, 0")
       
  2251         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 0 << 5 * 40;
       
  2252 
       
  2253     QTest::newRow("column 5, scroll per item, 5")
       
  2254         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerItem) << 5 << 0;
       
  2255 
       
  2256     QTest::newRow("column 9, scroll per item, 0")
       
  2257         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 0 << 9 * 40;
       
  2258 
       
  2259     QTest::newRow("column 9, scroll per item, 5")
       
  2260         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerItem) << 5 << 4 * 40;
       
  2261 
       
  2262     QTest::newRow("column 0, scroll per pixel 0")
       
  2263         << 10 << 40 << 0 << int(QAbstractItemView::ScrollPerPixel) << 0 << 0;
       
  2264 
       
  2265     QTest::newRow("column 1, scroll per pixel 0")
       
  2266         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 0 << 1 * 40;
       
  2267 
       
  2268     QTest::newRow("column 1, scroll per pixel 1")
       
  2269         << 10 << 40 << 1 << int(QAbstractItemView::ScrollPerPixel) << 1 * 40 << 0;
       
  2270 
       
  2271     QTest::newRow("column 5, scroll per pixel 0")
       
  2272         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 0 << 5 * 40;
       
  2273 
       
  2274     QTest::newRow("column 5, scroll per pixel 5")
       
  2275         << 10 << 40 << 5 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 0;
       
  2276 
       
  2277     QTest::newRow("column 9, scroll per pixel 0")
       
  2278         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 0 << 9 * 40;
       
  2279 
       
  2280     QTest::newRow("column 9, scroll per pixel 5")
       
  2281         << 10 << 40 << 9 << int(QAbstractItemView::ScrollPerPixel) << 5 * 40 << 4 * 40;
       
  2282 }
       
  2283 
       
  2284 void tst_QTableView::columnViewportPosition()
       
  2285 {
       
  2286     QFETCH(int, columnCount);
       
  2287     QFETCH(int, columnWidth);
       
  2288     QFETCH(int, column);
       
  2289     QFETCH(int, horizontalScrollMode);
       
  2290     QFETCH(int, horizontalScrollValue);
       
  2291     QFETCH(int, columnViewportPosition);
       
  2292 
       
  2293     QtTestTableModel model(1, columnCount);
       
  2294     QtTestTableView view;
       
  2295     view.resize(2 * columnWidth, 100);
       
  2296     view.show();
       
  2297 
       
  2298     view.setModel(&model);
       
  2299     for (int c = 0; c < columnCount; ++c)
       
  2300         view.setColumnWidth(c, columnWidth);
       
  2301 
       
  2302     view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)horizontalScrollMode);
       
  2303     view.horizontalScrollBar()->setValue(horizontalScrollValue);
       
  2304 
       
  2305     QCOMPARE(view.columnViewportPosition(column), columnViewportPosition);
       
  2306 }
       
  2307 
       
  2308 void tst_QTableView::columnAt_data()
       
  2309 {
       
  2310     QTest::addColumn<int>("columnCount");
       
  2311     QTest::addColumn<int>("columnWidth");
       
  2312     QTest::addColumn<IntList>("hiddenColumns");
       
  2313     QTest::addColumn<int>("coordinate");
       
  2314     QTest::addColumn<int>("column");
       
  2315 
       
  2316     QTest::newRow("column at 100") << 5 << 40 << IntList() << 100 << 2;
       
  2317     QTest::newRow("column at 180") << 5 << 40 << IntList() << 180 << 4;
       
  2318     QTest::newRow("column at 20")  << 5 << 40 << IntList() <<  20 << 0;
       
  2319 
       
  2320     // ### expand the dataset to include hidden coumns
       
  2321 }
       
  2322 
       
  2323 void tst_QTableView::columnAt()
       
  2324 {
       
  2325     QFETCH(int, columnCount);
       
  2326     QFETCH(int, columnWidth);
       
  2327     QFETCH(IntList, hiddenColumns);
       
  2328     QFETCH(int, coordinate);
       
  2329     QFETCH(int, column);
       
  2330 
       
  2331     QtTestTableModel model(1, columnCount);
       
  2332     QtTestTableView view;
       
  2333     view.resize(2 * columnWidth, 100);
       
  2334 
       
  2335     view.setModel(&model);
       
  2336 
       
  2337     for (int c = 0; c < columnCount; ++c)
       
  2338         view.setColumnWidth(c, columnWidth);
       
  2339 
       
  2340     for (int i = 0; i < hiddenColumns.count(); ++i)
       
  2341         view.hideColumn(hiddenColumns.at(i));
       
  2342 
       
  2343     QCOMPARE(view.columnAt(coordinate), column);
       
  2344 }
       
  2345 
       
  2346 void tst_QTableView::columnWidth_data()
       
  2347 {
       
  2348     QTest::addColumn<int>("columnCount");
       
  2349     QTest::addColumn<IntList>("columnWidths");
       
  2350     QTest::addColumn<BoolList>("hiddenColumns");
       
  2351 
       
  2352     QTest::newRow("increasing")
       
  2353       << 5
       
  2354       << (IntList() << 20 << 30 << 40 << 50 << 60)
       
  2355       << (BoolList() << false << false << false << false << false);
       
  2356 
       
  2357     QTest::newRow("decreasing")
       
  2358       << 5
       
  2359       << (IntList() << 60 << 50 << 40 << 30 << 20)
       
  2360       << (BoolList() << false << false << false << false << false);
       
  2361 
       
  2362     QTest::newRow("random")
       
  2363       << 5
       
  2364       << (IntList() << 87 << 34 << 68 << 91 << 27)
       
  2365       << (BoolList() << false << false << false << false << false);
       
  2366 
       
  2367     // ### expand the dataset to include hidden columns
       
  2368 }
       
  2369 
       
  2370 void tst_QTableView::columnWidth()
       
  2371 {
       
  2372     QFETCH(int, columnCount);
       
  2373     QFETCH(IntList, columnWidths);
       
  2374     QFETCH(BoolList, hiddenColumns);
       
  2375 
       
  2376     QtTestTableModel model(1, columnCount);
       
  2377     QtTestTableView view;
       
  2378 
       
  2379     view.setModel(&model);
       
  2380 
       
  2381     for (int c = 0; c < columnCount; ++c) {
       
  2382         view.setColumnWidth(c, columnWidths.at(c));
       
  2383         view.setColumnHidden(c, hiddenColumns.at(c));
       
  2384     }
       
  2385 
       
  2386     for (int c = 0; c < columnCount; ++c) {
       
  2387         if (hiddenColumns.at(c))
       
  2388             QCOMPARE(view.columnWidth(c), 0);
       
  2389         else
       
  2390             QCOMPARE(view.columnWidth(c), columnWidths.at(c));
       
  2391     }
       
  2392 }
       
  2393 
       
  2394 void tst_QTableView::hiddenRow_data()
       
  2395 {
       
  2396     QTest::addColumn<int>("rowCount");
       
  2397     QTest::addColumn<BoolList>("hiddenRows");
       
  2398 
       
  2399     QTest::newRow("first hidden")
       
  2400       << 5 << (BoolList() << true << false << false << false << false);
       
  2401 
       
  2402     QTest::newRow("last hidden")
       
  2403       << 5 << (BoolList() << false << false << false << false << true);
       
  2404 
       
  2405     QTest::newRow("none hidden")
       
  2406       << 5 << (BoolList() << false << false << false << false << false);
       
  2407 
       
  2408     QTest::newRow("all hidden")
       
  2409       << 5 << (BoolList() << true << true << true << true << true);
       
  2410  }
       
  2411 
       
  2412 void tst_QTableView::hiddenRow()
       
  2413 {
       
  2414     QFETCH(int, rowCount);
       
  2415     QFETCH(BoolList, hiddenRows);
       
  2416 
       
  2417 
       
  2418     QtTestTableModel model(rowCount, 1);
       
  2419     QtTestTableView view;
       
  2420 
       
  2421     view.setModel(&model);
       
  2422 
       
  2423     for (int r = 0; r < rowCount; ++r)
       
  2424         QVERIFY(!view.isRowHidden(r));
       
  2425 
       
  2426     for (int r = 0; r < rowCount; ++r)
       
  2427         view.setRowHidden(r, hiddenRows.at(r));
       
  2428 
       
  2429     for (int r = 0; r < rowCount; ++r)
       
  2430         QCOMPARE(view.isRowHidden(r), hiddenRows.at(r));
       
  2431 
       
  2432     for (int r = 0; r < rowCount; ++r)
       
  2433         view.setRowHidden(r, false);
       
  2434 
       
  2435     for (int r = 0; r < rowCount; ++r)
       
  2436         QVERIFY(!view.isRowHidden(r));
       
  2437 }
       
  2438 
       
  2439 void tst_QTableView::hiddenColumn_data()
       
  2440 {
       
  2441     QTest::addColumn<int>("columnCount");
       
  2442     QTest::addColumn<BoolList>("hiddenColumns");
       
  2443 
       
  2444     QTest::newRow("first hidden")
       
  2445       << 5 << (BoolList() << true << false << false << false << false);
       
  2446 
       
  2447     QTest::newRow("last hidden")
       
  2448       << 5 << (BoolList() << false << false << false << false << true);
       
  2449 
       
  2450     QTest::newRow("none hidden")
       
  2451       << 5 << (BoolList() << false << false << false << false << false);
       
  2452 
       
  2453     QTest::newRow("all hidden")
       
  2454       << 5 << (BoolList() << true << true << true << true << true);
       
  2455 }
       
  2456 
       
  2457 void tst_QTableView::hiddenColumn()
       
  2458 {
       
  2459     QFETCH(int, columnCount);
       
  2460     QFETCH(BoolList, hiddenColumns);
       
  2461 
       
  2462     QtTestTableModel model(1, columnCount);
       
  2463     QtTestTableView view;
       
  2464 
       
  2465     view.setModel(&model);
       
  2466 
       
  2467     for (int c = 0; c < columnCount; ++c)
       
  2468         QVERIFY(!view.isColumnHidden(c));
       
  2469 
       
  2470     for (int c = 0; c < columnCount; ++c)
       
  2471         view.setColumnHidden(c, hiddenColumns.at(c));
       
  2472 
       
  2473     for (int c = 0; c < columnCount; ++c)
       
  2474         QCOMPARE(view.isColumnHidden(c), hiddenColumns.at(c));
       
  2475 
       
  2476     for (int c = 0; c < columnCount; ++c)
       
  2477         view.setColumnHidden(c, false);
       
  2478 
       
  2479     for (int c = 0; c < columnCount; ++c)
       
  2480         QVERIFY(!view.isColumnHidden(c));
       
  2481 }
       
  2482 
       
  2483 void tst_QTableView::sortingEnabled_data()
       
  2484 {
       
  2485 //    QTest::addColumn<int>("columnCount");
       
  2486 }
       
  2487 
       
  2488 void tst_QTableView::sortingEnabled()
       
  2489 {
       
  2490 //    QFETCH(int, columnCount);
       
  2491 }
       
  2492 
       
  2493 void tst_QTableView::scrollTo_data()
       
  2494 {
       
  2495     QTest::addColumn<int>("verticalScrollMode");
       
  2496     QTest::addColumn<int>("horizontalScrollMode");
       
  2497     QTest::addColumn<int>("rowCount");
       
  2498     QTest::addColumn<int>("columnCount");
       
  2499     QTest::addColumn<int>("rowHeight");
       
  2500     QTest::addColumn<int>("columnWidth");
       
  2501     QTest::addColumn<int>("hiddenRow");
       
  2502     QTest::addColumn<int>("hiddenColumn");
       
  2503     QTest::addColumn<int>("row");
       
  2504     QTest::addColumn<int>("column");
       
  2505     QTest::addColumn<int>("rowSpan");
       
  2506     QTest::addColumn<int>("columnSpan");
       
  2507     QTest::addColumn<int>("horizontalScroll");
       
  2508     QTest::addColumn<int>("verticalScroll");
       
  2509     QTest::addColumn<int>("scrollHint");
       
  2510     QTest::addColumn<int>("expectedHorizontalScroll");
       
  2511     QTest::addColumn<int>("expectedVerticalScroll");
       
  2512 
       
  2513     QTest::newRow("no hidden, no span, no scroll, per item")
       
  2514         << (int)QAbstractItemView::ScrollPerItem
       
  2515         << (int)QAbstractItemView::ScrollPerItem
       
  2516         << 10 << 10  // table
       
  2517         << 80 << 80  // size
       
  2518         << -1 << -1  // hide
       
  2519         << 0 << 0    // cell
       
  2520         << 1 << 1    // span
       
  2521         << 0 << 0    // scroll
       
  2522         << (int)QAbstractItemView::PositionAtTop
       
  2523         << 0 << 0;   // expected
       
  2524 
       
  2525     QTest::newRow("no hidden, no span, no scroll, per pixel")
       
  2526         << (int)QAbstractItemView::ScrollPerPixel
       
  2527         << (int)QAbstractItemView::ScrollPerPixel
       
  2528         << 10 << 10  // table
       
  2529         << 80 << 80  // size
       
  2530         << -1 << -1  // hide
       
  2531         << 0 << 0    // cell
       
  2532         << 1 << 1    // span
       
  2533         << 0 << 0    // scroll
       
  2534         << (int)QAbstractItemView::PositionAtTop
       
  2535         << 0 << 0;   // expected
       
  2536 
       
  2537     QTest::newRow("hidden, no span, no scroll, per item")
       
  2538         << (int)QAbstractItemView::ScrollPerItem
       
  2539         << (int)QAbstractItemView::ScrollPerItem
       
  2540         << 10 << 10  // table
       
  2541         << 80 << 80  // size
       
  2542         << 3 << 3    // hide
       
  2543         << 5 << 5    // cell
       
  2544         << 1 << 1    // span
       
  2545         << 0 << 0    // scroll
       
  2546         << (int)QAbstractItemView::PositionAtTop
       
  2547         << 4 << 4;   // expected
       
  2548 }
       
  2549 
       
  2550 void tst_QTableView::scrollTo()
       
  2551 {
       
  2552     QFETCH(int, horizontalScrollMode);
       
  2553     QFETCH(int, verticalScrollMode);
       
  2554     QFETCH(int, rowCount);
       
  2555     QFETCH(int, columnCount);
       
  2556     QFETCH(int, rowHeight);
       
  2557     QFETCH(int, columnWidth);
       
  2558     QFETCH(int, hiddenRow);
       
  2559     QFETCH(int, hiddenColumn);
       
  2560     QFETCH(int, row);
       
  2561     QFETCH(int, column);
       
  2562     QFETCH(int, rowSpan);
       
  2563     QFETCH(int, columnSpan);
       
  2564     QFETCH(int, horizontalScroll);
       
  2565     QFETCH(int, verticalScroll);
       
  2566     QFETCH(int, scrollHint);
       
  2567     QFETCH(int, expectedHorizontalScroll);
       
  2568     QFETCH(int, expectedVerticalScroll);
       
  2569 
       
  2570     QtTestTableModel model(rowCount, columnCount);
       
  2571     QtTestTableView view;
       
  2572 
       
  2573     view.show();
       
  2574     // resizing to this size will ensure that there can ONLY_BE_ONE_CELL inside the view.
       
  2575     QSize forcedSize(columnWidth * 2, rowHeight * 2);
       
  2576     view.resize(forcedSize);
       
  2577     QTest::qWaitForWindowShown(&view);
       
  2578     QTest::qWait(50);
       
  2579     QTRY_COMPARE(view.size(), forcedSize);
       
  2580 
       
  2581     view.setModel(&model);
       
  2582     view.setSpan(row, column, rowSpan, columnSpan);
       
  2583     view.hideRow(hiddenRow);
       
  2584     view.hideColumn(hiddenColumn);
       
  2585     view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)horizontalScrollMode);
       
  2586     view.setVerticalScrollMode((QAbstractItemView::ScrollMode)verticalScrollMode);
       
  2587 
       
  2588     for (int r = 0; r < rowCount; ++r)
       
  2589         view.setRowHeight(r, rowHeight);
       
  2590     for (int c = 0; c < columnCount; ++c)
       
  2591         view.setColumnWidth(c, columnWidth);
       
  2592 
       
  2593     QTest::qWait(150); // ### needed to pass the test
       
  2594     view.horizontalScrollBar()->setValue(horizontalScroll);
       
  2595     view.verticalScrollBar()->setValue(verticalScroll);
       
  2596 
       
  2597     QModelIndex index = model.index(row, column);
       
  2598     QVERIFY(index.isValid());
       
  2599     view.scrollTo(index, (QAbstractItemView::ScrollHint)scrollHint);
       
  2600     QCOMPARE(view.verticalScrollBar()->value(), expectedVerticalScroll);
       
  2601     QCOMPARE(view.horizontalScrollBar()->value(), expectedHorizontalScroll);
       
  2602 }
       
  2603 
       
  2604 void tst_QTableView::indexAt_data()
       
  2605 {
       
  2606     QTest::addColumn<int>("rowCount");
       
  2607     QTest::addColumn<int>("columnCount");
       
  2608 
       
  2609     QTest::addColumn<int>("rowHeight");
       
  2610     QTest::addColumn<int>("columnWidth");
       
  2611 
       
  2612     QTest::addColumn<int>("hiddenRow");
       
  2613     QTest::addColumn<int>("hiddenColumn");
       
  2614 
       
  2615     QTest::addColumn<int>("row");
       
  2616     QTest::addColumn<int>("column");
       
  2617     QTest::addColumn<int>("rowSpan");
       
  2618     QTest::addColumn<int>("columnSpan");
       
  2619     QTest::addColumn<int>("horizontalScroll");
       
  2620     QTest::addColumn<int>("verticalScroll");
       
  2621     QTest::addColumn<int>("x");
       
  2622     QTest::addColumn<int>("y");
       
  2623     QTest::addColumn<int>("expectedRow");
       
  2624     QTest::addColumn<int>("expectedColumn");
       
  2625 
       
  2626     QTest::newRow("no hidden, no span, no scroll, (20,20)")
       
  2627       << 10 << 10  // dim
       
  2628       << 40 << 40  // size
       
  2629       << -1 << -1  // hide
       
  2630       << -1 << -1  // pos
       
  2631       << 1 << 1    // span
       
  2632       << 0 << 0    // scroll
       
  2633       << 20 << 20  // point
       
  2634       << 0 << 0;   // expected
       
  2635 
       
  2636     QTest::newRow("row hidden, no span, no scroll, at (20,20)")
       
  2637       << 10 << 10  // dim
       
  2638       << 40 << 40  // size
       
  2639       << 0 << -1   // hide
       
  2640       << -1 << -1  // pos
       
  2641       << 1 << 1    // span
       
  2642       << 0 << 0    // scroll
       
  2643       << 20 << 20  // point
       
  2644       << 1 << 0;   // expected
       
  2645 
       
  2646     QTest::newRow("col hidden, no span, no scroll, at (20,20)")
       
  2647       << 10 << 10  // dim
       
  2648       << 40 << 40  // size
       
  2649       << -1 << 0   // hide
       
  2650       << -1 << -1  // pos
       
  2651       << 1 << 1    // span
       
  2652       << 0 << 0    // scroll
       
  2653       << 20 << 20  // point
       
  2654       << 0 << 1;   // expected
       
  2655 
       
  2656     QTest::newRow("no hidden, row span, no scroll, at (60,20)")
       
  2657       << 10 << 10  // dim
       
  2658       << 40 << 40  // size
       
  2659       << -1 << -1  // hide
       
  2660       << 0 << 0    // pos
       
  2661       << 2 << 1    // span
       
  2662       << 0 << 0    // scroll
       
  2663       << 20 << 60  // point
       
  2664       << 0 << 0;   // expected
       
  2665 
       
  2666 
       
  2667     QTest::newRow("no hidden, col span, no scroll, at (60,20)")
       
  2668       << 10 << 10  // dim
       
  2669       << 40 << 40  // size
       
  2670       << -1 << -1  // hide
       
  2671       << 0 << 0    // pos
       
  2672       << 1 << 2    // span
       
  2673       << 0 << 0    // scroll
       
  2674       << 60 << 20  // point
       
  2675       << 0 << 0;   // expected
       
  2676 
       
  2677     QTest::newRow("no hidden, no span, scroll (5,0), at (20,20)")
       
  2678       << 10 << 10  // dim
       
  2679       << 40 << 40  // size
       
  2680       << -1 << -1  // hide
       
  2681       << -1 << -1  // pos
       
  2682       << 1 << 1    // span
       
  2683       << 5 << 0    // scroll
       
  2684       << 20 << 20  // point
       
  2685       << 0 << 5;   // expected
       
  2686 
       
  2687     QTest::newRow("no hidden, no span, scroll (0,5), at (20,20)")
       
  2688       << 10 << 10  // dim
       
  2689       << 40 << 40  // size
       
  2690       << -1 << -1  // hide
       
  2691       << -1 << -1  // pos
       
  2692       << 1 << 1    // span
       
  2693       << 0 << 5    // scroll
       
  2694       << 20 << 20  // point
       
  2695       << 5 << 0;   // expected
       
  2696 
       
  2697     QTest::newRow("no hidden, no span, scroll (5,5), at (20,20)")
       
  2698       << 10 << 10  // dim
       
  2699       << 40 << 40  // size
       
  2700       << -1 << -1  // hide
       
  2701       << -1 << -1  // pos
       
  2702       << 1 << 1    // span
       
  2703       << 5 << 5    // scroll
       
  2704       << 20 << 20  // point
       
  2705       << 5 << 5;   // expected
       
  2706 }
       
  2707 
       
  2708 void tst_QTableView::indexAt()
       
  2709 {
       
  2710     QFETCH(int, rowCount);
       
  2711     QFETCH(int, columnCount);
       
  2712     QFETCH(int, rowHeight);
       
  2713     QFETCH(int, columnWidth);
       
  2714     QFETCH(int, hiddenRow);
       
  2715     QFETCH(int, hiddenColumn);
       
  2716     QFETCH(int, row);
       
  2717     QFETCH(int, column);
       
  2718     QFETCH(int, rowSpan);
       
  2719     QFETCH(int, columnSpan);
       
  2720     QFETCH(int, horizontalScroll);
       
  2721     QFETCH(int, verticalScroll);
       
  2722     QFETCH(int, x);
       
  2723     QFETCH(int, y);
       
  2724     QFETCH(int, expectedRow);
       
  2725     QFETCH(int, expectedColumn);
       
  2726 
       
  2727     QtTestTableModel model(rowCount, columnCount);
       
  2728     QtTestTableView view;
       
  2729 
       
  2730     view.show();
       
  2731     QTest::qWaitForWindowShown(&view);
       
  2732 
       
  2733     //some styles change the scroll mode in their polish
       
  2734     view.setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
       
  2735     view.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
       
  2736 
       
  2737     view.setModel(&model);
       
  2738     view.setSpan(row, column, rowSpan, columnSpan);
       
  2739     view.hideRow(hiddenRow);
       
  2740     view.hideColumn(hiddenColumn);
       
  2741 
       
  2742     for (int r = 0; r < rowCount; ++r)
       
  2743         view.setRowHeight(r, rowHeight);
       
  2744     for (int c = 0; c < columnCount; ++c)
       
  2745         view.setColumnWidth(c, columnWidth);
       
  2746 
       
  2747     QTest::qWait(20);
       
  2748     view.horizontalScrollBar()->setValue(horizontalScroll);
       
  2749     view.verticalScrollBar()->setValue(verticalScroll);
       
  2750     QTest::qWait(20);
       
  2751 
       
  2752     QModelIndex index = view.indexAt(QPoint(x, y));
       
  2753     QCOMPARE(index.row(), expectedRow);
       
  2754     QCOMPARE(index.column(), expectedColumn);
       
  2755 }
       
  2756 
       
  2757 void tst_QTableView::span_data()
       
  2758 {
       
  2759     QTest::addColumn<int>("rowCount");
       
  2760     QTest::addColumn<int>("columnCount");
       
  2761     QTest::addColumn<int>("hiddenRow");
       
  2762     QTest::addColumn<int>("hiddenColumn");
       
  2763     QTest::addColumn<int>("row");
       
  2764     QTest::addColumn<int>("column");
       
  2765     QTest::addColumn<int>("rowSpan");
       
  2766     QTest::addColumn<int>("columnSpan");
       
  2767     QTest::addColumn<int>("expectedRowSpan");
       
  2768     QTest::addColumn<int>("expectedColumnSpan");
       
  2769     QTest::addColumn<bool>("clear");
       
  2770 
       
  2771     QTest::newRow("top left 2x2")
       
  2772       << 10 << 10
       
  2773       << -1 << -1
       
  2774       << 0 << 0
       
  2775       << 2 << 2
       
  2776       << 2 << 2
       
  2777       << false;
       
  2778 
       
  2779     QTest::newRow("top left 1x2")
       
  2780       << 10 << 10
       
  2781       << 3 << 3
       
  2782       << 0 << 0
       
  2783       << 1 << 2
       
  2784       << 1 << 2
       
  2785       << false;
       
  2786 
       
  2787     QTest::newRow("top left 2x1")
       
  2788       << 10 << 10
       
  2789       << -1 << -1
       
  2790       << 0 << 0
       
  2791       << 2 << 1
       
  2792       << 2 << 1
       
  2793       << false;
       
  2794 
       
  2795   /* This makes no sens.
       
  2796     QTest::newRow("top left 2x0")
       
  2797       << 10 << 10
       
  2798       << -1 << -1
       
  2799       << 0 << 0
       
  2800       << 2 << 0
       
  2801       << 2 << 0
       
  2802       << false;
       
  2803 
       
  2804     QTest::newRow("top left 0x2")
       
  2805       << 10 << 10
       
  2806       << -1 << -1
       
  2807       << 0 << 0
       
  2808       << 0 << 2
       
  2809       << 0 << 2
       
  2810       << false;*/
       
  2811 
       
  2812     QTest::newRow("invalid 2x2")
       
  2813       << 10 << 10
       
  2814       << -1 << -1
       
  2815       << -1 << -1
       
  2816       << 2 << 2
       
  2817       << 1 << 1
       
  2818       << false;
       
  2819 
       
  2820     QTest::newRow("top left 2x2")
       
  2821       << 10 << 10
       
  2822       << -1 << -1
       
  2823       << 0 << 0
       
  2824       << 2 << 2
       
  2825       << 2 << 2
       
  2826       << false;
       
  2827 
       
  2828     QTest::newRow("bottom right 2x2")
       
  2829       << 10 << 10
       
  2830       << -1 << -1
       
  2831       << 8 << 8
       
  2832       << 2 << 2
       
  2833       << 2 << 2
       
  2834       << false;
       
  2835 
       
  2836     QTest::newRow("invalid span 2x2")
       
  2837       << 10 << 10
       
  2838       << -1 << -1
       
  2839       << 8 << 8
       
  2840       << 2 << 2
       
  2841       << 2 << 2
       
  2842       << false;
       
  2843 
       
  2844     QTest::newRow("invalid span 3x3")
       
  2845       << 10 << 10
       
  2846       << -1 << -1
       
  2847       << 6 << 6
       
  2848       << 3 << 3
       
  2849       << 2 << 3
       
  2850       << true;
       
  2851 
       
  2852 }
       
  2853 
       
  2854 void tst_QTableView::span()
       
  2855 {
       
  2856     QFETCH(int, rowCount);
       
  2857     QFETCH(int, columnCount);
       
  2858     QFETCH(int, hiddenRow);
       
  2859     QFETCH(int, hiddenColumn);
       
  2860     QFETCH(int, row);
       
  2861     QFETCH(int, column);
       
  2862     QFETCH(int, rowSpan);
       
  2863     QFETCH(int, columnSpan);
       
  2864     QFETCH(int, expectedRowSpan);
       
  2865     QFETCH(int, expectedColumnSpan);
       
  2866     QFETCH(bool, clear);
       
  2867 
       
  2868     QtTestTableModel model(rowCount, columnCount);
       
  2869     QtTestTableView view;
       
  2870 
       
  2871     view.setModel(&model);
       
  2872     view.show();
       
  2873 
       
  2874     view.setSpan(row, column, rowSpan, columnSpan);
       
  2875     if (clear) {
       
  2876         model.removeLastRow();
       
  2877         model.removeLastRow();
       
  2878         view.update();
       
  2879     }
       
  2880 
       
  2881     view.hideRow(hiddenRow);
       
  2882     view.hideColumn(hiddenColumn);
       
  2883     view.show();
       
  2884 
       
  2885     QCOMPARE(view.rowSpan(row, column), expectedRowSpan);
       
  2886     QCOMPARE(view.columnSpan(row, column), expectedColumnSpan);
       
  2887 
       
  2888     if (hiddenRow > -1) {
       
  2889         QModelIndex hidden = model.index(hiddenRow, columnCount - 1);
       
  2890 	QVERIFY(view.isIndexHidden(hidden));
       
  2891     }
       
  2892 
       
  2893     if (hiddenColumn > -1) {
       
  2894         QModelIndex hidden = model.index(rowCount - 1, hiddenColumn);
       
  2895         QVERIFY(view.isIndexHidden(hidden));
       
  2896     }
       
  2897 
       
  2898     view.clearSpans();
       
  2899     QCOMPARE(view.rowSpan(row, column), 1);
       
  2900     QCOMPARE(view.columnSpan(row, column), 1);
       
  2901 }
       
  2902 
       
  2903 typedef QVector<QRect> SpanList;
       
  2904 Q_DECLARE_METATYPE(SpanList)
       
  2905 
       
  2906 void tst_QTableView::spans_data()
       
  2907 {
       
  2908     QTest::addColumn<int>("rows");
       
  2909     QTest::addColumn<int>("columns");
       
  2910     QTest::addColumn<SpanList>("spans");
       
  2911     QTest::addColumn<bool>("hideRowLastRowOfFirstSpan");
       
  2912     QTest::addColumn<QPoint>("pos");
       
  2913     QTest::addColumn<int>("expectedRowSpan");
       
  2914     QTest::addColumn<int>("expectedColumnSpan");
       
  2915 
       
  2916     QTest::newRow("1x3 span, query 3,0")
       
  2917       << 5 << 5
       
  2918       << (SpanList() << QRect(3, 0, 1, 3))
       
  2919       << false //no hidden row
       
  2920       << QPoint(3, 0)
       
  2921       << 1
       
  2922       << 3;
       
  2923 
       
  2924     QTest::newRow("1x3 span, query 3,1")
       
  2925       << 5 << 5
       
  2926       << (SpanList() << QRect(3, 0, 1, 3))
       
  2927       << false //no hidden row
       
  2928       << QPoint(3, 1)
       
  2929       << 1
       
  2930       << 3;
       
  2931 
       
  2932     QTest::newRow("1x3 span, query 3,2")
       
  2933       << 5 << 5
       
  2934       << (SpanList() << QRect(3, 0, 1, 3))
       
  2935       << false //no hidden row
       
  2936       << QPoint(3, 2)
       
  2937       << 1
       
  2938       << 3;
       
  2939 
       
  2940     QTest::newRow("two 1x2 spans at the same column, query at 3,0")
       
  2941       << 5 << 5
       
  2942       << (SpanList() << QRect(3, 0, 1, 2) << QRect(4, 0, 1, 2))
       
  2943       << false //no hidden row
       
  2944       << QPoint(3, 0)
       
  2945       << 1
       
  2946       << 2;
       
  2947 
       
  2948     QTest::newRow("two 1x2 spans at the same column, query at 4,0")
       
  2949       << 5 << 5
       
  2950       << (SpanList() << QRect(3, 0, 1, 2) << QRect(4, 0, 1, 2))
       
  2951       << false //no hidden row
       
  2952       << QPoint(4, 0)
       
  2953       << 1
       
  2954       << 2;
       
  2955 
       
  2956     QTest::newRow("how to order spans (1,1)")
       
  2957       << 5 << 5
       
  2958       << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
       
  2959       << false //no hidden row
       
  2960       << QPoint(1, 1)
       
  2961       << 3
       
  2962       << 1;
       
  2963 
       
  2964     QTest::newRow("how to order spans (2,1)")
       
  2965       << 5 << 5
       
  2966       << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
       
  2967       << false //no hidden row
       
  2968       << QPoint(2, 1)
       
  2969       << 3
       
  2970       << 1;
       
  2971 
       
  2972     QTest::newRow("how to order spans (3,1)")
       
  2973       << 5 << 5
       
  2974       << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
       
  2975       << false //no hidden row
       
  2976       << QPoint(3, 1)
       
  2977       << 3
       
  2978       << 1;
       
  2979 
       
  2980     QTest::newRow("how to order spans (1,2)")
       
  2981       << 5 << 5
       
  2982       << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
       
  2983       << false //no hidden row
       
  2984       << QPoint(1, 2)
       
  2985       << 2
       
  2986       << 1;
       
  2987 
       
  2988     QTest::newRow("how to order spans (2,2)")
       
  2989       << 5 << 5
       
  2990       << (SpanList() << QRect(1, 1, 3, 1) << QRect(1, 2, 2, 1))
       
  2991       << false //no hidden row
       
  2992       << QPoint(2, 2)
       
  2993       << 2
       
  2994       << 1;
       
  2995 
       
  2996     QTest::newRow("spans with hidden rows")
       
  2997       << 3 << 2
       
  2998       << (SpanList() << QRect(0, 0, 2, 2) << QRect(2, 0, 1, 2))
       
  2999       << true //we hide the last row of the first span
       
  3000       << QPoint(2, 0)
       
  3001       << 1
       
  3002       << 2;
       
  3003 }
       
  3004 
       
  3005 void tst_QTableView::spans()
       
  3006 {
       
  3007     QFETCH(int, rows);
       
  3008     QFETCH(int, columns);
       
  3009     QFETCH(SpanList, spans);
       
  3010     QFETCH(bool, hideRowLastRowOfFirstSpan);
       
  3011     QFETCH(QPoint, pos);
       
  3012     QFETCH(int, expectedRowSpan);
       
  3013     QFETCH(int, expectedColumnSpan);
       
  3014 
       
  3015     QtTestTableModel model(rows, columns);
       
  3016     QtTestTableView view;
       
  3017 
       
  3018     view.setModel(&model);
       
  3019     view.show();
       
  3020 
       
  3021     for (int i = 0; i < spans.count(); ++i) {
       
  3022         QRect sp = spans.at(i);
       
  3023         view.setSpan(sp.x(), sp.y(), sp.width(), sp.height());
       
  3024     }
       
  3025 
       
  3026     if (hideRowLastRowOfFirstSpan) {
       
  3027         view.setRowHidden(spans.at(0).bottom(), true);
       
  3028         //we check that the span didn't break the visual rects of the model indexes
       
  3029         QRect first = view.visualRect( model.index(spans.at(0).top(), 0));
       
  3030         QRect next = view.visualRect( model.index(spans.at(0).bottom() + 1, 0));
       
  3031         QVERIFY(first.intersected(next).isEmpty());
       
  3032     }
       
  3033 
       
  3034     QCOMPARE(view.columnSpan(pos.x(), pos.y()), expectedColumnSpan);
       
  3035     QCOMPARE(view.rowSpan(pos.x(), pos.y()), expectedRowSpan);
       
  3036 }
       
  3037 
       
  3038 void tst_QTableView::spansAfterRowInsertion()
       
  3039 {
       
  3040     QtTestTableModel model(10, 10);
       
  3041     QtTestTableView view;
       
  3042     view.setModel(&model);
       
  3043     view.setSpan(3, 3, 3, 3);
       
  3044     view.show();
       
  3045     QTest::qWait(50);
       
  3046 
       
  3047     // Insertion before the span only shifts the span.
       
  3048     view.model()->insertRows(0, 2);
       
  3049     QCOMPARE(view.rowSpan(3, 3), 1);
       
  3050     QCOMPARE(view.columnSpan(3, 3), 1);
       
  3051     QCOMPARE(view.rowSpan(5, 3), 3);
       
  3052     QCOMPARE(view.columnSpan(5, 3), 3);
       
  3053 
       
  3054     // Insertion happens before the given row, so it only shifts the span also.
       
  3055     view.model()->insertRows(5, 2);
       
  3056     QCOMPARE(view.rowSpan(5, 3), 1);
       
  3057     QCOMPARE(view.columnSpan(5, 3), 1);
       
  3058     QCOMPARE(view.rowSpan(7, 3), 3);
       
  3059     QCOMPARE(view.columnSpan(7, 3), 3);
       
  3060 
       
  3061     // Insertion inside the span expands it.
       
  3062     view.model()->insertRows(8, 2);
       
  3063     QCOMPARE(view.rowSpan(7, 3), 5);
       
  3064     QCOMPARE(view.columnSpan(7, 3), 3);
       
  3065 
       
  3066     // Insertion after the span does nothing to it.
       
  3067     view.model()->insertRows(12, 2);
       
  3068     QCOMPARE(view.rowSpan(7, 3), 5);
       
  3069     QCOMPARE(view.columnSpan(7, 3), 3);
       
  3070 }
       
  3071 
       
  3072 void tst_QTableView::spansAfterColumnInsertion()
       
  3073 {
       
  3074     QtTestTableModel model(10, 10);
       
  3075     QtTestTableView view;
       
  3076     view.setModel(&model);
       
  3077     view.setSpan(3, 3, 3, 3);
       
  3078     view.show();
       
  3079     QTest::qWait(50);
       
  3080 
       
  3081     // Insertion before the span only shifts the span.
       
  3082     view.model()->insertColumns(0, 2);
       
  3083     QCOMPARE(view.rowSpan(3, 3), 1);
       
  3084     QCOMPARE(view.columnSpan(3, 3), 1);
       
  3085     QCOMPARE(view.rowSpan(3, 5), 3);
       
  3086     QCOMPARE(view.columnSpan(3, 5), 3);
       
  3087 
       
  3088     // Insertion happens before the given column, so it only shifts the span also.
       
  3089     view.model()->insertColumns(5, 2);
       
  3090     QCOMPARE(view.rowSpan(3, 5), 1);
       
  3091     QCOMPARE(view.columnSpan(3, 5), 1);
       
  3092     QCOMPARE(view.rowSpan(3, 7), 3);
       
  3093     QCOMPARE(view.columnSpan(3, 7), 3);
       
  3094 
       
  3095     // Insertion inside the span expands it.
       
  3096     view.model()->insertColumns(8, 2);
       
  3097     QCOMPARE(view.rowSpan(3, 7), 3);
       
  3098     QCOMPARE(view.columnSpan(3, 7), 5);
       
  3099 
       
  3100     // Insertion after the span does nothing to it.
       
  3101     view.model()->insertColumns(12, 2);
       
  3102     QCOMPARE(view.rowSpan(3, 7), 3);
       
  3103     QCOMPARE(view.columnSpan(3, 7), 5);
       
  3104 }
       
  3105 
       
  3106 void tst_QTableView::spansAfterRowRemoval()
       
  3107 {
       
  3108     QtTestTableModel model(10, 10);
       
  3109     QtTestTableView view;
       
  3110     view.setModel(&model);
       
  3111 
       
  3112     QList<QRect> spans;
       
  3113     spans << QRect(0, 1, 1, 2)
       
  3114           << QRect(1, 2, 1, 2)
       
  3115           << QRect(2, 2, 1, 5)
       
  3116           << QRect(2, 8, 1, 2)
       
  3117           << QRect(3, 4, 1, 2)
       
  3118           << QRect(4, 4, 1, 4)
       
  3119           << QRect(5, 6, 1, 3)
       
  3120           << QRect(6, 7, 1, 3);
       
  3121     foreach (QRect span, spans)
       
  3122         view.setSpan(span.top(), span.left(), span.height(), span.width());
       
  3123 
       
  3124     view.show();
       
  3125     QTest::qWait(100);
       
  3126     view.model()->removeRows(3, 3);
       
  3127 
       
  3128     QList<QRect> expectedSpans;
       
  3129     expectedSpans << QRect(0, 1, 1, 2)
       
  3130           << QRect(1, 2, 1, 1)
       
  3131           << QRect(2, 2, 1, 2)
       
  3132           << QRect(2, 5, 1, 2)
       
  3133           << QRect(3, 4, 1, 1)
       
  3134           << QRect(4, 3, 1, 2)
       
  3135           << QRect(5, 3, 1, 3)
       
  3136           << QRect(6, 4, 1, 3);
       
  3137     foreach (QRect span, expectedSpans) {
       
  3138         QCOMPARE(view.columnSpan(span.top(), span.left()), span.width());
       
  3139         QCOMPARE(view.rowSpan(span.top(), span.left()), span.height());
       
  3140     }
       
  3141 }
       
  3142 
       
  3143 void tst_QTableView::spansAfterColumnRemoval()
       
  3144 {
       
  3145     QtTestTableModel model(10, 10);
       
  3146     QtTestTableView view;
       
  3147     view.setModel(&model);
       
  3148 
       
  3149     // Same set as above just swapping columns and rows.
       
  3150     QList<QRect> spans;
       
  3151     spans << QRect(0, 1, 1, 2)
       
  3152           << QRect(1, 2, 1, 2)
       
  3153           << QRect(2, 2, 1, 5)
       
  3154           << QRect(2, 8, 1, 2)
       
  3155           << QRect(3, 4, 1, 2)
       
  3156           << QRect(4, 4, 1, 4)
       
  3157           << QRect(5, 6, 1, 3)
       
  3158           << QRect(6, 7, 1, 3);
       
  3159     foreach (QRect span, spans)
       
  3160         view.setSpan(span.left(), span.top(), span.width(), span.height());
       
  3161 
       
  3162     view.show();
       
  3163     QTest::qWait(100);
       
  3164     view.model()->removeColumns(3, 3);
       
  3165 
       
  3166     QList<QRect> expectedSpans;
       
  3167     expectedSpans << QRect(0, 1, 1, 2)
       
  3168           << QRect(1, 2, 1, 1)
       
  3169           << QRect(2, 2, 1, 2)
       
  3170           << QRect(2, 5, 1, 2)
       
  3171           << QRect(3, 4, 1, 1)
       
  3172           << QRect(4, 3, 1, 2)
       
  3173           << QRect(5, 3, 1, 3)
       
  3174           << QRect(6, 4, 1, 3);
       
  3175     foreach (QRect span, expectedSpans) {
       
  3176         QCOMPARE(view.columnSpan(span.left(), span.top()), span.height());
       
  3177         QCOMPARE(view.rowSpan(span.left(), span.top()), span.width());
       
  3178     }
       
  3179 }
       
  3180 
       
  3181 class Model : public QAbstractTableModel {
       
  3182 
       
  3183 Q_OBJECT
       
  3184 
       
  3185 public:
       
  3186     Model(QObject * parent = 0) : QAbstractTableModel(parent) {
       
  3187     }
       
  3188 
       
  3189     int rowCount(const QModelIndex &) const {
       
  3190         return rows;
       
  3191     }
       
  3192     int columnCount(const QModelIndex &) const {
       
  3193         return columns;
       
  3194     }
       
  3195     QVariant data(const QModelIndex &, int) const
       
  3196     {
       
  3197         return QVariant();
       
  3198     }
       
  3199     void res() { reset(); }
       
  3200 
       
  3201     int rows;
       
  3202     int columns;
       
  3203 };
       
  3204 
       
  3205 void tst_QTableView::checkHeaderReset()
       
  3206 {
       
  3207     QTableView view;
       
  3208     Model m;
       
  3209     m.rows = 3;
       
  3210     m.columns = 3;
       
  3211     view.setModel(&m);
       
  3212 
       
  3213     m.rows = 4;
       
  3214     m.columns = 4;
       
  3215     m.res();
       
  3216     QCOMPARE(view.horizontalHeader()->count(), 4);
       
  3217 }
       
  3218 
       
  3219 void tst_QTableView::checkHeaderMinSize()
       
  3220 {
       
  3221     //tests if the minimumsize is of a header is taken into account
       
  3222     //while computing QTableView geometry. For that we test the position of the
       
  3223     //viewport.
       
  3224     QTableView view;
       
  3225     QStringListModel m;
       
  3226     m.setStringList( QStringList() << QLatin1String("one cell is enough"));
       
  3227     view.setModel(&m);
       
  3228 
       
  3229     //setting the minimum height on the horizontal header
       
  3230     //and the minimum width on the vertical header
       
  3231     view.horizontalHeader()->setMinimumHeight(50);
       
  3232     view.verticalHeader()->setMinimumWidth(100);
       
  3233 
       
  3234     view.show();
       
  3235 
       
  3236     QVERIFY( view.verticalHeader()->y() >= view.horizontalHeader()->minimumHeight());
       
  3237     QVERIFY( view.horizontalHeader()->x() >= view.verticalHeader()->minimumWidth());
       
  3238 }
       
  3239 
       
  3240 void tst_QTableView::resizeToContents()
       
  3241 {
       
  3242     //checks that the resize to contents is consistent
       
  3243     QTableWidget table(2,3);
       
  3244     QTableWidget table2(2,3);
       
  3245     QTableWidget table3(2,3);
       
  3246 
       
  3247 
       
  3248     table.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
       
  3249     table2.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
       
  3250     table3.setHorizontalHeaderItem(0, new QTableWidgetItem("A Lot of text here: BLA BLA BLA"));
       
  3251     table.horizontalHeader()->setVisible(false);
       
  3252     table2.horizontalHeader()->setVisible(false);
       
  3253     table.verticalHeader()->setVisible(false);
       
  3254     table2.verticalHeader()->setVisible(false);
       
  3255 
       
  3256 
       
  3257     for(int i = 0;i<table.columnCount();i++) {
       
  3258         table.resizeColumnToContents(i);
       
  3259     }
       
  3260     for(int i = 0;i<table.rowCount();i++) {
       
  3261         table.resizeRowToContents(i);
       
  3262     }
       
  3263     table2.resizeColumnsToContents();
       
  3264     table2.resizeRowsToContents();
       
  3265     table3.resizeColumnsToContents();
       
  3266     table3.resizeRowsToContents();
       
  3267 
       
  3268     //now let's check the row/col sizes
       
  3269     for(int i = 0;i<table.columnCount();i++) {
       
  3270         QVERIFY( table.columnWidth(i) == table2.columnWidth(i));
       
  3271         QVERIFY( table2.columnWidth(i) == table3.columnWidth(i));
       
  3272     }
       
  3273     for(int i = 0;i<table.rowCount();i++) {
       
  3274         QVERIFY( table.rowHeight(i) == table2.rowHeight(i));
       
  3275         QVERIFY( table2.rowHeight(i) == table3.rowHeight(i));
       
  3276     }
       
  3277 
       
  3278 }
       
  3279 
       
  3280 QT_BEGIN_NAMESPACE
       
  3281 extern bool Q_GUI_EXPORT qt_tab_all_widgets; // qapplication.cpp
       
  3282 QT_END_NAMESPACE
       
  3283 
       
  3284 void tst_QTableView::tabFocus()
       
  3285 {
       
  3286     if (!qt_tab_all_widgets)
       
  3287         QSKIP("This test requires full keyboard control to be enabled.", SkipAll);
       
  3288 
       
  3289     // QTableView enables tabKeyNavigation by default, but you should be able
       
  3290     // to change focus on an empty table view, or on a table view that doesn't
       
  3291     // have this property set.
       
  3292     QWidget window;
       
  3293 
       
  3294     QTableView *view = new QTableView(&window);
       
  3295     QLineEdit *edit = new QLineEdit(&window);
       
  3296 
       
  3297     window.show();
       
  3298     QApplication::setActiveWindow(&window);
       
  3299     QTest::qWaitForWindowShown(&window);
       
  3300     window.setFocus();
       
  3301     QTest::qWait(100);
       
  3302     window.activateWindow();
       
  3303     QTest::qWait(100);
       
  3304 
       
  3305     qApp->processEvents();
       
  3306 
       
  3307     WAIT_FOR_CONDITION(window.hasFocus(), true);
       
  3308 
       
  3309     qApp->processEvents();
       
  3310 
       
  3311     // window
       
  3312     QVERIFY(window.hasFocus());
       
  3313     QVERIFY(!view->hasFocus());
       
  3314     QVERIFY(!edit->hasFocus());
       
  3315 
       
  3316     for (int i = 0; i < 2; ++i) {
       
  3317         // tab to view
       
  3318         QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
       
  3319         QTRY_VERIFY(!window.hasFocus());
       
  3320         QVERIFY(view->hasFocus());
       
  3321         QVERIFY(!edit->hasFocus());
       
  3322 
       
  3323         // tab to edit
       
  3324         QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
       
  3325         QTRY_VERIFY(edit->hasFocus());
       
  3326         QVERIFY(!window.hasFocus());
       
  3327         QVERIFY(!view->hasFocus());
       
  3328     }
       
  3329 
       
  3330     // backtab to view
       
  3331     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3332     QTRY_VERIFY(view->hasFocus());
       
  3333     QVERIFY(!window.hasFocus());
       
  3334     QVERIFY(!edit->hasFocus());
       
  3335 
       
  3336     // backtab to edit
       
  3337     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3338     QTRY_VERIFY(edit->hasFocus());
       
  3339     QVERIFY(!window.hasFocus());
       
  3340     QVERIFY(!view->hasFocus());
       
  3341 
       
  3342     QStandardItemModel *model = new QStandardItemModel;
       
  3343     view->setModel(model);
       
  3344 
       
  3345     // backtab to view
       
  3346     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3347     QTRY_VERIFY(view->hasFocus());
       
  3348     QVERIFY(!window.hasFocus());
       
  3349     QVERIFY(!edit->hasFocus());
       
  3350 
       
  3351     // backtab to edit
       
  3352     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3353     QTRY_VERIFY(edit->hasFocus());
       
  3354     QVERIFY(!window.hasFocus());
       
  3355     QVERIFY(!view->hasFocus());
       
  3356 
       
  3357     model->insertRow(0, new QStandardItem("Hei"));
       
  3358     model->insertRow(0, new QStandardItem("Hei"));
       
  3359     model->insertRow(0, new QStandardItem("Hei"));
       
  3360 
       
  3361     // backtab to view
       
  3362     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3363     QTRY_VERIFY(view->hasFocus());
       
  3364     QVERIFY(!window.hasFocus());
       
  3365     QVERIFY(!edit->hasFocus());
       
  3366 
       
  3367     // backtab to edit doesn't work
       
  3368     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3369     QVERIFY(!window.hasFocus());
       
  3370     QVERIFY(view->hasFocus());
       
  3371     QVERIFY(!edit->hasFocus());
       
  3372 
       
  3373     view->setTabKeyNavigation(false);
       
  3374 
       
  3375     // backtab to edit
       
  3376     QTest::keyPress(qApp->focusWidget(), Qt::Key_Backtab);
       
  3377     QTRY_VERIFY(edit->hasFocus());
       
  3378     QVERIFY(!window.hasFocus());
       
  3379     QVERIFY(!view->hasFocus());
       
  3380 
       
  3381     QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
       
  3382     QTRY_VERIFY(view->hasFocus());
       
  3383     QTest::keyPress(qApp->focusWidget(), Qt::Key_Tab);
       
  3384     QTRY_VERIFY(edit->hasFocus());
       
  3385 
       
  3386     delete model;
       
  3387 }
       
  3388 
       
  3389 class BigModel : public QAbstractTableModel
       
  3390 {
       
  3391     Q_OBJECT
       
  3392 public:
       
  3393     virtual QVariant data(const QModelIndex &index,
       
  3394                           int role = Qt::DisplayRole) const
       
  3395     {
       
  3396         if (role == Qt::DisplayRole)
       
  3397             return QString("%1 - %2").arg(index.column()).arg(index.row());
       
  3398         return QVariant();
       
  3399     }
       
  3400 
       
  3401 
       
  3402     int rowCount(const QModelIndex & parent = QModelIndex()) const
       
  3403     {
       
  3404         Q_UNUSED(parent);
       
  3405         return 10000000;
       
  3406     }
       
  3407 
       
  3408     int columnCount(const QModelIndex & parent = QModelIndex()) const
       
  3409     {
       
  3410         Q_UNUSED(parent);
       
  3411         return 20000000;
       
  3412     }
       
  3413 };
       
  3414 
       
  3415 void tst_QTableView::bigModel()
       
  3416 {
       
  3417     //should not crash
       
  3418     QTableView view;
       
  3419     BigModel model;
       
  3420     view.setModel(&model);
       
  3421     view.show();
       
  3422     view.setSpan(10002,10002,6,6);
       
  3423     QTest::qWait(100);
       
  3424     view.resize(1000,1000);
       
  3425     QTest::qWait(100);
       
  3426     view.scrollTo(model.index(10010,10010));
       
  3427     QTest::qWait(100);
       
  3428 }
       
  3429 
       
  3430 void tst_QTableView::selectionSignal()
       
  3431 {
       
  3432     QtTestTableModel model(10, 10);
       
  3433     QtTestTableView view;
       
  3434     view.checkSignalOrder = true;
       
  3435     view.setModel(&model);
       
  3436     view.resize(200, 200);
       
  3437     view.show();
       
  3438     QTest::qWaitForWindowShown(&view);
       
  3439     QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, view.visualRect(model.index(2, 0)).center());
       
  3440 }
       
  3441 
       
  3442 class task173773_EventFilter : public QObject
       
  3443 {
       
  3444     int paintEventCount_;
       
  3445 public:
       
  3446     task173773_EventFilter() : paintEventCount_(0) {}
       
  3447     int paintEventCount() const { return paintEventCount_; }
       
  3448 private:
       
  3449     bool eventFilter(QObject *obj, QEvent *e)
       
  3450     {
       
  3451         Q_UNUSED(obj);
       
  3452         if (e->type() == QEvent::Paint)
       
  3453             ++paintEventCount_;
       
  3454         return false;
       
  3455     }
       
  3456 };
       
  3457 
       
  3458 void tst_QTableView::task173773_updateVerticalHeader()
       
  3459 {
       
  3460     QStandardItemModel model(2, 1);
       
  3461     model.setData(model.index(0, 0), 0);
       
  3462     model.setData(model.index(1, 0), 1);
       
  3463 
       
  3464     QSortFilterProxyModel proxyModel;
       
  3465     proxyModel.setSourceModel(&model);
       
  3466 
       
  3467     QTableView view;
       
  3468     view.setModel(&proxyModel);
       
  3469     view.setSortingEnabled(true);
       
  3470     view.show();
       
  3471     QTest::qWaitForWindowShown(&view);
       
  3472 
       
  3473     view.sortByColumn(0, Qt::AscendingOrder);
       
  3474     QTest::qWait(100);
       
  3475 
       
  3476     task173773_EventFilter eventFilter;
       
  3477     view.verticalHeader()->viewport()->installEventFilter(&eventFilter);
       
  3478 
       
  3479     view.sortByColumn(0, Qt::DescendingOrder);
       
  3480     QTest::qWait(100);
       
  3481 
       
  3482     // ### note: this test may occasionally pass even if the bug is present!
       
  3483     QVERIFY(eventFilter.paintEventCount() > 0);
       
  3484 }
       
  3485 
       
  3486 void tst_QTableView::task227953_setRootIndex()
       
  3487 {
       
  3488 	QTableView tableView;
       
  3489 
       
  3490     //model = tree with two items with tables as children
       
  3491     QStandardItemModel model;
       
  3492     QStandardItem item1, item2;
       
  3493     model.appendColumn(QList<QStandardItem*>() << &item1 << &item2);
       
  3494 
       
  3495 
       
  3496     //setup the first table as a child of the first item
       
  3497     for ( int row = 0; row < 40; ++row ) {
       
  3498         item1.appendRow(QList<QStandardItem*>() << new QStandardItem(QString("row %0").arg(row)));
       
  3499     }
       
  3500 
       
  3501     //setup the second table as a child of the second item
       
  3502     for ( int row = 0; row < 10; ++row ) {
       
  3503         item2.appendRow(QList<QStandardItem*>() << new QStandardItem(QString("row %0").arg(row)));
       
  3504     }
       
  3505 
       
  3506     tableView.setModel(&model);
       
  3507 
       
  3508     //show the first 10 rows of the first table
       
  3509     QModelIndex root = model.indexFromItem(&item1);
       
  3510 	tableView.setRootIndex(root);
       
  3511 	for (int i = 10; i != 40; ++i) {
       
  3512 		tableView.setRowHidden(i, true);
       
  3513 	}
       
  3514 
       
  3515     QCOMPARE(tableView.verticalHeader()->count(), 40);
       
  3516     QCOMPARE(tableView.verticalHeader()->hiddenSectionCount(), 30);
       
  3517 
       
  3518 	//show the first 10 rows of the second table
       
  3519 	tableView.setRootIndex(model.indexFromItem(&item2));
       
  3520 
       
  3521     QCOMPARE(tableView.verticalHeader()->count(), 10);
       
  3522     QCOMPARE(tableView.verticalHeader()->hiddenSectionCount(), 0);
       
  3523     QVERIFY(!tableView.verticalHeader()->isHidden());
       
  3524 }
       
  3525 
       
  3526 void tst_QTableView::task240266_veryBigColumn()
       
  3527 {
       
  3528     QTableView table;
       
  3529     table.setFixedSize(500, 300); //just to make sure we have the 2 first columns visible
       
  3530     QStandardItemModel model(1, 3);
       
  3531     table.setModel(&model);
       
  3532     table.setColumnWidth(0, 100); //normal column
       
  3533     table.setColumnWidth(1, 100); //normal column
       
  3534     table.setColumnWidth(2, 9000); //very big column
       
  3535     table.show();
       
  3536     QTest::qWaitForWindowShown(&table);
       
  3537 
       
  3538     //some styles change the scroll mode in their polish
       
  3539     table.setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
       
  3540     table.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
       
  3541 
       
  3542     QScrollBar *scroll = table.horizontalScrollBar();
       
  3543     QCOMPARE(scroll->minimum(), 0);
       
  3544     QCOMPARE(scroll->maximum(), model.columnCount() - 1);
       
  3545     QCOMPARE(scroll->singleStep(), 1);
       
  3546 
       
  3547     //1 is not always a very correct value for pageStep. Ideally this should be dynamic.
       
  3548     //Maybe something for Qt 5 ;-)
       
  3549     QCOMPARE(scroll->pageStep(), 1);
       
  3550 
       
  3551 }
       
  3552 
       
  3553 void tst_QTableView::task248688_autoScrollNavigation()
       
  3554 {
       
  3555     //we make sure that when navigating with the keyboard the view is correctly scrolled
       
  3556     //to the current item
       
  3557     QStandardItemModel model(16, 16);
       
  3558     QTableView view;
       
  3559     view.setModel(&model);
       
  3560 
       
  3561 	view.hideColumn(8);
       
  3562 	view.hideRow(8);
       
  3563     view.show();
       
  3564     for (int r = 0; r < model.rowCount(); ++r) {
       
  3565         if (view.isRowHidden(r))
       
  3566             continue;
       
  3567         for (int c = 0; c < model.columnCount(); ++c) {
       
  3568             if (view.isColumnHidden(c))
       
  3569                 continue;
       
  3570             QModelIndex index = model.index(r, c);
       
  3571             view.setCurrentIndex(index);
       
  3572             QVERIFY(view.viewport()->rect().contains(view.visualRect(index)));
       
  3573         }
       
  3574     }
       
  3575 }
       
  3576 
       
  3577 
       
  3578 void tst_QTableView::mouseWheel_data()
       
  3579 {
       
  3580     QTest::addColumn<int>("scrollMode");
       
  3581     QTest::addColumn<int>("delta");
       
  3582     QTest::addColumn<int>("horizontalPositon");
       
  3583     QTest::addColumn<int>("verticalPosition");
       
  3584 
       
  3585     QTest::newRow("scroll up per item")
       
  3586             << int(QAbstractItemView::ScrollPerItem) << 120
       
  3587             << 10 - qApp->wheelScrollLines() << 10 - qApp->wheelScrollLines();
       
  3588     QTest::newRow("scroll down per item")
       
  3589             << int(QAbstractItemView::ScrollPerItem) << -120
       
  3590             << 10 + qApp->wheelScrollLines() << 10 + qApp->wheelScrollLines();
       
  3591 #ifdef Q_WS_MAC
       
  3592     // On Mac, we always scroll one pixel per 120 delta (rather than multiplying with
       
  3593     // singleStep) since wheel events are accelerated by the OS.
       
  3594     QTest::newRow("scroll down per pixel")
       
  3595             << int(QAbstractItemView::ScrollPerPixel) << -120
       
  3596             << 10 + qApp->wheelScrollLines() << 10 + qApp->wheelScrollLines();
       
  3597 #else
       
  3598     QTest::newRow("scroll down per pixel")
       
  3599             << int(QAbstractItemView::ScrollPerPixel) << -120
       
  3600             << 10 + qApp->wheelScrollLines() * 89 << 10 + qApp->wheelScrollLines() * 28;
       
  3601 #endif
       
  3602 }
       
  3603 
       
  3604 void tst_QTableView::mouseWheel()
       
  3605 {
       
  3606 #ifdef Q_OS_WINCE
       
  3607     QSKIP("Since different Windows CE versions sport different taskbars, we skip this test", SkipAll);
       
  3608 #endif
       
  3609     QFETCH(int, scrollMode);
       
  3610     QFETCH(int, delta);
       
  3611     QFETCH(int, horizontalPositon);
       
  3612     QFETCH(int, verticalPosition);
       
  3613 
       
  3614     QtTestTableModel model(100, 100);
       
  3615     QtTestTableView view;
       
  3616     view.resize(500, 500);
       
  3617     for (int r = 0; r < 100; ++r)
       
  3618         view.setRowHeight(r, 50);
       
  3619     for (int c = 0; c < 100; ++c)
       
  3620         view.setColumnWidth(c, 100);
       
  3621     view.show();
       
  3622     QTest::qWaitForWindowShown(&view);
       
  3623 
       
  3624     view.setModel(&model);
       
  3625 
       
  3626     view.setHorizontalScrollMode((QAbstractItemView::ScrollMode)scrollMode);
       
  3627     view.setVerticalScrollMode((QAbstractItemView::ScrollMode)scrollMode);
       
  3628     view.horizontalScrollBar()->setValue(10);
       
  3629     view.verticalScrollBar()->setValue(10);
       
  3630 
       
  3631     QPoint pos = view.viewport()->geometry().center();
       
  3632     QWheelEvent verticalEvent(pos, delta, 0, 0, Qt::Vertical);
       
  3633     QWheelEvent horizontalEvent(pos, delta, 0, 0, Qt::Horizontal);
       
  3634     QApplication::sendEvent(view.viewport(), &horizontalEvent);
       
  3635     QVERIFY(qAbs(view.horizontalScrollBar()->value() - horizontalPositon) < 10);
       
  3636     QApplication::sendEvent(view.viewport(), &verticalEvent);
       
  3637     QVERIFY(qAbs(view.verticalScrollBar()->value() - verticalPosition) < 10);
       
  3638 }
       
  3639 
       
  3640 void tst_QTableView::addColumnWhileEditing()
       
  3641 {
       
  3642     QTableView view;
       
  3643     QStandardItemModel model(1, 10);
       
  3644     view.setModel(&model);
       
  3645     QModelIndex last = model.index(0,9);
       
  3646     view.show();
       
  3647 
       
  3648     view.openPersistentEditor(last);
       
  3649     view.scrollTo(last);
       
  3650 
       
  3651     //let's see if the editor is moved to the right location
       
  3652     //after adding a column
       
  3653     model.setColumnCount(model.columnCount() + 1);
       
  3654     QPointer<QLineEdit> editor = qFindChild<QLineEdit*>(&view);
       
  3655     QVERIFY(editor);
       
  3656     QCOMPARE(editor->geometry(), view.visualRect(last));
       
  3657 
       
  3658     //let's see if the editor is moved to the right location
       
  3659     //after removing a column
       
  3660     view.scrollTo(model.index(0, model.columnCount()-1));
       
  3661     model.setColumnCount(model.columnCount() - 1);
       
  3662     QVERIFY(editor);
       
  3663     QCOMPARE(editor->geometry(), view.visualRect(last));
       
  3664 }
       
  3665 
       
  3666 void tst_QTableView::task259308_scrollVerticalHeaderSwappedSections()
       
  3667 {
       
  3668     QStandardItemModel model;
       
  3669     model.setRowCount(50);
       
  3670     model.setColumnCount(2);
       
  3671     for (int row = 0; row < model.rowCount(); ++row)
       
  3672         for (int col = 0; col < model.columnCount(); ++col) {
       
  3673             const QModelIndex &idx = model.index(row, col);
       
  3674             model.setData(idx, QVariant(row), Qt::EditRole);
       
  3675         }
       
  3676 
       
  3677     QTableView tv;
       
  3678     tv.setModel(&model);
       
  3679     tv.show();
       
  3680     tv.verticalHeader()->swapSections(0, model.rowCount() - 1);
       
  3681     tv.setCurrentIndex(model.index(model.rowCount() - 1, 0));
       
  3682 
       
  3683     QTest::qWaitForWindowShown(&tv);
       
  3684     QTest::keyClick(&tv, Qt::Key_PageUp);   // PageUp won't scroll when at top
       
  3685     QTRY_COMPARE(tv.rowAt(0), tv.verticalHeader()->logicalIndex(0));
       
  3686 
       
  3687     int newRow = tv.rowAt(tv.viewport()->height());
       
  3688     if (newRow == tv.rowAt(tv.viewport()->height() - 1)) // Overlapping row
       
  3689         newRow++;
       
  3690     QTest::keyClick(&tv, Qt::Key_PageDown); // Scroll down and check current
       
  3691     QTRY_COMPARE(tv.currentIndex().row(), newRow);
       
  3692 
       
  3693     tv.setCurrentIndex(model.index(0, 0));
       
  3694     QTest::qWait(60);
       
  3695     QTest::keyClick(&tv, Qt::Key_PageDown); // PageDown won't scroll when at the bottom
       
  3696     QTRY_COMPARE(tv.rowAt(tv.viewport()->height() - 1), tv.verticalHeader()->logicalIndex(model.rowCount() - 1));
       
  3697 }
       
  3698 
       
  3699 template <typename T>
       
  3700 struct ValueSaver {
       
  3701     T &var, value;
       
  3702     ValueSaver(T &v) : var(v), value(v) { }
       
  3703     ~ValueSaver() { var = value; }
       
  3704 };
       
  3705 
       
  3706 void tst_QTableView::task191545_dragSelectRows()
       
  3707 {
       
  3708     QStandardItemModel model(10, 10);
       
  3709     QTableView table;
       
  3710     table.setModel(&model);
       
  3711     table.setSelectionBehavior(QAbstractItemView::SelectItems);
       
  3712     table.setSelectionMode(QAbstractItemView::ExtendedSelection);
       
  3713     table.setMinimumSize(1000, 400);
       
  3714     table.show();
       
  3715     QTest::qWait(200);
       
  3716 
       
  3717     ValueSaver<Qt::KeyboardModifiers> saver(QApplicationPrivate::modifier_buttons);
       
  3718     QApplicationPrivate::modifier_buttons = Qt::ControlModifier;
       
  3719 
       
  3720     {
       
  3721         QRect cellRect = table.visualRect(model.index(3, 0));
       
  3722         QHeaderView *vHeader = table.verticalHeader();
       
  3723         QWidget *vHeaderVp = vHeader->viewport();
       
  3724         QPoint rowPos(5, (cellRect.top() + cellRect.bottom()) / 2);
       
  3725         QMouseEvent rowPressEvent(QEvent::MouseButtonPress, rowPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3726         qApp->sendEvent(vHeaderVp, &rowPressEvent);
       
  3727 
       
  3728         for (int i = 0; i < 4; ++i) {
       
  3729             rowPos.setY(rowPos.y() + cellRect.height());
       
  3730             QMouseEvent moveEvent(QEvent::MouseMove, rowPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
       
  3731             qApp->sendEvent(vHeaderVp, &moveEvent);
       
  3732         }
       
  3733         QMouseEvent rowReleaseEvent(QEvent::MouseButtonRelease, rowPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3734         qApp->sendEvent(vHeaderVp, &rowReleaseEvent);
       
  3735 
       
  3736         for (int i = 0; i < 4; ++i) {
       
  3737             QModelIndex index = model.index(3 + i, 0, table.rootIndex());
       
  3738             QVERIFY(vHeader->selectionModel()->selectedRows().contains(index));
       
  3739         }
       
  3740     }
       
  3741 
       
  3742     {
       
  3743         QRect cellRect = table.visualRect(model.index(0, 3));
       
  3744         QHeaderView *hHeader = table.horizontalHeader();
       
  3745         QWidget *hHeaderVp = hHeader->viewport();
       
  3746         QPoint colPos((cellRect.left() + cellRect.right()) / 2, 5);
       
  3747         QMouseEvent colPressEvent(QEvent::MouseButtonPress, colPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3748         qApp->sendEvent(hHeaderVp, &colPressEvent);
       
  3749 
       
  3750         for (int i = 0; i < 4; ++i) {
       
  3751             colPos.setX(colPos.x() + cellRect.width());
       
  3752             QMouseEvent moveEvent(QEvent::MouseMove, colPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
       
  3753             qApp->sendEvent(hHeaderVp, &moveEvent);
       
  3754         }
       
  3755         QMouseEvent colReleaseEvent(QEvent::MouseButtonRelease, colPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3756         qApp->sendEvent(hHeaderVp, &colReleaseEvent);
       
  3757 
       
  3758         for (int i = 0; i < 4; ++i) {
       
  3759             QModelIndex index = model.index(0, 3 + i, table.rootIndex());
       
  3760             QVERIFY(hHeader->selectionModel()->selectedColumns().contains(index));
       
  3761         }
       
  3762     }
       
  3763 
       
  3764     {
       
  3765         QRect cellRect = table.visualRect(model.index(2, 2));
       
  3766         QWidget *tableVp = table.viewport();
       
  3767         QPoint cellPos = cellRect.center();
       
  3768         QMouseEvent cellPressEvent(QEvent::MouseButtonPress, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3769         qApp->sendEvent(tableVp, &cellPressEvent);
       
  3770 
       
  3771         for (int i = 0; i < 6; ++i) {
       
  3772             cellPos.setX(cellPos.x() + cellRect.width());
       
  3773             cellPos.setY(cellPos.y() + cellRect.height());
       
  3774             QMouseEvent moveEvent(QEvent::MouseMove, cellPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
       
  3775             qApp->sendEvent(tableVp, &moveEvent);
       
  3776         }
       
  3777         QMouseEvent cellReleaseEvent(QEvent::MouseButtonRelease, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3778         qApp->sendEvent(tableVp, &cellReleaseEvent);
       
  3779 
       
  3780         for (int i = 0; i < 6; ++i)
       
  3781             for (int j = 0; j < 6; ++j) {
       
  3782                 QModelIndex index = model.index(2 + i, 2 + j, table.rootIndex());
       
  3783                 QVERIFY(table.selectionModel()->isSelected(index));
       
  3784             }
       
  3785     }
       
  3786 
       
  3787     {
       
  3788         QRect cellRect = table.visualRect(model.index(3, 3));
       
  3789         QWidget *tableVp = table.viewport();
       
  3790         QPoint cellPos = cellRect.center();
       
  3791         QMouseEvent cellPressEvent(QEvent::MouseButtonPress, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3792         qApp->sendEvent(tableVp, &cellPressEvent);
       
  3793 
       
  3794         for (int i = 0; i < 6; ++i) {
       
  3795             cellPos.setX(cellPos.x() + cellRect.width());
       
  3796             cellPos.setY(cellPos.y() + cellRect.height());
       
  3797             QMouseEvent moveEvent(QEvent::MouseMove, cellPos, Qt::NoButton, Qt::LeftButton, Qt::ControlModifier);
       
  3798             qApp->sendEvent(tableVp, &moveEvent);
       
  3799         }
       
  3800         QMouseEvent cellReleaseEvent(QEvent::MouseButtonRelease, cellPos, Qt::LeftButton, Qt::NoButton, Qt::ControlModifier);
       
  3801         qApp->sendEvent(tableVp, &cellReleaseEvent);
       
  3802 
       
  3803         for (int i = 0; i < 6; ++i)
       
  3804             for (int j = 0; j < 6; ++j) {
       
  3805                 QModelIndex index = model.index(3 + i, 3 + j, table.rootIndex());
       
  3806                 QVERIFY(!table.selectionModel()->isSelected(index));
       
  3807             }
       
  3808     }
       
  3809 }
       
  3810 
       
  3811 void tst_QTableView::task234926_setHeaderSorting()
       
  3812 {
       
  3813     QStringListModel model;
       
  3814     QStringList data;
       
  3815     data << "orange" << "apple" << "banana" << "lemon" << "pumpkin";
       
  3816     QStringList sortedDataA = data;
       
  3817     QStringList sortedDataD = data;
       
  3818     qSort(sortedDataA);
       
  3819     qSort(sortedDataD.begin(), sortedDataD.end(), qGreater<QString>());
       
  3820     model.setStringList(data);
       
  3821     QTableView view;
       
  3822     view.setModel(&model);
       
  3823 //    view.show();
       
  3824     QTest::qWait(20);
       
  3825     QCOMPARE(model.stringList(), data);
       
  3826     view.setSortingEnabled(true);
       
  3827     view.sortByColumn(0, Qt::AscendingOrder);
       
  3828     QApplication::processEvents();
       
  3829     QCOMPARE(model.stringList() , sortedDataA);
       
  3830 
       
  3831     view.horizontalHeader()->setSortIndicator(0, Qt::DescendingOrder);
       
  3832     QApplication::processEvents();
       
  3833     QCOMPARE(model.stringList() , sortedDataD);
       
  3834 
       
  3835     QHeaderView *h = new QHeaderView(Qt::Horizontal);
       
  3836     h->setModel(&model);
       
  3837     view.setHorizontalHeader(h);
       
  3838     h->setSortIndicator(0, Qt::AscendingOrder);
       
  3839     QApplication::processEvents();
       
  3840     QCOMPARE(model.stringList() , sortedDataA);
       
  3841 
       
  3842     h->setSortIndicator(0, Qt::DescendingOrder);
       
  3843     QApplication::processEvents();
       
  3844     QCOMPARE(model.stringList() , sortedDataD);
       
  3845 }
       
  3846 
       
  3847 QTEST_MAIN(tst_QTableView)
       
  3848 #include "tst_qtableview.moc"