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