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_value (AccessibleValue *value)
216 float original_value;
218 /* Note: test_value assertions are known not to work as of Dec 09 */
220 fprintf (stderr, "Testing value ...\n");
222 original_value = AccessibleValue_getCurrentValue (value);
224 g_assert (AccessibleValue_getCurrentValue (value) <=
225 AccessibleValue_getMaximumValue (value));
227 g_assert (AccessibleValue_getCurrentValue (value) >=
228 AccessibleValue_getMinimumValue (value));
230 AccessibleValue_setCurrentValue (value,
231 AccessibleValue_getMinimumValue (value));
233 g_assert (AccessibleValue_getCurrentValue (value) ==
234 AccessibleValue_getMinimumValue (value));
236 AccessibleValue_setCurrentValue (value,
237 AccessibleValue_getMaximumValue (value));
239 g_assert (AccessibleValue_getCurrentValue (value) ==
240 AccessibleValue_getMaximumValue (value));
242 AccessibleValue_setCurrentValue (value, original_value);
244 g_assert (AccessibleValue_getCurrentValue (value) == original_value);
248 test_component (AccessibleComponent *component)
250 long x, y, width, height;
252 fprintf (stderr, "Testing component...\n");
254 AccessibleComponent_getExtents (
255 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
257 AccessibleComponent_getPosition (
258 component, &x, &y, SPI_COORD_TYPE_SCREEN);
260 AccessibleComponent_getSize (component, &width, &height);
262 if (width > 0 && height > 0) {
264 Accessible *accessible, *componentb;
267 g_assert (AccessibleComponent_contains (
268 component, x, y, SPI_COORD_TYPE_SCREEN));
270 g_assert (AccessibleComponent_contains (
271 component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
273 g_assert (AccessibleComponent_contains (
274 component, x + width - 1, y + height - 1,
275 SPI_COORD_TYPE_SCREEN));
278 accessible = AccessibleComponent_getAccessibleAtPoint (
279 component, x, y, SPI_COORD_TYPE_SCREEN);
281 g_assert (Accessible_isComponent (accessible));
282 componentb = Accessible_getComponent (accessible);
283 g_assert (componentb == component);
285 AccessibleComponent_unref (componentb);
286 Accessible_unref (accessible);
290 AccessibleComponent_getLayer (component);
291 AccessibleComponent_getMDIZOrder (component);
292 /* AccessibleComponent_grabFocus (component); */
296 validate_tree (Accessible *accessible,
298 gboolean recurse_down)
303 parent = Accessible_getParent (accessible);
306 Accessible *child_at_index;
308 g_assert (parent != NULL);
310 index = Accessible_getIndexInParent (accessible);
311 g_assert (index >= 0);
313 child_at_index = Accessible_getChildAtIndex (parent, index);
315 g_assert (child_at_index == accessible);
317 Accessible_unref (child_at_index);
318 Accessible_unref (parent);
321 len = Accessible_getChildCount (accessible);
323 for (i = 0; i < len; i++) {
326 child = Accessible_getChildAtIndex (accessible, i);
329 fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
331 g_assert (Accessible_getIndexInParent (child) == i);
332 g_assert (Accessible_getParent (child) == accessible);
335 if (recurse_down && child)
336 validate_accessible (child, has_parent, recurse_down);
338 Accessible_unref (child);
344 validate_accessible (Accessible *accessible,
346 gboolean recurse_down)
352 name = Accessible_getName (accessible);
353 g_assert (name != NULL);
355 descr = Accessible_getDescription (accessible);
356 g_assert (descr != NULL);
358 role = Accessible_getRole (accessible);
359 g_assert (role != NULL);
364 for (i = 0; i < print_tree_depth; i++)
366 fputs ("|-> [ ", stderr);
369 if (Accessible_isAction (accessible)) {
370 tmp = Accessible_getAction (accessible);
371 g_assert (tmp != NULL);
373 fprintf (stderr, "At");
374 AccessibleAction_unref (tmp);
377 if (Accessible_isApplication (accessible)) {
378 tmp = Accessible_getApplication (accessible);
380 fprintf (stderr, "Ap");
382 test_application (tmp);
383 AccessibleApplication_unref (tmp);
386 if (Accessible_isComponent (accessible)) {
387 tmp = Accessible_getComponent (accessible);
388 g_assert (tmp != NULL);
390 fprintf (stderr, "Co");
392 test_component (tmp);
393 AccessibleComponent_unref (tmp);
396 if (Accessible_isEditableText (accessible)) {
397 tmp = Accessible_getEditableText (accessible);
398 g_assert (tmp != NULL);
400 fprintf (stderr, "Et");
402 test_editable_text (tmp);
403 AccessibleEditableText_unref (tmp);
406 if (Accessible_isHypertext (accessible)) {
407 tmp = Accessible_getHypertext (accessible);
408 g_assert (tmp != NULL);
410 fprintf (stderr, "Ht");
411 AccessibleHypertext_unref (tmp);
414 if (Accessible_isImage (accessible)) {
415 tmp = Accessible_getImage (accessible);
416 g_assert (tmp != NULL);
418 fprintf (stderr, "Im");
419 AccessibleImage_unref (accessible);
422 if (Accessible_isSelection (accessible)) {
423 tmp = Accessible_getSelection (accessible);
424 g_assert (tmp != NULL);
426 fprintf (stderr, "Se");
427 AccessibleSelection_unref (tmp);
430 if (Accessible_isTable (accessible)) {
431 tmp = Accessible_getTable (accessible);
432 g_assert (tmp != NULL);
434 fprintf (stderr, "Ta");
435 AccessibleTable_unref (tmp);
438 if (Accessible_isText (accessible)) {
439 tmp = Accessible_getText (accessible);
440 g_assert (tmp != NULL);
442 fprintf (stderr, "Te");
445 AccessibleText_unref (tmp);
448 if (Accessible_isValue (accessible)) {
449 tmp = Accessible_getValue (accessible);
450 g_assert (tmp != NULL);
452 fprintf (stderr, "Va");
454 ; /* test_value (tmp); */
455 AccessibleValue_unref (tmp);
459 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
461 SPI_freeString (name);
462 SPI_freeString (descr);
464 validate_tree (accessible, has_parent, recurse_down);
470 fprintf (stderr, "Testing misc bits ...\n");
472 g_assert (!Accessible_isComponent (NULL));
473 g_assert (Accessible_getComponent (NULL) == NULL);
474 SPI_freeString (NULL);
478 global_listener_cb (AccessibleEvent *event,
481 TestWindow *win = user_data;
483 AccessibleApplication *application;
485 g_assert (win->magic == WINDOW_MAGIC);
486 g_assert (!strcmp (event->type, "focus:"));
488 fprintf (stderr, "Fielded focus event ...\n");
490 desktop = getDesktop (0);
491 application = Accessible_getChildAtIndex (desktop, 0);
492 g_assert (application != NULL);
493 Accessible_unref (desktop);
495 test_application (application);
497 AccessibleApplication_unref (application);
500 validate_accessible (event->source, TRUE, TRUE);
502 validate_accessible (event->source, TRUE, TRUE);
508 main (int argc, char **argv)
512 AccessibleEventListener *global_listener;
514 setenv ("GTK_MODULES", "gail:at-bridge", FALSE);
516 gtk_init (&argc, &argv);
518 g_assert (!SPI_init ());
519 g_assert (SPI_init ());
520 g_assert (getDesktopCount () == 1);
526 win = create_test_window ();
528 global_listener = createAccessibleEventListener (global_listener_cb, win);
529 g_assert (registerGlobalEventListener (global_listener, "focus:"));
531 fprintf (stderr, "Waiting for focus event ...\n");
534 g_assert (deregisterGlobalEventListenerAll (global_listener));
535 AccessibleEventListener_unref (global_listener);
537 test_window_destroy (win);
539 if ((leaked = SPI_exit ()))
540 g_error ("Leaked %d SPI handles", leaked);
542 g_assert (!SPI_exit ());
544 fprintf (stderr, "All tests passed\n");
546 if (g_getenv ("_MEMPROF_SOCKET")) {
547 fprintf (stderr, "Waiting for memprof\n");
551 setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);