Some pannable area tuning
[hildon] / tests / check-hildon-font-selection-dialog.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, or (at your option) any later version.
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 <check.h>
27 #include <gtk/gtkmain.h>
28 #include <glib/gprintf.h>
29 #include <string.h>
30 #include "test_suites.h"
31 #include "check_utils.h"
32
33 #include <hildon/hildon-font-selection-dialog.h>
34
35 #define PREVIEW_TEXT TEST_STRING
36 #define DEFAULT_FONT_FAMILY "Sans"
37
38 static const guint16 N_FONT_SIZES = 7;
39 static const guint16 FONT_SIZES[] = 
40   {
41     6, 8, 10, 12, 16, 24, 32
42   };
43
44 /* -------------------- Fixtures -------------------- */
45
46 static HildonFontSelectionDialog *font_selection_dialog = NULL;
47 static GtkWidget *showed_window = NULL;
48
49 static void 
50 fx_setup_default_font_selection_dialog ()
51 {
52   int argc = 0;
53   gtk_init(&argc, NULL);
54
55   showed_window = create_test_window();
56
57
58   font_selection_dialog = HILDON_FONT_SELECTION_DIALOG(hildon_font_selection_dialog_new(GTK_WINDOW(showed_window), "Font setup"));
59  
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.");
63
64   show_test_window(GTK_WIDGET(showed_window));
65   
66   show_test_window(GTK_WIDGET(font_selection_dialog));
67  
68 }
69
70 static void 
71 fx_teardown_default_font_selection_dialog ()
72 {
73   
74   gtk_widget_destroy(GTK_WIDGET(font_selection_dialog));
75     
76   gtk_widget_destroy(GTK_WIDGET(showed_window));
77
78 }
79
80 /* -------------------- Helpers -------------------- */
81
82 static int 
83 helper_cmp_families(const void *a, const void *b)
84 {
85   const char *a_name =
86     pango_font_family_get_name(*(PangoFontFamily **) a);
87   const char *b_name =
88     pango_font_family_get_name(*(PangoFontFamily **) b);
89   
90   return g_utf8_collate(a_name, b_name);
91 }
92
93 static void 
94 helper_get_available_fonts(HildonFontSelectionDialog *fsd,
95                            PangoFontFamily ***families,
96                            gint *n_families)
97
98 {
99   pango_context_list_families(gtk_widget_get_pango_context(GTK_WIDGET(fsd)), 
100                               families,
101                               n_families);
102
103   qsort(*families, *n_families, sizeof(PangoFontFamily *), helper_cmp_families);
104 }
105
106 /* -------------------- Test cases -------------------- */
107
108 /* ----- Test case for set/get_preview_text ----- */
109
110 /**
111  * Purpose: Check set and get of a valid preview text
112  * Cases considered:
113  *    - Set and get of a preview text
114  */
115 START_TEST (test_set_get_preview_text_regular)
116 {
117   gchar *ret_preview_text;
118  
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);
126 }
127 END_TEST
128
129 /**
130  * Purpose: Check handling of invalid values
131  * Cases considered:
132  *    - Set empty preview text
133  *    - Set a NULL preview text
134  *    - Set with NULL object
135  *    - Set with NULL object
136  */
137 START_TEST (test_set_get_preview_text_invalid)
138 {
139   gchar *ret_preview_text;
140
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);
147
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);
154   
155   /* Test3: Set with NULL object */
156   hildon_font_selection_dialog_set_preview_text(NULL, PREVIEW_TEXT);
157
158   /* Test4: Get with NULL object */
159   hildon_font_selection_dialog_get_preview_text(NULL);
160 }
161 END_TEST
162
163 /* ----- Test case for set/get property "family" ----- */
164
165 /**
166  * Purpose: Check handling of regular values
167  * Cases considered:
168  *    - Set and get a font from the middle of the available fonts list
169  */
170 START_TEST (test_set_get_property_family_regular)
171 {
172   PangoFontFamily **families;
173   gint n_families;
174   GValue value = {0,};
175   GValue ret_value = {0,};
176
177   helper_get_available_fonts(font_selection_dialog, &families, &n_families);
178   fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
179
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));
189 }
190 END_TEST
191
192 /**
193  * Purpose: Check handling of limit values
194  * Cases considered:
195  *    - Set and get first font of the available fonts list
196  *    - Set and get last font of the available fonts list
197  */
198 START_TEST (test_set_get_property_family_limits)
199 {
200   PangoFontFamily **families;
201   gint n_families;
202   GValue value = {0,};
203   GValue ret_value = {0,};
204  
205   helper_get_available_fonts(font_selection_dialog, &families, &n_families);
206   fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
207
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));     
217       
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));   
225 }
226 END_TEST
227
228 /**
229  * Purpose: Check handling of invalid values
230  * Cases considered:
231  *    - Set a non existing font
232  *    - Set a NULL font
233  */
234 START_TEST (test_set_get_property_family_invalid)
235 {
236   GValue value = {0,};
237   GValue ret_value = {0,};
238
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);   
248   
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);
258 }
259 END_TEST
260
261 /* ----- Test case for set/get property "family-set" ----- */
262
263 /**
264  * Purpose: Check set and get of valid values
265  * Cases considered:
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 
270  */
271 START_TEST (test_set_get_property_family_set_regular)
272 {
273   PangoFontFamily **families;
274   gint n_families;
275   GValue value = {0,};
276   GValue ret_value = {0,}; 
277   GValue family_value = {0,};
278
279   helper_get_available_fonts(font_selection_dialog, &families, &n_families);
280   fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");
281
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);
285
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)
289     {
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);      
294     }
295
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");
302       
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");
308
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);
319 }
320 END_TEST
321
322 /* ----- Test case for set/get property "size" ----- */
323
324 /**
325  * Purpose: Check set and get of valid values
326  * Cases considered:
327  *    - Set a font size from the middle of the available sizes list 
328  */
329 START_TEST (test_set_get_property_size_regular)
330 {
331   gint font_size;
332   GValue value = {0,};
333   GValue ret_value = {0,};
334
335   g_value_init(&value, G_TYPE_INT);
336   g_value_init(&ret_value, G_TYPE_INT);
337
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));
346 }
347 END_TEST
348
349 /**
350  * Purpose: Check set and get of limit values
351  * Cases considered:
352  *    - Set first font size in the available sizes list
353  *    - Set last font size in the available sizes list
354  */
355 START_TEST (test_set_get_property_size_limits)
356 {
357   gint font_size;
358   GValue value = {0,};
359   GValue ret_value = {0,};
360
361   g_value_init(&value, G_TYPE_INT);
362   g_value_init(&ret_value, G_TYPE_INT);
363
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));
372
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));
381 }
382 END_TEST
383
384 /**
385  * Purpose: Check set and get of invalid values
386  * Cases considered:
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
390  */
391 START_TEST (test_set_get_property_size_invalid)
392 {
393   gint font_size;
394   gint default_size;
395   GValue value = {0,};
396   GValue ret_value = {0,};
397
398   g_value_init(&value, G_TYPE_INT);
399   g_value_init(&ret_value, G_TYPE_INT);
400
401   default_size = 8;
402   g_value_set_int(&value, default_size);
403   g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
404
405   /* Test1: Set negative value */
406   font_size = -5;
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);  
413
414   /* Test2: Set a positive that is not in the available list */
415   font_size = 17;
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);
422
423   /* Test3: Set a positive value over the maximum allowed */
424   font_size = 112;
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);
431 }
432 END_TEST
433
434 /* ---------- Suite creation ---------- */
435
436 Suite *create_hildon_font_selection_dialog_suite()
437 {
438   /* Create the suite */
439   Suite *s = suite_create("HildonFontSelectionDialog");
440
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");
446
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);
453
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);
461
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);
467
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);
475
476   /* Return created suite */
477   return s;             
478 }