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