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