libpurple/tests/test_presence.c

Sat, 09 Aug 2025 17:37:27 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sat, 09 Aug 2025 17:37:27 +0800
branch
bird-header-fix
changeset 43304
2599d35e9750
parent 43240
0dd5d0ab4908
permissions
-rw-r--r--

Fix the birb header path

The birb header referred would only work with birb provided by wrap casuing
build to fail because of system-installed birb dependency. The commit points
it to the correct path <birb.h>.

See: https://keep.imfreedom.org/birb/birb/file/5bf00c7d7f80/birb/meson.build#l77

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

/******************************************************************************
 * Main
 *****************************************************************************/
static void
test_purple_presence_new(void) {
	PurplePresence *presence = NULL;

	presence = purple_presence_new();
	g_assert_true(PURPLE_IS_PRESENCE(presence));

	/* Cleanup. */
	g_clear_object(&presence);
}

static void
test_purple_presence_properties(void) {
	PurplePresence *presence = NULL;
	PurplePresencePrimitive primitive = PURPLE_PRESENCE_PRIMITIVE_OFFLINE;
	GDateTime *now = NULL;
	GDateTime *login = NULL;
	GDateTime *login1 = NULL;
	GDateTime *idle = NULL;
	GDateTime *idle1 = NULL;
	char *message = NULL;
	char *emoji = NULL;
	gboolean mobile = FALSE;
	gboolean notifications_disabled = FALSE;

	/* Create the login and idle times. */
	now = g_date_time_new_now_utc();
	login = g_date_time_add_hours(now, -1);
	idle = g_date_time_add_minutes(now, -10);
	g_clear_pointer(&now, g_date_time_unref);

	/* Create the presence using g_object_new to make sure set_property is
	 * wired up correctly.
	 */
	presence = g_object_new(
		PURPLE_TYPE_PRESENCE,
		"primitive", PURPLE_PRESENCE_PRIMITIVE_AVAILABLE,
		"message", "I'll be back!",
		"emoji", "🤖",
		"login-time", login,
		"idle-time", idle,
		"mobile", TRUE,
		"notifications-disabled", TRUE,
		NULL);

	/* Grab the values via g_object_get to make sure get_property is wired up
	 * correctly.
	 */
	g_object_get(
		presence,
		"primitive", &primitive,
		"message", &message,
		"emoji", &emoji,
		"login-time", &login1,
		"idle-time", &idle1,
		"mobile", &mobile,
		"notifications-disabled", &notifications_disabled,
		NULL);

	/* Test the things! */
	g_assert_cmpint(primitive, ==, PURPLE_PRESENCE_PRIMITIVE_AVAILABLE);

	g_assert_cmpstr(message, ==, "I'll be back!");
	g_clear_pointer(&message, g_free);

	g_assert_cmpstr(emoji, ==, "🤖");
	g_clear_pointer(&emoji, g_free);

	g_assert_nonnull(login1);
	g_assert_true(g_date_time_equal(login, login1));
	g_clear_pointer(&login1, g_date_time_unref);

	g_assert_nonnull(idle1);
	g_assert_true(g_date_time_equal(idle, idle1));
	g_clear_pointer(&idle1, g_date_time_unref);

	g_assert_true(mobile);

	g_assert_true(notifications_disabled);

	/* Cleanup. */
	g_clear_pointer(&login, g_date_time_unref);
	g_clear_pointer(&idle, g_date_time_unref);
	g_clear_object(&presence);
}

/******************************************************************************
 * Signal Tests
 *****************************************************************************/
static void
test_purple_presence_primitive_changed_signal_cb(PurplePresence *presence,
                                                 PurplePresencePrimitive new_primitive,
                                                 PurplePresencePrimitive old_primitive,
                                                 gpointer data)
{
	guint *counter = data;

	g_assert_true(PURPLE_IS_PRESENCE(presence));
	g_assert_cmpuint(new_primitive, ==, PURPLE_PRESENCE_PRIMITIVE_STREAMING);
	g_assert_cmpuint(old_primitive, ==, PURPLE_PRESENCE_PRIMITIVE_OFFLINE);

	*counter = *counter + 1;
}

static void
test_purple_presence_primitive_changed_signal(void) {
	PurplePresence *presence = NULL;
	guint counter = 0;

	presence = purple_presence_new();
	g_signal_connect(presence, "primitive-changed",
	                 G_CALLBACK(test_purple_presence_primitive_changed_signal_cb),
	                 &counter);

	purple_presence_set_primitive(presence,
	                              PURPLE_PRESENCE_PRIMITIVE_STREAMING);

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(presence);
}

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

	g_test_add_func("/presence/new", test_purple_presence_new);
	g_test_add_func("/presence/properties", test_purple_presence_properties);

	g_test_add_func("/presence/primitive-primitive-changed-signal",
	                test_purple_presence_primitive_changed_signal);

	return g_test_run();
}

mercurial