Sat, 01 Oct 2022 01:50:52 -0500
Create and add PurpleContacts to the manager when purple_buddy_new is called
This required some additional changes to PurpleContact. Namely that the contact always has a presence and it is no longer writeable.
Testing Done:
Ran the unit tests and verified nothing funky happens when running.
We can't test that all of the properties are properly bound because we would have to start up a lot more of libpurple than I'm willing to do for something that's temporary.
Bugs closed: PIDGIN-17685
Reviewed at https://reviews.imfreedom.org/r/1873/
| 41749 | 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 | #include "test_ui.h" | |
| 24 | ||
| 25 | /****************************************************************************** | |
| 26 | * Callbacks | |
| 27 | *****************************************************************************/ | |
| 28 | static void | |
| 29 | test_purple_person_items_changed_cb(G_GNUC_UNUSED GListModel *model, | |
| 30 | G_GNUC_UNUSED guint position, | |
| 31 | G_GNUC_UNUSED guint removed, | |
| 32 | G_GNUC_UNUSED guint added, | |
| 33 | gpointer data) | |
| 34 | { | |
| 35 | gboolean *called = data; | |
| 36 | ||
| 37 | *called = TRUE; | |
| 38 | } | |
| 39 | ||
| 40 | static void | |
| 41 | test_purple_person_notify_cb(G_GNUC_UNUSED GObject *obj, | |
| 42 | G_GNUC_UNUSED GParamSpec *pspec, | |
| 43 | gpointer data) | |
| 44 | { | |
| 45 | gboolean *called = data; | |
| 46 | ||
| 47 | *called = TRUE; | |
| 48 | } | |
| 49 | ||
| 50 | /****************************************************************************** | |
| 51 | * Tests | |
| 52 | *****************************************************************************/ | |
| 53 | static void | |
| 54 | test_purple_person_new(void) { | |
| 55 | PurplePerson *person = NULL; | |
| 56 | ||
| 57 | person = purple_person_new(); | |
| 58 | ||
| 59 | g_assert_true(PURPLE_IS_PERSON(person)); | |
| 60 | ||
| 61 | g_clear_object(&person); | |
| 62 | } | |
| 63 | ||
| 64 | static void | |
| 65 | test_purple_person_properties(void) { | |
| 66 | PurpleContact *person = NULL; | |
| 67 | PurpleTags *tags = NULL; | |
| 68 | GdkPixbuf *avatar = NULL; | |
| 69 | GdkPixbuf *avatar1 = NULL; | |
| 70 | gchar *id = NULL; | |
| 71 | gchar *alias = NULL; | |
| 72 | ||
| 73 | /* Create our avatar for testing. */ | |
| 74 | avatar = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 1, 1); | |
| 75 | ||
| 76 | /* Use g_object_new so we can test setting properties by name. All of them | |
| 77 | * call the setter methods, so by doing it this way we exercise more of the | |
| 78 | * code. | |
| 79 | */ | |
| 80 | person = g_object_new( | |
| 81 | PURPLE_TYPE_PERSON, | |
| 82 | "alias", "alias", | |
| 83 | "avatar", avatar, | |
| 84 | NULL); | |
| 85 | ||
| 86 | /* Now use g_object_get to read all of the properties. */ | |
| 87 | g_object_get(person, | |
| 88 | "id", &id, | |
| 89 | "alias", &alias, | |
| 90 | "avatar", &avatar1, | |
| 91 | "tags", &tags, | |
| 92 | NULL); | |
| 93 | ||
| 94 | /* Compare all the things. */ | |
| 95 | g_assert_nonnull(id); | |
| 96 | g_assert_cmpstr(alias, ==, "alias"); | |
| 97 | g_assert_true(avatar1 == avatar); | |
| 98 | g_assert_nonnull(tags); | |
| 99 | ||
| 100 | /* Free/unref all the things. */ | |
| 101 | g_clear_pointer(&id, g_free); | |
| 102 | g_clear_pointer(&alias, g_free); | |
| 103 | g_clear_object(&avatar1); | |
| 104 | g_clear_object(&tags); | |
| 105 | ||
| 106 | g_clear_object(&avatar); | |
| 107 | g_clear_object(&person); | |
| 108 | } | |
| 109 | ||
| 110 | static void | |
| 111 | test_purple_person_contacts_single(void) { | |
| 112 | PurpleAccount *account = NULL; | |
| 113 | PurpleContact *contact = NULL; | |
| 114 | PurplePerson *person = NULL; | |
|
41761
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
115 | PurplePerson *person1 = NULL; |
| 41749 | 116 | guint n_items = 0; |
| 117 | gboolean removed = FALSE; | |
| 118 | gboolean changed = FALSE; | |
| 119 | ||
| 120 | account = purple_account_new("test", "test"); | |
| 121 | contact = purple_contact_new(account, "username"); | |
| 122 | person = purple_person_new(); | |
| 123 | g_signal_connect(person, "items-changed", | |
| 124 | G_CALLBACK(test_purple_person_items_changed_cb), &changed); | |
| 125 | ||
| 126 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 127 | g_assert_cmpuint(n_items, ==, 0); | |
| 128 | purple_person_add_contact(person, contact); | |
| 129 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 130 | g_assert_cmpuint(n_items, ==, 1); | |
| 131 | g_assert_true(changed); | |
| 132 | ||
|
41761
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
133 | person1 = purple_contact_get_person(contact); |
|
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
134 | g_assert_true(person1 == person); |
|
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
135 | |
| 41749 | 136 | changed = FALSE; |
| 137 | ||
| 138 | removed = purple_person_remove_contact(person, contact); | |
| 139 | g_assert_true(removed); | |
| 140 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 141 | g_assert_cmpuint(n_items, ==, 0); | |
| 142 | g_assert_true(changed); | |
| 143 | ||
|
41761
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
144 | person1 = purple_contact_get_person(contact); |
|
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
145 | g_assert_null(person1); |
|
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
146 | |
| 41749 | 147 | g_clear_object(&person); |
| 148 | g_clear_object(&account); | |
|
41761
9dc5d28fca99
When a Contact is added to a Person set the Contact's Person to that Person.
Gary Kramlich <grim@reaperworld.com>
parents:
41749
diff
changeset
|
149 | g_clear_object(&contact); |
| 41749 | 150 | } |
| 151 | ||
| 152 | static void | |
| 153 | test_purple_person_contacts_multiple(void) { | |
| 154 | PurpleAccount *account = NULL; | |
| 155 | PurplePerson *person = NULL; | |
| 156 | GPtrArray *contacts = NULL; | |
| 157 | guint n_items = 0; | |
| 158 | const gint n_contacts = 5; | |
| 159 | gboolean changed = FALSE; | |
| 160 | ||
| 161 | account = purple_account_new("test", "test"); | |
| 162 | person = purple_person_new(); | |
| 163 | g_signal_connect(person, "items-changed", | |
| 164 | G_CALLBACK(test_purple_person_items_changed_cb), &changed); | |
| 165 | ||
| 166 | contacts = g_ptr_array_new_full(n_contacts, g_object_unref); | |
| 167 | for(gint i = 0; i < n_contacts; i++) { | |
| 168 | PurpleContact *contact = NULL; | |
| 169 | gchar *username = NULL; | |
| 170 | ||
| 171 | changed = FALSE; | |
| 172 | ||
| 173 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 174 | g_assert_cmpuint(n_items, ==, i); | |
| 175 | ||
| 176 | username = g_strdup_printf("username%d", i); | |
| 177 | contact = purple_contact_new(account, username); | |
| 178 | g_free(username); | |
| 179 | ||
| 180 | /* Add the contact to the ptr array so we can remove it below. */ | |
| 181 | g_ptr_array_add(contacts, contact); | |
| 182 | ||
| 183 | /* Add the contact to the person and make sure that all the magic | |
| 184 | * happened. | |
| 185 | */ | |
| 186 | purple_person_add_contact(person, contact); | |
| 187 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 188 | g_assert_cmpuint(n_items, ==, i + 1); | |
| 189 | g_assert_true(changed); | |
| 190 | } | |
| 191 | ||
| 192 | for(gint i = 0; i < n_contacts; i++) { | |
| 193 | PurpleContact *contact = contacts->pdata[i]; | |
| 194 | gboolean removed = FALSE; | |
| 195 | ||
| 196 | changed = FALSE; | |
| 197 | ||
| 198 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 199 | g_assert_cmpuint(n_items, ==, n_contacts - i); | |
| 200 | ||
| 201 | removed = purple_person_remove_contact(person, contact); | |
| 202 | g_assert_true(removed); | |
| 203 | ||
| 204 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 205 | g_assert_cmpuint(n_items, ==, n_contacts - (i + 1)); | |
| 206 | ||
| 207 | g_assert_true(changed); | |
| 208 | } | |
| 209 | ||
| 210 | /* Final sanity check that the person has no more contacts. */ | |
| 211 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 212 | g_assert_cmpuint(n_items, ==, 0); | |
| 213 | ||
| 214 | g_ptr_array_free(contacts, TRUE); | |
| 215 | ||
| 216 | g_clear_object(&person); | |
| 217 | g_clear_object(&account); | |
| 218 | } | |
| 219 | ||
| 220 | static void | |
| 221 | test_purple_person_priority_single(void) { | |
| 222 | PurpleAccount *account = NULL; | |
| 223 | PurpleContact *contact = NULL; | |
| 224 | PurpleContact *priority = NULL; | |
| 225 | PurplePerson *person = NULL; | |
| 226 | PurplePresence *presence = NULL; | |
| 227 | PurpleStatus *status = NULL; | |
| 228 | PurpleStatusType *status_type = NULL; | |
| 229 | gboolean called = FALSE; | |
| 230 | ||
| 231 | account = purple_account_new("test", "test"); | |
| 232 | ||
| 233 | person = purple_person_new(); | |
| 234 | g_signal_connect(person, "notify::priority-contact", | |
| 235 | G_CALLBACK(test_purple_person_notify_cb), &called); | |
| 236 | priority = purple_person_get_priority_contact(person); | |
| 237 | g_assert_null(priority); | |
| 238 | ||
|
41771
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
239 | /* Now create a real contact. */ |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
240 | contact = purple_contact_new(account, "username"); |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
241 | purple_person_add_contact(person, contact); |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
242 | |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
243 | /* Set the status of the contact. */ |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
244 | presence = purple_contact_get_presence(contact); |
| 41749 | 245 | status_type = purple_status_type_new(PURPLE_STATUS_AVAILABLE, "available", |
| 246 | "Available", FALSE); | |
| 247 | status = purple_status_new(status_type, presence); | |
| 248 | g_object_set(G_OBJECT(presence), "active-status", status, NULL); | |
| 249 | g_clear_object(&status); | |
| 250 | ||
| 251 | g_assert_true(called); | |
| 252 | ||
| 253 | priority = purple_person_get_priority_contact(person); | |
| 254 | g_assert_true(priority == contact); | |
| 255 | ||
| 256 | purple_status_type_destroy(status_type); | |
| 257 | g_clear_object(&account); | |
| 258 | g_clear_object(&person); | |
| 259 | g_clear_object(&contact); | |
| 260 | g_clear_object(&presence); | |
| 261 | } | |
| 262 | ||
| 263 | static void | |
| 264 | test_purple_person_priority_multiple_with_change(void) { | |
| 265 | PurpleAccount *account = NULL; | |
| 266 | PurpleContact *priority = NULL; | |
| 267 | PurpleContact *first = NULL; | |
| 268 | PurpleContact *sorted_contact = NULL; | |
| 269 | PurplePerson *person = NULL; | |
| 270 | PurplePresence *sorted_presence = NULL; | |
| 271 | PurpleStatus *status = NULL; | |
| 272 | PurpleStatusType *available = NULL; | |
| 273 | PurpleStatusType *offline = NULL; | |
| 274 | gboolean changed = FALSE; | |
| 275 | gint n_contacts = 5; | |
| 276 | guint n_items = 0; | |
| 277 | ||
| 278 | /* This unit test is a bit complicated, but it adds 5 contacts to a person | |
| 279 | * all whose presences are set to offline. After adding all the contacts, | |
| 280 | * we verify that the first contact we added is the priority contact. Then | |
| 281 | * we flip the active status of the n_contacts - 2 contact to available. | |
| 282 | * This should make it the priority contact which we then assert. | |
| 283 | */ | |
| 284 | ||
| 285 | account = purple_account_new("test", "test"); | |
| 286 | ||
| 287 | /* Create our status types. */ | |
| 288 | available = purple_status_type_new(PURPLE_STATUS_AVAILABLE, "available", | |
| 289 | "Available", FALSE); | |
| 290 | offline = purple_status_type_new(PURPLE_STATUS_OFFLINE, "offline", | |
| 291 | "Offline", FALSE); | |
| 292 | ||
| 293 | /* Create the person and connected to the notify signal for the | |
| 294 | * priority-contact property. | |
| 295 | */ | |
| 296 | person = purple_person_new(); | |
| 297 | g_signal_connect(person, "notify::priority-contact", | |
| 298 | G_CALLBACK(test_purple_person_notify_cb), &changed); | |
| 299 | priority = purple_person_get_priority_contact(person); | |
| 300 | g_assert_null(priority); | |
| 301 | ||
| 302 | /* Create and add all contacts. */ | |
| 303 | for(gint i = 0; i < n_contacts; i++) { | |
| 304 | PurpleContact *contact = NULL; | |
| 305 | PurplePresence *presence = NULL; | |
| 306 | gchar *username = NULL; | |
| 307 | ||
| 308 | /* Set changed to false as it shouldn't be changed. */ | |
| 309 | changed = FALSE; | |
| 310 | ||
| 311 | /* Now create a real contact. */ | |
| 312 | username = g_strdup_printf("username%d", i + 1); | |
| 313 | contact = purple_contact_new(account, username); | |
| 314 | g_free(username); | |
|
41771
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
315 | |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
316 | /* Set the status for the contact. */ |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
317 | presence = purple_contact_get_presence(contact); |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
318 | status = purple_status_new(offline, presence); |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
319 | g_object_set(G_OBJECT(presence), "active-status", status, NULL); |
|
c5877e2c93f2
Create and add PurpleContacts to the manager when purple_buddy_new is called
Gary Kramlich <grim@reaperworld.com>
parents:
41761
diff
changeset
|
320 | g_clear_object(&status); |
| 41749 | 321 | |
| 322 | purple_person_add_contact(person, contact); | |
| 323 | ||
| 324 | if(i == 0) { | |
| 325 | first = g_object_ref(contact); | |
| 326 | g_assert_true(changed); | |
| 327 | } else { | |
| 328 | g_assert_false(changed); | |
| 329 | ||
| 330 | if(i == n_contacts - 2) { | |
| 331 | sorted_contact = g_object_ref(contact); | |
| 332 | sorted_presence = g_object_ref(presence); | |
| 333 | } | |
| 334 | } | |
| 335 | ||
| 336 | g_clear_object(&contact); | |
| 337 | } | |
| 338 | ||
| 339 | n_items = g_list_model_get_n_items(G_LIST_MODEL(person)); | |
| 340 | g_assert_cmpuint(n_items, ==, n_contacts); | |
| 341 | ||
| 342 | priority = purple_person_get_priority_contact(person); | |
| 343 | g_assert_true(priority == first); | |
| 344 | g_clear_object(&first); | |
| 345 | ||
| 346 | /* Now set the second from the last contact's status to available, and | |
| 347 | * verify that that contact is now the priority contact. | |
| 348 | */ | |
| 349 | changed = FALSE; | |
| 350 | status = purple_status_new(available, sorted_presence); | |
| 351 | g_object_set(G_OBJECT(sorted_presence), "active-status", status, NULL); | |
| 352 | g_clear_object(&status); | |
| 353 | g_assert_true(changed); | |
| 354 | priority = purple_person_get_priority_contact(person); | |
| 355 | g_assert_true(priority == sorted_contact); | |
| 356 | ||
| 357 | /* Cleanup. */ | |
| 358 | purple_status_type_destroy(offline); | |
| 359 | purple_status_type_destroy(available); | |
| 360 | ||
| 361 | g_clear_object(&sorted_contact); | |
| 362 | g_clear_object(&sorted_presence); | |
| 363 | ||
| 364 | g_clear_object(&account); | |
| 365 | g_clear_object(&person); | |
| 366 | } | |
| 367 | ||
| 368 | /****************************************************************************** | |
| 369 | * Main | |
| 370 | *****************************************************************************/ | |
| 371 | gint | |
| 372 | main(gint argc, gchar *argv[]) { | |
| 373 | g_test_init(&argc, &argv, NULL); | |
| 374 | ||
| 375 | test_ui_purple_init(); | |
| 376 | ||
| 377 | g_test_add_func("/person/new", | |
| 378 | test_purple_person_new); | |
| 379 | g_test_add_func("/person/properties", | |
| 380 | test_purple_person_properties); | |
| 381 | g_test_add_func("/person/contacts/single", | |
| 382 | test_purple_person_contacts_single); | |
| 383 | g_test_add_func("/person/contacts/multiple", | |
| 384 | test_purple_person_contacts_multiple); | |
| 385 | ||
| 386 | g_test_add_func("/person/priority/single", | |
| 387 | test_purple_person_priority_single); | |
| 388 | g_test_add_func("/person/priority/multiple-with-change", | |
| 389 | test_purple_person_priority_multiple_with_change); | |
| 390 | ||
| 391 | return g_test_run(); | |
| 392 | } |