2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "search/bar.h"
8 #include "search/popup.h"
12 #include <QStandardPaths>
14 #include <QToolButton>
16 class DolphinSearchBarTest
: public QObject
25 void testPopupLazyLoading();
26 void testTextClearing();
27 void testUrlChangeSignals();
30 Search::Bar
*m_searchBar
;
33 void DolphinSearchBarTest::initTestCase()
35 QStandardPaths::setTestModeEnabled(true);
38 void DolphinSearchBarTest::init()
40 const auto homeUrl
{QUrl::fromUserInput(QDir::homePath())};
41 m_searchBar
= new Search::Bar(std::make_shared
<const Search::DolphinQuery
>(homeUrl
, homeUrl
));
44 void DolphinSearchBarTest::cleanup()
49 void DolphinSearchBarTest::testPopupLazyLoading()
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.");
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.
60 void DolphinSearchBarTest::testTextClearing()
62 m_searchBar
->setVisible(true, WithoutAnimation
);
63 QVERIFY(m_searchBar
->text().isEmpty());
64 QVERIFY(!m_searchBar
->isSearchConfigured());
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());
73 QTest::keyClick(m_searchBar
, Qt::Key_Escape
);
74 QVERIFY(m_searchBar
->text().isEmpty());
75 QVERIFY(!m_searchBar
->isSearchConfigured());
78 void DolphinSearchBarTest::testUrlChangeSignals()
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.");
84 m_searchBar
->m_everywhereButton
->click();
85 m_searchBar
->m_fromHereButton
->click();
86 QVERIFY(!m_searchBar
->isSearchConfigured());
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 "
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.");
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.");
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.");
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.");
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.");
122 QTEST_MAIN(DolphinSearchBarTest
)
124 #include "dolphinsearchbartest.moc"