/*
 * 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 PurpleUi Implementation
 *****************************************************************************/
#define TEST_PURPLE_TYPE_UI (test_purple_ui_get_type())

G_DECLARE_FINAL_TYPE(TestPurpleUi, test_purple_ui, TEST_PURPLE, UI, PurpleUi)

struct _TestPurpleUi {
	PurpleUi parent;
};

G_DEFINE_FINAL_TYPE(TestPurpleUi, test_purple_ui, PURPLE_TYPE_UI)

static void
test_purple_ui_init(G_GNUC_UNUSED TestPurpleUi *ui) {
}

static void
test_purple_ui_class_init(G_GNUC_UNUSED TestPurpleUiClass *klass)
{
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_ui_get_account_manager_backend(void) {
	PurpleAccountManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_account_manager_backend(ui, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_contact_manager_backend(void) {
	PurpleContactManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_contact_manager_backend(ui, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_conversation_manager_backend(void) {
	PurpleConversationManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_conversation_manager_backend(ui, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_presence_manager_backend(void) {
	PurplePresenceManagerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_presence_manager_backend(ui, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_scheduler_backend(void) {
	PurpleSchedulerBackend *backend = NULL;
	PurpleUi *ui = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	backend = purple_ui_get_scheduler_backend(ui, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(backend);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_display_version(void) {
	PurpleUi *ui = NULL;
	char *display_version = NULL;
	char *expected = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	expected = g_strdup_printf("Purple %d", PURPLE_API_MAJOR_VERSION);

	display_version = purple_ui_get_display_version(ui);
	g_assert_cmpstr(display_version, ==, expected);
	g_clear_pointer(&display_version, g_free);
	g_clear_pointer(&expected, g_free);

	g_assert_finalize_object(ui);
}

static void
test_purple_ui_get_user_agent(void) {
	PurpleUi *ui = NULL;
	char *user_agent = NULL;
	char *expected = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	birb_assert_type(ui, TEST_PURPLE_TYPE_UI);

	expected = g_strdup_printf("purple/%d", PURPLE_API_MAJOR_VERSION);

	user_agent = purple_ui_get_user_agent(ui);
	g_assert_cmpstr(user_agent, ==, expected);
	g_clear_pointer(&user_agent, g_free);
	g_clear_pointer(&expected, g_free);

	g_assert_finalize_object(ui);
}

/******************************************************************************
 * Present Qr Code tests
 *****************************************************************************/
static void
test_purple_ui_present_qr_code(void) {
	PurpleQrCode *qr_code = NULL;
	PurpleUi *ui = NULL;
	GCancellable *cancellable = NULL;
	GError *error = NULL;
	gboolean success = FALSE;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	qr_code = purple_qr_code_new("title", NULL, "https://imfreedom.org/",
	                             NULL);

	cancellable = g_cancellable_new();
	success = purple_ui_present_qr_code(ui, qr_code, cancellable, &error);
	g_assert_error(error,
	               PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_false(success);

	g_assert_finalize_object(ui);
	g_assert_finalize_object(qr_code);
	g_assert_finalize_object(cancellable);
}

/******************************************************************************
 * request_page tests
 *****************************************************************************/
static void
test_purple_ui_request_page_cb(GObject *source, GAsyncResult *result,
                               gpointer data)
{
	PurpleRequestPage *page = NULL;
	GError *error = NULL;
	guint *counter = data;

	page = purple_ui_request_page_finish(PURPLE_UI(source), result, &error);
	g_assert_error(error, PURPLE_UI_ERROR,
	               PURPLE_UI_ERROR_VFUNC_NOT_IMPLEMENTED);
	g_clear_error(&error);

	g_assert_null(page);

	*counter = *counter + 1;
}

static void
test_purple_ui_request_page_async(void) {
	PurpleRequestPage *page = NULL;
	PurpleUi *ui = NULL;
	guint counter = 0;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);
	page = purple_request_page_new();

	purple_ui_request_page_async(ui, page, NULL,
	                             test_purple_ui_request_page_cb, &counter);

	g_main_context_iteration(NULL, FALSE);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(page);
	g_assert_finalize_object(ui);
}

static void
test_purple_ui_request_page_finish(void) {
	PurpleRequestPage *page = NULL;
	PurpleUi *ui = NULL;
	GTask *task = NULL;
	GError *error = NULL;

	ui = g_object_new(TEST_PURPLE_TYPE_UI, NULL);

	/* Create a dummy task to get past some defenses. */
	task = g_task_new(ui, NULL, NULL, NULL);
	g_task_return_pointer(task, NULL, NULL);

	page = purple_ui_request_page_finish(ui, G_ASYNC_RESULT(task), &error);
	g_clear_object(&task);

	g_assert_no_error(error);
	g_assert_null(page);

	/* The dummy task isn't removing its reference for some reason so we can't
	 * use g_assert_finalize_object on the ui.
	 */
	g_clear_object(&ui);
}

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

	g_test_add_func("/ui/empty/get-account-manager-backend",
	                test_purple_ui_get_account_manager_backend);
	g_test_add_func("/ui/empty/get-contact-manager-backend",
	                test_purple_ui_get_contact_manager_backend);
	g_test_add_func("/ui/empty/get-conversation-manager-backend",
	                test_purple_ui_get_conversation_manager_backend);
	g_test_add_func("/ui/empty/get-presence-manager-backend",
	                test_purple_ui_get_presence_manager_backend);
	g_test_add_func("/ui/empty/get-scheduler-backend",
	                test_purple_ui_get_scheduler_backend);

	g_test_add_func("/ui/empty/get-display-version",
	                test_purple_ui_get_display_version);
	g_test_add_func("/ui/empty/get-user-agent", test_purple_ui_get_user_agent);

	g_test_add_func("/ui/empty/present-qr-code",
	                test_purple_ui_present_qr_code);

	g_test_add_func("/ui/empty/request-page/async",
	                test_purple_ui_request_page_async);
	g_test_add_func("/ui/empty/request-page/finish",
	                test_purple_ui_request_page_finish);

	return g_test_run();
}
