diff options
Diffstat (limited to 'tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp')
-rw-r--r-- | tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp | 159 |
1 files changed, 140 insertions, 19 deletions
diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index 55fcf04846..dac75dd9fa 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -47,6 +47,7 @@ #include <qitemdelegate.h> #include <qtreewidget.h> #include <qdebug.h> +#include <qscreen.h> typedef QList<int> IntList; @@ -106,15 +107,12 @@ class tst_QHeaderView : public QObject public: tst_QHeaderView(); - virtual ~tst_QHeaderView(); -public slots: +private slots: void initTestCase(); void cleanupTestCase(); void init(); void cleanup(); - -private slots: void getSetCheck(); void visualIndex(); @@ -238,6 +236,7 @@ private slots: void resizeToContentTest(); void testStreamWithHide(); void testStylePosition(); + void stretchAndRestoreLastSection(); void sizeHintCrash(); @@ -272,7 +271,8 @@ public: wrongIndex = true; qWarning("Invalid modelIndex [%d,%d,%p]", idx.row(), idx.column(), idx.internalPointer()); } - return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0);//idx.data()); + return QLatin1Char('[') + QString::number(idx.row()) + QLatin1Char(',') + + QString::number(idx.column()) + QLatin1String(",0]"); } void insertOneColumn(int col) @@ -382,10 +382,6 @@ tst_QHeaderView::tst_QHeaderView() qRegisterMetaType<int>("Qt::SortOrder"); } -tst_QHeaderView::~tst_QHeaderView() -{ -} - void tst_QHeaderView::initTestCase() { #ifdef Q_OS_WINCE //disable magic for WindowsCE @@ -560,7 +556,7 @@ void tst_QHeaderView::hidden() void tst_QHeaderView::stretch() { - // Show before resize and setStrechLastSection + // Show before resize and setStretchLastSection #if defined(Q_OS_WINCE) QSize viewSize(200,300); #else @@ -1569,7 +1565,7 @@ public: return QVariant(); } if (role == Qt::DisplayRole) { - return QString::fromLatin1("%1,%2").arg(index.row()).arg(index.column()); + return QString::number(index.row()) + QLatin1Char(',') + QString::number(index.column()); } return QVariant(); } @@ -2242,13 +2238,21 @@ void tst_QHeaderView::QTBUG8650_crashOnInsertSections() model.insertColumn(0, items); } +static void setModelTexts(QStandardItemModel *model) +{ + const int columnCount = model->columnCount(); + for (int i = 0, rowCount = model->rowCount(); i < rowCount; ++i) { + const QString prefix = QLatin1String("item [") + QString::number(i) + QLatin1Char(','); + for (int j = 0; j < columnCount; ++j) + model->setData(model->index(i, j), prefix + QString::number(j) + QLatin1Char(']')); + } +} + void tst_QHeaderView::QTBUG12268_hiddenMovedSectionSorting() { QTableView view; // ### this test fails on QTableView &view = *m_tableview; !? + shadowing view member QStandardItemModel *model = new QStandardItemModel(4,3, &view); - for (int i = 0; i< model->rowCount(); ++i) - for (int j = 0; j< model->columnCount(); ++j) - model->setData(model->index(i,j), QString("item [%1,%2]").arg(i).arg(j)); + setModelTexts(model); view.setModel(model); view.horizontalHeader()->setSectionsMovable(true); view.setSortingEnabled(true); @@ -2324,9 +2328,7 @@ void tst_QHeaderView::initialSortOrderRole() { QTableView view; // ### Shadowing member view (of type QHeaderView) QStandardItemModel *model = new QStandardItemModel(4, 3, &view); - for (int i = 0; i< model->rowCount(); ++i) - for (int j = 0; j< model->columnCount(); ++j) - model->setData(model->index(i,j), QString("item [%1,%2]").arg(i).arg(j)); + setModelTexts(model); QStandardItem *ascendingItem = new QStandardItem(); QStandardItem *descendingItem = new QStandardItem(); ascendingItem->setData(Qt::AscendingOrder, Qt::InitialSortOrderRole); @@ -2494,7 +2496,7 @@ void tst_QHeaderView::calculateAndCheck(int cppline, const int precalced_compare QString msg = "semantic problem at " + QString(__FILE__) + " (" + sline + ")"; msg += "\nThe *expected* result was : {" + istr(x[0]) + istr(x[1]) + istr(x[2]) + istr(x[3]) - + istr(x[4]) + istr(x[5]) + istr(x[6], false) + "}"; + + istr(x[4]) + istr(x[5]) + istr(x[6], false) + QLatin1Char('}'); msg += "\nThe calculated result was : {"; msg += istr(chk_visual) + istr(chk_logical) + istr(chk_sizes) + istr(chk_hidden_size) + istr(chk_lookup_visual) + istr(chk_lookup_logical) + istr(header_lenght, false) + "};"; @@ -2572,7 +2574,7 @@ void tst_QHeaderView::additionalInit() for (int i = 0; i < model->rowCount(); ++i) { model->setData(model->index(i, 0), QVariant(i)); s.setNum(i); - s += "."; + s += QLatin1Char('.'); s += 'a' + (i % 25); model->setData(model->index(i, 1), QVariant(s)); } @@ -2891,5 +2893,124 @@ void tst_QHeaderView::sizeHintCrash() treeView.header()->sizeHintForRow(0); } +void tst_QHeaderView::stretchAndRestoreLastSection() +{ + QStandardItemModel m(10, 10); + QTableView tv; + tv.setModel(&m); + tv.showMaximized(); + + const int defaultSectionSize = 30; + const int someOtherSectionSize = 40; + const int biggerSizeThanAnySection = 50; + + QVERIFY(QTest::qWaitForWindowExposed(&tv)); + + QHeaderView &header = *tv.horizontalHeader(); + header.setDefaultSectionSize(defaultSectionSize); + header.resizeSection(9, someOtherSectionSize); + header.setStretchLastSection(true); + + // Default last section is larger + QCOMPARE(header.sectionSize(8), defaultSectionSize); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + + // Moving last section away (restore old last section 9 - and make 8 larger) + header.swapSections(9, 8); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); + QVERIFY(header.sectionSize(8) >= biggerSizeThanAnySection); + + // Make section 9 the large one again + header.hideSection(8); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + + // Show section 8 again - and make that one the last one. + header.showSection(8); + QVERIFY(header.sectionSize(8) > biggerSizeThanAnySection); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); + + // Swap the sections so the logical indexes are equal to visible indexes again. + header.moveSection(9, 8); + QCOMPARE(header.sectionSize(8), defaultSectionSize); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + + // Append sections + m.setColumnCount(15); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); + QVERIFY(header.sectionSize(14) >= biggerSizeThanAnySection); + + // Truncate sections (remove sections with the last section) + m.setColumnCount(10); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + for (int u = 0; u < 9; ++u) + QCOMPARE(header.sectionSize(u), defaultSectionSize); + + // Insert sections + m.insertColumns(2, 2); + QCOMPARE(header.sectionSize(9), defaultSectionSize); + QCOMPARE(header.sectionSize(10), defaultSectionSize); + QVERIFY(header.sectionSize(11) >= biggerSizeThanAnySection); + + // Append an extra section and check restore + m.setColumnCount(m.columnCount() + 1); + QCOMPARE(header.sectionSize(11), someOtherSectionSize); + QVERIFY(header.sectionSize(12) >= biggerSizeThanAnySection); + + // Remove some sections but not the last one. + m.removeColumns(2, 2); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); + QVERIFY(header.sectionSize(10) >= biggerSizeThanAnySection); + for (int u = 0; u < 9; ++u) + QCOMPARE(header.sectionSize(u), defaultSectionSize); + + // Empty the header and start over with some more tests + m.setColumnCount(0); + m.setColumnCount(10); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + + // Check resize of the last section + header.resizeSection(9, someOtherSectionSize); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); // It should still be stretched + header.swapSections(9, 8); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); + + // Restore the order + header.swapSections(9, 8); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + + // Hide the last 3 sections and test stretch last section on swap/move + // when hidden sections with a larger visual index exists. + header.hideSection(7); + header.hideSection(8); + header.hideSection(9); + QVERIFY(header.sectionSize(6) >= biggerSizeThanAnySection); + header.moveSection(2, 7); + QVERIFY(header.sectionSize(2) >= biggerSizeThanAnySection); + header.swapSections(1, 8); + QCOMPARE(header.sectionSize(2), defaultSectionSize); + QVERIFY(header.sectionSize(1) >= biggerSizeThanAnySection); + + // Inserting sections 2 + m.setColumnCount(0); + m.setColumnCount(10); + header.resizeSection(1, someOtherSectionSize); + header.swapSections(1, 9); + m.insertColumns(9, 2); + header.swapSections(1, 11); + QCOMPARE(header.sectionSize(1), someOtherSectionSize); + + // Test import/export of the original (not stretched) sectionSize. + m.setColumnCount(0); + m.setColumnCount(10); + header.resizeSection(9, someOtherSectionSize); + QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); + QByteArray b = header.saveState(); + m.setColumnCount(0); + m.setColumnCount(10); + header.restoreState(b); + header.setStretchLastSection(false); + QCOMPARE(header.sectionSize(9), someOtherSectionSize); +} + QTEST_MAIN(tst_QHeaderView) #include "tst_qheaderview.moc" |