1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_BLUETOOTH_TEST_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_
#define DEVICE_BLUETOOTH_TEST_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_
#include <stdint.h>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_adapter.h"
namespace device {
// Test implementation of BluetoothAdapter::Observer counting method calls and
// caching last reported values.
class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer {
public:
explicit TestBluetoothAdapterObserver(
scoped_refptr<BluetoothAdapter> adapter);
TestBluetoothAdapterObserver(const TestBluetoothAdapterObserver&) = delete;
TestBluetoothAdapterObserver& operator=(const TestBluetoothAdapterObserver&) =
delete;
~TestBluetoothAdapterObserver() override;
// Reset counters and cached values.
void Reset();
void set_quit_closure(base::OnceClosure quit_closure);
// BluetoothAdapter::Observer
void AdapterPresentChanged(BluetoothAdapter* adapter, bool present) override;
void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override;
void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
bool discoverable) override;
void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
bool discovering) override;
void RegisterDiscoveringChangedWatcher(base::RepeatingClosure callback);
void RegisterDiscoveryChangeCompletedWatcher(base::RepeatingClosure callback);
void DeviceAdded(BluetoothAdapter* adapter, BluetoothDevice* device) override;
void DiscoveryChangeCompletedForTesting() override;
void DeviceChanged(BluetoothAdapter* adapter,
BluetoothDevice* device) override;
void DeviceAddressChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
const std::string& old_address) override;
void DeviceAdvertisementReceived(
const std::string& device_id,
const std::optional<std::string>& device_name,
const std::optional<std::string>& advertisement_name,
std::optional<int8_t> rssi,
std::optional<int8_t> tx_power,
std::optional<uint16_t> appearance,
const device::BluetoothDevice::UUIDList& advertised_uuids,
const device::BluetoothDevice::ServiceDataMap& service_data_map,
const device::BluetoothDevice::ManufacturerDataMap& manufacturer_data_map)
override;
#if BUILDFLAG(IS_CHROMEOS)
void DeviceBondedChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool new_bonded_status) override;
#endif
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
void DevicePairedChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool new_paired_status) override;
void DeviceMTUChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
uint16_t mtu) override;
void DeviceAdvertisementReceived(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
int16_t rssi,
const std::vector<uint8_t>& eir) override;
void DeviceConnectedStateChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device,
bool is_now_connected) override;
#endif
void DeviceRemoved(BluetoothAdapter* adapter,
BluetoothDevice* device) override;
void GattServiceAdded(BluetoothAdapter* adapter,
BluetoothDevice* device,
BluetoothRemoteGattService* service) override;
void GattServiceRemoved(BluetoothAdapter* adapter,
BluetoothDevice* device,
BluetoothRemoteGattService* service) override;
void GattServicesDiscovered(BluetoothAdapter* adapter,
BluetoothDevice* device) override;
void GattDiscoveryCompleteForService(
BluetoothAdapter* adapter,
BluetoothRemoteGattService* service) override;
void GattServiceChanged(BluetoothAdapter* adapter,
BluetoothRemoteGattService* service) override;
void GattCharacteristicAdded(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic) override;
void GattCharacteristicRemoved(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic) override;
void GattDescriptorAdded(BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor) override;
void GattDescriptorRemoved(
BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor) override;
void GattCharacteristicValueChanged(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic,
const std::vector<uint8_t>& value) override;
void GattDescriptorValueChanged(BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor,
const std::vector<uint8_t>& value) override;
#if BUILDFLAG(IS_CHROMEOS)
void LowEnergyScanSessionHardwareOffloadingStatusChanged(
BluetoothAdapter::LowEnergyScanSessionHardwareOffloadingStatus status)
override;
#endif // BUILDFLAG(IS_CHROMEOS)
// Adapter related:
int present_changed_count() const { return present_changed_count_; }
int powered_changed_count() const { return powered_changed_count_; }
int discoverable_changed_count() const { return discoverable_changed_count_; }
int discovering_changed_count() const { return discovering_changed_count_; }
bool last_present() const { return last_present_; }
bool last_powered() const { return last_powered_; }
bool last_discovering() const { return last_discovering_; }
// Device related:
int device_added_count() const { return device_added_count_; }
int device_changed_count() const { return device_changed_count_; }
int device_address_changed_count() const {
return device_address_changed_count_;
}
// Advertisement related:
int device_advertisement_raw_received_count() const {
return device_advertisement_raw_received_count_;
}
std::string last_device_address() const { return last_device_address_; }
const std::optional<std::string>& last_device_name() const {
return last_device_name_;
}
const std::optional<std::string>& last_advertisement_name() const {
return last_advertisement_name_;
}
const std::optional<int8_t>& last_rssi() const { return last_rssi_; }
const std::optional<int8_t>& last_tx_power() const { return last_tx_power_; }
const std::optional<uint16_t>& last_appearance() const {
return last_appearance_;
}
const device::BluetoothDevice::UUIDList& last_advertised_uuids() const {
return last_advertised_uuids_;
}
const device::BluetoothDevice::ServiceDataMap& last_service_data_map() const {
return last_service_data_map_;
}
const device::BluetoothDevice::ManufacturerDataMap&
last_manufacturer_data_map() const {
return last_manufacturer_data_map_;
}
#if BUILDFLAG(IS_CHROMEOS)
int device_bonded_changed_count() const {
return device_bonded_changed_count_;
}
bool device_new_bonded_status() const { return device_new_bonded_status_; }
#endif
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
int device_paired_changed_count() const {
return device_paired_changed_count_;
}
bool device_new_paired_status() const { return device_new_paired_status_; }
int device_mtu_changed_count() const { return device_mtu_changed_count_; }
uint16_t last_mtu_value() const { return device_mtu_; }
int device_advertisement_received_count() const {
return device_advertisement_received_count_;
}
const std::vector<uint8_t>& device_eir() const { return device_eir_; }
const std::vector<bool>& device_connected_state_changed_values() const {
return device_connected_state_changed_values_;
}
#endif
int device_removed_count() const { return device_removed_count_; }
BluetoothDevice* last_device() const { return last_device_; }
// GATT related:
int gatt_service_added_count() const { return gatt_service_added_count_; }
int gatt_service_removed_count() const { return gatt_service_removed_count_; }
int gatt_services_discovered_count() const {
return gatt_services_discovered_count_;
}
int gatt_service_changed_count() const { return gatt_service_changed_count_; }
int gatt_discovery_complete_count() const {
return gatt_discovery_complete_count_;
}
int gatt_characteristic_added_count() const {
return gatt_characteristic_added_count_;
}
int gatt_characteristic_removed_count() const {
return gatt_characteristic_removed_count_;
}
int gatt_characteristic_value_changed_count() const {
return gatt_characteristic_value_changed_count_;
}
int gatt_descriptor_added_count() const {
return gatt_descriptor_added_count_;
}
int gatt_descriptor_removed_count() const {
return gatt_descriptor_removed_count_;
}
int gatt_descriptor_value_changed_count() const {
return gatt_descriptor_value_changed_count_;
}
std::string last_gatt_service_id() const { return last_gatt_service_id_; }
BluetoothUUID last_gatt_service_uuid() const {
return last_gatt_service_uuid_;
}
std::string last_gatt_characteristic_id() const {
return last_gatt_characteristic_id_;
}
BluetoothUUID last_gatt_characteristic_uuid() const {
return last_gatt_characteristic_uuid_;
}
std::vector<uint8_t> last_changed_characteristic_value() const {
return last_changed_characteristic_value_;
}
std::vector<std::vector<uint8_t>>
previous_characteristic_value_changed_values() const {
return previous_characteristic_value_changed_values_;
}
std::string last_gatt_descriptor_id() const {
return last_gatt_descriptor_id_;
}
BluetoothUUID last_gatt_descriptor_uuid() const {
return last_gatt_descriptor_uuid_;
}
std::vector<uint8_t> last_changed_descriptor_value() const {
return last_changed_descriptor_value_;
}
#if BUILDFLAG(IS_CHROMEOS)
BluetoothAdapter::LowEnergyScanSessionHardwareOffloadingStatus
last_low_energy_scan_session_hardware_offloading_status() const {
return last_low_energy_scan_session_hardware_offloading_status_;
}
#endif // BUILDFLAG(IS_CHROMEOS)
private:
// Some tests use a message loop since background processing is simulated;
// break out of those loops.
void QuitMessageLoop();
scoped_refptr<BluetoothAdapter> adapter_;
// Adapter related:
int present_changed_count_;
int powered_changed_count_;
int discoverable_changed_count_;
int discovering_changed_count_;
bool last_present_;
bool last_powered_;
bool last_discovering_;
// Device related:
int device_added_count_;
int device_changed_count_;
int device_address_changed_count_;
// Advertisement related
int device_advertisement_raw_received_count_;
std::string last_device_address_;
std::optional<std::string> last_device_name_;
std::optional<std::string> last_advertisement_name_;
std::optional<int8_t> last_rssi_;
std::optional<int8_t> last_tx_power_;
std::optional<uint16_t> last_appearance_;
device::BluetoothDevice::UUIDList last_advertised_uuids_;
device::BluetoothDevice::ServiceDataMap last_service_data_map_;
device::BluetoothDevice::ManufacturerDataMap last_manufacturer_data_map_;
base::RepeatingClosure discovering_changed_callback_;
base::RepeatingClosure discovery_change_completed_callback_;
// RunLoop QuitClosure
base::OnceClosure quit_closure_;
#if BUILDFLAG(IS_CHROMEOS)
int device_bonded_changed_count_;
bool device_new_bonded_status_;
#endif
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
int device_paired_changed_count_;
bool device_new_paired_status_;
int device_mtu_changed_count_;
uint16_t device_mtu_;
int device_advertisement_received_count_;
std::vector<uint8_t> device_eir_;
std::vector<bool> device_connected_state_changed_values_;
#endif
int device_removed_count_;
raw_ptr<BluetoothDevice> last_device_;
// GATT related:
int gatt_service_added_count_;
int gatt_service_removed_count_;
int gatt_services_discovered_count_;
int gatt_service_changed_count_;
int gatt_discovery_complete_count_;
int gatt_characteristic_added_count_;
int gatt_characteristic_removed_count_;
int gatt_characteristic_value_changed_count_;
int gatt_descriptor_added_count_;
int gatt_descriptor_removed_count_;
int gatt_descriptor_value_changed_count_;
std::string last_gatt_service_id_;
BluetoothUUID last_gatt_service_uuid_;
std::string last_gatt_characteristic_id_;
BluetoothUUID last_gatt_characteristic_uuid_;
std::vector<uint8_t> last_changed_characteristic_value_;
std::vector<std::vector<uint8_t>>
previous_characteristic_value_changed_values_;
std::string last_gatt_descriptor_id_;
BluetoothUUID last_gatt_descriptor_uuid_;
std::vector<uint8_t> last_changed_descriptor_value_;
#if BUILDFLAG(IS_CHROMEOS)
BluetoothAdapter::LowEnergyScanSessionHardwareOffloadingStatus
last_low_energy_scan_session_hardware_offloading_status_ =
BluetoothAdapter::LowEnergyScanSessionHardwareOffloadingStatus::
kUndetermined;
#endif // BUILDFLAG(IS_CHROMEOS)
};
} // namespace device
#endif // DEVICE_BLUETOOTH_TEST_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_
|