1 /* Basic GtkTreeView unit tests.
2 * Copyright (C) 2009 Kristian Rietveld <kris@gtk.org>
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.
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.
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/>.
21 test_bug_546005 (void)
25 GtkTreePath *cursor_path;
26 GtkListStore *list_store;
29 /* Tests provided by Bjorn Lindqvist, Paul Pogonyshev */
30 view = gtk_tree_view_new ();
32 /* Invalid path on tree view without model */
33 path = gtk_tree_path_new_from_indices (1, -1);
34 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
36 gtk_tree_path_free (path);
38 list_store = gtk_list_store_new (1, G_TYPE_STRING);
39 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
40 GTK_TREE_MODEL (list_store));
42 /* Invalid path on tree view with empty model */
43 path = gtk_tree_path_new_from_indices (1, -1);
44 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
46 gtk_tree_path_free (path);
49 gtk_list_store_insert_with_values (list_store, &iter, 0,
53 path = gtk_tree_path_new_from_indices (0, -1);
54 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
57 gtk_tree_view_get_cursor (GTK_TREE_VIEW (view), &cursor_path, NULL);
58 //gtk_assert_cmptreepath (cursor_path, ==, path);
60 gtk_tree_path_free (path);
61 gtk_tree_path_free (cursor_path);
63 /* Invalid path on tree view with model */
64 path = gtk_tree_path_new_from_indices (1, -1);
65 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
67 gtk_tree_path_free (path);
71 test_bug_539377 (void)
75 GtkListStore *list_store;
77 /* Test provided by Bjorn Lindqvist */
79 /* Non-realized view, no model */
80 view = gtk_tree_view_new ();
81 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
82 NULL, NULL, NULL) == FALSE);
83 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
84 &path, NULL) == FALSE);
86 /* Non-realized view, with model */
87 list_store = gtk_list_store_new (1, G_TYPE_STRING);
88 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
89 GTK_TREE_MODEL (list_store));
91 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
92 NULL, NULL, NULL) == FALSE);
93 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
94 &path, NULL) == FALSE);
98 test_select_collapsed_row (void)
100 GtkTreeIter child, parent;
102 GtkTreeStore *tree_store;
103 GtkTreeSelection *selection;
106 /* Reported by Michael Natterer */
107 tree_store = gtk_tree_store_new (1, G_TYPE_STRING);
108 view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (tree_store));
110 gtk_tree_store_insert_with_values (tree_store, &parent, NULL, 0,
114 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
117 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
122 /* Try to select a child path. */
123 path = gtk_tree_path_new_from_indices (0, 1, -1);
124 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
126 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
128 /* Check that the parent is not selected. */
129 gtk_tree_path_up (path);
130 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == FALSE);
132 /* Nothing should be selected at this point. */
133 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 0);
135 /* Check that selection really still works. */
136 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
137 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == TRUE);
138 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 1);
140 /* Expand and select child node now. */
141 gtk_tree_path_append_index (path, 1);
142 gtk_tree_view_expand_all (GTK_TREE_VIEW (view));
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);
148 gtk_tree_path_free (path);
152 test_row_separator_height_func (GtkTreeModel *model,
156 gboolean ret = FALSE;
159 path = gtk_tree_model_get_path (model, iter);
160 if (gtk_tree_path_get_indices (path)[0] == 2)
162 gtk_tree_path_free (path);
168 test_row_separator_height (void)
170 int focus_pad, separator_height, height;
171 gboolean wide_separators;
176 GtkWidget *tree_view;
177 GdkRectangle rect, cell_rect;
179 store = gtk_list_store_new (1, G_TYPE_STRING);
180 gtk_list_store_insert_with_values (store, &iter, 0, 0, "Row content", -1);
181 gtk_list_store_insert_with_values (store, &iter, 1, 0, "Row content", -1);
182 gtk_list_store_insert_with_values (store, &iter, 2, 0, "Row content", -1);
183 gtk_list_store_insert_with_values (store, &iter, 3, 0, "Row content", -1);
184 gtk_list_store_insert_with_values (store, &iter, 4, 0, "Row content", -1);
186 window = gtk_offscreen_window_new ();
188 tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
189 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (tree_view),
190 test_row_separator_height_func,
194 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
197 gtk_cell_renderer_text_new (),
201 gtk_container_add (GTK_CONTAINER (window), tree_view);
202 gtk_widget_show_all (window);
205 path = gtk_tree_path_new_from_indices (2, -1);
206 gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
208 gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree_view),
209 path, NULL, &cell_rect);
210 gtk_tree_path_free (path);
212 gtk_widget_style_get (tree_view,
213 "focus-padding", &focus_pad,
214 "wide-separators", &wide_separators,
215 "separator-height", &separator_height,
219 height = separator_height + 2 * focus_pad;
221 height = 2 + 2 * focus_pad;
223 g_assert_cmpint (rect.height, ==, height);
224 g_assert_cmpint (cell_rect.height, ==, height);
226 gtk_widget_destroy (tree_view);
233 gtk_test_init (&argc, &argv, NULL);
235 g_test_add_func ("/TreeView/cursor/bug-546005", test_bug_546005);
236 g_test_add_func ("/TreeView/cursor/bug-539377", test_bug_539377);
237 g_test_add_func ("/TreeView/cursor/select-collapsed_row",
238 test_select_collapsed_row);
239 g_test_add_func ("/TreeView/sizing/row-separator-height",
240 test_row_separator_height);
242 return g_test_run ();