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