summaryrefslogtreecommitdiff
path: root/tests/test-dbus.c
blob: 8b14fdb018e7323fc8b2b15718068808067a2957 (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
#include <glib.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/util.h>

static GPtrArray *events;
static TpDBusDaemon *bus;
static GMainLoop *mainloop;
static gchar *two = "2", *five = "5";
static gboolean had_owners = FALSE;

static void
noc (TpDBusDaemon *bus,
     const gchar *name,
     const gchar *new_owner,
     gpointer user_data)
{
  const gchar *tag = user_data;

  g_message ("[%s] %s -> <%s>", tag, name, new_owner);

  g_ptr_array_add (events, g_strdup_printf ("[%s] %s %d",
        tag, name, new_owner[0]));

  if (new_owner[0] != '\0')
    had_owners = TRUE;

  if (!tp_strdiff (name, "net.example"))
    {
      if (new_owner[0] == '\0')
        {
          if (had_owners)
            {
              g_main_loop_quit (mainloop);
            }
          else
            {
              guint ret;
              GError *error = NULL;

              g_assert (tp_cli_dbus_daemon_block_on_request_name (bus, -1,
                    "com.example", 0, &ret, &error));
              g_assert (ret == 1 && error == NULL);
              g_assert (tp_cli_dbus_daemon_block_on_request_name (bus, -1,
                    "org.example", 0, &ret, &error));
              g_assert (ret == 1 && error == NULL);
              g_assert (tp_cli_dbus_daemon_block_on_request_name (bus, -1,
                    "net.example", 0, &ret, &error));
              g_assert (ret == 1 && error == NULL);
            }
        }
      else
        {
          guint ret;
          GError *error = NULL;

          g_assert (tp_dbus_daemon_cancel_name_owner_watch (bus,
                "org.example", noc, five));
          g_assert (tp_cli_dbus_daemon_block_on_release_name (bus, -1,
                "org.example", &ret, &error));
          g_assert (ret == 1 && error == NULL);
          g_assert (tp_cli_dbus_daemon_block_on_release_name (bus, -1,
                "net.example", &ret, &error));
          g_assert (ret == 1 && error == NULL);
        }
    }
}

int
main (int argc,
      char **argv)
{
  mainloop = g_main_loop_new (NULL, FALSE);

  events = g_ptr_array_new ();

  g_type_init ();

  bus = tp_dbus_daemon_new (tp_get_bus ());

  tp_dbus_daemon_watch_name_owner (bus, "com.example", noc, "1", NULL);
  tp_dbus_daemon_watch_name_owner (bus, "com.example", noc, two, NULL);
  tp_dbus_daemon_watch_name_owner (bus, "com.example", noc, "3", NULL);
  tp_dbus_daemon_cancel_name_owner_watch (bus, "com.example", noc, two);
  tp_dbus_daemon_watch_name_owner (bus, "net.example", noc, "4", NULL);
  tp_dbus_daemon_watch_name_owner (bus, "org.example", noc, five, NULL);

  g_main_loop_run (mainloop);

  g_assert (events->len == 9);

  /* 58 == ':' - i.e. the beginning of a unique name */
  g_assert (!tp_strdiff (g_ptr_array_index (events, 0),
        "[1] com.example 0"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 1),
        "[3] com.example 0"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 2),
        "[4] net.example 0"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 3),
        "[5] org.example 0"));

  g_assert (!tp_strdiff (g_ptr_array_index (events, 4),
        "[1] com.example 58"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 5),
        "[3] com.example 58"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 6),
        "[5] org.example 58"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 7),
        "[4] net.example 58"));
  g_assert (!tp_strdiff (g_ptr_array_index (events, 8),
        "[4] net.example 0"));

  return 0;
}