]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinmainwindowtest.cpp
Increase code coverage of places panel width resistance test
[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 <QScopedPointer>
18 #include <QSignalSpy>
19 #include <QStandardPaths>
20 #include <QTest>
21
22 class DolphinMainWindowTest : public QObject
23 {
24 Q_OBJECT
25
26 private Q_SLOTS:
27 void initTestCase();
28 void init();
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();
40 void testGoActions();
41 void cleanupTestCase();
42
43
44 private:
45 QScopedPointer<DolphinMainWindow> m_mainWindow;
46 };
47
48 void DolphinMainWindowTest::initTestCase()
49 {
50 QStandardPaths::setTestModeEnabled(true);
51 }
52
53 void DolphinMainWindowTest::init()
54 {
55 m_mainWindow.reset(new DolphinMainWindow());
56 }
57
58 // See https://bugs.kde.org/show_bug.cgi?id=379135
59 void DolphinMainWindowTest::testClosingTabsWithSearchBoxVisible()
60 {
61 m_mainWindow->openDirectories({ QUrl::fromLocalFile(QDir::homePath()) }, false);
62 m_mainWindow->show();
63 // Without this call the searchbox doesn't get FocusIn events.
64 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
65 QVERIFY(m_mainWindow->isVisible());
66
67 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget*>("tabWidget");
68 QVERIFY(tabWidget);
69
70 // Show search box on first tab.
71 tabWidget->currentTabPage()->activeViewContainer()->setSearchModeEnabled(true);
72
73 tabWidget->openNewActivatedTab(QUrl::fromLocalFile(QDir::homePath()));
74 QCOMPARE(tabWidget->count(), 2);
75
76 // Triggers the crash in bug #379135.
77 tabWidget->closeTab();
78 QCOMPARE(tabWidget->count(), 1);
79 }
80
81 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView_data()
82 {
83 QTest::addColumn<bool>("closeLeftView");
84
85 QTest::newRow("close left view") << true;
86 QTest::newRow("close right view") << false;
87 }
88
89 void DolphinMainWindowTest::testActiveViewAfterClosingSplitView()
90 {
91 m_mainWindow->openDirectories({ QUrl::fromLocalFile(QDir::homePath()) }, false);
92 m_mainWindow->show();
93 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
94 QVERIFY(m_mainWindow->isVisible());
95
96 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget*>("tabWidget");
97 QVERIFY(tabWidget);
98 QVERIFY(tabWidget->currentTabPage()->primaryViewContainer());
99 QVERIFY(!tabWidget->currentTabPage()->secondaryViewContainer());
100
101 // Open split view.
102 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
103 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
104 QVERIFY(tabWidget->currentTabPage()->secondaryViewContainer());
105
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());
111
112 QFETCH(bool, closeLeftView);
113 if (closeLeftView) {
114 // Activate left view.
115 leftViewContainer->setActive(true);
116 QVERIFY(leftViewContainer->isActive());
117 QVERIFY(!rightViewContainer->isActive());
118
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());
124 } else {
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());
130 }
131 }
132
133 // Test case for bug #385111
134 void DolphinMainWindowTest::testUpdateWindowTitleAfterClosingSplitView()
135 {
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());
140
141 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget*>("tabWidget");
142 QVERIFY(tabWidget);
143 QVERIFY(tabWidget->currentTabPage()->primaryViewContainer());
144 QVERIFY(!tabWidget->currentTabPage()->secondaryViewContainer());
145
146 // Open split view.
147 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
148 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
149 QVERIFY(tabWidget->currentTabPage()->secondaryViewContainer());
150
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());
156
157 // Activate left view.
158 leftViewContainer->setActive(true);
159 QVERIFY(leftViewContainer->isActive());
160 QVERIFY(!rightViewContainer->isActive());
161
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());
167
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);
172 }
173
174 // Test case for bug #402641
175 void DolphinMainWindowTest::testUpdateWindowTitleAfterChangingSplitView()
176 {
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());
181
182 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget*>("tabWidget");
183 QVERIFY(tabWidget);
184
185 // Open split view.
186 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger();
187 QVERIFY(tabWidget->currentTabPage()->splitViewEnabled());
188
189 auto leftViewContainer = tabWidget->currentTabPage()->primaryViewContainer();
190 auto rightViewContainer = tabWidget->currentTabPage()->secondaryViewContainer();
191
192 // Store old window title.
193 const auto oldTitle = m_mainWindow->windowTitle();
194
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);
198
199 // Activate back the left view and check whether the old title gets restored.
200 leftViewContainer->setActive(true);
201 QCOMPARE(m_mainWindow->windowTitle(), oldTitle);
202 }
203
204 // Test case for bug #397910
205 void DolphinMainWindowTest::testOpenInNewTabTitle()
206 {
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());
211
212 auto tabWidget = m_mainWindow->findChild<DolphinTabWidget*>("tabWidget");
213 QVERIFY(tabWidget);
214
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());
221 }
222 }
223
224 void DolphinMainWindowTest::testNewFileMenuEnabled_data()
225 {
226 QTest::addColumn<QUrl>("activeViewUrl");
227 QTest::addColumn<bool>("expectedEnabled");
228
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;
232 }
233
234 void DolphinMainWindowTest::testNewFileMenuEnabled()
235 {
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());
241
242 auto newFileMenu = m_mainWindow->findChild<DolphinNewFileMenu*>("new_menu");
243 QVERIFY(newFileMenu);
244
245 QFETCH(bool, expectedEnabled);
246 QTRY_COMPARE(newFileMenu->isEnabled(), expectedEnabled);
247 }
248
249 void DolphinMainWindowTest::testWindowTitle_data()
250 {
251 QTest::addColumn<QUrl>("activeViewUrl");
252 QTest::addColumn<QString>("expectedWindowTitle");
253
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");
258 }
259
260 void DolphinMainWindowTest::testWindowTitle()
261 {
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());
267
268 QFETCH(QString, expectedWindowTitle);
269 QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle);
270 }
271
272 /**
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.
275 */
276 void DolphinMainWindowTest::testPlacesPanelWidthResistance()
277 {
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());
283
284 QWidget *placesPanel = reinterpret_cast<QWidget *>(m_mainWindow->m_placesPanel);
285 QVERIFY2(QTest::qWaitFor([&](){ return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0; }, 5000), "The test couldn't be initialised properly. The places panel should be visible.");
286 QTest::qWait(100);
287 const int initialPlacesPanelWidth = placesPanel->width();
288
289 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
290 QTest::qWait(300); // wait for animation
291 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
292
293 m_mainWindow->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
294 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
295
296 // Make all selection mode bars appear and test for each that this doesn't affect the places panel's width.
297 // 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.
298 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::SelectAll))->trigger();
299 for (int selectionModeStates = SelectionMode::BottomBar::CopyContents; selectionModeStates != SelectionMode::BottomBar::RenameContents; selectionModeStates++) {
300 const auto contents = static_cast<SelectionMode::BottomBar::Contents>(selectionModeStates);
301 m_mainWindow->slotSetSelectionMode(true, contents);
302 QTest::qWait(20); // give time for a paint/resize
303 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
304 }
305
306 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Find))->trigger();
307 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
308
309 #if HAVE_BALOO
310 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
311 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
312 #endif
313
314 #if HAVE_TERMINAL
315 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
316 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
317 #endif
318
319 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
320 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
321
322 #if HAVE_BALOO
323 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
324 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
325 #endif
326
327 #if HAVE_TERMINAL
328 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
329 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
330 #endif
331
332 m_mainWindow->showMaximized();
333 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
334
335 QTest::qWait(300); // wait for split view closing animation
336 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
337 }
338
339 void DolphinMainWindowTest::testGoActions()
340 {
341 QScopedPointer<TestDir> testDir{new TestDir()};
342 testDir->createDir("a");
343 testDir->createDir("b");
344 testDir->createDir("b/b-1");
345 testDir->createFile("b/b-2");
346 testDir->createDir("c");
347 QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b"));
348 m_mainWindow->openDirectories({ childDirUrl }, false); // Open "b" dir
349 m_mainWindow->show();
350 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
351 QVERIFY(m_mainWindow->isVisible());
352 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
353
354 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Up))->trigger();
355 /**
356 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
357 * This is especially important when a user wants to peek into multiple folders in quick succession.
358 */
359 QSignalSpy spyDirectoryLoadingCompleted(m_mainWindow->m_activeViewContainer->view(), &DolphinView::directoryLoadingCompleted);
360 QVERIFY(spyDirectoryLoadingCompleted.wait());
361 QVERIFY(QTest::qWaitFor([&](){ return !m_mainWindow->actionCollection()->action(QStringLiteral("stop"))->isEnabled(); })); // "Stop" command should be disabled because it finished loading
362 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
363 const QUrl parentDirUrl = m_mainWindow->activeViewContainer()->url();
364 QVERIFY(parentDirUrl != childDirUrl);
365
366 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
367 // 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.
368 m_mainWindow->actionCollection()->action(QStringLiteral("compact"))->trigger();
369 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier);
370 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
371 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Up, Qt::NoModifier);
372 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
373 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::NoModifier);
374 QVERIFY(spyDirectoryLoadingCompleted.wait());
375 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
376 QVERIFY(m_mainWindow->isUrlOpen(childDirUrl.toString()));
377
378 // Go back to the parent folder
379 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
380 QVERIFY(spyDirectoryLoadingCompleted.wait());
381 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
382 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
383 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
384
385 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
386 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::ControlModifier); // Open new inactive tab
387 QVERIFY(m_mainWindow->m_tabWidget->count() == 2);
388 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
389 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
390 QVERIFY(!m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
391
392 // Go forward to the child folder.
393 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger();
394 QVERIFY(spyDirectoryLoadingCompleted.wait());
395 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
396 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
397
398 // Go back to the parent folder.
399 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
400 QVERIFY(spyDirectoryLoadingCompleted.wait());
401 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
402 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
403 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
404
405 // 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
406 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Close))->trigger(); // Close current tab
407 QVERIFY(m_mainWindow->m_tabWidget->count() == 1);
408 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
409 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
410 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->isEnabled());
411 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
412 QVERIFY(m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
413 }
414
415 void DolphinMainWindowTest::cleanupTestCase()
416 {
417 m_mainWindow->showNormal();
418 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
419
420 #if HAVE_BALOO
421 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
422 #endif
423
424 #if HAVE_TERMINAL
425 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
426 #endif
427
428 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
429 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Quit))->trigger();
430 }
431
432
433 QTEST_MAIN(DolphinMainWindowTest)
434
435 #include "dolphinmainwindowtest.moc"