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 <QDomDocument>
24 #include <QFileSystemWatcher>
25 #include <QScopedPointer>
27 #include <QStandardPaths>
31 #include <unordered_set>
33 class DolphinMainWindowTest
: public QObject
40 void testSyncDesktopAndPhoneUi();
41 void testClosingTabsWithSearchBoxVisible();
42 void testActiveViewAfterClosingSplitView_data();
43 void testActiveViewAfterClosingSplitView();
44 void testUpdateWindowTitleAfterClosingSplitView();
45 void testUpdateWindowTitleAfterChangingSplitView();
46 void testOpenInNewTabTitle();
47 void testNewFileMenuEnabled_data();
48 void testNewFileMenuEnabled();
49 void testWindowTitle_data();
50 void testWindowTitle();
51 void testFocusLocationBar();
52 void testFocusPlacesPanel();
53 void testPlacesPanelWidthResistance();
56 void testAccessibilityAncestorTree();
57 void testAutoSaveSession();
58 void cleanupTestCase();
61 QScopedPointer
<DolphinMainWindow
> m_mainWindow
;
64 void DolphinMainWindowTest::initTestCase()
66 QStandardPaths::setTestModeEnabled(true);
69 void DolphinMainWindowTest::init()
71 m_mainWindow
.reset(new DolphinMainWindow());
75 * It is too easy to forget that most changes in dolphinui.rc should be mirrored in dolphinuiforphones.rc. This test makes sure that these two files stay
76 * mostly identical. Differences between those files need to be explicitly added as exceptions to this test. So if you land here after changing either
77 * dolphinui.rc or dolphinuiforphones.rc, then resolve this test failure either by making the exact same change to the other ui.rc file, or by adding the
78 * changed object to the `exceptions` variable below.
80 void DolphinMainWindowTest::testSyncDesktopAndPhoneUi()
82 std::unordered_set
<QString
> exceptions
{{QStringLiteral("version"), QStringLiteral("ToolBar")}};
84 QDomDocument desktopUi
;
85 QFile
desktopUiXmlFile(":/kxmlgui5/dolphin/dolphinui.rc");
86 desktopUiXmlFile
.open(QIODevice::ReadOnly
);
87 desktopUi
.setContent(&desktopUiXmlFile
);
88 desktopUiXmlFile
.close();
91 QFile
phoneUiXmlFile(":/kxmlgui5/dolphin/dolphinuiforphones.rc");
92 phoneUiXmlFile
.open(QIODevice::ReadOnly
);
93 phoneUi
.setContent(&phoneUiXmlFile
);
94 phoneUiXmlFile
.close();
96 QDomElement desktopUiElement
= desktopUi
.documentElement();
97 QDomElement phoneUiElement
= phoneUi
.documentElement();
99 auto nextUiElement
= [&exceptions
](QDomElement uiElement
) -> QDomElement
{
100 QDomNode nextUiNode
{uiElement
};
102 // If the current node is an exception, we skip its children as well.
103 if (exceptions
.count(nextUiNode
.nodeName()) == 0) {
104 auto firstChild
{nextUiNode
.firstChild()};
105 if (!firstChild
.isNull()) {
106 nextUiNode
= firstChild
;
110 auto nextSibling
{nextUiNode
.nextSibling()};
111 if (!nextSibling
.isNull()) {
112 nextUiNode
= nextSibling
;
115 auto parent
{nextUiNode
.parentNode()};
117 if (parent
.isNull()) {
118 return QDomElement();
120 auto nextParentSibling
{parent
.nextSibling()};
121 if (!nextParentSibling
.isNull()) {
122 nextUiNode
= nextParentSibling
;
125 parent
= parent
.parentNode();
129 && (nextUiNode
.toElement().isNull() || exceptions
.count(nextUiNode
.nodeName()))); // We loop until we either give up finding an element or find one.
130 if (nextUiNode
.isNull()) {
131 return QDomElement();
133 return nextUiNode
.toElement();
136 int totalComparisonsCount
{0};
138 QVERIFY2(desktopUiElement
.tagName() == phoneUiElement
.tagName(),
139 qPrintable(QStringLiteral("Node mismatch: dolphinui.rc/%1::%2 and dolphinuiforphones.rc/%3::%4")
140 .arg(desktopUiElement
.parentNode().toElement().tagName())
141 .arg(desktopUiElement
.tagName())
142 .arg(phoneUiElement
.parentNode().toElement().tagName())
143 .arg(phoneUiElement
.tagName())));
144 QCOMPARE(desktopUiElement
.text(), phoneUiElement
.text());
145 const auto desktopUiElementAttributes
= desktopUiElement
.attributes();
146 const auto phoneUiElementAttributes
= phoneUiElement
.attributes();
147 for (int i
= 0; i
< desktopUiElementAttributes
.count(); i
++) {
148 QVERIFY2(phoneUiElementAttributes
.count() >= i
,
149 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has more attributes than dolphinuiforphones.rc/%3::%4")
150 .arg(desktopUiElement
.parentNode().toElement().tagName())
151 .arg(desktopUiElement
.tagName())
152 .arg(phoneUiElement
.parentNode().toElement().tagName())
153 .arg(phoneUiElement
.tagName())));
154 if (exceptions
.count(desktopUiElementAttributes
.item(i
).nodeName())) {
157 QCOMPARE(desktopUiElementAttributes
.item(i
).nodeName(), phoneUiElementAttributes
.item(i
).nodeName());
158 QCOMPARE(desktopUiElementAttributes
.item(i
).nodeValue(), phoneUiElementAttributes
.item(i
).nodeValue());
159 totalComparisonsCount
++;
161 QVERIFY2(desktopUiElementAttributes
.count() == phoneUiElementAttributes
.count(),
162 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has fewer attributes than dolphinuiforphones.rc/%3::%4. %5 < %6")
163 .arg(desktopUiElement
.parentNode().toElement().tagName())
164 .arg(desktopUiElement
.tagName())
165 .arg(phoneUiElement
.parentNode().toElement().tagName())
166 .arg(phoneUiElement
.tagName())
167 .arg(phoneUiElementAttributes
.count())
168 .arg(desktopUiElementAttributes
.count())));
170 desktopUiElement
= nextUiElement(desktopUiElement
);
171 phoneUiElement
= nextUiElement(phoneUiElement
);
172 totalComparisonsCount
++;
173 } while (!desktopUiElement
.isNull() || !phoneUiElement
.isNull());
174 QVERIFY2(totalComparisonsCount
> 200, qPrintable(QStringLiteral("There were only %1 comparisons. Did the test run correctly?").arg(totalComparisonsCount
)));
177 // See https://bugs.kde.org/show_bug.cgi?id=379135
178 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
180 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
181 m_mainWindow
->show();
182 // Without this call the searchbox doesn't get FocusIn events.
183 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
184 QVERIFY(m_mainWindow
->isVisible());
186 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
189 // Show search box on first tab.
190 tabWidget
->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
192 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
193 QCOMPARE(tabWidget
->count(), 2);
195 // Triggers the crash in bug #379135.
196 tabWidget
->closeTab();
197 QCOMPARE(tabWidget
->count(), 1);
200 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
202 QTest::addColumn
<bool>("closeLeftView");
204 QTest::newRow("close left view") << true;
205 QTest::newRow("close right view") << false;
208 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
210 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
211 m_mainWindow
->show();
212 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
213 QVERIFY(m_mainWindow
->isVisible());
215 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
217 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
218 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
221 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
222 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
223 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
225 // Make sure the right view is the active one.
226 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
227 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
228 QVERIFY(!leftViewContainer
->isActive());
229 QVERIFY(rightViewContainer
->isActive());
231 QFETCH(bool, closeLeftView
);
233 // Activate left view.
234 leftViewContainer
->setActive(true);
235 QVERIFY(leftViewContainer
->isActive());
236 QVERIFY(!rightViewContainer
->isActive());
238 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
239 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
240 QVERIFY(!leftViewContainer
->isActive());
241 QVERIFY(rightViewContainer
->isActive());
242 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
244 // Close right view. The left view will become active.
245 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
246 QVERIFY(leftViewContainer
->isActive());
247 QVERIFY(!rightViewContainer
->isActive());
248 QCOMPARE(leftViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
252 // Test case for bug #385111
253 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
255 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
256 m_mainWindow
->show();
257 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
258 QVERIFY(m_mainWindow
->isVisible());
260 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
262 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
263 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
266 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
267 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
268 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
270 // Make sure the right view is the active one.
271 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
272 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
273 QVERIFY(!leftViewContainer
->isActive());
274 QVERIFY(rightViewContainer
->isActive());
276 // Activate left view.
277 leftViewContainer
->setActive(true);
278 QVERIFY(leftViewContainer
->isActive());
279 QVERIFY(!rightViewContainer
->isActive());
281 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
282 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
283 QVERIFY(!leftViewContainer
->isActive());
284 QVERIFY(rightViewContainer
->isActive());
285 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
287 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
288 QSignalSpy
currentUrlChangedSpy(tabWidget
, &DolphinTabWidget::currentUrlChanged
);
289 tabWidget
->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
290 QCOMPARE(currentUrlChangedSpy
.count(), 1);
293 // Test case for bug #402641
294 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
296 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
297 m_mainWindow
->show();
298 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
299 QVERIFY(m_mainWindow
->isVisible());
301 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
305 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
306 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
308 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
309 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
311 // Store old window title.
312 const auto oldTitle
= m_mainWindow
->windowTitle();
314 // Change URL in the right view and make sure the title gets updated.
315 rightViewContainer
->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
316 QVERIFY(m_mainWindow
->windowTitle() != oldTitle
);
318 // Activate back the left view and check whether the old title gets restored.
319 leftViewContainer
->setActive(true);
320 QCOMPARE(m_mainWindow
->windowTitle(), oldTitle
);
323 // Test case for bug #397910
324 void DolphinMainWindowTest::testOpenInNewTabTitle()
326 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
327 m_mainWindow
->openDirectories({homePathUrl
}, false);
328 m_mainWindow
->show();
329 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
330 QVERIFY(m_mainWindow
->isVisible());
332 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
335 const QUrl tempPathUrl
{QUrl::fromLocalFile(QDir::tempPath())};
336 tabWidget
->openNewTab(tempPathUrl
);
337 QCOMPARE(tabWidget
->count(), 2);
338 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
340 QVERIFY2(!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
341 QCOMPARE(KIO::iconNameForUrl(homePathUrl
), tabWidget
->tabIcon(0).name());
342 QCOMPARE(KIO::iconNameForUrl(tempPathUrl
), tabWidget
->tabIcon(1).name());
345 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
347 QTest::addColumn
<QUrl
>("activeViewUrl");
348 QTest::addColumn
<bool>("expectedEnabled");
350 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
351 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
352 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
355 void DolphinMainWindowTest::testNewFileMenuEnabled()
357 QFETCH(QUrl
, activeViewUrl
);
358 m_mainWindow
->openDirectories({activeViewUrl
}, false);
359 m_mainWindow
->show();
360 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
361 QVERIFY(m_mainWindow
->isVisible());
363 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
364 QVERIFY(newFileMenu
);
366 QFETCH(bool, expectedEnabled
);
367 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
370 void DolphinMainWindowTest::testWindowTitle_data()
372 QTest::addColumn
<QUrl
>("activeViewUrl");
373 QTest::addColumn
<QString
>("expectedWindowTitle");
375 // TODO: this test should enforce the english locale.
376 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
377 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
378 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
381 void DolphinMainWindowTest::testWindowTitle()
383 QFETCH(QUrl
, activeViewUrl
);
384 m_mainWindow
->openDirectories({activeViewUrl
}, false);
385 m_mainWindow
->show();
386 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
387 QVERIFY(m_mainWindow
->isVisible());
389 QFETCH(QString
, expectedWindowTitle
);
390 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
393 void DolphinMainWindowTest::testFocusLocationBar()
395 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
396 m_mainWindow
->openDirectories({homePathUrl
}, false);
397 m_mainWindow
->show();
398 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
399 QVERIFY(m_mainWindow
->isVisible());
401 QAction
*replaceLocationAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("replace_location"));
402 replaceLocationAction
->trigger();
403 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
404 replaceLocationAction
->trigger();
405 QVERIFY(m_mainWindow
->activeViewContainer()->view()->hasFocus());
407 QAction
*editableLocationAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("editable_location"));
408 editableLocationAction
->trigger();
409 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
410 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isUrlEditable());
411 editableLocationAction
->trigger();
412 QVERIFY(!m_mainWindow
->activeViewContainer()->urlNavigator()->isUrlEditable());
414 replaceLocationAction
->trigger();
415 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
417 // Pressing Escape multiple times should eventually move the focus back to the active view.
418 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape
); // Focus might not go the view yet because it toggles the editable state of the location bar.
419 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape
);
420 QVERIFY(m_mainWindow
->activeViewContainer()->view()->hasFocus());
423 void DolphinMainWindowTest::testFocusPlacesPanel()
425 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
426 m_mainWindow
->show();
427 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
428 QVERIFY(m_mainWindow
->isVisible());
430 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
431 QVERIFY2(QTest::qWaitFor(
433 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0 && placesPanel
->height() > 0;
436 "The test couldn't be initialised properly. The places panel should be visible.");
438 QAction
*focusPlacesPanelAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("focus_places_panel"));
439 QAction
*showPlacesPanelAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("show_places_panel"));
441 focusPlacesPanelAction
->trigger();
442 QVERIFY(placesPanel
->hasFocus());
444 focusPlacesPanelAction
->trigger();
445 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
446 "Triggering focus_places_panel while the panel already has focus should return the focus to the view.");
448 focusPlacesPanelAction
->trigger();
449 QVERIFY(placesPanel
->hasFocus());
451 showPlacesPanelAction
->trigger();
452 QVERIFY(!placesPanel
->isVisible());
453 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
454 "Hiding the Places panel while it has focus should return the focus to the view.");
456 showPlacesPanelAction
->trigger();
457 QVERIFY(placesPanel
->isVisible());
458 QVERIFY2(placesPanel
->hasFocus(), "Enabling the Places panel should move keyboard focus there.");
462 * The places panel will resize itself if any of the other widgets requires too much horizontal space
463 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
465 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
467 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
468 m_mainWindow
->show();
469 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
470 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
471 QVERIFY(m_mainWindow
->isVisible());
473 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
474 QVERIFY2(QTest::qWaitFor(
476 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
479 "The test couldn't be initialised properly. The places panel should be visible.");
481 const int initialPlacesPanelWidth
= placesPanel
->width();
483 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
484 QTest::qWait(300); // wait for animation
485 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
487 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
488 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
490 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
491 // 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.
492 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
493 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
494 selectionModeStates
++) {
495 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
496 m_mainWindow
->slotSetSelectionMode(true, contents
);
497 QTest::qWait(20); // give time for a paint/resize
498 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
501 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
502 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
505 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
506 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
510 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
511 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
514 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
515 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
518 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
519 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
523 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
524 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
527 m_mainWindow
->showMaximized();
528 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
530 QTest::qWait(300); // wait for split view closing animation
531 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
534 void DolphinMainWindowTest::testGoActions()
536 QScopedPointer
<TestDir
> testDir
{new TestDir()};
537 testDir
->createDir("a");
538 testDir
->createDir("b");
539 testDir
->createDir("b/b-1");
540 testDir
->createFile("b/b-2");
541 testDir
->createDir("c");
542 const QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
543 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
544 m_mainWindow
->show();
545 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
546 QVERIFY(m_mainWindow
->isVisible());
547 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
549 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
551 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
552 * This is especially important when a user wants to peek into multiple folders in quick succession.
554 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
555 QVERIFY(spyDirectoryLoadingCompleted
.wait());
556 QVERIFY(QTest::qWaitFor([&]() {
557 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
558 })); // "Stop" command should be disabled because it finished loading
559 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
560 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
561 QVERIFY(parentDirUrl
!= childDirUrl
);
563 auto currentItemUrl
= [this]() {
564 const int currentIndex
= m_mainWindow
->m_activeViewContainer
->view()->m_container
->controller()->selectionManager()->currentItem();
565 const KFileItem currentItem
= m_mainWindow
->m_activeViewContainer
->view()->m_model
->fileItem(currentIndex
);
566 return currentItem
.url();
569 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we just emerged from should now have keyboard focus.
570 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // The item we just emerged from should not be selected. BUG: 424723
571 // Pressing arrow keys should not only move the keyboard focus but also select the item.
572 // We press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is selected for the first time.
573 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
574 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
575 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
576 QVERIFY2(currentItemUrl() != childDirUrl
, "The current item didn't change after pressing the 'Down' key.");
577 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
578 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
579 QCOMPARE(currentItemUrl(), childDirUrl
); // After pressing 'Down' and then 'Up' we should be back where we were.
581 // Enter the child folder "b".
582 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
583 QVERIFY(spyDirectoryLoadingCompleted
.wait());
584 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
585 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
587 // Go back to the parent folder.
588 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
589 QVERIFY(spyDirectoryLoadingCompleted
.wait());
590 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
591 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
592 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
593 // Going 'Back' means that the view should be in the same state it was in when we left.
594 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we last interacted with in this location should still have keyboard focus.
595 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
596 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), childDirUrl
); // It should still be selected.
598 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
599 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
600 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
601 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
602 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
603 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
605 // Go forward to the child folder.
606 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
607 QVERIFY(spyDirectoryLoadingCompleted
.wait());
608 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
609 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
610 QCOMPARE(currentItemUrl(), QUrl(QDir::cleanPath(testDir
->url().toString() + "/b/b-1"))); // The first item in the view should have keyboard focus.
612 // Press the 'Down' key in the child folder.
613 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
614 // The second item in the view should have keyboard focus and be selected.
615 const QUrl secondItemInChildFolderUrl
{QDir::cleanPath(testDir
->url().toString() + "/b/b-2")};
616 QCOMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
617 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
618 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
620 // Go back to the parent folder and then re-enter the child folder.
621 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
622 QVERIFY(spyDirectoryLoadingCompleted
.wait());
623 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
624 QVERIFY(spyDirectoryLoadingCompleted
.wait());
625 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
626 // The state of the view should be identical to how it was before we triggered "Back" and then "Forward".
627 QTRY_COMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
628 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
629 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
631 // Go back to the parent folder.
632 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
633 QVERIFY(spyDirectoryLoadingCompleted
.wait());
634 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
635 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
636 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
638 // 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
639 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
640 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
641 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
642 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
643 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
644 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
645 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
648 void DolphinMainWindowTest::testOpenFiles()
650 QScopedPointer
<TestDir
> testDir
{new TestDir()};
651 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
652 testDir
->createDir("a");
653 testDir
->createDir("a/b");
654 testDir
->createDir("a/b/c");
655 testDir
->createDir("a/b/c/d");
656 m_mainWindow
->openDirectories({testDirUrl
}, false);
657 m_mainWindow
->show();
659 // We only see the unselected "a" folder in the test dir. There are no other tabs.
660 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
661 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
662 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
663 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
664 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
665 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
666 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
668 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
669 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
670 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
671 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
672 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
674 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
675 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
676 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
677 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
678 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
679 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
680 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
681 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
683 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
684 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
685 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
686 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
687 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
688 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
689 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
690 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
692 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
693 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
694 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
695 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
697 // 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.
698 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
699 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
701 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
702 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
703 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
704 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
705 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
706 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
708 // Switch to first tab by opening it even though it is already open.
709 m_mainWindow
->openDirectories({testDirUrl
}, false);
710 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
711 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
713 // "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.
714 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
715 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
716 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
717 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
718 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
719 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
721 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
722 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
723 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
724 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
725 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
726 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
727 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
728 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
729 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
730 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
732 // "c" is in view in the second tab because "b" is expanded there,
733 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
734 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
735 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
736 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
737 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
739 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
740 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
741 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
742 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
744 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
745 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
746 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
747 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
748 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
749 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
752 void DolphinMainWindowTest::testAccessibilityAncestorTree()
754 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
755 m_mainWindow
->show();
756 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
757 QVERIFY(m_mainWindow
->isVisible());
759 QAccessibleInterface
*accessibleInterfaceOfMainWindow
= QAccessible::queryAccessibleInterface(m_mainWindow
.get());
760 Q_CHECK_PTR(accessibleInterfaceOfMainWindow
);
762 // We will test the accessibility of objects traversing forwards and backwards.
763 int testedObjectsSizeAfterTraversingForwards
= 0;
764 for (int i
= 0; i
< 2; i
++) {
765 std::tuple
<Qt::Key
, Qt::KeyboardModifier
> focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::NoModifier
};
767 focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::ShiftModifier
};
770 // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
771 std::set
<const QObject
*> testedObjects
; // Makes sure we stop testing when we arrive at an item that was already tested.
772 while (qApp
->focusObject() && !testedObjects
.count(qApp
->focusObject())) {
773 const auto currentlyFocusedObject
= qApp
->focusObject();
775 QAccessibleInterface
*accessibleInterface
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
776 // The accessibleInterfaces of focused objects might themselves have children.
777 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
778 while (accessibleInterface
->childCount() > 0) {
779 accessibleInterface
= accessibleInterface
->child(0);
781 while (accessibleInterface
!= accessibleInterfaceOfMainWindow
) {
782 QVERIFY2(accessibleInterface
,
783 qPrintable(QStringLiteral("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
784 .arg(currentlyFocusedObject
->metaObject()->className())));
785 accessibleInterface
= accessibleInterface
->parent();
788 testedObjects
.insert(currentlyFocusedObject
); // Add it to testedObjects so we won't test it again later.
789 QTest::keyClick(m_mainWindow
.get(), std::get
<0>(focusChainTraversalKeyCombination
), std::get
<1>(focusChainTraversalKeyCombination
));
790 QVERIFY2(currentlyFocusedObject
!= qApp
->focusObject(),
791 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
795 testedObjectsSizeAfterTraversingForwards
= testedObjects
.size();
797 QCOMPARE(testedObjects
.size(), testedObjectsSizeAfterTraversingForwards
); // The size after traversing backwards is different than
798 // after going forwards which is probably not intended.
803 void DolphinMainWindowTest::testAutoSaveSession()
805 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
806 m_mainWindow
->show();
807 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
808 QVERIFY(m_mainWindow
->isVisible());
810 // Create config file
811 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
812 KConfig
*config
= KConfigGui::sessionConfig();
813 m_mainWindow
->saveGlobalProperties(config
);
814 m_mainWindow
->savePropertiesInternal(config
, 1);
817 // Setup watcher for config file changes
818 const QString configFileName
= QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/" + KConfigGui::sessionConfig()->name();
819 QFileSystemWatcher
*configWatcher
= new QFileSystemWatcher({configFileName
}, this);
820 QSignalSpy
spySessionSaved(configWatcher
, &QFileSystemWatcher::fileChanged
);
822 // Enable session autosave.
823 m_mainWindow
->setSessionAutoSaveEnabled(true);
824 m_mainWindow
->m_sessionSaveTimer
->setInterval(200); // Lower the interval to speed up the testing
827 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
829 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
830 QCOMPARE(tabWidget
->count(), 2);
832 // Wait till a session save occurs
833 QVERIFY(spySessionSaved
.wait(60000));
835 // Disable session autosave.
836 m_mainWindow
->setSessionAutoSaveEnabled(false);
839 void DolphinMainWindowTest::cleanupTestCase()
841 m_mainWindow
->showNormal();
842 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
845 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
849 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
852 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
853 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
856 QTEST_MAIN(DolphinMainWindowTest
)
858 #include "dolphinmainwindowtest.moc"