/*
 * 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 <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_contact_new(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactInfo *info = NULL;

	account = purple_account_new("test", "test");
	contact = purple_contact_new(account, "id");
	info = PURPLE_CONTACT_INFO(contact);

	g_assert_true(purple_contact_get_account(contact) == account);
	g_assert_cmpstr(purple_contact_info_get_id(info), ==, "id");

	g_clear_object(&contact);
	g_clear_object(&account);
}

static void
test_purple_contact_properties(void) {
	PurpleAccount *account = NULL;
	PurpleAccount *account1 = NULL;
	PurpleContact *contact = NULL;
	char *id = NULL;

	account = purple_account_new("test", "test");

	/* Use g_object_new so we can test setting properties by name. All of them
	 * call the setter methods, so by doing it this way we exercise more of the
	 * code.
	 */
	contact = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account,
		"id", "id1",
		NULL);

	/* Now use g_object_get to read all of the properties. */
	g_object_get(contact,
		"id", &id,
		"account", &account1,
		NULL);

	/* Compare all the things. */
	g_assert_cmpstr(id, ==, "id1");
	g_assert_true(account1 == account);

	/* Free/unref all the things. */
	g_clear_pointer(&id, g_free);
	g_clear_object(&account1);
	g_clear_object(&contact);
	g_clear_object(&account);
}

static void
test_purple_contact_is_own_account(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleContactInfo *info = NULL;

	account = purple_account_new("test", "test");

	/* Create a random contact and verify that it is not the account. */
	contact = purple_contact_new(account, "id");

	g_assert_false(purple_contact_is_own_account(contact));
	g_assert_finalize_object(contact);

	/* Now create a contact info with the account's contact's id. */
	info = purple_account_get_contact_info(account);
	contact = purple_contact_new(account, purple_contact_info_get_id(info));
	g_assert_true(purple_contact_is_own_account(contact));
	g_assert_finalize_object(contact);

	g_clear_object(&account);
}

/******************************************************************************
 * Compare
 *****************************************************************************/
static void
test_purple_contact_compare_not_null__null(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;

	account = purple_account_new("test", "test");
	contact = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account,
		"id", "id1",
		NULL);

	g_assert_cmpint(purple_contact_compare(contact, NULL), <, 0);

	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_compare_null__not_null(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;

	account = purple_account_new("test", "test");
	contact = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account,
		"id", "id1",
		NULL);

	g_assert_cmpint(purple_contact_compare(NULL, contact), >, 0);

	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_compare_null__null(void) {
	g_assert_cmpint(purple_contact_compare(NULL, NULL), ==, 0);
}

static void
test_purple_contact_compare_not_null__not_null(void) {
	PurpleAccount *account_a = NULL;
	PurpleAccount *account_b = NULL;
	PurpleContact *contact_a = NULL;
	PurpleContact *contact_b = NULL;

	account_a = g_object_new(
		PURPLE_TYPE_ACCOUNT,
		"protocol-id", "test",
		"enabled", FALSE,
		NULL);
	account_b = g_object_new(
		PURPLE_TYPE_ACCOUNT,
		"protocol-id", "test",
		"enabled", FALSE,
		NULL);
	contact_a = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account_a,
		NULL);
	contact_b = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account_b,
		NULL);

	/* Account a and b have no id and should compare equal. */
	g_assert_cmpint(purple_contact_compare(contact_a, contact_b), ==, 0);

	/* Change display_name so the contact_info objects will be different. */
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_a), "name_a");
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_b), "name_b");
	g_assert_cmpint(purple_contact_compare(contact_a, contact_b), <, 0);

	/* Set the account ids so the account member will be different too. */
	g_object_set(account_a, "id", "account_id_a", NULL);
	g_object_set(account_b, "id", "account_id_b", NULL);
	g_assert_cmpint(purple_contact_compare(contact_b, contact_a), >, 0);

	/* Set the display name of contact_b to that of contact_a. The contact info
	 * is equal now, but account is still different.
	 */
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_b), "name_a");
	g_assert_cmpint(purple_contact_compare(contact_b, contact_a), >, 0);

	/* Set the account id of account_b to that of account_a. Both contact info and
	 * account are equal now.
	 */
	g_object_set(account_b, "id", "account_id_a", NULL);
	g_assert_cmpint(purple_contact_compare(contact_a, contact_b), ==, 0);

	g_assert_finalize_object(contact_a);
	g_assert_finalize_object(contact_b);
	g_assert_finalize_object(account_a);
	g_assert_finalize_object(account_b);
}

/******************************************************************************
 * Equal
 *****************************************************************************/
static void
test_purple_contact_equal_not_null__null(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;

	account = purple_account_new("test", "test");
	contact = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account,
		"id", "id1",
		NULL);

	g_assert_false(purple_contact_equal(contact, NULL));

	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_equal_null__not_null(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;

	account = purple_account_new("test", "test");
	contact = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account,
		"id", "id1",
		NULL);

	g_assert_false(purple_contact_equal(NULL, contact));

	g_assert_finalize_object(contact);
	g_assert_finalize_object(account);
}

static void
test_purple_contact_equal_null__null(void) {
	g_assert_true(purple_contact_equal(NULL, NULL));
}

static void
test_purple_contact_equal_not_null__not_null(void) {
	PurpleAccount *account_a = NULL;
	PurpleAccount *account_b = NULL;
	PurpleContact *contact_a = NULL;
	PurpleContact *contact_b = NULL;

	account_a = g_object_new(
		PURPLE_TYPE_ACCOUNT,
		"protocol-id", "test",
		"enabled", FALSE,
		NULL);
	account_b = g_object_new(
		PURPLE_TYPE_ACCOUNT,
		"protocol-id", "test",
		"enabled", FALSE,
		NULL);
	contact_a = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account_a,
		NULL);
	contact_b = g_object_new(
		PURPLE_TYPE_CONTACT,
		"account", account_b,
		NULL);

	/* Account a and b have no id and should compare equal. */
	g_assert_true(purple_contact_equal(contact_a, contact_b));

	/* Change display_name so the contact_info objects will be different. */
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_a), "name_a");
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_b), "name_b");
	g_assert_false(purple_contact_equal(contact_a, contact_b));

	/* Set the account ids so the account members will be different too. */
	g_object_set(account_a, "id", "account_id_a", NULL);
	g_object_set(account_b, "id", "account_id_b", NULL);
	g_assert_false(purple_contact_equal(contact_a, contact_b));

	/* Set the display name of contact_b to that of contact_a. The contact info
	 * is equal now, but account is still different.
	 */
	purple_contact_info_set_display_name(PURPLE_CONTACT_INFO(contact_b), "name_a");
	g_assert_false(purple_contact_equal(contact_a, contact_b));

	/* Set the account id of account_b to that of account_a. Both contact info and
	 * account are equal now.
	 */
	g_object_set(account_b, "id", "account_id_a", NULL);
	g_assert_true(purple_contact_equal(contact_a, contact_b));

	g_assert_finalize_object(contact_a);
	g_assert_finalize_object(contact_b);
	g_assert_finalize_object(account_a);
	g_assert_finalize_object(account_b);
}

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

	g_test_add_func("/contact/new",
	                test_purple_contact_new);
	g_test_add_func("/contact/properties",
	                test_purple_contact_properties);

	g_test_add_func("/contact/is_own_account",
	                test_purple_contact_is_own_account);

	g_test_add_func("/contact/compare/not_null__null",
	                test_purple_contact_compare_not_null__null);
	g_test_add_func("/contact/compare/null__not_null",
	                test_purple_contact_compare_null__not_null);
	g_test_add_func("/contact/compare/null__null",
	                test_purple_contact_compare_null__null);
	g_test_add_func("/contact/compare/not_null__not_null",
	                test_purple_contact_compare_not_null__not_null);

	g_test_add_func("/contact/equal/not_null__null",
	                test_purple_contact_equal_not_null__null);
	g_test_add_func("/contact/equal/null__not_null",
	                test_purple_contact_equal_null__not_null);
	g_test_add_func("/contact/equal/null__null",
	                test_purple_contact_equal_null__null);
	g_test_add_func("/contact/equal/not_null__not_null",
	                test_purple_contact_equal_not_null__not_null);


	return g_test_run();
}
