From 4102ccb80457eea44ea280f0ace2a419602bc34b Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Mon, 7 Apr 2025 21:09:00 +0000 Subject: Rewrite search integration This huge commit is a nearly complete rewrite of the Dolphin search code. It implements most of the improved Dolphin search UI/UX as designed and discussed in a collaborative effort by Kristen McWilliam, Jin Liu, Andy Betts, Tagwerk, a few others and me. See https://invent.kde.org/system/dolphin/-/issues/46. # Notable changes - A toggle to change the search tool is provided as most contributors deemed that useful in https://invent.kde.org/system/dolphin/-/merge_requests/642#note_985112. - The default search is changed to filenamesearch for maximum reliability. - Removing all search parameters will take users back to the view state prior to starting a search instead of keeping the search results open. - The UI for choosing file types or modification dates has been made more powerful with more granularity and more options. - Most search parameters can be configured from a popup menu which gives us extra space for extra clarity. - Labels and help buttons as well as hyperlinks to settings makes sure the user always knows why some search parameters are unavailable in some contexts. - Chips show important search parameters while the popup is closed. They allow quickly removing filters. - The titles of the search and the input field placeholder message change to make clear whether file names or file contents are searched. - When the user actively switches the search tool, whether content should be searched, or whether to search everywhere, this is preserved for the initial state of the search bar when the user opens it the next time after restarting Dolphin. # Architecture - The new DolphinQuery class is independent of the UI and contains all search parameters modifiable in Dolphin as easy setters and getters. - DolphinQuery objects are also used to update the states of every component in the search UI. There is now a clear separation of UI and search configuration/DolphinQuery. - DolphinQuery is responsible for exporting to and importing from search URLs. - The search UI always reflects the currently configured DolphinQuery no matter if the user changed the UI to change the DolphinQuery or loaded a DolphinQuery/older search URL which then is reflected in the UI. - I tried to simplify all classes and their interaction between each other as much as possible. - I added some tests BUG: 386754 CCBUG: 435119 CCBUG: 458761 BUG: 446387 BUG: 470136 CCBUG: 471556 CCBUG: 475439 CCBUG: 477969 BUG: 480001 BUG: 483578 BUG: 488047 BUG: 488845 BUG: 500103 FIXED-IN: 25.08 --- src/tests/dolphinsearchbartest.cpp | 124 +++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/tests/dolphinsearchbartest.cpp (limited to 'src/tests/dolphinsearchbartest.cpp') diff --git a/src/tests/dolphinsearchbartest.cpp b/src/tests/dolphinsearchbartest.cpp new file mode 100644 index 000000000..0efb05dc8 --- /dev/null +++ b/src/tests/dolphinsearchbartest.cpp @@ -0,0 +1,124 @@ +/* + * SPDX-FileCopyrightText: 2011 Peter Penz + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "search/bar.h" +#include "search/popup.h" + +#include +#include +#include +#include +#include + +class DolphinSearchBarTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void init(); + void cleanup(); + + void testPopupLazyLoading(); + void testTextClearing(); + void testUrlChangeSignals(); + +private: + Search::Bar *m_searchBar; +}; + +void DolphinSearchBarTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); +} + +void DolphinSearchBarTest::init() +{ + const auto homeUrl{QUrl::fromUserInput(QDir::homePath())}; + m_searchBar = new Search::Bar(std::make_shared(homeUrl, homeUrl)); +} + +void DolphinSearchBarTest::cleanup() +{ + delete m_searchBar; +} + +void DolphinSearchBarTest::testPopupLazyLoading() +{ + m_searchBar->setVisible(true, WithoutAnimation); + QVERIFY2(m_searchBar->m_popup->isEmpty(), "The popup should only be populated or updated when it was opened at least once by the user."); +} + +/** + * The test verifies whether the automatic clearing of the text works correctly. + * The text may not get cleared when the search bar gets visible or invisible, + * as this would clear the text when switching between tabs. + */ +void DolphinSearchBarTest::testTextClearing() +{ + m_searchBar->setVisible(true, WithoutAnimation); + QVERIFY(m_searchBar->text().isEmpty()); + QVERIFY(!m_searchBar->isSearchConfigured()); + + m_searchBar->updateStateToMatch(std::make_shared(QUrl::fromUserInput("filenamesearch:?search=xyz"), QUrl{})); + m_searchBar->setVisible(false, WithoutAnimation); + m_searchBar->setVisible(true, WithoutAnimation); + QCOMPARE(m_searchBar->text(), QStringLiteral("xyz")); + QVERIFY(m_searchBar->isSearchConfigured()); + QVERIFY(!m_searchBar->queryTitle().isEmpty()); + + QTest::keyClick(m_searchBar, Qt::Key_Escape); + QVERIFY(m_searchBar->text().isEmpty()); + QVERIFY(!m_searchBar->isSearchConfigured()); +} + +void DolphinSearchBarTest::testUrlChangeSignals() +{ + QSignalSpy spyUrlChangeRequested(m_searchBar, &Search::Bar::urlChangeRequested); + m_searchBar->setVisible(true, WithoutAnimation); + QVERIFY2(spyUrlChangeRequested.isEmpty(), "Opening the Search::Bar should not trigger anything."); + + m_searchBar->m_everywhereButton->click(); + m_searchBar->m_fromHereButton->click(); + QVERIFY(!m_searchBar->isSearchConfigured()); + + while (!spyUrlChangeRequested.isEmpty()) { + const QUrl requestedUrl = qvariant_cast(spyUrlChangeRequested.takeFirst().at(0)); + QVERIFY2(!Search::isSupportedSearchScheme(requestedUrl.scheme()) && requestedUrl == m_searchBar->m_searchConfiguration->searchPath(), + "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 " + "the Search::Bar."); + } + + Search::DolphinQuery searchConfiguration = *m_searchBar->m_searchConfiguration; + searchConfiguration.setSearchTerm(QStringLiteral("searchTerm")); + m_searchBar->updateStateToMatch(std::make_shared(searchConfiguration)); + QVERIFY2(m_searchBar->isSearchConfigured(), "The search bar now has enough information to trigger a meaningful search."); + QVERIFY2(spyUrlChangeRequested.isEmpty(), "The visual state was updated to match a new search configuration, but the user never triggered a search."); + + m_searchBar->commitCurrentConfiguration(); // We pretend to be a user interaction that would trigger a search to happen. + const QUrl requestedUrl = qvariant_cast(spyUrlChangeRequested.takeFirst().at(0)); + QVERIFY2(Search::isSupportedSearchScheme(requestedUrl.scheme()), "The search bar requested to open a search url and therefore start searching."); + QVERIFY2(spyUrlChangeRequested.isEmpty(), "The search URL was requested exactly once, so no additional urlChangeRequested signals should exist."); + + Search::DolphinQuery searchConfiguration2 = *m_searchBar->m_searchConfiguration; + searchConfiguration2.setSearchTerm(QString("")); + m_searchBar->updateStateToMatch(std::make_shared(searchConfiguration2)); + QVERIFY2(!m_searchBar->isSearchConfigured(), "The search bar does not have enough information anymore to trigger a meaningful search."); + QVERIFY2(spyUrlChangeRequested.isEmpty(), "The visual state was updated to match a new search configuration, but the user never triggered a search."); + + m_searchBar->commitCurrentConfiguration(); // We pretend to be a user interaction that would trigger a search to happen. + const QUrl requestedUrl2 = qvariant_cast(spyUrlChangeRequested.takeFirst().at(0)); + QVERIFY2(!Search::isSupportedSearchScheme(requestedUrl2.scheme()) && requestedUrl2 == m_searchBar->m_searchConfiguration->searchPath(), + "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 " + "again instead of any previous search URL."); + QVERIFY2(spyUrlChangeRequested.isEmpty(), "The non-search URL was requested exactly once, so no additional urlChangeRequested signals should exist."); + + QVERIFY2(m_searchBar->m_popup->isEmpty(), "Through all of this, the popup should still be empty because it was never opened by the user."); +} + +QTEST_MAIN(DolphinSearchBarTest) + +#include "dolphinsearchbartest.moc" -- cgit v1.3