gio/tests/socket: Update to check the error code
[platform/upstream/glib.git] / gio / tests / gdbus-names.c
index 9008fa0..90c9eb3 100644 (file)
@@ -1,6 +1,6 @@
 /* GLib testing framework examples and tests
  *
- * Copyright (C) 2008-2009 Red Hat, Inc.
+ * Copyright (C) 2008-2010 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: David Zeuthen <davidz@redhat.com>
  */
@@ -170,16 +168,17 @@ test_bus_own_name (void)
   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
   g_assert (c != NULL);
   g_assert (!g_dbus_connection_is_closed (c));
-  result = g_dbus_connection_invoke_method_sync (c,
-                                                 "org.freedesktop.DBus",  /* bus name */
-                                                 "/org/freedesktop/DBus", /* object path */
-                                                 "org.freedesktop.DBus",  /* interface name */
-                                                 "NameHasOwner",          /* method name */
-                                                 g_variant_new ("(s)", name),
-                                                 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
-                                                 -1,
-                                                 NULL,
-                                                 &error);
+  result = g_dbus_connection_call_sync (c,
+                                        "org.freedesktop.DBus",  /* bus name */
+                                        "/org/freedesktop/DBus", /* object path */
+                                        "org.freedesktop.DBus",  /* interface name */
+                                        "NameHasOwner",          /* method name */
+                                        g_variant_new ("(s)", name),
+                                        G_VARIANT_TYPE ("(b)"),
+                                        G_DBUS_CALL_FLAGS_NONE,
+                                        -1,
+                                        NULL,
+                                        &error);
   g_assert_no_error (error);
   g_assert (result != NULL);
   g_variant_get (result, "(b)", &name_has_owner_reply);
@@ -195,29 +194,28 @@ test_bus_own_name (void)
   /*
    * Check that the name was actually released.
    */
-  result = g_dbus_connection_invoke_method_sync (c,
-                                                 "org.freedesktop.DBus",  /* bus name */
-                                                 "/org/freedesktop/DBus", /* object path */
-                                                 "org.freedesktop.DBus",  /* interface name */
-                                                 "NameHasOwner",          /* method name */
-                                                 g_variant_new ("(s)", name),
-                                                 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
-                                                 -1,
-                                                 NULL,
-                                                 &error);
+  result = g_dbus_connection_call_sync (c,
+                                        "org.freedesktop.DBus",  /* bus name */
+                                        "/org/freedesktop/DBus", /* object path */
+                                        "org.freedesktop.DBus",  /* interface name */
+                                        "NameHasOwner",          /* method name */
+                                        g_variant_new ("(s)", name),
+                                        G_VARIANT_TYPE ("(b)"),
+                                        G_DBUS_CALL_FLAGS_NONE,
+                                        -1,
+                                        NULL,
+                                        &error);
   g_assert_no_error (error);
   g_assert (result != NULL);
   g_variant_get (result, "(b)", &name_has_owner_reply);
   g_assert (!name_has_owner_reply);
   g_variant_unref (result);
 
-  /*
-   * Own the name again.
-   */
-  data.num_bus_acquired = 0;
-  data.num_acquired = 0;
-  data.num_lost = 0;
-  data.expect_null_connection = FALSE;
+  /* Now try owning the name and then immediately decide to unown the name */
+  g_assert_cmpint (data.num_bus_acquired, ==, 1);
+  g_assert_cmpint (data.num_acquired, ==, 1);
+  g_assert_cmpint (data.num_lost,     ==, 0);
+  g_assert_cmpint (data.num_free_func, ==, 2);
   id = g_bus_own_name (G_BUS_TYPE_SESSION,
                        name,
                        G_BUS_NAME_OWNER_FLAGS_NONE,
@@ -226,6 +224,37 @@ test_bus_own_name (void)
                        name_lost_handler,
                        &data,
                        (GDestroyNotify) own_name_data_free_func);
+  g_assert_cmpint (data.num_bus_acquired, ==, 1);
+  g_assert_cmpint (data.num_acquired, ==, 1);
+  g_assert_cmpint (data.num_lost,     ==, 0);
+  g_assert_cmpint (data.num_free_func, ==, 2);
+  g_bus_unown_name (id);
+  g_assert_cmpint (data.num_bus_acquired, ==, 1);
+  g_assert_cmpint (data.num_acquired, ==, 1);
+  g_assert_cmpint (data.num_lost,     ==, 0);
+  g_assert_cmpint (data.num_free_func, ==, 2);
+  g_main_loop_run (loop); /* the GDestroyNotify is called in idle because the bus is acquired in idle */
+  g_assert_cmpint (data.num_free_func, ==, 3);
+
+  /*
+   * Own the name again.
+   */
+  data.num_bus_acquired = 0;
+  data.num_acquired = 0;
+  data.num_lost = 0;
+  data.expect_null_connection = FALSE;
+  id = g_bus_own_name_with_closures (G_BUS_TYPE_SESSION,
+                                     name,
+                                     G_BUS_NAME_OWNER_FLAGS_NONE,
+                                     g_cclosure_new (G_CALLBACK (bus_acquired_handler),
+                                                     &data,
+                                                     NULL),
+                                     g_cclosure_new (G_CALLBACK (name_acquired_handler),
+                                                     &data,
+                                                     NULL),
+                                     g_cclosure_new (G_CALLBACK (name_lost_handler),
+                                                     &data,
+                                                     (GClosureNotify) own_name_data_free_func));
   g_assert_cmpint (data.num_bus_acquired, ==, 0);
   g_assert_cmpint (data.num_acquired, ==, 0);
   g_assert_cmpint (data.num_lost,     ==, 0);
@@ -338,7 +367,7 @@ test_bus_own_name (void)
   g_bus_unown_name (id);
   g_assert_cmpint (data.num_bus_acquired, ==, 1);
   g_assert_cmpint (data.num_acquired, ==, 1);
-  g_assert_cmpint (data.num_free_func, ==, 3);
+  g_assert_cmpint (data.num_free_func, ==, 4);
   /* grab it again */
   data.num_bus_acquired = 0;
   data.num_acquired = 0;
@@ -431,16 +460,21 @@ test_bus_own_name (void)
    *
    */
   data.expect_null_connection = TRUE;
-  session_bus_down ();
+  session_bus_stop ();
   while (data.num_lost != 2)
     g_main_loop_run (loop);
   g_assert_cmpint (data.num_acquired, ==, 2);
   g_assert_cmpint (data.num_lost,     ==, 2);
   g_bus_unown_name (id);
-  g_assert_cmpint (data.num_free_func, ==, 4);
+  g_assert_cmpint (data.num_free_func, ==, 5);
 
   g_object_unref (c);
   g_object_unref (c2);
+
+  session_bus_down ();
+
+  /* See https://bugzilla.gnome.org/show_bug.cgi?id=711807 */
+  g_usleep (1000000);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -536,6 +570,7 @@ test_bus_watch_name (void)
   WatchNameData data;
   guint id;
   guint owner_id;
+  GDBusConnection *connection;
 
   /*
    * First check that name_vanished_handler() is invoked if there is no bus.
@@ -583,16 +618,20 @@ test_bus_watch_name (void)
   g_main_loop_run (loop);
   g_assert_cmpint (data.num_acquired, ==, 1);
   g_assert_cmpint (data.num_lost,     ==, 0);
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+  g_assert (connection != NULL);
+
   /* now watch the name */
   data.num_appeared = 0;
   data.num_vanished = 0;
-  id = g_bus_watch_name (G_BUS_TYPE_SESSION,
-                         "org.gtk.GDBus.Name1",
-                         G_BUS_NAME_WATCHER_FLAGS_NONE,
-                         name_appeared_handler,
-                         name_vanished_handler,
-                         &data,
-                         (GDestroyNotify) watch_name_data_free_func);
+  id = g_bus_watch_name_on_connection (connection,
+                                       "org.gtk.GDBus.Name1",
+                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                       name_appeared_handler,
+                                       name_vanished_handler,
+                                       &data,
+                                       (GDestroyNotify) watch_name_data_free_func);
   g_assert_cmpint (data.num_appeared, ==, 0);
   g_assert_cmpint (data.num_vanished, ==, 0);
   g_main_loop_run (loop);
@@ -605,6 +644,8 @@ test_bus_watch_name (void)
   g_bus_unwatch_name (id);
   g_assert_cmpint (data.num_free_func, ==, 1);
 
+  g_object_unref (connection);
+
   /* unown the name */
   g_bus_unown_name (owner_id);
   g_assert_cmpint (data.num_acquired, ==, 1);
@@ -619,13 +660,15 @@ test_bus_watch_name (void)
   data.num_appeared = 0;
   data.num_vanished = 0;
   data.num_free_func = 0;
-  id = g_bus_watch_name (G_BUS_TYPE_SESSION,
-                         "org.gtk.GDBus.Name1",
-                         G_BUS_NAME_WATCHER_FLAGS_NONE,
-                         name_appeared_handler,
-                         name_vanished_handler,
-                         &data,
-                         (GDestroyNotify) watch_name_data_free_func);
+  id = g_bus_watch_name_with_closures (G_BUS_TYPE_SESSION,
+                                       "org.gtk.GDBus.Name1",
+                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                       g_cclosure_new (G_CALLBACK (name_appeared_handler),
+                                                       &data,
+                                                       NULL),
+                                       g_cclosure_new (G_CALLBACK (name_vanished_handler),
+                                                       &data,
+                                                       (GClosureNotify) watch_name_data_free_func));
   g_assert_cmpint (data.num_appeared, ==, 0);
   g_assert_cmpint (data.num_vanished, ==, 0);
   g_main_loop_run (loop);
@@ -655,7 +698,7 @@ test_bus_watch_name (void)
    * Nuke the bus and check that the name vanishes and is lost.
    */
   data.expect_null_connection = TRUE;
-  session_bus_down ();
+  session_bus_stop ();
   g_main_loop_run (loop);
   g_assert_cmpint (data.num_lost,     ==, 1);
   g_assert_cmpint (data.num_vanished, ==, 2);
@@ -666,6 +709,7 @@ test_bus_watch_name (void)
   g_bus_unown_name (owner_id);
   g_assert_cmpint (data.num_free_func, ==, 2);
 
+  session_bus_down ();
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -726,16 +770,11 @@ main (int   argc,
 {
   gint ret;
 
-  g_type_init ();
   g_test_init (&argc, &argv, NULL);
 
   loop = g_main_loop_new (NULL, FALSE);
 
-  /* all the tests use a session bus with a well-known address that we can bring up and down
-   * using session_bus_up() and session_bus_down().
-   */
-  g_unsetenv ("DISPLAY");
-  g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
+  g_test_dbus_unset ();
 
   g_test_add_func ("/gdbus/validate-names", test_validate_names);
   g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name);