/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

#include <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_request_field_account_new_without_model(void) {
	PurpleRequestField *field = NULL;

	field = purple_request_field_account_new("id", "Account", NULL);
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_ACCOUNT);

	g_assert_finalize_object(field);
}

static void
test_purple_request_field_account_new_with_model(void) {
	PurpleRequestField *field = NULL;
	GListStore *store = NULL;

	store = g_list_store_new(PURPLE_TYPE_ACCOUNT);

	field = purple_request_field_account_new("id",
	                                         "Account",
	                                         G_LIST_MODEL(store));
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_ACCOUNT);

	g_assert_finalize_object(field);
	g_assert_finalize_object(store);
}

static void
test_purple_request_field_account_properties(void) {
	PurpleAccount *account = NULL;
	PurpleAccount *account1 = NULL;
	PurpleRequestField *field = NULL;
	GListStore *store = NULL;
	GListModel *model = NULL;

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

	store = g_list_store_new(PURPLE_TYPE_ACCOUNT);
	g_list_store_append(store, account);
	birb_assert_list_model_n_items(G_LIST_MODEL(store), 1);

	field = g_object_new(PURPLE_TYPE_REQUEST_FIELD_ACCOUNT,
	                     "account", account,
	                     "model", G_LIST_MODEL(store),
	                     NULL);

	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_ACCOUNT);

	g_object_get(G_OBJECT(field),
	             "account", &account1,
	             "model", &model,
	             NULL);

	g_assert_true(account1 == account);
	g_clear_object(&account1);

	g_assert_true(model == G_LIST_MODEL(store));
	g_clear_object(&model);

	g_assert_finalize_object(field);
	g_assert_finalize_object(store);
	g_assert_finalize_object(account);
}

static void
test_purple_request_field_account_account_supports_null(void) {
	PurpleAccount *account = NULL;
	PurpleAccount *account1 = NULL;
	PurpleRequestField *field = NULL;
	GListStore *store = NULL;

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

	store = g_list_store_new(PURPLE_TYPE_ACCOUNT);
	g_list_store_append(store, account);

	field = purple_request_field_account_new("id",
	                                         "account",
	                                         G_LIST_MODEL(store));
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_ACCOUNT);

	/* Make sure the default account is NULL. */
	account1 = purple_request_field_account_get_account(PURPLE_REQUEST_FIELD_ACCOUNT(field));
	g_assert_null(account1);

	/* Set the account to something non-null. */
	purple_request_field_account_set_account(PURPLE_REQUEST_FIELD_ACCOUNT(field),
	                                         account);
	account1 = purple_request_field_account_get_account(PURPLE_REQUEST_FIELD_ACCOUNT(field));
	g_assert_true(account1 == account);

	/* Set the account to NULL. */
	purple_request_field_account_set_account(PURPLE_REQUEST_FIELD_ACCOUNT(field),
	                                         NULL);
	account1 = purple_request_field_account_get_account(PURPLE_REQUEST_FIELD_ACCOUNT(field));
	g_assert_null(account1);

	/* Clean up. */
	g_assert_finalize_object(field);
	g_assert_finalize_object(store);
	g_assert_finalize_object(account);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
int
main(int argc, char **argv) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/request/field/account/new/without-model",
	                test_purple_request_field_account_new_without_model);
	g_test_add_func("/request/field/account/new/with-model",
	                test_purple_request_field_account_new_with_model);

	g_test_add_func("/request/field/account/properties",
	                test_purple_request_field_account_properties);

	g_test_add_func("/request/field/account/account-supports-null",
	                test_purple_request_field_account_account_supports_null);

	return g_test_run();
}

