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