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