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 create_test_window (void)
72 TestWindow *win = g_new0 (TestWindow, 1);
73 GtkWidget *widget, *vbox;
75 win->magic = WINDOW_MAGIC;
76 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
78 gtk_widget_show (win->window);
80 vbox = gtk_vbox_new (0, 0);
81 gtk_container_add (GTK_CONTAINER (win->window), vbox);
82 gtk_widget_show (vbox);
84 widget = gtk_entry_new ();
85 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
86 gtk_container_add (GTK_CONTAINER (vbox), widget);
87 gtk_widget_show (widget);
89 g_idle_add ((GSourceFunc) focus_me, win->window);
95 test_window_destroy (TestWindow *win)
97 gtk_widget_destroy (win->window);
106 fprintf (stderr, "Testing roles...\n");
107 for (i = -1; i < 1000; i++)
108 g_assert (AccessibleRole_getName (i) != NULL);
110 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
111 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
112 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
113 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
120 Accessible *application;
122 fprintf (stderr, "Testing desktop...\n");
124 g_assert (getDesktop (-1) == NULL);
125 desktop = getDesktop (0);
126 g_assert (desktop != NULL);
128 validate_accessible (desktop, FALSE, FALSE);
130 application = Accessible_getChildAtIndex (desktop, 0);
131 g_assert (application != NULL);
132 AccessibleApplication_unref (application);
134 Accessible_unref (desktop);
138 test_application (Accessible *application)
142 fprintf (stderr, "Testing application ...\n");
143 g_assert (Accessible_isApplication (application));
144 g_assert (Accessible_getApplication (application) ==
146 AccessibleApplication_unref (application);
148 str = AccessibleApplication_getToolkitName (application);
149 g_assert (str != NULL);
150 g_assert (!strcmp (str, "GAIL"));
151 SPI_freeString (str);
153 str = AccessibleApplication_getVersion (application);
154 g_assert (str != NULL);
155 SPI_freeString (str);
157 AccessibleApplication_getID (application);
161 test_editable_text (AccessibleEditableText *etext)
164 AccessibleText *text;
166 fprintf (stderr, "Testing editable text ...\n");
168 g_assert (Accessible_isText (etext));
169 text = Accessible_getText (etext);
171 AccessibleEditableText_setTextContents (
172 etext, TEST_STRING_B);
174 str = AccessibleText_getText (text, 0, -1);
175 g_assert (!strcmp (str, TEST_STRING_B));
177 SPI_freeString (str);
179 /* FIXME: lots more editing here */
181 AccessibleEditableText_setTextContents (
182 etext, TEST_STRING_A);
184 AccessibleText_unref (text);
188 test_table (AccessibleTable *table)
194 fprintf (stderr, "Testing table ...\n");
196 rows = AccessibleTable_getNRows (table);
199 columns = AccessibleTable_getNColumns (table);
200 g_assert (columns > 0);
202 index = AccessibleTable_getIndexAt (table, rows-1, columns-1);
204 g_assert (AccessibleTable_getRowAtIndex (table, index) == rows-1);
206 g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns-1);
208 g_assert (AccessibleTable_getColumnHeader (table, 0)); /* maybe bogus assertion */
210 AccessibleTable_isSelected (table, 0, 0); /* no assertion, but see if warnings are thrown */
212 /* FIXME: lots more tests */
216 test_text (AccessibleText *text)
220 fprintf (stderr, "Testing text ...\n");
222 g_assert (AccessibleText_getCharacterCount (text) ==
223 strlen (TEST_STRING_A));
225 str = AccessibleText_getText (text, 0, -1);
226 g_assert (!strcmp (str, TEST_STRING_A));
227 SPI_freeString (str);
229 str = AccessibleText_getText (text, 0, 5);
230 g_assert (!strncmp (str, TEST_STRING_A, 5));
231 SPI_freeString (str);
233 AccessibleText_setCaretOffset (text, 7);
234 g_assert (AccessibleText_getCaretOffset (text) == 7);
236 /* FIXME: lots more tests - selections etc. etc. */
240 test_value (AccessibleValue *value)
243 float original_value;
245 /* Note: test_value assertions are known not to work as of Dec 09 */
247 fprintf (stderr, "Testing value ...\n");
249 original_value = AccessibleValue_getCurrentValue (value);
251 g_assert (AccessibleValue_getCurrentValue (value) <=
252 AccessibleValue_getMaximumValue (value));
254 g_assert (AccessibleValue_getCurrentValue (value) >=
255 AccessibleValue_getMinimumValue (value));
257 AccessibleValue_setCurrentValue (value,
258 AccessibleValue_getMinimumValue (value));
260 g_assert (AccessibleValue_getCurrentValue (value) ==
261 AccessibleValue_getMinimumValue (value));
263 AccessibleValue_setCurrentValue (value,
264 AccessibleValue_getMaximumValue (value));
266 g_assert (AccessibleValue_getCurrentValue (value) ==
267 AccessibleValue_getMaximumValue (value));
269 AccessibleValue_setCurrentValue (value, original_value);
271 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
275 test_component (AccessibleComponent *component)
277 long x, y, width, height;
279 fprintf (stderr, "Testing component...\n");
281 AccessibleComponent_getExtents (
282 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
284 AccessibleComponent_getPosition (
285 component, &x, &y, SPI_COORD_TYPE_SCREEN);
287 AccessibleComponent_getSize (component, &width, &height);
289 if (width > 0 && height > 0) {
291 Accessible *accessible, *componentb;
294 g_assert (AccessibleComponent_contains (
295 component, x, y, SPI_COORD_TYPE_SCREEN));
297 g_assert (AccessibleComponent_contains (
298 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
300 g_assert (AccessibleComponent_contains (
301 component, x + width - 1, y + height - 1,
302 SPI_COORD_TYPE_SCREEN));
305 accessible = AccessibleComponent_getAccessibleAtPoint (
306 component, x, y, SPI_COORD_TYPE_SCREEN);
308 g_assert (Accessible_isComponent (accessible));
309 componentb = Accessible_getComponent (accessible);
310 g_assert (componentb == component);
312 AccessibleComponent_unref (componentb);
313 Accessible_unref (accessible);
317 AccessibleComponent_getLayer (component);
318 AccessibleComponent_getMDIZOrder (component);
319 /* AccessibleComponent_grabFocus (component); */
323 validate_tree (Accessible *accessible,
325 gboolean recurse_down)
330 parent = Accessible_getParent (accessible);
333 Accessible *child_at_index;
335 g_assert (parent != NULL);
337 index = Accessible_getIndexInParent (accessible);
338 g_assert (index >= 0);
340 child_at_index = Accessible_getChildAtIndex (parent, index);
342 g_assert (child_at_index == accessible);
344 Accessible_unref (child_at_index);
345 Accessible_unref (parent);
348 len = Accessible_getChildCount (accessible);
350 for (i = 0; i < len; i++) {
353 child = Accessible_getChildAtIndex (accessible, i);
356 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
358 g_assert (Accessible_getIndexInParent (child) == i);
359 g_assert (Accessible_getParent (child) == accessible);
362 if (recurse_down && child)
363 validate_accessible (child, has_parent, recurse_down);
365 Accessible_unref (child);
371 validate_accessible (Accessible *accessible,
373 gboolean recurse_down)
379 name = Accessible_getName (accessible);
380 g_assert (name != NULL);
382 descr = Accessible_getDescription (accessible);
383 g_assert (descr != NULL);
385 role = Accessible_getRole (accessible);
386 g_assert (role != NULL);
391 for (i = 0; i < print_tree_depth; i++)
393 fputs ("|-> [ ", stderr);
396 if (Accessible_isAction (accessible)) {
397 tmp = Accessible_getAction (accessible);
398 g_assert (tmp != NULL);
400 fprintf (stderr, "At");
401 AccessibleAction_unref (tmp);
404 if (Accessible_isApplication (accessible)) {
405 tmp = Accessible_getApplication (accessible);
407 fprintf (stderr, "Ap");
409 test_application (tmp);
410 AccessibleApplication_unref (tmp);
413 if (Accessible_isComponent (accessible)) {
414 tmp = Accessible_getComponent (accessible);
415 g_assert (tmp != NULL);
417 fprintf (stderr, "Co");
419 test_component (tmp);
420 AccessibleComponent_unref (tmp);
423 if (Accessible_isEditableText (accessible)) {
424 tmp = Accessible_getEditableText (accessible);
425 g_assert (tmp != NULL);
427 fprintf (stderr, "Et");
429 test_editable_text (tmp);
430 AccessibleEditableText_unref (tmp);
433 if (Accessible_isHypertext (accessible)) {
434 tmp = Accessible_getHypertext (accessible);
435 g_assert (tmp != NULL);
437 fprintf (stderr, "Ht");
438 AccessibleHypertext_unref (tmp);
441 if (Accessible_isImage (accessible)) {
442 tmp = Accessible_getImage (accessible);
443 g_assert (tmp != NULL);
445 fprintf (stderr, "Im");
446 AccessibleImage_unref (accessible);
449 if (Accessible_isSelection (accessible)) {
450 tmp = Accessible_getSelection (accessible);
451 g_assert (tmp != NULL);
453 fprintf (stderr, "Se");
454 AccessibleSelection_unref (tmp);
457 if (Accessible_isTable (accessible)) {
458 tmp = Accessible_getTable (accessible);
459 g_assert (tmp != NULL);
461 fprintf (stderr, "Ta");
464 AccessibleTable_unref (tmp);
467 if (Accessible_isText (accessible)) {
468 tmp = Accessible_getText (accessible);
469 g_assert (tmp != NULL);
471 fprintf (stderr, "Te");
474 AccessibleText_unref (tmp);
477 if (Accessible_isValue (accessible)) {
478 tmp = Accessible_getValue (accessible);
479 g_assert (tmp != NULL);
481 fprintf (stderr, "Va");
484 AccessibleValue_unref (tmp);
488 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
490 SPI_freeString (name);
491 SPI_freeString (descr);
493 validate_tree (accessible, has_parent, recurse_down);
499 fprintf (stderr, "Testing misc bits ...\n");
501 g_assert (!Accessible_isComponent (NULL));
502 g_assert (Accessible_getComponent (NULL) == NULL);
503 SPI_freeString (NULL);
507 global_listener_cb (AccessibleEvent *event,
510 TestWindow *win = user_data;
512 AccessibleApplication *application;
514 g_assert (win->magic == WINDOW_MAGIC);
515 g_assert (!strcmp (event->type, "focus:"));
517 fprintf (stderr, "Fielded focus event ...\n");
519 desktop = getDesktop (0);
520 application = Accessible_getChildAtIndex (desktop, 0);
521 g_assert (application != NULL);
522 Accessible_unref (desktop);
524 test_application (application);
526 AccessibleApplication_unref (application);
529 validate_accessible (event->source, TRUE, TRUE);
531 validate_accessible (event->source, TRUE, TRUE);
537 main (int argc, char **argv)
541 const char *modules, *tmp;
542 AccessibleEventListener *global_listener;
544 modules = g_getenv ("GTK_MODULES");
545 if (!modules || modules [0] == '\0')
546 putenv ("GTK_MODULES=gail:at-bridge");
549 gtk_init (&argc, &argv);
551 g_assert (!SPI_init ());
552 g_assert (SPI_init ());
553 g_assert (getDesktopCount () == 1);
559 win = create_test_window ();
561 global_listener = createAccessibleEventListener (global_listener_cb, win);
562 g_assert (registerGlobalEventListener (global_listener, "focus:"));
564 fprintf (stderr, "Waiting for focus event ...\n");
567 g_assert (deregisterGlobalEventListenerAll (global_listener));
568 AccessibleEventListener_unref (global_listener);
570 test_window_destroy (win);
572 if ((leaked = SPI_exit ()))
573 g_error ("Leaked %d SPI handles", leaked);
575 g_assert (!SPI_exit ());
577 fprintf (stderr, "All tests passed\n");
579 if (g_getenv ("_MEMPROF_SOCKET")) {
580 fprintf (stderr, "Waiting for memprof\n");
584 putenv ("AT_BRIDGE_SHUTDOWN=1");