Thu, 05 Dec 2024 21:31:16 -0600
Change Purple.Message:author to Purple.ConversationMember
This is necessary to show conversation specific nick names and badges in each
message and potentially more.
Testing Done:
* Sent some messages to Echo on the demo protocol plugin.
* Verified that the ircv3 status conversation was working properly.
* Called in the turtles.
Reviewed at https://reviews.imfreedom.org/r/3692/
| 42844 | 1 | /* |
| 2 | * Purple - Internet Messaging Library | |
| 3 | * Copyright (C) Pidgin Developers <devel@pidgin.im> | |
| 4 | * | |
| 5 | * This library is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU Lesser General Public | |
| 7 | * License as published by the Free Software Foundation; either | |
| 8 | * version 2 of the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | #include <glib.h> | |
| 20 | ||
| 21 | #include <purple.h> | |
| 22 | ||
| 23 | /****************************************************************************** | |
| 24 | * Callbacks | |
| 25 | *****************************************************************************/ | |
| 26 | static void | |
| 27 | test_purple_messages_items_changed_counter(G_GNUC_UNUSED GListModel *model, | |
| 28 | G_GNUC_UNUSED guint position, | |
| 29 | G_GNUC_UNUSED guint removed, | |
| 30 | G_GNUC_UNUSED guint added, | |
| 31 | gpointer data) | |
| 32 | { | |
| 33 | guint *counter = data; | |
| 34 | ||
| 35 | *counter = *counter + 1; | |
| 36 | } | |
| 37 | ||
| 38 | /****************************************************************************** | |
| 39 | * Tests | |
| 40 | *****************************************************************************/ | |
| 41 | static void | |
| 42 | test_purple_message_new_with_conversation(void) { | |
| 43 | PurpleAccount *account = NULL; | |
| 44 | PurpleConversation *conversation = NULL; | |
| 45 | PurpleMessages *messages = NULL; | |
| 46 | ||
| 47 | account = purple_account_new("test", "test"); | |
| 48 | conversation = g_object_new( | |
| 49 | PURPLE_TYPE_CONVERSATION, | |
| 50 | "account", account, | |
| 51 | NULL); | |
| 52 | ||
| 53 | messages = purple_messages_new(conversation); | |
| 54 | g_assert_true(PURPLE_IS_MESSAGES(messages)); | |
| 55 | g_assert_true(G_IS_LIST_MODEL(messages)); | |
| 56 | ||
| 57 | g_assert_finalize_object(messages); | |
| 58 | g_assert_finalize_object(conversation); | |
| 59 | g_clear_object(&account); | |
| 60 | } | |
| 61 | ||
| 62 | static void | |
| 63 | test_purple_message_new_without_conversation(void) { | |
| 64 | if(g_test_subprocess()) { | |
| 65 | PurpleMessages *messages = NULL; | |
| 66 | ||
| 67 | messages = purple_messages_new(NULL); | |
| 68 | g_assert_null(messages); | |
| 69 | ||
| 70 | g_assert_not_reached(); | |
| 71 | } | |
| 72 | ||
| 73 | g_test_trap_subprocess(NULL, 0, 0); | |
| 74 | g_test_trap_assert_stderr("*CRITICAL*IS_CONVERSATION*failed*"); | |
| 75 | } | |
| 76 | ||
| 77 | static void | |
| 78 | test_purple_messages_properties(void) { | |
| 79 | PurpleAccount *account = NULL; | |
| 80 | PurpleConversation *conversation1 = NULL; | |
| 81 | PurpleConversation *conversation2 = NULL; | |
| 82 | PurpleMessages *messages = NULL; | |
| 83 | ||
| 84 | account = purple_account_new("test", "test"); | |
| 85 | conversation1 = g_object_new( | |
| 86 | PURPLE_TYPE_CONVERSATION, | |
| 87 | "account", account, | |
| 88 | NULL); | |
| 89 | ||
| 90 | messages = g_object_new( | |
| 91 | PURPLE_TYPE_MESSAGES, | |
| 92 | "conversation", conversation1, | |
| 93 | NULL); | |
| 94 | ||
| 95 | g_object_get( | |
| 96 | G_OBJECT(messages), | |
| 97 | "conversation", &conversation2, | |
| 98 | NULL); | |
| 99 | ||
| 100 | g_assert_true(conversation2 == conversation1); | |
| 101 | g_clear_object(&conversation2); | |
| 102 | ||
| 103 | g_assert_finalize_object(messages); | |
| 104 | g_assert_finalize_object(conversation1); | |
| 105 | g_clear_object(&account); | |
| 106 | } | |
| 107 | ||
| 108 | static void | |
| 109 | test_purple_messages_add_single(void) { | |
| 110 | PurpleAccount *account = NULL; | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
111 | PurpleContactInfo *info = NULL; |
| 42844 | 112 | PurpleConversation *conversation = NULL; |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
113 | PurpleConversationMember *author = NULL; |
| 42844 | 114 | PurpleMessage *message1 = NULL; |
| 115 | PurpleMessage *message2 = NULL; | |
| 116 | PurpleMessages *messages = NULL; | |
| 117 | guint counter = 0; | |
| 118 | ||
| 119 | account = purple_account_new("test", "test"); | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
120 | info = purple_account_get_contact_info(account); |
| 42844 | 121 | conversation = g_object_new( |
| 122 | PURPLE_TYPE_CONVERSATION, | |
| 123 | "account", account, | |
| 124 | NULL); | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
125 | author = purple_conversation_find_or_add_member(conversation, info, FALSE, |
|
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
126 | NULL); |
| 42844 | 127 | |
| 128 | messages = purple_messages_new(conversation); | |
| 129 | g_signal_connect(messages, "items-changed", | |
| 130 | G_CALLBACK(test_purple_messages_items_changed_counter), | |
| 131 | &counter); | |
| 132 | ||
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
133 | message1 = purple_message_new(author, "test message"); |
| 42844 | 134 | purple_messages_add(messages, message1); |
| 135 | g_assert_cmpuint(counter, ==, 1); | |
| 136 | ||
| 137 | message2 = g_list_model_get_item(G_LIST_MODEL(messages), 0); | |
| 138 | g_assert_true(PURPLE_IS_MESSAGE(message2)); | |
| 139 | g_assert_true(message2 == message1); | |
| 140 | g_clear_object(&message2); | |
| 141 | ||
| 142 | g_assert_finalize_object(messages); | |
| 143 | g_assert_finalize_object(message1); | |
| 144 | g_assert_finalize_object(conversation); | |
| 145 | g_clear_object(&account); | |
| 146 | } | |
| 147 | ||
| 148 | static void | |
| 149 | test_purple_messages_add_multiple(void) { | |
| 150 | PurpleAccount *account = NULL; | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
151 | PurpleContactInfo *info = NULL; |
| 42844 | 152 | PurpleConversation *conversation = NULL; |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
153 | PurpleConversationMember *author = NULL; |
| 42844 | 154 | PurpleMessage *message = NULL; |
| 155 | PurpleMessage *message1 = NULL; | |
| 156 | PurpleMessage *message2 = NULL; | |
| 157 | PurpleMessages *messages = NULL; | |
| 158 | GDateTime *dt1 = NULL; | |
| 159 | GDateTime *dt2 = NULL; | |
| 160 | GTimeZone *zone = NULL; | |
| 161 | guint counter = 0; | |
| 162 | ||
| 163 | /* This test adds two messages to the collection, the first one has an | |
| 164 | * older timestamp than the first, which lets us test the automatic | |
| 165 | * sorting. | |
| 166 | */ | |
| 167 | ||
| 168 | account = purple_account_new("test", "test"); | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
169 | info = purple_account_get_contact_info(account); |
| 42844 | 170 | conversation = g_object_new( |
| 171 | PURPLE_TYPE_CONVERSATION, | |
| 172 | "account", account, | |
| 173 | NULL); | |
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
174 | author = purple_conversation_find_or_add_member(conversation, info, FALSE, |
|
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
175 | NULL); |
| 42844 | 176 | |
| 177 | messages = purple_messages_new(conversation); | |
| 178 | g_signal_connect(messages, "items-changed", | |
| 179 | G_CALLBACK(test_purple_messages_items_changed_counter), | |
| 180 | &counter); | |
| 181 | ||
| 182 | zone = g_time_zone_new_utc(); | |
| 183 | ||
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
184 | message1 = purple_message_new(author, "second message"); |
| 42844 | 185 | dt1 = g_date_time_new_from_iso8601("2024-08-07T03:07:33+0000", zone); |
| 186 | purple_message_set_timestamp(message1, dt1); | |
| 187 | g_clear_pointer(&dt1, g_date_time_unref); | |
| 188 | purple_messages_add(messages, message1); | |
| 189 | g_assert_cmpuint(counter, ==, 1); | |
| 190 | ||
|
43100
e6df74d36862
Change Purple.Message:author to Purple.ConversationMember
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
191 | message2 = purple_message_new(author, "first message"); |
| 42844 | 192 | dt2 = g_date_time_new_from_iso8601("2024-08-07T03:06:33+0000", zone); |
| 193 | purple_message_set_timestamp(message2, dt2); | |
| 194 | g_clear_pointer(&dt2, g_date_time_unref); | |
| 195 | purple_messages_add(messages, message2); | |
| 196 | g_assert_cmpuint(counter, ==, 2); | |
| 197 | ||
| 198 | /* Make sure that the first item in the list is message2. */ | |
| 199 | message = g_list_model_get_item(G_LIST_MODEL(messages), 0); | |
| 200 | g_assert_true(PURPLE_IS_MESSAGE(message)); | |
| 201 | g_assert_true(message == message2); | |
| 202 | g_clear_object(&message); | |
| 203 | ||
| 204 | /* Make sure that the second item in the list is message1. */ | |
| 205 | message = g_list_model_get_item(G_LIST_MODEL(messages), 1); | |
| 206 | g_assert_true(PURPLE_IS_MESSAGE(message)); | |
| 207 | g_assert_true(message == message1); | |
| 208 | g_clear_object(&message); | |
| 209 | ||
| 210 | g_clear_pointer(&zone, g_time_zone_unref); | |
| 211 | g_assert_finalize_object(messages); | |
| 212 | g_assert_finalize_object(message1); | |
| 213 | g_assert_finalize_object(message2); | |
| 214 | g_assert_finalize_object(conversation); | |
| 215 | g_clear_object(&account); | |
| 216 | } | |
| 217 | ||
| 218 | /****************************************************************************** | |
| 219 | * Main | |
| 220 | *****************************************************************************/ | |
| 221 | int | |
| 222 | main(int argc, char *argv[]) { | |
| 223 | g_test_init(&argc, &argv, NULL); | |
| 224 | g_test_set_nonfatal_assertions(); | |
| 225 | ||
| 226 | g_test_add_func("/messages/new/with-conversation", | |
| 227 | test_purple_message_new_with_conversation); | |
| 228 | g_test_add_func("/messages/new/without-conversation", | |
| 229 | test_purple_message_new_without_conversation); | |
| 230 | ||
| 231 | g_test_add_func("/messages/properties", test_purple_messages_properties); | |
| 232 | ||
| 233 | g_test_add_func("/messages/add/single", test_purple_messages_add_single); | |
| 234 | g_test_add_func("/messages/add/multiple", | |
| 235 | test_purple_messages_add_multiple); | |
| 236 | ||
| 237 | return g_test_run(); | |
| 238 | } |