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