From b1e69cae00a708aa50dbd03eca944e83a639d861 Mon Sep 17 00:00:00 2001 From: Frank Reininghaus Date: Fri, 29 Oct 2010 14:13:11 +0000 Subject: It's easier to put functionality that is used by many unit tests into a common base class. I took that idea from Nepomuk's unit tests in kdelibs. svn path=/trunk/KDE/kdebase/apps/; revision=1191051 --- src/tests/CMakeLists.txt | 2 +- src/tests/dolphindetailsviewtest.cpp | 23 ++----- src/tests/testbase.cpp | 122 +++++++++++++++++++++++++++++++++++ src/tests/testbase.h | 89 +++++++++++++++++++++++++ src/tests/testhelper.cpp | 122 ----------------------------------- src/tests/testhelper.h | 88 ------------------------- src/views/dolphinview.h | 2 +- 7 files changed, 218 insertions(+), 230 deletions(-) create mode 100644 src/tests/testbase.cpp create mode 100644 src/tests/testbase.h delete mode 100644 src/tests/testhelper.cpp delete mode 100644 src/tests/testhelper.h diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 411994f05..0bf57665b 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -4,5 +4,5 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/.. ${KDE4_INCLUDES} ) kde4_add_unit_test(dolphintreeviewtest TEST dolphintreeviewtest.cpp) target_link_libraries(dolphintreeviewtest dolphinprivate ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY}) -kde4_add_unit_test(dolphindetailsviewtest TEST dolphindetailsviewtest.cpp testhelper.cpp ../views/zoomlevelinfo.cpp) +kde4_add_unit_test(dolphindetailsviewtest TEST dolphindetailsviewtest.cpp testbase.cpp ../views/zoomlevelinfo.cpp) target_link_libraries(dolphindetailsviewtest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY}) diff --git a/src/tests/dolphindetailsviewtest.cpp b/src/tests/dolphindetailsviewtest.cpp index ab3006d9d..bf77830f7 100644 --- a/src/tests/dolphindetailsviewtest.cpp +++ b/src/tests/dolphindetailsviewtest.cpp @@ -19,7 +19,7 @@ #include -#include "testhelper.h" +#include "testbase.h" #include "views/dolphindetailsview.h" #include "views/dolphinview.h" @@ -34,21 +34,16 @@ #include "kdebug.h" -class DolphinDetailsViewTest : public QObject +class DolphinDetailsViewTest : public TestBase { Q_OBJECT private slots: void initTestCase(); - void cleanupTestCase(); void bug234600_overlappingIconsWhenZooming(); -private: - - TestHelper* m_helper; - DolphinView* m_view; }; void DolphinDetailsViewTest::initTestCase() @@ -56,14 +51,6 @@ void DolphinDetailsViewTest::initTestCase() // add time stamps to find origin of test failures due to timeout at // http://my.cdash.org/index.php?project=kdebase&date= qputenv("KDE_DEBUG_TIMESTAMP", QByteArray("1")); - - m_helper = new TestHelper; - m_view = m_helper->view(); -} - -void DolphinDetailsViewTest::cleanupTestCase() -{ - delete m_helper; } /** @@ -79,10 +66,10 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming() { QStringList files; files << "a" << "b" << "c" << "d"; - m_helper->createFiles(files); + createFiles(files); m_view->setMode(DolphinView::DetailsView); - DolphinDetailsView* detailsView = qobject_cast(m_helper->itemView()); + DolphinDetailsView* detailsView = qobject_cast(itemView()); QVERIFY(detailsView); m_view->resize(400, 400); m_view->show(); @@ -126,7 +113,7 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming() m_view->hide(); kDebug() << "Cleaning up test directory..."; - m_helper->cleanupTestDir(); + cleanupTestDir(); kDebug() << "Done."; } diff --git a/src/tests/testbase.cpp b/src/tests/testbase.cpp new file mode 100644 index 000000000..5945cd258 --- /dev/null +++ b/src/tests/testbase.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 "testbase.h" + +#include "views/dolphinview.h" +#include "views/dolphinmodel.h" +#include "views/dolphindirlister.h" +#include "views/dolphinsortfilterproxymodel.h" + +#include + +#include +#include + +TestBase::TestBase() +{ + 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); +} + +TestBase::~TestBase() +{ + 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* TestBase::itemView () const +{ + return m_view->m_viewAccessor.itemView(); +} + + +KUrl TestBase::testDirUrl() const +{ + return KUrl(m_path); +} + +void TestBase::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 TestBase::createFiles(const QStringList& files) +{ + foreach(const QString& path, files) { + createFile(path); + } +} + +void TestBase::createDir(const QString& path) +{ + QString absolutePath = path; + makePathAbsoluteAndCreateParents(absolutePath); + m_dir->mkdir(absolutePath); + + Q_ASSERT(QFile::exists(absolutePath)); +} + +void TestBase::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 TestBase::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/testbase.h b/src/tests/testbase.h new file mode 100644 index 000000000..121c230ca --- /dev/null +++ b/src/tests/testbase.h @@ -0,0 +1,89 @@ +/*************************************************************************** + * 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 TestBase 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 TestBase destructor. + * + * TODO: TestBase should also backup the DolphinSettings and restore them later! + */ + +class TestBase : public QObject +{ + Q_OBJECT + +public: + + TestBase(); + ~TestBase(); + + // 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(); + + // Make members that are accessed frequently by the derived test classes public + + DolphinDirLister* m_dirLister; + DolphinModel* m_dolphinModel; + DolphinSortFilterProxyModel* m_proxyModel; + DolphinView* m_view; + +private: + + KTempDir* m_tempDir; + QString m_path; + QDir* m_dir; + + void makePathAbsoluteAndCreateParents(QString& path); + +}; + +#endif \ No newline at end of file diff --git a/src/tests/testhelper.cpp b/src/tests/testhelper.cpp deleted file mode 100644 index 3e663f762..000000000 --- a/src/tests/testhelper.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/*************************************************************************** - * 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 deleted file mode 100644 index a06b5dea1..000000000 --- a/src/tests/testhelper.h +++ /dev/null @@ -1,88 +0,0 @@ -/*************************************************************************** - * 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/dolphinview.h b/src/views/dolphinview.h index 0c88d27ff..9d2032e9f 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -799,7 +799,7 @@ private: QSet m_newFileNames; // For unit tests - friend class TestHelper; + friend class TestBase; }; /// Allow using DolphinView::Mode in QVariant -- cgit v1.3