libpurple/tests/test_create_conversation_details.c

Thu, 30 Jan 2025 22:25:41 -0600

author
Gary Kramlich <grim@reaperworld.com>
date
Thu, 30 Jan 2025 22:25:41 -0600
changeset 43164
7709065d96eb
parent 42866
4b201e18638f
permissions
-rw-r--r--

Add Purple.CreateConversationDetails.is_valid

This does some basic checking and creates standard errors for invalid settings
on Purple.CreateConversationDetails.

Testing Done:
Ran the unit tests under valgrind and called in the turtles as well.

Bugs closed: PIDGIN-18032

Reviewed at https://reviews.imfreedom.org/r/3791/

/*
 * 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 library 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 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_create_conversation_details_new(void) {
	PurpleCreateConversationDetails *details = NULL;

	details = purple_create_conversation_details_new(9);
	g_assert_true(PURPLE_IS_CREATE_CONVERSATION_DETAILS(details));

	g_assert_finalize_object(details);
}

static void
test_purple_create_conversation_details_properties(void) {
	PurpleCreateConversationDetails *details = NULL;
	GListStore *store = NULL;
	GListModel *model = NULL;
	guint max_participants = 0;

	store = g_list_store_new(PURPLE_TYPE_CONTACT);

	details = g_object_new(
		PURPLE_TYPE_CREATE_CONVERSATION_DETAILS,
		"max-participants", 9,
		"participants", store,
		NULL);

	g_assert_true(PURPLE_IS_CREATE_CONVERSATION_DETAILS(details));

	g_object_get(
		G_OBJECT(details),
		"max-participants", &max_participants,
		"participants", &model,
		NULL);

	g_assert_cmpuint(max_participants, ==, 9);

	g_assert_true(G_IS_LIST_MODEL(model));
	g_assert_true(model == G_LIST_MODEL(store));
	g_clear_object(&model);

	g_assert_finalize_object(details);
	g_assert_finalize_object(store);
}

static void
test_purple_create_conversation_details_is_valid_null(void) {
	PurpleCreateConversationDetails *details = NULL;
	GError *error = NULL;
	gboolean is_valid = FALSE;

	details = purple_create_conversation_details_new(1);

	is_valid = purple_create_conversation_details_is_valid(details, &error);
	g_assert_error(error, PURPLE_CREATE_CONVERSATION_DETAILS_ERROR,
	               PURPLE_CREATE_CONVERSATION_DETAILS_ERROR_NO_PARTICIPANTS);
	g_clear_error(&error);
	g_assert_false(is_valid);

	g_assert_finalize_object(details);
}

static void
test_purple_create_conversation_details_is_valid_empty(void) {
	PurpleCreateConversationDetails *details = NULL;
	GListStore *participants = NULL;
	GError *error = NULL;
	gboolean is_valid = FALSE;

	details = purple_create_conversation_details_new(1);
	participants = g_list_store_new(PURPLE_TYPE_CONTACT);
	purple_create_conversation_details_set_participants(details,
	                                                    G_LIST_MODEL(participants));
	g_clear_object(&participants);

	is_valid = purple_create_conversation_details_is_valid(details, &error);
	g_assert_error(error, PURPLE_CREATE_CONVERSATION_DETAILS_ERROR,
	               PURPLE_CREATE_CONVERSATION_DETAILS_ERROR_NO_PARTICIPANTS);
	g_clear_error(&error);
	g_assert_false(is_valid);

	g_assert_finalize_object(details);
}


static void
test_purple_create_conversation_details_is_valid_too_many(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleCreateConversationDetails *details = NULL;
	GListStore *participants = NULL;
	GError *error = NULL;
	gboolean is_valid = FALSE;

	account = purple_account_new("test", "test");

	details = purple_create_conversation_details_new(1);
	participants = g_list_store_new(PURPLE_TYPE_CONTACT);

	/* We need to add 2 contacts to exceed the max-participants. */
	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	/* Set the participants on the details. */
	purple_create_conversation_details_set_participants(details,
	                                                    G_LIST_MODEL(participants));
	g_clear_object(&participants);

	/* Check validation. */
	is_valid = purple_create_conversation_details_is_valid(details, &error);
	g_assert_error(error, PURPLE_CREATE_CONVERSATION_DETAILS_ERROR,
	               PURPLE_CREATE_CONVERSATION_DETAILS_ERROR_TOO_MANY_PARTICIPANTS);
	g_clear_error(&error);
	g_assert_false(is_valid);

	g_assert_finalize_object(details);
	g_assert_finalize_object(account);
}

static void
test_purple_create_conversation_details_is_valid_limited(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleCreateConversationDetails *details = NULL;
	GListStore *participants = NULL;
	GError *error = NULL;
	gboolean is_valid = FALSE;

	account = purple_account_new("test", "test");

	details = purple_create_conversation_details_new(1);
	participants = g_list_store_new(PURPLE_TYPE_CONTACT);

	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	/* Set the participants on the details. */
	purple_create_conversation_details_set_participants(details,
	                                                    G_LIST_MODEL(participants));
	g_clear_object(&participants);

	/* Check validation. */
	is_valid = purple_create_conversation_details_is_valid(details, &error);
	g_assert_no_error(error);
	g_assert_true(is_valid);

	g_assert_finalize_object(details);
	g_assert_finalize_object(account);
}

static void
test_purple_create_conversation_details_is_valid_unlimited(void) {
	PurpleAccount *account = NULL;
	PurpleContact *contact = NULL;
	PurpleCreateConversationDetails *details = NULL;
	GListStore *participants = NULL;
	GError *error = NULL;
	gboolean is_valid = FALSE;

	account = purple_account_new("test", "test");

	details = purple_create_conversation_details_new(0);
	participants = g_list_store_new(PURPLE_TYPE_CONTACT);

	/* Add 3 additional contacts to spice things up! */
	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	contact = purple_contact_new(account, NULL);
	g_list_store_append(participants, contact);
	g_clear_object(&contact);

	/* Set the participants on the details. */
	purple_create_conversation_details_set_participants(details,
	                                                    G_LIST_MODEL(participants));
	g_clear_object(&participants);

	/* Check validation. */
	is_valid = purple_create_conversation_details_is_valid(details, &error);
	g_assert_no_error(error);
	g_assert_true(is_valid);

	g_assert_finalize_object(details);
	g_assert_finalize_object(account);
}

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

	g_test_add_func("/create-conversation-details/new",
	                test_purple_create_conversation_details_new);
	g_test_add_func("/create-conversation-details/properties",
	                test_purple_create_conversation_details_properties);

	g_test_add_func("/create-conversation-details/is-valid/null",
	                test_purple_create_conversation_details_is_valid_null);
	g_test_add_func("/create-conversation-details/is-valid/empty",
	                test_purple_create_conversation_details_is_valid_empty);
	g_test_add_func("/create-conversation-details/is-valid/too-many",
	                test_purple_create_conversation_details_is_valid_too_many);
	g_test_add_func("/create-conversation-details/is-valid/limited",
	                test_purple_create_conversation_details_is_valid_limited);
	g_test_add_func("/create-conversation-details/is-valid/unlimited",
	                test_purple_create_conversation_details_is_valid_unlimited);

	return g_test_run();
}

mercurial