| 1 |
/* |
| 2 |
* test-async-result.c |
| 3 |
* |
| 4 |
* Copyright 2022 Christian Hergert <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <libdex.h> |
| 25 |
|
| 26 |
typedef struct |
| 27 |
{ |
| 28 |
GMainLoop *main_loop; |
| 29 |
GObject *object; |
| 30 |
GError *error; |
| 31 |
int value; |
| 32 |
} State; |
| 33 |
|
| 34 |
static void |
| 35 |
test_async_result_await_cb (GObject *object, |
| 36 |
GAsyncResult *result, |
| 37 |
gpointer user_data) |
| 38 |
{ |
| 39 |
State *state = user_data; |
| 40 |
|
| 41 |
g_assert_true (G_IS_MENU (object)); |
| 42 |
g_assert_true (DEX_IS_ASYNC_RESULT (result)); |
| 43 |
g_assert_nonnull (state); |
| 44 |
g_assert_true (object == state->object); |
| 45 |
|
| 46 |
state->value = dex_async_result_propagate_int (DEX_ASYNC_RESULT (result), &state->error); |
| 47 |
|
| 48 |
g_main_loop_quit (state->main_loop); |
| 49 |
} |
| 50 |
|
| 51 |
static void |
| 52 |
test_async_result_await (void) |
| 53 |
{ |
| 54 |
GMainLoop *main_loop = g_main_loop_new (NULL, FALSE); |
| 55 |
DexPromise *future = dex_promise_new (); |
| 56 |
GMenu *obj = g_menu_new (); |
| 57 |
State state = {0}; |
| 58 |
DexAsyncResult *async_result = dex_async_result_new (obj, NULL, test_async_result_await_cb, &state); |
| 59 |
|
| 60 |
state.main_loop = main_loop; |
| 61 |
state.object = G_OBJECT (obj); |
| 62 |
|
| 63 |
dex_async_result_await (async_result, dex_ref (future)); |
| 64 |
g_clear_object (&async_result); |
| 65 |
|
| 66 |
dex_promise_resolve_int (future, 123); |
| 67 |
|
| 68 |
g_main_loop_run (main_loop); |
| 69 |
|
| 70 |
g_assert_cmpint (state.value, ==, 123); |
| 71 |
|
| 72 |
g_object_unref (obj); |
| 73 |
dex_unref (future); |
| 74 |
|
| 75 |
while (g_main_context_pending (NULL)) |
| 76 |
g_main_context_iteration (NULL, FALSE); |
| 77 |
|
| 78 |
g_main_loop_unref (main_loop); |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
main (int argc, |
| 83 |
char *argv[]) |
| 84 |
{ |
| 85 |
dex_init (); |
| 86 |
g_test_init (&argc, &argv, NULL); |
| 87 |
g_test_add_func ("/Dex/TestSuite/AsyncResult/await", test_async_result_await); |
| 88 |
return g_test_run (); |
| 89 |
} |