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>
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_B "Another test string"
47 static int print_tree_depth = 0;
48 static gboolean print_tree = FALSE;
49 static gboolean do_poke = FALSE;
57 focus_me (GtkWidget *widget)
59 AtkObject *aobject = atk_implementor_ref_accessible (
60 ATK_IMPLEMENTOR (widget));
62 /* Force a focus event - even if the WM focused
63 * us before our at-bridge's idle handler registered
65 if (!GTK_WIDGET_HAS_FOCUS (widget))
66 gtk_widget_grab_focus (widget);
67 /* else: FIXME - gtk_widget_grab_focus should send a notify */
68 atk_focus_tracker_notify (aobject);
70 g_object_unref (G_OBJECT (aobject));
76 test_window_add_and_show (GtkContainer *container, GtkWidget *widget)
78 gtk_container_add (container, widget);
79 gtk_widget_show (widget);
88 GtkTreeViewColumn *column;
90 store = gtk_list_store_new (1, G_TYPE_STRING);
91 gtk_list_store_append (store, &iter);
92 gtk_list_store_set (store, &iter, 0, TEST_STRING_A, -1);
93 column = gtk_tree_view_column_new_with_attributes ("String",
94 gtk_cell_renderer_text_new (), "text", 0, NULL);
95 widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
96 g_object_unref (G_OBJECT (store));
97 gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
103 create_test_window (void)
105 TestWindow *win = g_new0 (TestWindow, 1);
106 GtkWidget *widget, *vbox;
108 win->magic = WINDOW_MAGIC;
109 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
111 gtk_widget_show (win->window);
113 vbox = gtk_vbox_new (0, 0);
114 gtk_container_add (GTK_CONTAINER (win->window), vbox);
115 gtk_widget_show (vbox);
117 widget = gtk_entry_new ();
118 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
119 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
121 widget = gtk_button_new_with_label ("_Foobar");
122 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
124 widget = gtk_hseparator_new ();
125 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
127 widget = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION,
128 GTK_ICON_SIZE_LARGE_TOOLBAR);
129 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
131 widget = g_object_new (GTK_TYPE_RANGE, NULL);
132 gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
133 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
135 widget = create_tree ();
136 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
138 g_idle_add ((GSourceFunc) focus_me, win->window);
144 test_window_destroy (TestWindow *win)
146 gtk_widget_destroy (win->window);
155 fprintf (stderr, "Testing roles...\n");
156 for (i = -1; i < 1000; i++)
157 g_assert (AccessibleRole_getName (i) != NULL);
159 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
160 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
161 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
162 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
166 test_action (AccessibleAction *action)
170 g_assert ((n_actions = AccessibleAction_getNActions (action)) >= 0);
172 fprintf (stderr, "Testing actions...");
173 for (i = 0; i < n_actions; ++i)
175 s = AccessibleAction_getName (action, i);
177 sd = AccessibleAction_getDescription (action, i);
179 fprintf (stderr, "%d: %s (%s); ", i, s, sd);
182 g_assert (AccessibleAction_doAction (action, i));
184 fprintf (stderr, "\n");
191 Accessible *application;
195 fprintf (stderr, "Testing desktop...\n");
197 g_assert (SPI_getDesktop (-1) == NULL);
198 desktop = SPI_getDesktop (0);
199 g_assert (desktop != NULL);
201 g_assert ((length = SPI_getDesktopList (&list)) > 0);
202 g_assert (list[0] == desktop);
203 SPI_freeDesktopList (list);
205 validate_accessible (desktop, FALSE, FALSE);
207 application = Accessible_getChildAtIndex (desktop, 0);
208 g_assert (application != NULL);
209 AccessibleApplication_unref (application);
211 Accessible_unref (desktop);
215 test_application (Accessible *application)
219 fprintf (stderr, "Testing application ...\n");
220 g_assert (Accessible_isApplication (application));
221 g_assert (Accessible_getApplication (application) ==
223 AccessibleApplication_unref (application);
225 str = AccessibleApplication_getToolkitName (application);
226 g_assert (str != NULL);
227 g_assert (!strcmp (str, "GAIL"));
228 SPI_freeString (str);
230 str = AccessibleApplication_getVersion (application);
231 g_assert (str != NULL);
232 SPI_freeString (str);
234 AccessibleApplication_getID (application);
238 test_editable_text (AccessibleEditableText *etext)
241 AccessibleText *text;
243 fprintf (stderr, "Testing editable text ...\n");
245 g_assert (Accessible_isText (etext));
246 text = Accessible_getText (etext);
248 AccessibleEditableText_setTextContents (
249 etext, TEST_STRING_B);
251 str = AccessibleText_getText (text, 0, WHOLE_STRING);
252 g_assert (!strcmp (str, TEST_STRING_B));
254 SPI_freeString (str);
256 /* FIXME: lots more editing here */
258 AccessibleEditableText_setTextContents (
259 etext, TEST_STRING_A);
261 AccessibleText_unref (text);
265 test_table (AccessibleTable *table)
271 fprintf (stderr, "Testing table ...\n");
273 rows = AccessibleTable_getNRows (table);
276 columns = AccessibleTable_getNColumns (table);
277 g_assert (columns > 0);
279 index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
281 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
283 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
285 g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
286 Accessible_unref (header);
288 AccessibleTable_isSelected (table, 0, 0);
290 /* FIXME: lots more tests */
294 test_text (AccessibleText *text)
298 fprintf (stderr, "Testing text ...\n");
300 g_assert (AccessibleText_getCharacterCount (text) ==
301 strlen (TEST_STRING_A));
303 str = AccessibleText_getText (text, 0, WHOLE_STRING);
304 g_assert (!strcmp (str, TEST_STRING_A));
305 SPI_freeString (str);
307 str = AccessibleText_getText (text, 0, 5);
308 g_assert (!strncmp (str, TEST_STRING_A, 5));
309 SPI_freeString (str);
311 AccessibleText_setCaretOffset (text, 7);
312 g_assert (AccessibleText_getCaretOffset (text) == 7);
314 /* FIXME: lots more tests - selections etc. etc. */
318 test_value (AccessibleValue *value)
320 float original_value;
322 fprintf (stderr, "Testing value ...\n");
324 original_value = AccessibleValue_getCurrentValue (value);
326 g_assert (AccessibleValue_getCurrentValue (value) <=
327 AccessibleValue_getMaximumValue (value));
329 g_assert (AccessibleValue_getCurrentValue (value) >=
330 AccessibleValue_getMinimumValue (value));
332 AccessibleValue_setCurrentValue (value,
333 AccessibleValue_getMinimumValue (value));
335 g_assert (AccessibleValue_getCurrentValue (value) ==
336 AccessibleValue_getMinimumValue (value));
338 AccessibleValue_setCurrentValue (value,
339 AccessibleValue_getMaximumValue (value));
341 g_assert (AccessibleValue_getCurrentValue (value) ==
342 AccessibleValue_getMaximumValue (value));
344 AccessibleValue_setCurrentValue (value, original_value);
346 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
350 test_component (AccessibleComponent *component)
352 long x, y, width, height;
354 fprintf (stderr, "Testing component...\n");
356 AccessibleComponent_getExtents (
357 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
359 AccessibleComponent_getPosition (
360 component, &x, &y, SPI_COORD_TYPE_SCREEN);
362 AccessibleComponent_getSize (component, &width, &height);
364 if (width > 0 && height > 0) {
366 Accessible *accessible, *componentb;
369 g_assert (AccessibleComponent_contains (
370 component, x, y, SPI_COORD_TYPE_SCREEN));
372 g_assert (AccessibleComponent_contains (
373 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
375 g_assert (AccessibleComponent_contains (
376 component, x + width - 1, y + height - 1,
377 SPI_COORD_TYPE_SCREEN));
380 accessible = AccessibleComponent_getAccessibleAtPoint (
381 component, x, y, SPI_COORD_TYPE_SCREEN);
383 g_assert (Accessible_isComponent (accessible));
384 componentb = Accessible_getComponent (accessible);
385 g_assert (componentb == component);
387 AccessibleComponent_unref (componentb);
388 Accessible_unref (accessible);
392 AccessibleComponent_getLayer (component);
393 AccessibleComponent_getMDIZOrder (component);
394 /* AccessibleComponent_grabFocus (component); */
398 test_image (AccessibleImage *image)
401 long int x = -1, y = -1, width = -1, height = -1;
403 desc = AccessibleImage_getImageDescription (image);
404 g_assert (desc != NULL);
405 SPI_freeString (desc);
407 AccessibleImage_getImagePosition (image, &x, &y,
408 SPI_COORD_TYPE_SCREEN);
409 AccessibleImage_getImageSize (image, &width, &height);
410 AccessibleImage_getImageExtents (image, &x, &y, &width, &height,
411 SPI_COORD_TYPE_WINDOW);
415 validate_tree (Accessible *accessible,
417 gboolean recurse_down)
422 parent = Accessible_getParent (accessible);
425 Accessible *child_at_index;
427 g_assert (parent != NULL);
429 index = Accessible_getIndexInParent (accessible);
430 g_assert (index >= 0);
432 child_at_index = Accessible_getChildAtIndex (parent, index);
434 g_assert (child_at_index == accessible);
436 Accessible_unref (child_at_index);
437 Accessible_unref (parent);
440 len = Accessible_getChildCount (accessible);
442 for (i = 0; i < len; i++) {
445 child = Accessible_getChildAtIndex (accessible, i);
448 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
450 g_assert (Accessible_getIndexInParent (child) == i);
451 g_assert (Accessible_getParent (child) == accessible);
454 if (recurse_down && child)
455 validate_accessible (child, has_parent, recurse_down);
457 Accessible_unref (child);
463 validate_accessible (Accessible *accessible,
465 gboolean recurse_down)
470 AccessibleRelation **relations;
472 GString *item_str = g_string_new ("");
475 name = Accessible_getName (accessible);
476 g_assert (name != NULL);
478 descr = Accessible_getDescription (accessible);
479 g_assert (descr != NULL);
481 role = Accessible_getRole (accessible);
482 g_assert (role != SPI_ROLE_INVALID);
483 role_name = Accessible_getRoleName (accessible);
484 g_assert (role_name != NULL);
486 relations = Accessible_getRelationSet (accessible);
487 g_assert (relations != NULL);
489 for (i = 0; relations [i]; i++) {
490 AccessibleRelationType type;
493 fprintf (stderr, "relation %d\n", i);
495 type = AccessibleRelation_getRelationType (relations [i]);
496 g_assert (type != SPI_RELATION_NULL);
498 targets = AccessibleRelation_getNTargets (relations [i]);
499 g_assert (targets != -1);
501 AccessibleRelation_unref (relations [i]);
502 relations [i] = NULL;
509 for (i = 0; i < print_tree_depth; i++)
511 fputs ("|-> [ ", stderr);
514 if (Accessible_isAction (accessible)) {
515 tmp = Accessible_getAction (accessible);
516 g_assert (tmp != NULL);
518 fprintf (stderr, "At");
521 AccessibleAction_unref (tmp);
524 if (Accessible_isApplication (accessible)) {
525 tmp = Accessible_getApplication (accessible);
527 fprintf (stderr, "Ap");
529 test_application (tmp);
530 AccessibleApplication_unref (tmp);
533 if (Accessible_isComponent (accessible)) {
534 tmp = Accessible_getComponent (accessible);
535 g_assert (tmp != NULL);
537 fprintf (stderr, "Co");
539 test_component (tmp);
540 AccessibleComponent_unref (tmp);
543 if (Accessible_isEditableText (accessible)) {
544 tmp = Accessible_getEditableText (accessible);
545 g_assert (tmp != NULL);
547 fprintf (stderr, "Et");
549 test_editable_text (tmp);
550 AccessibleEditableText_unref (tmp);
553 if (Accessible_isHypertext (accessible)) {
554 tmp = Accessible_getHypertext (accessible);
555 g_assert (tmp != NULL);
557 fprintf (stderr, "Ht");
558 AccessibleHypertext_unref (tmp);
561 if (Accessible_isImage (accessible)) {
562 tmp = Accessible_getImage (accessible);
563 g_assert (tmp != NULL);
567 fprintf (stderr, "Im");
569 desc = AccessibleImage_getImageDescription (tmp);
570 g_string_append_printf (
571 item_str, " image descr: '%s'", desc);
572 SPI_freeString (desc);
576 AccessibleImage_unref (tmp);
579 if (Accessible_isSelection (accessible)) {
580 tmp = Accessible_getSelection (accessible);
581 g_assert (tmp != NULL);
583 fprintf (stderr, "Se");
584 AccessibleSelection_unref (tmp);
587 if (Accessible_isTable (accessible)) {
588 tmp = Accessible_getTable (accessible);
589 g_assert (tmp != NULL);
591 fprintf (stderr, "Ta");
594 AccessibleTable_unref (tmp);
597 if (Accessible_isText (accessible)) {
598 tmp = Accessible_getText (accessible);
599 g_assert (tmp != NULL);
601 fprintf (stderr, "Te");
604 AccessibleText_unref (tmp);
607 if (Accessible_isValue (accessible)) {
608 tmp = Accessible_getValue (accessible);
609 g_assert (tmp != NULL);
611 fprintf (stderr, "Va");
614 AccessibleValue_unref (tmp);
618 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
619 name, descr, role_name, item_str->str);
621 SPI_freeString (name);
622 SPI_freeString (descr);
623 SPI_freeString (role_name);
624 g_string_free (item_str, TRUE);
626 validate_tree (accessible, has_parent, recurse_down);
632 fprintf (stderr, "Testing misc bits ...\n");
634 g_assert (!Accessible_isComponent (NULL));
635 g_assert (Accessible_getComponent (NULL) == NULL);
636 SPI_freeString (NULL);
640 global_listener_cb (const AccessibleEvent *event,
643 TestWindow *win = user_data;
645 AccessibleApplication *application;
647 g_assert (win->magic == WINDOW_MAGIC);
648 g_assert (!strcmp (event->type, "focus:"));
650 fprintf (stderr, "Fielded focus event ...\n");
653 desktop = SPI_getDesktop (0);
654 application = Accessible_getChildAtIndex (desktop, 0);
655 g_assert (application != NULL);
656 Accessible_unref (desktop);
658 test_application (application);
660 AccessibleApplication_unref (application);
663 validate_accessible (event->source, TRUE, TRUE);
669 validate_accessible (event->source, TRUE, TRUE);
672 #ifdef KEY_IMPL_WORKS
674 key_listener_cb (const AccessibleKeystroke *stroke,
677 AccessibleKeystroke *s = user_data;
686 test_keylisteners (void)
688 #ifdef KEY_IMPL_WORKS
690 AccessibleKeystroke stroke;
691 AccessibleKeystrokeListener *key_listener;
692 AccessibleKeySet *test_keyset;
695 fprintf (stderr, "Testing keyboard listeners ...\n");
697 #ifdef KEY_IMPL_WORKS
698 key_listener = SPI_createAccessibleKeystrokeListener (
699 key_listener_cb, &stroke);
701 test_keyset = SPI_createAccessibleKeySet (1, "=", NULL, NULL);
703 g_assert (SPI_registerAccessibleKeystrokeListener (
707 SPI_KEY_PRESSED | SPI_KEY_RELEASED,
708 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS));
710 for (i = 0; i < 3; i++) {
711 memset (&stroke, 0, sizeof (AccessibleKeystroke));
712 g_assert (SPI_generateKeyboardEvent ('=', NULL, SPI_KEY_SYM));
713 while (!(stroke.type & SPI_KEY_PRESSED))
714 g_main_context_iteration (NULL, TRUE);
715 fprintf (stderr, "p");
716 g_assert (!strcmp (stroke.keystring, "="));
717 while (!(stroke.type & SPI_KEY_RELEASED))
718 g_main_context_iteration (NULL, TRUE);
719 fprintf (stderr, "r ");
721 g_assert (SPI_deregisterAccessibleKeystrokeListener (key_listener, 0));
722 SPI_freeAccessibleKeySet (test_keyset);
724 fprintf (stderr, "\n");
726 AccessibleKeystrokeListener_unref (key_listener);
728 fprintf (stderr, " key impl. impossibly broken\n");
731 g_assert (SPI_generateMouseEvent (100, 100, "rel"));
732 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
733 g_assert (SPI_generateMouseEvent (-50, -50, "rel"));
734 g_assert (SPI_generateMouseEvent (-1, -1, "b1c"));
738 main (int argc, char **argv)
743 AccessibleEventListener *global_listener;
745 modules = g_getenv ("GTK_MODULES");
746 if (!modules || modules [0] == '\0')
747 putenv ("GTK_MODULES=gail:atk-bridge");
750 for (i = 1; i < argc; i++) {
751 if (!g_strcasecmp (argv [i], "--poke"))
755 gtk_init (&argc, &argv);
757 g_assert (!SPI_init ());
758 g_assert (SPI_init ());
759 g_assert (SPI_getDesktopCount () == 1);
764 test_keylisteners ();
766 win = create_test_window ();
768 global_listener = SPI_createAccessibleEventListener (global_listener_cb, win);
769 g_assert (SPI_registerGlobalEventListener (global_listener, "focus:"));
771 fprintf (stderr, "Waiting for focus event ...\n");
774 g_assert (SPI_deregisterGlobalEventListenerAll (global_listener));
775 AccessibleEventListener_unref (global_listener);
777 test_window_destroy (win);
779 /* Wait for any pending events from the registry */
781 for (i = 0; i < 100; i++)
782 linc_main_iteration (FALSE);
784 if ((leaked = SPI_exit ()))
785 g_error ("Leaked %d SPI handles", leaked);
787 g_assert (!SPI_exit ());
789 fprintf (stderr, "All tests passed\n");
791 if (g_getenv ("_MEMPROF_SOCKET")) {
792 fprintf (stderr, "Waiting for memprof\n");
796 putenv ("AT_BRIDGE_SHUTDOWN=1");