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;
190 fprintf (stderr, "Testing desktop...\n");
192 g_assert (SPI_getDesktop (-1) == NULL);
193 desktop = SPI_getDesktop (0);
194 g_assert (desktop != NULL);
196 g_assert ((length = SPI_getDesktopList (&list)) > 0);
197 g_assert (list[0] == desktop);
198 SPI_freeDesktopList (list);
200 validate_accessible (desktop, FALSE, FALSE);
202 application = Accessible_getChildAtIndex (desktop, 0);
203 g_assert (application != NULL);
204 AccessibleApplication_unref (application);
206 Accessible_unref (desktop);
210 test_application (Accessible *application)
214 fprintf (stderr, "Testing application ...\n");
215 g_assert (Accessible_isApplication (application));
216 g_assert (Accessible_getApplication (application) ==
218 AccessibleApplication_unref (application);
220 str = AccessibleApplication_getToolkitName (application);
221 g_assert (str != NULL);
222 g_assert (!strcmp (str, "GAIL"));
223 SPI_freeString (str);
225 str = AccessibleApplication_getVersion (application);
226 g_assert (str != NULL);
227 SPI_freeString (str);
229 AccessibleApplication_getID (application);
233 test_editable_text (AccessibleEditableText *etext)
236 AccessibleText *text;
238 fprintf (stderr, "Testing editable text ...\n");
240 g_assert (Accessible_isText (etext));
241 text = Accessible_getText (etext);
243 AccessibleEditableText_setTextContents (
244 etext, TEST_STRING_B);
246 str = AccessibleText_getText (text, 0, -1);
247 g_assert (!strcmp (str, TEST_STRING_B));
249 SPI_freeString (str);
251 /* FIXME: lots more editing here */
253 AccessibleEditableText_setTextContents (
254 etext, TEST_STRING_A);
256 AccessibleText_unref (text);
260 test_table (AccessibleTable *table)
266 fprintf (stderr, "Testing table ...\n");
268 rows = AccessibleTable_getNRows (table);
271 columns = AccessibleTable_getNColumns (table);
272 g_assert (columns > 0);
274 index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
276 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
278 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
280 g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
281 Accessible_unref (header);
283 AccessibleTable_isSelected (table, 0, 0);
285 /* FIXME: lots more tests */
289 test_text (AccessibleText *text)
293 fprintf (stderr, "Testing text ...\n");
295 g_assert (AccessibleText_getCharacterCount (text) ==
296 strlen (TEST_STRING_A));
298 str = AccessibleText_getText (text, 0, -1);
299 g_assert (!strcmp (str, TEST_STRING_A));
300 SPI_freeString (str);
302 str = AccessibleText_getText (text, 0, 5);
303 g_assert (!strncmp (str, TEST_STRING_A, 5));
304 SPI_freeString (str);
306 AccessibleText_setCaretOffset (text, 7);
307 g_assert (AccessibleText_getCaretOffset (text) == 7);
309 /* FIXME: lots more tests - selections etc. etc. */
313 test_value (AccessibleValue *value)
315 float original_value;
317 fprintf (stderr, "Testing value ...\n");
319 original_value = AccessibleValue_getCurrentValue (value);
321 g_assert (AccessibleValue_getCurrentValue (value) <=
322 AccessibleValue_getMaximumValue (value));
324 g_assert (AccessibleValue_getCurrentValue (value) >=
325 AccessibleValue_getMinimumValue (value));
327 AccessibleValue_setCurrentValue (value,
328 AccessibleValue_getMinimumValue (value));
330 g_assert (AccessibleValue_getCurrentValue (value) ==
331 AccessibleValue_getMinimumValue (value));
333 AccessibleValue_setCurrentValue (value,
334 AccessibleValue_getMaximumValue (value));
336 g_assert (AccessibleValue_getCurrentValue (value) ==
337 AccessibleValue_getMaximumValue (value));
339 AccessibleValue_setCurrentValue (value, original_value);
341 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
345 test_component (AccessibleComponent *component)
347 long x, y, width, height;
349 fprintf (stderr, "Testing component...\n");
351 AccessibleComponent_getExtents (
352 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
354 AccessibleComponent_getPosition (
355 component, &x, &y, SPI_COORD_TYPE_SCREEN);
357 AccessibleComponent_getSize (component, &width, &height);
359 if (width > 0 && height > 0) {
361 Accessible *accessible, *componentb;
364 g_assert (AccessibleComponent_contains (
365 component, x, y, SPI_COORD_TYPE_SCREEN));
367 g_assert (AccessibleComponent_contains (
368 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
370 g_assert (AccessibleComponent_contains (
371 component, x + width - 1, y + height - 1,
372 SPI_COORD_TYPE_SCREEN));
375 accessible = AccessibleComponent_getAccessibleAtPoint (
376 component, x, y, SPI_COORD_TYPE_SCREEN);
378 g_assert (Accessible_isComponent (accessible));
379 componentb = Accessible_getComponent (accessible);
380 g_assert (componentb == component);
382 AccessibleComponent_unref (componentb);
383 Accessible_unref (accessible);
387 AccessibleComponent_getLayer (component);
388 AccessibleComponent_getMDIZOrder (component);
389 /* AccessibleComponent_grabFocus (component); */
393 test_image (AccessibleImage *image)
396 long int x = -1, y = -1, width = -1, height = -1;
398 desc = AccessibleImage_getImageDescription (image);
399 g_assert (desc != NULL);
400 SPI_freeString (desc);
402 AccessibleImage_getImagePosition (image, &x, &y,
403 SPI_COORD_TYPE_SCREEN);
404 AccessibleImage_getImageSize (image, &width, &height);
405 AccessibleImage_getImageExtents (image, &x, &y, &width, &height,
406 SPI_COORD_TYPE_WINDOW);
410 validate_tree (Accessible *accessible,
412 gboolean recurse_down)
417 parent = Accessible_getParent (accessible);
420 Accessible *child_at_index;
422 g_assert (parent != NULL);
424 index = Accessible_getIndexInParent (accessible);
425 g_assert (index >= 0);
427 child_at_index = Accessible_getChildAtIndex (parent, index);
429 g_assert (child_at_index == accessible);
431 Accessible_unref (child_at_index);
432 Accessible_unref (parent);
435 len = Accessible_getChildCount (accessible);
437 for (i = 0; i < len; i++) {
440 child = Accessible_getChildAtIndex (accessible, i);
443 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
445 g_assert (Accessible_getIndexInParent (child) == i);
446 g_assert (Accessible_getParent (child) == accessible);
449 if (recurse_down && child)
450 validate_accessible (child, has_parent, recurse_down);
452 Accessible_unref (child);
458 validate_accessible (Accessible *accessible,
460 gboolean recurse_down)
466 GString *item_str = g_string_new ("");
468 name = Accessible_getName (accessible);
469 g_assert (name != NULL);
471 descr = Accessible_getDescription (accessible);
472 g_assert (descr != NULL);
474 role = Accessible_getRole (accessible);
475 g_assert (role != SPI_ROLE_INVALID);
476 role_name = Accessible_getRoleName (accessible);
477 g_assert (role_name != NULL);
483 for (i = 0; i < print_tree_depth; i++)
485 fputs ("|-> [ ", stderr);
488 if (Accessible_isAction (accessible)) {
489 tmp = Accessible_getAction (accessible);
490 g_assert (tmp != NULL);
492 fprintf (stderr, "At");
495 AccessibleAction_unref (tmp);
498 if (Accessible_isApplication (accessible)) {
499 tmp = Accessible_getApplication (accessible);
501 fprintf (stderr, "Ap");
503 test_application (tmp);
504 AccessibleApplication_unref (tmp);
507 if (Accessible_isComponent (accessible)) {
508 tmp = Accessible_getComponent (accessible);
509 g_assert (tmp != NULL);
511 fprintf (stderr, "Co");
513 test_component (tmp);
514 AccessibleComponent_unref (tmp);
517 if (Accessible_isEditableText (accessible)) {
518 tmp = Accessible_getEditableText (accessible);
519 g_assert (tmp != NULL);
521 fprintf (stderr, "Et");
523 test_editable_text (tmp);
524 AccessibleEditableText_unref (tmp);
527 if (Accessible_isHypertext (accessible)) {
528 tmp = Accessible_getHypertext (accessible);
529 g_assert (tmp != NULL);
531 fprintf (stderr, "Ht");
532 AccessibleHypertext_unref (tmp);
535 if (Accessible_isImage (accessible)) {
536 tmp = Accessible_getImage (accessible);
537 g_assert (tmp != NULL);
541 fprintf (stderr, "Im");
543 desc = AccessibleImage_getImageDescription (tmp);
544 g_string_append_printf (
545 item_str, " image descr: '%s'", desc);
546 SPI_freeString (desc);
550 AccessibleImage_unref (tmp);
553 if (Accessible_isSelection (accessible)) {
554 tmp = Accessible_getSelection (accessible);
555 g_assert (tmp != NULL);
557 fprintf (stderr, "Se");
558 AccessibleSelection_unref (tmp);
561 if (Accessible_isTable (accessible)) {
562 tmp = Accessible_getTable (accessible);
563 g_assert (tmp != NULL);
565 fprintf (stderr, "Ta");
568 AccessibleTable_unref (tmp);
571 if (Accessible_isText (accessible)) {
572 tmp = Accessible_getText (accessible);
573 g_assert (tmp != NULL);
575 fprintf (stderr, "Te");
578 AccessibleText_unref (tmp);
581 if (Accessible_isValue (accessible)) {
582 tmp = Accessible_getValue (accessible);
583 g_assert (tmp != NULL);
585 fprintf (stderr, "Va");
588 AccessibleValue_unref (tmp);
592 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
593 name, descr, role_name, item_str->str);
595 SPI_freeString (name);
596 SPI_freeString (descr);
597 SPI_freeString (role_name);
598 g_string_free (item_str, TRUE);
600 validate_tree (accessible, has_parent, recurse_down);
606 fprintf (stderr, "Testing misc bits ...\n");
608 g_assert (!Accessible_isComponent (NULL));
609 g_assert (Accessible_getComponent (NULL) == NULL);
610 SPI_freeString (NULL);
614 global_listener_cb (const AccessibleEvent *event,
617 TestWindow *win = user_data;
619 AccessibleApplication *application;
621 g_assert (win->magic == WINDOW_MAGIC);
622 g_assert (!strcmp (event->type, "focus:"));
624 fprintf (stderr, "Fielded focus event ...\n");
627 desktop = SPI_getDesktop (0);
628 application = Accessible_getChildAtIndex (desktop, 0);
629 g_assert (application != NULL);
630 Accessible_unref (desktop);
632 test_application (application);
634 AccessibleApplication_unref (application);
637 validate_accessible (event->source, TRUE, TRUE);
643 validate_accessible (event->source, TRUE, TRUE);
647 key_listener_cb (const AccessibleKeystroke *stroke,
650 AccessibleKeystroke *s = user_data;
658 test_keylisteners (void)
661 AccessibleKeystroke stroke;
662 AccessibleKeystrokeListener *key_listener;
663 AccessibleKeySet *test_keyset;
665 fprintf (stderr, "Testing keyboard listeners ...\n");
667 key_listener = SPI_createAccessibleKeystrokeListener (
668 key_listener_cb, &stroke);
670 test_keyset = SPI_createAccessibleKeySet (1, "=", NULL, NULL);
672 g_assert (SPI_registerAccessibleKeystrokeListener (
676 SPI_KEY_PRESSED | SPI_KEY_RELEASED,
677 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS));
679 for (i = 0; i < 3; i++) {
680 memset (&stroke, 0, sizeof (AccessibleKeystroke));
681 g_assert (SPI_generateKeyboardEvent ('=', NULL, SPI_KEY_SYM));
682 while (stroke.type == 0)
683 g_main_iteration (TRUE);
686 g_assert (SPI_deregisterAccessibleKeystrokeListener (key_listener, 0));
687 SPI_freeAccessibleKeySet (test_keyset);
689 /* FIXME: expand the validation here */
690 g_assert (stroke.type == SPI_KEY_PRESSRELEASE);
692 AccessibleKeystrokeListener_unref (key_listener);
696 main (int argc, char **argv)
701 AccessibleEventListener *global_listener;
703 modules = g_getenv ("GTK_MODULES");
704 if (!modules || modules [0] == '\0')
705 putenv ("GTK_MODULES=gail:atk-bridge");
708 for (i = 1; i < argc; i++) {
709 if (!g_strcasecmp (argv [i], "--poke"))
713 gtk_init (&argc, &argv);
715 g_assert (!SPI_init ());
716 g_assert (SPI_init ());
717 g_assert (SPI_getDesktopCount () == 1);
722 test_keylisteners ();
724 win = create_test_window ();
726 global_listener = SPI_createAccessibleEventListener (global_listener_cb, win);
727 g_assert (SPI_registerGlobalEventListener (global_listener, "focus:"));
729 fprintf (stderr, "Waiting for focus event ...\n");
732 g_assert (SPI_deregisterGlobalEventListenerAll (global_listener));
733 AccessibleEventListener_unref (global_listener);
735 test_window_destroy (win);
737 if ((leaked = SPI_exit ()))
738 g_error ("Leaked %d SPI handles", leaked);
740 g_assert (!SPI_exit ());
742 fprintf (stderr, "All tests passed\n");
744 if (g_getenv ("_MEMPROF_SOCKET")) {
745 fprintf (stderr, "Waiting for memprof\n");
749 putenv ("AT_BRIDGE_SHUTDOWN=1");