summaryrefslogtreecommitdiff
path: root/tests/lib/my-conn-proxy.c
blob: ca1c40ab10de8dbc9d05e7586d2a4b88d4023b91 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * my-conn-proxy.c - a simple subclass of TpConnection
 *
 * Copyright (C) 2010 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 "my-conn-proxy.h"

#include <telepathy-glib/proxy-internal.h>

G_DEFINE_TYPE  (TpTestsMyConnProxy, tp_tests_my_conn_proxy,
    TP_TYPE_CONNECTION)

static void
tp_tests_my_conn_proxy_init (TpTestsMyConnProxy *self)
{
}

enum {
    FEAT_CORE,
    FEAT_A,
    FEAT_B,
    FEAT_WRONG_IFACE,
    FEAT_BAD_DEP,
    FEAT_FAIL,
    FEAT_FAIL_DEP,
    FEAT_RETRY,
    FEAT_RETRY_DEP,
    FEAT_BEFORE_CONNECTED,
    N_FEAT
};

static void
prepare_core_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  /* superclass core features are prepared first */
  g_assert (tp_proxy_is_prepared (proxy, TP_CONNECTION_FEATURE_CORE));

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_core_async);

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_a_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_a_async);

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_b_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));
  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_A));

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_b_async);

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
cannot_be_prepared_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  g_assert_not_reached ();
}

static void
prepare_fail_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));

  result = g_simple_async_result_new_error ((GObject *) proxy, callback,
      user_data, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
      "No feature for you!");

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_retry_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  TpTestsMyConnProxy *self = (TpTestsMyConnProxy *) proxy;
  GSimpleAsyncResult *result;

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_retry_async);

  if (!self->retry_feature_success)
    {
      /* Fail the first time we try to prepare the feature */
      g_simple_async_result_set_error (result, TP_ERRORS,
          TP_ERROR_NOT_YET, "Nah");
    }

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_retry_dep_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_retry_dep_async);

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_before_connected_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  TpTestsMyConnProxy *self = (TpTestsMyConnProxy *) proxy;
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_before_connected_async);

  if (tp_connection_get_status (TP_CONNECTION (self), NULL) ==
        TP_CONNECTION_STATUS_CONNECTED)
    self->before_connected_state = BEFORE_CONNECTED_STATE_CONNECTED;
  else
    self->before_connected_state = BEFORE_CONNECTED_STATE_NOT_CONNECTED;

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static void
prepare_before_connected_before_async (TpProxy *proxy,
    const TpProxyFeature *feature,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  TpTestsMyConnProxy *self = (TpTestsMyConnProxy *) proxy;
  GSimpleAsyncResult *result;

  g_assert (tp_proxy_is_prepared (proxy, TP_TESTS_MY_CONN_PROXY_FEATURE_CORE));

  g_assert_cmpuint (tp_connection_get_status (TP_CONNECTION (proxy), NULL), ==,
      TP_CONNECTION_STATUS_CONNECTING);

  result = g_simple_async_result_new ((GObject *) proxy, callback, user_data,
      prepare_before_connected_before_async);

  self->before_connected_state = BEFORE_CONNECTED_STATE_CONNECTED;

  g_simple_async_result_complete_in_idle (result);
  g_object_unref (result);
}

static const TpProxyFeature *
list_features (TpProxyClass *cls G_GNUC_UNUSED)
{
  static TpProxyFeature features[N_FEAT + 1] = { { 0 } };
  static GQuark need_a[2] = {0, 0};
  static GQuark need_channel_core[2] = {0, 0};
  static GQuark need_wrong_iface[2] = {0, 0};
  static GQuark need_fail[2] = {0, 0};
  static GQuark need_retry[2] = {0, 0};

    if (G_LIKELY (features[0].name != 0))
    return features;

  features[FEAT_CORE].name = TP_TESTS_MY_CONN_PROXY_FEATURE_CORE;
  features[FEAT_CORE].core = TRUE;
  features[FEAT_CORE].prepare_async = prepare_core_async;

  features[FEAT_A].name = TP_TESTS_MY_CONN_PROXY_FEATURE_A;
  features[FEAT_A].prepare_async = prepare_a_async;

  features[FEAT_B].name = TP_TESTS_MY_CONN_PROXY_FEATURE_B;
  features[FEAT_B].prepare_async = prepare_b_async;
  need_a[0] = TP_TESTS_MY_CONN_PROXY_FEATURE_A;
  features[FEAT_B].depends_on = need_a;

  features[FEAT_WRONG_IFACE].name = TP_TESTS_MY_CONN_PROXY_FEATURE_WRONG_IFACE;
  features[FEAT_WRONG_IFACE].prepare_async = cannot_be_prepared_async;
  need_channel_core[0] = TP_CHANNEL_FEATURE_CORE;
  features[FEAT_WRONG_IFACE].interfaces_needed = need_channel_core;

  features[FEAT_BAD_DEP].name = TP_TESTS_MY_CONN_PROXY_FEATURE_BAD_DEP;
  features[FEAT_BAD_DEP].prepare_async = cannot_be_prepared_async;
  need_wrong_iface[0] = TP_TESTS_MY_CONN_PROXY_FEATURE_WRONG_IFACE;
  features[FEAT_BAD_DEP].depends_on = need_wrong_iface;

  features[FEAT_FAIL].name = TP_TESTS_MY_CONN_PROXY_FEATURE_FAIL;
  features[FEAT_FAIL].prepare_async = prepare_fail_async;

  features[FEAT_FAIL_DEP].name = TP_TESTS_MY_CONN_PROXY_FEATURE_FAIL_DEP;
  features[FEAT_FAIL_DEP].prepare_async = cannot_be_prepared_async;
  need_fail[0] = TP_TESTS_MY_CONN_PROXY_FEATURE_FAIL;
  features[FEAT_FAIL_DEP].depends_on = need_fail;

  features[FEAT_RETRY].name = TP_TESTS_MY_CONN_PROXY_FEATURE_RETRY;
  features[FEAT_RETRY].prepare_async = prepare_retry_async;
  features[FEAT_RETRY].can_retry = TRUE;

  features[FEAT_RETRY_DEP].name = TP_TESTS_MY_CONN_PROXY_FEATURE_RETRY_DEP;
  need_retry[0] = TP_TESTS_MY_CONN_PROXY_FEATURE_RETRY;
  features[FEAT_RETRY_DEP].prepare_async = prepare_retry_dep_async;
  features[FEAT_RETRY_DEP].depends_on = need_retry;

  features[FEAT_BEFORE_CONNECTED].name =
    TP_TESTS_MY_CONN_PROXY_FEATURE_BEFORE_CONNECTED;
  features[FEAT_BEFORE_CONNECTED].prepare_async =
    prepare_before_connected_async;
  features[FEAT_BEFORE_CONNECTED].prepare_before_signalling_connected_async =
    prepare_before_connected_before_async;

  return features;
}

static void
tp_tests_my_conn_proxy_class_init (TpTestsMyConnProxyClass *klass)
{
  TpProxyClass *proxy_class = (TpProxyClass *) klass;

  proxy_class->list_features = list_features;
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_core (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-core");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_a (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-a");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_b (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-b");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_wrong_iface (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-wrong_iface");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_bad_dep (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-bad-dep");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_fail (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-fail");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_fail_dep (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-fail-dep");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_retry (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-retry");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_retry_dep (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-retry-dep");
}

GQuark
tp_tests_my_conn_proxy_get_feature_quark_before_connected (void)
{
  return g_quark_from_static_string ("tp-my-conn-proxy-feature-before-connected");
}