Release 2.1.94
[hildon] / tests / check-hildon-get-password-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 <string.h>
27 #include <check.h>
28 #include <gtk/gtkmain.h>
29 #include "test_suites.h"
30 #include "check_utils.h"
31 #include <hildon/hildon-get-password-dialog.h>
32
33 /* -------------------- Fixtures -------------------- */
34
35 static HildonGetPasswordDialog *get_password_dialog = NULL;
36 static GtkWindow * gpd_window = NULL;
37
38 static void 
39 fx_setup_default_get_password_dialog ()
40 {
41   int argc = 0;
42
43   gtk_init(&argc, NULL);
44
45   gpd_window = GTK_WINDOW(create_test_window());
46
47   /* Check the auxiliary window is being created properly */
48   fail_if (!GTK_IS_WINDOW (gpd_window),
49           "hildon-get-password-dialog: Window creation failed.");
50
51   get_password_dialog = HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new(gpd_window, TRUE));
52   /* Check get password dialog object has been created properly */
53   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
54           "hildon-get-password-dialog: Creation failed.");
55
56   show_test_window(GTK_WIDGET(gpd_window));
57
58   show_test_window(GTK_WIDGET(get_password_dialog));
59
60 }
61
62 static void 
63 fx_teardown_default_get_password_dialog ()
64 {
65   gtk_widget_destroy(GTK_WIDGET(get_password_dialog));
66
67   gtk_widget_destroy(GTK_WIDGET(gpd_window));
68 }
69
70 /* -------------------- Test cases -------------------- */
71
72 /* ----- Test case for hildon_get_password_dialog_new_with_default -----*/
73
74 /**
75  * Purpose: Check that regular password values are set and get properly.
76  * Cases considered:
77  *    - Create new get password dialog with "test_password" password by default.
78  *    - Create new get password dialog with "" password by default.
79  *    - Create new get password dialog with "1234567890" password by default.
80  *    - Create new get password dialog with TEST_STRING password by default
81  */
82 START_TEST (test_new_with_default_regular)
83 {
84   const gchar * default_password=NULL;
85   const gchar * ret_password;
86   HildonGetPasswordDialog * default_get_password_dialog;
87
88   /* Test 1: Create new dialog with password "test_password" by default */
89   default_password="test_password";
90   default_get_password_dialog = 
91     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
92   fail_if (!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
93           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
94
95   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
96   fail_if (strcmp (default_password,ret_password) != 0,
97           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
98   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
99
100   /* Test 2: Create new dialog with password "" by default */
101   default_password="";
102   default_get_password_dialog = 
103     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
104   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
105           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
106
107   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);   
108   fail_if (strcmp (default_password,ret_password) != 0,
109            "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
110
111   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
112
113   /* Test 3: Create new dialog with password "1234567890" by default */
114   default_password="1234567890";
115     
116   default_get_password_dialog = 
117     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
118   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
119           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
120
121   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);   
122   fail_if(strcmp (default_password,ret_password)!=0,
123           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
124
125   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
126
127   /* Test 4: Create new dialog with password TEST_STRING by default */
128   default_password=TEST_STRING;
129     
130   default_get_password_dialog = 
131     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
132   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
133           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
134
135   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
136   fail_if (strcmp (default_password,ret_password) != 0,
137            "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
138
139   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
140 }
141 END_TEST
142
143 /**
144  * Purpose: Check that invalid password values are set and get properly.
145  * Cases considered:
146  *    - Create new NULL get password dialog.
147  *    - Create new dialog with NULL window by default
148  */
149 START_TEST (test_new_with_default_invalid)
150 {
151   const gchar * default_password="";
152   const gchar * ret_password;
153   HildonGetPasswordDialog * default_get_password_dialog;
154
155   /* Check get password dialog object has been created properly */
156   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
157           "hildon-get-password-dialog: Creation failed.");
158   fail_if (!GTK_IS_WINDOW (gpd_window),
159           "hildon-get-password-dialog: Window creation failed.");
160
161   /* Test 1: Create new dialog with NULL password by default */
162   default_password=NULL;
163   default_get_password_dialog = 
164     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
165   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
166           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
167
168   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
169   fail_if(strcmp ("",ret_password) != 0,
170           "hildon-get-password-dialog: Error setting null password. Returned password is \"%s\"",ret_password);
171   
172   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
173
174   /* Test 2: Create new dialog with NULL window by default */
175   default_get_password_dialog = 
176     HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(NULL,default_password,FALSE));
177   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
178           "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");
179
180   ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);
181   fail_if (ret_password == NULL,
182               "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
183
184   gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
185 }
186 END_TEST
187
188 /**
189  * Purpose: Check that regular "max characters" values are set and get properly.
190  * Cases considered:
191  *    - Set max characters to 10.
192  *    - Set max characters to G_MAXUINT16+1.
193  *
194  */
195 START_TEST (test_set_max_characters_regular)
196 {
197   gint max_chars;
198   gint ret_max_chars;
199   GValue value={0, };
200
201   /* Check get password dialog object has been created properly */
202   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
203           "hildon-get-password-dialog: Creation failed.");
204   fail_if (!GTK_IS_WINDOW (gpd_window),
205           "hildon-get-password-dialog: Window creation failed.");    
206
207   g_value_init (&value, G_TYPE_INT);
208
209   /* Test 1: Set max characters to 10 */
210   max_chars = 10;
211   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
212   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
213   ret_max_chars = g_value_get_int (&value);
214
215   fail_if(ret_max_chars!=max_chars,
216           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);
217
218   g_value_unset (&value);
219
220   /* Test 2: Set max characters to G_MAXUINT16+1 */
221   g_value_init (&value, G_TYPE_INT);
222   max_chars = G_MAXUINT16+1;
223   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
224   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
225   ret_max_chars = g_value_get_int (&value);
226
227   fail_if(ret_max_chars!=G_MAXUINT16,
228           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);
229
230   g_value_unset (&value);
231 }
232 END_TEST
233
234 /**
235  * Purpose: Check that limits "max characters" values are set and get properly.
236  * Cases considered:
237  *    - Set max characters to 1.
238  *    - Set max characters to G_MAXINT.
239  */
240 START_TEST (test_set_max_characters_limits)
241 {
242   gint max_chars;
243   gint ret_max_chars;
244   GValue value={0, };
245
246   /* Check get password dialog object has been created properly */
247   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
248           "hildon-get-password-dialog: Creation failed.");
249   fail_if (!GTK_IS_WINDOW (gpd_window),
250           "hildon-get-password-dialog: Window creation failed.");
251     
252   g_value_init (&value, G_TYPE_INT);
253
254   /* Test 1: Set max characters to 1 */
255   max_chars = 1;
256   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
257   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
258   ret_max_chars = g_value_get_int (&value);
259
260   fail_if(ret_max_chars!=max_chars,
261           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);
262
263   g_value_unset (&value);
264
265   /* Test 2: Set max characters to G_MAXINT */
266   g_value_init (&value, G_TYPE_INT);
267   max_chars = G_MAXINT;
268   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
269   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
270   ret_max_chars = g_value_get_int (&value);
271
272   fail_if(ret_max_chars!=G_MAXUINT16,
273           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %u",max_chars,ret_max_chars);
274
275   g_value_unset (&value);
276 }
277 END_TEST
278
279 /**
280  * Purpose: Check that limits "max characters" values are set and get properly.
281  * Cases considered:
282  *    - Set max characters to 0.
283  *    - Set max characters to -1.
284  *    - Set max characters on NULL object.
285  */
286 START_TEST (test_set_max_characters_invalid)
287 {
288   gint max_chars;
289   gint ret_max_chars;
290   GValue value={0, };
291
292   /* Check get password dialog object has been created properly */
293   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
294           "hildon-get-password-dialog: Creation failed.");
295   fail_if (!GTK_IS_WINDOW (gpd_window),
296           "hildon-get-password-dialog: Window creation failed.");
297     
298   g_value_init (&value, G_TYPE_INT);
299
300   /* Test 1: Set max characters to 0 */
301   max_chars = 0;
302   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
303   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
304   ret_max_chars = g_value_get_int (&value);
305
306   fail_if(ret_max_chars!=max_chars,
307           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);
308
309   g_value_unset (&value);
310
311   /* Test 2: Set max characters to -1 */
312   g_value_init (&value, G_TYPE_INT);
313   max_chars = -1;
314   hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
315   g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
316   ret_max_chars = g_value_get_int (&value);
317
318   fail_if(ret_max_chars!=0,
319           "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i and expected is 0",max_chars,ret_max_chars);
320   g_value_unset (&value);
321     
322   /* Test 2: Set max characters on NULL object */
323   max_chars = 1;
324   hildon_get_password_dialog_set_max_characters(NULL,max_chars);
325 }
326 END_TEST
327
328 /**
329  * Purpose: Check that regular password values are get properly.
330  * Cases considered:
331  *    - Get password "test_password" set by g_object_set_property.
332  *    - Get password "01234567890" set by g_object_set_property.
333  *    - Get password TEST_STRING set by g_object_set_property.
334  *    - Get password "" set by set by g_object_set_property.
335  */
336 START_TEST (test_get_password_regular)
337 {
338   const gchar * default_password="test_password";  
339   const gchar * ret_password;
340   GValue value={0, };
341
342   /* Check get password dialog object has been created properly */
343   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
344           "hildon-get-password-dialog: Creation failed.");
345   fail_if (!GTK_IS_WINDOW (gpd_window),
346           "hildon-get-password-dialog: Window creation failed.");
347     
348   /* Test 1: Get password "test_password" set by g_object_set_property. */
349   g_value_init (&value, G_TYPE_STRING);
350   g_value_set_string(&value,default_password);
351   g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
352   g_value_unset (&value);
353     
354   ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
355   fail_if(strcmp (default_password,ret_password)!=0,
356           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
357
358   /* Test 2: Get password "01234567890" set by g_object_set_property. */
359   default_password = "01234567890";
360
361   g_value_init (&value, G_TYPE_STRING);
362   g_value_set_string(&value,default_password);
363   g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
364   g_value_unset (&value);
365     
366   ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
367   fail_if(strcmp (default_password,ret_password)!=0,
368           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
369
370   /* Test 3: Get password TEST_STRING set by g_object_set_property. */
371   default_password = TEST_STRING;
372
373   g_value_init (&value, G_TYPE_STRING);
374   g_value_set_string(&value,default_password);
375   g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
376   g_value_unset (&value);
377     
378   ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
379   fail_if(strcmp (default_password,ret_password)!=0,
380           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
381
382   /* Test 4: Get password "" set by g_object_set_property. */
383   default_password = "";
384
385   g_value_init (&value, G_TYPE_STRING);
386   g_value_set_string(&value,default_password);
387   g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
388   g_value_unset (&value);
389     
390   ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
391   fail_if(strcmp (default_password,ret_password)!=0,
392           "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
393 }
394 END_TEST
395
396 /**
397  * Purpose: Check that limit password values are get properly.
398  * Cases considered:
399  *    - Get empty password set by g_object_set_property with NULL value.
400  *    - Get password from NULL object.
401  *
402  */
403 START_TEST (test_get_password_invalid)
404 {
405   const gchar * default_password=NULL;  
406   const gchar * ret_password;
407   GValue value={0, };
408
409   /* Check get password dialog object has been created properly */
410   fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
411           "hildon-get-password-dialog: Creation failed.");
412   fail_if (!GTK_IS_WINDOW (gpd_window),
413           "hildon-get-password-dialog: Window creation failed.");
414     
415   /* Test 1: Get password NULL set by g_object_set_property. */
416   default_password = NULL;
417   g_value_init (&value, G_TYPE_STRING);
418   g_value_set_string(&value,default_password);
419   g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
420   g_value_unset (&value);
421     
422   ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
423   fail_if(strcmp(ret_password,"")!=0,
424           "hildon-get-password-dialog: Returned password is not empty (%s)",ret_password);
425
426   /* Test 2: Get password from NULL object. */
427   ret_password = hildon_get_password_dialog_get_password(NULL);
428 }
429 END_TEST
430
431
432 /* ---------- Suite creation ---------- */
433 Suite *create_hildon_get_password_dialog_suite()
434 {
435   /* Create the suite */
436   Suite *s = suite_create("HildonGetPasswordDialog");
437
438   /* Create test cases */
439   TCase *tc1 = tcase_create("new_with_default");
440   TCase *tc2 = tcase_create("set_max_characters");
441   TCase *tc3 = tcase_create("get_password");
442
443   /* Create test case for hildon_get_password_dialog_new_with_default and add it to the suite */
444   tcase_add_checked_fixture(tc1, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
445   tcase_add_test(tc1, test_new_with_default_regular);
446   tcase_add_test(tc1, test_new_with_default_invalid);
447   suite_add_tcase (s, tc1);
448
449   /* Create test case for hildon_get_password_dialog_set_max_characters and add it to the suite */
450   tcase_add_checked_fixture(tc2, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
451   tcase_add_test(tc2, test_set_max_characters_regular);
452   tcase_add_test(tc2, test_set_max_characters_limits);
453   tcase_add_test(tc2, test_set_max_characters_invalid);
454   suite_add_tcase (s, tc2);
455
456   /* Create test case for hildon_get_password_dialog_get_password and add it to the suite */
457   tcase_add_checked_fixture(tc3, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
458   tcase_add_test(tc3, test_get_password_regular);
459   tcase_add_test(tc3, test_get_password_invalid);
460   suite_add_tcase (s, tc3);
461
462
463   /* Return created suite */
464   return s;             
465 }