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;
84 win->magic = WINDOW_MAGIC;
85 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
87 gtk_widget_show (win->window);
89 vbox = gtk_vbox_new (0, 0);
90 gtk_container_add (GTK_CONTAINER (win->window), vbox);
91 gtk_widget_show (vbox);
93 widget = gtk_entry_new ();
94 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
95 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
97 widget = g_object_new (GTK_TYPE_RANGE, NULL);
98 gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
99 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
101 store = gtk_list_store_new (1, G_TYPE_INT);
102 widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
103 gtk_list_store_append (store, &iter);
104 gtk_list_store_set (store, &iter, 0, 100, -1);
105 test_window_add_and_show (GTK_CONTAINER (vbox), widget);
107 g_idle_add ((GSourceFunc) focus_me, win->window);
113 test_window_destroy (TestWindow *win)
115 gtk_widget_destroy (win->window);
124 fprintf (stderr, "Testing roles...\n");
125 for (i = -1; i < 1000; i++)
126 g_assert (AccessibleRole_getName (i) != NULL);
128 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
129 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
130 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
131 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
138 Accessible *application;
140 fprintf (stderr, "Testing desktop...\n");
142 g_assert (getDesktop (-1) == NULL);
143 desktop = getDesktop (0);
144 g_assert (desktop != NULL);
146 validate_accessible (desktop, FALSE, FALSE);
148 application = Accessible_getChildAtIndex (desktop, 0);
149 g_assert (application != NULL);
150 AccessibleApplication_unref (application);
152 Accessible_unref (desktop);
156 test_application (Accessible *application)
160 fprintf (stderr, "Testing application ...\n");
161 g_assert (Accessible_isApplication (application));
162 g_assert (Accessible_getApplication (application) ==
164 AccessibleApplication_unref (application);
166 str = AccessibleApplication_getToolkitName (application);
167 g_assert (str != NULL);
168 g_assert (!strcmp (str, "GAIL"));
169 SPI_freeString (str);
171 str = AccessibleApplication_getVersion (application);
172 g_assert (str != NULL);
173 SPI_freeString (str);
175 AccessibleApplication_getID (application);
179 test_editable_text (AccessibleEditableText *etext)
182 AccessibleText *text;
184 fprintf (stderr, "Testing editable text ...\n");
186 g_assert (Accessible_isText (etext));
187 text = Accessible_getText (etext);
189 AccessibleEditableText_setTextContents (
190 etext, TEST_STRING_B);
192 str = AccessibleText_getText (text, 0, -1);
193 g_assert (!strcmp (str, TEST_STRING_B));
195 SPI_freeString (str);
197 /* FIXME: lots more editing here */
199 AccessibleEditableText_setTextContents (
200 etext, TEST_STRING_A);
202 AccessibleText_unref (text);
206 test_table (AccessibleTable *table)
212 fprintf (stderr, "Testing table ...\n");
214 rows = AccessibleTable_getNRows (table);
217 columns = AccessibleTable_getNColumns (table);
218 g_assert (columns > 0); /* weird that this fails, surely a bug ? */
220 index = AccessibleTable_getIndexAt (table, rows-1, columns-1);
222 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows-1);
224 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns-1);
226 g_assert (AccessibleTable_getColumnHeader (table, 0));
227 /* maybe bogus assertion */
229 AccessibleTable_isSelected (table, 0, 0);
230 /* no assertion, but see if warnings are thrown */
232 /* FIXME: lots more tests */
236 test_text (AccessibleText *text)
240 fprintf (stderr, "Testing text ...\n");
242 g_assert (AccessibleText_getCharacterCount (text) ==
243 strlen (TEST_STRING_A));
245 str = AccessibleText_getText (text, 0, -1);
246 g_assert (!strcmp (str, TEST_STRING_A));
247 SPI_freeString (str);
249 str = AccessibleText_getText (text, 0, 5);
250 g_assert (!strncmp (str, TEST_STRING_A, 5));
251 SPI_freeString (str);
253 AccessibleText_setCaretOffset (text, 7);
254 g_assert (AccessibleText_getCaretOffset (text) == 7);
256 /* FIXME: lots more tests - selections etc. etc. */
260 test_value (AccessibleValue *value)
263 float original_value;
265 /* Note: test_value assertions are known not to work as of Dec 09 */
267 fprintf (stderr, "Testing value ...\n");
269 original_value = AccessibleValue_getCurrentValue (value);
271 g_assert (AccessibleValue_getCurrentValue (value) <=
272 AccessibleValue_getMaximumValue (value));
274 g_assert (AccessibleValue_getCurrentValue (value) >=
275 AccessibleValue_getMinimumValue (value));
277 AccessibleValue_setCurrentValue (value,
278 AccessibleValue_getMinimumValue (value));
280 g_assert (AccessibleValue_getCurrentValue (value) ==
281 AccessibleValue_getMinimumValue (value));
283 AccessibleValue_setCurrentValue (value,
284 AccessibleValue_getMaximumValue (value));
286 g_assert (AccessibleValue_getCurrentValue (value) ==
287 AccessibleValue_getMaximumValue (value));
289 AccessibleValue_setCurrentValue (value, original_value);
291 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
295 test_component (AccessibleComponent *component)
297 long x, y, width, height;
299 fprintf (stderr, "Testing component...\n");
301 AccessibleComponent_getExtents (
302 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
304 AccessibleComponent_getPosition (
305 component, &x, &y, SPI_COORD_TYPE_SCREEN);
307 AccessibleComponent_getSize (component, &width, &height);
309 if (width > 0 && height > 0) {
311 Accessible *accessible, *componentb;
314 g_assert (AccessibleComponent_contains (
315 component, x, y, SPI_COORD_TYPE_SCREEN));
317 g_assert (AccessibleComponent_contains (
318 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
320 g_assert (AccessibleComponent_contains (
321 component, x + width - 1, y + height - 1,
322 SPI_COORD_TYPE_SCREEN));
325 accessible = AccessibleComponent_getAccessibleAtPoint (
326 component, x, y, SPI_COORD_TYPE_SCREEN);
328 g_assert (Accessible_isComponent (accessible));
329 componentb = Accessible_getComponent (accessible);
330 g_assert (componentb == component);
332 AccessibleComponent_unref (componentb);
333 Accessible_unref (accessible);
337 AccessibleComponent_getLayer (component);
338 AccessibleComponent_getMDIZOrder (component);
339 /* AccessibleComponent_grabFocus (component); */
343 validate_tree (Accessible *accessible,
345 gboolean recurse_down)
350 parent = Accessible_getParent (accessible);
353 Accessible *child_at_index;
355 g_assert (parent != NULL);
357 index = Accessible_getIndexInParent (accessible);
358 g_assert (index >= 0);
360 child_at_index = Accessible_getChildAtIndex (parent, index);
362 g_assert (child_at_index == accessible);
364 Accessible_unref (child_at_index);
365 Accessible_unref (parent);
368 len = Accessible_getChildCount (accessible);
370 for (i = 0; i < len; i++) {
373 child = Accessible_getChildAtIndex (accessible, i);
376 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
378 g_assert (Accessible_getIndexInParent (child) == i);
379 g_assert (Accessible_getParent (child) == accessible);
382 if (recurse_down && child)
383 validate_accessible (child, has_parent, recurse_down);
385 Accessible_unref (child);
391 validate_accessible (Accessible *accessible,
393 gboolean recurse_down)
399 name = Accessible_getName (accessible);
400 g_assert (name != NULL);
402 descr = Accessible_getDescription (accessible);
403 g_assert (descr != NULL);
405 role = Accessible_getRole (accessible);
406 g_assert (role != NULL);
411 for (i = 0; i < print_tree_depth; i++)
413 fputs ("|-> [ ", stderr);
416 if (Accessible_isAction (accessible)) {
417 tmp = Accessible_getAction (accessible);
418 g_assert (tmp != NULL);
420 fprintf (stderr, "At");
421 AccessibleAction_unref (tmp);
424 if (Accessible_isApplication (accessible)) {
425 tmp = Accessible_getApplication (accessible);
427 fprintf (stderr, "Ap");
429 test_application (tmp);
430 AccessibleApplication_unref (tmp);
433 if (Accessible_isComponent (accessible)) {
434 tmp = Accessible_getComponent (accessible);
435 g_assert (tmp != NULL);
437 fprintf (stderr, "Co");
439 test_component (tmp);
440 AccessibleComponent_unref (tmp);
443 if (Accessible_isEditableText (accessible)) {
444 tmp = Accessible_getEditableText (accessible);
445 g_assert (tmp != NULL);
447 fprintf (stderr, "Et");
449 test_editable_text (tmp);
450 AccessibleEditableText_unref (tmp);
453 if (Accessible_isHypertext (accessible)) {
454 tmp = Accessible_getHypertext (accessible);
455 g_assert (tmp != NULL);
457 fprintf (stderr, "Ht");
458 AccessibleHypertext_unref (tmp);
461 if (Accessible_isImage (accessible)) {
462 tmp = Accessible_getImage (accessible);
463 g_assert (tmp != NULL);
465 fprintf (stderr, "Im");
466 AccessibleImage_unref (accessible);
469 if (Accessible_isSelection (accessible)) {
470 tmp = Accessible_getSelection (accessible);
471 g_assert (tmp != NULL);
473 fprintf (stderr, "Se");
474 AccessibleSelection_unref (tmp);
477 if (Accessible_isTable (accessible)) {
478 tmp = Accessible_getTable (accessible);
479 g_assert (tmp != NULL);
481 fprintf (stderr, "Ta");
484 AccessibleTable_unref (tmp);
487 if (Accessible_isText (accessible)) {
488 tmp = Accessible_getText (accessible);
489 g_assert (tmp != NULL);
491 fprintf (stderr, "Te");
494 AccessibleText_unref (tmp);
497 if (Accessible_isValue (accessible)) {
498 tmp = Accessible_getValue (accessible);
499 g_assert (tmp != NULL);
501 fprintf (stderr, "Va");
504 AccessibleValue_unref (tmp);
508 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
510 SPI_freeString (name);
511 SPI_freeString (descr);
513 validate_tree (accessible, has_parent, recurse_down);
519 fprintf (stderr, "Testing misc bits ...\n");
521 g_assert (!Accessible_isComponent (NULL));
522 g_assert (Accessible_getComponent (NULL) == NULL);
523 SPI_freeString (NULL);
527 global_listener_cb (AccessibleEvent *event,
530 TestWindow *win = user_data;
532 AccessibleApplication *application;
534 g_assert (win->magic == WINDOW_MAGIC);
535 g_assert (!strcmp (event->type, "focus:"));
537 fprintf (stderr, "Fielded focus event ...\n");
539 desktop = getDesktop (0);
540 application = Accessible_getChildAtIndex (desktop, 0);
541 g_assert (application != NULL);
542 Accessible_unref (desktop);
544 test_application (application);
546 AccessibleApplication_unref (application);
549 validate_accessible (event->source, TRUE, TRUE);
551 validate_accessible (event->source, TRUE, TRUE);
557 main (int argc, char **argv)
561 const char *modules, *tmp;
562 AccessibleEventListener *global_listener;
564 modules = g_getenv ("GTK_MODULES");
565 if (!modules || modules [0] == '\0')
566 putenv ("GTK_MODULES=gail:at-bridge");
569 gtk_init (&argc, &argv);
571 g_assert (!SPI_init ());
572 g_assert (SPI_init ());
573 g_assert (getDesktopCount () == 1);
579 win = create_test_window ();
581 global_listener = createAccessibleEventListener (global_listener_cb, win);
582 g_assert (registerGlobalEventListener (global_listener, "focus:"));
584 fprintf (stderr, "Waiting for focus event ...\n");
587 g_assert (deregisterGlobalEventListenerAll (global_listener));
588 AccessibleEventListener_unref (global_listener);
590 test_window_destroy (win);
592 if ((leaked = SPI_exit ()))
593 g_error ("Leaked %d SPI handles", leaked);
595 g_assert (!SPI_exit ());
597 fprintf (stderr, "All tests passed\n");
599 if (g_getenv ("_MEMPROF_SOCKET")) {
600 fprintf (stderr, "Waiting for memprof\n");
604 putenv ("AT_BRIDGE_SHUTDOWN=1");