X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/122fee5625f0285ec4ebda79162c72390989eb2a..b61721e2acff4081f2fc3402b8eeacf32acbb5f7:/src/tests/dolphinmainwindowtest.cpp diff --git a/src/tests/dolphinmainwindowtest.cpp b/src/tests/dolphinmainwindowtest.cpp index 2d90ae52f..cd68bb893 100644 --- a/src/tests/dolphinmainwindowtest.cpp +++ b/src/tests/dolphinmainwindowtest.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -27,6 +28,7 @@ #include #include +#include class DolphinMainWindowTest : public QObject { @@ -35,6 +37,7 @@ class DolphinMainWindowTest : public QObject private Q_SLOTS: void initTestCase(); void init(); + void testSyncDesktopAndPhoneUi(); void testClosingTabsWithSearchBoxVisible(); void testActiveViewAfterClosingSplitView_data(); void testActiveViewAfterClosingSplitView(); @@ -45,10 +48,12 @@ private Q_SLOTS: void testNewFileMenuEnabled(); void testWindowTitle_data(); void testWindowTitle(); + void testFocusLocationBar(); + void testFocusPlacesPanel(); void testPlacesPanelWidthResistance(); void testGoActions(); void testOpenFiles(); - void testAccessibilityAncestorTree(); + void testAccessibilityTree(); void testAutoSaveSession(); void cleanupTestCase(); @@ -66,6 +71,109 @@ void DolphinMainWindowTest::init() m_mainWindow.reset(new DolphinMainWindow()); } +/** + * It is too easy to forget that most changes in dolphinui.rc should be mirrored in dolphinuiforphones.rc. This test makes sure that these two files stay + * mostly identical. Differences between those files need to be explicitly added as exceptions to this test. So if you land here after changing either + * dolphinui.rc or dolphinuiforphones.rc, then resolve this test failure either by making the exact same change to the other ui.rc file, or by adding the + * changed object to the `exceptions` variable below. + */ +void DolphinMainWindowTest::testSyncDesktopAndPhoneUi() +{ + std::unordered_set exceptions{{QStringLiteral("version"), QStringLiteral("ToolBar")}}; + + QDomDocument desktopUi; + QFile desktopUiXmlFile(":/kxmlgui5/dolphin/dolphinui.rc"); + desktopUiXmlFile.open(QIODevice::ReadOnly); + desktopUi.setContent(&desktopUiXmlFile); + desktopUiXmlFile.close(); + + QDomDocument phoneUi; + QFile phoneUiXmlFile(":/kxmlgui5/dolphin/dolphinuiforphones.rc"); + phoneUiXmlFile.open(QIODevice::ReadOnly); + phoneUi.setContent(&phoneUiXmlFile); + phoneUiXmlFile.close(); + + QDomElement desktopUiElement = desktopUi.documentElement(); + QDomElement phoneUiElement = phoneUi.documentElement(); + + auto nextUiElement = [&exceptions](QDomElement uiElement) -> QDomElement { + QDomNode nextUiNode{uiElement}; + do { + // If the current node is an exception, we skip its children as well. + if (exceptions.count(nextUiNode.nodeName()) == 0) { + auto firstChild{nextUiNode.firstChild()}; + if (!firstChild.isNull()) { + nextUiNode = firstChild; + continue; + } + } + auto nextSibling{nextUiNode.nextSibling()}; + if (!nextSibling.isNull()) { + nextUiNode = nextSibling; + continue; + } + auto parent{nextUiNode.parentNode()}; + while (true) { + if (parent.isNull()) { + return QDomElement(); + } + auto nextParentSibling{parent.nextSibling()}; + if (!nextParentSibling.isNull()) { + nextUiNode = nextParentSibling; + break; + } + parent = parent.parentNode(); + } + } while ( + !nextUiNode.isNull() + && (nextUiNode.toElement().isNull() || exceptions.count(nextUiNode.nodeName()))); // We loop until we either give up finding an element or find one. + if (nextUiNode.isNull()) { + return QDomElement(); + } + return nextUiNode.toElement(); + }; + + int totalComparisonsCount{0}; + do { + QVERIFY2(desktopUiElement.tagName() == phoneUiElement.tagName(), + qPrintable(QStringLiteral("Node mismatch: dolphinui.rc/%1::%2 and dolphinuiforphones.rc/%3::%4") + .arg(desktopUiElement.parentNode().toElement().tagName()) + .arg(desktopUiElement.tagName()) + .arg(phoneUiElement.parentNode().toElement().tagName()) + .arg(phoneUiElement.tagName()))); + QCOMPARE(desktopUiElement.text(), phoneUiElement.text()); + const auto desktopUiElementAttributes = desktopUiElement.attributes(); + const auto phoneUiElementAttributes = phoneUiElement.attributes(); + for (int i = 0; i < desktopUiElementAttributes.count(); i++) { + QVERIFY2(phoneUiElementAttributes.count() >= i, + qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has more attributes than dolphinuiforphones.rc/%3::%4") + .arg(desktopUiElement.parentNode().toElement().tagName()) + .arg(desktopUiElement.tagName()) + .arg(phoneUiElement.parentNode().toElement().tagName()) + .arg(phoneUiElement.tagName()))); + if (exceptions.count(desktopUiElementAttributes.item(i).nodeName())) { + continue; + } + QCOMPARE(desktopUiElementAttributes.item(i).nodeName(), phoneUiElementAttributes.item(i).nodeName()); + QCOMPARE(desktopUiElementAttributes.item(i).nodeValue(), phoneUiElementAttributes.item(i).nodeValue()); + totalComparisonsCount++; + } + QVERIFY2(desktopUiElementAttributes.count() == phoneUiElementAttributes.count(), + qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has fewer attributes than dolphinuiforphones.rc/%3::%4. %5 < %6") + .arg(desktopUiElement.parentNode().toElement().tagName()) + .arg(desktopUiElement.tagName()) + .arg(phoneUiElement.parentNode().toElement().tagName()) + .arg(phoneUiElement.tagName()) + .arg(phoneUiElementAttributes.count()) + .arg(desktopUiElementAttributes.count()))); + + desktopUiElement = nextUiElement(desktopUiElement); + phoneUiElement = nextUiElement(phoneUiElement); + totalComparisonsCount++; + } while (!desktopUiElement.isNull() || !phoneUiElement.isNull()); + QVERIFY2(totalComparisonsCount > 200, qPrintable(QStringLiteral("There were only %1 comparisons. Did the test run correctly?").arg(totalComparisonsCount))); +} + // See https://bugs.kde.org/show_bug.cgi?id=379135 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible() { @@ -282,6 +390,74 @@ void DolphinMainWindowTest::testWindowTitle() QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle); } +void DolphinMainWindowTest::testFocusLocationBar() +{ + const QUrl homePathUrl{QUrl::fromLocalFile(QDir::homePath())}; + m_mainWindow->openDirectories({homePathUrl}, false); + m_mainWindow->show(); + QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); + QVERIFY(m_mainWindow->isVisible()); + + QAction *replaceLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("replace_location")); + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus()); + + QAction *editableLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("editable_location")); + editableLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable()); + editableLocationAction->trigger(); + QVERIFY(!m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable()); + + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + + // Pressing Escape multiple times should eventually move the focus back to the active view. + QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape); // Focus might not go the view yet because it toggles the editable state of the location bar. + QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape); + QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus()); +} + +void DolphinMainWindowTest::testFocusPlacesPanel() +{ + m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false); + m_mainWindow->show(); + QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); + QVERIFY(m_mainWindow->isVisible()); + + QWidget *placesPanel = reinterpret_cast(m_mainWindow->m_placesPanel); + QVERIFY2(QTest::qWaitFor( + [&]() { + return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0 && placesPanel->height() > 0; + }, + 5000), + "The test couldn't be initialised properly. The places panel should be visible."); + + QAction *focusPlacesPanelAction = m_mainWindow->actionCollection()->action(QStringLiteral("focus_places_panel")); + QAction *showPlacesPanelAction = m_mainWindow->actionCollection()->action(QStringLiteral("show_places_panel")); + + focusPlacesPanelAction->trigger(); + QVERIFY(placesPanel->hasFocus()); + + focusPlacesPanelAction->trigger(); + QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()), + "Triggering focus_places_panel while the panel already has focus should return the focus to the view."); + + focusPlacesPanelAction->trigger(); + QVERIFY(placesPanel->hasFocus()); + + showPlacesPanelAction->trigger(); + QVERIFY(!placesPanel->isVisible()); + QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()), + "Hiding the Places panel while it has focus should return the focus to the view."); + + showPlacesPanelAction->trigger(); + QVERIFY(placesPanel->isVisible()); + QVERIFY2(placesPanel->hasFocus(), "Enabling the Places panel should move keyboard focus there."); +} + /** * The places panel will resize itself if any of the other widgets requires too much horizontal space * but a user never wants the size of the places panel to change unless they resized it themselves explicitly. @@ -573,7 +749,7 @@ void DolphinMainWindowTest::testOpenFiles() QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); } -void DolphinMainWindowTest::testAccessibilityAncestorTree() +void DolphinMainWindowTest::testAccessibilityTree() { m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false); m_mainWindow->show(); @@ -583,7 +759,7 @@ void DolphinMainWindowTest::testAccessibilityAncestorTree() QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get()); Q_CHECK_PTR(accessibleInterfaceOfMainWindow); - // We will test the accessibility of objects traversing forwards and backwards. + /// Test the accessibility of objects while traversing forwards (Tab key) and backwards (Shift+Tab). int testedObjectsSizeAfterTraversingForwards = 0; for (int i = 0; i < 2; i++) { std::tuple focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::NoModifier}; @@ -591,11 +767,42 @@ void DolphinMainWindowTest::testAccessibilityAncestorTree() focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::ShiftModifier}; } - // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination. + /// @see firstNamedAncestor below. + QAccessibleInterface *firstNamedAncestorOfPreviousIteration = nullptr; + + /// Perform accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination. std::set testedObjects; // Makes sure we stop testing when we arrive at an item that was already tested. while (qApp->focusObject() && !testedObjects.count(qApp->focusObject())) { const auto currentlyFocusedObject = qApp->focusObject(); + const QAccessibleInterface *accessibleIntefaceOfCurrentlyFocusedObject = QAccessible::queryAccessibleInterface(currentlyFocusedObject); + QVERIFY(accessibleIntefaceOfCurrentlyFocusedObject); + + /// Test that each object reachable by Tab or Shift+Tab has at least some accessible information. Objects without any accessible information + /// are even less useful to accessibility software users than unlabeled buttons are e.g. to sighted users, because unlabeled buttons at least + /// convey some information through their placement and icon. + if (currentlyFocusedObject != m_mainWindow->m_activeViewContainer->view()->m_container) { // Skip the custom container widget which has no + // accessible name on purpose. + /** + * The first ancestor with an accessible name is interesting because it is sometimes used to identify an object if the object itself has no + * name. We keep it in mind to check if two subsequent objects without a name can at least be told apart by their first named ancestor. + */ + QAccessibleInterface *firstNamedAncestor = accessibleIntefaceOfCurrentlyFocusedObject->parent(); + while (firstNamedAncestor) { + if (!firstNamedAncestor->text(QAccessible::Name).isEmpty()) { + break; + } + firstNamedAncestor = firstNamedAncestor->parent(); + } + QTRY_VERIFY2(!accessibleIntefaceOfCurrentlyFocusedObject->text(QAccessible::Name).isEmpty() + || (firstNamedAncestor && firstNamedAncestor != firstNamedAncestorOfPreviousIteration), + qPrintable(QStringLiteral("%1's accessibleInterface does not have an accessible name and can not be distinguished from the object" + " that had focus previously. Please fix this. You can find this %1 within its parent %2.") + .arg(currentlyFocusedObject->metaObject()->className()) + .arg(currentlyFocusedObject->parent()->metaObject()->className()))); + firstNamedAncestorOfPreviousIteration = firstNamedAncestor; + } + /// Test that each accessible interface has the main window as its parent. QAccessibleInterface *accessibleInterface = QAccessible::queryAccessibleInterface(currentlyFocusedObject); // The accessibleInterfaces of focused objects might themselves have children. // We go down that hierarchy as far as possible and then test the ancestor tree from there. @@ -604,7 +811,7 @@ void DolphinMainWindowTest::testAccessibilityAncestorTree() } while (accessibleInterface != accessibleInterfaceOfMainWindow) { QVERIFY2(accessibleInterface, - qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.") + qPrintable(QStringLiteral("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.") .arg(currentlyFocusedObject->metaObject()->className()))); accessibleInterface = accessibleInterface->parent(); } @@ -622,6 +829,8 @@ void DolphinMainWindowTest::testAccessibilityAncestorTree() // after going forwards which is probably not intended. } } + QCOMPARE_GE(testedObjectsSizeAfterTraversingForwards, 12); // The test did not reach many objects while using the Tab key to move through Dolphin. Did the + // test run correctly? } void DolphinMainWindowTest::testAutoSaveSession()