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