c776ae1ed5bd68ceb2e3d206d966e357c956f694
[platform/upstream/at-spi2-atk.git] / tests / atk_test_util.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; https://wiki.gnome.org/Accessibility)
4  *
5  * Copyright (c) 2015 Samsung Electronics Co., 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 #include <signal.h>
24 #include "atk_test_util.h"
25
26 static void assert_clean_exit (int sig)
27 {
28   kill (child_pid, SIGTERM);
29 }
30
31 void clean_exit_on_fail ()
32 {
33   signal (SIGABRT, assert_clean_exit);
34 }
35
36 void
37 run_app (const char *file_name)
38 {
39   child_pid = fork ();
40   if (child_pid == 0) {
41     execlp ("./app-test",
42             "./app-test",
43             "--test-data-file",
44             file_name,
45             NULL);
46     _exit (EXIT_SUCCESS);
47   }
48 }
49
50 AtspiAccessible * get_root_obj (const char *file_name)
51 {
52   int i;
53   AtspiAccessible *obj = NULL;
54
55   run_app (file_name);
56
57   /* sleep is needed to wait for fored test application*/
58   sleep (1);
59
60   obj = atspi_get_desktop (0);
61   gint child_count = atspi_accessible_get_child_count (obj, NULL);
62   if (child_count < 1) {
63     g_test_message ("Fail, test application not found\n");
64     g_test_fail ();
65     kill (child_pid, SIGTERM);
66     return NULL;
67   }
68
69   for (i=0; i<child_count; i++) {
70     AtspiAccessible *child = atspi_accessible_get_child_at_index (obj,i, NULL);
71     if (!strcmp (atspi_accessible_get_name (child, NULL), "root_object"))
72       return child;
73   }
74   g_test_message ("test object not found\n");
75   g_test_fail ();
76
77   kill (child_pid, SIGTERM);
78   return NULL;
79 }