| 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 characteristic_ = BluetoothLocalGattCharacteristic::Create( | 19 read_characteristic_ = BluetoothLocalGattCharacteristic::Create( |
| 20 BluetoothUUID(kTestUUIDGenericAttribute), | 20 BluetoothUUID(kTestUUIDGenericAttribute), |
| 21 device::BluetoothLocalGattCharacteristic::Properties(), | 21 device::BluetoothLocalGattCharacteristic:: |
| 22 PROPERTY_READ_ENCRYPTED_AUTHENTICATED, |
| 22 device::BluetoothLocalGattCharacteristic::Permissions(), | 23 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 23 service_.get()); | 24 service_.get()); |
| 24 EXPECT_LT(0u, characteristic_->GetIdentifier().size()); | 25 write_characteristic_ = BluetoothLocalGattCharacteristic::Create( |
| 26 BluetoothUUID(kTestUUIDGenericAttribute), |
| 27 device::BluetoothLocalGattCharacteristic::PROPERTY_RELIABLE_WRITE, |
| 28 device::BluetoothLocalGattCharacteristic::Permissions(), |
| 29 service_.get()); |
| 30 EXPECT_LT(0u, read_characteristic_->GetIdentifier().size()); |
| 31 EXPECT_LT(0u, write_characteristic_->GetIdentifier().size()); |
| 25 CompleteGattSetup(); | 32 CompleteGattSetup(); |
| 26 } | 33 } |
| 27 | 34 |
| 28 protected: | 35 protected: |
| 29 base::WeakPtr<BluetoothLocalGattCharacteristic> characteristic_; | 36 base::WeakPtr<BluetoothLocalGattCharacteristic> read_characteristic_; |
| 37 base::WeakPtr<BluetoothLocalGattCharacteristic> write_characteristic_; |
| 30 }; | 38 }; |
| 31 | 39 |
| 32 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 40 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 33 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { | 41 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValue) { |
| 34 delegate_->value_to_write_ = 0x1337; | 42 delegate_->value_to_write_ = 0x1337; |
| 35 SimulateLocalGattCharacteristicValueReadRequest( | 43 SimulateLocalGattCharacteristicValueReadRequest( |
| 36 service_.get(), characteristic_.get(), | 44 service_.get(), read_characteristic_.get(), |
| 37 GetReadValueCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); | 45 GetReadValueCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); |
| 38 | 46 |
| 39 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); | 47 EXPECT_EQ(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 40 } | 48 } |
| 41 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 49 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 42 | 50 |
| 43 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 51 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 44 TEST_F(BluetoothLocalGattCharacteristicTest, WriteLocalCharacteristicValue) { | 52 TEST_F(BluetoothLocalGattCharacteristicTest, WriteLocalCharacteristicValue) { |
| 45 const uint64_t kValueToWrite = 0x7331ul; | 53 const uint64_t kValueToWrite = 0x7331ul; |
| 46 SimulateLocalGattCharacteristicValueWriteRequest( | 54 SimulateLocalGattCharacteristicValueWriteRequest( |
| 47 service_.get(), characteristic_.get(), GetValue(kValueToWrite), | 55 service_.get(), write_characteristic_.get(), GetValue(kValueToWrite), |
| 48 GetCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); | 56 GetCallback(Call::EXPECTED), GetCallback(Call::NOT_EXPECTED)); |
| 49 | 57 |
| 50 EXPECT_EQ(kValueToWrite, delegate_->last_written_value_); | 58 EXPECT_EQ(kValueToWrite, delegate_->last_written_value_); |
| 51 } | 59 } |
| 52 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 60 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 53 | 61 |
| 54 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 62 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 55 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValueFail) { | 63 TEST_F(BluetoothLocalGattCharacteristicTest, ReadLocalCharacteristicValueFail) { |
| 56 delegate_->value_to_write_ = 0x1337; | 64 delegate_->value_to_write_ = 0x1337; |
| 57 delegate_->should_fail_ = true; | 65 delegate_->should_fail_ = true; |
| 58 SimulateLocalGattCharacteristicValueReadRequest( | 66 SimulateLocalGattCharacteristicValueReadRequest( |
| 59 service_.get(), characteristic_.get(), | 67 service_.get(), read_characteristic_.get(), |
| 60 GetReadValueCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); | 68 GetReadValueCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 61 | 69 |
| 62 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); | 70 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 71 } |
| 72 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 73 |
| 74 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 75 TEST_F(BluetoothLocalGattCharacteristicTest, |
| 76 ReadLocalCharacteristicValueWrongPermission) { |
| 77 delegate_->value_to_write_ = 0x1337; |
| 78 SimulateLocalGattCharacteristicValueReadRequest( |
| 79 service_.get(), write_characteristic_.get(), |
| 80 GetReadValueCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 81 |
| 82 EXPECT_NE(delegate_->value_to_write_, GetInteger(last_read_value_)); |
| 63 } | 83 } |
| 64 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 84 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 65 | 85 |
| 66 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 86 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 67 TEST_F(BluetoothLocalGattCharacteristicTest, | 87 TEST_F(BluetoothLocalGattCharacteristicTest, |
| 68 WriteLocalCharacteristicValueFail) { | 88 WriteLocalCharacteristicValueFail) { |
| 69 const uint64_t kValueToWrite = 0x7331ul; | 89 const uint64_t kValueToWrite = 0x7331ul; |
| 70 delegate_->should_fail_ = true; | 90 delegate_->should_fail_ = true; |
| 71 SimulateLocalGattCharacteristicValueWriteRequest( | 91 SimulateLocalGattCharacteristicValueWriteRequest( |
| 72 service_.get(), characteristic_.get(), GetValue(kValueToWrite), | 92 service_.get(), write_characteristic_.get(), GetValue(kValueToWrite), |
| 73 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); | 93 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 74 | 94 |
| 75 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); | 95 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); |
| 96 } |
| 97 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 98 |
| 99 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 100 TEST_F(BluetoothLocalGattCharacteristicTest, |
| 101 WriteLocalCharacteristicValueWrongPermission) { |
| 102 const uint64_t kValueToWrite = 0x7331ul; |
| 103 SimulateLocalGattCharacteristicValueWriteRequest( |
| 104 service_.get(), read_characteristic_.get(), GetValue(kValueToWrite), |
| 105 GetCallback(Call::NOT_EXPECTED), GetCallback(Call::EXPECTED)); |
| 106 |
| 107 EXPECT_NE(kValueToWrite, delegate_->last_written_value_); |
| 76 } | 108 } |
| 77 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 109 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 78 | 110 |
| 79 } // namespace device | 111 } // namespace device |
| OLD | NEW |