2 * SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinmainwindow.h"
8 #include "dolphinnewfilemenu.h"
9 #include "dolphintabpage.h"
10 #include "dolphintabwidget.h"
11 #include "dolphinviewcontainer.h"
12 #include "kitemviews/kitemlistcontainer.h"
15 #include <KActionCollection>
19 #include <QAccessible>
20 #include <QFileSystemWatcher>
21 #include <QScopedPointer>
23 #include <QStandardPaths>
28 class DolphinMainWindowTest
: public QObject
35 void testClosingTabsWithSearchBoxVisible();
36 void testActiveViewAfterClosingSplitView_data();
37 void testActiveViewAfterClosingSplitView();
38 void testUpdateWindowTitleAfterClosingSplitView();
39 void testUpdateWindowTitleAfterChangingSplitView();
40 void testOpenInNewTabTitle();
41 void testNewFileMenuEnabled_data();
42 void testNewFileMenuEnabled();
43 void testWindowTitle_data();
44 void testWindowTitle();
45 void testPlacesPanelWidthResistance();
48 void testAccessibilityAncestorTree();
49 void testAutoSaveSession();
50 void cleanupTestCase();
53 QScopedPointer
<DolphinMainWindow
> m_mainWindow
;
56 void DolphinMainWindowTest::initTestCase()
58 QStandardPaths::setTestModeEnabled(true);
61 void DolphinMainWindowTest::init()
63 m_mainWindow
.reset(new DolphinMainWindow());
66 // See https://bugs.kde.org/show_bug.cgi?id=379135
67 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
69 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
71 // Without this call the searchbox doesn't get FocusIn events.
72 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
73 QVERIFY(m_mainWindow
->isVisible());
75 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
78 // Show search box on first tab.
79 tabWidget
->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
81 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
82 QCOMPARE(tabWidget
->count(), 2);
84 // Triggers the crash in bug #379135.
85 tabWidget
->closeTab();
86 QCOMPARE(tabWidget
->count(), 1);
89 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
91 QTest::addColumn
<bool>("closeLeftView");
93 QTest::newRow("close left view") << true;
94 QTest::newRow("close right view") << false;
97 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
99 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
100 m_mainWindow
->show();
101 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
102 QVERIFY(m_mainWindow
->isVisible());
104 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
106 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
107 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
110 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
111 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
112 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
114 // Make sure the right view is the active one.
115 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
116 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
117 QVERIFY(!leftViewContainer
->isActive());
118 QVERIFY(rightViewContainer
->isActive());
120 QFETCH(bool, closeLeftView
);
122 // Activate left view.
123 leftViewContainer
->setActive(true);
124 QVERIFY(leftViewContainer
->isActive());
125 QVERIFY(!rightViewContainer
->isActive());
127 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
128 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
129 QVERIFY(!leftViewContainer
->isActive());
130 QVERIFY(rightViewContainer
->isActive());
131 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
133 // Close right view. The left view will become active.
134 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
135 QVERIFY(leftViewContainer
->isActive());
136 QVERIFY(!rightViewContainer
->isActive());
137 QCOMPARE(leftViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
141 // Test case for bug #385111
142 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
144 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
145 m_mainWindow
->show();
146 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
147 QVERIFY(m_mainWindow
->isVisible());
149 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
151 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
152 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
155 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
156 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
157 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
159 // Make sure the right view is the active one.
160 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
161 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
162 QVERIFY(!leftViewContainer
->isActive());
163 QVERIFY(rightViewContainer
->isActive());
165 // Activate left view.
166 leftViewContainer
->setActive(true);
167 QVERIFY(leftViewContainer
->isActive());
168 QVERIFY(!rightViewContainer
->isActive());
170 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
171 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
172 QVERIFY(!leftViewContainer
->isActive());
173 QVERIFY(rightViewContainer
->isActive());
174 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
176 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
177 QSignalSpy
currentUrlChangedSpy(tabWidget
, &DolphinTabWidget::currentUrlChanged
);
178 tabWidget
->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
179 QCOMPARE(currentUrlChangedSpy
.count(), 1);
182 // Test case for bug #402641
183 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
185 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
186 m_mainWindow
->show();
187 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
188 QVERIFY(m_mainWindow
->isVisible());
190 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
194 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
195 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
197 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
198 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
200 // Store old window title.
201 const auto oldTitle
= m_mainWindow
->windowTitle();
203 // Change URL in the right view and make sure the title gets updated.
204 rightViewContainer
->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
205 QVERIFY(m_mainWindow
->windowTitle() != oldTitle
);
207 // Activate back the left view and check whether the old title gets restored.
208 leftViewContainer
->setActive(true);
209 QCOMPARE(m_mainWindow
->windowTitle(), oldTitle
);
212 // Test case for bug #397910
213 void DolphinMainWindowTest::testOpenInNewTabTitle()
215 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
216 m_mainWindow
->show();
217 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
218 QVERIFY(m_mainWindow
->isVisible());
220 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
223 tabWidget
->openNewTab(QUrl::fromLocalFile(QDir::tempPath()));
224 QCOMPARE(tabWidget
->count(), 2);
225 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
226 if (!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull()) {
227 QCOMPARE(QStringLiteral("inode-directory"), tabWidget
->tabIcon(0).name());
228 QCOMPARE(QStringLiteral("inode-directory"), tabWidget
->tabIcon(1).name());
232 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
234 QTest::addColumn
<QUrl
>("activeViewUrl");
235 QTest::addColumn
<bool>("expectedEnabled");
237 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
238 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
239 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
242 void DolphinMainWindowTest::testNewFileMenuEnabled()
244 QFETCH(QUrl
, activeViewUrl
);
245 m_mainWindow
->openDirectories({activeViewUrl
}, false);
246 m_mainWindow
->show();
247 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
248 QVERIFY(m_mainWindow
->isVisible());
250 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
251 QVERIFY(newFileMenu
);
253 QFETCH(bool, expectedEnabled
);
254 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
257 void DolphinMainWindowTest::testWindowTitle_data()
259 QTest::addColumn
<QUrl
>("activeViewUrl");
260 QTest::addColumn
<QString
>("expectedWindowTitle");
262 // TODO: this test should enforce the english locale.
263 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
264 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
265 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
268 void DolphinMainWindowTest::testWindowTitle()
270 QFETCH(QUrl
, activeViewUrl
);
271 m_mainWindow
->openDirectories({activeViewUrl
}, false);
272 m_mainWindow
->show();
273 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
274 QVERIFY(m_mainWindow
->isVisible());
276 QFETCH(QString
, expectedWindowTitle
);
277 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
281 * The places panel will resize itself if any of the other widgets requires too much horizontal space
282 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
284 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
286 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
287 m_mainWindow
->show();
288 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
289 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
290 QVERIFY(m_mainWindow
->isVisible());
292 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
293 QVERIFY2(QTest::qWaitFor(
295 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
298 "The test couldn't be initialised properly. The places panel should be visible.");
300 const int initialPlacesPanelWidth
= placesPanel
->width();
302 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
303 QTest::qWait(300); // wait for animation
304 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
306 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
307 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
309 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
310 // One of the bottom bars (SelectionMode::BottomBar::GeneralContents) only shows up when at least one item is selected so we do that before we begin iterating.
311 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
312 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
313 selectionModeStates
++) {
314 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
315 m_mainWindow
->slotSetSelectionMode(true, contents
);
316 QTest::qWait(20); // give time for a paint/resize
317 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
320 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
321 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
324 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
325 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
329 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
330 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
333 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
334 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
337 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
338 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
342 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
343 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
346 m_mainWindow
->showMaximized();
347 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
349 QTest::qWait(300); // wait for split view closing animation
350 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
353 void DolphinMainWindowTest::testGoActions()
355 QScopedPointer
<TestDir
> testDir
{new TestDir()};
356 testDir
->createDir("a");
357 testDir
->createDir("b");
358 testDir
->createDir("b/b-1");
359 testDir
->createFile("b/b-2");
360 testDir
->createDir("c");
361 QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
362 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
363 m_mainWindow
->show();
364 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
365 QVERIFY(m_mainWindow
->isVisible());
366 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
368 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
370 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
371 * This is especially important when a user wants to peek into multiple folders in quick succession.
373 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
374 QVERIFY(spyDirectoryLoadingCompleted
.wait());
375 QVERIFY(QTest::qWaitFor([&]() {
376 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
377 })); // "Stop" command should be disabled because it finished loading
378 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
379 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
380 QVERIFY(parentDirUrl
!= childDirUrl
);
382 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
383 // 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.
384 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
385 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
386 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
387 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
388 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
389 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
390 QVERIFY(spyDirectoryLoadingCompleted
.wait());
391 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
392 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
394 // Go back to the parent folder
395 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
396 QVERIFY(spyDirectoryLoadingCompleted
.wait());
397 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
398 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
399 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
401 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
402 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
403 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
404 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
405 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
406 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
408 // Go forward to the child folder.
409 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
410 QVERIFY(spyDirectoryLoadingCompleted
.wait());
411 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
412 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
414 // Go back to the parent folder.
415 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
416 QVERIFY(spyDirectoryLoadingCompleted
.wait());
417 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
418 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
419 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
421 // Close current tab and see if the "go" actions are correctly disabled in the remaining tab that was never active until now and shows the "b" dir
422 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
423 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
424 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
425 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
426 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
427 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
428 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
431 void DolphinMainWindowTest::testOpenFiles()
433 QScopedPointer
<TestDir
> testDir
{new TestDir()};
434 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
435 testDir
->createDir("a");
436 testDir
->createDir("a/b");
437 testDir
->createDir("a/b/c");
438 testDir
->createDir("a/b/c/d");
439 m_mainWindow
->openDirectories({testDirUrl
}, false);
440 m_mainWindow
->show();
442 // We only see the unselected "a" folder in the test dir. There are no other tabs.
443 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
444 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
445 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
446 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
447 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
448 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
449 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
451 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
452 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
453 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
454 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
455 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
457 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
458 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
459 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
460 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
461 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
462 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
463 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
464 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
466 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
467 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
468 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
469 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
470 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
471 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
472 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
473 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
475 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
476 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
477 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
478 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
480 // 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.
481 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
482 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
484 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
485 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
486 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
487 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
488 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
489 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
491 // Switch to first tab by opening it even though it is already open.
492 m_mainWindow
->openDirectories({testDirUrl
}, false);
493 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
494 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
496 // "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.
497 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
498 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
499 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
500 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
501 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
502 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
504 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
505 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
506 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
507 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
508 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
509 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
510 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
511 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
512 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
513 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
515 // "c" is in view in the second tab because "b" is expanded there,
516 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
517 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
518 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
519 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
520 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
522 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
523 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
524 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
525 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
527 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
528 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
529 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
530 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
531 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
532 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
535 void DolphinMainWindowTest::testAccessibilityAncestorTree()
537 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
538 m_mainWindow
->show();
539 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
540 QVERIFY(m_mainWindow
->isVisible());
542 QAccessibleInterface
*accessibleInterfaceOfMainWindow
= QAccessible::queryAccessibleInterface(m_mainWindow
.get());
543 Q_CHECK_PTR(accessibleInterfaceOfMainWindow
);
545 // We will test the accessibility of objects traversing forwards and backwards.
546 int testedObjectsSizeAfterTraversingForwards
= 0;
547 for (int i
= 0; i
< 2; i
++) {
548 std::tuple
<Qt::Key
, Qt::KeyboardModifier
> focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::NoModifier
};
550 focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::ShiftModifier
};
553 // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
554 std::set
<const QObject
*> testedObjects
; // Makes sure we stop testing when we arrive at an item that was already tested.
555 while (qApp
->focusObject() && !testedObjects
.count(qApp
->focusObject())) {
556 const auto currentlyFocusedObject
= qApp
->focusObject();
558 QAccessibleInterface
*accessibleInterface
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
559 // The accessibleInterfaces of focused objects might themselves have children.
560 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
561 while (accessibleInterface
->childCount() > 0) {
562 accessibleInterface
= accessibleInterface
->child(0);
564 while (accessibleInterface
!= accessibleInterfaceOfMainWindow
) {
565 QVERIFY2(accessibleInterface
,
566 qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
567 .arg(currentlyFocusedObject
->metaObject()->className())));
568 accessibleInterface
= accessibleInterface
->parent();
571 testedObjects
.insert(currentlyFocusedObject
); // Add it to testedObjects so we won't test it again later.
572 QTest::keyClick(m_mainWindow
.get(), std::get
<0>(focusChainTraversalKeyCombination
), std::get
<1>(focusChainTraversalKeyCombination
));
573 QVERIFY2(currentlyFocusedObject
!= qApp
->focusObject(),
574 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
578 testedObjectsSizeAfterTraversingForwards
= testedObjects
.size();
580 QCOMPARE(testedObjects
.size(), testedObjectsSizeAfterTraversingForwards
); // The size after traversing backwards is different than
581 // after going forwards which is probably not intended.
586 void DolphinMainWindowTest::testAutoSaveSession()
588 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
589 m_mainWindow
->show();
590 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
591 QVERIFY(m_mainWindow
->isVisible());
593 // Create config file
594 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
595 KConfig
*config
= KConfigGui::sessionConfig();
596 m_mainWindow
->saveGlobalProperties(config
);
597 m_mainWindow
->savePropertiesInternal(config
, 1);
600 // Setup watcher for config file changes
601 const QString configFileName
= QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/" + KConfigGui::sessionConfig()->name();
602 QFileSystemWatcher
*configWatcher
= new QFileSystemWatcher({configFileName
}, this);
603 QSignalSpy
spySessionSaved(configWatcher
, &QFileSystemWatcher::fileChanged
);
605 // Enable session autosave.
606 m_mainWindow
->setSessionAutoSaveEnabled(true);
607 m_mainWindow
->m_sessionSaveTimer
->setInterval(200); // Lower the interval to speed up the testing
610 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
612 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
613 QCOMPARE(tabWidget
->count(), 2);
615 // Wait till a session save occurs
616 QVERIFY(spySessionSaved
.wait(60000));
618 // Disable session autosave.
619 m_mainWindow
->setSessionAutoSaveEnabled(false);
622 void DolphinMainWindowTest::cleanupTestCase()
624 m_mainWindow
->showNormal();
625 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
628 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
632 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
635 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
636 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
639 QTEST_MAIN(DolphinMainWindowTest
)
641 #include "dolphinmainwindowtest.moc"