static void
test_modify_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact, *book_contact;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "name-only", &contact))
		g_error ("Failed to add contact");

	verify_premodify_and_prepare_contact (contact);

	if (!e_book_client_modify_contact_sync (book_client, contact, NULL, &error))
		g_error ("modify contact sync: %s", error->message);

	if (!e_book_client_get_contact_sync (book_client, e_contact_get_const (contact, E_CONTACT_UID),
					     &book_contact, NULL, &error))
		g_error ("get contact sync: %s", error->message);

	verify_modify (book_contact);
	g_object_unref (book_contact);
	g_object_unref (contact);
}
static void
test_get_view_sync (ETestServerFixture *fixture,
                    gconstpointer user_data)
{
	ECalClient *cal_client;
	GError *error = NULL;
	ECalClientView *view = NULL;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	if (!e_cal_client_get_view_sync (cal_client, "(contains? \"any\" \"event\")", &view, NULL, &error))
		g_error ("get view sync: %s", error->message);

	subtest_passed (SUBTEST_RESET, fixture->loop);
	g_signal_connect (view, "objects_added", G_CALLBACK (objects_added_cb), fixture->loop);
	g_signal_connect (view, "objects_modified", G_CALLBACK (objects_modified_cb), fixture->loop);
	g_signal_connect (view, "objects_removed", G_CALLBACK (objects_removed_cb), fixture->loop);
	g_signal_connect (view, "complete", G_CALLBACK (complete_cb), NULL);

	e_cal_client_view_set_fields_of_interest (view, NULL, &error);
	if (error)
		g_error ("set fields of interest: %s", error->message);
	e_cal_client_view_start (view, NULL);

	g_idle_add ((GSourceFunc) alter_cal_client, cal_client);
	g_main_loop_run (fixture->loop);

	g_object_unref (view);
}
static void
test_remove_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact = NULL;
	gchar *uid;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
		g_error ("Failed to add contact");

	uid = e_contact_get (contact, E_CONTACT_UID);

	if (!e_book_client_remove_contact_sync (book_client, contact, NULL, &error))
		g_error ("remove contact sync: %s", error->message);

	g_object_unref (contact);

	check_removed_contact (book_client, uid);

	g_free (uid);
}
static void
test_get_view_sync (ETestServerFixture *fixture,
                    gconstpointer user_data)
{
	EBookClient *book_client;
	EBookQuery *query;
	EBookClientView *view;
	gchar *sexp;
	GError *error = NULL;
	UIDOnlyClosure *closure = (UIDOnlyClosure *) user_data;

	uids_only = closure->uids_only;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	setup_book (book_client);

	query = e_book_query_any_field_contains ("");
	sexp = e_book_query_to_string (query);
	e_book_query_unref (query);
	if (!e_book_client_get_view_sync (book_client, sexp, &view, NULL, &error)) {
		g_error ("get book view sync: %s", error->message);
		g_free (sexp);
		g_object_unref (book_client);
	}

	g_free (sexp);

	setup_and_start_view (view, fixture->loop);
	g_main_loop_run (fixture->loop);
}
static void
test_remove_object (ETestServerFixture *fixture,
                    gconstpointer user_data)
{
	ECal *cal;
	icalcomponent *component;
	icalcomponent *component_final;
	gchar *uid;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);

	component = icalcomponent_new (ICAL_VEVENT_COMPONENT);
	uid = ecal_test_utils_cal_create_object (cal, component);

	/* FIXME: In the same way test-ecal-create-object.c,
	 * this part of the test is broken, need to fix this.
	 */
	component_final = ecal_test_utils_cal_get_object (cal, uid);
	/* ecal_test_utils_cal_assert_objects_equal_shallow (component, component_final); */

	ecal_test_utils_cal_remove_object (cal, uid);

	g_free (uid);
	icalcomponent_free (component);
	icalcomponent_free (component_final);
}
static void
test_create_object_2 (ETestServerFixture *fixture,
                      gconstpointer user_data)
{
	ECal *cal;
	ECalComponent *e_component, *e_component_final;
	icalcomponent *icalcomponent_final;
	gchar *uid;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);

	ecal_test_utils_create_component (
		cal,
		"20040109T090000Z", "UTC",
		"20040109T103000", "UTC",
		EVENT_SUMMARY, &e_component, &uid);

	icalcomponent_final = ecal_test_utils_cal_get_object (cal, uid);
	e_component_final = e_cal_component_new ();
	ecal_test_utils_cal_component_set_icalcomponent (
		e_component_final, icalcomponent_final);
	ecal_test_utils_cal_assert_e_cal_components_equal (
		e_component, e_component_final);

	g_object_unref (e_component_final);
	g_object_unref (e_component);
	g_free (uid);
}
static void
test_photo_is_uri (ETestServerFixture *fixture,
                   gconstpointer user_data)
{
	EBookClient *book_client;
	EBookClientView *view;
	EBookQuery *query;
	GError     *error = NULL;
	gchar      *sexp;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	add_contact_inline (book_client);
	add_contact_uri (book_client);

	query = e_book_query_any_field_contains ("");
	sexp = e_book_query_to_string (query);
	e_book_query_unref (query);
	if (!e_book_client_get_view_sync (book_client, sexp, &view, NULL, &error))
		g_error ("get book view sync: %s", error->message);

	g_free (sexp);

	setup_and_start_view (view);

	loop = fixture->loop;
	g_main_loop_run (loop);
}
static void
test_remove_contact_async (ETestServerFixture *fixture,
                           gconstpointer user_data)
{
	EBookClient *book_client;
	EContact *contact = NULL;
	gchar *uid;
	RemoveData data;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
		g_error ("Failed to add contact");

	uid = e_contact_get (contact, E_CONTACT_UID);

	data.uid = uid;
	data.loop = fixture->loop;
	e_book_client_remove_contact (book_client, contact, NULL, remove_contact_cb, &data);

	g_object_unref (contact);

	g_main_loop_run (fixture->loop);
	g_free (uid);
}
static void
test_get_capabilities (ETestServerFixture *fixture,
                       gconstpointer user_data)
{
	ECal *cal;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);
	ecal_test_utils_cal_get_capabilities (cal);
}
static void
test_get_revision_view_async (ETestServerFixture *fixture,
                              gconstpointer user_data)
{
	ECalClient *cal_client;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	e_cal_client_get_view (cal_client, "(contains? \"any\" \"event\")", NULL, async_get_view_ready, fixture->loop);
	g_main_loop_run (fixture->loop);
}
static void
test_get_supported_fields_async (ETestServerFixture *fixture,
                                 gconstpointer user_data)
{
	EBook *book;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);

	ebook_test_utils_book_async_get_supported_fields (
		book, (GSourceFunc) get_supported_fields_cb, fixture->loop);
	g_main_loop_run (fixture->loop);
}
static void
test_get_static_capabilities_sync (ETestServerFixture *fixture,
                                   gconstpointer user_data)
{
	EBook *book;
	const gchar *caps;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);

	caps = ebook_test_utils_book_get_static_capabilities (book);
	test_print ("successfully retrieved static capabilities: '%s'\n", caps);
}
static void
test_get_revision (ETestServerFixture *fixture,
                   gconstpointer user_data)
{
	EBookClient *book_client;
	gint i;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	/* Test that modifications make the revisions increment */
	for (i = 0; i < CYCLES; i++)
		get_revision_compare_cycle (book_client);
}
static void
test_get_alarm_email_address (ETestServerFixture *fixture,
                              gconstpointer user_data)
{
	ECal *cal;
	gchar *address;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);

	address = ecal_test_utils_cal_get_alarm_email_address (cal);
	test_print ("alarm email address: '%s'\n", address);
	g_free (address);
}
static void
test_get_default_object (ETestServerFixture *fixture,
                         gconstpointer user_data)
{
	ECal *cal;
	icalcomponent *component;
	gchar *component_string;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);

	component = ecal_test_utils_cal_get_default_object (cal);
	component_string = icalcomponent_as_ical_string (component);
	test_print ("default object:\n%s", component_string);

	g_free (component_string);
}
static void
test_get_supported_fields_sync (ETestServerFixture *fixture,
                                gconstpointer user_data)
{
	EBook *book;
	GList *fields;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);

	fields = ebook_test_utils_book_get_supported_fields (book);

	test_print ("successfully retrieved supported fields:\n");
	g_list_foreach (fields, (GFunc) list_member_print_and_free, NULL);
	test_print ("----------------\n");
	g_list_free (fields);
}
static void
test_remove_object_async (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	ECalClient *cal_client;
	gchar *uid;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	uid = create_object (cal_client);
	g_assert (uid != NULL);

	e_cal_client_remove_object (cal_client, uid, NULL, E_CAL_OBJ_MOD_ALL, NULL, async_remove_result_ready, fixture->loop);
	g_free (uid);
	g_main_loop_run (fixture->loop);
}
static void
test_send_objects_async (ETestServerFixture *fixture,
                        gconstpointer user_data)
{
	ECalClient *cal_client;
	icalcomponent *icalcomp;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	icalcomp = create_object ();
	g_assert (icalcomp);

	e_cal_client_send_objects (cal_client, icalcomp, NULL, async_send_result_ready, fixture->loop);
	icalcomponent_free (icalcomp);

	g_main_loop_run (fixture->loop);
}
static void
test_modify_object (ETestServerFixture *fixture,
                    gconstpointer user_data)
{
	ECal *cal;
	ECalComponent *e_component;
	ECalComponent *e_component_final;
	icalcomponent *component;
	icalcomponent *component_final;
	struct icaltimetype icaltime;
	gchar *uid;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECal);

	ecal_test_utils_create_component (
		cal,
		INITIAL_BEGIN_TIME, INITIAL_BEGIN_TIMEZONE,
		INITIAL_END_TIME, INITIAL_END_TIMEZONE,
		EVENT_SUMMARY, &e_component, &uid);
	component = e_cal_component_get_icalcomponent (e_component);

	component_final = ecal_test_utils_cal_get_object (cal, uid);
	ecal_test_utils_cal_assert_objects_equal_shallow (
		component, component_final);
	icalcomponent_free (component_final);

	/* make and commit changes */
	icaltime = icaltime_from_string (FINAL_BEGIN_TIME);
	icalcomponent_set_dtstart (component, icaltime);
	ecal_test_utils_cal_component_set_icalcomponent (
		e_component, component);
	ecal_test_utils_cal_modify_object (cal, component, CALOBJ_MOD_ALL);

	/* verify */
	component_final = ecal_test_utils_cal_get_object (cal, uid);
	e_component_final = e_cal_component_new ();
	ecal_test_utils_cal_component_set_icalcomponent (
		e_component_final, component_final);

	ecal_test_utils_cal_assert_e_cal_components_equal (
		e_component, e_component_final);

	g_object_unref (e_component_final);
	g_free (uid);
	icalcomponent_free (component);
}
static void
test_refresh_async (ETestServerFixture *fixture,
                    gconstpointer user_data)
{
	ECalClient *cal;
	gboolean supported;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	supported = e_client_check_refresh_supported (E_CLIENT (cal));
	if (!supported)
		return;

	setup_cal (cal);
	e_client_refresh (E_CLIENT (cal), NULL, async_refresh_result_ready, fixture->loop);
	g_main_loop_run (fixture->loop);
}
static void
test_remove_object_empty_uid (ETestServerFixture *fixture,
                              gconstpointer user_data)
{
	ECalClient *cal_client;
	GError *error = NULL;

	g_test_bug ("697705");

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	e_cal_client_remove_object_sync (
		cal_client, "", NULL, E_CAL_OBJ_MOD_ALL, NULL, &error);
	g_assert_error (
		error, E_CAL_CLIENT_ERROR,
		E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND);
}
static void
test_send_objects_sync (ETestServerFixture *fixture,
                        gconstpointer user_data)
{
	ECalClient *cal_client;
	GError *error = NULL;
	icalcomponent *icalcomp, *modified_icalcomp = NULL;
	GSList *users = NULL;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	icalcomp = create_object ();
	if (!e_cal_client_send_objects_sync (cal_client, icalcomp, &users, &modified_icalcomp, NULL, &error))
		g_error ("send objects sync: %s", error->message);

	icalcomponent_free (icalcomp);
	manage_result (users, modified_icalcomp);
}
static void
test_remove_object_sync (ETestServerFixture *fixture,
                         gconstpointer user_data)
{
	ECalClient *cal_client;
	GError *error = NULL;
	gchar *uid;

	cal_client = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	uid = create_object (cal_client);
	g_assert (uid != NULL);

	if (!e_cal_client_remove_object_sync (cal_client, uid, NULL, E_CAL_OBJ_MOD_ALL, NULL, &error))
		g_error ("remove object sync: %s", error->message);

	g_free (uid);
}
static void
test_remove_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBook *book;
	EContact *contact_final = NULL;
	gchar *uid;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);

	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", NULL);
	ebook_test_utils_book_remove_contact (book, uid);
	e_book_get_contact (book, uid, &contact_final, NULL);

	g_assert (contact_final == NULL);
	test_print ("successfully added and removed contact '%s'\n", uid);

	g_free (uid);
}
static void
test_get_book_view_async (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBook *book;
	EBookQuery *query;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);
	setup_book (book);

	query = e_book_query_any_field_contains ("");

	ebook_test_utils_book_async_get_book_view (
		book, query,
			(GSourceFunc) get_book_view_cb, fixture->loop);

	g_timeout_add_seconds (5, (GSourceFunc) main_loop_fail_timeout, NULL);
	g_main_loop_run (fixture->loop);
	e_book_query_unref (query);
}
static void
test_refresh_sync (ETestServerFixture *fixture,
                   gconstpointer user_data)
{
	ECalClient *cal;
	GError *error = NULL;
	gboolean supported;

	cal = E_TEST_SERVER_UTILS_SERVICE (fixture, ECalClient);

	supported = e_client_check_refresh_supported (E_CLIENT (cal));

	g_print ("Refresh supported: %s\n", supported ? "yes" : "no");
	if (!supported)
		return;

	setup_cal (cal);
	if (!e_client_refresh_sync (E_CLIENT (cal), NULL, &error))
		g_error ("refresh sync: %s", error->message);
}
static void
test_get_view_async (ETestServerFixture *fixture,
                     gconstpointer user_data)
{
	EBookClient *book_client;
	EBookQuery *query;
	gchar *sexp;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	setup_book (book_client);
	query = e_book_query_any_field_contains ("");
	sexp = e_book_query_to_string (query);
	e_book_query_unref (query);

	e_book_client_get_view (book_client, sexp, NULL, get_view_cb, fixture->loop);

	g_free (sexp);
	g_main_loop_run (fixture->loop);
}
static void
test_commit_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EContact *contact;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);
	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "name-only", &contact);

	verify_precommit_and_prepare_contact (contact);
	ebook_test_utils_book_commit_contact (book, contact);
	verify_commit (contact);

	test_print ("successfully committed changes to contact contact '%s'\n", uid);
	g_object_unref (contact);
	g_free (uid);

	contact = NULL;
	uid = NULL;
}
static void
test_get_book_view_sync (ETestServerFixture *fixture,
                         gconstpointer user_data)
{
	EBook *book;
	EBookQuery *query;
	EBookView *view;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);
	setup_book (book);

	query = e_book_query_any_field_contains ("");
	ebook_test_utils_book_get_book_view (book, query, &view);
	setup_and_start_view (view, fixture->loop);

	test_print ("successfully set up the book view\n");

	g_main_loop_run (fixture->loop);

	e_book_query_unref (query);
}
static void
test_remove_contact_async (ETestServerFixture *fixture,
                           gconstpointer user_data)
{
	EBook *book;
	EContact *contact_final = NULL;
	gchar *uid;

	book = E_TEST_SERVER_UTILS_SERVICE (fixture, EBook);

	/* contact_final has 2 refs by the end of this */
	uid = ebook_test_utils_book_add_contact_from_test_case_verify (book, "simple-1", &contact_final);

	/* contact_final is unref'd by e_book_remove_contact() here */
	ebook_test_utils_book_async_remove_contact (
		book, contact_final, ebook_test_utils_callback_quit, fixture->loop);

	g_main_loop_run (fixture->loop);
	g_object_unref (contact_final);
	g_free (uid);
}