Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / glistmodel.c
blobdcf571f295e36dfda0d8f623055f792758a9450b
1 /*
2 * Copyright 2015 Lars Uebernickel
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Authors: Lars Uebernickel <lars@uebernic.de>
20 #include <gio/gio.h>
22 #include <string.h>
24 static void
25 test_store_boundaries (void)
27 GListStore *store;
28 GMenuItem *item;
30 store = g_list_store_new (G_TYPE_MENU_ITEM);
32 item = g_menu_item_new (NULL, NULL);
33 g_object_add_weak_pointer (G_OBJECT (item), (gpointer *) &item);
35 /* remove an item from an empty list */
36 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
37 g_list_store_remove (store, 0);
38 g_test_assert_expected_messages ();
40 /* don't allow inserting an item past the end ... */
41 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
42 g_list_store_insert (store, 1, item);
43 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
44 g_test_assert_expected_messages ();
46 /* ... except exactly at the end */
47 g_list_store_insert (store, 0, item);
48 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
50 /* remove a non-existing item at exactly the end of the list */
51 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
52 g_list_store_remove (store, 1);
53 g_test_assert_expected_messages ();
55 g_list_store_remove (store, 0);
56 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
58 /* splice beyond the end of the list */
59 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
60 g_list_store_splice (store, 1, 0, NULL, 0);
61 g_test_assert_expected_messages ();
63 /* remove items from an empty list */
64 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
65 g_list_store_splice (store, 0, 1, NULL, 0);
66 g_test_assert_expected_messages ();
68 g_list_store_append (store, item);
69 g_list_store_splice (store, 0, 1, (gpointer *) &item, 1);
70 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
72 /* remove more items than exist */
73 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
74 g_list_store_splice (store, 0, 5, NULL, 0);
75 g_test_assert_expected_messages ();
76 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
78 g_object_unref (store);
79 g_object_unref (item);
80 g_assert_null (item);
83 static void
84 test_store_refcounts (void)
86 GListStore *store;
87 GMenuItem *items[10];
88 GMenuItem *tmp;
89 guint i;
90 guint n_items;
92 store = g_list_store_new (G_TYPE_MENU_ITEM);
94 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
95 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), 0));
97 n_items = G_N_ELEMENTS (items);
98 for (i = 0; i < n_items; i++)
100 items[i] = g_menu_item_new (NULL, NULL);
101 g_object_add_weak_pointer (G_OBJECT (items[i]), (gpointer *) &items[i]);
102 g_list_store_append (store, items[i]);
104 g_object_unref (items[i]);
105 g_assert_nonnull (items[i]);
108 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items);
109 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items));
111 tmp = g_list_model_get_item (G_LIST_MODEL (store), 3);
112 g_assert (tmp == items[3]);
113 g_object_unref (tmp);
115 g_list_store_remove (store, 4);
116 g_assert_null (items[4]);
117 n_items--;
118 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items);
119 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items));
121 g_object_unref (store);
122 for (i = 0; i < G_N_ELEMENTS (items); i++)
123 g_assert_null (items[i]);
126 static gchar *
127 make_random_string (void)
129 gchar *str = g_malloc (10);
130 gint i;
132 for (i = 0; i < 9; i++)
133 str[i] = g_test_rand_int_range ('a', 'z');
134 str[i] = '\0';
136 return str;
139 static gint
140 compare_items (gconstpointer a_p,
141 gconstpointer b_p,
142 gpointer user_data)
144 GObject *a_o = (GObject *) a_p;
145 GObject *b_o = (GObject *) b_p;
147 gchar *a = g_object_get_data (a_o, "key");
148 gchar *b = g_object_get_data (b_o, "key");
150 g_assert (user_data == GUINT_TO_POINTER(0x1234u));
152 return strcmp (a, b);
155 static void
156 insert_string (GListStore *store,
157 const gchar *str)
159 GObject *obj;
161 obj = g_object_new (G_TYPE_OBJECT, NULL);
162 g_object_set_data_full (obj, "key", g_strdup (str), g_free);
164 g_list_store_insert_sorted (store, obj, compare_items, GUINT_TO_POINTER(0x1234u));
166 g_object_unref (obj);
169 static void
170 test_store_sorted (void)
172 GListStore *store;
173 guint i;
175 store = g_list_store_new (G_TYPE_OBJECT);
177 for (i = 0; i < 1000; i++)
179 gchar *str = make_random_string ();
180 insert_string (store, str);
181 insert_string (store, str); /* multiple copies of the same are OK */
182 g_free (str);
185 g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 2000);
187 for (i = 0; i < 1000; i++)
189 GObject *a, *b;
191 /* should see our two copies */
192 a = g_list_model_get_item (G_LIST_MODEL (store), i * 2);
193 b = g_list_model_get_item (G_LIST_MODEL (store), i * 2 + 1);
195 g_assert (compare_items (a, b, GUINT_TO_POINTER(0x1234)) == 0);
196 g_assert (a != b);
198 if (i)
200 GObject *c;
202 c = g_list_model_get_item (G_LIST_MODEL (store), i * 2 - 1);
203 g_assert (c != a);
204 g_assert (c != b);
206 g_assert (compare_items (b, c, GUINT_TO_POINTER(0x1234)) > 0);
207 g_assert (compare_items (a, c, GUINT_TO_POINTER(0x1234)) > 0);
209 g_object_unref (c);
212 g_object_unref (a);
213 g_object_unref (b);
216 g_object_unref (store);
219 /* Test that using splice() to replace the middle element in a list store works. */
220 static void
221 test_store_splice_replace_middle (void)
223 GListStore *store;
224 GListModel *model;
225 GAction *item;
226 GPtrArray *array;
228 g_test_bug ("795307");
230 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
231 model = G_LIST_MODEL (store);
233 array = g_ptr_array_new_full (0, g_object_unref);
234 g_ptr_array_add (array, g_simple_action_new ("1", NULL));
235 g_ptr_array_add (array, g_simple_action_new ("2", NULL));
236 g_ptr_array_add (array, g_simple_action_new ("3", NULL));
237 g_ptr_array_add (array, g_simple_action_new ("4", NULL));
238 g_ptr_array_add (array, g_simple_action_new ("5", NULL));
240 /* Add three items through splice */
241 g_list_store_splice (store, 0, 0, array->pdata, 3);
242 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 3);
244 item = g_list_model_get_item (model, 0);
245 g_assert_cmpstr (g_action_get_name (item), ==, "1");
246 g_object_unref (item);
247 item = g_list_model_get_item (model, 1);
248 g_assert_cmpstr (g_action_get_name (item), ==, "2");
249 g_object_unref (item);
250 item = g_list_model_get_item (model, 2);
251 g_assert_cmpstr (g_action_get_name (item), ==, "3");
252 g_object_unref (item);
254 /* Replace the middle one with two new items */
255 g_list_store_splice (store, 1, 1, array->pdata + 3, 2);
256 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 4);
258 item = g_list_model_get_item (model, 0);
259 g_assert_cmpstr (g_action_get_name (item), ==, "1");
260 g_object_unref (item);
261 item = g_list_model_get_item (model, 1);
262 g_assert_cmpstr (g_action_get_name (item), ==, "4");
263 g_object_unref (item);
264 item = g_list_model_get_item (model, 2);
265 g_assert_cmpstr (g_action_get_name (item), ==, "5");
266 g_object_unref (item);
267 item = g_list_model_get_item (model, 3);
268 g_assert_cmpstr (g_action_get_name (item), ==, "3");
269 g_object_unref (item);
271 g_ptr_array_unref (array);
272 g_object_unref (store);
275 /* Test that using splice() to replace the whole list store works. */
276 static void
277 test_store_splice_replace_all (void)
279 GListStore *store;
280 GListModel *model;
281 GPtrArray *array;
282 GAction *item;
284 g_test_bug ("795307");
286 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
287 model = G_LIST_MODEL (store);
289 array = g_ptr_array_new_full (0, g_object_unref);
290 g_ptr_array_add (array, g_simple_action_new ("1", NULL));
291 g_ptr_array_add (array, g_simple_action_new ("2", NULL));
292 g_ptr_array_add (array, g_simple_action_new ("3", NULL));
293 g_ptr_array_add (array, g_simple_action_new ("4", NULL));
295 /* Add the first two */
296 g_list_store_splice (store, 0, 0, array->pdata, 2);
298 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 2);
299 item = g_list_model_get_item (model, 0);
300 g_assert_cmpstr (g_action_get_name (item), ==, "1");
301 g_object_unref (item);
302 item = g_list_model_get_item (model, 1);
303 g_assert_cmpstr (g_action_get_name (item), ==, "2");
304 g_object_unref (item);
306 /* Replace all with the last two */
307 g_list_store_splice (store, 0, 2, array->pdata + 2, 2);
309 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 2);
310 item = g_list_model_get_item (model, 0);
311 g_assert_cmpstr (g_action_get_name (item), ==, "3");
312 g_object_unref (item);
313 item = g_list_model_get_item (model, 1);
314 g_assert_cmpstr (g_action_get_name (item), ==, "4");
315 g_object_unref (item);
317 g_ptr_array_unref (array);
318 g_object_unref (store);
321 /* Test that using splice() without removing or adding anything works */
322 static void
323 test_store_splice_noop (void)
325 GListStore *store;
326 GListModel *model;
327 GAction *item;
329 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
330 model = G_LIST_MODEL (store);
332 /* splice noop with an empty list */
333 g_list_store_splice (store, 0, 0, NULL, 0);
334 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 0);
336 /* splice noop with a non-empty list */
337 item = G_ACTION (g_simple_action_new ("1", NULL));
338 g_list_store_append (store, item);
339 g_object_unref (item);
341 g_list_store_splice (store, 0, 0, NULL, 0);
342 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 1);
344 g_list_store_splice (store, 1, 0, NULL, 0);
345 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 1);
347 item = g_list_model_get_item (model, 0);
348 g_assert_cmpstr (g_action_get_name (item), ==, "1");
349 g_object_unref (item);
351 g_object_unref (store);
354 static gboolean
355 model_array_equal (GListModel *model, GPtrArray *array)
357 guint i;
359 if (g_list_model_get_n_items (model) != array->len)
360 return FALSE;
362 for (i = 0; i < array->len; i++)
364 GObject *ptr;
365 gboolean ptrs_equal;
367 ptr = g_list_model_get_item (model, i);
368 ptrs_equal = (g_ptr_array_index (array, i) == ptr);
369 g_object_unref (ptr);
370 if (!ptrs_equal)
371 return FALSE;
374 return TRUE;
377 /* Test that using splice() to remove multiple items at different
378 * positions works */
379 static void
380 test_store_splice_remove_multiple (void)
382 GListStore *store;
383 GListModel *model;
384 GPtrArray *array;
386 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
387 model = G_LIST_MODEL (store);
389 array = g_ptr_array_new_full (0, g_object_unref);
390 g_ptr_array_add (array, g_simple_action_new ("1", NULL));
391 g_ptr_array_add (array, g_simple_action_new ("2", NULL));
392 g_ptr_array_add (array, g_simple_action_new ("3", NULL));
393 g_ptr_array_add (array, g_simple_action_new ("4", NULL));
394 g_ptr_array_add (array, g_simple_action_new ("5", NULL));
395 g_ptr_array_add (array, g_simple_action_new ("6", NULL));
396 g_ptr_array_add (array, g_simple_action_new ("7", NULL));
397 g_ptr_array_add (array, g_simple_action_new ("8", NULL));
398 g_ptr_array_add (array, g_simple_action_new ("9", NULL));
399 g_ptr_array_add (array, g_simple_action_new ("10", NULL));
401 /* Add all */
402 g_list_store_splice (store, 0, 0, array->pdata, array->len);
403 g_assert_true (model_array_equal (model, array));
405 /* Remove the first two */
406 g_list_store_splice (store, 0, 2, NULL, 0);
407 g_assert_false (model_array_equal (model, array));
408 g_ptr_array_remove_range (array, 0, 2);
409 g_assert_true (model_array_equal (model, array));
410 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 8);
412 /* Remove two in the middle */
413 g_list_store_splice (store, 2, 2, NULL, 0);
414 g_assert_false (model_array_equal (model, array));
415 g_ptr_array_remove_range (array, 2, 2);
416 g_assert_true (model_array_equal (model, array));
417 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 6);
419 /* Remove two at the end */
420 g_list_store_splice (store, 4, 2, NULL, 0);
421 g_assert_false (model_array_equal (model, array));
422 g_ptr_array_remove_range (array, 4, 2);
423 g_assert_true (model_array_equal (model, array));
424 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 4);
426 g_ptr_array_unref (array);
427 g_object_unref (store);
430 /* Test that using splice() to add multiple items at different
431 * positions works */
432 static void
433 test_store_splice_add_multiple (void)
435 GListStore *store;
436 GListModel *model;
437 GPtrArray *array;
439 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
440 model = G_LIST_MODEL (store);
442 array = g_ptr_array_new_full (0, g_object_unref);
443 g_ptr_array_add (array, g_simple_action_new ("1", NULL));
444 g_ptr_array_add (array, g_simple_action_new ("2", NULL));
445 g_ptr_array_add (array, g_simple_action_new ("3", NULL));
446 g_ptr_array_add (array, g_simple_action_new ("4", NULL));
447 g_ptr_array_add (array, g_simple_action_new ("5", NULL));
448 g_ptr_array_add (array, g_simple_action_new ("6", NULL));
450 /* Add two at the beginning */
451 g_list_store_splice (store, 0, 0, array->pdata, 2);
453 /* Add two at the end */
454 g_list_store_splice (store, 2, 0, array->pdata + 4, 2);
456 /* Add two in the middle */
457 g_list_store_splice (store, 2, 0, array->pdata + 2, 2);
459 g_assert_true (model_array_equal (model, array));
461 g_ptr_array_unref (array);
462 g_object_unref (store);
465 /* Test that get_item_type() returns the right type */
466 static void
467 test_store_item_type (void)
469 GListStore *store;
470 GType gtype;
472 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
473 gtype = g_list_model_get_item_type (G_LIST_MODEL (store));
474 g_assert (gtype == G_TYPE_SIMPLE_ACTION);
476 g_object_unref (store);
479 /* Test that remove_all() removes all items */
480 static void
481 test_store_remove_all (void)
483 GListStore *store;
484 GListModel *model;
485 GSimpleAction *item;
487 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
488 model = G_LIST_MODEL (store);
490 /* Test with an empty list */
491 g_list_store_remove_all (store);
492 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 0);
494 /* Test with a non-empty list */
495 item = g_simple_action_new ("42", NULL);
496 g_list_store_append (store, item);
497 g_list_store_append (store, item);
498 g_object_unref (item);
499 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 2);
500 g_list_store_remove_all (store);
501 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 0);
503 g_object_unref (store);
506 /* Test that splice() logs an error when passed the wrong item type */
507 static void
508 test_store_splice_wrong_type (void)
510 GListStore *store;
512 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
514 g_test_expect_message (G_LOG_DOMAIN,
515 G_LOG_LEVEL_CRITICAL,
516 "*GListStore instead of a GSimpleAction*");
517 g_list_store_splice (store, 0, 0, (gpointer)&store, 1);
519 g_object_unref (store);
522 static gint
523 ptr_array_cmp_action_by_name (GAction **a, GAction **b)
525 return g_strcmp0 (g_action_get_name (*a), g_action_get_name (*b));
528 static gint
529 list_model_cmp_action_by_name (GAction *a, GAction *b, gpointer user_data)
531 return g_strcmp0 (g_action_get_name (a), g_action_get_name (b));
534 /* Test if sort() works */
535 static void
536 test_store_sort (void)
538 GListStore *store;
539 GListModel *model;
540 GPtrArray *array;
542 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
543 model = G_LIST_MODEL (store);
545 array = g_ptr_array_new_full (0, g_object_unref);
546 g_ptr_array_add (array, g_simple_action_new ("2", NULL));
547 g_ptr_array_add (array, g_simple_action_new ("3", NULL));
548 g_ptr_array_add (array, g_simple_action_new ("9", NULL));
549 g_ptr_array_add (array, g_simple_action_new ("4", NULL));
550 g_ptr_array_add (array, g_simple_action_new ("5", NULL));
551 g_ptr_array_add (array, g_simple_action_new ("8", NULL));
552 g_ptr_array_add (array, g_simple_action_new ("6", NULL));
553 g_ptr_array_add (array, g_simple_action_new ("7", NULL));
554 g_ptr_array_add (array, g_simple_action_new ("1", NULL));
556 /* Sort an empty list */
557 g_list_store_sort (store, (GCompareDataFunc)list_model_cmp_action_by_name, NULL);
559 /* Add all */
560 g_list_store_splice (store, 0, 0, array->pdata, array->len);
561 g_assert_true (model_array_equal (model, array));
563 /* Sort both and check if the result is the same */
564 g_ptr_array_sort (array, (GCompareFunc)ptr_array_cmp_action_by_name);
565 g_assert_false (model_array_equal (model, array));
566 g_list_store_sort (store, (GCompareDataFunc)list_model_cmp_action_by_name, NULL);
567 g_assert_true (model_array_equal (model, array));
569 g_ptr_array_unref (array);
570 g_object_unref (store);
573 /* Test the cases where the item store tries to speed up item access by caching
574 * the last iter/position */
575 static void
576 test_store_get_item_cache (void)
578 GListStore *store;
579 GListModel *model;
580 GSimpleAction *item1, *item2, *temp;
582 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
583 model = G_LIST_MODEL (store);
585 /* Add two */
586 item1 = g_simple_action_new ("1", NULL);
587 g_list_store_append (store, item1);
588 item2 = g_simple_action_new ("2", NULL);
589 g_list_store_append (store, item2);
591 /* Clear the cache */
592 g_assert_null (g_list_model_get_item (model, 42));
594 /* Access the same position twice */
595 temp = g_list_model_get_item (model, 1);
596 g_assert (temp == item2);
597 g_object_unref (temp);
598 temp = g_list_model_get_item (model, 1);
599 g_assert (temp == item2);
600 g_object_unref (temp);
602 g_assert_null (g_list_model_get_item (model, 42));
604 /* Access forwards */
605 temp = g_list_model_get_item (model, 0);
606 g_assert (temp == item1);
607 g_object_unref (temp);
608 temp = g_list_model_get_item (model, 1);
609 g_assert (temp == item2);
610 g_object_unref (temp);
612 g_assert_null (g_list_model_get_item (model, 42));
614 /* Access backwards */
615 temp = g_list_model_get_item (model, 1);
616 g_assert (temp == item2);
617 g_object_unref (temp);
618 temp = g_list_model_get_item (model, 0);
619 g_assert (temp == item1);
620 g_object_unref (temp);
622 g_object_unref (item1);
623 g_object_unref (item2);
624 g_object_unref (store);
627 struct ItemsChangedData
629 guint position;
630 guint removed;
631 guint added;
632 gboolean called;
635 static void
636 expect_items_changed (struct ItemsChangedData *expected,
637 guint position,
638 guint removed,
639 guint added)
641 expected->position = position;
642 expected->removed = removed;
643 expected->added = added;
644 expected->called = FALSE;
647 static void
648 on_items_changed (GListModel *model,
649 guint position,
650 guint removed,
651 guint added,
652 struct ItemsChangedData *expected)
654 g_assert_false (expected->called);
655 g_assert_cmpuint (expected->position, ==, position);
656 g_assert_cmpuint (expected->removed, ==, removed);
657 g_assert_cmpuint (expected->added, ==, added);
658 expected->called = TRUE;
661 /* Test that all operations on the list emit the items-changed signal */
662 static void
663 test_store_signal_items_changed (void)
665 GListStore *store;
666 GListModel *model;
667 GSimpleAction *item;
668 struct ItemsChangedData expected = {0};
670 store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
671 model = G_LIST_MODEL (store);
673 g_object_connect (model, "signal::items-changed",
674 on_items_changed, &expected, NULL);
676 /* Emit the signal manually */
677 expect_items_changed (&expected, 0, 0, 0);
678 g_list_model_items_changed (model, 0, 0, 0);
679 g_assert_true (expected.called);
681 /* Append an item */
682 expect_items_changed (&expected, 0, 0, 1);
683 item = g_simple_action_new ("2", NULL);
684 g_list_store_append (store, item);
685 g_object_unref (item);
686 g_assert_true (expected.called);
688 /* Insert an item */
689 expect_items_changed (&expected, 1, 0, 1);
690 item = g_simple_action_new ("1", NULL);
691 g_list_store_insert (store, 1, item);
692 g_object_unref (item);
693 g_assert_true (expected.called);
695 /* Sort the list */
696 expect_items_changed (&expected, 0, 2, 2);
697 g_list_store_sort (store,
698 (GCompareDataFunc)list_model_cmp_action_by_name,
699 NULL);
700 g_assert_true (expected.called);
702 /* Insert sorted */
703 expect_items_changed (&expected, 2, 0, 1);
704 item = g_simple_action_new ("3", NULL);
705 g_list_store_insert_sorted (store,
706 item,
707 (GCompareDataFunc)list_model_cmp_action_by_name,
708 NULL);
709 g_object_unref (item);
710 g_assert_true (expected.called);
712 /* Remove an item */
713 expect_items_changed (&expected, 1, 1, 0);
714 g_list_store_remove (store, 1);
715 g_assert_true (expected.called);
717 /* Splice */
718 expect_items_changed (&expected, 0, 2, 1);
719 item = g_simple_action_new ("4", NULL);
720 g_assert_cmpuint (g_list_model_get_n_items (model), >=, 2);
721 g_list_store_splice (store, 0, 2, (gpointer)&item, 1);
722 g_object_unref (item);
723 g_assert_true (expected.called);
725 /* Remove all */
726 expect_items_changed (&expected, 0, 1, 0);
727 g_assert_cmpuint (g_list_model_get_n_items (model), ==, 1);
728 g_list_store_remove_all (store);
729 g_assert_true (expected.called);
731 g_object_unref (store);
734 int main (int argc, char *argv[])
736 g_test_init (&argc, &argv, NULL);
737 g_test_bug_base ("https://bugzilla.gnome.org/");
739 g_test_add_func ("/glistmodel/store/boundaries", test_store_boundaries);
740 g_test_add_func ("/glistmodel/store/refcounts", test_store_refcounts);
741 g_test_add_func ("/glistmodel/store/sorted", test_store_sorted);
742 g_test_add_func ("/glistmodel/store/splice-replace-middle",
743 test_store_splice_replace_middle);
744 g_test_add_func ("/glistmodel/store/splice-replace-all",
745 test_store_splice_replace_all);
746 g_test_add_func ("/glistmodel/store/splice-noop", test_store_splice_noop);
747 g_test_add_func ("/glistmodel/store/splice-remove-multiple",
748 test_store_splice_remove_multiple);
749 g_test_add_func ("/glistmodel/store/splice-add-multiple",
750 test_store_splice_add_multiple);
751 g_test_add_func ("/glistmodel/store/splice-wrong-type",
752 test_store_splice_wrong_type);
753 g_test_add_func ("/glistmodel/store/item-type",
754 test_store_item_type);
755 g_test_add_func ("/glistmodel/store/remove-all",
756 test_store_remove_all);
757 g_test_add_func ("/glistmodel/store/sort",
758 test_store_sort);
759 g_test_add_func ("/glistmodel/store/get-item-cache",
760 test_store_get_item_cache);
761 g_test_add_func ("/glistmodel/store/items-changed",
762 test_store_signal_items_changed);
764 return g_test_run ();