tst_QItemView: cleanup

Cleanup QItemView autotest:
 - use range-based for loops
 - use nullptr
 - use member initialization
 - use new signal/slot syntax
 - use static invocations
 - use override

Change-Id: I46edc2dc5bbaa09fb11710fa34ef4c6639181f62
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-09-08 20:07:22 +02:00
parent 65cb6f5f29
commit 2431765d57
2 changed files with 86 additions and 76 deletions

View File

@ -26,9 +26,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QRandomGenerator>
#include <QtTest/QtTest> #include <QStack>
#include <QtCore/QtCore> #include <QStandardItemModel>
#include <QTest>
#include "viewstotest.cpp" #include "viewstotest.cpp"
/*! /*!
@ -89,17 +90,19 @@ class CheckerModel : public QStandardItemModel
Q_OBJECT Q_OBJECT
public: public:
CheckerModel() : QStandardItemModel() {}; using QStandardItemModel::QStandardItemModel;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole ) const { QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if (!index.isValid()) { if (!index.isValid()) {
qWarning("%s: index is not valid", Q_FUNC_INFO); qWarning("%s: index is not valid", Q_FUNC_INFO);
return QVariant(); return QVariant();
} }
return QStandardItemModel::data(index, role); return QStandardItemModel::data(index, role);
}; }
Qt::ItemFlags flags(const QModelIndex & index) const { Qt::ItemFlags flags(const QModelIndex &index) const override
{
if (!index.isValid()) { if (!index.isValid()) {
qWarning("%s: index is not valid", Q_FUNC_INFO); qWarning("%s: index is not valid", Q_FUNC_INFO);
return Qt::ItemFlags(); return Qt::ItemFlags();
@ -107,21 +110,24 @@ public:
if (index.row() == 2 || index.row() == rowCount() - 3 if (index.row() == 2 || index.row() == rowCount() - 3
|| index.column() == 2 || index.column() == columnCount() - 3) { || index.column() == 2 || index.column() == columnCount() - 3) {
Qt::ItemFlags f = QStandardItemModel::flags(index); Qt::ItemFlags f = QStandardItemModel::flags(index);
f &= ~Qt::ItemIsEnabled; f.setFlag(Qt::ItemIsEnabled, false);
return f; return f;
} }
return QStandardItemModel::flags(index); return QStandardItemModel::flags(index);
}; }
QModelIndex parent ( const QModelIndex & child ) const { QModelIndex parent(const QModelIndex &child) const override
{
if (!child.isValid()) { if (!child.isValid()) {
qWarning("%s: child index is not valid", Q_FUNC_INFO); qWarning("%s: child index is not valid", Q_FUNC_INFO);
return QModelIndex(); return QModelIndex();
} }
return QStandardItemModel::parent(child); return QStandardItemModel::parent(child);
}; }
QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const { QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override
{
if (orientation == Qt::Horizontal if (orientation == Qt::Horizontal
&& (section < 0 || section > columnCount())) { && (section < 0 || section > columnCount())) {
qWarning("%s: invalid section %d, must be in range 0..%d", qWarning("%s: invalid section %d, must be in range 0..%d",
@ -137,11 +143,9 @@ public:
return QStandardItemModel::headerData(section, orientation, role); return QStandardItemModel::headerData(section, orientation, role);
} }
QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const { bool setData(const QModelIndex &index, const QVariant &value,
return QStandardItemModel::index(row, column, parent); int role = Qt::EditRole) override
}; {
bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) {
if (!index.isValid()) { if (!index.isValid()) {
qWarning("%s: index is not valid", Q_FUNC_INFO); qWarning("%s: index is not valid", Q_FUNC_INFO);
return false; return false;
@ -149,15 +153,19 @@ public:
return QStandardItemModel::setData(index, value, role); return QStandardItemModel::setData(index, value, role);
} }
void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) { void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override
{
if (column < 0 || column > columnCount()) if (column < 0 || column > columnCount())
qWarning("%s: invalid column %d, must be in range 0..%d", qWarning("%s: invalid column %d, must be in range 0..%d",
Q_FUNC_INFO, column, columnCount()); Q_FUNC_INFO, column, columnCount());
else else
QStandardItemModel::sort(column, order); QStandardItemModel::sort(column, order);
}; }
QModelIndexList match ( const QModelIndex & start, int role, const QVariant & value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap ) ) const { QModelIndexList match(const QModelIndex &start, int role,
const QVariant &value, int hits = 1,
Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchWrap)) const override
{
if (hits <= 0) { if (hits <= 0) {
qWarning("%s: hits must be greater than zero", Q_FUNC_INFO); qWarning("%s: hits must be greater than zero", Q_FUNC_INFO);
return QModelIndexList(); return QModelIndexList();
@ -167,9 +175,11 @@ public:
return QModelIndexList(); return QModelIndexList();
} }
return QAbstractItemModel::match(start, role, value, hits, flags); return QAbstractItemModel::match(start, role, value, hits, flags);
}; }
bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole ) { bool setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role = Qt::EditRole) override
{
if (orientation == Qt::Horizontal if (orientation == Qt::Horizontal
&& (section < 0 || section > columnCount())) { && (section < 0 || section > columnCount())) {
qWarning("%s: invalid section %d, must be in range 0..%d", qWarning("%s: invalid section %d, must be in range 0..%d",
@ -183,7 +193,7 @@ public:
return false; return false;
} }
return QAbstractItemModel::setHeaderData(section, orientation, value, role); return QAbstractItemModel::setHeaderData(section, orientation, value, role);
}; }
}; };
void tst_QItemView::init() void tst_QItemView::init()
@ -197,9 +207,9 @@ void tst_QItemView::cleanup()
delete testViews; delete testViews;
delete view; delete view;
delete treeModel; delete treeModel;
view = 0; view = nullptr;
testViews = 0; testViews = nullptr;
treeModel = 0; treeModel = nullptr;
} }
void tst_QItemView::setupWithNoTestData() void tst_QItemView::setupWithNoTestData()
@ -207,15 +217,15 @@ void tst_QItemView::setupWithNoTestData()
ViewsToTest testViews; ViewsToTest testViews;
QTest::addColumn<QString>("viewType"); QTest::addColumn<QString>("viewType");
QTest::addColumn<bool>("displays"); QTest::addColumn<bool>("displays");
QTest::addColumn<int>("vscroll"); QTest::addColumn<QAbstractItemView::ScrollMode>("vscroll");
QTest::addColumn<int>("hscroll"); QTest::addColumn<QAbstractItemView::ScrollMode>("hscroll");
for (int i = 0; i < testViews.tests.size(); ++i) { for (int i = 0; i < testViews.tests.size(); ++i) {
QString view = testViews.tests.at(i).viewType; QString view = testViews.tests.at(i).viewType;
QString test = view + " ScrollPerPixel"; QString test = view + " ScrollPerPixel";
bool displayIndexes = (testViews.tests.at(i).display == ViewsToTest::DisplayRoot); bool displayIndexes = (testViews.tests.at(i).display == ViewsToTest::DisplayRoot);
QTest::newRow(test.toLatin1().data()) << view << displayIndexes QTest::newRow(test.toLatin1().data()) << view << displayIndexes
<< (int)QAbstractItemView::ScrollPerPixel << QAbstractItemView::ScrollPerPixel
<< (int)QAbstractItemView::ScrollPerPixel << QAbstractItemView::ScrollPerPixel
; ;
} }
for (int i = 0; i < testViews.tests.size(); ++i) { for (int i = 0; i < testViews.tests.size(); ++i) {
@ -223,8 +233,8 @@ void tst_QItemView::setupWithNoTestData()
QString test = view + " ScrollPerItem"; QString test = view + " ScrollPerItem";
bool displayIndexes = (testViews.tests.at(i).display == ViewsToTest::DisplayRoot); bool displayIndexes = (testViews.tests.at(i).display == ViewsToTest::DisplayRoot);
QTest::newRow(test.toLatin1().data()) << view << displayIndexes QTest::newRow(test.toLatin1().data()) << view << displayIndexes
<< (int)QAbstractItemView::ScrollPerItem << QAbstractItemView::ScrollPerItem
<< (int)QAbstractItemView::ScrollPerItem << QAbstractItemView::ScrollPerItem
; ;
} }
} }
@ -267,13 +277,13 @@ void tst_QItemView::nonDestructiveBasicTest_data()
void tst_QItemView::nonDestructiveBasicTest() void tst_QItemView::nonDestructiveBasicTest()
{ {
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
// setSelectionModel() will assert // setSelectionModel() will assert
//view->setSelectionModel(0); //view->setSelectionModel(0);
@ -396,7 +406,8 @@ void tst_QItemView::spider_data()
setupWithNoTestData(); setupWithNoTestData();
} }
void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress){ void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress)
{
int width = widget->width(); int width = widget->width();
int height = widget->height(); int height = widget->height();
for (int i = 0; i < 5; ++i) { for (int i = 0; i < 5; ++i) {
@ -409,7 +420,7 @@ void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress){
QTest::mousePress(widget, Qt::LeftButton, modifier, press); QTest::mousePress(widget, Qt::LeftButton, modifier, press);
QTest::mouseMove(widget, releasePoint); QTest::mouseMove(widget, releasePoint);
if (QRandomGenerator::global()->bounded(1) == 0) if (QRandomGenerator::global()->bounded(1) == 0)
QTest::mouseRelease(widget, Qt::LeftButton, 0, releasePoint); QTest::mouseRelease(widget, Qt::LeftButton, {}, releasePoint);
else else
QTest::mouseRelease(widget, Qt::LeftButton, modifier, releasePoint); QTest::mouseRelease(widget, Qt::LeftButton, modifier, releasePoint);
QTest::keyClick(widget, keyPress); QTest::keyClick(widget, keyPress);
@ -425,13 +436,13 @@ void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress){
void tst_QItemView::spider() void tst_QItemView::spider()
{ {
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
view->setModel(treeModel); view->setModel(treeModel);
view->show(); view->show();
QVERIFY(QTest::qWaitForWindowActive(view)); QVERIFY(QTest::qWaitForWindowActive(view));
@ -454,21 +465,21 @@ void tst_QItemView::resize()
QSKIP("This test needs to be re-thought out, it takes too long and doesn't really catch the problem."); QSKIP("This test needs to be re-thought out, it takes too long and doesn't really catch the problem.");
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
view->setModel(treeModel); view->setModel(treeModel);
view->show(); view->show();
for (int w = 100; w < 400; w+=10) { for (int w = 100; w < 400; w += 10) {
for (int h = 100; h < 400; h+=10) { for (int h = 100; h < 400; h += 10) {
view->resize(w, h); view->resize(w, h);
QTest::qWait(1); QTest::qWait(1);
qApp->processEvents(); QCoreApplication::processEvents();
} }
} }
} }
@ -481,13 +492,13 @@ void tst_QItemView::visualRect_data()
void tst_QItemView::visualRect() void tst_QItemView::visualRect()
{ {
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
QCOMPARE(view->visualRect(QModelIndex()), QRect()); QCOMPARE(view->visualRect(QModelIndex()), QRect());
// Add model // Add model
@ -617,13 +628,13 @@ void tst_QItemView::indexAt_data()
void tst_QItemView::indexAt() void tst_QItemView::indexAt()
{ {
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
view->show(); view->show();
view->setModel(treeModel); view->setModel(treeModel);
checkChildren(view); checkChildren(view);
@ -648,13 +659,13 @@ void tst_QItemView::scrollTo_data()
void tst_QItemView::scrollTo() void tst_QItemView::scrollTo()
{ {
QFETCH(QString, viewType); QFETCH(QString, viewType);
QFETCH(int, vscroll); QFETCH(QAbstractItemView::ScrollMode, vscroll);
QFETCH(int, hscroll); QFETCH(QAbstractItemView::ScrollMode, hscroll);
view = testViews->createView(viewType); view = testViews->createView(viewType);
QVERIFY(view); QVERIFY(view);
view->setVerticalScrollMode((QAbstractItemView::ScrollMode)vscroll); view->setVerticalScrollMode(vscroll);
view->setHorizontalScrollMode((QAbstractItemView::ScrollMode)hscroll); view->setHorizontalScrollMode(hscroll);
view->setModel(treeModel); view->setModel(treeModel);
view->show(); view->show();
@ -682,10 +693,10 @@ void tst_QItemView::moveCursor_data()
setupWithNoTestData(); setupWithNoTestData();
} }
class Event { struct Event
public: {
Event(){} Event(Qt::Key k, const QModelIndex &s, const QModelIndex &e, const QString &n)
Event(Qt::Key k, QModelIndex s, QModelIndex e, QString n) : key(k), start(s), end(e), name(n){} : key(k), start(s), end(e), name(n){}
Qt::Key key; Qt::Key key;
QModelIndex start; QModelIndex start;
QModelIndex end; QModelIndex end;

View File

@ -26,11 +26,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QHeaderView>
#include <QtTest/QtTest> #include <QListView>
#include <QtCore/QtCore> #include <QTableView>
#include <QtGui/QtGui> #include <QTreeView>
#include <QtWidgets/QtWidgets>
/* /*
To add a view to be tested add the header file to the includes To add a view to be tested add the header file to the includes
@ -54,12 +53,12 @@ public:
enum Display { DisplayNone, DisplayRoot }; enum Display { DisplayNone, DisplayRoot };
struct test { struct test {
test(QString m, Display d) : viewType(m), display(d){}; test(const QString &m, Display d) : viewType(m), display(d) {}
QString viewType; QString viewType;
Display display; Display display;
}; };
QList<test> tests; QVector<test> tests;
}; };
@ -84,7 +83,7 @@ ViewsToTest::ViewsToTest()
*/ */
QAbstractItemView *ViewsToTest::createView(const QString &viewType) QAbstractItemView *ViewsToTest::createView(const QString &viewType)
{ {
QAbstractItemView *view = 0; QAbstractItemView *view = nullptr;
if (viewType == "QListView_ScrollPerItem") { if (viewType == "QListView_ScrollPerItem") {
view = new QListView(); view = new QListView();
view->setObjectName("QListView"); view->setObjectName("QListView");
@ -137,16 +136,16 @@ void ViewsToTest::hideIndexes(QAbstractItemView *view)
if (QTableView *tableView = qobject_cast<QTableView *>(view)) { if (QTableView *tableView = qobject_cast<QTableView *>(view)) {
tableView->setColumnHidden(1, true); tableView->setColumnHidden(1, true);
tableView->setRowHidden(1, true); tableView->setRowHidden(1, true);
tableView->setRowHidden(tableView->model()->rowCount()-2, true); tableView->setRowHidden(tableView->model()->rowCount() - 2, true);
} }
if (QTreeView *treeView = qobject_cast<QTreeView *>(view)) { if (QTreeView *treeView = qobject_cast<QTreeView *>(view)) {
treeView->setColumnHidden(1, true); treeView->setColumnHidden(1, true);
treeView->setRowHidden(1, QModelIndex(), true); treeView->setRowHidden(1, QModelIndex(), true);
treeView->setRowHidden(treeView->model()->rowCount()-2, QModelIndex(), true); treeView->setRowHidden(treeView->model()->rowCount() - 2, QModelIndex(), true);
} }
if (QListView *listView = qobject_cast<QListView *>(view)) { if (QListView *listView = qobject_cast<QListView *>(view)) {
listView->setRowHidden(1, true); listView->setRowHidden(1, true);
listView->setRowHidden(listView->model()->rowCount()-2, true); listView->setRowHidden(listView->model()->rowCount() - 2, true);
} }
} }