73c3c8c709ba5c0f1db6185c11577eee06aa5c53
[platform/core/uifw/at-spi2-atk.git] / atk-tests / main.c
1 #include "atk/atk.h"
2 #include "hello.h"
3 #include "test-simple-table.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <termios.h>
8 #include <unistd.h>
9
10 static AtkObject *root;
11 static GList *children;
12
13 gboolean quit(GIOChannel *source, GIOCondition condition, gpointer data)
14 {
15   //g_signal_emit_by_name(root, "children-changed::remove", child);
16   tcsetattr(0, TCSAFLUSH, data);
17   exit(0);
18 }
19
20
21 // our parent class
22 static AtkObjectClass *parent_class = NULL;
23
24 /**************************************************************************/
25 /**************************************************************************/
26
27 #define GET_CLASS(instance) G_TYPE_INSTANCE_GET_CLASS (instance, AP_DOCVIEW_TYPE, ApDocViewClass)
28
29 static gint test_hello_get_n_children(AtkObject * obj)
30 {
31 printf("get n children: %p\n", obj);
32   if (obj == root) return g_list_length(children);
33   return 0;
34 }
35
36 static AtkStateSet *test_hello_ref_state_set(AtkObject * accessible)
37 {
38     AtkStateSet *state_set;
39
40     state_set = ATK_OBJECT_CLASS(parent_class)->ref_state_set(accessible);
41     //atk_state_set_remove_state(state_set, ATK_STATE_FOCUSABLE);
42
43     return state_set;
44 }
45
46 static void test_hello_real_initialize(AtkObject * obj, gpointer data)
47 {
48   ATK_OBJECT_CLASS(parent_class)->initialize(obj, data);
49   printf("Initialize: %p\n", obj);
50 }
51
52 static void test_hello_finalize(AtkObject * obj)
53 {
54   printf("Finalizing: %p\n", obj);
55 }
56
57 static AtkObject *test_hello_ref_child(AtkObject * obj, gint i)
58 {
59   gpointer data;
60   printf("ref child: %p, index %d\n", obj, i);
61   if (obj != root) return NULL;
62   data = g_list_nth_data(children, i);
63   if (data) return ATK_OBJECT(data);
64   return NULL;
65 }
66
67 static void test_hello_class_init(test_helloClass * klass)
68 {
69     AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
70
71     parent_class = (AtkObjectClass *) g_type_class_peek_parent(klass);
72     atk_class->ref_state_set = test_hello_ref_state_set;
73     atk_class->get_n_children = test_hello_get_n_children;
74     atk_class->ref_child = test_hello_ref_child;
75     atk_class->initialize = test_hello_real_initialize;
76 }
77
78 static gint test_hello_get_caret_offset(AtkText * text)
79 {
80   test_hello *obj = TEST_HELLO(text);
81   return 0;
82 }
83
84 static void atk_text_interface_init (AtkTextIface * iface)
85 {
86     g_return_if_fail (iface != NULL);
87     iface->get_caret_offset = test_hello_get_caret_offset;
88 }
89
90 GType test_hello_get_type(void)
91 {
92     static GType type = 0;
93
94     if (!type)
95     {
96         static const GTypeInfo tinfo = {
97             sizeof(test_helloClass),
98             (GBaseInitFunc) NULL,       /* base init */
99             (GBaseFinalizeFunc) test_hello_finalize,    /* base finalize */
100             (GClassInitFunc) test_hello_class_init,     /* class init */
101             (GClassFinalizeFunc) NULL,  /* class finalize */
102             NULL,               /* class data */
103             sizeof(test_hello), /* instance size */
104             0,                  /* nb preallocs */
105             (GInstanceInitFunc) NULL,   /* instance init */
106             NULL                /* value table */
107         };
108
109         static const GInterfaceInfo atk_text_info = {
110             (GInterfaceInitFunc) atk_text_interface_init,
111             (GInterfaceFinalizeFunc) NULL,
112             NULL
113         };
114
115 #if 0
116         /* Following code would use the factory to fetch the parent type
117          * based on the atk type associated with GTK_TYPE_WIDGET */
118         GType parent_type;
119         AtkObjectFactory *factory;
120
121         factory =
122             atk_registry_get_factory(atk_get_default_registry(),
123                                      GTK_TYPE_WIDGET);
124         parent_type = atk_object_factory_get_accessible_type(factory);
125 #endif
126
127         type = g_type_register_static(atk_object_get_type(),
128                                       "test_hello", &tinfo, (GTypeFlags) 0);
129         g_type_add_interface_static (type, ATK_TYPE_TEXT, &atk_text_info);
130     }
131     return type;
132 }
133 static AtkObject *get_root()
134 {
135   return root;
136 }
137
138 static void add_child(AtkObject *child)
139 {
140   if (!child) return;
141   children = g_list_append(children, child);
142   g_signal_emit_by_name(root, "children-changed::add", child);
143 }
144
145 main(int argc, char *argv[])
146 {
147   AtkUtilClass *klass;
148   GMainLoop *mainloop;
149   GIOChannel *stdin_channel;
150   struct termios termios, rt;
151   GModule *bridge;
152   void (*gnome_accessibility_module_init)();
153   AtkObject *table;
154
155   g_type_init();
156   klass = g_type_class_ref(ATK_TYPE_UTIL);
157   klass->get_root = get_root;
158   g_type_class_unref(klass);
159   root = g_object_new(TEST_TYPE_HELLO, NULL);
160   table = ATK_OBJECT(g_object_new(TEST_TYPE_SIMPLE_TABLE, NULL));
161   add_child(table);
162   /* The below line isn't really right -- normally gtk will build the path */
163   bridge = g_module_open("/usr/lib/gtk-2.0/modules/libatk-bridge.so", G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
164   if (!bridge)
165   {
166     fprintf(stderr, "Couldn't load atk-bridge.so\n");
167     exit(1);
168   }
169   if (!g_module_symbol(bridge, "gnome_accessibility_module_init", (gpointer *)&gnome_accessibility_module_init))
170   {
171     fprintf(stderr, "Couldn't find gnome_accessibility_module_init\n");    exit(1);
172   }
173   (*gnome_accessibility_module_init)();
174   atk_object_set_name(root, "root object");
175
176   mainloop = g_main_loop_new (NULL, FALSE);
177
178   /* Make stdin "raw" so that we will see a key immediately */
179   tcgetattr(0, &termios);
180   rt = termios;
181   cfmakeraw(&rt);
182   tcsetattr(0, TCSAFLUSH, &rt);
183   /* Set a watch on stdin so that we'll know when a key is pressed */
184   stdin_channel = g_io_channel_unix_new(0);
185   g_io_add_watch(stdin_channel, G_IO_IN, quit, &termios);
186
187   g_main_loop_run (mainloop);
188   return 0;
189 }