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

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_attachments_properties(void) {
	PurpleAttachments *attachments = NULL;
	GType item_type = G_TYPE_INVALID;
	guint n_items = 0;

	attachments = g_object_new(
		PURPLE_TYPE_ATTACHMENTS,
		NULL);

	g_object_get(
		attachments,
		"item-type", &item_type,
		"n-items", &n_items,
		NULL);

	g_assert_cmpuint(item_type, ==, PURPLE_TYPE_ATTACHMENT);
	g_assert_cmpuint(n_items, ==, 0);

	g_assert_finalize_object(attachments);
}

static void
test_purple_attachments_double_add(void) {
	PurpleAttachment *attachment = NULL;
	PurpleAttachments *attachments = NULL;
	gboolean success = FALSE;
	guint items_changed_counter = 0;
	guint n_items_counter = 0;

	attachments = purple_attachments_new();
	birb_count_list_model_items_changed(G_LIST_MODEL(attachments),
	                                    &items_changed_counter);
	birb_count_property_changed(G_OBJECT(attachments), "n-items",
	                            &n_items_counter);

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

	/* The first add. */
	items_changed_counter = 0;
	n_items_counter = 0;
	success = purple_attachments_add_attachment(attachments, attachment);
	g_assert_true(success);
	g_assert_cmpuint(items_changed_counter, ==, 1);
	g_assert_cmpuint(n_items_counter, ==, 1);

	/* The double add. */
	items_changed_counter = 0;
	n_items_counter = 0;
	success = purple_attachments_add_attachment(attachments, attachment);
	g_assert_false(success);
	g_assert_cmpuint(items_changed_counter, ==, 0);
	g_assert_cmpuint(n_items_counter, ==, 0);

	g_assert_finalize_object(attachments);
	g_assert_finalize_object(attachment);
}

static void
test_purple_attachments_double_remove(void) {
	PurpleAttachment *attachment = NULL;
	PurpleAttachments *attachments = NULL;
	gboolean success = FALSE;
	guint items_changed_counter = 0;
	guint n_items_counter = 0;

	attachments = purple_attachments_new();
	birb_count_list_model_items_changed(G_LIST_MODEL(attachments),
	                                    &items_changed_counter);
	birb_count_property_changed(G_OBJECT(attachments), "n-items",
	                            &n_items_counter);

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

	/* The first add. */
	items_changed_counter = 0;
	n_items_counter = 0;
	success = purple_attachments_add_attachment(attachments, attachment);
	g_assert_true(success);
	g_assert_cmpuint(items_changed_counter, ==, 1);
	g_assert_cmpuint(n_items_counter, ==, 1);

	/* The first remove. */
	items_changed_counter = 0;
	n_items_counter = 0;
	success = purple_attachments_remove_attachment(attachments, attachment);
	g_assert_true(success);
	g_assert_cmpuint(items_changed_counter, ==, 1);
	g_assert_cmpuint(n_items_counter, ==, 1);

	/* The second remove. */
	items_changed_counter = 0;
	n_items_counter = 0;
	success = purple_attachments_remove_attachment(attachments, attachment);
	g_assert_false(success);
	g_assert_cmpuint(items_changed_counter, ==, 0);
	g_assert_cmpuint(n_items_counter, ==, 0);

	g_assert_finalize_object(attachments);
	g_assert_finalize_object(attachment);
}

static void
test_purple_attachments_find_with_id(void) {
	PurpleAttachment *attachment = NULL;
	PurpleAttachment *found = NULL;
	PurpleAttachments *attachments = NULL;

	attachments = purple_attachments_new();

	/* Search on the empty collection. */
	found = purple_attachments_find_with_id(attachments, "1337");
	g_assert_null(found);

	/* Create an attachment and add it. */
	attachment = purple_attachment_new("1337", "text/plain");
	purple_attachments_add_attachment(attachments, attachment);

	found = purple_attachments_find_with_id(attachments, "1337");
	g_assert_true(PURPLE_IS_ATTACHMENT(found));
	g_assert_true(found == attachment);

	/* Search for an attachment that doesn't exist. */
	found = purple_attachments_find_with_id(attachments, "42");
	g_assert_null(found);

	g_assert_finalize_object(attachments);
	g_assert_finalize_object(attachment);
}

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

	g_test_add_func("/attachments/properties",
	                test_purple_attachments_properties);
	g_test_add_func("/attachments/double-add",
	                test_purple_attachments_double_add);
	g_test_add_func("/attachments/double-remove",
	                test_purple_attachments_double_remove);
	g_test_add_func("/attachments/find-with-id",
	                test_purple_attachments_find_with_id);

	return g_test_run();
}
