+void DolphinMainWindowTest::testOpenFiles()
+{
+ QScopedPointer<TestDir> testDir{new TestDir()};
+ QString testDirUrl(QDir::cleanPath(testDir->url().toString()));
+ testDir->createDir("a");
+ testDir->createDir("a/b");
+ testDir->createDir("a/b/c");
+ testDir->createDir("a/b/c/d");
+ m_mainWindow->openDirectories({testDirUrl}, false);
+ m_mainWindow->show();
+
+ // We only see the unselected "a" folder in the test dir. There are no other tabs.
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
+ QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
+ QVERIFY(!m_mainWindow->isUrlOpen(testDirUrl + "/a"));
+ QVERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
+ QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
+
+ // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
+ m_mainWindow->openFiles({testDirUrl + "/a"}, false);
+ QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
+ QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
+
+ // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
+ m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
+ QTRY_VERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
+ QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
+ QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
+ QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
+
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
+ QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
+ // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
+ m_mainWindow->openFiles({testDirUrl + "/a"}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
+
+ // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
+ m_mainWindow->openDirectories({testDirUrl + "/a"}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
+
+ // In the details view mode directories can be expanded, which changes if openFiles() needs to open a new tab or not to open a file.
+ m_mainWindow->actionCollection()->action(QStringLiteral("details"))->trigger();
+ QTRY_VERIFY(m_mainWindow->activeViewContainer()->view()->itemsExpandable());
+
+ // Expand the already selected "b" with the right arrow key. This should make "c" visible.
+ QVERIFY2(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
+ QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Right);
+ QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
+ QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
+
+ // Switch to first tab by opening it even though it is already open.
+ m_mainWindow->openDirectories({testDirUrl}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
+
+ // "c" is in view in the second tab because "b" is expanded there, so "opening" "c" should switch to that tab and select "c" there.
+ m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
+ QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
+
+ // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
+ // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
+ m_mainWindow->openDirectories({testDirUrl + "/a/b/c/d", testDirUrl + "/a/b/c"}, true);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
+ QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->splitViewEnabled());
+ QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c")); // It should still be visible in the second tab.
+ QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c/d"));
+ QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c"));
+
+ // "c" is in view in the second tab because "b" is expanded there,
+ // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
+ m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
+ QVERIFY2(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
+
+ // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
+ m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
+ QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Left);
+ QTRY_VERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
+
+ // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
+ m_mainWindow->openFiles({testDirUrl + "/a/b/c/d"}, false);
+ QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
+ QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
+ QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->secondaryViewContainer()->isActive());
+ QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
+}
+
+void DolphinMainWindowTest::testAccessibilityTree()
+{
+ m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
+ m_mainWindow->show();
+ QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
+ QVERIFY(m_mainWindow->isVisible());
+
+ QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get());
+ Q_CHECK_PTR(accessibleInterfaceOfMainWindow);
+
+ /// 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<Qt::Key, Qt::KeyboardModifier> focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::NoModifier};
+ if (i) {
+ focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::ShiftModifier};
+ }
+
+ /// @see firstNamedAncestor below.
+ QAccessibleInterface *firstNamedAncestorOfPreviousIteration = nullptr;
+
+ /// Perform accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
+ std::set<const QObject *> 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.
+ while (accessibleInterface->childCount() > 0) {
+ accessibleInterface = accessibleInterface->child(0);
+ }
+ while (accessibleInterface != accessibleInterfaceOfMainWindow) {
+ QVERIFY2(accessibleInterface,
+ 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();
+ }
+
+ 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.");
+ }
+
+ 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.
+ }
+ }
+ QCOMPARE_GE(testedObjectsSizeAfterTraversingForwards, 11); // The test did not reach many objects while using the Tab key to move through Dolphin. Did the
+ // test run correctly?
+}
+
+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<DolphinTabWidget *>("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::testInlineRename()
+{
+ QScopedPointer<TestDir> testDir{new TestDir()};
+ testDir->createFiles({"aaaa", "bbbb", "cccc", "dddd"});
+ m_mainWindow->openDirectories({testDir->url()}, false);
+ m_mainWindow->show();
+ QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
+ QVERIFY(m_mainWindow->isVisible());
+
+ DolphinView *view = m_mainWindow->activeViewContainer()->view();
+ QSignalSpy viewDirectoryLoadingCompletedSpy(view, &DolphinView::directoryLoadingCompleted);
+ QSignalSpy itemsReorderedSpy(view->m_model, &KFileItemModel::itemsMoved);
+ QSignalSpy modelDirectoryLoadingCompletedSpy(view->m_model, &KFileItemModel::directoryLoadingCompleted);
+
+ QVERIFY(viewDirectoryLoadingCompletedSpy.wait());
+ QTest::qWait(500); // we need to wait for the file widgets to become visible
+ view->markUrlsAsSelected({QUrl(testDir->url().toString() + "/aaaa")});
+ view->updateViewState();
+ view->renameSelectedItems();
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_E);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
+
+ QVERIFY(itemsReorderedSpy.wait());
+ QVERIFY(view->m_view->m_editingRole);
+ KItemListWidget *widget = view->m_view->m_visibleItems.value(view->m_view->firstVisibleIndex());
+ QVERIFY(!widget->editedRole().isEmpty());
+
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_A);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_A);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
+
+ QVERIFY(itemsReorderedSpy.wait());
+ QVERIFY(view->m_view->m_editingRole);
+ widget = view->m_view->m_visibleItems.value(view->m_view->lastVisibleIndex());
+ QVERIFY(!widget->editedRole().isEmpty());
+
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape);
+ QVERIFY(widget->isCurrent());
+ view->m_model->refreshDirectory(testDir->url());
+ QVERIFY(modelDirectoryLoadingCompletedSpy.wait());
+
+ QCOMPARE(view->m_model->fileItem(0).name(), "abbbb");
+ QCOMPARE(view->m_model->fileItem(1).name(), "adddd");
+ QCOMPARE(view->m_model->fileItem(2).name(), "cccc");
+ QCOMPARE(view->m_model->fileItem(3).name(), "eaaaa");
+ QCOMPARE(view->m_model->count(), 4);
+}
+
+void DolphinMainWindowTest::testThumbnailAfterRename()
+{
+ // Create testdir and red square jpg for testing
+ QScopedPointer<TestDir> testDir{new TestDir()};
+ QImage testImage(256, 256, QImage::Format_Mono);
+ testImage.setColorCount(1);
+ testImage.setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
+ for (short x = 0; x < 256; ++x) {
+ for (short y = 0; y < 256; ++y) {
+ testImage.setPixel(x, y, 0);
+ }
+ }
+ testImage.save(testDir.data()->path() + "/a.jpg");
+
+ // Open dir and show it
+ m_mainWindow->openDirectories({testDir->url()}, false);
+ DolphinView *view = m_mainWindow->activeViewContainer()->view();
+ // Prepare signal spies
+ QSignalSpy viewDirectoryLoadingCompletedSpy(view, &DolphinView::directoryLoadingCompleted);
+ QSignalSpy itemsChangedSpy(view->m_model, &KFileItemModel::itemsChanged);
+ QSignalSpy modelDirectoryLoadingCompletedSpy(view->m_model, &KFileItemModel::directoryLoadingCompleted);
+ QSignalSpy previewUpdatedSpy(view->m_view->m_modelRolesUpdater, &KFileItemModelRolesUpdater::previewJobFinished);
+ // Show window and check that our preview has been updated, then wait for it to appear
+ m_mainWindow->show();
+ QVERIFY(viewDirectoryLoadingCompletedSpy.wait());
+ QVERIFY(previewUpdatedSpy.wait());
+ QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
+ QVERIFY(m_mainWindow->isVisible());
+ QTest::qWait(500); // we need to wait for the file widgets to become visible
+
+ // Set image selected and rename it to b.jpg, make sure editing role is working
+ view->markUrlsAsSelected({QUrl(testDir->url().toString() + "/a.jpg")});
+ view->updateViewState();
+ view->renameSelectedItems();
+ QVERIFY(view->m_view->m_editingRole);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_B);
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Enter);
+ QVERIFY(itemsChangedSpy.wait()); // Make sure that rename worked
+
+ // Check that preview gets updated and filename is correct
+ QVERIFY(previewUpdatedSpy.wait());
+ QVERIFY(!view->m_view->m_editingRole);
+ QCOMPARE(view->m_model->fileItem(0).name(), "b.jpg");
+ QCOMPARE(view->m_model->count(), 1);
+}
+