aboutsummaryrefslogtreecommitdiff
path: root/tests/check-hildon-get-password-dialog.c
blob: 8349b1eee6340b8f7e0efca486645d0cc7258f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * This file is a part of hildon tests
 *
 * Copyright (C) 2006, 2007 Nokia Corporation, all rights reserved.
 *
 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <stdlib.h>
#include <string.h>
#include <check.h>
#include <gtk/gtkmain.h>
#include "test_suites.h"
#include "check_utils.h"
#include <hildon/hildon-get-password-dialog.h>

/* -------------------- Fixtures -------------------- */

static HildonGetPasswordDialog *get_password_dialog = NULL;
static GtkWindow * gpd_window = NULL;

static void 
fx_setup_default_get_password_dialog ()
{
  int argc = 0;

  gtk_init(&argc, NULL);

  gpd_window = GTK_WINDOW(create_test_window());

  /* Check the auxiliary window is being created properly */
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");

  get_password_dialog = HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new(gpd_window, TRUE));
  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");

  show_test_window(GTK_WIDGET(gpd_window));

  show_test_window(GTK_WIDGET(get_password_dialog));

}

static void 
fx_teardown_default_get_password_dialog ()
{
  gtk_widget_destroy(GTK_WIDGET(get_password_dialog));

  gtk_widget_destroy(GTK_WIDGET(gpd_window));
}

/* -------------------- Test cases -------------------- */

/* ----- Test case for hildon_get_password_dialog_new_with_default -----*/

/**
 * Purpose: Check that regular password values are set and get properly.
 * Cases considered:
 *    - Create new get password dialog with "test_password" password by default.
 *    - Create new get password dialog with "" password by default.
 *    - Create new get password dialog with "1234567890" password by default.
 *    - Create new get password dialog with TEST_STRING password by default
 */
START_TEST (test_new_with_default_regular)
{
  const gchar * default_password=NULL;
  const gchar * ret_password;
  HildonGetPasswordDialog * default_get_password_dialog;

  /* Test 1: Create new dialog with password "test_password" by default */
  default_password="test_password";
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
  fail_if (!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
  fail_if (strcmp (default_password,ret_password) != 0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));

  /* Test 2: Create new dialog with password "" by default */
  default_password="";
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);   
  fail_if (strcmp (default_password,ret_password) != 0,
	   "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));

  /* Test 3: Create new dialog with password "1234567890" by default */
  default_password="1234567890";
    
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);   
  fail_if(strcmp (default_password,ret_password)!=0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));

  /* Test 4: Create new dialog with password TEST_STRING by default */
  default_password=TEST_STRING;
    
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
  fail_if (strcmp (default_password,ret_password) != 0,
	   "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
}
END_TEST

/**
 * Purpose: Check that invalid password values are set and get properly.
 * Cases considered:
 *    - Create new NULL get password dialog.
 *    - Create new dialog with NULL window by default
 */
START_TEST (test_new_with_default_invalid)
{
  const gchar * default_password="";
  const gchar * ret_password;
  HildonGetPasswordDialog * default_get_password_dialog;

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");

  /* Test 1: Create new dialog with NULL password by default */
  default_password=NULL;
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(gpd_window,default_password,FALSE));
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);  
  fail_if(strcmp ("",ret_password) != 0,
	  "hildon-get-password-dialog: Error setting null password. Returned password is \"%s\"",ret_password);
  
  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));

  /* Test 2: Create new dialog with NULL window by default */
  default_get_password_dialog = 
    HILDON_GET_PASSWORD_DIALOG(hildon_get_password_dialog_new_with_default(NULL,default_password,FALSE));
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG(default_get_password_dialog),
	  "hildon-get-password-dialog: Creation failed with hildon_get_password_dialog_new_with_default");

  ret_password = hildon_get_password_dialog_get_password(default_get_password_dialog);
  fail_if (ret_password == NULL,
	      "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  gtk_widget_destroy(GTK_WIDGET(default_get_password_dialog));
}
END_TEST

/**
 * Purpose: Check that regular "max characters" values are set and get properly.
 * Cases considered:
 *    - Set max characters to 10.
 *    - Set max characters to G_MAXUINT16+1.
 *
 */
START_TEST (test_set_max_characters_regular)
{
  gint max_chars;
  gint ret_max_chars;
  GValue value={0, };

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");    

  g_value_init (&value, G_TYPE_INT);

  /* Test 1: Set max characters to 10 */
  max_chars = 10;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=max_chars,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);

  g_value_unset (&value);

  /* Test 2: Set max characters to G_MAXUINT16+1 */
  g_value_init (&value, G_TYPE_INT);
  max_chars = G_MAXUINT16+1;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=G_MAXUINT16,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);

  g_value_unset (&value);
}
END_TEST

/**
 * Purpose: Check that limits "max characters" values are set and get properly.
 * Cases considered:
 *    - Set max characters to 1.
 *    - Set max characters to G_MAXINT.
 */
START_TEST (test_set_max_characters_limits)
{
  gint max_chars;
  gint ret_max_chars;
  GValue value={0, };

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");
    
  g_value_init (&value, G_TYPE_INT);

  /* Test 1: Set max characters to 1 */
  max_chars = 1;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=max_chars,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);

  g_value_unset (&value);

  /* Test 2: Set max characters to G_MAXINT */
  g_value_init (&value, G_TYPE_INT);
  max_chars = G_MAXINT;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=G_MAXUINT16,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %u",max_chars,ret_max_chars);

  g_value_unset (&value);
}
END_TEST

/**
 * Purpose: Check that limits "max characters" values are set and get properly.
 * Cases considered:
 *    - Set max characters to 0.
 *    - Set max characters to -1.
 *    - Set max characters on NULL object.
 */
START_TEST (test_set_max_characters_invalid)
{
  gint max_chars;
  gint ret_max_chars;
  GValue value={0, };

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");
    
  g_value_init (&value, G_TYPE_INT);

  /* Test 1: Set max characters to 0 */
  max_chars = 0;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=max_chars,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i",max_chars,ret_max_chars);

  g_value_unset (&value);

  /* Test 2: Set max characters to -1 */
  g_value_init (&value, G_TYPE_INT);
  max_chars = -1;
  hildon_get_password_dialog_set_max_characters(get_password_dialog,max_chars);
  g_object_get_property(G_OBJECT (get_password_dialog),"max-characters",&value);
  ret_max_chars = g_value_get_int (&value);

  fail_if(ret_max_chars!=0,
	  "hildon-get-password-dialog: Set %i max chars incorrect. Return value is %i and expected is 0",max_chars,ret_max_chars);
  g_value_unset (&value);
    
  /* Test 2: Set max characters on NULL object */
  max_chars = 1;
  hildon_get_password_dialog_set_max_characters(NULL,max_chars);
}
END_TEST

/**
 * Purpose: Check that regular password values are get properly.
 * Cases considered:
 *    - Get password "test_password" set by g_object_set_property.
 *    - Get password "01234567890" set by g_object_set_property.
 *    - Get password TEST_STRING set by g_object_set_property.
 *    - Get password "" set by set by g_object_set_property.
 */
START_TEST (test_get_password_regular)
{
  const gchar * default_password="test_password";  
  const gchar * ret_password;
  GValue value={0, };

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");
    
  /* Test 1: Get password "test_password" set by g_object_set_property. */
  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string(&value,default_password);
  g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
  g_value_unset (&value);
    
  ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
  fail_if(strcmp (default_password,ret_password)!=0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  /* Test 2: Get password "01234567890" set by g_object_set_property. */
  default_password = "01234567890";

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string(&value,default_password);
  g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
  g_value_unset (&value);
    
  ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
  fail_if(strcmp (default_password,ret_password)!=0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  /* Test 3: Get password TEST_STRING set by g_object_set_property. */
  default_password = TEST_STRING;

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string(&value,default_password);
  g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
  g_value_unset (&value);
    
  ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
  fail_if(strcmp (default_password,ret_password)!=0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);

  /* Test 4: Get password "" set by g_object_set_property. */
  default_password = "";

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string(&value,default_password);
  g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
  g_value_unset (&value);
    
  ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
  fail_if(strcmp (default_password,ret_password)!=0,
	  "hildon-get-password-dialog: Default password and returned password are diferent (%s,%s)",default_password,ret_password);
}
END_TEST

/**
 * Purpose: Check that limit password values are get properly.
 * Cases considered:
 *    - Get empty password set by g_object_set_property with NULL value.
 *    - Get password from NULL object.
 *
 */
START_TEST (test_get_password_invalid)
{
  const gchar * default_password=NULL;  
  const gchar * ret_password;
  GValue value={0, };

  /* Check get password dialog object has been created properly */
  fail_if(!HILDON_IS_GET_PASSWORD_DIALOG (get_password_dialog), 
          "hildon-get-password-dialog: Creation failed.");
  fail_if (!GTK_IS_WINDOW (gpd_window),
          "hildon-get-password-dialog: Window creation failed.");
    
  /* Test 1: Get password NULL set by g_object_set_property. */
  default_password = NULL;
  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string(&value,default_password);
  g_object_set_property(G_OBJECT (get_password_dialog),"password",&value);
  g_value_unset (&value);
    
  ret_password = hildon_get_password_dialog_get_password(get_password_dialog);
  fail_if(strcmp(ret_password,"")!=0,
	  "hildon-get-password-dialog: Returned password is not empty (%s)",ret_password);

  /* Test 2: Get password from NULL object. */
  ret_password = hildon_get_password_dialog_get_password(NULL);
}
END_TEST


/* ---------- Suite creation ---------- */
Suite *create_hildon_get_password_dialog_suite()
{
  /* Create the suite */
  Suite *s = suite_create("HildonGetPasswordDialog");

  /* Create test cases */
  TCase *tc1 = tcase_create("new_with_default");
  TCase *tc2 = tcase_create("set_max_characters");
  TCase *tc3 = tcase_create("get_password");

  /* Create test case for hildon_get_password_dialog_new_with_default and add it to the suite */
  tcase_add_checked_fixture(tc1, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
  tcase_add_test(tc1, test_new_with_default_regular);
  tcase_add_test(tc1, test_new_with_default_invalid);
  suite_add_tcase (s, tc1);

  /* Create test case for hildon_get_password_dialog_set_max_characters and add it to the suite */
  tcase_add_checked_fixture(tc2, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
  tcase_add_test(tc2, test_set_max_characters_regular);
  tcase_add_test(tc2, test_set_max_characters_limits);
  tcase_add_test(tc2, test_set_max_characters_invalid);
  suite_add_tcase (s, tc2);

  /* Create test case for hildon_get_password_dialog_get_password and add it to the suite */
  tcase_add_checked_fixture(tc3, fx_setup_default_get_password_dialog, fx_teardown_default_get_password_dialog);
  tcase_add_test(tc3, test_get_password_regular);
  tcase_add_test(tc3, test_get_password_invalid);
  suite_add_tcase (s, tc3);


  /* Return created suite */
  return s;             
}