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