Merge lp:~larryprice/libertine-scope/implement-search into lp:libertine-scope

Proposed by Larry Price
Status: Merged
Approved by: Stephen M. Webb
Approved revision: 32
Merged at revision: 33
Proposed branch: lp:~larryprice/libertine-scope/implement-search
Merge into: lp:libertine-scope
Diff against target: 222 lines (+153/-6)
4 files modified
libertine-scope/query.cpp (+10/-5)
po/libertine-scope.pot (+1/-1)
tests/CMakeLists.txt (+3/-0)
tests/test_query.cpp (+139/-0)
To merge this branch: bzr merge lp:~larryprice/libertine-scope/implement-search
Reviewer Review Type Date Requested Status
Stephen M. Webb (community) Approve
Review via email: mp+292047@code.launchpad.net

Commit message

Filter app launchers based on a user-input regular expression.

Description of the change

Filter app launchers based on a user-input regular expression.

To post a comment you must log in.
Revision history for this message
Stephen M. Webb (bregma) wrote :

Works!!!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libertine-scope/query.cpp'
2--- libertine-scope/query.cpp 2016-01-19 21:10:09 +0000
3+++ libertine-scope/query.cpp 2016-04-15 20:09:21 +0000
4@@ -22,6 +22,9 @@
5 #include <unity/scopes/CategoryRenderer.h>
6 #include <unity/scopes/QueryBase.h>
7 #include <unity/scopes/SearchReply.h>
8+#include <QString>
9+#include <QRegExp>
10+
11
12 namespace usc = unity::scopes;
13
14@@ -82,26 +85,28 @@
15 void Query::
16 run(usc::SearchReplyProxy const& reply)
17 {
18- usc::CannedQuery const& query(usc::SearchQueryBase::query());
19- std::string query_string = query.query_string();
20-
21+ QRegExp re(QString::fromStdString(query().query_string()), Qt::CaseInsensitive);
22 Libertine::UPtr libertine = libertine_factory_();
23+
24 for (auto const& container: libertine->get_container_list())
25 {
26 auto category = reply->register_category(container->id(),
27 container->name(),
28 "Application",
29 usc::CategoryRenderer(CATEGORY_APPS_DISPLAY));
30-
31 for (auto const& app: container->app_launchers())
32 {
33- if (app.no_display())
34+ if (app.no_display() ||
35+ !(re.isEmpty() || QString::fromStdString(app.name()).contains(re)))
36+ {
37 continue;
38+ }
39
40 usc::CategorisedResult result(category);
41 result.set_title(app.name());
42 result.set_art(app.icon());
43 result.set_uri(app_uri(*container, app));
44+
45 if (!reply->push(result))
46 {
47 break;
48
49=== modified file 'po/libertine-scope.pot'
50--- po/libertine-scope.pot 2016-02-27 03:54:52 +0000
51+++ po/libertine-scope.pot 2016-04-15 20:09:21 +0000
52@@ -8,7 +8,7 @@
53 msgstr ""
54 "Project-Id-Version: PACKAGE VERSION\n"
55 "Report-Msgid-Bugs-To: \n"
56-"POT-Creation-Date: 2016-02-26 22:50-0500\n"
57+"POT-Creation-Date: 2016-04-15 14:36-0400\n"
58 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
59 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
60 "Language-Team: LANGUAGE <LL@li.org>\n"
61
62=== modified file 'tests/CMakeLists.txt'
63--- tests/CMakeLists.txt 2016-01-19 00:16:56 +0000
64+++ tests/CMakeLists.txt 2016-04-15 20:09:21 +0000
65@@ -7,6 +7,7 @@
66 fake_container.cpp
67 fake_libertine.cpp
68 test_scope.cpp
69+ test_query.cpp
70 )
71
72 target_link_libraries(libertine_scope_tests
73@@ -19,3 +20,5 @@
74 add_test(test_scope
75 libertine_scope_tests
76 )
77+
78+add_test(test_query libertine_scope_tests)
79
80=== added file 'tests/test_query.cpp'
81--- tests/test_query.cpp 1970-01-01 00:00:00 +0000
82+++ tests/test_query.cpp 2016-04-15 20:09:21 +0000
83@@ -0,0 +1,139 @@
84+/*
85+ * Copyright 2016 Canonical Ltd.
86+ *
87+ * This program is free software: you can redistribute it and/or modify it under
88+ * the terms of the GNU General Public License, version 3, as published by the
89+ * Free Software Foundation.
90+ *
91+ * This program is distributed in the hope that it will be useful,
92+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
93+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94+ * GNU General Public License for more details.
95+ *
96+ * You should have received a copy of the GNU General Public License
97+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
98+ */
99+
100+#include "libertine-scope/query.h"
101+#include "tests/fake_libertine.h"
102+#include <unity/scopes/SearchMetadata.h>
103+#include <unity/scopes/CannedQuery.h>
104+#include <unity/scopes/SearchReplyProxyFwd.h>
105+#include <unity/scopes/testing/MockSearchReply.h>
106+#include <unity/scopes/Category.h>
107+#include <vector>
108+#include <memory>
109+#include <gtest/gtest.h>
110+#include <gmock/gmock.h>
111+
112+namespace
113+{
114+const std::string LIBERTINE_OUTPUT_WITH_APPS = R"(
115+ {
116+ "name": "fake-container",
117+ "app_launchers": [{
118+ "name": "LibreOffice",
119+ "no_display": false,
120+ "desktop_file_name": "/link/to/libreoffice.desktop"
121+ }, {
122+ "name": "Linux",
123+ "no_display": true,
124+ "desktop_file_name": "/link/to/linux.desktop"
125+ }, {
126+ "name": "Library",
127+ "no_display": false,
128+ "desktop_file_name": "/link/to/library.desktop"
129+ }]
130+ }
131+)";
132+
133+
134+class FakeCategory : public unity::scopes::Category
135+{
136+public:
137+ FakeCategory(std::string const& id, std::string const& title,
138+ std::string const& icon, unity::scopes::CategoryRenderer const& renderer) :
139+ unity::scopes::Category(id, title, icon, renderer)
140+ {
141+ }
142+
143+};
144+
145+
146+MATCHER_P3(ResultPropertiesMatch, title, art, uri, "")
147+{
148+ return arg.contains("title") && arg.contains("art") && arg.contains("uri") &&
149+ arg["title"] == unity::scopes::Variant(title) &&
150+ arg["art"] == unity::scopes::Variant(art) &&
151+ arg["uri"] == unity::scopes::Variant(uri);
152+}
153+
154+
155+class TestQueryFixture : public ::testing::Test
156+{
157+public:
158+ TestQueryFixture()
159+ : metadata("en_US", "phone")
160+ , canned_query("libertine-scope")
161+ , reply()
162+ , proxy(&reply, [](unity::scopes::SearchReply*) {})
163+ , category(std::make_shared<FakeCategory>("fake", "fake-container", "Application", unity::scopes::CategoryRenderer()))
164+ {
165+ }
166+
167+ void expect_registry()
168+ {
169+ EXPECT_CALL(reply, register_category("fake", "fake-container", "Application", testing::_)).WillOnce(testing::Return(category));
170+ }
171+
172+ void expect_push(std::string title, std::string art, std::string appId, bool success = true)
173+ {
174+ EXPECT_CALL(reply, push(testing::Matcher<unity::scopes::CategorisedResult const&>(ResultPropertiesMatch(title, art, appId)))).WillOnce(testing::Return(success));
175+ }
176+
177+ unity::scopes::SearchMetadata metadata;
178+ unity::scopes::CannedQuery canned_query;
179+ testing::NiceMock<unity::scopes::testing::MockSearchReply> reply;
180+ unity::scopes::SearchReplyProxy proxy;
181+ std::shared_ptr<FakeCategory> category;
182+};
183+
184+
185+TEST_F(TestQueryFixture, returnsAllDisplayableAppsWithoutFilters)
186+{
187+ expect_registry();
188+ expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0");
189+ expect_push("Library", "", "appid://fake/library/0.0");
190+
191+ Query query(canned_query, metadata, []() {
192+ return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
193+ });
194+ query.run(proxy);
195+}
196+
197+
198+TEST_F(TestQueryFixture, returnsRegularExpressionFilteredListOfApps)
199+{
200+ expect_registry();
201+ expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0");
202+ canned_query.set_query_string("li.*office");
203+
204+ Query query(canned_query, metadata, []() {
205+ return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
206+ });
207+ query.run(proxy);
208+}
209+
210+
211+TEST_F(TestQueryFixture, haltsFurtherPushesAfterFailedPush)
212+{
213+ expect_registry();
214+ expect_push("LibreOffice", "", "appid://fake/libreoffice/0.0", false);
215+
216+ Query query(canned_query, metadata, []() {
217+ return FakeLibertine::make_fake(LIBERTINE_OUTPUT_WITH_APPS);
218+ });
219+ query.run(proxy);
220+}
221+
222+} // anonymous namespace

Subscribers

People subscribed via source and target branches