summaryrefslogtreecommitdiff
path: root/cras/src/tests/bt_profile_unittest.cc
blob: 0758b448baa2af1db4c7af4003e7b6f8f9bc2696 (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
/* Copyright (c) 2013 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 <dbus/dbus.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <sys/socket.h>

#include "dbus_test.h"

extern "C" {
#include "cras_bt_constants.h"
#include "cras_bt_profile.h"
}

namespace {

static struct cras_bt_profile fake_profile;
static struct cras_bt_transport* fake_transport;
static int profile_release_called;
static struct cras_bt_profile* profile_release_arg_value;
static int profile_new_connection_called;
static struct cras_bt_transport* profile_new_connection_arg_value;
static int profile_request_disconnection_called;
static struct cras_bt_transport* profile_request_disconnection_arg_value;
static int profile_cancel_called;
static struct cras_bt_profile* profile_cancel_arg_value;
static int cras_bt_transport_get_called;
static const char* cras_bt_transport_get_arg_value;

void fake_profile_release(struct cras_bt_profile* profile);
void fake_profile_new_connection(struct cras_bt_profile* profile,
                                 struct cras_bt_transport* transport);
void fake_profile_request_disconnection(struct cras_bt_profile* profile,
                                        struct cras_bt_transport* transport);
void fake_profile_cancel(struct cras_bt_profile* profile);

class BtProfileTestSuite : public DBusTest {
  virtual void SetUp() {
    DBusTest::SetUp();

    profile_release_called = 0;
    profile_new_connection_called = 0;
    profile_request_disconnection_called = 0;
    profile_cancel_called = 0;

    fake_profile.name = "fake";
    fake_profile.object_path = "/fake";
    fake_profile.uuid = "0";
    fake_profile.version = 0;
    fake_profile.role = NULL;
    fake_profile.features = 0;
    fake_profile.release = fake_profile_release;
    fake_profile.new_connection = fake_profile_new_connection;
    fake_profile.request_disconnection = fake_profile_request_disconnection;
    fake_profile.cancel = fake_profile_cancel;

    fake_transport = reinterpret_cast<struct cras_bt_transport*>(0x321);
    cras_bt_transport_get_called = 0;
  }
};

TEST_F(BtProfileTestSuite, RegisterProfile) {
  struct cras_bt_profile* profile;

  ExpectMethodCall(PROFILE_MANAGER_OBJ_PATH, BLUEZ_PROFILE_MGMT_INTERFACE,
                   "RegisterProfile")
      .WithObjectPath("/fake")
      .SendReply();

  cras_bt_add_profile(conn_, &fake_profile);
  cras_bt_register_profiles(conn_);

  WaitForMatches();
  profile = cras_bt_profile_get("/fake");

  EXPECT_TRUE(profile == &fake_profile);
}

TEST_F(BtProfileTestSuite, ResetProfile) {
  cras_bt_add_profile(conn_, &fake_profile);
  cras_bt_profile_reset();

  ASSERT_EQ(1, profile_release_called);
}

TEST_F(BtProfileTestSuite, HandleMessage) {
  ExpectMethodCall(PROFILE_MANAGER_OBJ_PATH, BLUEZ_PROFILE_MGMT_INTERFACE,
                   "RegisterProfile")
      .WithObjectPath("/fake")
      .SendReply();

  cras_bt_add_profile(conn_, &fake_profile);
  cras_bt_register_profiles(conn_);

  WaitForMatches();

  /* Use stdin as mock fd */
  CreateMessageCall("/fake", "org.bluez.Profile1", "NewConnection")
      .WithString("device")
      .WithUnixFd(0)
      .Send();

  WaitForMatches();
  ASSERT_EQ(1, profile_new_connection_called);
  ASSERT_STREQ("device", cras_bt_transport_get_arg_value);
  ASSERT_EQ(1, cras_bt_transport_get_called);
  ASSERT_EQ(fake_transport, profile_new_connection_arg_value);

  CreateMessageCall("/fake", "org.bluez.Profile1", "RequestDisconnection")
      .WithString("device")
      .Send();
  WaitForMatches();
  ASSERT_EQ(2, cras_bt_transport_get_called);
  ASSERT_EQ(1, profile_request_disconnection_called);
  ASSERT_EQ(fake_transport, profile_request_disconnection_arg_value);

  CreateMessageCall("/fake", "org.bluez.Profile1", "Release").Send();
  WaitForMatches();
  ASSERT_EQ(1, profile_release_called);
  ASSERT_EQ(&fake_profile, profile_release_arg_value);

  CreateMessageCall("/fake", "org.bluez.Profile1", "Cancel").Send();
  WaitForMatches();
  ASSERT_EQ(1, profile_cancel_called);
  ASSERT_EQ(&fake_profile, profile_cancel_arg_value);
}

void fake_profile_release(struct cras_bt_profile* profile) {
  profile_release_arg_value = profile;
  profile_release_called++;
}

void fake_profile_new_connection(struct cras_bt_profile* profile,
                                 struct cras_bt_transport* transport) {
  profile_new_connection_arg_value = transport;
  profile_new_connection_called++;
}

void fake_profile_request_disconnection(struct cras_bt_profile* profile,
                                        struct cras_bt_transport* transport) {
  profile_request_disconnection_arg_value = transport;
  profile_request_disconnection_called++;
}

void fake_profile_cancel(struct cras_bt_profile* profile) {
  profile_cancel_arg_value = profile;
  profile_cancel_called++;
}

}  // namespace

extern "C" {
dbus_bool_t append_key_value(DBusMessageIter* iter,
                             const char* key,
                             int type,
                             const char* type_string,
                             void* value) {
  return TRUE;
}

struct cras_bt_transport* cras_bt_transport_get(const char* object_path) {
  cras_bt_transport_get_called++;
  cras_bt_transport_get_arg_value = object_path;
  return fake_transport;
}

void cras_bt_transport_destroy(struct cras_bt_transport* transport) {}

struct cras_bt_transport* cras_bt_transport_create(DBusConnection* conn,
                                                   const char* object_path) {
  return fake_transport;
}

}  // extern "C"

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