SVN commit 1214822 by freininghaus: DolphinTreeViewTest: Add unit test for bug 220898 (rubberband selection failure after a keyboard focus change in between key press and key release events) M +78 -1 dolphintreeviewtest.cpp --- trunk/KDE/kdebase/apps/dolphin/src/tests/dolphintreeviewtest.cpp #1214821:1214822 @@ -36,6 +36,7 @@ void testKeyboardNavigationSelectionUpdate(); void bug218114_visualRegionForSelection(); + void bug220898_focusOut(); private: @@ -333,6 +334,82 @@ 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"