/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * 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; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Empty PurpleContactManager Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND (test_purple_contact_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleContactManagerBackend,
                     test_purple_contact_manager_backend,
                     TEST_PURPLE,
                     CONTACT_MANAGER_BACKEND,
                     PurpleContactManagerBackend);

struct _TestPurpleContactManagerBackend {
	PurpleContactManagerBackend parent;
};

G_DEFINE_FINAL_TYPE(TestPurpleContactManagerBackend,
                    test_purple_contact_manager_backend,
                    PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

static void
test_purple_contact_manager_backend_init(G_GNUC_UNUSED TestPurpleContactManagerBackend *backend)
{
}

static void
test_purple_contact_manager_backend_class_init(G_GNUC_UNUSED TestPurpleContactManagerBackendClass *klass)
{
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_contact_manager_backend_empty_delete_contact_cb(GObject *source,
                                                            GAsyncResult *result,
                                                            gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_delete_contact_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_delete_contact_async(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	account = purple_account_new("test", "test");
	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);
	contact = purple_contact_new(account, NULL);

	purple_contact_manager_backend_delete_contact_async(backend,
	                                                    contact,
	                                                    NULL,
	                                                    test_purple_contact_manager_backend_empty_delete_contact_cb,
	                                                    &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_manager_backend_empty_delete_contact_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_contact_manager_backend_delete_contact_finish(backend,
	                                                               G_ASYNC_RESULT(task),
	                                                               &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

static void
test_purple_contact_manager_backend_empty_delete_person_cb(GObject *source,
                                                           GAsyncResult *result,
                                                           gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_delete_person_finish(backend,
	                                                              result,
	                                                              &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_delete_person_async(void) {
	PurplePerson *person = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);
	person = purple_person_new();

	purple_contact_manager_backend_delete_person_async(backend,
	                                                   person,
	                                                   NULL,
	                                                   test_purple_contact_manager_backend_empty_delete_person_cb,
	                                                   &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(person);
}

static void
test_purple_contact_manager_backend_empty_delete_person_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_contact_manager_backend_delete_person_finish(backend,
	                                                              G_ASYNC_RESULT(task),
	                                                              &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

static void
test_purple_contact_manager_backend_empty_load_contacts_cb(GObject *source,
                                                           GAsyncResult *result,
                                                           gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	PurpleContacts *contacts = NULL;
	GError *error = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	contacts = purple_contact_manager_backend_load_contacts_finish(backend,
	                                                               result,
	                                                               &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(contacts);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_load_contacts_async(void) {
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	purple_contact_manager_backend_load_contacts_async(backend,
	                                                   NULL,
	                                                   test_purple_contact_manager_backend_empty_load_contacts_cb,
	                                                   &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
}

static void
test_purple_contact_manager_backend_empty_load_contacts_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	PurpleContacts *contacts = NULL;
	GError *error = NULL;
	GTask *task = NULL;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_pointer(task, NULL, NULL);

	contacts = purple_contact_manager_backend_load_contacts_finish(backend,
	                                                               G_ASYNC_RESULT(task),
	                                                               &error);

	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(contacts);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

static void
test_purple_contact_manager_backend_empty_load_people_cb(GObject *source,
                                                         GAsyncResult *result,
                                                         gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *people = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	people = purple_contact_manager_backend_load_people_finish(backend,
	                                                           result,
	                                                           &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(people);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_load_people_async(void) {
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	purple_contact_manager_backend_load_people_async(backend,
	                                                 NULL,
	                                                 test_purple_contact_manager_backend_empty_load_people_cb,
	                                                 &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
}

static void
test_purple_contact_manager_backend_empty_load_people_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *people = NULL;
	GTask *task = NULL;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_pointer(task, NULL, NULL);

	people = purple_contact_manager_backend_load_people_finish(backend,
	                                                           G_ASYNC_RESULT(task),
	                                                           &error);

	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(people);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

static void
test_purple_contact_manager_backend_empty_save_contact_cb(GObject *source,
                                                          GAsyncResult *result,
                                                          gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_save_contact_finish(backend,
	                                                             result,
	                                                             &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_save_contact_async(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	account = purple_account_new("test", "test");
	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);
	contact = purple_contact_new(account, NULL);

	purple_contact_manager_backend_save_contact_async(backend,
	                                                  contact,
	                                                  NULL,
	                                                  test_purple_contact_manager_backend_empty_save_contact_cb,
	                                                  &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_manager_backend_empty_save_contact_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_contact_manager_backend_save_contact_finish(backend,
	                                                             G_ASYNC_RESULT(task),
	                                                             &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

static void
test_purple_contact_manager_backend_empty_save_person_cb(GObject *source,
                                                         GAsyncResult *result,
                                                         gpointer data)
{
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND);

	backend = PURPLE_CONTACT_MANAGER_BACKEND(source);
	success = purple_contact_manager_backend_save_person_finish(backend,
	                                                            result,
	                                                            &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_contact_manager_backend_empty_save_person_async(void) {
	PurplePerson *person = NULL;
	PurpleContactManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);
	person = purple_person_new();

	purple_contact_manager_backend_save_person_async(backend,
	                                                 person,
	                                                 NULL,
	                                                 test_purple_contact_manager_backend_empty_save_person_cb,
	                                                 &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(person);
}

static void
test_purple_contact_manager_backend_empty_save_person_finish(void) {
	PurpleContactManagerBackend *backend = NULL;
	GError *error = NULL;
	GTask *task = NULL;
	gboolean success = FALSE;

	backend = g_object_new(TEST_PURPLE_TYPE_CONTACT_MANAGER_BACKEND, NULL);

	/* Create a dummy task that's completed to get past the normal checks. */
	task = g_task_new(G_OBJECT(backend), NULL, NULL, NULL);
	g_task_return_boolean(task, TRUE);

	success = purple_contact_manager_backend_save_person_finish(backend,
	                                                            G_ASYNC_RESULT(task),
	                                                            &error);
	g_assert_error(error,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR,
	               PURPLE_CONTACT_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	/* We use g_clear_object here because there's some weird accounting from
	 * our task to get past the normal error checkers.
	 */
	g_clear_object(&task);
	g_clear_object(&backend);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/contact-manager-backend-empty/delete-contact-async",
	                test_purple_contact_manager_backend_empty_delete_contact_async);
	g_test_add_func("/contact-manager-backend-empty/delete-contact-finish",
	                test_purple_contact_manager_backend_empty_delete_contact_finish);
	g_test_add_func("/contact-manager-backend-empty/delete-person-async",
	                test_purple_contact_manager_backend_empty_delete_person_async);
	g_test_add_func("/contact-manager-backend-empty/delete-person-finish",
	                test_purple_contact_manager_backend_empty_delete_person_finish);
	g_test_add_func("/contact-manager-backend-empty/load-contacts-async",
	                test_purple_contact_manager_backend_empty_load_contacts_async);
	g_test_add_func("/contact-manager-backend-empty/load-contacts-finish",
	                test_purple_contact_manager_backend_empty_load_contacts_finish);
	g_test_add_func("/contact-manager-backend-empty/load-people-async",
	                test_purple_contact_manager_backend_empty_load_people_async);
	g_test_add_func("/contact-manager-backend-empty/load-people-finish",
	                test_purple_contact_manager_backend_empty_load_people_finish);
	g_test_add_func("/contact-manager-backend-empty/save-contact-async",
	                test_purple_contact_manager_backend_empty_save_contact_async);
	g_test_add_func("/contact-manager-backend-empty/save-contact-finish",
	                test_purple_contact_manager_backend_empty_save_contact_finish);
	g_test_add_func("/contact-manager-backend-empty/save-person-async",
	                test_purple_contact_manager_backend_empty_save_person_async);
	g_test_add_func("/contact-manager-backend-empty/save-person-finish",
	                test_purple_contact_manager_backend_empty_save_person_finish);

	return g_test_run();
}
