550f810c77288ab34781de19127c8f51936a834c
[platform/core/uifw/at-spi2-atk.git] / tests / apps / test-application.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Codethink Ltd.
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  * Testing AT-SPI requires both a test application and AT client. 
25  * Test applications are built using the Dummy ATK implementation: MyAtk.
26  * This file contains the entry point for all test applications.
27  * Each test is built as a GModule, and this program loads the 
28  * test module, as well as the AT-SPI module. The test module will
29  * provide its own implementation of atk_get_root, and as such provide
30  * all the application state for the test.
31  */
32
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <gmodule.h>
36 #include <atk/atk.h>
37 #include <dbus/dbus.h>
38
39 /* The test module, GModule containing interface for an atk-test */
40 static GModule *test_module;
41 static gpointer test_module_get_root;
42 static gpointer test_module_next;
43 static gpointer test_module_finished;
44
45 static DBusConnection *dbus_bus;
46 static GMainLoop *mainloop;
47
48 /* Test module interface */
49 /*************************/
50
51 typedef AtkObject *(*TestModuleGetRoot) (void);
52
53 /* Calls into the test module to get the root atk object */
54 static AtkObject *
55 get_root(void)
56 {
57   return ((TestModuleGetRoot) test_module_get_root)();
58 }
59
60 typedef void (*VoidVoid) (void);
61
62 /* Called to move to next stage of test.*/
63 static void
64 next(void)
65 {
66   ((VoidVoid) test_module_next)();
67 }
68
69
70 /*************************/
71
72 /* The AtkUtil class is called to find the root accessible and to deal
73  * with events. Its an incomplete class, its v-table needs to be filled in.
74  */
75 static void
76 setup_atk_util(void)
77 {
78   AtkUtilClass *klass;
79
80   klass = g_type_class_ref(ATK_TYPE_UTIL);
81   klass->get_root = get_root;
82   g_type_class_unref(klass);
83 }
84
85 typedef void (*GtkModuleInit) (int *argc, char **argv[]);
86
87 /* AT-SPI is a gtk module that must be loaded and initialized */
88 static void
89 load_atspi_module(const char *path, int *argc, char **argv[])
90 {
91   GModule *bridge;
92   gpointer init;
93
94   bridge = g_module_open(path, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
95   if (!bridge)
96     g_error("Couldn't load atk-bridge module : %s\n", path);
97
98   if (!g_module_symbol(bridge, "gtk_module_init", &init))
99     g_error("Couldn't load symbol \"gtk_module_init\"\n");
100
101   ((GtkModuleInit) init)(argc, argv);
102 }
103
104 typedef void (*TestModuleInit) (gchar *path);
105
106 static void
107 load_test_module(const char *path, const char *tdpath)
108 {
109   gpointer init;
110
111   test_module = g_module_open(path, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
112   if (!test_module)
113     g_error("Couldn't load test module : %s\n", path);
114
115   if (!g_module_symbol(test_module, "test_init", &init))
116     g_error("Couldn't load symbol \"test_init\"\n");
117
118   if (!g_module_symbol(test_module, "test_get_root", &test_module_get_root))
119     g_error("Couldn't load symbol \"test_get_root\"\n");
120
121   if (!g_module_symbol(test_module, "test_next", &test_module_next))
122     g_error("Couldn't load symbol \"test_next\"\n");
123
124   if (!g_module_symbol(test_module, "test_finished", &test_module_finished))
125     g_error("Couldn't load symbol \"test_finished\"\n");
126
127   ((TestModuleInit) init)((gchar *)tdpath);
128 }
129
130 static const char* introspection_string = 
131 "<node name=\"/org/codethink/atspi/test\">"
132 "       <interface name=\"org.codethink.atspi.test\">"
133 "               <method name=\"next\"/>"
134 "               <method name=\"finish\"/>"
135 "               <signal name=\"started\"/>"
136 "       </interface>"
137 "</node>";
138
139 static DBusHandlerResult
140 message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
141 {
142   const char *iface = dbus_message_get_interface (message);
143   const char *member = dbus_message_get_member (message);
144   DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
145   gboolean exit = FALSE;
146
147   DBusMessage *reply = NULL;
148
149   g_return_val_if_fail(iface != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
150   
151   if (!strcmp(iface, "org.codethink.atspi.test"))
152     {
153       if (!strcmp(member, "next"))
154         {
155           next();
156           reply = dbus_message_new_method_return (message);
157           g_assert(reply != NULL);
158           result = DBUS_HANDLER_RESULT_HANDLED;
159         }
160
161       if (!strcmp(member, "finish"))
162         {
163           ((VoidVoid) test_module_finished)();
164           reply = dbus_message_new_method_return (message);
165           g_assert(reply != NULL);
166           result = DBUS_HANDLER_RESULT_HANDLED;
167           exit = TRUE;
168         }
169     }
170
171   if (!strcmp(iface, "org.freedesktop.DBus.Introspectable"))
172     {
173       if (!strcmp(member, "Introspect"))
174         {
175           reply = dbus_message_new_method_return (message);
176           g_assert(reply != NULL);
177           dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_string,
178                                    DBUS_TYPE_INVALID);
179           result = DBUS_HANDLER_RESULT_HANDLED;
180         }
181     }
182
183   if (reply)
184     {
185       dbus_connection_send (bus, reply, NULL);
186       dbus_message_unref (reply);
187     }
188
189   if (exit == TRUE)
190     {
191       dbus_connection_flush(bus);
192       dbus_connection_unref(bus);
193       g_main_loop_quit(mainloop);
194       abort();
195     }
196   return result;
197 }
198
199 static DBusObjectPathVTable test_vtable =
200 {
201   NULL,
202   &message_handler,
203   NULL, NULL, NULL, NULL
204 };
205
206 static void
207 init_dbus_interface(void)
208 {
209   DBusError error;
210
211   dbus_error_init(&error);
212   dbus_bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
213   g_print("\nUnique D-Bus name is: %s\n", dbus_bus_get_unique_name(dbus_bus));
214
215   if (!dbus_bus)
216     g_error("Couldn't get the session bus - %s\n", error.message);
217
218   g_assert(dbus_connection_register_object_path(dbus_bus,
219                                                 "/org/codethink/atspi/test",
220                                                 &test_vtable,
221                                                 NULL));
222
223   dbus_connection_setup_with_g_main(dbus_bus, g_main_context_default());
224 }
225
226 static void
227 send_started_signal(void)
228 {
229   DBusMessage* sig;
230   DBusMessageIter args;
231
232   sig = dbus_message_new_signal("/org/codethink/atspi/test", "org.codethink.atspi.test", "started");
233   g_assert(sig != NULL);
234   if (!dbus_connection_send(dbus_bus, sig, NULL))
235     g_error("Out of memory");
236   dbus_connection_flush(dbus_bus);
237   dbus_message_unref(sig);
238 }
239
240 /*Command line data*/
241 static gchar *tmodule_path = NULL;
242 static gchar *amodule_path = NULL;
243 static gchar *tdata_path = NULL;
244
245 static GOptionEntry optentries[] = 
246 {
247   {"test-module", 0, 0, G_OPTION_ARG_STRING, &tmodule_path, "Module containing test scenario", NULL},
248   {"test-atspi-library", 0, 0, G_OPTION_ARG_STRING, &amodule_path, "Gtk module with atk-atspi adaptor", NULL},
249   {"test-data-directory", 0, 0, G_OPTION_ARG_STRING, &tdata_path, "Path to directory of test data", NULL},
250   {NULL}
251 };
252
253 /* main
254  * 
255  * Entry point for all test applications.
256  */
257 main(int argc, char *argv[])
258 {
259   GOptionContext *opt;
260   GError *err = NULL;
261
262   /*Parse command options*/
263   opt = g_option_context_new(NULL);
264   g_option_context_add_main_entries(opt, optentries, NULL);
265   g_option_context_set_ignore_unknown_options(opt, TRUE);
266   
267   if (!g_option_context_parse(opt, &argc, &argv, &err))
268       g_error("Option parsing failed: %s\n", err->message);
269
270   if (tmodule_path == NULL)
271       g_error("No test module provided");
272   if (amodule_path == NULL)
273       g_error("No atspi module provided");
274
275   g_type_init();
276
277   setup_atk_util();
278   load_test_module(tmodule_path, tdata_path);
279   load_atspi_module(amodule_path, &argc, &argv);
280   init_dbus_interface();
281   send_started_signal();
282
283   mainloop = g_main_loop_new (NULL, FALSE);
284   g_main_loop_run (mainloop);
285
286   return 0;
287 }