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