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
27 #include <gtk/gtkmain.h>
28 #include <glib/gprintf.h>
30 #include "test_suites.h"
31 #include "check_utils.h"
33 #include "hildon-font-selection-dialog.h"
35 #define PREVIEW_TEXT TEST_STRING
36 #define DEFAULT_FONT_FAMILY "Sans"
38 static const guint16 N_FONT_SIZES = 7;
39 static const guint16 FONT_SIZES[] =
41 6, 8, 10, 12, 16, 24, 32
44 /* -------------------- Fixtures -------------------- */
46 static HildonFontSelectionDialog *font_selection_dialog = NULL;
47 static GtkWidget *showed_window = NULL;
50 fx_setup_default_font_selection_dialog ()
53 gtk_init(&argc, NULL);
55 showed_window = create_test_window();
58 font_selection_dialog = HILDON_FONT_SELECTION_DIALOG(hildon_font_selection_dialog_new(GTK_WINDOW(showed_window), "Font setup"));
60 /* Check font selection dialog object has been created properly */
61 fail_if(!HILDON_IS_FONT_SELECTION_DIALOG(font_selection_dialog),
62 "hildon-font-selection-dialog: Creation failed.");
64 show_test_window(GTK_WIDGET(showed_window));
66 show_test_window(GTK_WIDGET(font_selection_dialog));
71 fx_teardown_default_font_selection_dialog ()
74 gtk_widget_destroy(GTK_WIDGET(font_selection_dialog));
76 gtk_widget_destroy(GTK_WIDGET(showed_window));
80 /* -------------------- Helpers -------------------- */
83 helper_cmp_families(const void *a, const void *b)
86 pango_font_family_get_name(*(PangoFontFamily **) a);
88 pango_font_family_get_name(*(PangoFontFamily **) b);
90 return g_utf8_collate(a_name, b_name);
94 helper_get_available_fonts(HildonFontSelectionDialog *fsd,
95 PangoFontFamily ***families,
99 pango_context_list_families(gtk_widget_get_pango_context(GTK_WIDGET(fsd)),
103 qsort(*families, *n_families, sizeof(PangoFontFamily *), helper_cmp_families);
106 /* -------------------- Test cases -------------------- */
108 /* ----- Test case for set/get_preview_text ----- */
111 * Purpose: Check set and get of a valid preview text
113 * - Set and get of a preview text
115 START_TEST (test_set_get_preview_text_regular)
117 gchar *ret_preview_text;
119 /* Test1: set and get a preview text */
120 hildon_font_selection_dialog_set_preview_text(font_selection_dialog, PREVIEW_TEXT);
121 ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
122 fail_if(strcmp(PREVIEW_TEXT, ret_preview_text) != 0,
123 "hildon-font-selection-dialog: Called set_previex_text with \"%s\", but get_preview_text returned \"%s\"",
124 PREVIEW_TEXT, ret_preview_text);
125 g_free(ret_preview_text);
130 * Purpose: Check handling of invalid values
132 * - Set empty preview text
133 * - Set a NULL preview text
134 * - Set with NULL object
135 * - Set with NULL object
137 START_TEST (test_set_get_preview_text_invalid)
139 gchar *ret_preview_text;
141 /* Test1: Set a NULL preview text */
142 hildon_font_selection_dialog_set_preview_text(font_selection_dialog, NULL);
143 ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
144 fail_if(ret_preview_text == NULL,
145 "hildon-font-selection-dialog: Call to set_preview_text with a NULL text is allowed");
146 g_free(ret_preview_text);
148 /* Test2: Set an empty preview text */
149 hildon_font_selection_dialog_set_preview_text(font_selection_dialog, "");
150 ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
151 fail_if(strcmp("", ret_preview_text) != 0,
152 "hildon-font-selection-dialog: Call to set_preview_text with an empty text is allowed");
153 g_free(ret_preview_text);
155 /* Test3: Set with NULL object */
156 hildon_font_selection_dialog_set_preview_text(NULL, PREVIEW_TEXT);
158 /* Test4: Get with NULL object */
159 hildon_font_selection_dialog_get_preview_text(NULL);
163 /* ----- Test case for set/get property "family" ----- */
166 * Purpose: Check handling of regular values
168 * - Set and get a font from the middle of the available fonts list
170 START_TEST (test_set_get_property_family_regular)
172 PangoFontFamily **families;
175 GValue ret_value = {0,};
177 helper_get_available_fonts(font_selection_dialog, &families, &n_families);
178 fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
180 /* Test1: set and get a font family */
181 g_value_init(&value, G_TYPE_STRING);
182 g_value_init(&ret_value, G_TYPE_STRING);
183 g_value_set_string(&value, pango_font_family_get_name(families[n_families/2]));
184 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
185 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
186 fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
187 "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
188 g_value_get_string(&value), g_value_get_string(&ret_value));
193 * Purpose: Check handling of limit values
195 * - Set and get first font of the available fonts list
196 * - Set and get last font of the available fonts list
198 START_TEST (test_set_get_property_family_limits)
200 PangoFontFamily **families;
203 GValue ret_value = {0,};
205 helper_get_available_fonts(font_selection_dialog, &families, &n_families);
206 fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
208 /* Test1: Test first font in the list */
209 g_value_init(&value, G_TYPE_STRING);
210 g_value_init(&ret_value, G_TYPE_STRING);
211 g_value_set_string(&value, pango_font_family_get_name(families[0]));
212 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
213 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
214 fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
215 "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
216 g_value_get_string(&value), g_value_get_string(&ret_value));
218 /* Test2: Test last font in the list */
219 g_value_set_string(&value, pango_font_family_get_name(families[n_families-1]));
220 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
221 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
222 fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
223 "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
224 g_value_get_string(&value), g_value_get_string(&ret_value));
229 * Purpose: Check handling of invalid values
231 * - Set a non existing font
234 START_TEST (test_set_get_property_family_invalid)
237 GValue ret_value = {0,};
239 /* Test1: Set non existing font */
240 g_value_init(&value, G_TYPE_STRING);
241 g_value_init(&ret_value, G_TYPE_STRING);
242 g_value_set_string(&value, "-- invalid font name --");
243 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
244 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
245 fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&ret_value)) != 0,
246 "hildon-font-selection-dialog: set property \"family\" to invalid font name \"%s\", but get property returned value \"%s\" instead of \"%s\"",
247 g_value_get_string(&value), g_value_get_string(&ret_value), DEFAULT_FONT_FAMILY);
249 /* Test2: Set NULL font */
250 g_value_reset (&ret_value);
251 g_value_reset (&value);
252 g_value_set_string(&value, NULL);
253 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
254 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
255 fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&ret_value)) != 0,
256 "hildon-font-selection-dialog: set property \"family\" to NULL, but get property returned value \"%s\" instead of \"%s\"",
257 g_value_get_string(&ret_value), DEFAULT_FONT_FAMILY);
261 /* ----- Test case for set/get property "family-set" ----- */
264 * Purpose: Check set and get of valid values
266 * - Get returns FALSE when no font is selected.
267 * - Get returns TRUE when a font has been selected.
268 * - Set and get of value TRUE once a font has been selected
269 * - Set and get of value FALSE
271 START_TEST (test_set_get_property_family_set_regular)
273 PangoFontFamily **families;
276 GValue ret_value = {0,};
277 GValue family_value = {0,};
279 helper_get_available_fonts(font_selection_dialog, &families, &n_families);
280 fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
282 g_value_init(&value, G_TYPE_BOOLEAN);
283 g_value_init(&ret_value, G_TYPE_BOOLEAN);
284 g_value_init(&family_value, G_TYPE_STRING);
286 /* Test1: Check get value returns FALSE when no font is selected */
287 g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
288 if (g_value_get_boolean(&ret_value) == FALSE)
290 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &family_value);
291 fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&family_value)) != 0,
292 "hildon-font-selection-dialog: After dialog creation, get value of property \"family-set\" returns FALSE, but get value of property \"family\" is \"%s\" instead of \"%s\"",
293 g_value_get_string(&family_value), DEFAULT_FONT_FAMILY);
296 /* Test2: Check get value returns TRUE when a font has been selected */
297 g_value_set_string(&family_value, pango_font_family_get_name(families[0]));
298 g_object_set_property(G_OBJECT(font_selection_dialog), "family", &family_value);
299 g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
300 fail_if(g_value_get_boolean(&ret_value) != TRUE,
301 "hildon-font-selection-dialog: Set a valid font for property \"family\", but get property \"family-set\" returned FALSE");
303 /* Test3: Check set value to TRUE */
304 g_value_set_boolean(&value, TRUE);
305 g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
306 fail_if(g_value_get_boolean(&ret_value) != TRUE,
307 "hildon-font-selection-dialog: Set property \"family-set\" to TRUE once a valid font has been selected, but get_property returned FALSE");
309 /* Test4: Check set value to FALSE */
310 g_value_set_boolean(&value, FALSE);
311 g_object_set_property(G_OBJECT(font_selection_dialog), "family-set", &value);
312 g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
313 fail_if(g_value_get_boolean(&value) != g_value_get_boolean(&ret_value),
314 "hildon-font-selection-dialog: Set property \"family-set\" to FALSE, but get_property returned TRUE");
315 g_object_get_property(G_OBJECT(font_selection_dialog), "family", &family_value);
316 fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&family_value)) != 0,
317 "hildon-font-selection-dialog: Set property \"family-set\" to FALSE, but get of propery \"family\" returns \"%s\" instead of \"%s\"",
318 g_value_get_string(&family_value), DEFAULT_FONT_FAMILY);
322 /* ----- Test case for set/get property "size" ----- */
325 * Purpose: Check set and get of valid values
327 * - Set a font size from the middle of the available sizes list
329 START_TEST (test_set_get_property_size_regular)
333 GValue ret_value = {0,};
335 g_value_init(&value, G_TYPE_INT);
336 g_value_init(&ret_value, G_TYPE_INT);
338 /* Test1: set a valid font size */
339 font_size = FONT_SIZES[N_FONT_SIZES/2];
340 g_value_set_int(&value, font_size);
341 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
342 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
343 fail_if(g_value_get_int(&ret_value) != font_size,
344 "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
345 font_size, g_value_get_int(&ret_value));
350 * Purpose: Check set and get of limit values
352 * - Set first font size in the available sizes list
353 * - Set last font size in the available sizes list
355 START_TEST (test_set_get_property_size_limits)
359 GValue ret_value = {0,};
361 g_value_init(&value, G_TYPE_INT);
362 g_value_init(&ret_value, G_TYPE_INT);
364 /* Test1: set first font size */
365 font_size = FONT_SIZES[0];
366 g_value_set_int(&value, font_size);
367 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
368 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
369 fail_if(g_value_get_int(&ret_value) != font_size,
370 "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
371 font_size, g_value_get_int(&ret_value));
373 /* Test2: set last font size */
374 font_size = FONT_SIZES[N_FONT_SIZES-1];
375 g_value_set_int(&value, font_size);
376 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
377 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
378 fail_if(g_value_get_int(&ret_value) != font_size,
379 "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
380 font_size, g_value_get_int(&ret_value));
385 * Purpose: Check set and get of invalid values
387 * - Set negative size
388 * - Set a positive size that is not in the available sizes list
389 * - Set a positive size over the maximum allowed for the property
391 START_TEST (test_set_get_property_size_invalid)
396 GValue ret_value = {0,};
398 g_value_init(&value, G_TYPE_INT);
399 g_value_init(&ret_value, G_TYPE_INT);
402 g_value_set_int(&value, default_size);
403 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
405 /* Test1: Set negative value */
407 g_value_set_int(&value, font_size);
408 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
409 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
410 fail_if(g_value_get_int(&ret_value) != default_size,
411 "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
412 font_size, g_value_get_int(&ret_value), default_size);
414 /* Test2: Set a positive that is not in the available list */
416 g_value_set_int(&value, font_size);
417 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
418 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
419 fail_if(g_value_get_int(&ret_value) != default_size,
420 "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
421 font_size, g_value_get_int(&ret_value), default_size);
423 /* Test3: Set a positive value over the maximum allowed */
425 g_value_set_int(&value, font_size);
426 g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
427 g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
428 fail_if(g_value_get_int(&ret_value) != default_size,
429 "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
430 font_size, g_value_get_int(&ret_value), default_size);
434 /* ---------- Suite creation ---------- */
436 Suite *create_hildon_font_selection_dialog_suite()
438 /* Create the suite */
439 Suite *s = suite_create("HildonFontSelectionDialog");
441 /* Create test cases */
442 TCase *tc1 = tcase_create("set_get_preview_text");
443 TCase *tc2 = tcase_create("set_get_property_family");
444 TCase *tc3 = tcase_create("set_get_property_family_set");
445 TCase *tc4 = tcase_create("set_get_property_size");
447 /* Create test case for set/get_preview_text and add it to the suite */
448 tcase_add_checked_fixture(tc1, fx_setup_default_font_selection_dialog,
449 fx_teardown_default_font_selection_dialog);
450 tcase_add_test(tc1, test_set_get_preview_text_regular);
451 tcase_add_test(tc1, test_set_get_preview_text_invalid);
452 suite_add_tcase(s, tc1);
454 /* Create test case for set/get property "family" and add it to the suite */
455 tcase_add_checked_fixture(tc2, fx_setup_default_font_selection_dialog,
456 fx_teardown_default_font_selection_dialog);
457 tcase_add_test(tc2, test_set_get_property_family_regular);
458 tcase_add_test(tc2, test_set_get_property_family_limits);
459 tcase_add_test(tc2, test_set_get_property_family_invalid);
460 suite_add_tcase(s, tc2);
462 /* Create test case for set/get property "family-set" and add it to the suite */
463 tcase_add_checked_fixture(tc3, fx_setup_default_font_selection_dialog,
464 fx_teardown_default_font_selection_dialog);
465 tcase_add_test(tc3, test_set_get_property_family_set_regular);
466 suite_add_tcase(s, tc3);
468 /* Create test case for set/get property "size" and add it to the suite */
469 tcase_add_checked_fixture(tc4, fx_setup_default_font_selection_dialog,
470 fx_teardown_default_font_selection_dialog);
471 tcase_add_test(tc4, test_set_get_property_size_regular);
472 tcase_add_test(tc4, test_set_get_property_size_limits);
473 tcase_add_test(tc4, test_set_get_property_size_invalid);
474 suite_add_tcase(s, tc4);
476 /* Return created suite */