X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/30a807e44afb334dd153c8bcbdbde4f36942bee0..6e752f507a1dd82a40d4bd140457203842fc0c80:/src/tests/dolphinmainwindowtest.cpp diff --git a/src/tests/dolphinmainwindowtest.cpp b/src/tests/dolphinmainwindowtest.cpp index e6c355a87..94e6d5be4 100644 --- a/src/tests/dolphinmainwindowtest.cpp +++ b/src/tests/dolphinmainwindowtest.cpp @@ -9,12 +9,18 @@ #include "dolphintabpage.h" #include "dolphintabwidget.h" #include "dolphinviewcontainer.h" +#include "kitemviews/kfileitemmodel.h" #include "kitemviews/kitemlistcontainer.h" +#include "kitemviews/kitemlistcontroller.h" +#include "kitemviews/kitemlistselectionmanager.h" #include "testdir.h" #include +#include +#include #include +#include #include #include #include @@ -39,10 +45,12 @@ private Q_SLOTS: void testNewFileMenuEnabled(); void testWindowTitle_data(); void testWindowTitle(); + void testFocusPlacesPanel(); void testPlacesPanelWidthResistance(); void testGoActions(); void testOpenFiles(); void testAccessibilityAncestorTree(); + void testAutoSaveSession(); void cleanupTestCase(); private: @@ -208,7 +216,8 @@ void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView() // Test case for bug #397910 void DolphinMainWindowTest::testOpenInNewTabTitle() { - m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false); + 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()); @@ -216,13 +225,14 @@ void DolphinMainWindowTest::testOpenInNewTabTitle() auto tabWidget = m_mainWindow->findChild("tabWidget"); QVERIFY(tabWidget); - tabWidget->openNewTab(QUrl::fromLocalFile(QDir::tempPath())); + const QUrl tempPathUrl{QUrl::fromLocalFile(QDir::tempPath())}; + tabWidget->openNewTab(tempPathUrl); QCOMPARE(tabWidget->count(), 2); QVERIFY(tabWidget->tabText(0) != tabWidget->tabText(1)); - if (!tabWidget->tabIcon(0).isNull() && !tabWidget->tabIcon(1).isNull()) { - QCOMPARE(QStringLiteral("inode-directory"), tabWidget->tabIcon(0).name()); - QCOMPARE(QStringLiteral("inode-directory"), tabWidget->tabIcon(1).name()); - } + + QVERIFY2(!tabWidget->tabIcon(0).isNull() && !tabWidget->tabIcon(1).isNull(), "Tabs are supposed to have icons."); + QCOMPARE(KIO::iconNameForUrl(homePathUrl), tabWidget->tabIcon(0).name()); + QCOMPARE(KIO::iconNameForUrl(tempPathUrl), tabWidget->tabIcon(1).name()); } void DolphinMainWindowTest::testNewFileMenuEnabled_data() @@ -273,6 +283,44 @@ void DolphinMainWindowTest::testWindowTitle() QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle); } +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. @@ -354,7 +402,7 @@ void DolphinMainWindowTest::testGoActions() testDir->createDir("b/b-1"); testDir->createFile("b/b-2"); testDir->createDir("c"); - QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b")); + const QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b")); m_mainWindow->openDirectories({childDirUrl}, false); // Open "b" dir m_mainWindow->show(); QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); @@ -375,24 +423,40 @@ void DolphinMainWindowTest::testGoActions() const QUrl parentDirUrl = m_mainWindow->activeViewContainer()->url(); QVERIFY(parentDirUrl != childDirUrl); - // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected. - // To test if it has keyboard focus, we press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is actually selected. + auto currentItemUrl = [this]() { + const int currentIndex = m_mainWindow->m_activeViewContainer->view()->m_container->controller()->selectionManager()->currentItem(); + const KFileItem currentItem = m_mainWindow->m_activeViewContainer->view()->m_model->fileItem(currentIndex); + return currentItem.url(); + }; + + QCOMPARE(currentItemUrl(), childDirUrl); // The item we just emerged from should now have keyboard focus. + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // The item we just emerged from should not be selected. BUG: 424723 + // Pressing arrow keys should not only move the keyboard focus but also select the item. + // We press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is selected for the first time. m_mainWindow->actionCollection()->action(QStringLiteral("compact"))->trigger(); QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier); QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); + QVERIFY2(currentItemUrl() != childDirUrl, "The current item didn't change after pressing the 'Down' key."); QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Up, Qt::NoModifier); QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); + QCOMPARE(currentItemUrl(), childDirUrl); // After pressing 'Down' and then 'Up' we should be back where we were. + + // Enter the child folder "b". QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::NoModifier); QVERIFY(spyDirectoryLoadingCompleted.wait()); QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl); QVERIFY(m_mainWindow->isUrlOpen(childDirUrl.toString())); - // Go back to the parent folder + // Go back to the parent folder. m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger(); QVERIFY(spyDirectoryLoadingCompleted.wait()); QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second. QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl); QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString())); + // Going 'Back' means that the view should be in the same state it was in when we left. + QCOMPARE(currentItemUrl(), childDirUrl); // The item we last interacted with in this location should still have keyboard focus. + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), childDirUrl); // It should still be selected. // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything. QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::ControlModifier); // Open new inactive tab @@ -406,6 +470,26 @@ void DolphinMainWindowTest::testGoActions() QVERIFY(spyDirectoryLoadingCompleted.wait()); QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl); QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection. + QCOMPARE(currentItemUrl(), QUrl(QDir::cleanPath(testDir->url().toString() + "/b/b-1"))); // The first item in the view should have keyboard focus. + + // Press the 'Down' key in the child folder. + QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier); + // The second item in the view should have keyboard focus and be selected. + const QUrl secondItemInChildFolderUrl{QDir::cleanPath(testDir->url().toString() + "/b/b-2")}; + QCOMPARE(currentItemUrl(), secondItemInChildFolderUrl); + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl); + + // Go back to the parent folder and then re-enter the child folder. + m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger(); + QVERIFY(spyDirectoryLoadingCompleted.wait()); + m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger(); + QVERIFY(spyDirectoryLoadingCompleted.wait()); + QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl); + // The state of the view should be identical to how it was before we triggered "Back" and then "Forward". + QTRY_COMPARE(currentItemUrl(), secondItemInChildFolderUrl); + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1); + QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl); // Go back to the parent folder. m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger(); @@ -535,32 +619,86 @@ void DolphinMainWindowTest::testAccessibilityAncestorTree() QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); QVERIFY(m_mainWindow->isVisible()); - std::set testedObjects; // Makes sure we stop testing if we arrive at an item that was already tested. QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get()); Q_CHECK_PTR(accessibleInterfaceOfMainWindow); - // We will do accessibility checks for every object that gets focus. Focus will be changed using the Tab key. - while (qApp->focusObject() && !testedObjects.count(qApp->focusObject())) { - const auto currentlyFocusedObject = qApp->focusObject(); - 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. - while (accessibleInterface->childCount() > 0) { - accessibleInterface = accessibleInterface->child(0); + // We will test the accessibility of objects traversing forwards and backwards. + int testedObjectsSizeAfterTraversingForwards = 0; + for (int i = 0; i < 2; i++) { + std::tuple focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::NoModifier}; + if (i) { + focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::ShiftModifier}; } - 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.") - .arg(currentlyFocusedObject->metaObject()->className()))); - accessibleInterface = accessibleInterface->parent(); + + // We will do 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(); + + 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. + while (accessibleInterface->childCount() > 0) { + accessibleInterface = accessibleInterface->child(0); + } + 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.") + .arg(currentlyFocusedObject->metaObject()->className()))); + accessibleInterface = accessibleInterface->parent(); + } + + testedObjects.insert(currentlyFocusedObject); // Add it to testedObjects so we won't test it again later. + QTest::keyClick(m_mainWindow.get(), std::get<0>(focusChainTraversalKeyCombination), std::get<1>(focusChainTraversalKeyCombination)); + QVERIFY2(currentlyFocusedObject != qApp->focusObject(), + "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination."); } - testedObjects.insert(currentlyFocusedObject); // Add it to testedObjects so we won't test it again later. - QTest::keyClick(m_mainWindow.get(), Qt::Key::Key_Tab, Qt::ShiftModifier); // ShiftModifier because the Tab cycle is currently broken going forward. + if (i == 0) { + testedObjectsSizeAfterTraversingForwards = testedObjects.size(); + } else { + QCOMPARE(testedObjects.size(), testedObjectsSizeAfterTraversingForwards); // The size after traversing backwards is different than + // after going forwards which is probably not intended. + } } } +void DolphinMainWindowTest::testAutoSaveSession() +{ + m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false); + m_mainWindow->show(); + QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); + QVERIFY(m_mainWindow->isVisible()); + + // Create config file + KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin")); + KConfig *config = KConfigGui::sessionConfig(); + m_mainWindow->saveGlobalProperties(config); + m_mainWindow->savePropertiesInternal(config, 1); + config->sync(); + + // Setup watcher for config file changes + const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/" + KConfigGui::sessionConfig()->name(); + QFileSystemWatcher *configWatcher = new QFileSystemWatcher({configFileName}, this); + QSignalSpy spySessionSaved(configWatcher, &QFileSystemWatcher::fileChanged); + + // Enable session autosave. + m_mainWindow->setSessionAutoSaveEnabled(true); + m_mainWindow->m_sessionSaveTimer->setInterval(200); // Lower the interval to speed up the testing + + // Open a new tab + auto tabWidget = m_mainWindow->findChild("tabWidget"); + QVERIFY(tabWidget); + tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath())); + QCOMPARE(tabWidget->count(), 2); + + // Wait till a session save occurs + QVERIFY(spySessionSaved.wait(60000)); + + // Disable session autosave. + m_mainWindow->setSessionAutoSaveEnabled(false); +} + void DolphinMainWindowTest::cleanupTestCase() { m_mainWindow->showNormal();