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 *********
33 #include <libbonobo.h>
37 #define WHOLE_STRING -1
39 static void validate_accessible (Accessible *accessible,
41 gboolean recurse_down);
43 #define WINDOW_MAGIC 0x123456a
44 #define TEST_STRING_A "A test string"
45 #define TEST_STRING_A_OBJECT "A_test_string_object"
46 #define TEST_STRING_B "Another test string"
48 static int print_tree_depth = 0;
49 static gboolean print_tree = FALSE;
50 static gboolean do_poke = FALSE;
51 static gboolean key_press_received = FALSE;
52 static gboolean key_release_received = FALSE;
60 focus_me (GtkWidget *widget)
62 AtkObject *aobject = atk_implementor_ref_accessible (
63 ATK_IMPLEMENTOR (widget));
65 /* Force a focus event - even if the WM focused
66 * us before our at-bridge's idle handler registered
68 if (!GTK_WIDGET_HAS_FOCUS (widget))
69 gtk_widget_grab_focus (widget);
70 /* else: FIXME - gtk_widget_grab_focus should send a notify */
71 atk_focus_tracker_notify (aobject);
73 g_object_unref (G_OBJECT (aobject));
79 test_window_add_and_show (GtkContainer *container, GtkWidget *widget)
81 gtk_container_add (container, widget);
82 gtk_widget_show (widget);
91 GtkTreeViewColumn *column;
93 store = gtk_list_store_new (1, G_TYPE_STRING);
94 gtk_list_store_append (store, &iter);
95 gtk_list_store_set (store, &iter, 0, TEST_STRING_A, -1);
96 column = gtk_tree_view_column_new_with_attributes ("String",
97 gtk_cell_renderer_text_new (), "text", 0, NULL);
98 widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
99 g_object_unref (G_OBJECT (store));
100 gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
106 create_test_window (void)
108 TestWindow *win = g_new0 (TestWindow, 1);
109 GtkWidget *widget, *vbox;
112 win->magic = WINDOW_MAGIC;
113 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
115 gtk_widget_show (win->window);
117 vbox = gtk_vbox_new (0, 0);
118 gtk_container_add (GTK_CONTAINER (win->window), vbox);
119 gtk_widget_show (vbox);
121 widget = gtk_entry_new ();
122 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
123 obj = gtk_widget_get_accessible (widget);
124 atk_object_set_name (obj, TEST_STRING_A_OBJECT);
126 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
128 widget = gtk_button_new_with_label ("_Foobar");
129 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
131 widget = gtk_hseparator_new ();
132 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
134 widget = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION,
135 GTK_ICON_SIZE_LARGE_TOOLBAR);
136 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
138 widget = g_object_new (GTK_TYPE_HSCALE, NULL);
139 gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
140 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
142 widget = create_tree ();
143 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
145 g_idle_add ((GSourceFunc) focus_me, win->window);
151 test_window_destroy (TestWindow *win)
153 gtk_widget_destroy (win->window);
162 fprintf (stderr, "Testing roles...\n");
163 for (i = -1; i < 1000; i++)
164 g_assert (AccessibleRole_getName (i) != NULL);
166 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file-chooser"));
167 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radio-button"));
168 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
169 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
173 test_action (AccessibleAction *action)
177 g_assert ((n_actions = AccessibleAction_getNActions (action)) >= 0);
179 fprintf (stderr, "Testing actions...");
180 for (i = 0; i < n_actions; ++i)
182 s = AccessibleAction_getName (action, i);
184 sd = AccessibleAction_getDescription (action, i);
186 fprintf (stderr, "%d: %s (%s); ", i, s, sd);
189 g_assert (AccessibleAction_doAction (action, i));
191 fprintf (stderr, "\n");
198 Accessible *application;
202 fprintf (stderr, "Testing desktop...\n");
204 g_assert (SPI_getDesktop (-1) == NULL);
205 desktop = SPI_getDesktop (0);
206 g_assert (desktop != NULL);
208 g_assert ((length = SPI_getDesktopList (&list)) > 0);
209 g_assert (list[0] == desktop);
210 SPI_freeDesktopList (list);
212 validate_accessible (desktop, FALSE, FALSE);
214 application = Accessible_getChildAtIndex (desktop, 0);
215 g_assert (application != NULL);
216 AccessibleApplication_unref (application);
218 Accessible_unref (desktop);
222 test_application (Accessible *application)
226 fprintf (stderr, "Testing application ...\n");
227 g_assert (Accessible_isApplication (application));
228 g_assert (Accessible_getApplication (application) ==
230 AccessibleApplication_unref (application);
232 str = AccessibleApplication_getToolkitName (application);
233 g_assert (str != NULL);
234 g_assert (!strcmp (str, "GAIL"));
235 SPI_freeString (str);
237 str = AccessibleApplication_getLocale (application, LC_MESSAGES);
238 g_assert (!strcmp (str, setlocale (LC_MESSAGES, NULL)));
239 SPI_freeString (str);
241 str = AccessibleApplication_getVersion (application);
242 g_assert (str != NULL);
243 SPI_freeString (str);
245 AccessibleApplication_getID (application);
249 test_editable_text (AccessibleEditableText *etext)
252 AccessibleText *text;
254 fprintf (stderr, "Testing editable text ...\n");
256 g_assert (Accessible_isText (etext));
257 text = Accessible_getText (etext);
259 AccessibleEditableText_setTextContents (
260 etext, TEST_STRING_B);
262 str = AccessibleText_getText (text, 0, WHOLE_STRING);
263 g_assert (!strcmp (str, TEST_STRING_B));
265 SPI_freeString (str);
267 /* FIXME: lots more editing here */
269 AccessibleEditableText_setTextContents (
270 etext, TEST_STRING_A);
272 AccessibleText_unref (text);
276 test_table (AccessibleTable *table)
282 fprintf (stderr, "Testing table ...\n");
284 rows = AccessibleTable_getNRows (table);
287 columns = AccessibleTable_getNColumns (table);
288 g_assert (columns > 0);
290 index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
292 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
294 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
296 g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
297 Accessible_unref (header);
299 AccessibleTable_isSelected (table, 0, 0);
301 /* FIXME: lots more tests */
305 test_text (AccessibleText *text)
309 fprintf (stderr, "Testing text ...\n");
311 g_assert (AccessibleText_getCharacterCount (text) ==
312 strlen (TEST_STRING_A));
314 str = AccessibleText_getText (text, 0, WHOLE_STRING);
315 g_assert (!strcmp (str, TEST_STRING_A));
316 SPI_freeString (str);
318 str = AccessibleText_getText (text, 0, 5);
319 g_assert (!strncmp (str, TEST_STRING_A, 5));
320 SPI_freeString (str);
322 AccessibleText_setCaretOffset (text, 7);
323 g_assert (AccessibleText_getCaretOffset (text) == 7);
325 /* FIXME: lots more tests - selections etc. etc. */
329 test_value (AccessibleValue *value)
331 float original_value;
333 fprintf (stderr, "Testing value ...\n");
335 original_value = AccessibleValue_getCurrentValue (value);
337 g_assert (AccessibleValue_getCurrentValue (value) <=
338 AccessibleValue_getMaximumValue (value));
340 g_assert (AccessibleValue_getCurrentValue (value) >=
341 AccessibleValue_getMinimumValue (value));
343 AccessibleValue_setCurrentValue (value,
344 AccessibleValue_getMinimumValue (value));
346 g_assert (AccessibleValue_getCurrentValue (value) ==
347 AccessibleValue_getMinimumValue (value));
349 AccessibleValue_setCurrentValue (value,
350 AccessibleValue_getMaximumValue (value));
352 g_assert (AccessibleValue_getCurrentValue (value) ==
353 AccessibleValue_getMaximumValue (value));
355 AccessibleValue_setCurrentValue (value, original_value);
357 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
361 test_component (AccessibleComponent *component)
363 long x, y, width, height;
365 fprintf (stderr, "Testing component...\n");
367 AccessibleComponent_getExtents (
368 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
370 AccessibleComponent_getPosition (
371 component, &x, &y, SPI_COORD_TYPE_SCREEN);
373 AccessibleComponent_getSize (component, &width, &height);
375 if (width > 0 && height > 0) {
377 Accessible *accessible, *componentb;
380 g_assert (AccessibleComponent_contains (
381 component, x, y, SPI_COORD_TYPE_SCREEN));
383 g_assert (AccessibleComponent_contains (
384 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
386 g_assert (AccessibleComponent_contains (
387 component, x + width - 1, y + height - 1,
388 SPI_COORD_TYPE_SCREEN));
391 accessible = AccessibleComponent_getAccessibleAtPoint (
392 component, x, y, SPI_COORD_TYPE_SCREEN);
394 g_assert (Accessible_isComponent (accessible));
395 componentb = Accessible_getComponent (accessible);
396 g_assert (componentb == component);
398 AccessibleComponent_unref (componentb);
399 Accessible_unref (accessible);
403 AccessibleComponent_getLayer (component);
404 AccessibleComponent_getMDIZOrder (component);
405 /* AccessibleComponent_grabFocus (component); */
409 test_image (AccessibleImage *image)
412 long int x = -1, y = -1, width = -1, height = -1;
414 desc = AccessibleImage_getImageDescription (image);
415 g_assert (desc != NULL);
416 SPI_freeString (desc);
418 AccessibleImage_getImagePosition (image, &x, &y,
419 SPI_COORD_TYPE_SCREEN);
420 AccessibleImage_getImageSize (image, &width, &height);
421 AccessibleImage_getImageExtents (image, &x, &y, &width, &height,
422 SPI_COORD_TYPE_WINDOW);
426 validate_tree (Accessible *accessible,
428 gboolean recurse_down)
433 parent = Accessible_getParent (accessible);
436 Accessible *child_at_index;
438 g_assert (parent != NULL);
440 index = Accessible_getIndexInParent (accessible);
441 g_assert (index >= 0);
443 child_at_index = Accessible_getChildAtIndex (parent, index);
445 g_assert (child_at_index == accessible);
447 Accessible_unref (child_at_index);
448 Accessible_unref (parent);
451 len = Accessible_getChildCount (accessible);
453 for (i = 0; i < len; i++) {
456 child = Accessible_getChildAtIndex (accessible, i);
459 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
461 g_assert (Accessible_getIndexInParent (child) == i);
462 g_assert (Accessible_getParent (child) == accessible);
465 if (recurse_down && child)
466 validate_accessible (child, has_parent, recurse_down);
468 Accessible_unref (child);
474 validate_accessible (Accessible *accessible,
476 gboolean recurse_down)
481 AccessibleRelation **relations;
483 GString *item_str = g_string_new ("");
486 name = Accessible_getName (accessible);
487 g_assert (name != NULL);
489 descr = Accessible_getDescription (accessible);
490 g_assert (descr != NULL);
492 role = Accessible_getRole (accessible);
493 g_assert (role != SPI_ROLE_INVALID);
494 role_name = Accessible_getRoleName (accessible);
495 g_assert (role_name != NULL);
497 relations = Accessible_getRelationSet (accessible);
498 g_assert (relations != NULL);
500 for (i = 0; relations [i]; i++) {
501 AccessibleRelationType type;
504 fprintf (stderr, "relation %d\n", i);
506 type = AccessibleRelation_getRelationType (relations [i]);
507 g_assert (type != SPI_RELATION_NULL);
509 targets = AccessibleRelation_getNTargets (relations [i]);
510 g_assert (targets != -1);
512 AccessibleRelation_unref (relations [i]);
513 relations [i] = NULL;
520 for (i = 0; i < print_tree_depth; i++)
522 fputs ("|-> [ ", stderr);
525 if (Accessible_isAction (accessible)) {
526 tmp = Accessible_getAction (accessible);
527 g_assert (tmp != NULL);
529 fprintf (stderr, "At");
532 AccessibleAction_unref (tmp);
535 if (Accessible_isApplication (accessible)) {
536 tmp = Accessible_getApplication (accessible);
538 fprintf (stderr, "Ap");
540 test_application (tmp);
541 AccessibleApplication_unref (tmp);
544 if (Accessible_isComponent (accessible)) {
545 tmp = Accessible_getComponent (accessible);
546 g_assert (tmp != NULL);
548 fprintf (stderr, "Co");
550 test_component (tmp);
551 AccessibleComponent_unref (tmp);
554 if (Accessible_isEditableText (accessible)) {
555 tmp = Accessible_getEditableText (accessible);
556 g_assert (tmp != NULL);
558 fprintf (stderr, "Et");
560 test_editable_text (tmp);
561 AccessibleEditableText_unref (tmp);
564 if (Accessible_isHypertext (accessible)) {
565 tmp = Accessible_getHypertext (accessible);
566 g_assert (tmp != NULL);
568 fprintf (stderr, "Ht");
569 AccessibleHypertext_unref (tmp);
572 if (Accessible_isImage (accessible)) {
573 tmp = Accessible_getImage (accessible);
574 g_assert (tmp != NULL);
578 fprintf (stderr, "Im");
580 desc = AccessibleImage_getImageDescription (tmp);
581 g_string_append_printf (
582 item_str, " image descr: '%s'", desc);
583 SPI_freeString (desc);
587 AccessibleImage_unref (tmp);
590 if (Accessible_isSelection (accessible)) {
591 tmp = Accessible_getSelection (accessible);
592 g_assert (tmp != NULL);
594 fprintf (stderr, "Se");
595 AccessibleSelection_unref (tmp);
598 if (Accessible_isTable (accessible)) {
599 tmp = Accessible_getTable (accessible);
600 g_assert (tmp != NULL);
602 fprintf (stderr, "Ta");
605 AccessibleTable_unref (tmp);
608 if (Accessible_isText (accessible)) {
609 tmp = Accessible_getText (accessible);
610 g_assert (tmp != NULL);
612 fprintf (stderr, "Te");
614 if (strcmp (name, TEST_STRING_A_OBJECT) == 0)
617 AccessibleText_unref (tmp);
620 if (Accessible_isValue (accessible)) {
621 tmp = Accessible_getValue (accessible);
622 g_assert (tmp != NULL);
624 fprintf (stderr, "Va");
627 AccessibleValue_unref (tmp);
631 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
632 name, descr, role_name, item_str->str);
634 SPI_freeString (name);
635 SPI_freeString (descr);
636 SPI_freeString (role_name);
637 g_string_free (item_str, TRUE);
639 validate_tree (accessible, has_parent, recurse_down);
645 fprintf (stderr, "Testing misc bits ...\n");
647 g_assert (!Accessible_isComponent (NULL));
648 g_assert (Accessible_getComponent (NULL) == NULL);
649 SPI_freeString (NULL);
653 global_listener_cb (const AccessibleEvent *event,
656 TestWindow *win = user_data;
658 AccessibleApplication *application;
660 g_assert (win->magic == WINDOW_MAGIC);
661 g_assert (!strcmp (event->type, "focus:"));
663 fprintf (stderr, "Fielded focus event ...\n");
666 desktop = SPI_getDesktop (0);
667 application = Accessible_getChildAtIndex (desktop, 0);
668 g_assert (application != NULL);
669 Accessible_unref (desktop);
671 test_application (application);
673 AccessibleApplication_unref (application);
677 validate_accessible (event->source, TRUE, TRUE);
679 fprintf (stderr, "quitting mainloop.\n");
684 validate_accessible (event->source, TRUE, TRUE);
688 key_listener_cb (const AccessibleKeystroke *stroke,
691 AccessibleKeystroke *s = user_data;
694 if (stroke->keystring) s->keystring = g_strdup (stroke->keystring);
696 if (s->type == SPI_KEY_PRESSED)
697 key_press_received = TRUE;
698 else if (s->type == SPI_KEY_RELEASED)
699 key_release_received = TRUE;
706 test_keylisteners (void)
709 AccessibleKeystroke stroke;
710 AccessibleKeystrokeListener *key_listener;
711 AccessibleKeySet *test_keyset;
713 fprintf (stderr, "Testing keyboard listeners ...\n");
715 key_listener = SPI_createAccessibleKeystrokeListener (
716 key_listener_cb, &stroke);
718 test_keyset = SPI_createAccessibleKeySet (1, "=", NULL, NULL);
720 g_assert (SPI_registerAccessibleKeystrokeListener (
724 SPI_KEY_PRESSED | SPI_KEY_RELEASED,
725 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS));
727 for (i = 0; i < 3; i++) {
728 memset (&stroke, 0, sizeof (AccessibleKeystroke));
729 g_assert (SPI_generateKeyboardEvent ('=', NULL, SPI_KEY_SYM));
730 while (!(key_press_received))
731 g_main_context_iteration (NULL, TRUE);
732 fprintf (stderr, "p [%s]", stroke.keystring);
733 g_assert (!strcmp (stroke.keystring, "="));
734 while (!(key_release_received))
735 g_main_context_iteration (NULL, TRUE);
736 fprintf (stderr, "r [%s]", stroke.keystring);
737 key_press_received = FALSE;
738 key_release_received = FALSE;
740 g_assert (SPI_deregisterAccessibleKeystrokeListener (key_listener, 0));
741 SPI_freeAccessibleKeySet (test_keyset);
743 fprintf (stderr, "\n");
745 AccessibleKeystrokeListener_unref (key_listener);
747 g_assert (SPI_generateMouseEvent (100, 100, "rel"));
748 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
749 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
750 g_assert (SPI_generateMouseEvent (-1, -1, "b1c"));
754 main (int argc, char **argv)
759 AccessibleEventListener *global_listener;
761 modules = g_getenv ("GTK_MODULES");
762 if (!modules || modules [0] == '\0')
763 putenv ("GTK_MODULES=gail:atk-bridge");
766 for (i = 1; i < argc; i++) {
767 if (!g_strcasecmp (argv [i], "--poke"))
771 gtk_init (&argc, &argv);
773 g_assert (!SPI_init ());
774 g_assert (SPI_init ());
775 g_assert (SPI_getDesktopCount () == 1);
780 test_keylisteners ();
782 win = create_test_window ();
784 global_listener = SPI_createAccessibleEventListener (global_listener_cb, win);
786 g_assert (SPI_registerGlobalEventListener (global_listener, "focus:"));
788 fprintf (stderr, "Waiting for focus event ...\n");
791 g_assert (SPI_deregisterGlobalEventListenerAll (global_listener));
792 AccessibleEventListener_unref (global_listener);
794 test_window_destroy (win);
796 /* Wait for any pending events from the registry */
798 for (i = 0; i < 100; i++)
799 CORBA_ORB_perform_work (NULL, NULL);
801 if ((leaked = SPI_exit ()))
802 g_error ("Leaked %d SPI handles", leaked);
804 g_assert (!SPI_exit ());
806 fprintf (stderr, "All tests passed\n");
808 if (g_getenv ("_MEMPROF_SOCKET")) {
809 fprintf (stderr, "Waiting for memprof\n");
813 putenv ("AT_BRIDGE_SHUTDOWN=1");