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;
201 fprintf (stderr, "Testing desktop...\n");
203 g_assert (SPI_getDesktop (-1) == NULL);
204 desktop = SPI_getDesktop (0);
205 g_assert (desktop != NULL);
207 g_assert ((SPI_getDesktopList (&list)) > 0);
208 g_assert (list[0] == desktop);
209 SPI_freeDesktopList (list);
211 validate_accessible (desktop, FALSE, FALSE);
213 application = Accessible_getChildAtIndex (desktop, 0);
214 g_assert (application != NULL);
215 AccessibleApplication_unref (application);
217 Accessible_unref (desktop);
221 test_application (Accessible *application)
225 fprintf (stderr, "Testing application ...\n");
226 g_assert (Accessible_isApplication (application));
227 g_assert (Accessible_getApplication (application) ==
229 AccessibleApplication_unref (application);
231 str = AccessibleApplication_getToolkitName (application);
232 g_assert (str != NULL);
233 g_assert (!strcmp (str, "GAIL"));
234 SPI_freeString (str);
236 str = AccessibleApplication_getLocale (application, LC_MESSAGES);
237 g_assert (!strcmp (str, setlocale (LC_MESSAGES, NULL)));
238 SPI_freeString (str);
240 str = AccessibleApplication_getVersion (application);
241 g_assert (str != NULL);
242 SPI_freeString (str);
244 AccessibleApplication_getID (application);
248 test_editable_text (AccessibleEditableText *etext)
251 AccessibleText *text;
253 fprintf (stderr, "Testing editable text ...\n");
255 g_assert (Accessible_isText (etext));
256 text = Accessible_getText (etext);
258 AccessibleEditableText_setTextContents (
259 etext, TEST_STRING_B);
261 str = AccessibleText_getText (text, 0, WHOLE_STRING);
262 g_assert (!strcmp (str, TEST_STRING_B));
264 SPI_freeString (str);
266 /* FIXME: lots more editing here */
268 AccessibleEditableText_setTextContents (
269 etext, TEST_STRING_A);
271 AccessibleText_unref (text);
275 test_table (AccessibleTable *table)
281 fprintf (stderr, "Testing table ...\n");
283 rows = AccessibleTable_getNRows (table);
286 columns = AccessibleTable_getNColumns (table);
287 g_assert (columns > 0);
289 index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
291 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
293 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
295 g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
296 Accessible_unref (header);
298 AccessibleTable_isSelected (table, 0, 0);
300 /* FIXME: lots more tests */
304 test_text (AccessibleText *text)
308 fprintf (stderr, "Testing text ...\n");
310 g_assert (AccessibleText_getCharacterCount (text) ==
311 strlen (TEST_STRING_A));
313 str = AccessibleText_getText (text, 0, WHOLE_STRING);
314 g_assert (!strcmp (str, TEST_STRING_A));
315 SPI_freeString (str);
317 str = AccessibleText_getText (text, 0, 5);
318 g_assert (!strncmp (str, TEST_STRING_A, 5));
319 SPI_freeString (str);
321 AccessibleText_setCaretOffset (text, 7);
322 g_assert (AccessibleText_getCaretOffset (text) == 7);
324 /* FIXME: lots more tests - selections etc. etc. */
328 test_value (AccessibleValue *value)
330 float original_value;
332 fprintf (stderr, "Testing value ...\n");
334 original_value = AccessibleValue_getCurrentValue (value);
336 g_assert (AccessibleValue_getCurrentValue (value) <=
337 AccessibleValue_getMaximumValue (value));
339 g_assert (AccessibleValue_getCurrentValue (value) >=
340 AccessibleValue_getMinimumValue (value));
342 AccessibleValue_setCurrentValue (value,
343 AccessibleValue_getMinimumValue (value));
345 g_assert (AccessibleValue_getCurrentValue (value) ==
346 AccessibleValue_getMinimumValue (value));
348 AccessibleValue_setCurrentValue (value,
349 AccessibleValue_getMaximumValue (value));
351 g_assert (AccessibleValue_getCurrentValue (value) ==
352 AccessibleValue_getMaximumValue (value));
354 AccessibleValue_setCurrentValue (value, original_value);
356 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
360 test_component (AccessibleComponent *component)
362 long x, y, width, height;
364 fprintf (stderr, "Testing component...\n");
366 AccessibleComponent_getExtents (
367 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
369 AccessibleComponent_getPosition (
370 component, &x, &y, SPI_COORD_TYPE_SCREEN);
372 AccessibleComponent_getSize (component, &width, &height);
374 if (width > 0 && height > 0) {
376 Accessible *accessible, *componentb;
379 g_assert (AccessibleComponent_contains (
380 component, x, y, SPI_COORD_TYPE_SCREEN));
382 g_assert (AccessibleComponent_contains (
383 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
385 g_assert (AccessibleComponent_contains (
386 component, x + width - 1, y + height - 1,
387 SPI_COORD_TYPE_SCREEN));
390 accessible = AccessibleComponent_getAccessibleAtPoint (
391 component, x, y, SPI_COORD_TYPE_SCREEN);
393 g_assert (Accessible_isComponent (accessible));
394 componentb = Accessible_getComponent (accessible);
395 g_assert (componentb == component);
397 AccessibleComponent_unref (componentb);
398 Accessible_unref (accessible);
402 AccessibleComponent_getLayer (component);
403 AccessibleComponent_getMDIZOrder (component);
404 /* AccessibleComponent_grabFocus (component); */
408 test_image (AccessibleImage *image)
411 long int x = -1, y = -1, width = -1, height = -1;
413 desc = AccessibleImage_getImageDescription (image);
414 g_assert (desc != NULL);
415 SPI_freeString (desc);
417 AccessibleImage_getImagePosition (image, &x, &y,
418 SPI_COORD_TYPE_SCREEN);
419 AccessibleImage_getImageSize (image, &width, &height);
420 AccessibleImage_getImageExtents (image, &x, &y, &width, &height,
421 SPI_COORD_TYPE_WINDOW);
425 validate_tree (Accessible *accessible,
427 gboolean recurse_down)
432 parent = Accessible_getParent (accessible);
435 Accessible *child_at_index;
437 g_assert (parent != NULL);
439 index = Accessible_getIndexInParent (accessible);
440 g_assert (index >= 0);
442 child_at_index = Accessible_getChildAtIndex (parent, index);
444 g_assert (child_at_index == accessible);
446 Accessible_unref (child_at_index);
447 Accessible_unref (parent);
450 len = Accessible_getChildCount (accessible);
452 for (i = 0; i < len; i++) {
455 child = Accessible_getChildAtIndex (accessible, i);
458 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
460 g_assert (Accessible_getIndexInParent (child) == i);
461 g_assert (Accessible_getParent (child) == accessible);
464 if (recurse_down && child)
465 validate_accessible (child, has_parent, recurse_down);
467 Accessible_unref (child);
473 validate_accessible (Accessible *accessible,
475 gboolean recurse_down)
480 AccessibleRelation **relations;
482 GString *item_str = g_string_new ("");
485 name = Accessible_getName (accessible);
486 g_assert (name != NULL);
488 descr = Accessible_getDescription (accessible);
489 g_assert (descr != NULL);
491 role = Accessible_getRole (accessible);
492 g_assert (role != SPI_ROLE_INVALID);
493 role_name = Accessible_getRoleName (accessible);
494 g_assert (role_name != NULL);
496 relations = Accessible_getRelationSet (accessible);
497 g_assert (relations != NULL);
499 for (i = 0; relations [i]; i++) {
500 AccessibleRelationType type;
503 fprintf (stderr, "relation %d\n", i);
505 type = AccessibleRelation_getRelationType (relations [i]);
506 g_assert (type != SPI_RELATION_NULL);
508 targets = AccessibleRelation_getNTargets (relations [i]);
509 g_assert (targets != -1);
511 AccessibleRelation_unref (relations [i]);
512 relations [i] = NULL;
519 for (i = 0; i < print_tree_depth; i++)
521 fputs ("|-> [ ", stderr);
524 if (Accessible_isAction (accessible)) {
525 tmp = Accessible_getAction (accessible);
526 g_assert (tmp != NULL);
528 fprintf (stderr, "At");
531 AccessibleAction_unref (tmp);
534 if (Accessible_isApplication (accessible)) {
535 tmp = Accessible_getApplication (accessible);
537 fprintf (stderr, "Ap");
539 test_application (tmp);
540 AccessibleApplication_unref (tmp);
543 if (Accessible_isComponent (accessible)) {
544 tmp = Accessible_getComponent (accessible);
545 g_assert (tmp != NULL);
547 fprintf (stderr, "Co");
549 test_component (tmp);
550 AccessibleComponent_unref (tmp);
553 if (Accessible_isEditableText (accessible)) {
554 tmp = Accessible_getEditableText (accessible);
555 g_assert (tmp != NULL);
557 fprintf (stderr, "Et");
559 test_editable_text (tmp);
560 AccessibleEditableText_unref (tmp);
563 if (Accessible_isHypertext (accessible)) {
564 tmp = Accessible_getHypertext (accessible);
565 g_assert (tmp != NULL);
567 fprintf (stderr, "Ht");
568 AccessibleHypertext_unref (tmp);
571 if (Accessible_isImage (accessible)) {
572 tmp = Accessible_getImage (accessible);
573 g_assert (tmp != NULL);
577 fprintf (stderr, "Im");
579 desc = AccessibleImage_getImageDescription (tmp);
580 g_string_append_printf (
581 item_str, " image descr: '%s'", desc);
582 SPI_freeString (desc);
586 AccessibleImage_unref (tmp);
589 if (Accessible_isSelection (accessible)) {
590 tmp = Accessible_getSelection (accessible);
591 g_assert (tmp != NULL);
593 fprintf (stderr, "Se");
594 AccessibleSelection_unref (tmp);
597 if (Accessible_isTable (accessible)) {
598 tmp = Accessible_getTable (accessible);
599 g_assert (tmp != NULL);
601 fprintf (stderr, "Ta");
604 AccessibleTable_unref (tmp);
607 if (Accessible_isText (accessible)) {
608 tmp = Accessible_getText (accessible);
609 g_assert (tmp != NULL);
611 fprintf (stderr, "Te");
613 if (strcmp (name, TEST_STRING_A_OBJECT) == 0)
616 AccessibleText_unref (tmp);
619 if (Accessible_isValue (accessible)) {
620 tmp = Accessible_getValue (accessible);
621 g_assert (tmp != NULL);
623 fprintf (stderr, "Va");
626 AccessibleValue_unref (tmp);
630 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
631 name, descr, role_name, item_str->str);
633 SPI_freeString (name);
634 SPI_freeString (descr);
635 SPI_freeString (role_name);
636 g_string_free (item_str, TRUE);
638 validate_tree (accessible, has_parent, recurse_down);
644 fprintf (stderr, "Testing misc bits ...\n");
646 g_assert (!Accessible_isComponent (NULL));
647 g_assert (Accessible_getComponent (NULL) == NULL);
648 SPI_freeString (NULL);
652 global_listener_cb (const AccessibleEvent *event,
655 TestWindow *win = user_data;
657 AccessibleApplication *application;
659 g_assert (win->magic == WINDOW_MAGIC);
660 g_assert (!strcmp (event->type, "focus:"));
662 fprintf (stderr, "Fielded focus event ...\n");
665 desktop = SPI_getDesktop (0);
666 application = Accessible_getChildAtIndex (desktop, 0);
667 g_assert (application != NULL);
668 Accessible_unref (desktop);
670 test_application (application);
672 AccessibleApplication_unref (application);
676 validate_accessible (event->source, TRUE, TRUE);
678 fprintf (stderr, "quitting mainloop.\n");
683 validate_accessible (event->source, TRUE, TRUE);
687 key_listener_cb (const AccessibleKeystroke *stroke,
690 AccessibleKeystroke *s = user_data;
693 if (stroke->keystring) s->keystring = g_strdup (stroke->keystring);
695 if (s->type == SPI_KEY_PRESSED)
696 key_press_received = TRUE;
697 else if (s->type == SPI_KEY_RELEASED)
698 key_release_received = TRUE;
705 test_keylisteners (void)
708 AccessibleKeystroke stroke;
709 AccessibleKeystrokeListener *key_listener;
710 AccessibleKeySet *test_keyset;
712 fprintf (stderr, "Testing keyboard listeners ...\n");
714 key_listener = SPI_createAccessibleKeystrokeListener (
715 key_listener_cb, &stroke);
717 test_keyset = SPI_createAccessibleKeySet (1, "=", NULL, NULL);
719 g_assert (SPI_registerAccessibleKeystrokeListener (
723 SPI_KEY_PRESSED | SPI_KEY_RELEASED,
724 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS));
726 for (i = 0; i < 3; i++) {
727 memset (&stroke, 0, sizeof (AccessibleKeystroke));
728 g_assert (SPI_generateKeyboardEvent ('=', NULL, SPI_KEY_SYM));
729 while (!(key_press_received))
730 g_main_context_iteration (NULL, TRUE);
731 fprintf (stderr, "p [%s]", stroke.keystring);
732 g_assert (!strcmp (stroke.keystring, "="));
733 while (!(key_release_received))
734 g_main_context_iteration (NULL, TRUE);
735 fprintf (stderr, "r [%s]", stroke.keystring);
736 key_press_received = FALSE;
737 key_release_received = FALSE;
739 g_assert (SPI_deregisterAccessibleKeystrokeListener (key_listener, 0));
740 SPI_freeAccessibleKeySet (test_keyset);
742 fprintf (stderr, "\n");
744 AccessibleKeystrokeListener_unref (key_listener);
746 g_assert (SPI_generateMouseEvent (100, 100, "rel"));
747 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
748 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
749 g_assert (SPI_generateMouseEvent (-1, -1, "b1c"));
753 main (int argc, char **argv)
758 AccessibleEventListener *global_listener;
760 modules = g_getenv ("GTK_MODULES");
761 if (!modules || modules [0] == '\0')
762 putenv ("GTK_MODULES=gail:atk-bridge");
765 for (i = 1; i < argc; i++) {
766 if (!g_strcasecmp (argv [i], "--poke"))
770 gtk_init (&argc, &argv);
772 g_assert (!SPI_init ());
773 g_assert (SPI_init ());
774 g_assert (SPI_getDesktopCount () == 1);
779 test_keylisteners ();
781 win = create_test_window ();
783 global_listener = SPI_createAccessibleEventListener (global_listener_cb, win);
785 g_assert (SPI_registerGlobalEventListener (global_listener, "focus:"));
787 fprintf (stderr, "Waiting for focus event ...\n");
790 g_assert (SPI_deregisterGlobalEventListenerAll (global_listener));
791 AccessibleEventListener_unref (global_listener);
793 test_window_destroy (win);
795 /* Wait for any pending events from the registry */
797 for (i = 0; i < 100; i++)
798 CORBA_ORB_perform_work (NULL, NULL);
800 if ((leaked = SPI_exit ()))
801 g_error ("Leaked %d SPI handles", leaked);
803 g_assert (!SPI_exit ());
805 fprintf (stderr, "All tests passed\n");
807 if (g_getenv ("_MEMPROF_SOCKET")) {
808 fprintf (stderr, "Waiting for memprof\n");
812 putenv ("AT_BRIDGE_SHUTDOWN=1");