Use a fake directory model instead of QDirModel in item view tests.

The tests then have a predictable, stable environment and do not depend
on file system operations.

Task-number: QTBUG-23697
Change-Id: Ibbd356f8bd7419ec4a3a88d2c0b5cd0830049790
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
This commit is contained in:
Friedemann Kleint 2014-01-23 16:04:16 +01:00 committed by The Qt Project
parent 9ac20dc44c
commit b7de9e7353
5 changed files with 201 additions and 82 deletions

View File

@ -4,4 +4,5 @@ QT += widgets widgets-private
QT += gui-private core-private testlib
SOURCES += tst_qcolumnview.cpp
HEADERS += ../../../../shared/fakedirmodel.h
TARGET = tst_qcolumnview

View File

@ -39,14 +39,12 @@
**
****************************************************************************/
#include "../../../../shared/fakedirmodel.h"
#include <QtTest/QtTest>
#include <qstandarditemmodel.h>
#include <qitemdelegate.h>
#include <qcolumnview.h>
#include <private/qcolumnviewgrip_p.h>
#include <private/qfilesystemmodel_p.h>
#include <qdirmodel.h>
#include <qstringlistmodel.h>
#include <qdebug.h>
#include <qitemdelegate.h>
@ -64,6 +62,7 @@ public:
virtual ~tst_QColumnView();
public Q_SLOTS:
void initTestCase();
void init();
void cleanup();
@ -107,6 +106,10 @@ private slots:
protected slots:
void setPreviewWidget();
private:
QStandardItemModel m_fakeDirModel;
QModelIndex m_fakeDirHomeIndex;
};
class TreeModel : public QStandardItemModel
@ -182,12 +185,20 @@ protected:
tst_QColumnView::tst_QColumnView()
{
QStandardItem *homeItem = populateFakeDirModel(&m_fakeDirModel);
m_fakeDirHomeIndex = m_fakeDirModel.indexFromItem(homeItem);
}
tst_QColumnView::~tst_QColumnView()
{
}
void tst_QColumnView::initTestCase()
{
QVERIFY(m_fakeDirHomeIndex.isValid());
QVERIFY(m_fakeDirModel.rowCount(m_fakeDirHomeIndex) > 1); // Needs some entries in 'home'.
}
void tst_QColumnView::init()
{
qApp->setLayoutDirection(Qt::LeftToRight);
@ -268,8 +279,7 @@ void tst_QColumnView::rootIndex()
void tst_QColumnView::grips()
{
QColumnView view;
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
QCOMPARE(view.resizeGripsVisible(), true);
view.setResizeGripsVisible(true);
@ -304,8 +314,7 @@ void tst_QColumnView::isIndexHidden()
ColumnView view;
QModelIndex idx;
QCOMPARE(view.IsIndexHidden(idx), false);
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
QCOMPARE(view.IsIndexHidden(idx), false);
}
@ -313,22 +322,20 @@ void tst_QColumnView::indexAt()
{
QColumnView view;
QCOMPARE(view.indexAt(QPoint(0,0)), QModelIndex());
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
QModelIndex home = model.index(QDir::homePath());
QModelIndex homeFile = model.index(0, 0, home);
QModelIndex homeFile = m_fakeDirModel.index(0, 0, m_fakeDirHomeIndex);
if (!homeFile.isValid())
return;
view.setRootIndex(home);
view.setRootIndex(m_fakeDirHomeIndex);
QRect rect = view.visualRect(QModelIndex());
QVERIFY(!rect.isValid());
rect = view.visualRect(homeFile);
QVERIFY(rect.isValid());
QModelIndex child;
for (int i = 0; i < model.rowCount(home); ++i) {
child = model.index(i, 0, home);
for (int i = 0; i < m_fakeDirModel.rowCount(m_fakeDirHomeIndex); ++i) {
child = m_fakeDirModel.index(i, 0, m_fakeDirHomeIndex);
rect = view.visualRect(child);
QVERIFY(rect.isValid());
if (i > 0)
@ -341,8 +348,8 @@ void tst_QColumnView::indexAt()
QTest::qWait(200);
// test that the second row doesn't start at 0
if (model.rowCount(child) > 0) {
child = model.index(0, 0, child);
if (m_fakeDirModel.rowCount(child) > 0) {
child = m_fakeDirModel.index(0, 0, child);
QVERIFY(child.isValid());
rect = view.visualRect(child);
QVERIFY(rect.isValid());
@ -517,47 +524,48 @@ void tst_QColumnView::moveCursor()
// don't do anything
QCOMPARE(view.MoveCursor(ColumnView::MoveEnd, Qt::NoModifier), QModelIndex());
QDirModel model;
view.setModel(&model);
QModelIndex home = model.index(QDir::homePath());
view.setModel(&m_fakeDirModel);
QModelIndex ci = view.currentIndex();
QCOMPARE(view.MoveCursor(ColumnView::MoveUp, Qt::NoModifier), QModelIndex());
QCOMPARE(view.MoveCursor(ColumnView::MoveDown, Qt::NoModifier), QModelIndex());
// left at root
view.setCurrentIndex(model.index(0,0));
view.setCurrentIndex(m_fakeDirModel.index(0,0));
ColumnView::PublicCursorAction action = reverse ? ColumnView::MoveRight : ColumnView::MoveLeft;
QCOMPARE(view.MoveCursor(action, Qt::NoModifier), model.index(0,0));
QCOMPARE(view.MoveCursor(action, Qt::NoModifier), m_fakeDirModel.index(0,0));
// left shouldn't move up
int i = 0;
ci = model.index(0, 0);
while (i < model.rowCount() - 1 && !model.hasChildren(ci))
ci = model.index(++i, 0);
QVERIFY(model.hasChildren(ci));
ci = m_fakeDirModel.index(0, 0);
while (i < m_fakeDirModel.rowCount() - 1 && !m_fakeDirModel.hasChildren(ci))
ci = m_fakeDirModel.index(++i, 0);
QVERIFY(m_fakeDirModel.hasChildren(ci));
view.setCurrentIndex(ci);
action = reverse ? ColumnView::MoveRight : ColumnView::MoveLeft;
QCOMPARE(view.MoveCursor(action, Qt::NoModifier), ci);
// now move to the left (i.e. move over one column)
view.setCurrentIndex(home);
QCOMPARE(view.MoveCursor(action, Qt::NoModifier), home.parent());
view.setCurrentIndex(m_fakeDirHomeIndex);
QCOMPARE(view.MoveCursor(action, Qt::NoModifier), m_fakeDirHomeIndex.parent());
// right
action = reverse ? ColumnView::MoveLeft : ColumnView::MoveRight;
view.setCurrentIndex(ci);
QModelIndex mc = view.MoveCursor(action, Qt::NoModifier);
QCOMPARE(mc, model.index(0,0, ci));
QCOMPARE(mc, m_fakeDirModel.index(0,0, ci));
// next one should move down
QModelIndex idx = model.index(0, 0, ci);
while (model.hasChildren(idx) && model.rowCount(ci) > idx.row() + 1)
// for empty directories (no way to go 'right'), next one should move down
QModelIndex idx = m_fakeDirModel.index(0, 0, ci);
const int rowCount = m_fakeDirModel.rowCount(ci);
while (m_fakeDirModel.hasChildren(idx) && rowCount > idx.row() + 1) {
idx = idx.sibling(idx.row() + 1, idx.column());
}
static const char error[] = "This test requires an empty directory followed by another directory.";
QVERIFY2(idx.isValid(), error);
QVERIFY2(!m_fakeDirModel.hasChildren(idx), error);
QVERIFY2(idx.row() + 1 < rowCount, error);
view.setCurrentIndex(idx);
mc = view.MoveCursor(action, Qt::NoModifier);
#ifdef Q_OS_MAC
QEXPECT_FAIL("", "QTBUG-23697", Continue);
#endif
QCOMPARE(mc, idx.sibling(idx.row() + 1, idx.column()));
}
@ -566,20 +574,18 @@ void tst_QColumnView::selectAll()
ColumnView view;
view.selectAll();
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
view.selectAll();
QVERIFY(view.selectionModel()->selectedIndexes().count() >= 0);
QModelIndex home = model.index(QDir::homePath());
view.setCurrentIndex(home);
view.setCurrentIndex(m_fakeDirHomeIndex);
view.selectAll();
QVERIFY(view.selectionModel()->selectedIndexes().count() > 0);
QModelIndex file;
for (int i = 0; i < model.rowCount(home); ++i)
if (!model.hasChildren(model.index(i, 0, home))) {
file = model.index(i, 0, home);
for (int i = 0; i < m_fakeDirModel.rowCount(m_fakeDirHomeIndex); ++i)
if (!m_fakeDirModel.hasChildren(m_fakeDirModel.index(i, 0, m_fakeDirHomeIndex))) {
file = m_fakeDirModel.index(i, 0, m_fakeDirHomeIndex);
break;
}
view.setCurrentIndex(file);
@ -594,22 +600,19 @@ void tst_QColumnView::clicked()
{
ColumnView view;
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
view.resize(800,300);
view.show();
QModelIndex home = model.index(QDir::homePath());
QVERIFY(home.isValid());
view.setCurrentIndex(home);
view.setCurrentIndex(m_fakeDirHomeIndex);
QTest::qWait(ANIMATION_DELAY);
QModelIndex parent = home.parent();
QModelIndex parent = m_fakeDirHomeIndex.parent();
QVERIFY(parent.isValid());
QSignalSpy clickedSpy(&view, SIGNAL(clicked(QModelIndex)));
QPoint localPoint = view.visualRect(home).center();
QPoint localPoint = view.visualRect(m_fakeDirHomeIndex).center();
QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, localPoint);
QCOMPARE(clickedSpy.count(), 1);
qApp->processEvents();
@ -619,7 +622,7 @@ void tst_QColumnView::clicked()
for (int i = 0; i < view.createdColumns.count(); ++i) {
QAbstractItemView *column = view.createdColumns.at(i);
if (column && column->selectionModel() && (column->rootIndex() == home))
if (column && column->selectionModel() && (column->rootIndex() == m_fakeDirHomeIndex))
QVERIFY(column->selectionModel()->selectedIndexes().isEmpty());
}
}
@ -627,13 +630,11 @@ void tst_QColumnView::clicked()
void tst_QColumnView::selectedColumns()
{
ColumnView view;
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
view.resize(800,300);
view.show();
QModelIndex home = model.index(QDir::homePath());
view.setCurrentIndex(home);
view.setCurrentIndex(m_fakeDirHomeIndex);
QTest::qWait(ANIMATION_DELAY);
@ -641,7 +642,7 @@ void tst_QColumnView::selectedColumns()
QAbstractItemView *column = view.createdColumns.at(i);
if (!column)
continue;
if (!column->rootIndex().isValid() || column->rootIndex() == home)
if (!column->rootIndex().isValid() || column->rootIndex() == m_fakeDirHomeIndex)
continue;
QTRY_VERIFY(column->currentIndex().isValid());
}
@ -658,15 +659,13 @@ void tst_QColumnView::setSelection()
void tst_QColumnView::setSelectionModel()
{
ColumnView view;
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
view.show();
QModelIndex home = model.index(QDir::homePath());
view.setCurrentIndex(home);
view.setCurrentIndex(m_fakeDirHomeIndex);
QTest::qWait(ANIMATION_DELAY);
QItemSelectionModel *selectionModel = new QItemSelectionModel(&model);
QItemSelectionModel *selectionModel = new QItemSelectionModel(&m_fakeDirModel);
view.setSelectionModel(selectionModel);
bool found = false;
@ -686,19 +685,10 @@ void tst_QColumnView::visualRegionForSelection()
QCOMPARE(QRegion(), view.getVisualRegionForSelection(emptyItemSelection));
// a region that isn't empty
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
// On Windows CE the home directory might actually be empty.
#ifndef Q_OS_WINCE
QString location = QDir::homePath();
#else
QString location = QLatin1String("/Windows");
#endif
QModelIndex home = model.index(location);
QVERIFY(model.rowCount(home) > 1);
QItemSelection itemSelection(model.index(0, 0, home), model.index(model.rowCount(home) - 1, 0, home));
QItemSelection itemSelection(m_fakeDirModel.index(0, 0, m_fakeDirHomeIndex), m_fakeDirModel.index(m_fakeDirModel.rowCount(m_fakeDirHomeIndex) - 1, 0, m_fakeDirHomeIndex));
QVERIFY(QRegion() != view.getVisualRegionForSelection(itemSelection));
}
@ -871,10 +861,8 @@ void tst_QColumnView::sizes()
view.setColumnWidths(newSizes);
QCOMPARE(view.columnWidths(), visibleSizes);
QDirModel model;
view.setModel(&model);
QModelIndex home = model.index(QDir::homePath());
view.setCurrentIndex(home);
view.setModel(&m_fakeDirModel);
view.setCurrentIndex(m_fakeDirHomeIndex);
QList<int> postSizes = view.columnWidths().mid(0, newSizes.count());
QCOMPARE(postSizes, newSizes.mid(0, postSizes.count()));
@ -895,8 +883,7 @@ void tst_QColumnView::rowDelegate()
QItemDelegate *d = new QItemDelegate;
view.setItemDelegateForRow(3, d);
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
for (int i = 0; i < view.createdColumns.count(); ++i) {
QAbstractItemView *column = view.createdColumns.at(i);
QCOMPARE(column->itemDelegateForRow(3), (QAbstractItemDelegate*)d);
@ -908,13 +895,11 @@ void tst_QColumnView::resize()
{
QWidget topLevel;
ColumnView view(&topLevel);
QDirModel model;
view.setModel(&model);
view.setModel(&m_fakeDirModel);
view.resize(200, 200);
topLevel.show();
QModelIndex home = model.index(QDir::homePath()).parent();
view.setCurrentIndex(home);
view.setCurrentIndex(m_fakeDirHomeIndex);
QTest::qWait(ANIMATION_DELAY);
view.resize(200, 300);
QTest::qWait(ANIMATION_DELAY);

View File

@ -3,4 +3,4 @@ TARGET = tst_qtreeview
QT += widgets testlib
QT += widgets-private gui-private core-private
SOURCES += tst_qtreeview.cpp
HEADERS += ../../../../shared/fakedirmodel.h

View File

@ -39,6 +39,7 @@
**
****************************************************************************/
#include "../../../../shared/fakedirmodel.h"
#include <qabstractitemview.h>
#include <QtTest/QtTest>
#include <QtGui/QtGui>
@ -3719,7 +3720,9 @@ void tst_QTreeView::task246536_scrollbarsNotWorking()
void tst_QTreeView::task250683_wrongSectionSize()
{
QDirModel model;
QStandardItemModel model;
populateFakeDirModel(&model);
QTreeView treeView;
treeView.header()->setSectionResizeMode(QHeaderView::ResizeToContents);
treeView.setModel(&model);

130
tests/shared/fakedirmodel.h Normal file
View File

@ -0,0 +1,130 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt 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.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef FAKEDIRMODEL_H
#define FAKEDIRMODEL_H
#include <QtGui/QStandardItemModel>
#include <QtGui/QStandardItem>
#include <QtGui/QIcon>
#include <QtGui/QPixmap>
#include <QtGui/QImage>
#include <QtCore/QStringList>
typedef QList<QStandardItem *> StandardItemList;
static inline QIcon coloredIcon(Qt::GlobalColor color)
{
QImage image(22, 22, QImage::Format_ARGB32);
image.fill(color);
return QPixmap::fromImage(image);
}
static void addFileEntry(const StandardItemList &directory, const QString &name, const QString &size)
{
static const QIcon fileIcon = coloredIcon(Qt::blue);
directory.front()->appendRow(StandardItemList() << new QStandardItem(fileIcon, name) << new QStandardItem(size));
}
static StandardItemList createDirEntry(const QString &name)
{
static const QIcon dirIcon = coloredIcon(Qt::red);
StandardItemList result;
result << new QStandardItem(dirIcon, name) << new QStandardItem;
return result;
}
static inline StandardItemList addDirEntry(const StandardItemList &directory, const QString &name)
{
const StandardItemList entry = createDirEntry(name);
directory.front()->appendRow(entry);
return entry;
}
static QStandardItem *populateFakeDirModel(QStandardItemModel *model)
{
enum Columns { NameColumn, SizeColumn, ColumnCount };
model->setColumnCount(ColumnCount);
model->setHorizontalHeaderLabels(QStringList() << QStringLiteral("Name") << QStringLiteral("Size"));
const StandardItemList root = createDirEntry(QStringLiteral("/"));
model->appendRow(root);
const StandardItemList binDir = addDirEntry(root, QStringLiteral("bin"));
addFileEntry(binDir, QStringLiteral("ls"), QStringLiteral("100 KB"));
addFileEntry(binDir, QStringLiteral("bash"), QStringLiteral("200 KB"));
const StandardItemList devDir = addDirEntry(root, QStringLiteral("dev"));
addFileEntry(devDir, QStringLiteral("tty1"), QStringLiteral("0 B"));
addDirEntry(devDir, QStringLiteral("proc"));
const StandardItemList etcDir = addDirEntry(root, QStringLiteral("etc"));
addFileEntry(etcDir, QStringLiteral("foo1.config"), QStringLiteral("1 KB"));
addFileEntry(etcDir, QStringLiteral("foo2.conf"), QStringLiteral("654 B"));
const StandardItemList homeDir = addDirEntry(root, QStringLiteral("home"));
addFileEntry(homeDir, QStringLiteral("file1"), QStringLiteral("1 KB"));
const StandardItemList documentsDir = addDirEntry(homeDir, QStringLiteral("Documents"));
addFileEntry(documentsDir, QStringLiteral("txt1.odt"), QStringLiteral("2 MB"));
addFileEntry(documentsDir, QStringLiteral("sheet1.xls"), QStringLiteral("32 KB"));
addFileEntry(documentsDir, QStringLiteral("foo.doc"), QStringLiteral("214 KB"));
const StandardItemList downloadsDir = addDirEntry(homeDir, QStringLiteral("Downloads"));
addFileEntry(downloadsDir, QStringLiteral("package1.zip"), QStringLiteral("34 MB"));
addFileEntry(downloadsDir, QStringLiteral("package2.zip"), QStringLiteral("623 KB"));
const StandardItemList picturesDir = addDirEntry(homeDir, QStringLiteral("Pictures"));
addFileEntry(picturesDir, QStringLiteral("img0001.jpg"), QStringLiteral("4 MB"));
addFileEntry(picturesDir, QStringLiteral("img0002.png"), QStringLiteral("10 MB"));
// qcolumnview::moveCursor() requires an empty directory followed by another one.
addDirEntry(root, QStringLiteral("lost+found"));
const StandardItemList tmpDir = addDirEntry(root, QStringLiteral("tmp"));
addFileEntry(tmpDir, "asdujhsdjys", "435 B");
addFileEntry(tmpDir, "krtbldfhd", "5557 B");
return homeDir.front();
}
#endif // FAKEDIRMODEL_H