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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
|
/*
* 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.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <libebook/libebook.h>
#include "ebook-test-utils.h"
void
test_print (const gchar *format,
...)
{
va_list args;
const gchar *debug_string;
static gboolean debug_set = FALSE;
static gboolean debug = FALSE;
if (!debug_set) {
debug_string = g_getenv ("EDS_TEST_DEBUG");
if (debug_string) {
debug = (g_ascii_strtoll (debug_string, NULL, 10) >= 1);
}
debug_set = TRUE;
}
if (debug) {
va_start (args, format);
vprintf (format, args);
va_end (args);
}
}
gboolean
ebook_test_utils_callback_quit (gpointer user_data)
{
EBookTestClosure *closure = user_data;
g_main_loop_quit ((GMainLoop *) closure->user_data);
return FALSE;
}
static const gchar *args_data_dir = NULL;
void
ebook_test_utils_read_args (gint argc,
gchar **argv)
{
gint ii;
for (ii = 0; ii < argc; ii++) {
if (g_strcmp0 (argv[ii], "--data-dir") == 0) {
if (ii + 1 < argc)
args_data_dir = argv[ii + 1];
break;
}
}
}
gchar *
ebook_test_utils_new_vcard_from_test_case (const gchar *case_name)
{
gchar *filename;
gchar *case_filename;
GFile * file;
GError *error = NULL;
gchar *vcard;
case_filename = g_strdup_printf ("%s.vcf", case_name);
/* In the case of installed tests, they run in ${pkglibexecdir}/installed-tests
* and the vcards are installed in ${pkglibexecdir}/installed-tests/vcards
*/
if (g_getenv ("TEST_INSTALLED_SERVICES") != NULL) {
filename = g_build_filename (INSTALLED_TEST_DIR, "vcards", case_filename, NULL);
} else {
if (!args_data_dir) {
g_warning ("Data directory not set, pass it with `--data-dir PATH`");
exit(1);
}
filename = g_build_filename (args_data_dir, case_filename, NULL);
}
file = g_file_new_for_path (filename);
if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error)) {
g_warning (
"failed to read test contact file '%s': %s",
filename, error->message);
exit (1);
}
g_free (case_filename);
g_free (filename);
g_object_unref (file);
return vcard;
}
gchar *
ebook_test_utils_book_add_contact_from_test_case_verify (EBook *book,
const gchar *case_name,
EContact **contact)
{
gchar *vcard;
EContact *contact_orig;
EContact *contact_final;
gchar *uid;
vcard = ebook_test_utils_new_vcard_from_test_case (case_name);
contact_orig = e_contact_new_from_vcard (vcard);
uid = g_strdup (ebook_test_utils_book_add_contact (book, contact_orig));
contact_final = ebook_test_utils_book_get_contact (book, uid);
/* verify the contact was added "successfully" (not thorough) */
g_assert_true (ebook_test_utils_contacts_are_equal_shallow (contact_orig, contact_final));
if (contact)
*contact = g_object_ref (contact_final);
return uid;
}
/* This is not a thorough comparison (which is difficult, assuming we give the
* back-ends leniency in implementation) and is best suited for simple tests */
gboolean
ebook_test_utils_contacts_are_equal_shallow (EContact *a,
EContact *b)
{
const gchar *uid_a, *uid_b;
/* Avoid warnings if one or more are NULL, to make this function
* "NULL-friendly" */
if (!a && !b)
return TRUE;
if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
return FALSE;
uid_a = e_contact_get_const (a, E_CONTACT_UID);
uid_b = e_contact_get_const (b, E_CONTACT_UID);
return g_strcmp0 (uid_a, uid_b) == 0;
}
const gchar *
ebook_test_utils_book_add_contact (EBook *book,
EContact *contact)
{
GError *error = NULL;
if (!e_book_add_contact (book, contact, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to add contact to addressbook: `%s': %s",
name, error->message);
exit (1);
}
return e_contact_get_const (contact, E_CONTACT_UID);
}
static void
add_contact_cb (EBook *book,
const GError *error,
const gchar *uid,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously add the contact '%s': "
"status %d (%s)", uid, error->code, error->message);
exit (1);
}
test_print ("successfully asynchronously added the contact "
"addressbook\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_add_contact (EBook *book,
EContact *contact,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_add_contact_async (book, contact,
(EBookIdAsyncCallback) add_contact_cb, closure)) {
g_warning ("failed to set up contact add");
exit (1);
}
}
void
ebook_test_utils_book_commit_contact (EBook *book,
EContact *contact)
{
GError *error = NULL;
if (!e_book_commit_contact (book, contact, &error)) {
ESource *source;
const gchar *name;
const gchar *uid;
uid = (const gchar *) e_contact_get_const (contact, E_CONTACT_UID);
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to commit changes to contact '%s' to "
"addressbook: `%s': %s", uid, name, error->message);
exit (1);
}
}
static void
commit_contact_cb (EBook *book,
const GError *error,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously commit the contact: "
"status %d (%s)", error->code, error->message);
exit (1);
}
test_print ("successfully asynchronously committed the contact to the "
"addressbook\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_commit_contact (EBook *book,
EContact *contact,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_commit_contact_async (book, contact,
(EBookAsyncCallback) commit_contact_cb, closure)) {
g_warning ("failed to set up contact commit");
exit (1);
}
}
EContact *
ebook_test_utils_book_get_contact (EBook *book,
const gchar *uid)
{
EContact *contact = NULL;
GError *error = NULL;
if (!e_book_get_contact (book, uid, &contact, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get contact '%s' in addressbook: `%s': "
"%s", uid, name, error->message);
exit (1);
}
return contact;
}
static void
get_contact_cb (EBook *book,
const GError *error,
EContact *contact,
EBookTestClosure *closure)
{
const gchar *uid;
if (error) {
g_warning (
"failed to asynchronously get the contact: "
"status %d (%s)", error->code, error->message);
exit (1);
}
uid = e_contact_get_const (contact, E_CONTACT_UID);
test_print (
"successfully asynchronously retrieved the contact '%s'\n",
uid);
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_get_contact (EBook *book,
const gchar *uid,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_get_contact_async (book, uid,
(EBookContactAsyncCallback) get_contact_cb,
closure)) {
g_warning ("failed to set up async getContact");
exit (1);
}
}
GList *
ebook_test_utils_book_get_required_fields (EBook *book)
{
GList *fields = NULL;
GError *error = NULL;
if (!e_book_get_required_fields (book, &fields, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get required fields for addressbook "
"`%s': %s", name, error->message);
exit (1);
}
return fields;
}
static void
get_required_fields_cb (EBook *book,
const GError *error,
EList *fields,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously get the required fields: "
"status %d (%s)", error->code, error->message);
exit (1);
}
closure->list = fields;
test_print ("successfully asynchronously retrieved the required fields\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_get_required_fields (EBook *book,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_get_required_fields_async (book,
(EBookEListAsyncCallback) get_required_fields_cb,
closure)) {
g_warning ("failed to set up async getRequiredFields");
exit (1);
}
}
const gchar *
ebook_test_utils_book_get_static_capabilities (EBook *book)
{
GError *error = NULL;
const gchar *caps;
if (!(caps = e_book_get_static_capabilities (book, &error))) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get capabilities for addressbook: `%s': "
"%s", name, error->message);
exit (1);
}
return caps;
}
GList *
ebook_test_utils_book_get_supported_auth_methods (EBook *book)
{
GList *fields = NULL;
GError *error = NULL;
if (!e_book_get_supported_auth_methods (book, &fields, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get supported auth methods for "
"addressbook `%s': %s", name, error ? error->message : "Unknown error");
exit (1);
}
return fields;
}
static void
get_supported_auth_methods_cb (EBook *book,
const GError *error,
EList *methods,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously get the supported auth "
"methods: status %d (%s)", error->code, error->message);
exit (1);
}
closure->list = methods;
test_print ("successfully asynchronously retrieved the supported auth "
"methods\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_get_supported_auth_methods (EBook *book,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_get_supported_auth_methods_async (book,
(EBookEListAsyncCallback) get_supported_auth_methods_cb,
closure)) {
g_warning ("failed to set up async getSupportedAuthMethods");
exit (1);
}
}
GList *
ebook_test_utils_book_get_supported_fields (EBook *book)
{
GList *fields = NULL;
GError *error = NULL;
if (!e_book_get_supported_fields (book, &fields, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get supported fields for addressbook "
"`%s': %s", name, error->message);
exit (1);
}
return fields;
}
static void
get_supported_fields_cb (EBook *book,
const GError *error,
EList *fields,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously get the supported fields: "
"status %d (%s)", error->code, error->message);
exit (1);
}
closure->list = fields;
test_print ("successfully asynchronously retrieved the supported fields\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_get_supported_fields (EBook *book,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_get_supported_fields_async (book,
(EBookEListAsyncCallback) get_supported_fields_cb,
closure)) {
g_warning ("failed to set up async getSupportedFields");
exit (1);
}
}
void
ebook_test_utils_book_remove_contact (EBook *book,
const gchar *uid)
{
GError *error = NULL;
if (!e_book_remove_contact (book, uid, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to remove contact '%s' from addressbook: "
"`%s': %s", uid, name, error->message);
exit (1);
}
}
static void
remove_contact_cb (EBook *book,
const GError *error,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously remove the contact: "
"status %d (%s)", error->code, error->message);
exit (1);
}
test_print ("successfully asynchronously removed the contact\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_remove_contact (EBook *book,
EContact *contact,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_remove_contact_async (book, contact,
(EBookAsyncCallback) remove_contact_cb,
closure)) {
g_warning (
"failed to set up async removeContacts "
"(for a single contact)");
exit (1);
}
}
static void
remove_contact_by_id_cb (EBook *book,
const GError *error,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously remove the contact by id: "
"status %d (%s)", error->code, error->message);
exit (1);
}
test_print ("successfully asynchronously removed the contact by id\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_remove_contact_by_id (EBook *book,
const gchar *uid,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_remove_contact_by_id_async (book, uid,
(EBookAsyncCallback) remove_contact_by_id_cb,
closure)) {
g_warning ("failed to set up async removeContacts (by id)");
exit (1);
}
}
void
ebook_test_utils_book_remove_contacts (EBook *book,
GList *ids)
{
GError *error = NULL;
if (!e_book_remove_contacts (book, ids, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to remove contacts from addressbook: `%s': %s",
name, error->message);
exit (1);
}
}
static void
remove_contacts_cb (EBook *book,
const GError *error,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously remove the contacts: "
"status %d (%s)", error->code, error->message);
exit (1);
}
test_print ("successfully asynchronously removed the contacts\n");
if (closure->cb)
(*closure->cb) (closure);
g_free (closure);
}
void
ebook_test_utils_book_async_remove_contacts (EBook *book,
GList *uids,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_remove_contacts_async (book, uids,
(EBookAsyncCallback) remove_contacts_cb,
closure)) {
g_warning ("failed to set up async removeContacts");
exit (1);
}
}
void
ebook_test_utils_book_get_book_view (EBook *book,
EBookQuery *query,
EBookView **view)
{
GError *error = NULL;
if (!e_book_get_book_view (book, query, NULL, -1, view, &error)) {
ESource *source;
const gchar *name;
source = e_book_get_source (book);
name = e_source_get_display_name (source);
g_warning (
"failed to get view for addressbook: `%s': %s",
name, error->message);
exit (1);
}
}
static void
get_book_view_cb (EBook *book,
const GError *error,
EBookView *view,
EBookTestClosure *closure)
{
if (error) {
g_warning (
"failed to asynchronously get book view for the "
"book: status %d (%s)", error->code, error->message);
exit (1);
}
closure->view = view;
test_print ("successfully asynchronously retrieved the book view\n");
if (closure->cb)
(*closure->cb) (closure);
}
void
ebook_test_utils_book_async_get_book_view (EBook *book,
EBookQuery *query,
GSourceFunc callback,
gpointer user_data)
{
EBookTestClosure *closure;
closure = g_new0 (EBookTestClosure, 1);
closure->cb = callback;
closure->user_data = user_data;
if (!e_book_get_book_view_async (book, query, NULL, -1, (EBookBookViewAsyncCallback) get_book_view_cb, closure)) {
g_warning ("failed to set up book view retrieval");
exit (1);
}
}
|