fake_bluetooth_battery_client.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2020 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. #include "device/bluetooth/dbus/fake_bluetooth_battery_client.h"
  5. #include "base/containers/contains.h"
  6. #include "base/logging.h"
  7. #include "base/observer_list.h"
  8. #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
  9. #include "third_party/cros_system_api/dbus/service_constants.h"
  10. namespace bluez {
  11. FakeBluetoothBatteryClient::Properties::Properties(
  12. const PropertyChangedCallback& callback)
  13. : BluetoothBatteryClient::Properties(
  14. nullptr,
  15. bluetooth_battery::kBluetoothBatteryInterface,
  16. callback) {}
  17. FakeBluetoothBatteryClient::Properties::~Properties() = default;
  18. void FakeBluetoothBatteryClient::Properties::Get(
  19. dbus::PropertyBase* property,
  20. dbus::PropertySet::GetCallback callback) {
  21. DVLOG(1) << "Get " << property->name();
  22. std::move(callback).Run(false);
  23. }
  24. void FakeBluetoothBatteryClient::Properties::GetAll() {
  25. DVLOG(1) << "GetAll";
  26. }
  27. void FakeBluetoothBatteryClient::Properties::Set(
  28. dbus::PropertyBase* property,
  29. dbus::PropertySet::SetCallback callback) {
  30. DVLOG(1) << "Set " << property->name();
  31. std::move(callback).Run(false);
  32. }
  33. FakeBluetoothBatteryClient::FakeBluetoothBatteryClient() = default;
  34. FakeBluetoothBatteryClient::~FakeBluetoothBatteryClient() = default;
  35. void FakeBluetoothBatteryClient::CreateBattery(const dbus::ObjectPath& path,
  36. uint8_t percentage) {
  37. DCHECK(!base::Contains(battery_list_, path));
  38. auto properties = std::make_unique<Properties>(
  39. base::BindRepeating(&FakeBluetoothBatteryClient::OnPropertyChanged,
  40. base::Unretained(this), path));
  41. properties->percentage.ReplaceValue(percentage);
  42. properties->percentage.set_valid(true);
  43. properties_map_.insert(std::make_pair(path, std::move(properties)));
  44. battery_list_.push_back(path);
  45. for (auto& observer : observers_)
  46. observer.BatteryAdded(path);
  47. }
  48. void FakeBluetoothBatteryClient::ChangeBatteryPercentage(
  49. const dbus::ObjectPath& path,
  50. uint8_t percentage) {
  51. DCHECK(base::Contains(battery_list_, path));
  52. DCHECK(base::Contains(properties_map_, path));
  53. properties_map_[path]->percentage.ReplaceValue(percentage);
  54. for (auto& observer : observers_) {
  55. observer.BatteryPropertyChanged(path,
  56. bluetooth_battery::kPercentageProperty);
  57. }
  58. }
  59. void FakeBluetoothBatteryClient::RemoveBattery(const dbus::ObjectPath& path) {
  60. DCHECK(base::Contains(battery_list_, path));
  61. properties_map_.erase(path);
  62. battery_list_.erase(
  63. std::find(battery_list_.begin(), battery_list_.end(), path));
  64. for (auto& observer : observers_)
  65. observer.BatteryRemoved(path);
  66. }
  67. void FakeBluetoothBatteryClient::Init(
  68. dbus::Bus* bus,
  69. const std::string& bluetooth_service_name) {}
  70. void FakeBluetoothBatteryClient::AddObserver(Observer* observer) {
  71. observers_.AddObserver(observer);
  72. }
  73. void FakeBluetoothBatteryClient::RemoveObserver(Observer* observer) {
  74. observers_.RemoveObserver(observer);
  75. }
  76. FakeBluetoothBatteryClient::Properties*
  77. FakeBluetoothBatteryClient::GetProperties(const dbus::ObjectPath& object_path) {
  78. PropertiesMap::const_iterator iter = properties_map_.find(object_path);
  79. if (iter != properties_map_.end())
  80. return iter->second.get();
  81. return nullptr;
  82. }
  83. void FakeBluetoothBatteryClient::OnPropertyChanged(
  84. const dbus::ObjectPath& object_path,
  85. const std::string& property_name) {
  86. DVLOG(2) << "Fake Bluetooth battery property changed: " << object_path.value()
  87. << ": " << property_name;
  88. for (auto& observer : observers_)
  89. observer.BatteryPropertyChanged(object_path, property_name);
  90. }
  91. } // namespace bluez