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

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
test_purple_attachment_notify_counter_cb(GObject *self,
                                         G_GNUC_UNUSED GParamSpec *pspec,
                                         gpointer data)
{
	guint *counter = data;

	g_assert_true(PURPLE_IS_ATTACHMENT(self));

	*counter = *counter + 1;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_attachment_new(void) {
	PurpleAttachment *attachment = NULL;

	attachment = purple_attachment_new("1337", "text/plain");

	g_assert_true(PURPLE_IS_ATTACHMENT(attachment));

	g_assert_finalize_object(attachment);
}

static void
test_purple_attachment_properties(void) {
	PurpleAttachment *attachment = NULL;
	char *content_type = NULL;
	char *id = NULL;
	char *local_uri = NULL;
	char *remote_uri = NULL;
	gboolean _inline = FALSE;
	guint64 in_size = 0x1337;
	guint64 out_size = 0;

	attachment = g_object_new(
		PURPLE_TYPE_ATTACHMENT,
		"content-type", "text/plain",
		"id", "1337",
		"inline", TRUE,
		"local-uri", "local-uri",
		"remote-uri", "remote-uri",
		"size", in_size,
		NULL);

	g_object_get(
		attachment,
		"content-type", &content_type,
		"id", &id,
		"inline", &_inline,
		"local-uri", &local_uri,
		"remote-uri", &remote_uri,
		"size", &out_size,
		NULL);

	g_assert_cmpstr(content_type, ==, "text/plain");
	g_clear_pointer(&content_type, g_free);

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

	g_assert_true(_inline);

	g_assert_cmpstr(local_uri, ==, "local-uri");
	g_clear_pointer(&local_uri, g_free);

	g_assert_cmpstr(remote_uri, ==, "remote-uri");
	g_clear_pointer(&remote_uri, g_free);

	g_assert_cmpuint(out_size, ==, in_size);

	g_assert_finalize_object(attachment);
}

static void
test_purple_attachment_notify(void) {
	PurpleAttachment *attachment = NULL;
	guint counter = 0;

	attachment = g_object_new(PURPLE_TYPE_ATTACHMENT, NULL);
	g_signal_connect(attachment, "notify",
	                 G_CALLBACK(test_purple_attachment_notify_counter_cb),
	                 &counter);

	counter = 0;
	purple_attachment_set_content_type(attachment, "text/plain");
	g_assert_cmpuint(counter, ==, 1);

	counter = 0;
	purple_attachment_set_id(attachment, "1337");
	g_assert_cmpuint(counter, ==, 1);

	counter = 0;
	purple_attachment_set_inline(attachment, TRUE);
	g_assert_cmpuint(counter, ==, 1);

	counter = 0;
	purple_attachment_set_local_uri(attachment, "/dev/null");
	g_assert_cmpuint(counter, ==, 1);

	counter = 0;
	purple_attachment_set_remote_uri(attachment, "/dev/null");
	g_assert_cmpuint(counter, ==, 1);

	counter = 0;
	purple_attachment_set_size(attachment, 42lu);
	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(attachment);
}

static void
test_purple_attachment_equal_self(void) {
	PurpleAttachment *attachment = NULL;

	attachment = purple_attachment_new("1337", "text/plain");

	g_assert_true(purple_attachment_equal(attachment, attachment));

	g_assert_finalize_object(attachment);
}

static void
test_purple_attachment_equal_true(void) {
	PurpleAttachment *attachment1 = NULL;
	PurpleAttachment *attachment2 = NULL;

	attachment1 = purple_attachment_new("1337", "text/plain");
	attachment2 = purple_attachment_new("1337", "text/html");

	g_assert_true(purple_attachment_equal(attachment1, attachment2));

	g_assert_finalize_object(attachment1);
	g_assert_finalize_object(attachment2);
}

static void
test_purple_attachment_equal_false(void) {
	PurpleAttachment *attachment1 = NULL;
	PurpleAttachment *attachment2 = NULL;

	attachment1 = purple_attachment_new("1337", "text/plain");
	attachment2 = purple_attachment_new("7331", "text/html");

	g_assert_false(purple_attachment_equal(attachment1, attachment2));

	g_assert_finalize_object(attachment1);
	g_assert_finalize_object(attachment2);
}

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

	g_test_add_func("/attachment/new", test_purple_attachment_new);
	g_test_add_func("/attachment/properties",
	                test_purple_attachment_properties);
	g_test_add_func("/attachment/notify", test_purple_attachment_notify);

	g_test_add_func("/attachment/equal/self",
	                test_purple_attachment_equal_self);
	g_test_add_func("/attachment/equal/true",
	                test_purple_attachment_equal_true);
	g_test_add_func("/attachment/equal/false",
	                test_purple_attachment_equal_false);

	return g_test_run();
}
