2 * Copyright (C) 2006 Nokia Corporation.
4 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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.
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.
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
25 #include <gtk/gtkmain.h>
26 #include "test_suites.h"
27 #include "check_utils.h"
29 #include "hildon-sort-dialog.h"
31 #define SORT_KEY_0 "maemo"
32 #define SORT_KEY_1 TEST_STRING
35 /* -------------------- Fixtures -------------------- */
37 static HildonSortDialog *sort_dialog = NULL;
38 static GtkWindow * showed_window = NULL;
41 fx_setup_default_sort_dialog ()
44 gtk_init(&argc, NULL);
46 showed_window = GTK_WINDOW(create_test_window());
48 sort_dialog = HILDON_SORT_DIALOG(hildon_sort_dialog_new(showed_window));
50 show_test_window(GTK_WIDGET(showed_window));
52 show_test_window(GTK_WIDGET(sort_dialog));
54 /* Check sort_dialog object has been created properly */
55 fail_if(!HILDON_IS_SORT_DIALOG(sort_dialog),
56 "hildon-sort-dialog: Creation failed.");
60 fx_teardown_default_sort_dialog ()
63 gtk_widget_destroy (GTK_WIDGET (sort_dialog));
65 gtk_widget_destroy (GTK_WIDGET(showed_window));
69 /* -------------------- Test cases -------------------- */
71 /* ----- Test case for set/get_sort_key -----*/
74 * Purpose: Test add, set and get of sort keys.
76 * - Add 3 keys to the list, then set and get key 1
79 START_TEST (test_add_set_get_sort_key_regular)
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);
88 hildon_sort_dialog_set_sort_key(sort_dialog, 1);
89 ret_key = hildon_sort_dialog_get_sort_key(sort_dialog);
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);
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",
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
110 START_TEST (test_add_set_get_sort_key_limits)
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",
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);
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);
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);
140 * Purpose: Check handling of invalid values regarding the sort keys management
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
151 START_TEST (test_add_set_get_sort_key_invalid)
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",
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);
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",
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",
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",
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",
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.");
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.");
209 /* Test6: add key to a NULL object */
210 hildon_sort_dialog_add_sort_key(NULL, SORT_KEY_0);
212 /* Test7: set key in a NULL object */
213 hildon_sort_dialog_set_sort_key(NULL, 0);
215 /* Test8: get key in a NULL object */
216 hildon_sort_dialog_get_sort_key(NULL);
220 /* ---------- Suite creation ---------- */
222 Suite *create_hildon_sort_dialog_suite()
224 /* Create the suite */
225 Suite *s = suite_create("HildonSortDialog");
227 /* Create test cases */
228 TCase *tc1 = tcase_create("add_get_set_sort_key");
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);
237 /* Return created suite */