summaryrefslogtreecommitdiff
path: root/tests/atk_test_util.c
blob: 366725c456b58b39871f9250d72dea26d2199125 (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
/*
 * 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 <signal.h>
#include "atk_test_util.h"

pid_t child_pid;

static void assert_clean_exit (int sig)
{
  kill (child_pid, SIGTERM);
}

void clean_exit_on_fail ()
{
  signal (SIGABRT, assert_clean_exit);
}

void
run_app (const char *file_name)
{
  child_pid = fork ();
  if (child_pid == 0) {
    execlp (TESTS_BUILD_DIR "/app-test",
            TESTS_BUILD_DIR "/app-test",
            "--test-data-file",
            file_name,
            NULL);
    _exit (EXIT_SUCCESS);
  }
  if (child_pid) fprintf(stderr, "child_pid %d\n", child_pid);
}

static AtspiAccessible *try_get_root_obj (AtspiAccessible *obj)
{
  gchar *name;
  int i;

  gint child_count = atspi_accessible_get_child_count (obj, NULL);
  if (child_count < 1) {
    return NULL;
  }

  for (i = 0; i < child_count; i++) {
    AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, i, NULL);
    if (!child)
      continue;
    if ((name = atspi_accessible_get_name (child, NULL)) != NULL) {
      if (!strcmp (name, "root_object")) {
        g_free(name);
        return child;
      }
      g_free(name);
    }
    g_object_unref (child);
  }

  return NULL;
}

AtspiAccessible * get_root_obj (const char *file_name)
{
  int tries = 0;
  AtspiAccessible *child;
  struct timespec timeout = { .tv_sec = 0, .tv_nsec = 10 * 1000000 };
  AtspiAccessible *obj = NULL;

  fprintf(stderr, "run_app: %s\n", file_name);
  run_app (file_name);

  obj = atspi_get_desktop (0);

  /* Wait for application to start, up to 100 times 10ms.  */
  while (++tries <= 100)
  {
    child = try_get_root_obj (obj);
    if (child)
      return child;

    nanosleep(&timeout, NULL);
  }

  if (atspi_accessible_get_child_count (obj, NULL) < 1) {
    g_test_message ("Fail, test application not found\n");
  } else {
    g_test_message ("test object not found\n");
  }
  g_test_fail ();
  kill (child_pid, SIGTERM);
  return NULL;
}

void terminate_app (void)
{
  int tries = 0;

  AtspiAccessible *child;
  struct timespec timeout = { .tv_sec = 0, .tv_nsec = 10 * 1000000 };
  AtspiAccessible *obj = NULL;

  kill (child_pid, SIGTERM);

  obj = atspi_get_desktop (0);

  /* Wait for application to stop, up to 100 times 10ms.  */
  while (++tries <= 100)
  {
    child = try_get_root_obj (obj);
    if (child == NULL)
      return;

    nanosleep(&timeout, NULL);
  }

  g_test_message ("Fail, test application still running\n");
  g_test_fail ();
}