OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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_remote_gatt_service_win.h" |
| 11 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 const char kDeviceName[] = "TestDevice"; |
| 16 const char kDeviceAddress[] = "01:02:03:0A:10:A0"; |
| 17 } |
| 18 |
| 19 namespace device { |
| 20 class BluetoothRemoteGattServiceWinTest : public testing::Test { |
| 21 public: |
| 22 BluetoothRemoteGattServiceWinTest() |
| 23 : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| 24 bluetooth_task_runner_(new base::TestSimpleTaskRunner()), |
| 25 socket_thread_(BluetoothSocketThread::Get()), |
| 26 adapter_(new BluetoothAdapterWin( |
| 27 base::Bind(&BluetoothRemoteGattServiceWinTest::AdapterInitCallback, |
| 28 base::Unretained(this)))) { |
| 29 adapter_->InitForTest(ui_task_runner_, bluetooth_task_runner_); |
| 30 |
| 31 // Create an empty device. |
| 32 BluetoothTaskManagerWin::DeviceState* device_state = |
| 33 new BluetoothTaskManagerWin::DeviceState(); |
| 34 device_state->name = kDeviceName; |
| 35 device_state->address = kDeviceAddress; |
| 36 device_.reset(new BluetoothDeviceWin(adapter_.get(), *device_state, |
| 37 ui_task_runner_, socket_thread_, |
| 38 nullptr, net::NetLog::Source())); |
| 39 service_ui_task_runner_ = ui_task_runner_; |
| 40 bluetooth_task_manager_ = adapter_->GetWinBluetoothTaskManager(); |
| 41 } |
| 42 |
| 43 void SetUp() override { |
| 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 TearDown() override { service_.reset(nullptr); } |
| 54 |
| 55 void AdapterInitCallback() {} |
| 56 |
| 57 void FinishPendingTasks() { |
| 58 bluetooth_task_runner_->RunPendingTasks(); |
| 59 ui_task_runner_->RunPendingTasks(); |
| 60 } |
| 61 |
| 62 PBTH_LE_GATT_CHARACTERISTIC CreateSystemGattCharacteristicInfo( |
| 63 std::vector<uint16_t> uuids) { |
| 64 PBTH_LE_GATT_CHARACTERISTIC characteristics = |
| 65 new BTH_LE_GATT_CHARACTERISTIC[uuids.size()]; |
| 66 for (uint16_t i = 0; i < uuids.size(); i++) { |
| 67 characteristics[i].CharacteristicUuid.IsShortUuid = true; |
| 68 characteristics[i].CharacteristicUuid.Value.ShortUuid = uuids[i]; |
| 69 } |
| 70 return characteristics; |
| 71 } |
| 72 |
| 73 PBTH_LE_GATT_SERVICE CreateSystemGattServiceInfo( |
| 74 std::vector<uint16_t> uuids) { |
| 75 PBTH_LE_GATT_SERVICE services = new BTH_LE_GATT_SERVICE[uuids.size()]; |
| 76 for (uint16_t i = 0; i < uuids.size(); i++) { |
| 77 services[i].ServiceUuid.IsShortUuid = true; |
| 78 services[i].ServiceUuid.Value.ShortUuid = uuids[i]; |
| 79 } |
| 80 return services; |
| 81 } |
| 82 |
| 83 void UpdateIncludedCharacteristics( |
| 84 PBTH_LE_GATT_CHARACTERISTIC characteristics, |
| 85 uint16_t num) { |
| 86 service_->GetIncludedCharacteristicsCallback(characteristics, num, S_OK); |
| 87 } |
| 88 |
| 89 void UpdateIncludedServices(PBTH_LE_GATT_SERVICE services, uint16_t num) { |
| 90 service_->GetIncludedServicesCallback(services, num, S_OK); |
| 91 } |
| 92 |
| 93 size_t NumberOfIncludedCharacteristics() { |
| 94 return service_->included_characteristic_objects_.size(); |
| 95 } |
| 96 |
| 97 size_t NumberOfIncludedServices() { |
| 98 return service_->included_service_objects_.size(); |
| 99 } |
| 100 |
| 101 bool IsThisCharacteristicIncluded(std::string uuid_string_value) { |
| 102 BluetoothRemoteGattServiceWin::GattCharacteristicsMap::iterator it = |
| 103 service_->included_characteristic_objects_.begin(); |
| 104 for (; it != service_->included_characteristic_objects_.end(); it++) { |
| 105 if (it->second->GetUUID().value() == uuid_string_value) |
| 106 break; |
| 107 } |
| 108 if (it != service_->included_characteristic_objects_.end()) |
| 109 return true; |
| 110 return false; |
| 111 } |
| 112 |
| 113 bool IsThisServiceIncluded(std::string uuid_string_value) { |
| 114 BluetoothRemoteGattServiceWin::GattServicesMap::iterator it = |
| 115 service_->included_service_objects_.begin(); |
| 116 for (; it != service_->included_service_objects_.end(); it++) { |
| 117 if (it->second->GetUUID().value() == uuid_string_value) |
| 118 break; |
| 119 } |
| 120 if (it != service_->included_service_objects_.end()) |
| 121 return true; |
| 122 return false; |
| 123 } |
| 124 |
| 125 protected: |
| 126 BluetoothTaskManagerWin* bluetooth_task_manager_; |
| 127 |
| 128 private: |
| 129 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; |
| 130 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; |
| 131 scoped_refptr<BluetoothSocketThread> socket_thread_; |
| 132 |
| 133 scoped_refptr<BluetoothAdapterWin> adapter_; |
| 134 scoped_ptr<BluetoothDeviceWin> device_; |
| 135 scoped_ptr<BluetoothRemoteGattServiceWin> service_; |
| 136 scoped_refptr<base::SequencedTaskRunner> service_ui_task_runner_; |
| 137 }; |
| 138 |
| 139 TEST_F(BluetoothRemoteGattServiceWinTest, UpdateIncludedCharacteristics) { |
| 140 EXPECT_EQ(NumberOfIncludedCharacteristics(), 0); |
| 141 |
| 142 // Add number_of_characteristics to the service. |
| 143 uint16_t number_of_characteristics = 3; |
| 144 uint16_t uuid_start_from = 1000; |
| 145 std::vector<uint16_t> characteristic_uuids; |
| 146 for (uint16_t i = 0; i < number_of_characteristics; i++) { |
| 147 characteristic_uuids.push_back(uuid_start_from + i); |
| 148 } |
| 149 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristics( |
| 150 CreateSystemGattCharacteristicInfo(characteristic_uuids)); |
| 151 UpdateIncludedCharacteristics(characteristics.get(), |
| 152 number_of_characteristics); |
| 153 FinishPendingTasks(); |
| 154 |
| 155 // Check characteristic objects have been created. |
| 156 std::vector<std::string> characteristic_uuid_string_value; |
| 157 for (uint16_t i = 0; i < number_of_characteristics; i++) { |
| 158 std::string uuid_string_value = |
| 159 bluetooth_task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 160 characteristics.get()[i].CharacteristicUuid) |
| 161 .value(); |
| 162 characteristic_uuid_string_value.push_back(uuid_string_value); |
| 163 } |
| 164 EXPECT_EQ(NumberOfIncludedCharacteristics(), number_of_characteristics); |
| 165 for (auto uuid_string_value : characteristic_uuid_string_value) { |
| 166 EXPECT_TRUE(IsThisCharacteristicIncluded(uuid_string_value)); |
| 167 } |
| 168 |
| 169 // Update characteristics without changing. |
| 170 UpdateIncludedCharacteristics(characteristics.get(), |
| 171 number_of_characteristics); |
| 172 FinishPendingTasks(); |
| 173 |
| 174 // Check the Characteristics have not been changed. |
| 175 EXPECT_EQ(NumberOfIncludedCharacteristics(), number_of_characteristics); |
| 176 for (auto uuid_string_value : characteristic_uuid_string_value) { |
| 177 EXPECT_TRUE(IsThisCharacteristicIncluded(uuid_string_value)); |
| 178 } |
| 179 |
| 180 // Remove a Characteristic. |
| 181 characteristic_uuids.erase(characteristic_uuids.begin()); |
| 182 // Add a new Characteristic. |
| 183 characteristic_uuids.push_back(characteristic_uuids.back() + 1); |
| 184 characteristics.reset( |
| 185 CreateSystemGattCharacteristicInfo(characteristic_uuids)); |
| 186 UpdateIncludedCharacteristics(characteristics.get(), |
| 187 number_of_characteristics); |
| 188 FinishPendingTasks(); |
| 189 |
| 190 // Check the removed characteristic object has been removed. |
| 191 EXPECT_FALSE( |
| 192 IsThisCharacteristicIncluded(characteristic_uuid_string_value[0])); |
| 193 |
| 194 // Check the new and the non removed Characteristic objects are there. |
| 195 characteristic_uuid_string_value.erase( |
| 196 characteristic_uuid_string_value.begin()); |
| 197 std::string uuid_value = |
| 198 bluetooth_task_manager_ |
| 199 ->BluetoothLowEnergyUuidToBluetoothUuid( |
| 200 characteristics.get()[number_of_characteristics - 1] |
| 201 .CharacteristicUuid) |
| 202 .value(); |
| 203 characteristic_uuid_string_value.push_back(uuid_value); |
| 204 EXPECT_EQ(NumberOfIncludedCharacteristics(), number_of_characteristics); |
| 205 for (auto uuid_string_value : characteristic_uuid_string_value) { |
| 206 EXPECT_TRUE(IsThisCharacteristicIncluded(uuid_string_value)); |
| 207 } |
| 208 } |
| 209 |
| 210 TEST_F(BluetoothRemoteGattServiceWinTest, UpdateIncludedServices) { |
| 211 EXPECT_EQ(NumberOfIncludedServices(), 0); |
| 212 |
| 213 // Add number_of_services to the service. |
| 214 uint16_t number_of_services = 3; |
| 215 uint16_t uuid_start_from = 1000; |
| 216 std::vector<uint16_t> service_uuids; |
| 217 for (uint16_t i = 0; i < number_of_services; i++) { |
| 218 service_uuids.push_back(uuid_start_from + i); |
| 219 } |
| 220 scoped_ptr<BTH_LE_GATT_SERVICE> services( |
| 221 CreateSystemGattServiceInfo(service_uuids)); |
| 222 UpdateIncludedServices(services.get(), number_of_services); |
| 223 FinishPendingTasks(); |
| 224 |
| 225 // Check the service objects have been created. |
| 226 std::vector<std::string> service_uuid_string_value; |
| 227 for (uint16_t i = 0; i < number_of_services; i++) { |
| 228 std::string uuid_value = |
| 229 bluetooth_task_manager_->BluetoothLowEnergyUuidToBluetoothUuid( |
| 230 services.get()[i].ServiceUuid) |
| 231 .value(); |
| 232 service_uuid_string_value.push_back(uuid_value); |
| 233 } |
| 234 EXPECT_EQ(NumberOfIncludedServices(), number_of_services); |
| 235 for (uint16_t i = 0; i < number_of_services; i++) { |
| 236 EXPECT_TRUE(IsThisServiceIncluded(service_uuid_string_value[i])); |
| 237 } |
| 238 |
| 239 // Update included services without changing. |
| 240 UpdateIncludedServices(services.get(), number_of_services); |
| 241 FinishPendingTasks(); |
| 242 |
| 243 // Check the included services haven't been changed. |
| 244 EXPECT_EQ(NumberOfIncludedServices(), number_of_services); |
| 245 for (uint16_t i = 0; i < number_of_services; i++) { |
| 246 EXPECT_TRUE(IsThisServiceIncluded(service_uuid_string_value[i])); |
| 247 } |
| 248 |
| 249 // Remove a service. |
| 250 service_uuids.erase(service_uuids.begin()); |
| 251 // Add a new service. |
| 252 service_uuids.push_back(service_uuids.back() + 1); |
| 253 services.reset(CreateSystemGattServiceInfo(service_uuids)); |
| 254 UpdateIncludedServices(services.get(), number_of_services); |
| 255 FinishPendingTasks(); |
| 256 |
| 257 // Check the removed service's object has been removed. |
| 258 EXPECT_FALSE(IsThisServiceIncluded(service_uuid_string_value[0])); |
| 259 |
| 260 // Check the new service and the non removed services object are there. |
| 261 service_uuid_string_value.erase(service_uuid_string_value.begin()); |
| 262 std::string uuid_value = |
| 263 bluetooth_task_manager_ |
| 264 ->BluetoothLowEnergyUuidToBluetoothUuid( |
| 265 services.get()[number_of_services - 1].ServiceUuid) |
| 266 .value(); |
| 267 service_uuid_string_value.push_back(uuid_value); |
| 268 EXPECT_EQ(NumberOfIncludedServices(), number_of_services); |
| 269 for (uint16_t i = 0; i < number_of_services; i++) { |
| 270 EXPECT_TRUE(IsThisServiceIncluded(service_uuid_string_value[i])); |
| 271 } |
| 272 } |
| 273 |
| 274 } // namespace device |
OLD | NEW |