Adding missing debian changelog. Modyfying hildon-banner to allow creation with null...
[hildon] / tests / check-hildon-color-button.c
1 /*
2  * Copyright (C) 2006 Nokia Corporation.
3  *
4  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include <stdlib.h>
24 #include <check.h>
25 #include <gtk/gtkmain.h>
26 #include <gtk/gtkhbox.h>
27 #include <glib/gprintf.h>
28 #include "test_suites.h"
29 #include "check_utils.h"
30 #include "hildon-color-button.h"
31
32 /* -------------------- Fixtures -------------------- */
33 static HildonColorButton *color_button = NULL;
34 static GtkWidget *showed_window = NULL;
35
36 static void 
37 fx_setup_default_color_button()
38 {
39   int argc = 0;
40   gtk_init(&argc, NULL);
41
42   color_button = HILDON_COLOR_BUTTON(hildon_color_button_new());
43       /* Check that the color button object has been created properly */
44
45   showed_window =  create_test_window ();
46  
47   /* This packs the widget into the window (a gtk container). */
48   gtk_container_add (GTK_CONTAINER (showed_window), GTK_WIDGET (color_button));
49   
50   /* Displays the widget and the window */
51   show_all_test_window (showed_window);
52
53
54   fail_if(!HILDON_IS_COLOR_BUTTON(color_button),
55           "hildon-color-button: Creation failed.");
56
57 }
58
59 static void 
60 fx_teardown_default_color_button()
61 {
62   
63   /* Destroy the widget and the window */
64   gtk_widget_destroy(GTK_WIDGET(showed_window)); 
65
66 }
67
68 /* -------------------- Test cases -------------------- */
69
70 /* ----- Test case for set_color -----*/
71 /**
72  * Purpose: Check that a regular color can be set safely
73  * Cases considered:
74  *    - Set color defined by (255, 255, 255) and pixel = 255
75  *    - Set color defined by (10, 20, 30) and pixel = 10
76  *    - Set color defined by (20, 10, 30) and pixel = 10
77  *    - Set color defined by (30, 10, 20) and pixel = 20
78  */
79
80 START_TEST (test_set_color_regular)
81 {
82   GdkColor color;
83   GdkColor * b_color;
84   GdkColor * ret_color = g_new (GdkColor, 1);
85   guint red;
86   guint green;
87   guint blue;
88   guint pixel;
89
90   b_color = gdk_color_copy(&color);
91
92   /* Test 1: Set color defined by (255,255,255)*/
93   red = 255;
94   green = 255;
95   blue = 255;
96   pixel = 255;
97
98   b_color->red=red;
99   b_color->green=green;
100   b_color->blue=blue;
101   b_color->pixel=pixel;
102     
103   hildon_color_button_set_color(color_button,b_color);
104   hildon_color_button_get_color(color_button, ret_color);
105
106   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
107            (blue != ret_color->blue) || (pixel != ret_color->pixel),
108            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
109            pixel, ret_color->red, ret_color->green, 
110            ret_color->blue, ret_color->pixel);
111
112   /* Test 2: Set color defined by (10,20,30)*/
113   red = 10;
114   green = 20;
115   blue = 30;
116   pixel = 10;
117
118   b_color->red=red;
119   b_color->green=green;
120   b_color->blue=blue;
121   b_color->pixel=pixel;
122     
123   hildon_color_button_set_color(color_button,b_color);
124   hildon_color_button_get_color(color_button, ret_color);
125
126   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
127            (blue != ret_color->blue) || (pixel != ret_color->pixel),
128            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
129            pixel, ret_color->red, ret_color->green, 
130            ret_color->blue, ret_color->pixel);
131
132   /* Test 3: Set color defined by (20,10,30)*/
133   red = 20;
134   green = 10;
135   blue = 30;
136   pixel = 10;
137
138   b_color->red=red;
139   b_color->green=green;
140   b_color->blue=blue;
141   b_color->pixel=pixel;
142     
143   hildon_color_button_set_color(color_button,b_color);
144   hildon_color_button_get_color(color_button, ret_color);
145
146   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
147            (blue != ret_color->blue) || (pixel != ret_color->pixel),
148            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
149            pixel, ret_color->red, ret_color->green, 
150            ret_color->blue, ret_color->pixel);
151
152   /* Test 4: Set color defined by (30,10,20)*/
153   red = 30;
154   green = 10;
155   blue = 20;
156   pixel = 20;
157
158   b_color->red=red;
159   b_color->green=green;
160   b_color->blue=blue;
161   b_color->pixel=pixel;
162     
163   hildon_color_button_set_color(color_button,b_color);
164   hildon_color_button_get_color(color_button, ret_color);
165
166   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
167            (blue != ret_color->blue) || (pixel != ret_color->pixel),
168            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
169            pixel, ret_color->red, ret_color->green, 
170            ret_color->blue, ret_color->pixel);
171     
172   if (b_color)
173           gdk_color_free(b_color);
174
175   if (ret_color)
176           gdk_color_free(ret_color);
177 }
178 END_TEST
179
180 /**
181  * Purpose: Check that a limits values on color components can be set safely.
182  * Cases considered:
183  *    - Set color defined by (0, 0, 0) and pixel = 0
184  *    - Set color defined by (G_MAXUINT16, G_MAXUINT16, G_MAXUINT16) and pixel = G_MAXUINT32
185  *    - Set color defined by (G_MAXUINT16, 0, 0) and pixel = G_MAXUINT32
186  *    - Set color defined by (0, G_MAXUINT16, 0) and pixel = 0
187  *    - Set color defined by (0, 0, G_MAXUINT16) and pixel = G_MAXUINT32
188  */
189
190 START_TEST (test_set_color_limits)
191 {
192   GdkColor color;
193   GdkColor * b_color;
194   GdkColor * ret_color = g_new (GdkColor, 1);
195   guint red;
196   guint green;
197   guint blue;
198   guint pixel;
199
200   b_color = gdk_color_copy(&color);
201
202   /* Test 1: Set color defined by (0,0,0)*/
203   red = 0;
204   green = 0;
205   blue = 0;
206   pixel = 0;
207
208   b_color->red=red;
209   b_color->green=green;
210   b_color->blue=blue;
211   b_color->pixel=pixel;
212
213   hildon_color_button_set_color(color_button,b_color);
214   hildon_color_button_get_color(color_button, ret_color);
215
216   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
217            (blue != ret_color->blue) || (pixel != ret_color->pixel),
218            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
219            pixel, ret_color->red, ret_color->green, 
220            ret_color->blue, ret_color->pixel);
221
222   /* Test 2: Set color defined by (G_MAXUINT16,G_MAXUINT16,G_MAXUINT16)*/
223   red = G_MAXUINT16;
224   green = G_MAXUINT16;
225   blue = G_MAXUINT16;
226   pixel = G_MAXUINT32;
227
228   b_color->pixel=pixel;
229   b_color->red=red;
230   b_color->green=green;
231   b_color->blue=blue;
232     
233   hildon_color_button_set_color(color_button,b_color);
234   hildon_color_button_get_color(color_button, ret_color);
235
236   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
237            (blue != ret_color->blue) || (pixel != ret_color->pixel),
238            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
239            pixel, ret_color->red, ret_color->green, 
240            ret_color->blue, ret_color->pixel);
241
242   /* Test 3: Set color defined by (G_MAXUINT16,0,0)*/
243   red = G_MAXUINT16;
244   green = 0;
245   blue = 0;
246   pixel = G_MAXUINT32;
247
248   b_color->red=red;
249   b_color->green=green;
250   b_color->blue=blue;
251   b_color->pixel=pixel;
252
253   hildon_color_button_set_color(color_button,b_color);
254   hildon_color_button_get_color(color_button, ret_color);
255
256   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
257            (blue != ret_color->blue) || (pixel != ret_color->pixel),
258            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
259            pixel, ret_color->red, ret_color->green, 
260            ret_color->blue, ret_color->pixel);
261
262   /* Test 4: Set color defined by (0,G_MAXUINT16,0)*/
263   red = 0;
264   green = G_MAXUINT16;
265   blue = 0;
266   pixel = 0;
267
268   b_color->red=red;
269   b_color->green=green;
270   b_color->blue=blue;
271   b_color->pixel=pixel;
272         
273   hildon_color_button_set_color(color_button,b_color);
274   hildon_color_button_get_color(color_button, ret_color);
275
276   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
277            (blue != ret_color->blue) || (pixel != ret_color->pixel),
278            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
279            pixel, ret_color->red, ret_color->green, 
280            ret_color->blue, ret_color->pixel);
281
282   /* Test 5: Set color defined by (0,0,G_MAXUINT16)*/
283   red = 0;
284   green = 0;
285   blue = G_MAXUINT16;
286   pixel = G_MAXUINT32;
287
288   b_color->red=red;
289   b_color->green=green;
290   b_color->blue=blue;
291   b_color->pixel=pixel;
292
293   hildon_color_button_set_color(color_button,b_color);
294   hildon_color_button_get_color(color_button, ret_color);
295
296   fail_if ((red != ret_color->red) || (green != ret_color->green) || 
297            (blue != ret_color->blue) || (pixel != ret_color->pixel),
298            "hildon-color-button: The returned color for the pixel %d (returned %d) in RGB is %d/%d/%d and should be %d/%d/%d",
299            pixel, ret_color->red, ret_color->green, 
300            ret_color->blue, ret_color->pixel);
301     
302   if (b_color) 
303           gdk_color_free(b_color);
304
305   if (ret_color)
306           gdk_color_free(ret_color);
307 }
308 END_TEST
309
310 /**
311  * Purpose: Check that a limits values on color components can be set safely.
312  * Cases considered:
313  *    - Set color defined by (0, 0, 0) on NULL object.
314  *    - Get color from NULL object.
315  *    - Set color defined by (0, 0, 0) on GtkHBox object.
316  *    - Get a color from GtkHBox object.
317  */
318
319 START_TEST (test_set_color_invalid)
320 {
321   GdkColor color;
322   GdkColor * b_color;
323   GdkColor * ret_color = g_new (GdkColor, 1);
324   GtkWidget *aux_object = NULL;
325   guint red;
326   guint green;
327   guint blue;
328     
329   b_color = gdk_color_copy(&color);
330
331   red = 0;
332   green = 0;
333   blue = 0;
334
335   b_color->red=red;
336   b_color->green=green;
337   b_color->blue=blue;
338   b_color->pixel=0;
339
340   /* Test 1: Set color defined by (0,0,0) on NULL object*/
341   hildon_color_button_set_color(NULL,b_color);
342
343   /* Test 2: Get color from NULL object*/
344   hildon_color_button_get_color(NULL, ret_color);    
345    
346   /* Test 3: Set color defined by (0, 0, 0) on GtkHBox object. */
347   aux_object = gtk_hbox_new (TRUE, 0);
348   hildon_color_button_set_color((HildonColorButton *) (aux_object), b_color);
349   gdk_color_free(b_color);
350
351   /* Test 4: Get color from GtkHBox object. */
352   ret_color->red = 99;
353   ret_color->green = 99;
354   ret_color->blue = 99;
355   hildon_color_button_get_color((HildonColorButton *) (aux_object), ret_color);
356     
357   if (ret_color->red != 99      || 
358       ret_color->green != 99    ||
359       ret_color->blue != 99)
360     {
361       gtk_widget_destroy(aux_object);
362       fail ("hildon-color-button: get_color must not modify the color when launched on invalid widget");
363     }
364   
365   gtk_widget_destroy(aux_object);
366 }
367 END_TEST
368
369 /* ---------- Suite creation ---------- */
370
371 Suite *create_hildon_color_button_suite()
372 {
373   /* Create the suite */
374   Suite *s = suite_create("HildonColorButton");
375
376   /* Create test cases */
377   TCase *tc1 = tcase_create("set_color");
378
379   /* Create test case for hildon_color_button_set_color and add it to the suite */
380   tcase_add_checked_fixture(tc1, fx_setup_default_color_button, fx_teardown_default_color_button);
381   tcase_add_test(tc1, test_set_color_regular);
382   tcase_add_test(tc1, test_set_color_limits);
383   tcase_add_test(tc1, test_set_color_invalid);
384   suite_add_tcase(s, tc1);
385
386   /* Return created suite */
387   return s;             
388 }