From 843d10ae2ab6a7f2c6f69b63fa278fdd6162254c Mon Sep 17 00:00:00 2001 From: Tomasz Kot Date: Fri, 28 Nov 2025 16:11:00 +0100 Subject: Add keyboard anchor assignments to mouse events The mouse events need to modify the keyboard anchor assignments as well, because selecting an item with a mouse and then navigating with keyboard wouldn't follow the same selection. BUG: 508609 --- src/tests/kitemlistcontrollertest.cpp | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'src/tests') diff --git a/src/tests/kitemlistcontrollertest.cpp b/src/tests/kitemlistcontrollertest.cpp index 8fd0f6158..a38e7803d 100644 --- a/src/tests/kitemlistcontrollertest.cpp +++ b/src/tests/kitemlistcontrollertest.cpp @@ -78,6 +78,7 @@ private Q_SLOTS: void testKeyboardNavigationSingleSelectionNoSelection_data(); void testKeyboardNavigationSingleSelectionNoSelection(); void testMouseClickActivation(); + void testKeyboardNavigationAfterMouseSelection(); private: /** @@ -85,6 +86,7 @@ private: * by changing the geometry of the container. */ void adjustGeometryForColumnCount(int count); + void simulateMouseClickOnItem(int index); private: KFileItemListView *m_view; @@ -1151,6 +1153,76 @@ void KItemListControllerTest::testMouseClickActivation() m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick); } +/** + * This function simulates a mouse click on an item under a given index. + */ +void KItemListControllerTest::simulateMouseClickOnItem(int index) +{ + const QPointF pos = m_view->itemContextRect(index).center(); + QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress); + mousePressEvent.setPos(pos); + mousePressEvent.setButton(Qt::LeftButton); + mousePressEvent.setButtons(Qt::LeftButton); + + QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease); + mouseReleaseEvent.setPos(pos); + mouseReleaseEvent.setButton(Qt::LeftButton); + mouseReleaseEvent.setButtons(Qt::NoButton); + + m_view->event(&mousePressEvent); + m_view->event(&mouseReleaseEvent); +} + +void KItemListControllerTest::testKeyboardNavigationAfterMouseSelection() +{ + QApplication::setLayoutDirection(Qt::LeftToRight); + m_view->setLayoutDirection(Qt::LeftToRight); + m_view->setItemLayout(KFileItemListView::IconsLayout); + m_model->setGroupedSorting(false); + + adjustGeometryForColumnCount(3); + QCOMPARE(m_view->m_layouter->m_columnCount, 3); + + m_view->setScrollOffset(0); + QCOMPARE(m_view->firstVisibleIndex(), 0); + + simulateMouseClickOnItem(0); + QCOMPARE(m_selectionManager->currentItem(), 0); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 3); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 6); + + simulateMouseClickOnItem(1); + QCOMPARE(m_selectionManager->currentItem(), 1); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 4); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 7); + + simulateMouseClickOnItem(2); + QCOMPARE(m_selectionManager->currentItem(), 2); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 5); + + QTest::keyClick(m_container, Qt::Key_Down); + QCOMPARE(m_selectionManager->currentItem(), 8); + + simulateMouseClickOnItem(10); + QCOMPARE(m_selectionManager->currentItem(), 10); + + QTest::keyClick(m_container, Qt::Key_Up); + QCOMPARE(m_selectionManager->currentItem(), 7); + + QTest::keyClick(m_container, Qt::Key_Up); + QCOMPARE(m_selectionManager->currentItem(), 4); +} + void KItemListControllerTest::adjustGeometryForColumnCount(int count) { const QSize size = m_view->itemSize().toSize(); -- cgit v1.3