aboutsummaryrefslogtreecommitdiff
path: root/tests/check-hildon-font-selection-dialog.c
blob: c71915a8447630543314c4f755c5a8c2135395ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * This file is a part of hildon tests
 *
 * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
 *
 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <stdlib.h>
#include <check.h>
#include <gtk/gtkmain.h>
#include <glib/gprintf.h>
#include <string.h>
#include "test_suites.h"
#include "check_utils.h"

#include <hildon/hildon-font-selection-dialog.h>

#define PREVIEW_TEXT TEST_STRING
#define DEFAULT_FONT_FAMILY "Sans"

static const guint16 N_FONT_SIZES = 7;
static const guint16 FONT_SIZES[] = 
  {
    6, 8, 10, 12, 16, 24, 32
  };

/* -------------------- Fixtures -------------------- */

static HildonFontSelectionDialog *font_selection_dialog = NULL;
static GtkWidget *showed_window = NULL;

static void 
fx_setup_default_font_selection_dialog ()
{
  int argc = 0;
  gtk_init(&argc, NULL);

  showed_window = create_test_window();


  font_selection_dialog = HILDON_FONT_SELECTION_DIALOG(hildon_font_selection_dialog_new(GTK_WINDOW(showed_window), "Font setup"));
 
  /* Check font selection dialog object has been created properly */
  fail_if(!HILDON_IS_FONT_SELECTION_DIALOG(font_selection_dialog), 
          "hildon-font-selection-dialog: Creation failed.");

  show_test_window(GTK_WIDGET(showed_window));
  
  show_test_window(GTK_WIDGET(font_selection_dialog));
 
}

static void 
fx_teardown_default_font_selection_dialog ()
{
  
  gtk_widget_destroy(GTK_WIDGET(font_selection_dialog));
    
  gtk_widget_destroy(GTK_WIDGET(showed_window));

}

/* -------------------- Helpers -------------------- */

static int 
helper_cmp_families(const void *a, const void *b)
{
  const char *a_name =
    pango_font_family_get_name(*(PangoFontFamily **) a);
  const char *b_name =
    pango_font_family_get_name(*(PangoFontFamily **) b);
  
  return g_utf8_collate(a_name, b_name);
}

static void 
helper_get_available_fonts(HildonFontSelectionDialog *fsd,
                           PangoFontFamily ***families,
                           gint *n_families)

{
  pango_context_list_families(gtk_widget_get_pango_context(GTK_WIDGET(fsd)), 
                              families,
			      n_families);

  qsort(*families, *n_families, sizeof(PangoFontFamily *), helper_cmp_families);
}

/* -------------------- Test cases -------------------- */

/* ----- Test case for set/get_preview_text ----- */

/**
 * Purpose: Check set and get of a valid preview text
 * Cases considered:
 *    - Set and get of a preview text
 */
START_TEST (test_set_get_preview_text_regular)
{
  gchar *ret_preview_text;
 
  /* Test1: set and get a preview text */
  hildon_font_selection_dialog_set_preview_text(font_selection_dialog, PREVIEW_TEXT);
  ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
  fail_if(strcmp(PREVIEW_TEXT, ret_preview_text) != 0,
          "hildon-font-selection-dialog: Called set_previex_text with \"%s\", but get_preview_text returned \"%s\"",
          PREVIEW_TEXT, ret_preview_text);
  g_free(ret_preview_text);
}
END_TEST

/**
 * Purpose: Check handling of invalid values
 * Cases considered:
 *    - Set empty preview text
 *    - Set a NULL preview text
 *    - Set with NULL object
 *    - Set with NULL object
 */
START_TEST (test_set_get_preview_text_invalid)
{
  gchar *ret_preview_text;

  /* Test1: Set a NULL preview text  */
  hildon_font_selection_dialog_set_preview_text(font_selection_dialog, NULL);
  ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
  fail_if(ret_preview_text == NULL,
          "hildon-font-selection-dialog: Call to set_preview_text with a NULL text is allowed");
  g_free(ret_preview_text);

  /* Test2: Set an empty preview text  */
  hildon_font_selection_dialog_set_preview_text(font_selection_dialog, "");
  ret_preview_text = hildon_font_selection_dialog_get_preview_text(font_selection_dialog);
  fail_if(strcmp("", ret_preview_text) != 0,
          "hildon-font-selection-dialog: Call to set_preview_text with an empty text is allowed");
  g_free(ret_preview_text);
  
  /* Test3: Set with NULL object */
  hildon_font_selection_dialog_set_preview_text(NULL, PREVIEW_TEXT);

  /* Test4: Get with NULL object */
  hildon_font_selection_dialog_get_preview_text(NULL);
}
END_TEST

/* ----- Test case for set/get property "family" ----- */

/**
 * Purpose: Check handling of regular values
 * Cases considered:
 *    - Set and get a font from the middle of the available fonts list
 */
START_TEST (test_set_get_property_family_regular)
{
  PangoFontFamily **families;
  gint n_families;
  GValue value = {0,};
  GValue ret_value = {0,};

  helper_get_available_fonts(font_selection_dialog, &families, &n_families);
  fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");

  /* Test1: set and get a font family */
  g_value_init(&value, G_TYPE_STRING);
  g_value_init(&ret_value, G_TYPE_STRING); 
  g_value_set_string(&value, pango_font_family_get_name(families[n_families/2]));
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
  fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
	  "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
	  g_value_get_string(&value), g_value_get_string(&ret_value));
}
END_TEST

/**
 * Purpose: Check handling of limit values
 * Cases considered:
 *    - Set and get first font of the available fonts list
 *    - Set and get last font of the available fonts list
 */
START_TEST (test_set_get_property_family_limits)
{
  PangoFontFamily **families;
  gint n_families;
  GValue value = {0,};
  GValue ret_value = {0,};
 
  helper_get_available_fonts(font_selection_dialog, &families, &n_families);
  fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");

  /* Test1: Test first font in the list */
  g_value_init(&value, G_TYPE_STRING);
  g_value_init(&ret_value, G_TYPE_STRING); 
  g_value_set_string(&value, pango_font_family_get_name(families[0]));
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
  fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
	  "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
	  g_value_get_string(&value), g_value_get_string(&ret_value));     
      
  /* Test2: Test last font in the list */
  g_value_set_string(&value, pango_font_family_get_name(families[n_families-1]));
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
  fail_if(strcmp(g_value_get_string(&value), g_value_get_string(&ret_value)) != 0,
	  "hildon-font-selection-dialog: set property \"family\" to value \"%s\", but get property returned value \"%s\"",
	  g_value_get_string(&value), g_value_get_string(&ret_value));   
}
END_TEST

/**
 * Purpose: Check handling of invalid values
 * Cases considered:
 *    - Set a non existing font
 *    - Set a NULL font
 */
START_TEST (test_set_get_property_family_invalid)
{
  GValue value = {0,};
  GValue ret_value = {0,};

  /* Test1: Set non existing font */
  g_value_init(&value, G_TYPE_STRING);
  g_value_init(&ret_value, G_TYPE_STRING); 
  g_value_set_string(&value, "-- invalid font name --");
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
  fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&ret_value)) != 0,
          "hildon-font-selection-dialog: set property \"family\" to invalid font name \"%s\", but get property returned value \"%s\" instead of \"%s\"",
          g_value_get_string(&value), g_value_get_string(&ret_value), DEFAULT_FONT_FAMILY);   
  
  /* Test2: Set NULL font */
  g_value_reset (&ret_value);
  g_value_reset (&value);
  g_value_set_string(&value, NULL);
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &ret_value);
  fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&ret_value)) != 0,
          "hildon-font-selection-dialog: set property \"family\" to NULL, but get property returned value \"%s\" instead of \"%s\"",
          g_value_get_string(&ret_value), DEFAULT_FONT_FAMILY);
}
END_TEST

/* ----- Test case for set/get property "family-set" ----- */

/**
 * Purpose: Check set and get of valid values
 * Cases considered:
 *    - Get returns FALSE when no font is selected.
 *    - Get returns TRUE when a font has been selected.
 *    - Set and get of value TRUE once a font has been selected
 *    - Set and get of value FALSE 
 */
START_TEST (test_set_get_property_family_set_regular)
{
  PangoFontFamily **families;
  gint n_families;
  GValue value = {0,};
  GValue ret_value = {0,}; 
  GValue family_value = {0,};

  helper_get_available_fonts(font_selection_dialog, &families, &n_families);
  fail_if (n_families <= 0, "hildon-font-selection-dialog: No available fonts");

  g_value_init(&value, G_TYPE_BOOLEAN);
  g_value_init(&ret_value, G_TYPE_BOOLEAN);
  g_value_init(&family_value, G_TYPE_STRING);

  /* Test1: Check get value returns FALSE when no font is selected */
  g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
  if (g_value_get_boolean(&ret_value) == FALSE)
    {
      g_object_get_property(G_OBJECT(font_selection_dialog), "family", &family_value);
      fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&family_value)) != 0,
	      "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\"",
	      g_value_get_string(&family_value), DEFAULT_FONT_FAMILY);      
    }

  /* Test2: Check get value returns TRUE when a font has been selected */
  g_value_set_string(&family_value, pango_font_family_get_name(families[0]));
  g_object_set_property(G_OBJECT(font_selection_dialog), "family", &family_value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
  fail_if(g_value_get_boolean(&ret_value) != TRUE,
	  "hildon-font-selection-dialog: Set a valid font for property \"family\", but get property \"family-set\" returned FALSE");
      
  /* Test3: Check set value to TRUE */
  g_value_set_boolean(&value, TRUE);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
  fail_if(g_value_get_boolean(&ret_value) != TRUE,
	  "hildon-font-selection-dialog: Set property \"family-set\" to TRUE once a valid font has been selected, but get_property returned FALSE");

  /* Test4: Check set value to FALSE */
  g_value_set_boolean(&value, FALSE);
  g_object_set_property(G_OBJECT(font_selection_dialog), "family-set", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "family-set", &ret_value);
  fail_if(g_value_get_boolean(&value) != g_value_get_boolean(&ret_value),
	  "hildon-font-selection-dialog: Set property \"family-set\" to FALSE, but get_property returned TRUE");
  g_object_get_property(G_OBJECT(font_selection_dialog), "family", &family_value);
  fail_if(strcmp(DEFAULT_FONT_FAMILY, g_value_get_string(&family_value)) != 0,
	  "hildon-font-selection-dialog: Set property  \"family-set\" to FALSE, but get of propery \"family\" returns \"%s\" instead of \"%s\"",
	  g_value_get_string(&family_value), DEFAULT_FONT_FAMILY);
}
END_TEST

/* ----- Test case for set/get property "size" ----- */

/**
 * Purpose: Check set and get of valid values
 * Cases considered:
 *    - Set a font size from the middle of the available sizes list 
 */
START_TEST (test_set_get_property_size_regular)
{
  gint font_size;
  GValue value = {0,};
  GValue ret_value = {0,};

  g_value_init(&value, G_TYPE_INT);
  g_value_init(&ret_value, G_TYPE_INT);

  /* Test1: set a valid font size */
  font_size = FONT_SIZES[N_FONT_SIZES/2];
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != font_size,
          "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
          font_size, g_value_get_int(&ret_value));
}
END_TEST

/**
 * Purpose: Check set and get of limit values
 * Cases considered:
 *    - Set first font size in the available sizes list
 *    - Set last font size in the available sizes list
 */
START_TEST (test_set_get_property_size_limits)
{
  gint font_size;
  GValue value = {0,};
  GValue ret_value = {0,};

  g_value_init(&value, G_TYPE_INT);
  g_value_init(&ret_value, G_TYPE_INT);

  /* Test1: set first font size */
  font_size = FONT_SIZES[0];
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != font_size,
          "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
          font_size, g_value_get_int(&ret_value));

  /* Test2: set last font size */
  font_size = FONT_SIZES[N_FONT_SIZES-1];
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != font_size,
          "hildon-font-selection-dialog: Set property \"size\" to %d, but get property returned \"%d\"",
          font_size, g_value_get_int(&ret_value));
}
END_TEST

/**
 * Purpose: Check set and get of invalid values
 * Cases considered:
 *    - Set negative size
 *    - Set a positive size that is not in the available sizes list
 *    - Set a positive size over the maximum allowed for the property
 */
START_TEST (test_set_get_property_size_invalid)
{
  gint font_size;
  gint default_size;
  GValue value = {0,};
  GValue ret_value = {0,};

  g_value_init(&value, G_TYPE_INT);
  g_value_init(&ret_value, G_TYPE_INT);

  default_size = 8;
  g_value_set_int(&value, default_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);

  /* Test1: Set negative value */
  font_size = -5;
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != default_size,
          "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
          font_size, g_value_get_int(&ret_value), default_size);  

  /* Test2: Set a positive that is not in the available list */
  font_size = 17;
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != default_size,
          "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
          font_size, g_value_get_int(&ret_value), default_size);

  /* Test3: Set a positive value over the maximum allowed */
  font_size = 112;
  g_value_set_int(&value, font_size);
  g_object_set_property(G_OBJECT(font_selection_dialog), "size", &value);
  g_object_get_property(G_OBJECT(font_selection_dialog), "size", &ret_value);
  fail_if(g_value_get_int(&ret_value) != default_size,
          "hildon-font-selection-dialog: Set property \"size\" to invalid %d, but get property returned \"%d\" instead of previous valid value \"%d\"",
          font_size, g_value_get_int(&ret_value), default_size);
}
END_TEST

/* ---------- Suite creation ---------- */

Suite *create_hildon_font_selection_dialog_suite()
{
  /* Create the suite */
  Suite *s = suite_create("HildonFontSelectionDialog");

  /* Create test cases */
  TCase *tc1 = tcase_create("set_get_preview_text");
  TCase *tc2 = tcase_create("set_get_property_family");
  TCase *tc3 = tcase_create("set_get_property_family_set");
  TCase *tc4 = tcase_create("set_get_property_size");

  /* Create test case for set/get_preview_text and add it to the suite */
  tcase_add_checked_fixture(tc1, fx_setup_default_font_selection_dialog, 
                            fx_teardown_default_font_selection_dialog);
  tcase_add_test(tc1, test_set_get_preview_text_regular);
  tcase_add_test(tc1, test_set_get_preview_text_invalid);
  suite_add_tcase(s, tc1);

  /* Create test case for set/get property "family" and add it to the suite */
  tcase_add_checked_fixture(tc2, fx_setup_default_font_selection_dialog,
                            fx_teardown_default_font_selection_dialog);
  tcase_add_test(tc2, test_set_get_property_family_regular);
  tcase_add_test(tc2, test_set_get_property_family_limits);
  tcase_add_test(tc2, test_set_get_property_family_invalid);
  suite_add_tcase(s, tc2);

  /* Create test case for set/get property "family-set" and add it to the suite */
  tcase_add_checked_fixture(tc3, fx_setup_default_font_selection_dialog,
                            fx_teardown_default_font_selection_dialog);
  tcase_add_test(tc3, test_set_get_property_family_set_regular);
  suite_add_tcase(s, tc3);

  /* Create test case for set/get property "size" and add it to the suite */
  tcase_add_checked_fixture(tc4, fx_setup_default_font_selection_dialog,
                            fx_teardown_default_font_selection_dialog);
  tcase_add_test(tc4, test_set_get_property_size_regular);
  tcase_add_test(tc4, test_set_get_property_size_limits);
  tcase_add_test(tc4, test_set_get_property_size_invalid);
  suite_add_tcase(s, tc4);

  /* Return created suite */
  return s;             
}