| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <tuple> |
| 6 |
| 5 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 6 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h" |
| 7 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" | 9 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" |
| 8 #include "device/bluetooth/test/bluetooth_test.h" | 10 #include "device/bluetooth/test/bluetooth_test.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 12 |
| 11 namespace device { | 13 namespace device { |
| 12 | 14 |
| 13 class BluetoothLocalGattCharacteristicTest : public BluetoothGattServerTest { | 15 class BluetoothLocalGattCharacteristicTest : public BluetoothGattServerTest { |
| 14 public: | 16 public: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 39 BluetoothUUID(kTestUUIDGenericAttribute), | 41 BluetoothUUID(kTestUUIDGenericAttribute), |
| 40 device::BluetoothLocalGattCharacteristic::PROPERTY_INDICATE, | 42 device::BluetoothLocalGattCharacteristic::PROPERTY_INDICATE, |
| 41 device::BluetoothLocalGattCharacteristic::Permissions(), | 43 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 42 service_.get()); | 44 service_.get()); |
| 43 EXPECT_LT(0u, read_characteristic_->GetIdentifier().size()); | 45 EXPECT_LT(0u, read_characteristic_->GetIdentifier().size()); |
| 44 EXPECT_LT(0u, write_characteristic_->GetIdentifier().size()); | 46 EXPECT_LT(0u, write_characteristic_->GetIdentifier().size()); |
| 45 EXPECT_LT(0u, notify_characteristic_->GetIdentifier().size()); | 47 EXPECT_LT(0u, notify_characteristic_->GetIdentifier().size()); |
| 46 CompleteGattSetup(); | 48 CompleteGattSetup(); |
| 47 } | 49 } |
| 48 | 50 |
| 51 void CheckNotification( |
| 52 const BluetoothDevice* expected_device, |
| 53 uint64_t expected_value, |
| 54 bool expected_indicate_flag, |
| 55 const BluetoothTestBase::NotificationType& actual_notification) { |
| 56 if (expected_device) { |
| 57 EXPECT_EQ(expected_device->GetIdentifier(), |
| 58 actual_notification.device_path); |
| 59 } |
| 60 EXPECT_EQ(GetValue(expected_value), actual_notification.value); |
| 61 EXPECT_EQ(expected_indicate_flag, actual_notification.indicate); |
| 62 } |
| 63 |
| 49 protected: | 64 protected: |
| 50 base::WeakPtr<BluetoothLocalGattCharacteristic> read_characteristic_; | 65 base::WeakPtr<BluetoothLocalGattCharacteristic> read_characteristic_; |
| 51 base::WeakPtr<BluetoothLocalGattCharacteristic> write_characteristic_; | 66 base::WeakPtr<BluetoothLocalGattCharacteristic> write_characteristic_; |
| 52 base::WeakPtr<BluetoothLocalGattCharacteristic> notify_characteristic_; | 67 base::WeakPtr<BluetoothLocalGattCharacteristic> notify_characteristic_; |
| 53 base::WeakPtr<BluetoothLocalGattCharacteristic> indicate_characteristic_; | 68 base::WeakPtr<BluetoothLocalGattCharacteristic> indicate_characteristic_; |
| 54 BluetoothDevice* device_; | 69 BluetoothDevice* device_; |
| 55 }; | 70 }; |
| 56 | 71 |
| 57 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 72 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 58 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { | 73 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 165 |
| 151 EXPECT_TRUE(SimulateLocalGattCharacteristicNotificationsRequest( | 166 EXPECT_TRUE(SimulateLocalGattCharacteristicNotificationsRequest( |
| 152 notify_characteristic_.get(), false)); | 167 notify_characteristic_.get(), false)); |
| 153 EXPECT_FALSE(delegate_->NotificationStatusForCharacteristic( | 168 EXPECT_FALSE(delegate_->NotificationStatusForCharacteristic( |
| 154 notify_characteristic_.get())); | 169 notify_characteristic_.get())); |
| 155 } | 170 } |
| 156 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 171 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 157 | 172 |
| 158 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 173 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 159 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotifications) { | 174 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotifications) { |
| 175 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 176 const uint64_t kNotifyValue = 0x7331ul; |
| 177 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, |
| 178 notify_characteristic_->NotifyValueChanged( |
| 179 device, GetValue(kNotifyValue), false)); |
| 180 CheckNotification( |
| 181 device, kNotifyValue, false, |
| 182 LastNotifactionValueForCharacteristic(notify_characteristic_.get())); |
| 183 |
| 184 const uint64_t kIndicateValue = 0x1337ul; |
| 185 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, |
| 186 indicate_characteristic_->NotifyValueChanged( |
| 187 device, GetValue(kIndicateValue), true)); |
| 188 CheckNotification( |
| 189 device, kIndicateValue, true, |
| 190 LastNotifactionValueForCharacteristic(indicate_characteristic_.get())); |
| 191 } |
| 192 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 193 |
| 194 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 195 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotificationsToNullDevice) { |
| 160 const uint64_t kNotifyValue = 0x7331ul; | 196 const uint64_t kNotifyValue = 0x7331ul; |
| 161 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, | 197 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, |
| 162 notify_characteristic_->NotifyValueChanged( | 198 notify_characteristic_->NotifyValueChanged( |
| 163 nullptr, GetValue(kNotifyValue), false)); | 199 nullptr, GetValue(kNotifyValue), false)); |
| 164 EXPECT_EQ(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | 200 CheckNotification( |
| 165 notify_characteristic_.get()))); | 201 nullptr, kNotifyValue, false, |
| 202 LastNotifactionValueForCharacteristic(notify_characteristic_.get())); |
| 166 | 203 |
| 167 const uint64_t kIndicateValue = 0x1337ul; | 204 const uint64_t kIndicateValue = 0x1337ul; |
| 168 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, | 205 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, |
| 169 indicate_characteristic_->NotifyValueChanged( | 206 indicate_characteristic_->NotifyValueChanged( |
| 170 nullptr, GetValue(kIndicateValue), true)); | 207 nullptr, GetValue(kIndicateValue), true)); |
| 171 EXPECT_EQ(kIndicateValue, GetInteger(LastNotifactionValueForCharacteristic( | 208 CheckNotification( |
| 172 indicate_characteristic_.get()))); | 209 nullptr, kIndicateValue, true, |
| 210 LastNotifactionValueForCharacteristic(indicate_characteristic_.get())); |
| 173 } | 211 } |
| 174 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 212 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 175 | 213 |
| 176 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 214 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 177 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotificationsWrongProperties) { | 215 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotificationsWrongProperties) { |
| 216 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 178 const uint64_t kNewValue = 0x3334ul; | 217 const uint64_t kNewValue = 0x3334ul; |
| 179 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | 218 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, |
| 180 read_characteristic_->NotifyValueChanged( | 219 read_characteristic_->NotifyValueChanged( |
| 181 nullptr, GetValue(kNewValue), false)); | 220 device, GetValue(kNewValue), false)); |
| 182 EXPECT_NE(kNewValue, GetInteger(LastNotifactionValueForCharacteristic( | 221 EXPECT_NE(kNewValue, GetInteger(LastNotifactionValueForCharacteristic( |
| 183 read_characteristic_.get()))); | 222 read_characteristic_.get()) |
| 223 .value)); |
| 184 | 224 |
| 185 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | 225 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, |
| 186 write_characteristic_->NotifyValueChanged( | 226 write_characteristic_->NotifyValueChanged( |
| 187 nullptr, GetValue(kNewValue), false)); | 227 device, GetValue(kNewValue), false)); |
| 188 EXPECT_NE(kNewValue, GetInteger(LastNotifactionValueForCharacteristic( | 228 EXPECT_NE(kNewValue, GetInteger(LastNotifactionValueForCharacteristic( |
| 189 write_characteristic_.get()))); | 229 write_characteristic_.get()) |
| 230 .value)); |
| 190 | 231 |
| 191 const uint64_t kNotifyValue = 0x7331ul; | 232 const uint64_t kNotifyValue = 0x7331ul; |
| 192 EXPECT_EQ(BluetoothLocalGattCharacteristic::INDICATE_PROPERTY_NOT_SET, | 233 EXPECT_EQ(BluetoothLocalGattCharacteristic::INDICATE_PROPERTY_NOT_SET, |
| 193 notify_characteristic_->NotifyValueChanged( | 234 notify_characteristic_->NotifyValueChanged( |
| 194 nullptr, GetValue(kNotifyValue), true)); | 235 device, GetValue(kNotifyValue), true)); |
| 195 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | 236 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( |
| 196 notify_characteristic_.get()))); | 237 notify_characteristic_.get()) |
| 238 .value)); |
| 197 | 239 |
| 198 const uint64_t kIndicateValue = 0x1337ul; | 240 const uint64_t kIndicateValue = 0x1337ul; |
| 199 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | 241 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, |
| 200 indicate_characteristic_->NotifyValueChanged( | 242 indicate_characteristic_->NotifyValueChanged( |
| 201 nullptr, GetValue(kIndicateValue), false)); | 243 device, GetValue(kIndicateValue), false)); |
| 202 EXPECT_NE(kIndicateValue, GetInteger(LastNotifactionValueForCharacteristic( | 244 EXPECT_NE(kIndicateValue, GetInteger(LastNotifactionValueForCharacteristic( |
| 203 indicate_characteristic_.get()))); | 245 indicate_characteristic_.get()) |
| 246 .value)); |
| 204 } | 247 } |
| 205 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 248 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 206 | 249 |
| 207 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 250 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 208 TEST_F(BluetoothLocalGattCharacteristicTest, | 251 TEST_F(BluetoothLocalGattCharacteristicTest, |
| 209 SendNotificationsServiceNotRegistered) { | 252 SendNotificationsServiceNotRegistered) { |
| 253 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 210 service_->Unregister(GetCallback(Call::EXPECTED), | 254 service_->Unregister(GetCallback(Call::EXPECTED), |
| 211 GetGattErrorCallback(Call::NOT_EXPECTED)); | 255 GetGattErrorCallback(Call::NOT_EXPECTED)); |
| 212 const uint64_t kNotifyValue = 0x7331ul; | 256 const uint64_t kNotifyValue = 0x7331ul; |
| 213 EXPECT_EQ(BluetoothLocalGattCharacteristic::SERVICE_NOT_REGISTERED, | 257 EXPECT_EQ(BluetoothLocalGattCharacteristic::SERVICE_NOT_REGISTERED, |
| 214 notify_characteristic_->NotifyValueChanged( | 258 notify_characteristic_->NotifyValueChanged( |
| 215 nullptr, GetValue(kNotifyValue), false)); | 259 device, GetValue(kNotifyValue), false)); |
| 216 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | 260 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( |
| 217 notify_characteristic_.get()))); | 261 notify_characteristic_.get()) |
| 262 .value)); |
| 218 } | 263 } |
| 219 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 264 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 220 | 265 |
| 221 } // namespace device | 266 } // namespace device |
| OLD | NEW |