summaryrefslogtreecommitdiff
path: root/tests/dbus/self-names.c
blob: e05460377f4b4c2113933dee4dbd3d6ab87f3a42 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Feature test for setting your own name.
 *
 * Copyright © 2013 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "config.h"

#include <telepathy-glib/connection.h>
#include <telepathy-glib/contact.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/debug.h>

#include "tests/lib/contacts-conn.h"
#include "tests/lib/debug.h"
#include "tests/lib/util.h"

typedef struct {
    enum { NAMES_BOTH, NAMES_MISSING, NAMES_ONLY } names;
} Options;

static Options normal_options = { NAMES_BOTH };
static Options no_names_options = { NAMES_MISSING };
static Options only_names_options = { NAMES_ONLY };

typedef struct {
  TpBaseConnection *base_conn;
  TpTestsContactsConnection *service_conn;
  TpHandleRepoIface *service_repo;
  TpConnection *client_conn;
  GAsyncResult *result;
  GError *error;
  guint pending;
} Fixture;

static void
setup (Fixture *f,
    gconstpointer user_data)
{
  const Options *options = user_data;
  GType type;

  if (options == NULL)
    options = &normal_options;

  switch (options->names)
    {
      case NAMES_MISSING:
        type = TP_TESTS_TYPE_NO_NAMES_CONNECTION;
        break;

      case NAMES_ONLY:
        type = TP_TESTS_TYPE_NO_ALIASING_CONNECTION;
        break;

      case NAMES_BOTH:
      default:
        type = TP_TESTS_TYPE_CONTACTS_CONNECTION;
        break;
    }

  tp_tests_create_conn (type,
      "me@test.com", TRUE, &f->base_conn, &f->client_conn);

  f->service_conn = TP_TESTS_CONTACTS_CONNECTION (f->base_conn);
  g_object_ref (f->service_conn);

  f->service_repo = tp_base_connection_get_handles (f->base_conn,
      TP_HANDLE_TYPE_CONTACT);
}

static void
conn_void_cb (TpConnection *conn,
    const GError *error,
    gpointer user_data,
    GObject *nil G_GNUC_UNUSED)
{
  Fixture *f = user_data;

  g_assert_no_error (error);
  g_assert (f->client_conn == conn);
  f->pending--;
}

static void
proxy_void_cb (TpProxy *proxy,
    const GError *error,
    gpointer user_data,
    GObject *nil G_GNUC_UNUSED)
{
  Fixture *f = user_data;

  g_assert_no_error (error);
  g_assert (f->client_conn == (TpConnection *) proxy);
  f->pending--;
}

static void
test_my_name (Fixture *f,
    gconstpointer opts)
{
  const Options *options = opts;
  TpContact *self_contact;
  TpHandle self_handle;
  TpContactFeature features[] = { TP_CONTACT_FEATURE_NAMES };

  if (options == NULL)
    options = &normal_options;

  tp_tests_proxy_run_until_prepared (f->client_conn, NULL);

  self_contact = tp_connection_get_self_contact (f->client_conn);
  self_handle = tp_contact_get_handle (self_contact);

  tp_connection_upgrade_contacts_async (f->client_conn, 1, &self_contact,
      1, features, tp_tests_result_ready_cb, &f->result);
  tp_tests_run_until_result (&f->result);
  tp_connection_upgrade_contacts_finish (f->client_conn, f->result,
      NULL, &f->error);
  g_assert_no_error (f->error);
  g_clear_object (&f->result);

  if (options->names != NAMES_ONLY)
    {
      GHashTable *aus = g_hash_table_new (NULL, NULL);

      /* Changing my legacy alias via Aliasing should modify my nickname */
      f->pending = 1;
      g_hash_table_insert (aus, GUINT_TO_POINTER (self_handle),
          "new legacy alias");
      tp_cli_connection_interface_aliasing_call_set_aliases (f->client_conn,
          -1, aus, conn_void_cb, f, NULL, NULL);
      g_hash_table_unref (aus);

      while (f->pending > 0)
        g_main_context_iteration (NULL, TRUE);

      g_assert_cmpstr (tp_contact_get_alias (self_contact), ==,
          "new legacy alias");

      if (options->names == NAMES_MISSING)
        g_assert_cmpstr (tp_contact_get_nickname (self_contact), ==, NULL);
      else
        g_assert_cmpstr (tp_contact_get_nickname (self_contact), ==,
            "new legacy alias");

      g_assert_cmpstr (tp_contact_get_local_alias (self_contact), ==, NULL);
    }

  if (options->names != NAMES_MISSING)
    {
      GValue value = G_VALUE_INIT;

      /* Changing my nickname via Names should work */
      f->pending = 1;
      g_value_init (&value, G_TYPE_STRING);
      g_value_set_static_string (&value, "my new nickname");
      tp_cli_dbus_properties_call_set (f->client_conn, -1,
          TP_IFACE_CONNECTION_INTERFACE_NAMES, "Nickname",
          &value, proxy_void_cb, f, NULL, NULL);
      g_value_unset (&value);

      while (f->pending > 0)
        g_main_context_iteration (NULL, TRUE);

      g_assert_cmpstr (tp_contact_get_alias (self_contact), ==,
          "my new nickname");
      g_assert_cmpstr (tp_contact_get_nickname (self_contact), ==,
          "my new nickname");
      g_assert_cmpstr (tp_contact_get_local_alias (self_contact), ==, NULL);

      /* Changing my local alias via Names should also work */
      f->pending = 1;
      tp_cli_connection_interface_names_call_set_local_alias (f->client_conn,
          -1, self_handle, "my new local-alias", conn_void_cb, f, NULL,
          NULL);

      while (f->pending > 0)
        g_main_context_iteration (NULL, TRUE);

      g_assert_cmpstr (tp_contact_get_alias (self_contact), ==,
          "my new local-alias");
      g_assert_cmpstr (tp_contact_get_nickname (self_contact), ==,
          "my new nickname");
      g_assert_cmpstr (tp_contact_get_local_alias (self_contact), ==,
          "my new local-alias");
    }
}

static void
teardown (Fixture *f,
    gconstpointer unused G_GNUC_UNUSED)
{
  if (f->client_conn != NULL)
    tp_tests_connection_assert_disconnect_succeeds (f->client_conn);

  g_clear_object (&f->client_conn);
  f->service_repo = NULL;
  g_clear_object (&f->service_conn);
  g_clear_object (&f->base_conn);
  g_clear_object (&f->result);
  g_clear_error (&f->error);
}

int
main (int argc,
      char **argv)
{
  tp_tests_init (&argc, &argv);
  g_test_bug_base ("http://bugs.freedesktop.org/show_bug.cgi?id=");

#define ADD(x, opts) \
  g_test_add ("/self-names/" #x "/" #opts, \
      Fixture, &opts, setup, test_ ## x, teardown)

  ADD (my_name, normal_options);
  ADD (my_name, no_names_options);
  ADD (my_name, only_names_options);

  return g_test_run ();
}