Debian Codegraph @

testsuite/test-future-list-model.c

raw
1 /* test-future-list-model.c
2 *
3 * Copyright 2025 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * 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 General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <libdex.h>
24
25 #include <gio/gio.h>
26
27 static guint items_changed_count = 0;
28 static guint last_position = 0;
29 static guint last_removed = 0;
30 static guint last_added = 0;
31
32 static void
33 on_items_changed (GListModel *model,
34 guint position,
35 guint removed,
36 guint added,
37 gpointer user_data)
38 {
39 items_changed_count++;
40 last_position = position;
41 last_removed = removed;
42 last_added = added;
43 }
44
45 static void
46 test_future_list_model_basic (void)
47 {
48 DexPromise *promise;
49 GListModel *future_model;
50 GListStore *base_store;
51 GObject *item1, *item2, *item3;
52
53 /* Reset counters */
54 items_changed_count = 0;
55 last_position = 0;
56 last_removed = 0;
57 last_added = 0;
58
59 /* Create a promise that will resolve to a GListStore */
60 promise = dex_promise_new ();
61 future_model = dex_future_list_model_new (dex_ref (DEX_FUTURE (promise)));
62
63 /* Before the future completes, it should have zero items */
64 g_assert_cmpuint (g_list_model_get_n_items (future_model), ==, 0);
65
66 /* Connect to items-changed signal */
67 g_signal_connect (future_model, "items-changed", G_CALLBACK (on_items_changed), NULL);
68
69 /* Create the base GListStore and add an item to it */
70 base_store = g_list_store_new (G_TYPE_OBJECT);
71 item1 = g_object_new (G_TYPE_OBJECT, NULL);
72 g_list_store_append (base_store, item1);
73 g_object_unref (item1);
74
75 /* Resolve the promise with the base store */
76 dex_promise_resolve_object (promise, g_object_ref (base_store));
77
78 /* Process pending events to allow the future to resolve */
79 while (g_main_context_pending (NULL))
80 g_main_context_iteration (NULL, FALSE);
81
82 /* Wait for the future to actually resolve */
83 while (dex_future_get_status (DEX_FUTURE (promise)) == DEX_FUTURE_STATUS_PENDING)
84 {
85 if (!g_main_context_iteration (NULL, TRUE))
86 break;
87 }
88
89 /* Check that items-changed was emitted when the model was added (since it had 1 item) */
90 g_assert_cmpuint (items_changed_count, ==, 1);
91 g_assert_cmpuint (last_added, ==, 1);
92 g_assert_cmpuint (last_removed, ==, 0);
93 g_assert_cmpuint (last_position, ==, 0);
94
95 /* Add more items to the base model */
96 item2 = g_object_new (G_TYPE_OBJECT, NULL);
97 g_list_store_append (base_store, item2);
98 g_object_unref (item2);
99
100 /* Process pending events */
101 while (g_main_context_pending (NULL))
102 g_main_context_iteration (NULL, FALSE);
103
104 /* Check that items-changed was emitted */
105 g_assert_cmpuint (items_changed_count, ==, 2);
106 g_assert_cmpuint (last_added, ==, 1);
107 g_assert_cmpuint (last_removed, ==, 0);
108 g_assert_cmpuint (last_position, ==, 1);
109
110 /* Add another item */
111 item3 = g_object_new (G_TYPE_OBJECT, NULL);
112 g_list_store_append (base_store, item3);
113 g_object_unref (item3);
114
115 /* Process pending events */
116 while (g_main_context_pending (NULL))
117 g_main_context_iteration (NULL, FALSE);
118
119 /* Check that items-changed was emitted for the addition */
120 g_assert_cmpuint (items_changed_count, ==, 3);
121 g_assert_cmpuint (last_added, ==, 1);
122 g_assert_cmpuint (last_removed, ==, 0);
123 g_assert_cmpuint (last_position, ==, 2);
124
125 /* Verify the items are accessible */
126 g_assert_cmpuint (g_list_model_get_n_items (future_model), ==, 3);
127 {
128 GObject *retrieved_item;
129
130 retrieved_item = g_list_model_get_item (future_model, 0);
131 g_assert_nonnull (retrieved_item);
132 g_object_unref (retrieved_item);
133
134 retrieved_item = g_list_model_get_item (future_model, 1);
135 g_assert_nonnull (retrieved_item);
136 g_object_unref (retrieved_item);
137
138 retrieved_item = g_list_model_get_item (future_model, 2);
139 g_assert_nonnull (retrieved_item);
140 g_object_unref (retrieved_item);
141 }
142
143 g_clear_object (&future_model);
144 g_clear_object (&base_store);
145 dex_clear (&promise);
146 }
147
148 int
149 main (int argc,
150 char *argv[])
151 {
152 dex_init ();
153 g_test_init (&argc, &argv, NULL);
154 g_test_add_func ("/Dex/TestSuite/FutureListModel/basic", test_future_list_model_basic);
155 return g_test_run ();
156 }