/*
 * 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 PurpleConversationManager Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND (test_purple_conversation_manager_backend_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleConversationManagerBackend,
                     test_purple_conversation_manager_backend,
                     TEST_PURPLE,
                     CONVERSATION_MANAGER_BACKEND,
                     PurpleConversationManagerBackend);

struct _TestPurpleConversationManagerBackend {
	PurpleConversationManagerBackend parent;
};

G_DEFINE_FINAL_TYPE(TestPurpleConversationManagerBackend,
                    test_purple_conversation_manager_backend,
                    PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

static void
test_purple_conversation_manager_backend_init(G_GNUC_UNUSED TestPurpleConversationManagerBackend *backend)
{
}

static void
test_purple_conversation_manager_backend_class_init(G_GNUC_UNUSED TestPurpleConversationManagerBackendClass *klass)
{
}

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

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	success = purple_conversation_manager_backend_delete_conversation_finish(backend,
	                                                                         result,
	                                                                         &error);
	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_empty_delete_conversation_async(void)
{
	PurpleConversation *conversation = NULL;
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND,
	                       NULL);
	conversation = g_object_new(PURPLE_TYPE_CONVERSATION, NULL);

	purple_conversation_manager_backend_delete_conversation_async(backend,
	                                                              conversation,
	                                                              NULL,
	                                                              test_purple_conversation_manager_backend_empty_delete_conversation_cb,
	                                                              &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(conversation);
}

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

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_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_conversation_manager_backend_delete_conversation_finish(backend,
	                                                                         G_ASYNC_RESULT(task),
	                                                                         &error);
	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_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_conversation_manager_backend_empty_load_conversations_cb(GObject *source,
                                                                     GAsyncResult *result,
                                                                     gpointer data)
{
	PurpleConversationManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *conversations = NULL;
	guint *counter = data;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	conversations = purple_conversation_manager_backend_load_conversations_finish(backend,
	                                                                              result,
	                                                                              &error);
	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(conversations);

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_empty_load_conversations_async(void) {
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND, NULL);

	purple_conversation_manager_backend_load_conversations_async(backend,
	                                                             NULL,
	                                                             test_purple_conversation_manager_backend_empty_load_conversations_cb,
	                                                             &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
}

static void
test_purple_conversation_manager_backend_empty_load_conversations_finish(void)
{
	PurpleConversationManagerBackend *backend = NULL;
	GError *error = NULL;
	GListModel *conversations = NULL;
	GTask *task = NULL;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_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);

	conversations = purple_conversation_manager_backend_load_conversations_finish(backend,
	                                                                              G_ASYNC_RESULT(task),
	                                                                              &error);

	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(conversations);

	/* 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_conversation_manager_backend_empty_save_conversation_cb(GObject *source,
                                                                    GAsyncResult *result,
                                                                    gpointer data)
{
	PurpleConversationManagerBackend *backend = NULL;
	GError *error = NULL;
	guint *counter = data;
	gboolean success = FALSE;

	birb_assert_type(source, TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND);

	backend = PURPLE_CONVERSATION_MANAGER_BACKEND(source);
	success = purple_conversation_manager_backend_save_conversation_finish(backend,
	                                                                       result,
	                                                                       &error);
	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	*counter = *counter + 1;
}

static void
test_purple_conversation_manager_backend_empty_save_conversation_async(void) {
	PurpleConversation *conversation = NULL;
	PurpleConversationManagerBackend *backend = NULL;
	guint counter = 0;

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_MANAGER_BACKEND, NULL);
	conversation = g_object_new(PURPLE_TYPE_CONVERSATION, NULL);

	purple_conversation_manager_backend_save_conversation_async(backend,
	                                                            conversation,
	                                                            NULL,
	                                                            test_purple_conversation_manager_backend_empty_save_conversation_cb,
	                                                            &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(backend);
	g_assert_finalize_object(conversation);
}

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

	backend = g_object_new(TEST_PURPLE_TYPE_CONVERSATION_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_conversation_manager_backend_save_conversation_finish(backend,
	                                                                       G_ASYNC_RESULT(task),
	                                                                       &error);
	g_assert_error(error,
	               PURPLE_CONVERSATION_MANAGER_BACKEND_ERROR,
	               PURPLE_CONVERSATION_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("/conversation-manager-backend-empty/delete-task-async",
	                test_purple_conversation_manager_backend_empty_delete_conversation_async);
	g_test_add_func("/conversation-manager-backend-empty/delete-task-finish",
	                test_purple_conversation_manager_backend_empty_delete_conversation_finish);
	g_test_add_func("/conversation-manager-backend-empty/loads-tasks-async",
	                test_purple_conversation_manager_backend_empty_load_conversations_async);
	g_test_add_func("/conversation-manager-backend-empty/loads-tasks-finish",
	                test_purple_conversation_manager_backend_empty_load_conversations_finish);
	g_test_add_func("/conversation-manager-backend-empty/save-task-async",
	                test_purple_conversation_manager_backend_empty_save_conversation_async);
	g_test_add_func("/conversation-manager-backend-empty/save-task-finish",
	                test_purple_conversation_manager_backend_empty_save_conversation_finish);

	return g_test_run();
}
