| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 void ResponseSenderCallback(const std::string& expected_message, | 281 void ResponseSenderCallback(const std::string& expected_message, |
| 282 std::unique_ptr<dbus::Response> response) { | 282 std::unique_ptr<dbus::Response> response) { |
| 283 EXPECT_EQ(expected_message, response->ToString()); | 283 EXPECT_EQ(expected_message, response->ToString()); |
| 284 } | 284 } |
| 285 | 285 |
| 286 } // namespace | 286 } // namespace |
| 287 | 287 |
| 288 class BluetoothGattApplicationServiceProviderTest : public testing::Test { | 288 class BluetoothGattApplicationServiceProviderTest : public testing::Test { |
| 289 public: | 289 public: |
| 290 std::string CreateFakeService( | 290 std::string CreateFakeService( |
| 291 dbus::Bus* bus, | |
| 292 BluetoothGattApplicationServiceProviderImpl* app_provider, | 291 BluetoothGattApplicationServiceProviderImpl* app_provider, |
| 293 const std::string& service_path) { | 292 const std::string& service_path) { |
| 294 const std::string& full_service_path = | 293 const std::string& full_service_path = |
| 295 std::string(kAppObjectPath) + "/" + service_path; | 294 std::string(kAppObjectPath) + "/" + service_path; |
| 296 app_provider->service_providers_.push_back( | 295 app_provider->service_providers_.push_back( |
| 297 base::WrapUnique(new BluetoothGattServiceServiceProviderImpl( | 296 base::WrapUnique(new BluetoothGattServiceServiceProviderImpl( |
| 298 nullptr, dbus::ObjectPath(full_service_path), kFakeServiceUuid, | 297 nullptr, dbus::ObjectPath(full_service_path), kFakeServiceUuid, |
| 299 true, std::vector<dbus::ObjectPath>()))); | 298 true, std::vector<dbus::ObjectPath>()))); |
| 300 return full_service_path; | 299 return full_service_path; |
| 301 } | 300 } |
| 302 | 301 |
| 303 std::string CreateFakeCharacteristic( | 302 std::string CreateFakeCharacteristic( |
| 304 dbus::Bus* bus, | |
| 305 BluetoothGattApplicationServiceProviderImpl* app_provider, | 303 BluetoothGattApplicationServiceProviderImpl* app_provider, |
| 306 const std::string& characteristic_path, | 304 const std::string& characteristic_path, |
| 307 const std::string& service_path) { | 305 const std::string& service_path) { |
| 308 const std::string& full_characteristic_path = | 306 const std::string& full_characteristic_path = |
| 309 service_path + "/" + characteristic_path; | 307 service_path + "/" + characteristic_path; |
| 310 app_provider->characteristic_providers_.push_back( | 308 app_provider->characteristic_providers_.push_back( |
| 311 base::WrapUnique(new BluetoothGattCharacteristicServiceProviderImpl( | 309 base::WrapUnique(new BluetoothGattCharacteristicServiceProviderImpl( |
| 312 nullptr, dbus::ObjectPath(full_characteristic_path), nullptr, | 310 nullptr, dbus::ObjectPath(full_characteristic_path), nullptr, |
| 313 kFakeCharacteristicUuid, | 311 kFakeCharacteristicUuid, |
| 314 std::vector<std::string>({"read", "write"}), | 312 std::vector<std::string>({"read", "write"}), |
| 315 dbus::ObjectPath(service_path)))); | 313 dbus::ObjectPath(service_path)))); |
| 316 return full_characteristic_path; | 314 return full_characteristic_path; |
| 317 } | 315 } |
| 318 | 316 |
| 319 void CreateFakeDescriptor( | 317 void CreateFakeDescriptor( |
| 320 dbus::Bus* bus, | |
| 321 BluetoothGattApplicationServiceProviderImpl* app_provider, | 318 BluetoothGattApplicationServiceProviderImpl* app_provider, |
| 322 const std::string& descriptor_path, | 319 const std::string& descriptor_path, |
| 323 const std::string& characteristic_path) { | 320 const std::string& characteristic_path) { |
| 324 const std::string& full_descriptor_path = | 321 const std::string& full_descriptor_path = |
| 325 characteristic_path + "/" + descriptor_path; | 322 characteristic_path + "/" + descriptor_path; |
| 326 app_provider->descriptor_providers_.push_back( | 323 app_provider->descriptor_providers_.push_back( |
| 327 base::WrapUnique(new BluetoothGattDescriptorServiceProviderImpl( | 324 base::WrapUnique(new BluetoothGattDescriptorServiceProviderImpl( |
| 328 nullptr, dbus::ObjectPath(full_descriptor_path), nullptr, | 325 nullptr, dbus::ObjectPath(full_descriptor_path), nullptr, |
| 329 kFakeDescriptorUuid, std::vector<std::string>({"read", "write"}), | 326 kFakeDescriptorUuid, std::vector<std::string>({"read", "write"}), |
| 330 dbus::ObjectPath(characteristic_path)))); | 327 dbus::ObjectPath(characteristic_path)))); |
| 331 } | 328 } |
| 332 | 329 |
| 333 void CreateFakeAttributes( | 330 void CreateFakeAttributes( |
| 334 dbus::Bus* bus, | |
| 335 BluetoothGattApplicationServiceProviderImpl* app_provider) { | 331 BluetoothGattApplicationServiceProviderImpl* app_provider) { |
| 336 const std::string& kServicePath1 = | 332 const std::string& kServicePath1 = |
| 337 CreateFakeService(bus, app_provider, "service0"); | 333 CreateFakeService(app_provider, "service0"); |
| 338 const std::string& kServicePath2 = | 334 const std::string& kServicePath2 = |
| 339 CreateFakeService(bus, app_provider, "service1"); | 335 CreateFakeService(app_provider, "service1"); |
| 340 | 336 |
| 341 const std::string& kCharacteristicPath1 = CreateFakeCharacteristic( | 337 const std::string& kCharacteristicPath1 = CreateFakeCharacteristic( |
| 342 bus, app_provider, "characteristic0", kServicePath1); | 338 app_provider, "characteristic0", kServicePath1); |
| 343 const std::string& kCharacteristicPath2 = CreateFakeCharacteristic( | 339 const std::string& kCharacteristicPath2 = CreateFakeCharacteristic( |
| 344 bus, app_provider, "characteristic1", kServicePath1); | 340 app_provider, "characteristic1", kServicePath1); |
| 345 const std::string& kCharacteristicPath3 = CreateFakeCharacteristic( | 341 const std::string& kCharacteristicPath3 = CreateFakeCharacteristic( |
| 346 bus, app_provider, "characteristic0", kServicePath2); | 342 app_provider, "characteristic0", kServicePath2); |
| 347 | 343 |
| 348 CreateFakeDescriptor(bus, app_provider, "descriptor0", | 344 CreateFakeDescriptor(app_provider, "descriptor0", kCharacteristicPath1); |
| 349 kCharacteristicPath1); | 345 CreateFakeDescriptor(app_provider, "descriptor1", kCharacteristicPath2); |
| 350 CreateFakeDescriptor(bus, app_provider, "descriptor1", | 346 CreateFakeDescriptor(app_provider, "descriptor2", kCharacteristicPath3); |
| 351 kCharacteristicPath2); | 347 CreateFakeDescriptor(app_provider, "descriptor3", kCharacteristicPath1); |
| 352 CreateFakeDescriptor(bus, app_provider, "descriptor2", | |
| 353 kCharacteristicPath3); | |
| 354 CreateFakeDescriptor(bus, app_provider, "descriptor3", | |
| 355 kCharacteristicPath1); | |
| 356 } | 348 } |
| 357 }; | 349 }; |
| 358 | 350 |
| 359 TEST_F(BluetoothGattApplicationServiceProviderTest, GetManagedObjects) { | 351 TEST_F(BluetoothGattApplicationServiceProviderTest, GetManagedObjects) { |
| 360 std::unique_ptr<BluetoothGattApplicationServiceProviderImpl> app_provider = | 352 std::unique_ptr<BluetoothGattApplicationServiceProviderImpl> app_provider = |
| 361 base::WrapUnique(new BluetoothGattApplicationServiceProviderImpl( | 353 base::WrapUnique(new BluetoothGattApplicationServiceProviderImpl( |
| 362 dbus::ObjectPath(kAppObjectPath))); | 354 nullptr, dbus::ObjectPath(kAppObjectPath), |
| 363 CreateFakeAttributes(nullptr, app_provider.get()); | 355 std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>())); |
| 356 CreateFakeAttributes(app_provider.get()); |
| 364 | 357 |
| 365 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); | 358 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); |
| 366 // Not setting the serial causes a crash. | 359 // Not setting the serial causes a crash. |
| 367 method_call.SetSerial(123); | 360 method_call.SetSerial(123); |
| 368 app_provider->GetManagedObjects( | 361 app_provider->GetManagedObjects( |
| 369 &method_call, base::Bind(&ResponseSenderCallback, kExpectedMessage)); | 362 &method_call, base::Bind(&ResponseSenderCallback, kExpectedMessage)); |
| 370 } | 363 } |
| 371 | 364 |
| 365 TEST_F(BluetoothGattApplicationServiceProviderTest, SendValueChanged) { |
| 366 std::unique_ptr<BluetoothGattApplicationServiceProviderImpl> app_provider = |
| 367 base::WrapUnique(new BluetoothGattApplicationServiceProviderImpl( |
| 368 nullptr, dbus::ObjectPath(kAppObjectPath), |
| 369 std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>())); |
| 370 const std::string& kServicePath = |
| 371 CreateFakeService(app_provider.get(), "service0"); |
| 372 const std::string& kCharacteristicPath = CreateFakeCharacteristic( |
| 373 app_provider.get(), "characteristic0", kServicePath); |
| 374 |
| 375 std::vector<uint8_t> kNewValue = {0x13, 0x37, 0xba, 0xad, 0xf0}; |
| 376 app_provider->SendValueChanged(dbus::ObjectPath(kCharacteristicPath), |
| 377 kNewValue); |
| 378 // TODO(rkc): Write a test implementation of dbus::Bus and |
| 379 // dbus::ExportedObject so we can capture the actual signal that is sent and |
| 380 // verify its contents. |
| 381 } |
| 382 |
| 372 } // namespace bluez | 383 } // namespace bluez |
| OLD | NEW |