From 76ae17b24f365488c3e1846033ec229e42e4d92c Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 6 Nov 2011 08:59:20 +0100 Subject: [PATCH] Add test for GSecretService singleton, and make it work --- library/gsecret-service.c | 12 +++++++++++- library/tests/test-service.c | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/library/gsecret-service.c b/library/gsecret-service.c index 61182c9..6603856 100644 --- a/library/gsecret-service.c +++ b/library/gsecret-service.c @@ -120,11 +120,21 @@ GSecretService * _gsecret_service_bare_instance (GDBusConnection *connection, const gchar *bus_name) { - GSecretService *service; + GSecretService *service = NULL; GError *error = NULL; g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); + G_LOCK (service_instance); + + if (service_instance != NULL) + service = g_object_ref (service_instance); + + G_UNLOCK (service_instance); + + if (service != NULL) + return service; + /* Alternate bus name is only used for testing */ if (bus_name == NULL) bus_name = GSECRET_SERVICE_BUS_NAME; diff --git a/library/tests/test-service.c b/library/tests/test-service.c index 2642d41..bb5a5c4 100644 --- a/library/tests/test-service.c +++ b/library/tests/test-service.c @@ -66,6 +66,42 @@ teardown (Test *test, g_spawn_close_pid (test->pid); } +static void +test_instance (void) +{ + GSecretService *service1; + GSecretService *service2; + GSecretService *service3; + GError *error = NULL; + GDBusConnection *connection; + + connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); + g_assert_no_error (error); + + /* Both these sohuld point to the same thing */ + + service1 = _gsecret_service_bare_instance (connection, MOCK_NAME); + service2 = _gsecret_service_bare_instance (connection, MOCK_NAME); + + g_assert (GSECRET_IS_SERVICE (service1)); + g_assert (service1 == service2); + + g_object_unref (service1); + g_assert (GSECRET_IS_SERVICE (service1)); + + g_object_unref (service2); + g_assert (!GSECRET_IS_SERVICE (service2)); + + /* Services were unreffed, so this should create a new one */ + service3 = _gsecret_service_bare_instance (connection, MOCK_NAME); + g_assert (GSECRET_IS_SERVICE (service3)); + + g_object_unref (service3); + g_assert (!GSECRET_IS_SERVICE (service3)); + + g_object_unref (connection); +} + static void test_search_paths (Test *test, gconstpointer used) @@ -103,6 +139,7 @@ main (int argc, char **argv) g_set_prgname ("test-service"); g_type_init (); + g_test_add_func ("/service/instance", test_instance); g_test_add ("/service/search-paths", Test, "mock-service-normal.py", setup, test_search_paths, teardown); return egg_tests_run_in_thread_with_loop ();