]> Pileus Git - ~andy/gtk/blob - gtk/tests/treeview.c
filechooserbutton: When the combo box changes, set the *file*, not the current folder
[~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, 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   /* Tests provided by Bjorn Lindqvist, Paul Pogonyshev */
30   view = gtk_tree_view_new ();
31
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,
35                             NULL, FALSE);
36   gtk_tree_path_free (path);
37
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));
41
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,
45                             NULL, FALSE);
46   gtk_tree_path_free (path);
47
48   /* Valid path */
49   gtk_list_store_insert_with_values (list_store, &iter, 0,
50                                      0, "hi",
51                                      -1);
52
53   path = gtk_tree_path_new_from_indices (0, -1);
54   gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path,
55                             NULL, FALSE);
56
57   gtk_tree_view_get_cursor (GTK_TREE_VIEW (view), &cursor_path, NULL);
58   //gtk_assert_cmptreepath (cursor_path, ==, path);
59
60   gtk_tree_path_free (path);
61   gtk_tree_path_free (cursor_path);
62
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,
66                             NULL, FALSE);
67   gtk_tree_path_free (path);
68 }
69
70 static void
71 test_bug_539377 (void)
72 {
73   GtkWidget *view;
74   GtkTreePath *path;
75   GtkListStore *list_store;
76
77   /* Test provided by Bjorn Lindqvist */
78
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);
85
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));
90
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);
95 }
96
97 static void
98 test_select_collapsed_row (void)
99 {
100   GtkTreeIter child, parent;
101   GtkTreePath *path;
102   GtkTreeStore *tree_store;
103   GtkTreeSelection *selection;
104   GtkWidget *view;
105
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));
109
110   gtk_tree_store_insert_with_values (tree_store, &parent, NULL, 0,
111                                      0, "Parent",
112                                      -1);
113
114   gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
115                                      0, "Child",
116                                      -1);
117   gtk_tree_store_insert_with_values (tree_store, &child, &parent, 0,
118                                      0, "Child",
119                                      -1);
120
121
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);
125
126   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
127
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);
131
132   /* Nothing should be selected at this point. */
133   g_return_if_fail (gtk_tree_selection_count_selected_rows (selection) == 0);
134
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);
139
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));
143
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   gtk_tree_path_free (path);
149 }
150
151 static gboolean
152 test_row_separator_height_func (GtkTreeModel *model,
153                                 GtkTreeIter  *iter,
154                                 gpointer      data)
155 {
156   gboolean ret = FALSE;
157   GtkTreePath *path;
158
159   path = gtk_tree_model_get_path (model, iter);
160   if (gtk_tree_path_get_indices (path)[0] == 2)
161     ret = TRUE;
162   gtk_tree_path_free (path);
163
164   return ret;
165 }
166
167 static void
168 test_row_separator_height (void)
169 {
170   int focus_pad, separator_height, height;
171   gboolean wide_separators;
172   GtkTreeIter iter;
173   GtkTreePath *path;
174   GtkListStore *store;
175   GtkWidget *window;
176   GtkWidget *tree_view;
177   GdkRectangle rect, cell_rect;
178
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);
185
186   window = gtk_offscreen_window_new ();
187
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,
191                                         NULL,
192                                         NULL);
193
194   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
195                                                0,
196                                                "Test",
197                                                gtk_cell_renderer_text_new (),
198                                                "text", 0,
199                                                NULL);
200
201   gtk_container_add (GTK_CONTAINER (window), tree_view);
202   gtk_widget_show_all (window);
203
204
205   path = gtk_tree_path_new_from_indices (2, -1);
206   gtk_tree_view_get_background_area (GTK_TREE_VIEW (tree_view),
207                                      path, NULL, &rect);
208   gtk_tree_view_get_cell_area (GTK_TREE_VIEW (tree_view),
209                                path, NULL, &cell_rect);
210   gtk_tree_path_free (path);
211
212   gtk_widget_style_get (tree_view,
213                         "focus-padding", &focus_pad,
214                         "wide-separators", &wide_separators,
215                         "separator-height", &separator_height,
216                         NULL);
217
218   if (wide_separators)
219     height = separator_height + 2 * focus_pad;
220   else
221     height = 2 + 2 * focus_pad;
222
223   g_assert_cmpint (rect.height, ==, height);
224   g_assert_cmpint (cell_rect.height, ==, height);
225
226   gtk_widget_destroy (tree_view);
227 }
228
229 int
230 main (int    argc,
231       char **argv)
232 {
233   gtk_test_init (&argc, &argv, NULL);
234
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);
241
242   return g_test_run ();
243 }