]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
KFileItemListWidget: wrong selection when renamed file ends with a dot
[dolphin.git] / src / tests / dolphinquerytest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "search/dolphinquery.h"
8
9 #include <QTest>
10
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 #include <QStandardPaths>
14 #include <QStringList>
15 #include <QUrl>
16 #include <QUrlQuery>
17
18 class DolphinQueryTest : public QObject
19 {
20 Q_OBJECT
21
22 private Q_SLOTS:
23 void initTestCase();
24 void testBalooSearchParsing_data();
25 void testBalooSearchParsing();
26 };
27
28 /**
29 * Helper function to compose the baloo query URL used for searching
30 */
31 QUrl balooQueryUrl(const QString &searchString)
32 {
33 const QJsonObject jsonObject{{"searchString", searchString}};
34
35 const QJsonDocument doc(jsonObject);
36 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
37
38 QUrlQuery urlQuery;
39 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
40
41 QUrl searchUrl;
42 searchUrl.setScheme(QLatin1String("baloosearch"));
43 searchUrl.setQuery(urlQuery);
44
45 return searchUrl;
46 }
47
48 void DolphinQueryTest::initTestCase()
49 {
50 QStandardPaths::setTestModeEnabled(true);
51 }
52
53 /**
54 * Defines the parameters for the test cases in testBalooSearchParsing()
55 */
56 void DolphinQueryTest::testBalooSearchParsing_data()
57 {
58 QTest::addColumn<QUrl>("searchUrl");
59 QTest::addColumn<QString>("expectedText");
60 QTest::addColumn<QStringList>("expectedTerms");
61 QTest::addColumn<bool>("hasContent");
62 QTest::addColumn<bool>("hasFileName");
63
64 const QString text = QStringLiteral("abc");
65 const QString textS = QStringLiteral("abc xyz");
66 const QString textQ = QStringLiteral("\"abc xyz\"");
67 const QString textM = QStringLiteral("\"abc xyz\" tuv");
68
69 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
70 const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
71 const QString filenameQ = QStringLiteral("filename:\"%1\"").arg(textQ);
72 const QString filenameM = QStringLiteral("filename:\"%1\"").arg(textM);
73
74 const QString rating = QStringLiteral("rating>=2");
75 const QString modified = QStringLiteral("modified>=2019-08-07");
76
77 const QString tag = QStringLiteral("tag:tagA");
78 const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
79 const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
80
81 // Test for "Content"
82 QTest::newRow("content") << balooQueryUrl(text) << text << QStringList() << true << false;
83 QTest::newRow("content/space") << balooQueryUrl(textS) << textS << QStringList() << true << false;
84 QTest::newRow("content/quoted") << balooQueryUrl(textQ) << textS << QStringList() << true << false;
85 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
86 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
87 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
88
89 // Test for "FileName"
90 QTest::newRow("filename") << balooQueryUrl(filename) << text << QStringList() << false << true;
91 QTest::newRow("filename/space") << balooQueryUrl(filenameS) << textS << QStringList() << false << true;
92 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ) << textQ << QStringList() << false << true;
93 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM) << textM << QStringList() << false << true;
94 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
95 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
96 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
97
98 // Combined content and filename search
99 QTest::newRow("content+filename") << balooQueryUrl(text + " " + filename) << text + " " + filename << QStringList() << true << true;
100
101 QTest::newRow("content+filename/quoted") << balooQueryUrl(textQ + " " + filenameQ) << textS + " " + filenameQ << QStringList() << true << true;
102
103 // Test for rating
104 QTest::newRow("rating") << balooQueryUrl(rating) << "" << QStringList({rating}) << false << false;
105 QTest::newRow("rating+content") << balooQueryUrl(rating + " " + text) << text << QStringList({rating}) << true << false;
106 QTest::newRow("rating+filename") << balooQueryUrl(rating + " " + filename) << text << QStringList({rating}) << false << true;
107
108 // Test for modified date
109 QTest::newRow("modified") << balooQueryUrl(modified) << "" << QStringList({modified}) << false << false;
110 QTest::newRow("modified+content") << balooQueryUrl(modified + " " + text) << text << QStringList({modified}) << true << false;
111 QTest::newRow("modified+filename") << balooQueryUrl(modified + " " + filename) << text << QStringList({modified}) << false << true;
112
113 // Test for tags
114 QTest::newRow("tag") << balooQueryUrl(tag) << "" << QStringList({tag}) << false << false;
115 QTest::newRow("tag/space") << balooQueryUrl(tagS) << "" << QStringList({tagR}) << false << false;
116 QTest::newRow("tag/double") << balooQueryUrl(tag + " " + tagS) << "" << QStringList({tag, tagR}) << false << false;
117 QTest::newRow("tag+content") << balooQueryUrl(tag + " " + text) << text << QStringList({tag}) << true << false;
118 QTest::newRow("tag+filename") << balooQueryUrl(tag + " " + filename) << text << QStringList({tag}) << false << true;
119
120 // Combined search terms
121 QTest::newRow("searchTerms") << balooQueryUrl(rating + " AND " + modified + " AND " + tag + " AND " + tagS) << ""
122 << QStringList({modified, rating, tag, tagR}) << false << false;
123
124 QTest::newRow("searchTerms+content") << balooQueryUrl(rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS) << text
125 << QStringList({modified, rating, tag, tagR}) << true << false;
126
127 QTest::newRow("searchTerms+filename") << balooQueryUrl(rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS) << text
128 << QStringList({modified, rating, tag, tagR}) << false << true;
129
130 QTest::newRow("allTerms") << balooQueryUrl(text + " " + filename + " " + rating + " AND " + modified + " AND " + tag) << text + " " + filename
131 << QStringList({modified, rating, tag}) << true << true;
132
133 QTest::newRow("allTerms/space") << balooQueryUrl(textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS) << textS + " " + filenameS
134 << QStringList({modified, rating, tagR}) << true << true;
135
136 // Test tags:/ URL scheme
137 const auto tagUrl = [](const QString &tag) {
138 return QUrl(QStringLiteral("tags:/%1/").arg(tag));
139 };
140 const auto tagTerms = [](const QString &tag) {
141 return QStringList{QStringLiteral("tag:%1").arg(tag)};
142 };
143
144 QTest::newRow("tagsUrl") << tagUrl("tagA") << "" << tagTerms("tagA") << false << false;
145 QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
146 QTest::newRow("tagsUrl/hash") << tagUrl("tagC#hash") << "" << tagTerms("tagC#hash") << false << false;
147 QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash") << "" << tagTerms("tagD/with/slash") << false << false;
148 }
149
150 /**
151 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
152 * properly handled by the searchbox, and only "user" or filename terms are added to the
153 * text bar of the searchbox.
154 */
155 void DolphinQueryTest::testBalooSearchParsing()
156 {
157 QFETCH(QUrl, searchUrl);
158 QFETCH(QString, expectedText);
159 QFETCH(QStringList, expectedTerms);
160 QFETCH(bool, hasContent);
161 QFETCH(bool, hasFileName);
162
163 const DolphinQuery query = DolphinQuery::fromSearchUrl(searchUrl);
164
165 // Checkt that the URL is supported
166 QVERIFY(DolphinQuery::supportsScheme(searchUrl.scheme()));
167
168 // Check for parsed text (would be displayed on the input search bar)
169 QCOMPARE(query.text(), expectedText);
170
171 // Check for parsed search terms (would be displayed by the facetsWidget)
172 QStringList searchTerms = query.searchTerms();
173 searchTerms.sort();
174
175 QCOMPARE(searchTerms.count(), expectedTerms.count());
176 for (int i = 0; i < expectedTerms.count(); i++) {
177 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
178 }
179
180 // Check for filename and content detection
181 QCOMPARE(query.hasContentSearch(), hasContent);
182 QCOMPARE(query.hasFileName(), hasFileName);
183 }
184
185 QTEST_MAIN(DolphinQueryTest)
186
187 #include "dolphinquerytest.moc"