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 *********
27 /* UGLY HACK for (unutterable_horror) */
28 #include <libspi/libspi.h>
34 #include <libbonobo.h>
36 static void validate_accessible (Accessible *accessible,
38 gboolean recurse_down);
40 #define WINDOW_MAGIC 0x123456a
41 #define TEST_STRING_A "A test string"
42 #define TEST_STRING_B "Another test string"
44 static int print_tree_depth = 0;
45 static gboolean print_tree = FALSE;
53 focus_me (GtkWidget *widget)
55 AtkObject *aobject = atk_implementor_ref_accessible (
56 ATK_IMPLEMENTOR (widget));
58 /* Force a focus event - even if the WM focused
59 * us before our at-bridge's idle handler registered
61 if (!GTK_WIDGET_HAS_FOCUS (widget))
62 gtk_widget_grab_focus (widget);
63 /* else: FIXME - gtk_widget_grab_focus should send a notify */
64 atk_focus_tracker_notify (aobject);
66 g_object_unref (G_OBJECT (aobject));
68 /* Pull focus away and then back - thus sucks */
73 create_test_window (void)
75 TestWindow *win = g_new0 (TestWindow, 1);
76 GtkWidget *widget, *vbox;
78 win->magic = WINDOW_MAGIC;
79 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
81 gtk_widget_show (win->window);
83 vbox = gtk_vbox_new (0, 0);
84 gtk_container_add (GTK_CONTAINER (win->window), vbox);
85 gtk_widget_show (vbox);
87 widget = gtk_entry_new ();
88 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
89 gtk_container_add (GTK_CONTAINER (vbox), widget);
90 gtk_widget_show (widget);
92 g_idle_add ((GSourceFunc) focus_me, win->window);
98 test_window_destroy (TestWindow *win)
100 gtk_widget_destroy (win->window);
109 fprintf (stderr, "Testing roles...\n");
110 for (i = -1; i < 1000; i++)
111 g_assert (AccessibleRole_getName (i) != NULL);
113 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
114 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
115 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
116 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
123 Accessible *application;
125 fprintf (stderr, "Testing desktop...\n");
127 g_assert (getDesktop (-1) == NULL);
128 desktop = getDesktop (0);
129 g_assert (desktop != NULL);
131 validate_accessible (desktop, FALSE, FALSE);
133 application = Accessible_getChildAtIndex (desktop, 0);
134 g_assert (application != NULL);
136 Accessible_unref (desktop);
140 test_application (Accessible *application)
144 fprintf (stderr, "Testing application ...\n");
145 g_assert (Accessible_isApplication (application));
146 g_assert (Accessible_getApplication (application) ==
148 AccessibleApplication_unref (application);
150 str = AccessibleApplication_getToolkitName (application);
151 g_assert (str != NULL);
152 g_assert (!strcmp (str, "GAIL"));
153 SPI_freeString (str);
155 str = AccessibleApplication_getVersion (application);
156 g_assert (str != NULL);
157 SPI_freeString (str);
159 AccessibleApplication_getID (application);
163 test_editable_text (AccessibleEditableText *etext)
166 AccessibleText *text;
168 fprintf (stderr, "Testing editable text ...\n");
170 g_assert (Accessible_isText (etext));
171 text = Accessible_getText (etext);
173 AccessibleEditableText_setTextContents (
174 etext, TEST_STRING_B);
176 str = AccessibleText_getText (text, 0, -1);
177 g_assert (!strcmp (str, TEST_STRING_B));
179 SPI_freeString (str);
181 /* FIXME: lots more editing here */
183 AccessibleEditableText_setTextContents (
184 etext, TEST_STRING_A);
186 AccessibleText_unref (text);
190 test_text (AccessibleText *text)
194 fprintf (stderr, "Testing text ...\n");
196 g_assert (AccessibleText_getCharacterCount (text) ==
197 strlen (TEST_STRING_A));
199 str = AccessibleText_getText (text, 0, -1);
200 g_assert (!strcmp (str, TEST_STRING_A));
201 SPI_freeString (str);
203 str = AccessibleText_getText (text, 0, 5);
204 g_assert (!strncmp (str, TEST_STRING_A, 5));
205 SPI_freeString (str);
207 AccessibleText_setCaretOffset (text, 7);
208 g_assert (AccessibleText_getCaretOffset (text) == 7);
210 /* FIXME: lots more tests - selections etc. etc. */
214 test_component (AccessibleComponent *component)
216 long x, y, width, height;
218 fprintf (stderr, "Testing component...\n");
220 AccessibleComponent_getExtents (
221 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
223 AccessibleComponent_getPosition (
224 component, &x, &y, SPI_COORD_TYPE_SCREEN);
226 AccessibleComponent_getSize (component, &width, &height);
228 if (width > 0 && height > 0) {
230 Accessible *accessible, *componentb;
233 g_assert (AccessibleComponent_contains (
234 component, x, y, SPI_COORD_TYPE_SCREEN));
236 g_assert (AccessibleComponent_contains (
237 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
239 g_assert (AccessibleComponent_contains (
240 component, x + width - 1, y + height - 1,
241 SPI_COORD_TYPE_SCREEN));
244 accessible = AccessibleComponent_getAccessibleAtPoint (
245 component, x, y, SPI_COORD_TYPE_SCREEN);
247 g_assert (Accessible_isComponent (accessible));
248 componentb = Accessible_getComponent (accessible);
249 g_assert (componentb == component);
251 AccessibleComponent_unref (componentb);
252 Accessible_unref (accessible);
256 AccessibleComponent_getLayer (component);
257 AccessibleComponent_getMDIZOrder (component);
258 /* AccessibleComponent_grabFocus (component); */
262 validate_tree (Accessible *accessible,
264 gboolean recurse_down)
269 parent = Accessible_getParent (accessible);
272 Accessible *child_at_index;
274 g_assert (parent != NULL);
276 index = Accessible_getIndexInParent (accessible);
277 g_assert (index >= 0);
279 child_at_index = Accessible_getChildAtIndex (parent, index);
281 g_assert (child_at_index == accessible);
283 Accessible_unref (child_at_index);
284 Accessible_unref (parent);
287 len = Accessible_getChildCount (accessible);
289 for (i = 0; i < len; i++) {
292 child = Accessible_getChildAtIndex (accessible, i);
295 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
297 g_assert (Accessible_getIndexInParent (child) == i);
298 g_assert (Accessible_getParent (child) == accessible);
301 if (recurse_down && child)
302 validate_accessible (child, has_parent, recurse_down);
304 Accessible_unref (child);
310 validate_accessible (Accessible *accessible,
312 gboolean recurse_down)
318 name = Accessible_getName (accessible);
319 g_assert (name != NULL);
321 descr = Accessible_getDescription (accessible);
322 g_assert (descr != NULL);
324 role = Accessible_getRole (accessible);
325 g_assert (role != NULL);
330 for (i = 0; i < print_tree_depth; i++)
332 fputs ("|-> [ ", stderr);
335 if (Accessible_isAction (accessible)) {
336 tmp = Accessible_getAction (accessible);
337 g_assert (tmp != NULL);
339 fprintf (stderr, "At");
340 AccessibleAction_unref (tmp);
343 if (Accessible_isApplication (accessible)) {
344 tmp = Accessible_getApplication (accessible);
346 fprintf (stderr, "Ap");
348 test_application (tmp);
349 AccessibleApplication_unref (tmp);
352 if (Accessible_isComponent (accessible)) {
353 tmp = Accessible_getComponent (accessible);
354 g_assert (tmp != NULL);
356 fprintf (stderr, "Co");
358 test_component (tmp);
359 AccessibleComponent_unref (tmp);
362 if (Accessible_isEditableText (accessible)) {
363 tmp = Accessible_getEditableText (accessible);
364 g_assert (tmp != NULL);
366 fprintf (stderr, "Et");
368 test_editable_text (tmp);
369 AccessibleEditableText_unref (tmp);
372 if (Accessible_isHypertext (accessible)) {
373 tmp = Accessible_getHypertext (accessible);
374 g_assert (tmp != NULL);
376 fprintf (stderr, "Ht");
377 AccessibleHypertext_unref (tmp);
380 if (Accessible_isImage (accessible)) {
381 tmp = Accessible_getImage (accessible);
382 g_assert (tmp != NULL);
384 fprintf (stderr, "Im");
385 AccessibleImage_unref (accessible);
388 if (Accessible_isSelection (accessible)) {
389 tmp = Accessible_getSelection (accessible);
390 g_assert (tmp != NULL);
392 fprintf (stderr, "Se");
393 AccessibleSelection_unref (tmp);
396 if (Accessible_isTable (accessible)) {
397 tmp = Accessible_getTable (accessible);
398 g_assert (tmp != NULL);
400 fprintf (stderr, "Ta");
401 AccessibleTable_unref (tmp);
404 if (Accessible_isText (accessible)) {
405 tmp = Accessible_getText (accessible);
406 g_assert (tmp != NULL);
408 fprintf (stderr, "Te");
411 AccessibleText_unref (tmp);
415 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
417 SPI_freeString (name);
418 SPI_freeString (descr);
420 validate_tree (accessible, has_parent, recurse_down);
426 fprintf (stderr, "Testing misc bits ...\n");
428 g_assert (!Accessible_isComponent (NULL));
429 g_assert (Accessible_getComponent (NULL) == NULL);
430 SPI_freeString (NULL);
433 #ifdef UNUTTERABLY_HORRIFIC
435 unutterable_horror (void)
437 /* Brutal ugliness to de-register ourself and exit cleanly */
438 CORBA_Environment ev;
440 Accessible *app_access;
441 Accessibility_Accessible app;
442 Accessibility_Registry registry;
444 fprintf (stderr, "Unutterable horror ...\n");
446 /* First get the application ! - gack */
447 desktop = getDesktop (0);
448 app_access = Accessible_getChildAtIndex (desktop, 0);
451 app = *(Accessibility_Accessible *) app_access;
453 CORBA_exception_init (&ev);
455 registry = bonobo_activation_activate_from_id (
456 "OAFIID:Accessibility_Registry:proto0.1", 0, NULL, &ev);
458 Accessibility_Registry_deregisterApplication (
461 bonobo_object_release_unref (registry, &ev); /* the original ref */
462 bonobo_object_release_unref (registry, &ev); /* taken by the bridge */
463 bonobo_object_release_unref (registry, &ev); /* taken by spi_main.c */
465 Accessible_unref (desktop);
466 Accessible_unref (app_access);
468 /* Urgh ! - get a pointer to app */
469 bonobo_object_unref (bonobo_object (ORBit_small_get_servant (app)));
474 utterable_normal_derefs (void)
476 /* Normal cleanup to exit cleanly */
477 CORBA_Environment ev;
478 Accessibility_Registry registry;
480 CORBA_exception_init (&ev);
482 registry = bonobo_activation_activate_from_id (
483 "OAFIID:Accessibility_Registry:proto0.1", 0, NULL, &ev);
485 bonobo_object_release_unref (registry, &ev); /* the ref above */
486 bonobo_object_release_unref (registry, &ev); /* taken by spi_main.c */
488 /* Yes, it would be nicer to have both SPI_main_quit and SPI_shutdown,
489 then the above code could be dispensed with */
493 global_listener_cb (AccessibleEvent *event,
496 TestWindow *win = user_data;
498 AccessibleApplication *application;
500 g_assert (win->magic == WINDOW_MAGIC);
501 g_assert (!strcmp (event->type, "focus:"));
503 fprintf (stderr, "Fielded focus event ...\n");
505 /* The application is now registered - this happens idly */
507 desktop = getDesktop (0);
508 application = Accessible_getChildAtIndex (desktop, 0);
509 g_assert (application != NULL);
510 Accessible_unref (desktop);
512 test_application (application);
514 AccessibleApplication_unref (application);
517 validate_accessible (event->source, TRUE, TRUE);
519 validate_accessible (event->source, TRUE, TRUE);
525 main (int argc, char **argv)
528 AccessibleEventListener *global_listener;
530 gtk_init (&argc, &argv);
532 if (!g_getenv ("GTK_MODULES") ||
533 !strstr (g_getenv ("GTK_MODULES"), "gail:at-bridge")) {
534 g_error ("You need to export GTK_MODULES='gail:at-bridge'");
537 g_assert (!SPI_init (TRUE));
538 g_assert (SPI_init (TRUE));
539 g_assert (getDesktopCount () == 1);
545 win = create_test_window ();
547 global_listener = createAccessibleEventListener (global_listener_cb, win);
548 g_assert (registerGlobalEventListener (global_listener, "focus:"));
550 fprintf (stderr, "Waiting for focus event ...\n");
553 /* First de-register myself - we only want the toplevel of ourself */
554 g_assert (deregisterGlobalEventListenerAll (global_listener));
556 test_window_destroy (win);
558 /* FIXME: we need to resolve the exit / quit mainloop function */
561 /* unutterable_horror (); ! */
562 utterable_normal_derefs ();
564 fprintf (stderr, "All tests passed\n");
566 return bonobo_debug_shutdown ();