Debian Codegraph @

testsuite/gtk/treeview.c

raw
1 /* Basic GtkTreeView unit tests.
2 * Copyright (C) 2009 Kristian Rietveld <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <gtk/gtk.h>
19
20 static void
21 test_bug_546005 (void)
22 {
23 GtkTreeIter iter;
24 GtkTreePath *path;
25 GtkTreePath *cursor_path;
26 GtkListStore *list_store;
27 GtkWidget *view;
28
29 g_test_bug ("546005");
30
31 /* Tests provided by Bjorn Lindqvist, Paul Pogonyshev */
32 view = gtk_tree_view_new ();
33
34 /* Invalid path on tree view without model */
35 path = gtk_tree_path_new_from_indices (1, -1);
36 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
37 NULL, FALSE);
38 gtk_tree_path_free (path);
39
40 list_store = gtk_list_store_new (1, G_TYPE_STRING);
41 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
42 GTK_TREE_MODEL (list_store));
43
44 /* Invalid path on tree view with empty model */
45 path = gtk_tree_path_new_from_indices (1, -1);
46 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
47 NULL, FALSE);
48 gtk_tree_path_free (path);
49
50 /* Valid path */
51 gtk_list_store_insert_with_values (list_store, &iter, 0,
52 0, "hi",
53 -1);
54
55 path = gtk_tree_path_new_from_indices (0, -1);
56 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
57 NULL, FALSE);
58
59 gtk_tree_view_get_cursor (GTK_TREE_VIEW (view), &cursor_path, NULL);
60 //gtk_assert_cmptreepath (cursor_path, ==, path);
61
62 gtk_tree_path_free (path);
63 gtk_tree_path_free (cursor_path);
64
65 /* Invalid path on tree view with model */
66 path = gtk_tree_path_new_from_indices (1, -1);
67 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
68 NULL, FALSE);
69 gtk_tree_path_free (path);
70
71 gtk_widget_destroy (view);
72 }
73
74 static void
75 test_bug_539377 (void)
76 {
77 GtkWidget *view;
78 GtkTreePath *path;
79 GtkListStore *list_store;
80
81 g_test_bug ("539377");
82
83 /* Test provided by Bjorn Lindqvist */
84
85 /* Non-realized view, no model */
86 view = gtk_tree_view_new ();
87 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
88 NULL, NULL, NULL) == FALSE);
89 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
90 &path, NULL) == FALSE);
91
92 /* Non-realized view, with model */
93 list_store = gtk_list_store_new (1, G_TYPE_STRING);
94 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
95 GTK_TREE_MODEL (list_store));
96
97 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
98 NULL, NULL, NULL) == FALSE);
99 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
100 &path, NULL) == FALSE);
101
102 gtk_widget_destroy (view);
103 }
104
105 static void
106 test_select_collapsed_row (void)
107 {
108 GtkTreeIter child, parent;
109 GtkTreePath *path;
110 GtkTreeStore *tree_store;
111 GtkTreeSelection *selection;
112 GtkWidget *view;
113
114 /* Reported by Michael Natterer */
115 tree_store = gtk_tree_store_new (1, G_TYPE_STRING);
116 view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (tree_store));
117
118 gtk_tree_store_insert_with_values (tree_store, &parent, NULL, 0,
119 0, "Parent",
120 -1);
121
122 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
123 0, "Child",
124 -1);
125 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
126 0, "Child",
127 -1);
128
129
130 /* Try to select a child path. */
131 path = gtk_tree_path_new_from_indices (0, 1, -1);
132 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
133
134 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
135
136 /* Check that the parent is not selected. */
137 gtk_tree_path_up (path);
138 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == FALSE);
139
140 /* Nothing should be selected at this point. */
141 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 0);
142
143 /* Check that selection really still works. */
144 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
145 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == TRUE);
146 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 1);
147
148 /* Expand and select child node now. */
149 gtk_tree_path_append_index (path, 1);
150 gtk_tree_view_expand_all (GTK_TREE_VIEW (view));
151
152 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
153 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == TRUE);
154 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 1);
155
156 gtk_tree_path_free (path);
157
158 gtk_widget_destroy (view);
159 }
160
161 static gboolean
162 test_row_separator_height_func (GtkTreeModel *model,
163 GtkTreeIter *iter,
164 gpointer data)
165 {
166 gboolean ret = FALSE;
167 GtkTreePath *path;
168
169 path = gtk_tree_model_get_path (model, iter);
170 if (gtk_tree_path_get_indices (path)[0] == 2)
171 ret = TRUE;
172 gtk_tree_path_free (path);
173
174 return ret;
175 }
176
177 static void
178 test_row_separator_height (void)
179 {
180 int focus_pad, separator_height, height;
181 gboolean wide_separators;
182 GtkTreeIter iter;
183 GtkTreePath *path;
184 GtkListStore *store;
185 GtkWidget *window;
186 GtkWidget *tree_view;
187 GdkRectangle rect = { 0, }, cell_rect = { 0, };
188
189 store = gtk_list_store_new (1, G_TYPE_STRING);
190 gtk_list_store_insert_with_values (store, &iter, 0, 0, "Row content", -1);
191 gtk_list_store_insert_with_values (store, &iter, 1, 0, "Row content", -1);
192 gtk_list_store_insert_with_values (store, &iter, 2, 0, "Row content", -1);
193 gtk_list_store_insert_with_values (store, &iter, 3, 0, "Row content", -1);
194 gtk_list_store_insert_with_values (store, &iter, 4, 0, "Row content", -1);
195
196 window = gtk_offscreen_window_new ();
197
198 tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
199 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (tree_view),
200 test_row_separator_height_func,
201 NULL,
202 NULL);
203
204 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
205 0,
206 "Test",
207 gtk_cell_renderer_text_new (),
208 "text", 0,
209 NULL);
210
211 gtk_container_add (GTK_CONTAINER (window), tree_view);
212 gtk_widget_show_all (window);
213
214 gtk_test_widget_wait_for_draw (window);
215
216 path = gtk_tree_path_new_from_indices (2, -1);
217 gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
218 path, NULL, &rect);
219 gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree_view),
220 path, NULL, &cell_rect);
221 gtk_tree_path_free (path);
222
223 gtk_widget_style_get (tree_view,
224 "focus-padding", &focus_pad,
225 "wide-separators", &wide_separators,
226 "separator-height", &separator_height,
227 NULL);
228
229 if (wide_separators)
230 height = separator_height;
231 else
232 height = 2;
233
234 g_assert_cmpint (rect.height, ==, height);
235 g_assert_cmpint (cell_rect.height, ==, height);
236
237 gtk_widget_destroy (tree_view);
238 }
239
240 static void
241 test_selection_count (void)
242 {
243 GtkTreePath *path;
244 GtkListStore *list_store;
245 GtkTreeSelection *selection;
246 GtkWidget *view;
247
248 g_test_bug ("702957");
249
250 list_store = gtk_list_store_new (1, G_TYPE_STRING);
251 view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
252
253 gtk_list_store_insert_with_values (list_store, NULL, 0, 0, "One", -1);
254 gtk_list_store_insert_with_values (list_store, NULL, 1, 0, "Two", -1);
255 gtk_list_store_insert_with_values (list_store, NULL, 2, 0, "Tree", -1);
256
257 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
258 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
259
260 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
261
262 path = gtk_tree_path_new_from_indices (0, -1);
263 gtk_tree_selection_select_path (selection, path);
264 gtk_tree_path_free (path);
265
266 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 1);
267
268 path = gtk_tree_path_new_from_indices (2, -1);
269 gtk_tree_selection_select_path (selection, path);
270 gtk_tree_path_free (path);
271
272 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 2);
273
274 path = gtk_tree_path_new_from_indices (2, -1);
275 gtk_tree_selection_select_path (selection, path);
276 gtk_tree_path_free (path);
277
278 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 2);
279
280 path = gtk_tree_path_new_from_indices (1, -1);
281 gtk_tree_selection_select_path (selection, path);
282 gtk_tree_path_free (path);
283
284 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 3);
285
286 gtk_tree_selection_unselect_all (selection);
287
288 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
289
290 gtk_widget_destroy (view);
291 }
292
293 static void
294 abort_cb (GtkTreeModel *model,
295 GtkTreePath *path,
296 GtkTreeIter *iter,
297 gpointer data)
298 {
299 g_assert_not_reached ();
300 }
301
302 static void
303 test_selection_empty (void)
304 {
305 GtkTreePath *path;
306 GtkListStore *list_store;
307 GtkTreeSelection *selection;
308 GtkWidget *view;
309 GtkTreeIter iter;
310
311 g_test_bug ("712760");
312
313 list_store = gtk_list_store_new (1, G_TYPE_STRING);
314 view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
315 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
316
317 g_assert_false (gtk_tree_selection_get_selected (selection, NULL, &iter));
318 gtk_tree_selection_selected_foreach (selection, abort_cb, NULL);
319 g_assert_null (gtk_tree_selection_get_selected_rows (selection, NULL));
320 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
321
322 path = gtk_tree_path_new_from_indices (0, -1);
323
324 gtk_tree_selection_select_path (selection, path);
325 gtk_tree_selection_unselect_path (selection, path);
326 g_assert_false (gtk_tree_selection_path_is_selected (selection, path));
327
328 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
329
330 gtk_tree_selection_select_all (selection);
331 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
332
333 gtk_tree_selection_unselect_all (selection);
334 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
335
336 gtk_tree_selection_select_range (selection, path, path);
337 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
338
339 gtk_tree_selection_unselect_range (selection, path, path);
340 g_assert_cmpint (gtk_tree_selection_count_selected_rows (selection), ==, 0);
341
342 gtk_tree_path_free (path);
343
344 gtk_widget_destroy (view);
345 }
346
347 int
348 main (int argc,
349 char **argv)
350 {
351 gtk_test_init (&argc, &argv, NULL);
352 g_test_bug_base ("http://bugzilla.gnome.org/");
353
354 g_test_add_func ("/TreeView/cursor/bug-546005", test_bug_546005);
355 g_test_add_func ("/TreeView/cursor/bug-539377", test_bug_539377);
356 g_test_add_func ("/TreeView/cursor/select-collapsed_row",
357 test_select_collapsed_row);
358 g_test_add_func ("/TreeView/sizing/row-separator-height",
359 test_row_separator_height);
360 g_test_add_func ("/TreeView/selection/count", test_selection_count);
361 g_test_add_func ("/TreeView/selection/empty", test_selection_empty);
362
363 return g_test_run ();
364 }