2 * SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
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"
15 #include <KActionCollection>
17 #include <QScopedPointer>
19 #include <QStandardPaths>
22 class DolphinMainWindowTest
: public QObject
29 void testClosingTabsWithSearchBoxVisible();
30 void testActiveViewAfterClosingSplitView_data();
31 void testActiveViewAfterClosingSplitView();
32 void testUpdateWindowTitleAfterClosingSplitView();
33 void testUpdateWindowTitleAfterChangingSplitView();
34 void testOpenInNewTabTitle();
35 void testNewFileMenuEnabled_data();
36 void testNewFileMenuEnabled();
37 void testWindowTitle_data();
38 void testWindowTitle();
39 void testPlacesPanelWidthResistance();
42 void cleanupTestCase();
45 QScopedPointer
<DolphinMainWindow
> m_mainWindow
;
48 void DolphinMainWindowTest::initTestCase()
50 QStandardPaths::setTestModeEnabled(true);
53 void DolphinMainWindowTest::init()
55 m_mainWindow
.reset(new DolphinMainWindow());
58 // See https://bugs.kde.org/show_bug.cgi?id=379135
59 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
61 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
63 // Without this call the searchbox doesn't get FocusIn events.
64 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
65 QVERIFY(m_mainWindow
->isVisible());
67 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
70 // Show search box on first tab.
71 tabWidget
->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
73 tabWidget
->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
74 QCOMPARE(tabWidget
->count(), 2);
76 // Triggers the crash in bug #379135.
77 tabWidget
->closeTab();
78 QCOMPARE(tabWidget
->count(), 1);
81 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
83 QTest::addColumn
<bool>("closeLeftView");
85 QTest::newRow("close left view") << true;
86 QTest::newRow("close right view") << false;
89 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
91 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
93 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
94 QVERIFY(m_mainWindow
->isVisible());
96 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
98 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
99 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
102 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
103 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
104 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
106 // Make sure the right view is the active one.
107 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
108 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
109 QVERIFY(!leftViewContainer
->isActive());
110 QVERIFY(rightViewContainer
->isActive());
112 QFETCH(bool, closeLeftView
);
114 // Activate left view.
115 leftViewContainer
->setActive(true);
116 QVERIFY(leftViewContainer
->isActive());
117 QVERIFY(!rightViewContainer
->isActive());
119 // Close left view. The secondary view (which was on the right) will become the primary one and must be active.
120 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
121 QVERIFY(!leftViewContainer
->isActive());
122 QVERIFY(rightViewContainer
->isActive());
123 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
125 // Close right view. The left view will become active.
126 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
127 QVERIFY(leftViewContainer
->isActive());
128 QVERIFY(!rightViewContainer
->isActive());
129 QCOMPARE(leftViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
133 // Test case for bug #385111
134 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
136 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
137 m_mainWindow
->show();
138 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
139 QVERIFY(m_mainWindow
->isVisible());
141 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
143 QVERIFY(tabWidget
->currentTabPage()->primaryViewContainer());
144 QVERIFY(!tabWidget
->currentTabPage()->secondaryViewContainer());
147 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
148 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
149 QVERIFY(tabWidget
->currentTabPage()->secondaryViewContainer());
151 // Make sure the right view is the active one.
152 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
153 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
154 QVERIFY(!leftViewContainer
->isActive());
155 QVERIFY(rightViewContainer
->isActive());
157 // Activate left view.
158 leftViewContainer
->setActive(true);
159 QVERIFY(leftViewContainer
->isActive());
160 QVERIFY(!rightViewContainer
->isActive());
162 // Close split view. The secondary view (which was on the right) will become the primary one and must be active.
163 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
164 QVERIFY(!leftViewContainer
->isActive());
165 QVERIFY(rightViewContainer
->isActive());
166 QCOMPARE(rightViewContainer
, tabWidget
->currentTabPage()->activeViewContainer());
168 // Change URL and make sure we emit the currentUrlChanged signal (which triggers the window title update).
169 QSignalSpy
currentUrlChangedSpy(tabWidget
, &DolphinTabWidget::currentUrlChanged
);
170 tabWidget
->currentTabPage()->activeViewContainer()->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
171 QCOMPARE(currentUrlChangedSpy
.count(), 1);
174 // Test case for bug #402641
175 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
177 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
178 m_mainWindow
->show();
179 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
180 QVERIFY(m_mainWindow
->isVisible());
182 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
186 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger();
187 QVERIFY(tabWidget
->currentTabPage()->splitViewEnabled());
189 auto leftViewContainer
= tabWidget
->currentTabPage()->primaryViewContainer();
190 auto rightViewContainer
= tabWidget
->currentTabPage()->secondaryViewContainer();
192 // Store old window title.
193 const auto oldTitle
= m_mainWindow
->windowTitle();
195 // Change URL in the right view and make sure the title gets updated.
196 rightViewContainer
->setUrl(QUrl::fromLocalFile(QDir::rootPath()));
197 QVERIFY(m_mainWindow
->windowTitle() != oldTitle
);
199 // Activate back the left view and check whether the old title gets restored.
200 leftViewContainer
->setActive(true);
201 QCOMPARE(m_mainWindow
->windowTitle(), oldTitle
);
204 // Test case for bug #397910
205 void DolphinMainWindowTest::testOpenInNewTabTitle()
207 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
208 m_mainWindow
->show();
209 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
210 QVERIFY(m_mainWindow
->isVisible());
212 auto tabWidget
= m_mainWindow
->findChild
<DolphinTabWidget
*>("tabWidget");
215 tabWidget
->openNewTab(QUrl::fromLocalFile(QDir::tempPath()));
216 QCOMPARE(tabWidget
->count(), 2);
217 QVERIFY(tabWidget
->tabText(0) != tabWidget
->tabText(1));
218 if (!tabWidget
->tabIcon(0).isNull() && !tabWidget
->tabIcon(1).isNull()) {
219 QCOMPARE(QStringLiteral("inode-directory"), tabWidget
->tabIcon(0).name());
220 QCOMPARE(QStringLiteral("inode-directory"), tabWidget
->tabIcon(1).name());
224 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
226 QTest::addColumn
<QUrl
>("activeViewUrl");
227 QTest::addColumn
<bool>("expectedEnabled");
229 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << true;
230 QTest::newRow("root") << QUrl::fromLocalFile(QDir::rootPath()) << false;
231 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << false;
234 void DolphinMainWindowTest::testNewFileMenuEnabled()
236 QFETCH(QUrl
, activeViewUrl
);
237 m_mainWindow
->openDirectories({activeViewUrl
}, false);
238 m_mainWindow
->show();
239 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
240 QVERIFY(m_mainWindow
->isVisible());
242 auto newFileMenu
= m_mainWindow
->findChild
<DolphinNewFileMenu
*>("new_menu");
243 QVERIFY(newFileMenu
);
245 QFETCH(bool, expectedEnabled
);
246 QTRY_COMPARE(newFileMenu
->isEnabled(), expectedEnabled
);
249 void DolphinMainWindowTest::testWindowTitle_data()
251 QTest::addColumn
<QUrl
>("activeViewUrl");
252 QTest::addColumn
<QString
>("expectedWindowTitle");
254 // TODO: this test should enforce the english locale.
255 QTest::newRow("home") << QUrl::fromLocalFile(QDir::homePath()) << QStringLiteral("Home");
256 QTest::newRow("home with trailing slash") << QUrl::fromLocalFile(QStringLiteral("%1/").arg(QDir::homePath())) << QStringLiteral("Home");
257 QTest::newRow("trash") << QUrl::fromUserInput(QStringLiteral("trash:/")) << QStringLiteral("Trash");
260 void DolphinMainWindowTest::testWindowTitle()
262 QFETCH(QUrl
, activeViewUrl
);
263 m_mainWindow
->openDirectories({activeViewUrl
}, false);
264 m_mainWindow
->show();
265 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
266 QVERIFY(m_mainWindow
->isVisible());
268 QFETCH(QString
, expectedWindowTitle
);
269 QCOMPARE(m_mainWindow
->windowTitle(), expectedWindowTitle
);
273 * The places panel will resize itself if any of the other widgets requires too much horizontal space
274 * but a user never wants the size of the places panel to change unless they resized it themselves explicitly.
276 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
278 m_mainWindow
->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false);
279 m_mainWindow
->show();
280 m_mainWindow
->resize(800, m_mainWindow
->height()); // make sure the size is sufficient so a places panel resize shouldn't be necessary.
281 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
282 QVERIFY(m_mainWindow
->isVisible());
284 QWidget
*placesPanel
= reinterpret_cast<QWidget
*>(m_mainWindow
->m_placesPanel
);
285 QVERIFY2(QTest::qWaitFor(
287 return placesPanel
&& placesPanel
->isVisible() && placesPanel
->width() > 0;
290 "The test couldn't be initialised properly. The places panel should be visible.");
292 const int initialPlacesPanelWidth
= placesPanel
->width();
294 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
295 QTest::qWait(300); // wait for animation
296 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
298 m_mainWindow
->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
299 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
301 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
302 // 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.
303 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll
))->trigger();
304 for (int selectionModeStates
= SelectionMode::BottomBar::CopyContents
; selectionModeStates
!= SelectionMode::BottomBar::RenameContents
;
305 selectionModeStates
++) {
306 const auto contents
= static_cast<SelectionMode::BottomBar::Contents
>(selectionModeStates
);
307 m_mainWindow
->slotSetSelectionMode(true, contents
);
308 QTest::qWait(20); // give time for a paint/resize
309 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
312 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Find
))->trigger();
313 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
316 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
317 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
321 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
322 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
325 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
326 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
329 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
330 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
334 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
335 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
338 m_mainWindow
->showMaximized();
339 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
341 QTest::qWait(300); // wait for split view closing animation
342 QCOMPARE(placesPanel
->width(), initialPlacesPanelWidth
);
345 void DolphinMainWindowTest::testGoActions()
347 QScopedPointer
<TestDir
> testDir
{new TestDir()};
348 testDir
->createDir("a");
349 testDir
->createDir("b");
350 testDir
->createDir("b/b-1");
351 testDir
->createFile("b/b-2");
352 testDir
->createDir("c");
353 QUrl
childDirUrl(QDir::cleanPath(testDir
->url().toString() + "/b"));
354 m_mainWindow
->openDirectories({childDirUrl
}, false); // Open "b" dir
355 m_mainWindow
->show();
356 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow
.data()));
357 QVERIFY(m_mainWindow
->isVisible());
358 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
360 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Up
))->trigger();
362 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
363 * This is especially important when a user wants to peek into multiple folders in quick succession.
365 QSignalSpy
spyDirectoryLoadingCompleted(m_mainWindow
->m_activeViewContainer
->view(), &DolphinView::directoryLoadingCompleted
);
366 QVERIFY(spyDirectoryLoadingCompleted
.wait());
367 QVERIFY(QTest::qWaitFor([&]() {
368 return !m_mainWindow
->actionCollection()->action(QStringLiteral("stop"))->isEnabled();
369 })); // "Stop" command should be disabled because it finished loading
370 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
371 const QUrl parentDirUrl
= m_mainWindow
->activeViewContainer()->url();
372 QVERIFY(parentDirUrl
!= childDirUrl
);
374 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
375 // 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.
376 m_mainWindow
->actionCollection()->action(QStringLiteral("compact"))->trigger();
377 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Down
, Qt::NoModifier
);
378 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
379 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Up
, Qt::NoModifier
);
380 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
381 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::NoModifier
);
382 QVERIFY(spyDirectoryLoadingCompleted
.wait());
383 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
384 QVERIFY(m_mainWindow
->isUrlOpen(childDirUrl
.toString()));
386 // Go back to the parent folder
387 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
388 QVERIFY(spyDirectoryLoadingCompleted
.wait());
389 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
390 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
391 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
393 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
394 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Enter
, Qt::ControlModifier
); // Open new inactive tab
395 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 2);
396 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
397 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
398 QVERIFY(!m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
400 // Go forward to the child folder.
401 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->trigger();
402 QVERIFY(spyDirectoryLoadingCompleted
.wait());
403 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
404 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
406 // Go back to the parent folder.
407 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->trigger();
408 QVERIFY(spyDirectoryLoadingCompleted
.wait());
409 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
410 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), parentDirUrl
);
411 QVERIFY(m_mainWindow
->isUrlOpen(parentDirUrl
.toString()));
413 // 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
414 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Close
))->trigger(); // Close current tab
415 QVERIFY(m_mainWindow
->m_tabWidget
->count() == 1);
416 QCOMPARE(m_mainWindow
->activeViewContainer()->url(), childDirUrl
);
417 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
418 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Back
))->isEnabled());
419 QVERIFY(!m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Forward
))->isEnabled());
420 QVERIFY(m_mainWindow
->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
423 void DolphinMainWindowTest::testOpenFiles()
425 QScopedPointer
<TestDir
> testDir
{new TestDir()};
426 QString
testDirUrl(QDir::cleanPath(testDir
->url().toString()));
427 testDir
->createDir("a");
428 testDir
->createDir("a/b");
429 testDir
->createDir("a/b/c");
430 testDir
->createDir("a/b/c/d");
431 m_mainWindow
->openDirectories({testDirUrl
}, false);
432 m_mainWindow
->show();
434 // We only see the unselected "a" folder in the test dir. There are no other tabs.
435 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
436 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
437 QVERIFY(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
438 QVERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
439 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
440 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
441 QCOMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
443 // "a" is already in view, so "opening" "a" should simply select it without opening a new tab.
444 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
445 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
446 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 1);
447 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
449 // "b" is not in view, so "opening" "b" should open a new active tab of the parent folder "a" and select "b" there.
450 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
451 QTRY_VERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
452 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
453 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
454 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b"));
455 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "The directory b is supposed to be visible but not open in its own tab.");
456 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
458 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
459 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a"));
460 // "a" is still in view in the first tab, so "opening" "a" should switch to the first tab and select "a" there.
461 m_mainWindow
->openFiles({testDirUrl
+ "/a"}, false);
462 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
463 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
464 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
465 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
467 // Directory "a" is already open in the second tab in which "b" is selected, so opening the directory "a" should switch to that tab.
468 m_mainWindow
->openDirectories({testDirUrl
+ "/a"}, false);
469 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
470 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
472 // 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.
473 m_mainWindow
->actionCollection()->action(QStringLiteral("details"))->trigger();
474 QTRY_VERIFY(m_mainWindow
->activeViewContainer()->view()->itemsExpandable());
476 // Expand the already selected "b" with the right arrow key. This should make "c" visible.
477 QVERIFY2(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"), "The parent folder wasn't expanded yet, so c shouldn't be visible.");
478 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Right
);
479 QTRY_VERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
480 QVERIFY2(!m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b"), "b is supposed to be expanded, however it shouldn't be open in its own tab.");
481 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
483 // Switch to first tab by opening it even though it is already open.
484 m_mainWindow
->openDirectories({testDirUrl
}, false);
485 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
486 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 0);
488 // "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.
489 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
490 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 2);
491 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
492 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
493 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
));
494 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a"));
496 // Opening the directory "c" on the other hand will open it in a new tab even though it is already visible in the view
497 // because openDirecories() and openFiles() serve different purposes. One opens views at urls, the other selects files within views.
498 m_mainWindow
->openDirectories({testDirUrl
+ "/a/b/c/d", testDirUrl
+ "/a/b/c"}, true);
499 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
500 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
501 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->splitViewEnabled());
502 QVERIFY(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c")); // It should still be visible in the second tab.
503 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 0);
504 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c/d"));
505 QVERIFY(m_mainWindow
->isUrlOpen(testDirUrl
+ "/a/b/c"));
507 // "c" is in view in the second tab because "b" is expanded there,
508 // so "opening" "c" should switch to that tab even though "c" as a directory is open in the current tab.
509 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c"}, false);
510 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
511 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 1);
512 QVERIFY2(m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c/d"), "It should be visible in the secondary view of the third tab.");
514 // Select "b" and un-expand it with the left arrow key. This should make "c" invisible.
515 m_mainWindow
->openFiles({testDirUrl
+ "/a/b"}, false);
516 QTest::keyClick(m_mainWindow
->activeViewContainer()->view()->m_container
, Qt::Key::Key_Left
);
517 QTRY_VERIFY(!m_mainWindow
->isItemVisibleInAnyView(testDirUrl
+ "/a/b/c"));
519 // "d" is in view in the third tab in the secondary view, so "opening" "d" should select that view.
520 m_mainWindow
->openFiles({testDirUrl
+ "/a/b/c/d"}, false);
521 QCOMPARE(m_mainWindow
->m_tabWidget
->count(), 3);
522 QCOMPARE(m_mainWindow
->m_tabWidget
->currentIndex(), 2);
523 QVERIFY(m_mainWindow
->m_tabWidget
->currentTabPage()->secondaryViewContainer()->isActive());
524 QTRY_COMPARE(m_mainWindow
->m_activeViewContainer
->view()->selectedItems().count(), 1);
527 void DolphinMainWindowTest::cleanupTestCase()
529 m_mainWindow
->showNormal();
530 m_mainWindow
->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
533 m_mainWindow
->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
537 m_mainWindow
->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
540 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
541 m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Quit
))->trigger();
544 QTEST_MAIN(DolphinMainWindowTest
)
546 #include "dolphinmainwindowtest.moc"