2 * @file utest_userapi.h
3 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
4 * @date Started on June 24 2008
6 * @brief Implementation module for user api unit tests.
8 * This is a good place to look when learning to use libgcal. The following
9 * operations are covered:
11 * - getting all calendar events
13 * - adding a new calendar event
14 * - editing and deleting an event
15 * - querying for updated calendar events
19 #include "utest_userapi.h"
20 #include "gcalendar.h"
22 #include "gcal_parser.h"
27 /* I use this variable to exchange data between the contacts tests */
28 char *deleted_contact_id
= NULL
;
30 START_TEST (test_get_calendar
)
33 struct gcal_event_array event_array
;
36 gcal
= gcal_new(GCALENDAR
);
37 fail_if(gcal
== NULL
, "Failed constructing gcal object!");
39 result
= gcal_get_authentication(gcal
, "gcal4tester", "66libgcal");
40 fail_if(result
== -1, "Cannot authenticate!");
42 result
= gcal_get_events(gcal
, &event_array
);
43 fail_if(result
== -1, "Failed downloading events!");
44 fail_if(event_array
.length
< 1, "gcal4tester must have at least"
48 gcal_cleanup_events(&event_array
);
55 START_TEST (test_access_calendar
)
58 struct gcal_event_array event_array
;
64 gcal
= gcal_new(GCALENDAR
);
65 result
= gcal_get_authentication(gcal
, "gcal4tester", "66libgcal");
66 result
= gcal_get_events(gcal
, &event_array
);
68 /* Access events properties */
69 for (i
= 0; i
< event_array
.length
; ++i
) {
71 /* Access i-nth calendar event */
72 event
= gcal_event_element(&event_array
, i
);
74 /* Common fields between calendar and contacts are
75 * of type 'gcal_entry'
77 ptr
= gcal_event_get_id(event
);
78 ptr
= gcal_event_get_updated(event
);
79 ptr
= gcal_event_get_title(event
);
80 ptr
= gcal_event_get_url(event
);
82 fail_if(ptr
== NULL
, "Can't get edit url!");
84 /* This are the fields unique to calendar events */
85 ptr
= gcal_event_get_content(event
);
86 ptr
= gcal_event_get_recurrent(event
);
87 ptr
= gcal_event_get_start(event
);
88 ptr
= gcal_event_get_end(event
);
89 ptr
= gcal_event_get_where(event
);
90 ptr
= gcal_event_get_status(event
);
93 /* This code block is for testing overflow only! Please dont use
96 ptr
= gcal_event_get_id(gcal_event_element(&event_array
,
98 fail_if(ptr
!= NULL
, "Getting field must fail!");
99 ptr
= gcal_event_get_updated(gcal_event_element(&event_array
,
100 event_array
.length
));
101 fail_if(ptr
!= NULL
, "Getting field must fail!");
102 ptr
= gcal_event_get_title(gcal_event_element(&event_array
,
103 event_array
.length
));
104 fail_if(ptr
!= NULL
, "Getting field must fail!");
105 ptr
= gcal_event_get_url(gcal_event_element(&event_array
,
106 event_array
.length
));
107 fail_if(ptr
!= NULL
, "Getting field must fail!");
108 ptr
= gcal_event_get_content(gcal_event_element(&event_array
,
109 event_array
.length
));
110 fail_if(ptr
!= NULL
, "Getting field must fail!");
111 ptr
= gcal_event_get_recurrent(gcal_event_element(&event_array
,
112 event_array
.length
));
113 fail_if(ptr
!= NULL
, "Getting field must fail!");
114 ptr
= gcal_event_get_start(gcal_event_element(&event_array
,
115 event_array
.length
));
116 fail_if(ptr
!= NULL
, "Getting field must fail!");
117 ptr
= gcal_event_get_end(gcal_event_element(&event_array
,
118 event_array
.length
));
119 fail_if(ptr
!= NULL
, "Getting field must fail!");
120 ptr
= gcal_event_get_where(gcal_event_element(&event_array
,
121 event_array
.length
));
122 fail_if(ptr
!= NULL
, "Getting field must fail!");
123 ptr
= gcal_event_get_status(gcal_event_element(&event_array
,
124 event_array
.length
));
125 fail_if(ptr
!= NULL
, "Getting field must fail!");
129 gcal_cleanup_events(&event_array
);
135 START_TEST (test_oper_event_event
)
141 /* Create a new event object */
142 event
= gcal_event_new(NULL
);
143 fail_if (!event
, "Cannot construct event object!");
144 gcal_event_set_title(event
, "A new event");
145 gcal_event_set_content(event
, "Here goes the description");
146 gcal_event_set_start(event
, "2008-06-24T16:00:00Z");
147 gcal_event_set_end(event
, "2008-06-24T18:00:00Z");
148 gcal_event_set_where(event
, "A nice place for a meeting");
150 /* Create a gcal object and authenticate */
151 gcal
= gcal_new(GCALENDAR
);
152 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
153 fail_if(result
== -1, "Failed getting authentication");
155 /* Add a new event */
156 result
= gcal_add_event(gcal
, event
);
157 fail_if(result
== -1, "Failed adding a new event!");
160 /* Edit this event */
161 gcal_event_set_title(event
, "Changing the title");
162 result
= gcal_update_event(gcal
, event
);
163 fail_if(result
== -1, "Failed editing event!");
165 /* Delete this event (note: google doesn't really deletes
166 * the event, but set its status to 'cancelled' and keeps
167 * then for nearly 4 weeks).
169 result
= gcal_erase_event(gcal
, event
);
170 fail_if(result
== -1, "Failed deleting event!");
173 gcal_event_delete(event
);
178 START_TEST (test_query_event_updated
)
181 struct gcal_event_array event_array
;
184 /* Previous test added/edited/deleted an event with this title */
185 char *title
= "Changing the title";
187 gcal
= gcal_new(GCALENDAR
);
188 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
190 /* This will query for all updated events (fall in this category
191 * added/deleted/updated events) starting for 06:00Z UTC of today).
193 result
= gcal_get_updated_events(gcal
, &event_array
, NULL
);
194 fail_if(result
== -1, "Failed downloading updated events!");
195 fail_if(event_array
.length
< 1, "If previous test was ok, it must"
196 " return at least one updated event!");
198 /* Google returns the last updated event first */
199 event
= gcal_event_element(&event_array
, 0);
200 if (gcal_event_is_deleted(event
)) {
201 if (gcal_event_get_title(event
))
202 result
= strcmp(gcal_event_get_title(event
), title
);
205 fail_if(result
!= 0, "Cannot locate event!");
208 gcal_cleanup_events(&event_array
);
214 START_TEST (test_get_contacts
)
217 struct gcal_contact_array contact_array
;
220 gcal
= gcal_new(GCONTACT
);
221 fail_if(gcal
== NULL
, "Failed constructing gcal object!");
223 result
= gcal_get_authentication(gcal
, "gcal4tester", "66libgcal");
224 fail_if(result
== -1, "Cannot authenticate!");
226 result
= gcal_get_contacts(gcal
, &contact_array
);
227 fail_if(result
== -1, "Failed downloading contacts!");
228 fail_if(contact_array
.length
!= 3, "gcal4tester must have only"
232 gcal_cleanup_contacts(&contact_array
);
239 START_TEST (test_access_contacts
)
242 struct gcal_contact_array contact_array
;
243 gcal_contact_t contact
;
251 gcal
= gcal_new(GCONTACT
);
252 result
= gcal_get_authentication(gcal
, "gcal4tester", "66libgcal");
253 result
= gcal_get_contacts(gcal
, &contact_array
);
255 /* Access events properties */
256 for (i
= 0; i
< contact_array
.length
; ++i
) {
258 /* Access i-nth calendar event */
259 contact
= gcal_contact_element(&contact_array
, i
);
261 /* Common fields between calendar and contacts are
262 * of type 'gcal_entry'
264 ptr
= gcal_contact_get_id(contact
);
265 ptr
= gcal_contact_get_updated(contact
);
266 /* Tip: it *is* valid a contact have no name. */
267 ptr
= gcal_contact_get_title(contact
);
268 ptr
= gcal_contact_get_url(contact
);
270 fail_if(ptr
== NULL
, "Can't get edit url!");
272 /* This are the fields unique to calendar events */
273 j
= gcal_contact_get_emails_count(contact
);
274 j
= gcal_contact_get_pref_email(contact
);
275 ptr
= gcal_contact_get_email_address(contact
, 0);
276 get
= gcal_contact_get_email_address_type(contact
, 0);
277 ptr
= gcal_contact_get_content(contact
);
278 ptr
= gcal_contact_get_organization(contact
);
279 ptr
= gcal_contact_get_profission(contact
);
280 ptr
= gcal_contact_get_im(contact
);
281 j
= gcal_contact_get_phone_numbers_count(contact
);
282 ptr
= gcal_contact_get_phone_number(contact
, 0);
283 gpt
= gcal_contact_get_phone_number_type(contact
, 0);
284 ptr
= gcal_contact_get_address(contact
);
288 /* This code block is for testing overflow only! Please dont use
291 ptr
= gcal_contact_get_id(gcal_contact_element(&contact_array
,
292 contact_array
.length
));
293 fail_if(ptr
!= NULL
, "Getting field must fail!");
294 ptr
= gcal_contact_get_updated(gcal_contact_element(&contact_array
,
295 contact_array
.length
));
296 fail_if(ptr
!= NULL
, "Getting field must fail!");
297 ptr
= gcal_contact_get_title(gcal_contact_element(&contact_array
,
298 contact_array
.length
));
299 fail_if(ptr
!= NULL
, "Getting field must fail!");
300 ptr
= gcal_contact_get_url(gcal_contact_element(&contact_array
,
301 contact_array
.length
));
302 fail_if(ptr
!= NULL
, "Getting field must fail!");
303 ptr
= gcal_contact_get_email_address(gcal_contact_element(&contact_array
,
304 contact_array
.length
), 0);
305 fail_if(ptr
!= NULL
, "Getting field must fail!");
306 ptr
= gcal_contact_get_content(gcal_contact_element(&contact_array
,
307 contact_array
.length
));
308 fail_if(ptr
!= NULL
, "Getting field must fail!");
309 ptr
= gcal_contact_get_organization(gcal_contact_element(&contact_array
,
310 contact_array
.length
));
311 fail_if(ptr
!= NULL
, "Getting field must fail!");
312 ptr
= gcal_contact_get_profission(gcal_contact_element(&contact_array
,
313 contact_array
.length
));
314 fail_if(ptr
!= NULL
, "Getting field must fail!");
315 ptr
= gcal_contact_get_im(gcal_contact_element(&contact_array
,
316 contact_array
.length
));
317 fail_if(ptr
!= NULL
, "Getting field must fail!");
318 ptr
= gcal_contact_get_phone_number(gcal_contact_element(&contact_array
,
319 contact_array
.length
), 0);
320 fail_if(ptr
!= NULL
, "Getting field must fail!");
321 ptr
= gcal_contact_get_address(gcal_contact_element(&contact_array
,
322 contact_array
.length
));
323 fail_if(ptr
!= NULL
, "Getting field must fail!");
327 gcal_cleanup_contacts(&contact_array
);
333 START_TEST (test_oper_contact
)
336 gcal_contact_t contact
;
339 /* Create a new contact object */
340 contact
= gcal_contact_new(NULL
);
341 fail_if (!contact
, "Cannot construct contact object!");
342 // gcal_contact_set_title(contact, "John Doe");
344 contact
->structured_name_nr
= 1;
345 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"givenName","John");
346 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"familyName","Doe");
348 gcal_contact_delete_email_addresses(contact
);
349 gcal_contact_add_email_address(contact
, "john.doe@foo.bar.com", E_OTHER
, 1);
351 /* Create a gcal object and authenticate */
352 gcal
= gcal_new(GCONTACT
);
353 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
354 fail_if(result
== -1, "Failed getting authentication");
356 /* Add a new contact */
357 result
= gcal_add_contact(gcal
, contact
);
358 fail_if(result
== -1, "Failed adding a new contact!");
360 /* Edit this contact */
361 // gcal_contact_set_title(contact, "John 'The Generic' Doe");
363 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"givenName","John");
364 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"additionalName","'The Generic'");
365 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"familyName","Doe");
367 fail_if(result
== -1, "Failed editing contact!");
368 gcal_contact_delete_email_addresses(contact
);
369 gcal_contact_add_email_address(contact
, "john.super.doe@foo.bar.com", E_OTHER
, 1);
370 fail_if(result
== -1, "Failed editing contact!");
371 result
= gcal_update_contact(gcal
, contact
);
372 fail_if(result
== -1, "Failed uploading edited contact!");
374 /* Save this contact's ID to use it in the next test, where we
375 * search for updated contacts.
377 deleted_contact_id
= strdup(gcal_contact_get_id(contact
));
379 /* Delete this contact (note: google still keeps a deleted contact
380 * for nearly 4 weeks. Its possible to retrieve it using
381 * 'gcal_deleted(gcal, SHOW)' before downloading contacts)
383 result
= gcal_erase_contact(gcal
, contact
);
384 fail_if(result
== -1, "Failed deleting contact!");
387 gcal_contact_delete(contact
);
392 START_TEST (test_query_contact_updated
)
395 struct gcal_contact_array contact_array
;
396 gcal_contact_t contact
;
400 gcal
= gcal_new(GCONTACT
);
401 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
403 /* This will query for all updated contacts (fall in this category
404 * added/updated contacts) starting for 06:00Z UTC of today).
406 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
407 fail_if(result
== -1, "Failed downloading updated contacts!");
408 fail_if(contact_array
.length
> 3, "This user should not have more"
409 " than 3 updated contacts!");
411 /* Now we query for deleted contacts (previous test
412 * added/updated/deleted one contact, remember?)
414 tmp
= contact_array
.length
;
415 gcal_deleted(gcal
, SHOW
);
416 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
417 fail_if(result
== -1, "Failed downloading updated contacts!");
418 fail_if(contact_array
.length
<= tmp
, "If previous test was ok, it must"
419 " return one more contact!");
421 /* FIXME: Contacts doesn't return the last updated contact
422 * first when running with 'showdeleted'.
425 for (tmp
= 0; tmp
< contact_array
.length
; ++tmp
) {
426 contact
= gcal_contact_element(&contact_array
, tmp
);
427 /* only compare deleted contacts */
428 if (gcal_contact_is_deleted(contact
))
429 result
= strcmp(gcal_contact_get_id(contact
),
435 fail_if(result
!= 0, "Cannot locate contact!");
438 gcal_cleanup_contacts(&contact_array
);
445 START_TEST (test_contact_photo
)
448 gcal_contact_t contact
, tmp
;
450 struct gcal_contact_array contact_array
;
453 if (find_load_photo("/utests/images/gromit.jpg", &photo_data
, &result
))
454 fail_if(1, "Cannot load photo!");
456 /* Create a new contact object */
457 contact
= gcal_contact_new(NULL
);
458 fail_if (!contact
, "Cannot construct contact object!");
459 gcal_contact_set_title(contact
, "Gromit");
460 gcal_contact_delete_email_addresses(contact
);
461 gcal_contact_add_email_address(contact
, "gromit@wallace.com", E_OTHER
, 1);
462 fail_if(gcal_contact_set_photo(contact
, photo_data
, result
),
463 "Failed copying photo data");
465 /* Create a gcal object and authenticate */
466 gcal
= gcal_new(GCONTACT
);
467 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
468 fail_if(result
== -1, "Failed getting authentication");
470 /* Create a new contact with photo */
471 result
= gcal_add_contact(gcal
, contact
);
472 fail_if(result
== -1, "Failed adding a new contact!");
474 /* Update the contact: new title, photo, name, etc */
477 if (find_load_photo("/utests/images/hutch.png", &photo_data
, &result
))
478 fail_if(1, "Cannot load photo!");
479 gcal_contact_set_title(contact
, "hutch");
480 gcal_contact_delete_email_addresses(contact
);
481 gcal_contact_add_email_address(contact
, "hutch@wallace.com", E_OTHER
, 1);
482 fail_if(gcal_contact_set_photo(contact
, photo_data
, result
),
483 "Failed copying photo data");
484 result
= gcal_update_contact(gcal
, contact
);
485 fail_if(result
== -1, "Failed updating a contact!");
488 /* Retrieve updated contacts and test for contact photo */
489 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
490 fail_if(result
== -1, "Failed downloading updated contacts!");
491 /* fail_if(contact_array.length > 3, "This user should not have more" */
492 /* " than 3 updated contacts!"); */
494 /* Last updated contact (i.e. last) should have photo */
495 tmp
= gcal_contact_element(&contact_array
, (contact_array
.length
- 1));
496 fail_if(tmp
== NULL
, "Last contact must not be NULL!");
497 fail_if(gcal_contact_get_photo(tmp
) == NULL
,
498 "Last updated contact must have photo: %s",
499 gcal_contact_get_title(tmp
));
500 fail_if(gcal_contact_get_photolength(tmp
) < 2,
501 "Last updated contact photo length must be bigger");
504 result
= gcal_erase_contact(gcal
, contact
);
505 fail_if(result
== -1, "Failed deleting contact!");
508 gcal_contact_delete(contact
);
511 gcal_cleanup_contacts(&contact_array
);
516 START_TEST (test_url_sanity_calendar
)
520 struct gcal_event_array all_events
;
522 event
= gcal_event_new(NULL
);
524 gcal
= gcal_new(GCALENDAR
);
525 gcal_set_store_xml(gcal
, 1);
526 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
527 fail_if(result
== -1, "Cannot authenticate!");
529 char start
[] = "2009-03-26T11:00:00.000Z";
530 char end
[] = "2009-03-26T12:00:00.000Z";
531 gcal_event_set_title(event
, "Insanity in edit URL");
532 gcal_event_set_content(event
, "I'm bored of gcalendar bugs");
533 gcal_event_set_where(event
, "someplace");
534 gcal_event_set_start(event
, start
);
535 gcal_event_set_end(event
, end
);
537 fail_if((result
= gcal_add_event(gcal
, event
)) != 0,
538 "Failed adding new event!");
539 fail_if((result
= gcal_get_events(gcal
, &all_events
)) != 0,
540 "Failed retrieving all events!");
541 fail_if((strcmp(gcal_event_get_url(event
),
542 gcal_event_get_url(gcal_event_element(&all_events
, 0)))
543 != 0), "Edit url is different!");
545 /* fprintf(stderr, "add: %s\nretrieve: %s\n", gcal_event_get_url(event), */
546 /* gcal_event_get_url(gcal_event_element(&all_events, 0))); */
548 fail_if((result
= gcal_erase_event(gcal
, event
)) != 0,
549 "Failed deleting test event!");
551 gcal_event_delete(event
);
552 gcal_cleanup_events(&all_events
);
558 START_TEST (test_url_sanity_contact
)
561 gcal_contact_t contact
;
562 struct gcal_contact_array all_contacts
;
564 contact
= gcal_contact_new(NULL
);
566 gcal
= gcal_new(GCONTACT
);
567 gcal_set_store_xml(gcal
, 1);
568 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
569 fail_if(result
== -1, "Cannot authenticate!");
571 gcal_contact_set_title(contact
, "Insanity in edit URL");
572 gcal_contact_delete_email_addresses(contact
);
573 gcal_contact_add_email_address(contact
, "prooftest@add.get.com", E_OTHER
, 1);
575 fail_if((result
= gcal_add_contact(gcal
, contact
)) != 0,
576 "Failed adding new contact!");
577 fail_if((result
= gcal_get_contacts(gcal
, &all_contacts
)) != 0,
578 "Failed retrieving all contacts!");
579 fail_if((strcmp(gcal_contact_get_url(contact
),
580 gcal_contact_get_url(gcal_contact_element(&all_contacts
, all_contacts
.length
- 1)))
581 != 0), "Edit url is different!");
583 /* fprintf(stderr, "add: %s\nretrieve: %s\n", */
584 /* gcal_contact_get_url(contact), */
585 /* gcal_contact_get_url(gcal_contact_element(&all_contacts, all_contacts.length - 1))); */
587 fail_if((result
= gcal_erase_contact(gcal
, contact
)) != 0,
588 "Failed deleting test contact!");
590 gcal_contact_delete(contact
);
591 gcal_cleanup_contacts(&all_contacts
);
597 TCase
*gcal_userapi(void)
600 int timeout_seconds
= 60;
601 tc
= tcase_create("gcaluserapi");
602 tcase_set_timeout (tc
, timeout_seconds
);
604 tcase_add_test(tc
, test_get_calendar
);
605 tcase_add_test(tc
, test_access_calendar
);
606 tcase_add_test(tc
, test_oper_event_event
);
607 tcase_add_test(tc
, test_query_event_updated
);
608 tcase_add_test(tc
, test_get_contacts
);
609 tcase_add_test(tc
, test_access_contacts
);
610 tcase_add_test(tc
, test_oper_contact
);
611 tcase_add_test(tc
, test_query_contact_updated
);
612 tcase_add_test(tc
, test_contact_photo
);
613 tcase_add_test(tc
, test_url_sanity_calendar
);
614 tcase_add_test(tc
, test_url_sanity_contact
);