File: dtmf-player.c

package info (click to toggle)
telepathy-glib 0.24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 34,628 kB
  • sloc: ansic: 124,643; xml: 34,410; sh: 11,299; python: 3,520; makefile: 1,727; cpp: 16
file content (318 lines) | stat: -rw-r--r-- 8,004 bytes parent folder | download | duplicates (3)
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
#include "config.h"

#include "telepathy-glib/dtmf.h"

#include <telepathy-glib/util.h>

#include "tests/lib/util.h"

static void
test_to_char (void)
{
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_0), ==, '0');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_1), ==, '1');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_2), ==, '2');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_3), ==, '3');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_4), ==, '4');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_5), ==, '5');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_6), ==, '6');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_7), ==, '7');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_8), ==, '8');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_DIGIT_9), ==, '9');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_LETTER_A), ==,
      'A');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_LETTER_B), ==,
      'B');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_LETTER_C), ==,
      'C');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_LETTER_D), ==,
      'D');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_HASH), ==, '#');
  g_assert_cmpint (tp_dtmf_event_to_char (TP_DTMF_EVENT_ASTERISK), ==,
      '*');
}

typedef struct {
    TpDTMFPlayer *dtmf_player;
    GString *log;
    GError *error /* initially NULL */ ;
} Fixture;

static void
fixture_log (Fixture *f,
    const gchar *format,
    ...)
{
  va_list ap;

  va_start (ap, format);
  g_string_append_vprintf (f->log, format, ap);
  g_string_append_c (f->log, '\n');
  va_end (ap);
}

static void
fixture_assert_log (Fixture *f,
    const gchar *expected)
{
  g_assert_cmpstr (f->log->str, ==, expected);
}

static void
started_tone_cb (TpDTMFPlayer *dtmf_player G_GNUC_UNUSED,
    guint event,
    Fixture *f)
{
  fixture_log (f, "started '%c'", tp_dtmf_event_to_char (event));
}

static void
stopped_tone_cb (TpDTMFPlayer *dtmf_player G_GNUC_UNUSED,
    Fixture *f)
{
  fixture_log (f, "stopped");
}

static void
finished_cb (TpDTMFPlayer *dtmf_player G_GNUC_UNUSED,
    gboolean cancelled,
    Fixture *f)
{
  if (cancelled)
    fixture_log (f, "cancelled");
  else
    fixture_log (f, "finished");
}

static void
deferred_cb (TpDTMFPlayer *dtmf_player G_GNUC_UNUSED,
    const gchar *tones,
    Fixture *f)
{
  fixture_log (f, "deferred '%s'", tones);
}

static void
setup (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  f->dtmf_player = tp_dtmf_player_new ();
  g_assert (f->dtmf_player != NULL);

  f->log = g_string_new ("");

  g_signal_connect (f->dtmf_player, "started-tone",
      G_CALLBACK (started_tone_cb), f);
  g_signal_connect (f->dtmf_player, "stopped-tone",
      G_CALLBACK (stopped_tone_cb), f);
  g_signal_connect (f->dtmf_player, "finished",
      G_CALLBACK (finished_cb), f);
  g_signal_connect (f->dtmf_player, "tones-deferred",
      G_CALLBACK (deferred_cb), f);
}

static void
teardown (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  g_signal_handlers_disconnect_matched (f->dtmf_player, G_SIGNAL_MATCH_DATA,
      0, 0, 0, 0, f);

  tp_clear_object (&f->dtmf_player);
  g_string_free (f->log, TRUE);
  g_clear_error (&f->error);
}

static void
test_noop (Fixture *f G_GNUC_UNUSED,
    gconstpointer nil G_GNUC_UNUSED)
{
}

static void
test_empty (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "", 1, 1, 1, &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (!tp_dtmf_player_is_active (f->dtmf_player));

  fixture_assert_log (f, "finished\n");
}

static void
test_cancel (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "#", 10000, 1, 1, &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));

  tp_dtmf_player_cancel (f->dtmf_player);
  g_assert (!tp_dtmf_player_is_active (f->dtmf_player));

  fixture_assert_log (f,
      "started '#'\n"
      "stopped\n"
      "cancelled\n");
}

static void
test_cancel_in_gap (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "#*", 1, 10000, 1, &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));
  fixture_assert_log (f, "started '#'\n");

  tp_dtmf_player_cancel (f->dtmf_player);
  g_assert (!tp_dtmf_player_is_active (f->dtmf_player));

  fixture_assert_log (f,
      "started '#'\n"
      "stopped\n"
      "cancelled\n");
}

static void
test_cancel_in_pause (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "#p*", 1, 1, 10000, &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));
  fixture_assert_log (f, "started '#'\n");

  /* wait 1ms for it to reach the pause */
  while (tp_strdiff (f->log->str, "started '#'\nstopped\n"))
    g_main_context_iteration (NULL, TRUE);

  tp_dtmf_player_cancel (f->dtmf_player);
  g_assert (!tp_dtmf_player_is_active (f->dtmf_player));

  fixture_assert_log (f,
      "started '#'\n"
      "stopped\n"
      "cancelled\n");
}

static void
test_sequence (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "*12,3#", 1, 1, 1,
      &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));
  fixture_assert_log (f, "started '*'\n");

  while (tp_dtmf_player_is_active (f->dtmf_player))
    g_main_context_iteration (NULL, TRUE);

  fixture_assert_log (f,
      "started '*'\n"
      "stopped\n"
      "started '1'\n"
      "stopped\n"
      "started '2'\n"
      "stopped\n"
      /* at this point we'd wait longer than usual - you can't tell here,
       * because we have 1ms as the gap time and also as the pause time,
       * and we're not keeping track anyway */
      "started '3'\n"
      "stopped\n"
      "started '#'\n"
      "stopped\n"
      "finished\n");
}

static void
test_wait (Fixture *f,
    gconstpointer nil G_GNUC_UNUSED)
{
  gboolean ok;

  ok = tp_dtmf_player_play (f->dtmf_player, "*w#w", 1, 1, 1,
      &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));
  fixture_assert_log (f, "started '*'\n");

  while (tp_dtmf_player_is_active (f->dtmf_player))
    g_main_context_iteration (NULL, TRUE);

  fixture_assert_log (f,
      "started '*'\n"
      "stopped\n"
      "deferred '#w'\n"
      "finished\n");

  /* Now the user's ready to continue. */

  ok = tp_dtmf_player_play (f->dtmf_player, "#w", 1, 1, 1,
      &f->error);
  g_assert_no_error (f->error);
  g_assert (ok);
  g_assert (tp_dtmf_player_is_active (f->dtmf_player));

  fixture_assert_log (f,
      "started '*'\n"
      "stopped\n"
      "deferred '#w'\n"
      "finished\n"
      "started '#'\n");

  while (tp_dtmf_player_is_active (f->dtmf_player))
    g_main_context_iteration (NULL, TRUE);

  /* It doesn't really make sense to defer "", so we just stop there */
  fixture_assert_log (f,
      "started '*'\n"
      "stopped\n"
      "deferred '#w'\n"
      "finished\n"
      "started '#'\n"
      "stopped\n"
      "finished\n");
}

int
main (int argc,
    char **argv)
{
#define TEST_PREFIX "/dtmf-player/"
#define FIXTURE_TEST(x) \
  g_test_add (TEST_PREFIX #x, Fixture, NULL, setup, test_ ## x, teardown)

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

  g_test_add_func (TEST_PREFIX "to_char", test_to_char);

  FIXTURE_TEST (noop);
  FIXTURE_TEST (empty);
  FIXTURE_TEST (cancel);
  FIXTURE_TEST (cancel_in_gap);
  FIXTURE_TEST (cancel_in_pause);
  FIXTURE_TEST (sequence);
  FIXTURE_TEST (wait);

  return g_test_run ();
}