Clean up the Purple.ProtocolConversation unit tests

Mon, 02 Dec 2024 20:45:33 -0600

author
Gary Kramlich <grim@reaperworld.com>
date
Mon, 02 Dec 2024 20:45:33 -0600
changeset 43095
be478d840409
parent 43094
4ff60371673f
child 43096
a242e45846bb

Clean up the Purple.ProtocolConversation unit tests

This replaces the use of a main loop with g_main_context_iteration and removes
the test ui as it's no longer necessary.

Testing Done:
Ran the tests under valgrind and called in the turtles for the rest.

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

libpurple/tests/test_protocol_conversation.c file | annotate | diff | comparison | revisions
--- a/libpurple/tests/test_protocol_conversation.c	Sat Nov 30 02:54:29 2024 -0600
+++ b/libpurple/tests/test_protocol_conversation.c	Mon Dec 02 20:45:33 2024 -0600
@@ -26,32 +26,13 @@
 
 #include <purple.h>
 
-#include "test_ui.h"
-
 /******************************************************************************
  * Globals
  *****************************************************************************/
 
-/* Since we're using GTask to test asynchronous functions, we need to use a
- * main loop.
- */
-static GMainLoop *loop = NULL;
-
 #define TEST_PURPLE_PROTOCOL_CONVERSATION_DOMAIN (g_quark_from_static_string("test-protocol-conversation"))
 
 /******************************************************************************
- * Helpers
- *****************************************************************************/
-static gboolean
-test_purple_protocol_conversation_timeout_cb(gpointer data) {
-	g_main_loop_quit(data);
-
-	g_warning("timed out waiting for the callback function to be called");
-
-	return G_SOURCE_REMOVE;
-}
-
-/******************************************************************************
  * TestProtocolConversationEmpty implementation
  *****************************************************************************/
 G_DECLARE_FINAL_TYPE(TestPurpleProtocolConversationEmpty,
@@ -1096,59 +1077,42 @@
 		g_assert_no_error(error);
 		g_assert_true(result);
 	}
-
-	g_main_loop_quit(loop);
 }
 
-static gboolean
-test_purple_protocol_conversation_send_message_idle(gpointer data) {
+static void
+test_purple_protocol_conversation_send_message_normal(gconstpointer data) {
+	TestPurpleProtocolConversation *protocol = NULL;
 	PurpleAccount *account = NULL;
 	PurpleConversation *conversation = NULL;
 	PurpleMessage *message = NULL;
-	PurpleProtocolConversation *protocol = data;
+
+	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
+	                        NULL);
+	protocol->should_error = GPOINTER_TO_INT(data);
 
 	account = purple_account_new("test", "test");
-	g_object_set_data_full(G_OBJECT(protocol), "account", account, g_object_unref);
-
 	conversation = g_object_new(
 		PURPLE_TYPE_CONVERSATION,
 		"account", account,
 		"name", "this is required at the moment",
 		"type", PURPLE_CONVERSATION_TYPE_DM,
 		NULL);
-	g_object_set_data_full(G_OBJECT(protocol), "conversation", conversation,
-	                       g_object_unref);
 
 	message = g_object_new(PURPLE_TYPE_MESSAGE, NULL);
-	g_object_set_data_full(G_OBJECT(protocol), "message", message,
-	                       g_object_unref);
-
-	purple_protocol_conversation_send_message_async(protocol, conversation,
-	                                                message, NULL,
+	purple_protocol_conversation_send_message_async(PURPLE_PROTOCOL_CONVERSATION(protocol),
+	                                                conversation, message, NULL,
 	                                                test_purple_protocol_conversation_send_message_cb,
 	                                                NULL);
 
-	return G_SOURCE_REMOVE;
-}
-
-static void
-test_purple_protocol_conversation_send_message_normal(gconstpointer data) {
-	TestPurpleProtocolConversation *protocol = NULL;
-
-	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
-	                        NULL);
-	protocol->should_error = GPOINTER_TO_INT(data);
-
-	g_idle_add(test_purple_protocol_conversation_send_message_idle, protocol);
-	g_timeout_add_seconds(10, test_purple_protocol_conversation_timeout_cb,
-	                      loop);
-
-	g_main_loop_run(loop);
+	g_main_context_iteration(NULL, FALSE);
 
 	g_assert_cmpuint(protocol->send_message_async, ==, 1);
 	g_assert_cmpuint(protocol->send_message_finish, ==, 1);
 
-	g_clear_object(&protocol);
+	g_assert_finalize_object(message);
+	g_assert_finalize_object(conversation);
+	g_assert_finalize_object(protocol);
+	g_clear_object(&account);
 }
 
 /******************************************************************************
@@ -1178,54 +1142,39 @@
 		g_assert_no_error(error);
 		g_assert_true(result);
 	}
-
-	g_main_loop_quit(loop);
 }
 
-static gboolean
-test_purple_protocol_conversation_set_topic_idle(gpointer data) {
+static void
+test_purple_protocol_conversation_set_topic_normal(gconstpointer data) {
+	TestPurpleProtocolConversation *protocol = NULL;
 	PurpleAccount *account = NULL;
 	PurpleConversation *conversation = NULL;
-	PurpleProtocolConversation *protocol = data;
+
+	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
+	                        NULL);
+	protocol->should_error = GPOINTER_TO_INT(data);
 
 	account = purple_account_new("test", "test");
-	g_object_set_data_full(G_OBJECT(protocol), "account", account, g_object_unref);
-
 	conversation = g_object_new(
 		PURPLE_TYPE_CONVERSATION,
 		"account", account,
 		"name", "this is required at the moment",
 		"type", PURPLE_CONVERSATION_TYPE_DM,
 		NULL);
-	g_object_set_data_full(G_OBJECT(protocol), "conversation", conversation,
-	                       g_object_unref);
 
-	purple_protocol_conversation_set_topic_async(protocol, conversation,
-	                                             "woo hoo", NULL,
+	purple_protocol_conversation_set_topic_async(PURPLE_PROTOCOL_CONVERSATION(protocol),
+	                                             conversation, "woo hoo", NULL,
 	                                             test_purple_protocol_conversation_set_topic_cb,
 	                                             NULL);
 
-	return G_SOURCE_REMOVE;
-}
-
-static void
-test_purple_protocol_conversation_set_topic_normal(gconstpointer data) {
-	TestPurpleProtocolConversation *protocol = NULL;
-
-	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
-	                        NULL);
-	protocol->should_error = GPOINTER_TO_INT(data);
-
-	g_idle_add(test_purple_protocol_conversation_set_topic_idle, protocol);
-	g_timeout_add_seconds(10, test_purple_protocol_conversation_timeout_cb,
-	                      loop);
-
-	g_main_loop_run(loop);
+	g_main_context_iteration(NULL, FALSE);
 
 	g_assert_cmpuint(protocol->set_topic_async, ==, 1);
 	g_assert_cmpuint(protocol->set_topic_finish, ==, 1);
 
-	g_clear_object(&protocol);
+	g_assert_finalize_object(conversation);
+	g_assert_finalize_object(protocol);
+	g_clear_object(&account);
 }
 
 /******************************************************************************
@@ -1280,50 +1229,34 @@
 		g_assert_no_error(error);
 		g_assert_true(result);
 	}
-
-	g_main_loop_quit(loop);
-}
-
-static gboolean
-test_purple_protocol_conversation_join_channel_idle(gpointer data) {
-	PurpleAccount *account = NULL;
-	PurpleChannelJoinDetails *details = NULL;
-	PurpleProtocolConversation *protocol = data;
-
-	account = purple_account_new("test", "test");
-	g_object_set_data_full(G_OBJECT(protocol), "account", account,
-	                       g_object_unref);
-
-	details = purple_channel_join_details_new(0, FALSE, 0, FALSE, 0);
-	g_object_set_data_full(G_OBJECT(protocol), "details", details,
-	                       g_object_unref);
-
-	purple_protocol_conversation_join_channel_async(protocol, account, details,
-	                                                NULL,
-	                                                test_purple_protocol_conversation_join_channel_cb,
-	                                                NULL);
-
-	return G_SOURCE_REMOVE;
 }
 
 static void
 test_purple_protocol_conversation_join_channel_normal(gconstpointer data) {
 	TestPurpleProtocolConversation *protocol = NULL;
+	PurpleAccount *account = NULL;
+	PurpleChannelJoinDetails *details = NULL;
 
 	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
 	                        NULL);
 	protocol->should_error = GPOINTER_TO_INT(data);
 
-	g_idle_add(test_purple_protocol_conversation_join_channel_idle, protocol);
-	g_timeout_add_seconds(10, test_purple_protocol_conversation_timeout_cb,
-	                      loop);
+	account = purple_account_new("test", "test");
+	details = purple_channel_join_details_new(0, FALSE, 0, FALSE, 0);
 
-	g_main_loop_run(loop);
+	purple_protocol_conversation_join_channel_async(PURPLE_PROTOCOL_CONVERSATION(protocol),
+	                                                account, details, NULL,
+	                                                test_purple_protocol_conversation_join_channel_cb,
+	                                                NULL);
+
+	g_main_context_iteration(NULL, FALSE);
 
 	g_assert_cmpuint(protocol->join_channel_async, ==, 1);
 	g_assert_cmpuint(protocol->join_channel_finish, ==, 1);
 
-	g_clear_object(&protocol);
+	g_assert_finalize_object(protocol);
+	g_assert_finalize_object(details);
+	g_clear_object(&account);
 }
 
 /******************************************************************************
@@ -1353,55 +1286,39 @@
 		g_assert_no_error(error);
 		g_assert_true(result);
 	}
-
-	g_main_loop_quit(loop);
 }
 
-static gboolean
-test_purple_protocol_conversation_set_avatar_idle(gpointer data) {
+static void
+test_purple_protocol_conversation_set_avatar_normal(gconstpointer data) {
+	TestPurpleProtocolConversation *protocol = NULL;
 	PurpleAccount *account = NULL;
 	PurpleConversation *conversation = NULL;
-	PurpleProtocolConversation *protocol = data;
+
+	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
+	                        NULL);
+	protocol->should_error = GPOINTER_TO_INT(data);
 
 	account = purple_account_new("test", "test");
-	g_object_set_data_full(G_OBJECT(protocol), "account", account,
-	                       g_object_unref);
-
 	conversation = g_object_new(
 		PURPLE_TYPE_CONVERSATION,
 		"account", account,
 		"name", "this is required at the moment",
 		"type", PURPLE_CONVERSATION_TYPE_DM,
 		NULL);
-	g_object_set_data_full(G_OBJECT(protocol), "conversation", conversation,
-	                       g_object_unref);
 
-	purple_protocol_conversation_set_avatar_async(protocol, conversation,
-	                                              NULL, NULL,
+	purple_protocol_conversation_set_avatar_async(PURPLE_PROTOCOL_CONVERSATION(protocol),
+	                                              conversation, NULL, NULL,
 	                                              test_purple_protocol_conversation_set_avatar_cb,
 	                                              NULL);
 
-	return G_SOURCE_REMOVE;
-}
-
-static void
-test_purple_protocol_conversation_set_avatar_normal(gconstpointer data) {
-	TestPurpleProtocolConversation *protocol = NULL;
-
-	protocol = g_object_new(test_purple_protocol_conversation_get_type(),
-	                        NULL);
-	protocol->should_error = GPOINTER_TO_INT(data);
-
-	g_idle_add(test_purple_protocol_conversation_set_avatar_idle, protocol);
-	g_timeout_add_seconds(10, test_purple_protocol_conversation_timeout_cb,
-	                      loop);
-
-	g_main_loop_run(loop);
+	g_main_context_iteration(NULL, FALSE);
 
 	g_assert_cmpuint(protocol->set_avatar_async, ==, 1);
 	g_assert_cmpuint(protocol->set_avatar_finish, ==, 1);
 
-	g_clear_object(&protocol);
+	g_assert_finalize_object(conversation);
+	g_assert_finalize_object(protocol);
+	g_clear_object(&account);
 }
 
 /******************************************************************************
@@ -1454,16 +1371,10 @@
  *****************************************************************************/
 gint
 main(int argc, char **argv) {
-	int ret = 0;
-
 	g_test_init(&argc, &argv, NULL);
 
 	g_test_set_nonfatal_assertions();
 
-	test_ui_purple_init();
-
-	loop = g_main_loop_new(NULL, FALSE);
-
 	/* Empty create conversation tests. */
 	g_test_add_func("/protocol-conversation/empty/implements-create-conversation",
 	                test_purple_protocol_conversation_empty_implements_create_conversation);
@@ -1565,11 +1476,5 @@
 	g_test_add_func("/protocol-conversation/normal/send-typing",
 	                test_purple_protocol_conversation_send_typing_normal);
 
-	ret = g_test_run();
-
-	g_main_loop_unref(loop);
-
-	test_ui_purple_uninit();
-
-	return ret;
+	return g_test_run();
 }

mercurial