]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
a0774e88c8f63a4a682ad06a72eda36ae228ce3d
[dolphin.git] / src / tests / dolphinquerytest.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@gmail.com> *
3 * *
4 * 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 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "search/dolphinquery.h"
21
22 #include <QTest>
23
24 #include <QJsonDocument>
25 #include <QJsonObject>
26 #include <QStringList>
27 #include <QUrl>
28 #include <QUrlQuery>
29
30 class DolphinSearchBoxTest : public QObject
31 {
32 Q_OBJECT
33
34 private slots:
35 void testBalooSearchParsing_data();
36 void testBalooSearchParsing();
37 };
38
39 /**
40 * Helper function to compose the baloo query URL used for searching
41 */
42 QUrl balooQueryUrl(const QString& searchString)
43 {
44 const QJsonObject jsonObject {
45 {"searchString", searchString}
46 };
47
48 const QJsonDocument doc(jsonObject);
49 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
50
51 QUrlQuery urlQuery;
52 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
53
54 QUrl searchUrl;
55 searchUrl.setScheme(QLatin1String("baloosearch"));
56 searchUrl.setQuery(urlQuery);
57
58 return searchUrl;
59 }
60
61 /**
62 * Defines the parameters for the test cases in testBalooSearchParsing()
63 */
64 void DolphinSearchBoxTest::testBalooSearchParsing_data()
65 {
66
67 QTest::addColumn<QUrl>("searchUrl");
68 QTest::addColumn<QString>("expectedText");
69 QTest::addColumn<QStringList>("expectedTerms");
70 QTest::addColumn<bool>("hasContent");
71 QTest::addColumn<bool>("hasFileName");
72
73 const QString text = QStringLiteral("abc");
74 const QString textS = QStringLiteral("abc xyz");
75 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
76 const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
77
78 const QString rating = QStringLiteral("rating>=2");
79 const QString modified = QStringLiteral("modified>=2019-08-07");
80
81 const QString tag = QStringLiteral("tag:tagA");
82 const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
83 const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
84
85 // Test for "Content"
86 QTest::newRow("content") << balooQueryUrl(text) << text << QStringList() << true << false;
87 QTest::newRow("content/space") << balooQueryUrl(textS) << textS << QStringList() << true << false;
88 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
89 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
90 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
91
92 // Test for "FileName"
93 QTest::newRow("filename") << balooQueryUrl(filename) << text << QStringList() << false << true;
94 QTest::newRow("filename/space") << balooQueryUrl(filenameS) << textS << QStringList() << false << true;
95 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
96 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
97 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
98
99 // Combined content and filename search
100 QTest::newRow("content+filename")
101 << balooQueryUrl(text + " " + filename)
102 << text + " " + filename << QStringList() << true << true;
103
104 // Test for rating
105 QTest::newRow("rating") << balooQueryUrl(rating) << "" << QStringList({rating}) << false << false;
106 QTest::newRow("rating+content") << balooQueryUrl(rating + " " + text) << text << QStringList({rating}) << true << false;
107 QTest::newRow("rating+filename") << balooQueryUrl(rating + " " + filename) << text << QStringList({rating}) << false << true;
108
109 // Test for modified date
110 QTest::newRow("modified") << balooQueryUrl(modified) << "" << QStringList({modified}) << false << false;
111 QTest::newRow("modified+content") << balooQueryUrl(modified + " " + text) << text << QStringList({modified}) << true << false;
112 QTest::newRow("modified+filename") << balooQueryUrl(modified + " " + filename) << text << QStringList({modified}) << false << true;
113
114 // Test for tags
115 QTest::newRow("tag") << balooQueryUrl(tag) << "" << QStringList({tag}) << false << false;
116 QTest::newRow("tag/space" ) << balooQueryUrl(tagS) << "" << QStringList({tagR}) << false << false;
117 QTest::newRow("tag/double") << balooQueryUrl(tag + " " + tagS) << "" << QStringList({tag, tagR}) << false << false;
118 QTest::newRow("tag+content") << balooQueryUrl(tag + " " + text) << text << QStringList({tag}) << true << false;
119 QTest::newRow("tag+filename") << balooQueryUrl(tag + " " + filename) << text << QStringList({tag}) << false << true;
120
121 // Combined search terms
122 QTest::newRow("searchTerms")
123 << balooQueryUrl(rating + " AND " + modified + " AND " + tag + " AND " + tagS)
124 << "" << QStringList({modified, rating, tag, tagR}) << false << false;
125
126 QTest::newRow("searchTerms+content")
127 << balooQueryUrl(rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS)
128 << text << QStringList({modified, rating, tag, tagR}) << true << false;
129
130 QTest::newRow("searchTerms+filename")
131 << balooQueryUrl(rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS)
132 << text << QStringList({modified, rating, tag, tagR}) << false << true;
133
134 QTest::newRow("allTerms")
135 << balooQueryUrl(text + " " + filename + " " + rating + " AND " + modified + " AND " + tag)
136 << text + " " + filename << QStringList({modified, rating, tag}) << true << true;
137
138 QTest::newRow("allTerms/space")
139 << balooQueryUrl(textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS)
140 << textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
141 }
142
143 /**
144 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
145 * properly handled by the searchbox, and only "user" or filename terms are added to the
146 * text bar of the searchbox.
147 */
148 void DolphinSearchBoxTest::testBalooSearchParsing()
149 {
150 QFETCH(QUrl, searchUrl);
151 QFETCH(QString, expectedText);
152 QFETCH(QStringList, expectedTerms);
153 QFETCH(bool, hasContent);
154 QFETCH(bool, hasFileName);
155
156 const DolphinQuery query = DolphinQuery::fromSearchUrl(searchUrl);
157
158 // Checkt that the URL is supported
159 QVERIFY(DolphinQuery::supportsScheme(searchUrl.scheme()));
160
161 // Check for parsed text (would be displayed on the input search bar)
162 QCOMPARE(query.text(), expectedText);
163
164 // Check for parsed search terms (would be displayed by the facetsWidget)
165 QStringList searchTerms = query.searchTerms();
166 searchTerms.sort();
167
168 QCOMPARE(searchTerms.count(), expectedTerms.count());
169 for (int i = 0; i < expectedTerms.count(); i++) {
170 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
171 }
172
173 // Check for filename and content detection
174 QCOMPARE(query.hasContentSearch(), hasContent);
175 QCOMPARE(query.hasFileName(), hasFileName);
176 }
177
178 QTEST_MAIN(DolphinSearchBoxTest)
179
180 #include "dolphinquerytest.moc"