-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
mock_bluetooth_adapter.h
207 lines (182 loc) · 8.35 KB
/
mock_bluetooth_adapter.h
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
// Copyright 2012 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_MOCK_BLUETOOTH_ADAPTER_H_
#define DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_ADAPTER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "testing/gmock/include/gmock/gmock.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "device/bluetooth/bluetooth_low_energy_scan_filter.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace device {
class MockBluetoothAdapter : public BluetoothAdapter {
public:
class Observer : public BluetoothAdapter::Observer {
public:
Observer(scoped_refptr<BluetoothAdapter> adapter);
Observer(const Observer&) = delete;
Observer& operator=(const Observer&) = delete;
~Observer() override;
MOCK_METHOD2(AdapterPresentChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(AdapterPoweredChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(AdapterDiscoveringChanged, void(BluetoothAdapter*, bool));
MOCK_METHOD2(DeviceAdded, void(BluetoothAdapter*, BluetoothDevice*));
MOCK_METHOD2(DeviceChanged, void(BluetoothAdapter*, BluetoothDevice*));
MOCK_METHOD2(DeviceRemoved, void(BluetoothAdapter*, BluetoothDevice*));
MOCK_METHOD3(GattCharacteristicValueChanged,
void(BluetoothAdapter*,
BluetoothRemoteGattCharacteristic*,
const std::vector<uint8_t>&));
private:
const scoped_refptr<BluetoothAdapter> adapter_;
};
MockBluetoothAdapter();
bool IsInitialized() const override { return true; }
void Initialize(base::OnceClosure callback) override;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
void Shutdown() override;
#endif
MOCK_METHOD1(AddObserver, void(BluetoothAdapter::Observer*));
MOCK_METHOD1(RemoveObserver, void(BluetoothAdapter::Observer*));
MOCK_CONST_METHOD0(GetAddress, std::string());
MOCK_CONST_METHOD0(GetName, std::string());
MOCK_METHOD3(SetName,
void(const std::string& name,
base::OnceClosure callback,
ErrorCallback error_callback));
MOCK_CONST_METHOD0(IsPresent, bool());
MOCK_CONST_METHOD0(IsPowered, bool());
MOCK_CONST_METHOD0(CanPower, bool());
MOCK_METHOD3(SetPowered,
void(bool powered,
base::OnceClosure callback,
ErrorCallback error_callback));
MOCK_CONST_METHOD0(IsDiscoverable, bool());
MOCK_METHOD3(SetDiscoverable,
void(bool discoverable,
base::OnceClosure callback,
ErrorCallback error_callback));
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
MOCK_CONST_METHOD0(GetDiscoverableTimeout, base::TimeDelta());
#endif
MOCK_CONST_METHOD0(IsDiscovering, bool());
MOCK_METHOD2(
StartScanWithFilter_,
void(const BluetoothDiscoveryFilter*,
base::OnceCallback<void(/*is_error=*/bool,
UMABluetoothDiscoverySessionOutcome)>&
callback));
MOCK_METHOD2(
UpdateFilter_,
void(const BluetoothDiscoveryFilter*,
base::OnceCallback<void(/*is_error=*/bool,
UMABluetoothDiscoverySessionOutcome)>&
callback));
MOCK_METHOD1(StopScan, void(DiscoverySessionResultCallback callback));
MOCK_METHOD3(SetDiscoveryFilterRaw,
void(const BluetoothDiscoveryFilter*,
const base::RepeatingClosure& callback,
DiscoverySessionErrorCallback& error_callback));
MOCK_CONST_METHOD0(GetDevices, BluetoothAdapter::ConstDeviceList());
MOCK_METHOD1(GetDevice, BluetoothDevice*(const std::string& address));
MOCK_CONST_METHOD1(GetDevice,
const BluetoothDevice*(const std::string& address));
MOCK_CONST_METHOD0(GetUUIDs, UUIDList());
MOCK_METHOD2(AddPairingDelegate,
void(BluetoothDevice::PairingDelegate* pairing_delegate,
enum PairingDelegatePriority priority));
MOCK_METHOD1(RemovePairingDelegate,
void(BluetoothDevice::PairingDelegate* pairing_delegate));
MOCK_METHOD0(DefaultPairingDelegate, BluetoothDevice::PairingDelegate*());
MOCK_METHOD4(CreateRfcommService,
void(const BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback));
MOCK_METHOD4(CreateL2capService,
void(const BluetoothUUID& uuid,
const ServiceOptions& options,
CreateServiceCallback callback,
CreateServiceErrorCallback error_callback));
MOCK_CONST_METHOD1(GetGattService,
BluetoothLocalGattService*(const std::string& identifier));
#if BUILDFLAG(IS_CHROMEOS)
MOCK_METHOD3(SetServiceAllowList,
void(const UUIDList& uuids,
base::OnceClosure callback,
ErrorCallback error_callback));
MOCK_METHOD0(GetLowEnergyScanSessionHardwareOffloadingStatus,
LowEnergyScanSessionHardwareOffloadingStatus());
MOCK_METHOD2(
StartLowEnergyScanSession,
std::unique_ptr<BluetoothLowEnergyScanSession>(
std::unique_ptr<BluetoothLowEnergyScanFilter> filter,
base::WeakPtr<BluetoothLowEnergyScanSession::Delegate> delegate));
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS_ASH)
MOCK_METHOD0(SetStandardChromeOSAdapterName, void());
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
MOCK_METHOD4(
ConnectDevice,
void(const std::string& address,
const absl::optional<BluetoothDevice::AddressType>& address_type,
ConnectDeviceCallback callback,
ConnectDeviceErrorCallback error_callback));
#endif
// BluetoothAdapter is supposed to manage the lifetime of BluetoothDevices.
// This method takes ownership of the MockBluetoothDevice. This is only for
// convenience as far testing is concerned and it's possible to write test
// cases without using these functions.
void AddMockDevice(std::unique_ptr<MockBluetoothDevice> mock_device);
std::unique_ptr<MockBluetoothDevice> RemoveMockDevice(
const std::string& address);
BluetoothAdapter::ConstDeviceList GetConstMockDevices();
BluetoothAdapter::DeviceList GetMockDevices();
// The observers are maintained by the default behavior of AddObserver() and
// RemoveObserver(). Test fakes can use this function to notify the observers
// about events.
base::ObserverList<device::BluetoothAdapter::Observer>::Unchecked&
GetObservers() {
return observers_;
}
protected:
base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
bool SetPoweredImpl(bool powered) override;
void StartScanWithFilter(
std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
DiscoverySessionResultCallback callback) override;
void UpdateFilter(std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
DiscoverySessionResultCallback callback) override;
void RegisterAdvertisement(
std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
CreateAdvertisementCallback callback,
AdvertisementErrorCallback error_callback) override;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
void SetAdvertisingInterval(
const base::TimeDelta& min,
const base::TimeDelta& max,
base::OnceClosure callback,
AdvertisementErrorCallback error_callback) override;
void ResetAdvertising(base::OnceClosure callback,
AdvertisementErrorCallback error_callback) override;
#endif
~MockBluetoothAdapter() override;
MOCK_METHOD1(RemovePairingDelegateInternal,
void(BluetoothDevice::PairingDelegate* pairing_delegate));
std::vector<std::unique_ptr<MockBluetoothDevice>> mock_devices_;
// This must be the last field in the class so that weak pointers are
// invalidated first.
base::WeakPtrFactory<MockBluetoothAdapter> weak_ptr_factory_{this};
};
} // namespace device
#endif // DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_ADAPTER_H_