]> Pileus Git - ~andy/gtk/blob - gtk/tests/treeview.c
let the close button reflect the page_complete status of SUMMARY pages
[~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 int
154 main (int    argc,
155       char **argv)
156 {
157   gtk_test_init (&argc, &argv, NULL);
158
159   g_test_add_func ("/TreeView/cursor/bug-546005", test_bug_546005);
160   g_test_add_func ("/TreeView/cursor/bug-539377", test_bug_539377);
161   g_test_add_func ("/TreeView/cursor/select-collapsed_row",
162                    test_select_collapsed_row);
163
164   return g_test_run ();
165 }