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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 test_bug_546005 (void)
27 GtkTreePath *cursor_path;
28 GtkListStore *list_store;
31 /* Tests provided by Bjorn Lindqvist, Paul Pogonyshev */
32 view = gtk_tree_view_new ();
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,
38 gtk_tree_path_free (path);
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));
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,
48 gtk_tree_path_free (path);
51 gtk_list_store_insert_with_values (list_store, &iter, 0,
55 path = gtk_tree_path_new_from_indices (0, -1);
56 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
59 gtk_tree_view_get_cursor (GTK_TREE_VIEW (view), &cursor_path, NULL);
60 //gtk_assert_cmptreepath (cursor_path, ==, path);
62 gtk_tree_path_free (path);
63 gtk_tree_path_free (cursor_path);
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,
69 gtk_tree_path_free (path);
73 test_bug_539377 (void)
77 GtkListStore *list_store;
79 /* Test provided by Bjorn Lindqvist */
81 /* Non-realized view, no model */
82 view = gtk_tree_view_new ();
83 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
84 NULL, NULL, NULL) == FALSE);
85 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
86 &path, NULL) == FALSE);
88 /* Non-realized view, with model */
89 list_store = gtk_list_store_new (1, G_TYPE_STRING);
90 gtk_tree_view_set_model (GTK_TREE_VIEW (view),
91 GTK_TREE_MODEL (list_store));
93 g_assert (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view), 10, 10, &path,
94 NULL, NULL, NULL) == FALSE);
95 g_assert (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view), 10, 10,
96 &path, NULL) == FALSE);
100 test_select_collapsed_row (void)
102 GtkTreeIter child, parent;
104 GtkTreeStore *tree_store;
105 GtkTreeSelection *selection;
108 /* Reported by Michael Natterer */
109 tree_store = gtk_tree_store_new (1, G_TYPE_STRING);
110 view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (tree_store));
112 gtk_tree_store_insert_with_values (tree_store, &parent, NULL, 0,
116 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
119 gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
124 /* Try to select a child path. */
125 path = gtk_tree_path_new_from_indices (0, 1, -1);
126 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
128 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
130 /* Check that the parent is not selected. */
131 gtk_tree_path_up (path);
132 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == FALSE);
134 /* Nothing should be selected at this point. */
135 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 0);
137 /* Check that selection really still works. */
138 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
139 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == TRUE);
140 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 1);
142 /* Expand and select child node now. */
143 gtk_tree_path_append_index (path, 1);
144 gtk_tree_view_expand_all (GTK_TREE_VIEW (view));
146 gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE);
147 g_return_if_fail (gtk_tree_selection_path_is_selected (selection, path) == TRUE);
148 g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 1);
150 gtk_tree_path_free (path);
154 test_row_separator_height_func (GtkTreeModel *model,
158 gboolean ret = FALSE;
161 path = gtk_tree_model_get_path (model, iter);
162 if (gtk_tree_path_get_indices (path)[0] == 2)
164 gtk_tree_path_free (path);
170 test_row_separator_height (void)
172 int focus_pad, separator_height, height;
173 gboolean wide_separators;
178 GtkWidget *tree_view;
179 GdkRectangle rect, cell_rect;
181 store = gtk_list_store_new (1, G_TYPE_STRING);
182 gtk_list_store_insert_with_values (store, &iter, 0, 0, "Row content", -1);
183 gtk_list_store_insert_with_values (store, &iter, 1, 0, "Row content", -1);
184 gtk_list_store_insert_with_values (store, &iter, 2, 0, "Row content", -1);
185 gtk_list_store_insert_with_values (store, &iter, 3, 0, "Row content", -1);
186 gtk_list_store_insert_with_values (store, &iter, 4, 0, "Row content", -1);
188 window = gtk_offscreen_window_new ();
190 tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
191 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (tree_view),
192 test_row_separator_height_func,
196 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
199 gtk_cell_renderer_text_new (),
203 gtk_container_add (GTK_CONTAINER (window), tree_view);
204 gtk_widget_show_all (window);
207 path = gtk_tree_path_new_from_indices (2, -1);
208 gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
210 gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree_view),
211 path, NULL, &cell_rect);
212 gtk_tree_path_free (path);
214 gtk_widget_style_get (tree_view,
215 "focus-padding", &focus_pad,
216 "wide-separators", &wide_separators,
217 "separator-height", &separator_height,
221 height = separator_height + 2 * focus_pad;
223 height = 2 + 2 * focus_pad;
225 g_assert_cmpint (rect.height, ==, height);
226 g_assert_cmpint (cell_rect.height, ==, height);
228 gtk_widget_destroy (tree_view);
235 gtk_test_init (&argc, &argv, NULL);
237 g_test_add_func ("/TreeView/cursor/bug-546005", test_bug_546005);
238 g_test_add_func ("/TreeView/cursor/bug-539377", test_bug_539377);
239 g_test_add_func ("/TreeView/cursor/select-collapsed_row",
240 test_select_collapsed_row);
241 g_test_add_func ("/TreeView/sizing/row-separator-height",
242 test_row_separator_height);
244 return g_test_run ();