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;
51 focus_me (GtkWidget *widget)
53 AtkObject *aobject = atk_implementor_ref_accessible (
54 ATK_IMPLEMENTOR (widget));
56 /* Force a focus event - even if the WM focused
57 * us before our at-bridge's idle handler registered
59 if (!GTK_WIDGET_HAS_FOCUS (widget))
60 gtk_widget_grab_focus (widget);
61 /* else: FIXME - gtk_widget_grab_focus should send a notify */
62 atk_focus_tracker_notify (aobject);
64 g_object_unref (G_OBJECT (aobject));
70 test_window_add_and_show (GtkContainer *container, GtkWidget *widget)
72 gtk_container_add (container, widget);
73 gtk_widget_show (widget);
77 create_test_window (void)
79 TestWindow *win = g_new0 (TestWindow, 1);
80 GtkWidget *widget, *vbox;
82 GtkTreeViewColumn *column;
85 win->magic = WINDOW_MAGIC;
86 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
88 gtk_widget_show (win->window);
90 vbox = gtk_vbox_new (0, 0);
91 gtk_container_add (GTK_CONTAINER (win->window), vbox);
92 gtk_widget_show (vbox);
94 widget = gtk_entry_new ();
95 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
96 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
98 widget = g_object_new (GTK_TYPE_RANGE, NULL);
99 gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
100 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
102 store = gtk_list_store_new (1, G_TYPE_STRING);
103 gtk_list_store_append (store, &iter);
104 gtk_list_store_set (store, &iter, 0, TEST_STRING_A, -1);
105 column = gtk_tree_view_column_new_with_attributes ("String",
106 gtk_cell_renderer_text_new (), "text", 0, NULL);
107 widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
108 g_object_unref (G_OBJECT (store));
109 gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
110 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
112 g_idle_add ((GSourceFunc) focus_me, win->window);
118 test_window_destroy (TestWindow *win)
120 gtk_widget_destroy (win->window);
129 fprintf (stderr, "Testing roles...\n");
130 for (i = -1; i < 1000; i++)
131 g_assert (AccessibleRole_getName (i) != NULL);
133 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
134 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
135 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
136 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
143 Accessible *application;
145 fprintf (stderr, "Testing desktop...\n");
147 g_assert (getDesktop (-1) == NULL);
148 desktop = getDesktop (0);
149 g_assert (desktop != NULL);
151 validate_accessible (desktop, FALSE, FALSE);
153 application = Accessible_getChildAtIndex (desktop, 0);
154 g_assert (application != NULL);
155 AccessibleApplication_unref (application);
157 Accessible_unref (desktop);
161 test_application (Accessible *application)
165 fprintf (stderr, "Testing application ...\n");
166 g_assert (Accessible_isApplication (application));
167 g_assert (Accessible_getApplication (application) ==
169 AccessibleApplication_unref (application);
171 str = AccessibleApplication_getToolkitName (application);
172 g_assert (str != NULL);
173 g_assert (!strcmp (str, "GAIL"));
174 SPI_freeString (str);
176 str = AccessibleApplication_getVersion (application);
177 g_assert (str != NULL);
178 SPI_freeString (str);
180 AccessibleApplication_getID (application);
184 test_editable_text (AccessibleEditableText *etext)
187 AccessibleText *text;
189 fprintf (stderr, "Testing editable text ...\n");
191 g_assert (Accessible_isText (etext));
192 text = Accessible_getText (etext);
194 AccessibleEditableText_setTextContents (
195 etext, TEST_STRING_B);
197 str = AccessibleText_getText (text, 0, -1);
198 g_assert (!strcmp (str, TEST_STRING_B));
200 SPI_freeString (str);
202 /* FIXME: lots more editing here */
204 AccessibleEditableText_setTextContents (
205 etext, TEST_STRING_A);
207 AccessibleText_unref (text);
211 test_table (AccessibleTable *table)
217 fprintf (stderr, "Testing table ...\n");
219 rows = AccessibleTable_getNRows (table);
222 columns = AccessibleTable_getNColumns (table);
223 g_assert (columns > 0); /* weird that this fails, surely a bug ? */
225 index = AccessibleTable_getIndexAt (table, rows-1, columns-1);
227 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows-1);
229 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns-1);
231 g_assert (AccessibleTable_getColumnHeader (table, 0));
232 /* maybe bogus assertion */
234 AccessibleTable_isSelected (table, 0, 0);
235 /* no assertion, but see if warnings are thrown */
237 /* FIXME: lots more tests */
241 test_text (AccessibleText *text)
245 fprintf (stderr, "Testing text ...\n");
247 g_assert (AccessibleText_getCharacterCount (text) ==
248 strlen (TEST_STRING_A));
250 str = AccessibleText_getText (text, 0, -1);
251 g_assert (!strcmp (str, TEST_STRING_A));
252 SPI_freeString (str);
254 str = AccessibleText_getText (text, 0, 5);
255 g_assert (!strncmp (str, TEST_STRING_A, 5));
256 SPI_freeString (str);
258 AccessibleText_setCaretOffset (text, 7);
259 g_assert (AccessibleText_getCaretOffset (text) == 7);
261 /* FIXME: lots more tests - selections etc. etc. */
265 test_value (AccessibleValue *value)
268 float original_value;
270 /* Note: test_value assertions are known not to work as of Dec 09 */
272 fprintf (stderr, "Testing value ...\n");
274 original_value = AccessibleValue_getCurrentValue (value);
276 g_assert (AccessibleValue_getCurrentValue (value) <=
277 AccessibleValue_getMaximumValue (value));
279 g_assert (AccessibleValue_getCurrentValue (value) >=
280 AccessibleValue_getMinimumValue (value));
282 AccessibleValue_setCurrentValue (value,
283 AccessibleValue_getMinimumValue (value));
285 g_assert (AccessibleValue_getCurrentValue (value) ==
286 AccessibleValue_getMinimumValue (value));
288 AccessibleValue_setCurrentValue (value,
289 AccessibleValue_getMaximumValue (value));
291 g_assert (AccessibleValue_getCurrentValue (value) ==
292 AccessibleValue_getMaximumValue (value));
294 AccessibleValue_setCurrentValue (value, original_value);
296 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
300 test_component (AccessibleComponent *component)
302 long x, y, width, height;
304 fprintf (stderr, "Testing component...\n");
306 AccessibleComponent_getExtents (
307 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
309 AccessibleComponent_getPosition (
310 component, &x, &y, SPI_COORD_TYPE_SCREEN);
312 AccessibleComponent_getSize (component, &width, &height);
314 if (width > 0 && height > 0) {
316 Accessible *accessible, *componentb;
319 g_assert (AccessibleComponent_contains (
320 component, x, y, SPI_COORD_TYPE_SCREEN));
322 g_assert (AccessibleComponent_contains (
323 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
325 g_assert (AccessibleComponent_contains (
326 component, x + width - 1, y + height - 1,
327 SPI_COORD_TYPE_SCREEN));
330 accessible = AccessibleComponent_getAccessibleAtPoint (
331 component, x, y, SPI_COORD_TYPE_SCREEN);
333 g_assert (Accessible_isComponent (accessible));
334 componentb = Accessible_getComponent (accessible);
335 g_assert (componentb == component);
337 AccessibleComponent_unref (componentb);
338 Accessible_unref (accessible);
342 AccessibleComponent_getLayer (component);
343 AccessibleComponent_getMDIZOrder (component);
344 /* AccessibleComponent_grabFocus (component); */
348 validate_tree (Accessible *accessible,
350 gboolean recurse_down)
355 parent = Accessible_getParent (accessible);
358 Accessible *child_at_index;
360 g_assert (parent != NULL);
362 index = Accessible_getIndexInParent (accessible);
363 g_assert (index >= 0);
365 child_at_index = Accessible_getChildAtIndex (parent, index);
367 g_assert (child_at_index == accessible);
369 Accessible_unref (child_at_index);
370 Accessible_unref (parent);
373 len = Accessible_getChildCount (accessible);
375 for (i = 0; i < len; i++) {
378 child = Accessible_getChildAtIndex (accessible, i);
381 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
383 g_assert (Accessible_getIndexInParent (child) == i);
384 g_assert (Accessible_getParent (child) == accessible);
387 if (recurse_down && child)
388 validate_accessible (child, has_parent, recurse_down);
390 Accessible_unref (child);
396 validate_accessible (Accessible *accessible,
398 gboolean recurse_down)
404 name = Accessible_getName (accessible);
405 g_assert (name != NULL);
407 descr = Accessible_getDescription (accessible);
408 g_assert (descr != NULL);
410 role = Accessible_getRole (accessible);
411 g_assert (role != NULL);
416 for (i = 0; i < print_tree_depth; i++)
418 fputs ("|-> [ ", stderr);
421 if (Accessible_isAction (accessible)) {
422 tmp = Accessible_getAction (accessible);
423 g_assert (tmp != NULL);
425 fprintf (stderr, "At");
426 AccessibleAction_unref (tmp);
429 if (Accessible_isApplication (accessible)) {
430 tmp = Accessible_getApplication (accessible);
432 fprintf (stderr, "Ap");
434 test_application (tmp);
435 AccessibleApplication_unref (tmp);
438 if (Accessible_isComponent (accessible)) {
439 tmp = Accessible_getComponent (accessible);
440 g_assert (tmp != NULL);
442 fprintf (stderr, "Co");
444 test_component (tmp);
445 AccessibleComponent_unref (tmp);
448 if (Accessible_isEditableText (accessible)) {
449 tmp = Accessible_getEditableText (accessible);
450 g_assert (tmp != NULL);
452 fprintf (stderr, "Et");
454 test_editable_text (tmp);
455 AccessibleEditableText_unref (tmp);
458 if (Accessible_isHypertext (accessible)) {
459 tmp = Accessible_getHypertext (accessible);
460 g_assert (tmp != NULL);
462 fprintf (stderr, "Ht");
463 AccessibleHypertext_unref (tmp);
466 if (Accessible_isImage (accessible)) {
467 tmp = Accessible_getImage (accessible);
468 g_assert (tmp != NULL);
470 fprintf (stderr, "Im");
471 AccessibleImage_unref (accessible);
474 if (Accessible_isSelection (accessible)) {
475 tmp = Accessible_getSelection (accessible);
476 g_assert (tmp != NULL);
478 fprintf (stderr, "Se");
479 AccessibleSelection_unref (tmp);
482 if (Accessible_isTable (accessible)) {
483 tmp = Accessible_getTable (accessible);
484 g_assert (tmp != NULL);
486 fprintf (stderr, "Ta");
489 AccessibleTable_unref (tmp);
492 if (Accessible_isText (accessible)) {
493 tmp = Accessible_getText (accessible);
494 g_assert (tmp != NULL);
496 fprintf (stderr, "Te");
499 AccessibleText_unref (tmp);
502 if (Accessible_isValue (accessible)) {
503 tmp = Accessible_getValue (accessible);
504 g_assert (tmp != NULL);
506 fprintf (stderr, "Va");
509 AccessibleValue_unref (tmp);
513 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
515 SPI_freeString (name);
516 SPI_freeString (descr);
518 validate_tree (accessible, has_parent, recurse_down);
524 fprintf (stderr, "Testing misc bits ...\n");
526 g_assert (!Accessible_isComponent (NULL));
527 g_assert (Accessible_getComponent (NULL) == NULL);
528 SPI_freeString (NULL);
532 global_listener_cb (AccessibleEvent *event,
535 TestWindow *win = user_data;
537 AccessibleApplication *application;
539 g_assert (win->magic == WINDOW_MAGIC);
540 g_assert (!strcmp (event->type, "focus:"));
542 fprintf (stderr, "Fielded focus event ...\n");
544 desktop = getDesktop (0);
545 application = Accessible_getChildAtIndex (desktop, 0);
546 g_assert (application != NULL);
547 Accessible_unref (desktop);
549 test_application (application);
551 AccessibleApplication_unref (application);
554 validate_accessible (event->source, TRUE, TRUE);
556 validate_accessible (event->source, TRUE, TRUE);
562 main (int argc, char **argv)
566 const char *modules, *tmp;
567 AccessibleEventListener *global_listener;
569 modules = g_getenv ("GTK_MODULES");
570 if (!modules || modules [0] == '\0')
571 putenv ("GTK_MODULES=gail:at-bridge");
574 gtk_init (&argc, &argv);
576 g_assert (!SPI_init ());
577 g_assert (SPI_init ());
578 g_assert (getDesktopCount () == 1);
584 win = create_test_window ();
586 global_listener = createAccessibleEventListener (global_listener_cb, win);
587 g_assert (registerGlobalEventListener (global_listener, "focus:"));
589 fprintf (stderr, "Waiting for focus event ...\n");
592 g_assert (deregisterGlobalEventListenerAll (global_listener));
593 AccessibleEventListener_unref (global_listener);
595 test_window_destroy (win);
597 if ((leaked = SPI_exit ()))
598 g_error ("Leaked %d SPI handles", leaked);
600 g_assert (!SPI_exit ());
602 fprintf (stderr, "All tests passed\n");
604 if (g_getenv ("_MEMPROF_SOCKET")) {
605 fprintf (stderr, "Waiting for memprof\n");
609 putenv ("AT_BRIDGE_SHUTDOWN=1");