ef1f6c56be9ba0f1d847862e94e038df990ec889
[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 void
70 test_gtest_dbus (TestFixture *fixture, gconstpointer unused)
71 {
72
73   GError *error = NULL;
74
75   fixture->manager =
76     example_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SESSION,
77                                                     G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
78                                                     "org.gtk.GDBus.Examples.ObjectManager",
79                                                     "/example/Animals",
80                                                     NULL, /* GCancellable */
81                                                     &error);
82   if (fixture->manager == NULL)
83     g_error ("Error getting object manager client: %s", error->message);
84
85   assert_ten_objects (fixture->manager);
86 }
87
88 int
89 main (int   argc,
90       char *argv[])
91 {
92 #if !GLIB_CHECK_VERSION (2, 35, 1)
93   g_type_init ();
94 #endif
95   g_test_init (&argc, &argv, NULL);
96
97   /* Ensure that we can bring the GTestDBus up and down a hand full of times
98    * in a row, each time successfully activating the in-tree service
99    */
100   g_test_add ("/GTestDBus/Cycle1", TestFixture, NULL,
101               fixture_setup, test_gtest_dbus, fixture_teardown);
102   g_test_add ("/GTestDBus/Cycle2", TestFixture, NULL,
103               fixture_setup, test_gtest_dbus, fixture_teardown);
104   g_test_add ("/GTestDBus/Cycle3", TestFixture, NULL,
105               fixture_setup, test_gtest_dbus, fixture_teardown);
106   g_test_add ("/GTestDBus/Cycle4", TestFixture, NULL,
107               fixture_setup, test_gtest_dbus, fixture_teardown);
108   g_test_add ("/GTestDBus/Cycle5", TestFixture, NULL,
109               fixture_setup, test_gtest_dbus, fixture_teardown);
110   
111   return g_test_run ();
112 }