]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinmainwindowtest.cpp
Fix inline renaming multiple files when renaming changes sorting order
[dolphin.git] / src / tests / dolphinmainwindowtest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
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"
16 #include "kitemviews/kitemlistwidget.h"
17 #include "testdir.h"
18 #include "views/dolphinitemlistview.h"
19
20 #include <KActionCollection>
21 #include <KConfig>
22 #include <KConfigGui>
23
24 #include <QAccessible>
25 #include <QDomDocument>
26 #include <QFileSystemWatcher>
27 #include <QScopedPointer>
28 #include <QSignalSpy>
29 #include <QStandardPaths>
30 #include <QTest>
31
32 #include <set>
33 #include <unordered_set>
34
35 class DolphinMainWindowTest : public QObject
36 {
37 Q_OBJECT
38
39 private Q_SLOTS:
40 void initTestCase();
41 void init();
42 void testSyncDesktopAndPhoneUi();
43 void testClosingTabsWithSearchBoxVisible();
44 void testActiveViewAfterClosingSplitView_data();
45 void testActiveViewAfterClosingSplitView();
46 void testUpdateWindowTitleAfterClosingSplitView();
47 void testUpdateWindowTitleAfterChangingSplitView();
48 void testOpenInNewTabTitle();
49 void testNewFileMenuEnabled_data();
50 void testNewFileMenuEnabled();
51 void testWindowTitle_data();
52 void testWindowTitle();
53 void testFocusLocationBar();
54 void testFocusPlacesPanel();
55 void testPlacesPanelWidthResistance();
56 void testGoActions();
57 void testOpenFiles();
58 void testAccessibilityTree();
59 void testAutoSaveSession();
60 void testInlineRename();
61 void cleanupTestCase();
62
63 private:
64 QScopedPointer<DolphinMainWindow> m_mainWindow;
65 };
66
67 void DolphinMainWindowTest::initTestCase()
68 {
69 QStandardPaths::setTestModeEnabled(true);
70 }
71
72 void DolphinMainWindowTest::init()
73 {
74 m_mainWindow.reset(new DolphinMainWindow());
75 }
76
77 /**
78 * 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
79 * mostly identical. Differences between those files need to be explicitly added as exceptions to this test. So if you land here after changing either
80 * 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
81 * changed object to the `exceptions` variable below.
82 */
83 void DolphinMainWindowTest::testSyncDesktopAndPhoneUi()
84 {
85 std::unordered_set<QString> exceptions{{QStringLiteral("version"), QStringLiteral("ToolBar")}};
86
87 QDomDocument desktopUi;
88 QFile desktopUiXmlFile(":/kxmlgui5/dolphin/dolphinui.rc");
89 desktopUiXmlFile.open(QIODevice::ReadOnly);
90 desktopUi.setContent(&desktopUiXmlFile);
91 desktopUiXmlFile.close();
92
93 QDomDocument phoneUi;
94 QFile phoneUiXmlFile(":/kxmlgui5/dolphin/dolphinuiforphones.rc");
95 phoneUiXmlFile.open(QIODevice::ReadOnly);
96 phoneUi.setContent(&phoneUiXmlFile);
97 phoneUiXmlFile.close();
98
99 QDomElement desktopUiElement = desktopUi.documentElement();
100 QDomElement phoneUiElement = phoneUi.documentElement();
101
102 auto nextUiElement = [&exceptions](QDomElement uiElement) -> QDomElement {
103 QDomNode nextUiNode{uiElement};
104 do {
105 // If the current node is an exception, we skip its children as well.
106 if (exceptions.count(nextUiNode.nodeName()) == 0) {
107 auto firstChild{nextUiNode.firstChild()};
108 if (!firstChild.isNull()) {
109 nextUiNode = firstChild;
110 continue;
111 }
112 }
113 auto nextSibling{nextUiNode.nextSibling()};
114 if (!nextSibling.isNull()) {
115 nextUiNode = nextSibling;
116 continue;
117 }
118 auto parent{nextUiNode.parentNode()};
119 while (true) {
120 if (parent.isNull()) {
121 return QDomElement();
122 }
123 auto nextParentSibling{parent.nextSibling()};
124 if (!nextParentSibling.isNull()) {
125 nextUiNode = nextParentSibling;
126 break;
127 }
128 parent = parent.parentNode();
129 }
130 } while (
131 !nextUiNode.isNull()
132 && (nextUiNode.toElement().isNull() || exceptions.count(nextUiNode.nodeName()))); // We loop until we either give up finding an element or find one.
133 if (nextUiNode.isNull()) {
134 return QDomElement();
135 }
136 return nextUiNode.toElement();
137 };
138
139 int totalComparisonsCount{0};
140 do {
141 QVERIFY2(desktopUiElement.tagName() == phoneUiElement.tagName(),
142 qPrintable(QStringLiteral("Node mismatch: dolphinui.rc/%1::%2 and dolphinuiforphones.rc/%3::%4")
143 .arg(desktopUiElement.parentNode().toElement().tagName())
144 .arg(desktopUiElement.tagName())
145 .arg(phoneUiElement.parentNode().toElement().tagName())
146 .arg(phoneUiElement.tagName())));
147 QCOMPARE(desktopUiElement.text(), phoneUiElement.text());
148 const auto desktopUiElementAttributes = desktopUiElement.attributes();
149 const auto phoneUiElementAttributes = phoneUiElement.attributes();
150 for (int i = 0; i < desktopUiElementAttributes.count(); i++) {
151 QVERIFY2(phoneUiElementAttributes.count() >= i,
152 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has more attributes than dolphinuiforphones.rc/%3::%4")
153 .arg(desktopUiElement.parentNode().toElement().tagName())
154 .arg(desktopUiElement.tagName())
155 .arg(phoneUiElement.parentNode().toElement().tagName())
156 .arg(phoneUiElement.tagName())));
157 if (exceptions.count(desktopUiElementAttributes.item(i).nodeName())) {
158 continue;
159 }
160 QCOMPARE(desktopUiElementAttributes.item(i).nodeName(), phoneUiElementAttributes.item(i).nodeName());
161 QCOMPARE(desktopUiElementAttributes.item(i).nodeValue(), phoneUiElementAttributes.item(i).nodeValue());
162 totalComparisonsCount++;
163 }
164 QVERIFY2(desktopUiElementAttributes.count() == phoneUiElementAttributes.count(),
165 qPrintable(QStringLiteral("Attribute mismatch: dolphinui.rc/%1::%2 has fewer attributes than dolphinuiforphones.rc/%3::%4. %5 < %6")
166 .arg(desktopUiElement.parentNode().toElement().tagName())
167 .arg(desktopUiElement.tagName())
168 .arg(phoneUiElement.parentNode().toElement().tagName())
169 .arg(phoneUiElement.tagName())
170 .arg(phoneUiElementAttributes.count())
171 .arg(desktopUiElementAttributes.count())));
172
173 desktopUiElement = nextUiElement(desktopUiElement);
174 phoneUiElement = nextUiElement(phoneUiElement);
175 totalComparisonsCount++;
176 } while (!desktopUiElement.isNull() || !phoneUiElement.isNull());
177 QVERIFY2(totalComparisonsCount > 200, qPrintable(QStringLiteral("There were only %1 comparisons. Did the test run correctly?").arg(totalComparisonsCount)));
178 }
179
180 // See https://bugs.kde.org/show_bug.cgi?id=379135
181 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
182 {
183 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
184 m_mainWindow->show();
185 // Without this call the searchbox doesn't get FocusIn events.
186 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
187 QVERIFY(m_mainWindow->isVisible());
188
189 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
190 QVERIFY(tabWidget);
191
192 // Show search box on first tab.
193 tabWidget->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
194
195 tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
196 QCOMPARE(tabWidget->count(), 2);
197
198 // Triggers the crash in bug #379135.
199 tabWidget->closeTab();
200 QCOMPARE(tabWidget->count(), 1);
201 }
202
203 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
204 {
205 QTest::addColumn<bool>("closeLeftView");
206
207 QTest::newRow("close left view") << true;
208 QTest::newRow("close right view") << false;
209 }
210
211 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
212 {
213 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
214 m_mainWindow->show();
215 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
216 QVERIFY(m_mainWindow->isVisible());
217
218 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
219 QVERIFY(tabWidget);
220 QVERIFY(tabWidget->currentTabPage()->primaryViewContainer());
221 QVERIFY(!tabWidget->currentTabPage()->secondaryViewContainer());
222
223 // Open split view.
224 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
225 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
226 QVERIFY(tabWidget->currentTabPage()->secondaryViewContainer());
227
228 // Make sure the right view is the active one.
229 auto leftViewContainer = tabWidget->currentTabPage()->primaryViewContainer();
230 auto rightViewContainer = tabWidget->currentTabPage()->secondaryViewContainer();
231 QVERIFY(!leftViewContainer->isActive());
232 QVERIFY(rightViewContainer->isActive());
233
234 QFETCH(bool, closeLeftView);
235 if (closeLeftView) {
236 // Activate left view.
237 leftViewContainer->setActive(true);
238 QVERIFY(leftViewContainer->isActive());
239 QVERIFY(!rightViewContainer->isActive());
240
241 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
242 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
243 QVERIFY(!leftViewContainer->isActive());
244 QVERIFY(rightViewContainer->isActive());
245 QCOMPARE(rightViewContainer, tabWidget->currentTabPage()->activeViewContainer());
246 } else {
247 // Close right view. The left view will become active.
248 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
249 QVERIFY(leftViewContainer->isActive());
250 QVERIFY(!rightViewContainer->isActive());
251 QCOMPARE(leftViewContainer, tabWidget->currentTabPage()->activeViewContainer());
252 }
253 }
254
255 // Test case for bug #385111
256 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
257 {
258 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
259 m_mainWindow->show();
260 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
261 QVERIFY(m_mainWindow->isVisible());
262
263 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
264 QVERIFY(tabWidget);
265 QVERIFY(tabWidget->currentTabPage()->primaryViewContainer());
266 QVERIFY(!tabWidget->currentTabPage()->secondaryViewContainer());
267
268 // Open split view.
269 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
270 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
271 QVERIFY(tabWidget->currentTabPage()->secondaryViewContainer());
272
273 // Make sure the right view is the active one.
274 auto leftViewContainer = tabWidget->currentTabPage()->primaryViewContainer();
275 auto rightViewContainer = tabWidget->currentTabPage()->secondaryViewContainer();
276 QVERIFY(!leftViewContainer->isActive());
277 QVERIFY(rightViewContainer->isActive());
278
279 // Activate left view.
280 leftViewContainer->setActive(true);
281 QVERIFY(leftViewContainer->isActive());
282 QVERIFY(!rightViewContainer->isActive());
283
284 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
285 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
286 QVERIFY(!leftViewContainer->isActive());
287 QVERIFY(rightViewContainer->isActive());
288 QCOMPARE(rightViewContainer, tabWidget->currentTabPage()->activeViewContainer());
289
290 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
291 QSignalSpy currentUrlChangedSpy(tabWidget, &DolphinTabWidget::currentUrlChanged);
292 tabWidget->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
293 QCOMPARE(currentUrlChangedSpy.count(), 1);
294 }
295
296 // Test case for bug #402641
297 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
298 {
299 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
300 m_mainWindow->show();
301 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
302 QVERIFY(m_mainWindow->isVisible());
303
304 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
305 QVERIFY(tabWidget);
306
307 // Open split view.
308 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
309 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
310
311 auto leftViewContainer = tabWidget->currentTabPage()->primaryViewContainer();
312 auto rightViewContainer = tabWidget->currentTabPage()->secondaryViewContainer();
313
314 // Store old window title.
315 const auto oldTitle = m_mainWindow->windowTitle();
316
317 // Change URL in the right view and make sure the title gets updated.
318 rightViewContainer->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
319 QVERIFY(m_mainWindow->windowTitle() != oldTitle);
320
321 // Activate back the left view and check whether the old title gets restored.
322 leftViewContainer->setActive(true);
323 QCOMPARE(m_mainWindow->windowTitle(), oldTitle);
324 }
325
326 // Test case for bug #397910
327 void DolphinMainWindowTest::testOpenInNewTabTitle()
328 {
329 const QUrl homePathUrl{QUrl::fromLocalFile(QDir::homePath())};
330 m_mainWindow->openDirectories({homePathUrl}, false);
331 m_mainWindow->show();
332 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
333 QVERIFY(m_mainWindow->isVisible());
334
335 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
336 QVERIFY(tabWidget);
337
338 const QUrl tempPathUrl{QUrl::fromLocalFile(QDir::tempPath())};
339 tabWidget->openNewTab(tempPathUrl);
340 QCOMPARE(tabWidget->count(), 2);
341 QVERIFY(tabWidget->tabText(0) != tabWidget->tabText(1));
342
343 QVERIFY2(!tabWidget->tabIcon(0).isNull() && !tabWidget->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
344 QCOMPARE(KIO::iconNameForUrl(homePathUrl), tabWidget->tabIcon(0).name());
345 QCOMPARE(KIO::iconNameForUrl(tempPathUrl), tabWidget->tabIcon(1).name());
346 }
347
348 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
349 {
350 QTest::addColumn<QUrl>("activeViewUrl");
351 QTest::addColumn<bool>("expectedEnabled");
352
353 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
354 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
355 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
356 }
357
358 void DolphinMainWindowTest::testNewFileMenuEnabled()
359 {
360 QFETCH(QUrl, activeViewUrl);
361 m_mainWindow->openDirectories({activeViewUrl}, false);
362 m_mainWindow->show();
363 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
364 QVERIFY(m_mainWindow->isVisible());
365
366 auto newFileMenu = m_mainWindow->findChild<DolphinNewFileMenu *>("new_menu");
367 QVERIFY(newFileMenu);
368
369 QFETCH(bool, expectedEnabled);
370 QTRY_COMPARE(newFileMenu->isEnabled(), expectedEnabled);
371 }
372
373 void DolphinMainWindowTest::testWindowTitle_data()
374 {
375 QTest::addColumn<QUrl>("activeViewUrl");
376 QTest::addColumn<QString>("expectedWindowTitle");
377
378 // TODO: this test should enforce the english locale.
379 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
380 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
381 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
382 }
383
384 void DolphinMainWindowTest::testWindowTitle()
385 {
386 QFETCH(QUrl, activeViewUrl);
387 m_mainWindow->openDirectories({activeViewUrl}, false);
388 m_mainWindow->show();
389 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
390 QVERIFY(m_mainWindow->isVisible());
391
392 QFETCH(QString, expectedWindowTitle);
393 QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle);
394 }
395
396 void DolphinMainWindowTest::testFocusLocationBar()
397 {
398 const QUrl homePathUrl{QUrl::fromLocalFile(QDir::homePath())};
399 m_mainWindow->openDirectories({homePathUrl}, false);
400 m_mainWindow->show();
401 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
402 QVERIFY(m_mainWindow->isVisible());
403
404 QAction *replaceLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("replace_location"));
405 replaceLocationAction->trigger();
406 QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
407 replaceLocationAction->trigger();
408 QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus());
409
410 QAction *editableLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("editable_location"));
411 editableLocationAction->trigger();
412 QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
413 QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable());
414 editableLocationAction->trigger();
415 QVERIFY(!m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable());
416
417 replaceLocationAction->trigger();
418 QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget()));
419
420 // Pressing Escape multiple times should eventually move the focus back to the active view.
421 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape); // Focus might not go the view yet because it toggles the editable state of the location bar.
422 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape);
423 QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus());
424 }
425
426 void DolphinMainWindowTest::testFocusPlacesPanel()
427 {
428 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
429 m_mainWindow->show();
430 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
431 QVERIFY(m_mainWindow->isVisible());
432
433 QWidget *placesPanel = reinterpret_cast<QWidget *>(m_mainWindow->m_placesPanel);
434 QVERIFY2(QTest::qWaitFor(
435 [&]() {
436 return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0 && placesPanel->height() > 0;
437 },
438 5000),
439 "The test couldn't be initialised properly. The places panel should be visible.");
440
441 QAction *focusPlacesPanelAction = m_mainWindow->actionCollection()->action(QStringLiteral("focus_places_panel"));
442 QAction *showPlacesPanelAction = m_mainWindow->actionCollection()->action(QStringLiteral("show_places_panel"));
443
444 focusPlacesPanelAction->trigger();
445 QVERIFY(placesPanel->hasFocus());
446
447 focusPlacesPanelAction->trigger();
448 QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
449 "Triggering focus_places_panel while the panel already has focus should return the focus to the view.");
450
451 focusPlacesPanelAction->trigger();
452 QVERIFY(placesPanel->hasFocus());
453
454 showPlacesPanelAction->trigger();
455 QVERIFY(!placesPanel->isVisible());
456 QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
457 "Hiding the Places panel while it has focus should return the focus to the view.");
458
459 showPlacesPanelAction->trigger();
460 QVERIFY(placesPanel->isVisible());
461 QVERIFY2(placesPanel->hasFocus(), "Enabling the Places panel should move keyboard focus there.");
462
463 /// Test that activating a place always moves focus to the view.
464 QTest::keyClick(QApplication::focusWidget(), Qt::Key::Key_Enter);
465 QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
466 "Activating a place should move focus to the view that loads that place.");
467
468 focusPlacesPanelAction->trigger();
469 QVERIFY(placesPanel->hasFocus());
470
471 QTest::keyClick(QApplication::focusWidget(), Qt::Key::Key_Enter);
472 QVERIFY2(m_mainWindow->activeViewContainer()->isAncestorOf(QApplication::focusWidget()),
473 "Activating a place should move focus to the view even if the view already has that place loaded.");
474 }
475
476 /**
477 * The places panel will resize itself if any of the other widgets requires too much horizontal space
478 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
479 */
480 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
481 {
482 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
483 m_mainWindow->show();
484 m_mainWindow->resize(800, m_mainWindow->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
485 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
486 QVERIFY(m_mainWindow->isVisible());
487
488 QWidget *placesPanel = reinterpret_cast<QWidget *>(m_mainWindow->m_placesPanel);
489 QVERIFY2(QTest::qWaitFor(
490 [&]() {
491 return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0;
492 },
493 5000),
494 "The test couldn't be initialised properly. The places panel should be visible.");
495 QTest::qWait(100);
496 const int initialPlacesPanelWidth = placesPanel->width();
497
498 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
499 QTest::qWait(300); // wait for animation
500 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
501
502 m_mainWindow->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
503 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
504
505 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
506 // 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.
507 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll))->trigger();
508 for (int selectionModeStates = SelectionMode::BottomBar::CopyContents; selectionModeStates != SelectionMode::BottomBar::RenameContents;
509 selectionModeStates++) {
510 const auto contents = static_cast<SelectionMode::BottomBar::Contents>(selectionModeStates);
511 m_mainWindow->slotSetSelectionMode(true, contents);
512 QTest::qWait(20); // give time for a paint/resize
513 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
514 }
515
516 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Find))->trigger();
517 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
518
519 #if HAVE_BALOO
520 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
521 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
522 #endif
523
524 #if HAVE_TERMINAL
525 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
526 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
527 #endif
528
529 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
530 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
531
532 #if HAVE_BALOO
533 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
534 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
535 #endif
536
537 #if HAVE_TERMINAL
538 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
539 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
540 #endif
541
542 m_mainWindow->showMaximized();
543 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
544
545 QTest::qWait(300); // wait for split view closing animation
546 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
547 }
548
549 void DolphinMainWindowTest::testGoActions()
550 {
551 QScopedPointer<TestDir> testDir{new TestDir()};
552 testDir->createDir("a");
553 testDir->createDir("b");
554 testDir->createDir("b/b-1");
555 testDir->createFile("b/b-2");
556 testDir->createDir("c");
557 const QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b"));
558 m_mainWindow->openDirectories({childDirUrl}, false); // Open "b" dir
559 m_mainWindow->show();
560 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
561 QVERIFY(m_mainWindow->isVisible());
562 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
563
564 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Up))->trigger();
565 /**
566 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
567 * This is especially important when a user wants to peek into multiple folders in quick succession.
568 */
569 QSignalSpy spyDirectoryLoadingCompleted(m_mainWindow->m_activeViewContainer->view(), &DolphinView::directoryLoadingCompleted);
570 QVERIFY(spyDirectoryLoadingCompleted.wait());
571 QVERIFY(QTest::qWaitFor([&]() {
572 return !m_mainWindow->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
573 })); // "Stop" command should be disabled because it finished loading
574 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
575 const QUrl parentDirUrl = m_mainWindow->activeViewContainer()->url();
576 QVERIFY(parentDirUrl != childDirUrl);
577
578 auto currentItemUrl = [this]() {
579 const int currentIndex = m_mainWindow->m_activeViewContainer->view()->m_container->controller()->selectionManager()->currentItem();
580 const KFileItem currentItem = m_mainWindow->m_activeViewContainer->view()->m_model->fileItem(currentIndex);
581 return currentItem.url();
582 };
583
584 QCOMPARE(currentItemUrl(), childDirUrl); // The item we just emerged from should now have keyboard focus.
585 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // The item we just emerged from should not be selected. BUG: 424723
586 // Pressing arrow keys should not only move the keyboard focus but also select the item.
587 // We press "Down" to select "c" below and then "Up" so the folder "b" we just emerged from is selected for the first time.
588 m_mainWindow->actionCollection()->action(QStringLiteral("compact"))->trigger();
589 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier);
590 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
591 QVERIFY2(currentItemUrl() != childDirUrl, "The current item didn't change after pressing the 'Down' key.");
592 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Up, Qt::NoModifier);
593 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
594 QCOMPARE(currentItemUrl(), childDirUrl); // After pressing 'Down' and then 'Up' we should be back where we were.
595
596 // Enter the child folder "b".
597 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::NoModifier);
598 QVERIFY(spyDirectoryLoadingCompleted.wait());
599 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
600 QVERIFY(m_mainWindow->isUrlOpen(childDirUrl.toString()));
601
602 // Go back to the parent folder.
603 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
604 QVERIFY(spyDirectoryLoadingCompleted.wait());
605 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
606 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
607 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
608 // Going 'Back' means that the view should be in the same state it was in when we left.
609 QCOMPARE(currentItemUrl(), childDirUrl); // The item we last interacted with in this location should still have keyboard focus.
610 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
611 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), childDirUrl); // It should still be selected.
612
613 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
614 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::ControlModifier); // Open new inactive tab
615 QVERIFY(m_mainWindow->m_tabWidget->count() == 2);
616 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
617 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
618 QVERIFY(!m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
619
620 // Go forward to the child folder.
621 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger();
622 QVERIFY(spyDirectoryLoadingCompleted.wait());
623 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
624 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
625 QCOMPARE(currentItemUrl(), QUrl(QDir::cleanPath(testDir->url().toString() + "/b/b-1"))); // The first item in the view should have keyboard focus.
626
627 // Press the 'Down' key in the child folder.
628 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier);
629 // The second item in the view should have keyboard focus and be selected.
630 const QUrl secondItemInChildFolderUrl{QDir::cleanPath(testDir->url().toString() + "/b/b-2")};
631 QCOMPARE(currentItemUrl(), secondItemInChildFolderUrl);
632 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
633 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl);
634
635 // Go back to the parent folder and then re-enter the child folder.
636 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
637 QVERIFY(spyDirectoryLoadingCompleted.wait());
638 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger();
639 QVERIFY(spyDirectoryLoadingCompleted.wait());
640 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
641 // The state of the view should be identical to how it was before we triggered "Back" and then "Forward".
642 QTRY_COMPARE(currentItemUrl(), secondItemInChildFolderUrl);
643 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
644 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().constFirst().url(), secondItemInChildFolderUrl);
645
646 // Go back to the parent folder.
647 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
648 QVERIFY(spyDirectoryLoadingCompleted.wait());
649 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
650 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
651 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
652
653 // 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
654 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Close))->trigger(); // Close current tab
655 QVERIFY(m_mainWindow->m_tabWidget->count() == 1);
656 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
657 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
658 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->isEnabled());
659 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
660 QVERIFY(m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
661 }
662
663 void DolphinMainWindowTest::testOpenFiles()
664 {
665 QScopedPointer<TestDir> testDir{new TestDir()};
666 QString testDirUrl(QDir::cleanPath(testDir->url().toString()));
667 testDir->createDir("a");
668 testDir->createDir("a/b");
669 testDir->createDir("a/b/c");
670 testDir->createDir("a/b/c/d");
671 m_mainWindow->openDirectories({testDirUrl}, false);
672 m_mainWindow->show();
673
674 // We only see the unselected "a" folder in the test dir. There are no other tabs.
675 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
676 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
677 QVERIFY(!m_mainWindow->isUrlOpen(testDirUrl + "/a"));
678 QVERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
679 QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
680 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
681 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
682
683 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
684 m_mainWindow->openFiles({testDirUrl + "/a"}, false);
685 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
686 QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
687 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
688
689 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
690 m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
691 QTRY_VERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
692 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
693 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
694 QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
695 QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
696 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
697
698 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
699 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
700 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
701 m_mainWindow->openFiles({testDirUrl + "/a"}, false);
702 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
703 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
704 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
705 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
706
707 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
708 m_mainWindow->openDirectories({testDirUrl + "/a"}, false);
709 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
710 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
711
712 // 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.
713 m_mainWindow->actionCollection()->action(QStringLiteral("details"))->trigger();
714 QTRY_VERIFY(m_mainWindow->activeViewContainer()->view()->itemsExpandable());
715
716 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
717 QVERIFY2(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
718 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Right);
719 QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
720 QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
721 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
722
723 // Switch to first tab by opening it even though it is already open.
724 m_mainWindow->openDirectories({testDirUrl}, false);
725 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
726 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
727
728 // "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.
729 m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
730 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
731 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
732 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
733 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
734 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
735
736 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
737 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
738 m_mainWindow->openDirectories({testDirUrl + "/a/b/c/d", testDirUrl + "/a/b/c"}, true);
739 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
740 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
741 QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->splitViewEnabled());
742 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c")); // It should still be visible in the second tab.
743 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
744 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c/d"));
745 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c"));
746
747 // "c" is in view in the second tab because "b" is expanded there,
748 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
749 m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
750 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
751 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
752 QVERIFY2(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
753
754 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
755 m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
756 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Left);
757 QTRY_VERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
758
759 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
760 m_mainWindow->openFiles({testDirUrl + "/a/b/c/d"}, false);
761 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
762 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
763 QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->secondaryViewContainer()->isActive());
764 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
765 }
766
767 void DolphinMainWindowTest::testAccessibilityTree()
768 {
769 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
770 m_mainWindow->show();
771 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
772 QVERIFY(m_mainWindow->isVisible());
773
774 QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get());
775 Q_CHECK_PTR(accessibleInterfaceOfMainWindow);
776
777 /// Test the accessibility of objects while traversing forwards (Tab key) and backwards (Shift+Tab).
778 int testedObjectsSizeAfterTraversingForwards = 0;
779 for (int i = 0; i < 2; i++) {
780 std::tuple<Qt::Key, Qt::KeyboardModifier> focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::NoModifier};
781 if (i) {
782 focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::ShiftModifier};
783 }
784
785 /// @see firstNamedAncestor below.
786 QAccessibleInterface *firstNamedAncestorOfPreviousIteration = nullptr;
787
788 /// Perform accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
789 std::set<const QObject *> testedObjects; // Makes sure we stop testing when we arrive at an item that was already tested.
790 while (qApp->focusObject() && !testedObjects.count(qApp->focusObject())) {
791 const auto currentlyFocusedObject = qApp->focusObject();
792 const QAccessibleInterface *accessibleIntefaceOfCurrentlyFocusedObject = QAccessible::queryAccessibleInterface(currentlyFocusedObject);
793 QVERIFY(accessibleIntefaceOfCurrentlyFocusedObject);
794
795 /// Test that each object reachable by Tab or Shift+Tab has at least some accessible information. Objects without any accessible information
796 /// are even less useful to accessibility software users than unlabeled buttons are e.g. to sighted users, because unlabeled buttons at least
797 /// convey some information through their placement and icon.
798 if (currentlyFocusedObject != m_mainWindow->m_activeViewContainer->view()->m_container) { // Skip the custom container widget which has no
799 // accessible name on purpose.
800 /**
801 * The first ancestor with an accessible name is interesting because it is sometimes used to identify an object if the object itself has no
802 * 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.
803 */
804 QAccessibleInterface *firstNamedAncestor = accessibleIntefaceOfCurrentlyFocusedObject->parent();
805 while (firstNamedAncestor) {
806 if (!firstNamedAncestor->text(QAccessible::Name).isEmpty()) {
807 break;
808 }
809 firstNamedAncestor = firstNamedAncestor->parent();
810 }
811 QTRY_VERIFY2(!accessibleIntefaceOfCurrentlyFocusedObject->text(QAccessible::Name).isEmpty()
812 || (firstNamedAncestor && firstNamedAncestor != firstNamedAncestorOfPreviousIteration),
813 qPrintable(QStringLiteral("%1's accessibleInterface does not have an accessible name and can not be distinguished from the object"
814 " that had focus previously. Please fix this. You can find this %1 within its parent %2.")
815 .arg(currentlyFocusedObject->metaObject()->className())
816 .arg(currentlyFocusedObject->parent()->metaObject()->className())));
817 firstNamedAncestorOfPreviousIteration = firstNamedAncestor;
818 }
819
820 /// Test that each accessible interface has the main window as its parent.
821 QAccessibleInterface *accessibleInterface = QAccessible::queryAccessibleInterface(currentlyFocusedObject);
822 // The accessibleInterfaces of focused objects might themselves have children.
823 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
824 while (accessibleInterface->childCount() > 0) {
825 accessibleInterface = accessibleInterface->child(0);
826 }
827 while (accessibleInterface != accessibleInterfaceOfMainWindow) {
828 QVERIFY2(accessibleInterface,
829 qPrintable(QStringLiteral("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
830 .arg(currentlyFocusedObject->metaObject()->className())));
831 accessibleInterface = accessibleInterface->parent();
832 }
833
834 testedObjects.insert(currentlyFocusedObject); // Add it to testedObjects so we won't test it again later.
835 QTest::keyClick(m_mainWindow.get(), std::get<0>(focusChainTraversalKeyCombination), std::get<1>(focusChainTraversalKeyCombination));
836 QVERIFY2(currentlyFocusedObject != qApp->focusObject(),
837 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
838 }
839
840 if (i == 0) {
841 testedObjectsSizeAfterTraversingForwards = testedObjects.size();
842 } else {
843 QCOMPARE(testedObjects.size(), testedObjectsSizeAfterTraversingForwards); // The size after traversing backwards is different than
844 // after going forwards which is probably not intended.
845 }
846 }
847 QCOMPARE_GE(testedObjectsSizeAfterTraversingForwards, 12); // The test did not reach many objects while using the Tab key to move through Dolphin. Did the
848 // test run correctly?
849 }
850
851 void DolphinMainWindowTest::testAutoSaveSession()
852 {
853 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
854 m_mainWindow->show();
855 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
856 QVERIFY(m_mainWindow->isVisible());
857
858 // Create config file
859 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
860 KConfig *config = KConfigGui::sessionConfig();
861 m_mainWindow->saveGlobalProperties(config);
862 m_mainWindow->savePropertiesInternal(config, 1);
863 config->sync();
864
865 // Setup watcher for config file changes
866 const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/" + KConfigGui::sessionConfig()->name();
867 QFileSystemWatcher *configWatcher = new QFileSystemWatcher({configFileName}, this);
868 QSignalSpy spySessionSaved(configWatcher, &QFileSystemWatcher::fileChanged);
869
870 // Enable session autosave.
871 m_mainWindow->setSessionAutoSaveEnabled(true);
872 m_mainWindow->m_sessionSaveTimer->setInterval(200); // Lower the interval to speed up the testing
873
874 // Open a new tab
875 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
876 QVERIFY(tabWidget);
877 tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
878 QCOMPARE(tabWidget->count(), 2);
879
880 // Wait till a session save occurs
881 QVERIFY(spySessionSaved.wait(60000));
882
883 // Disable session autosave.
884 m_mainWindow->setSessionAutoSaveEnabled(false);
885 }
886
887 void DolphinMainWindowTest::testInlineRename()
888 {
889 QScopedPointer<TestDir> testDir{new TestDir()};
890 testDir->createFiles({"aaaa", "bbbb", "cccc", "dddd"});
891 m_mainWindow->openDirectories({testDir->url()}, false);
892 m_mainWindow->show();
893 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
894 QVERIFY(m_mainWindow->isVisible());
895
896 DolphinView *view = m_mainWindow->activeViewContainer()->view();
897 QSignalSpy viewDirectoryLoadingCompletedSpy(view, &DolphinView::directoryLoadingCompleted);
898 QSignalSpy itemsReorderedSpy(view->m_model, &KFileItemModel::itemsMoved);
899 QSignalSpy modelDirectoryLoadingCompletedSpy(view->m_model, &KFileItemModel::directoryLoadingCompleted);
900
901 QVERIFY(viewDirectoryLoadingCompletedSpy.wait());
902 QTest::qWait(500); // we need to wait for the file widgets to become visible
903 view->markUrlsAsSelected({QUrl(testDir->url().toString() + "/aaaa")});
904 view->updateViewState();
905 view->renameSelectedItems();
906 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
907 QTest::keyClick(QApplication::focusWidget(), Qt::Key_E);
908 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
909
910 QVERIFY(itemsReorderedSpy.wait());
911 QVERIFY(view->m_view->m_editingRole);
912 KItemListWidget *widget = view->m_view->m_visibleItems.value(view->m_view->firstVisibleIndex());
913 QVERIFY(!widget->editedRole().isEmpty());
914
915 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
916 QTest::keyClick(QApplication::focusWidget(), Qt::Key_A);
917 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
918 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
919 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Left);
920 QTest::keyClick(QApplication::focusWidget(), Qt::Key_A);
921 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Down);
922
923 QVERIFY(itemsReorderedSpy.wait());
924 QVERIFY(view->m_view->m_editingRole);
925 widget = view->m_view->m_visibleItems.value(view->m_view->lastVisibleIndex());
926 QVERIFY(!widget->editedRole().isEmpty());
927
928 QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape);
929 QVERIFY(widget->isCurrent());
930 view->m_model->refreshDirectory(testDir->url());
931 QVERIFY(modelDirectoryLoadingCompletedSpy.wait());
932
933 QCOMPARE(view->m_model->fileItem(0).name(), "abbbb");
934 QCOMPARE(view->m_model->fileItem(1).name(), "adddd");
935 QCOMPARE(view->m_model->fileItem(2).name(), "cccc");
936 QCOMPARE(view->m_model->fileItem(3).name(), "eaaaa");
937 QCOMPARE(view->m_model->count(), 4);
938 }
939
940 void DolphinMainWindowTest::cleanupTestCase()
941 {
942 m_mainWindow->showNormal();
943 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
944
945 #if HAVE_BALOO
946 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
947 #endif
948
949 #if HAVE_TERMINAL
950 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
951 #endif
952
953 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
954 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Quit))->trigger();
955 }
956
957 QTEST_MAIN(DolphinMainWindowTest)
958
959 #include "dolphinmainwindowtest.moc"