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
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 /* -------------------- Fixtures -------------------- */
43 static HildonCaption *caption = NULL;
44 static const gchar *DEFAULT_TEST_CAPTION = "Default caption";
45 static GtkWidget *showed_window = NULL;
48 fx_setup_default_caption()
51 GtkWidget *control = NULL;
52 GtkWidget *icon = NULL;
53 GtkSizeGroup *group = NULL;
55 gtk_init(&argc, NULL);
57 control = gtk_entry_new();
58 caption = HILDON_CAPTION(hildon_caption_new(group,
62 HILDON_CAPTION_OPTIONAL));
64 showed_window = create_test_window ();
66 /* This packs the widget into the window (a gtk container). */
67 gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (caption));
69 /* Displays the widget and the window */
70 show_test_window (showed_window);
72 show_test_window (GTK_WIDGET(caption));
74 /* Check that the hildon-caption object has been created properly */
75 fail_if (!HILDON_IS_CAPTION (caption), "hildon-caption: Creation failed.");
80 fx_teardown_default_caption()
83 gtk_widget_destroy (GTK_WIDGET(caption));
85 /* Destroy the window */
86 gtk_widget_destroy (showed_window);
91 /* -------------------- Test cases -------------------- */
93 /* ----- Test case for is_mandatory -----*/
96 * Purpose: test getting the mandatory property of a HildonCaption
98 * - check an optional captioned control
99 * - check a mandatory captioned control
102 START_TEST (test_is_mandatory_regular)
104 GtkWidget *control = NULL, *icon = NULL;
105 GtkSizeGroup *group = NULL;
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");
111 gtk_widget_destroy (GTK_WIDGET (caption));
113 /* Test 2: create a mandatory caption */
114 control = GTK_WIDGET (gtk_entry_new());
115 caption = HILDON_CAPTION (hildon_caption_new(group,
119 HILDON_CAPTION_MANDATORY));
121 fail_if (hildon_caption_is_mandatory (caption) == FALSE,
122 "hildon-caption: The created HildonCaption has a mandatory captioned control and it shouldn't");
127 * Purpose: test is_mandatory with invalid arguments
129 * - use a NULL HildonCaption
130 * - use an object that is not a HildonCaption (a HBox)
132 START_TEST (test_is_mandatory_invalid)
134 GtkWidget *hbox = NULL;
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");
140 /* Test 2: invalid HildonCaption */
141 hbox = gtk_hbox_new (FALSE, 0);
142 if (hildon_caption_is_mandatory ((HildonCaption *) (hbox)) == TRUE)
144 gtk_widget_destroy (GTK_WIDGET (hbox));
145 fail ("hildon-caption: an invalid HildonCaption should return FALSE");
148 gtk_widget_destroy (GTK_WIDGET (hbox));
152 /* ----- Test case for set_status -----*/
155 * Purpose: test setting the status of a HildonCaption
157 * - Set the status HILDON_CAPTION_MANDATORY
159 START_TEST (test_set_status_regular)
162 hildon_caption_set_status (caption, HILDON_CAPTION_MANDATORY);
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);
171 * Purpose: test setting some status passing invalid parameters
174 * - set the status to a NULL HildonCaption
175 * - set the status to an invalid HildonCaption (use a HBox instead)
177 START_TEST (test_set_status_invalid)
179 const guint INVALID_ENUM = 8;
180 GtkWidget *hbox = NULL;
183 hildon_caption_set_status (caption, INVALID_ENUM);
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);
190 hildon_caption_set_status (NULL, HILDON_CAPTION_MANDATORY);
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);
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)
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);
206 gtk_widget_destroy (GTK_WIDGET (hbox));
210 /* ----- Test case for get_status -----*/
213 * Purpose: test getting the status of a HildonCaption
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
219 START_TEST (test_get_status_regular)
221 GValue value = {0, };
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);
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);
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);
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);
246 * Purpose: test get_status with invalid values
248 * - Get the status with a NULL HildonCaption
249 * - Get the status with an invalid HildonCaption (used a HBox)
251 START_TEST (test_get_status_invalid)
253 GtkWidget *hbox = NULL;
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);
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)
264 gtk_widget_destroy (GTK_WIDGET (hbox));
265 fail ("hildon-caption: an invalid HildonCaption should return HILDON_CAPTION_OPTIONAL");
268 gtk_widget_destroy (GTK_WIDGET (hbox));
272 /* ----- Test case for set_label -----*/
275 * Purpose: test setting labels for a HildonCaption
278 * - Set an empty label
280 START_TEST (test_set_label_regular)
282 const gchar *TEST_LABEL = TEST_STRING;
283 gchar * expected_ret_label=NULL;
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);
291 hildon_caption_set_label (caption, TEST_LABEL);
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);
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);
301 g_free(expected_ret_label);
304 hildon_caption_set_label (caption, "");
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));
313 * Purpose: test setting labels with invalid parameters
316 * - set a label to a NULL HildonCaption
317 * - set a label to an invalid HildonCaption (use a GtkHbox instead)
319 START_TEST (test_set_label_invalid)
321 const gchar *TEST_LABEL = TEST_STRING;
322 GtkWidget *hbox = NULL;
325 hildon_caption_set_label (caption, NULL);
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));
332 hildon_caption_set_label (NULL, TEST_LABEL);
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), "");
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)
343 gtk_widget_destroy (GTK_WIDGET (hbox));
344 fail ("hildon-caption: an invalid HildonCaption should return NULL");
347 gtk_widget_destroy (GTK_WIDGET (hbox));
351 /* ----- Test case for get_label -----*/
354 * Purpose: test getting a valid value for a label previously set
356 * - get label set with set_label
357 * - get empty label set with set_label
359 START_TEST (test_get_label_regular)
361 const gchar *TEST_LABEL = TEST_STRING;
362 gchar * expected_ret_label=NULL;
365 hildon_caption_set_label (caption, TEST_LABEL);
366 expected_ret_label = g_strconcat(TEST_LABEL,"ecdg_ti_caption_separator",NULL);
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);
372 g_free(expected_ret_label);
375 hildon_caption_set_label (caption, "");
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));
384 * Purpose: test getting labels with invalid arguments
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)
390 START_TEST (test_get_label_invalid)
392 GValue value = {0, };
393 GtkWidget *hbox = NULL;
394 const gchar *EMPTY_STRING = "";
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);
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));
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));
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))
414 gtk_widget_destroy (GTK_WIDGET (hbox));
415 fail ("hildon-caption: an invalid HildonCaption should return %s", EMPTY_STRING);
418 gtk_widget_destroy (GTK_WIDGET (hbox));
422 /* ---------- Suite creation ---------- */
424 Suite *create_hildon_caption_suite()
426 /* Create the suite */
427 Suite *s = suite_create("HildonCaption");
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");
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);
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);
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);
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);
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);
466 /* Return created suite */