Chromium Code Reviews| 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 "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h" | 6 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h" |
| 7 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" | 7 #include "device/bluetooth/test/bluetooth_gatt_server_test.h" |
| 8 #include "device/bluetooth/test/bluetooth_test.h" | 8 #include "device/bluetooth/test/bluetooth_test.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace device { | 11 namespace device { |
| 12 | 12 |
| 13 class BluetoothLocalGattCharacteristicTest : public BluetoothGattServerTest { | 13 class BluetoothLocalGattCharacteristicTest : public BluetoothGattServerTest { |
| 14 public: | 14 public: |
| 15 void SetUp() override { | 15 void SetUp() override { |
| 16 BluetoothGattServerTest::SetUp(); | 16 BluetoothGattServerTest::SetUp(); |
| 17 | 17 |
| 18 StartGattSetup(); | 18 StartGattSetup(); |
| 19 read_characteristic_ = BluetoothLocalGattCharacteristic::Create( | 19 read_characteristic_ = BluetoothLocalGattCharacteristic::Create( |
| 20 BluetoothUUID(kTestUUIDGenericAttribute), | 20 BluetoothUUID(kTestUUIDGenericAttribute), |
| 21 device::BluetoothLocalGattCharacteristic:: | 21 device::BluetoothLocalGattCharacteristic:: |
| 22 PROPERTY_READ_ENCRYPTED_AUTHENTICATED, | 22 PROPERTY_READ_ENCRYPTED_AUTHENTICATED, |
| 23 device::BluetoothLocalGattCharacteristic::Permissions(), | 23 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 24 service_.get()); | 24 service_.get()); |
| 25 write_characteristic_ = BluetoothLocalGattCharacteristic::Create( | 25 write_characteristic_ = BluetoothLocalGattCharacteristic::Create( |
| 26 BluetoothUUID(kTestUUIDGenericAttribute), | 26 BluetoothUUID(kTestUUIDGenericAttribute), |
| 27 device::BluetoothLocalGattCharacteristic::PROPERTY_RELIABLE_WRITE, | 27 device::BluetoothLocalGattCharacteristic::PROPERTY_RELIABLE_WRITE, |
| 28 device::BluetoothLocalGattCharacteristic::Permissions(), | 28 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 29 service_.get()); | 29 service_.get()); |
| 30 notify_characteristic_ = BluetoothLocalGattCharacteristic::Create( | |
| 31 BluetoothUUID(kTestUUIDGenericAttribute), | |
| 32 device::BluetoothLocalGattCharacteristic::PROPERTY_NOTIFY, | |
| 33 device::BluetoothLocalGattCharacteristic::Permissions(), | |
| 34 service_.get()); | |
| 35 indicate_characteristic_ = BluetoothLocalGattCharacteristic::Create( | |
| 36 BluetoothUUID(kTestUUIDGenericAttribute), | |
| 37 device::BluetoothLocalGattCharacteristic::PROPERTY_INDICATE, | |
| 38 device::BluetoothLocalGattCharacteristic::Permissions(), | |
| 39 service_.get()); | |
| 30 EXPECT_LT(0u, read_characteristic_->GetIdentifier().size()); | 40 EXPECT_LT(0u, read_characteristic_->GetIdentifier().size()); |
| 31 EXPECT_LT(0u, write_characteristic_->GetIdentifier().size()); | 41 EXPECT_LT(0u, write_characteristic_->GetIdentifier().size()); |
| 42 EXPECT_LT(0u, notify_characteristic_->GetIdentifier().size()); | |
| 32 CompleteGattSetup(); | 43 CompleteGattSetup(); |
| 33 } | 44 } |
| 34 | 45 |
| 35 protected: | 46 protected: |
| 36 base::WeakPtr<BluetoothLocalGattCharacteristic> read_characteristic_; | 47 base::WeakPtr<BluetoothLocalGattCharacteristic> read_characteristic_; |
| 37 base::WeakPtr<BluetoothLocalGattCharacteristic> write_characteristic_; | 48 base::WeakPtr<BluetoothLocalGattCharacteristic> write_characteristic_; |
| 49 base::WeakPtr<BluetoothLocalGattCharacteristic> notify_characteristic_; | |
| 50 base::WeakPtr<BluetoothLocalGattCharacteristic> indicate_characteristic_; | |
| 38 }; | 51 }; |
| 39 | 52 |
| 40 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 53 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 41 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { | 54 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { |
| 42 delegate_->value_to_write_ = 0x1337; | 55 delegate_->value_to_write_ = 0x1337; |
| 43 SimulateLocalGattCharacteristicValueReadRequest( | 56 SimulateLocalGattCharacteristicValueReadRequest( |
| 44 service_.get(), read_characteristic_.get(), | 57 service_.get(), read_characteristic_.get(), |
| 45 GetReadValueCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); | 58 GetReadValueCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); |
| 46 | 59 |
| 47 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); | 60 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 WriteLocalCharacteristicValueWrongPermission) { | 114 WriteLocalCharacteristicValueWrongPermission) { |
| 102 const uint64_t kValueToWrite = 0x7331ul; | 115 const uint64_t kValueToWrite = 0x7331ul; |
| 103 SimulateLocalGattCharacteristicValueWriteRequest( | 116 SimulateLocalGattCharacteristicValueWriteRequest( |
| 104 service_.get(), read_characteristic_.get(), GetValue(kValueToWrite), | 117 service_.get(), read_characteristic_.get(), GetValue(kValueToWrite), |
| 105 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); | 118 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 106 | 119 |
| 107 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); | 120 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); |
| 108 } | 121 } |
| 109 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 122 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 110 | 123 |
| 124 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 125 TEST_F(BluetoothLocalGattCharacteristicTest, StartAndStopNotifications) { | |
| 126 EXPECT_FALSE(SimulateLocalGattCharacteristicNotificationsRequest( | |
| 127 service_.get(), read_characteristic_.get(), true)); | |
| 128 EXPECT_FALSE(delegate_->NotificationStatusForCharacteristic( | |
| 129 read_characteristic_.get())); | |
| 130 | |
| 131 EXPECT_FALSE(SimulateLocalGattCharacteristicNotificationsRequest( | |
| 132 service_.get(), write_characteristic_.get(), true)); | |
| 133 EXPECT_FALSE(delegate_->NotificationStatusForCharacteristic( | |
| 134 write_characteristic_.get())); | |
| 135 | |
| 136 EXPECT_TRUE(SimulateLocalGattCharacteristicNotificationsRequest( | |
| 137 service_.get(), notify_characteristic_.get(), true)); | |
| 138 EXPECT_TRUE(delegate_->NotificationStatusForCharacteristic( | |
| 139 notify_characteristic_.get())); | |
| 140 | |
| 141 EXPECT_TRUE(SimulateLocalGattCharacteristicNotificationsRequest( | |
| 142 service_.get(), notify_characteristic_.get(), false)); | |
| 143 EXPECT_FALSE(delegate_->NotificationStatusForCharacteristic( | |
| 144 notify_characteristic_.get())); | |
| 145 } | |
| 146 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 147 | |
| 148 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 149 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotifications) { | |
| 150 const uint64_t kNewValue = 0x3334ul; | |
| 151 EXPECT_EQ( | |
| 152 BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | |
|
scheib
2016/05/12 21:26:59
I think these top two belong in SendNotificationsW
rkc
2016/05/12 21:42:19
Done.
| |
| 153 read_characteristic_->NotifyValueChanged(GetValue(kNewValue), false)); | |
| 154 | |
| 155 EXPECT_EQ( | |
| 156 BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | |
| 157 write_characteristic_->NotifyValueChanged(GetValue(kNewValue), false)); | |
| 158 | |
| 159 const uint64_t kNotifyValue = 0x7331ul; | |
| 160 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, | |
| 161 notify_characteristic_->NotifyValueChanged(GetValue(kNotifyValue), | |
| 162 false)); | |
| 163 EXPECT_EQ(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | |
| 164 notify_characteristic_.get()))); | |
| 165 | |
| 166 const uint64_t kIndicateValue = 0x1337ul; | |
| 167 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFICATION_SUCCESS, | |
| 168 indicate_characteristic_->NotifyValueChanged( | |
| 169 GetValue(kIndicateValue), true)); | |
| 170 EXPECT_EQ(kIndicateValue, GetInteger(LastNotifactionValueForCharacteristic( | |
| 171 indicate_characteristic_.get()))); | |
| 172 } | |
| 173 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 174 | |
| 175 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 176 TEST_F(BluetoothLocalGattCharacteristicTest, SendNotificationsWrongProperties) { | |
| 177 const uint64_t kNotifyValue = 0x7331ul; | |
| 178 EXPECT_EQ( | |
| 179 BluetoothLocalGattCharacteristic::INDICATE_PROPERTY_NOT_SET, | |
| 180 notify_characteristic_->NotifyValueChanged(GetValue(kNotifyValue), true)); | |
| 181 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | |
| 182 notify_characteristic_.get()))); | |
| 183 | |
| 184 const uint64_t kIndicateValue = 0x1337ul; | |
| 185 EXPECT_EQ(BluetoothLocalGattCharacteristic::NOTIFY_PROPERTY_NOT_SET, | |
| 186 indicate_characteristic_->NotifyValueChanged( | |
| 187 GetValue(kIndicateValue), false)); | |
| 188 EXPECT_NE(kIndicateValue, GetInteger(LastNotifactionValueForCharacteristic( | |
| 189 indicate_characteristic_.get()))); | |
| 190 } | |
| 191 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 192 | |
| 193 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 194 TEST_F(BluetoothLocalGattCharacteristicTest, | |
| 195 SendNotificationsServiceNotRegistered) { | |
| 196 service_->Unregister(GetCallback(Call::EXPECTED), | |
| 197 GetGattErrorCallback(Call::NOT_EXPECTED)); | |
| 198 const uint64_t kNotifyValue = 0x7331ul; | |
| 199 EXPECT_EQ(BluetoothLocalGattCharacteristic::SERVICE_NOT_REGISTERED, | |
| 200 notify_characteristic_->NotifyValueChanged(GetValue(kNotifyValue), | |
| 201 false)); | |
| 202 EXPECT_NE(kNotifyValue, GetInteger(LastNotifactionValueForCharacteristic( | |
| 203 notify_characteristic_.get()))); | |
| 204 } | |
| 205 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 206 | |
| 111 } // namespace device | 207 } // namespace device |
| OLD | NEW |