1
2 #include "gdbus-object-manager-example/objectmanager-gen.h"
3
4 /* ---------------------------------------------------------------------------------------------------- */
5
6 /* The fixture contains a GTestDBus object and
7 * a proxy to the service we're going to be testing.
8 */
9 typedef struct {
10 GTestDBus *dbus;
11 GDBusObjectManager *manager;
12 } TestFixture;
13
14 static void
fixture_setup(TestFixture * fixture,gconstpointer unused)15 fixture_setup (TestFixture *fixture, gconstpointer unused)
16 {
17 GError *error = NULL;
18
19 /* Create the global dbus-daemon for this test suite
20 */
21 fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
22
23 /* Add the private directory with our in-tree service files,
24 * TEST_SERVICES is defined by the build system to point
25 * to the right directory.
26 */
27 g_test_dbus_add_service_dir (fixture->dbus, TEST_SERVICES);
28
29 /* Start the private D-Bus daemon
30 */
31 g_test_dbus_up (fixture->dbus);
32
33 /* Create the proxy that we're going to test
34 */
35 fixture->manager =
36 example_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SESSION,
37 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
38 "org.gtk.GDBus.Examples.ObjectManager",
39 "/example/Animals",
40 NULL, /* GCancellable */
41 &error);
42 if (fixture->manager == NULL)
43 g_error ("Error getting object manager client: %s", error->message);
44 }
45
46 static void
fixture_teardown(TestFixture * fixture,gconstpointer unused)47 fixture_teardown (TestFixture *fixture, gconstpointer unused)
48 {
49 /* Tear down the proxy
50 */
51 if (fixture->manager)
52 g_object_unref (fixture->manager);
53
54 /* Stop the private D-Bus daemon
55 */
56 g_test_dbus_down (fixture->dbus);
57 g_object_unref (fixture->dbus);
58 }
59
60 /* The gdbus-example-objectmanager-server exports 10 objects,
61 * to test the server has actually activated, let's ensure
62 * that 10 objects exist.
63 */
64 static void
test_gtest_dbus(TestFixture * fixture,gconstpointer unused)65 test_gtest_dbus (TestFixture *fixture, gconstpointer unused)
66 {
67 GList *objects;
68
69 objects = g_dbus_object_manager_get_objects (fixture->manager);
70
71 g_assert_cmpint (g_list_length (objects), ==, 10);
72 g_list_free_full (objects, g_object_unref);
73 }
74
75 int
main(int argc,char * argv[])76 main (int argc,
77 char *argv[])
78 {
79 g_test_init (&argc, &argv, NULL);
80
81 /* This test simply ensures that we can bring the GTestDBus up and down a hand
82 * full of times in a row, each time successfully activating the in-tree service
83 */
84 g_test_add ("/GTestDBus/Cycle1", TestFixture, NULL,
85 fixture_setup, test_gtest_dbus, fixture_teardown);
86 g_test_add ("/GTestDBus/Cycle2", TestFixture, NULL,
87 fixture_setup, test_gtest_dbus, fixture_teardown);
88 g_test_add ("/GTestDBus/Cycle3", TestFixture, NULL,
89 fixture_setup, test_gtest_dbus, fixture_teardown);
90 g_test_add ("/GTestDBus/Cycle4", TestFixture, NULL,
91 fixture_setup, test_gtest_dbus, fixture_teardown);
92 g_test_add ("/GTestDBus/Cycle5", TestFixture, NULL,
93 fixture_setup, test_gtest_dbus, fixture_teardown);
94
95 return g_test_run ();
96 }
97