2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / test / test-simple.c
1 /*
2  * test-simple.c: A set of simple regression tests
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Ximian, Inc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
24  * ******** Do not copy this code as an example *********
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <gtk/gtk.h>
31 #include <cspi/spi.h>
32 #include <libbonobo.h>
33
34 static void validate_accessible (Accessible *accessible,
35                                  gboolean    has_parent,
36                                  gboolean    recurse_down);
37
38 #define WINDOW_MAGIC 0x123456a
39 #define TEST_STRING_A "A test string"
40 #define TEST_STRING_B "Another test string"
41
42 static int      print_tree_depth = 0;
43 static gboolean print_tree = FALSE;
44
45 typedef struct {
46         gulong     magic;
47         GtkWidget *window;
48 } TestWindow;
49
50 static gboolean
51 focus_me (GtkWidget *widget)
52 {
53         AtkObject *aobject = atk_implementor_ref_accessible (
54                 ATK_IMPLEMENTOR (widget));
55
56         /* Force a focus event - even if the WM focused
57          * us before our at-bridge's idle handler registered
58          * our interest */
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);
63         
64         g_object_unref (G_OBJECT (aobject));
65
66         return FALSE;
67 }
68
69 static TestWindow *
70 create_test_window (void)
71 {
72         TestWindow *win = g_new0 (TestWindow, 1);
73         GtkWidget  *widget, *vbox;
74
75         win->magic  = WINDOW_MAGIC;
76         win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
77  
78         gtk_widget_show (win->window);
79
80         vbox = gtk_vbox_new (0, 0);
81         gtk_container_add (GTK_CONTAINER (win->window), vbox);
82         gtk_widget_show (vbox);
83
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);
88
89         g_idle_add ((GSourceFunc) focus_me, win->window);
90
91         return win;
92 }
93
94 static void
95 test_window_destroy (TestWindow *win)
96 {
97         gtk_widget_destroy (win->window);
98         g_free (win);
99 }
100
101 static void
102 test_roles (void)
103 {
104         int i;
105
106         fprintf (stderr, "Testing roles...\n");
107         for (i = -1; i < 1000; i++)
108                 g_assert (AccessibleRole_getName (i) != NULL);
109
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"));
114 }
115
116 static void
117 test_desktop (void)
118 {
119         Accessible *desktop;
120         Accessible *application;
121
122         fprintf (stderr, "Testing desktop...\n");
123
124         g_assert (getDesktop (-1) == NULL);
125         desktop = getDesktop (0);
126         g_assert (desktop != NULL);
127
128         validate_accessible (desktop, FALSE, FALSE);
129
130         application = Accessible_getChildAtIndex (desktop, 0);
131         g_assert (application != NULL);
132         AccessibleApplication_unref (application);
133
134         Accessible_unref (desktop);
135 }
136
137 static void
138 test_application (Accessible *application)
139 {
140         char *str;
141
142         fprintf (stderr, "Testing application ...\n");
143         g_assert (Accessible_isApplication (application));
144         g_assert (Accessible_getApplication (application) ==
145                   application);
146         AccessibleApplication_unref (application);
147
148         str = AccessibleApplication_getToolkitName (application);
149         g_assert (str != NULL);
150         g_assert (!strcmp (str, "GAIL"));
151         SPI_freeString (str);
152
153         str = AccessibleApplication_getVersion (application);
154         g_assert (str != NULL);
155         SPI_freeString (str);
156
157         AccessibleApplication_getID (application);
158 }
159
160 static void
161 test_editable_text (AccessibleEditableText *etext)
162 {
163         char *str;
164         AccessibleText *text;
165
166         fprintf (stderr, "Testing editable text ...\n");
167         
168         g_assert (Accessible_isText (etext));
169         text = Accessible_getText (etext);
170
171         AccessibleEditableText_setTextContents (
172                 etext, TEST_STRING_B);
173
174         str = AccessibleText_getText (text, 0, -1);
175         g_assert (!strcmp (str, TEST_STRING_B));
176
177         SPI_freeString (str);
178
179         /* FIXME: lots more editing here */
180
181         AccessibleEditableText_setTextContents (
182                 etext, TEST_STRING_A);
183
184         AccessibleText_unref (text);
185 }
186
187 static void
188 test_text (AccessibleText *text)
189 {
190         char *str;
191
192         fprintf (stderr, "Testing text ...\n");
193
194         g_assert (AccessibleText_getCharacterCount (text) ==
195                   strlen (TEST_STRING_A));
196
197         str = AccessibleText_getText (text, 0, -1);
198         g_assert (!strcmp (str, TEST_STRING_A));
199         SPI_freeString (str);
200
201         str = AccessibleText_getText (text, 0, 5);
202         g_assert (!strncmp (str, TEST_STRING_A, 5));
203         SPI_freeString (str);
204
205         AccessibleText_setCaretOffset (text, 7);
206         g_assert (AccessibleText_getCaretOffset (text) == 7);
207
208         /* FIXME: lots more tests - selections etc. etc. */
209 }
210
211 static void
212 test_value (AccessibleValue *value)
213 {
214         char *str;
215         float original_value;
216         
217         /* Note: test_value assertions are known not to work as of Dec 09 */
218         
219         fprintf (stderr, "Testing value ...\n");
220
221         original_value = AccessibleValue_getCurrentValue (value);
222         
223         g_assert (AccessibleValue_getCurrentValue (value) <=
224                   AccessibleValue_getMaximumValue (value));
225
226         g_assert (AccessibleValue_getCurrentValue (value) >=
227                   AccessibleValue_getMinimumValue (value));
228
229         AccessibleValue_setCurrentValue (value, 
230                   AccessibleValue_getMinimumValue (value));
231         
232         g_assert (AccessibleValue_getCurrentValue (value) ==
233                   AccessibleValue_getMinimumValue (value));
234
235         AccessibleValue_setCurrentValue (value, 
236                   AccessibleValue_getMaximumValue (value));
237         
238         g_assert (AccessibleValue_getCurrentValue (value) ==
239                   AccessibleValue_getMaximumValue (value));
240
241         AccessibleValue_setCurrentValue (value, original_value);
242         
243         g_assert (AccessibleValue_getCurrentValue (value) == original_value);
244 }
245
246 static void
247 test_component (AccessibleComponent *component)
248 {
249         long x, y, width, height;
250
251         fprintf (stderr, "Testing component...\n");
252
253         AccessibleComponent_getExtents (
254                 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
255
256         AccessibleComponent_getPosition (
257                 component, &x, &y, SPI_COORD_TYPE_SCREEN);
258
259         AccessibleComponent_getSize (component, &width, &height);
260
261         if (width > 0 && height > 0) {
262 #ifdef FIXME
263                 Accessible *accessible, *componentb;
264 #endif
265
266                 g_assert (AccessibleComponent_contains (
267                         component, x, y, SPI_COORD_TYPE_SCREEN));
268
269                 g_assert (AccessibleComponent_contains (
270                         component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
271
272                 g_assert (AccessibleComponent_contains (
273                         component, x + width - 1, y + height - 1,
274                         SPI_COORD_TYPE_SCREEN));
275
276 #ifdef FIXME
277                 accessible = AccessibleComponent_getAccessibleAtPoint (
278                         component, x, y, SPI_COORD_TYPE_SCREEN);
279
280                 g_assert (Accessible_isComponent (accessible));
281                 componentb = Accessible_getComponent (accessible);
282                 g_assert (componentb == component);
283
284                 AccessibleComponent_unref (componentb);
285                 Accessible_unref (accessible);
286 #endif
287         }
288
289         AccessibleComponent_getLayer (component);
290         AccessibleComponent_getMDIZOrder (component);
291 /*      AccessibleComponent_grabFocus (component); */
292 }
293
294 static void
295 validate_tree (Accessible *accessible,
296                gboolean    has_parent,
297                gboolean    recurse_down)
298 {
299         Accessible  *parent;
300         long         len, i;
301
302         parent = Accessible_getParent (accessible);
303         if (has_parent) {
304                 long        index;
305                 Accessible *child_at_index;
306
307                 g_assert (parent != NULL);
308
309                 index = Accessible_getIndexInParent (accessible);
310                 g_assert (index >= 0); 
311
312                 child_at_index = Accessible_getChildAtIndex (parent, index);
313
314                 g_assert (child_at_index == accessible);
315
316                 Accessible_unref (child_at_index);
317                 Accessible_unref (parent);
318         }
319
320         len = Accessible_getChildCount (accessible);
321         print_tree_depth++;
322         for (i = 0; i < len; i++) {
323                 Accessible *child;
324
325                 child = Accessible_getChildAtIndex (accessible, i);
326 #ifdef ROPEY
327                 if (!child)
328                         fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
329
330                 g_assert (Accessible_getIndexInParent (child) == i);
331                 g_assert (Accessible_getParent (child) == accessible);
332 #endif
333
334                 if (recurse_down && child)
335                         validate_accessible (child, has_parent, recurse_down);
336
337                 Accessible_unref (child);
338         }
339         print_tree_depth--;
340 }
341
342 static void
343 validate_accessible (Accessible *accessible,
344                      gboolean    has_parent,
345                      gboolean    recurse_down)
346 {
347         Accessible *tmp;
348         char       *name, *descr;
349         const char *role;
350
351         name = Accessible_getName (accessible);
352         g_assert (name != NULL);
353         
354         descr = Accessible_getDescription (accessible);
355         g_assert (descr != NULL);
356
357         role = Accessible_getRole (accessible);
358         g_assert (role != NULL);
359
360         if (print_tree) {
361                 int i;
362
363                 for (i = 0; i < print_tree_depth; i++)
364                         fputc (' ', stderr);
365                 fputs ("|-> [ ", stderr);
366         }
367
368         if (Accessible_isAction (accessible)) {
369                 tmp = Accessible_getAction (accessible);
370                 g_assert (tmp != NULL);
371                 if (print_tree)
372                         fprintf (stderr, "At");
373                 AccessibleAction_unref (tmp);
374         }
375
376         if (Accessible_isApplication (accessible)) {
377                 tmp = Accessible_getApplication (accessible);
378                 if (print_tree)
379                         fprintf (stderr, "Ap");
380                 else
381                         test_application (tmp);
382                 AccessibleApplication_unref (tmp);
383         }
384
385         if (Accessible_isComponent (accessible)) {
386                 tmp = Accessible_getComponent (accessible);
387                 g_assert (tmp != NULL);
388                 if (print_tree)
389                         fprintf (stderr, "Co");
390                 else
391                         test_component (tmp);
392                 AccessibleComponent_unref (tmp);
393         }
394
395         if (Accessible_isEditableText (accessible)) {
396                 tmp = Accessible_getEditableText (accessible);
397                 g_assert (tmp != NULL);
398                 if (print_tree)
399                         fprintf (stderr, "Et");
400                 else
401                         test_editable_text (tmp);
402                 AccessibleEditableText_unref (tmp);
403         }
404
405         if (Accessible_isHypertext (accessible)) {
406                 tmp = Accessible_getHypertext (accessible);
407                 g_assert (tmp != NULL);
408                 if (print_tree)
409                         fprintf (stderr, "Ht");
410                 AccessibleHypertext_unref (tmp);
411         }
412
413         if (Accessible_isImage (accessible)) {
414                 tmp = Accessible_getImage (accessible);
415                 g_assert (tmp != NULL);
416                 if (print_tree)
417                         fprintf (stderr, "Im");
418                 AccessibleImage_unref (accessible);
419         }
420
421         if (Accessible_isSelection (accessible)) {
422                 tmp = Accessible_getSelection (accessible);
423                 g_assert (tmp != NULL);
424                 if (print_tree)
425                         fprintf (stderr, "Se");
426                 AccessibleSelection_unref (tmp);
427         }
428
429         if (Accessible_isTable (accessible)) {
430                 tmp = Accessible_getTable (accessible);
431                 g_assert (tmp != NULL);
432                 if (print_tree)
433                         fprintf (stderr, "Ta");
434                 AccessibleTable_unref (tmp);
435         }
436
437         if (Accessible_isText (accessible)) {
438                 tmp = Accessible_getText (accessible);
439                 g_assert (tmp != NULL);
440                 if (print_tree)
441                         fprintf (stderr, "Te");
442                 else
443                         test_text (tmp);
444                 AccessibleText_unref (tmp);
445         }
446
447         if (Accessible_isValue (accessible)) {
448                 tmp = Accessible_getValue (accessible);
449                 g_assert (tmp != NULL);
450                 if (print_tree)
451                         fprintf (stderr, "Va");
452                 else
453                         ; /* test_value (tmp); */
454                 AccessibleValue_unref (tmp);
455         }
456
457         if (print_tree)
458                 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
459
460         SPI_freeString (name);
461         SPI_freeString (descr);
462
463         validate_tree (accessible, has_parent, recurse_down);
464 }
465
466 static void
467 test_misc (void)
468 {
469         fprintf (stderr, "Testing misc bits ...\n");
470
471         g_assert (!Accessible_isComponent (NULL));
472         g_assert (Accessible_getComponent (NULL) == NULL);
473         SPI_freeString (NULL);
474 }
475
476 static void
477 global_listener_cb (AccessibleEvent     *event,
478                     void                *user_data)
479 {
480         TestWindow *win = user_data;
481         Accessible *desktop;
482         AccessibleApplication *application;
483
484         g_assert (win->magic == WINDOW_MAGIC);
485         g_assert (!strcmp (event->type, "focus:"));
486
487         fprintf (stderr, "Fielded focus event ...\n");
488
489         desktop = getDesktop (0);
490         application = Accessible_getChildAtIndex (desktop, 0);
491         g_assert (application != NULL);
492         Accessible_unref (desktop);
493
494         test_application (application);
495         
496         AccessibleApplication_unref (application);
497
498         print_tree = TRUE;
499         validate_accessible (event->source, TRUE, TRUE);
500         print_tree = FALSE;
501         validate_accessible (event->source, TRUE, TRUE);
502
503         gtk_main_quit ();
504 }
505
506 int
507 main (int argc, char **argv)
508 {
509         int leaked;
510         TestWindow *win;
511         const char *modules, *tmp;
512         AccessibleEventListener *global_listener;
513
514         modules = g_getenv ("GTK_MODULES");
515         if (!modules || modules [0] == '\0')
516                 putenv ("GTK_MODULES=gail:at-bridge");
517         modules = NULL;
518
519         gtk_init (&argc, &argv);
520
521         g_assert (!SPI_init ());
522         g_assert (SPI_init ());
523         g_assert (getDesktopCount () == 1);
524
525         test_roles ();
526         test_misc ();
527         test_desktop ();
528
529         win = create_test_window ();
530
531         global_listener = createAccessibleEventListener (global_listener_cb, win);
532         g_assert (registerGlobalEventListener (global_listener, "focus:"));
533
534         fprintf (stderr, "Waiting for focus event ...\n");
535         gtk_main ();
536
537         g_assert (deregisterGlobalEventListenerAll (global_listener));
538         AccessibleEventListener_unref (global_listener);
539
540         test_window_destroy (win);
541
542         if ((leaked = SPI_exit ()))
543                 g_error ("Leaked %d SPI handles", leaked);
544
545         g_assert (!SPI_exit ());
546
547         fprintf (stderr, "All tests passed\n");
548
549         if (g_getenv ("_MEMPROF_SOCKET")) {
550                 fprintf (stderr, "Waiting for memprof\n");
551                 gtk_main ();
552         }
553
554         putenv ("AT_BRIDGE_SHUTDOWN=1");
555
556         return 0;
557 }