/*
 * Copyright (C) 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 Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <string.h>

#include <birb.h>

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
test_action_menu_foreach_action_group_counter(const char *prefix,
                                              GActionGroup *group,
                                              gpointer data)
{
	guint *counter = data;

	g_assert_nonnull(prefix);
	birb_assert_type(group, G_TYPE_SIMPLE_ACTION_GROUP);

	*counter = *counter + 1;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_action_menu_new(void) {
	BirbActionMenu *action_menu = NULL;
	GMenu *menu = NULL;
	guint counter = 0;

	action_menu = birb_action_menu_new();
	birb_assert_type(action_menu, BIRB_TYPE_ACTION_MENU);

	menu = birb_action_menu_get_menu(action_menu);
	birb_assert_type(menu, G_TYPE_MENU);

	birb_action_menu_foreach_action_group(action_menu,
	                                      test_action_menu_foreach_action_group_counter,
	                                      &counter);

	g_assert_cmpuint(counter, ==, 0);

	g_assert_finalize_object(action_menu);
}

static void
test_action_menu_new_multiple_groups(void) {
	BirbActionMenu *menu = NULL;
	GSimpleActionGroup *group1 = NULL;
	GSimpleActionGroup *group2 = NULL;
	guint counter = 0;

	menu = birb_action_menu_new();
	birb_assert_type(menu, BIRB_TYPE_ACTION_MENU);

	group1 = g_simple_action_group_new();
	birb_action_menu_add_action_group(menu, "foo", G_ACTION_GROUP(group1));

	group2 = g_simple_action_group_new();
	birb_action_menu_add_action_group(menu, "bar", G_ACTION_GROUP(group2));

	birb_action_menu_foreach_action_group(menu,
	                                      test_action_menu_foreach_action_group_counter,
	                                      &counter);

	g_assert_cmpuint(counter, ==, 2);

	g_assert_finalize_object(menu);
	g_assert_finalize_object(group1);
	g_assert_finalize_object(group2);
}

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

	g_test_set_nonfatal_assertions();

	g_test_add_func("/action-menu/new", test_action_menu_new);
	g_test_add_func("/action-menu/new-multiple-groups",
	                test_action_menu_new_multiple_groups);

	return g_test_run();
}
