gint
main (gint argc,
      gchar **argv)
{
	GError *error = NULL;
	gint ii;

	main_initialize ();

	/* Serially create, open, (close), and remove many books */
	for (ii = 0; ii < NUM_CLIENTS; ii++) {
		EBookClient *book_client = new_temp_client (NULL);
		g_return_val_if_fail (book_client != NULL, 1);

		if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
			report_error ("client open sync", &error);
			return 1;
		}

		if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
			report_error ("client remove sync", &error);
			g_object_unref (book_client);
			return 1;
		}

		g_object_unref (book_client);
	}

	return 0;
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClientSourceType source_type = E_CAL_CLIENT_SOURCE_TYPE_EVENTS;
	GError *error = NULL;
	gint ii;

	main_initialize ();

	/* Serially create, open, (close), and remove many cals */
	for (ii = 0; ii < NUM_CLIENTS; ii++) {
		ECalClient *cal_client = new_temp_client (source_type, NULL);
		g_return_val_if_fail (cal_client != NULL, 1);

		if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
			report_error ("client open sync", &error);
			return 1;
		}

		if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
			report_error ("client remove sync", &error);
			g_object_unref (cal_client);
			return 1;
		}

		g_object_unref (cal_client);
	}

	return 0;
}
static gboolean
setup_book (EBookClient **book_out)
{
	GError *error = NULL;
	gint    i;

	g_return_val_if_fail (book_out != NULL, FALSE);

	*book_out = new_temp_client (NULL);
	g_return_val_if_fail (*book_out != NULL, FALSE);

	if (!e_client_open_sync (E_CLIENT (*book_out), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (*book_out);
		return FALSE;
	}

	for (i = 0; i < N_TEST_CONTACTS; i++)
	{
		EContact *contact = e_contact_new ();
		gchar    *name      = g_strdup_printf ("Contact #%d", i + 1);

		e_contact_set (contact, E_CONTACT_FULL_NAME, name);
		e_contact_set (contact, E_CONTACT_NICKNAME, name);

		/* verify the contact was added "successfully" (not thorough) */
		if (!add_contact_verify (*book_out, contact))
			g_error ("Failed to add contact");

		g_free (name);
		g_object_unref (contact);
	}

	return TRUE;
}
static gboolean
test_sync (void)
{
	ECalClient *cal_client;
	GError *error = NULL;

	cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);
	g_return_val_if_fail (cal_client != NULL, FALSE);

	if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (cal_client);
		return FALSE;
	}

	if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (cal_client);
		return FALSE;
	}

	g_object_unref (cal_client);

	return TRUE;
}
gint
main (gint argc,
      gchar **argv)
{
#if 0  /* ACCOUNT_MGMT */
	gchar *uri = NULL;
	EBookClient *book_client;
	GError *error = NULL;
	gint ii;

	main_initialize ();

	book_client = new_temp_client (&uri);
	g_return_val_if_fail (book_client != NULL, 1);
	g_return_val_if_fail (uri != NULL, 1);

	g_object_unref (book_client);

	/* open and close the same book repeatedly */
	for (ii = 0; ii < NUM_OPENS; ii++) {
		book_client = e_book_client_new_from_uri (uri, &error);
		if (!book_client) {
			report_error ("new from uri", &error);
			break;
		}

		if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
			report_error ("client open sync", &error);
			g_object_unref (book_client);
			break;
		}

		g_object_unref (book_client);
	}

	book_client = e_book_client_new_from_uri (uri, &error);
	if (!book_client) {
		g_clear_error (&error);
	} else if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		g_free (uri);
		return 1;
	} else	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		g_free (uri);
		return 1;
	}

	g_free (uri);
	g_object_unref (book_client);

	return ii == NUM_OPENS ? 0 : 1;
#endif  /* ACCOUNT_MGMT */

	return 0;
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClient *cal_client;
	GError *error = NULL;
	icalcomponent *icalcomp;
	struct icaltimetype now;
	gchar *uid = NULL;

	main_initialize ();

	cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);
	g_return_val_if_fail (cal_client != NULL, FALSE);

	if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	now = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
	icalcomp = icalcomponent_new (ICAL_VEVENT_COMPONENT);
	icalcomponent_set_summary (icalcomp, "Test event summary");
	icalcomponent_set_dtstart (icalcomp, now);
	icalcomponent_set_dtend   (icalcomp, icaltime_from_timet (icaltime_as_timet (now) + 60 * 60 * 60, 0));

	if (!e_cal_client_create_object_sync (cal_client, icalcomp, &uid, NULL, &error)) {
		report_error ("create object sync", &error);
		icalcomponent_free (icalcomp);
		g_object_unref (cal_client);
		return 1;
	}

	icalcomponent_free (icalcomp);
	g_free (uid);

	/* synchronously without main-loop */
	if (!test_sync (cal_client)) {
		g_object_unref (cal_client);
		return 1;
	}

	start_in_thread_with_main_loop (test_sync_in_thread, cal_client);

	if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	g_object_unref (cal_client);

	if (get_main_loop_stop_result () == 0)
		g_print ("Test finished successfully.\n");

	return get_main_loop_stop_result ();
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClientSourceType source_type = E_CAL_CLIENT_SOURCE_TYPE_EVENTS;
	gchar *uri = NULL;
	ECalClient *cal_client;
	GError *error = NULL;
	gint ii;

	main_initialize ();

	cal_client = new_temp_client (source_type, &uri);
	g_return_val_if_fail (cal_client != NULL, 1);
	g_return_val_if_fail (uri != NULL, 1);

	g_object_unref (cal_client);

	/* open and close the same cal repeatedly */
	for (ii = 0; ii < NUM_OPENS; ii++) {
		cal_client = e_cal_client_new_from_uri (uri, source_type, &error);
		if (!cal_client) {
			report_error ("new from uri", &error);
			break;
		}

		if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
			report_error ("client open sync", &error);
			g_object_unref (cal_client);
			break;
		}

		g_object_unref (cal_client);
	}

	cal_client = e_cal_client_new_from_uri (uri, source_type, &error);
	if (!cal_client) {
		g_clear_error (&error);
	} else if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (cal_client);
		g_free (uri);
		return 1;
	} else	if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (cal_client);
		g_free (uri);
		return 1;
	}

	g_free (uri);
	g_object_unref (cal_client);

	return ii == NUM_OPENS ? 0 : 1;
}
/* synchronously in idle with main-loop running */
static gboolean
test_sync_in_idle (gpointer user_data)
{
	ECalClient *cal_client;

	if (!test_sync ()) {
		stop_main_loop (1);
		return FALSE;
	}

	cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);
	g_return_val_if_fail (cal_client != NULL, FALSE);

	e_client_open (E_CLIENT (cal_client), FALSE, NULL, async_open_ready, NULL);

	return FALSE;
}
static void
setup_book (EBookClient **book_out)
{
	EBookClient *book;
	GError      *error = NULL;

	book = new_temp_client (NULL);
	g_assert (book != NULL);

	if (!e_client_open_sync (E_CLIENT (book), FALSE, NULL, &error)) {
		g_error ("failed to open client: %s", error->message);
	}

	add_contact_inline (book);
	add_contact_uri (book);

	*book_out = book;
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClientSourceType source_type = E_CAL_CLIENT_SOURCE_TYPE_EVENTS;
	ECalClient *cal_clients[NUM_CLIENTS];
	GError *error = NULL;
	gint ii;

	main_initialize ();

	/* Create and open many cals; then remove each of them */

	for (ii = 0; ii < NUM_CLIENTS; ii++) {
		cal_clients[ii] = new_temp_client (source_type, NULL);
		g_return_val_if_fail (cal_clients[ii] != NULL, 1);

		if (!e_client_open_sync (E_CLIENT (cal_clients[ii]), FALSE, NULL, &error)) {
			report_error ("client open sync", &error);
			while (ii >= 0) {
				g_object_unref (cal_clients[ii]);
				ii--;
			}

			return 1;
		}
	}

	for (ii = 0; ii < NUM_CLIENTS; ii++) {
		if (!e_client_remove_sync (E_CLIENT (cal_clients[ii]), NULL, &error)) {
			report_error ("client remove sync", &error);
			while (ii < NUM_CLIENTS) {
				g_object_unref (cal_clients[ii]);
				ii++;
			}
			return 1;
		}

		g_object_unref (cal_clients[ii]);
	}

	return 0;
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClient *cal_client;
	GError *error = NULL;

	main_initialize ();

	cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);
	g_return_val_if_fail (cal_client != NULL, FALSE);

	if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	/* synchronously without main-loop */
	if (!test_sync (cal_client)) {
		g_object_unref (cal_client);
		return 1;
	}

	start_in_thread_with_main_loop (test_sync_in_thread, cal_client);

	if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	g_object_unref (cal_client);

	if (get_main_loop_stop_result () == 0)
		g_print ("Test finished successfully.\n");

	return get_main_loop_stop_result ();
}
gint
main (gint argc,
      gchar **argv)
{
	ECalClient *cal_client;
	GError      *error = NULL;
	gint         i;

	main_initialize ();

	/*
	 * Setup
	 */
	cal_client = new_temp_client (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL);

	g_return_val_if_fail (cal_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (cal_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	/* Test that modifications make the revisions increment */
	for (i = 0; i < CYCLES; i++)
		get_revision_compare_cycle (cal_client);

	if (!e_client_remove_sync (E_CLIENT (cal_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (cal_client);
		return 1;
	}

	g_object_unref (cal_client);

	return 0;
}
gint
main (gint argc,
      gchar **argv)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact;
	gchar *vcard;

	main_initialize ();

	/*
	 * Setup
	 */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	/*
	 * Sync version
	 */
	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (contact);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	/*
	 * Async version
	 */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	vcard = new_vcard_from_test_case ("simple-1");
	contact = e_contact_new_from_vcard (vcard);
	g_free (vcard);

	e_book_client_add_contact (book_client, contact, NULL, add_contact_cb, NULL);
	g_object_unref (contact);

	start_main_loop (NULL, NULL);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	return get_main_loop_stop_result ();
}
gint
main (gint argc,
      gchar **argv)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact;
	gchar *uid;

	main_initialize ();

        /*
         * Setup
         */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

        /*
         * Sync version
         */
	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	uid = e_contact_get (contact, E_CONTACT_UID);
	g_object_unref (contact);

	if (!e_book_client_remove_contact_by_uid_sync (book_client, uid, NULL, &error)) {
		report_error ("remove contact sync", &error);
		g_object_unref (book_client);
		g_free (uid);
		return 1;
	}

	if (!e_book_client_get_contact_sync (book_client, uid, &contact, NULL, &error) &&
	    g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) {
		g_clear_error (&error);
	} else {
		report_error ("fail with get contact sync on removed contact", &error);
		g_object_unref (book_client);
		g_free (uid);
		return 1;
	}

	g_free (uid);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

        /*
         * Async version
         */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	contact = NULL;

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	uid = e_contact_get (contact, E_CONTACT_UID);
	g_object_unref (contact);
	e_book_client_remove_contact_by_uid (book_client, uid, NULL, remove_contact_by_uid_cb, uid);

	start_main_loop (NULL, NULL);

	g_free (uid);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	return 0;
}