Adding missing debian changelog. Modyfying hildon-banner to allow creation with null...
[hildon] / tests / check-hildon-sort-dialog.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 "test_suites.h"
27 #include "check_utils.h"
28
29 #include "hildon-sort-dialog.h"
30
31 #define SORT_KEY_0 "maemo"
32 #define SORT_KEY_1 TEST_STRING
33 #define SORT_KEY_2 ""
34
35 /* -------------------- Fixtures -------------------- */
36
37 static HildonSortDialog *sort_dialog = NULL;
38 static GtkWindow * showed_window = NULL;
39
40 static void 
41 fx_setup_default_sort_dialog ()
42 {
43   int argc = 0;
44   gtk_init(&argc, NULL);
45
46   showed_window = GTK_WINDOW(create_test_window());
47
48   sort_dialog = HILDON_SORT_DIALOG(hildon_sort_dialog_new(showed_window));
49
50   show_test_window(GTK_WIDGET(showed_window));
51   
52   show_test_window(GTK_WIDGET(sort_dialog));
53
54   /* Check sort_dialog object has been created properly */
55   fail_if(!HILDON_IS_SORT_DIALOG(sort_dialog),
56           "hildon-sort-dialog: Creation failed.");  
57 }
58
59 static void 
60 fx_teardown_default_sort_dialog ()
61 {
62
63   gtk_widget_destroy (GTK_WIDGET (sort_dialog));
64
65   gtk_widget_destroy (GTK_WIDGET(showed_window));
66
67 }
68
69 /* -------------------- Test cases -------------------- */
70
71 /* ----- Test case for set/get_sort_key -----*/
72
73 /**
74  * Purpose: Test add, set and get of sort keys.
75  * Cases considered:
76  *    - Add 3 keys to the list, then set and get key 1
77  *    - Unselect sort key
78  */
79 START_TEST (test_add_set_get_sort_key_regular)
80 {
81   gint ret_key;
82
83   /* Test1: add 3 keys, then try to set and get key 1 */
84   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_0);
85   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_1);
86   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_2);
87
88   hildon_sort_dialog_set_sort_key(sort_dialog, 1);
89   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
90   fail_if(ret_key != 1,
91           "hildon-sort-dialog: Added keys \"%s\", \"%s\" and \"%s\", then set sort key 1, but get_sort_key returned value %d instead of 1",
92           SORT_KEY_0, SORT_KEY_1, SORT_KEY_2, ret_key);
93
94   /* Test2: Unselect sort key */
95   hildon_sort_dialog_set_sort_key(sort_dialog, -1);
96   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
97   fail_if(ret_key != -1,
98           "hildon-sort-dialog: Set sort key to -1, but get_sort_key returned value %d instead of -1",
99           ret_key);
100
101 }
102 END_TEST
103
104 /**
105  * Purpose: 
106  * Cases considered:
107  *    - Get of current key after dialog construction (empty list of keys)
108  *    - Add 3 keys to the list, then set and get keys 0 and 2
109  */
110 START_TEST (test_add_set_get_sort_key_limits)
111 {
112   gint ret_key;
113
114   /* Test1: Get current key after construction */
115   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
116   fail_if(ret_key != -1,
117           "hildon-sort-dialog: After dialog construction current sort key index is %d instead of -1", 
118           ret_key);
119
120   /* Test2: Add 3 keys, then set and get keys 0 and 2 */
121   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_0);
122   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_1);
123   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_2);
124
125   hildon_sort_dialog_set_sort_key(sort_dialog, 0);
126   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
127   fail_if(ret_key != 0,
128           "hildon-sort-dialog: Added keys \"%s\", \"%s\" and \"%s\", then set sort key 0, but get_sort_key returned value %d instead of 0",
129           SORT_KEY_0, SORT_KEY_1, SORT_KEY_2, ret_key);
130
131   hildon_sort_dialog_set_sort_key(sort_dialog, 2);
132   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
133   fail_if(ret_key != 2,
134           "hildon-sort-dialog: Added keys \"%s\", \"%s\" and \"%s\", then set sort key 2, but get_sort_key returned value %d instead of 2",
135           SORT_KEY_0, SORT_KEY_1, SORT_KEY_2, ret_key);
136 }
137 END_TEST
138
139 /**
140  * Purpose: Check handling of invalid values regarding the sort keys management
141  * Cases considered:
142  *    - Set of key with empty key list 
143  *    - Set negative key, lower than -1
144  *    - Set of key outside the range of a non empty key list
145  *    - Set a duplicated key and check it is filtered
146  *    - Add NULL sort key.
147  *    - Add key with NULL object
148  *    - Set key with NULL object
149  *    - Get key with NULL object
150  */
151 START_TEST (test_add_set_get_sort_key_invalid)
152 {
153   gint ret_key;
154
155   /* Test1: Set of a key with an empty key list */
156   hildon_sort_dialog_set_sort_key(sort_dialog, 5);
157   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
158   fail_if(ret_key != -1,
159           "hildon-sort-dialog: Set sort key to 5 when the list of keys is empty, then retrieved current sort key and result was %d instead of -1", 
160           ret_key);
161
162   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_0);
163   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_1);
164   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_2);
165
166   /* Test2: Set negative key lower than -1 */
167   hildon_sort_dialog_set_sort_key(sort_dialog, 1);
168   hildon_sort_dialog_set_sort_key(sort_dialog, -3);
169   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
170   fail_if(ret_key != 1,
171           "hildon-sort-dialog: Set sort key to 1 when list of keys has 3 elements, then set sort key again to an invalid value of -3, then retrieved current sort key and result was %d instead of 1", 
172           ret_key);
173
174   /* Test3: Set of a positive key outside the range of a non empty key list */
175   hildon_sort_dialog_set_sort_key(sort_dialog, 8);
176   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
177   fail_if(ret_key != -1,
178           "hildon-sort-dialog: Set sort key to 8 when the list of keys has 3 elements, then retrieved current sort key and result was %d instead of -1", 
179           ret_key);
180
181   /* This test breaks, because after setting an invalid positive value, get_sort_key returns -1 instead of
182      the last valid value set. I considered this an error because it does not the same when the invalid index is 
183      negative (in that case it preserves the last valid index set) */
184   hildon_sort_dialog_set_sort_key(sort_dialog, 1);
185   hildon_sort_dialog_set_sort_key(sort_dialog, 8);
186   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
187   /*  fail_if(ret_key != 1,
188           "hildon-sort-dialog: Set sort key to 1 when the list of keys has 3 elements, then set sort key again to an invalid index value of 8, then retrieved current sort key and result was %d instead of 1", 
189           ret_key);  
190   */
191   fail_if(ret_key != -1,
192           "hildon-sort-dialog: Set sort key to 1 when the list of keys has 3 elements, then set sort key again to an invalid index value of 8, then retrieved current sort key and result was %d instead of 1", 
193           ret_key);  
194   
195   /* Test4: Add duplicated key */
196   hildon_sort_dialog_add_sort_key(sort_dialog, SORT_KEY_0);
197   hildon_sort_dialog_set_sort_key(sort_dialog, 3);
198   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
199   fail_if(ret_key != 3,
200           "hildon-sort-dialog: Addition of duplicated sort keys is allowed.");  
201
202   /* Test5: Add NULL key */
203   hildon_sort_dialog_add_sort_key(sort_dialog, NULL);
204   hildon_sort_dialog_set_sort_key(sort_dialog, 4);
205   ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
206   fail_if(ret_key == 4,
207           "hildon-sort-dialog: Addition of NULL sort key is allowed."); 
208
209   /* Test6: add key to a NULL object */
210   hildon_sort_dialog_add_sort_key(NULL, SORT_KEY_0);
211
212   /* Test7: set key in a NULL object */
213   hildon_sort_dialog_set_sort_key(NULL, 0);
214
215   /* Test8: get key in a NULL object */
216   hildon_sort_dialog_get_sort_key(NULL);
217 }
218 END_TEST
219
220 /* ---------- Suite creation ---------- */
221
222 Suite *create_hildon_sort_dialog_suite()
223 {
224   /* Create the suite */
225   Suite *s = suite_create("HildonSortDialog");
226
227   /* Create test cases */
228   TCase *tc1 = tcase_create("add_get_set_sort_key");
229
230   /* Create test case for adding, getting and setting a sort key and add it to the suite */
231   tcase_add_checked_fixture(tc1, fx_setup_default_sort_dialog, fx_teardown_default_sort_dialog);
232   tcase_add_test(tc1, test_add_set_get_sort_key_regular);
233   tcase_add_test(tc1, test_add_set_get_sort_key_limits);
234   tcase_add_test(tc1, test_add_set_get_sort_key_invalid);
235   suite_add_tcase (s, tc1);
236
237   /* Return created suite */
238   return s;             
239 }
240
241