summaryrefslogtreecommitdiff
path: root/tests/tests-common.h
blob: 1e656d7d6818e2867c4fdf2a03419da844772bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  xfconf
 *
 *  Copyright (c) 2007 Brian Tarricone <bjt23@cornell.edu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License ONLY.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef __XFCONF_TESTS_COMMON_H__
#define __XFCONF_TESTS_COMMON_H__

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <glib.h>
#include <gio/gio.h>
#include <xfconf/xfconf.h>

#define TEST_CHANNEL_NAME  "test-channel"
#define WAIT_TIMEOUT       15

#define TEST_OPERATION(x) G_STMT_START{ \
    if(!(x)) { \
        g_critical("Test failed: " # x); \
        xfconf_tests_end(); \
        return 1; \
    } \
}G_STMT_END

/* don't use static to avoid compiler warnings in tests that don't use
 * all of them */
const gchar *test_string_property = "/test/stringtest/string";
const gchar *test_string = "teststring";
const gchar *test_strlist_property = "/test/stringtest/strlist";
const gchar *test_strlist[] = { "teststring1", "teststring2", NULL };
const gchar *test_int_property = "/test/inttest/int";
const gint test_int = 42;
const gchar *test_uint64_property = "/test/uint64test/uint64";
const guint64 test_uint64 = 42000000000LL;
const gchar *test_double_property = "/test/doubletest/double";
const gdouble test_double = 42.4242;
const gchar *test_bool_property = "/test/booltest/bool";
const gboolean test_bool = TRUE;
const gchar *test_array_property = "/test/arrayprop";

static void xfconf_tests_end();

static gboolean
xfconf_tests_start(void)
{
    GDBusConnection *conn;
    GDBusMessage *msg, *ret;
    gint64 start, now;
    GError *error = NULL;

    /* wait until xfconfd finishes starting */
    conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
    if(!conn) {
        g_critical("Failed to connect to D-Bus: %s", error->message);
        g_error_free (error);
        xfconf_tests_end();
        return FALSE;
    }
    msg = g_dbus_message_new_method_call("org.xfce.XfconfTest",
                                         "/org/xfce/Xfconf",
                                         "org.freedesktop.DBus.Peer",
                                         "Ping");
    start = g_get_monotonic_time ();
    while(!(ret = g_dbus_connection_send_message_with_reply_sync(conn,
                                                                 msg, 
                                                                 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
                                                                 -1, NULL, NULL, NULL)))
    {
        now = g_get_monotonic_time ();
        if(now - start > WAIT_TIMEOUT * G_USEC_PER_SEC) {
            g_critical("xfconfd failed to start after %d seconds", WAIT_TIMEOUT);
            g_object_unref (msg);
            xfconf_tests_end();
            return FALSE;
        }
    }
    if (g_dbus_message_get_message_type(ret) != G_DBUS_MESSAGE_TYPE_METHOD_RETURN)
    {
        g_critical("xfconfd is not running and can not be autostarted");
        g_object_unref (msg);
        g_object_unref (ret);
        xfconf_tests_end();
        return FALSE;
    }
    g_object_unref (msg);
    g_object_unref (ret);

    if(!xfconf_init(&error)) {
        g_critical("Failed to init libxfconf: %s", error->message);
        g_error_free(error);
        xfconf_tests_end();
        return FALSE;
    }
    
    return TRUE;
}

static void
xfconf_tests_end(void)
{
    xfconf_shutdown();
}

#endif  /* __XFCONF_TESTS_COMMON_H__ */