]> Pileus Git - ~andy/gtk/blob - tests/a11y/children.c
stylecontext: Do invalidation on first resize container
[~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 #undef GTK_DISABLE_DEPRECATED
23
24 #include <gtk/gtk.h>
25 #include <string.h>
26
27 typedef struct
28 {
29   GtkWidget *widget;
30   gpointer child[3];
31 } STATE;
32
33 static void
34 test_scrolled_window_child_count (void)
35 {
36   GtkWidget *sw;
37   AtkObject *accessible;
38
39   sw = gtk_scrolled_window_new (NULL, NULL);
40   g_object_ref_sink (sw);
41   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
42                                   GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS);
43   gtk_container_add (GTK_CONTAINER (sw), gtk_label_new ("Bla"));
44
45   accessible = gtk_widget_get_accessible (sw);
46   g_assert_cmpint (atk_object_get_n_accessible_children (accessible), ==, 3);
47
48   g_object_unref (sw);
49 }
50
51 typedef struct {
52   gint count;
53   gint index;
54   gint n_children;
55   gpointer parent;
56 } SignalData;
57
58 static void
59 children_changed (AtkObject  *accessible,
60                   guint       index,
61                   gpointer    child,
62                   SignalData *data)
63 {
64   data->count++;
65   data->index = index;
66   data->n_children = atk_object_get_n_accessible_children (accessible);
67 }
68
69 static void
70 remove_child (STATE *state,
71               gint i)
72 {
73   GtkWidget *child;
74
75   if (GTK_IS_ENTRY (state->widget))
76     {
77       switch (i)
78         {
79         case 0:
80           gtk_entry_set_icon_from_gicon (GTK_ENTRY (state->widget),
81                                          GTK_ENTRY_ICON_PRIMARY,
82                                          NULL);
83         return;
84         case 1:
85           gtk_entry_set_icon_from_gicon (GTK_ENTRY (state->widget),
86                                          GTK_ENTRY_ICON_SECONDARY,
87                                          NULL);
88         return;
89         default:
90           return;
91         }
92     }
93
94   child = state->child [i];
95   if (GTK_IS_SCROLLED_WINDOW (state->widget))
96     {
97       if (gtk_widget_get_parent (child) != state->widget)
98         child = gtk_widget_get_parent (child);
99     }
100
101   gtk_container_remove (GTK_CONTAINER (state->widget), child);
102 }
103
104 static void
105 parent_notify (AtkObject *obj, GParamSpec *pspec, SignalData *data)
106 {
107   data->count++;
108   data->parent = atk_object_get_parent (obj);
109 }
110
111 gboolean
112 do_create_child (STATE *state, gint i)
113 {
114   if (GTK_IS_ENTRY (state->widget))
115     {
116       switch (i)
117         {
118         case 0:
119           gtk_entry_set_icon_from_stock (GTK_ENTRY (state->widget),
120                                          GTK_ENTRY_ICON_PRIMARY,
121                                          GTK_STOCK_CAPS_LOCK_WARNING);
122         return TRUE;
123         case 1:
124           gtk_entry_set_icon_from_stock (GTK_ENTRY (state->widget),
125                                          GTK_ENTRY_ICON_SECONDARY,
126                                          GTK_STOCK_CLEAR);
127         return TRUE;
128         default:
129           return FALSE;
130         }
131     }
132   else if (gtk_container_child_type (GTK_CONTAINER (state->widget)) == G_TYPE_NONE)
133     return FALSE;
134
135   state->child[i] = gtk_label_new ("bla");
136   return TRUE;
137 }
138
139 static void
140 test_add_remove (GtkWidget *widget)
141 {
142   AtkObject *accessible;
143   AtkObject *child_accessible;
144   SignalData add_data;
145   SignalData remove_data;
146   SignalData parent_data[3];
147   STATE state;
148   gint i, j;
149   gint step_children;
150
151   state.widget = widget;
152   accessible = gtk_widget_get_accessible (widget);
153
154   add_data.count = 0;
155   remove_data.count = 0;
156   g_signal_connect (accessible, "children_changed::add",
157                     G_CALLBACK (children_changed), &add_data);
158   g_signal_connect (accessible, "children_changed::remove",
159                     G_CALLBACK (children_changed), &remove_data);
160
161   step_children = atk_object_get_n_accessible_children (accessible);
162
163   for (i = 0; i < 3; i++)
164     {
165       if (!do_create_child (&state, i))
166         break;
167       if (!GTK_IS_ENTRY (widget))
168         {
169           parent_data[i].count = 0;
170           child_accessible = gtk_widget_get_accessible (state.child[i]);
171           g_signal_connect (child_accessible, "notify::accessible-parent",
172                             G_CALLBACK (parent_notify), &(parent_data[i]));
173           gtk_container_add (GTK_CONTAINER (widget), state.child[i]);
174         }
175       else
176         child_accessible = atk_object_ref_accessible_child (accessible, i);
177
178       g_assert_cmpint (add_data.count, ==, i + 1);
179       g_assert_cmpint (add_data.n_children, ==, step_children + i + 1);
180       g_assert_cmpint (remove_data.count, ==, 0);
181       if (!GTK_IS_ENTRY (widget))
182         g_assert_cmpint (parent_data[i].count, ==, 1);
183       if (GTK_IS_SCROLLED_WINDOW (widget) ||
184           GTK_IS_NOTEBOOK (widget))
185         g_assert (atk_object_get_parent (ATK_OBJECT (parent_data[i].parent)) == accessible);
186       else if (GTK_IS_ENTRY (widget))
187         g_assert (atk_object_get_parent (child_accessible) == accessible);
188       else
189         g_assert (parent_data[i].parent == accessible);
190
191       if (GTK_IS_ENTRY (widget))
192         g_object_unref (child_accessible);
193     }
194   for (j = 0 ; j < i; j++)
195     {
196       remove_child (&state, j);
197       g_assert_cmpint (add_data.count, ==, i);
198       g_assert_cmpint (remove_data.count, ==, j + 1);
199       g_assert_cmpint (remove_data.n_children, ==, step_children + i - j - 1);
200       if (parent_data[j].count == 2)
201         g_assert (parent_data[j].parent == NULL);
202       else if (!GTK_IS_ENTRY (widget))
203         {
204           AtkStateSet *set;
205           set = atk_object_ref_state_set (ATK_OBJECT (parent_data[j].parent));
206           g_assert (atk_state_set_contains_state (set, ATK_STATE_DEFUNCT));
207           g_object_unref (set);
208         }
209     }
210
211   g_signal_handlers_disconnect_by_func (accessible, G_CALLBACK (children_changed), &add_data);
212   g_signal_handlers_disconnect_by_func (accessible, G_CALLBACK (children_changed), &remove_data);
213 }
214
215 static void
216 add_child_test (const gchar      *prefix,
217                 GTestFixtureFunc  test_func,
218                 GtkWidget        *widget)
219 {
220   gchar *path;
221
222   path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
223   g_test_add_vtable (path,
224                      0,
225                      g_object_ref (widget),
226                      0,
227                      (GTestFixtureFunc) test_func,
228                      (GTestFixtureFunc) g_object_unref);
229   g_free (path);
230 }
231
232 static void
233 add_child_tests (GtkWidget *widget)
234 {
235   g_object_ref_sink (widget);
236   add_child_test ("/child/add-remove", (GTestFixtureFunc)test_add_remove, widget);
237   g_object_unref (widget);
238 }
239
240 int
241 main (int argc, char *argv[])
242 {
243   gtk_test_init (&argc, &argv, NULL);
244
245   g_test_add_func ("/scrolledwindow/child-count", test_scrolled_window_child_count);
246
247   add_child_tests (gtk_scrolled_window_new (NULL, NULL));
248   add_child_tests (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
249   add_child_tests (gtk_paned_new (GTK_ORIENTATION_HORIZONTAL));
250   add_child_tests (gtk_grid_new ());
251   add_child_tests (gtk_event_box_new ());
252   add_child_tests (gtk_window_new (GTK_WINDOW_TOPLEVEL));
253   add_child_tests (gtk_assistant_new ());
254   add_child_tests (gtk_frame_new ("frame"));
255   add_child_tests (gtk_expander_new ("expander"));
256   add_child_tests (gtk_table_new (2, 2, FALSE));
257   add_child_tests (gtk_text_view_new ());
258   add_child_tests (gtk_tree_view_new ());
259 #if 0
260   /* gail doesn't handle non-label children in these */
261   add_child_tests (gtk_button_new ());
262   add_child_tests (gtk_statusbar_new ());
263 #endif
264   add_child_tests (gtk_notebook_new ());
265   add_child_tests (gtk_entry_new ());
266
267   return g_test_run ();
268 }
269