Adding test coverage for GTestDBus activating in-tree services.
[platform/upstream/glib.git] / gio / tests / gdbus-test-fixture.c
1 /* gdbus-test-fixture.c - Test covering activation of in-tree servers.
2  *
3  * Copyright (C) 2012 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but 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 Lesser General Public
16  * License along with the program; if not, see <http://www.gnu.org/licenses/>
17  *
18  * Authors: Tristan Van Berkom <tristanvb@openismus.com>
19  */
20
21 #include <gio/gio.h>
22 #include "gdbus-example-objectmanager-generated.h"
23
24 typedef struct {
25   GTestDBus *dbus;
26   GDBusObjectManager *manager;
27 } TestFixture;
28
29 static void
30 fixture_setup (TestFixture *fixture, gconstpointer unused)
31 {
32   /* Create the global dbus-daemon for this test suite */
33   fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
34
35   /* Add the private directory with our in-tree service files */
36   g_test_dbus_add_service_dir (fixture->dbus, TEST_SERVICES);
37
38   /* Start the private D-Bus daemon */
39   g_test_dbus_up (fixture->dbus);
40 }
41
42 static void
43 fixture_teardown (TestFixture *fixture, gconstpointer unused)
44 {
45   if (fixture->manager)
46     g_object_unref (fixture->manager);
47
48   /* Stop the private D-Bus daemon */
49   g_test_dbus_down (fixture->dbus);
50
51   g_object_unref (fixture->dbus);
52 }
53
54 /* The gdbus-example-objectmanager-server exports 10 objects,
55  * to test the server has actually activated, let's ensure
56  * that 10 objects exist.
57  */
58 static void
59 assert_ten_objects (GDBusObjectManager *manager)
60 {
61   GList *objects;
62
63   objects = g_dbus_object_manager_get_objects (manager);
64
65   g_assert_cmpint (g_list_length (objects), ==, 10);
66   g_list_free_full (objects, g_object_unref);
67 }
68
69 static gboolean
70 quit_loop (gconstpointer data)
71 {
72   GMainLoop *loop = (GMainLoop *)data;
73
74   g_main_loop_quit (loop);
75   return FALSE;
76 }
77
78 static void
79 test_gtest_dbus (TestFixture *fixture, gconstpointer unused)
80 {
81
82   GError *error = NULL;
83
84   fixture->manager =
85     example_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SESSION,
86                                                     G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
87                                                     "org.gtk.GDBus.Examples.ObjectManager",
88                                                     "/example/Animals",
89                                                     NULL, /* GCancellable */
90                                                     &error);
91   if (fixture->manager == NULL)
92     g_error ("Error getting object manager client: %s", error->message);
93
94   assert_ten_objects (fixture->manager);
95 }
96
97 int
98 main (int   argc,
99       char *argv[])
100 {
101 #if !GLIB_CHECK_VERSION (2, 35, 1)
102   g_type_init ();
103 #endif
104   g_test_init (&argc, &argv, NULL);
105
106   /* Ensure that we can bring the GTestDBus up and down a hand full of times
107    * in a row, each time successfully activating the in-tree service
108    */
109   g_test_add ("/GTestDBus/Cycle1", TestFixture, NULL,
110               fixture_setup, test_gtest_dbus, fixture_teardown);
111   g_test_add ("/GTestDBus/Cycle2", TestFixture, NULL,
112               fixture_setup, test_gtest_dbus, fixture_teardown);
113   g_test_add ("/GTestDBus/Cycle3", TestFixture, NULL,
114               fixture_setup, test_gtest_dbus, fixture_teardown);
115   g_test_add ("/GTestDBus/Cycle4", TestFixture, NULL,
116               fixture_setup, test_gtest_dbus, fixture_teardown);
117   g_test_add ("/GTestDBus/Cycle5", TestFixture, NULL,
118               fixture_setup, test_gtest_dbus, fixture_teardown);
119   
120   return g_test_run ();
121 }