┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/tests/dolphinsearchbartest.cpp
blob: 0efb05dc8ef867e5921c852da4b5ae4319caad49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * SPDX-FileCopyrightText: 2011 Peter Penz <[email protected]>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "search/bar.h"
#include "search/popup.h"

#include <QLineEdit>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
#include <QToolButton>

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<const Search::DolphinQuery>(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<const Search::DolphinQuery>(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<QUrl>(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<const Search::DolphinQuery>(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<QUrl>(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<const Search::DolphinQuery>(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<QUrl>(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"