Release 2.1.94
[hildon] / tests / check-hildon-caption.c
1 /*
2  * This file is a part of hildon tests
3  *
4  * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <check.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkentry.h>
30 #include <gtk/gtkhbox.h>
31 #include <gtk/gtkwindow.h>
32 #include "test_suites.h"
33 #include "check_utils.h"
34 #include <hildon/hildon-caption.h>
35
36 #include <gtk/gtkvbox.h>
37 #include <hildon/hildon-window.h>
38
39 #include <libintl.h>
40
41 #define _(String) dgettext("hildon-libs", String)
42
43 /* -------------------- Fixtures -------------------- */
44
45 static HildonCaption *caption = NULL;
46 static const gchar *DEFAULT_TEST_CAPTION = "Default caption";
47 static GtkWidget *showed_window = NULL;
48
49 static void 
50 fx_setup_default_caption()
51 {
52   int argc = 0;
53   GtkWidget *control = NULL;
54   GtkWidget *icon = NULL;
55   GtkSizeGroup *group = NULL;
56
57   gtk_init(&argc, NULL);
58
59   control = gtk_entry_new();
60   caption = HILDON_CAPTION(hildon_caption_new(group, 
61                                               DEFAULT_TEST_CAPTION, 
62                                               control, 
63                                               icon,
64                                               HILDON_CAPTION_OPTIONAL));
65
66   showed_window =  create_test_window ();
67  
68   /* This packs the widget into the window (a gtk container). */
69   gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (caption));
70   
71   /* Displays the widget and the window */
72   show_test_window (showed_window);
73   
74   show_test_window (GTK_WIDGET(caption));
75
76   /* Check that the hildon-caption object has been created properly */
77   fail_if (!HILDON_IS_CAPTION (caption), "hildon-caption: Creation failed.");
78    
79 }
80
81 static void 
82 fx_teardown_default_caption()
83 {
84   /* Destroy caption */
85   gtk_widget_destroy (GTK_WIDGET(caption));
86
87   /* Destroy the window */
88   gtk_widget_destroy (showed_window);
89
90 }
91
92
93 /* -------------------- Test cases -------------------- */
94
95 /* ----- Test case for is_mandatory -----*/
96
97 /**
98  * Purpose: test getting the mandatory property of a HildonCaption
99  * Cases considered:
100  *    - check an optional captioned control
101  *    - check a mandatory captioned control
102  */
103
104 START_TEST (test_is_mandatory_regular)
105 {
106   GtkWidget *control = NULL, *icon = NULL;
107   GtkSizeGroup *group = NULL;
108
109   /* Test 1: check an optional (i.e. NOT mandatory) caption */
110   fail_if (hildon_caption_is_mandatory (caption) == TRUE, 
111            "hildon-caption: The created HildonCaption has a mandatory captioned control and it shouldn't");
112  
113   gtk_widget_destroy (GTK_WIDGET (caption));
114
115   /* Test 2: create a mandatory caption */
116   control = GTK_WIDGET (gtk_entry_new());
117   caption = HILDON_CAPTION (hildon_caption_new(group, 
118                                                "Default caption", 
119                                                control, 
120                                                icon,
121                                                HILDON_CAPTION_MANDATORY));
122   
123   fail_if (hildon_caption_is_mandatory (caption) == FALSE, 
124            "hildon-caption: The created HildonCaption has a mandatory captioned control and it shouldn't");
125 }
126 END_TEST
127
128 /**
129  * Purpose: test is_mandatory with invalid arguments
130  * Cases considered:
131  *    - use a NULL HildonCaption
132  *    - use an object that is not a HildonCaption (a HBox)
133  */
134 START_TEST (test_is_mandatory_invalid)
135 {
136   GtkWidget *hbox = NULL;
137
138   /* Test 1: NULL HildonCaption */
139   fail_if (hildon_caption_is_mandatory (NULL) == TRUE, 
140            "hildon-caption: a NULL HildonCaption should fail and return FALSE");
141
142   /* Test 2: invalid HildonCaption */
143   hbox = gtk_hbox_new (FALSE, 0);
144   if (hildon_caption_is_mandatory ((HildonCaption *) (hbox)) == TRUE) 
145     {
146       gtk_widget_destroy (GTK_WIDGET (hbox));
147       fail ("hildon-caption: an invalid HildonCaption should return FALSE");
148     }
149
150   gtk_widget_destroy (GTK_WIDGET (hbox));
151 }
152 END_TEST
153
154 /* ----- Test case for set_status -----*/
155         
156 /**
157  * Purpose: test setting the status of a HildonCaption
158  * Cases considered:
159  *    - Set the status HILDON_CAPTION_MANDATORY
160  */
161 START_TEST (test_set_status_regular)
162 {
163
164   hildon_caption_set_status (caption, HILDON_CAPTION_MANDATORY);
165
166   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_MANDATORY,
167            "hildon-caption: the returned status is %d and should be %d",
168            hildon_caption_get_status (caption), HILDON_CAPTION_MANDATORY);
169 }
170 END_TEST
171
172 /**
173  * Purpose: test setting some status passing invalid parameters
174  * Cases considered:
175  *    - set the status 8
176  *    - set the status to a NULL HildonCaption
177  *    - set the status to an invalid HildonCaption (use a HBox instead)
178  */
179 START_TEST (test_set_status_invalid)
180 {
181   const guint INVALID_ENUM = 8;
182   GtkWidget *hbox = NULL;
183
184   /* Test 1 */
185   hildon_caption_set_status (caption, INVALID_ENUM);
186
187   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_OPTIONAL,
188            "hildon-caption: the returned status is %d and should be %d",
189            hildon_caption_get_status (caption), HILDON_CAPTION_OPTIONAL);
190
191   /* Test 2 */
192   hildon_caption_set_status (NULL, HILDON_CAPTION_MANDATORY);
193
194   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_OPTIONAL,
195            "hildon-caption: the returned status is %d and should be %d",
196            hildon_caption_get_status (caption), HILDON_CAPTION_OPTIONAL);
197
198   /* Test 3 */
199   hbox = gtk_hbox_new (FALSE, 0);
200   hildon_caption_set_status ((HildonCaption *) (hbox), HILDON_CAPTION_MANDATORY);
201   if (hildon_caption_get_status (caption) != HILDON_CAPTION_OPTIONAL) 
202     {
203       gtk_widget_destroy (GTK_WIDGET (hbox));
204       fail ("hildon-caption: the returned status is %d and should be %d",
205             hildon_caption_get_status (caption), HILDON_CAPTION_OPTIONAL);
206     }
207
208   gtk_widget_destroy (GTK_WIDGET (hbox));
209 }
210 END_TEST
211
212 /* ----- Test case for get_status -----*/
213
214 /**
215  * Purpose: test getting the status of a HildonCaption
216  * Cases considered:
217  *    - Get HILDON_CAPTION_OPTIONAL set in the method creation
218  *    - Get HILDON_CAPTION_MANDATORY set with set_status
219  *    - Get HILDON_CAPTION_OPTIONAL set with set_property
220  */
221 START_TEST (test_get_status_regular)
222 {
223   GValue value = {0, };
224
225   /* Test 1: set status in object creation */
226   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_OPTIONAL,
227            "hildon-caption: the returned status is %d and should be %d",
228            hildon_caption_get_status (caption), HILDON_CAPTION_OPTIONAL);
229   
230   /* Test 2: set status with set_status */
231   hildon_caption_set_status (caption, HILDON_CAPTION_MANDATORY);
232   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_MANDATORY,
233            "hildon-caption: the returned status is %d and should be %d",
234            hildon_caption_get_status (caption), HILDON_CAPTION_MANDATORY);
235   
236   /* Test 3: set status with set_property */
237   g_value_init (&value, G_TYPE_UINT);
238   g_value_set_uint (&value, HILDON_CAPTION_OPTIONAL);
239
240   g_object_set_property (G_OBJECT (caption), "status", &value);
241   fail_if (hildon_caption_get_status (caption) != HILDON_CAPTION_OPTIONAL,
242            "hildon-caption: the returned status is %d and should be %d",
243            hildon_caption_get_status (caption), HILDON_CAPTION_OPTIONAL);
244 }
245 END_TEST
246
247 /**
248  * Purpose: test get_status with invalid values
249  * Cases considered:
250  *    - Get the status with a NULL HildonCaption
251  *    - Get the status with an invalid HildonCaption (used a HBox)
252  */
253 START_TEST (test_get_status_invalid)
254 {
255   GtkWidget *hbox = NULL;
256
257   /* Test 1: check a NULL caption */
258   fail_if (hildon_caption_get_status (NULL) != HILDON_CAPTION_OPTIONAL,
259            "hildon-caption: the returned status is %d and should be %d",
260            hildon_caption_get_status (NULL), HILDON_CAPTION_OPTIONAL);
261
262   /* Test 2: check an invalid HildonCaption */
263   hbox = gtk_hbox_new (FALSE, 0);
264   if (hildon_caption_get_status ((HildonCaption *) (hbox)) != HILDON_CAPTION_OPTIONAL) 
265     {
266       gtk_widget_destroy (GTK_WIDGET (hbox));
267       fail ("hildon-caption: an invalid HildonCaption should return HILDON_CAPTION_OPTIONAL");
268     }
269
270   gtk_widget_destroy (GTK_WIDGET (hbox));
271 }
272 END_TEST
273
274 /* ----- Test case for set_label -----*/
275
276 /**
277  * Purpose: test setting labels for a HildonCaption
278  * Cases considered:
279  *    - Set a test label
280  *    - Set an empty label
281  */
282 START_TEST (test_set_label_regular)
283 {
284   const gchar *TEST_LABEL = TEST_STRING;
285   gchar * expected_ret_label=NULL;
286   
287   /* We control i18n so we will never set it properly because apparently it will not be useful for testing */
288   /* so _("ecdg_ti_caption_separato") should return the same result that "ecdg_ti_caption_separator" */
289   /* If in the future we decide activate internationalization we must modify test implementation */
290   expected_ret_label = g_strconcat(TEST_LABEL,_("ecdg_ti_caption_separator"),NULL);
291
292   /* Test 1 */
293   hildon_caption_set_label (caption, TEST_LABEL);
294
295   /*  fail_if (strcmp (hildon_caption_get_label (caption), TEST_LABEL) != 0,
296       "hildon-caption: the returned label is %s and should be %s",
297       hildon_caption_get_label (caption), TEST_LABEL);
298   */
299   fail_if (strcmp (hildon_caption_get_label (caption), expected_ret_label) != 0,
300            "hildon-caption: the returned label is %s and should be %s",
301            hildon_caption_get_label (caption), expected_ret_label);
302   
303   g_free(expected_ret_label);
304
305   /* Test 2 */
306   hildon_caption_set_label (caption, "");
307   
308   fail_if (strcmp (hildon_caption_get_label (caption),_("ecdg_ti_caption_separator")) != 0,
309            "hildon-caption: the returned label is %s and should be default separator",
310            hildon_caption_get_label (caption));
311 }
312 END_TEST
313
314 /**
315  * Purpose: test setting labels with invalid parameters
316  * Cases considered:
317  *    - Set a NULL label
318  *    - set a label to a NULL HildonCaption
319  *    - set a label to an invalid HildonCaption (use a GtkHbox instead)
320  */
321 START_TEST (test_set_label_invalid)
322 {
323   const gchar *TEST_LABEL = TEST_STRING;
324   GtkWidget *hbox = NULL;
325
326   /* Test 1 */
327   hildon_caption_set_label (caption, NULL);
328
329   fail_if (strcmp (hildon_caption_get_label (caption), "") != 0,
330            "hildon-caption: the returned label is %s and should be empty",
331            hildon_caption_get_label (caption));
332
333   /* Test 2 */
334   hildon_caption_set_label (NULL, TEST_LABEL);
335
336   fail_if (strcmp (hildon_caption_get_label (caption), "") != 0,
337            "hildon-caption: the returned label is %s and should be default separator",
338            hildon_caption_get_label (caption), "");
339
340   /* Test 3 */
341   hbox = gtk_hbox_new (FALSE, 0);
342   hildon_caption_set_label ((HildonCaption *) (hbox), TEST_LABEL);
343   if (strcmp (hildon_caption_get_label ((HildonCaption *) (hbox)), "")!=0) 
344     {
345       gtk_widget_destroy (GTK_WIDGET (hbox));
346       fail ("hildon-caption: an invalid HildonCaption should return NULL");
347     }
348
349   gtk_widget_destroy (GTK_WIDGET (hbox));
350 }
351 END_TEST
352
353 /* ----- Test case for get_label -----*/
354
355 /**
356  * Purpose: test getting a valid value for a label previously set
357  * Cases considered:
358  *    - get label set with set_label
359  *    - get empty label set with set_label
360  */
361 START_TEST (test_get_label_regular)
362 {
363   const gchar *TEST_LABEL = TEST_STRING;
364   gchar * expected_ret_label=NULL;
365
366   /* Test 1 */
367   hildon_caption_set_label (caption, TEST_LABEL);
368   expected_ret_label = g_strconcat(TEST_LABEL, _("ecdg_ti_caption_separator"),NULL);
369
370   fail_if (strcmp (hildon_caption_get_label (caption), expected_ret_label) != 0,
371            "hildon-caption: the returned label is %s and should be %s",
372            hildon_caption_get_label (caption), expected_ret_label);
373
374   g_free(expected_ret_label);
375
376   /* Test 2 */
377   hildon_caption_set_label (caption, "");
378
379   fail_if (strcmp (hildon_caption_get_label (caption), _("ecdg_ti_caption_separator")) != 0,
380            "hildon-caption: the returned label is %s and should be default separator",
381            hildon_caption_get_label (caption));
382 }
383 END_TEST
384
385 /**
386  * Purpose: test getting labels with invalid arguments
387  * Cases considered:
388  *    - get NULL label set with set_property
389  *    - get the default label with a NULL HildonCaption
390  *    - get the default label with an invalid HildonCaption (used a GtkHBox instead)
391  */
392 START_TEST (test_get_label_invalid)
393 {
394   GValue value = {0, };
395   GtkWidget *hbox = NULL;
396   const gchar *EMPTY_STRING = "";
397
398   /* Test 1 */
399   g_value_init (&value, G_TYPE_STRING);
400   g_value_set_string (&value, NULL);
401   g_object_set_property (G_OBJECT (caption), "label", &value);
402
403   fail_if (strcmp(hildon_caption_get_label (caption), EMPTY_STRING) != 0,
404            "hildon-caption: the returned label is %s and should be empty",
405            hildon_caption_get_label (caption));
406
407   /* Test 2: check a NULL caption */
408   fail_if (strcmp (hildon_caption_get_label (NULL), EMPTY_STRING) != 0,
409            "hildon-caption: the returned label is %s and should be empty",
410            hildon_caption_get_label (NULL));
411
412   /* Test 3: check an invalid HildonCaption */
413   hbox = gtk_hbox_new (FALSE, 0);
414   if (strcmp (hildon_caption_get_label ((HildonCaption *) (hbox)), EMPTY_STRING)) 
415     {
416       gtk_widget_destroy (GTK_WIDGET (hbox));
417       fail ("hildon-caption: an invalid HildonCaption should return %s", EMPTY_STRING);
418     }
419
420   gtk_widget_destroy (GTK_WIDGET (hbox));
421 }
422 END_TEST
423
424 /* ---------- Suite creation ---------- */
425
426 Suite *create_hildon_caption_suite()
427 {
428   /* Create the suite */
429   Suite *s = suite_create("HildonCaption");
430
431   /* Create test cases */
432   TCase *tc1 = tcase_create("is_mandatory");
433   TCase *tc2 = tcase_create("set_status");
434   TCase *tc3 = tcase_create("get_status");
435   TCase *tc4 = tcase_create("set_label");
436   TCase *tc5 = tcase_create("get_label");
437
438   /* Create test case for is_mandatory and add it to the suite */
439   tcase_add_checked_fixture(tc1, fx_setup_default_caption, fx_teardown_default_caption);
440   tcase_add_test(tc1, test_is_mandatory_regular);
441   tcase_add_test(tc1, test_is_mandatory_invalid);
442   suite_add_tcase (s, tc1);
443
444   /* Create test case for set_status and add it to the suite */
445   tcase_add_checked_fixture(tc2, fx_setup_default_caption, fx_teardown_default_caption);
446   tcase_add_test(tc2, test_set_status_regular);
447   tcase_add_test(tc2, test_set_status_invalid);
448   suite_add_tcase (s, tc2);
449
450   /* Create test case for get_status and add it to the suite */
451   tcase_add_checked_fixture(tc3, fx_setup_default_caption, fx_teardown_default_caption);
452   tcase_add_test(tc3, test_get_status_regular);
453   tcase_add_test(tc3, test_get_status_invalid);
454   suite_add_tcase (s, tc3);
455
456   /* Create test case for set_label and add it to the suite */
457   tcase_add_checked_fixture(tc4, fx_setup_default_caption, fx_teardown_default_caption);
458   tcase_add_test(tc4, test_set_label_regular);
459   tcase_add_test(tc4, test_set_label_invalid);
460   suite_add_tcase (s, tc4);
461
462   /* Create test case for get_label and add it to the suite */
463   tcase_add_checked_fixture(tc5, fx_setup_default_caption, fx_teardown_default_caption);
464   tcase_add_test(tc5, test_get_label_regular);
465   tcase_add_test(tc5, test_get_label_invalid);
466   suite_add_tcase (s, tc5);
467
468   /* Return created suite */
469   return s;             
470 }