]> Pileus Git - ~andy/gtk/blob - gtk/a11y/tests/testselection.c
gail: Move from modules/other/gail to gtk/a11y
[~andy/gtk] / gtk / a11y / tests / testselection.c
1 #include <string.h>
2 #include <atk/atk.h>
3 #include <gtk/gtk.h>
4
5 /*
6  * This module tests the selection interface on menu items.
7  * To use this module run the test program testgtk and use the menus 
8  * option.
9  */
10 static void _do_selection (AtkObject *obj);
11 static gint _finish_selection (gpointer data);
12 static AtkObject* _find_object (AtkObject* obj, AtkRole role);
13 static void _print_type (AtkObject *obj);
14
15 static AtkObject* 
16 _find_object (AtkObject *obj, 
17               AtkRole   role)
18 {
19   /*
20    * Find the first object which is a descendant of the specified object
21    * which matches the specified role.
22    *
23    * This function returns a reference to the AtkObject which should be
24    * removed when finished with the object.
25    */
26   gint i;
27   gint n_children;
28   AtkObject *child;
29
30   n_children = atk_object_get_n_accessible_children (obj);
31   for (i = 0; i < n_children; i++) 
32   {
33     AtkObject* found_obj;
34
35     child = atk_object_ref_accessible_child (obj, i);
36     if (atk_object_get_role (child) == role)
37     {
38       return child;
39     }
40     found_obj = _find_object (child, role);
41     g_object_unref (child);
42     if (found_obj)
43     {
44       return found_obj;
45     }
46   }
47   return NULL;
48 }
49
50 static void _print_type (AtkObject *obj)
51 {
52   const gchar * typename = NULL;
53   const gchar * name = NULL;
54   AtkRole role;
55
56   if (GTK_IS_ACCESSIBLE (obj))
57   {
58     GtkWidget* widget = NULL;
59
60     widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
61     typename = g_type_name (G_OBJECT_TYPE (widget));
62     g_print ("Widget type name: %s\n", typename ? typename : "NULL");
63   }
64   typename = g_type_name (G_OBJECT_TYPE (obj));
65   g_print ("Accessible type name: %s\n", typename ? typename : "NULL");
66   name = atk_object_get_name (obj);
67   g_print("Accessible Name: %s\n", (name) ? name : "NULL");
68   role = atk_object_get_role(obj);
69   g_print ("Accessible Role: %d\n", role);
70 }
71
72 static void 
73 _do_selection (AtkObject *obj)
74 {
75   gint i;
76   AtkObject *selected;
77   AtkRole role;
78   AtkObject *selection_obj;
79
80   role = atk_object_get_role (obj);
81
82   if ((role == ATK_ROLE_FRAME) &&
83       (strcmp (atk_object_get_name (obj), "menus") == 0))
84   {
85     selection_obj = _find_object (obj, ATK_ROLE_MENU_BAR);
86     if (selection_obj)
87     {
88       g_object_unref (selection_obj);
89     }
90   }
91   else if (role == ATK_ROLE_COMBO_BOX)
92   {
93     selection_obj = obj;
94   }
95   else
96     return;
97
98   g_print ("*** Start do_selection ***\n");
99
100   
101   if (!selection_obj)
102   {
103     g_print ("no selection_obj\n");
104     return;
105   }
106
107   i = atk_selection_get_selection_count (ATK_SELECTION (selection_obj));
108   if (i != 0)
109   {
110     for (i = 0; i < atk_object_get_n_accessible_children (selection_obj); i++)
111     {
112       if (atk_selection_is_child_selected (ATK_SELECTION (selection_obj), i))
113       {
114         g_print ("%d child selected\n", i);
115       }
116       else
117       {
118         g_print ("%d child not selected\n", i);
119       }
120     } 
121   } 
122   /*
123    * Should not be able to select all items on a menu bar
124    */
125   atk_selection_select_all_selection (ATK_SELECTION (selection_obj));
126   i = atk_selection_get_selection_count (ATK_SELECTION (selection_obj));
127   if ( i != 0)
128   {
129     g_print ("Unexpected selection count: %d, expected 0\n", i);
130     return;
131   }
132   /*
133    * There should not be any items selected
134    */
135   selected = atk_selection_ref_selection (ATK_SELECTION (selection_obj), 0);
136   if ( selected != NULL)
137   {
138     g_print ("Unexpected selection: %d, expected 0\n", i);
139   }
140   atk_selection_add_selection (ATK_SELECTION (selection_obj), 1);
141   g_timeout_add (2000, _finish_selection, selection_obj);
142   g_print ("*** End _do_selection ***\n");
143
144
145 static gint _finish_selection (gpointer data)
146 {
147   AtkObject *obj = ATK_OBJECT (data);
148   AtkObject *selected;
149   gint      i;
150   gboolean is_selected;
151
152   g_print ("*** Start Finish selection ***\n");
153
154   /*
155    * If being run for for menus, at this point menu item foo should be 
156    * selected which means that its submenu should be visible.
157    */
158   i = atk_selection_get_selection_count (ATK_SELECTION (obj));
159   if (i != 1)
160   {
161     g_print ("Unexpected selection count: %d, expected 1\n", i);
162     return FALSE;
163   }
164   selected = atk_selection_ref_selection (ATK_SELECTION (obj), 0);
165   g_return_val_if_fail (selected != NULL, FALSE);
166   g_print ("*** Selected Item ***\n");
167   _print_type (selected);
168   g_object_unref (selected);
169   is_selected = atk_selection_is_child_selected (ATK_SELECTION (obj), 1);
170   g_return_val_if_fail (is_selected, FALSE);
171   is_selected = atk_selection_is_child_selected (ATK_SELECTION (obj), 0);
172   g_return_val_if_fail (!is_selected, FALSE);
173   selected = atk_selection_ref_selection (ATK_SELECTION (obj), 1);
174   g_return_val_if_fail (selected == NULL, FALSE);
175   atk_selection_remove_selection (ATK_SELECTION (obj), 0);
176   i = atk_selection_get_selection_count (ATK_SELECTION (obj));
177   g_return_val_if_fail (i == 0, FALSE);
178   selected = atk_selection_ref_selection (ATK_SELECTION (obj), 0);
179   g_return_val_if_fail (selected == NULL, FALSE);
180   g_print ("*** End Finish selection ***\n");
181   return FALSE;
182 }
183
184 static void
185 _create_event_watcher (void)
186 {
187   atk_add_focus_tracker (_do_selection);
188 }
189
190 int
191 gtk_module_init(gint argc, char* argv[])
192 {
193   g_print("testselection Module loaded\n");
194
195   _create_event_watcher();
196
197   return 0;
198 }