summaryrefslogtreecommitdiff
path: root/tests/at-spi2-atk/atk_test_util.c
blob: 2af00135f51e348ac8ae542363a04fba76f05824 (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
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "atk_test_util.h"
#include <signal.h>

static AtspiEventListener *fixture_listener = NULL;
static TestAppFixture *current_fixture = NULL;

static pid_t
run_app (const char *file_name, const char *name_to_claim)
{
  pid_t child_pid = fork ();
  if (child_pid == 0)
    {
      execlp (TESTS_BUILD_DIR "/app-test",
              TESTS_BUILD_DIR "/app-test",
              "--test-data-file",
              file_name,
              "--atspi-dbus-name",
              name_to_claim,
              NULL);
      _exit (EXIT_SUCCESS);
    }

  return child_pid;
}

static AtspiAccessible *
try_get_root_obj (AtspiAccessible *obj)
{
  GError *error = NULL;
  gchar *name;
  int i;

  gint child_count = atspi_accessible_get_child_count (obj, &error);
  if (child_count < 0)
    {
      if (error)
        {
          g_print ("  get_child_count: %s\n", error->message);
          g_error_free (error);
        }
      else
        {
          g_print ("  get_child_count=%d with no error\n", child_count);
        }
      return NULL;
    }
  else if (child_count < 1)
    {
      g_print ("  child_count == %d, bailing out\n", child_count);
      return NULL;
    }

  for (i = 0; i < child_count; i++)
    {
      AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, i, &error);
      if (!child)
        {
          if (error)
            {
              g_print ("  getting child_at_index: %s\n", error->message);
              g_error_free (error);
            }
          else
            {
              g_print ("  getting child_at_index returned NULL child with no error\n");
            }
          continue;
        }
      if ((name = atspi_accessible_get_name (child, &error)) != NULL)
        {
          if (!strcmp (name, "root_object"))
            {
              g_free (name);
              return child;
            }
          g_print ("  name=%s\n", name);
          g_free (name);
        }
      else
        {
          if (error)
            {
              g_print ("try_get_root_obj getting child name: %s\n", error->message);
              g_error_free (error);
            }
          else
            {
              g_print ("  get_name returned NULL name with no error\n");
            }
        }
      g_object_unref (child);
    }

  return NULL;
}

/* Callback from AtspiEventListener.  We monitor children-changed on the root, so we can know
 * when the helper test-application has launched and registered.
 */
static void
listener_event_cb (AtspiEvent *event, void *user_data)
{
  TestAppFixture *fixture = current_fixture;

  if (atspi_accessible_get_role (event->source, NULL) == ATSPI_ROLE_DESKTOP_FRAME && strstr (event->type, "add"))
    {
      AtspiAccessible *obj = atspi_get_desktop (0);

      fixture->root_obj = try_get_root_obj (obj);

      if (fixture->root_obj)
        {
          fixture->state = FIXTURE_STATE_CHILD_ACQUIRED;
          atspi_event_quit ();
        }
    }
}

/* Sets up the atspi event listener for the test-application helpers.
 *
 * We get notified when the test-application registers its root object by listening
 * to the children-changed signal.
 */
void
fixture_listener_init (void)
{
  GError *error = NULL;

  fixture_listener = atspi_event_listener_new (listener_event_cb, NULL, NULL);
  if (!atspi_event_listener_register (fixture_listener, "object:children-changed", &error))
    {
      g_error ("Could not register event listener for children-changed: %s\n", error->message);
    }
}

void
fixture_listener_destroy (void)
{
  GError *error = NULL;

  if (!atspi_event_listener_deregister (fixture_listener, "object:children-changed", &error))
    {
      g_error ("Could not deregister event listener: %s", error->message);
    }

  g_object_unref (fixture_listener);
  fixture_listener = NULL;
}

static gboolean
wait_for_test_app_timeout_cb (gpointer user_data)
{
  TestAppFixture *fixture = user_data;

  fixture->test_app_timed_out = TRUE;
  atspi_event_quit ();

  return FALSE;
}

/* Each of the helper programs with the test fixtures claims a different DBus name,
 * to make them non-ambiguous when they get restarted all the time.  This is the serial
 * number that gets appended to each name.
 */
static guint fixture_serial = 0;

void
fixture_setup (TestAppFixture *fixture, gconstpointer user_data)
{
  const char *file_name = user_data;

  fixture->state = FIXTURE_STATE_WAITING_FOR_CHILD;
  fixture->name_to_claim = g_strdup_printf ("org.a11y.Atspi2Atk.TestApplication_%u", fixture_serial);
  fixture_serial += 1;

  fixture->child_pid = run_app (file_name, fixture->name_to_claim);

  fixture->test_app_timed_out = FALSE;
  fixture->wait_for_test_app_timeout = g_timeout_add (500, wait_for_test_app_timeout_cb, fixture); /* 500 msec */

  current_fixture = fixture;
  atspi_event_main ();

  g_source_remove (fixture->wait_for_test_app_timeout);
  fixture->wait_for_test_app_timeout = 0;

  if (fixture->test_app_timed_out)
    {
      g_print ("test app timed out before registering its root object");
      g_test_fail ();
    }
}

void
fixture_teardown (TestAppFixture *fixture, gconstpointer user_data)
{
  current_fixture = NULL;

  kill (fixture->child_pid, SIGTERM);
  fixture->child_pid = -1;

  if (fixture->root_obj)
    {
      g_object_unref (fixture->root_obj);
      fixture->root_obj = NULL;
    }

  g_free (fixture->name_to_claim);
  fixture->name_to_claim = NULL;
}