Merge lp:~kamstrup/libunity/sorted-filters into lp:libunity

Proposed by Mikkel Kamstrup Erlandsen on 2011-09-12
Status: Merged
Approved by: Neil J. Patel on 2011-09-12
Approved revision: 83
Merged at revision: 79
Proposed branch: lp:~kamstrup/libunity/sorted-filters
Merge into: lp:libunity
Diff against target: 229 lines (+170/-3)
4 files modified
src/unity-lens-filters.vala (+33/-3)
test/vala/Makefile.am (+1/-0)
test/vala/test-filters.vala (+132/-0)
test/vala/test-vala.vala (+4/-0)
To merge this branch: bzr merge lp:~kamstrup/libunity/sorted-filters
Reviewer Review Type Date Requested Status
Neil J. Patel (community) 2011-09-12 Approve on 2011-09-12
Review via email: mp+75014@code.launchpad.net

Description of the change

Adds API to sort Unity.FilterOptions in various ways inside a Unity.OptionsFilter. The default behaviour remains the same as before this change. The apps lens will use it to sort the filter options according to translation (and locale rules)

To post a comment you must log in.
Neil J. Patel (njpatel) wrote :

Approved!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/unity-lens-filters.vala'
2--- src/unity-lens-filters.vala 2011-09-08 12:08:56 +0000
3+++ src/unity-lens-filters.vala 2011-09-12 14:51:24 +0000
4@@ -34,7 +34,6 @@
5 FILTERING
6 }
7
8-
9 /*
10 * Base class for filters. If you are implmenting a scope, checking first that
11 * a filter is "filtering" is a good way to avoid unnecessary extra logic in
12@@ -103,14 +102,45 @@
13 public class OptionsFilter : Filter
14 {
15 public List<FilterOption> options;
16+ public SortType sort_type { get; set; default = SortType.MANUAL; }
17+
18+ public enum SortType
19+ {
20+ MANUAL,
21+ DISPLAY_NAME,
22+ ID
23+ }
24
25 public FilterOption add_option (string id, string display_name, Icon? icon_hint=null)
26 {
27 var option = new FilterOption(id, display_name, icon_hint);
28- options.append (option);
29-
30+
31+ switch (sort_type)
32+ {
33+ case SortType.DISPLAY_NAME:
34+ options.insert_sorted (option, sort_by_display_name);
35+ break;
36+ case SortType.ID:
37+ options.insert_sorted (option, sort_by_id);
38+ break;
39+ case SortType.MANUAL :
40+ default:
41+ options.append (option);
42+ break;
43+ }
44+
45 return option;
46 }
47+
48+ private static int sort_by_display_name (FilterOption f1, FilterOption f2)
49+ {
50+ return f1.display_name.collate (f2.display_name);
51+ }
52+
53+ private static int sort_by_id (FilterOption f1, FilterOption f2)
54+ {
55+ return f1.id.collate (f2.id);
56+ }
57
58 public FilterOption? get_option (string id)
59 {
60
61=== modified file 'test/vala/Makefile.am'
62--- test/vala/Makefile.am 2011-08-04 09:29:04 +0000
63+++ test/vala/Makefile.am 2011-09-12 14:51:24 +0000
64@@ -32,6 +32,7 @@
65
66 test_vala_SOURCES = \
67 test-appinfo-manager.vala \
68+ test-filters.vala \
69 test-io.vala \
70 test-launcher.vala \
71 test-sound-menu.vala \
72
73=== added file 'test/vala/test-filters.vala'
74--- test/vala/test-filters.vala 1970-01-01 00:00:00 +0000
75+++ test/vala/test-filters.vala 2011-09-12 14:51:24 +0000
76@@ -0,0 +1,132 @@
77+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
78+/*
79+ * Copyright (C) 2011 Canonical Ltd
80+ *
81+ * This program is free software: you can redistribute it and/or modify
82+ * it under the terms of the GNU General Public License version 3 as
83+ * published by the Free Software Foundation.
84+ *
85+ * This program is distributed in the hope that it will be useful,
86+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
87+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88+ * GNU General Public License for more details.
89+ *
90+ * You should have received a copy of the GNU General Public License
91+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
92+ *
93+ * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
94+ *
95+ */
96+using Unity;
97+
98+namespace Unity.Test
99+{
100+ public class FilterSuite
101+ {
102+ public FilterSuite ()
103+ {
104+ GLib.Test.add_data_func ("/Unit/Filters/OptionsFilter/Empty",
105+ FilterSuite.test_options_filter_empty);
106+ GLib.Test.add_data_func ("/Unit/Filters/OptionsFilter/SortManual",
107+ FilterSuite.test_options_filter_sort_manual);
108+ GLib.Test.add_data_func ("/Unit/Filters/OptionsFilter/SortDisplayName",
109+ FilterSuite.test_options_filter_sort_display_name);
110+ GLib.Test.add_data_func ("/Unit/Filters/OptionsFilter/SortId",
111+ FilterSuite.test_options_filter_sort_id);
112+ }
113+
114+ internal static void test_options_filter_empty ()
115+ {
116+ var filter = new OptionsFilter ();
117+ assert (filter.options.length () == 0);
118+ }
119+
120+ private static int find_filter_offset_by_id (OptionsFilter filter, string id)
121+ {
122+ int i = 0;
123+ foreach (var f in filter.options)
124+ {
125+ if (f.id == id)
126+ return i;
127+ i++;
128+ }
129+ return -1;
130+ }
131+
132+ internal static void test_options_filter_sort_manual ()
133+ {
134+ var filter = new OptionsFilter ();
135+ assert (filter.sort_type == OptionsFilter.SortType.MANUAL);
136+
137+ filter.add_option ("accessories", "Accessories");
138+ filter.add_option ("bccessories", "Bccessories");
139+ filter.add_option ("xenomorphs", "Xenomorphic Creatures");
140+
141+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
142+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
143+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 2);
144+
145+ filter.add_option ("aardvaark", "Weird Animal");
146+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
147+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
148+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 2);
149+ assert (find_filter_offset_by_id (filter, "aardvaark") == 3);
150+ }
151+
152+ internal static void test_options_filter_sort_display_name ()
153+ {
154+ var filter = new OptionsFilter ();
155+ filter.sort_type = OptionsFilter.SortType.DISPLAY_NAME;
156+ assert (filter.sort_type == OptionsFilter.SortType.DISPLAY_NAME);
157+
158+ filter.add_option ("accessories", "Accessories");
159+ filter.add_option ("bccessories", "Bccessories");
160+ filter.add_option ("xenomorphs", "Xenomorphic Creatures");
161+
162+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
163+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
164+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 2);
165+
166+ filter.add_option ("weirdanimal", "Aardvaark");
167+ assert (find_filter_offset_by_id (filter, "weirdanimal") == 0);
168+ assert (find_filter_offset_by_id (filter, "accessories") == 1);
169+ assert (find_filter_offset_by_id (filter, "bccessories") == 2);
170+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 3);
171+
172+ filter.add_option ("zooanimal", "Zebra");
173+ assert (find_filter_offset_by_id (filter, "weirdanimal") == 0);
174+ assert (find_filter_offset_by_id (filter, "accessories") == 1);
175+ assert (find_filter_offset_by_id (filter, "bccessories") == 2);
176+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 3);
177+ assert (find_filter_offset_by_id (filter, "zooanimal") == 4);
178+ }
179+
180+ internal static void test_options_filter_sort_id ()
181+ {
182+ var filter = new OptionsFilter ();
183+ filter.sort_type = OptionsFilter.SortType.ID;
184+ assert (filter.sort_type == OptionsFilter.SortType.ID);
185+
186+ filter.add_option ("accessories", "Accessories");
187+ filter.add_option ("bccessories", "Bccessories");
188+ filter.add_option ("xenomorphs", "Xenomorphic Creatures");
189+
190+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
191+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
192+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 2);
193+
194+ filter.add_option ("weirdanimal", "Aardvaark");
195+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
196+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
197+ assert (find_filter_offset_by_id (filter, "weirdanimal") == 2);
198+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 3);
199+
200+ filter.add_option ("zooanimal", "Zebra");
201+ assert (find_filter_offset_by_id (filter, "accessories") == 0);
202+ assert (find_filter_offset_by_id (filter, "bccessories") == 1);
203+ assert (find_filter_offset_by_id (filter, "weirdanimal") == 2);
204+ assert (find_filter_offset_by_id (filter, "xenomorphs") == 3);
205+ assert (find_filter_offset_by_id (filter, "zooanimal") == 4);
206+ }
207+ }
208+}
209
210=== modified file 'test/vala/test-vala.vala'
211--- test/vala/test-vala.vala 2011-09-08 15:10:30 +0000
212+++ test/vala/test-vala.vala 2011-09-12 14:51:24 +0000
213@@ -27,6 +27,7 @@
214 AppInfoManagerSuite appinfo_manager;
215 LauncherSuite launcher;
216 SoundMenuSuite sound_menu_test_suite;
217+ FilterSuite filter_suite;
218
219 Environment.set_variable ("XDG_DATA_HOME", Config.TESTDIR+"/data", true);
220 Test.init (ref args);
221@@ -39,6 +40,9 @@
222 appinfo_manager = new AppInfoManagerSuite ();
223
224 launcher = new LauncherSuite ();
225+
226+ /* Lens Filters */
227+ filter_suite = new FilterSuite ();
228
229 Test.run ();
230

Subscribers

People subscribed via source and target branches