]> Pileus Git - ~andy/gtk/blob - modules/other/gail/tests/testbutton.c
gail/tests: Use const instead G_CONST_RETURN
[~andy/gtk] / modules / other / gail / tests / testbutton.c
1 #include <gtk/gtk.h>
2 #include "testlib.h"
3
4 /*
5  * This module is used to test the accessible implementation for buttons
6  *
7  * 1) It verifies that ATK_STATE_ARMED is set when a button is pressed
8  * To check this click on the button whose name is specified in the
9  * environment variable TEST_ACCESSIBLE_NAME or "button box" if the
10  * environment variable is not set.
11  *
12  * 2) If the environment variable TEST_ACCESSIBLE_AUTO is set the program
13  * will execute the action defined for a GailButton once.
14  *
15  * 3) Change an inconsistent toggle button to be consistent and vice versa.
16  *
17  * Note that currently this code needs to be changed manually to test
18  * different actions.
19  */
20
21 static void _create_event_watcher (void);
22 static void _check_object (AtkObject *obj);
23 static void button_pressed_handler (GtkButton *button);
24 static void _print_states (AtkObject *obj);
25 static void _print_button_image_info(AtkObject *obj);
26 static gint _do_button_action (gpointer data);
27 static gint _toggle_inconsistent (gpointer data);
28 static gint _finish_button_action (gpointer data);
29
30 #define NUM_VALID_ROLES 4
31
32 static void 
33 _check_object (AtkObject *obj)
34 {
35   AtkRole role;
36   static gboolean first_time = TRUE;
37
38   role = atk_object_get_role (obj);
39   if (role == ATK_ROLE_FRAME)
40   /*
41    * Find the specified button in the window
42    */
43   {
44     AtkRole valid_roles[NUM_VALID_ROLES];
45     const char *name;
46     AtkObject *atk_button;
47     GtkWidget *widget;
48
49     valid_roles[0] = ATK_ROLE_PUSH_BUTTON;
50     valid_roles[1] = ATK_ROLE_TOGGLE_BUTTON;
51     valid_roles[2] = ATK_ROLE_CHECK_BOX;
52     valid_roles[3] = ATK_ROLE_RADIO_BUTTON;
53
54     name = g_getenv ("TEST_ACCESSIBLE_NAME");
55     if (name == NULL)
56       name = "button box";
57     atk_button = find_object_by_accessible_name_and_role (obj, name,
58                      valid_roles, NUM_VALID_ROLES);
59
60     if (atk_button == NULL)
61     {
62       g_print ("Object not found for %s\n", name);
63       return;
64     }
65     g_assert (GTK_IS_ACCESSIBLE (atk_button));
66     widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_button));
67     g_assert (GTK_IS_BUTTON (widget));
68     g_signal_connect (widget,
69                       "pressed",
70                       G_CALLBACK (button_pressed_handler),
71                       NULL);
72     if (GTK_IS_TOGGLE_BUTTON (widget))
73     {
74       _toggle_inconsistent (GTK_TOGGLE_BUTTON (widget));
75     }
76     if (first_time)
77       first_time = FALSE;
78     else
79       return;
80
81     if (g_getenv ("TEST_ACCESSIBLE_AUTO"))
82       {
83         g_idle_add (_do_button_action, atk_button);
84       }
85   }
86 }
87
88 static gint _toggle_inconsistent (gpointer data)
89 {
90   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (data);
91
92   if (gtk_toggle_button_get_inconsistent (toggle_button))
93   {
94     gtk_toggle_button_set_inconsistent (toggle_button, FALSE);
95   }
96   else
97   {
98     gtk_toggle_button_set_inconsistent (toggle_button, TRUE);
99   }
100   return FALSE;
101
102
103 static gint _do_button_action (gpointer data)
104 {
105   AtkObject *obj = ATK_OBJECT (data);
106
107   atk_action_do_action (ATK_ACTION (obj), 2);
108
109   g_timeout_add (5000, _finish_button_action, obj);
110   return FALSE;
111 }
112
113 static gint _finish_button_action (gpointer data)
114 {
115 #if 0
116   AtkObject *obj = ATK_OBJECT (data);
117
118   atk_action_do_action (ATK_ACTION (obj), 0);
119 #endif
120
121   return FALSE;
122 }
123
124 static void
125 button_pressed_handler (GtkButton *button)
126 {
127   AtkObject *obj;
128
129   obj = gtk_widget_get_accessible (GTK_WIDGET (button));
130   _print_states (obj);
131   _print_button_image_info (obj);
132
133   if (GTK_IS_TOGGLE_BUTTON (button))
134     {
135       g_idle_add (_toggle_inconsistent, GTK_TOGGLE_BUTTON (button));
136     }
137 }
138
139 static void 
140 _print_states (AtkObject *obj)
141 {
142   AtkStateSet *state_set;
143   gint i;
144
145   state_set = atk_object_ref_state_set (obj);
146
147   g_print ("*** Start states ***\n");
148   for (i = 0; i < 64; i++)
149   {
150      AtkStateType one_state;
151      const gchar *name;
152
153      if (atk_state_set_contains_state (state_set, i))
154      {
155        one_state = i;
156
157        name = atk_state_type_get_name (one_state);
158
159        if (name)
160          g_print("%s\n", name);
161      }
162   }
163   g_object_unref (state_set);
164   g_print ("*** End states ***\n");
165 }
166
167 static void 
168 _print_button_image_info(AtkObject *obj) {
169
170   gint height, width;
171   const gchar *desc;
172
173   height = width = 0;
174
175   if(!ATK_IS_IMAGE(obj)) 
176         return;
177
178   g_print("*** Start Button Image Info ***\n");
179   desc = atk_image_get_image_description(ATK_IMAGE(obj));
180   g_print ("atk_image_get_image_desc returns : %s\n", desc ? desc : "<NULL>");
181   atk_image_get_image_size(ATK_IMAGE(obj), &height ,&width);
182   g_print("atk_image_get_image_size returns: height %d width %d\n",height,width);
183   if(atk_image_set_image_description(ATK_IMAGE(obj), "New image Description")){
184         desc = atk_image_get_image_description(ATK_IMAGE(obj));
185         g_print ("atk_image_get_image_desc now returns : %s\n",desc ?desc:"<NULL>");
186   }
187   g_print("*** End Button Image Info ***\n");
188
189
190 }
191
192 static void
193 _create_event_watcher (void)
194 {
195   atk_add_focus_tracker (_check_object);
196 }
197
198 int
199 gtk_module_init(gint argc, char* argv[])
200 {
201   g_print("testbutton Module loaded\n");
202
203   _create_event_watcher();
204
205   return 0;
206 }