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/kfileitemmodel.h"
13 #include "kitemviews/kitemlistcontainer.h"
14 #include "kitemviews/kitemlistcontroller.h"
15 #include "kitemviews/kitemlistselectionmanager.h"
18 #include <KActionCollection>
22 #include <QAccessible>
23 #include <QFileSystemWatcher>
24 #include <QScopedPointer>
26 #include <QStandardPaths>
31 class DolphinMainWindowTest
: public QObject
38 void testClosingTabsWithSearchBoxVisible();
39 void testActiveViewAfterClosingSplitView_data();
40 void testActiveViewAfterClosingSplitView();
41 void testUpdateWindowTitleAfterClosingSplitView();
42 void testUpdateWindowTitleAfterChangingSplitView();
43 void testOpenInNewTabTitle();
44 void testNewFileMenuEnabled_data();
45 void testNewFileMenuEnabled();
46 void testWindowTitle_data();
47 void testWindowTitle();
48 void testPlacesPanelWidthResistance();
51 void testAccessibilityAncestorTree();
52 void testAutoSaveSession();
53 void cleanupTestCase();
56 QScopedPointer
<DolphinMainWindow
> m_mainWindow
;
59 void DolphinMainWindowTest::initTestCase()
61 QStandardPaths::setTestModeEnabled(true);
64 void DolphinMainWindowTest::init()
66 m_mainWindow
.reset(new DolphinMainWindow());
69 // See https://bugs.kde.org/show_bug.cgi?id=379135
70 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
72 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
74 // Without this call the searchbox doesn't get FocusIn events.
75 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
76 QVERIFY(m_mainWindow
->isVisible());
78 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
81 // Show search box on first tab.
82 tabWidget
->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
84 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
85 QCOMPARE(tabWidget
->count(), 2);
87 // Triggers the crash in bug #379135.
88 tabWidget
->closeTab();
89 QCOMPARE(tabWidget
->count(), 1);
92 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
94 QTest::addColumn
<bool>("closeLeftView");
96 QTest::newRow("close left view") << true;
97 QTest::newRow("close right view") << false;
100 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
102 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
103 m_mainWindow
->show();
104 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
105 QVERIFY(m_mainWindow
->isVisible());
107 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
109 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
110 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
113 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
114 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
115 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
117 // Make sure the right view is the active one.
118 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
119 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
120 QVERIFY(!leftViewContainer
->isActive());
121 QVERIFY(rightViewContainer
->isActive());
123 QFETCH(bool, closeLeftView
);
125 // Activate left view.
126 leftViewContainer
->setActive(true);
127 QVERIFY(leftViewContainer
->isActive());
128 QVERIFY(!rightViewContainer
->isActive());
130 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
131 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
132 QVERIFY(!leftViewContainer
->isActive());
133 QVERIFY(rightViewContainer
->isActive());
134 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
136 // Close right view. The left view will become active.
137 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
138 QVERIFY(leftViewContainer
->isActive());
139 QVERIFY(!rightViewContainer
->isActive());
140 QCOMPARE(leftViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
144 // Test case for bug #385111
145 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
147 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
148 m_mainWindow
->show();
149 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
150 QVERIFY(m_mainWindow
->isVisible());
152 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
154 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
155 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
158 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
159 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
160 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
162 // Make sure the right view is the active one.
163 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
164 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
165 QVERIFY(!leftViewContainer
->isActive());
166 QVERIFY(rightViewContainer
->isActive());
168 // Activate left view.
169 leftViewContainer
->setActive(true);
170 QVERIFY(leftViewContainer
->isActive());
171 QVERIFY(!rightViewContainer
->isActive());
173 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
174 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
175 QVERIFY(!leftViewContainer
->isActive());
176 QVERIFY(rightViewContainer
->isActive());
177 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
179 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
180 QSignalSpy
currentUrlChangedSpy(tabWidget
, &DolphinTabWidget::currentUrlChanged
);
181 tabWidget
->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
182 QCOMPARE(currentUrlChangedSpy
.count(), 1);
185 // Test case for bug #402641
186 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
188 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
189 m_mainWindow
->show();
190 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
191 QVERIFY(m_mainWindow
->isVisible());
193 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
197 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
198 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
200 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
201 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
203 // Store old window title.
204 const auto oldTitle
= m_mainWindow
->windowTitle();
206 // Change URL in the right view and make sure the title gets updated.
207 rightViewContainer
->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
208 QVERIFY(m_mainWindow
->windowTitle() != oldTitle
);
210 // Activate back the left view and check whether the old title gets restored.
211 leftViewContainer
->setActive(true);
212 QCOMPARE(m_mainWindow
->windowTitle(), oldTitle
);
215 // Test case for bug #397910
216 void DolphinMainWindowTest::testOpenInNewTabTitle()
218 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
219 m_mainWindow
->openDirectories({homePathUrl
}, false);
220 m_mainWindow
->show();
221 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
222 QVERIFY(m_mainWindow
->isVisible());
224 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
227 const QUrl tempPathUrl
{QUrl::fromLocalFile(QDir::tempPath())};
228 tabWidget
->openNewTab(tempPathUrl
);
229 QCOMPARE(tabWidget
->count(), 2);
230 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
232 QVERIFY2(!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
233 QCOMPARE(KIO::iconNameForUrl(homePathUrl
), tabWidget
->tabIcon(0).name());
234 QCOMPARE(KIO::iconNameForUrl(tempPathUrl
), tabWidget
->tabIcon(1).name());
237 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
239 QTest::addColumn
<QUrl
>("activeViewUrl");
240 QTest::addColumn
<bool>("expectedEnabled");
242 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
243 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
244 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
247 void DolphinMainWindowTest::testNewFileMenuEnabled()
249 QFETCH(QUrl
, activeViewUrl
);
250 m_mainWindow
->openDirectories({activeViewUrl
}, false);
251 m_mainWindow
->show();
252 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
253 QVERIFY(m_mainWindow
->isVisible());
255 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
256 QVERIFY(newFileMenu
);
258 QFETCH(bool, expectedEnabled
);
259 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
262 void DolphinMainWindowTest::testWindowTitle_data()
264 QTest::addColumn
<QUrl
>("activeViewUrl");
265 QTest::addColumn
<QString
>("expectedWindowTitle");
267 // TODO: this test should enforce the english locale.
268 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
269 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
270 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
273 void DolphinMainWindowTest::testWindowTitle()
275 QFETCH(QUrl
, activeViewUrl
);
276 m_mainWindow
->openDirectories({activeViewUrl
}, false);
277 m_mainWindow
->show();
278 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
279 QVERIFY(m_mainWindow
->isVisible());
281 QFETCH(QString
, expectedWindowTitle
);
282 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
286 * The places panel will resize itself if any of the other widgets requires too much horizontal space
287 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
289 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
291 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
292 m_mainWindow
->show();
293 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
294 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
295 QVERIFY(m_mainWindow
->isVisible());
297 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
298 QVERIFY2(QTest::qWaitFor(
300 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
303 "The test couldn't be initialised properly. The places panel should be visible.");
305 const int initialPlacesPanelWidth
= placesPanel
->width();
307 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
308 QTest::qWait(300); // wait for animation
309 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
311 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
312 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
314 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
315 // 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.
316 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
317 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
318 selectionModeStates
++) {
319 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
320 m_mainWindow
->slotSetSelectionMode(true, contents
);
321 QTest::qWait(20); // give time for a paint/resize
322 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
325 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
326 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
329 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
330 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
334 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
335 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
338 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
339 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
342 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
343 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
347 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
348 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
351 m_mainWindow
->showMaximized();
352 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
354 QTest::qWait(300); // wait for split view closing animation
355 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
358 void DolphinMainWindowTest::testGoActions()
360 QScopedPointer
<TestDir
> testDir
{new TestDir()};
361 testDir
->createDir("a");
362 testDir
->createDir("b");
363 testDir
->createDir("b/b-1");
364 testDir
->createFile("b/b-2");
365 testDir
->createDir("c");
366 const QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
367 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
368 m_mainWindow
->show();
369 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
370 QVERIFY(m_mainWindow
->isVisible());
371 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
373 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
375 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
376 * This is especially important when a user wants to peek into multiple folders in quick succession.
378 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
379 QVERIFY(spyDirectoryLoadingCompleted
.wait());
380 QVERIFY(QTest::qWaitFor([&]() {
381 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
382 })); // "Stop" command should be disabled because it finished loading
383 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
384 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
385 QVERIFY(parentDirUrl
!= childDirUrl
);
387 auto currentItemUrl
= [this]() {
388 const int currentIndex
= m_mainWindow
->m_activeViewContainer
->view()->m_container
->controller()->selectionManager()->currentItem();
389 const KFileItem currentItem
= m_mainWindow
->m_activeViewContainer
->view()->m_model
->fileItem(currentIndex
);
390 return currentItem
.url();
393 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we just emerged from should now have keyboard focus.
394 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // The item we just emerged from should not be selected. BUG: 424723
395 // Pressing arrow keys should not only move the keyboard focus but also select the item.
396 // We press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is selected for the first time.
397 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
398 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
399 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
400 QVERIFY2(currentItemUrl() != childDirUrl
, "The current item didn't change after pressing the 'Down' key.");
401 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
402 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
403 QCOMPARE(currentItemUrl(), childDirUrl
); // After pressing 'Down' and then 'Up' we should be back where we were.
405 // Enter the child folder "b".
406 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
407 QVERIFY(spyDirectoryLoadingCompleted
.wait());
408 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
409 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
411 // Go back to the parent folder.
412 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
413 QVERIFY(spyDirectoryLoadingCompleted
.wait());
414 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
415 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
416 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
417 // Going 'Back' means that the view should be in the same state it was in when we left.
418 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we last interacted with in this location should still have keyboard focus.
419 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
420 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), childDirUrl
); // It should still be selected.
422 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
423 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
424 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
425 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
426 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
427 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
429 // Go forward to the child folder.
430 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
431 QVERIFY(spyDirectoryLoadingCompleted
.wait());
432 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
433 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
434 QCOMPARE(currentItemUrl(), QUrl(QDir::cleanPath(testDir
->url().toString() + "/b/b-1"))); // The first item in the view should have keyboard focus.
436 // Press the 'Down' key in the child folder.
437 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
438 // The second item in the view should have keyboard focus and be selected.
439 const QUrl secondItemInChildFolderUrl
{QDir::cleanPath(testDir
->url().toString() + "/b/b-2")};
440 QCOMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
441 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
442 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
444 // Go back to the parent folder and then re-enter the child folder.
445 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
446 QVERIFY(spyDirectoryLoadingCompleted
.wait());
447 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
448 QVERIFY(spyDirectoryLoadingCompleted
.wait());
449 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
450 // The state of the view should be identical to how it was before we triggered "Back" and then "Forward".
451 QTRY_COMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
452 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
453 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
455 // Go back to the parent folder.
456 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
457 QVERIFY(spyDirectoryLoadingCompleted
.wait());
458 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
459 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
460 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
462 // 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
463 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
464 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
465 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
466 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
467 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
468 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
469 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
472 void DolphinMainWindowTest::testOpenFiles()
474 QScopedPointer
<TestDir
> testDir
{new TestDir()};
475 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
476 testDir
->createDir("a");
477 testDir
->createDir("a/b");
478 testDir
->createDir("a/b/c");
479 testDir
->createDir("a/b/c/d");
480 m_mainWindow
->openDirectories({testDirUrl
}, false);
481 m_mainWindow
->show();
483 // We only see the unselected "a" folder in the test dir. There are no other tabs.
484 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
485 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
486 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
487 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
488 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
489 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
490 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
492 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
493 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
494 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
495 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
496 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
498 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
499 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
500 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
501 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
502 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
503 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
504 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
505 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
507 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
508 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
509 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
510 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
511 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
512 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
513 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
514 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
516 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
517 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
518 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
519 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
521 // 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.
522 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
523 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
525 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
526 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
527 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
528 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
529 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
530 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
532 // Switch to first tab by opening it even though it is already open.
533 m_mainWindow
->openDirectories({testDirUrl
}, false);
534 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
535 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
537 // "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.
538 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
539 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
540 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
541 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
542 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
543 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
545 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
546 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
547 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
548 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
549 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
550 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
551 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
552 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
553 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
554 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
556 // "c" is in view in the second tab because "b" is expanded there,
557 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
558 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
559 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
560 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
561 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
563 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
564 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
565 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
566 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
568 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
569 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
570 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
571 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
572 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
573 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
576 void DolphinMainWindowTest::testAccessibilityAncestorTree()
578 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
579 m_mainWindow
->show();
580 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
581 QVERIFY(m_mainWindow
->isVisible());
583 QAccessibleInterface
*accessibleInterfaceOfMainWindow
= QAccessible::queryAccessibleInterface(m_mainWindow
.get());
584 Q_CHECK_PTR(accessibleInterfaceOfMainWindow
);
586 // We will test the accessibility of objects traversing forwards and backwards.
587 int testedObjectsSizeAfterTraversingForwards
= 0;
588 for (int i
= 0; i
< 2; i
++) {
589 std::tuple
<Qt::Key
, Qt::KeyboardModifier
> focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::NoModifier
};
591 focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::ShiftModifier
};
594 // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
595 std::set
<const QObject
*> testedObjects
; // Makes sure we stop testing when we arrive at an item that was already tested.
596 while (qApp
->focusObject() && !testedObjects
.count(qApp
->focusObject())) {
597 const auto currentlyFocusedObject
= qApp
->focusObject();
599 QAccessibleInterface
*accessibleInterface
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
600 // The accessibleInterfaces of focused objects might themselves have children.
601 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
602 while (accessibleInterface
->childCount() > 0) {
603 accessibleInterface
= accessibleInterface
->child(0);
605 while (accessibleInterface
!= accessibleInterfaceOfMainWindow
) {
606 QVERIFY2(accessibleInterface
,
607 qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
608 .arg(currentlyFocusedObject
->metaObject()->className())));
609 accessibleInterface
= accessibleInterface
->parent();
612 testedObjects
.insert(currentlyFocusedObject
); // Add it to testedObjects so we won't test it again later.
613 QTest::keyClick(m_mainWindow
.get(), std::get
<0>(focusChainTraversalKeyCombination
), std::get
<1>(focusChainTraversalKeyCombination
));
614 QVERIFY2(currentlyFocusedObject
!= qApp
->focusObject(),
615 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
619 testedObjectsSizeAfterTraversingForwards
= testedObjects
.size();
621 QCOMPARE(testedObjects
.size(), testedObjectsSizeAfterTraversingForwards
); // The size after traversing backwards is different than
622 // after going forwards which is probably not intended.
627 void DolphinMainWindowTest::testAutoSaveSession()
629 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
630 m_mainWindow
->show();
631 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
632 QVERIFY(m_mainWindow
->isVisible());
634 // Create config file
635 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
636 KConfig
*config
= KConfigGui::sessionConfig();
637 m_mainWindow
->saveGlobalProperties(config
);
638 m_mainWindow
->savePropertiesInternal(config
, 1);
641 // Setup watcher for config file changes
642 const QString configFileName
= QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/" + KConfigGui::sessionConfig()->name();
643 QFileSystemWatcher
*configWatcher
= new QFileSystemWatcher({configFileName
}, this);
644 QSignalSpy
spySessionSaved(configWatcher
, &QFileSystemWatcher::fileChanged
);
646 // Enable session autosave.
647 m_mainWindow
->setSessionAutoSaveEnabled(true);
648 m_mainWindow
->m_sessionSaveTimer
->setInterval(200); // Lower the interval to speed up the testing
651 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
653 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
654 QCOMPARE(tabWidget
->count(), 2);
656 // Wait till a session save occurs
657 QVERIFY(spySessionSaved
.wait(60000));
659 // Disable session autosave.
660 m_mainWindow
->setSessionAutoSaveEnabled(false);
663 void DolphinMainWindowTest::cleanupTestCase()
665 m_mainWindow
->showNormal();
666 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
669 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
673 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
676 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
677 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
680 QTEST_MAIN(DolphinMainWindowTest
)
682 #include "dolphinmainwindowtest.moc"