]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinmainwindowtest.cpp
Improve goActions 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 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
281 QVERIFY(m_mainWindow->isVisible());
282
283 QWidget *placesPanel = reinterpret_cast<QWidget *>(m_mainWindow->m_placesPanel);
284 QVERIFY2(QTest::qWaitFor([&](){ return placesPanel && placesPanel->isVisible() && placesPanel->width() > 0; }, 5000), "The test couldn't be initialised properly. The places panel should be visible.");
285 QTest::qWait(100);
286 const int initialPlacesPanelWidth = placesPanel->width();
287
288 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // enable split view (starts animation)
289 QTest::qWait(300); // wait for animation
290 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
291
292 m_mainWindow->actionCollection()->action(QStringLiteral("show_filter_bar"))->trigger();
293 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
294
295 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Find))->trigger();
296 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
297
298 #if HAVE_BALOO
299 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(true); // toggle visible
300 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
301 #endif
302
303 #if HAVE_TERMINAL
304 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(true); // toggle visible
305 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
306 #endif
307
308 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->trigger(); // disable split view (starts animation)
309 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
310
311 #if HAVE_BALOO
312 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->trigger(); // toggle invisible
313 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
314 #endif
315
316 #if HAVE_TERMINAL
317 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->trigger(); // toggle invisible
318 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
319 #endif
320
321 m_mainWindow->showMaximized();
322 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
323
324 QTest::qWait(300); // wait for split view closing animation
325 QCOMPARE(placesPanel->width(), initialPlacesPanelWidth);
326 }
327
328 void DolphinMainWindowTest::testGoActions()
329 {
330 QScopedPointer<TestDir> testDir{new TestDir()};
331 testDir->createDir("a");
332 testDir->createDir("b");
333 testDir->createDir("b/b-1");
334 testDir->createFile("b/b-2");
335 testDir->createDir("c");
336 QUrl childDirUrl(QDir::cleanPath(testDir->url().toString() + "/b"));
337 m_mainWindow->openDirectories({ childDirUrl }, false); // Open "b" dir
338 m_mainWindow->show();
339 QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data()));
340 QVERIFY(m_mainWindow->isVisible());
341 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
342
343 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Up))->trigger();
344 /**
345 * Now, after going "up" in the file hierarchy (to "testDir"), the folder one has emerged from ("b") should have keyboard focus.
346 * This is especially important when a user wants to peek into multiple folders in quick succession.
347 */
348 QSignalSpy spyDirectoryLoadingCompleted(m_mainWindow->m_activeViewContainer->view(), &DolphinView::directoryLoadingCompleted);
349 QVERIFY(spyDirectoryLoadingCompleted.wait());
350 QVERIFY(QTest::qWaitFor([&](){ return !m_mainWindow->actionCollection()->action(QStringLiteral("stop"))->isEnabled(); })); // "Stop" command should be disabled because it finished loading
351 QTest::qWait(500); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
352 const QUrl parentDirUrl = m_mainWindow->activeViewContainer()->url();
353 QVERIFY(parentDirUrl != childDirUrl);
354
355 // The item we just emerged from should now have keyboard focus but this doesn't necessarily mean that it is selected.
356 // 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.
357 m_mainWindow->actionCollection()->action(QStringLiteral("compact"))->trigger();
358 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Down, Qt::NoModifier);
359 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
360 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Up, Qt::NoModifier);
361 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 1);
362 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::NoModifier);
363 QVERIFY(spyDirectoryLoadingCompleted.wait());
364 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
365 QVERIFY(m_mainWindow->isUrlOpen(childDirUrl.toString()));
366
367 // Go back to the parent folder
368 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
369 QVERIFY(spyDirectoryLoadingCompleted.wait());
370 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
371 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
372 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
373
374 // Open a new tab for the "b" child dir and verify that this doesn't interfere with anything.
375 QTest::keyClick(m_mainWindow->activeViewContainer()->view()->m_container, Qt::Key::Key_Enter, Qt::ControlModifier); // Open new inactive tab
376 QVERIFY(m_mainWindow->m_tabWidget->count() == 2);
377 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
378 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
379 QVERIFY(!m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
380
381 // Go forward to the child folder.
382 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->trigger();
383 QVERIFY(spyDirectoryLoadingCompleted.wait());
384 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
385 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this view yet that would warrant a selection.
386
387 // Go back to the parent folder.
388 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->trigger();
389 QVERIFY(spyDirectoryLoadingCompleted.wait());
390 QTest::qWait(100); // Somehow the item we emerged from doesn't have keyboard focus yet if we don't wait a split second.
391 QCOMPARE(m_mainWindow->activeViewContainer()->url(), parentDirUrl);
392 QVERIFY(m_mainWindow->isUrlOpen(parentDirUrl.toString()));
393
394 // 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
395 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Close))->trigger(); // Close current tab
396 QVERIFY(m_mainWindow->m_tabWidget->count() == 1);
397 QCOMPARE(m_mainWindow->activeViewContainer()->url(), childDirUrl);
398 QCOMPARE(m_mainWindow->m_activeViewContainer->view()->selectedItems().count(), 0); // There was no action in this tab yet that would warrant a selection.
399 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Back))->isEnabled());
400 QVERIFY(!m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Forward))->isEnabled());
401 QVERIFY(m_mainWindow->actionCollection()->action(QStringLiteral("undo_close_tab"))->isEnabled());
402 }
403
404 void DolphinMainWindowTest::cleanupTestCase()
405 {
406 m_mainWindow->showNormal();
407 m_mainWindow->actionCollection()->action(QStringLiteral("split_view"))->setChecked(false); // disable split view (starts animation)
408
409 #if HAVE_BALOO
410 m_mainWindow->actionCollection()->action(QStringLiteral("show_information_panel"))->setChecked(false); // hide panel
411 #endif
412
413 #if HAVE_TERMINAL
414 m_mainWindow->actionCollection()->action(QStringLiteral("show_terminal_panel"))->setChecked(false); // hide panel
415 #endif
416
417 // Quit Dolphin to save the hiding of panels and make sure that normal Quit doesn't crash.
418 m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Quit))->trigger();
419 }
420
421
422 QTEST_MAIN(DolphinMainWindowTest)
423
424 #include "dolphinmainwindowtest.moc"