]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/dolphintreeviewtest.cpp
DolphinTreeViewTest: Add unit test for bug 220898 (rubberband
[dolphin.git] / src / tests / dolphintreeviewtest.cpp
index 8e524293670a39dc62e3b7b257b2d3f0cdf032d2..18069125a62eddb5692be601cf0ab9913fb50132 100644 (file)
@@ -1,24 +1,25 @@
-/***************************************************************************
- *   Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com)    *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*****************************************************************************
+ *   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 <qtest_kde.h>
-#include <kdebug.h> 
+#include <kdebug.h>
+#include <kaction.h>
 
 #include "views/dolphintreeview.h"
 
@@ -35,6 +36,7 @@ private slots:
     void testKeyboardNavigationSelectionUpdate();
 
     void bug218114_visualRegionForSelection();
+    void bug220898_focusOut();
 
 private:
 
@@ -99,10 +101,40 @@ public:
  * is why DolphinTreeView has some custom code for this. The test verifies that this
  * works without unwanted side effects.
  *
- * TODO: Add test for deletion of multiple files if Shift-Delete is pressed for some time, see
- * https://bugs.kde.org/show_bug.cgi?id=259656
+ * 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";
@@ -113,7 +145,7 @@ void DolphinTreeViewTest::testKeyboardNavigationSelectionUpdate() {
         index[i] = model.index(i, 0);
     }
 
-    DolphinTreeView view;
+    TreeViewWithDeleteShortcut view;
     view.setModel(&model);
     view.setSelectionMode(QAbstractItemView::ExtendedSelection);
     view.resize(400, 400);
@@ -225,6 +257,29 @@ void DolphinTreeViewTest::testKeyboardNavigationSelectionUpdate() {
     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);
 }
 
 /**
@@ -279,6 +334,82 @@ void DolphinTreeViewTest::bug218114_visualRegionForSelection()
     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() || !widget.hasFocus()) {
+        QTest::qWait(10);
+    }
+    QVERIFY(widget.hasFocus());
+    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 (!view.viewport()->hasFocus() || widget.hasFocus()) {
+        QTest::qWait(10);
+    }
+    QVERIFY(!widget.hasFocus());
+    QVERIFY(view.viewport()->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"
\ No newline at end of file
+#include "dolphintreeviewtest.moc"