(root)/
glib-2.79.0/
gio/
tests/
simple-async-result.c
       1  /*
       2   * Copyright © 2009 Ryan Lortie
       3   *
       4   * SPDX-License-Identifier: LGPL-2.1-or-later
       5   *
       6   * This library 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   * See the included COPYING file for more information.
      12   */
      13  
      14  #include <glib/glib.h>
      15  #include <gio/gio.h>
      16  #include <stdlib.h>
      17  #include <string.h>
      18  
      19  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      20  
      21  static GObject      *got_source;
      22  static GAsyncResult *got_result;
      23  static gpointer      got_user_data;
      24  
      25  static void
      26  ensure_destroyed (gpointer obj)
      27  {
      28    g_object_add_weak_pointer (obj, &obj);
      29    g_object_unref (obj);
      30    g_assert (obj == NULL);
      31  }
      32  
      33  static void
      34  reset (void)
      35  {
      36    got_source = NULL;
      37  
      38    if (got_result)
      39      ensure_destroyed (got_result);
      40  
      41    got_result = NULL;
      42    got_user_data = NULL;
      43  }
      44  
      45  static void
      46  check (gpointer a, gpointer b, gpointer c)
      47  {
      48    g_assert (a == got_source);
      49    g_assert (b == got_result);
      50    g_assert (c == got_user_data);
      51  }
      52  
      53  static void
      54  callback_func (GObject      *source,
      55                 GAsyncResult *result,
      56                 gpointer      user_data)
      57  {
      58    got_source = source;
      59    got_result = g_object_ref (result);
      60    got_user_data = user_data;
      61  }
      62  
      63  static gboolean
      64  test_simple_async_idle (gpointer user_data)
      65  {
      66    GSimpleAsyncResult *result;
      67    GObject *a, *b, *c;
      68    gboolean *ran = user_data;
      69  
      70    a = g_object_new (G_TYPE_OBJECT, NULL);
      71    b = g_object_new (G_TYPE_OBJECT, NULL);
      72    c = g_object_new (G_TYPE_OBJECT, NULL);
      73  
      74    result = g_simple_async_result_new (a, callback_func, b, test_simple_async_idle);
      75    g_assert (g_async_result_get_user_data (G_ASYNC_RESULT (result)) == b);
      76    check (NULL, NULL, NULL);
      77    g_simple_async_result_complete (result);
      78    check (a, result, b);
      79    g_object_unref (result);
      80  
      81    g_assert (g_simple_async_result_is_valid (got_result, a, test_simple_async_idle));
      82    g_assert (!g_simple_async_result_is_valid (got_result, b, test_simple_async_idle));
      83    g_assert (!g_simple_async_result_is_valid (got_result, c, test_simple_async_idle));
      84    g_assert (!g_simple_async_result_is_valid (got_result, b, callback_func));
      85    g_assert (!g_simple_async_result_is_valid ((gpointer) a, NULL, NULL));
      86    reset ();
      87    reset ();
      88    reset ();
      89  
      90    ensure_destroyed (a);
      91    ensure_destroyed (b);
      92    ensure_destroyed (c);
      93  
      94    *ran = TRUE;
      95    return G_SOURCE_REMOVE;
      96  }
      97  
      98  static void
      99  test_simple_async (void)
     100  {
     101    GSimpleAsyncResult *result;
     102    GObject *a, *b;
     103    gboolean ran_test_in_idle = FALSE;
     104  
     105    g_idle_add (test_simple_async_idle, &ran_test_in_idle);
     106    g_main_context_iteration (NULL, FALSE);
     107  
     108    g_assert (ran_test_in_idle);
     109  
     110    a = g_object_new (G_TYPE_OBJECT, NULL);
     111    b = g_object_new (G_TYPE_OBJECT, NULL);
     112  
     113    result = g_simple_async_result_new (a, callback_func, b, test_simple_async);
     114    check (NULL, NULL, NULL);
     115    g_simple_async_result_complete_in_idle (result);
     116    g_object_unref (result);
     117    check (NULL, NULL, NULL);
     118    g_main_context_iteration (NULL, FALSE);
     119    check (a, result, b);
     120    reset ();
     121  
     122    ensure_destroyed (a);
     123    ensure_destroyed (b);
     124  }
     125  
     126  static void
     127  test_valid (void)
     128  {
     129    GAsyncResult *result;
     130    GObject *a, *b;
     131  
     132    a = g_object_new (G_TYPE_OBJECT, NULL);
     133    b = g_object_new (G_TYPE_OBJECT, NULL);
     134  
     135    /* Without source or tag */
     136    result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, NULL);
     137    g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
     138    g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
     139    g_assert_true (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     140    g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
     141    g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
     142    g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     143    g_object_unref (result);
     144  
     145    /* Without source, with tag */
     146    result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, test_valid);
     147    g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
     148    g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
     149    g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     150    g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
     151    g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
     152    g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     153    g_object_unref (result);
     154  
     155    /* With source, without tag */
     156    result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, NULL);
     157    g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
     158    g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
     159    g_assert_true (g_simple_async_result_is_valid (result, a, test_simple_async));
     160    g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     161    g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
     162    g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     163    g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
     164    g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
     165    g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
     166    g_object_unref (result);
     167  
     168    /* With source and tag */
     169    result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, test_valid);
     170    g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
     171    g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
     172    g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     173    g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     174    g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
     175    g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     176    g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
     177    g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
     178    g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
     179    g_object_unref (result);
     180  
     181    /* Non-GSimpleAsyncResult */
     182    result = (GAsyncResult *) g_task_new (NULL, NULL, NULL, NULL);
     183    g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     184    g_object_unref (result);
     185  
     186    g_object_unref (a);
     187    g_object_unref (b);
     188  }
     189  
     190  int
     191  main (int argc, char **argv)
     192  {
     193    g_test_init (&argc, &argv, NULL);
     194  
     195    g_test_add_func ("/gio/simple-async-result/test", test_simple_async);
     196    g_test_add_func ("/gio/simple-async-result/valid", test_valid);
     197  
     198    return g_test_run();
     199  }
     200  
     201  G_GNUC_END_IGNORE_DEPRECATIONS