OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 7 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
8 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" | 8 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" |
9 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 9 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
10 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" | 10 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" |
11 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" |
12 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" | 12 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" |
13 #include "chromeos/dbus/fake_bluetooth_input_client.h" | 13 #include "chromeos/dbus/fake_bluetooth_input_client.h" |
14 #include "chromeos/dbus/fake_dbus_thread_manager.h" | 14 #include "chromeos/dbus/fake_dbus_thread_manager.h" |
15 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
16 #include "device/bluetooth/bluetooth_adapter.h" | 16 #include "device/bluetooth/bluetooth_adapter.h" |
17 #include "device/bluetooth/bluetooth_adapter_factory.h" | 17 #include "device/bluetooth/bluetooth_adapter_factory.h" |
18 #include "device/bluetooth/bluetooth_device.h" | 18 #include "device/bluetooth/bluetooth_device.h" |
19 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 19 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 20 #include "device/bluetooth/bluetooth_gatt_connection.h" |
20 #include "device/bluetooth/bluetooth_gatt_descriptor.h" | 21 #include "device/bluetooth/bluetooth_gatt_descriptor.h" |
21 #include "device/bluetooth/bluetooth_gatt_service.h" | 22 #include "device/bluetooth/bluetooth_gatt_service.h" |
22 #include "device/bluetooth/bluetooth_uuid.h" | 23 #include "device/bluetooth/bluetooth_uuid.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 using device::BluetoothAdapter; | 26 using device::BluetoothAdapter; |
26 using device::BluetoothDevice; | 27 using device::BluetoothDevice; |
27 using device::BluetoothGattCharacteristic; | 28 using device::BluetoothGattCharacteristic; |
| 29 using device::BluetoothGattConnection; |
28 using device::BluetoothGattDescriptor; | 30 using device::BluetoothGattDescriptor; |
29 using device::BluetoothGattService; | 31 using device::BluetoothGattService; |
30 using device::BluetoothUUID; | 32 using device::BluetoothUUID; |
31 | 33 |
32 namespace chromeos { | 34 namespace chromeos { |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
36 const BluetoothUUID kHeartRateMeasurementUUID( | 38 const BluetoothUUID kHeartRateMeasurementUUID( |
37 FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID); | 39 FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 336 |
335 adapter_->SetPowered( | 337 adapter_->SetPowered( |
336 true, | 338 true, |
337 base::Bind(&base::DoNothing), | 339 base::Bind(&base::DoNothing), |
338 base::Bind(&base::DoNothing)); | 340 base::Bind(&base::DoNothing)); |
339 ASSERT_TRUE(adapter_->IsPowered()); | 341 ASSERT_TRUE(adapter_->IsPowered()); |
340 } | 342 } |
341 | 343 |
342 virtual void TearDown() { | 344 virtual void TearDown() { |
343 adapter_ = NULL; | 345 adapter_ = NULL; |
| 346 gatt_conn_.reset(); |
344 DBusThreadManager::Shutdown(); | 347 DBusThreadManager::Shutdown(); |
345 } | 348 } |
346 | 349 |
347 void GetAdapter() { | 350 void GetAdapter() { |
348 device::BluetoothAdapterFactory::GetAdapter( | 351 device::BluetoothAdapterFactory::GetAdapter( |
349 base::Bind(&BluetoothGattChromeOSTest::AdapterCallback, | 352 base::Bind(&BluetoothGattChromeOSTest::AdapterCallback, |
350 base::Unretained(this))); | 353 base::Unretained(this))); |
351 ASSERT_TRUE(adapter_.get() != NULL); | 354 ASSERT_TRUE(adapter_.get() != NULL); |
352 ASSERT_TRUE(adapter_->IsInitialized()); | 355 ASSERT_TRUE(adapter_->IsInitialized()); |
353 ASSERT_TRUE(adapter_->IsPresent()); | 356 ASSERT_TRUE(adapter_->IsPresent()); |
354 } | 357 } |
355 | 358 |
356 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | 359 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { |
357 adapter_ = adapter; | 360 adapter_ = adapter; |
358 } | 361 } |
359 | 362 |
360 void SuccessCallback() { | 363 void SuccessCallback() { |
361 ++success_callback_count_; | 364 ++success_callback_count_; |
362 } | 365 } |
363 | 366 |
364 void ValueCallback(const std::vector<uint8>& value) { | 367 void ValueCallback(const std::vector<uint8>& value) { |
365 ++success_callback_count_; | 368 ++success_callback_count_; |
366 last_read_value_ = value; | 369 last_read_value_ = value; |
367 } | 370 } |
368 | 371 |
| 372 void GattConnectionCallback(scoped_ptr<BluetoothGattConnection> conn) { |
| 373 ++success_callback_count_; |
| 374 gatt_conn_ = conn.Pass(); |
| 375 } |
| 376 |
369 void ErrorCallback() { | 377 void ErrorCallback() { |
370 ++error_callback_count_; | 378 ++error_callback_count_; |
371 } | 379 } |
372 | 380 |
| 381 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) { |
| 382 ++error_callback_count_; |
| 383 } |
| 384 |
373 protected: | 385 protected: |
374 base::MessageLoop message_loop_; | 386 base::MessageLoop message_loop_; |
375 | 387 |
376 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; | 388 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
377 FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; | 389 FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; |
378 FakeBluetoothGattCharacteristicClient* | 390 FakeBluetoothGattCharacteristicClient* |
379 fake_bluetooth_gatt_characteristic_client_; | 391 fake_bluetooth_gatt_characteristic_client_; |
380 FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_; | 392 FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_; |
| 393 scoped_ptr<device::BluetoothGattConnection> gatt_conn_; |
381 scoped_refptr<BluetoothAdapter> adapter_; | 394 scoped_refptr<BluetoothAdapter> adapter_; |
382 | 395 |
383 int success_callback_count_; | 396 int success_callback_count_; |
384 int error_callback_count_; | 397 int error_callback_count_; |
385 std::vector<uint8> last_read_value_; | 398 std::vector<uint8> last_read_value_; |
386 }; | 399 }; |
387 | 400 |
| 401 TEST_F(BluetoothGattChromeOSTest, GattConnection) { |
| 402 fake_bluetooth_device_client_->CreateDevice( |
| 403 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
| 404 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 405 BluetoothDevice* device = adapter_->GetDevice( |
| 406 FakeBluetoothDeviceClient::kLowEnergyAddress); |
| 407 ASSERT_TRUE(device); |
| 408 ASSERT_FALSE(device->IsConnected()); |
| 409 ASSERT_FALSE(gatt_conn_.get()); |
| 410 ASSERT_EQ(0, success_callback_count_); |
| 411 ASSERT_EQ(0, error_callback_count_); |
| 412 |
| 413 device->CreateGattConnection( |
| 414 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, |
| 415 base::Unretained(this)), |
| 416 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, |
| 417 base::Unretained(this))); |
| 418 |
| 419 EXPECT_EQ(1, success_callback_count_); |
| 420 EXPECT_EQ(0, error_callback_count_); |
| 421 EXPECT_TRUE(device->IsConnected()); |
| 422 ASSERT_TRUE(gatt_conn_.get()); |
| 423 EXPECT_TRUE(gatt_conn_->IsConnected()); |
| 424 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress, |
| 425 gatt_conn_->GetDeviceAddress()); |
| 426 |
| 427 gatt_conn_->Disconnect( |
| 428 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, |
| 429 base::Unretained(this))); |
| 430 EXPECT_EQ(2, success_callback_count_); |
| 431 EXPECT_EQ(0, error_callback_count_); |
| 432 EXPECT_TRUE(device->IsConnected()); |
| 433 EXPECT_FALSE(gatt_conn_->IsConnected()); |
| 434 |
| 435 device->CreateGattConnection( |
| 436 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, |
| 437 base::Unretained(this)), |
| 438 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, |
| 439 base::Unretained(this))); |
| 440 |
| 441 EXPECT_EQ(3, success_callback_count_); |
| 442 EXPECT_EQ(0, error_callback_count_); |
| 443 EXPECT_TRUE(device->IsConnected()); |
| 444 ASSERT_TRUE(gatt_conn_.get()); |
| 445 EXPECT_TRUE(gatt_conn_->IsConnected()); |
| 446 |
| 447 device->Disconnect( |
| 448 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, |
| 449 base::Unretained(this)), |
| 450 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, |
| 451 base::Unretained(this))); |
| 452 |
| 453 EXPECT_EQ(4, success_callback_count_); |
| 454 EXPECT_EQ(0, error_callback_count_); |
| 455 ASSERT_TRUE(gatt_conn_.get()); |
| 456 EXPECT_FALSE(gatt_conn_->IsConnected()); |
| 457 |
| 458 device->CreateGattConnection( |
| 459 base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback, |
| 460 base::Unretained(this)), |
| 461 base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback, |
| 462 base::Unretained(this))); |
| 463 |
| 464 EXPECT_EQ(5, success_callback_count_); |
| 465 EXPECT_EQ(0, error_callback_count_); |
| 466 EXPECT_TRUE(device->IsConnected()); |
| 467 EXPECT_TRUE(gatt_conn_->IsConnected()); |
| 468 |
| 469 fake_bluetooth_device_client_->RemoveDevice( |
| 470 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
| 471 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 472 ASSERT_TRUE(gatt_conn_.get()); |
| 473 EXPECT_FALSE(gatt_conn_->IsConnected()); |
| 474 } |
| 475 |
388 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { | 476 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { |
389 // Create a fake LE device. We store the device pointer here because this is a | 477 // Create a fake LE device. We store the device pointer here because this is a |
390 // test. It's unsafe to do this in production as the device might get deleted. | 478 // test. It's unsafe to do this in production as the device might get deleted. |
391 fake_bluetooth_device_client_->CreateDevice( | 479 fake_bluetooth_device_client_->CreateDevice( |
392 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), | 480 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
393 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); | 481 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
394 BluetoothDevice* device = adapter_->GetDevice( | 482 BluetoothDevice* device = adapter_->GetDevice( |
395 FakeBluetoothDeviceClient::kLowEnergyAddress); | 483 FakeBluetoothDeviceClient::kLowEnergyAddress); |
396 ASSERT_TRUE(device); | 484 ASSERT_TRUE(device); |
397 | 485 |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 base::Unretained(this))); | 1080 base::Unretained(this))); |
993 EXPECT_EQ(2, success_callback_count_); | 1081 EXPECT_EQ(2, success_callback_count_); |
994 EXPECT_EQ(1, error_callback_count_); | 1082 EXPECT_EQ(1, error_callback_count_); |
995 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); | 1083 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); |
996 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); | 1084 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); |
997 EXPECT_EQ(4, service_observer.gatt_service_changed_count_); | 1085 EXPECT_EQ(4, service_observer.gatt_service_changed_count_); |
998 EXPECT_EQ(2, service_observer.gatt_descriptor_value_changed_count_); | 1086 EXPECT_EQ(2, service_observer.gatt_descriptor_value_changed_count_); |
999 } | 1087 } |
1000 | 1088 |
1001 } // namespace chromeos | 1089 } // namespace chromeos |
OLD | NEW |