2.38.0
[platform/upstream/at-spi2-atk.git] / tests / atk_test_state_set.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 Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "atk_suite.h"
24 #include "atk_test_util.h"
25
26 #define DATA_FILE TESTS_DATA_DIR"/test-accessible.xml"
27
28 static void
29 teardown_state_set_test (gpointer fixture, gconstpointer user_data)
30 {
31   terminate_app ();
32 }
33
34 static void
35 atk_test_accessible_get_state_set (gpointer fixture, gconstpointer user_data)
36 {
37   AtspiAccessible *obj = get_root_obj (DATA_FILE);
38   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
39   AtspiStateSet *states = atspi_accessible_get_state_set (child);
40   GArray *states_arr = atspi_state_set_get_states (states);
41
42   AtspiStateType valid_states[] = {
43     ATSPI_STATE_MODAL,
44     ATSPI_STATE_MULTI_LINE,
45   };
46   g_assert_cmpint (states_arr->len, ==, 2);
47   int i = 0;
48   for (i = 0; i < states_arr->len; ++i) {
49     g_assert_cmpint (valid_states[i], ==, g_array_index (states_arr, AtspiStateType, i));
50     g_assert (atspi_state_set_contains (states, ATSPI_STATE_MODAL));
51     g_assert (atspi_state_set_contains (states, ATSPI_STATE_MULTI_LINE));
52   }
53 }
54
55 static void
56 atk_test_state_set_new (gpointer fixture, gconstpointer user_data)
57 {
58   GArray *states_arr = g_array_new (FALSE, FALSE, sizeof (AtspiStateType));
59
60   gint state = 0;
61   state = 11; // ATSPI_STATE_FOCUSABLE
62   g_array_append_val (states_arr, state);
63   state = 12; // ATSPI_STATE_FOCUSED
64   g_array_append_val (states_arr, state);
65
66   g_assert_cmpint (states_arr->len, ==, 2);
67
68   AtspiStateSet *ss = atspi_state_set_new (states_arr);
69
70   AtspiStateType valid_states[] = {
71     ATSPI_STATE_FOCUSABLE,
72     ATSPI_STATE_FOCUSED,
73   };
74
75   g_assert (atspi_state_set_contains (ss, valid_states[0]));
76   g_assert (atspi_state_set_contains (ss, valid_states[1]));
77   int i = 0;
78   for (i = 0; i < states_arr->len; ++i) {
79     g_assert_cmpint (valid_states[i], ==, g_array_index (states_arr, AtspiStateType, i));
80   }
81 }
82
83 static void
84 atk_test_state_set_set_by_name (gpointer fixture, gconstpointer user_data)
85 {
86   AtspiAccessible *obj = get_root_obj (DATA_FILE);
87   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
88   AtspiStateSet *states = atspi_accessible_get_state_set (child);
89   GArray *states_arr = atspi_state_set_get_states (states);
90
91   atspi_state_set_set_by_name (states, "modal", FALSE);
92
93   states_arr = atspi_state_set_get_states (states);
94
95   g_assert_cmpint (states_arr->len, ==, 1);
96   g_assert (!atspi_state_set_contains (states, ATSPI_STATE_MODAL));
97   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MULTI_LINE));
98
99   atspi_state_set_set_by_name (states, "modal", TRUE);
100   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MODAL));
101 }
102
103 static void
104 atk_test_state_set_add (gpointer fixture, gconstpointer user_data)
105 {
106   AtspiAccessible *obj = get_root_obj (DATA_FILE);
107   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
108   AtspiStateSet *states = atspi_accessible_get_state_set (child);
109
110   g_assert (!atspi_state_set_contains (states, ATSPI_STATE_FOCUSABLE));
111
112   atspi_state_set_add (states, ATSPI_STATE_FOCUSABLE);
113
114   g_assert (atspi_state_set_contains (states, ATSPI_STATE_FOCUSABLE));
115
116 }
117
118 static void
119 atk_test_state_set_compare (gpointer fixture, gconstpointer user_data)
120 {
121   AtspiAccessible *obj = get_root_obj (DATA_FILE);
122   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
123   AtspiStateSet *states = atspi_accessible_get_state_set (child);
124   GArray *states_arr = g_array_new (FALSE, FALSE, sizeof (AtspiStateType));
125
126   gint state = 0;
127   state = 11; // ATSPI_STATE_FOCUSABLE
128   g_array_append_val (states_arr, state);
129   state = 12; // ATSPI_STATE_FOCUSED
130   g_array_append_val (states_arr, state);
131
132   g_assert_cmpint (states_arr->len, ==, 2);
133
134   AtspiStateSet *ss = atspi_state_set_new (states_arr);
135
136   AtspiStateSet *ret = atspi_state_set_compare (states, ss);
137
138   g_assert (atspi_state_set_contains (ret, ATSPI_STATE_MODAL));
139   g_assert (atspi_state_set_contains (ret, ATSPI_STATE_MULTI_LINE));
140   g_assert (atspi_state_set_contains (ret, ATSPI_STATE_FOCUSED));
141   g_assert (atspi_state_set_contains (ret, ATSPI_STATE_FOCUSABLE));
142 }
143
144 static void
145 atk_test_state_set_contains (gpointer fixture, gconstpointer user_data)
146 {
147   AtspiAccessible *obj = get_root_obj (DATA_FILE);
148   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
149   AtspiStateSet *states = atspi_accessible_get_state_set (child);
150
151   g_assert (!atspi_state_set_contains (states, ATSPI_STATE_FOCUSABLE));
152   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MODAL));
153 }
154
155 static void
156 atk_test_state_set_equals (gpointer fixture, gconstpointer user_data)
157 {
158   AtspiAccessible *obj = get_root_obj (DATA_FILE);
159   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
160   AtspiStateSet *states = atspi_accessible_get_state_set (child);
161   GArray *states_arr = g_array_new (FALSE, FALSE, sizeof (AtspiStateType));
162
163   gint state = 0;
164   state = 16; // ATSPI_STATE_MODAL
165   g_array_append_val (states_arr, state);
166   state = 17; // ATSPI_STATE_MULTI_LINE
167   g_array_append_val (states_arr, state);
168
169   g_assert_cmpint (states_arr->len, ==, 2);
170
171   AtspiStateSet *ss = atspi_state_set_new (states_arr);
172
173   g_assert (atspi_state_set_equals (states, ss));
174 }
175
176 static void
177 atk_test_state_set_get_states (gpointer fixture, gconstpointer user_data)
178 {
179   AtspiAccessible *obj = get_root_obj (DATA_FILE);
180   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
181   AtspiStateSet *states = atspi_accessible_get_state_set (child);
182   GArray *states_arr = atspi_state_set_get_states (states);
183
184   AtspiStateType valid_states[] = {
185     ATSPI_STATE_MODAL,
186     ATSPI_STATE_MULTI_LINE,
187   };
188   g_assert_cmpint (states_arr->len, ==, 2);
189   int i = 0;
190   for (i = 0; i < states_arr->len; ++i)
191     g_assert_cmpint (valid_states[i], ==, g_array_index (states_arr, AtspiStateType, i));
192   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MODAL));
193   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MULTI_LINE));
194 }
195
196 static void
197 atk_test_state_set_is_empty (gpointer fixture, gconstpointer user_data)
198 {
199   AtspiAccessible *obj = get_root_obj (DATA_FILE);
200   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
201   AtspiStateSet *states = atspi_accessible_get_state_set (child);
202   AtspiStateSet *root_states = atspi_accessible_get_state_set (obj);
203
204   g_assert (!atspi_state_set_is_empty (states));
205   g_assert (atspi_state_set_is_empty (root_states));
206 }
207
208 static void
209 atk_test_state_set_remove (gpointer fixture, gconstpointer user_data)
210 {
211   AtspiAccessible *obj = get_root_obj (DATA_FILE);
212   AtspiAccessible *child = atspi_accessible_get_child_at_index (obj, 0, NULL);
213   AtspiStateSet *states = atspi_accessible_get_state_set (child);
214   GArray *states_arr = atspi_state_set_get_states (states);
215
216   g_assert_cmpint (states_arr->len, ==, 2);
217   atspi_state_set_remove (states, ATSPI_STATE_MODAL);
218
219   states_arr = atspi_state_set_get_states (states);
220
221   g_assert_cmpint (states_arr->len, ==, 1);
222   g_assert (!atspi_state_set_contains (states, ATSPI_STATE_MODAL));
223   g_assert (atspi_state_set_contains (states, ATSPI_STATE_MULTI_LINE));
224 }
225
226 void
227 atk_test_state_set (void)
228 {
229   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_accessible_get_state_set",
230                      0, NULL, NULL, atk_test_accessible_get_state_set, teardown_state_set_test);
231   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_new",
232                      0, NULL, NULL, atk_test_state_set_new, teardown_state_set_test);
233   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_set_by_name",
234                      0, NULL, NULL, atk_test_state_set_set_by_name, teardown_state_set_test);
235   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_add",
236                      0, NULL, NULL, atk_test_state_set_add, teardown_state_set_test);
237   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_compare",
238                      0, NULL, NULL, atk_test_state_set_compare, teardown_state_set_test);
239   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_contains",
240                      0, NULL, NULL, atk_test_state_set_contains, teardown_state_set_test);
241   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_equals",
242                      0, NULL, NULL, atk_test_state_set_equals, teardown_state_set_test);
243   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_get_states",
244                      0, NULL, NULL, atk_test_state_set_get_states, teardown_state_set_test);
245   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_is_empty",
246                      0, NULL, NULL, atk_test_state_set_is_empty, teardown_state_set_test);
247   g_test_add_vtable (ATK_TEST_PATH_STATE_SET "/atk_test_state_set_remove",
248                      0, NULL, NULL, atk_test_state_set_remove, teardown_state_set_test);
249 }
250