]> Pileus Git - ~andy/gtk/blob - gtk/tests/treeview.c
rbtree: Remove needless tests
[~andy/gtk] / gtk / tests / treeview.c
1 /* Basic GtkTreeView unit tests.
2  * Copyright (C) 2009  Kristian Rietveld  <kris@gtk.org>
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21
22 static void
23 test_bug_546005 (void)
24 {
25   GtkTreeIter iter;
26   GtkTreePath *path;
27   GtkTreePath *cursor_path;
28   GtkListStore *list_store;
29   GtkWidget *view;
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
72 static void
73 test_bug_539377 (void)
74 {
75   GtkWidget *view;
76   GtkTreePath *path;
77   GtkListStore *list_store;
78
79   /* Test provided by Bjorn Lindqvist */
80
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);
87
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));
92
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);
97 }
98
99 static void
100 test_select_collapsed_row (void)
101 {
102   GtkTreeIter child, parent;
103   GtkTreePath *path;
104   GtkTreeStore *tree_store;
105   GtkTreeSelection *selection;
106   GtkWidget *view;
107
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));
111
112   gtk_tree_store_insert_with_values (tree_store, &parent, NULL, 0,
113                                      0, "Parent",
114                                      -1);
115
116   gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
117                                      0, "Child",
118                                      -1);
119   gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
120                                      0, "Child",
121                                      -1);
122
123
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);
127
128   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
129
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);
133
134   /* Nothing should be selected at this point. */
135   g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 0);
136
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);
141
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));
145
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);
149
150   gtk_tree_path_free (path);
151 }
152
153 static gboolean
154 test_row_separator_height_func (GtkTreeModel *model,
155                                 GtkTreeIter  *iter,
156                                 gpointer      data)
157 {
158   gboolean ret = FALSE;
159   GtkTreePath *path;
160
161   path = gtk_tree_model_get_path (model, iter);
162   if (gtk_tree_path_get_indices (path)[0] == 2)
163     ret = TRUE;
164   gtk_tree_path_free (path);
165
166   return ret;
167 }
168
169 static void
170 test_row_separator_height (void)
171 {
172   int focus_pad, separator_height, height;
173   gboolean wide_separators;
174   GtkTreeIter iter;
175   GtkTreePath *path;
176   GtkListStore *store;
177   GtkWidget *window;
178   GtkWidget *tree_view;
179   GdkRectangle rect, cell_rect;
180
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);
187
188   window = gtk_offscreen_window_new ();
189
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,
193                                         NULL,
194                                         NULL);
195
196   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
197                                                0,
198                                                "Test",
199                                                gtk_cell_renderer_text_new (),
200                                                "text", 0,
201                                                NULL);
202
203   gtk_container_add (GTK_CONTAINER (window), tree_view);
204   gtk_widget_show_all (window);
205
206
207   path = gtk_tree_path_new_from_indices (2, -1);
208   gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
209                                      path, NULL, &rect);
210   gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree_view),
211                                path, NULL, &cell_rect);
212   gtk_tree_path_free (path);
213
214   gtk_widget_style_get (tree_view,
215                         "focus-padding", &focus_pad,
216                         "wide-separators", &wide_separators,
217                         "separator-height", &separator_height,
218                         NULL);
219
220   if (wide_separators)
221     height = separator_height + 2 * focus_pad;
222   else
223     height = 2 + 2 * focus_pad;
224
225   g_assert_cmpint (rect.height, ==, height);
226   g_assert_cmpint (cell_rect.height, ==, height);
227
228   gtk_widget_destroy (tree_view);
229 }
230
231 int
232 main (int    argc,
233       char **argv)
234 {
235   gtk_test_init (&argc, &argv, NULL);
236
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);
243
244   return g_test_run ();
245 }