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));
66 /* Pull focus away and then back - thus sucks */
71 create_test_window (void)
73 TestWindow *win = g_new0 (TestWindow, 1);
74 GtkWidget *widget, *vbox;
76 win->magic = WINDOW_MAGIC;
77 win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
79 gtk_widget_show (win->window);
81 vbox = gtk_vbox_new (0, 0);
82 gtk_container_add (GTK_CONTAINER (win->window), vbox);
83 gtk_widget_show (vbox);
85 widget = gtk_entry_new ();
86 gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
87 gtk_container_add (GTK_CONTAINER (vbox), widget);
88 gtk_widget_show (widget);
90 g_idle_add ((GSourceFunc) focus_me, win->window);
96 test_window_destroy (TestWindow *win)
98 gtk_widget_destroy (win->window);
107 fprintf (stderr, "Testing roles...\n");
108 for (i = -1; i < 1000; i++)
109 g_assert (AccessibleRole_getName (i) != NULL);
111 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
112 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
113 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
114 g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
121 Accessible *application;
123 fprintf (stderr, "Testing desktop...\n");
125 g_assert (getDesktop (-1) == NULL);
126 desktop = getDesktop (0);
127 g_assert (desktop != NULL);
129 validate_accessible (desktop, FALSE, FALSE);
131 application = Accessible_getChildAtIndex (desktop, 0);
132 g_assert (application != NULL);
133 AccessibleApplication_unref (application);
135 Accessible_unref (desktop);
139 test_application (Accessible *application)
143 fprintf (stderr, "Testing application ...\n");
144 g_assert (Accessible_isApplication (application));
145 g_assert (Accessible_getApplication (application) ==
147 AccessibleApplication_unref (application);
149 str = AccessibleApplication_getToolkitName (application);
150 g_assert (str != NULL);
151 g_assert (!strcmp (str, "GAIL"));
152 SPI_freeString (str);
154 str = AccessibleApplication_getVersion (application);
155 g_assert (str != NULL);
156 SPI_freeString (str);
158 AccessibleApplication_getID (application);
162 test_editable_text (AccessibleEditableText *etext)
165 AccessibleText *text;
167 fprintf (stderr, "Testing editable text ...\n");
169 g_assert (Accessible_isText (etext));
170 text = Accessible_getText (etext);
172 AccessibleEditableText_setTextContents (
173 etext, TEST_STRING_B);
175 str = AccessibleText_getText (text, 0, -1);
176 g_assert (!strcmp (str, TEST_STRING_B));
178 SPI_freeString (str);
180 /* FIXME: lots more editing here */
182 AccessibleEditableText_setTextContents (
183 etext, TEST_STRING_A);
185 AccessibleText_unref (text);
189 test_text (AccessibleText *text)
193 fprintf (stderr, "Testing text ...\n");
195 g_assert (AccessibleText_getCharacterCount (text) ==
196 strlen (TEST_STRING_A));
198 str = AccessibleText_getText (text, 0, -1);
199 g_assert (!strcmp (str, TEST_STRING_A));
200 SPI_freeString (str);
202 str = AccessibleText_getText (text, 0, 5);
203 g_assert (!strncmp (str, TEST_STRING_A, 5));
204 SPI_freeString (str);
206 AccessibleText_setCaretOffset (text, 7);
207 g_assert (AccessibleText_getCaretOffset (text) == 7);
209 /* FIXME: lots more tests - selections etc. etc. */
213 test_component (AccessibleComponent *component)
215 long x, y, width, height;
217 fprintf (stderr, "Testing component...\n");
219 AccessibleComponent_getExtents (
220 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
222 AccessibleComponent_getPosition (
223 component, &x, &y, SPI_COORD_TYPE_SCREEN);
225 AccessibleComponent_getSize (component, &width, &height);
227 if (width > 0 && height > 0) {
229 Accessible *accessible, *componentb;
232 g_assert (AccessibleComponent_contains (
233 component, x, y, SPI_COORD_TYPE_SCREEN));
235 g_assert (AccessibleComponent_contains (
236 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
238 g_assert (AccessibleComponent_contains (
239 component, x + width - 1, y + height - 1,
240 SPI_COORD_TYPE_SCREEN));
243 accessible = AccessibleComponent_getAccessibleAtPoint (
244 component, x, y, SPI_COORD_TYPE_SCREEN);
246 g_assert (Accessible_isComponent (accessible));
247 componentb = Accessible_getComponent (accessible);
248 g_assert (componentb == component);
250 AccessibleComponent_unref (componentb);
251 Accessible_unref (accessible);
255 AccessibleComponent_getLayer (component);
256 AccessibleComponent_getMDIZOrder (component);
257 /* AccessibleComponent_grabFocus (component); */
261 validate_tree (Accessible *accessible,
263 gboolean recurse_down)
268 parent = Accessible_getParent (accessible);
271 Accessible *child_at_index;
273 g_assert (parent != NULL);
275 index = Accessible_getIndexInParent (accessible);
276 g_assert (index >= 0);
278 child_at_index = Accessible_getChildAtIndex (parent, index);
280 g_assert (child_at_index == accessible);
282 Accessible_unref (child_at_index);
283 Accessible_unref (parent);
286 len = Accessible_getChildCount (accessible);
288 for (i = 0; i < len; i++) {
291 child = Accessible_getChildAtIndex (accessible, i);
294 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
296 g_assert (Accessible_getIndexInParent (child) == i);
297 g_assert (Accessible_getParent (child) == accessible);
300 if (recurse_down && child)
301 validate_accessible (child, has_parent, recurse_down);
303 Accessible_unref (child);
309 validate_accessible (Accessible *accessible,
311 gboolean recurse_down)
317 name = Accessible_getName (accessible);
318 g_assert (name != NULL);
320 descr = Accessible_getDescription (accessible);
321 g_assert (descr != NULL);
323 role = Accessible_getRole (accessible);
324 g_assert (role != NULL);
329 for (i = 0; i < print_tree_depth; i++)
331 fputs ("|-> [ ", stderr);
334 if (Accessible_isAction (accessible)) {
335 tmp = Accessible_getAction (accessible);
336 g_assert (tmp != NULL);
338 fprintf (stderr, "At");
339 AccessibleAction_unref (tmp);
342 if (Accessible_isApplication (accessible)) {
343 tmp = Accessible_getApplication (accessible);
345 fprintf (stderr, "Ap");
347 test_application (tmp);
348 AccessibleApplication_unref (tmp);
351 if (Accessible_isComponent (accessible)) {
352 tmp = Accessible_getComponent (accessible);
353 g_assert (tmp != NULL);
355 fprintf (stderr, "Co");
357 test_component (tmp);
358 AccessibleComponent_unref (tmp);
361 if (Accessible_isEditableText (accessible)) {
362 tmp = Accessible_getEditableText (accessible);
363 g_assert (tmp != NULL);
365 fprintf (stderr, "Et");
367 test_editable_text (tmp);
368 AccessibleEditableText_unref (tmp);
371 if (Accessible_isHypertext (accessible)) {
372 tmp = Accessible_getHypertext (accessible);
373 g_assert (tmp != NULL);
375 fprintf (stderr, "Ht");
376 AccessibleHypertext_unref (tmp);
379 if (Accessible_isImage (accessible)) {
380 tmp = Accessible_getImage (accessible);
381 g_assert (tmp != NULL);
383 fprintf (stderr, "Im");
384 AccessibleImage_unref (accessible);
387 if (Accessible_isSelection (accessible)) {
388 tmp = Accessible_getSelection (accessible);
389 g_assert (tmp != NULL);
391 fprintf (stderr, "Se");
392 AccessibleSelection_unref (tmp);
395 if (Accessible_isTable (accessible)) {
396 tmp = Accessible_getTable (accessible);
397 g_assert (tmp != NULL);
399 fprintf (stderr, "Ta");
400 AccessibleTable_unref (tmp);
403 if (Accessible_isText (accessible)) {
404 tmp = Accessible_getText (accessible);
405 g_assert (tmp != NULL);
407 fprintf (stderr, "Te");
410 AccessibleText_unref (tmp);
414 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
416 SPI_freeString (name);
417 SPI_freeString (descr);
419 validate_tree (accessible, has_parent, recurse_down);
425 fprintf (stderr, "Testing misc bits ...\n");
427 g_assert (!Accessible_isComponent (NULL));
428 g_assert (Accessible_getComponent (NULL) == NULL);
429 SPI_freeString (NULL);
433 global_listener_cb (AccessibleEvent *event,
436 TestWindow *win = user_data;
438 AccessibleApplication *application;
440 g_assert (win->magic == WINDOW_MAGIC);
441 g_assert (!strcmp (event->type, "focus:"));
443 fprintf (stderr, "Fielded focus event ...\n");
445 desktop = getDesktop (0);
446 application = Accessible_getChildAtIndex (desktop, 0);
447 g_assert (application != NULL);
448 Accessible_unref (desktop);
450 test_application (application);
452 AccessibleApplication_unref (application);
455 validate_accessible (event->source, TRUE, TRUE);
457 validate_accessible (event->source, TRUE, TRUE);
463 main (int argc, char **argv)
467 AccessibleEventListener *global_listener;
469 setenv ("GTK_MODULES", "gail:at-bridge", FALSE);
471 gtk_init (&argc, &argv);
473 g_assert (!SPI_init ());
474 g_assert (SPI_init ());
475 g_assert (getDesktopCount () == 1);
481 win = create_test_window ();
483 global_listener = createAccessibleEventListener (global_listener_cb, win);
484 g_assert (registerGlobalEventListener (global_listener, "focus:"));
486 fprintf (stderr, "Waiting for focus event ...\n");
489 g_assert (deregisterGlobalEventListenerAll (global_listener));
490 AccessibleEventListener_unref (global_listener);
492 test_window_destroy (win);
494 if ((leaked = SPI_exit ()))
495 g_error ("Leaked %d SPI handles", leaked);
497 g_assert (!SPI_exit ());
499 fprintf (stderr, "All tests passed\n");
501 if (g_getenv ("_MEMPROF_SOCKET")) {
502 fprintf (stderr, "Waiting for memprof\n");
506 setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);