Center label of HildonNote
[hildon] / tests / check-hildon-controlbar.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 <gtk/gtkcontainer.h>
29 #include "test_suites.h"
30 #include "check_utils.h"
31 #include "hildon-controlbar.h"
32
33 /* -------------------- Fixtures -------------------- */
34 static HildonControlbar *controlbar = NULL;
35 static GtkWidget *showed_window = NULL;
36
37 static void 
38 fx_setup_default_controlbar ()
39 {
40   int argc = 0;
41   gtk_init(&argc, NULL);
42
43   controlbar = HILDON_CONTROLBAR(hildon_controlbar_new());
44   /* Check controlbar object has been created properly */
45   fail_if(!HILDON_IS_CONTROLBAR(controlbar), 
46           "hildon-controlbar: Creation failed.");
47
48   showed_window =  create_test_window ();
49   
50   /* This packs the widget into the window (a gtk container). */
51   gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (controlbar));
52   
53   /* Displays the widget and the window */
54   show_all_test_window (showed_window);
55   
56 }
57
58 static void 
59 fx_teardown_default_controlbar ()
60 {
61
62   /* Destroy the widget and the window */
63   gtk_widget_destroy (GTK_WIDGET(showed_window));
64
65 }
66
67 /* -------------------- Test cases -------------------- */
68
69 /* ----- Test case for set_range -----*/
70 /**
71  * Purpose: Check that regular range values are set and get properly
72  * Cases considered:
73  *    - Set a range of [20,1000].
74  *    - Set a range of [20,20].
75  */
76 START_TEST (test_set_range_regular)
77 {
78   gint init_min;
79   gint init_max;
80   gint min;
81   gint max;
82     
83   /* Test 1: Set a range of [20,1000] */
84   init_min = 20;
85   init_max = 1000;
86   hildon_controlbar_set_range(controlbar,init_min,init_max);
87
88   min = hildon_controlbar_get_min(controlbar);
89
90   fail_if (min != init_min,
91            "hildon-controlbar: The returned min is %d and should be %d", 
92            min, init_min);
93
94   max = hildon_controlbar_get_max(controlbar);
95
96   fail_if (max != init_max,
97            "hildon-controlbar: The returned max is %d and should be %d", 
98            max, init_max);
99
100   /* Test 2: Set a range of [20, 20] */
101   init_min = 20;
102   init_max = 20;
103   hildon_controlbar_set_range(controlbar,init_min,init_max);
104
105   min = hildon_controlbar_get_min(controlbar);
106
107   fail_if (min != init_min,
108            "hildon-controlbar: The returned min is %d and should be %d", 
109            min, init_min);
110
111   max = hildon_controlbar_get_max(controlbar);
112
113   fail_if (max != init_max,
114            "hildon-controlbar: The returned max is %d and should be %d", 
115            max, init_max);
116     
117 }
118 END_TEST
119
120 /**
121  * Purpose: Check that limits range values are set and get properly
122  * Cases considered:
123  *    - Set a range of [0,G_MAXINT-1].
124  */
125 START_TEST (test_set_range_limits)
126 {
127   gint init_min;
128   gint init_max;
129   gint min;
130   gint max;
131     
132   /* Test 1: Set a range of [0,G_MAXINT-1] */
133   init_min = 0;
134   init_max = G_MAXINT-1;
135   hildon_controlbar_set_range(controlbar,init_min,init_max);
136
137   min=hildon_controlbar_get_min(controlbar);
138
139   fail_if (min != init_min,
140            "hildon-controlbar: The returned min is %d and should be %d", 
141            min, init_min);
142
143   max=hildon_controlbar_get_max(controlbar);
144
145   fail_if (max != init_max,
146            "hildon-controlbar: The returned max is %d and should be %d", 
147            max, init_max);
148 }
149 END_TEST
150
151 /**
152  * Purpose: Check that invalid range values are set and get properly
153  * Cases considered:
154  *    - Set a range of [10,100] on NULL object.
155  *    - Set a range of [-1,G_MAXINT].
156  *    - Set a range of [G_MININT,-1].
157  *    - Set a range of [G_MAXINT,-1].
158  *    - Set a range of [1,G_MININT].
159  */
160 START_TEST (test_set_range_invalid)
161 {
162   gint init_min;
163   gint init_max;
164   gint min;
165   gint max;
166
167   init_min = 10;
168   init_max = 100;
169   /* Test 1: Set range on a NULL object */
170   hildon_controlbar_set_range(NULL,init_min,init_max);
171
172   init_min = -1;
173   init_max = G_MAXINT;
174   /* Test 2: Set a range of [-1,G_MAXINT] */
175   hildon_controlbar_set_range(controlbar,init_min,init_max);
176
177   min=hildon_controlbar_get_min(controlbar);
178
179   fail_if (min != init_min,
180            "hildon-controlbar: The returned min is %d and should be %d",
181            min, init_min);
182
183   max=hildon_controlbar_get_max(controlbar);
184
185   fail_if (max != init_max,
186            "hildon-controlbar: The returned max is %d and should be %d",
187            max, init_max);
188
189   /* Test 3: Set a range of [G_MININT,-1] */
190   init_min = G_MININT;
191   init_max = -1;
192   hildon_controlbar_set_range(controlbar,init_min,init_max);
193
194   min=hildon_controlbar_get_min(controlbar);
195
196   fail_if (min != init_min,
197            "hildon-controlbar: The returned min is %d and should be %d",
198            min, init_min);
199   max=hildon_controlbar_get_max(controlbar);
200
201   fail_if (max != init_max,
202            "hildon-controlbar: The returned max is %d and should be %d",
203            max, init_max);
204
205   /* Test 4: Set a range of [G_MAXINT,-1] */
206   init_min = G_MAXINT;
207   init_max = -1;
208   hildon_controlbar_set_range(controlbar,init_min,init_max);
209
210   max=hildon_controlbar_get_max(controlbar);
211
212   fail_if (max != init_max,
213            "hildon-controlbar: The returned max is %d and should be %d",
214            max, init_max);
215
216   min=hildon_controlbar_get_min(controlbar);
217
218   fail_if (min != init_max,
219            "hildon-controlbar: The returned min is %d and should be %d",
220            min, init_max);
221
222   /* Test 5: Set a range of [1,G_MININT] */
223   init_min = 1;
224   init_max = G_MININT;
225   hildon_controlbar_set_range(controlbar,init_min,init_max);
226
227   max=hildon_controlbar_get_max(controlbar);
228
229   fail_if (max != init_min,
230            "hildon-controlbar: The returned max is %d and should be %d",
231            max, init_min);
232
233   min=hildon_controlbar_get_min(controlbar);
234
235   fail_if (min != init_min,
236            "hildon-controlbar: The returned min is %d and should be %d",
237            min, init_min);
238 }
239 END_TEST
240
241 /* ----- Test case for set_value -----*/
242 /**
243  * Purpose: Check that regular values are set and get properly
244  * Cases considered:
245  *    - Set a value of 500 in the range [0,1000].
246  */
247 START_TEST (test_set_value_regular)
248 {
249   gint init_value;
250   gint value;
251
252   init_value=500;
253   /* Must set a range different than [0,0], if not set you can not set a value*/
254   hildon_controlbar_set_range(controlbar,0,1000);
255
256   /* Test 1: Set a value of 500 in the range [0,1000].*/
257   hildon_controlbar_set_value(controlbar,init_value);
258   value=hildon_controlbar_get_value(controlbar);
259   fail_if (value != init_value,
260            "hildon-controlbar: The returned value is %d and should be %d", 
261            value, init_value);
262     
263 }
264 END_TEST
265
266 /**
267  * Purpose: Check that limit values are set and get properly
268  * Cases considered:
269  *    - Set a value of 0 in the range [0,1000].
270  *    - Set a value of 1000 in the range [0,1000].
271  */
272 START_TEST (test_set_value_limits)
273 {
274   gint init_value;
275   gint value;
276
277   /* Must set a range diferent than [0,0], if not set you can not set a value*/
278   hildon_controlbar_set_range(controlbar,0,1000);
279
280   /*  Test 1: Set a value of 0 in the range [0,1000] */
281   init_value=0;
282
283   hildon_controlbar_set_value(controlbar,init_value);
284   value=hildon_controlbar_get_value(controlbar);
285   fail_if (value != init_value,
286            "hildon-controlbar: The returned value is %d and should be %d", 
287            value, init_value);
288
289   /* Test 2: Set a value of 1000 in the range [0,1000] */
290   init_value=1000;
291
292   hildon_controlbar_set_value(controlbar,init_value);
293   value=hildon_controlbar_get_value(controlbar);
294   fail_if (value != init_value,
295            "hildon-controlbar: The returned value is %d and should be %d", 
296            value, init_value);    
297 }
298 END_TEST
299
300
301 /**
302  * Purpose: Check that invalid values are set and get properly
303  * Cases considered:
304  *    - Set a value of 1 in a NULL controlbar.
305  *    - Set a value of -1 in the range [2,1000].
306  *    - Set a value of 1 in the range [2,1000].
307  *    - Set a value of G_MININT in the range [2,1000].
308  *    - Set a value of 1001 in the range [2,1000].
309  *    - Set a value of G_MAXINT in the range [2,1000].
310  *    - Set a value of G_MAXINT in the range [2,G_MAXINT].
311  */
312 START_TEST (test_set_value_invalid)
313 {
314   gint init_value;
315   gint initial_value=4;
316   gint value;
317   gint current_value;
318   gint min_value;
319   gint max_value;
320
321   min_value = 2;
322   max_value = 1000;
323   /* Must set a range diferent than [0,0], if not set you can not set a value*/
324   hildon_controlbar_set_range(controlbar,min_value,max_value);
325   hildon_controlbar_set_value(controlbar,initial_value);
326
327   /* Test 1: Set a value of 1 in a NULL controlbar. */
328   init_value=10;
329   hildon_controlbar_set_value(NULL,init_value);
330
331
332   /* Test 2: Set a value of -1 in the range [2,1000] */
333   init_value=-1;
334   hildon_controlbar_set_value(controlbar,init_value);
335   value=hildon_controlbar_get_value(controlbar);
336   fail_if (value != initial_value,
337            "hildon-controlbar: The returned value is %d and should be %d",
338            value, initial_value);
339
340   /* Test 3: Set a value of 1 in the range [2,1000] */
341   init_value=1;
342   hildon_controlbar_set_value(controlbar,min_value);
343
344   hildon_controlbar_set_value(controlbar,init_value);
345   value=hildon_controlbar_get_value(controlbar);
346   fail_if (value != min_value,
347            "hildon-controlbar: The returned value is %d and should be %d",
348            value, min_value);
349
350
351   /* Test 4: Set a value of G_MININT in the range [2,1000] */
352   init_value=G_MININT;
353   hildon_controlbar_set_value(controlbar,min_value+2);
354   current_value = hildon_controlbar_get_value(controlbar);
355
356   hildon_controlbar_set_value(controlbar,init_value);
357   value=hildon_controlbar_get_value(controlbar);
358   fail_if (value != current_value,
359            "hildon-controlbar: The returned value is %d and should be %d",
360            value, current_value);
361
362   /* Test 5: Set a value of 1001 in the range [2,1000] */
363   init_value=1001;
364
365   hildon_controlbar_set_value(controlbar,init_value);
366   value=hildon_controlbar_get_value(controlbar);
367   fail_if (value != max_value,
368            "hildon-controlbar: The returned value is %d and should be %d",
369            value, max_value);
370
371
372   /* Test 6: Set a value of G_MAXINT in the range [2,1000] */
373   init_value=G_MAXINT;
374
375   hildon_controlbar_set_value(controlbar,init_value);
376   value=hildon_controlbar_get_value(controlbar);
377   fail_if (value != max_value,
378            "hildon-controlbar: The returned value is %d and should be %d",
379            value, max_value);
380
381   /* Test 7: Set a value of G_MAXINT in the range [2,G_MAXINT] */
382   init_value=G_MAXINT;
383   hildon_controlbar_set_range(controlbar,2,G_MAXINT);
384   hildon_controlbar_set_value(controlbar,init_value);
385   value=hildon_controlbar_get_value(controlbar);
386   fail_if (value != G_MAXINT-1,
387            "hildon-controlbar: The returned value is %d and should be %d",
388            value, G_MAXINT-1);
389 }
390 END_TEST
391
392 /* ---------- Suite creation ---------- */
393
394 Suite *create_hildon_controlbar_suite()
395 {
396   /* Create the suite */
397   Suite *s = suite_create("HildonControlbar");
398
399   /* Create test cases */
400   TCase *tc1 = tcase_create("set_range");
401   TCase *tc2 = tcase_create("set_value");
402
403   /* Create test case for hildon_controlbar_set_range and add it to the suite */
404   tcase_add_checked_fixture(tc1, fx_setup_default_controlbar, fx_teardown_default_controlbar);
405   tcase_add_test(tc1, test_set_range_regular);
406   tcase_add_test(tc1, test_set_range_limits);
407   tcase_add_test(tc1, test_set_range_invalid);
408   suite_add_tcase (s, tc1);
409
410   /* Create test case for hildon_controlbar_set_value and add it to the suite */
411   tcase_add_checked_fixture(tc2, fx_setup_default_controlbar, fx_teardown_default_controlbar);
412   tcase_add_test(tc2, test_set_value_regular);
413   tcase_add_test(tc2, test_set_value_limits);
414   tcase_add_test(tc2, test_set_value_invalid);
415   suite_add_tcase (s, tc2);
416
417   return s;
418 }