]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinsearchbartest.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / tests / dolphinsearchbartest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "search/bar.h"
8 #include "search/popup.h"
9
10 #include <QLineEdit>
11 #include <QSignalSpy>
12 #include <QStandardPaths>
13 #include <QTest>
14 #include <QToolButton>
15
16 class DolphinSearchBarTest : public QObject
17 {
18 Q_OBJECT
19
20 private Q_SLOTS:
21 void initTestCase();
22 void init();
23 void cleanup();
24
25 void testPopupLazyLoading();
26 void testTextClearing();
27 void testUrlChangeSignals();
28
29 private:
30 Search::Bar *m_searchBar;
31 };
32
33 void DolphinSearchBarTest::initTestCase()
34 {
35 QStandardPaths::setTestModeEnabled(true);
36 }
37
38 void DolphinSearchBarTest::init()
39 {
40 const auto homeUrl{QUrl::fromUserInput(QDir::homePath())};
41 m_searchBar = new Search::Bar(std::make_shared<const Search::DolphinQuery>(homeUrl, homeUrl));
42 }
43
44 void DolphinSearchBarTest::cleanup()
45 {
46 delete m_searchBar;
47 }
48
49 void DolphinSearchBarTest::testPopupLazyLoading()
50 {
51 m_searchBar->setVisible(true, WithoutAnimation);
52 QVERIFY2(m_searchBar->m_popup->isEmpty(), "The popup should only be populated or updated when it was opened at least once by the user.");
53 }
54
55 /**
56 * The test verifies whether the automatic clearing of the text works correctly.
57 * The text may not get cleared when the search bar gets visible or invisible,
58 * as this would clear the text when switching between tabs.
59 */
60 void DolphinSearchBarTest::testTextClearing()
61 {
62 m_searchBar->setVisible(true, WithoutAnimation);
63 QVERIFY(m_searchBar->text().isEmpty());
64 QVERIFY(!m_searchBar->isSearchConfigured());
65
66 m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(QUrl::fromUserInput("filenamesearch:?search=xyz"), QUrl{}));
67 m_searchBar->setVisible(false, WithoutAnimation);
68 m_searchBar->setVisible(true, WithoutAnimation);
69 QCOMPARE(m_searchBar->text(), QStringLiteral("xyz"));
70 QVERIFY(m_searchBar->isSearchConfigured());
71 QVERIFY(!m_searchBar->queryTitle().isEmpty());
72
73 QTest::keyClick(m_searchBar, Qt::Key_Escape);
74 QVERIFY(m_searchBar->text().isEmpty());
75 QVERIFY(!m_searchBar->isSearchConfigured());
76 }
77
78 void DolphinSearchBarTest::testUrlChangeSignals()
79 {
80 QSignalSpy spyUrlChangeRequested(m_searchBar, &Search::Bar::urlChangeRequested);
81 m_searchBar->setVisible(true, WithoutAnimation);
82 QVERIFY2(spyUrlChangeRequested.isEmpty(), "Opening the Search::Bar should not trigger anything.");
83
84 m_searchBar->m_everywhereButton->click();
85 m_searchBar->m_fromHereButton->click();
86 QVERIFY(!m_searchBar->isSearchConfigured());
87
88 while (!spyUrlChangeRequested.isEmpty()) {
89 const QUrl requestedUrl = qvariant_cast<QUrl>(spyUrlChangeRequested.takeFirst().at(0));
90 QVERIFY2(!Search::isSupportedSearchScheme(requestedUrl.scheme()) && requestedUrl == m_searchBar->m_searchConfiguration->searchPath(),
91 "The search is still not in a state to search for anything specific, so any requested URLs would be identical to the current search path of "
92 "the Search::Bar.");
93 }
94
95 Search::DolphinQuery searchConfiguration = *m_searchBar->m_searchConfiguration;
96 searchConfiguration.setSearchTerm(QStringLiteral("searchTerm"));
97 m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(searchConfiguration));
98 QVERIFY2(m_searchBar->isSearchConfigured(), "The search bar now has enough information to trigger a meaningful search.");
99 QVERIFY2(spyUrlChangeRequested.isEmpty(), "The visual state was updated to match a new search configuration, but the user never triggered a search.");
100
101 m_searchBar->commitCurrentConfiguration(); // We pretend to be a user interaction that would trigger a search to happen.
102 const QUrl requestedUrl = qvariant_cast<QUrl>(spyUrlChangeRequested.takeFirst().at(0));
103 QVERIFY2(Search::isSupportedSearchScheme(requestedUrl.scheme()), "The search bar requested to open a search url and therefore start searching.");
104 QVERIFY2(spyUrlChangeRequested.isEmpty(), "The search URL was requested exactly once, so no additional urlChangeRequested signals should exist.");
105
106 Search::DolphinQuery searchConfiguration2 = *m_searchBar->m_searchConfiguration;
107 searchConfiguration2.setSearchTerm(QString(""));
108 m_searchBar->updateStateToMatch(std::make_shared<const Search::DolphinQuery>(searchConfiguration2));
109 QVERIFY2(!m_searchBar->isSearchConfigured(), "The search bar does not have enough information anymore to trigger a meaningful search.");
110 QVERIFY2(spyUrlChangeRequested.isEmpty(), "The visual state was updated to match a new search configuration, but the user never triggered a search.");
111
112 m_searchBar->commitCurrentConfiguration(); // We pretend to be a user interaction that would trigger a search to happen.
113 const QUrl requestedUrl2 = qvariant_cast<QUrl>(spyUrlChangeRequested.takeFirst().at(0));
114 QVERIFY2(!Search::isSupportedSearchScheme(requestedUrl2.scheme()) && requestedUrl2 == m_searchBar->m_searchConfiguration->searchPath(),
115 "The Search::Bar is not in a state to search for anything specific, so the search bar requests to show the previously visited location normally "
116 "again instead of any previous search URL.");
117 QVERIFY2(spyUrlChangeRequested.isEmpty(), "The non-search URL was requested exactly once, so no additional urlChangeRequested signals should exist.");
118
119 QVERIFY2(m_searchBar->m_popup->isEmpty(), "Through all of this, the popup should still be empty because it was never opened by the user.");
120 }
121
122 QTEST_MAIN(DolphinSearchBarTest)
123
124 #include "dolphinsearchbartest.moc"