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