Skip to content

Commit

Permalink
[Chromecast][BLE] Unittest for BLE indication
Browse files Browse the repository at this point in the history
Bug: internal 123046902
Test: cast_bluetooth_unittests
Change-Id: Id9e9db5bfb9ae8c7c4fe3fc9b68ece5ccdb4978b
Reviewed-on: https://chromium-review.googlesource.com/c/1428408
Reviewed-by: Luke Halliwell <halliwell@chromium.org>
Commit-Queue: Tiansong Cui <tiansong@google.com>
Cr-Commit-Position: refs/heads/master@{#626747}
  • Loading branch information
tiansong0101 authored and Commit Bot committed Jan 28, 2019
1 parent d89b790 commit 15ebd7d
Showing 1 changed file with 128 additions and 1 deletion.
129 changes: 128 additions & 1 deletion chromecast/device/bluetooth/le/gatt_client_manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ std::vector<bluetooth_v2_shlib::Gatt::Service> GenerateServices() {
service.handle = 0x1;
service.primary = true;

// Generate a characteristic that supports notification only.
characteristic.uuid = {{0x1, 0x1}};
characteristic.handle = 0x2;
characteristic.permissions =
Expand All @@ -85,6 +86,7 @@ std::vector<bluetooth_v2_shlib::Gatt::Service> GenerateServices() {
characteristic.descriptors.push_back(descriptor);
service.characteristics.push_back(characteristic);

// Generate a characteristic that does not support notification or indication.
characteristic.uuid = {{0x1, 0x2}};
characteristic.handle = 0x5;
characteristic.permissions =
Expand All @@ -96,10 +98,61 @@ std::vector<bluetooth_v2_shlib::Gatt::Service> GenerateServices() {
characteristic.descriptors.clear();
service.characteristics.push_back(characteristic);

// Generate a characteristic that supports indication only.
characteristic.uuid = {{0x1, 0x3}};
characteristic.handle = 0x6;
characteristic.permissions =
static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.properties = bluetooth_v2_shlib::Gatt::PROPERTY_INDICATE;

descriptor.uuid = {{0x1, 0x3, 0x1}};
descriptor.handle = 0x7;
descriptor.permissions = static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.descriptors.push_back(descriptor);

descriptor.uuid = RemoteDescriptor::kCccdUuid;
descriptor.handle = 0x8;
descriptor.permissions = static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.descriptors.push_back(descriptor);
service.characteristics.push_back(characteristic);

// Generate a characteristic that supports both notification and indication.
characteristic.uuid = {{0x1, 0x4}};
characteristic.handle = 0x9;
characteristic.permissions =
static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.properties = static_cast<bluetooth_v2_shlib::Gatt::Properties>(
bluetooth_v2_shlib::Gatt::PROPERTY_NOTIFY |
bluetooth_v2_shlib::Gatt::PROPERTY_INDICATE);
characteristic.descriptors.clear();

descriptor.uuid = {{0x1, 0x4, 0x1}};
descriptor.handle = 0xA;
descriptor.permissions = static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.descriptors.push_back(descriptor);

descriptor.uuid = RemoteDescriptor::kCccdUuid;
descriptor.handle = 0xB;
descriptor.permissions = static_cast<bluetooth_v2_shlib::Gatt::Permissions>(
bluetooth_v2_shlib::Gatt::PERMISSION_READ |
bluetooth_v2_shlib::Gatt::PERMISSION_WRITE);
characteristic.descriptors.push_back(descriptor);
service.characteristics.push_back(characteristic);

ret.push_back(service);

service.uuid = {{0x2}};
service.handle = 0x6;
service.handle = 0xC;
service.primary = true;
service.characteristics.clear();
ret.push_back(service);
Expand Down Expand Up @@ -707,6 +760,80 @@ TEST_F(GattClientManagerTest,
fake_task_runner_->RunUntilIdle();
}

TEST_F(GattClientManagerTest, RemoteDeviceCharacteristicSetRegisterIndication) {
const std::vector<uint8_t> kTestData1 = {0x1, 0x2, 0x3};
const auto kServices = GenerateServices();
Connect(kTestAddr1);
scoped_refptr<RemoteDevice> device = GetDevice(kTestAddr1);
bluetooth_v2_shlib::Gatt::Client::Delegate* delegate =
gatt_client_->delegate();
delegate->OnServicesAdded(kTestAddr1, kServices);
std::vector<scoped_refptr<RemoteService>> services =
GetServices(device.get());
ASSERT_EQ(kServices.size(), services.size());

scoped_refptr<RemoteService> service = services[0];
std::vector<scoped_refptr<RemoteCharacteristic>> characteristics =
service->GetCharacteristics();
ASSERT_EQ(characteristics.size(), 4ul);

// |characteristics[2]| supports indication only.
scoped_refptr<RemoteCharacteristic> characteristic = characteristics[2];

scoped_refptr<RemoteDescriptor> cccd =
characteristic->GetDescriptorByUuid(RemoteDescriptor::kCccdUuid);
ASSERT_TRUE(cccd);

EXPECT_CALL(*gatt_client_,
SetCharacteristicNotification(
kTestAddr1, characteristic->characteristic(), true))
.WillOnce(Return(true));
std::vector<uint8_t> cccd_enable_indication = {
std::begin(bluetooth::RemoteDescriptor::kEnableIndicationValue),
std::end(bluetooth::RemoteDescriptor::kEnableIndicationValue)};
EXPECT_CALL(*gatt_client_, WriteDescriptor(kTestAddr1, cccd->descriptor(), _,
cccd_enable_indication))
.WillOnce(Return(true));

characteristic->SetRegisterNotificationOrIndication(true, cb_.Get());
EXPECT_CALL(cb_, Run(true));
delegate->OnDescriptorWriteResponse(kTestAddr1, true, cccd->handle());

EXPECT_CALL(*observer_,
OnCharacteristicNotification(device, characteristic, kTestData1));
delegate->OnNotification(kTestAddr1, characteristic->handle(), kTestData1);
fake_task_runner_->RunUntilIdle();

// |characteristics[3]| supports both notification and indication.
characteristic = characteristics[3];

cccd = characteristic->GetDescriptorByUuid(RemoteDescriptor::kCccdUuid);
ASSERT_TRUE(cccd);

// Notification has higher priority than indication. So
// SetRegisterNotificationOrIndication will behave the same as
// SetRegisterNotification.
EXPECT_CALL(*gatt_client_,
SetCharacteristicNotification(
kTestAddr1, characteristic->characteristic(), true))
.WillOnce(Return(true));
std::vector<uint8_t> cccd_enable_notification = {
std::begin(bluetooth::RemoteDescriptor::kEnableNotificationValue),
std::end(bluetooth::RemoteDescriptor::kEnableNotificationValue)};
EXPECT_CALL(*gatt_client_, WriteDescriptor(kTestAddr1, cccd->descriptor(), _,
cccd_enable_notification))
.WillOnce(Return(true));

characteristic->SetRegisterNotificationOrIndication(true, cb_.Get());
EXPECT_CALL(cb_, Run(true));
delegate->OnDescriptorWriteResponse(kTestAddr1, true, cccd->handle());

EXPECT_CALL(*observer_,
OnCharacteristicNotification(device, characteristic, kTestData1));
delegate->OnNotification(kTestAddr1, characteristic->handle(), kTestData1);
fake_task_runner_->RunUntilIdle();
}

TEST_F(GattClientManagerTest, RemoteDeviceDescriptor) {
const std::vector<uint8_t> kTestData1 = {0x1, 0x2, 0x3};
const std::vector<uint8_t> kTestData2 = {0x4, 0x5, 0x6};
Expand Down

0 comments on commit 15ebd7d

Please sign in to comment.