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