From: Frank Reininghaus Date: Fri, 22 Oct 2010 15:41:47 +0000 (+0000) Subject: Add a second Dolphin unit test (for a regression in DolphinDetailsView X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/28bc452f0d4489e0f3076996dde128a5b2940209 Add a second Dolphin unit test (for a regression in DolphinDetailsView which has been fixed recently). This commit also adds a new class TestHelper which provides some funtionality that most Dolphin unit tests will need. I hope that this makes implementing additional tests as easy as possible :-) svn path=/trunk/KDE/kdebase/apps/; revision=1188536 --- diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 6e146cc2e..510b330aa 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -3,3 +3,6 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/.. ${KDE4_INCLUDES} ) kde4_add_unit_test(dolphintreeviewtest TEST dolphintreeviewtest.cpp) target_link_libraries(dolphintreeviewtest dolphinprivate ${QT_QTCORE_LIBRARY} ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY}) + +kde4_add_unit_test(dolphindetailsviewtest TEST dolphindetailsviewtest.cpp testhelper.cpp ../views/zoomlevelinfo.cpp) +target_link_libraries(dolphindetailsviewtest dolphinprivate ${QT_QTCORE_LIBRARY} ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY}) diff --git a/src/tests/dolphindetailsviewtest.cpp b/src/tests/dolphindetailsviewtest.cpp new file mode 100644 index 000000000..dc5c94a6b --- /dev/null +++ b/src/tests/dolphindetailsviewtest.cpp @@ -0,0 +1,123 @@ +/*************************************************************************** + * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include + +#include "testhelper.h" + +#include "views/dolphindetailsview.h" +#include "views/dolphinview.h" +#include "views/dolphinmodel.h" +#include "views/dolphindirlister.h" +#include "views/dolphinsortfilterproxymodel.h" +#include "views/zoomlevelinfo.h" + +#include + +#include + +#include "kdebug.h" + +class DolphinDetailsViewTest : public QObject +{ + Q_OBJECT + +private slots: + + void initTestCase(); + void cleanupTestCase(); + + void bug234600_overlappingIconsWhenZooming(); + +private: + + TestHelper* m_helper; + DolphinView* m_view; +}; + +void DolphinDetailsViewTest::initTestCase() +{ + m_helper = new TestHelper; + m_view = m_helper->view(); +} + +void DolphinDetailsViewTest::cleanupTestCase() +{ + delete m_helper; +} + +/** + * When the icon size is changed, we have to make sure that the maximumSize given + * to KFileItemDelegate for rendering each item is updated correctly. If this is not + * done, the visualRects are clipped by the incorrect maximum size, and the icons + * may overlap, see + * + * https://bugs.kde.org/show_bug.cgi?id=234600 + */ + +void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming() +{ + QStringList files; + files << "a" << "b" << "c" << "d"; + m_helper->createFiles(files); + + m_view->setMode(DolphinView::DetailsView); + DolphinDetailsView* detailsView = qobject_cast(m_helper->itemView()); + QVERIFY(detailsView); + m_view->resize(400, 400); + m_view->show(); + QTest::qWaitForWindowShown(m_view); + + // We have to make sure that the view has loaded the directory before we start the test. + // TODO: This will be needed frequently. Maybe move to TestHelper. + QSignalSpy finished(m_view, SIGNAL(finishedPathLoading(const KUrl&))); + m_view->reload(); + while (finished.count() != 1) { + QTest::qWait(50); + } + + QModelIndex index0 = detailsView->model()->index(0, 0); + detailsView->setCurrentIndex(index0); + QCOMPARE(detailsView->currentIndex(), index0); + + // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...) + // should make sure that the bug is triggered. + int zoomLevel = ZoomLevelInfo::minimumLevel(); + m_view->setZoomLevel(zoomLevel); + + QModelIndex index1 = detailsView->model()->index(1, 0); + detailsView->setCurrentIndex(index1); + QCOMPARE(detailsView->currentIndex(), index1); + + // Increase the zoom level successively to the maximum. + while(zoomLevel < ZoomLevelInfo::maximumLevel()) { + zoomLevel++; + m_view->setZoomLevel(zoomLevel); + + //Check for each zoom level that the height of each item is at least the icon size. + QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel)); + } + + m_view->hide(); + m_helper->cleanupTestDir(); +} + +QTEST_KDEMAIN(DolphinDetailsViewTest, GUI) + +#include "dolphindetailsviewtest.moc" \ No newline at end of file diff --git a/src/tests/testhelper.cpp b/src/tests/testhelper.cpp new file mode 100644 index 000000000..3e663f762 --- /dev/null +++ b/src/tests/testhelper.cpp @@ -0,0 +1,122 @@ +/*************************************************************************** + * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "testhelper.h" + +#include "views/dolphinview.h" +#include "views/dolphinmodel.h" +#include "views/dolphindirlister.h" +#include "views/dolphinsortfilterproxymodel.h" + +#include + +#include +#include + +TestHelper::TestHelper() +{ + m_tempDir = new KTempDir; + Q_ASSERT(m_tempDir->exists()); + m_path = m_tempDir->name(); + m_dir = new QDir(m_path); + m_dirLister = new DolphinDirLister(); + m_dirLister->setAutoUpdate(true); + m_dolphinModel = new DolphinModel(); + m_dolphinModel->setDirLister(m_dirLister); + m_proxyModel = new DolphinSortFilterProxyModel(0); + m_proxyModel->setSourceModel(m_dolphinModel); + m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); + m_view = new DolphinView(0, KUrl(m_path), m_proxyModel); +} + +TestHelper::~TestHelper() +{ + delete m_view; + delete m_proxyModel; + // m_dolphinModel owns m_dirLister -> do not delete it here! + delete m_dolphinModel; + delete m_dir; + delete m_tempDir; +} + +QAbstractItemView* TestHelper::itemView () const +{ + return m_view->m_viewAccessor.itemView(); +} + + +KUrl TestHelper::testDirUrl() const +{ + return KUrl(m_path); +} + +void TestHelper::createFile(const QString& path, const QByteArray& data) +{ + QString absolutePath = path; + makePathAbsoluteAndCreateParents(absolutePath); + + QFile f(absolutePath); + f.open(QIODevice::WriteOnly); + f.write(data); + f.close(); + + Q_ASSERT(QFile::exists(absolutePath)); +} + +void TestHelper::createFiles(const QStringList& files) +{ + foreach(const QString& path, files) { + createFile(path); + } +} + +void TestHelper::createDir(const QString& path) +{ + QString absolutePath = path; + makePathAbsoluteAndCreateParents(absolutePath); + m_dir->mkdir(absolutePath); + + Q_ASSERT(QFile::exists(absolutePath)); +} + +void TestHelper::makePathAbsoluteAndCreateParents(QString& path) +{ + QFileInfo fileInfo(path); + if (!fileInfo.isAbsolute()) { + path = m_path + path; + fileInfo.setFile(path); + } + + const QDir dir = fileInfo.dir(); + if (!dir.exists()) { + createDir(dir.absolutePath()); + } + + Q_ASSERT(dir.exists()); +} + +void TestHelper::cleanupTestDir() +{ + delete m_tempDir; + m_tempDir = new KTempDir; + Q_ASSERT(m_tempDir->exists()); + m_path = m_tempDir->name(); + m_dir->setPath(m_path); + m_view->setUrl(m_path); +} diff --git a/src/tests/testhelper.h b/src/tests/testhelper.h new file mode 100644 index 000000000..a06b5dea1 --- /dev/null +++ b/src/tests/testhelper.h @@ -0,0 +1,88 @@ +/*************************************************************************** + * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef TESTHELPER_H +#define TESTHELPER_H + +#include + +class KTempDir; +class QAbstractItemView; +class QDir; +class DolphinDirLister; +class DolphinModel; +class DolphinSortFilterProxyModel; +class DolphinView; + +/* + * The class TestHelper aims to make writing Dolphin unit tests easier. + * It provides functionality that almost every unit test needs: setup of the DolphinView and + * easy creation of test files and subfolders in a temporary directory which is removed in + * the TestHelper destructor. + * + * TODO: TestHelper should also backup the DolphinSettings and restore them later! + */ + +class TestHelper +{ + +public: + + TestHelper(); + ~TestHelper(); + + DolphinView* view() const { return m_view; } + DolphinSortFilterProxyModel* proxyModel() const { return m_proxyModel; } + + // Returns the item view (icons, details, or columns) + QAbstractItemView* itemView () const; + + KUrl testDirUrl() const; + + /* + * The following functions create either a file, a list of files, or a directory. + * The paths may be absolute or relative to the test directory. Any missing parent + * directories will be created automatically. + */ + + void createFile(const QString& path, const QByteArray& data = QByteArray("test")); + void createFiles(const QStringList& files); + void createDir(const QString& path); + + /* + * Remove the test directory and create an empty one. + */ + + void cleanupTestDir(); + +private: + + void makePathAbsoluteAndCreateParents(QString& path); + + KTempDir* m_tempDir; + QString m_path; + QDir* m_dir; + DolphinDirLister* m_dirLister; + DolphinModel* m_dolphinModel; + DolphinSortFilterProxyModel* m_proxyModel; + DolphinView* m_view; + +}; + +#endif \ No newline at end of file diff --git a/src/views/dolphindetailsview.h b/src/views/dolphindetailsview.h index 67f7f2e3e..479683f66 100644 --- a/src/views/dolphindetailsview.h +++ b/src/views/dolphindetailsview.h @@ -62,6 +62,9 @@ public: */ QSet expandedUrls() const; +public: + virtual QRect visualRect(const QModelIndex& index) const; + protected: virtual bool event(QEvent* event); virtual QStyleOptionViewItem viewOptions() const; @@ -76,7 +79,6 @@ protected: virtual void wheelEvent(QWheelEvent* event); virtual void currentChanged(const QModelIndex& current, const QModelIndex& previous); virtual bool eventFilter(QObject* watched, QEvent* event); - virtual QRect visualRect(const QModelIndex& index) const; virtual bool acceptsDrop(const QModelIndex& index) const; protected slots: diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index 178e0ca6a..0c88d27ff 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -797,6 +797,9 @@ private: * slotDirListerCompleted(). */ QSet m_newFileNames; + + // For unit tests + friend class TestHelper; }; /// Allow using DolphinView::Mode in QVariant