2 * test-simple.c: A set of simple regression tests
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Ximian, Inc.
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.
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.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * ******** Do not copy this code as an example *********
32 #include <libbonobo.h>
34 static void validate_accessible (Accessible *accessible,
36 gboolean recurse_down);
38 #define WINDOW_MAGIC 0x123456a
39 #define TEST_STRING_A "A test string"
40 #define TEST_STRING_B "Another test string"
42 static int print_tree_depth = 0;
43 static gboolean print_tree = FALSE;
51 focus_me (GtkWidget *widget)
53 AtkObject *aobject = atk_implementor_ref_accessible (
54 ATK_IMPLEMENTOR (widget));
56 /* Force a focus event - even if the WM focused
57 * us before our at-bridge's idle handler registered
59 if (!GTK_WIDGET_HAS_FOCUS (widget))
60 gtk_widget_grab_focus (widget);
61 /* else: FIXME - gtk_widget_grab_focus should send a notify */
62 atk_focus_tracker_notify (aobject);
64 g_object_unref (G_OBJECT (aobject));
70 create_test_window (void)
72 TestWindow *win = g_new0 (TestWindow, 1);
73 GtkWidget *widget, *vbox;
75 win->magic = WINDOW_MAGIC;
76 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
78 gtk_widget_show (win->window);
80 vbox = gtk_vbox_new (0, 0);
81 gtk_container_add (GTK_CONTAINER (win->window), vbox);
82 gtk_widget_show (vbox);
84 widget = gtk_entry_new ();
85 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
86 gtk_container_add (GTK_CONTAINER (vbox), widget);
87 gtk_widget_show (widget);
89 g_idle_add ((GSourceFunc) focus_me, win->window);
95 test_window_destroy (TestWindow *win)
97 gtk_widget_destroy (win->window);
106 fprintf (stderr, "Testing roles...\n");
107 for (i = -1; i < 1000; i++)
108 g_assert (AccessibleRole_getName (i) != NULL);
110 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
111 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
112 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
113 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
120 Accessible *application;
122 fprintf (stderr, "Testing desktop...\n");
124 g_assert (getDesktop (-1) == NULL);
125 desktop = getDesktop (0);
126 g_assert (desktop != NULL);
128 validate_accessible (desktop, FALSE, FALSE);
130 application = Accessible_getChildAtIndex (desktop, 0);
131 g_assert (application != NULL);
132 AccessibleApplication_unref (application);
134 Accessible_unref (desktop);
138 test_application (Accessible *application)
142 fprintf (stderr, "Testing application ...\n");
143 g_assert (Accessible_isApplication (application));
144 g_assert (Accessible_getApplication (application) ==
146 AccessibleApplication_unref (application);
148 str = AccessibleApplication_getToolkitName (application);
149 g_assert (str != NULL);
150 g_assert (!strcmp (str, "GAIL"));
151 SPI_freeString (str);
153 str = AccessibleApplication_getVersion (application);
154 g_assert (str != NULL);
155 SPI_freeString (str);
157 AccessibleApplication_getID (application);
161 test_editable_text (AccessibleEditableText *etext)
164 AccessibleText *text;
166 fprintf (stderr, "Testing editable text ...\n");
168 g_assert (Accessible_isText (etext));
169 text = Accessible_getText (etext);
171 AccessibleEditableText_setTextContents (
172 etext, TEST_STRING_B);
174 str = AccessibleText_getText (text, 0, -1);
175 g_assert (!strcmp (str, TEST_STRING_B));
177 SPI_freeString (str);
179 /* FIXME: lots more editing here */
181 AccessibleEditableText_setTextContents (
182 etext, TEST_STRING_A);
184 AccessibleText_unref (text);
188 test_text (AccessibleText *text)
192 fprintf (stderr, "Testing text ...\n");
194 g_assert (AccessibleText_getCharacterCount (text) ==
195 strlen (TEST_STRING_A));
197 str = AccessibleText_getText (text, 0, -1);
198 g_assert (!strcmp (str, TEST_STRING_A));
199 SPI_freeString (str);
201 str = AccessibleText_getText (text, 0, 5);
202 g_assert (!strncmp (str, TEST_STRING_A, 5));
203 SPI_freeString (str);
205 AccessibleText_setCaretOffset (text, 7);
206 g_assert (AccessibleText_getCaretOffset (text) == 7);
208 /* FIXME: lots more tests - selections etc. etc. */
212 test_value (AccessibleValue *value)
215 float original_value;
217 /* Note: test_value assertions are known not to work as of Dec 09 */
219 fprintf (stderr, "Testing value ...\n");
221 original_value = AccessibleValue_getCurrentValue (value);
223 g_assert (AccessibleValue_getCurrentValue (value) <=
224 AccessibleValue_getMaximumValue (value));
226 g_assert (AccessibleValue_getCurrentValue (value) >=
227 AccessibleValue_getMinimumValue (value));
229 AccessibleValue_setCurrentValue (value,
230 AccessibleValue_getMinimumValue (value));
232 g_assert (AccessibleValue_getCurrentValue (value) ==
233 AccessibleValue_getMinimumValue (value));
235 AccessibleValue_setCurrentValue (value,
236 AccessibleValue_getMaximumValue (value));
238 g_assert (AccessibleValue_getCurrentValue (value) ==
239 AccessibleValue_getMaximumValue (value));
241 AccessibleValue_setCurrentValue (value, original_value);
243 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
247 test_component (AccessibleComponent *component)
249 long x, y, width, height;
251 fprintf (stderr, "Testing component...\n");
253 AccessibleComponent_getExtents (
254 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
256 AccessibleComponent_getPosition (
257 component, &x, &y, SPI_COORD_TYPE_SCREEN);
259 AccessibleComponent_getSize (component, &width, &height);
261 if (width > 0 && height > 0) {
263 Accessible *accessible, *componentb;
266 g_assert (AccessibleComponent_contains (
267 component, x, y, SPI_COORD_TYPE_SCREEN));
269 g_assert (AccessibleComponent_contains (
270 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
272 g_assert (AccessibleComponent_contains (
273 component, x + width - 1, y + height - 1,
274 SPI_COORD_TYPE_SCREEN));
277 accessible = AccessibleComponent_getAccessibleAtPoint (
278 component, x, y, SPI_COORD_TYPE_SCREEN);
280 g_assert (Accessible_isComponent (accessible));
281 componentb = Accessible_getComponent (accessible);
282 g_assert (componentb == component);
284 AccessibleComponent_unref (componentb);
285 Accessible_unref (accessible);
289 AccessibleComponent_getLayer (component);
290 AccessibleComponent_getMDIZOrder (component);
291 /* AccessibleComponent_grabFocus (component); */
295 validate_tree (Accessible *accessible,
297 gboolean recurse_down)
302 parent = Accessible_getParent (accessible);
305 Accessible *child_at_index;
307 g_assert (parent != NULL);
309 index = Accessible_getIndexInParent (accessible);
310 g_assert (index >= 0);
312 child_at_index = Accessible_getChildAtIndex (parent, index);
314 g_assert (child_at_index == accessible);
316 Accessible_unref (child_at_index);
317 Accessible_unref (parent);
320 len = Accessible_getChildCount (accessible);
322 for (i = 0; i < len; i++) {
325 child = Accessible_getChildAtIndex (accessible, i);
328 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
330 g_assert (Accessible_getIndexInParent (child) == i);
331 g_assert (Accessible_getParent (child) == accessible);
334 if (recurse_down && child)
335 validate_accessible (child, has_parent, recurse_down);
337 Accessible_unref (child);
343 validate_accessible (Accessible *accessible,
345 gboolean recurse_down)
351 name = Accessible_getName (accessible);
352 g_assert (name != NULL);
354 descr = Accessible_getDescription (accessible);
355 g_assert (descr != NULL);
357 role = Accessible_getRole (accessible);
358 g_assert (role != NULL);
363 for (i = 0; i < print_tree_depth; i++)
365 fputs ("|-> [ ", stderr);
368 if (Accessible_isAction (accessible)) {
369 tmp = Accessible_getAction (accessible);
370 g_assert (tmp != NULL);
372 fprintf (stderr, "At");
373 AccessibleAction_unref (tmp);
376 if (Accessible_isApplication (accessible)) {
377 tmp = Accessible_getApplication (accessible);
379 fprintf (stderr, "Ap");
381 test_application (tmp);
382 AccessibleApplication_unref (tmp);
385 if (Accessible_isComponent (accessible)) {
386 tmp = Accessible_getComponent (accessible);
387 g_assert (tmp != NULL);
389 fprintf (stderr, "Co");
391 test_component (tmp);
392 AccessibleComponent_unref (tmp);
395 if (Accessible_isEditableText (accessible)) {
396 tmp = Accessible_getEditableText (accessible);
397 g_assert (tmp != NULL);
399 fprintf (stderr, "Et");
401 test_editable_text (tmp);
402 AccessibleEditableText_unref (tmp);
405 if (Accessible_isHypertext (accessible)) {
406 tmp = Accessible_getHypertext (accessible);
407 g_assert (tmp != NULL);
409 fprintf (stderr, "Ht");
410 AccessibleHypertext_unref (tmp);
413 if (Accessible_isImage (accessible)) {
414 tmp = Accessible_getImage (accessible);
415 g_assert (tmp != NULL);
417 fprintf (stderr, "Im");
418 AccessibleImage_unref (accessible);
421 if (Accessible_isSelection (accessible)) {
422 tmp = Accessible_getSelection (accessible);
423 g_assert (tmp != NULL);
425 fprintf (stderr, "Se");
426 AccessibleSelection_unref (tmp);
429 if (Accessible_isTable (accessible)) {
430 tmp = Accessible_getTable (accessible);
431 g_assert (tmp != NULL);
433 fprintf (stderr, "Ta");
434 AccessibleTable_unref (tmp);
437 if (Accessible_isText (accessible)) {
438 tmp = Accessible_getText (accessible);
439 g_assert (tmp != NULL);
441 fprintf (stderr, "Te");
444 AccessibleText_unref (tmp);
447 if (Accessible_isValue (accessible)) {
448 tmp = Accessible_getValue (accessible);
449 g_assert (tmp != NULL);
451 fprintf (stderr, "Va");
453 ; /* test_value (tmp); */
454 AccessibleValue_unref (tmp);
458 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
460 SPI_freeString (name);
461 SPI_freeString (descr);
463 validate_tree (accessible, has_parent, recurse_down);
469 fprintf (stderr, "Testing misc bits ...\n");
471 g_assert (!Accessible_isComponent (NULL));
472 g_assert (Accessible_getComponent (NULL) == NULL);
473 SPI_freeString (NULL);
477 global_listener_cb (AccessibleEvent *event,
480 TestWindow *win = user_data;
482 AccessibleApplication *application;
484 g_assert (win->magic == WINDOW_MAGIC);
485 g_assert (!strcmp (event->type, "focus:"));
487 fprintf (stderr, "Fielded focus event ...\n");
489 desktop = getDesktop (0);
490 application = Accessible_getChildAtIndex (desktop, 0);
491 g_assert (application != NULL);
492 Accessible_unref (desktop);
494 test_application (application);
496 AccessibleApplication_unref (application);
499 validate_accessible (event->source, TRUE, TRUE);
501 validate_accessible (event->source, TRUE, TRUE);
507 main (int argc, char **argv)
511 const char *modules, *tmp;
512 AccessibleEventListener *global_listener;
514 modules = g_getenv ("GTK_MODULES");
515 if (!modules || modules [0] == '\0')
516 putenv ("GTK_MODULES=gail:at-bridge");
519 gtk_init (&argc, &argv);
521 g_assert (!SPI_init ());
522 g_assert (SPI_init ());
523 g_assert (getDesktopCount () == 1);
529 win = create_test_window ();
531 global_listener = createAccessibleEventListener (global_listener_cb, win);
532 g_assert (registerGlobalEventListener (global_listener, "focus:"));
534 fprintf (stderr, "Waiting for focus event ...\n");
537 g_assert (deregisterGlobalEventListenerAll (global_listener));
538 AccessibleEventListener_unref (global_listener);
540 test_window_destroy (win);
542 if ((leaked = SPI_exit ()))
543 g_error ("Leaked %d SPI handles", leaked);
545 g_assert (!SPI_exit ());
547 fprintf (stderr, "All tests passed\n");
549 if (g_getenv ("_MEMPROF_SOCKET")) {
550 fprintf (stderr, "Waiting for memprof\n");
554 putenv ("AT_BRIDGE_SHUTDOWN=1");