Add reference to atk/atkrelationtype.h
[platform/upstream/atk.git] / tests / testrelation.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <atk/atk.h>
21
22 #include <string.h>
23
24 static gboolean  test_relation (void);
25 static gboolean  test_role (void);
26
27 static gboolean
28 test_relation (void)
29 {
30   AtkRelationType type1, type2;
31   G_CONST_RETURN gchar *name;
32   AtkObject *obj;
33   gboolean ret_value;
34   AtkRelationSet *set;
35   AtkRelation *relation;
36   gint n_relations;
37   GPtrArray *array; 
38
39   name = atk_relation_type_get_name (ATK_RELATION_LABEL_FOR);
40   g_return_val_if_fail (name, FALSE);
41   if (strcmp (name, "label-for") != 0)
42     {
43       g_print ("Unexpected name for ATK_RELATION_LABEL_FOR %s\n", name);
44       return FALSE;
45     }
46
47   name = atk_relation_type_get_name (ATK_RELATION_NODE_CHILD_OF);
48   g_return_val_if_fail (name, FALSE);
49   if (strcmp (name, "node-child-of") != 0)
50     {
51       g_print ("Unexpected name for ATK_RELATION_NODE_CHILD_OF %s\n", name);
52       return FALSE;
53     }
54
55   type1 = atk_relation_type_for_name ("controlled-by");
56   if (type1 != ATK_RELATION_CONTROLLED_BY)
57     {
58       g_print ("Unexpected type for focused\n");
59       return FALSE;
60     }
61
62   type1 = atk_relation_type_register ("test-state");
63   name = atk_relation_type_get_name (type1);
64   g_return_val_if_fail (name, FALSE);
65   if (strcmp (name, "test-state") != 0)
66     {
67       g_print ("Unexpected name for test-state %s\n", name);
68       return FALSE;
69     }
70   type2 = atk_relation_type_for_name ("test-state");
71   if (type1 != type2)
72   {
73     g_print ("Unexpected type for test-state\n");
74     return FALSE;
75   }
76   type2 = atk_relation_type_for_name ("TEST_STATE");
77   if (type2 != 0)
78     {
79       g_print ("Unexpected type for TEST_STATE\n");
80       return FALSE;
81     }
82   /*
83    * Check that a non-existent type returns NULL
84    */
85   name = atk_relation_type_get_name (ATK_RELATION_LAST_DEFINED + 2);
86   if (name)
87     {
88       g_print ("Unexpected name for undefined type %s\n", name);
89       return FALSE;
90     }
91
92   obj = g_object_new (ATK_TYPE_OBJECT, NULL);
93   ret_value = atk_object_add_relationship (obj, ATK_RELATION_LABEL_FOR, obj);
94   if (!ret_value)
95     {
96       g_print ("Unexpected return value for atk_object_add_relationship\n");
97       return FALSE;
98     }
99   set = atk_object_ref_relation_set (obj);
100   if (!set)
101     {
102       g_print ("Unexpected return value for atk_object_ref_relation_set\n");
103       return FALSE;
104     }
105   n_relations = atk_relation_set_get_n_relations (set);
106   if (n_relations != 1)
107     {
108       g_print ("Unexpected return value (%d) for atk_relation_set_get_n_relations expected value: %d\n", n_relations, 1);
109       return FALSE;
110     }
111   relation = atk_relation_set_get_relation (set, 0);  
112   if (!relation)
113     {
114       g_print ("Unexpected return value for atk_object_relation_set_get_relation\n");
115       return FALSE;
116     }
117   type1 = atk_relation_get_relation_type (relation);
118   if (type1 != ATK_RELATION_LABEL_FOR)
119     {
120       g_print ("Unexpected return value for atk_relation_get_relation_type\n");
121       return FALSE;
122     }
123   array = atk_relation_get_target (relation);
124   if (obj != g_ptr_array_index (array, 0))
125     {
126       g_print ("Unexpected return value for atk_relation_get_target\n");
127       return FALSE;
128     }
129   g_object_unref (set);
130   ret_value = atk_object_remove_relationship (obj, ATK_RELATION_LABEL_FOR, obj);
131   if (!ret_value)
132     {
133       g_print ("Unexpected return value for atk_object_remove_relationship\n");
134       return FALSE;
135     }
136   set = atk_object_ref_relation_set (obj);
137   if (!set)
138     {
139       g_print ("Unexpected return value for atk_object_ref_relation_set\n");
140       return FALSE;
141     }
142   n_relations = atk_relation_set_get_n_relations (set);
143   if (n_relations != 0)
144     {
145       g_print ("Unexpected return value (%d) for atk_relation_set_get_n_relations expected value: %d\n", n_relations, 0);
146       return FALSE;
147     }
148   g_object_unref (set);
149   g_object_unref (obj);
150   return TRUE;
151 }
152
153 static gboolean
154 test_role (void)
155 {
156   AtkRole role1, role2;
157   G_CONST_RETURN gchar *name;
158
159   name = atk_role_get_name (ATK_ROLE_PAGE_TAB);
160   g_return_val_if_fail (name, FALSE);
161   if (strcmp (name, "page-tab") != 0)
162     {
163       g_print ("Unexpected name for ATK_ROLE_PAGE_TAB %s\n", name);
164       return FALSE;
165     }
166
167   name = atk_role_get_name (ATK_ROLE_LAYERED_PANE);
168   g_return_val_if_fail (name, FALSE);
169   if (strcmp (name, "layered-pane") != 0)
170     {
171       g_print ("Unexpected name for ATK_ROLE_LAYERED_PANE %s\n", name);
172       return FALSE;
173     }
174
175   role1 = atk_role_for_name ("list-item");
176   if (role1 != ATK_ROLE_LIST_ITEM)
177     {
178       g_print ("Unexpected role for list-item\n");
179       return FALSE;
180     }
181
182   role1 = atk_role_register ("test-role");
183   name = atk_role_get_name (role1);
184   g_return_val_if_fail (name, FALSE);
185   if (strcmp (name, "test-role") != 0)
186     {
187       g_print ("Unexpected name for test-role %s\n", name);
188       return FALSE;
189     }
190   role2 = atk_role_for_name ("test-role");
191   if (role1 != role2)
192   {
193     g_print ("Unexpected role for test-role\n");
194     return FALSE;
195   }
196   role2 = atk_role_for_name ("TEST_ROLE");
197   if (role2 != 0)
198     {
199       g_print ("Unexpected role for TEST_ROLE\n");
200       return FALSE;
201     }
202   /*
203    * Check that a non-existent role returns NULL
204    */
205   name = atk_role_get_name (ATK_ROLE_LAST_DEFINED + 2);
206   if (name)
207     {
208       g_print ("Unexpected name for undefined role %s\n", name);
209       return FALSE;
210     }
211   return TRUE;
212 }
213
214 static gboolean
215 test_text_attr (void)
216 {
217   AtkTextAttribute attr1, attr2;
218   G_CONST_RETURN gchar *name;
219
220   name = atk_text_attribute_get_name (ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP);
221   g_return_val_if_fail (name, FALSE);
222   if (strcmp (name, "pixels-inside-wrap") != 0)
223     {
224       g_print ("Unexpected name for ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP %s\n", name);
225       return FALSE;
226     }
227
228   name = atk_text_attribute_get_name (ATK_TEXT_ATTR_BG_STIPPLE);
229   g_return_val_if_fail (name, FALSE);
230   if (strcmp (name, "bg-stipple") != 0)
231     {
232       g_print ("Unexpected name for ATK_TEXT_ATTR_BG_STIPPLE %s\n", name);
233       return FALSE;
234     }
235
236   attr1 = atk_text_attribute_for_name ("left-margin");
237   if (attr1 != ATK_TEXT_ATTR_LEFT_MARGIN)
238     {
239       g_print ("Unexpected attribute for left-margin\n");
240       return FALSE;
241     }
242
243   attr1 = atk_text_attribute_register ("test-attribute");
244   name = atk_text_attribute_get_name (attr1);
245   g_return_val_if_fail (name, FALSE);
246   if (strcmp (name, "test-attribute") != 0)
247     {
248       g_print ("Unexpected name for test-attribute %s\n", name);
249       return FALSE;
250     }
251   attr2 = atk_text_attribute_for_name ("test-attribute");
252   if (attr1 != attr2)
253   {
254     g_print ("Unexpected attribute for test-attribute\n");
255     return FALSE;
256   }
257   attr2 = atk_text_attribute_for_name ("TEST_ATTR");
258   if (attr2 != 0)
259     {
260       g_print ("Unexpected attribute for TEST_ATTR\n");
261       return FALSE;
262     }
263   /*
264    * Check that a non-existent attribute returns NULL
265    */
266   name = atk_text_attribute_get_name (ATK_TEXT_ATTR_LAST_DEFINED + 2);
267   if (name)
268     {
269       g_print ("Unexpected name for undefined attribute %s\n", name);
270       return FALSE;
271     }
272   return TRUE;
273 }
274
275 int
276 gtk_module_init (gint  argc, 
277                  char* argv[])
278 {
279   gboolean b_ret;
280
281   g_print("Relation test module loaded\n");
282
283   b_ret = test_relation ();
284   if (b_ret)
285     g_print ("Relation tests succeeded\n");
286   else
287     g_print ("Relation tests failed\n");
288   b_ret = test_role ();
289   if (b_ret)
290     g_print ("Role tests succeeded\n");
291   else
292     g_print ("Role tests failed\n");
293   b_ret = test_text_attr ();
294   if (b_ret)
295     g_print ("Text Attribute tests succeeded\n");
296   else
297     g_print ("Text Attribute tests failed\n");
298   return 0;
299 }