]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Dolphin unit test refactoring
authorFrank Reininghaus <frank78ac@googlemail.com>
Sun, 3 Apr 2011 13:31:27 +0000 (15:31 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Sun, 3 Apr 2011 13:32:12 +0000 (15:32 +0200)
This commit changes mainly two things:
1. The functionality related to the handling of temporary files and
folders is moved to a new class TestDir.
2. The setup of a temporary folder and of DolphinView instances is
now done in each unit test function. A benefit of this change is that
there are no unwanted interactions between different unit test functions
any more. Moreover, different test functions could now share a test
folder more easily.

Additionally, the reference arguments of all functions are changed to
pointers for consistency.

src/tests/CMakeLists.txt
src/tests/dolphindetailsviewtest.cpp
src/tests/dolphinviewtest_allviewmodes.cpp
src/tests/dolphinviewtest_allviewmodes.h
src/tests/dolphinviewtest_columns.cpp
src/tests/dolphinviewtest_details.cpp
src/tests/dolphinviewtest_icons.cpp
src/tests/testbase.cpp
src/tests/testbase.h
src/tests/testdir.cpp [new file with mode: 0644]
src/tests/testdir.h [new file with mode: 0644]

index 0556cd696cb7d1e5ef0379a537eb7f6854cd6cc9..5cb05d0096cc16bc362bb2727dd44b8eedb75d88 100644 (file)
@@ -2,7 +2,7 @@ set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
 include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_BUILD_DIR}/.. ${KDE4_INCLUDES} )
 
 # DolphinDetailsView
-kde4_add_unit_test(dolphindetailsviewtest TEST dolphindetailsviewtest.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
+kde4_add_unit_test(dolphindetailsviewtest TEST dolphindetailsviewtest.cpp testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
 target_link_libraries(dolphindetailsviewtest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
 
 # DolphinSearchBox
@@ -22,13 +22,13 @@ kde4_add_unit_test(dolphintreeviewtest TEST dolphintreeviewtest.cpp)
 target_link_libraries(dolphintreeviewtest dolphinprivate ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY})
 
 # DolphinView - columns
-kde4_add_unit_test(dolphinviewtest_columns TEST dolphinviewtest_columns.cpp dolphinviewtest_allviewmodes.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
+kde4_add_unit_test(dolphinviewtest_columns TEST dolphinviewtest_columns.cpp dolphinviewtest_allviewmodes.cpp testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
 target_link_libraries(dolphinviewtest_columns dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
 
 # DolphinView - details
-kde4_add_unit_test(dolphinviewtest_details TEST dolphinviewtest_details.cpp dolphinviewtest_allviewmodes.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
+kde4_add_unit_test(dolphinviewtest_details TEST dolphinviewtest_details.cpp dolphinviewtest_allviewmodes.cpp testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
 target_link_libraries(dolphinviewtest_details dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
 
 # DolphinView - icons
-kde4_add_unit_test(dolphinviewtest_icons TEST dolphinviewtest_icons.cpp dolphinviewtest_allviewmodes.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
+kde4_add_unit_test(dolphinviewtest_icons TEST dolphinviewtest_icons.cpp dolphinviewtest_allviewmodes.cpp testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
 target_link_libraries(dolphinviewtest_icons dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
index 49fbb629ff89cd8309b38af69e0a1a53b0ab9663..87a9d253db1693a1a67d779bb8670eb5599db74e 100644 (file)
@@ -20,6 +20,7 @@
 #include <qtest_kde.h>
 
 #include "testbase.h"
+#include "testdir.h"
 
 #include "views/dolphindetailsview.h"
 #include "views/dolphinview.h"
@@ -36,21 +37,6 @@ class DolphinDetailsViewTest : public TestBase
 
 private slots:
 
-    void init() {
-        m_view->setMode(DolphinView::DetailsView);
-        m_detailsView = qobject_cast<DolphinDetailsView*>(itemView());
-        QVERIFY(m_detailsView);
-        m_detailsView->setFoldersExpandable(true);
-        m_view->resize(400, 400);
-        m_view->show();
-        QTest::qWaitForWindowShown(m_view);
-    }
-
-    void cleanup() {
-        m_view->hide();
-        cleanupTestDir();
-    }
-
     void testExpandedUrls();
 
     void bug217447_shiftArrowSelection();
@@ -59,12 +45,28 @@ private slots:
 
 private:
 
-    QModelIndex proxyModelIndexForUrl(const KUrl& url) const {
-        const QModelIndex index = m_view->m_viewAccessor.m_dolphinModel->indexForUrl(url);
-        return m_view->m_viewAccessor.m_proxyModel->mapFromSource(index);
+    /**
+     * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits
+     * until loading the folder in the view is finished.
+     *
+     * Many unit tests need access to the internal DolphinDetailsView in DolphinView.
+     * Therefore, a pointer to the details view is returned by initView(DolphinView*).
+     */
+    DolphinDetailsView* initView(DolphinView* view) const {
+        view->setMode(DolphinView::DetailsView);
+        DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view));
+        detailsView->setFoldersExpandable(true);
+        view->resize(400, 400);
+        view->show();
+        QTest::qWaitForWindowShown(view);
+        reloadViewAndWait(view);
+        return detailsView;
     }
 
-    DolphinDetailsView* m_detailsView;
+    QModelIndex proxyModelIndexForUrl(const DolphinView* view, const KUrl& url) const {
+        const QModelIndex index = view->m_viewAccessor.m_dolphinModel->indexForUrl(url);
+        return view->m_viewAccessor.m_proxyModel->mapFromSource(index);
+    }
 };
 
 /**
@@ -75,8 +77,6 @@ private:
 
 void DolphinDetailsViewTest::testExpandedUrls()
 {
-    m_detailsView->setFoldersExpandable(true);
-
     QStringList files;
     QStringList subFolderNames;
     subFolderNames << "a" << "b" << "c";
@@ -89,27 +89,29 @@ void DolphinDetailsViewTest::testExpandedUrls()
         }
     }
 
-    createFiles(files);
-    reloadViewAndWait();
+    TestDir dir;
+    dir.createFiles(files);
+    DolphinView view(dir.url(), 0);
+    DolphinDetailsView* detailsView = initView(&view);
 
     // We start with an empty set of expanded URLs.
     QSet<KUrl> expectedExpandedUrls;
-    QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
+    QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
 
     // Every time we expand a folder, we have to wait until the view has finished loading
     // its contents before we can expand further subfolders. We keep track of the reloading
     // using a signal spy.
-    QSignalSpy spyFinishedPathLoading(m_view, SIGNAL(finishedPathLoading(const KUrl&)));
+    QSignalSpy spyFinishedPathLoading(&view, SIGNAL(finishedPathLoading(const KUrl&)));
 
     // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
     QStringList itemsToExpand;
     itemsToExpand << "b" << "b/a" << "b/a/c" << "b/c" << "c";
 
     foreach(const QString& item, itemsToExpand) {
-        KUrl url(m_path + item);
-        m_detailsView->expand(proxyModelIndexForUrl(url));
+        KUrl url(dir.name() + item);
+        detailsView->expand(proxyModelIndexForUrl(&view, url));
         expectedExpandedUrls += url;
-        QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
+        QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
 
         // Before we proceed, we have to make sure that the view has finished
         // loading the contents of the expanded folder.
@@ -124,10 +126,10 @@ void DolphinDetailsViewTest::testExpandedUrls()
     itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b";
 
     foreach(const QString& item, itemsToCollapse) {
-        KUrl url(m_path + item);
-        m_detailsView->collapse(proxyModelIndexForUrl(url));
+        KUrl url(dir.name() + item);
+        detailsView->collapse(proxyModelIndexForUrl(&view, url));
         expectedExpandedUrls -= url;
-        QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
+        QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
     }
 }
 
@@ -153,33 +155,35 @@ void DolphinDetailsViewTest::testExpandedUrls()
 
 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
 {
+    TestDir dir;
     for (int i = 0; i < 100; i++) {
-        createFile(QString("%1").arg(i));
+        dir.createFile(QString("%1").arg(i));
     }
-    reloadViewAndWait();
+    DolphinView view(dir.url(), 0);
+    DolphinDetailsView* detailsView = initView(&view);
 
     // Select the first item
-    QModelIndex index0 = m_detailsView->model()->index(0, 0);
-    m_detailsView->setCurrentIndex(index0);
-    QCOMPARE(m_detailsView->currentIndex(), index0);
+    QModelIndex index0 = detailsView->model()->index(0, 0);
+    detailsView->setCurrentIndex(index0);
+    QCOMPARE(detailsView->currentIndex(), index0);
 
     // Before we test Shift-selection, we verify that the root cause is fixed a bit more
     // directly: we check that passing the corners or the center of an item's visualRect
     // to itemAt() returns the item (and not an invalid model index).
-    QRect rect = m_detailsView->visualRect(index0);
-    QCOMPARE(m_detailsView->indexAt(rect.center()), index0);
-    QCOMPARE(m_detailsView->indexAt(rect.topLeft()), index0);
-    QCOMPARE(m_detailsView->indexAt(rect.topRight()), index0);
-    QCOMPARE(m_detailsView->indexAt(rect.bottomLeft()), index0);
-    QCOMPARE(m_detailsView->indexAt(rect.bottomRight()), index0);
+    QRect rect = detailsView->visualRect(index0);
+    QCOMPARE(detailsView->indexAt(rect.center()), index0);
+    QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
+    QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
+    QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
+    QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
 
     // Another way to test this is to Ctrl-click the center of the visualRect.
     // The selection state of the item should be toggled.
-    m_detailsView->clearSelection();
-    QItemSelectionModel* selectionModel = m_detailsView->selectionModel();
+    detailsView->clearSelection();
+    QItemSelectionModel* selectionModel = detailsView->selectionModel();
     QCOMPARE(selectionModel->selectedIndexes().count(), 0);
 
-    QTest::mouseClick(m_detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
+    QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
     QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
     QCOMPARE(selectedIndexes.count(), 1);
     QVERIFY(selectedIndexes.contains(index0));
@@ -190,9 +194,9 @@ void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
     int current = 1;
 
     while (current < 100) {
-        QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
-        QModelIndex currentIndex = m_detailsView->model()->index(current, 0);
-        QCOMPARE(m_detailsView->currentIndex(), currentIndex);
+        QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
+        QModelIndex currentIndex = detailsView->model()->index(current, 0);
+        QCOMPARE(detailsView->currentIndex(), currentIndex);
 
         selectedIndexes = selectionModel->selectedIndexes();
         QCOMPARE(selectedIndexes.count(), current + 1);
@@ -216,34 +220,36 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
     QStringList files;
     files << "a" << "b" << "c" << "d";
 
-    createFiles(files);
-    reloadViewAndWait();
+    TestDir dir;
+    dir.createFiles(files);
+    DolphinView view(dir.url(), 0);
+    DolphinDetailsView* detailsView = initView(&view);
 
-    QModelIndex index0 = m_detailsView->model()->index(0, 0);
-    m_detailsView->setCurrentIndex(index0);
-    QCOMPARE(m_detailsView->currentIndex(), index0);
+    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 zoomLevelBackup = m_view->zoomLevel();
+    int zoomLevelBackup = view.zoomLevel();
     int zoomLevel = ZoomLevelInfo::minimumLevel();
-    m_view->setZoomLevel(zoomLevel);
+    view.setZoomLevel(zoomLevel);
 
-    QModelIndex index1 = m_detailsView->model()->index(1, 0);
-    m_detailsView->setCurrentIndex(index1);
-    QCOMPARE(m_detailsView->currentIndex(), index1);
+    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);
-        QCOMPARE(m_view->zoomLevel(), zoomLevel);
+        view.setZoomLevel(zoomLevel);
+        QCOMPARE(view.zoomLevel(), zoomLevel);
 
         //Check for each zoom level that the height of each item is at least the icon size.
-        QVERIFY(m_detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
+        QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
     }
 
-    m_view->setZoomLevel(zoomLevelBackup);
+    view.setZoomLevel(zoomLevelBackup);
 }
 
 /**
@@ -257,20 +263,22 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
  */
 
 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
+    TestDir dir;
     QString name;
     for (int i = 0; i < 20; i++) {
         name += "mmmmmmmmmm";
-        createFile(name);
+        dir.createFile(name);
     }
-    reloadViewAndWait();
+    DolphinView view(dir.url(), 0);
+    DolphinDetailsView* detailsView = initView(&view);
 
     // Select the first item
-    QModelIndex index0 = m_detailsView->model()->index(0, 0);
-    m_detailsView->setCurrentIndex(index0);
-    QCOMPARE(m_detailsView->currentIndex(), index0);
-    QVERIFY(m_detailsView->visualRect(index0).width() < m_detailsView->columnWidth(DolphinModel::Name));
+    QModelIndex index0 = detailsView->model()->index(0, 0);
+    detailsView->setCurrentIndex(index0);
+    QCOMPARE(detailsView->currentIndex(), index0);
+    QVERIFY(detailsView->visualRect(index0).width() < detailsView->columnWidth(DolphinModel::Name));
 
-    QItemSelectionModel* selectionModel = m_detailsView->selectionModel();
+    QItemSelectionModel* selectionModel = detailsView->selectionModel();
     QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
     QCOMPARE(selectedIndexes.count(), 1);
     QVERIFY(selectedIndexes.contains(index0));
@@ -278,10 +286,10 @@ void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
     // Move down successively using the "Down" key and check that current item
     // and selection are as expected.
     for (int i = 0; i < 19; i++) {
-        QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
-        QModelIndex currentIndex = m_detailsView->model()->index(i + 1, 0);
-        QCOMPARE(m_detailsView->currentIndex(), currentIndex);
-        QVERIFY(m_detailsView->visualRect(currentIndex).width() <= m_detailsView->columnWidth(DolphinModel::Name));
+        QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
+        QModelIndex currentIndex = detailsView->model()->index(i + 1, 0);
+        QCOMPARE(detailsView->currentIndex(), currentIndex);
+        QVERIFY(detailsView->visualRect(currentIndex).width() <= detailsView->columnWidth(DolphinModel::Name));
         selectedIndexes = selectionModel->selectedIndexes();
         QCOMPARE(selectedIndexes.count(), 1);
         QVERIFY(selectedIndexes.contains(currentIndex));
index 8d7a5bd0d73d2f279573a777ce905861b4d867b8..00f5604cd15008d0c1f1cc9d338a0bb84129f20a 100644 (file)
@@ -1,4 +1,4 @@
-/*****************************************************************************
+/****************************************************************************
  *   Copyright (C) 2010-2011 by Frank Reininghaus (frank78ac@googlemail.com) *
  *                                                                           *
  *   This program is free software; you can redistribute it and/or modify    *
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA              *
  *****************************************************************************/
 
+#include <kdebug.h>
+
 #include "dolphinviewtest_allviewmodes.h"
 
 #include <qtest_kde.h>
 
-#include "testbase.h"
+#include "testdir.h"
 
 #include "views/dolphinview.h"
 #include "views/dolphinmodel.h"
@@ -39,26 +41,6 @@ DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
     qRegisterMetaType<KFileItemList>("KFileItemList");
 }
 
-void DolphinViewTest_AllViewModes::init() {
-    if (mode() == DolphinView::ColumnView) {
-        // In Columns View mode, we need to create a new DolphinView after each
-        // test to make the file-related tests after the first one pass.
-        // TODO: Try to find out if there is a hidden bug in the Columns View that causes this.
-        delete m_view;
-        m_view = new DolphinView(KUrl(m_path), 0);
-    }
-    m_view->setMode(mode());
-    QVERIFY(verifyCorrectViewMode());
-    m_view->resize(200, 300);
-    m_view->show();
-    QTest::qWaitForWindowShown(m_view);
-}
-
-void DolphinViewTest_AllViewModes::cleanup() {
-    m_view->hide();
-    cleanupTestDir();
-}
-
 /**
  * testSelection() checks the basic selection functionality of DolphinView, including:
  *
@@ -75,55 +57,57 @@ void DolphinViewTest_AllViewModes::cleanup() {
 Q_DECLARE_METATYPE(KFileItemList)
 
 void DolphinViewTest_AllViewModes::testSelection() {
+    TestDir dir;    
     const int totalItems = 50;
-
     for (int i = 0; i < totalItems; i++) {
-        createFile(QString("%1").arg(i));
+        dir.createFile(QString("%1").arg(i));
     }
-    reloadViewAndWait();
+
+    DolphinView view(dir.url(), 0);
+    QAbstractItemView* itemView = initView(&view);
 
     // Start with an empty selection
-    m_view->clearSelection();
+    view.clearSelection();
 
-    QCOMPARE(m_view->selectedItems().count(), 0);
-    QCOMPARE(m_view->selectedItemsCount(), 0);
-    QVERIFY(!m_view->hasSelection());
+    QCOMPARE(view.selectedItems().count(), 0);
+    QCOMPARE(view.selectedItemsCount(), 0);
+    QVERIFY(!view.hasSelection());
 
     // First some simple tests where either all or no items are selected
-    m_view->selectAll();
-    verifySelectedItemsCount(totalItems);
+    view.selectAll();
+    verifySelectedItemsCount(&view, totalItems);
 
-    m_view->invertSelection();
-    verifySelectedItemsCount(0);
+    view.invertSelection();
+    verifySelectedItemsCount(&view, 0);
 
-    m_view->invertSelection();
-    verifySelectedItemsCount(totalItems);
+    view.invertSelection();
+    verifySelectedItemsCount(&view, totalItems);
 
-    m_view->clearSelection();
-    verifySelectedItemsCount(0);
+    view.clearSelection();
+    verifySelectedItemsCount(&view, 0);
 
     // Now we select individual items using mouse clicks
-    QModelIndex index = itemView()->model()->index(2, 0);
-    itemView()->scrollTo(index);
-    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
-    verifySelectedItemsCount(1);
+    QModelIndex index = itemView->model()->index(2, 0);
+    itemView->scrollTo(index);
+    QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView->visualRect(index).center());
+    verifySelectedItemsCount(&view, 1);
 
-    index = itemView()->model()->index(totalItems - 5, 0);
-    itemView()->scrollTo(index);
-    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView()->visualRect(index).center());
-    verifySelectedItemsCount(2);
+    index = itemView->model()->index(totalItems - 5, 0);
+    itemView->scrollTo(index);
+    QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView->visualRect(index).center());
+    verifySelectedItemsCount(&view, 2);
 
-    index = itemView()->model()->index(totalItems - 2, 0);
-    itemView()->scrollTo(index);
-    QTest::mouseClick(itemView()->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView()->visualRect(index).center());
-    verifySelectedItemsCount(5);
+    index = itemView->model()->index(totalItems - 2, 0);
+    itemView->scrollTo(index);
+    QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView->visualRect(index).center());
+    verifySelectedItemsCount(&view, 5);
 
-    m_view->invertSelection();
-    verifySelectedItemsCount(totalItems - 5);
+    view.invertSelection();
+    verifySelectedItemsCount(&view, totalItems - 5);
 
     // Pressing Esc should clear the selection
-    QTest::keyClick(itemView()->viewport(), Qt::Key_Escape);
-    verifySelectedItemsCount(0);
+    QTest::keyClick(itemView->viewport(), Qt::Key_Escape);
+    verifySelectedItemsCount(&view, 0);
 }
 
 /**
@@ -135,90 +119,92 @@ void DolphinViewTest_AllViewModes::testViewPropertySettings()
     // Create some files with different sizes and modification times to check the different sorting options
     QDateTime now = QDateTime::currentDateTime();
 
-    createFile("a", "A file", now.addDays(-3));
-    createFile("b", "A larger file", now.addDays(0));
-    createDir("c", now.addDays(-2));
-    createFile("d", "The largest file in this directory", now.addDays(-1));
-    createFile("e", "An even larger file", now.addDays(-4));
-    createFile(".f");
+    TestDir dir;
+    dir.createFile("a", "A file", now.addDays(-3));
+    dir.createFile("b", "A larger file", now.addDays(0));
+    dir.createDir("c", now.addDays(-2));
+    dir.createFile("d", "The largest file in this directory", now.addDays(-1));
+    dir.createFile("e", "An even larger file", now.addDays(-4));
+    dir.createFile(".f");
 
-    reloadViewAndWait();
+    DolphinView view(dir.url(), 0);
+    initView(&view);
 
     // First set all settings to the default.
-    m_view->setSorting(DolphinView::SortByName);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
+    view.setSorting(DolphinView::SortByName);
+    QCOMPARE(view.sorting(), DolphinView::SortByName);
 
-    m_view->setSortOrder(Qt::AscendingOrder);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
+    view.setSortOrder(Qt::AscendingOrder);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
 
-    m_view->setSortFoldersFirst(true);
-    QVERIFY(m_view->sortFoldersFirst());
+    view.setSortFoldersFirst(true);
+    QVERIFY(view.sortFoldersFirst());
 
-    m_view->setShowPreview(false);
-    QVERIFY(!m_view->showPreview());
+    view.setShowPreview(false);
+    QVERIFY(!view.showPreview());
 
-    m_view->setShowHiddenFiles(false);
-    QVERIFY(!m_view->showHiddenFiles());
+    view.setShowHiddenFiles(false);
+    QVERIFY(!view.showHiddenFiles());
 
     /** Check that the sort order is correct for different kinds of settings */
 
     // Sort by Name, ascending
-    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "d" << "e");
+    QCOMPARE(view.sorting(), DolphinView::SortByName);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "a" << "b" << "d" << "e");
 
     // Sort by Name, descending
-    m_view->setSortOrder(Qt::DescendingOrder);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
-    QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "e" << "d" << "b" << "a");
+    view.setSortOrder(Qt::DescendingOrder);
+    QCOMPARE(view.sorting(), DolphinView::SortByName);
+    QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "e" << "d" << "b" << "a");
 
     // Sort by Size, descending
-    m_view->setSorting(DolphinView::SortBySize);
-    QCOMPARE(m_view->sorting(), DolphinView::SortBySize);
-    QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "d" << "e" << "b" << "a");
+    view.setSorting(DolphinView::SortBySize);
+    QCOMPARE(view.sorting(), DolphinView::SortBySize);
+    QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "d" << "e" << "b" << "a");
 
     // Sort by Size, ascending
-    m_view->setSortOrder(Qt::AscendingOrder);
-    QCOMPARE(m_view->sorting(), DolphinView::SortBySize);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "e" << "d");
+    view.setSortOrder(Qt::AscendingOrder);
+    QCOMPARE(view.sorting(), DolphinView::SortBySize);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "a" << "b" << "e" << "d");
 
     // Sort by Date, ascending
-    m_view->setSorting(DolphinView::SortByDate);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByDate);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "e" << "a" << "d" << "b");
+    view.setSorting(DolphinView::SortByDate);
+    QCOMPARE(view.sorting(), DolphinView::SortByDate);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "e" << "a" << "d" << "b");
 
     // Sort by Date, descending
-    m_view->setSortOrder(Qt::DescendingOrder);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByDate);
-    QCOMPARE(m_view->sortOrder(), Qt::DescendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "c" << "b" << "d" << "a" << "e");
+    view.setSortOrder(Qt::DescendingOrder);
+    QCOMPARE(view.sorting(), DolphinView::SortByDate);
+    QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "c" << "b" << "d" << "a" << "e");
 
     // Disable "Sort Folders First"
-    m_view->setSortFoldersFirst(false);
-    QVERIFY(!m_view->sortFoldersFirst());
-    QCOMPARE(viewItems(), QStringList()<< "b" << "d" << "c"  << "a" << "e");
+    view.setSortFoldersFirst(false);
+    QVERIFY(!view.sortFoldersFirst());
+    QCOMPARE(viewItems(&view), QStringList()<< "b" << "d" << "c"  << "a" << "e");
 
     // Try again with Sort by Name, ascending
-    m_view->setSorting(DolphinView::SortByName);
-    m_view->setSortOrder(Qt::AscendingOrder);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
-    QCOMPARE(viewItems(), QStringList() << "a" << "b" << "c" << "d" << "e");
+    view.setSorting(DolphinView::SortByName);
+    view.setSortOrder(Qt::AscendingOrder);
+    QCOMPARE(view.sorting(), DolphinView::SortByName);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(viewItems(&view), QStringList() << "a" << "b" << "c" << "d" << "e");
 
     // Show hidden files. This triggers the dir lister
     // -> we have to wait until loading the hidden files is finished
-    m_view->setShowHiddenFiles(true);
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
-    QVERIFY(m_view->showHiddenFiles());
-    QCOMPARE(viewItems(), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
+    view.setShowHiddenFiles(true);
+    QVERIFY(QTest::kWaitForSignal(&view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    QVERIFY(view.showHiddenFiles());
+    QCOMPARE(viewItems(&view), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
 
     // Previews
-    m_view->setShowPreview(true);
-    QVERIFY(m_view->showPreview());
+    view.setShowPreview(true);
+    QVERIFY(view.showPreview());
 
     // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
 }
@@ -229,71 +215,73 @@ void DolphinViewTest_AllViewModes::testViewPropertySettings()
 
 void DolphinViewTest_AllViewModes::testZoomLevel()
 {
-    createFiles(QStringList() << "a" << "b");
-    reloadViewAndWait();
+    TestDir dir;
+    dir.createFiles(QStringList() << "a" << "b");
+    DolphinView view(dir.url(), 0);
+    QAbstractItemView* itemView = initView(&view);
 
-    m_view->setShowPreview(false);
-    QVERIFY(!m_view->showPreview());
+    view.setShowPreview(false);
+    QVERIFY(!view.showPreview());
 
-    int zoomLevelBackup = m_view->zoomLevel();
+    int zoomLevelBackup = view.zoomLevel();
 
     int zoomLevel = ZoomLevelInfo::minimumLevel();
-    m_view->setZoomLevel(zoomLevel);
-    QCOMPARE(m_view->zoomLevel(), zoomLevel);
+    view.setZoomLevel(zoomLevel);
+    QCOMPARE(view.zoomLevel(), zoomLevel);
 
     // Increase the zoom level successively to the maximum.
     while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
         zoomLevel++;
-        m_view->setZoomLevel(zoomLevel);
-        QCOMPARE(m_view->zoomLevel(), zoomLevel);
+        view.setZoomLevel(zoomLevel);
+        QCOMPARE(view.zoomLevel(), zoomLevel);
     }
 
     // Try setting a zoom level larger than the maximum
-    m_view->setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+    view.setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
 
     // Turn previews on and try setting a zoom level smaller than the minimum
-    m_view->setShowPreview(true);
-    QVERIFY(m_view->showPreview());
-    m_view->setZoomLevel(ZoomLevelInfo::minimumLevel() - 1);
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+    view.setShowPreview(true);
+    QVERIFY(view.showPreview());
+    view.setZoomLevel(ZoomLevelInfo::minimumLevel() - 1);
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
 
     // Turn previews off again and check that the zoom level is restored
-    m_view->setShowPreview(false);
-    QVERIFY(!m_view->showPreview());
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+    view.setShowPreview(false);
+    QVERIFY(!view.showPreview());
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
 
     // Change the zoom level using Ctrl+mouse wheel
-    QModelIndex index = itemView()->model()->index(0, 0);
-    itemView()->scrollTo(index);
+    QModelIndex index = itemView->model()->index(0, 0);
+    itemView->scrollTo(index);
 
-    while (m_view->zoomLevel() > ZoomLevelInfo::minimumLevel()) {
-        int oldZoomLevel = m_view->zoomLevel();
-        QWheelEvent wheelEvent(itemView()->visualRect(index).center(), -1, Qt::NoButton, Qt::ControlModifier);
-        bool wheelEventReceived = qApp->notify(itemView()->viewport(), &wheelEvent);
+    while (view.zoomLevel() > ZoomLevelInfo::minimumLevel()) {
+        int oldZoomLevel = view.zoomLevel();
+        QWheelEvent wheelEvent(itemView->visualRect(index).center(), -1, Qt::NoButton, Qt::ControlModifier);
+        bool wheelEventReceived = qApp->notify(itemView->viewport(), &wheelEvent);
         QVERIFY(wheelEventReceived);
-        QVERIFY(m_view->zoomLevel() < oldZoomLevel);
+        QVERIFY(view.zoomLevel() < oldZoomLevel);
     }
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
 
-    while (m_view->zoomLevel() < ZoomLevelInfo::maximumLevel()) {
-        int oldZoomLevel = m_view->zoomLevel();
-        QWheelEvent wheelEvent(itemView()->visualRect(index).center(), 1, Qt::NoButton, Qt::ControlModifier);
-        bool wheelEventReceived = qApp->notify(itemView()->viewport(), &wheelEvent);
+    while (view.zoomLevel() < ZoomLevelInfo::maximumLevel()) {
+        int oldZoomLevel = view.zoomLevel();
+        QWheelEvent wheelEvent(itemView->visualRect(index).center(), 1, Qt::NoButton, Qt::ControlModifier);
+        bool wheelEventReceived = qApp->notify(itemView->viewport(), &wheelEvent);
         QVERIFY(wheelEventReceived);
-        QVERIFY(m_view->zoomLevel() > oldZoomLevel);
+        QVERIFY(view.zoomLevel() > oldZoomLevel);
     }
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::maximumLevel());
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
 
     // Turn previews on again and check that the zoom level is restored
-    m_view->setShowPreview(true);
-    QVERIFY(m_view->showPreview());
-    QCOMPARE(m_view->zoomLevel(), ZoomLevelInfo::minimumLevel());
+    view.setShowPreview(true);
+    QVERIFY(view.showPreview());
+    QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
 
     // Restore the initial state
-    m_view->setZoomLevel(zoomLevelBackup);
-    m_view->setShowPreview(false);
-    m_view->setZoomLevel(zoomLevelBackup);
+    view.setZoomLevel(zoomLevelBackup);
+    view.setShowPreview(false);
+    view.setZoomLevel(zoomLevelBackup);
 }
 
 /**
@@ -308,60 +296,67 @@ void DolphinViewTest_AllViewModes::testZoomLevel()
 void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
 {
     const int totalItems = 50;
-
+    TestDir dir;
     for (int i = 0; i < totalItems; i++) {
-        createFile(QString("%1").arg(i));
+        dir.createFile(QString("%1").arg(i));
     }
-    createDir("51");
-    reloadViewAndWait();
+    dir.createDir("51");
+    DolphinView view(dir.url(), 0);
+    initView(&view);
 
     // Set sorting settings to the default to make sure that the item positions are reproducible.
-    m_view->setSorting(DolphinView::SortByName);
-    QCOMPARE(m_view->sorting(), DolphinView::SortByName);
-    m_view->setSortOrder(Qt::AscendingOrder);
-    QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
-
-    const QModelIndex index45 = itemView()->model()->index(45, 0);
-    itemView()->scrollTo(index45);
-    itemView()->setCurrentIndex(index45);
-    const int scrollPosX = itemView()->horizontalScrollBar()->value();
-    const int scrollPosY = itemView()->verticalScrollBar()->value();
+    view.setSorting(DolphinView::SortByName);
+    QCOMPARE(view.sorting(), DolphinView::SortByName);
+    view.setSortOrder(Qt::AscendingOrder);
+    QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
+
+    const QModelIndex index45 = itemView(&view)->model()->index(45, 0);
+    itemView(&view)->scrollTo(index45);
+    itemView(&view)->setCurrentIndex(index45);
+    const int scrollPosX = itemView(&view)->horizontalScrollBar()->value();
+    const int scrollPosY = itemView(&view)->verticalScrollBar()->value();
+    QTest::qWait(2000);
 
     // Save the view state
     QByteArray viewState;
     QDataStream saveStream(&viewState, QIODevice::WriteOnly);
-    m_view->saveState(saveStream);
+    view.saveState(saveStream);
 
-    // Change the URL and then go back
-    m_view->setUrl(m_path + "/51");
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    // Change the URL
+    view.setUrl(dir.name() + "51");
+    QVERIFY(QTest::kWaitForSignal(&view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
     qApp->sendPostedEvents();
 
-    m_view->setUrl(m_path);
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    // Go back, but do not call DolphinView::restoreState()
+    view.setUrl(dir.url());
+    QVERIFY(QTest::kWaitForSignal(&view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
     qApp->sendPostedEvents();
 
-    // Verify that the view is scrolled to top and that item 45 is not the current item
-    QVERIFY(itemView()->currentIndex() != index45);
-    QCOMPARE(itemView()->horizontalScrollBar()->value(), 0);
-    QCOMPARE(itemView()->verticalScrollBar()->value(), 0);
+    // Verify that the view is scrolled to top-left corner and that item 45 is not the current item.
+    // Note that the vertical position of the columns view might not be zero -> skip that part
+    // of the check in this case.
+    QVERIFY(itemView(&view)->currentIndex() != index45);
+    QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), 0);
+    if (mode() != DolphinView::ColumnView) {
+        QCOMPARE(itemView(&view)->verticalScrollBar()->value(), 0);
+    }
 
     // Change the URL again
-    m_view->setUrl(m_path + "/51");
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    view.setUrl(dir.name() + "51");
+    QVERIFY(QTest::kWaitForSignal(&view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
     qApp->sendPostedEvents();
 
     // Check that the current item and scroll position are correct if DolphinView::restoreState()
     // is called after the URL change
-    m_view->setUrl(m_path);
+    view.setUrl(dir.url());
     QDataStream restoreStream(viewState);
-    m_view->restoreState(restoreStream);
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
+    view.restoreState(restoreStream);
+    QVERIFY(QTest::kWaitForSignal(&view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
     qApp->sendPostedEvents();
 
-    QCOMPARE(itemView()->currentIndex(), index45);
-    QCOMPARE(itemView()->horizontalScrollBar()->value(), scrollPosX);
-    QCOMPARE(itemView()->verticalScrollBar()->value(), scrollPosY);
+    QCOMPARE(itemView(&view)->currentIndex(), index45);
+    QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
+    QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
 }
 
 /**
@@ -375,7 +370,10 @@ void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
 
 void DolphinViewTest_AllViewModes::testKeyboardFocus()
 {
-    const DolphinView::Mode mode = m_view->mode();
+    TestDir dir;
+    dir.createFiles(QStringList() << "a" << "b");
+    DolphinView view(dir.url(), 0);
+    initView(&view);
 
     // Move keyboard focus to another widget. To see that this is needed, run only this test,
     // i.e., pass 'testKeyboardFocus' as a parameter on the command line.
@@ -384,15 +382,24 @@ void DolphinViewTest_AllViewModes::testKeyboardFocus()
     QTest::qWaitForWindowShown(&widget);
     widget.setFocus();
 
-    QVERIFY(!m_view->hasFocus());
+    QVERIFY(!view.hasFocus());
 
     // Switch view modes and verify that the view does not get the focus back
     for (int i = 0; i <= DolphinView::MaxModeEnum; ++i) {
-        m_view->setMode(static_cast<DolphinView::Mode>(i));
-        QVERIFY(!m_view->hasFocus());
+        view.setMode(static_cast<DolphinView::Mode>(i));
+        QVERIFY(!view.hasFocus());
     }
+}
 
-    m_view->setMode(mode);
+QAbstractItemView* DolphinViewTest_AllViewModes::initView(DolphinView* view) const
+{
+    view->setMode(mode());
+    view->resize(200, 300);
+    view->show();
+    QTest::qWaitForWindowShown(view);
+    Q_ASSERT(verifyCorrectViewMode(view));
+    reloadViewAndWait(view);
+    return itemView(view);
 }
 
 /**
@@ -400,20 +407,20 @@ void DolphinViewTest_AllViewModes::testKeyboardFocus()
  * signal is received and checks that the selection state of the view is as expected.
  */
 
-void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
+void DolphinViewTest_AllViewModes::verifySelectedItemsCount(DolphinView* view, int itemsCount) const
 {
-    QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)), 500));
+    QSignalSpy spySelectionChanged(view, SIGNAL(selectionChanged(const KFileItemList&)));
+    QVERIFY(QTest::kWaitForSignal(view, SIGNAL(selectionChanged(const KFileItemList&)), 500));
 
-    QCOMPARE(m_view->selectedItems().count(), itemsCount);
-    QCOMPARE(m_view->selectedItemsCount(), itemsCount);
+    QCOMPARE(view->selectedItems().count(), itemsCount);
+    QCOMPARE(view->selectedItemsCount(), itemsCount);
     QCOMPARE(spySelectionChanged.count(), 1);
     QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
     if (itemsCount) {
-        QVERIFY(m_view->hasSelection());
+        QVERIFY(view->hasSelection());
     }
     else {
-        QVERIFY(!m_view->hasSelection());
+        QVERIFY(!view->hasSelection());
     }
 }
 
index c8ea6100299cbb87de5cdf005ce4ddf1d85b279d..940ebbc1d3edc8f891741c201aec725a554b56dc 100644 (file)
@@ -45,9 +45,6 @@ public:
 
 private slots:
 
-    void init();
-    void cleanup();
-
     void testSelection();
     void testViewPropertySettings();
     void testZoomLevel();
@@ -55,19 +52,28 @@ private slots:
 
     void testKeyboardFocus();
 
-public:
+private:
+
+    /**
+     * Sets the correct view mode, shows the view on the screen, and waits until loading the
+     * folder in the view is finished.
+     *
+     * Many unit tests need access to DolphinVie's internal item view (icons, details, or columns).
+     * Therefore, a pointer to the item view is returned by initView(DolphinView*).
+     */
+    QAbstractItemView* initView(DolphinView* view) const;
 
     /** Returns the view mode (Icons, Details, Columns) to be used in the test. */
     virtual DolphinView::Mode mode() const = 0;
 
     /** Should return true if the view mode is correct. */
-    virtual bool verifyCorrectViewMode() const = 0;
+    virtual bool verifyCorrectViewMode(const DolphinView* view) const = 0;
 
     /**
      * Waits for the DolphinView's selectionChanged(const KFileItemList&) to be emitted
      * and verifies that the number of selected items is as expected.
      */
-    void verifySelectedItemsCount(int) const;
+    void verifySelectedItemsCount(DolphinView* view, int itemsCount) const;
 
 };
 
index 398446fca32828aeec278c849128bf5583c13927..40b88a2230ddf9b86c313cb79429742c13fac00f 100644 (file)
@@ -31,8 +31,8 @@ public:
         return DolphinView::ColumnView;
     }
 
-    virtual bool verifyCorrectViewMode() const {
-        return (m_view->mode() == DolphinView::ColumnView);
+    virtual bool verifyCorrectViewMode(const DolphinView* view) const {
+        return (view->mode() == DolphinView::ColumnView);
     }
 
 };
index c3c950a6af540f93741d485e96cb64c749073fa4..0034afd90c86db9308616d1a21ee9475b0222f6e 100644 (file)
@@ -31,8 +31,8 @@ public:
         return DolphinView::DetailsView;
     }
 
-    virtual bool verifyCorrectViewMode() const {
-        return (m_view->mode() == DolphinView::DetailsView);
+    virtual bool verifyCorrectViewMode(const DolphinView* view) const {
+        return (view->mode() == DolphinView::DetailsView);
     }
 
 };
index e90e31cfa9c74f47152c6fb3cb53f8f7edfd77ff..5f928f40317a2451eb98ae459911252105bc7ae3 100644 (file)
@@ -31,8 +31,8 @@ public:
         return DolphinView::IconsView;
     }
 
-    virtual bool verifyCorrectViewMode() const {
-        return (m_view->mode() == DolphinView::IconsView);
+    virtual bool verifyCorrectViewMode(const DolphinView* view) const {
+        return (view->mode() == DolphinView::IconsView);
     }
 
 };
index a0031d0f5e22e458c95b2d8e93205625392df235..59c40fb947a0510475d267f1e75cf079ae289370 100644 (file)
 #include "views/dolphindirlister.h"
 #include "views/dolphinsortfilterproxymodel.h"
 
-#include <KTempDir>
-
-#include <QDir>
 #include <QAbstractItemView>
 
-#include <KDebug>
-
-#ifdef Q_OS_UNIX
-#include <utime.h>
-#else
-#include <sys/utime.h>
-#endif
-
-TestBase::TestBase()
-{
-    m_tempDir = new KTempDir;
-    Q_ASSERT(m_tempDir->exists());
-    m_path = m_tempDir->name();
-    m_dir = new QDir(m_path);
-    m_view = new DolphinView(KUrl(m_path), 0);
-}
-
-TestBase::~TestBase()
-{
-    delete m_view;
-    delete m_dir;
-    delete m_tempDir;
-}
-
-QAbstractItemView* TestBase::itemView() const
-{
-    return m_view->m_viewAccessor.itemView();
-}
-
-void TestBase::reloadViewAndWait()
-{
-    m_view->reload();
-    QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
-}
-
-KUrl TestBase::testDirUrl() const
+QAbstractItemView* TestBase::itemView(const DolphinView* view)
 {
-    return KUrl(m_path);
+    return view->m_viewAccessor.itemView();
 }
 
-/** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
-
-static void setTimeStamp(const QString& path, const QDateTime& mtime)
+void TestBase::reloadViewAndWait(DolphinView* view)
 {
-#ifdef Q_OS_UNIX
-    struct utimbuf utbuf;
-    utbuf.actime = mtime.toTime_t();
-    utbuf.modtime = utbuf.actime;
-    utime(QFile::encodeName(path), &utbuf);
-#elif defined(Q_OS_WIN)
-    struct _utimbuf utbuf;
-    utbuf.actime = mtime.toTime_t();
-    utbuf.modtime = utbuf.actime;
-    _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
-#endif
+    view->reload();
+    QVERIFY(QTest::kWaitForSignal(view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
 }
 
-void TestBase::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
-{
-    QString absolutePath = path;
-    makePathAbsoluteAndCreateParents(absolutePath);
-
-    QFile f(absolutePath);
-    f.open(QIODevice::WriteOnly);
-    f.write(data);
-    f.close();
-
-    if (time.isValid()) {
-        setTimeStamp(absolutePath, time);
-    }
-
-    Q_ASSERT(QFile::exists(absolutePath));
-}
-
-void TestBase::createFiles(const QStringList& files)
-{
-    foreach(const QString& path, files) {
-        createFile(path);
-    }
-}
-
-void TestBase::createDir(const QString& path, const QDateTime& time)
-{
-    QString absolutePath = path;
-    makePathAbsoluteAndCreateParents(absolutePath);
-    m_dir->mkdir(absolutePath);
-
-    if (time.isValid()) {
-        setTimeStamp(absolutePath, time);
-    }
-
-    Q_ASSERT(QFile::exists(absolutePath));
-}
-
-QStringList TestBase::viewItems() const
+QStringList TestBase::viewItems(const DolphinView* view)
 {
     QStringList itemList;
-    const QAbstractItemModel* model = itemView()->model();
+    const QAbstractItemModel* model = itemView(view)->model();
 
     for (int row = 0; row < model->rowCount(); row++) {
         itemList << model->data(model->index(row, 0), Qt::DisplayRole).toString();
@@ -136,29 +50,3 @@ QStringList TestBase::viewItems() const
 
     return itemList;
 }
-
-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);
-}
index 7e6ff4bb50ccd438eb078b596ca09e9ad9ee7bd9..7034cbe4149fecdac7c587cc546dfdbba7f47e7a 100644 (file)
  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA              *
  *****************************************************************************/
 
-#ifndef TESTHELPER_H
-#define TESTHELPER_H
+#ifndef TESTBASE_H
+#define TESTBASE_H
 
-#include <KUrl>
+#include <QtCore/QObject>
 
-#include <QDateTime>
-
-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.
+ * The class TestBase (which is a friend of DolphinView's) provides access to some
+ * parts of DolphinView to the unit tests.
  *
  * TODO: TestBase should also backup the DolphinSettings and restore them later!
  */
@@ -47,51 +41,17 @@ class TestBase : public QObject
 
 public:
 
-    TestBase();
-    ~TestBase();
-
-    // Returns the item view (icons, details, or columns)
-    QAbstractItemView* itemView() const;
-
-    // Reloads the view and waits for the finishedPathLoading(const KUrl&) signal.
-    void reloadViewAndWait();
-
-    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"), const QDateTime& time = QDateTime());
-    void createFiles(const QStringList& files);
-    void createDir(const QString& path, const QDateTime& time = QDateTime());
-
-    /**
-     * Returns the items shown in the view. The order corresponds to the sort order of the view.
-     */
-
-    QStringList viewItems() const;
-
-    /**
-     * Remove the test directory and create an empty one.
-     */
-
-    void cleanupTestDir();
-
-    //  Make members that are accessed frequently by the derived test classes public
-
-    DolphinView* m_view;
-
-    QString m_path;
+    TestBase() {};
+    ~TestBase() {};
 
-private:
+    /** Returns the item view (icons, details, or columns) */
+    static QAbstractItemView* itemView(const DolphinView* view);
 
-    KTempDir* m_tempDir;
-    QDir* m_dir;
+    /** Reloads the view and waits for the finishedPathLoading(const KUrl&) signal. */
+    static void reloadViewAndWait(DolphinView* view);
 
-    void makePathAbsoluteAndCreateParents(QString& path);
+    /** Returns the items shown in the view. The order corresponds to the sort order of the view. */
+    static QStringList viewItems(const DolphinView* view);
 
 };
 
diff --git a/src/tests/testdir.cpp b/src/tests/testdir.cpp
new file mode 100644 (file)
index 0000000..64d7f1a
--- /dev/null
@@ -0,0 +1,98 @@
+/*****************************************************************************
+ *   Copyright (C) 2010-2011 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 "testdir.h"
+
+#include <QDir>
+
+#ifdef Q_OS_UNIX
+#include <utime.h>
+#else
+#include <sys/utime.h>
+#endif
+
+/** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
+
+static void setTimeStamp(const QString& path, const QDateTime& mtime)
+{
+#ifdef Q_OS_UNIX
+    struct utimbuf utbuf;
+    utbuf.actime = mtime.toTime_t();
+    utbuf.modtime = utbuf.actime;
+    utime(QFile::encodeName(path), &utbuf);
+#elif defined(Q_OS_WIN)
+    struct _utimbuf utbuf;
+    utbuf.actime = mtime.toTime_t();
+    utbuf.modtime = utbuf.actime;
+    _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
+#endif
+}
+
+void TestDir::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
+{
+    QString absolutePath = path;
+    makePathAbsoluteAndCreateParents(absolutePath);
+
+    QFile f(absolutePath);
+    f.open(QIODevice::WriteOnly);
+    f.write(data);
+    f.close();
+
+    if (time.isValid()) {
+        setTimeStamp(absolutePath, time);
+    }
+
+    Q_ASSERT(QFile::exists(absolutePath));
+}
+
+void TestDir::createFiles(const QStringList& files)
+{
+    foreach(const QString& path, files) {
+        createFile(path);
+    }
+}
+
+void TestDir::createDir(const QString& path, const QDateTime& time)
+{
+    QString absolutePath = path;
+    makePathAbsoluteAndCreateParents(absolutePath);
+    QDir(name()).mkdir(absolutePath);
+
+    if (time.isValid()) {
+        setTimeStamp(absolutePath, time);
+    }
+
+    Q_ASSERT(QFile::exists(absolutePath));
+}
+
+void TestDir::makePathAbsoluteAndCreateParents(QString& path)
+{
+    QFileInfo fileInfo(path);
+    if (!fileInfo.isAbsolute()) {
+        path = name() + path;
+        fileInfo.setFile(path);
+    }
+
+    const QDir dir = fileInfo.dir();
+    if (!dir.exists()) {
+        createDir(dir.absolutePath());
+    }
+
+    Q_ASSERT(dir.exists());
+}
diff --git a/src/tests/testdir.h b/src/tests/testdir.h
new file mode 100644 (file)
index 0000000..bff4626
--- /dev/null
@@ -0,0 +1,59 @@
+/*****************************************************************************
+ *   Copyright (C) 2010-2011 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 TESTDIR_H
+#define TESTDIR_H
+
+#include <KTempDir>
+#include <KUrl>
+
+#include <QDateTime>
+
+/**
+ * TestDir provides a temporary directory. In addition to KTempDir, it has
+ * methods that create files and subdirectories inside the directory.
+ */
+
+class TestDir : public KTempDir
+{
+
+public:
+
+    TestDir() {}
+    ~TestDir() {}
+
+    KUrl url() const { return KUrl(name()); }
+
+    /**
+     * 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"), const QDateTime& time = QDateTime());
+    void createFiles(const QStringList& files);
+    void createDir(const QString& path, const QDateTime& time = QDateTime());
+
+private:
+
+    void makePathAbsoluteAndCreateParents(QString& path);
+
+};
+
+#endif