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;
44 static gboolean do_poke = FALSE;
52 focus_me (GtkWidget *widget)
54 AtkObject *aobject = atk_implementor_ref_accessible (
55 ATK_IMPLEMENTOR (widget));
57 /* Force a focus event - even if the WM focused
58 * us before our at-bridge's idle handler registered
60 if (!GTK_WIDGET_HAS_FOCUS (widget))
61 gtk_widget_grab_focus (widget);
62 /* else: FIXME - gtk_widget_grab_focus should send a notify */
63 atk_focus_tracker_notify (aobject);
65 g_object_unref (G_OBJECT (aobject));
71 test_window_add_and_show (GtkContainer *container, GtkWidget *widget)
73 gtk_container_add (container, widget);
74 gtk_widget_show (widget);
83 GtkTreeViewColumn *column;
85 store = gtk_list_store_new (1, G_TYPE_STRING);
86 gtk_list_store_append (store, &iter);
87 gtk_list_store_set (store, &iter, 0, TEST_STRING_A, -1);
88 column = gtk_tree_view_column_new_with_attributes ("String",
89 gtk_cell_renderer_text_new (), "text", 0, NULL);
90 widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
91 g_object_unref (G_OBJECT (store));
92 gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
98 create_test_window (void)
100 TestWindow *win = g_new0 (TestWindow, 1);
101 GtkWidget *widget, *vbox;
103 win->magic = WINDOW_MAGIC;
104 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
106 gtk_widget_show (win->window);
108 vbox = gtk_vbox_new (0, 0);
109 gtk_container_add (GTK_CONTAINER (win->window), vbox);
110 gtk_widget_show (vbox);
112 widget = gtk_entry_new ();
113 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
114 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
116 widget = gtk_button_new_with_label ("_Foobar");
117 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
119 widget = gtk_hseparator_new ();
120 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
122 widget = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION,
123 GTK_ICON_SIZE_LARGE_TOOLBAR);
124 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
126 widget = g_object_new (GTK_TYPE_RANGE, NULL);
127 gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
128 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
130 widget = create_tree ();
131 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
133 g_idle_add ((GSourceFunc) focus_me, win->window);
139 test_window_destroy (TestWindow *win)
141 gtk_widget_destroy (win->window);
150 fprintf (stderr, "Testing roles...\n");
151 for (i = -1; i < 1000; i++)
152 g_assert (AccessibleRole_getName (i) != NULL);
154 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
155 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
156 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
157 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
161 test_action (AccessibleAction *action)
165 g_assert ((n_actions = AccessibleAction_getNActions (action)) >= 0);
167 fprintf (stderr, "Testing actions...");
168 for (i = 0; i < n_actions; ++i)
170 s = AccessibleAction_getName (action, i);
172 sd = AccessibleAction_getDescription (action, i);
174 fprintf (stderr, "%d: %s (%s); ", i, s, sd);
177 /* g_assert (AccessibleAction_doAction (action, i)); */
179 fprintf (stderr, "\n");
186 Accessible *application;
188 fprintf (stderr, "Testing desktop...\n");
190 g_assert (getDesktop (-1) == NULL);
191 desktop = getDesktop (0);
192 g_assert (desktop != NULL);
194 validate_accessible (desktop, FALSE, FALSE);
196 application = Accessible_getChildAtIndex (desktop, 0);
197 g_assert (application != NULL);
198 AccessibleApplication_unref (application);
200 Accessible_unref (desktop);
204 test_application (Accessible *application)
208 fprintf (stderr, "Testing application ...\n");
209 g_assert (Accessible_isApplication (application));
210 g_assert (Accessible_getApplication (application) ==
212 AccessibleApplication_unref (application);
214 str = AccessibleApplication_getToolkitName (application);
215 g_assert (str != NULL);
216 g_assert (!strcmp (str, "GAIL"));
217 SPI_freeString (str);
219 str = AccessibleApplication_getVersion (application);
220 g_assert (str != NULL);
221 SPI_freeString (str);
223 AccessibleApplication_getID (application);
227 test_editable_text (AccessibleEditableText *etext)
230 AccessibleText *text;
232 fprintf (stderr, "Testing editable text ...\n");
234 g_assert (Accessible_isText (etext));
235 text = Accessible_getText (etext);
237 AccessibleEditableText_setTextContents (
238 etext, TEST_STRING_B);
240 str = AccessibleText_getText (text, 0, -1);
241 g_assert (!strcmp (str, TEST_STRING_B));
243 SPI_freeString (str);
245 /* FIXME: lots more editing here */
247 AccessibleEditableText_setTextContents (
248 etext, TEST_STRING_A);
250 AccessibleText_unref (text);
254 test_table (AccessibleTable *table)
260 fprintf (stderr, "Testing table ...\n");
262 rows = AccessibleTable_getNRows (table);
265 columns = AccessibleTable_getNColumns (table);
266 g_assert (columns > 0);
268 index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
270 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
272 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
274 g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
275 Accessible_unref (header);
277 AccessibleTable_isSelected (table, 0, 0);
279 /* FIXME: lots more tests */
283 test_text (AccessibleText *text)
287 fprintf (stderr, "Testing text ...\n");
289 g_assert (AccessibleText_getCharacterCount (text) ==
290 strlen (TEST_STRING_A));
292 str = AccessibleText_getText (text, 0, -1);
293 g_assert (!strcmp (str, TEST_STRING_A));
294 SPI_freeString (str);
296 str = AccessibleText_getText (text, 0, 5);
297 g_assert (!strncmp (str, TEST_STRING_A, 5));
298 SPI_freeString (str);
300 AccessibleText_setCaretOffset (text, 7);
301 g_assert (AccessibleText_getCaretOffset (text) == 7);
303 /* FIXME: lots more tests - selections etc. etc. */
307 test_value (AccessibleValue *value)
309 float original_value;
311 /* Note: test_value assertions are known not to work as of Dec 09 */
313 fprintf (stderr, "Testing value ...\n");
315 original_value = AccessibleValue_getCurrentValue (value);
317 g_assert (AccessibleValue_getCurrentValue (value) <=
318 AccessibleValue_getMaximumValue (value));
320 g_assert (AccessibleValue_getCurrentValue (value) >=
321 AccessibleValue_getMinimumValue (value));
323 AccessibleValue_setCurrentValue (value,
324 AccessibleValue_getMinimumValue (value));
326 g_assert (AccessibleValue_getCurrentValue (value) ==
327 AccessibleValue_getMinimumValue (value));
329 AccessibleValue_setCurrentValue (value,
330 AccessibleValue_getMaximumValue (value));
332 g_assert (AccessibleValue_getCurrentValue (value) ==
333 AccessibleValue_getMaximumValue (value));
335 AccessibleValue_setCurrentValue (value, original_value);
337 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
341 test_component (AccessibleComponent *component)
343 long x, y, width, height;
345 fprintf (stderr, "Testing component...\n");
347 AccessibleComponent_getExtents (
348 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
350 AccessibleComponent_getPosition (
351 component, &x, &y, SPI_COORD_TYPE_SCREEN);
353 AccessibleComponent_getSize (component, &width, &height);
355 if (width > 0 && height > 0) {
357 Accessible *accessible, *componentb;
360 g_assert (AccessibleComponent_contains (
361 component, x, y, SPI_COORD_TYPE_SCREEN));
363 g_assert (AccessibleComponent_contains (
364 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
366 g_assert (AccessibleComponent_contains (
367 component, x + width - 1, y + height - 1,
368 SPI_COORD_TYPE_SCREEN));
371 accessible = AccessibleComponent_getAccessibleAtPoint (
372 component, x, y, SPI_COORD_TYPE_SCREEN);
374 g_assert (Accessible_isComponent (accessible));
375 componentb = Accessible_getComponent (accessible);
376 g_assert (componentb == component);
378 AccessibleComponent_unref (componentb);
379 Accessible_unref (accessible);
383 AccessibleComponent_getLayer (component);
384 AccessibleComponent_getMDIZOrder (component);
385 /* AccessibleComponent_grabFocus (component); */
389 test_image (AccessibleImage *image)
392 long int x = -1, y = -1, width = -1, height = -1;
394 desc = AccessibleImage_getImageDescription (image);
395 g_assert (desc != NULL);
396 SPI_freeString (desc);
398 AccessibleImage_getImagePosition (image, &x, &y,
399 SPI_COORD_TYPE_SCREEN);
400 AccessibleImage_getImageSize (image, &width, &height);
401 AccessibleImage_getImageExtents (image, &x, &y, &width, &height,
402 SPI_COORD_TYPE_WINDOW);
406 validate_tree (Accessible *accessible,
408 gboolean recurse_down)
413 parent = Accessible_getParent (accessible);
416 Accessible *child_at_index;
418 g_assert (parent != NULL);
420 index = Accessible_getIndexInParent (accessible);
421 g_assert (index >= 0);
423 child_at_index = Accessible_getChildAtIndex (parent, index);
425 g_assert (child_at_index == accessible);
427 Accessible_unref (child_at_index);
428 Accessible_unref (parent);
431 len = Accessible_getChildCount (accessible);
433 for (i = 0; i < len; i++) {
436 child = Accessible_getChildAtIndex (accessible, i);
439 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
441 g_assert (Accessible_getIndexInParent (child) == i);
442 g_assert (Accessible_getParent (child) == accessible);
445 if (recurse_down && child)
446 validate_accessible (child, has_parent, recurse_down);
448 Accessible_unref (child);
454 validate_accessible (Accessible *accessible,
456 gboolean recurse_down)
462 GString *item_str = g_string_new ("");
464 name = Accessible_getName (accessible);
465 g_assert (name != NULL);
467 descr = Accessible_getDescription (accessible);
468 g_assert (descr != NULL);
470 role = Accessible_getRole (accessible);
471 g_assert (role != SPI_ROLE_INVALID);
472 role_name = Accessible_getRoleName (accessible);
473 g_assert (role_name != NULL);
479 for (i = 0; i < print_tree_depth; i++)
481 fputs ("|-> [ ", stderr);
484 if (Accessible_isAction (accessible)) {
485 tmp = Accessible_getAction (accessible);
486 g_assert (tmp != NULL);
488 fprintf (stderr, "At");
491 AccessibleAction_unref (tmp);
494 if (Accessible_isApplication (accessible)) {
495 tmp = Accessible_getApplication (accessible);
497 fprintf (stderr, "Ap");
499 test_application (tmp);
500 AccessibleApplication_unref (tmp);
503 if (Accessible_isComponent (accessible)) {
504 tmp = Accessible_getComponent (accessible);
505 g_assert (tmp != NULL);
507 fprintf (stderr, "Co");
509 test_component (tmp);
510 AccessibleComponent_unref (tmp);
513 if (Accessible_isEditableText (accessible)) {
514 tmp = Accessible_getEditableText (accessible);
515 g_assert (tmp != NULL);
517 fprintf (stderr, "Et");
519 test_editable_text (tmp);
520 AccessibleEditableText_unref (tmp);
523 if (Accessible_isHypertext (accessible)) {
524 tmp = Accessible_getHypertext (accessible);
525 g_assert (tmp != NULL);
527 fprintf (stderr, "Ht");
528 AccessibleHypertext_unref (tmp);
531 if (Accessible_isImage (accessible)) {
532 tmp = Accessible_getImage (accessible);
533 g_assert (tmp != NULL);
537 fprintf (stderr, "Im");
539 desc = AccessibleImage_getImageDescription (tmp);
540 g_string_append_printf (
541 item_str, " image descr: '%s'", desc);
542 SPI_freeString (desc);
546 AccessibleImage_unref (tmp);
549 if (Accessible_isSelection (accessible)) {
550 tmp = Accessible_getSelection (accessible);
551 g_assert (tmp != NULL);
553 fprintf (stderr, "Se");
554 AccessibleSelection_unref (tmp);
557 if (Accessible_isTable (accessible)) {
558 tmp = Accessible_getTable (accessible);
559 g_assert (tmp != NULL);
561 fprintf (stderr, "Ta");
564 AccessibleTable_unref (tmp);
567 if (Accessible_isText (accessible)) {
568 tmp = Accessible_getText (accessible);
569 g_assert (tmp != NULL);
571 fprintf (stderr, "Te");
574 AccessibleText_unref (tmp);
577 if (Accessible_isValue (accessible)) {
578 tmp = Accessible_getValue (accessible);
579 g_assert (tmp != NULL);
581 fprintf (stderr, "Va");
584 AccessibleValue_unref (tmp);
588 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
589 name, descr, role_name, item_str->str);
591 SPI_freeString (name);
592 SPI_freeString (descr);
593 SPI_freeString (role_name);
594 g_string_free (item_str, TRUE);
596 validate_tree (accessible, has_parent, recurse_down);
602 fprintf (stderr, "Testing misc bits ...\n");
604 g_assert (!Accessible_isComponent (NULL));
605 g_assert (Accessible_getComponent (NULL) == NULL);
606 SPI_freeString (NULL);
610 global_listener_cb (AccessibleEvent *event,
613 TestWindow *win = user_data;
615 AccessibleApplication *application;
617 g_assert (win->magic == WINDOW_MAGIC);
618 g_assert (!strcmp (event->type, "focus:"));
620 fprintf (stderr, "Fielded focus event ...\n");
623 desktop = getDesktop (0);
624 application = Accessible_getChildAtIndex (desktop, 0);
625 g_assert (application != NULL);
626 Accessible_unref (desktop);
628 test_application (application);
630 AccessibleApplication_unref (application);
633 validate_accessible (event->source, TRUE, TRUE);
639 validate_accessible (event->source, TRUE, TRUE);
643 main (int argc, char **argv)
648 AccessibleEventListener *global_listener;
650 modules = g_getenv ("GTK_MODULES");
651 if (!modules || modules [0] == '\0')
652 putenv ("GTK_MODULES=gail:at-bridge");
655 for (i = 1; i < argc; i++) {
656 if (!g_strcasecmp (argv [i], "--poke"))
660 gtk_init (&argc, &argv);
662 g_assert (!SPI_init ());
663 g_assert (SPI_init ());
664 g_assert (getDesktopCount () == 1);
670 win = create_test_window ();
672 global_listener = createAccessibleEventListener (global_listener_cb, win);
673 g_assert (registerGlobalEventListener (global_listener, "focus:"));
675 fprintf (stderr, "Waiting for focus event ...\n");
678 g_assert (deregisterGlobalEventListenerAll (global_listener));
679 AccessibleEventListener_unref (global_listener);
681 test_window_destroy (win);
683 if ((leaked = SPI_exit ()))
684 g_error ("Leaked %d SPI handles", leaked);
686 g_assert (!SPI_exit ());
688 fprintf (stderr, "All tests passed\n");
690 if (g_getenv ("_MEMPROF_SOCKET")) {
691 fprintf (stderr, "Waiting for memprof\n");
695 putenv ("AT_BRIDGE_SHUTDOWN=1");