Changeset View
Changeset View
Standalone View
Standalone View
src/tests/dolphinsearchboxtest.cpp
1 | /*************************************************************************** | 1 | /*************************************************************************** | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> * | 2 | * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> * | ||
3 | * Copyright (C) 2019 by Ismael Asensio <isma.af@gmail.com> * | ||||
3 | * * | 4 | * * | ||
4 | * This program is free software; you can redistribute it and/or modify * | 5 | * This program is free software; you can redistribute it and/or modify * | ||
5 | * it under the terms of the GNU General Public License as published by * | 6 | * it under the terms of the GNU General Public License as published by * | ||
6 | * the Free Software Foundation; either version 2 of the License, or * | 7 | * the Free Software Foundation; either version 2 of the License, or * | ||
7 | * (at your option) any later version. * | 8 | * (at your option) any later version. * | ||
8 | * * | 9 | * * | ||
9 | * This program is distributed in the hope that it will be useful, * | 10 | * This program is distributed in the hope that it will be useful, * | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
12 | * GNU General Public License for more details. * | 13 | * GNU General Public License for more details. * | ||
13 | * * | 14 | * * | ||
14 | * You should have received a copy of the GNU General Public License * | 15 | * You should have received a copy of the GNU General Public License * | ||
15 | * along with this program; if not, write to the * | 16 | * along with this program; if not, write to the * | ||
16 | * Free Software Foundation, Inc., * | 17 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||
18 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
19 | 20 | | |||
20 | #include "search/dolphinsearchbox.h" | 21 | #include "search/dolphinsearchbox.h" | ||
22 | #include "search/dolphinquerymodel.h" | ||||
21 | 23 | | |||
22 | #include <QTest> | 24 | #include <QTest> | ||
23 | 25 | | |||
26 | #include <config-baloo.h> | ||||
27 | #ifdef HAVE_BALOO | ||||
28 | #include <Baloo/IndexerConfig> | ||||
29 | #include <QDate> | ||||
30 | #include <QJsonDocument> | ||||
31 | #include <QJsonObject> | ||||
32 | #include <QStringList> | ||||
33 | #include <QUrl> | ||||
34 | #include <QUrlQuery> | ||||
35 | #endif | ||||
36 | | ||||
24 | class DolphinSearchBoxTest : public QObject | 37 | class DolphinSearchBoxTest : public QObject | ||
25 | { | 38 | { | ||
26 | Q_OBJECT | 39 | Q_OBJECT | ||
27 | 40 | | |||
28 | private slots: | 41 | private slots: | ||
29 | void init(); | 42 | void init(); | ||
30 | void cleanup(); | 43 | void cleanup(); | ||
31 | 44 | | |||
32 | void testTextClearing(); | 45 | void testTextClearing(); | ||
33 | 46 | #ifdef HAVE_BALOO | |||
47 | void testBalooSearchParsing_data(); | ||||
48 | void testBalooSearchParsing(); | ||||
49 | #endif | ||||
34 | private: | 50 | private: | ||
35 | DolphinSearchBox* m_searchBox; | 51 | DolphinSearchBox* m_searchBox; | ||
36 | }; | 52 | }; | ||
37 | 53 | | |||
38 | void DolphinSearchBoxTest::init() | 54 | void DolphinSearchBoxTest::init() | ||
39 | { | 55 | { | ||
40 | m_searchBox = new DolphinSearchBox(); | 56 | m_searchBox = new DolphinSearchBox(); | ||
57 | m_searchBox->show(); //required to initialize the searchbox widgets | ||||
elvisangelaccio: Please try to use `QVERIFY(QTest::qWaitForWindowExposed(m_searchBox));` in the actual test… | |||||
And unrelated, separate review please. Also likely unnecessary, as testTextClearing already has a show(). bruns: And unrelated, separate review please. Also likely unnecessary, as `testTextClearing` already… | |||||
41 | } | 58 | } | ||
42 | 59 | | |||
43 | void DolphinSearchBoxTest::cleanup() | 60 | void DolphinSearchBoxTest::cleanup() | ||
44 | { | 61 | { | ||
45 | delete m_searchBox; | 62 | delete m_searchBox; | ||
46 | } | 63 | } | ||
47 | 64 | | |||
48 | /** | 65 | /** | ||
Show All 10 Lines | 71 | { | |||
59 | m_searchBox->hide(); | 76 | m_searchBox->hide(); | ||
60 | m_searchBox->show(); | 77 | m_searchBox->show(); | ||
61 | QCOMPARE(m_searchBox->text(), QString("xyz")); | 78 | QCOMPARE(m_searchBox->text(), QString("xyz")); | ||
62 | 79 | | |||
63 | QTest::keyClick(m_searchBox, Qt::Key_Escape); | 80 | QTest::keyClick(m_searchBox, Qt::Key_Escape); | ||
64 | QVERIFY(m_searchBox->text().isEmpty()); | 81 | QVERIFY(m_searchBox->text().isEmpty()); | ||
65 | } | 82 | } | ||
66 | 83 | | |||
84 | #ifdef HAVE_BALOO | ||||
85 | /** | ||||
86 | * Defines the parameters for the test cases in testBalooSearchParsing() | ||||
87 | */ | ||||
88 | void DolphinSearchBoxTest::testBalooSearchParsing_data() | ||||
89 | { | ||||
90 | const Baloo::IndexerConfig searchInfo; | ||||
bruns: The parsing is independent from the config. | |||||
91 | if (!searchInfo.fileIndexingEnabled()) { | ||||
92 | QSKIP("Baloo is not available. Not-indexed search is used instead"); | ||||
93 | } | ||||
94 | | ||||
95 | const QString text = QStringLiteral("xyz"); | ||||
96 | const QString filename = QStringLiteral("filename:\"xyz\""); | ||||
97 | const QString rating = QStringLiteral("rating>=2 "); | ||||
98 | const QString modified = QString("modified>=%1 ").arg(QDate::currentDate().toString(Qt::DateFormat::ISODate)); | ||||
bruns: use a fixed datetime here. | |||||
99 | | ||||
100 | QTest::addColumn<QString>("searchString"); | ||||
101 | QTest::addColumn<QString>("expectedText"); | ||||
102 | QTest::addColumn<QStringList>("expectedTerms"); | ||||
103 | | ||||
104 | // Test for "Content" | ||||
105 | QTest::newRow("content") << text << text << QStringList(); | ||||
106 | QTest::newRow("content/empty") << "" << "" << QStringList(); | ||||
107 | QTest::newRow("content/singleQuote") << "\"" << "" << QStringList(); | ||||
108 | QTest::newRow("content/doubleQuote") << "\"\"" << "" << QStringList(); | ||||
109 | // Test for empty `filename` | ||||
110 | QTest::newRow("filename") << filename << text << QStringList(); | ||||
111 | QTest::newRow("filename/empty") << "filename:" << "" << QStringList(); | ||||
112 | QTest::newRow("filename/singleQuote") << "filename:\"" << "" << QStringList(); | ||||
113 | QTest::newRow("filename/doubleQuote") << "filename:\"\"" << "" << QStringList(); | ||||
114 | | ||||
115 | // Test for rating | ||||
116 | QTest::newRow("rating") << rating << "" << QStringList({rating}); | ||||
117 | QTest::newRow("rating+content") << rating + text << text << QStringList({rating}); | ||||
118 | QTest::newRow("rating+filename") << rating + filename << text << QStringList({rating}); | ||||
119 | // Test for modified date | ||||
120 | QTest::newRow("modified") << modified << "" << QStringList({modified}); | ||||
121 | QTest::newRow("modified+content") << modified + text << text << QStringList({modified}); | ||||
122 | QTest::newRow("modified+filename") << modified + filename << text << QStringList({modified}); | ||||
123 | // Combined tests | ||||
124 | QTest::newRow("rating+modified") << rating + "AND " + modified << "" << QStringList({modified, rating}); | ||||
125 | QTest::newRow("rating+modified+content") << rating + "AND " + modified + text << text << QStringList({modified, rating}); | ||||
126 | QTest::newRow("rating+modified+filename") << rating + "AND " + modified + filename << text << QStringList({modified, rating}); | ||||
127 | } | ||||
128 | | ||||
129 | /** | ||||
130 | * Helper function to compose the baloo query URL used for searching | ||||
131 | */ | ||||
132 | QUrl _composeQueryUrl(const QString searchString) | ||||
Coding style: we never use a leading underscore in function names. Missing pass-by-reference for searchString. elvisangelaccio: Coding style: we never use a leading underscore in function names.
Missing pass-by-reference… | |||||
133 | { | ||||
134 | QJsonObject jsonObject { | ||||
135 | {"searchString", searchString} | ||||
136 | }; | ||||
137 | | ||||
138 | QJsonDocument doc(jsonObject); | ||||
139 | QByteArray docByteArray = doc.toJson(QJsonDocument::Compact); | ||||
140 | QString queryString = QLatin1String(docByteArray); | ||||
One-liner: const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact)); elvisangelaccio: One-liner:
```
const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument… | |||||
141 | | ||||
142 | QUrlQuery urlQuery; | ||||
143 | urlQuery.addQueryItem(QStringLiteral("json"), queryString); | ||||
144 | | ||||
145 | QUrl searchUrl; | ||||
146 | searchUrl.setScheme(QLatin1String("baloosearch")); | ||||
147 | searchUrl.setQuery(urlQuery); | ||||
148 | | ||||
149 | return searchUrl; | ||||
150 | } | ||||
151 | | ||||
152 | /** | ||||
153 | * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are | ||||
154 | * properly handled by the searchbox, and only "user" or filename terms are added to the | ||||
155 | * text bar of the searchbox. | ||||
156 | */ | ||||
157 | void DolphinSearchBoxTest::testBalooSearchParsing() | ||||
158 | { | ||||
159 | QFETCH(QString, searchString); | ||||
160 | QFETCH(QString, expectedText); | ||||
161 | QFETCH(QStringList, expectedTerms); | ||||
162 | | ||||
163 | QUrl testUrl = _composeQueryUrl(searchString); | ||||
elvisangelaccio: `const` | |||||
164 | DolphinQueryModel queryModel = DolphinQueryModel::fromBalooSearchUrl(testUrl); | ||||
165 | | ||||
166 | QStringList searchTerms = queryModel.searchTerms(); | ||||
167 | searchTerms.sort(); | ||||
168 | | ||||
169 | // FIXME: Current parsing bugs | ||||
170 | QEXPECT_FAIL("content/singleQuote", "Quotes around text are shown", Continue); | ||||
Just add a column "failureReason" to the data, and do if (!failureReason.empty()) { QEXPECT_FAIL("", qPrintable(failureReason), Continue); } Keeps test and result together. bruns: Just add a column "failureReason" to the data, and do
```
if (!failureReason.empty()) {… | |||||
Since this is temporary until the fix commit, I think this would keep the revisions simpler iasensio: Since this is temporary until the fix commit, I think this would keep the revisions simpler | |||||
171 | QEXPECT_FAIL("content/doubleQuote", "Quotes around text are shown", Continue); | ||||
172 | | ||||
173 | QEXPECT_FAIL("filename", "Quotes around text are shown", Continue); | ||||
174 | QEXPECT_FAIL("filename/singleQuote", "Quotes around text are shown", Continue); | ||||
175 | QEXPECT_FAIL("filename/doubleQuote", "Quotes around text are shown", Continue); | ||||
176 | | ||||
177 | QEXPECT_FAIL("rating" , "Text includes also search terms", Continue); | ||||
178 | QEXPECT_FAIL("rating+content" , "Text includes also search terms", Continue); | ||||
179 | QEXPECT_FAIL("rating+filename" , "Text includes also search terms", Continue); | ||||
180 | QEXPECT_FAIL("modified" , "Text includes also search terms", Continue); | ||||
181 | QEXPECT_FAIL("modified+content" , "Text includes also search terms", Continue); | ||||
182 | QEXPECT_FAIL("modified+filename" , "Text includes also search terms", Continue); | ||||
183 | QEXPECT_FAIL("rating+modified" , "Text includes also search terms", Continue); | ||||
184 | QEXPECT_FAIL("rating+modified+content" , "Text includes also search terms", Continue); | ||||
185 | QEXPECT_FAIL("rating+modified+filename", "Text includes also search terms", Continue); | ||||
186 | | ||||
187 | // Check for parsed text (would be displayed on the input search bar) | ||||
188 | QCOMPARE(queryModel.text(), expectedText); | ||||
189 | | ||||
190 | // Check for parsed search terms (would be displayed by the facetsWidget) | ||||
191 | QCOMPARE(searchTerms.count(), expectedTerms.count()); | ||||
192 | for (int i=0; i < expectedTerms.count(); i++) { | ||||
elvisangelaccio: Coding style: missing space before/after = | |||||
193 | QCOMPARE(searchTerms.at(i).trimmed(), expectedTerms.at(i).trimmed()); | ||||
bruns: the second `.trimmed()` should better be not necessary. | |||||
I removed both since this is a remnant from my first attempt comparing searchStrings directly. iasensio: I removed both since this is a remnant from my first attempt comparing `searchString`s directly. | |||||
194 | } | ||||
195 | } | ||||
196 | #endif //HAVE_BALOO | ||||
197 | | ||||
67 | QTEST_MAIN(DolphinSearchBoxTest) | 198 | QTEST_MAIN(DolphinSearchBoxTest) | ||
68 | 199 | | |||
69 | #include "dolphinsearchboxtest.moc" | 200 | #include "dolphinsearchboxtest.moc" |
Please try to use QVERIFY(QTest::qWaitForWindowExposed(m_searchBox)); in the actual test instead.