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