summaryrefslogtreecommitdiff
path: root/tests/apps/test-application.c
blob: 550f810c77288ab34781de19127c8f51936a834c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2008 Codethink Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Testing AT-SPI requires both a test application and AT client. 
 * Test applications are built using the Dummy ATK implementation: MyAtk.
 * This file contains the entry point for all test applications.
 * Each test is built as a GModule, and this program loads the 
 * test module, as well as the AT-SPI module. The test module will
 * provide its own implementation of atk_get_root, and as such provide
 * all the application state for the test.
 */

#include <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include <atk/atk.h>
#include <dbus/dbus.h>

/* The test module, GModule containing interface for an atk-test */
static GModule *test_module;
static gpointer test_module_get_root;
static gpointer test_module_next;
static gpointer test_module_finished;

static DBusConnection *dbus_bus;
static GMainLoop *mainloop;

/* Test module interface */
/*************************/

typedef AtkObject *(*TestModuleGetRoot) (void);

/* Calls into the test module to get the root atk object */
static AtkObject *
get_root(void)
{
  return ((TestModuleGetRoot) test_module_get_root)();
}

typedef void (*VoidVoid) (void);

/* Called to move to next stage of test.*/
static void
next(void)
{
  ((VoidVoid) test_module_next)();
}


/*************************/

/* The AtkUtil class is called to find the root accessible and to deal
 * with events. Its an incomplete class, its v-table needs to be filled in.
 */
static void
setup_atk_util(void)
{
  AtkUtilClass *klass;

  klass = g_type_class_ref(ATK_TYPE_UTIL);
  klass->get_root = get_root;
  g_type_class_unref(klass);
}

typedef void (*GtkModuleInit) (int *argc, char **argv[]);

/* AT-SPI is a gtk module that must be loaded and initialized */
static void
load_atspi_module(const char *path, int *argc, char **argv[])
{
  GModule *bridge;
  gpointer init;

  bridge = g_module_open(path, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
  if (!bridge)
    g_error("Couldn't load atk-bridge module : %s\n", path);

  if (!g_module_symbol(bridge, "gtk_module_init", &init))
    g_error("Couldn't load symbol \"gtk_module_init\"\n");

  ((GtkModuleInit) init)(argc, argv);
}

typedef void (*TestModuleInit) (gchar *path);

static void
load_test_module(const char *path, const char *tdpath)
{
  gpointer init;

  test_module = g_module_open(path, G_MODULE_BIND_LOCAL|G_MODULE_BIND_LAZY);
  if (!test_module)
    g_error("Couldn't load test module : %s\n", path);

  if (!g_module_symbol(test_module, "test_init", &init))
    g_error("Couldn't load symbol \"test_init\"\n");

  if (!g_module_symbol(test_module, "test_get_root", &test_module_get_root))
    g_error("Couldn't load symbol \"test_get_root\"\n");

  if (!g_module_symbol(test_module, "test_next", &test_module_next))
    g_error("Couldn't load symbol \"test_next\"\n");

  if (!g_module_symbol(test_module, "test_finished", &test_module_finished))
    g_error("Couldn't load symbol \"test_finished\"\n");

  ((TestModuleInit) init)((gchar *)tdpath);
}

static const char* introspection_string = 
"<node name=\"/org/codethink/atspi/test\">"
"	<interface name=\"org.codethink.atspi.test\">"
"		<method name=\"next\"/>"
"		<method name=\"finish\"/>"
"               <signal name=\"started\"/>"
"	</interface>"
"</node>";

static DBusHandlerResult
message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
{
  const char *iface = dbus_message_get_interface (message);
  const char *member = dbus_message_get_member (message);
  DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  gboolean exit = FALSE;

  DBusMessage *reply = NULL;

  g_return_val_if_fail(iface != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
  
  if (!strcmp(iface, "org.codethink.atspi.test"))
    {
      if (!strcmp(member, "next"))
	{
	  next();
          reply = dbus_message_new_method_return (message);
	  g_assert(reply != NULL);
	  result = DBUS_HANDLER_RESULT_HANDLED;
	}

      if (!strcmp(member, "finish"))
	{
	  ((VoidVoid) test_module_finished)();
          reply = dbus_message_new_method_return (message);
	  g_assert(reply != NULL);
	  result = DBUS_HANDLER_RESULT_HANDLED;
	  exit = TRUE;
	}
    }

  if (!strcmp(iface, "org.freedesktop.DBus.Introspectable"))
    {
      if (!strcmp(member, "Introspect"))
	{
	  reply = dbus_message_new_method_return (message);
	  g_assert(reply != NULL);
	  dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_string,
				   DBUS_TYPE_INVALID);
	  result = DBUS_HANDLER_RESULT_HANDLED;
	}
    }

  if (reply)
    {
      dbus_connection_send (bus, reply, NULL);
      dbus_message_unref (reply);
    }

  if (exit == TRUE)
    {
      dbus_connection_flush(bus);
      dbus_connection_unref(bus);
      g_main_loop_quit(mainloop);
      abort();
    }
  return result;
}

static DBusObjectPathVTable test_vtable =
{
  NULL,
  &message_handler,
  NULL, NULL, NULL, NULL
};

static void
init_dbus_interface(void)
{
  DBusError error;

  dbus_error_init(&error);
  dbus_bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
  g_print("\nUnique D-Bus name is: %s\n", dbus_bus_get_unique_name(dbus_bus));

  if (!dbus_bus)
    g_error("Couldn't get the session bus - %s\n", error.message);

  g_assert(dbus_connection_register_object_path(dbus_bus,
						"/org/codethink/atspi/test",
					       	&test_vtable,
						NULL));

  dbus_connection_setup_with_g_main(dbus_bus, g_main_context_default());
}

static void
send_started_signal(void)
{
  DBusMessage* sig;
  DBusMessageIter args;

  sig = dbus_message_new_signal("/org/codethink/atspi/test", "org.codethink.atspi.test", "started");
  g_assert(sig != NULL);
  if (!dbus_connection_send(dbus_bus, sig, NULL))
    g_error("Out of memory");
  dbus_connection_flush(dbus_bus);
  dbus_message_unref(sig);
}

/*Command line data*/
static gchar *tmodule_path = NULL;
static gchar *amodule_path = NULL;
static gchar *tdata_path = NULL;

static GOptionEntry optentries[] = 
{
  {"test-module", 0, 0, G_OPTION_ARG_STRING, &tmodule_path, "Module containing test scenario", NULL},
  {"test-atspi-library", 0, 0, G_OPTION_ARG_STRING, &amodule_path, "Gtk module with atk-atspi adaptor", NULL},
  {"test-data-directory", 0, 0, G_OPTION_ARG_STRING, &tdata_path, "Path to directory of test data", NULL},
  {NULL}
};

/* main
 * 
 * Entry point for all test applications.
 */
main(int argc, char *argv[])
{
  GOptionContext *opt;
  GError *err = NULL;

  /*Parse command options*/
  opt = g_option_context_new(NULL);
  g_option_context_add_main_entries(opt, optentries, NULL);
  g_option_context_set_ignore_unknown_options(opt, TRUE);
  
  if (!g_option_context_parse(opt, &argc, &argv, &err))
      g_error("Option parsing failed: %s\n", err->message);

  if (tmodule_path == NULL)
      g_error("No test module provided");
  if (amodule_path == NULL)
      g_error("No atspi module provided");

  g_type_init();

  setup_atk_util();
  load_test_module(tmodule_path, tdata_path);
  load_atspi_module(amodule_path, &argc, &argv);
  init_dbus_interface();
  send_started_signal();

  mainloop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (mainloop);

  return 0;
}