1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/message_loop/message_loop.h"
7 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
11 #include "chromeos/dbus/fake_bluetooth_device_client.h"
12 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h"
17 #include "device/bluetooth/bluetooth_uuid.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using device::BluetoothAdapter
;
21 using device::BluetoothUUID
;
25 class BluetoothAdapterProfileChromeOSTest
: public testing::Test
{
27 BluetoothAdapterProfileChromeOSTest()
28 : fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath
),
29 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath
),
30 fake_delegate_listen_(""),
32 success_callback_count_(0),
33 error_callback_count_(0) {}
35 void SetUp() override
{
36 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
37 DBusThreadManager::GetSetterForTesting();
39 dbus_setter
->SetBluetoothAdapterClient(
40 scoped_ptr
<BluetoothAdapterClient
>(new FakeBluetoothAdapterClient
));
41 dbus_setter
->SetBluetoothAgentManagerClient(
42 scoped_ptr
<BluetoothAgentManagerClient
>(
43 new FakeBluetoothAgentManagerClient
));
44 dbus_setter
->SetBluetoothDeviceClient(
45 scoped_ptr
<BluetoothDeviceClient
>(new FakeBluetoothDeviceClient
));
46 dbus_setter
->SetBluetoothProfileManagerClient(
47 scoped_ptr
<BluetoothProfileManagerClient
>(
48 new FakeBluetoothProfileManagerClient
));
50 // Grab a pointer to the adapter.
51 device::BluetoothAdapterFactory::GetAdapter(
52 base::Bind(&BluetoothAdapterProfileChromeOSTest::AdapterCallback
,
53 base::Unretained(this)));
54 ASSERT_TRUE(adapter_
.get() != nullptr);
55 ASSERT_TRUE(adapter_
->IsInitialized());
56 ASSERT_TRUE(adapter_
->IsPresent());
58 // Turn on the adapter.
59 adapter_
->SetPowered(true, base::Bind(&base::DoNothing
),
60 base::Bind(&base::DoNothing
));
61 ASSERT_TRUE(adapter_
->IsPowered());
64 void TearDown() override
{
66 DBusThreadManager::Shutdown();
69 void AdapterCallback(scoped_refptr
<BluetoothAdapter
> adapter
) {
74 : public chromeos::BluetoothProfileServiceProvider::Delegate
{
76 FakeDelegate(std::string device_path
) : connections_(0) {
77 device_path_
= dbus::ObjectPath(device_path
);
80 // BluetoothProfileServiceProvider::Delegate:
81 void Released() override
{
86 const dbus::ObjectPath
& device_path
,
87 scoped_ptr
<dbus::FileDescriptor
> fd
,
88 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
89 const ConfirmationCallback
& callback
) override
{
90 VLOG(1) << "connection for " << device_path
.value() << " on "
91 << device_path_
.value();
94 close(fd
->TakeValue());
95 callback
.Run(SUCCESS
);
96 if (device_path_
.value() != "")
97 ASSERT_EQ(device_path_
, device_path
);
100 void RequestDisconnection(const dbus::ObjectPath
& device_path
,
101 const ConfirmationCallback
& callback
) override
{
102 VLOG(1) << "disconnect " << device_path
.value();
106 void Cancel() override
{
111 unsigned int connections_
;
112 unsigned int disconnections_
;
113 dbus::ObjectPath device_path_
;
116 FakeDelegate fake_delegate_paired_
;
117 FakeDelegate fake_delegate_autopair_
;
118 FakeDelegate fake_delegate_listen_
;
120 BluetoothAdapterProfileChromeOS
* profile_
;
122 void ProfileSuccessCallback(BluetoothAdapterProfileChromeOS
* profile
) {
124 ++success_callback_count_
;
127 void MatchedProfileCallback(BluetoothAdapterProfileChromeOS
* profile
) {
128 VLOG(1) << "Matched Profile Callback";
129 ASSERT_EQ(profile_
, profile
);
130 ++success_callback_count_
;
133 void DBusConnectSuccessCallback() { ++success_callback_count_
; }
135 void DBusErrorCallback(const std::string
& error_name
,
136 const std::string
& error_message
) {
137 VLOG(1) << "DBus Connect Error: " << error_name
<< " - " << error_message
;
138 ++error_callback_count_
;
141 void BasicErrorCallback(const std::string
& error_message
) {
142 VLOG(1) << "Error: " << error_message
;
143 ++error_callback_count_
;
147 base::MessageLoop message_loop_
;
149 scoped_refptr
<BluetoothAdapter
> adapter_
;
151 unsigned int success_callback_count_
;
152 unsigned int error_callback_count_
;
155 TEST_F(BluetoothAdapterProfileChromeOSTest
, DelegateCount
) {
156 BluetoothUUID
uuid(FakeBluetoothProfileManagerClient::kRfcommUuid
);
157 BluetoothProfileManagerClient::Options options
;
159 options
.require_authentication
.reset(new bool(false));
161 BluetoothAdapterProfileChromeOS::Register(
163 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback
,
164 base::Unretained(this)),
165 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
166 base::Unretained(this)));
168 message_loop_
.RunUntilIdle();
170 EXPECT_TRUE(profile_
);
171 EXPECT_EQ(1U, success_callback_count_
);
172 EXPECT_EQ(0U, error_callback_count_
);
174 EXPECT_EQ(0U, profile_
->DelegateCount());
176 profile_
->SetDelegate(fake_delegate_paired_
.device_path_
,
177 &fake_delegate_paired_
);
179 EXPECT_EQ(1U, profile_
->DelegateCount());
181 profile_
->RemoveDelegate(fake_delegate_autopair_
.device_path_
,
182 base::Bind(&base::DoNothing
));
184 EXPECT_EQ(1U, profile_
->DelegateCount());
186 profile_
->RemoveDelegate(fake_delegate_paired_
.device_path_
,
187 base::Bind(&base::DoNothing
));
189 EXPECT_EQ(0U, profile_
->DelegateCount());
194 TEST_F(BluetoothAdapterProfileChromeOSTest
, BlackHole
) {
195 BluetoothUUID
uuid(FakeBluetoothProfileManagerClient::kRfcommUuid
);
196 BluetoothProfileManagerClient::Options options
;
198 options
.require_authentication
.reset(new bool(false));
200 BluetoothAdapterProfileChromeOS::Register(
202 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback
,
203 base::Unretained(this)),
204 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
205 base::Unretained(this)));
207 message_loop_
.RunUntilIdle();
209 EXPECT_TRUE(profile_
);
210 EXPECT_EQ(1U, success_callback_count_
);
211 EXPECT_EQ(0U, error_callback_count_
);
213 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
214 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
),
215 FakeBluetoothProfileManagerClient::kRfcommUuid
,
217 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback
,
218 base::Unretained(this)),
219 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
220 base::Unretained(this)));
222 message_loop_
.RunUntilIdle();
224 EXPECT_EQ(1U, success_callback_count_
);
225 EXPECT_EQ(1U, error_callback_count_
);
227 EXPECT_EQ(0U, fake_delegate_paired_
.connections_
);
232 TEST_F(BluetoothAdapterProfileChromeOSTest
, Routing
) {
233 BluetoothUUID
uuid(FakeBluetoothProfileManagerClient::kRfcommUuid
);
234 BluetoothProfileManagerClient::Options options
;
236 options
.require_authentication
.reset(new bool(false));
238 profile_
= BluetoothAdapterProfileChromeOS::Register(
240 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback
,
241 base::Unretained(this)),
242 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
243 base::Unretained(this)));
245 message_loop_
.RunUntilIdle();
247 ASSERT_TRUE(profile_
);
248 ASSERT_EQ(1U, success_callback_count_
);
249 ASSERT_EQ(0U, error_callback_count_
);
251 profile_
->SetDelegate(fake_delegate_paired_
.device_path_
,
252 &fake_delegate_paired_
);
253 profile_
->SetDelegate(fake_delegate_autopair_
.device_path_
,
254 &fake_delegate_autopair_
);
255 profile_
->SetDelegate(fake_delegate_listen_
.device_path_
,
256 &fake_delegate_listen_
);
258 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
259 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
),
260 FakeBluetoothProfileManagerClient::kRfcommUuid
,
262 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback
,
263 base::Unretained(this)),
264 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
265 base::Unretained(this)));
267 message_loop_
.RunUntilIdle();
269 EXPECT_EQ(2U, success_callback_count_
);
270 EXPECT_EQ(0U, error_callback_count_
);
272 EXPECT_EQ(1U, fake_delegate_paired_
.connections_
);
274 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
275 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath
),
276 FakeBluetoothProfileManagerClient::kRfcommUuid
,
278 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback
,
279 base::Unretained(this)),
280 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
281 base::Unretained(this)));
283 message_loop_
.RunUntilIdle();
285 EXPECT_EQ(3U, success_callback_count_
);
286 EXPECT_EQ(0U, error_callback_count_
);
288 EXPECT_EQ(1U, fake_delegate_autopair_
.connections_
);
290 // Incoming connections look the same from BlueZ.
291 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
292 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath
),
293 FakeBluetoothProfileManagerClient::kRfcommUuid
,
295 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback
,
296 base::Unretained(this)),
297 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback
,
298 base::Unretained(this)));
300 message_loop_
.RunUntilIdle();
302 EXPECT_EQ(4U, success_callback_count_
);
303 EXPECT_EQ(0U, error_callback_count_
);
305 EXPECT_EQ(1U, fake_delegate_listen_
.connections_
);
310 TEST_F(BluetoothAdapterProfileChromeOSTest
, SimultaneousRegister
) {
311 BluetoothUUID
uuid(FakeBluetoothProfileManagerClient::kRfcommUuid
);
312 BluetoothProfileManagerClient::Options options
;
313 BluetoothAdapterChromeOS
* adapter
=
314 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
316 options
.require_authentication
.reset(new bool(false));
318 success_callback_count_
= 0;
319 error_callback_count_
= 0;
322 uuid
, fake_delegate_paired_
.device_path_
, options
, &fake_delegate_paired_
,
323 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback
,
324 base::Unretained(this)),
325 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback
,
326 base::Unretained(this)));
329 uuid
, fake_delegate_autopair_
.device_path_
, options
,
330 &fake_delegate_autopair_
,
331 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback
,
332 base::Unretained(this)),
333 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback
,
334 base::Unretained(this)));
336 message_loop_
.RunUntilIdle();
338 EXPECT_TRUE(profile_
);
339 EXPECT_EQ(2U, success_callback_count_
);
340 EXPECT_EQ(0U, error_callback_count_
);
342 adapter
->ReleaseProfile(fake_delegate_paired_
.device_path_
, profile_
);
343 adapter
->ReleaseProfile(fake_delegate_autopair_
.device_path_
, profile_
);
345 message_loop_
.RunUntilIdle();
348 TEST_F(BluetoothAdapterProfileChromeOSTest
, SimultaneousRegisterFail
) {
349 BluetoothUUID
uuid(FakeBluetoothProfileManagerClient::kUnregisterableUuid
);
350 BluetoothProfileManagerClient::Options options
;
351 BluetoothAdapterChromeOS
* adapter
=
352 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
354 options
.require_authentication
.reset(new bool(false));
356 success_callback_count_
= 0;
357 error_callback_count_
= 0;
360 uuid
, fake_delegate_paired_
.device_path_
, options
, &fake_delegate_paired_
,
361 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback
,
362 base::Unretained(this)),
363 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback
,
364 base::Unretained(this)));
367 uuid
, fake_delegate_autopair_
.device_path_
, options
,
368 &fake_delegate_autopair_
,
369 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback
,
370 base::Unretained(this)),
371 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback
,
372 base::Unretained(this)));
374 message_loop_
.RunUntilIdle();
376 EXPECT_FALSE(profile_
);
377 EXPECT_EQ(0U, success_callback_count_
);
378 EXPECT_EQ(2U, error_callback_count_
);
381 } // namespace chromeos