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