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