]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinmainwindowtest.cpp
Adapt testOpenInNewTabTitle() to upstream change
[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 const QUrl homePathUrl{QUrl::fromLocalFile(QDir::homePath())};
216 m_mainWindow->openDirectories({homePathUrl}, false);
217 m_mainWindow->show();
218 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
219 QVERIFY(m_mainWindow->isVisible());
220
221 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
222 QVERIFY(tabWidget);
223
224 const QUrl tempPathUrl{QUrl::fromLocalFile(QDir::tempPath())};
225 tabWidget->openNewTab(tempPathUrl);
226 QCOMPARE(tabWidget->count(), 2);
227 QVERIFY(tabWidget->tabText(0) != tabWidget->tabText(1));
228
229 QVERIFY2(!tabWidget->tabIcon(0).isNull() && !tabWidget->tabIcon(1).isNull(), "Tabs are supposed to have icons.");
230 QCOMPARE(KIO::iconNameForUrl(homePathUrl), tabWidget->tabIcon(0).name());
231 QCOMPARE(KIO::iconNameForUrl(tempPathUrl), tabWidget->tabIcon(1).name());
232 }
233
234 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
235 {
236 QTest::addColumn<QUrl>("activeViewUrl");
237 QTest::addColumn<bool>("expectedEnabled");
238
239 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
240 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
241 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
242 }
243
244 void DolphinMainWindowTest::testNewFileMenuEnabled()
245 {
246 QFETCH(QUrl, activeViewUrl);
247 m_mainWindow->openDirectories({activeViewUrl}, false);
248 m_mainWindow->show();
249 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
250 QVERIFY(m_mainWindow->isVisible());
251
252 auto newFileMenu = m_mainWindow->findChild<DolphinNewFileMenu *>("new_menu");
253 QVERIFY(newFileMenu);
254
255 QFETCH(bool, expectedEnabled);
256 QTRY_COMPARE(newFileMenu->isEnabled(), expectedEnabled);
257 }
258
259 void DolphinMainWindowTest::testWindowTitle_data()
260 {
261 QTest::addColumn<QUrl>("activeViewUrl");
262 QTest::addColumn<QString>("expectedWindowTitle");
263
264 // TODO: this test should enforce the english locale.
265 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
266 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
267 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
268 }
269
270 void DolphinMainWindowTest::testWindowTitle()
271 {
272 QFETCH(QUrl, activeViewUrl);
273 m_mainWindow->openDirectories({activeViewUrl}, false);
274 m_mainWindow->show();
275 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
276 QVERIFY(m_mainWindow->isVisible());
277
278 QFETCH(QString, expectedWindowTitle);
279 QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle);
280 }
281
282 /**
283 * The places panel will resize itself if any of the other widgets requires too much horizontal space
284 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
285 */
286 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
287 {
288 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
289 m_mainWindow->show();
290 m_mainWindow->resize(800, m_mainWindow->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
291 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
292 QVERIFY(m_mainWindow->isVisible());
293
294 QWidget *placesPanel = reinterpret_cast<QWidget *>(m_mainWindow->m_placesPanel);
295 QVERIFY2(QTest::qWaitFor(
296 [&]() {
297 return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0;
298 },
299 5000),
300 "The test couldn't be initialised properly. The places panel should be visible.");
301 QTest::qWait(100);
302 const int initialPlacesPanelWidth = placesPanel->width();
303
304 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
305 QTest::qWait(300); // wait for animation
306 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
307
308 m_mainWindow->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
309 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
310
311 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
312 // 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.
313 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll))->trigger();
314 for (int selectionModeStates = SelectionMode::BottomBar::CopyContents; selectionModeStates != SelectionMode::BottomBar::RenameContents;
315 selectionModeStates++) {
316 const auto contents = static_cast<SelectionMode::BottomBar::Contents>(selectionModeStates);
317 m_mainWindow->slotSetSelectionMode(true, contents);
318 QTest::qWait(20); // give time for a paint/resize
319 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
320 }
321
322 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Find))->trigger();
323 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
324
325 #if HAVE_BALOO
326 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
327 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
328 #endif
329
330 #if HAVE_TERMINAL
331 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
332 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
333 #endif
334
335 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
336 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
337
338 #if HAVE_BALOO
339 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
340 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
341 #endif
342
343 #if HAVE_TERMINAL
344 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
345 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
346 #endif
347
348 m_mainWindow->showMaximized();
349 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
350
351 QTest::qWait(300); // wait for split view closing animation
352 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
353 }
354
355 void DolphinMainWindowTest::testGoActions()
356 {
357 QScopedPointer<TestDir> testDir{new TestDir()};
358 testDir->createDir("a");
359 testDir->createDir("b");
360 testDir->createDir("b/b-1");
361 testDir->createFile("b/b-2");
362 testDir->createDir("c");
363 QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b"));
364 m_mainWindow->openDirectories({childDirUrl}, false); // Open "b" dir
365 m_mainWindow->show();
366 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
367 QVERIFY(m_mainWindow->isVisible());
368 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
369
370 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Up))->trigger();
371 /**
372 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
373 * This is especially important when a user wants to peek into multiple folders in quick succession.
374 */
375 QSignalSpy spyDirectoryLoadingCompleted(m_mainWindow->m_activeViewContainer->view(), &DolphinView::directoryLoadingCompleted);
376 QVERIFY(spyDirectoryLoadingCompleted.wait());
377 QVERIFY(QTest::qWaitFor([&]() {
378 return !m_mainWindow->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
379 })); // "Stop" command should be disabled because it finished loading
380 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
381 const QUrl parentDirUrl = m_mainWindow->activeViewContainer()->url();
382 QVERIFY(parentDirUrl != childDirUrl);
383
384 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
385 // 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.
386 m_mainWindow->actionCollection()->action(QStringLiteral("compact"))->trigger();
387 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier);
388 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
389 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Up, Qt::NoModifier);
390 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
391 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::NoModifier);
392 QVERIFY(spyDirectoryLoadingCompleted.wait());
393 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
394 QVERIFY(m_mainWindow->isUrlOpen(childDirUrl.toString()));
395
396 // Go back to the parent folder
397 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
398 QVERIFY(spyDirectoryLoadingCompleted.wait());
399 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
400 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
401 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
402
403 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
404 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::ControlModifier); // Open new inactive tab
405 QVERIFY(m_mainWindow->m_tabWidget->count() == 2);
406 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
407 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
408 QVERIFY(!m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
409
410 // Go forward to the child folder.
411 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger();
412 QVERIFY(spyDirectoryLoadingCompleted.wait());
413 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
414 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
415
416 // Go back to the parent folder.
417 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
418 QVERIFY(spyDirectoryLoadingCompleted.wait());
419 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
420 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
421 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
422
423 // 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
424 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Close))->trigger(); // Close current tab
425 QVERIFY(m_mainWindow->m_tabWidget->count() == 1);
426 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
427 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
428 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->isEnabled());
429 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
430 QVERIFY(m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
431 }
432
433 void DolphinMainWindowTest::testOpenFiles()
434 {
435 QScopedPointer<TestDir> testDir{new TestDir()};
436 QString testDirUrl(QDir::cleanPath(testDir->url().toString()));
437 testDir->createDir("a");
438 testDir->createDir("a/b");
439 testDir->createDir("a/b/c");
440 testDir->createDir("a/b/c/d");
441 m_mainWindow->openDirectories({testDirUrl}, false);
442 m_mainWindow->show();
443
444 // We only see the unselected "a" folder in the test dir. There are no other tabs.
445 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
446 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
447 QVERIFY(!m_mainWindow->isUrlOpen(testDirUrl + "/a"));
448 QVERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
449 QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
450 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
451 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
452
453 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
454 m_mainWindow->openFiles({testDirUrl + "/a"}, false);
455 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
456 QCOMPARE(m_mainWindow->m_tabWidget->count(), 1);
457 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
458
459 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
460 m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
461 QTRY_VERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
462 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
463 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
464 QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b"));
465 QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
466 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
467
468 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
469 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a"));
470 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
471 m_mainWindow->openFiles({testDirUrl + "/a"}, false);
472 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
473 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
474 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
475 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
476
477 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
478 m_mainWindow->openDirectories({testDirUrl + "/a"}, false);
479 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
480 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
481
482 // 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.
483 m_mainWindow->actionCollection()->action(QStringLiteral("details"))->trigger();
484 QTRY_VERIFY(m_mainWindow->activeViewContainer()->view()->itemsExpandable());
485
486 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
487 QVERIFY2(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
488 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Right);
489 QTRY_VERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
490 QVERIFY2(!m_mainWindow->isUrlOpen(testDirUrl + "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
491 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
492
493 // Switch to first tab by opening it even though it is already open.
494 m_mainWindow->openDirectories({testDirUrl}, false);
495 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
496 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 0);
497
498 // "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.
499 m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
500 QCOMPARE(m_mainWindow->m_tabWidget->count(), 2);
501 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
502 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
503 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl));
504 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a"));
505
506 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
507 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
508 m_mainWindow->openDirectories({testDirUrl + "/a/b/c/d", testDirUrl + "/a/b/c"}, true);
509 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
510 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
511 QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->splitViewEnabled());
512 QVERIFY(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c")); // It should still be visible in the second tab.
513 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0);
514 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c/d"));
515 QVERIFY(m_mainWindow->isUrlOpen(testDirUrl + "/a/b/c"));
516
517 // "c" is in view in the second tab because "b" is expanded there,
518 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
519 m_mainWindow->openFiles({testDirUrl + "/a/b/c"}, false);
520 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
521 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 1);
522 QVERIFY2(m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
523
524 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
525 m_mainWindow->openFiles({testDirUrl + "/a/b"}, false);
526 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Left);
527 QTRY_VERIFY(!m_mainWindow->isItemVisibleInAnyView(testDirUrl + "/a/b/c"));
528
529 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
530 m_mainWindow->openFiles({testDirUrl + "/a/b/c/d"}, false);
531 QCOMPARE(m_mainWindow->m_tabWidget->count(), 3);
532 QCOMPARE(m_mainWindow->m_tabWidget->currentIndex(), 2);
533 QVERIFY(m_mainWindow->m_tabWidget->currentTabPage()->secondaryViewContainer()->isActive());
534 QTRY_COMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
535 }
536
537 void DolphinMainWindowTest::testAccessibilityAncestorTree()
538 {
539 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
540 m_mainWindow->show();
541 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
542 QVERIFY(m_mainWindow->isVisible());
543
544 QAccessibleInterface *accessibleInterfaceOfMainWindow = QAccessible::queryAccessibleInterface(m_mainWindow.get());
545 Q_CHECK_PTR(accessibleInterfaceOfMainWindow);
546
547 // We will test the accessibility of objects traversing forwards and backwards.
548 int testedObjectsSizeAfterTraversingForwards = 0;
549 for (int i = 0; i < 2; i++) {
550 std::tuple<Qt::Key, Qt::KeyboardModifier> focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::NoModifier};
551 if (i) {
552 focusChainTraversalKeyCombination = {Qt::Key::Key_Tab, Qt::ShiftModifier};
553 }
554
555 // We will do accessibility checks for every object that gets focus. Focus will be changed using the focusChainTraversalKeyCombination.
556 std::set<const QObject *> testedObjects; // Makes sure we stop testing when we arrive at an item that was already tested.
557 while (qApp->focusObject() && !testedObjects.count(qApp->focusObject())) {
558 const auto currentlyFocusedObject = qApp->focusObject();
559
560 QAccessibleInterface *accessibleInterface = QAccessible::queryAccessibleInterface(currentlyFocusedObject);
561 // The accessibleInterfaces of focused objects might themselves have children.
562 // We go down that hierarchy as far as possible and then test the ancestor tree from there.
563 while (accessibleInterface->childCount() > 0) {
564 accessibleInterface = accessibleInterface->child(0);
565 }
566 while (accessibleInterface != accessibleInterfaceOfMainWindow) {
567 QVERIFY2(accessibleInterface,
568 qPrintable(QString("%1's accessibleInterface or one of its accessible children doesn't have the main window as an ancestor.")
569 .arg(currentlyFocusedObject->metaObject()->className())));
570 accessibleInterface = accessibleInterface->parent();
571 }
572
573 testedObjects.insert(currentlyFocusedObject); // Add it to testedObjects so we won't test it again later.
574 QTest::keyClick(m_mainWindow.get(), std::get<0>(focusChainTraversalKeyCombination), std::get<1>(focusChainTraversalKeyCombination));
575 QVERIFY2(currentlyFocusedObject != qApp->focusObject(),
576 "The focus chain is broken. The focused object should have changed after pressing the focusChainTraversalKeyCombination.");
577 }
578
579 if (i == 0) {
580 testedObjectsSizeAfterTraversingForwards = testedObjects.size();
581 } else {
582 QCOMPARE(testedObjects.size(), testedObjectsSizeAfterTraversingForwards); // The size after traversing backwards is different than
583 // after going forwards which is probably not intended.
584 }
585 }
586 }
587
588 void DolphinMainWindowTest::testAutoSaveSession()
589 {
590 m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
591 m_mainWindow->show();
592 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
593 QVERIFY(m_mainWindow->isVisible());
594
595 // Create config file
596 KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
597 KConfig *config = KConfigGui::sessionConfig();
598 m_mainWindow->saveGlobalProperties(config);
599 m_mainWindow->savePropertiesInternal(config, 1);
600 config->sync();
601
602 // Setup watcher for config file changes
603 const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/" + KConfigGui::sessionConfig()->name();
604 QFileSystemWatcher *configWatcher = new QFileSystemWatcher({configFileName}, this);
605 QSignalSpy spySessionSaved(configWatcher, &QFileSystemWatcher::fileChanged);
606
607 // Enable session autosave.
608 m_mainWindow->setSessionAutoSaveEnabled(true);
609 m_mainWindow->m_sessionSaveTimer->setInterval(200); // Lower the interval to speed up the testing
610
611 // Open a new tab
612 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget *>("tabWidget");
613 QVERIFY(tabWidget);
614 tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::tempPath()));
615 QCOMPARE(tabWidget->count(), 2);
616
617 // Wait till a session save occurs
618 QVERIFY(spySessionSaved.wait(60000));
619
620 // Disable session autosave.
621 m_mainWindow->setSessionAutoSaveEnabled(false);
622 }
623
624 void DolphinMainWindowTest::cleanupTestCase()
625 {
626 m_mainWindow->showNormal();
627 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
628
629 #if HAVE_BALOO
630 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
631 #endif
632
633 #if HAVE_TERMINAL
634 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
635 #endif
636
637 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
638 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Quit))->trigger();
639 }
640
641 QTEST_MAIN(DolphinMainWindowTest)
642
643 #include "dolphinmainwindowtest.moc"