┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorPeter Penz <[email protected]>2011-07-30 20:13:09 +0200
committerPeter Penz <[email protected]>2011-07-30 20:13:41 +0200
commitf23e9496f303995557b744c03178f5dbd9b35016 (patch)
tree1139c4340ac173718d1fa847e0124d6175781fd9 /src/tests
parent69e4007e5e330f2ca87c0176a186967b5ca156e8 (diff)
Merged very early alpha-version of Dolphin 2.0
Dolphin 2.0 will get a new view-engine with the following improvements: - Better performance - Animated transitions - No clipped filenames due to dynamic item-sizes - Grouping support for all view-modes - Non-rectangular selection areas - Simplified code for better maintenance More details will be provided in a blog-entry during the next days. Please note that the code is in a very early alpha-stage and although the most tricky parts have been implemented already very basic things like drag and drop or selections have not been pushed yet. Those things are rather trivial to implement but this still will take some time.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/CMakeLists.txt41
-rw-r--r--src/tests/dolphindetailsviewtest.cpp304
-rw-r--r--src/tests/dolphintreeviewtest.cpp413
-rw-r--r--src/tests/dolphinviewtest_allviewmodes.cpp596
-rw-r--r--src/tests/dolphinviewtest_allviewmodes.h80
-rw-r--r--src/tests/dolphinviewtest_columns.cpp42
-rw-r--r--src/tests/dolphinviewtest_details.cpp42
-rw-r--r--src/tests/dolphinviewtest_icons.cpp42
-rw-r--r--src/tests/kfileitemlistviewtest.cpp100
-rw-r--r--src/tests/kfileitemmodeltest.cpp210
-rw-r--r--src/tests/testbase.cpp77
-rw-r--r--src/tests/testbase.h67
-rw-r--r--src/tests/testdir.cpp16
-rw-r--r--src/tests/testdir.h14
14 files changed, 351 insertions, 1693 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index e10f8d0c9..5038ee8d7 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,9 +1,27 @@
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 testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
-target_link_libraries(dolphindetailsviewtest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
+# KFileItemListViewTest
+set(kfileitemlistviewtest_SRCS
+ kfileitemlistviewtest.cpp
+ testdir.cpp
+ ../kitemviews/kfileitemmodel.cpp
+ ../kitemviews/kfileitemlistview.cpp
+ ../kitemviews/kitemmodelbase.cpp
+ ../kitemviews/kitemlistview.cpp
+)
+kde4_add_unit_test(kfileitemlistviewtest TEST ${kfileitemlistviewtest_SRCS})
+target_link_libraries(kfileitemlistviewtest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
+
+# KFileItemModelTest
+set(kfileitemmodeltest_SRCS
+ kfileitemmodeltest.cpp
+ testdir.cpp
+ ../kitemviews/kfileitemmodel.cpp
+ ../kitemviews/kitemmodelbase.cpp
+)
+kde4_add_unit_test(kfileitemmodeltest TEST ${kfileitemmodeltest_SRCS})
+target_link_libraries(kfileitemmodeltest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
# DolphinSearchBox
if (Nepomuk_FOUND)
@@ -18,20 +36,3 @@ if (Nepomuk_FOUND)
kde4_add_unit_test(dolphinsearchboxtest TEST ${dolphinsearchboxtest_SRCS})
target_link_libraries(dolphinsearchboxtest ${KDE4_KIO_LIBS} ${SOPRANO_LIBRARIES} ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} nepomukutils ${QT_QTTEST_LIBRARY})
endif (Nepomuk_FOUND)
-
-
-# DolphinTreeView
-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 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 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 testdir.cpp testbase.cpp ../views/zoomlevelinfo.cpp)
-target_link_libraries(dolphinviewtest_icons dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
diff --git a/src/tests/dolphindetailsviewtest.cpp b/src/tests/dolphindetailsviewtest.cpp
deleted file mode 100644
index 31b4a57cd..000000000
--- a/src/tests/dolphindetailsviewtest.cpp
+++ /dev/null
@@ -1,304 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2010 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-
-#include "testbase.h"
-#include "testdir.h"
-
-#include "views/dolphindetailsview.h"
-#include "views/dolphinview.h"
-#include "views/dolphinmodel.h"
-#include "views/dolphinsortfilterproxymodel.h"
-#include "views/zoomlevelinfo.h"
-
-#include <qtestmouse.h>
-#include <qtestkeyboard.h>
-
-class DolphinDetailsViewTest : public TestBase
-{
- Q_OBJECT
-
-private slots:
-
- void testExpandedUrls();
-
- void bug217447_shiftArrowSelection();
- void bug234600_overlappingIconsWhenZooming();
- void bug257401_longFilenamesKeyboardNavigation();
-
-private:
-
- /**
- * 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 {
- QSignalSpy spyFinishedPathLoading(view, SIGNAL(finishedPathLoading(const KUrl&)));
- view->setMode(DolphinView::DetailsView);
- DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view));
- Q_ASSERT(detailsView);
- detailsView->setFoldersExpandable(true);
- view->resize(400, 400);
- view->show();
- QTest::qWaitForWindowShown(view);
-
- // If the DolphinView's finishedPathLoading(const KUrl&) signal has not been received yet,
- // we have to wait a bit more.
- // The reason why the if-statement is needed here is that the signal might have been emitted
- // while we were waiting in QTest::qWaitForWindowShown(view)
- // -> waitForFinishedPathLoading(view) would fail in that case.
- if (spyFinishedPathLoading.isEmpty()) {
- waitForFinishedPathLoading(view);
- }
-
- return 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);
- }
-};
-
-/**
- * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
- * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
- * Each of those contains 3 further subfolders of the same name.
- */
-
-void DolphinDetailsViewTest::testExpandedUrls()
-{
- QStringList files;
- QStringList subFolderNames;
- subFolderNames << "a" << "b" << "c";
-
- foreach(const QString& level1, subFolderNames) {
- foreach(const QString& level2, subFolderNames) {
- foreach(const QString& level3, subFolderNames) {
- files << level1 + "/" + level2 + "/" + level3 + "/testfile";
- }
- }
- }
-
- 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(detailsView->expandedUrls(), expectedExpandedUrls);
-
- // 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(dir.name() + item);
- detailsView->expand(proxyModelIndexForUrl(&view, url));
- expectedExpandedUrls += url;
- QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
-
- // Before we proceed, we have to make sure that the view has finished
- // loading the contents of the expanded folder.
- waitForFinishedPathLoading(&view);
- }
-
- // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
- QStringList itemsToCollapse;
- itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b";
-
- foreach(const QString& item, itemsToCollapse) {
- KUrl url(dir.name() + item);
- detailsView->collapse(proxyModelIndexForUrl(&view, url));
- expectedExpandedUrls -= url;
- QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
- }
-}
-
-/**
- * When the first item in the view is active and Shift is held while the "arrow down"
- * key is pressed repeatedly, the selection should grow by one item for each key press.
- * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=217447
- *
- * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
- * column for an item, but only the width of the actual file name, did not reimplement
- * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
- * uses the center of the visualRect of an item internally. If the width of the file name
- * is less than half the width of the "Name" column, the center of an item's visualRect
- * was therefore outside the space that DolphinDetailsView actually assigned to the
- * item, and this led to unexpected deselection of items.
- *
- * TODO: To make the test more reliable, one could adjust the width of the "Name"
- * column before the test in order to really make sure that the column is more than twice
- * as wide as the space actually occupied by the file names (this triggers the bug).
- */
-
-void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
-{
- TestDir dir;
- for (int i = 0; i < 100; i++) {
- dir.createFile(QString("%1").arg(i));
- }
- DolphinView view(dir.url(), 0);
- DolphinDetailsView* detailsView = initView(&view);
-
- // Select the first item
- 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 = 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.
- detailsView->clearSelection();
- QItemSelectionModel* selectionModel = detailsView->selectionModel();
- QCOMPARE(selectionModel->selectedIndexes().count(), 0);
-
- QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
- QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
- QCOMPARE(selectedIndexes.count(), 1);
- QVERIFY(selectedIndexes.contains(index0));
-
- // Now we go down item by item using Shift+Down. In each step, we check that the current item
- // is added to the selection and that the size of the selection grows by one.
-
- int current = 1;
-
- while (current < 100) {
- 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);
- QVERIFY(selectedIndexes.contains(currentIndex));
-
- current++;
- }
-}
-
-/**
- * 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";
-
- TestDir dir;
- dir.createFiles(files);
- DolphinView view(dir.url(), 0);
- DolphinDetailsView* detailsView = initView(&view);
-
- 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 = view.zoomLevel();
- int zoomLevel = ZoomLevelInfo::minimumLevel();
- 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++;
- 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(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
- }
-
- view.setZoomLevel(zoomLevelBackup);
-}
-
-/**
- * The width of the visualRect of an item is usually replaced by the width of the file name.
- * However, if the file name is wider then the view's name column, this leads to problems with
- * keyboard navigation if files with very long names are present in the current folder, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=257401
- *
- * This test checks that the visualRect of an item is never wider than the "Name" column.
- */
-
-void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
- TestDir dir;
- QString name;
- for (int i = 0; i < 20; i++) {
- name += "mmmmmmmmmm";
- dir.createFile(name);
- }
- DolphinView view(dir.url(), 0);
- DolphinDetailsView* detailsView = initView(&view);
-
- // Select the first item
- 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 = detailsView->selectionModel();
- QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
- QCOMPARE(selectedIndexes.count(), 1);
- QVERIFY(selectedIndexes.contains(index0));
-
- // 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(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));
- }
-}
-
-QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
-
-#include "dolphindetailsviewtest.moc"
diff --git a/src/tests/dolphintreeviewtest.cpp b/src/tests/dolphintreeviewtest.cpp
deleted file mode 100644
index 149570d1a..000000000
--- a/src/tests/dolphintreeviewtest.cpp
+++ /dev/null
@@ -1,413 +0,0 @@
-/*****************************************************************************
- * Copyright (C) 2010-2011 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-#include <KDebug>
-#include <KAction>
-
-#include "views/dolphintreeview.h"
-
-#include <qtestkeyboard.h>
-#include <qtestmouse.h>
-#include <QStringListModel>
-
-class DolphinTreeViewTest : public QObject
-{
- Q_OBJECT
-
-private slots:
-
- void testKeyboardNavigationSelectionUpdate();
-
- void bug218114_visualRegionForSelection();
- void bug220898_focusOut();
-
-private:
-
- /** A method that simplifies checking a view's current item and selection */
- static void verifyCurrentItemAndSelection(const QAbstractItemView& view, const QModelIndex& expectedCurrent, const QModelIndexList& expectedSelection) {
- QCOMPARE(view.currentIndex(), expectedCurrent);
- const QModelIndexList selectedIndexes = view.selectionModel()->selectedIndexes();
- QCOMPARE(selectedIndexes.count(), expectedSelection.count());
- foreach(const QModelIndex& index, expectedSelection) {
- QVERIFY(selectedIndexes.contains(index));
- }
- }
-
- /** Use this method if only one item is selected */
- static void verifyCurrentItemAndSelection(const QAbstractItemView& view, const QModelIndex& current, const QModelIndex& selected) {
- QModelIndexList list;
- list << selected;
- verifyCurrentItemAndSelection(view, current, list);
- }
-
- /** Use this method if the only selected item is the current item */
- static void verifyCurrentItemAndSelection(const QAbstractItemView& view, const QModelIndex& current) {
- verifyCurrentItemAndSelection(view, current, current);
- }
-
-};
-
-/**
- * TestView is a simple view class derived from DolphinTreeView.
- * It makes sure that the visualRect for each index contains only the item text as
- * returned by QAbstractItemModel::data(...) for the role Qt::DisplayRole.
- *
- * We have to check that DolphinTreeView handles the case of visualRects with different widths
- * correctly because this is the case in DolphinDetailsView which is derived from DolphinTreeView.
- */
-
-class TestView : public DolphinTreeView
-{
- Q_OBJECT
-
-public:
-
- TestView(QWidget* parent = 0) : DolphinTreeView(parent) {};
- ~TestView() {};
-
- QRect visualRect(const QModelIndex& index) const {
- QRect rect = DolphinTreeView::visualRect(index);
-
- const QStyleOptionViewItem option = viewOptions();
- const QFontMetrics fontMetrics(option.font);
- int width = option.decorationSize.width() + fontMetrics.width(model()->data(index).toString());
-
- rect.setWidth(width);
- return rect;
- }
-
-};
-
-/**
- * This test checks that updating the selection after key presses works as expected.
- * Qt does not handle this internally if the first letter of an item is pressed, which
- * is why DolphinTreeView has some custom code for this. The test verifies that this
- * works without unwanted side effects.
- *
- * The test uses the class TreeViewWithDeleteShortcut which deletes the selected items
- * when Shift-Delete is pressed. This is needed to test the fix for bug 259656 (see below).
- */
-
-class TreeViewWithDeleteShortcut : public DolphinTreeView {
-
- Q_OBJECT
-
-public:
-
- TreeViewWithDeleteShortcut(QWidget* parent = 0) : DolphinTreeView(parent) {
- // To test the fix for bug 259656, we need a delete shortcut.
- KAction* deleteAction = new KAction(this);
- deleteAction->setShortcut(Qt::SHIFT | Qt::Key_Delete);
- addAction(deleteAction);
- connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteSelectedItems()));
- };
-
- ~TreeViewWithDeleteShortcut() {};
-
-public slots:
-
- void deleteSelectedItems() {
- // We have to delete the items one by one and update the list of selected items after
- // each step because every removal will invalidate the model indexes in the list.
- QModelIndexList selectedItems = selectionModel()->selectedIndexes();
- while (!selectedItems.isEmpty()) {
- const QModelIndex index = selectedItems.takeFirst();
- model()->removeRow(index.row());
- selectedItems = selectionModel()->selectedIndexes();
- }
- }
-};
-
-void DolphinTreeViewTest::testKeyboardNavigationSelectionUpdate() {
- QStringList items;
- items << "a" << "b" << "c" << "d" << "e";
- QStringListModel model(items);
-
- QModelIndex index[5];
- for (int i = 0; i < 5; i++) {
- index[i] = model.index(i, 0);
- }
-
- TreeViewWithDeleteShortcut view;
- view.setModel(&model);
- view.setSelectionMode(QAbstractItemView::ExtendedSelection);
- view.resize(400, 400);
- view.show();
- QTest::qWaitForWindowShown(&view);
-
- view.clearSelection();
- QVERIFY(view.selectionModel()->selectedIndexes().isEmpty());
-
- /**
- * Check that basic keyboard navigation with arrow keys works.
- */
-
- view.setCurrentIndex(index[0]);
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Go down -> item 1 ("b") should be selected
- kDebug() << "Down";
- QTest::keyClick(view.viewport(), Qt::Key_Down);
- verifyCurrentItemAndSelection(view, index[1]);
-
- // Go down -> item 2 ("c") should be selected
- kDebug() << "Down";
- QTest::keyClick(view.viewport(), Qt::Key_Down);
- verifyCurrentItemAndSelection(view, index[2]);
-
- // Ctrl-Up -> item 2 ("c") remains selected
- kDebug() << "Ctrl-Up";
- QTest::keyClick(view.viewport(), Qt::Key_Up, Qt::ControlModifier);
- verifyCurrentItemAndSelection(view, index[1], index[2]);
-
- // Go up -> item 0 ("a") should be selected
- kDebug() << "Up";
- QTest::keyClick(view.viewport(), Qt::Key_Up);
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Shift-Down -> items 0 and 1 ("a" and "b") should be selected
- kDebug() << "Shift-Down";
- QTest::keyClick(view.viewport(), Qt::Key_Down, Qt::ShiftModifier);
- QModelIndexList expectedSelection;
- expectedSelection << index[0] << index[1];
- verifyCurrentItemAndSelection(view, index[1], expectedSelection);
-
- /**
- * When the first letter of a file name is pressed, this file becomes the current item
- * and gets selected. If the user then Shift-clicks another item, it is expected that
- * all items between these two items get selected. Before the bug
- *
- * https://bugs.kde.org/show_bug.cgi?id=201459
- *
- * was fixed, this was not the case: the starting point for the Shift-selection was not
- * updated if an item was selected by pressing the first letter of the file name.
- */
-
- view.clearSelection();
- QVERIFY(view.selectionModel()->selectedIndexes().isEmpty());
-
- // Control-click item 0 ("a")
- kDebug() << "Ctrl-click on \"a\"";
- QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ControlModifier, view.visualRect(index[0]).center());
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Press "c", such that item 2 ("c") should be the current one.
- kDebug() << "Press \"c\"";
- QTest::keyClick(view.viewport(), Qt::Key_C);
- verifyCurrentItemAndSelection(view, index[2]);
-
- // Now Shift-Click the last item ("e"). We expect that 3 items ("c", "d", "e") are selected.
- kDebug() << "Shift-click on \"e\"";
- QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, view.visualRect(index[4]).center());
- expectedSelection.clear();
- expectedSelection << index[2] << index[3] << index[4];
- verifyCurrentItemAndSelection(view, index[4], expectedSelection);
-
- /**
- * Starting a drag&drop operation should not clear the selection, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=158649
- */
-
- view.clearSelection();
- QVERIFY(view.selectionModel()->selectedIndexes().isEmpty());
-
- // Click item 0 ("a")
- kDebug() << "Click on \"a\"";
- QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[0]).center());
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Shift-Down -> "a" and "b" should be selected
- kDebug() << "Shift-Down";
- QTest::keyClick(view.viewport(), Qt::Key_Down, Qt::ShiftModifier);
- expectedSelection.clear();
- expectedSelection << index[0] << index[1];
- verifyCurrentItemAndSelection(view, index[1], expectedSelection);
-
- // Press mouse button on item 0 ("a"), but do not release it. Check that the selection is unchanged
- kDebug() << "Mouse press on \"a\"";
- QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[0]).center());
- verifyCurrentItemAndSelection(view, index[0], expectedSelection);
-
- // Move mouse to item 1 ("b"), check that selection is unchanged
- kDebug() << "Move mouse to \"b\"";
- QMouseEvent moveEvent(QEvent::MouseMove, view.visualRect(index[1]).center(), Qt::NoButton, Qt::LeftButton, Qt::NoModifier);
- bool moveEventReceived = qApp->notify(view.viewport(), &moveEvent);
- QVERIFY(moveEventReceived);
- verifyCurrentItemAndSelection(view, index[0], expectedSelection);
-
- // Release mouse button on item 1 ("b"), check that selection is unchanged
- kDebug() << "Mouse release on \"b\"";
- QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[1]).center());
- verifyCurrentItemAndSelection(view, index[0], expectedSelection);
-
- /**
- * Keeping Shift+Delete pressed for some time should delete only one item, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=259656
- */
-
- view.clearSelection();
- QVERIFY(view.selectionModel()->selectedIndexes().isEmpty());
-
- // Click item 0 ("a")
- kDebug() << "Click on \"a\"";
- QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[0]).center());
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Press Shift-Delete and keep the keys pressed for some time
- kDebug() << "Press Shift-Delete";
- QTest::keyPress(view.viewport(), Qt::Key_Delete, Qt::ShiftModifier);
- QTest::qWait(200);
- QTest::keyRelease(view.viewport(), Qt::Key_Delete, Qt::ShiftModifier);
-
- // Verify that only one item has been deleted
- QCOMPARE(view.model()->rowCount(), 4);
-}
-
-/**
- * QTreeView assumes implicitly that the width of each item's visualRect is the same. This leads to painting
- * problems in Dolphin if items with different widths are in one QItemSelectionRange, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=218114
- *
- * To fix this, DolphinTreeView has a custom implementation of visualRegionForSelection(). The following
- * unit test checks that.
- */
-
-void DolphinTreeViewTest::bug218114_visualRegionForSelection()
-{
- QStringList items;
- items << "a" << "an item with a long name" << "a";
- QStringListModel model(items);
-
- QModelIndex index0 = model.index(0, 0);
- QModelIndex index1 = model.index(1, 0);
- QModelIndex index2 = model.index(2, 0);
-
- TestView view;
- view.setModel(&model);
- view.setSelectionMode(QAbstractItemView::ExtendedSelection);
- view.resize(400, 400);
- view.show();
- QTest::qWaitForWindowShown(&view);
-
- // First check that the width of index1 is larger than that of index0 and index2 (this triggers the bug).
-
- QVERIFY(view.visualRect(index0).width() < view.visualRect(index1).width());
- QVERIFY(view.visualRect(index2).width() < view.visualRect(index1).width());
-
- // Select all items in one go.
-
- view.selectAll();
- const QItemSelection selection = view.selectionModel()->selection();
- QCOMPARE(selection.count(), 1);
- QCOMPARE(selection.indexes().count(), 3);
-
- // Verify that the visualRegionForSelection contains all visualRects.
- // We do this indirectly using QRegion::boundingRect() because
- // QRegion::contains(const QRect&) returns true even if the QRect is not
- // entirely inside the QRegion.
-
- const QRegion region = view.visualRegionForSelection(selection);
- const QRect boundingRect = region.boundingRect();
-
- QVERIFY(boundingRect.contains(view.visualRect(index0)));
- QVERIFY(boundingRect.contains(view.visualRect(index1)));
- QVERIFY(boundingRect.contains(view.visualRect(index2)));
-}
-
-/**
- * This test verifies that selection of multiple items with the mouse works
- * if a key was pressed and the keyboard focus moved to another window before the
- * key was released, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=220898
- */
-
-void DolphinTreeViewTest::bug220898_focusOut()
-{
- QStringList items;
- items << "a" << "b" << "c" << "d" << "e";
- QStringListModel model(items);
-
- QModelIndex index[5];
- for (int i = 0; i < 5; i++) {
- index[i] = model.index(i, 0);
- }
-
- TestView view;
- view.setModel(&model);
- view.setSelectionMode(QAbstractItemView::ExtendedSelection);
- view.resize(400, 400);
- view.show();
- QTest::qWaitForWindowShown(&view);
-
- view.setCurrentIndex(index[0]);
- verifyCurrentItemAndSelection(view, index[0]);
-
- // Press Down
- QTest::keyPress(view.viewport(), Qt::Key_Down, Qt::NoModifier);
-
- // Move keyboard focus to another widget
- QWidget widget;
- widget.show();
- QTest::qWaitForWindowShown(&widget);
- widget.setFocus();
-
- // Wait until the widgets have received the focus events
- while (view.viewport()->hasFocus()) {
- QTest::qWait(10);
- }
- QVERIFY(!view.viewport()->hasFocus());
-
- // Release the "Down" key
- QTest::keyRelease(&widget, Qt::Key_Down, Qt::NoModifier);
-
- // Move keyboard focus back to the view
- widget.hide();
- view.viewport()->setFocus();
-
- // Wait until the widgets have received the focus events
- while (widget.hasFocus()) {
- QTest::qWait(10);
- }
- QVERIFY(!widget.hasFocus());
-
- // Press left mouse button below the last item
- const int lastRowHeight = view.sizeHintForRow(4);
- QTest::mousePress(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[4]).center() + QPoint(0, lastRowHeight));
-
- // Move mouse to the first item and release
- QTest::mouseMove(view.viewport(), view.visualRect(index[0]).center());
- QMouseEvent moveEvent(QEvent::MouseMove, view.visualRect(index[0]).center(), Qt::NoButton, Qt::LeftButton, Qt::NoModifier);
- bool moveEventReceived = qApp->notify(view.viewport(), &moveEvent);
- QVERIFY(moveEventReceived);
- QTest::mouseRelease(view.viewport(), Qt::LeftButton, Qt::NoModifier, view.visualRect(index[0]).center());
-
- // All items should be selected
- QModelIndexList expectedSelection;
- expectedSelection << index[0] << index[1] << index[2] << index[3] << index[4];
- verifyCurrentItemAndSelection(view, index[0], expectedSelection);
-}
-
-QTEST_KDEMAIN(DolphinTreeViewTest, GUI)
-
-#include "dolphintreeviewtest.moc"
diff --git a/src/tests/dolphinviewtest_allviewmodes.cpp b/src/tests/dolphinviewtest_allviewmodes.cpp
deleted file mode 100644
index cac4c7102..000000000
--- a/src/tests/dolphinviewtest_allviewmodes.cpp
+++ /dev/null
@@ -1,596 +0,0 @@
-/****************************************************************************
- * Copyright (C) 2010-2011 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <kdebug.h>
-
-#include "dolphinviewtest_allviewmodes.h"
-
-#include <qtest_kde.h>
-
-#include "testdir.h"
-
-#include "views/dolphinview.h"
-#include "views/dolphinmodel.h"
-#include "views/dolphindirlister.h"
-#include "views/dolphinsortfilterproxymodel.h"
-#include "views/zoomlevelinfo.h"
-
-#include <QScrollBar>
-
-#include <qtestmouse.h>
-#include <qtestkeyboard.h>
-
-DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
- // Need to register KFileItemList for use with QSignalSpy
- qRegisterMetaType<KFileItemList>("KFileItemList");
-}
-
-/**
- * testSelection() checks the basic selection functionality of DolphinView, including:
- *
- * selectedItems()
- * selectedItemsCount()
- * selectAll()
- * invertSelection()
- * clearSelection()
- * hasSelection()
- *
- * and the signal selectionChanged(const KFileItemList& selection)
- */
-
-Q_DECLARE_METATYPE(KFileItemList)
-
-void DolphinViewTest_AllViewModes::testSelection() {
- TestDir dir;
- const int totalItems = 50;
- for (int i = 0; i < totalItems; i++) {
- dir.createFile(QString("%1").arg(i));
- }
-
- DolphinView view(dir.url(), 0);
- QAbstractItemView* itemView = initView(&view);
-
- // Start with an empty selection
- view.clearSelection();
-
- QCOMPARE(view.selectedItems().count(), 0);
- QCOMPARE(view.selectedItemsCount(), 0);
- QVERIFY(!view.hasSelection());
-
- // First some simple tests where either all or no items are selected
- view.selectAll();
- verifySelectedItemsCount(&view, totalItems);
-
- view.invertSelection();
- verifySelectedItemsCount(&view, 0);
-
- view.invertSelection();
- verifySelectedItemsCount(&view, totalItems);
-
- 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(&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(&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(&view, 5);
-
- view.invertSelection();
- verifySelectedItemsCount(&view, totalItems - 5);
-
- // Pressing Esc should clear the selection
- QTest::keyClick(itemView->viewport(), Qt::Key_Escape);
- verifySelectedItemsCount(&view, 0);
-}
-
-/**
- * Check that setting the directory view properties works.
- */
-
-void DolphinViewTest_AllViewModes::testViewPropertySettings()
-{
- // Create some files with different sizes and modification times to check the different sorting options
- QDateTime now = QDateTime::currentDateTime();
-
- 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");
-
- DolphinView view(dir.url(), 0);
- initView(&view);
-
- // First set all settings to the default.
- view.setSorting(DolphinView::SortByName);
- QCOMPARE(view.sorting(), DolphinView::SortByName);
-
- view.setSortOrder(Qt::AscendingOrder);
- QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
-
- view.setSortFoldersFirst(true);
- QVERIFY(view.sortFoldersFirst());
-
- view.setShowPreview(false);
- QVERIFY(!view.showPreview());
-
- if (view.showHiddenFiles()) {
- // Changing the "hidden files" setting triggers the dir lister
- // -> we have to wait until loading the hidden files is finished
- view.setShowHiddenFiles(false);
- waitForFinishedPathLoading(&view);
- }
- QVERIFY(!view.showHiddenFiles());
-
- /** Check that the sort order is correct for different kinds of settings */
-
- // Sort by Name, ascending
- QCOMPARE(view.sorting(), DolphinView::SortByName);
- QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
- QCOMPARE(viewItems(&view), QStringList() << "c" << "a" << "b" << "d" << "e");
-
- // Sort by Name, descending
- 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
- 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
- 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
- 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
- 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"
- view.setSortFoldersFirst(false);
- QVERIFY(!view.sortFoldersFirst());
- QCOMPARE(viewItems(&view), QStringList()<< "b" << "d" << "c" << "a" << "e");
-
- // Try again with Sort by Name, ascending
- 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
- view.setShowHiddenFiles(true);
- waitForFinishedPathLoading(&view);
- QVERIFY(view.showHiddenFiles());
-
- // Depending on the settings, a .directory file might have been created.
- // Remove it from the list to get consistent results.
- QStringList result = viewItems(&view);
- result.removeAll(".directory");
- QCOMPARE(result, QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
-
- // Previews
- view.setShowPreview(true);
- QVERIFY(view.showPreview());
-
- // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
-}
-
-/**
- * testZoomLevel() checks that setting the zoom level works, both using DolphinView's API and using Ctrl+mouse wheel.
- */
-
-void DolphinViewTest_AllViewModes::testZoomLevel()
-{
- TestDir dir;
- dir.createFiles(QStringList() << "a" << "b");
- DolphinView view(dir.url(), 0);
- QAbstractItemView* itemView = initView(&view);
-
- view.setShowPreview(false);
- QVERIFY(!view.showPreview());
-
- int zoomLevelBackup = view.zoomLevel();
-
- int zoomLevel = ZoomLevelInfo::minimumLevel();
- view.setZoomLevel(zoomLevel);
- QCOMPARE(view.zoomLevel(), zoomLevel);
-
- // Increase the zoom level successively to the maximum.
- while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
- zoomLevel++;
- view.setZoomLevel(zoomLevel);
- QCOMPARE(view.zoomLevel(), zoomLevel);
- }
-
- // Try setting a zoom level larger than the maximum
- view.setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
- QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
-
- // Turn previews on and try setting a zoom level smaller than the minimum
- 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
- 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);
-
- 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(view.zoomLevel() < oldZoomLevel);
- }
- QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
-
- 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(view.zoomLevel() > oldZoomLevel);
- }
- QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
-
- // Turn previews on again and check that the zoom level is restored
- view.setShowPreview(true);
- QVERIFY(view.showPreview());
- QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
-
- // Restore the initial state
- view.setZoomLevel(zoomLevelBackup);
- view.setShowPreview(false);
- view.setZoomLevel(zoomLevelBackup);
-}
-
-/**
- * testSaveAndRestoreState() checks if saving and restoring the view state (current item, scroll position).
- *
- * Note that we call qApp->sendPostedEvents() every time the view's finishedPathLoading(const KUrl&) signal
- * is received. The reason is that the scroll position is restored in the slot restoreContentsPosition(),
- * which is been invoked using a queued connection in DolphinView::slotLoadingCompleted(). To make sure
- * that this slot is really executed before we proceed, we have to empty the event queue using qApp->sendPostedEvents().
- */
-
-void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
-{
- const int totalItems = 50;
- TestDir dir;
- for (int i = 0; i < totalItems; i++) {
- dir.createFile(QString("%1").arg(i));
- }
- 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.
- view.setSorting(DolphinView::SortByName);
- QCOMPARE(view.sorting(), DolphinView::SortByName);
- view.setSortOrder(Qt::AscendingOrder);
- QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
-
- // Make sure that previews are off and that the icon size does not depend on the preview setting.
- // This is needed for the test for bug 270437, see below.
- view.setShowPreview(false);
- int zoomLevel = view.zoomLevel();
- view.setShowPreview(true);
- view.setZoomLevel(zoomLevel);
- view.setShowPreview(false);
-
- // Select item 45
- 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();
-
- // Save the view state
- QByteArray viewState;
- QDataStream saveStream(&viewState, QIODevice::WriteOnly);
- view.saveState(saveStream);
-
- // Change the URL
- view.setUrl(KUrl(dir.name() + "51"));
- waitForFinishedPathLoading(&view);
- qApp->sendPostedEvents();
-
- // Go back, but do not call DolphinView::restoreState()
- view.setUrl(dir.url());
- waitForFinishedPathLoading(&view);
- qApp->sendPostedEvents();
-
- // 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
- view.setUrl(KUrl(dir.name() + "51"));
- waitForFinishedPathLoading(&view);
- qApp->sendPostedEvents();
-
- // Check that the current item and scroll position are correct if DolphinView::restoreState()
- // is called after the URL change
- view.setUrl(dir.url());
- QDataStream restoreStream(viewState);
- view.restoreState(restoreStream);
- waitForFinishedPathLoading(&view);
- qApp->sendPostedEvents();
-
- QCOMPARE(itemView(&view)->currentIndex(), index45);
- QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
- QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
-
- /**
- * Additionally, we verify the fix for the bug https://bugs.kde.org/show_bug.cgi?id=270437
- * Actually, it's a bug in KFilePreviewGenerator, but it is easier to test it here.
- */
-
- // Turn previews on.
- view.setShowPreview(true);
- QVERIFY(view.showPreview());
-
- // We have to process all events in the queue to make sure that previews are really on.
- qApp->sendPostedEvents();
-
- // Current item and scroll position should not change.
- QCOMPARE(itemView(&view)->currentIndex(), index45);
- QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
- QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
-
- // Turn previews off again. Before bug 270437, this triggered the dir lister's openUrl() method
- // -> we check that by listening to the view's startedPathLoading() signal and wait until the loading is finished in that case.
- QSignalSpy spy(&view, SIGNAL(startedPathLoading(const KUrl&)));
- view.setShowPreview(false);
- QVERIFY(!view.showPreview());
- qApp->sendPostedEvents();
- if (!spy.isEmpty()) {
- // The dir lister reloads the directory. We wait until the loading is finished.
- waitForFinishedPathLoading(&view);
- }
-
- // Current item and scroll position should not change.
- QCOMPARE(itemView(&view)->currentIndex(), index45);
- QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
- QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
-}
-
-/**
- * testKeyboardFocus() checks whether a view grabs the keyboard focus.
- *
- * A view may never grab the keyboard focus itself and must respect the focus-state
- * when switching the view mode, see
- *
- * https://bugs.kde.org/show_bug.cgi?id=261147
- */
-
-void DolphinViewTest_AllViewModes::testKeyboardFocus()
-{
- TestDir dir;
- dir.createFiles(QStringList() << "a" << "b");
- DolphinView view(dir.url(), 0);
- initView(&view);
-
- // Move the keyboard focus to another widget.
- QWidget widget;
- widget.show();
- QTest::qWaitForWindowShown(&widget);
- widget.setFocus();
-
- 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) {
- view.setMode(static_cast<DolphinView::Mode>(i));
- QVERIFY(!view.hasFocus());
- }
-}
-
-/**
- * testCutCopyPaste() checks if cutting or copying items in one view and pasting
- * them in another one works.
- */
-
-void DolphinViewTest_AllViewModes::testCutCopyPaste()
-{
- TestDir dir1;
- dir1.createFiles(QStringList() << "a" << "b" << "c" << "d");
- DolphinView view1(dir1.url(), 0);
- QAbstractItemView* itemView1 = initView(&view1);
-
- TestDir dir2;
- dir2.createFiles(QStringList() << "1" << "2" << "3" << "4");
- dir2.createDir("subfolder");
- DolphinView view2(dir2.url(), 0);
- QAbstractItemView* itemView2 = initView(&view2);
-
- // Make sure that both views are sorted by name in ascending order
- // TODO: Maybe that should be done in initView(), such all tests can rely on it...?
- view1.setSorting(DolphinView::SortByName);
- view1.setSortOrder(Qt::AscendingOrder);
- view2.setSorting(DolphinView::SortByName);
- view2.setSortOrder(Qt::AscendingOrder);
- view2.setSortFoldersFirst(true);
-
- QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
- QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4");
-
- /** Copy and paste */
- // Select an item ("d") n view1, copy it and paste it in view2.
- // Note that we have to wait for view2's finishedPathLoading() signal because the pasting is done in the background.
- QModelIndex index = itemView1->model()->index(3, 0);
- itemView1->scrollTo(index);
- QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
- verifySelectedItemsCount(&view1, 1);
- QCOMPARE(selectedItems(&view1), QStringList() << "d");
- view1.copySelectedItems();
- view2.paste();
- waitForFinishedPathLoading(&view2);
- QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
- QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4" << "d");
- // The pasted item should be selected
- QCOMPARE(selectedItems(&view2), QStringList() << "d");
-
- /** Cut and paste */
- // Select two items ("3", "4") in view2, cut and paste in view1.
- view2.clearSelection();
- index = itemView2->model()->index(3, 0);
- itemView2->scrollTo(index);
- QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
- verifySelectedItemsCount(&view2, 1);
- index = itemView2->model()->index(4, 0);
- itemView2->scrollTo(index);
- QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView2->visualRect(index).center());
- verifySelectedItemsCount(&view2, 2);
- QCOMPARE(selectedItems(&view2), QStringList() << "3" << "4");
- view2.cutSelectedItems();
- // In view1, "d" is still selected
- QCOMPARE(selectedItems(&view1), QStringList() << "d");
- // Paste "3" and "4"
- view1.paste();
- waitForFinishedPathLoading(&view1);
- // In principle, KIO could implement copy&paste such that the pasted items are already there, but the cut items
- // have not been removed yet. Therefore, we check the number of items in view2 and also wait for that view's
- // finishedPathLoading() signal if the cut items are still there.
- if (viewItems(&view2).count() > 4) {
- waitForFinishedPathLoading(&view2);
- }
- QCOMPARE(viewItems(&view1), QStringList() << "3" << "4" << "a" << "b" << "c" << "d");
- QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "d");
- // The pasted items ("3", "4") should be selected now, and the previous selection ("d") should be cleared.
- QCOMPARE(selectedItems(&view1), QStringList() << "3" << "4");
-
- /** Copy and paste into subfolder */
- view1.clearSelection();
- index = itemView1->model()->index(3, 0);
- itemView1->scrollTo(index);
- QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
- verifySelectedItemsCount(&view1, 1);
- QCOMPARE(selectedItems(&view1), QStringList() << "b");
- view1.copySelectedItems();
- // Now we use view1 to display the subfolder, which is still empty.
- view1.setUrl(KUrl(dir2.name() + "subfolder"));
- waitForFinishedPathLoading(&view1);
- QCOMPARE(viewItems(&view1), QStringList());
- // Select the subfolder.in view2
- view2.clearSelection();
- index = itemView2->model()->index(0, 0);
- itemView2->scrollTo(index);
- QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
- verifySelectedItemsCount(&view2, 1);
- // Paste into the subfolder
- view2.pasteIntoFolder();
- waitForFinishedPathLoading(&view1);
- QCOMPARE(viewItems(&view1), QStringList() << "b");
- // The pasted items in view1 are *not* selected now (because the pasting was done indirectly using view2.pasteIntoFolder()).
-}
-
-// Private member functions which are used by the tests
-
-/**
- * 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 DolphinView's internal item view (icons, details, or columns).
- * Therefore, a pointer to the item view is returned by initView(DolphinView*).
- */
-
-QAbstractItemView* DolphinViewTest_AllViewModes::initView(DolphinView* view) const
-{
- QSignalSpy spyFinishedPathLoading(view, SIGNAL(finishedPathLoading(const KUrl&)));
- view->setMode(mode());
- Q_ASSERT(verifyCorrectViewMode(view));
- view->resize(200, 300);
- view->show();
- QTest::qWaitForWindowShown(view);
-
- // If the DolphinView's finishedPathLoading(const KUrl&) signal has not been received yet,
- // we have to wait a bit more.
- // The reason why the if-statement is needed here is that the signal might have been emitted
- // while we were waiting in QTest::qWaitForWindowShown(view)
- // -> waitForFinishedPathLoading(view) would fail in that case.
- if (spyFinishedPathLoading.isEmpty()) {
- waitForFinishedPathLoading(view);
- }
-
- return itemView(view);
-}
-
-/**
- * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
- * signal is received and checks that the selection state of the view is as expected.
- */
-
-void DolphinViewTest_AllViewModes::verifySelectedItemsCount(DolphinView* view, int itemsCount) const
-{
- QSignalSpy spySelectionChanged(view, SIGNAL(selectionChanged(const KFileItemList&)));
- QVERIFY(QTest::kWaitForSignal(view, SIGNAL(selectionChanged(const KFileItemList&)), 2000));
-
- 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(view->hasSelection());
- }
- else {
- QVERIFY(!view->hasSelection());
- }
-}
-
-#include "dolphinviewtest_allviewmodes.moc"
diff --git a/src/tests/dolphinviewtest_allviewmodes.h b/src/tests/dolphinviewtest_allviewmodes.h
deleted file mode 100644
index d062bb016..000000000
--- a/src/tests/dolphinviewtest_allviewmodes.h
+++ /dev/null
@@ -1,80 +0,0 @@
-/*****************************************************************************
- * Copyright (C) 2010-2011 by Frank Reininghaus ([email protected]) *
- * *
- * 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 DOLPHINVIEWTEST_ALLVIEWMODES
-#define DOLPHINVIEWTEST_ALLVIEWMODES
-
-#include "testbase.h"
-
-#include "views/dolphinview.h"
-
-/**
- * DolphinViewTest_AllViewModes is used as a base class for tests that check the
- * basic functionality of DolphinView in all view modes. The derived classes
- * have to provide implementations for the virtual methods mode() and verifyCorrectViewMode(),
- * see below.
- *
- * Tests for DolphinView functionality that is specific to a particular view mode or
- * to switching between different view modes should not be added here, but to another
- * DolphinView unit test.
- */
-
-class DolphinViewTest_AllViewModes : public TestBase
-{
- Q_OBJECT
-
-public:
-
- DolphinViewTest_AllViewModes();
-
-private slots:
-
- void testSelection();
- void testViewPropertySettings();
- void testZoomLevel();
- void testSaveAndRestoreState();
- void testKeyboardFocus();
- void testCutCopyPaste();
-
-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 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(DolphinView* view, int itemsCount) const;
-
-};
-
-#endif
diff --git a/src/tests/dolphinviewtest_columns.cpp b/src/tests/dolphinviewtest_columns.cpp
deleted file mode 100644
index 40b88a223..000000000
--- a/src/tests/dolphinviewtest_columns.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2010 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-
-#include "dolphinviewtest_allviewmodes.h"
-
-class DolphinViewTest_Columns : public DolphinViewTest_AllViewModes
-{
- Q_OBJECT
-
-public:
-
- virtual DolphinView::Mode mode() const {
- return DolphinView::ColumnView;
- }
-
- virtual bool verifyCorrectViewMode(const DolphinView* view) const {
- return (view->mode() == DolphinView::ColumnView);
- }
-
-};
-
-QTEST_KDEMAIN(DolphinViewTest_Columns, GUI)
-
-#include "dolphinviewtest_columns.moc"
diff --git a/src/tests/dolphinviewtest_details.cpp b/src/tests/dolphinviewtest_details.cpp
deleted file mode 100644
index 0034afd90..000000000
--- a/src/tests/dolphinviewtest_details.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2010 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-
-#include "dolphinviewtest_allviewmodes.h"
-
-class DolphinViewTest_Details : public DolphinViewTest_AllViewModes
-{
- Q_OBJECT
-
-public:
-
- virtual DolphinView::Mode mode() const {
- return DolphinView::DetailsView;
- }
-
- virtual bool verifyCorrectViewMode(const DolphinView* view) const {
- return (view->mode() == DolphinView::DetailsView);
- }
-
-};
-
-QTEST_KDEMAIN(DolphinViewTest_Details, GUI)
-
-#include "dolphinviewtest_details.moc"
diff --git a/src/tests/dolphinviewtest_icons.cpp b/src/tests/dolphinviewtest_icons.cpp
deleted file mode 100644
index 5f928f403..000000000
--- a/src/tests/dolphinviewtest_icons.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-/***************************************************************************
- * Copyright (C) 2010 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-
-#include "dolphinviewtest_allviewmodes.h"
-
-class DolphinViewTest_Icons : public DolphinViewTest_AllViewModes
-{
- Q_OBJECT
-
-public:
-
- virtual DolphinView::Mode mode() const {
- return DolphinView::IconsView;
- }
-
- virtual bool verifyCorrectViewMode(const DolphinView* view) const {
- return (view->mode() == DolphinView::IconsView);
- }
-
-};
-
-QTEST_KDEMAIN(DolphinViewTest_Icons, GUI)
-
-#include "dolphinviewtest_icons.moc"
diff --git a/src/tests/kfileitemlistviewtest.cpp b/src/tests/kfileitemlistviewtest.cpp
new file mode 100644
index 000000000..1b9e1b312
--- /dev/null
+++ b/src/tests/kfileitemlistviewtest.cpp
@@ -0,0 +1,100 @@
+/***************************************************************************
+ * Copyright (C) 2011 by Peter Penz <[email protected]> *
+ * *
+ * 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 <qtest_kde.h>
+
+#include <KDirLister>
+#include "kitemviews/kfileitemlistview.h"
+#include "kitemviews/kfileitemmodel.h"
+#include "testdir.h"
+
+#include <QGraphicsView>
+
+namespace {
+ const int DefaultTimeout = 2000;
+};
+
+class KFileItemListViewTest : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void init();
+ void cleanup();
+
+ void testFeffi();
+
+private:
+ KFileItemListView* m_listView;
+ KFileItemModel* m_model;
+ KDirLister* m_dirLister;
+ TestDir* m_testDir;
+ QGraphicsView* m_graphicsView;
+};
+
+void KFileItemListViewTest::init()
+{
+ qRegisterMetaType<KItemRangeList>("KItemRangeList");
+ qRegisterMetaType<KFileItemList>("KFileItemList");
+
+ m_testDir = new TestDir();
+ m_dirLister = new KDirLister();
+ m_model = new KFileItemModel(m_dirLister);
+
+ m_listView = new KFileItemListView();
+ m_listView->onModelChanged(m_model, 0);
+
+ m_graphicsView = new QGraphicsView();
+ m_graphicsView->show();
+ QTest::qWaitForWindowShown(m_graphicsView);
+}
+
+void KFileItemListViewTest::cleanup()
+{
+ delete m_graphicsView;
+ m_graphicsView = 0;
+
+ delete m_listView;
+ m_listView = 0;
+
+ delete m_model;
+ m_model = 0;
+
+ delete m_dirLister;
+ m_dirLister = 0;
+
+ delete m_testDir;
+ m_testDir = 0;
+}
+
+void KFileItemListViewTest::testFeffi()
+{
+ QStringList files;
+ files << "a.txt" << "b.txt" << "c.txt";
+ m_testDir->createFiles(files);
+
+ m_dirLister->openUrl(m_testDir->url());
+ QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
+
+ QCOMPARE(m_model->count(), 3);
+}
+
+QTEST_KDEMAIN(KFileItemListViewTest, GUI)
+
+#include "kfileitemlistviewtest.moc"
diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp
new file mode 100644
index 000000000..82fc3fbc0
--- /dev/null
+++ b/src/tests/kfileitemmodeltest.cpp
@@ -0,0 +1,210 @@
+/***************************************************************************
+ * Copyright (C) 2011 by Peter Penz <[email protected]> *
+ * *
+ * 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 <qtest_kde.h>
+
+#include <KDirLister>
+#include "kitemviews/kfileitemmodel.h"
+#include "testdir.h"
+
+namespace {
+ const int DefaultTimeout = 2000;
+};
+
+class KFileItemModelTest : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void init();
+ void cleanup();
+
+ void testDefaultRoles();
+ void testDefaultSortRole();
+ void testDefaultGroupRole();
+ void testNewItems();
+ void testInsertingItems();
+
+ void testExpansionLevelsCompare_data();
+ void testExpansionLevelsCompare();
+
+private:
+ bool isModelConsistent() const;
+
+private:
+ KFileItemModel* m_model;
+ KDirLister* m_dirLister;
+ TestDir* m_testDir;
+};
+
+void KFileItemModelTest::init()
+{
+ qRegisterMetaType<KItemRangeList>("KItemRangeList");
+ qRegisterMetaType<KFileItemList>("KFileItemList");
+
+ m_testDir = new TestDir();
+ m_dirLister = new KDirLister();
+ m_model = new KFileItemModel(m_dirLister);
+}
+
+void KFileItemModelTest::cleanup()
+{
+ delete m_model;
+ m_model = 0;
+
+ delete m_dirLister;
+ m_dirLister = 0;
+
+ delete m_testDir;
+ m_testDir = 0;
+}
+
+void KFileItemModelTest::testDefaultRoles()
+{
+ const QSet<QByteArray> roles = m_model->roles();
+ QCOMPARE(roles.count(), 2);
+ QVERIFY(roles.contains("name"));
+ QVERIFY(roles.contains("isDir"));
+}
+
+void KFileItemModelTest::testDefaultSortRole()
+{
+ QCOMPARE(m_model->sortRole(), QByteArray("name"));
+
+ QStringList files;
+ files << "c.txt" << "a.txt" << "b.txt";
+
+ m_testDir->createFiles(files);
+
+ m_dirLister->openUrl(m_testDir->url());
+ QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
+
+ QCOMPARE(m_model->count(), 3);
+ QCOMPARE(m_model->data(0)["name"].toString(), QString("a.txt"));
+ QCOMPARE(m_model->data(1)["name"].toString(), QString("b.txt"));
+ QCOMPARE(m_model->data(2)["name"].toString(), QString("c.txt"));
+}
+
+void KFileItemModelTest::testDefaultGroupRole()
+{
+ QVERIFY(m_model->groupRole().isEmpty());
+}
+
+void KFileItemModelTest::testNewItems()
+{
+ QStringList files;
+ files << "a.txt" << "b.txt" << "c.txt";
+ m_testDir->createFiles(files);
+
+ m_dirLister->openUrl(m_testDir->url());
+ QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
+
+ QCOMPARE(m_model->count(), 3);
+
+ QVERIFY(isModelConsistent());
+}
+
+void KFileItemModelTest::testInsertingItems()
+{
+ // QSKIP("Temporary disabled", SkipSingle);
+
+ // KFileItemModel prevents that inserting a punch of items sequentially
+ // results in an itemsInserted()-signal for each item. Instead internally
+ // a timeout is given that collects such operations and results in only
+ // one itemsInserted()-signal. However in this test we want to stress
+ // KFileItemModel to do a lot of insert operation and hence decrease
+ // the timeout to 1 millisecond.
+ m_model->m_minimumUpdateIntervalTimer->setInterval(1);
+
+ m_testDir->createFile("1");
+ m_dirLister->openUrl(m_testDir->url());
+ QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
+ QCOMPARE(m_model->count(), 1);
+
+ // Insert 10 items for 20 times. After each insert operation the model consistency
+ // is checked.
+ QSet<int> insertedItems;
+ for (int i = 0; i < 20; ++i) {
+ QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
+
+ for (int j = 0; j < 10; ++j) {
+ int itemName = qrand();
+ while (insertedItems.contains(itemName)) {
+ itemName = qrand();
+ }
+ insertedItems.insert(itemName);
+
+ m_testDir->createFile(QString::number(itemName));
+ }
+
+ m_dirLister->updateDirectory(m_testDir->url());
+ if (spy.count() == 0) {
+ QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
+ }
+
+ QVERIFY(isModelConsistent());
+ }
+
+ QCOMPARE(m_model->count(), 201);
+}
+
+void KFileItemModelTest::testExpansionLevelsCompare_data()
+{
+ QTest::addColumn<QString>("urlA");
+ QTest::addColumn<QString>("urlB");
+ QTest::addColumn<int>("result");
+
+ QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
+ QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
+ QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
+}
+
+void KFileItemModelTest::testExpansionLevelsCompare()
+{
+ QFETCH(QString, urlA);
+ QFETCH(QString, urlB);
+ QFETCH(int, result);
+
+ const KFileItem a(KUrl(urlA), QString(), mode_t(-1));
+ const KFileItem b(KUrl(urlB), QString(), mode_t(-1));
+ QCOMPARE(m_model->expansionLevelsCompare(a, b), result);
+}
+
+bool KFileItemModelTest::isModelConsistent() const
+{
+ for (int i = 0; i < m_model->count(); ++i) {
+ const KFileItem item = m_model->fileItem(i);
+ if (item.isNull()) {
+ qWarning() << "Item" << i << "is null";
+ return false;
+ }
+
+ const int itemIndex = m_model->index(item);
+ if (itemIndex != i) {
+ qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
+ return false;
+ }
+ }
+
+ return true;
+}
+
+QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
+
+#include "kfileitemmodeltest.moc"
diff --git a/src/tests/testbase.cpp b/src/tests/testbase.cpp
deleted file mode 100644
index de323aac2..000000000
--- a/src/tests/testbase.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/*****************************************************************************
- * Copyright (C) 2010-2011 by Frank Reininghaus ([email protected]) *
- * *
- * 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 <qtest_kde.h>
-
-#include "views/dolphinview.h"
-#include "views/dolphinmodel.h"
-#include "views/dolphindirlister.h"
-#include "views/dolphinsortfilterproxymodel.h"
-
-#include <QAbstractItemView>
-
-QAbstractItemView* TestBase::itemView(const DolphinView* view)
-{
- return view->m_viewAccessor.itemView();
-}
-
-void TestBase::waitForFinishedPathLoading(DolphinView* view, int milliseconds)
-{
- // If the signal is not received, somthing is going seriously wrong.
- // -> assert here rather than continuing, which might result in test failures which are hard to unterstand.
- bool viewHasFinishedLoading = QTest::kWaitForSignal(view, SIGNAL(finishedPathLoading(const KUrl&)), milliseconds);
- Q_ASSERT(viewHasFinishedLoading);
- Q_UNUSED(viewHasFinishedLoading) // suppress compiler warining is asserts are disabled
-}
-
-void TestBase::reloadViewAndWait(DolphinView* view)
-{
- view->reload();
- waitForFinishedPathLoading(view);
-}
-
-QStringList TestBase::viewItems(const DolphinView* view)
-{
- QStringList itemList;
- const QAbstractItemModel* model = itemView(view)->model();
-
- for (int row = 0; row < model->rowCount(); row++) {
- itemList << model->data(model->index(row, 0), Qt::DisplayRole).toString();
- }
-
- return itemList;
-}
-
-QStringList TestBase::selectedItems(const DolphinView* view)
-{
- QStringList itemList;
- const QAbstractItemModel* model = itemView(view)->model();
- const QModelIndexList selectedIndexes = itemView(view)->selectionModel()->selectedIndexes();
-
- for (int row = 0; row < model->rowCount(); row++) {
- const QModelIndex index = model->index(row, 0);
- if (selectedIndexes.contains(index)) {
- itemList << model->data(model->index(row, 0), Qt::DisplayRole).toString();
- }
- }
-
- return itemList;
-}
diff --git a/src/tests/testbase.h b/src/tests/testbase.h
deleted file mode 100644
index 957d822c3..000000000
--- a/src/tests/testbase.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*****************************************************************************
- * Copyright (C) 2010-2011 by Frank Reininghaus ([email protected]) *
- * *
- * 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 TESTBASE_H
-#define TESTBASE_H
-
-#include <QtCore/QObject>
-
-class QAbstractItemView;
-class DolphinDirLister;
-class DolphinModel;
-class DolphinSortFilterProxyModel;
-class DolphinView;
-
-/**
- * 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!
- */
-
-class TestBase : public QObject
-{
- Q_OBJECT
-
-public:
-
- TestBase() {};
- ~TestBase() {};
-
- /** Returns the item view (icons, details, or columns) */
- static QAbstractItemView* itemView(const DolphinView* view);
-
- /**
- * Waits until the view emits its finishedPathLoading(const KUrl&) signal.
- * Asserts if the signal is not received within the given number of milliseconds.
- */
- static void waitForFinishedPathLoading(DolphinView* view, int milliseconds=20000);
-
- /** Reloads the view and waits for the finishedPathLoading(const KUrl&) signal. */
- static void reloadViewAndWait(DolphinView* view);
-
- /** Returns the items shown in the view. The order corresponds to the sort order of the view. */
- static QStringList viewItems(const DolphinView* view);
-
- /** Returns the items which are selected in the view. The order corresponds to the sort order of the view. */
- static QStringList selectedItems(const DolphinView* view);
-
-};
-
-#endif
diff --git a/src/tests/testdir.cpp b/src/tests/testdir.cpp
index 64d7f1aaa..5e961d2ab 100644
--- a/src/tests/testdir.cpp
+++ b/src/tests/testdir.cpp
@@ -27,8 +27,20 @@
#include <sys/utime.h>
#endif
-/** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
+TestDir::TestDir()
+{
+}
+
+TestDir::~TestDir()
+{
+}
+KUrl TestDir::url() const
+{
+ return KUrl(name());
+}
+
+/** 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
@@ -63,7 +75,7 @@ void TestDir::createFile(const QString& path, const QByteArray& data, const QDat
void TestDir::createFiles(const QStringList& files)
{
- foreach(const QString& path, files) {
+ foreach (const QString& path, files) {
createFile(path);
}
}
diff --git a/src/tests/testdir.h b/src/tests/testdir.h
index bff462690..bcaa034b5 100644
--- a/src/tests/testdir.h
+++ b/src/tests/testdir.h
@@ -29,29 +29,27 @@
* 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();
+ virtual ~TestDir();
- TestDir() {}
- ~TestDir() {}
-
- KUrl url() const { return KUrl(name()); }
+ KUrl url() 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 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);
};