Merge "custom eail widget implementation" into tizen
[platform/core/uifw/eail.git] / tests / eail_test_utils.c
1 #include <atk/atk.h>
2 #include <Elementary.h>
3 #include <string.h>
4
5 #include "eail_test_utils.h"
6
7 #define EAILU_WINDOW_TEST_TITLE "Eail test window"
8 #define EAILU_TEST_WINDOW_WIDTH 420
9 #define EAILU_TEST_WINDOW_HEIGHT 420
10
11 gboolean
12 eailu_is_object_with_type(AtkObject *obj, gchar *type_name)
13 {
14    const char *obj_type = g_type_name(G_TYPE_FROM_INSTANCE(obj));
15
16    if ((!type_name) || (!obj)) return FALSE;
17
18    return !(strcmp(obj_type, type_name));
19 }
20
21 gboolean
22 eailu_is_object_with_name(AtkObject *obj, gchar *name)
23 {
24    const char *obj_name = NULL;
25
26    if (!ATK_IS_OBJECT(obj))
27      return FALSE;
28
29    obj_name = atk_object_get_name(obj);
30    if ((!obj_name) || (!name))
31      return FALSE;
32
33    return !(strcmp(obj_name, name));
34 }
35
36 gboolean
37 eailu_is_object_with_role(AtkObject *obj, AtkRole role)
38 {
39    AtkRole obj_role = ATK_ROLE_INVALID;
40
41    if (!ATK_IS_OBJECT(obj))
42      return FALSE;
43
44    obj_role = atk_object_get_role(obj);
45
46    return (obj_role == role);
47 }
48
49 AtkObject *
50 eailu_find_child_with_name(AtkObject *obj, gchar *name)
51 {
52    int child_count = 0, i = 0;
53
54    if (eailu_is_object_with_name(obj, name))
55      return obj;
56
57    child_count = atk_object_get_n_accessible_children(obj);
58    for (i = 0; i < child_count; i++)
59      {
60         AtkObject *found_obj = NULL;
61         AtkObject *child = atk_object_ref_accessible_child(obj, i);
62
63         found_obj = eailu_find_child_with_name(child, name);
64         if (found_obj != NULL)
65           return found_obj;
66
67         g_object_unref(child);
68      }
69
70    return NULL;
71 }
72
73 void
74 eailu_traverse_children_for_type_call_cb(AtkObject *obj,
75                                          gchar *type_name,
76                                          Eailu_Test_Func_Cb test_cb)
77 {
78    int child_count = atk_object_get_n_accessible_children(obj);
79    int i;
80    for (i = 0; i < child_count; i++)
81      {
82         AtkObject *child = atk_object_ref_accessible_child(obj, i);
83         if (ATK_IS_OBJECT(child))
84           {
85              if (type_name == NULL ||
86                       eailu_is_object_with_type(child, type_name))
87                test_cb(child);
88           }
89
90         eailu_traverse_children_for_type_call_cb(child, type_name, test_cb);
91
92         g_object_unref(child);
93      }
94 }
95
96 void
97 eailu_traverse_children_for_role_call_cb(AtkObject *obj,
98                                          AtkRole role,
99                                          Eailu_Test_Func_Cb test_cb)
100 {
101    gint number_of_children = atk_object_get_n_accessible_children(obj);
102    gint children_count;
103
104    for (children_count = 0; number_of_children > children_count; children_count++)
105      {
106         AtkObject *child = atk_object_ref_accessible_child(obj, children_count);
107
108         if (eailu_is_object_with_role(child, role))
109           test_cb(child);
110
111         eailu_traverse_children_for_role_call_cb(child, role, test_cb);
112         g_object_unref(child);
113      }
114 }
115
116 Evas_Object *
117 eailu_create_test_window_with_glib_init(Evas_Smart_Cb on_done,
118                                         Evas_Smart_Cb on_focus_in)
119 {
120    Evas_Object *win;
121
122    if (!ecore_main_loop_glib_integrate())
123      {
124         fprintf(stderr, "Cannot integrate with glib main loop");
125         return NULL;
126      }
127
128 #if !GLIB_CHECK_VERSION(2,35,0)
129    g_type_init();
130 #endif
131
132    win = elm_win_add(NULL, EAILU_WINDOW_TEST_TITLE, ELM_WIN_BASIC);
133    elm_win_title_set(win, EAILU_WINDOW_TEST_TITLE);
134    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
135    evas_object_smart_callback_add(win, "delete,request", on_done, NULL);
136    elm_win_autodel_set(win, EINA_TRUE);
137
138    evas_object_smart_callback_add(win, "focus,in", on_focus_in, NULL);
139    evas_object_resize(win, EAILU_TEST_WINDOW_WIDTH, EAILU_TEST_WINDOW_HEIGHT);
140
141    return win;
142 }
143
144 void
145 eailu_test_atk_focus(AtkObject *obj, gboolean focusable)
146 {
147    AtkStateSet *state_set = NULL;
148
149    if (focusable)
150      {
151         /* checking is setting focus works properly */
152         state_set = atk_object_ref_state_set(obj);
153
154         g_assert(atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE));
155         g_assert(!atk_state_set_contains_state(state_set, ATK_STATE_FOCUSED));
156
157         /* grabbing focus */
158         g_assert(ATK_IS_COMPONENT(obj));
159
160         atk_component_grab_focus(ATK_COMPONENT(obj));
161         state_set = atk_object_ref_state_set(obj);
162         g_assert(
163             atk_state_set_contains_state(state_set, ATK_STATE_FOCUSED));
164      }
165     else
166      {
167         state_set = atk_object_ref_state_set(obj);
168         g_assert(
169              !atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE));
170         g_assert(
171              !atk_state_set_contains_state(state_set, ATK_STATE_FOCUSED));
172      }
173 }
174
175 void
176 _eailu_print_atk_text_obj(AtkText *atk_text)
177 {
178    int count = 0, i = 0;
179    char *text = atk_text_get_text(atk_text, 0, -1);
180
181    printf("ATK TEXT:\n");
182    printf("atk_text_get_text: %s\n", text ? text : "NULL");
183    count = atk_text_get_character_count(atk_text);
184    printf("atk_text_get_character_count: %d\n", count);
185
186    printf("Text from offset single chars: ");
187    for (i = 0; i < count; ++i)
188      {
189         printf("%c", atk_text_get_character_at_offset(atk_text, i));
190      }
191    printf("\n");
192 }
193
194 void
195 eailu_print_atk_object_info(AtkObject *obj)
196 {
197    const char *name = NULL;
198    const char *type_name = NULL;
199    const char *role_name = NULL;
200    AtkRole atk_role = ATK_ROLE_INVALID;
201
202    name = atk_object_get_name(ATK_OBJECT(obj));
203    type_name = g_type_name(G_TYPE_FROM_INSTANCE(ATK_OBJECT(obj)));
204    atk_role = atk_object_get_role(ATK_OBJECT(obj));
205    role_name = atk_role_get_name(atk_role);
206
207    printf("atk_object_get_name: %s\n", name ? name : "NULL");
208    printf("atk_object_get_role: %s\n", role_name ? role_name : "NULL");
209    printf("atk_object_get_type_name: %s\n", type_name ? type_name : "NULL");
210
211    if (ATK_IS_TEXT(obj))
212      {
213         _eailu_print_atk_text_obj(ATK_TEXT(obj));
214      }
215
216    printf("\n");
217 }
218
219 /**
220  * @return returns action index number or -1 if action with given name has
221  * not been found
222  */
223 int
224 eailu_get_action_number(AtkAction *action_obj, const gchar *action_name)
225 {
226    int i = 0;
227    int action_index = -1;
228    const int actions_num = atk_action_get_n_actions(action_obj);
229
230    for (i = 0; i < actions_num; i++)
231      {
232         const char *local_name = atk_action_get_name(action_obj, i);
233         if ((strcmp(action_name, local_name)) == 0)
234           return i;
235      }
236
237    return action_index;
238 }
239
240 void
241 eailu_test_action_activate(AtkAction *action_obj, const gchar *action_name)
242 {
243    int action_index = -1;
244
245    action_index = eailu_get_action_number(action_obj, action_name);
246    g_assert(-1 != action_index);
247    g_assert(TRUE == atk_action_do_action(action_obj, action_index));
248 }
249
250 void
251 eailu_test_action_description_all(AtkAction *action_obj)
252 {
253    int actions_num = atk_action_get_n_actions(action_obj);
254    const char *desc_set = "description test";
255    const char *desc_get;
256
257    int count;
258    for (count = 0; count < actions_num; ++count)
259      {
260         g_assert(atk_action_get_name(action_obj, count));
261         g_assert(atk_action_set_description(action_obj, count, desc_set));
262         desc_get = atk_action_get_description(action_obj, count);
263         g_assert(desc_get);
264         g_assert(!strcmp(desc_set, desc_get));
265      }
266
267    g_assert(NULL == atk_action_get_name(action_obj, actions_num));
268    g_assert(FALSE == atk_action_set_description(action_obj, actions_num, desc_set));
269    g_assert(NULL == atk_action_get_description(action_obj, actions_num));
270 }