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 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
216 m_mainWindow
->openDirectories({homePathUrl
}, false);
217 m_mainWindow
->show();
218 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
219 QVERIFY(m_mainWindow
->isVisible());
221 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
224 const QUrl tempPathUrl
{QUrl::fromLocalFile(QDir::tempPath())};
225 tabWidget
->openNewTab(tempPathUrl
);
226 QCOMPARE(tabWidget
->count(), 2);
227 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
229 QVERIFY2(!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
230 QCOMPARE(KIO::iconNameForUrl(homePathUrl
), tabWidget
->tabIcon(0).name());
231 QCOMPARE(KIO::iconNameForUrl(tempPathUrl
), tabWidget
->tabIcon(1).name());
234 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
236 QTest::addColumn
<QUrl
>("activeViewUrl");
237 QTest::addColumn
<bool>("expectedEnabled");
239 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
240 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
241 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
244 void DolphinMainWindowTest::testNewFileMenuEnabled()
246 QFETCH(QUrl
, activeViewUrl
);
247 m_mainWindow
->openDirectories({activeViewUrl
}, false);
248 m_mainWindow
->show();
249 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
250 QVERIFY(m_mainWindow
->isVisible());
252 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
253 QVERIFY(newFileMenu
);
255 QFETCH(bool, expectedEnabled
);
256 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
259 void DolphinMainWindowTest::testWindowTitle_data()
261 QTest::addColumn
<QUrl
>("activeViewUrl");
262 QTest::addColumn
<QString
>("expectedWindowTitle");
264 // TODO: this test should enforce the english locale.
265 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
266 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
267 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
270 void DolphinMainWindowTest::testWindowTitle()
272 QFETCH(QUrl
, activeViewUrl
);
273 m_mainWindow
->openDirectories({activeViewUrl
}, false);
274 m_mainWindow
->show();
275 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
276 QVERIFY(m_mainWindow
->isVisible());
278 QFETCH(QString
, expectedWindowTitle
);
279 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
283 * The places panel will resize itself if any of the other widgets requires too much horizontal space
284 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
286 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
288 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
289 m_mainWindow
->show();
290 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
291 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
292 QVERIFY(m_mainWindow
->isVisible());
294 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
295 QVERIFY2(QTest::qWaitFor(
297 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
300 "The test couldn't be initialised properly. The places panel should be visible.");
302 const int initialPlacesPanelWidth
= placesPanel
->width();
304 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
305 QTest::qWait(300); // wait for animation
306 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
308 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
309 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
311 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
312 // 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.
313 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
314 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
315 selectionModeStates
++) {
316 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
317 m_mainWindow
->slotSetSelectionMode(true, contents
);
318 QTest::qWait(20); // give time for a paint/resize
319 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
322 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
323 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
326 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
327 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
331 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
332 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
335 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
336 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
339 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
340 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
344 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
345 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
348 m_mainWindow
->showMaximized();
349 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
351 QTest::qWait(300); // wait for split view closing animation
352 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
355 void DolphinMainWindowTest::testGoActions()
357 QScopedPointer
<TestDir
> testDir
{new TestDir()};
358 testDir
->createDir("a");
359 testDir
->createDir("b");
360 testDir
->createDir("b/b-1");
361 testDir
->createFile("b/b-2");
362 testDir
->createDir("c");
363 QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
364 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
365 m_mainWindow
->show();
366 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
367 QVERIFY(m_mainWindow
->isVisible());
368 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
370 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
372 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
373 * This is especially important when a user wants to peek into multiple folders in quick succession.
375 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
376 QVERIFY(spyDirectoryLoadingCompleted
.wait());
377 QVERIFY(QTest::qWaitFor([&]() {
378 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
379 })); // "Stop" command should be disabled because it finished loading
380 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
381 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
382 QVERIFY(parentDirUrl
!= childDirUrl
);
384 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
385 // 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.
386 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
387 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
388 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
389 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
390 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
391 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
392 QVERIFY(spyDirectoryLoadingCompleted
.wait());
393 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
394 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
396 // Go back to the parent folder
397 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
398 QVERIFY(spyDirectoryLoadingCompleted
.wait());
399 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
400 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
401 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
403 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
404 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
405 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
406 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
407 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
408 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
410 // Go forward to the child folder.
411 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
412 QVERIFY(spyDirectoryLoadingCompleted
.wait());
413 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
414 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
416 // Go back to the parent folder.
417 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
418 QVERIFY(spyDirectoryLoadingCompleted
.wait());
419 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
420 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
421 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
423 // 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
424 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
425 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
426 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
427 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
428 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
429 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
430 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
433 void DolphinMainWindowTest::testOpenFiles()
435 QScopedPointer
<TestDir
> testDir
{new TestDir()};
436 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
437 testDir
->createDir("a");
438 testDir
->createDir("a/b");
439 testDir
->createDir("a/b/c");
440 testDir
->createDir("a/b/c/d");
441 m_mainWindow
->openDirectories({testDirUrl
}, false);
442 m_mainWindow
->show();
444 // We only see the unselected "a" folder in the test dir. There are no other tabs.
445 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
446 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
447 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
448 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
449 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
450 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
451 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
453 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
454 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
455 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
456 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
457 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
459 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
460 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
461 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
462 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
463 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
464 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
465 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
466 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
468 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
469 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
470 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
471 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
472 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
473 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
474 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
475 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
477 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
478 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
479 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
480 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
482 // 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.
483 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
484 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
486 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
487 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
488 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
489 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
490 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
491 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
493 // Switch to first tab by opening it even though it is already open.
494 m_mainWindow
->openDirectories({testDirUrl
}, false);
495 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
496 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
498 // "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.
499 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
500 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
501 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
502 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
503 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
504 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
506 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
507 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
508 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
509 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
510 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
511 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
512 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
513 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
514 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
515 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
517 // "c" is in view in the second tab because "b" is expanded there,
518 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
519 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
520 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
521 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
522 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
524 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
525 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
526 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
527 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
529 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
530 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
531 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
532 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
533 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
534 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
537 void DolphinMainWindowTest::testAccessibilityAncestorTree()
539 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
540 m_mainWindow
->show();
541 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
542 QVERIFY(m_mainWindow
->isVisible());
544 QAccessibleInterface
*accessibleInterfaceOfMainWindow
= QAccessible::queryAccessibleInterface(m_mainWindow
.get());
545 Q_CHECK_PTR(accessibleInterfaceOfMainWindow
);
547 // We will test the accessibility of objects traversing forwards and backwards.
548 int testedObjectsSizeAfterTraversingForwards
= 0;
549 for (int i
= 0; i
< 2; i
++) {
550 std::tuple
<Qt::Key
, Qt::KeyboardModifier
> focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::NoModifier
};
552 focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::ShiftModifier
};
555 // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
556 std::set
<const QObject
*> testedObjects
; // Makes sure we stop testing when we arrive at an item that was already tested.
557 while (qApp
->focusObject() && !testedObjects
.count(qApp
->focusObject())) {
558 const auto currentlyFocusedObject
= qApp
->focusObject();
560 QAccessibleInterface
*accessibleInterface
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
561 // The accessibleInterfaces of focused objects might themselves have children.
562 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
563 while (accessibleInterface
->childCount() > 0) {
564 accessibleInterface
= accessibleInterface
->child(0);
566 while (accessibleInterface
!= accessibleInterfaceOfMainWindow
) {
567 QVERIFY2(accessibleInterface
,
568 qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
569 .arg(currentlyFocusedObject
->metaObject()->className())));
570 accessibleInterface
= accessibleInterface
->parent();
573 testedObjects
.insert(currentlyFocusedObject
); // Add it to testedObjects so we won't test it again later.
574 QTest::keyClick(m_mainWindow
.get(), std::get
<0>(focusChainTraversalKeyCombination
), std::get
<1>(focusChainTraversalKeyCombination
));
575 QVERIFY2(currentlyFocusedObject
!= qApp
->focusObject(),
576 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
580 testedObjectsSizeAfterTraversingForwards
= testedObjects
.size();
582 QCOMPARE(testedObjects
.size(), testedObjectsSizeAfterTraversingForwards
); // The size after traversing backwards is different than
583 // after going forwards which is probably not intended.
588 void DolphinMainWindowTest::testAutoSaveSession()
590 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
591 m_mainWindow
->show();
592 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
593 QVERIFY(m_mainWindow
->isVisible());
595 // Create config file
596 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
597 KConfig
*config
= KConfigGui::sessionConfig();
598 m_mainWindow
->saveGlobalProperties(config
);
599 m_mainWindow
->savePropertiesInternal(config
, 1);
602 // Setup watcher for config file changes
603 const QString configFileName
= QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/" + KConfigGui::sessionConfig()->name();
604 QFileSystemWatcher
*configWatcher
= new QFileSystemWatcher({configFileName
}, this);
605 QSignalSpy
spySessionSaved(configWatcher
, &QFileSystemWatcher::fileChanged
);
607 // Enable session autosave.
608 m_mainWindow
->setSessionAutoSaveEnabled(true);
609 m_mainWindow
->m_sessionSaveTimer
->setInterval(200); // Lower the interval to speed up the testing
612 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
614 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
615 QCOMPARE(tabWidget
->count(), 2);
617 // Wait till a session save occurs
618 QVERIFY(spySessionSaved
.wait(60000));
620 // Disable session autosave.
621 m_mainWindow
->setSessionAutoSaveEnabled(false);
624 void DolphinMainWindowTest::cleanupTestCase()
626 m_mainWindow
->showNormal();
627 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
630 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
634 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
637 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
638 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
641 QTEST_MAIN(DolphinMainWindowTest
)
643 #include "dolphinmainwindowtest.moc"