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/kfileitemmodelrolesupdater.h"
14 #include "kitemviews/kitemlistcontainer.h"
15 #include "kitemviews/kitemlistcontroller.h"
16 #include "kitemviews/kitemlistselectionmanager.h"
17 #include "kitemviews/kitemlistwidget.h"
19 #include "views/dolphinitemlistview.h"
21 #include <KActionCollection>
25 #include <QAccessible>
26 #include <QDomDocument>
27 #include <QFileSystemWatcher>
28 #include <QScopedPointer>
30 #include <QStandardPaths>
34 #include <unordered_set>
36 class DolphinMainWindowTest
: public QObject
43 void testSyncDesktopAndPhoneUi();
44 void testClosingTabsWithSearchBoxVisible();
45 void testActiveViewAfterClosingSplitView_data();
46 void testActiveViewAfterClosingSplitView();
47 void testUpdateWindowTitleAfterClosingSplitView();
48 void testUpdateWindowTitleAfterChangingSplitView();
49 void testOpenInNewTabTitle();
50 void testNewFileMenuEnabled_data();
51 void testNewFileMenuEnabled();
52 void testWindowTitle_data();
53 void testWindowTitle();
54 void testFocusLocationBar();
55 void testFocusPlacesPanel();
56 void testPlacesPanelWidthResistance();
59 void testAccessibilityTree();
60 void testAutoSaveSession();
61 void testInlineRename();
62 void testThumbnailAfterRename();
63 void cleanupTestCase();
66 QScopedPointer
<DolphinMainWindow
> m_mainWindow
;
69 void DolphinMainWindowTest::initTestCase()
71 QStandardPaths::setTestModeEnabled(true);
74 void DolphinMainWindowTest::init()
76 m_mainWindow
.reset(new DolphinMainWindow());
80 * 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
81 * mostly identical. Differences between those files need to be explicitly added as exceptions to this test. So if you land here after changing either
82 * 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
83 * changed object to the `exceptions` variable below.
85 void DolphinMainWindowTest::testSyncDesktopAndPhoneUi()
87 std::unordered_set
<QString
> exceptions
{{QStringLiteral("version"), QStringLiteral("ToolBar")}};
89 QDomDocument desktopUi
;
90 QFile
desktopUiXmlFile(":/kxmlgui5/dolphin/dolphinui.rc");
91 desktopUiXmlFile
.open(QIODevice::ReadOnly
);
92 desktopUi
.setContent(&desktopUiXmlFile
);
93 desktopUiXmlFile
.close();
96 QFile
phoneUiXmlFile(":/kxmlgui5/dolphin/dolphinuiforphones.rc");
97 phoneUiXmlFile
.open(QIODevice::ReadOnly
);
98 phoneUi
.setContent(&phoneUiXmlFile
);
99 phoneUiXmlFile
.close();
101 QDomElement desktopUiElement
= desktopUi
.documentElement();
102 QDomElement phoneUiElement
= phoneUi
.documentElement();
104 auto nextUiElement
= [&exceptions
](QDomElement uiElement
) -> QDomElement
{
105 QDomNode nextUiNode
{uiElement
};
107 // If the current node is an exception, we skip its children as well.
108 if (exceptions
.count(nextUiNode
.nodeName()) == 0) {
109 auto firstChild
{nextUiNode
.firstChild()};
110 if (!firstChild
.isNull()) {
111 nextUiNode
= firstChild
;
115 auto nextSibling
{nextUiNode
.nextSibling()};
116 if (!nextSibling
.isNull()) {
117 nextUiNode
= nextSibling
;
120 auto parent
{nextUiNode
.parentNode()};
122 if (parent
.isNull()) {
123 return QDomElement();
125 auto nextParentSibling
{parent
.nextSibling()};
126 if (!nextParentSibling
.isNull()) {
127 nextUiNode
= nextParentSibling
;
130 parent
= parent
.parentNode();
134 && (nextUiNode
.toElement().isNull() || exceptions
.count(nextUiNode
.nodeName()))); // We loop until we either give up finding an element or find one.
135 if (nextUiNode
.isNull()) {
136 return QDomElement();
138 return nextUiNode
.toElement();
141 int totalComparisonsCount
{0};
143 QVERIFY2(desktopUiElement
.tagName() == phoneUiElement
.tagName(),
144 qPrintable(QStringLiteral("Node mismatch: dolphinui.rc/%1::%2 and dolphinuiforphones.rc/%3::%4")
145 .arg(desktopUiElement
.parentNode().toElement().tagName())
146 .arg(desktopUiElement
.tagName())
147 .arg(phoneUiElement
.parentNode().toElement().tagName())
148 .arg(phoneUiElement
.tagName())));
149 QCOMPARE(desktopUiElement
.text(), phoneUiElement
.text());
150 const auto desktopUiElementAttributes
= desktopUiElement
.attributes();
151 const auto phoneUiElementAttributes
= phoneUiElement
.attributes();
152 for (int i
= 0; i
< desktopUiElementAttributes
.count(); i
++) {
153 QVERIFY2(phoneUiElementAttributes
.count() >= i
,
154 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has more attributes than dolphinuiforphones.rc/%3::%4")
155 .arg(desktopUiElement
.parentNode().toElement().tagName())
156 .arg(desktopUiElement
.tagName())
157 .arg(phoneUiElement
.parentNode().toElement().tagName())
158 .arg(phoneUiElement
.tagName())));
159 if (exceptions
.count(desktopUiElementAttributes
.item(i
).nodeName())) {
162 QCOMPARE(desktopUiElementAttributes
.item(i
).nodeName(), phoneUiElementAttributes
.item(i
).nodeName());
163 QCOMPARE(desktopUiElementAttributes
.item(i
).nodeValue(), phoneUiElementAttributes
.item(i
).nodeValue());
164 totalComparisonsCount
++;
166 QVERIFY2(desktopUiElementAttributes
.count() == phoneUiElementAttributes
.count(),
167 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has fewer attributes than dolphinuiforphones.rc/%3::%4. %5 < %6")
168 .arg(desktopUiElement
.parentNode().toElement().tagName())
169 .arg(desktopUiElement
.tagName())
170 .arg(phoneUiElement
.parentNode().toElement().tagName())
171 .arg(phoneUiElement
.tagName())
172 .arg(phoneUiElementAttributes
.count())
173 .arg(desktopUiElementAttributes
.count())));
175 desktopUiElement
= nextUiElement(desktopUiElement
);
176 phoneUiElement
= nextUiElement(phoneUiElement
);
177 totalComparisonsCount
++;
178 } while (!desktopUiElement
.isNull() || !phoneUiElement
.isNull());
179 QVERIFY2(totalComparisonsCount
> 200, qPrintable(QStringLiteral("There were only %1 comparisons. Did the test run correctly?").arg(totalComparisonsCount
)));
182 // See https://bugs.kde.org/show_bug.cgi?id=379135
183 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
185 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
186 m_mainWindow
->show();
187 // Without this call the searchbox doesn't get FocusIn events.
188 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
189 QVERIFY(m_mainWindow
->isVisible());
191 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
194 // Show search box on first tab.
195 tabWidget
->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
197 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
198 QCOMPARE(tabWidget
->count(), 2);
200 // Triggers the crash in bug #379135.
201 tabWidget
->closeTab();
202 QCOMPARE(tabWidget
->count(), 1);
205 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
207 QTest::addColumn
<bool>("closeLeftView");
209 QTest::newRow("close left view") << true;
210 QTest::newRow("close right view") << false;
213 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
215 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
216 m_mainWindow
->show();
217 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
218 QVERIFY(m_mainWindow
->isVisible());
220 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
222 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
223 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
226 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
227 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
228 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
230 // Make sure the right view is the active one.
231 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
232 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
233 QVERIFY(!leftViewContainer
->isActive());
234 QVERIFY(rightViewContainer
->isActive());
236 QFETCH(bool, closeLeftView
);
238 // Activate left view.
239 leftViewContainer
->setActive(true);
240 QVERIFY(leftViewContainer
->isActive());
241 QVERIFY(!rightViewContainer
->isActive());
243 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
244 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
245 QVERIFY(!leftViewContainer
->isActive());
246 QVERIFY(rightViewContainer
->isActive());
247 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
249 // Close right view. The left view will become active.
250 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
251 QVERIFY(leftViewContainer
->isActive());
252 QVERIFY(!rightViewContainer
->isActive());
253 QCOMPARE(leftViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
257 // Test case for bug #385111
258 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
260 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
261 m_mainWindow
->show();
262 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
263 QVERIFY(m_mainWindow
->isVisible());
265 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
267 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
268 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
271 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
272 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
273 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
275 // Make sure the right view is the active one.
276 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
277 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
278 QVERIFY(!leftViewContainer
->isActive());
279 QVERIFY(rightViewContainer
->isActive());
281 // Activate left view.
282 leftViewContainer
->setActive(true);
283 QVERIFY(leftViewContainer
->isActive());
284 QVERIFY(!rightViewContainer
->isActive());
286 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
287 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
288 QVERIFY(!leftViewContainer
->isActive());
289 QVERIFY(rightViewContainer
->isActive());
290 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
292 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
293 QSignalSpy
currentUrlChangedSpy(tabWidget
, &DolphinTabWidget::currentUrlChanged
);
294 tabWidget
->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
295 QCOMPARE(currentUrlChangedSpy
.count(), 1);
298 // Test case for bug #402641
299 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
301 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
302 m_mainWindow
->show();
303 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
304 QVERIFY(m_mainWindow
->isVisible());
306 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
310 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
311 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
313 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
314 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
316 // Store old window title.
317 const auto oldTitle
= m_mainWindow
->windowTitle();
319 // Change URL in the right view and make sure the title gets updated.
320 rightViewContainer
->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
321 QVERIFY(m_mainWindow
->windowTitle() != oldTitle
);
323 // Activate back the left view and check whether the old title gets restored.
324 leftViewContainer
->setActive(true);
325 QCOMPARE(m_mainWindow
->windowTitle(), oldTitle
);
328 // Test case for bug #397910
329 void DolphinMainWindowTest::testOpenInNewTabTitle()
331 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
332 m_mainWindow
->openDirectories({homePathUrl
}, false);
333 m_mainWindow
->show();
334 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
335 QVERIFY(m_mainWindow
->isVisible());
337 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
340 const QUrl tempPathUrl
{QUrl::fromLocalFile(QDir::tempPath())};
341 tabWidget
->openNewTab(tempPathUrl
);
342 QCOMPARE(tabWidget
->count(), 2);
343 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
345 QVERIFY2(!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
346 QCOMPARE(KIO::iconNameForUrl(homePathUrl
), tabWidget
->tabIcon(0).name());
347 QCOMPARE(KIO::iconNameForUrl(tempPathUrl
), tabWidget
->tabIcon(1).name());
350 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
352 QTest::addColumn
<QUrl
>("activeViewUrl");
353 QTest::addColumn
<bool>("expectedEnabled");
355 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
356 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
357 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
360 void DolphinMainWindowTest::testNewFileMenuEnabled()
362 QFETCH(QUrl
, activeViewUrl
);
363 m_mainWindow
->openDirectories({activeViewUrl
}, false);
364 m_mainWindow
->show();
365 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
366 QVERIFY(m_mainWindow
->isVisible());
368 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
369 QVERIFY(newFileMenu
);
371 QFETCH(bool, expectedEnabled
);
372 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
375 void DolphinMainWindowTest::testWindowTitle_data()
377 QTest::addColumn
<QUrl
>("activeViewUrl");
378 QTest::addColumn
<QString
>("expectedWindowTitle");
380 // TODO: this test should enforce the english locale.
381 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
382 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
383 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
386 void DolphinMainWindowTest::testWindowTitle()
388 QFETCH(QUrl
, activeViewUrl
);
389 m_mainWindow
->openDirectories({activeViewUrl
}, false);
390 m_mainWindow
->show();
391 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
392 QVERIFY(m_mainWindow
->isVisible());
394 QFETCH(QString
, expectedWindowTitle
);
395 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
398 void DolphinMainWindowTest::testFocusLocationBar()
400 const QUrl homePathUrl
{QUrl::fromLocalFile(QDir::homePath())};
401 m_mainWindow
->openDirectories({homePathUrl
}, false);
402 m_mainWindow
->show();
403 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
404 QVERIFY(m_mainWindow
->isVisible());
406 QAction
*replaceLocationAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("replace_location"));
407 replaceLocationAction
->trigger();
408 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
409 replaceLocationAction
->trigger();
410 QVERIFY(m_mainWindow
->activeViewContainer()->view()->hasFocus());
412 QAction
*editableLocationAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("editable_location"));
413 editableLocationAction
->trigger();
414 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
415 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isUrlEditable());
416 editableLocationAction
->trigger();
417 QVERIFY(!m_mainWindow
->activeViewContainer()->urlNavigator()->isUrlEditable());
419 replaceLocationAction
->trigger();
420 QVERIFY(m_mainWindow
->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
422 // Pressing Escape multiple times should eventually move the focus back to the active view.
423 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape
); // Focus might not go the view yet because it toggles the editable state of the location bar.
424 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape
);
425 QVERIFY(m_mainWindow
->activeViewContainer()->view()->hasFocus());
428 void DolphinMainWindowTest::testFocusPlacesPanel()
430 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
431 m_mainWindow
->show();
432 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
433 QVERIFY(m_mainWindow
->isVisible());
435 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
436 QVERIFY2(QTest::qWaitFor(
438 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0 && placesPanel
->height() > 0;
441 "The test couldn't be initialised properly. The places panel should be visible.");
443 QAction
*focusPlacesPanelAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("focus_places_panel"));
444 QAction
*showPlacesPanelAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("show_places_panel"));
446 focusPlacesPanelAction
->trigger();
447 QVERIFY(placesPanel
->hasFocus());
449 focusPlacesPanelAction
->trigger();
450 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
451 "Triggering focus_places_panel while the panel already has focus should return the focus to the view.");
453 focusPlacesPanelAction
->trigger();
454 QVERIFY(placesPanel
->hasFocus());
456 showPlacesPanelAction
->trigger();
457 QVERIFY(!placesPanel
->isVisible());
458 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
459 "Hiding the Places panel while it has focus should return the focus to the view.");
461 showPlacesPanelAction
->trigger();
462 QVERIFY(placesPanel
->isVisible());
463 QVERIFY2(placesPanel
->hasFocus(), "Enabling the Places panel should move keyboard focus there.");
465 /// Test that activating a place always moves focus to the view.
466 QTest::keyClick(QApplication::focusWidget(), Qt::Key::Key_Enter
);
467 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
468 "Activating a place should move focus to the view that loads that place.");
470 focusPlacesPanelAction
->trigger();
471 QVERIFY(placesPanel
->hasFocus());
473 QTest::keyClick(QApplication::focusWidget(), Qt::Key::Key_Enter
);
474 QVERIFY2(m_mainWindow
->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
475 "Activating a place should move focus to the view even if the view already has that place loaded.");
479 * The places panel will resize itself if any of the other widgets requires too much horizontal space
480 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
482 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
484 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
485 m_mainWindow
->show();
486 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
487 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
488 QVERIFY(m_mainWindow
->isVisible());
490 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
491 QVERIFY2(QTest::qWaitFor(
493 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
496 "The test couldn't be initialised properly. The places panel should be visible.");
498 const int initialPlacesPanelWidth
= placesPanel
->width();
500 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
501 QTest::qWait(300); // wait for animation
502 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
504 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
505 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
507 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
508 // 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.
509 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
510 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
511 selectionModeStates
++) {
512 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
513 m_mainWindow
->slotSetSelectionMode(true, contents
);
514 QTest::qWait(20); // give time for a paint/resize
515 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
518 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
519 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
522 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
523 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
527 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
528 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
531 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
532 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
535 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
536 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
540 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
541 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
544 m_mainWindow
->showMaximized();
545 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
547 QTest::qWait(300); // wait for split view closing animation
548 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
551 void DolphinMainWindowTest::testGoActions()
553 QScopedPointer
<TestDir
> testDir
{new TestDir()};
554 testDir
->createDir("a");
555 testDir
->createDir("b");
556 testDir
->createDir("b/b-1");
557 testDir
->createFile("b/b-2");
558 testDir
->createDir("c");
559 const QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
560 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
561 m_mainWindow
->show();
562 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
563 QVERIFY(m_mainWindow
->isVisible());
564 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
566 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
568 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
569 * This is especially important when a user wants to peek into multiple folders in quick succession.
571 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
572 QVERIFY(spyDirectoryLoadingCompleted
.wait());
573 QVERIFY(QTest::qWaitFor([&]() {
574 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
575 })); // "Stop" command should be disabled because it finished loading
576 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
577 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
578 QVERIFY(parentDirUrl
!= childDirUrl
);
580 auto currentItemUrl
= [this]() {
581 const int currentIndex
= m_mainWindow
->m_activeViewContainer
->view()->m_container
->controller()->selectionManager()->currentItem();
582 const KFileItem currentItem
= m_mainWindow
->m_activeViewContainer
->view()->m_model
->fileItem(currentIndex
);
583 return currentItem
.url();
586 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we just emerged from should now have keyboard focus.
587 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // The item we just emerged from should not be selected. BUG: 424723
588 // Pressing arrow keys should not only move the keyboard focus but also select the item.
589 // We press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is selected for the first time.
590 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
591 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
592 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
593 QVERIFY2(currentItemUrl() != childDirUrl
, "The current item didn't change after pressing the 'Down' key.");
594 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
595 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
596 QCOMPARE(currentItemUrl(), childDirUrl
); // After pressing 'Down' and then 'Up' we should be back where we were.
598 // Enter the child folder "b".
599 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
600 QVERIFY(spyDirectoryLoadingCompleted
.wait());
601 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
602 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
604 // Go back to the parent folder.
605 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
606 QVERIFY(spyDirectoryLoadingCompleted
.wait());
607 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
608 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
609 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
610 // Going 'Back' means that the view should be in the same state it was in when we left.
611 QCOMPARE(currentItemUrl(), childDirUrl
); // The item we last interacted with in this location should still have keyboard focus.
612 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
613 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), childDirUrl
); // It should still be selected.
615 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
616 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
617 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
618 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
619 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
620 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
622 // Go forward to the child folder.
623 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
624 QVERIFY(spyDirectoryLoadingCompleted
.wait());
625 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
626 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
627 QCOMPARE(currentItemUrl(), QUrl(QDir::cleanPath(testDir
->url().toString() + "/b/b-1"))); // The first item in the view should have keyboard focus.
629 // Press the 'Down' key in the child folder.
630 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
631 // The second item in the view should have keyboard focus and be selected.
632 const QUrl secondItemInChildFolderUrl
{QDir::cleanPath(testDir
->url().toString() + "/b/b-2")};
633 QCOMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
634 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
635 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
637 // Go back to the parent folder and then re-enter the child folder.
638 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
639 QVERIFY(spyDirectoryLoadingCompleted
.wait());
640 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
641 QVERIFY(spyDirectoryLoadingCompleted
.wait());
642 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
643 // The state of the view should be identical to how it was before we triggered "Back" and then "Forward".
644 QTRY_COMPARE(currentItemUrl(), secondItemInChildFolderUrl
);
645 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
646 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl
);
648 // Go back to the parent folder.
649 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
650 QVERIFY(spyDirectoryLoadingCompleted
.wait());
651 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
652 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
653 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
655 // 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
656 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
657 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
658 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
659 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
660 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
661 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
662 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
665 void DolphinMainWindowTest::testOpenFiles()
667 QScopedPointer
<TestDir
> testDir
{new TestDir()};
668 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
669 testDir
->createDir("a");
670 testDir
->createDir("a/b");
671 testDir
->createDir("a/b/c");
672 testDir
->createDir("a/b/c/d");
673 m_mainWindow
->openDirectories({testDirUrl
}, false);
674 m_mainWindow
->show();
676 // We only see the unselected "a" folder in the test dir. There are no other tabs.
677 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
678 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
679 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
680 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
681 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
682 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
683 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
685 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
686 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
687 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
688 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
689 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
691 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
692 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
693 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
694 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
695 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
696 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
697 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
698 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
700 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
701 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
702 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
703 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
704 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
705 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
706 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
707 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
709 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
710 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
711 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
712 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
714 // 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.
715 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
716 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
718 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
719 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
720 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
721 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
722 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
723 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
725 // Switch to first tab by opening it even though it is already open.
726 m_mainWindow
->openDirectories({testDirUrl
}, false);
727 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
728 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
730 // "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.
731 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
732 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
733 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
734 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
735 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
736 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
738 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
739 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
740 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
741 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
742 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
743 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
744 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
745 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
746 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
747 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
749 // "c" is in view in the second tab because "b" is expanded there,
750 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
751 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
752 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
753 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
754 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
756 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
757 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
758 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
759 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
761 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
762 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
763 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
764 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
765 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
766 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
769 void DolphinMainWindowTest::testAccessibilityTree()
771 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
772 m_mainWindow
->show();
773 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
774 QVERIFY(m_mainWindow
->isVisible());
776 QAccessibleInterface
*accessibleInterfaceOfMainWindow
= QAccessible::queryAccessibleInterface(m_mainWindow
.get());
777 Q_CHECK_PTR(accessibleInterfaceOfMainWindow
);
779 /// Test the accessibility of objects while traversing forwards (Tab key) and backwards (Shift+Tab).
780 int testedObjectsSizeAfterTraversingForwards
= 0;
781 for (int i
= 0; i
< 2; i
++) {
782 std::tuple
<Qt::Key
, Qt::KeyboardModifier
> focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::NoModifier
};
784 focusChainTraversalKeyCombination
= {Qt::Key::Key_Tab
, Qt::ShiftModifier
};
787 /// @see firstNamedAncestor below.
788 QAccessibleInterface
*firstNamedAncestorOfPreviousIteration
= nullptr;
790 /// Perform accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
791 std::set
<const QObject
*> testedObjects
; // Makes sure we stop testing when we arrive at an item that was already tested.
792 while (qApp
->focusObject() && !testedObjects
.count(qApp
->focusObject())) {
793 const auto currentlyFocusedObject
= qApp
->focusObject();
794 const QAccessibleInterface
*accessibleIntefaceOfCurrentlyFocusedObject
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
795 QVERIFY(accessibleIntefaceOfCurrentlyFocusedObject
);
797 /// Test that each object reachable by Tab or Shift+Tab has at least some accessible information. Objects without any accessible information
798 /// are even less useful to accessibility software users than unlabeled buttons are e.g. to sighted users, because unlabeled buttons at least
799 /// convey some information through their placement and icon.
800 if (currentlyFocusedObject
!= m_mainWindow
->m_activeViewContainer
->view()->m_container
) { // Skip the custom container widget which has no
801 // accessible name on purpose.
803 * The first ancestor with an accessible name is interesting because it is sometimes used to identify an object if the object itself has no
804 * name. We keep it in mind to check if two subsequent objects without a name can at least be told apart by their first named ancestor.
806 QAccessibleInterface
*firstNamedAncestor
= accessibleIntefaceOfCurrentlyFocusedObject
->parent();
807 while (firstNamedAncestor
) {
808 if (!firstNamedAncestor
->text(QAccessible::Name
).isEmpty()) {
811 firstNamedAncestor
= firstNamedAncestor
->parent();
813 QTRY_VERIFY2(!accessibleIntefaceOfCurrentlyFocusedObject
->text(QAccessible::Name
).isEmpty()
814 || (firstNamedAncestor
&& firstNamedAncestor
!= firstNamedAncestorOfPreviousIteration
),
815 qPrintable(QStringLiteral("%1's accessibleInterface does not have an accessible name and can not be distinguished from the object"
816 " that had focus previously. Please fix this. You can find this %1 within its parent %2.")
817 .arg(currentlyFocusedObject
->metaObject()->className())
818 .arg(currentlyFocusedObject
->parent()->metaObject()->className())));
819 firstNamedAncestorOfPreviousIteration
= firstNamedAncestor
;
822 /// Test that each accessible interface has the main window as its parent.
823 QAccessibleInterface
*accessibleInterface
= QAccessible::queryAccessibleInterface(currentlyFocusedObject
);
824 // The accessibleInterfaces of focused objects might themselves have children.
825 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
826 while (accessibleInterface
->childCount() > 0) {
827 accessibleInterface
= accessibleInterface
->child(0);
829 while (accessibleInterface
!= accessibleInterfaceOfMainWindow
) {
830 QVERIFY2(accessibleInterface
,
831 qPrintable(QStringLiteral("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
832 .arg(currentlyFocusedObject
->metaObject()->className())));
833 accessibleInterface
= accessibleInterface
->parent();
836 testedObjects
.insert(currentlyFocusedObject
); // Add it to testedObjects so we won't test it again later.
837 QTest::keyClick(m_mainWindow
.get(), std::get
<0>(focusChainTraversalKeyCombination
), std::get
<1>(focusChainTraversalKeyCombination
));
838 QVERIFY2(currentlyFocusedObject
!= qApp
->focusObject(),
839 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
843 testedObjectsSizeAfterTraversingForwards
= testedObjects
.size();
845 QCOMPARE(testedObjects
.size(), testedObjectsSizeAfterTraversingForwards
); // The size after traversing backwards is different than
846 // after going forwards which is probably not intended.
849 QCOMPARE_GE(testedObjectsSizeAfterTraversingForwards
, 12); // The test did not reach many objects while using the Tab key to move through Dolphin. Did the
850 // test run correctly?
853 void DolphinMainWindowTest::testAutoSaveSession()
855 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
856 m_mainWindow
->show();
857 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
858 QVERIFY(m_mainWindow
->isVisible());
860 // Create config file
861 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
862 KConfig
*config
= KConfigGui::sessionConfig();
863 m_mainWindow
->saveGlobalProperties(config
);
864 m_mainWindow
->savePropertiesInternal(config
, 1);
867 // Setup watcher for config file changes
868 const QString configFileName
= QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation
) + "/" + KConfigGui::sessionConfig()->name();
869 QFileSystemWatcher
*configWatcher
= new QFileSystemWatcher({configFileName
}, this);
870 QSignalSpy
spySessionSaved(configWatcher
, &QFileSystemWatcher::fileChanged
);
872 // Enable session autosave.
873 m_mainWindow
->setSessionAutoSaveEnabled(true);
874 m_mainWindow
->m_sessionSaveTimer
->setInterval(200); // Lower the interval to speed up the testing
877 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
879 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
880 QCOMPARE(tabWidget
->count(), 2);
882 // Wait till a session save occurs
883 QVERIFY(spySessionSaved
.wait(60000));
885 // Disable session autosave.
886 m_mainWindow
->setSessionAutoSaveEnabled(false);
889 void DolphinMainWindowTest::testInlineRename()
891 QScopedPointer
<TestDir
> testDir
{new TestDir()};
892 testDir
->createFiles({"aaaa", "bbbb", "cccc", "dddd"});
893 m_mainWindow
->openDirectories({testDir
->url()}, false);
894 m_mainWindow
->show();
895 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
896 QVERIFY(m_mainWindow
->isVisible());
898 DolphinView
*view
= m_mainWindow
->activeViewContainer()->view();
899 QSignalSpy
viewDirectoryLoadingCompletedSpy(view
, &DolphinView::directoryLoadingCompleted
);
900 QSignalSpy
itemsReorderedSpy(view
->m_model
, &KFileItemModel::itemsMoved
);
901 QSignalSpy
modelDirectoryLoadingCompletedSpy(view
->m_model
, &KFileItemModel::directoryLoadingCompleted
);
903 QVERIFY(viewDirectoryLoadingCompletedSpy
.wait());
904 QTest::qWait(500); // we need to wait for the file widgets to become visible
905 view
->markUrlsAsSelected({QUrl(testDir
->url().toString() + "/aaaa")});
906 view
->updateViewState();
907 view
->renameSelectedItems();
908 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left
);
909 QTest::keyClick(QApplication::focusWidget(), Qt::Key_E
);
910 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down
);
912 QVERIFY(itemsReorderedSpy
.wait());
913 QVERIFY(view
->m_view
->m_editingRole
);
914 KItemListWidget
*widget
= view
->m_view
->m_visibleItems
.value(view
->m_view
->firstVisibleIndex());
915 QVERIFY(!widget
->editedRole().isEmpty());
917 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left
);
918 QTest::keyClick(QApplication::focusWidget(), Qt::Key_A
);
919 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down
);
920 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down
);
921 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left
);
922 QTest::keyClick(QApplication::focusWidget(), Qt::Key_A
);
923 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down
);
925 QVERIFY(itemsReorderedSpy
.wait());
926 QVERIFY(view
->m_view
->m_editingRole
);
927 widget
= view
->m_view
->m_visibleItems
.value(view
->m_view
->lastVisibleIndex());
928 QVERIFY(!widget
->editedRole().isEmpty());
930 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape
);
931 QVERIFY(widget
->isCurrent());
932 view
->m_model
->refreshDirectory(testDir
->url());
933 QVERIFY(modelDirectoryLoadingCompletedSpy
.wait());
935 QCOMPARE(view
->m_model
->fileItem(0).name(), "abbbb");
936 QCOMPARE(view
->m_model
->fileItem(1).name(), "adddd");
937 QCOMPARE(view
->m_model
->fileItem(2).name(), "cccc");
938 QCOMPARE(view
->m_model
->fileItem(3).name(), "eaaaa");
939 QCOMPARE(view
->m_model
->count(), 4);
942 void DolphinMainWindowTest::testThumbnailAfterRename()
944 // Create testdir and red square jpg for testing
945 QScopedPointer
<TestDir
> testDir
{new TestDir()};
946 QImage
testImage(256, 256, QImage::Format_Mono
);
947 testImage
.setColorCount(1);
948 testImage
.setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
949 for (short x
= 0; x
< 256; ++x
) {
950 for (short y
= 0; y
< 256; ++y
) {
951 testImage
.setPixel(x
, y
, 0);
954 testImage
.save(testDir
.data()->path() + "/a.jpg");
956 // Open dir and show it
957 m_mainWindow
->openDirectories({testDir
->url()}, false);
958 DolphinView
*view
= m_mainWindow
->activeViewContainer()->view();
959 // Prepare signal spies
960 QSignalSpy
viewDirectoryLoadingCompletedSpy(view
, &DolphinView::directoryLoadingCompleted
);
961 QSignalSpy
itemsChangedSpy(view
->m_model
, &KFileItemModel::itemsChanged
);
962 QSignalSpy
modelDirectoryLoadingCompletedSpy(view
->m_model
, &KFileItemModel::directoryLoadingCompleted
);
963 QSignalSpy
previewUpdatedSpy(view
->m_view
->m_modelRolesUpdater
, &KFileItemModelRolesUpdater::previewJobFinished
);
964 // Show window and check that our preview has been updated, then wait for it to appear
965 m_mainWindow
->show();
966 QVERIFY(viewDirectoryLoadingCompletedSpy
.wait());
967 QVERIFY(previewUpdatedSpy
.wait());
968 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
969 QVERIFY(m_mainWindow
->isVisible());
970 QTest::qWait(500); // we need to wait for the file widgets to become visible
972 // Set image selected and rename it to b.jpg, make sure editing role is working
973 view
->markUrlsAsSelected({QUrl(testDir
->url().toString() + "/a.jpg")});
974 view
->updateViewState();
975 view
->renameSelectedItems();
976 QVERIFY(view
->m_view
->m_editingRole
);
977 QTest::keyClick(QApplication::focusWidget(), Qt::Key_B
);
978 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Enter
);
979 QVERIFY(itemsChangedSpy
.wait()); // Make sure that rename worked
981 // Check that preview gets updated and filename is correct
982 QVERIFY(previewUpdatedSpy
.wait());
983 QVERIFY(!view
->m_view
->m_editingRole
);
984 QCOMPARE(view
->m_model
->fileItem(0).name(), "b.jpg");
985 QCOMPARE(view
->m_model
->count(), 1);
988 void DolphinMainWindowTest::cleanupTestCase()
990 m_mainWindow
->showNormal();
991 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
994 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
998 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
1001 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
1002 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
1005 QTEST_MAIN(DolphinMainWindowTest
)
1007 #include "dolphinmainwindowtest.moc"