/*
 * Copyright (C) 2023-2025 Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS 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 <birb.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_birb_count_list_items_changed(void) {
	GListStore *model = NULL;
	GObject *obj = NULL;
	guint items_changed_counter = 0;
	guint n_items_changed_counter = 0;
	guint properties_changed_counter = 0;

	model = g_list_store_new(G_TYPE_OBJECT);
	birb_count_list_model_items_changed(G_LIST_MODEL(model),
	                                    &items_changed_counter);
	birb_count_property_changed(G_OBJECT(model), "n-items",
	                            &n_items_changed_counter);
	birb_count_property_changed(G_OBJECT(model), NULL,
	                            &properties_changed_counter);

	/* Add an item to the list and make sure our counters were updated. */
	items_changed_counter = 0;
	n_items_changed_counter = 0;
	properties_changed_counter = 0;
	obj = g_object_new(G_TYPE_OBJECT, NULL);
	g_list_store_append(model, obj);
	birb_assert_list_model_n_items(model, 1);
	g_assert_cmpuint(items_changed_counter, ==, 1);
	g_assert_cmpuint(n_items_changed_counter, ==, 1);
	g_assert_cmpuint(properties_changed_counter, ==, 1);
	g_clear_object(&obj);

	/* Add another item without clearing the counters to make sure we're still
	 * using our pointer and it hasn't been cleared or anything.
	 */
	obj = g_object_new(G_TYPE_OBJECT, NULL);
	g_list_store_append(model, obj);
	birb_assert_list_model_n_items(model, 2);
	g_assert_cmpuint(items_changed_counter, ==, 2);
	g_assert_cmpuint(n_items_changed_counter, ==, 2);
	g_assert_cmpuint(properties_changed_counter, ==, 2);
	g_clear_object(&obj);

	/* Clear the counter and add an item again. */
	items_changed_counter = 0;
	n_items_changed_counter = 0;
	properties_changed_counter = 0;
	obj = g_object_new(G_TYPE_OBJECT, NULL);
	g_list_store_append(model, obj);
	birb_assert_list_model_n_items(model, 3);
	g_assert_cmpuint(items_changed_counter, ==, 1);
	g_assert_cmpuint(n_items_changed_counter, ==, 1);
	g_assert_cmpuint(properties_changed_counter, ==, 1);
	g_clear_object(&obj);

	/* Cleanup. */
	g_assert_finalize_object(model);
}

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

	g_test_add_func("/birb/count/list-items-changed",
	                test_birb_count_list_items_changed);

	return g_test_run();
}
