summaryrefslogtreecommitdiff
path: root/cras/src/tests/hfp_ag_profile_unittest.cc
blob: 3ecd2407e10b0bb79f769c7a1905e6dfae1fcea4 (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
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <gtest/gtest.h>

extern "C" {
#include "cras_bt_log.h"
#include "cras_bt_profile.h"
#include "cras_hfp_ag_profile.h"
#include "cras_iodev.h"
}

static int with_sco_pcm;
static struct cras_iodev fake_sco_out, fake_sco_in;
static struct cras_bt_device* fake_device;
static struct cras_bt_profile* internal_bt_profile;

static size_t hfp_alsa_iodev_create_called;
static size_t hfp_alsa_iodev_destroy_called;
static size_t hfp_iodev_create_called;
static size_t hfp_iodev_destroy_called;
static size_t cras_bt_device_notify_profile_dropped_called;
static struct cras_bt_device* cras_bt_device_notify_profile_dropped_dev;
static enum cras_bt_device_profile
    cras_bt_device_notify_profile_dropped_profile;

static void ResetStubData() {
  hfp_alsa_iodev_create_called = 0;
  hfp_alsa_iodev_destroy_called = 0;
  hfp_iodev_create_called = 0;
  hfp_iodev_destroy_called = 0;
  cras_bt_device_notify_profile_dropped_called = 0;
}

namespace {

class HfpAgProfile : public testing::Test {
 protected:
  virtual void SetUp() {
    btlog = cras_bt_event_log_init();
    ResetStubData();
  }

  virtual void TearDown() { cras_bt_event_log_deinit(btlog); }
};

TEST_F(HfpAgProfile, StartWithoutScoPCM) {
  int ret;
  struct cras_bt_profile* bt_profile;

  with_sco_pcm = 0;
  fake_device = (struct cras_bt_device*)0xdeadbeef;
  /* to get the cras_hfp_ag_profile */
  cras_hfp_ag_profile_create(NULL);
  bt_profile = internal_bt_profile;
  bt_profile->new_connection(NULL, bt_profile, fake_device, 0);

  ret = cras_hfp_ag_start(fake_device);

  EXPECT_EQ(0, ret);
  EXPECT_EQ(2, hfp_iodev_create_called);

  /* Start ag twice won't create more iodev. */
  ret = cras_hfp_ag_start(fake_device);
  EXPECT_EQ(0, ret);
  EXPECT_EQ(2, hfp_iodev_create_called);

  bt_profile->release(bt_profile);

  EXPECT_EQ(2, hfp_iodev_destroy_called);
}

TEST_F(HfpAgProfile, StartWithScoPCM) {
  int ret;
  struct cras_bt_profile* bt_profile;

  with_sco_pcm = 1;
  fake_device = (struct cras_bt_device*)0xdeadbeef;
  /* to get the cras_hfp_ag_profile */
  cras_hfp_ag_profile_create(NULL);
  bt_profile = internal_bt_profile;
  bt_profile->new_connection(NULL, bt_profile, fake_device, 0);

  ret = cras_hfp_ag_start(fake_device);

  EXPECT_EQ(0, ret);
  EXPECT_EQ(2, hfp_alsa_iodev_create_called);

  bt_profile->release(bt_profile);

  EXPECT_EQ(2, hfp_alsa_iodev_destroy_called);
}

TEST_F(HfpAgProfile, RemoveConflictAG) {
  struct cras_bt_profile* bt_profile;
  struct cras_bt_device* new_dev =
      reinterpret_cast<struct cras_bt_device*>(0x123);
  ;

  with_sco_pcm = 0;
  fake_device = (struct cras_bt_device*)0xdeadbeef;
  /* to get the cras_hfp_ag_profile */
  cras_hfp_ag_profile_create(NULL);
  bt_profile = internal_bt_profile;
  bt_profile->new_connection(NULL, bt_profile, fake_device, 0);

  bt_profile->new_connection(NULL, bt_profile, new_dev, 0);

  /* Expect removing conflict of new_dev triggers profile drop on
   * fake_device. */
  cras_hfp_ag_remove_conflict(new_dev);
  EXPECT_EQ(1, cras_bt_device_notify_profile_dropped_called);
  EXPECT_EQ(fake_device, cras_bt_device_notify_profile_dropped_dev);
  EXPECT_EQ(CRAS_BT_DEVICE_PROFILE_HFP_HANDSFREE,
            cras_bt_device_notify_profile_dropped_profile);

  bt_profile->release(bt_profile);
}

}  // namespace

extern "C" {

struct cras_bt_event_log* btlog;

struct cras_iodev* cras_iodev_list_get_sco_pcm_iodev(
    enum CRAS_STREAM_DIRECTION direction) {
  if (with_sco_pcm) {
    if (direction == CRAS_STREAM_OUTPUT)
      return &fake_sco_out;
    else
      return &fake_sco_in;
  }

  return NULL;
}

struct cras_iodev* hfp_alsa_iodev_create(enum CRAS_STREAM_DIRECTION dir,
                                         struct cras_bt_device* device,
                                         struct hfp_slc_handle* slc,
                                         enum cras_bt_device_profile profile) {
  hfp_alsa_iodev_create_called++;
  return (struct cras_iodev*)0xdeadbeef;
}

void hfp_alsa_iodev_destroy(struct cras_iodev* iodev) {
  hfp_alsa_iodev_destroy_called++;
}

struct cras_iodev* hfp_iodev_create(enum CRAS_STREAM_DIRECTION dir,
                                    struct cras_bt_device* device,
                                    struct hfp_slc_handle* slc,
                                    enum cras_bt_device_profile profile,
                                    struct hfp_info* info) {
  hfp_iodev_create_called++;
  return (struct cras_iodev*)0xdeadbeef;
}

void hfp_iodev_destroy(struct cras_iodev* iodev) {
  hfp_iodev_destroy_called++;
}

int cras_bt_add_profile(DBusConnection* conn, struct cras_bt_profile* profile) {
  internal_bt_profile = profile;
  return 0;
}

int cras_bt_rm_profile(DBusConnection* conn, struct cras_bt_profile* profile) {
  internal_bt_profile = NULL;
  return 0;
}

int cras_bt_register_profile(DBusConnection* conn,
                             struct cras_bt_profile* profile) {
  return 0;
}

int cras_bt_register_profiles(DBusConnection* conn) {
  return 0;
}

int cras_bt_unregister_profile(DBusConnection* conn,
                               struct cras_bt_profile* profile) {
  return 0;
}

struct hfp_info* hfp_info_create() {
  return NULL;
}

int hfp_info_running(struct hfp_info* info) {
  return 0;
}

int hfp_info_stop(struct hfp_info* info) {
  return 0;
}

void hfp_info_destroy(struct hfp_info* info) {}

void hfp_slc_destroy(struct hfp_slc_handle* slc_handle) {}

int cras_bt_device_has_a2dp(struct cras_bt_device* device) {
  return 0;
}

int cras_bt_device_disconnect(DBusConnection* conn,
                              struct cras_bt_device* device) {
  return 0;
}

const char* cras_bt_device_name(const struct cras_bt_device* device) {
  return NULL;
}

void cras_bt_device_set_append_iodev_cb(struct cras_bt_device* device,
                                        void (*cb)(void* data)) {}

enum cras_bt_device_profile cras_bt_device_profile_from_uuid(const char* uuid) {
  return CRAS_BT_DEVICE_PROFILE_HSP_AUDIOGATEWAY;
}

struct cras_bt_adapter* cras_bt_device_adapter(
    const struct cras_bt_device* device) {
  return reinterpret_cast<struct cras_bt_adapter*>(0x123);
}

int cras_bt_adapter_wbs_supported(struct cras_bt_adapter* adapter) {
  return 1;
}

struct hfp_slc_handle* hfp_slc_create(int fd,
                                      int is_hsp,
                                      int ag_supported_features,
                                      struct cras_bt_device* device,
                                      hfp_slc_init_cb init_cb,
                                      hfp_slc_disconnect_cb disconnect_cb) {
  return NULL;
}

int hfp_slc_get_selected_codec(struct hfp_slc_handle* handle) {
  return HFP_CODEC_ID_CVSD;
}
int hfp_slc_get_ag_codec_negotiation_supported(struct hfp_slc_handle* handle) {
  return 1;
}

int hfp_slc_get_hf_codec_negotiation_supported(struct hfp_slc_handle* handle) {
  return 1;
}

int hfp_slc_get_hf_supports_battery_indicator(struct hfp_slc_handle* handle) {
  return 0;
}

int hfp_slc_get_hf_battery_level(struct hfp_slc_handle* handle) {
  return -1;
}

struct cras_bt_device* cras_a2dp_connected_device() {
  return NULL;
}

int cras_bt_device_supports_profile(const struct cras_bt_device* device,
                                    enum cras_bt_device_profile profile) {
  return 0;
}

void cras_a2dp_suspend_connected_device(struct cras_bt_device* device) {}

const char* cras_bt_device_address(const struct cras_bt_device* device) {
  return "";
}

int cras_bt_device_audio_gateway_initialized(struct cras_bt_device* device) {
  return 0;
}

void cras_bt_device_notify_profile_dropped(
    struct cras_bt_device* device,
    enum cras_bt_device_profile profile) {
  cras_bt_device_notify_profile_dropped_called++;
  cras_bt_device_notify_profile_dropped_dev = device;
  cras_bt_device_notify_profile_dropped_profile = profile;
}

void hfp_info_set_wbs_logger(struct hfp_info* info,
                             struct packet_status_logger* wbs_logger) {}

void cras_observer_notify_bt_battery_changed(const char* address,
                                             uint32_t level) {
  return;
}

bool cras_system_get_bt_wbs_enabled() {
  return true;
}

int cras_server_metrics_hfp_wideband_selected_codec(int codec) {
  return HFP_CODEC_ID_MSBC;
}

}  // extern "C"

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}