libpurple/tests/test_person.c

changeset 43265
7960b5f85729
parent 43171
914049a55a72
child 43301
0e43dc8462e8
--- a/libpurple/tests/test_person.c	Tue May 20 17:21:16 2025 -0500
+++ b/libpurple/tests/test_person.c	Thu Jun 12 23:48:22 2025 -0500
@@ -18,32 +18,9 @@
 
 #include <glib.h>
 
-#include <purple.h>
+#include <birb.h>
 
-/******************************************************************************
- * Callbacks
- *****************************************************************************/
-static void
-test_purple_person_items_changed_cb(G_GNUC_UNUSED GListModel *model,
-                                    G_GNUC_UNUSED guint position,
-                                    G_GNUC_UNUSED guint removed,
-                                    G_GNUC_UNUSED guint added,
-                                    gpointer data)
-{
-	guint *counter = data;
-
-	*counter = *counter + 1;
-}
-
-static void
-test_purple_person_notify_cb(G_GNUC_UNUSED GObject *obj,
-                             G_GNUC_UNUSED GParamSpec *pspec,
-                             gpointer data)
-{
-	guint *called = data;
-
-	*called = *called + 1;
-}
+#include <purple.h>
 
 /******************************************************************************
  * Tests
@@ -133,16 +110,17 @@
 	PurpleAvatar *avatar = NULL;
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
-	guint called = 0;
+	guint avatar_counter = 0;
+	guint avatar_for_display_counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::avatar",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
-	g_signal_connect(person, "notify::avatar-for-display",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "avatar", &avatar_counter);
+	birb_count_property_changed(G_OBJECT(person), "avatar-for-display",
+	                            &avatar_for_display_counter);
 	avatar = g_object_new(PURPLE_TYPE_AVATAR, NULL);
 	purple_person_set_avatar(person, avatar);
-	g_assert_cmpuint(called, ==, 2);
+	g_assert_cmpuint(avatar_counter, ==, 1);
+	g_assert_cmpuint(avatar_for_display_counter, ==, 1);
 
 	info = purple_contact_info_new("id");
 	purple_person_add_contact_info(person, info);
@@ -165,31 +143,30 @@
 	PurpleAvatar *avatar = NULL;
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
-	guint called = 0;
+	guint counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::avatar-for-display",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "avatar-for-display",
+	                            &counter);
 
 	info = purple_contact_info_new("id");
 	avatar = g_object_new(PURPLE_TYPE_AVATAR, NULL);
 	purple_contact_info_set_avatar(info, avatar);
 	purple_person_add_contact_info(person, info);
 
-	g_assert_cmpuint(called, ==, 1);
-	called = 0;
+	g_assert_cmpuint(counter, ==, 1);
 
-	/* Make sure the person's alias is overriding the contact info. */
+	/* Make sure the person's avatar is overriding the contact info. */
 	g_assert_true(purple_person_get_avatar_for_display(person) == avatar);
 	g_clear_object(&avatar);
 
 	/* Now change the avatar on the contact info an verify that we not notified
 	 * of the property changing.
 	 */
-	called = 0;
+	counter = 0;
 	avatar = g_object_new(PURPLE_TYPE_AVATAR, NULL);
 	purple_contact_info_set_avatar(info, avatar);
-	g_assert_cmpuint(called, ==, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	/* Contact info's have a reference on the person, so the easiest way to
 	 * remove that is to remove them from the person.
@@ -206,32 +183,29 @@
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
 	const char *color = NULL;
-	guint color_called = 0;
-	guint color_for_display_called = 0;
+	guint color_counter = 0;
+	guint color_for_display_counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::color",
-	                 G_CALLBACK(test_purple_person_notify_cb), &color_called);
-	g_signal_connect(person, "notify::color-for-display",
-	                 G_CALLBACK(test_purple_person_notify_cb),
-	                 &color_for_display_called);
+	birb_count_property_changed(G_OBJECT(person), "color", &color_counter);
+	birb_count_property_changed(G_OBJECT(person), "color-for-display",
+	                            &color_for_display_counter);
 	purple_person_set_color(person, "#abcdef");
 
-	g_assert_cmpuint(color_called, ==, 1);
-	color_called = 0;
-
-	g_assert_cmpuint(color_for_display_called, ==, 1);
-	color_for_display_called = 0;
+	g_assert_cmpuint(color_counter, ==, 1);
+	g_assert_cmpuint(color_for_display_counter, ==, 1);
 
 	/* Make sure the person's color is overriding the contact info. */
+	color_counter = 0;
+	color_for_display_counter = 0;
 	info = purple_contact_info_new("id");
 	purple_person_add_contact_info(person, info);
 
 	color = purple_person_get_color_for_display(person);
 	g_assert_cmpstr(color, ==, "#abcdef");
 
-	g_assert_cmpuint(color_called, ==, 0);
-	g_assert_cmpuint(color_for_display_called, ==, 0);
+	g_assert_cmpuint(color_counter, ==, 0);
+	g_assert_cmpuint(color_for_display_counter, ==, 0);
 
 	/* Contact info's have a reference on the person, so the easiest way to
 	 * remove that is to remove them from the person.
@@ -248,28 +222,28 @@
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
 	const char *color = NULL;
-	guint called = 0;
+	guint counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::color-for-display",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "color-for-display",
+	                            &counter);
 
 	info = purple_contact_info_new("id");
 	purple_contact_info_set_color(info, "#012345");
 	purple_person_add_contact_info(person, info);
 
-	g_assert_cmpuint(called, ==, 1);
-	called = 0;
+	g_assert_cmpuint(counter, ==, 1);
 
-	/* Make sure the person's alias is overriding the contact info. */
+	/* Make sure the person's color is overriding the contact info. */
 	color = purple_person_get_color_for_display(person);
 	g_assert_cmpstr(color, ==, "#012345");
 
-	/* Now change the avatar on the contact info and verify that we are
-	 * notified of the property changing.
+	/* Now change the color on the contact info and verify that we are notified
+	 * of the property changing.
 	 */
+	counter = 0;
 	purple_contact_info_set_color(info, "#6789ab");
-	g_assert_cmpuint(called, ==, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	/* Contact info's have a reference on the person, so the easiest way to
 	 * remove that is to remove them from the person.
@@ -308,15 +282,15 @@
 test_purple_person_name_for_display_contact(void) {
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
-	guint called = 0;
+	guint counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::name-for-display",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "name-for-display",
+	                            &counter);
 
 	info = purple_contact_info_new("id");
 	purple_person_add_contact_info(person, info);
-	g_assert_cmpuint(called, ==, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	/* Make sure the name for display matches the id of the contact. */
 	g_assert_cmpstr(purple_person_get_name_for_display(person), ==, "id");
@@ -324,9 +298,10 @@
 	/* Now set a username on the contact and verify that the name for display
 	 * matches and that the notify signal was emitted for the property.
 	 */
+	counter = 0;
 	purple_contact_info_set_username(info, "clu");
 	g_assert_cmpstr(purple_person_get_name_for_display(person), ==, "clu");
-	g_assert_cmpuint(called, ==, 2);
+	g_assert_cmpuint(counter, ==, 1);
 
 	/* Contact info's have a reference on the person, so the easiest way to
 	 * remove that is to remove them from the person.
@@ -342,31 +317,26 @@
 	PurpleContactInfo *info = NULL;
 	PurplePerson *person = NULL;
 	PurplePerson *person1 = NULL;
-	guint n_items = 0;
 	gboolean removed = FALSE;
-	guint changed = 0;
+	guint counter = 0;
 
 	info = purple_contact_info_new("id");
 	person = purple_person_new();
-	g_signal_connect(person, "items-changed",
-	                 G_CALLBACK(test_purple_person_items_changed_cb), &changed);
+	birb_count_list_model_items_changed(G_LIST_MODEL(person), &counter);
 
-	n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-	g_assert_cmpuint(n_items, ==, 0);
+	birb_assert_list_model_n_items(person, 0);
 	purple_person_add_contact_info(person, info);
-	n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-	g_assert_cmpuint(n_items, ==, 1);
-	g_assert_cmpuint(changed, ==, 1);
+	birb_assert_list_model_n_items(person, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	person1 = purple_contact_info_get_person(info);
 	g_assert_true(person1 == person);
 
-	changed = 0;
+	counter = 0;
 	removed = purple_person_remove_contact_info(person, info);
 	g_assert_true(removed);
-	g_assert_cmpuint(changed, ==, 1);
-	n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-	g_assert_cmpuint(n_items, ==, 0);
+	g_assert_cmpuint(counter, ==, 1);
+	birb_assert_list_model_n_items(person, 0);
 
 	person1 = purple_contact_info_get_person(info);
 	g_assert_null(person1);
@@ -379,23 +349,20 @@
 test_purple_person_contacts_multiple(void) {
 	PurplePerson *person = NULL;
 	GPtrArray *infos = NULL;
-	guint n_items = 0;
 	const int n_infos = 5;
-	guint changed = 0;
+	guint counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "items-changed",
-	                 G_CALLBACK(test_purple_person_items_changed_cb), &changed);
+	birb_count_list_model_items_changed(G_LIST_MODEL(person), &counter);
 
 	infos = g_ptr_array_new_full(n_infos, g_object_unref);
 	for(int i = 0; i < n_infos; i++) {
 		PurpleContactInfo *info = NULL;
 		char *username = NULL;
 
-		changed = 0;
+		counter = 0;
 
-		n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-		g_assert_cmpuint(n_items, ==, i);
+		birb_assert_list_model_n_items(person, i);
 
 		username = g_strdup_printf("username%d", i);
 		info = purple_contact_info_new(NULL);
@@ -409,32 +376,27 @@
 		 * happened.
 		 */
 		purple_person_add_contact_info(person, info);
-		n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-		g_assert_cmpuint(n_items, ==, i + 1);
-		g_assert_cmpuint(changed, ==, 1);
+		birb_assert_list_model_n_items(person, i + 1);
+		g_assert_cmpuint(counter, ==, 1);
 	}
 
 	for(int i = 0; i < n_infos; i++) {
 		PurpleContactInfo *info = g_ptr_array_index(infos, i);
 		gboolean removed = FALSE;
 
-		changed = 0;
+		counter = 0;
 
-		n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-		g_assert_cmpuint(n_items, ==, n_infos - i);
+		birb_assert_list_model_n_items(person, n_infos - i);
 
 		removed = purple_person_remove_contact_info(person, info);
 		g_assert_true(removed);
 
-		n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-		g_assert_cmpuint(n_items, ==, n_infos - (i + 1));
-
-		g_assert_cmpuint(changed, ==, 1);
+		birb_assert_list_model_n_items(person, n_infos - (i + 1));
+		g_assert_cmpuint(counter, ==, 1);
 	}
 
 	/* Final sanity check that the person has no more contacts. */
-	n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-	g_assert_cmpuint(n_items, ==, 0);
+	birb_assert_list_model_n_items(person, 0);
 
 	g_ptr_array_free(infos, TRUE);
 
@@ -447,11 +409,11 @@
 	PurpleContactInfo *priority = NULL;
 	PurplePerson *person = NULL;
 	PurplePresence *presence = NULL;
-	guint called = 0;
+	guint counter = 0;
 
 	person = purple_person_new();
-	g_signal_connect(person, "notify::priority-contact-info",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "priority-contact-info",
+	                            &counter);
 	priority = purple_person_get_priority_contact_info(person);
 	g_assert_null(priority);
 
@@ -464,7 +426,7 @@
 	purple_presence_set_primitive(presence,
 	                              PURPLE_PRESENCE_PRIMITIVE_AVAILABLE);
 
-	g_assert_cmpuint(called, ==, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	priority = purple_person_get_priority_contact_info(person);
 	g_assert_true(priority == info);
@@ -485,9 +447,8 @@
 	PurpleContactInfo *sorted_contact = NULL;
 	PurplePerson *person = NULL;
 	PurplePresence *sorted_presence = NULL;
-	guint called = 0;
+	guint counter = 0;
 	int n_infos = 5;
-	guint n_items = 0;
 
 	/* This unit test is a bit complicated, but it adds 5 contact infos to a
 	 * person all whose presences are set to offline. After adding all the
@@ -501,8 +462,8 @@
 	 * priority-contact property.
 	 */
 	person = purple_person_new();
-	g_signal_connect(person, "notify::priority-contact-info",
-	                 G_CALLBACK(test_purple_person_notify_cb), &called);
+	birb_count_property_changed(G_OBJECT(person), "priority-contact-info",
+	                            &counter);
 	priority = purple_person_get_priority_contact_info(person);
 	g_assert_null(priority);
 
@@ -511,10 +472,10 @@
 		PurpleContactInfo *info = NULL;
 		char *username = NULL;
 
-		/* Set called to 0 as it shouldn't be called as the priority contact
+		/* Set counter to 0 as it shouldn't be called as the priority contact
 		 * info shouldn't change except for the first index.
 		 */
-		called = 0;
+		counter = 0;
 
 		/* Now create a real contact. */
 		username = g_strdup_printf("username%d", i + 1);
@@ -526,9 +487,9 @@
 
 		if(i == 0) {
 			first = g_object_ref(info);
-			g_assert_cmpuint(called, ==, 1);
+			g_assert_cmpuint(counter, ==, 1);
 		} else {
-			g_assert_cmpuint(called, ==, 0);
+			g_assert_cmpuint(counter, ==, 0);
 
 			if(i == n_infos - 2) {
 				PurplePresence *presence = NULL;
@@ -546,8 +507,7 @@
 		g_clear_object(&info);
 	}
 
-	n_items = g_list_model_get_n_items(G_LIST_MODEL(person));
-	g_assert_cmpuint(n_items, ==, n_infos);
+	birb_assert_list_model_n_items(person, n_infos);
 
 	priority = purple_person_get_priority_contact_info(person);
 	g_assert_true(priority == first);
@@ -556,12 +516,12 @@
 	/* Now set the second from the last contact info's status to available, and
 	 * verify that that contact info is now the priority contact info.
 	 */
-	called = 0;
+	counter = 0;
 	purple_presence_set_primitive(sorted_presence,
 	                              PURPLE_PRESENCE_PRIMITIVE_AVAILABLE);
 	priority = purple_person_get_priority_contact_info(person);
 	g_assert_true(priority == sorted_contact);
-	g_assert_cmpuint(called, ==, 1);
+	g_assert_cmpuint(counter, ==, 1);
 
 	/* Cleanup. */
 	purple_person_remove_all_contact_infos(person);

mercurial