2 * This file is a part of hildon tests
4 * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
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.
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.
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
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"
36 #include <gtk/gtkvbox.h>
37 #include "hildon-window.h"
41 #define _(String) dgettext("hildon-libs", String)
43 /* -------------------- Fixtures -------------------- */
45 static HildonCaption *caption = NULL;
46 static const gchar *DEFAULT_TEST_CAPTION = "Default caption";
47 static GtkWidget *showed_window = NULL;
50 fx_setup_default_caption()
53 GtkWidget *control = NULL;
54 GtkWidget *icon = NULL;
55 GtkSizeGroup *group = NULL;
57 gtk_init(&argc, NULL);
59 control = gtk_entry_new();
60 caption = HILDON_CAPTION(hildon_caption_new(group,
64 HILDON_CAPTION_OPTIONAL));
66 showed_window = create_test_window ();
68 /* This packs the widget into the window (a gtk container). */
69 gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (caption));
71 /* Displays the widget and the window */
72 show_test_window (showed_window);
74 show_test_window (GTK_WIDGET(caption));
76 /* Check that the hildon-caption object has been created properly */
77 fail_if (!HILDON_IS_CAPTION (caption), "hildon-caption: Creation failed.");
82 fx_teardown_default_caption()
85 gtk_widget_destroy (GTK_WIDGET(caption));
87 /* Destroy the window */
88 gtk_widget_destroy (showed_window);
93 /* -------------------- Test cases -------------------- */
95 /* ----- Test case for is_mandatory -----*/
98 * Purpose: test getting the mandatory property of a HildonCaption
100 * - check an optional captioned control
101 * - check a mandatory captioned control
104 START_TEST (test_is_mandatory_regular)
106 GtkWidget *control = NULL, *icon = NULL;
107 GtkSizeGroup *group = NULL;
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");
113 gtk_widget_destroy (GTK_WIDGET (caption));
115 /* Test 2: create a mandatory caption */
116 control = GTK_WIDGET (gtk_entry_new());
117 caption = HILDON_CAPTION (hildon_caption_new(group,
121 HILDON_CAPTION_MANDATORY));
123 fail_if (hildon_caption_is_mandatory (caption) == FALSE,
124 "hildon-caption: The created HildonCaption has a mandatory captioned control and it shouldn't");
129 * Purpose: test is_mandatory with invalid arguments
131 * - use a NULL HildonCaption
132 * - use an object that is not a HildonCaption (a HBox)
134 START_TEST (test_is_mandatory_invalid)
136 GtkWidget *hbox = NULL;
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");
142 /* Test 2: invalid HildonCaption */
143 hbox = gtk_hbox_new (FALSE, 0);
144 if (hildon_caption_is_mandatory ((HildonCaption *) (hbox)) == TRUE)
146 gtk_widget_destroy (GTK_WIDGET (hbox));
147 fail ("hildon-caption: an invalid HildonCaption should return FALSE");
150 gtk_widget_destroy (GTK_WIDGET (hbox));
154 /* ----- Test case for set_status -----*/
157 * Purpose: test setting the status of a HildonCaption
159 * - Set the status HILDON_CAPTION_MANDATORY
161 START_TEST (test_set_status_regular)
164 hildon_caption_set_status (caption, HILDON_CAPTION_MANDATORY);
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);
173 * Purpose: test setting some status passing invalid parameters
176 * - set the status to a NULL HildonCaption
177 * - set the status to an invalid HildonCaption (use a HBox instead)
179 START_TEST (test_set_status_invalid)
181 const guint INVALID_ENUM = 8;
182 GtkWidget *hbox = NULL;
185 hildon_caption_set_status (caption, INVALID_ENUM);
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);
192 hildon_caption_set_status (NULL, HILDON_CAPTION_MANDATORY);
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);
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)
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);
208 gtk_widget_destroy (GTK_WIDGET (hbox));
212 /* ----- Test case for get_status -----*/
215 * Purpose: test getting the status of a HildonCaption
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
221 START_TEST (test_get_status_regular)
223 GValue value = {0, };
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);
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);
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);
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);
248 * Purpose: test get_status with invalid values
250 * - Get the status with a NULL HildonCaption
251 * - Get the status with an invalid HildonCaption (used a HBox)
253 START_TEST (test_get_status_invalid)
255 GtkWidget *hbox = NULL;
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);
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)
266 gtk_widget_destroy (GTK_WIDGET (hbox));
267 fail ("hildon-caption: an invalid HildonCaption should return HILDON_CAPTION_OPTIONAL");
270 gtk_widget_destroy (GTK_WIDGET (hbox));
274 /* ----- Test case for set_label -----*/
277 * Purpose: test setting labels for a HildonCaption
280 * - Set an empty label
282 START_TEST (test_set_label_regular)
284 const gchar *TEST_LABEL = TEST_STRING;
285 gchar * expected_ret_label=NULL;
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);
293 hildon_caption_set_label (caption, TEST_LABEL);
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);
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);
303 g_free(expected_ret_label);
306 hildon_caption_set_label (caption, "");
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));
315 * Purpose: test setting labels with invalid parameters
318 * - set a label to a NULL HildonCaption
319 * - set a label to an invalid HildonCaption (use a GtkHbox instead)
321 START_TEST (test_set_label_invalid)
323 const gchar *TEST_LABEL = TEST_STRING;
324 GtkWidget *hbox = NULL;
327 hildon_caption_set_label (caption, NULL);
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));
334 hildon_caption_set_label (NULL, TEST_LABEL);
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), "");
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)
345 gtk_widget_destroy (GTK_WIDGET (hbox));
346 fail ("hildon-caption: an invalid HildonCaption should return NULL");
349 gtk_widget_destroy (GTK_WIDGET (hbox));
353 /* ----- Test case for get_label -----*/
356 * Purpose: test getting a valid value for a label previously set
358 * - get label set with set_label
359 * - get empty label set with set_label
361 START_TEST (test_get_label_regular)
363 const gchar *TEST_LABEL = TEST_STRING;
364 gchar * expected_ret_label=NULL;
367 hildon_caption_set_label (caption, TEST_LABEL);
368 expected_ret_label = g_strconcat(TEST_LABEL, _("ecdg_ti_caption_separator"),NULL);
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);
374 g_free(expected_ret_label);
377 hildon_caption_set_label (caption, "");
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));
386 * Purpose: test getting labels with invalid arguments
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)
392 START_TEST (test_get_label_invalid)
394 GValue value = {0, };
395 GtkWidget *hbox = NULL;
396 const gchar *EMPTY_STRING = "";
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);
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));
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));
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))
416 gtk_widget_destroy (GTK_WIDGET (hbox));
417 fail ("hildon-caption: an invalid HildonCaption should return %s", EMPTY_STRING);
420 gtk_widget_destroy (GTK_WIDGET (hbox));
424 /* ---------- Suite creation ---------- */
426 Suite *create_hildon_caption_suite()
428 /* Create the suite */
429 Suite *s = suite_create("HildonCaption");
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");
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);
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);
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);
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);
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);
468 /* Return created suite */