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