1 // Copyright (c) 2013 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.
5 #include "base/message_loop/message_loop.h"
6 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
7 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
8 #include "chromeos/dbus/fake_bluetooth_device_client.h"
9 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
10 #include "chromeos/dbus/fake_bluetooth_input_client.h"
11 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
13 #include "chromeos/dbus/fake_dbus_thread_manager.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
16 #include "device/bluetooth/bluetooth_adapter_factory.h"
17 #include "device/bluetooth/bluetooth_device.h"
18 #include "device/bluetooth/bluetooth_device_chromeos.h"
19 #include "device/bluetooth/bluetooth_profile.h"
20 #include "device/bluetooth/bluetooth_profile_chromeos.h"
21 #include "device/bluetooth/bluetooth_socket.h"
22 #include "device/bluetooth/bluetooth_socket_chromeos.h"
23 #include "device/bluetooth/bluetooth_uuid.h"
24 #include "net/base/io_buffer.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using device::BluetoothAdapter
;
28 using device::BluetoothDevice
;
29 using device::BluetoothProfile
;
30 using device::BluetoothSocket
;
31 using device::BluetoothUUID
;
35 class BluetoothProfileChromeOSTest
: public testing::Test
{
37 BluetoothProfileChromeOSTest()
39 error_callback_count_(0),
40 profile_callback_count_(0),
41 connection_callback_count_(0),
45 virtual void SetUp() {
46 FakeDBusThreadManager
* fake_dbus_thread_manager
= new FakeDBusThreadManager
;
47 fake_bluetooth_profile_manager_client_
=
48 new FakeBluetoothProfileManagerClient
;
49 fake_dbus_thread_manager
->SetBluetoothProfileManagerClient(
50 scoped_ptr
<BluetoothProfileManagerClient
>(
51 fake_bluetooth_profile_manager_client_
));
52 fake_dbus_thread_manager
->SetBluetoothAgentManagerClient(
53 scoped_ptr
<BluetoothAgentManagerClient
>(
54 new FakeBluetoothAgentManagerClient
));
55 fake_dbus_thread_manager
->SetBluetoothAdapterClient(
56 scoped_ptr
<BluetoothAdapterClient
>(new FakeBluetoothAdapterClient
));
57 fake_dbus_thread_manager
->SetBluetoothDeviceClient(
58 scoped_ptr
<BluetoothDeviceClient
>(new FakeBluetoothDeviceClient
));
59 fake_dbus_thread_manager
->SetBluetoothInputClient(
60 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
61 fake_dbus_thread_manager
->SetBluetoothGattServiceClient(
62 scoped_ptr
<BluetoothGattServiceClient
>(
63 new FakeBluetoothGattServiceClient
));
64 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager
);
66 device::BluetoothAdapterFactory::GetAdapter(
67 base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback
,
68 base::Unretained(this)));
69 ASSERT_TRUE(adapter_
.get() != NULL
);
70 ASSERT_TRUE(adapter_
->IsInitialized());
71 ASSERT_TRUE(adapter_
->IsPresent());
75 base::Bind(&base::DoNothing
),
76 base::Bind(&base::DoNothing
));
77 ASSERT_TRUE(adapter_
->IsPowered());
80 virtual void TearDown() {
82 DBusThreadManager::Shutdown();
85 void AdapterCallback(scoped_refptr
<BluetoothAdapter
> adapter
) {
93 void ErrorCallback() {
94 ++error_callback_count_
;
99 void ConnectToProfileErrorCallback(const std::string
& error
) {
103 void ProfileCallback(BluetoothProfile
* profile
) {
104 ++profile_callback_count_
;
105 last_profile_
= profile
;
108 void ConnectionCallback(const BluetoothDevice
*device
,
109 scoped_refptr
<BluetoothSocket
> socket
) {
110 ++connection_callback_count_
;
111 last_device_
= device
;
112 last_socket_
= socket
;
114 message_loop_
.Quit();
118 base::MessageLoopForIO message_loop_
;
120 FakeBluetoothProfileManagerClient
* fake_bluetooth_profile_manager_client_
;
121 scoped_refptr
<BluetoothAdapter
> adapter_
;
123 unsigned int callback_count_
;
124 unsigned int error_callback_count_
;
125 unsigned int profile_callback_count_
;
126 unsigned int connection_callback_count_
;
127 BluetoothProfile
* last_profile_
;
128 const BluetoothDevice
* last_device_
;
129 scoped_refptr
<BluetoothSocket
> last_socket_
;
132 TEST_F(BluetoothProfileChromeOSTest
, L2capEndToEnd
) {
133 // TODO(rpaquay): Implement this test once the ChromeOS implementation of
134 // BluetoothSocket is done.
136 // Register the profile and expect the profile object to be passed to the
138 BluetoothProfile::Options options
;
139 BluetoothProfile::Register(
140 BluetoothUUID(FakeBluetoothProfileManagerClient::kL2capUuid
),
142 base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback
,
143 base::Unretained(this)));
145 EXPECT_EQ(1U, profile_callback_count_
);
146 EXPECT_TRUE(last_profile_
!= NULL
);
147 BluetoothProfile
* profile
= last_profile_
;
149 // Make sure we have a profile service provider for it.
150 FakeBluetoothProfileServiceProvider
* profile_service_provider
=
151 fake_bluetooth_profile_manager_client_
->GetProfileServiceProvider(
152 FakeBluetoothProfileManagerClient::kL2capUuid
);
153 EXPECT_TRUE(profile_service_provider
!= NULL
);
155 // Register the connection callback.
156 profile
->SetConnectionCallback(
157 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback
,
158 base::Unretained(this)));
160 // Connect to the device, expect the success callback to be called and the
161 // connection callback to be called with the device we passed and a new
163 BluetoothDevice
* device
= adapter_
->GetDevice(
164 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
165 ASSERT_TRUE(device
!= NULL
);
167 device
->ConnectToProfile(
169 base::Bind(&BluetoothProfileChromeOSTest::Callback
,
170 base::Unretained(this)),
171 base::Bind(&BluetoothProfileChromeOSTest::ConnectToProfileErrorCallback
,
172 base::Unretained(this)));
176 EXPECT_EQ(1U, callback_count_
);
177 EXPECT_EQ(0U, error_callback_count_
);
179 EXPECT_EQ(1U, connection_callback_count_
);
180 EXPECT_EQ(device
, last_device_
);
181 EXPECT_TRUE(last_socket_
.get() != NULL
);
183 // Take the ownership of the socket for the remainder of the test and set
184 // up buffers for read/write tests.
185 scoped_refptr
<BluetoothSocket
> socket
= last_socket_
;
189 scoped_refptr
<net::GrowableIOBuffer
> read_buffer
;
191 scoped_refptr
<net::StringIOBuffer
> base_buffer(
192 new net::StringIOBuffer("test"));
193 scoped_refptr
<net::DrainableIOBuffer
> write_buffer
;
195 // Read data from the socket; since no data should be waiting, this should
196 // return success but no data.
197 read_buffer
= new net::GrowableIOBuffer
;
198 success
= socket
->Receive(read_buffer
.get());
199 EXPECT_TRUE(success
);
200 EXPECT_EQ(0, read_buffer
->capacity());
201 EXPECT_EQ(0, read_buffer
->offset());
202 EXPECT_EQ("", socket
->GetLastErrorMessage());
204 // Write data to the socket; the data should be consumed and no bytes should
207 new net::DrainableIOBuffer(base_buffer
.get(), base_buffer
->size());
208 success
= socket
->Send(write_buffer
.get());
209 EXPECT_TRUE(success
);
210 EXPECT_EQ(base_buffer
->size(), write_buffer
->BytesConsumed());
211 EXPECT_EQ(0, write_buffer
->BytesRemaining());
212 EXPECT_EQ("", socket
->GetLastErrorMessage());
214 // Read data from the socket; this should match the data we sent since the
215 // server just echoes us. We have to spin here until there is actually data
217 read_buffer
= new net::GrowableIOBuffer
;
219 success
= socket
->Receive(read_buffer
.get());
220 } while (success
&& read_buffer
->offset() == 0);
221 EXPECT_TRUE(success
);
222 EXPECT_NE(0, read_buffer
->capacity());
223 EXPECT_EQ(base_buffer
->size(), read_buffer
->offset());
224 EXPECT_EQ("", socket
->GetLastErrorMessage());
226 std::string data
= std::string(read_buffer
->StartOfBuffer(),
227 read_buffer
->offset());
228 EXPECT_EQ("test", data
);
230 // Write data to the socket; since the socket is closed, this should return
231 // an error without writing the data and "Disconnected" as the message.
233 new net::DrainableIOBuffer(base_buffer
.get(), base_buffer
->size());
234 success
= socket
->Send(write_buffer
.get());
235 EXPECT_FALSE(success
);
236 EXPECT_EQ(0, write_buffer
->BytesConsumed());
237 EXPECT_EQ(base_buffer
->size(), write_buffer
->BytesRemaining());
238 EXPECT_EQ("Disconnected", socket
->GetLastErrorMessage());
240 // Read data from the socket; since the socket is closed, this should return
241 // an error with "Disconnected" as the last message.
242 read_buffer
= new net::GrowableIOBuffer
;
243 success
= socket
->Receive(read_buffer
.get());
244 EXPECT_FALSE(success
);
245 EXPECT_EQ(0, read_buffer
->capacity());
246 EXPECT_EQ(0, read_buffer
->offset());
247 EXPECT_EQ("Disconnected", socket
->GetLastErrorMessage());
249 // Close our end of the socket.
252 // Unregister the profile, make sure it's no longer registered.
253 last_profile_
->Unregister();
255 profile_service_provider
=
256 fake_bluetooth_profile_manager_client_
->GetProfileServiceProvider(
257 FakeBluetoothProfileManagerClient::kL2capUuid
);
258 EXPECT_TRUE(profile_service_provider
== NULL
);
262 TEST_F(BluetoothProfileChromeOSTest
, RfcommEndToEnd
) {
263 // TODO(rpaquay): Implement this test once the ChromeOS implementation of
264 // BluetoothSocket is done.
266 // Register the profile and expect the profile object to be passed to the
268 BluetoothProfile::Options options
;
269 BluetoothProfile::Register(
270 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid
),
272 base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback
,
273 base::Unretained(this)));
275 EXPECT_EQ(1U, profile_callback_count_
);
276 EXPECT_TRUE(last_profile_
!= NULL
);
277 BluetoothProfile
* profile
= last_profile_
;
279 // Make sure we have a profile service provider for it.
280 FakeBluetoothProfileServiceProvider
* profile_service_provider
=
281 fake_bluetooth_profile_manager_client_
->GetProfileServiceProvider(
282 FakeBluetoothProfileManagerClient::kRfcommUuid
);
283 EXPECT_TRUE(profile_service_provider
!= NULL
);
285 // Register the connection callback.
286 profile
->SetConnectionCallback(
287 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback
,
288 base::Unretained(this)));
290 // Connect to the device, expect the success callback to be called and the
291 // connection callback to be called with the device we passed and a new
293 BluetoothDevice
* device
= adapter_
->GetDevice(
294 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
295 ASSERT_TRUE(device
!= NULL
);
297 device
->ConnectToProfile(
299 base::Bind(&BluetoothProfileChromeOSTest::Callback
,
300 base::Unretained(this)),
301 base::Bind(&BluetoothProfileChromeOSTest::ConnectToProfileErrorCallback
,
302 base::Unretained(this)));
306 EXPECT_EQ(1U, callback_count_
);
307 EXPECT_EQ(0U, error_callback_count_
);
309 EXPECT_EQ(1U, connection_callback_count_
);
310 EXPECT_EQ(device
, last_device_
);
311 EXPECT_TRUE(last_socket_
.get() != NULL
);
313 // Take the ownership of the socket for the remainder of the test and set
314 // up buffers for read/write tests.
315 scoped_refptr
<BluetoothSocket
> socket
= last_socket_
;
319 scoped_refptr
<net::GrowableIOBuffer
> read_buffer
;
321 scoped_refptr
<net::StringIOBuffer
> base_buffer(
322 new net::StringIOBuffer("test"));
323 scoped_refptr
<net::DrainableIOBuffer
> write_buffer
;
325 // Read data from the socket; since no data should be waiting, this should
326 // return success but no data.
327 read_buffer
= new net::GrowableIOBuffer
;
328 success
= socket
->Receive(read_buffer
.get());
329 EXPECT_TRUE(success
);
330 EXPECT_EQ(0, read_buffer
->offset());
331 EXPECT_EQ("", socket
->GetLastErrorMessage());
333 // Write data to the socket; the data should be consumed and no bytes should
336 new net::DrainableIOBuffer(base_buffer
.get(), base_buffer
->size());
337 success
= socket
->Send(write_buffer
.get());
338 EXPECT_TRUE(success
);
339 EXPECT_EQ(base_buffer
->size(), write_buffer
->BytesConsumed());
340 EXPECT_EQ(0, write_buffer
->BytesRemaining());
341 EXPECT_EQ("", socket
->GetLastErrorMessage());
343 // Read data from the socket; this should match the data we sent since the
344 // server just echoes us. We have to spin here until there is actually data
346 read_buffer
= new net::GrowableIOBuffer
;
348 success
= socket
->Receive(read_buffer
.get());
349 } while (success
&& read_buffer
->offset() == 0);
350 EXPECT_TRUE(success
);
351 EXPECT_NE(0, read_buffer
->capacity());
352 EXPECT_EQ(base_buffer
->size(), read_buffer
->offset());
353 EXPECT_EQ("", socket
->GetLastErrorMessage());
355 std::string data
= std::string(read_buffer
->StartOfBuffer(),
356 read_buffer
->offset());
357 EXPECT_EQ("test", data
);
359 // Write data to the socket; since the socket is closed, this should return
360 // an error without writing the data and "Disconnected" as the message.
362 new net::DrainableIOBuffer(base_buffer
.get(), base_buffer
->size());
363 success
= socket
->Send(write_buffer
.get());
364 EXPECT_FALSE(success
);
365 EXPECT_EQ(0, write_buffer
->BytesConsumed());
366 EXPECT_EQ(base_buffer
->size(), write_buffer
->BytesRemaining());
367 EXPECT_EQ("Disconnected", socket
->GetLastErrorMessage());
369 // Read data from the socket; since the socket is closed, this should return
370 // an error with "Disconnected" as the last message.
371 read_buffer
= new net::GrowableIOBuffer
;
372 success
= socket
->Receive(read_buffer
.get());
373 EXPECT_FALSE(success
);
374 EXPECT_EQ(0, read_buffer
->offset());
375 EXPECT_EQ("Disconnected", socket
->GetLastErrorMessage());
377 // Close our end of the socket.
380 // Unregister the profile, make sure it's no longer registered.
381 last_profile_
->Unregister();
383 profile_service_provider
=
384 fake_bluetooth_profile_manager_client_
->GetProfileServiceProvider(
385 FakeBluetoothProfileManagerClient::kRfcommUuid
);
386 EXPECT_TRUE(profile_service_provider
== NULL
);
390 } // namespace chromeos