diff options
Diffstat (limited to 'tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp')
-rw-r--r-- | tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp | 239 |
1 files changed, 121 insertions, 118 deletions
diff --git a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp index 3cf9f7fe0b..98cc9b29ed 100644 --- a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp +++ b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp @@ -1,31 +1,26 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -49,6 +44,7 @@ #include <QtWidgets/QDialog> #include <QtWidgets/QStyledItemDelegate> #include <QtWidgets/QStyleFactory> +#include <QtWidgets/QVBoxLayout> #if defined(Q_OS_WIN) || defined(Q_OS_WINCE) # include <windows.h> @@ -74,21 +70,22 @@ static inline void setFrameless(QWidget *w) w->setWindowFlags(flags); } +static QStringList generateList(const QString &prefix, int size) +{ + QStringList result; + result.reserve(size); + for (int i = 0; i < size; ++i) + result.append(prefix + QString::number(i)); + return result; +} + class tst_QListView : public QObject { Q_OBJECT -public: - tst_QListView(); - virtual ~tst_QListView(); - - -public slots: +private slots: void initTestCase(); - void cleanupTestCase(); - void init(); void cleanup(); -private slots: void getSetCheck(); void noDelegate(); void noModel(); @@ -115,7 +112,7 @@ private slots: void scrollBarAsNeeded(); void moveItems(); void wordWrap(); -#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && WINVER >= 0x0500 +#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) void setCurrentIndexAfterAppendRowCrash(); #endif void emptyItemSize(); @@ -152,6 +149,7 @@ private slots: void taskQTBUG_39902_mutualScrollBars_data(); void taskQTBUG_39902_mutualScrollBars(); void horizontalScrollingByVerticalWheelEvents(); + void taskQTBUG_7232_AllowUserToControlSingleStep(); void taskQTBUG_51086_skippingIndexesInSelectedIndexes(); }; @@ -256,7 +254,7 @@ public: wrongIndex = true; qWarning("got invalid modelIndex %d/%d", idx.row(), idx.column()); } - return QString("%1/%2").arg(idx.row()).arg(idx.column()); + return QString::number(idx.row()) + QLatin1Char('/') + QString::number(idx.column()); } void removeLastRow() @@ -283,24 +281,20 @@ public: mutable bool wrongIndex; }; -tst_QListView::tst_QListView() +class ScrollPerItemListView : public QListView { -} - -tst_QListView::~tst_QListView() -{ -} +public: + explicit ScrollPerItemListView(QWidget *parent = Q_NULLPTR) + : QListView(parent) + { + // Force per item scroll mode since it comes from the style by default + setVerticalScrollMode(QAbstractItemView::ScrollPerItem); + setHorizontalScrollMode(QAbstractItemView::ScrollPerItem); + } +}; void tst_QListView::initTestCase() { -} - -void tst_QListView::cleanupTestCase() -{ -} - -void tst_QListView::init() -{ #ifdef Q_OS_WINCE //disable magic for WindowsCE qApp->setAutoMaximizeThreshold(-1); #endif @@ -311,7 +305,6 @@ void tst_QListView::cleanup() QVERIFY(QApplication::topLevelWidgets().isEmpty()); } - void tst_QListView::noDelegate() { QtTestModel model(0); @@ -365,10 +358,11 @@ void tst_QListView::cursorMove() view.setModel(&model); for (int j = 0; j < columns; ++j) { + const QString postfix = QLatin1Char(',') + QString::number(j) + QLatin1Char(']'); view.setModelColumn(j); for (int i = 0; i < rows; ++i) { QModelIndex index = model.index(i, j); - model.setData(index, QString("[%1,%2]").arg(i).arg(j)); + model.setData(index, QLatin1Char('[') + QString::number(i) + postfix); view.setCurrentIndex(index); QApplication::processEvents(); QCOMPARE(view.currentIndex(), index); @@ -469,7 +463,7 @@ void tst_QListView::hideRows() QStandardItemModel sim(0); QStandardItem *root = new QStandardItem("Root row"); for (int i=0;i<5;i++) - root->appendRow(new QStandardItem(QString("Row %1").arg(i))); + root->appendRow(new QStandardItem(QLatin1String("Row ") + QString::number(i))); sim.appendRow(root); view.setModel(&sim); view.setRootIndex(root->index()); @@ -703,9 +697,11 @@ void tst_QListView::singleSelectionRemoveColumn() int numCols = 3; int numRows = 3; QStandardItemModel model(numCols, numRows); - for (int r = 0; r < numRows; ++r) + for (int r = 0; r < numRows; ++r) { + const QString prefix = QString::number(r) + QLatin1Char(','); for (int c = 0; c < numCols; ++c) - model.setData(model.index(r, c), QString("%1,%2").arg(r).arg(c)); + model.setData(model.index(r, c), prefix + QString::number(c)); + } QListView view; view.setModel(&model); @@ -730,10 +726,11 @@ void tst_QListView::modelColumn() int numCols = 3; int numRows = 3; QStandardItemModel model(numCols, numRows); - for (int r = 0; r < numRows; ++r) + for (int r = 0; r < numRows; ++r) { + const QString prefix = QString::number(r) + QLatin1Char(','); for (int c = 0; c < numCols; ++c) - model.setData(model.index(r, c), QString("%1,%2").arg(r).arg(c)); - + model.setData(model.index(r, c), prefix + QString::number(c)); + } QListView view; view.setModel(&model); @@ -812,10 +809,7 @@ void tst_QListView::batchedMode() { const int rowCount = 3; - QStringList items; - for (int i = 0; i < rowCount; ++i) - items << QLatin1String("item ") + QString::number(i); - QStringListModel model(items); + QStringListModel model(generateList(QLatin1String("item "), rowCount)); QListView view; view.setWindowTitle(QTest::currentTestFunction()); @@ -841,13 +835,9 @@ void tst_QListView::batchedMode() void tst_QListView::setCurrentIndex() { - QStringList items; - int i; - for (i=0; i <20; ++i) - items << QString("item %1").arg(i); - QStringListModel model(items); + QStringListModel model(generateList(QLatin1String("item "), 20)); - QListView view; + ScrollPerItemListView view; view.setModel(&model); view.resize(220,182); @@ -867,7 +857,7 @@ void tst_QListView::setCurrentIndex() int offset = sb->value(); // first "scroll" down, verify that we scroll one step at a time - i = 0; + int i = 0; for (i = 0; i < 20; ++i) { QModelIndex idx = model.index(i,0); view.setCurrentIndex(idx); @@ -1177,7 +1167,7 @@ void tst_QListView::scrollTo() { QWidget topLevel; setFrameless(&topLevel); - QListView lv(&topLevel); + ScrollPerItemListView lv(&topLevel); QStringListModel model(&lv); QStringList list; list << "Short item 1"; @@ -1213,6 +1203,7 @@ void tst_QListView::scrollTo() model.setStringList(list); lv.setModel(&model); lv.setFixedSize(110, 200); + topLevel.show(); QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); @@ -1286,15 +1277,16 @@ void tst_QListView::scrollBarRanges() const int rowHeight = 20; QWidget topLevel; - QListView lv(&topLevel); + ScrollPerItemListView lv(&topLevel); QStringListModel model(&lv); QStringList list; for (int i = 0; i < rowCount; ++i) - list << QString::fromLatin1("Item %1").arg(i); + list << QLatin1String("Item ") + QString::number(i); model.setStringList(list); lv.setModel(&model); lv.resize(250, 130); + TestDelegate *delegate = new TestDelegate(&lv); delegate->m_sizeHint = QSize(100, rowHeight); lv.setItemDelegate(delegate); @@ -1388,17 +1380,13 @@ void tst_QListView::scrollBarAsNeeded() QStringList list; int i; for (i = 0; i < rowCounts[r]; ++i) - list << QString::fromLatin1("Item %1").arg(i); + list << QLatin1String("Item ") + QString::number(i); model.setStringList(list); QApplication::processEvents(); QTest::qWait(50); - QStringList replacement; - for (i = 0; i < itemCount; ++i) { - replacement << QString::fromLatin1("Item %1").arg(i); - } - model.setStringList(replacement); + model.setStringList(generateList(QLatin1String("Item "), itemCount)); QApplication::processEvents(); @@ -1411,10 +1399,9 @@ void tst_QListView::moveItems() { QStandardItemModel model; for (int r = 0; r < 4; ++r) { - for (int c = 0; c < 4; ++c) { - QStandardItem* item = new QStandardItem(QString("standard item (%1,%2)").arg(r).arg(c)); - model.setItem(r, c, item); - } + const QString prefix = QLatin1String("standard item (") + QString::number(r) + QLatin1Char(','); + for (int c = 0; c < 4; ++c) + model.setItem(r, c, new QStandardItem(prefix + QString::number(c) + QLatin1Char(')'))); } PublicListView view; @@ -1456,15 +1443,6 @@ void tst_QListView::wordWrap() lv.setModel(&model); lv.setWordWrap(true); lv.setFixedSize(400, 150); - -#if defined Q_OS_BLACKBERRY - QFont font = lv.font(); - // On BB10 the root window is stretched over the whole screen - // This makes sure that the text will be long enough to produce - // a vertical scrollbar - font.setPixelSize(50); - lv.setFont(font); -#endif lv.showNormal(); QApplication::processEvents(); @@ -1479,8 +1457,10 @@ class SetCurrentIndexAfterAppendRowCrashDialog : public QDialog public: SetCurrentIndexAfterAppendRowCrashDialog() { -#if WINVER >= 0x0500 - listView = new QListView(); + setWindowTitle(QTest::currentTestFunction()); + listView = new QListView(this); + QVBoxLayout *layout = new QVBoxLayout(this); + layout->addWidget(listView); listView->setViewMode(QListView::IconMode); model = new QStandardItemModel(this); @@ -1489,12 +1469,16 @@ public: timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(buttonClicked())); timer->start(1000); + } +protected: + void showEvent(QShowEvent *event) override + { + QDialog::showEvent(event); DWORD lParam = 0xFFFFFFFC/*OBJID_CLIENT*/; DWORD wParam = 0; if (const HWND hwnd =getHWNDForWidget(this)) SendMessage(hwnd, WM_GETOBJECT, wParam, lParam); -#endif } private slots: @@ -1511,24 +1495,22 @@ private: QStandardItemModel *model; QTimer *timer; }; -#endif -#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) && WINVER >= 0x0500 -// This test only makes sense on windows 2000 and higher. void tst_QListView::setCurrentIndexAfterAppendRowCrash() { SetCurrentIndexAfterAppendRowCrashDialog w; w.exec(); } -#endif +#endif // Q_OS_WIN && !Q_OS_WINCE && !Q_OS_WINRT void tst_QListView::emptyItemSize() { QStandardItemModel model; for (int r = 0; r < 4; ++r) { - QStandardItem* item = new QStandardItem(QString("standard item (%1)").arg(r)); - model.setItem(r, 0, item); + const QString text = QLatin1String("standard item (") + QString::number(r) + QLatin1Char(')'); + model.setItem(r, new QStandardItem(text)); } + model.setItem(4, 0, new QStandardItem()); PublicListView view; @@ -1851,7 +1833,7 @@ void tst_QListView::taskQTBUG_2233_scrollHiddenItems() QWidget topLevel; setFrameless(&topLevel); - QListView view(&topLevel); + ScrollPerItemListView view(&topLevel); QStringListModel model(&view); QStringList list; for (int i = 0; i < rowCount; ++i) @@ -1995,12 +1977,7 @@ public: void tst_QListView::taskQTBUG_9455_wrongScrollbarRanges() { - QStringList list; - const int nrItems = 8; - for (int i = 0; i < nrItems; i++) - list << QString::asprintf("item %d", i); - - QStringListModel model(list); + QStringListModel model(generateList("item ", 8)); ListView_9455 w; setFrameless(&w); w.setModel(&model); @@ -2084,7 +2061,7 @@ void tst_QListView::taskQTBUG_12308_wrongFlowLayout() QListWidgetItem *item = new QListWidgetItem(); item->setText(QString("Item %L1").arg(i)); lw.addItem(item); - if (!item->text().contains(QString::fromLatin1("1"))) + if (!item->text().contains(QLatin1Char('1'))) item->setHidden(true); } lw.show(); @@ -2100,15 +2077,9 @@ void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems_data() void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems() { -#if defined Q_OS_BLACKBERRY - // On BB10 we need to create a root window which is automatically stretched - // over the whole screen - QWindow rootWindow; - rootWindow.show(); -#endif QFETCH(int, flow); - QListView lv; + ScrollPerItemListView lv; lv.setUniformItemSizes(true); lv.setFlow(static_cast<QListView::Flow>(flow)); @@ -2174,9 +2145,9 @@ void tst_QListView::draggablePaintPairs() view.scrollTo(expectedIndex); QItemViewPaintPairs pairs = privateClass->draggablePaintPairs(indexList, &rect); QCOMPARE(indexList.size(), pairs.size()); - foreach (const QItemViewPaintPair pair, pairs) { - QCOMPARE(rect, pair.first); - QCOMPARE(expectedIndex, pair.second); + foreach (const QItemViewPaintPair &pair, pairs) { + QCOMPARE(rect, pair.rect); + QCOMPARE(expectedIndex, pair.index); } } @@ -2203,7 +2174,7 @@ void tst_QListView::taskQTBUG_21804_hiddenItemsAndScrollingWithKeys() model.setStringList(list); // create listview - QListView lv; + ScrollPerItemListView lv; lv.setFlow(static_cast<QListView::Flow>(flow)); lv.setSpacing(spacing); lv.setModel(&model); @@ -2275,7 +2246,7 @@ void tst_QListView::spacing() model.setStringList(list); // create listview - QListView lv; + ScrollPerItemListView lv; lv.setFlow(static_cast<QListView::Flow>(flow)); lv.setModel(&model); lv.setSpacing(spacing); @@ -2298,12 +2269,6 @@ void tst_QListView::spacing() void tst_QListView::testScrollToWithHidden() { -#if defined Q_OS_BLACKBERRY - // On BB10 we need to create a root window which is automatically stretched - // over the whole screen - QWindow rootWindow; - rootWindow.show(); -#endif QListView lv; QStringListModel model; @@ -2494,6 +2459,44 @@ void tst_QListView::horizontalScrollingByVerticalWheelEvents() QVERIFY(lv.verticalScrollBar()->value() > vValue); } +void tst_QListView::taskQTBUG_7232_AllowUserToControlSingleStep() +{ + // When we set the scrollMode to ScrollPerPixel it will adjust the scrollbars singleStep automatically + // Setting a singlestep on a scrollbar should however imply that the user takes control. + // Setting a singlestep to -1 return to an automatic control of the singleStep. + QListView lv; + lv.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + + QStandardItemModel model(1000, 100); + QString str = QString::fromLatin1("This is a long string made to ensure that we get some horizontal scroll (and we want scroll)"); + model.setData(model.index(0, 0), str); + lv.setModel(&model); + lv.setGeometry(150, 150, 150, 150); + lv.show(); + lv.setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); + lv.setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); + QVERIFY(QTest::qWaitForWindowExposed(&lv)); + + int vStep1 = lv.verticalScrollBar()->singleStep(); + int hStep1 = lv.horizontalScrollBar()->singleStep(); + QVERIFY(lv.verticalScrollBar()->singleStep() > 1); + QVERIFY(lv.horizontalScrollBar()->singleStep() > 1); + + lv.verticalScrollBar()->setSingleStep(1); + lv.setGeometry(200, 200, 200, 200); + QCOMPARE(lv.verticalScrollBar()->singleStep(), 1); + + lv.horizontalScrollBar()->setSingleStep(1); + lv.setGeometry(150, 150, 150, 150); + QCOMPARE(lv.horizontalScrollBar()->singleStep(), 1); + + lv.verticalScrollBar()->setSingleStep(-1); + lv.horizontalScrollBar()->setSingleStep(-1); + QCOMPARE(vStep1, lv.verticalScrollBar()->singleStep()); + QCOMPARE(hStep1, lv.horizontalScrollBar()->singleStep()); +} + void tst_QListView::taskQTBUG_51086_skippingIndexesInSelectedIndexes() { // simple way to get access to selectedIndexes() |