]> Pileus Git - ~andy/gtk/blob - tests/a11y/children.c
Update expected dump output for color choooser
[~andy/gtk] / tests / a11y / children.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * Author:
5  *      Matthias Clasen <mclasen@redhat.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #define GDK_DISABLE_DEPRECATION_WARNINGS
22
23 #include <gtk/gtk.h>
24 #include <string.h>
25
26 static void
27 test_scrolled_window_child_count (void)
28 {
29   GtkWidget *sw;
30   AtkObject *accessible;
31
32   sw = gtk_scrolled_window_new (NULL, NULL);
33   g_object_ref_sink (sw);
34   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
35                                   GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS);
36   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), gtk_label_new ("Bla"));
37
38   accessible = gtk_widget_get_accessible (sw);
39   g_assert_cmpint (atk_object_get_n_accessible_children (accessible), ==, 3);
40
41   g_object_unref (sw);
42 }
43
44 typedef struct {
45   gint count;
46   gint index;
47   gint n_children;
48   gpointer parent;
49 } SignalData;
50
51 static void
52 children_changed (AtkObject  *accessible,
53                   guint       index,
54                   gpointer    child,
55                   SignalData *data)
56 {
57   data->count++;
58   data->index = index;
59   data->n_children = atk_object_get_n_accessible_children (accessible);
60 }
61
62 static void
63 add_child (GtkWidget *container,
64            GtkWidget *child)
65 {
66   if (GTK_IS_SCROLLED_WINDOW (container))
67     gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (container), child);
68   else
69     gtk_container_add (GTK_CONTAINER (container), child);
70 }
71
72 static void
73 remove_child (GtkWidget *container,
74               GtkWidget *child)
75 {
76   if (GTK_IS_SCROLLED_WINDOW (container))
77     {
78       if (gtk_widget_get_parent (child) != container)
79         child = gtk_widget_get_parent (child);
80     }
81
82   gtk_container_remove (GTK_CONTAINER (container), child);
83 }
84
85 static void
86 parent_notify (AtkObject *obj, GParamSpec *pspec, SignalData *data)
87 {
88   data->count++;
89   data->parent = atk_object_get_parent (obj);
90 }
91
92 static void
93 test_add_remove (GtkWidget *widget)
94 {
95   AtkObject *accessible;
96   AtkObject *child_accessible;
97   SignalData add_data;
98   SignalData remove_data;
99   SignalData parent_data[3];
100   GtkWidget *child[3];
101   gint i, j;
102   gint step_children;
103
104   accessible = gtk_widget_get_accessible (widget);
105
106   add_data.count = 0;
107   remove_data.count = 0;
108   g_signal_connect (accessible, "children_changed::add",
109                     G_CALLBACK (children_changed), &add_data);
110   g_signal_connect (accessible, "children_changed::remove",
111                     G_CALLBACK (children_changed), &remove_data);
112
113   step_children = atk_object_get_n_accessible_children (accessible);
114
115   for (i = 0; i < 3; i++)
116     {
117       if (gtk_container_child_type (GTK_CONTAINER (widget)) == G_TYPE_NONE)
118         break;
119
120       child[i] = gtk_label_new ("bla");
121       parent_data[i].count = 0;
122       child_accessible = gtk_widget_get_accessible (child[i]);
123       g_signal_connect (child_accessible, "notify::accessible-parent",
124                         G_CALLBACK (parent_notify), &(parent_data[i]));
125       add_child (widget, child[i]);
126
127       g_assert_cmpint (add_data.count, ==, i + 1);
128       g_assert_cmpint (add_data.n_children, ==, step_children + i + 1);
129       g_assert_cmpint (remove_data.count, ==, 0);
130       g_assert_cmpint (parent_data[i].count, ==, 1);
131       if (GTK_IS_SCROLLED_WINDOW (widget) ||
132           GTK_IS_NOTEBOOK (widget))
133         g_assert (atk_object_get_parent (ATK_OBJECT (parent_data[i].parent)) == accessible);
134       else
135         g_assert (parent_data[i].parent == accessible);
136     }
137   for (j = 0 ; j < i; j++)
138     {
139       remove_child (widget, child[j]);
140       g_assert_cmpint (add_data.count, ==, i);
141       g_assert_cmpint (remove_data.count, ==, j + 1);
142       g_assert_cmpint (remove_data.n_children, ==, step_children + i - j - 1);
143       if (parent_data[j].count == 2)
144         g_assert (parent_data[j].parent == NULL);
145       else
146         {
147           AtkStateSet *set;
148           set = atk_object_ref_state_set (ATK_OBJECT (parent_data[j].parent));
149           g_assert (atk_state_set_contains_state (set, ATK_STATE_DEFUNCT));
150           g_object_unref (set);
151         }
152     }
153
154   g_signal_handlers_disconnect_by_func (accessible, G_CALLBACK (children_changed), &add_data);
155   g_signal_handlers_disconnect_by_func (accessible, G_CALLBACK (children_changed), &remove_data);
156 }
157
158 static void
159 add_child_test (const gchar      *prefix,
160                 GTestFixtureFunc  test_func,
161                 GtkWidget        *widget)
162 {
163   gchar *path;
164
165   path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
166   g_test_add_vtable (path,
167                      0,
168                      g_object_ref (widget),
169                      0,
170                      (GTestFixtureFunc) test_func,
171                      (GTestFixtureFunc) g_object_unref);
172   g_free (path);
173 }
174
175 static void
176 add_child_tests (GtkWidget *widget)
177 {
178   g_object_ref_sink (widget);
179   add_child_test ("/child/add-remove", (GTestFixtureFunc)test_add_remove, widget);
180   g_object_unref (widget);
181 }
182
183 int
184 main (int argc, char *argv[])
185 {
186   gtk_test_init (&argc, &argv, NULL);
187
188   g_test_add_func ("/scrolledwindow/child-count", test_scrolled_window_child_count);
189
190   add_child_tests (gtk_scrolled_window_new (NULL, NULL));
191   add_child_tests (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
192   add_child_tests (gtk_paned_new (GTK_ORIENTATION_HORIZONTAL));
193   add_child_tests (gtk_grid_new ());
194   add_child_tests (gtk_event_box_new ());
195   add_child_tests (gtk_window_new (GTK_WINDOW_TOPLEVEL));
196   add_child_tests (gtk_assistant_new ());
197   add_child_tests (gtk_frame_new ("frame"));
198   add_child_tests (gtk_expander_new ("expander"));
199   add_child_tests (gtk_table_new (2, 2, FALSE));
200   add_child_tests (gtk_text_view_new ());
201   add_child_tests (gtk_tree_view_new ());
202 #if 0
203   /* gail doesn't handle non-label children in these */
204   add_child_tests (gtk_button_new ());
205   add_child_tests (gtk_statusbar_new ());
206 #endif
207   add_child_tests (gtk_notebook_new ());
208
209   return g_test_run ();
210 }
211