3992aff02926174f9fcb7ebf5d258481026f76d1
[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_table (AccessibleTable *table)
189 {
190         char *str;
191         gint index;
192         gint rows, columns;
193
194         fprintf (stderr, "Testing table ...\n");
195
196         rows = AccessibleTable_getNRows (table);
197         g_assert (rows > 0);
198         
199         columns = AccessibleTable_getNColumns (table);
200         g_assert (columns > 0);
201         
202         index = AccessibleTable_getIndexAt (table, rows-1, columns-1);
203
204         g_assert (AccessibleTable_getRowAtIndex (table, index) == rows-1);
205
206         g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns-1);
207
208         g_assert (AccessibleTable_getColumnHeader (table, 0)); /* maybe bogus assertion */
209
210         AccessibleTable_isSelected (table, 0, 0); /* no assertion, but see if warnings are thrown */
211         
212         /* FIXME: lots more tests */
213 }
214
215 static void
216 test_text (AccessibleText *text)
217 {
218         char *str;
219
220         fprintf (stderr, "Testing text ...\n");
221
222         g_assert (AccessibleText_getCharacterCount (text) ==
223                   strlen (TEST_STRING_A));
224
225         str = AccessibleText_getText (text, 0, -1);
226         g_assert (!strcmp (str, TEST_STRING_A));
227         SPI_freeString (str);
228
229         str = AccessibleText_getText (text, 0, 5);
230         g_assert (!strncmp (str, TEST_STRING_A, 5));
231         SPI_freeString (str);
232
233         AccessibleText_setCaretOffset (text, 7);
234         g_assert (AccessibleText_getCaretOffset (text) == 7);
235
236         /* FIXME: lots more tests - selections etc. etc. */
237 }
238
239 static void
240 test_value (AccessibleValue *value)
241 {
242         char *str;
243         float original_value;
244         
245         /* Note: test_value assertions are known not to work as of Dec 09 */
246         
247         fprintf (stderr, "Testing value ...\n");
248
249         original_value = AccessibleValue_getCurrentValue (value);
250         
251         g_assert (AccessibleValue_getCurrentValue (value) <=
252                   AccessibleValue_getMaximumValue (value));
253
254         g_assert (AccessibleValue_getCurrentValue (value) >=
255                   AccessibleValue_getMinimumValue (value));
256
257         AccessibleValue_setCurrentValue (value, 
258                   AccessibleValue_getMinimumValue (value));
259         
260         g_assert (AccessibleValue_getCurrentValue (value) ==
261                   AccessibleValue_getMinimumValue (value));
262
263         AccessibleValue_setCurrentValue (value, 
264                   AccessibleValue_getMaximumValue (value));
265         
266         g_assert (AccessibleValue_getCurrentValue (value) ==
267                   AccessibleValue_getMaximumValue (value));
268
269         AccessibleValue_setCurrentValue (value, original_value);
270         
271         g_assert (AccessibleValue_getCurrentValue (value) == original_value);
272 }
273
274 static void
275 test_component (AccessibleComponent *component)
276 {
277         long x, y, width, height;
278
279         fprintf (stderr, "Testing component...\n");
280
281         AccessibleComponent_getExtents (
282                 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
283
284         AccessibleComponent_getPosition (
285                 component, &x, &y, SPI_COORD_TYPE_SCREEN);
286
287         AccessibleComponent_getSize (component, &width, &height);
288
289         if (width > 0 && height > 0) {
290 #ifdef FIXME
291                 Accessible *accessible, *componentb;
292 #endif
293
294                 g_assert (AccessibleComponent_contains (
295                         component, x, y, SPI_COORD_TYPE_SCREEN));
296
297                 g_assert (AccessibleComponent_contains (
298                         component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
299
300                 g_assert (AccessibleComponent_contains (
301                         component, x + width - 1, y + height - 1,
302                         SPI_COORD_TYPE_SCREEN));
303
304 #ifdef FIXME
305                 accessible = AccessibleComponent_getAccessibleAtPoint (
306                         component, x, y, SPI_COORD_TYPE_SCREEN);
307
308                 g_assert (Accessible_isComponent (accessible));
309                 componentb = Accessible_getComponent (accessible);
310                 g_assert (componentb == component);
311
312                 AccessibleComponent_unref (componentb);
313                 Accessible_unref (accessible);
314 #endif
315         }
316
317         AccessibleComponent_getLayer (component);
318         AccessibleComponent_getMDIZOrder (component);
319 /*      AccessibleComponent_grabFocus (component); */
320 }
321
322 static void
323 validate_tree (Accessible *accessible,
324                gboolean    has_parent,
325                gboolean    recurse_down)
326 {
327         Accessible  *parent;
328         long         len, i;
329
330         parent = Accessible_getParent (accessible);
331         if (has_parent) {
332                 long        index;
333                 Accessible *child_at_index;
334
335                 g_assert (parent != NULL);
336
337                 index = Accessible_getIndexInParent (accessible);
338                 g_assert (index >= 0); 
339
340                 child_at_index = Accessible_getChildAtIndex (parent, index);
341
342                 g_assert (child_at_index == accessible);
343
344                 Accessible_unref (child_at_index);
345                 Accessible_unref (parent);
346         }
347
348         len = Accessible_getChildCount (accessible);
349         print_tree_depth++;
350         for (i = 0; i < len; i++) {
351                 Accessible *child;
352
353                 child = Accessible_getChildAtIndex (accessible, i);
354 #ifdef ROPEY
355                 if (!child)
356                         fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
357
358                 g_assert (Accessible_getIndexInParent (child) == i);
359                 g_assert (Accessible_getParent (child) == accessible);
360 #endif
361
362                 if (recurse_down && child)
363                         validate_accessible (child, has_parent, recurse_down);
364
365                 Accessible_unref (child);
366         }
367         print_tree_depth--;
368 }
369
370 static void
371 validate_accessible (Accessible *accessible,
372                      gboolean    has_parent,
373                      gboolean    recurse_down)
374 {
375         Accessible *tmp;
376         char       *name, *descr;
377         const char *role;
378
379         name = Accessible_getName (accessible);
380         g_assert (name != NULL);
381         
382         descr = Accessible_getDescription (accessible);
383         g_assert (descr != NULL);
384
385         role = Accessible_getRole (accessible);
386         g_assert (role != NULL);
387
388         if (print_tree) {
389                 int i;
390
391                 for (i = 0; i < print_tree_depth; i++)
392                         fputc (' ', stderr);
393                 fputs ("|-> [ ", stderr);
394         }
395
396         if (Accessible_isAction (accessible)) {
397                 tmp = Accessible_getAction (accessible);
398                 g_assert (tmp != NULL);
399                 if (print_tree)
400                         fprintf (stderr, "At");
401                 AccessibleAction_unref (tmp);
402         }
403
404         if (Accessible_isApplication (accessible)) {
405                 tmp = Accessible_getApplication (accessible);
406                 if (print_tree)
407                         fprintf (stderr, "Ap");
408                 else
409                         test_application (tmp);
410                 AccessibleApplication_unref (tmp);
411         }
412
413         if (Accessible_isComponent (accessible)) {
414                 tmp = Accessible_getComponent (accessible);
415                 g_assert (tmp != NULL);
416                 if (print_tree)
417                         fprintf (stderr, "Co");
418                 else
419                         test_component (tmp);
420                 AccessibleComponent_unref (tmp);
421         }
422
423         if (Accessible_isEditableText (accessible)) {
424                 tmp = Accessible_getEditableText (accessible);
425                 g_assert (tmp != NULL);
426                 if (print_tree)
427                         fprintf (stderr, "Et");
428                 else
429                         test_editable_text (tmp);
430                 AccessibleEditableText_unref (tmp);
431         }
432
433         if (Accessible_isHypertext (accessible)) {
434                 tmp = Accessible_getHypertext (accessible);
435                 g_assert (tmp != NULL);
436                 if (print_tree)
437                         fprintf (stderr, "Ht");
438                 AccessibleHypertext_unref (tmp);
439         }
440
441         if (Accessible_isImage (accessible)) {
442                 tmp = Accessible_getImage (accessible);
443                 g_assert (tmp != NULL);
444                 if (print_tree)
445                         fprintf (stderr, "Im");
446                 AccessibleImage_unref (accessible);
447         }
448
449         if (Accessible_isSelection (accessible)) {
450                 tmp = Accessible_getSelection (accessible);
451                 g_assert (tmp != NULL);
452                 if (print_tree)
453                         fprintf (stderr, "Se");
454                 AccessibleSelection_unref (tmp);
455         }
456
457         if (Accessible_isTable (accessible)) {
458                 tmp = Accessible_getTable (accessible);
459                 g_assert (tmp != NULL);
460                 if (print_tree)
461                         fprintf (stderr, "Ta");
462                 else
463                         test_table (tmp);
464                 AccessibleTable_unref (tmp);
465         }
466
467         if (Accessible_isText (accessible)) {
468                 tmp = Accessible_getText (accessible);
469                 g_assert (tmp != NULL);
470                 if (print_tree)
471                         fprintf (stderr, "Te");
472                 else
473                         test_text (tmp);
474                 AccessibleText_unref (tmp);
475         }
476
477         if (Accessible_isValue (accessible)) {
478                 tmp = Accessible_getValue (accessible);
479                 g_assert (tmp != NULL);
480                 if (print_tree)
481                         fprintf (stderr, "Va");
482                 else
483                         test_value (tmp); 
484                 AccessibleValue_unref (tmp);
485         }
486
487         if (print_tree)
488                 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
489
490         SPI_freeString (name);
491         SPI_freeString (descr);
492
493         validate_tree (accessible, has_parent, recurse_down);
494 }
495
496 static void
497 test_misc (void)
498 {
499         fprintf (stderr, "Testing misc bits ...\n");
500
501         g_assert (!Accessible_isComponent (NULL));
502         g_assert (Accessible_getComponent (NULL) == NULL);
503         SPI_freeString (NULL);
504 }
505
506 static void
507 global_listener_cb (AccessibleEvent     *event,
508                     void                *user_data)
509 {
510         TestWindow *win = user_data;
511         Accessible *desktop;
512         AccessibleApplication *application;
513
514         g_assert (win->magic == WINDOW_MAGIC);
515         g_assert (!strcmp (event->type, "focus:"));
516
517         fprintf (stderr, "Fielded focus event ...\n");
518
519         desktop = getDesktop (0);
520         application = Accessible_getChildAtIndex (desktop, 0);
521         g_assert (application != NULL);
522         Accessible_unref (desktop);
523
524         test_application (application);
525         
526         AccessibleApplication_unref (application);
527
528         print_tree = TRUE;
529         validate_accessible (event->source, TRUE, TRUE);
530         print_tree = FALSE;
531         validate_accessible (event->source, TRUE, TRUE);
532
533         gtk_main_quit ();
534 }
535
536 int
537 main (int argc, char **argv)
538 {
539         int leaked;
540         TestWindow *win;
541         const char *modules, *tmp;
542         AccessibleEventListener *global_listener;
543
544         modules = g_getenv ("GTK_MODULES");
545         if (!modules || modules [0] == '\0')
546                 putenv ("GTK_MODULES=gail:at-bridge");
547         modules = NULL;
548
549         gtk_init (&argc, &argv);
550
551         g_assert (!SPI_init ());
552         g_assert (SPI_init ());
553         g_assert (getDesktopCount () == 1);
554
555         test_roles ();
556         test_misc ();
557         test_desktop ();
558
559         win = create_test_window ();
560
561         global_listener = createAccessibleEventListener (global_listener_cb, win);
562         g_assert (registerGlobalEventListener (global_listener, "focus:"));
563
564         fprintf (stderr, "Waiting for focus event ...\n");
565         gtk_main ();
566
567         g_assert (deregisterGlobalEventListenerAll (global_listener));
568         AccessibleEventListener_unref (global_listener);
569
570         test_window_destroy (win);
571
572         if ((leaked = SPI_exit ()))
573                 g_error ("Leaked %d SPI handles", leaked);
574
575         g_assert (!SPI_exit ());
576
577         fprintf (stderr, "All tests passed\n");
578
579         if (g_getenv ("_MEMPROF_SOCKET")) {
580                 fprintf (stderr, "Waiting for memprof\n");
581                 gtk_main ();
582         }
583
584         putenv ("AT_BRIDGE_SHUTDOWN=1");
585
586         return 0;
587 }