Modified Value.idl interface to use unions, removes the assumption that
[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 void
70 test_window_add_and_show (GtkContainer *container, GtkWidget *widget)
71 {
72         gtk_container_add (container, widget);
73         gtk_widget_show (widget);
74 }
75
76 static TestWindow *
77 create_test_window (void)
78 {
79         TestWindow *win = g_new0 (TestWindow, 1);
80         GtkWidget  *widget, *vbox;
81         GtkListStore *store;
82         GtkTreeViewColumn *column;
83         GtkTreeIter iter;
84
85         win->magic  = WINDOW_MAGIC;
86         win->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
87  
88         gtk_widget_show (win->window);
89
90         vbox = gtk_vbox_new (0, 0);
91         gtk_container_add (GTK_CONTAINER (win->window), vbox);
92         gtk_widget_show (vbox);
93
94         widget = gtk_entry_new ();
95         gtk_entry_set_text (GTK_ENTRY (widget), TEST_STRING_A);
96         test_window_add_and_show (GTK_CONTAINER (vbox), widget);
97
98         widget = g_object_new (GTK_TYPE_RANGE, NULL);
99         gtk_range_set_range (GTK_RANGE (widget), 0.0, 100.0);
100         test_window_add_and_show (GTK_CONTAINER (vbox), widget);
101
102         store = gtk_list_store_new (1, G_TYPE_STRING);
103         gtk_list_store_append (store, &iter);
104         gtk_list_store_set (store, &iter, 0, TEST_STRING_A, -1); 
105         column = gtk_tree_view_column_new_with_attributes ("String",
106                 gtk_cell_renderer_text_new (), "text", 0, NULL);
107         widget = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); 
108         g_object_unref (G_OBJECT (store));
109         gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
110         test_window_add_and_show (GTK_CONTAINER (vbox), widget);
111
112         g_idle_add ((GSourceFunc) focus_me, win->window);
113
114         return win;
115 }
116
117 static void
118 test_window_destroy (TestWindow *win)
119 {
120         gtk_widget_destroy (win->window);
121         g_free (win);
122 }
123
124 static void
125 test_roles (void)
126 {
127         int i;
128
129         fprintf (stderr, "Testing roles...\n");
130         for (i = -1; i < 1000; i++)
131                 g_assert (AccessibleRole_getName (i) != NULL);
132
133         g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_FILE_CHOOSER), "file chooser"));
134         g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_RADIO_BUTTON), "radiobutton"));
135         g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_TABLE), "table"));
136         g_assert (!strcmp (AccessibleRole_getName (SPI_ROLE_WINDOW), "window"));
137 }
138
139 static void
140 test_desktop (void)
141 {
142         Accessible *desktop;
143         Accessible *application;
144
145         fprintf (stderr, "Testing desktop...\n");
146
147         g_assert (getDesktop (-1) == NULL);
148         desktop = getDesktop (0);
149         g_assert (desktop != NULL);
150
151         validate_accessible (desktop, FALSE, FALSE);
152
153         application = Accessible_getChildAtIndex (desktop, 0);
154         g_assert (application != NULL);
155         AccessibleApplication_unref (application);
156
157         Accessible_unref (desktop);
158 }
159
160 static void
161 test_application (Accessible *application)
162 {
163         char *str;
164
165         fprintf (stderr, "Testing application ...\n");
166         g_assert (Accessible_isApplication (application));
167         g_assert (Accessible_getApplication (application) ==
168                   application);
169         AccessibleApplication_unref (application);
170
171         str = AccessibleApplication_getToolkitName (application);
172         g_assert (str != NULL);
173         g_assert (!strcmp (str, "GAIL"));
174         SPI_freeString (str);
175
176         str = AccessibleApplication_getVersion (application);
177         g_assert (str != NULL);
178         SPI_freeString (str);
179
180         AccessibleApplication_getID (application);
181 }
182
183 static void
184 test_editable_text (AccessibleEditableText *etext)
185 {
186         char *str;
187         AccessibleText *text;
188
189         fprintf (stderr, "Testing editable text ...\n");
190         
191         g_assert (Accessible_isText (etext));
192         text = Accessible_getText (etext);
193
194         AccessibleEditableText_setTextContents (
195                 etext, TEST_STRING_B);
196
197         str = AccessibleText_getText (text, 0, -1);
198         g_assert (!strcmp (str, TEST_STRING_B));
199
200         SPI_freeString (str);
201
202         /* FIXME: lots more editing here */
203
204         AccessibleEditableText_setTextContents (
205                 etext, TEST_STRING_A);
206
207         AccessibleText_unref (text);
208 }
209
210 static void
211 test_table (AccessibleTable *table)
212 {
213         char *str;
214         gint index;
215         gint rows, columns;
216
217         fprintf (stderr, "Testing table ...\n");
218
219         rows = AccessibleTable_getNRows (table);
220         g_assert (rows > 0);
221
222         columns = AccessibleTable_getNColumns (table);
223         g_assert (columns > 0); /* weird that this fails, surely a bug ? */
224
225         index = AccessibleTable_getIndexAt (table, rows-1, columns-1);
226
227         g_assert (AccessibleTable_getRowAtIndex (table, index) == rows-1);
228
229         g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns-1);
230
231         g_assert (AccessibleTable_getColumnHeader (table, 0));
232                   /* maybe bogus assertion */
233
234         AccessibleTable_isSelected (table, 0, 0);
235                   /* no assertion, but see if warnings are thrown */
236         
237         /* FIXME: lots more tests */
238 }
239
240 static void
241 test_text (AccessibleText *text)
242 {
243         char *str;
244
245         fprintf (stderr, "Testing text ...\n");
246
247         g_assert (AccessibleText_getCharacterCount (text) ==
248                   strlen (TEST_STRING_A));
249
250         str = AccessibleText_getText (text, 0, -1);
251         g_assert (!strcmp (str, TEST_STRING_A));
252         SPI_freeString (str);
253
254         str = AccessibleText_getText (text, 0, 5);
255         g_assert (!strncmp (str, TEST_STRING_A, 5));
256         SPI_freeString (str);
257
258         AccessibleText_setCaretOffset (text, 7);
259         g_assert (AccessibleText_getCaretOffset (text) == 7);
260
261         /* FIXME: lots more tests - selections etc. etc. */
262 }
263
264 static void
265 test_value (AccessibleValue *value)
266 {
267         char *str;
268         float original_value;
269         
270         /* Note: test_value assertions are known not to work as of Dec 09 */
271         
272         fprintf (stderr, "Testing value ...\n");
273
274         original_value = AccessibleValue_getCurrentValue (value);
275         
276         g_assert (AccessibleValue_getCurrentValue (value) <=
277                   AccessibleValue_getMaximumValue (value));
278
279         g_assert (AccessibleValue_getCurrentValue (value) >=
280                   AccessibleValue_getMinimumValue (value));
281
282         AccessibleValue_setCurrentValue (value, 
283                   AccessibleValue_getMinimumValue (value));
284         
285         g_assert (AccessibleValue_getCurrentValue (value) ==
286                   AccessibleValue_getMinimumValue (value));
287
288         AccessibleValue_setCurrentValue (value, 
289                   AccessibleValue_getMaximumValue (value));
290         
291         g_assert (AccessibleValue_getCurrentValue (value) ==
292                   AccessibleValue_getMaximumValue (value));
293
294         AccessibleValue_setCurrentValue (value, original_value);
295         
296         g_assert (AccessibleValue_getCurrentValue (value) == original_value);
297 }
298
299 static void
300 test_component (AccessibleComponent *component)
301 {
302         long x, y, width, height;
303
304         fprintf (stderr, "Testing component...\n");
305
306         AccessibleComponent_getExtents (
307                 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
308
309         AccessibleComponent_getPosition (
310                 component, &x, &y, SPI_COORD_TYPE_SCREEN);
311
312         AccessibleComponent_getSize (component, &width, &height);
313
314         if (width > 0 && height > 0) {
315 #ifdef FIXME
316                 Accessible *accessible, *componentb;
317 #endif
318
319                 g_assert (AccessibleComponent_contains (
320                         component, x, y, SPI_COORD_TYPE_SCREEN));
321
322                 g_assert (AccessibleComponent_contains (
323                         component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
324
325                 g_assert (AccessibleComponent_contains (
326                         component, x + width - 1, y + height - 1,
327                         SPI_COORD_TYPE_SCREEN));
328
329 #ifdef FIXME
330                 accessible = AccessibleComponent_getAccessibleAtPoint (
331                         component, x, y, SPI_COORD_TYPE_SCREEN);
332
333                 g_assert (Accessible_isComponent (accessible));
334                 componentb = Accessible_getComponent (accessible);
335                 g_assert (componentb == component);
336
337                 AccessibleComponent_unref (componentb);
338                 Accessible_unref (accessible);
339 #endif
340         }
341
342         AccessibleComponent_getLayer (component);
343         AccessibleComponent_getMDIZOrder (component);
344 /*      AccessibleComponent_grabFocus (component); */
345 }
346
347 static void
348 validate_tree (Accessible *accessible,
349                gboolean    has_parent,
350                gboolean    recurse_down)
351 {
352         Accessible  *parent;
353         long         len, i;
354
355         parent = Accessible_getParent (accessible);
356         if (has_parent) {
357                 long        index;
358                 Accessible *child_at_index;
359
360                 g_assert (parent != NULL);
361
362                 index = Accessible_getIndexInParent (accessible);
363                 g_assert (index >= 0); 
364
365                 child_at_index = Accessible_getChildAtIndex (parent, index);
366
367                 g_assert (child_at_index == accessible);
368
369                 Accessible_unref (child_at_index);
370                 Accessible_unref (parent);
371         }
372
373         len = Accessible_getChildCount (accessible);
374         print_tree_depth++;
375         for (i = 0; i < len; i++) {
376                 Accessible *child;
377
378                 child = Accessible_getChildAtIndex (accessible, i);
379 #ifdef ROPEY
380                 if (!child)
381                         fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
382
383                 g_assert (Accessible_getIndexInParent (child) == i);
384                 g_assert (Accessible_getParent (child) == accessible);
385 #endif
386
387                 if (recurse_down && child)
388                         validate_accessible (child, has_parent, recurse_down);
389
390                 Accessible_unref (child);
391         }
392         print_tree_depth--;
393 }
394
395 static void
396 validate_accessible (Accessible *accessible,
397                      gboolean    has_parent,
398                      gboolean    recurse_down)
399 {
400         Accessible *tmp;
401         char       *name, *descr;
402         const char *role;
403
404         name = Accessible_getName (accessible);
405         g_assert (name != NULL);
406         
407         descr = Accessible_getDescription (accessible);
408         g_assert (descr != NULL);
409
410         role = Accessible_getRole (accessible);
411         g_assert (role != NULL);
412
413         if (print_tree) {
414                 int i;
415
416                 for (i = 0; i < print_tree_depth; i++)
417                         fputc (' ', stderr);
418                 fputs ("|-> [ ", stderr);
419         }
420
421         if (Accessible_isAction (accessible)) {
422                 tmp = Accessible_getAction (accessible);
423                 g_assert (tmp != NULL);
424                 if (print_tree)
425                         fprintf (stderr, "At");
426                 AccessibleAction_unref (tmp);
427         }
428
429         if (Accessible_isApplication (accessible)) {
430                 tmp = Accessible_getApplication (accessible);
431                 if (print_tree)
432                         fprintf (stderr, "Ap");
433                 else
434                         test_application (tmp);
435                 AccessibleApplication_unref (tmp);
436         }
437
438         if (Accessible_isComponent (accessible)) {
439                 tmp = Accessible_getComponent (accessible);
440                 g_assert (tmp != NULL);
441                 if (print_tree)
442                         fprintf (stderr, "Co");
443                 else
444                         test_component (tmp);
445                 AccessibleComponent_unref (tmp);
446         }
447
448         if (Accessible_isEditableText (accessible)) {
449                 tmp = Accessible_getEditableText (accessible);
450                 g_assert (tmp != NULL);
451                 if (print_tree)
452                         fprintf (stderr, "Et");
453                 else
454                         test_editable_text (tmp);
455                 AccessibleEditableText_unref (tmp);
456         }
457
458         if (Accessible_isHypertext (accessible)) {
459                 tmp = Accessible_getHypertext (accessible);
460                 g_assert (tmp != NULL);
461                 if (print_tree)
462                         fprintf (stderr, "Ht");
463                 AccessibleHypertext_unref (tmp);
464         }
465
466         if (Accessible_isImage (accessible)) {
467                 tmp = Accessible_getImage (accessible);
468                 g_assert (tmp != NULL);
469                 if (print_tree)
470                         fprintf (stderr, "Im");
471                 AccessibleImage_unref (accessible);
472         }
473
474         if (Accessible_isSelection (accessible)) {
475                 tmp = Accessible_getSelection (accessible);
476                 g_assert (tmp != NULL);
477                 if (print_tree)
478                         fprintf (stderr, "Se");
479                 AccessibleSelection_unref (tmp);
480         }
481
482         if (Accessible_isTable (accessible)) {
483                 tmp = Accessible_getTable (accessible);
484                 g_assert (tmp != NULL);
485                 if (print_tree)
486                         fprintf (stderr, "Ta");
487                 else
488                         test_table (tmp);
489                 AccessibleTable_unref (tmp);
490         }
491
492         if (Accessible_isText (accessible)) {
493                 tmp = Accessible_getText (accessible);
494                 g_assert (tmp != NULL);
495                 if (print_tree)
496                         fprintf (stderr, "Te");
497                 else
498                         test_text (tmp);
499                 AccessibleText_unref (tmp);
500         }
501
502         if (Accessible_isValue (accessible)) {
503                 tmp = Accessible_getValue (accessible);
504                 g_assert (tmp != NULL);
505                 if (print_tree)
506                         fprintf (stderr, "Va");
507                 else
508                         test_value (tmp); 
509                 AccessibleValue_unref (tmp);
510         }
511
512         if (print_tree)
513                 fprintf (stderr, " ] '%s' (%s) - %s:\n", name, descr, role);
514
515         SPI_freeString (name);
516         SPI_freeString (descr);
517
518         validate_tree (accessible, has_parent, recurse_down);
519 }
520
521 static void
522 test_misc (void)
523 {
524         fprintf (stderr, "Testing misc bits ...\n");
525
526         g_assert (!Accessible_isComponent (NULL));
527         g_assert (Accessible_getComponent (NULL) == NULL);
528         SPI_freeString (NULL);
529 }
530
531 static void
532 global_listener_cb (AccessibleEvent     *event,
533                     void                *user_data)
534 {
535         TestWindow *win = user_data;
536         Accessible *desktop;
537         AccessibleApplication *application;
538
539         g_assert (win->magic == WINDOW_MAGIC);
540         g_assert (!strcmp (event->type, "focus:"));
541
542         fprintf (stderr, "Fielded focus event ...\n");
543
544         desktop = getDesktop (0);
545         application = Accessible_getChildAtIndex (desktop, 0);
546         g_assert (application != NULL);
547         Accessible_unref (desktop);
548
549         test_application (application);
550         
551         AccessibleApplication_unref (application);
552
553         print_tree = TRUE;
554         validate_accessible (event->source, TRUE, TRUE);
555         print_tree = FALSE;
556         validate_accessible (event->source, TRUE, TRUE);
557
558         gtk_main_quit ();
559 }
560
561 int
562 main (int argc, char **argv)
563 {
564         int leaked;
565         TestWindow *win;
566         const char *modules, *tmp;
567         AccessibleEventListener *global_listener;
568
569         modules = g_getenv ("GTK_MODULES");
570         if (!modules || modules [0] == '\0')
571                 putenv ("GTK_MODULES=gail:at-bridge");
572         modules = NULL;
573
574         gtk_init (&argc, &argv);
575
576         g_assert (!SPI_init ());
577         g_assert (SPI_init ());
578         g_assert (getDesktopCount () == 1);
579
580         test_roles ();
581         test_misc ();
582         test_desktop ();
583
584         win = create_test_window ();
585
586         global_listener = createAccessibleEventListener (global_listener_cb, win);
587         g_assert (registerGlobalEventListener (global_listener, "focus:"));
588
589         fprintf (stderr, "Waiting for focus event ...\n");
590         gtk_main ();
591
592         g_assert (deregisterGlobalEventListenerAll (global_listener));
593         AccessibleEventListener_unref (global_listener);
594
595         test_window_destroy (win);
596
597         if ((leaked = SPI_exit ()))
598                 g_error ("Leaked %d SPI handles", leaked);
599
600         g_assert (!SPI_exit ());
601
602         fprintf (stderr, "All tests passed\n");
603
604         if (g_getenv ("_MEMPROF_SOCKET")) {
605                 fprintf (stderr, "Waiting for memprof\n");
606                 gtk_main ();
607         }
608
609         putenv ("AT_BRIDGE_SHUTDOWN=1");
610
611         return 0;
612 }