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