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
;
252 gcal
= gcal_new(GCONTACT
);
253 result
= gcal_get_authentication(gcal
, "gcal4tester", "66libgcal");
254 result
= gcal_get_contacts(gcal
, &contact_array
);
256 /* Access events properties */
257 for (i
= 0; i
< contact_array
.length
; ++i
) {
259 /* Access i-nth calendar event */
260 contact
= gcal_contact_element(&contact_array
, i
);
262 /* Common fields between calendar and contacts are
263 * of type 'gcal_entry'
265 ptr
= gcal_contact_get_id(contact
);
266 ptr
= gcal_contact_get_updated(contact
);
267 /* Tip: it *is* valid a contact have no name. */
268 ptr
= gcal_contact_get_title(contact
);
269 ptr
= gcal_contact_get_url(contact
);
271 fail_if(ptr
== NULL
, "Can't get edit url!");
273 /* This are the fields unique to calendar events */
274 j
= gcal_contact_get_emails_count(contact
);
275 j
= gcal_contact_get_pref_email(contact
);
276 ptr
= gcal_contact_get_email_address(contact
, 0);
277 get
= gcal_contact_get_email_address_type(contact
, 0);
278 ptr
= gcal_contact_get_content(contact
);
279 ptr
= gcal_contact_get_organization(contact
);
280 ptr
= gcal_contact_get_profission(contact
);
281 j
= gcal_contact_get_im_count(contact
);
282 ptr
= gcal_contact_get_im_address(contact
, 0);
283 ptr
= gcal_contact_get_im_protocol(contact
, 0);
284 git
= gcal_contact_get_im_type(contact
, 0);
285 j
= gcal_contact_get_phone_numbers_count(contact
);
286 ptr
= gcal_contact_get_phone_number(contact
, 0);
287 gpt
= gcal_contact_get_phone_number_type(contact
, 0);
288 ptr
= gcal_contact_get_address(contact
);
292 /* This code block is for testing overflow only! Please dont use
295 ptr
= gcal_contact_get_id(gcal_contact_element(&contact_array
,
296 contact_array
.length
));
297 fail_if(ptr
!= NULL
, "Getting field must fail!");
298 ptr
= gcal_contact_get_updated(gcal_contact_element(&contact_array
,
299 contact_array
.length
));
300 fail_if(ptr
!= NULL
, "Getting field must fail!");
301 ptr
= gcal_contact_get_title(gcal_contact_element(&contact_array
,
302 contact_array
.length
));
303 fail_if(ptr
!= NULL
, "Getting field must fail!");
304 ptr
= gcal_contact_get_url(gcal_contact_element(&contact_array
,
305 contact_array
.length
));
306 fail_if(ptr
!= NULL
, "Getting field must fail!");
307 ptr
= gcal_contact_get_email_address(gcal_contact_element(&contact_array
,
308 contact_array
.length
), 0);
309 fail_if(ptr
!= NULL
, "Getting field must fail!");
310 ptr
= gcal_contact_get_content(gcal_contact_element(&contact_array
,
311 contact_array
.length
));
312 fail_if(ptr
!= NULL
, "Getting field must fail!");
313 ptr
= gcal_contact_get_organization(gcal_contact_element(&contact_array
,
314 contact_array
.length
));
315 fail_if(ptr
!= NULL
, "Getting field must fail!");
316 ptr
= gcal_contact_get_profission(gcal_contact_element(&contact_array
,
317 contact_array
.length
));
318 fail_if(ptr
!= NULL
, "Getting field must fail!");
319 ptr
= gcal_contact_get_im_address(gcal_contact_element(&contact_array
,
320 contact_array
.length
), 0);
321 fail_if(ptr
!= NULL
, "Getting field must fail!");
322 ptr
= gcal_contact_get_phone_number(gcal_contact_element(&contact_array
,
323 contact_array
.length
), 0);
324 fail_if(ptr
!= NULL
, "Getting field must fail!");
325 ptr
= gcal_contact_get_address(gcal_contact_element(&contact_array
,
326 contact_array
.length
));
327 fail_if(ptr
!= NULL
, "Getting field must fail!");
330 gcal_cleanup_contacts(&contact_array
);
336 START_TEST (test_oper_contact
)
339 gcal_contact_t contact
;
342 /* Create a new contact object */
343 contact
= gcal_contact_new(NULL
);
344 fail_if (!contact
, "Cannot construct contact object!");
346 gcal_contact_set_title(contact
, "John Doe");
347 gcal_contact_set_email(contact
, "john.doe@foo.bar.com");
348 gcal_contact_add_email_address(contact
, "jonny@theman.com", E_OTHER
, 0);
349 gcal_contact_set_phone(contact
, "111-2222-3333-888");
351 contact
->structured_name_nr
= 1;
352 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"givenName","John");
353 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"familyName","Doe");
355 gcal_contact_delete_email_addresses(contact
);
356 gcal_contact_add_email_address(contact
, "john.doe@foo.bar.com", E_OTHER
, 1);
358 /* Create a gcal object and authenticate */
359 gcal
= gcal_new(GCONTACT
);
360 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
361 fail_if(result
== -1, "Failed getting authentication");
363 /* Add a new contact */
364 result
= gcal_add_contact(gcal
, contact
);
365 fail_if(result
== -1, "Failed adding a new contact!");
368 /* Tests integraty */
369 result
= strcmp(gcal_contact_get_phone(contact
), "111-2222-3333-888");
370 fail_if(result
!= 0, "Failed to extract phone from gcal_contact_t!");
373 /* Edit this contact */
374 // gcal_contact_set_title(contact, "John 'The Generic' Doe");
376 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"givenName","John");
377 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"additionalName","'The Generic'");
378 gcal_contact_set_structured_entry(contact
->structured_name
,0,1,"familyName","Doe");
380 fail_if(result
== -1, "Failed editing contact!");
381 gcal_contact_delete_email_addresses(contact
);
382 gcal_contact_set_email(contact
, "john.super.doe@foo.bar.com");
383 fail_if(result
== -1, "Failed editing contact!");
384 result
= gcal_update_contact(gcal
, contact
);
385 fail_if(result
== -1, "Failed uploading edited contact!");
387 /* Save this contact's ID to use it in the next test, where we
388 * search for updated contacts.
390 deleted_contact_id
= strdup(gcal_contact_get_id(contact
));
392 /* Delete this contact (note: google still keeps a deleted contact
393 * for nearly 4 weeks. Its possible to retrieve it using
394 * 'gcal_deleted(gcal, SHOW)' before downloading contacts)
396 result
= gcal_erase_contact(gcal
, contact
);
397 fail_if(result
== -1, "Failed deleting contact!");
400 gcal_contact_delete(contact
);
406 START_TEST (test_query_contact_updated
)
409 struct gcal_contact_array contact_array
;
410 gcal_contact_t contact
;
414 gcal
= gcal_new(GCONTACT
);
415 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
417 /* This will query for all updated contacts (fall in this category
418 * added/updated contacts) starting for 06:00Z UTC of today).
420 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
421 fail_if(result
== -1, "Failed downloading updated contacts!");
422 fail_if(contact_array
.length
> 3, "This user should not have more"
423 " than 3 updated contacts!");
425 /* Now we query for deleted contacts (previous test
426 * added/updated/deleted one contact, remember?)
428 tmp
= contact_array
.length
;
429 gcal_deleted(gcal
, SHOW
);
430 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
431 fail_if(result
== -1, "Failed downloading updated contacts!");
432 fail_if(contact_array
.length
<= tmp
, "If previous test was ok, it must"
433 " return one more contact!");
435 /* FIXME: Contacts doesn't return the last updated contact
436 * first when running with 'showdeleted'.
439 for (tmp
= 0; tmp
< contact_array
.length
; ++tmp
) {
440 contact
= gcal_contact_element(&contact_array
, tmp
);
441 /* only compare deleted contacts */
442 if (gcal_contact_is_deleted(contact
))
443 result
= strcmp(gcal_contact_get_id(contact
),
449 fail_if(result
!= 0, "Cannot locate contact!");
452 gcal_cleanup_contacts(&contact_array
);
459 START_TEST (test_contact_photo
)
462 gcal_contact_t contact
, tmp
;
464 struct gcal_contact_array contact_array
;
467 if (find_load_photo("/utests/images/gromit.jpg", &photo_data
, &result
))
468 fail_if(1, "Cannot load photo!");
470 /* Create a new contact object */
471 contact
= gcal_contact_new(NULL
);
472 fail_if (!contact
, "Cannot construct contact object!");
473 gcal_contact_set_title(contact
, "Gromit");
474 gcal_contact_add_email_address(contact
, "gromit@wallace.com", E_OTHER
, 1);
475 fail_if(gcal_contact_set_photo(contact
, photo_data
, result
),
476 "Failed copying photo data");
478 /* Create a gcal object and authenticate */
479 gcal
= gcal_new(GCONTACT
);
480 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
481 fail_if(result
== -1, "Failed getting authentication");
483 /* Create a new contact with photo */
484 result
= gcal_add_contact(gcal
, contact
);
485 fail_if(result
== -1, "Failed adding a new contact!");
487 /* Update the contact: new title, photo, name, etc */
490 if (find_load_photo("/utests/images/hutch.png", &photo_data
, &result
))
491 fail_if(1, "Cannot load photo!");
492 gcal_contact_set_title(contact
, "hutch");
493 gcal_contact_delete_email_addresses(contact
);
494 gcal_contact_add_email_address(contact
, "hutch@wallace.com", E_OTHER
, 1);
495 fail_if(gcal_contact_set_photo(contact
, photo_data
, result
),
496 "Failed copying photo data");
497 result
= gcal_update_contact(gcal
, contact
);
498 fail_if(result
== -1, "Failed updating a contact!");
501 /* Retrieve updated contacts and test for contact photo */
502 result
= gcal_get_updated_contacts(gcal
, &contact_array
, NULL
);
503 fail_if(result
== -1, "Failed downloading updated contacts!");
504 /* fail_if(contact_array.length > 3, "This user should not have more" */
505 /* " than 3 updated contacts!"); */
507 /* Last updated contact (i.e. last) should have photo */
508 tmp
= gcal_contact_element(&contact_array
, (contact_array
.length
- 1));
509 fail_if(tmp
== NULL
, "Last contact must not be NULL!");
510 fail_if(gcal_contact_get_photo(tmp
) == NULL
,
511 "Last updated contact must have photo: %s",
512 gcal_contact_get_title(tmp
));
513 fail_if(gcal_contact_get_photolength(tmp
) < 2,
514 "Last updated contact photo length must be bigger");
517 result
= gcal_erase_contact(gcal
, contact
);
518 fail_if(result
== -1, "Failed deleting contact!");
521 gcal_contact_delete(contact
);
524 gcal_cleanup_contacts(&contact_array
);
529 START_TEST (test_url_sanity_calendar
)
533 struct gcal_event_array all_events
;
535 event
= gcal_event_new(NULL
);
537 gcal
= gcal_new(GCALENDAR
);
538 gcal_set_store_xml(gcal
, 1);
539 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
540 fail_if(result
== -1, "Cannot authenticate!");
542 char start
[] = "2009-03-26T11:00:00.000Z";
543 char end
[] = "2009-03-26T12:00:00.000Z";
544 gcal_event_set_title(event
, "Insanity in edit URL");
545 gcal_event_set_content(event
, "I'm bored of gcalendar bugs");
546 gcal_event_set_where(event
, "someplace");
547 gcal_event_set_start(event
, start
);
548 gcal_event_set_end(event
, end
);
550 fail_if((result
= gcal_add_event(gcal
, event
)) != 0,
551 "Failed adding new event!");
552 fail_if((result
= gcal_get_events(gcal
, &all_events
)) != 0,
553 "Failed retrieving all events!");
554 fail_if((strcmp(gcal_event_get_url(event
),
555 gcal_event_get_url(gcal_event_element(&all_events
, 0)))
556 != 0), "Edit url is different!");
558 /* fprintf(stderr, "add: %s\nretrieve: %s\n", gcal_event_get_url(event), */
559 /* gcal_event_get_url(gcal_event_element(&all_events, 0))); */
561 fail_if((result
= gcal_erase_event(gcal
, event
)) != 0,
562 "Failed deleting test event!");
564 gcal_event_delete(event
);
565 gcal_cleanup_events(&all_events
);
571 START_TEST (test_url_sanity_contact
)
574 gcal_contact_t contact
;
575 struct gcal_contact_array all_contacts
;
577 contact
= gcal_contact_new(NULL
);
579 gcal
= gcal_new(GCONTACT
);
580 gcal_set_store_xml(gcal
, 1);
581 result
= gcal_get_authentication(gcal
, "gcalntester", "77libgcal");
582 fail_if(result
== -1, "Cannot authenticate!");
584 gcal_contact_set_title(contact
, "Insanity in edit URL");
585 gcal_contact_add_email_address(contact
, "prooftest@add.get.com", E_OTHER
, 1);
587 fail_if((result
= gcal_add_contact(gcal
, contact
)) != 0,
588 "Failed adding new contact!");
589 fail_if((result
= gcal_get_contacts(gcal
, &all_contacts
)) != 0,
590 "Failed retrieving all contacts!");
591 fail_if((strcmp(gcal_contact_get_url(contact
),
592 gcal_contact_get_url(gcal_contact_element(&all_contacts
, all_contacts
.length
- 1)))
593 != 0), "Edit url is different!");
595 /* fprintf(stderr, "add: %s\nretrieve: %s\n", */
596 /* gcal_contact_get_url(contact), */
597 /* gcal_contact_get_url(gcal_contact_element(&all_contacts, all_contacts.length - 1))); */
599 fail_if((result
= gcal_erase_contact(gcal
, contact
)) != 0,
600 "Failed deleting test contact!");
602 gcal_contact_delete(contact
);
603 gcal_cleanup_contacts(&all_contacts
);
609 TCase
*gcal_userapi(void)
612 int timeout_seconds
= 60;
613 tc
= tcase_create("gcaluserapi");
614 tcase_set_timeout (tc
, timeout_seconds
);
616 tcase_add_test(tc
, test_get_calendar
);
617 tcase_add_test(tc
, test_access_calendar
);
618 tcase_add_test(tc
, test_oper_event_event
);
619 tcase_add_test(tc
, test_query_event_updated
);
620 tcase_add_test(tc
, test_get_contacts
);
621 tcase_add_test(tc
, test_access_contacts
);
622 tcase_add_test(tc
, test_oper_contact
);
623 tcase_add_test(tc
, test_query_contact_updated
);
624 tcase_add_test(tc
, test_contact_photo
);
625 tcase_add_test(tc
, test_url_sanity_calendar
);
626 tcase_add_test(tc
, test_url_sanity_contact
);