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