/*
 * 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);
}

/******************************************************************************
 * 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);

	return g_test_run();
}
