2 * Copyright (C) 2006 Nokia Corporation.
4 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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.
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.
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
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"
34 #include <gtk/gtkvbox.h>
35 #include "hildon-window.h"
39 /* -------------------- Fixtures -------------------- */
41 static HildonCaption *caption = NULL;
42 static const gchar *DEFAULT_TEST_CAPTION = "Default caption";
43 static GtkWidget *showed_window = NULL;
46 fx_setup_default_caption()
49 GtkWidget *control = NULL;
50 GtkWidget *icon = NULL;
51 GtkSizeGroup *group = NULL;
53 gtk_init(&argc, NULL);
55 control = gtk_entry_new();
56 caption = HILDON_CAPTION(hildon_caption_new(group,
60 HILDON_CAPTION_OPTIONAL));
62 showed_window = create_test_window ();
64 /* This packs the widget into the window (a gtk container). */
65 gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (caption));
67 /* Displays the widget and the window */
68 show_test_window (showed_window);
70 show_test_window (GTK_WIDGET(caption));
72 /* Check that the hildon-caption object has been created properly */
73 fail_if (!HILDON_IS_CAPTION (caption), "hildon-caption: Creation failed.");
78 fx_teardown_default_caption()
81 gtk_widget_destroy (GTK_WIDGET(caption));
83 /* Destroy the window */
84 gtk_widget_destroy (showed_window);
89 /* -------------------- Test cases -------------------- */
91 /* ----- Test case for is_mandatory -----*/
94 * Purpose: test getting the mandatory property of a HildonCaption
96 * - check an optional captioned control
97 * - check a mandatory captioned control
100 START_TEST (test_is_mandatory_regular)
102 GtkWidget *control = NULL, *icon = NULL;
103 GtkSizeGroup *group = NULL;
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");
109 gtk_widget_destroy (GTK_WIDGET (caption));
111 /* Test 2: create a mandatory caption */
112 control = GTK_WIDGET (gtk_entry_new());
113 caption = HILDON_CAPTION (hildon_caption_new(group,
117 HILDON_CAPTION_MANDATORY));
119 fail_if (hildon_caption_is_mandatory (caption) == FALSE,
120 "hildon-caption: The created HildonCaption has a mandatory captioned control and it shouldn't");
125 * Purpose: test is_mandatory with invalid arguments
127 * - use a NULL HildonCaption
128 * - use an object that is not a HildonCaption (a HBox)
130 START_TEST (test_is_mandatory_invalid)
132 GtkWidget *hbox = NULL;
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");
138 /* Test 2: invalid HildonCaption */
139 hbox = gtk_hbox_new (FALSE, 0);
140 if (hildon_caption_is_mandatory ((HildonCaption *) (hbox)) == TRUE)
142 gtk_widget_destroy (GTK_WIDGET (hbox));
143 fail ("hildon-caption: an invalid HildonCaption should return FALSE");
146 gtk_widget_destroy (GTK_WIDGET (hbox));
150 /* ----- Test case for set_status -----*/
153 * Purpose: test setting the status of a HildonCaption
155 * - Set the status HILDON_CAPTION_MANDATORY
157 START_TEST (test_set_status_regular)
160 hildon_caption_set_status (caption, HILDON_CAPTION_MANDATORY);
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);
169 * Purpose: test setting some status passing invalid parameters
172 * - set the status to a NULL HildonCaption
173 * - set the status to an invalid HildonCaption (use a HBox instead)
175 START_TEST (test_set_status_invalid)
177 const guint INVALID_ENUM = 8;
178 GtkWidget *hbox = NULL;
181 hildon_caption_set_status (caption, INVALID_ENUM);
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);
188 hildon_caption_set_status (NULL, HILDON_CAPTION_MANDATORY);
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);
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)
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);
204 gtk_widget_destroy (GTK_WIDGET (hbox));
208 /* ----- Test case for get_status -----*/
211 * Purpose: test getting the status of a HildonCaption
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
217 START_TEST (test_get_status_regular)
219 GValue value = {0, };
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);
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);
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);
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);
244 * Purpose: test get_status with invalid values
246 * - Get the status with a NULL HildonCaption
247 * - Get the status with an invalid HildonCaption (used a HBox)
249 START_TEST (test_get_status_invalid)
251 GtkWidget *hbox = NULL;
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);
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)
262 gtk_widget_destroy (GTK_WIDGET (hbox));
263 fail ("hildon-caption: an invalid HildonCaption should return HILDON_CAPTION_OPTIONAL");
266 gtk_widget_destroy (GTK_WIDGET (hbox));
270 /* ----- Test case for set_label -----*/
273 * Purpose: test setting labels for a HildonCaption
276 * - Set an empty label
278 START_TEST (test_set_label_regular)
280 const gchar *TEST_LABEL = TEST_STRING;
281 gchar * expected_ret_label=NULL;
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);
289 hildon_caption_set_label (caption, TEST_LABEL);
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);
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);
299 g_free(expected_ret_label);
302 hildon_caption_set_label (caption, "");
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));
311 * Purpose: test setting labels with invalid parameters
314 * - set a label to a NULL HildonCaption
315 * - set a label to an invalid HildonCaption (use a GtkHbox instead)
317 START_TEST (test_set_label_invalid)
319 const gchar *TEST_LABEL = TEST_STRING;
320 GtkWidget *hbox = NULL;
323 hildon_caption_set_label (caption, NULL);
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));
330 hildon_caption_set_label (NULL, TEST_LABEL);
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), "");
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)
341 gtk_widget_destroy (GTK_WIDGET (hbox));
342 fail ("hildon-caption: an invalid HildonCaption should return NULL");
345 gtk_widget_destroy (GTK_WIDGET (hbox));
349 /* ----- Test case for get_label -----*/
352 * Purpose: test getting a valid value for a label previously set
354 * - get label set with set_label
355 * - get empty label set with set_label
357 START_TEST (test_get_label_regular)
359 const gchar *TEST_LABEL = TEST_STRING;
360 gchar * expected_ret_label=NULL;
363 hildon_caption_set_label (caption, TEST_LABEL);
364 expected_ret_label = g_strconcat(TEST_LABEL,"ecdg_ti_caption_separator",NULL);
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);
370 g_free(expected_ret_label);
373 hildon_caption_set_label (caption, "");
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));
382 * Purpose: test getting labels with invalid arguments
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)
388 START_TEST (test_get_label_invalid)
390 GValue value = {0, };
391 GtkWidget *hbox = NULL;
392 const gchar *EMPTY_STRING = "";
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);
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));
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));
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))
412 gtk_widget_destroy (GTK_WIDGET (hbox));
413 fail ("hildon-caption: an invalid HildonCaption should return %s", EMPTY_STRING);
416 gtk_widget_destroy (GTK_WIDGET (hbox));
420 /* ---------- Suite creation ---------- */
422 Suite *create_hildon_caption_suite()
424 /* Create the suite */
425 Suite *s = suite_create("HildonCaption");
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");
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);
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);
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);
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);
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);
464 /* Return created suite */