/*
 * 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_image_new_normal(void) {
	PurpleImage *image = NULL;
	PurpleRequestField *field = NULL;

	image = g_object_new(PURPLE_TYPE_IMAGE, NULL);
	birb_assert_type(image, PURPLE_TYPE_IMAGE);

	field = purple_request_field_image_new("id", "label", image);
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_IMAGE);

	g_assert_finalize_object(field);
	g_assert_finalize_object(image);
}

static void
test_purple_request_field_image_new_null(void) {
	PurpleRequestField *field = NULL;

	field = purple_request_field_image_new("id", "label", NULL);
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_IMAGE);

	g_assert_finalize_object(field);
}

static void
test_purple_request_field_image_properties(void) {
	PurpleImage *image = NULL;
	PurpleImage *image1 = NULL;
	PurpleRequestField *field = NULL;
	char *id = NULL;
	char *label = NULL;

	image = g_object_new(PURPLE_TYPE_IMAGE, NULL);

	field = g_object_new(PURPLE_TYPE_REQUEST_FIELD_IMAGE,
	                     "id", "42",
	                     "image", image,
	                     "label", "The Answer",
	                     NULL);

	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_IMAGE);

	g_object_get(G_OBJECT(field),
	             "id", &id,
	             "image", &image1,
	             "label", &label,
	             NULL);

	g_assert_cmpstr(id, ==, "42");
	g_clear_pointer(&id, g_free);

	g_assert_true(image1 == image);
	g_clear_object(&image1);

	g_assert_cmpstr(label, ==, "The Answer");
	g_clear_pointer(&label, g_free);

	g_assert_finalize_object(field);
	g_assert_finalize_object(image);
}

static void
test_purple_request_field_image_image_supports_null(void) {
	PurpleImage *image = NULL;
	PurpleImage *image1 = NULL;
	PurpleRequestField *field = NULL;

	image = g_object_new(PURPLE_TYPE_IMAGE, NULL);
	birb_assert_type(image, PURPLE_TYPE_IMAGE);

	field = purple_request_field_image_new("id", "label", image);
	birb_assert_type(field, PURPLE_TYPE_REQUEST_FIELD_IMAGE);

	image1 = purple_request_field_image_get_image(PURPLE_REQUEST_FIELD_IMAGE(field));
	g_assert_true(image1 == image);

	purple_request_field_image_set_image(PURPLE_REQUEST_FIELD_IMAGE(field),
	                                     NULL);
	image1 = purple_request_field_image_get_image(PURPLE_REQUEST_FIELD_IMAGE(field));
	g_assert_null(image1);

	g_assert_finalize_object(field);
	g_assert_finalize_object(image);
}

/******************************************************************************
 * 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/image/new/normal",
	                test_purple_request_field_image_new_normal);
	g_test_add_func("/request/field/image/new/null",
	                test_purple_request_field_image_new_null);

	g_test_add_func("/request/field/image/properties",
	                test_purple_request_field_image_properties);

	g_test_add_func("/request/field/image/image-supports-null",
	                test_purple_request_field_image_image_supports_null);

	return g_test_run();
}

