Release 2.1.92-1
[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/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   /* NOTE: The test below has been deliverately commented out, since
223      it fails miserably. The range check in hildon_control_bar_set_range()
224      is _broken_ and fixing it causes other tests to fail. It is safe to assume
225      that by now, fixing this is going to cause more headaches than anything else,
226      and being this a deprecated widget, there is no point on wasting time on it.
227   */
228
229   /* Test 5: Set a range of [1,G_MININT] */
230 #if 0
231   init_min = 1;
232   init_max = G_MININT;
233   hildon_controlbar_set_range(controlbar,init_min,init_max);
234
235   max=hildon_controlbar_get_max(controlbar);
236
237   fail_if (max != init_min,
238            "hildon-controlbar: The returned max is %d and should be %d",
239            max, init_min);
240
241   min=hildon_controlbar_get_min(controlbar);
242
243   fail_if (min != init_min,
244            "hildon-controlbar: The returned min is %d and should be %d",
245            min, init_min);
246 #endif
247 }
248 END_TEST
249
250 /* ----- Test case for set_value -----*/
251 /**
252  * Purpose: Check that regular values are set and get properly
253  * Cases considered:
254  *    - Set a value of 500 in the range [0,1000].
255  */
256 START_TEST (test_set_value_regular)
257 {
258   gint init_value;
259   gint value;
260
261   init_value=500;
262   /* Must set a range different than [0,0], if not set you can not set a value*/
263   hildon_controlbar_set_range(controlbar,0,1000);
264
265   /* Test 1: Set a value of 500 in the range [0,1000].*/
266   hildon_controlbar_set_value(controlbar,init_value);
267   value=hildon_controlbar_get_value(controlbar);
268   fail_if (value != init_value,
269            "hildon-controlbar: The returned value is %d and should be %d", 
270            value, init_value);
271     
272 }
273 END_TEST
274
275 /**
276  * Purpose: Check that limit values are set and get properly
277  * Cases considered:
278  *    - Set a value of 0 in the range [0,1000].
279  *    - Set a value of 1000 in the range [0,1000].
280  */
281 START_TEST (test_set_value_limits)
282 {
283   gint init_value;
284   gint value;
285
286   /* Must set a range diferent than [0,0], if not set you can not set a value*/
287   hildon_controlbar_set_range(controlbar,0,1000);
288
289   /*  Test 1: Set a value of 0 in the range [0,1000] */
290   init_value=0;
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   /* Test 2: Set a value of 1000 in the range [0,1000] */
299   init_value=1000;
300
301   hildon_controlbar_set_value(controlbar,init_value);
302   value=hildon_controlbar_get_value(controlbar);
303   fail_if (value != init_value,
304            "hildon-controlbar: The returned value is %d and should be %d", 
305            value, init_value);    
306 }
307 END_TEST
308
309
310 /**
311  * Purpose: Check that invalid values are set and get properly
312  * Cases considered:
313  *    - Set a value of 1 in a NULL controlbar.
314  *    - Set a value of -1 in the range [2,1000].
315  *    - Set a value of 1 in the range [2,1000].
316  *    - Set a value of G_MININT in the range [2,1000].
317  *    - Set a value of 1001 in the range [2,1000].
318  *    - Set a value of G_MAXINT in the range [2,1000].
319  *    - Set a value of G_MAXINT in the range [2,G_MAXINT].
320  */
321 START_TEST (test_set_value_invalid)
322 {
323   gint init_value;
324   gint initial_value=4;
325   gint value;
326   gint current_value;
327   gint min_value;
328   gint max_value;
329
330   min_value = 2;
331   max_value = 1000;
332   /* Must set a range diferent than [0,0], if not set you can not set a value*/
333   hildon_controlbar_set_range(controlbar,min_value,max_value);
334   hildon_controlbar_set_value(controlbar,initial_value);
335
336   /* Test 1: Set a value of 1 in a NULL controlbar. */
337   init_value=10;
338   hildon_controlbar_set_value(NULL,init_value);
339
340
341   /* Test 2: Set a value of -1 in the range [2,1000] */
342   init_value=-1;
343   hildon_controlbar_set_value(controlbar,init_value);
344   value=hildon_controlbar_get_value(controlbar);
345   fail_if (value != initial_value,
346            "hildon-controlbar: The returned value is %d and should be %d",
347            value, initial_value);
348
349   /* Test 3: Set a value of 1 in the range [2,1000] */
350   init_value=1;
351   hildon_controlbar_set_value(controlbar,min_value);
352
353   hildon_controlbar_set_value(controlbar,init_value);
354   value=hildon_controlbar_get_value(controlbar);
355   fail_if (value != min_value,
356            "hildon-controlbar: The returned value is %d and should be %d",
357            value, min_value);
358
359
360   /* Test 4: Set a value of G_MININT in the range [2,1000] */
361   init_value=G_MININT;
362   hildon_controlbar_set_value(controlbar,min_value+2);
363   current_value = hildon_controlbar_get_value(controlbar);
364
365   hildon_controlbar_set_value(controlbar,init_value);
366   value=hildon_controlbar_get_value(controlbar);
367   fail_if (value != current_value,
368            "hildon-controlbar: The returned value is %d and should be %d",
369            value, current_value);
370
371   /* Test 5: Set a value of 1001 in the range [2,1000] */
372   init_value=1001;
373
374   hildon_controlbar_set_value(controlbar,init_value);
375   value=hildon_controlbar_get_value(controlbar);
376   fail_if (value != max_value,
377            "hildon-controlbar: The returned value is %d and should be %d",
378            value, max_value);
379
380
381   /* Test 6: Set a value of G_MAXINT in the range [2,1000] */
382   init_value=G_MAXINT;
383
384   hildon_controlbar_set_value(controlbar,init_value);
385   value=hildon_controlbar_get_value(controlbar);
386   fail_if (value != max_value,
387            "hildon-controlbar: The returned value is %d and should be %d",
388            value, max_value);
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 }