Updating the copyright headers.
[hildon] / tests / check-hildon-color-chooser.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.
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 #include <stdlib.h>
25 #include <check.h>
26 #include <gtk/gtkmain.h>
27 #include <glib/gprintf.h>
28 #include "test_suites.h"
29 #include "check_utils.h"
30
31 #include "hildon-color-chooser.h"
32 #include "hildon-window.h"
33
34 #define MAX_COLOR 65535
35
36 /* -------------------- Fixtures -------------------- */
37 static HildonColorChooser *color_chooser = NULL;
38 static GtkWindow * cs_window;
39
40 static void 
41 fx_setup_default_color_chooser ()
42 {
43   int argc = 0;
44   gtk_init(&argc, NULL);
45   
46   cs_window = GTK_WINDOW(create_test_window());
47
48   /* Check window object has been created properly */
49   fail_if(!HILDON_IS_WINDOW(cs_window),
50          "hildon-color-chooser: Window creation failed.");  
51
52   color_chooser = HILDON_COLOR_CHOOSER(hildon_color_chooser_new());
53   /* Check that the color chooser object has been created properly */
54   fail_if(!HILDON_IS_COLOR_CHOOSER(color_chooser),
55           "hildon-color-chooser: Creation failed.");  
56
57   gtk_container_add (GTK_CONTAINER(cs_window), GTK_WIDGET (color_chooser));
58
59   show_all_test_window(GTK_WIDGET(cs_window));
60   show_all_test_window(GTK_WIDGET(color_chooser));
61
62 }
63
64 static void 
65 fx_teardown_default_color_chooser ()
66 {    
67   gtk_widget_destroy (GTK_WIDGET (color_chooser)); 
68   gtk_widget_destroy (GTK_WIDGET (cs_window));
69  
70 }
71
72 /* -------------------- Test cases -------------------- */
73
74 /* ----- Test case for set_color -----*/
75
76 /**
77  * Purpose: Check that regular base colors are set and get properly
78  * Cases considered:
79  *    - Set the color #33CC33.
80  */
81 START_TEST (test_set_color_regular)
82 {
83   GdkColor color;
84   GdkColor * b_color;
85   GdkColor * ret_color = g_new (GdkColor, 1);    
86    
87   /* Test 1: Set the color #33CC33*/
88   gdk_color_parse( "#33CC33", &color);
89   b_color = gdk_color_copy(&color);
90
91   hildon_color_chooser_set_color(color_chooser,b_color);
92   hildon_color_chooser_get_color(color_chooser, ret_color);    
93
94   fail_if ((b_color->red != ret_color->red) || (b_color->green != ret_color->green) || 
95            (b_color->blue != ret_color->blue),
96            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
97            ret_color->red, ret_color->green, ret_color->blue,
98            b_color->red, b_color->green, b_color->blue);
99
100   if (b_color)
101     gdk_color_free(b_color);
102
103   if (ret_color)
104     gdk_color_free(ret_color);
105 }
106 END_TEST
107
108 /**
109  * Purpose: Check that limits base colors are set and get properly
110  * Cases considered:
111  *    - Set and get base color defined by #000000.
112  *    - Set and get base color defined by #FFFFFF.
113  *    - Set and get base color defined by #FF0000.
114  *    - Set and get base color defined by #0000FF.
115  */
116 START_TEST (test_set_color_limits)
117 {
118   GdkColor color;
119   GdkColor * b_color;
120   GdkColor * ret_color = g_new (GdkColor, 1);
121
122   /* Check that the color chooser object has been created properly */
123   fail_if(!HILDON_IS_COLOR_CHOOSER(color_chooser),
124           "hildon-color-chooser: Creation failed.");   
125     
126   b_color = gdk_color_copy(&color);
127     
128   /* Test 1: Set the color #000000 */
129   gdk_color_parse( "#000000", &color);
130   b_color = gdk_color_copy(&color);
131
132   hildon_color_chooser_set_color(color_chooser,b_color);
133   hildon_color_chooser_get_color(color_chooser, ret_color);    
134
135   fail_if ((b_color->red != ret_color->red) || (b_color->green != ret_color->green) || 
136            (b_color->blue != ret_color->blue),
137            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
138            ret_color->red, ret_color->green, ret_color->blue,
139            b_color->red, b_color->green, b_color->blue);
140
141   gdk_color_free(b_color);
142
143
144   /* Test 2: Set the color #FFFFFF */
145   gdk_color_parse( "#000000", &color);
146   b_color = gdk_color_copy(&color);
147
148   hildon_color_chooser_set_color(color_chooser,b_color);
149   hildon_color_chooser_get_color(color_chooser, ret_color);    
150
151   fail_if ((b_color->red != ret_color->red) || (b_color->green != ret_color->green) || 
152            (b_color->blue != ret_color->blue),
153            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
154            ret_color->red, ret_color->green, ret_color->blue,
155            b_color->red, b_color->green, b_color->blue);
156
157   gdk_color_free(b_color);
158
159
160   /* Test 3: Set the color #FF0000 */
161   gdk_color_parse( "#FF0000", &color);
162   b_color = gdk_color_copy(&color);
163
164   hildon_color_chooser_set_color(color_chooser,b_color);
165   hildon_color_chooser_get_color(color_chooser, ret_color);    
166
167   fail_if ((b_color->red != ret_color->red) || (b_color->green != ret_color->green) || 
168            (b_color->blue != ret_color->blue),
169            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
170            ret_color->red, ret_color->green, ret_color->blue,
171            b_color->red, b_color->green, b_color->blue);
172
173   gdk_color_free(b_color);
174
175   /* Test 4: Set the color to #0000FF*/
176   gdk_color_parse( "#0000FF", &color);
177   b_color = gdk_color_copy(&color);
178
179   hildon_color_chooser_set_color(color_chooser,b_color);
180   hildon_color_chooser_get_color(color_chooser, ret_color);    
181
182   fail_if ((b_color->red != ret_color->red) || (b_color->green != ret_color->green) || 
183            (b_color->blue != ret_color->blue),
184            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
185            ret_color->red, ret_color->green, ret_color->blue,
186            b_color->red, b_color->green, b_color->blue);
187
188   if (b_color)
189     gdk_color_free(b_color);
190
191   if (ret_color)
192     gdk_color_free(ret_color);
193 }
194 END_TEST
195 /**
196  * Purpose: Check that invalid base colors are set and get properly
197  * Cases considered:
198  *    - Set and get base color defined by #00FFFF and pixel 0.
199  *    - Set and get base color defined by #0ABCDE and pixel 0.
200  *    - Set and get base color defined by #FF00FF and pixel 0.
201  *    - Set the color (65536,65536,65536).
202  *    - Set and get base color defined by NULL color.
203  *    - Set base color on NULL object.
204  *    - Get base color from NULL object.
205  */
206 START_TEST (test_set_color_invalid)
207 {
208   GdkColor color;
209   GdkColor * b_color;
210   GdkColor * ret_color = g_new (GdkColor, 1);
211   guint red;
212   guint green;
213   guint blue;
214
215   /* Check that the color chooser object has been created properly */
216   fail_if(!HILDON_IS_COLOR_CHOOSER(color_chooser),
217           "hildon-color-chooser: Creation failed.");
218    
219   b_color = gdk_color_copy(&color);
220     
221   /* Initialize color to check that invalid values doesn't set colors*/
222   gdk_color_parse( "#000000", &color);
223   b_color = gdk_color_copy(&color);
224
225   hildon_color_chooser_set_color(color_chooser,b_color);
226   gdk_color_free(b_color);
227
228   /* Test 1: Set the color #00FFFF*/
229   gdk_color_parse( "#00FFFF", &color);
230   b_color = gdk_color_copy(&color);
231
232   hildon_color_chooser_set_color(color_chooser,b_color);
233   hildon_color_chooser_get_color(color_chooser,ret_color);    
234     
235   fail_if ((0 != ret_color->red) || (0 != ret_color->green) || (0 != ret_color->blue),
236            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
237            ret_color->red, ret_color->green, ret_color->blue,
238            0, 0, 0);
239
240   gdk_color_free(b_color);
241
242   /* Test 2: Set the color #FFFFFF */
243   gdk_color_parse( "#0ABCDE", &color);
244   b_color = gdk_color_copy(&color);
245
246   hildon_color_chooser_set_color(color_chooser,b_color);
247   hildon_color_chooser_get_color(color_chooser,ret_color);    
248
249   fail_if ((0 != ret_color->red) || (0 != ret_color->green) || (0 != ret_color->blue),
250            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
251            ret_color->red, ret_color->green, ret_color->blue,
252            0, 0, 0);
253
254   gdk_color_free(b_color);
255
256   /* Test 2: Set the color #FF00FF */
257   gdk_color_parse( "#FF00FF", &color);
258   b_color = gdk_color_copy(&color);
259
260   hildon_color_chooser_set_color(color_chooser,b_color);
261   hildon_color_chooser_get_color(color_chooser,ret_color);    
262
263   fail_if ((0 != ret_color->red) || (0 != ret_color->green) || (0 != ret_color->blue),
264            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
265            ret_color->red, ret_color->green, ret_color->blue,
266            0, 0, 0);
267
268   gdk_color_free(b_color);
269
270   /* Test 3: Set the color (65536,65536,65536)*/
271   gdk_color_parse( "#000000", &color);
272   b_color = gdk_color_copy(&color);
273   red = MAX_COLOR+1;
274   green = MAX_COLOR+1;
275   blue = MAX_COLOR+1;
276     
277   b_color->red=red;
278   b_color->green=green;
279   b_color->blue=blue;
280     
281   hildon_color_chooser_set_color(color_chooser,b_color);
282   hildon_color_chooser_get_color(color_chooser, ret_color);    
283
284   fail_if ((0 != ret_color->red) || (0 != ret_color->green) || (0 != ret_color->blue),
285            "hildon-color-chooser: The returned RGB color is %i/%i/%i and should be %i/%i/%i",
286            ret_color->red, ret_color->green, ret_color->blue,
287            0, 0, 0);
288     
289   /* Test 5: Set the color NULL on color chooser*/
290   hildon_color_chooser_set_color(color_chooser,NULL);
291
292   /* Test 6: Set the color on NULL object*/
293   hildon_color_chooser_set_color(NULL,b_color);
294
295   /* Test 7: Get color from NULL object*/
296   hildon_color_chooser_get_color(NULL, ret_color);
297
298   if (b_color)
299     gdk_color_free(b_color);
300
301   if (ret_color)
302     gdk_color_free(ret_color);
303 }
304 END_TEST
305
306
307 /* ---------- Suite creation ---------- */
308
309 Suite *create_hildon_color_chooser_suite()
310 {
311   /* Create the suite */
312   Suite *s = suite_create("HildonColorChooser");
313
314   /* Create test case for hildon_color_chooser_set_color and add it to the suite */
315   TCase *tc1 = tcase_create("set_color");
316   tcase_add_checked_fixture(tc1, fx_setup_default_color_chooser, fx_teardown_default_color_chooser);
317   tcase_add_test(tc1, test_set_color_regular);
318   tcase_add_test(tc1, test_set_color_limits);
319   tcase_add_test(tc1, test_set_color_invalid);
320   suite_add_tcase (s, tc1);
321
322   /* Return created suite */
323   return s;             
324 }