OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 11 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
| 12 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| 13 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 const char kDeviceName[] = "TestDevice"; |
| 18 const char kDeviceAddress[] = "01:02:03:0A:10:A0"; |
| 19 } |
| 20 |
| 21 namespace device { |
| 22 class BluetoothRemoteGattCharacteristicWinTest : public testing::Test { |
| 23 public: |
| 24 BluetoothRemoteGattCharacteristicWinTest() |
| 25 : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| 26 bluetooth_task_runner_(new base::TestSimpleTaskRunner()), |
| 27 socket_thread_(BluetoothSocketThread::Get()), |
| 28 adapter_(new BluetoothAdapterWin(base::Bind( |
| 29 &BluetoothRemoteGattCharacteristicWinTest::AdapterInitCallback, |
| 30 base::Unretained(this)))) { |
| 31 adapter_->InitForTest(ui_task_runner_, bluetooth_task_runner_); |
| 32 |
| 33 // Create an empty device. |
| 34 BluetoothTaskManagerWin::DeviceState* device_state = |
| 35 new BluetoothTaskManagerWin::DeviceState(); |
| 36 device_state->name = kDeviceName; |
| 37 device_state->address = kDeviceAddress; |
| 38 device_.reset(new BluetoothDeviceWin(adapter_.get(), *device_state, |
| 39 ui_task_runner_, socket_thread_, |
| 40 nullptr, net::NetLog::Source())); |
| 41 service_ui_task_runner_ = ui_task_runner_; |
| 42 bluetooth_task_manager_ = adapter_->GetWinBluetoothTaskManager(); |
| 43 |
| 44 // Create an empty service. |
| 45 base::FilePath service_path = |
| 46 base::FilePath(std::wstring(L"/bluetooth/test")); |
| 47 service_.reset(new BluetoothRemoteGattServiceWin( |
| 48 device_.get(), service_path, BluetoothUUID("1234"), 0, true, nullptr, |
| 49 service_ui_task_runner_)); |
| 50 FinishPendingTasks(); |
| 51 } |
| 52 |
| 53 void SetUp() { |
| 54 BTH_LE_GATT_CHARACTERISTIC* characteristic_info = |
| 55 new BTH_LE_GATT_CHARACTERISTIC[1]; |
| 56 characteristic_info[0].CharacteristicUuid.IsShortUuid = true; |
| 57 characteristic_info[0].CharacteristicUuid.Value.ShortUuid = 4321; |
| 58 characteristic_.reset(new BluetoothRemoteGattCharacteristicWin( |
| 59 service_.get(), characteristic_info, service_ui_task_runner_)); |
| 60 FinishPendingTasks(); |
| 61 } |
| 62 |
| 63 void TearDown() override { characteristic_.reset(nullptr); } |
| 64 |
| 65 void AdapterInitCallback() {} |
| 66 |
| 67 void FinishPendingTasks() { |
| 68 bluetooth_task_runner_->RunPendingTasks(); |
| 69 ui_task_runner_->RunPendingTasks(); |
| 70 } |
| 71 |
| 72 PBTH_LE_GATT_DESCRIPTOR CreateSystemDescriptorInfo( |
| 73 std::vector<uint16_t> uuids) { |
| 74 PBTH_LE_GATT_DESCRIPTOR descriptors_info = |
| 75 new BTH_LE_GATT_DESCRIPTOR[uuids.size()]; |
| 76 for (unsigned int i = 0; i < uuids.size(); i++) { |
| 77 descriptors_info[i].DescriptorUuid.IsShortUuid = true; |
| 78 descriptors_info[i].DescriptorUuid.Value.ShortUuid = uuids[i]; |
| 79 } |
| 80 return descriptors_info; |
| 81 } |
| 82 |
| 83 size_t NumberOfIncludedDescriptors() { |
| 84 return characteristic_->included_descriptor_objects_.size(); |
| 85 } |
| 86 |
| 87 void UpdateIncludedDescriptors(PBTH_LE_GATT_DESCRIPTOR sys_decriptors_info, |
| 88 uint16_t num) { |
| 89 characteristic_->GetIncludedDescriptorsCallback(sys_decriptors_info, num, |
| 90 S_OK); |
| 91 } |
| 92 |
| 93 bool IsThisDecriptorIncluded(std::string uuid_string_value) { |
| 94 BluetoothRemoteGattCharacteristicWin::GattDescriptorMap::iterator it = |
| 95 characteristic_->included_descriptor_objects_.begin(); |
| 96 for (; it != characteristic_->included_descriptor_objects_.end(); it++) { |
| 97 if (it->second->GetUUID().value() == uuid_string_value) |
| 98 break; |
| 99 } |
| 100 if (it != characteristic_->included_descriptor_objects_.end()) |
| 101 return true; |
| 102 return false; |
| 103 } |
| 104 |
| 105 protected: |
| 106 BluetoothTaskManagerWin* bluetooth_task_manager_; |
| 107 |
| 108 private: |
| 109 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; |
| 110 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; |
| 111 scoped_refptr<BluetoothSocketThread> socket_thread_; |
| 112 |
| 113 scoped_refptr<BluetoothAdapterWin> adapter_; |
| 114 scoped_ptr<BluetoothDeviceWin> device_; |
| 115 scoped_ptr<BluetoothRemoteGattServiceWin> service_; |
| 116 scoped_ptr<BluetoothRemoteGattCharacteristicWin> characteristic_; |
| 117 scoped_refptr<base::SequencedTaskRunner> service_ui_task_runner_; |
| 118 }; |
| 119 |
| 120 TEST_F(BluetoothRemoteGattCharacteristicWinTest, UpdateIncludedDescriptors) { |
| 121 EXPECT_EQ(NumberOfIncludedDescriptors(), 0); |
| 122 |
| 123 // Add number_of_included_desriptors to the Characteristic. |
| 124 uint16_t number_of_included_desriptors = 3; |
| 125 uint16_t uuids_start_from = 1000; |
| 126 std::vector<uint16_t> descriptor_uuids; |
| 127 for (uint16_t i = 0; i < number_of_included_desriptors; i++) |
| 128 descriptor_uuids.push_back(uuids_start_from + i); |
| 129 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> sys_decriptors_info( |
| 130 CreateSystemDescriptorInfo(descriptor_uuids)); |
| 131 UpdateIncludedDescriptors(sys_decriptors_info.get(), |
| 132 number_of_included_desriptors); |
| 133 FinishPendingTasks(); |
| 134 |
| 135 // Check Descriptor objects have been created. |
| 136 std::vector<std::string> descriptor_uuids_string_value; |
| 137 for (uint16_t i = 0; i < number_of_included_desriptors; i++) { |
| 138 std::string uuid_string_value = |
| 139 bluetooth_task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 140 sys_decriptors_info.get()[i].DescriptorUuid) |
| 141 .value(); |
| 142 descriptor_uuids_string_value.push_back(uuid_string_value); |
| 143 } |
| 144 EXPECT_EQ(NumberOfIncludedDescriptors(), number_of_included_desriptors); |
| 145 for (auto uuid_string_value : descriptor_uuids_string_value) { |
| 146 EXPECT_TRUE(IsThisDecriptorIncluded(uuid_string_value)); |
| 147 } |
| 148 |
| 149 // Update Descriptors without changing. |
| 150 UpdateIncludedDescriptors(sys_decriptors_info.get(), |
| 151 number_of_included_desriptors); |
| 152 FinishPendingTasks(); |
| 153 |
| 154 // Check the included Descriptors have not been changed. |
| 155 EXPECT_EQ(NumberOfIncludedDescriptors(), number_of_included_desriptors); |
| 156 for (auto uuid_string_value : descriptor_uuids_string_value) { |
| 157 EXPECT_TRUE(IsThisDecriptorIncluded(uuid_string_value)); |
| 158 } |
| 159 |
| 160 // Remove a Descriptor. |
| 161 descriptor_uuids.erase(descriptor_uuids.begin()); |
| 162 // Add a new Descriptor. |
| 163 descriptor_uuids.push_back(descriptor_uuids.back() + 1); |
| 164 sys_decriptors_info.reset(CreateSystemDescriptorInfo(descriptor_uuids)); |
| 165 UpdateIncludedDescriptors(sys_decriptors_info.get(), |
| 166 number_of_included_desriptors); |
| 167 FinishPendingTasks(); |
| 168 |
| 169 // Check the removed Descriptor's object has been removed. |
| 170 EXPECT_FALSE(IsThisDecriptorIncluded(descriptor_uuids_string_value[0])); |
| 171 |
| 172 // Check the new and the non removed Descriptors are included. |
| 173 descriptor_uuids_string_value.erase(descriptor_uuids_string_value.begin()); |
| 174 std::string uuid_string_value = |
| 175 bluetooth_task_manager_ |
| 176 ->BluetoothLowEnergyUuidToBluetoothUuid( |
| 177 sys_decriptors_info.get()[number_of_included_desriptors - 1] |
| 178 .DescriptorUuid) |
| 179 .value(); |
| 180 descriptor_uuids_string_value.push_back(uuid_string_value); |
| 181 EXPECT_EQ(NumberOfIncludedDescriptors(), number_of_included_desriptors); |
| 182 for (auto uuid_string_value : descriptor_uuids_string_value) { |
| 183 EXPECT_TRUE(IsThisDecriptorIncluded(uuid_string_value)); |
| 184 } |
| 185 } |
| 186 |
| 187 } // namespace device |
OLD | NEW |