Added support for key event strings, and renamespaced some of the at-bridge.
[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 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_action (AccessibleAction *action)
162 {
163         gint n_actions, i;
164         gchar *s, *sd;
165         g_assert ((n_actions = AccessibleAction_getNActions (action)) >= 0);
166
167         fprintf (stderr, "Testing actions...");
168         for (i = 0; i < n_actions; ++i)
169         {
170                 s = AccessibleAction_getName (action, i);
171                 g_assert (s);
172                 sd = AccessibleAction_getDescription (action, i);
173                 g_assert (sd);
174                 fprintf (stderr, "%d: %s (%s);  ", i, s, sd);
175                 SPI_freeString (s);
176                 SPI_freeString (sd);
177                 /* g_assert (AccessibleAction_doAction (action, i)); */
178         }
179         fprintf (stderr, "\n");
180 }
181
182 static void
183 test_desktop (void)
184 {
185         Accessible *desktop;
186         Accessible *application;
187
188         fprintf (stderr, "Testing desktop...\n");
189
190         g_assert (SPI_getDesktop (-1) == NULL);
191         desktop = SPI_getDesktop (0);
192         g_assert (desktop != NULL);
193
194         validate_accessible (desktop, FALSE, FALSE);
195
196         application = Accessible_getChildAtIndex (desktop, 0);
197         g_assert (application != NULL);
198         AccessibleApplication_unref (application);
199
200         Accessible_unref (desktop);
201 }
202
203 static void
204 test_application (Accessible *application)
205 {
206         char *str;
207
208         fprintf (stderr, "Testing application ...\n");
209         g_assert (Accessible_isApplication (application));
210         g_assert (Accessible_getApplication (application) ==
211                   application);
212         AccessibleApplication_unref (application);
213
214         str = AccessibleApplication_getToolkitName (application);
215         g_assert (str != NULL);
216         g_assert (!strcmp (str, "GAIL"));
217         SPI_freeString (str);
218
219         str = AccessibleApplication_getVersion (application);
220         g_assert (str != NULL);
221         SPI_freeString (str);
222
223         AccessibleApplication_getID (application);
224 }
225
226 static void
227 test_editable_text (AccessibleEditableText *etext)
228 {
229         char *str;
230         AccessibleText *text;
231
232         fprintf (stderr, "Testing editable text ...\n");
233         
234         g_assert (Accessible_isText (etext));
235         text = Accessible_getText (etext);
236
237         AccessibleEditableText_setTextContents (
238                 etext, TEST_STRING_B);
239
240         str = AccessibleText_getText (text, 0, -1);
241         g_assert (!strcmp (str, TEST_STRING_B));
242
243         SPI_freeString (str);
244
245         /* FIXME: lots more editing here */
246
247         AccessibleEditableText_setTextContents (
248                 etext, TEST_STRING_A);
249
250         AccessibleText_unref (text);
251 }
252
253 static void
254 test_table (AccessibleTable *table)
255 {
256         Accessible *header;
257         gint index;
258         gint rows, columns;
259
260         fprintf (stderr, "Testing table ...\n");
261
262         rows = AccessibleTable_getNRows (table);
263         g_assert (rows > 0);
264
265         columns = AccessibleTable_getNColumns (table);
266         g_assert (columns > 0);
267
268         index = AccessibleTable_getIndexAt (table, rows - 1, columns - 1);
269
270         g_assert (AccessibleTable_getRowAtIndex (table, index) == rows - 1);
271
272         g_assert (AccessibleTable_getColumnAtIndex (table, index) == columns - 1);
273
274         g_assert ((header = AccessibleTable_getColumnHeader (table, 0)));
275         Accessible_unref (header);
276
277         AccessibleTable_isSelected (table, 0, 0);
278         
279         /* FIXME: lots more tests */
280 }
281
282 static void
283 test_text (AccessibleText *text)
284 {
285         char *str;
286
287         fprintf (stderr, "Testing text ...\n");
288
289         g_assert (AccessibleText_getCharacterCount (text) ==
290                   strlen (TEST_STRING_A));
291
292         str = AccessibleText_getText (text, 0, -1);
293         g_assert (!strcmp (str, TEST_STRING_A));
294         SPI_freeString (str);
295
296         str = AccessibleText_getText (text, 0, 5);
297         g_assert (!strncmp (str, TEST_STRING_A, 5));
298         SPI_freeString (str);
299
300         AccessibleText_setCaretOffset (text, 7);
301         g_assert (AccessibleText_getCaretOffset (text) == 7);
302
303         /* FIXME: lots more tests - selections etc. etc. */
304 }
305
306 static void
307 test_value (AccessibleValue *value)
308 {
309         float original_value;
310         
311         fprintf (stderr, "Testing value ...\n");
312
313         original_value = AccessibleValue_getCurrentValue (value);
314         
315         g_assert (AccessibleValue_getCurrentValue (value) <=
316                   AccessibleValue_getMaximumValue (value));
317
318         g_assert (AccessibleValue_getCurrentValue (value) >=
319                   AccessibleValue_getMinimumValue (value));
320
321         AccessibleValue_setCurrentValue (value, 
322                   AccessibleValue_getMinimumValue (value));
323         
324         g_assert (AccessibleValue_getCurrentValue (value) ==
325                   AccessibleValue_getMinimumValue (value));
326
327         AccessibleValue_setCurrentValue (value, 
328                   AccessibleValue_getMaximumValue (value));
329         
330         g_assert (AccessibleValue_getCurrentValue (value) ==
331                   AccessibleValue_getMaximumValue (value));
332
333         AccessibleValue_setCurrentValue (value, original_value);
334         
335         g_assert (AccessibleValue_getCurrentValue (value) == original_value);
336 }
337
338 static void
339 test_component (AccessibleComponent *component)
340 {
341         long x, y, width, height;
342
343         fprintf (stderr, "Testing component...\n");
344
345         AccessibleComponent_getExtents (
346                 component, &x, &y, &width, &height, SPI_COORD_TYPE_SCREEN);
347
348         AccessibleComponent_getPosition (
349                 component, &x, &y, SPI_COORD_TYPE_SCREEN);
350
351         AccessibleComponent_getSize (component, &width, &height);
352
353         if (width > 0 && height > 0) {
354 #ifdef FIXME
355                 Accessible *accessible, *componentb;
356 #endif
357
358                 g_assert (AccessibleComponent_contains (
359                         component, x, y, SPI_COORD_TYPE_SCREEN));
360
361                 g_assert (AccessibleComponent_contains (
362                         component, x + width - 1, y, SPI_COORD_TYPE_SCREEN));
363
364                 g_assert (AccessibleComponent_contains (
365                         component, x + width - 1, y + height - 1,
366                         SPI_COORD_TYPE_SCREEN));
367
368 #ifdef FIXME
369                 accessible = AccessibleComponent_getAccessibleAtPoint (
370                         component, x, y, SPI_COORD_TYPE_SCREEN);
371
372                 g_assert (Accessible_isComponent (accessible));
373                 componentb = Accessible_getComponent (accessible);
374                 g_assert (componentb == component);
375
376                 AccessibleComponent_unref (componentb);
377                 Accessible_unref (accessible);
378 #endif
379         }
380
381         AccessibleComponent_getLayer (component);
382         AccessibleComponent_getMDIZOrder (component);
383 /*      AccessibleComponent_grabFocus (component); */
384 }
385
386 static void
387 test_image (AccessibleImage *image)
388 {
389         char *desc;
390         long int x = -1, y = -1, width = -1, height = -1;
391
392         desc = AccessibleImage_getImageDescription (image);
393         g_assert (desc != NULL);
394         SPI_freeString (desc);
395
396         AccessibleImage_getImagePosition (image, &x, &y,
397                                           SPI_COORD_TYPE_SCREEN);
398         AccessibleImage_getImageSize     (image, &width, &height);
399         AccessibleImage_getImageExtents  (image, &x, &y, &width, &height,
400                                           SPI_COORD_TYPE_WINDOW);
401 }
402
403 static void
404 validate_tree (Accessible *accessible,
405                gboolean    has_parent,
406                gboolean    recurse_down)
407 {
408         Accessible  *parent;
409         long         len, i;
410
411         parent = Accessible_getParent (accessible);
412         if (has_parent) {
413                 long        index;
414                 Accessible *child_at_index;
415
416                 g_assert (parent != NULL);
417
418                 index = Accessible_getIndexInParent (accessible);
419                 g_assert (index >= 0); 
420
421                 child_at_index = Accessible_getChildAtIndex (parent, index);
422
423                 g_assert (child_at_index == accessible);
424
425                 Accessible_unref (child_at_index);
426                 Accessible_unref (parent);
427         }
428
429         len = Accessible_getChildCount (accessible);
430         print_tree_depth++;
431         for (i = 0; i < len; i++) {
432                 Accessible *child;
433
434                 child = Accessible_getChildAtIndex (accessible, i);
435 #ifdef ROPEY
436                 if (!child)
437                         fprintf (stderr, "Unusual - ChildGone at %ld\n", i);
438
439                 g_assert (Accessible_getIndexInParent (child) == i);
440                 g_assert (Accessible_getParent (child) == accessible);
441 #endif
442
443                 if (recurse_down && child)
444                         validate_accessible (child, has_parent, recurse_down);
445
446                 Accessible_unref (child);
447         }
448         print_tree_depth--;
449 }
450
451 static void
452 validate_accessible (Accessible *accessible,
453                      gboolean    has_parent,
454                      gboolean    recurse_down)
455 {
456         Accessible    *tmp;
457         char          *name, *descr;
458         AccessibleRole role;
459         char          *role_name;
460         GString       *item_str = g_string_new ("");
461
462         name = Accessible_getName (accessible);
463         g_assert (name != NULL);
464         
465         descr = Accessible_getDescription (accessible);
466         g_assert (descr != NULL);
467
468         role = Accessible_getRole (accessible);
469         g_assert (role != SPI_ROLE_INVALID);
470         role_name = Accessible_getRoleName (accessible);
471         g_assert (role_name != NULL);
472         
473
474         if (print_tree) {
475                 int i;
476
477                 for (i = 0; i < print_tree_depth; i++)
478                         fputc (' ', stderr);
479                 fputs ("|-> [ ", stderr);
480         }
481
482         if (Accessible_isAction (accessible)) {
483                 tmp = Accessible_getAction (accessible);
484                 g_assert (tmp != NULL);
485                 if (print_tree)
486                         fprintf (stderr, "At");
487                 else
488                         test_action (tmp);
489                 AccessibleAction_unref (tmp);
490         }
491
492         if (Accessible_isApplication (accessible)) {
493                 tmp = Accessible_getApplication (accessible);
494                 if (print_tree)
495                         fprintf (stderr, "Ap");
496                 else
497                         test_application (tmp);
498                 AccessibleApplication_unref (tmp);
499         }
500
501         if (Accessible_isComponent (accessible)) {
502                 tmp = Accessible_getComponent (accessible);
503                 g_assert (tmp != NULL);
504                 if (print_tree)
505                         fprintf (stderr, "Co");
506                 else
507                         test_component (tmp);
508                 AccessibleComponent_unref (tmp);
509         }
510
511         if (Accessible_isEditableText (accessible)) {
512                 tmp = Accessible_getEditableText (accessible);
513                 g_assert (tmp != NULL);
514                 if (print_tree)
515                         fprintf (stderr, "Et");
516                 else
517                         test_editable_text (tmp);
518                 AccessibleEditableText_unref (tmp);
519         }
520
521         if (Accessible_isHypertext (accessible)) {
522                 tmp = Accessible_getHypertext (accessible);
523                 g_assert (tmp != NULL);
524                 if (print_tree)
525                         fprintf (stderr, "Ht");
526                 AccessibleHypertext_unref (tmp);
527         }
528
529         if (Accessible_isImage (accessible)) {
530                 tmp = Accessible_getImage (accessible);
531                 g_assert (tmp != NULL);
532                 if (print_tree) {
533                         char *desc;
534
535                         fprintf (stderr, "Im");
536
537                         desc = AccessibleImage_getImageDescription (tmp);
538                         g_string_append_printf (
539                                 item_str, " image descr: '%s'", desc);
540                         SPI_freeString (desc);
541                 } else
542                         test_image (tmp);
543
544                 AccessibleImage_unref (tmp);
545         }
546
547         if (Accessible_isSelection (accessible)) {
548                 tmp = Accessible_getSelection (accessible);
549                 g_assert (tmp != NULL);
550                 if (print_tree)
551                         fprintf (stderr, "Se");
552                 AccessibleSelection_unref (tmp);
553         }
554
555         if (Accessible_isTable (accessible)) {
556                 tmp = Accessible_getTable (accessible);
557                 g_assert (tmp != NULL);
558                 if (print_tree)
559                         fprintf (stderr, "Ta");
560                 else
561                         test_table (tmp);
562                 AccessibleTable_unref (tmp);
563         }
564
565         if (Accessible_isText (accessible)) {
566                 tmp = Accessible_getText (accessible);
567                 g_assert (tmp != NULL);
568                 if (print_tree)
569                         fprintf (stderr, "Te");
570                 else
571                         test_text (tmp);
572                 AccessibleText_unref (tmp);
573         }
574
575         if (Accessible_isValue (accessible)) {
576                 tmp = Accessible_getValue (accessible);
577                 g_assert (tmp != NULL);
578                 if (print_tree)
579                         fprintf (stderr, "Va");
580                 else
581                         test_value (tmp); 
582                 AccessibleValue_unref (tmp);
583         }
584
585         if (print_tree)
586                 fprintf (stderr, " ] '%s' (%s) - %s: %s\n",
587                          name, descr, role_name, item_str->str);
588
589         SPI_freeString (name);
590         SPI_freeString (descr);
591         SPI_freeString (role_name);
592         g_string_free (item_str, TRUE);
593
594         validate_tree (accessible, has_parent, recurse_down);
595 }
596
597 static void
598 test_misc (void)
599 {
600         fprintf (stderr, "Testing misc bits ...\n");
601
602         g_assert (!Accessible_isComponent (NULL));
603         g_assert (Accessible_getComponent (NULL) == NULL);
604         SPI_freeString (NULL);
605 }
606
607 static void
608 global_listener_cb (AccessibleEvent     *event,
609                     void                *user_data)
610 {
611         TestWindow *win = user_data;
612         Accessible *desktop;
613         AccessibleApplication *application;
614
615         g_assert (win->magic == WINDOW_MAGIC);
616         g_assert (!strcmp (event->type, "focus:"));
617
618         fprintf (stderr, "Fielded focus event ...\n");
619
620         if (!do_poke) {
621                 desktop = SPI_getDesktop (0);
622                 application = Accessible_getChildAtIndex (desktop, 0);
623                 g_assert (application != NULL);
624                 Accessible_unref (desktop);
625                 
626                 test_application (application);
627                 
628                 AccessibleApplication_unref (application);
629                 
630                 print_tree = FALSE;
631                 validate_accessible (event->source, TRUE, TRUE);
632
633                 gtk_main_quit ();
634         }
635
636         print_tree = TRUE;
637         validate_accessible (event->source, TRUE, TRUE);
638 }
639
640 int
641 main (int argc, char **argv)
642 {
643         int leaked, i;
644         TestWindow *win;
645         const char *modules;
646         AccessibleEventListener *global_listener;
647
648         modules = g_getenv ("GTK_MODULES");
649         if (!modules || modules [0] == '\0')
650                 putenv ("GTK_MODULES=gail:at-bridge");
651         modules = NULL;
652
653         for (i = 1; i < argc; i++) {
654                 if (!g_strcasecmp (argv [i], "--poke"))
655                         do_poke = TRUE;
656         }
657
658         gtk_init (&argc, &argv);
659
660         g_assert (!SPI_init ());
661         g_assert (SPI_init ());
662         g_assert (SPI_getDesktopCount () == 1);
663
664         test_roles ();
665         test_misc ();
666         test_desktop ();
667
668         win = create_test_window ();
669
670         global_listener = SPI_createAccessibleEventListener (global_listener_cb, win);
671         g_assert (SPI_registerGlobalEventListener (global_listener, "focus:"));
672
673         fprintf (stderr, "Waiting for focus event ...\n");
674         gtk_main ();
675
676         g_assert (SPI_deregisterGlobalEventListenerAll (global_listener));
677         AccessibleEventListener_unref (global_listener);
678
679         test_window_destroy (win);
680
681         if ((leaked = SPI_exit ()))
682                 g_error ("Leaked %d SPI handles", leaked);
683
684         g_assert (!SPI_exit ());
685
686         fprintf (stderr, "All tests passed\n");
687
688         if (g_getenv ("_MEMPROF_SOCKET")) {
689                 fprintf (stderr, "Waiting for memprof\n");
690                 gtk_main ();
691         }
692
693         putenv ("AT_BRIDGE_SHUTDOWN=1");
694
695         return 0;
696 }