File: bluetooth_test_win.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (253 lines) | stat: -rw-r--r-- 11,428 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 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_BLUETOOTH_TEST_WIN_H_
#define DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_WIN_H_

#include <Windows.Devices.Enumeration.h>

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_pending_task.h"
#include "base/test/test_simple_task_runner.h"
#include "base/win/scoped_winrt_initializer.h"
#include "device/bluetooth/bluetooth_classic_win_fake.h"
#include "device/bluetooth/bluetooth_task_manager_win.h"
#include "device/bluetooth/test/bluetooth_test.h"

namespace device {

// Windows implementation of BluetoothTestBase.
class BluetoothTestWin : public BluetoothTestBase {
 public:
  BluetoothTestWin();
  ~BluetoothTestWin() override;

  // BluetoothTestBase overrides
  bool PlatformSupportsLowEnergy() override;
  void InitWithDefaultAdapter() override;
  void InitWithoutDefaultAdapter() override;
  void InitWithFakeAdapter() override;
  bool DenyPermission() override;
  void StartLowEnergyDiscoverySession() override;
  BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override;
  std::optional<BluetoothUUID> GetTargetGattService(
      BluetoothDevice* device) override;
  void SimulateGattConnection(BluetoothDevice* device) override;
  void SimulateStatusChangeToDisconnect(BluetoothDevice* device) override;
  void SimulateGattServicesDiscovered(
      BluetoothDevice* device,
      const std::vector<std::string>& uuids,
      const std::vector<std::string>& blocked_uuids = {}) override;
  void SimulateGattServiceRemoved(BluetoothRemoteGattService* service) override;
  void SimulateGattCharacteristic(BluetoothRemoteGattService* service,
                                  const std::string& uuid,
                                  int properties) override;
  void SimulateGattCharacteristicRemoved(
      BluetoothRemoteGattService* service,
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattCharacteristicRead(
      BluetoothRemoteGattCharacteristic* characteristic,
      const std::vector<uint8_t>& value) override;
  void SimulateGattCharacteristicReadError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattCharacteristicWrite(
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattCharacteristicWriteError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void DeleteDevice(BluetoothDevice* device) override;
  void SimulateGattDescriptor(BluetoothRemoteGattCharacteristic* characteristic,
                              const std::string& uuid) override;
  void SimulateGattNotifySessionStarted(
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattNotifySessionStartError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattCharacteristicChanged(
      BluetoothRemoteGattCharacteristic* characteristic,
      const std::vector<uint8_t>& value) override;

 private:
  scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
  scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_;

  void AdapterInitCallback();

  void FinishPendingTasks();
};

// Defines common test fixture name. Use TEST_F(BluetoothTest, YourTestName).
typedef BluetoothTestWin BluetoothTest;

struct BluetoothTestWinrtParam {
  // The feature state of |kNewBLEGattSessionHandling|.
  bool new_gatt_session_handling_enabled;
  // The feature state of |kUncachedGattDiscoveryForGattConnection|.
  bool uncached_gatt_discovery_for_gatt_connection;

  friend std::ostream& operator<<(std::ostream& os,
                                  const BluetoothTestWinrtParam& p) {
    return os << "{new_gatt_session_handling_enabled="
              << p.new_gatt_session_handling_enabled << "}"
              << "{uncached_gatt_discovery_for_gatt_connection="
              << p.uncached_gatt_discovery_for_gatt_connection << "}";
  }
};

constexpr BluetoothTestWinrtParam kBluetoothTestWinrtParam[] = {
    {/*new_gatt_session_handling_enabled=*/true,
     /*uncached_gatt_discovery_for_gatt_connection=*/true},
    {/*new_gatt_session_handling_enabled=*/true,
     /*uncached_gatt_discovery_for_gatt_connection=*/false},
    {/*new_gatt_session_handling_enabled=*/false,
     /*uncached_gatt_discovery_for_gatt_connection=*/true},
    {/*new_gatt_session_handling_enabled=*/false,
     /*uncached_gatt_discovery_for_gatt_connection=*/false},
};

// This test suite represents tests that are parameterized on Windows. This
// requires declaring tests in the following way:
//
// TEST_P(BluetoothTestWinrt, YourTestName).
//
// Test suites inheriting from this class should be instantiated as
//
// INSTANTIATE_TEST_SUITE_P(
//     All, FooTestSuiteWinrt,
//     ::testing::ValuesIn(kBluetoothTestWinrtParam>));
class BluetoothTestWinrt
    : public BluetoothTestWin,
      public ::testing::WithParamInterface<BluetoothTestWinrtParam> {
 public:
  BluetoothTestWinrt();

  BluetoothTestWinrt(const BluetoothTestWinrt&) = delete;
  BluetoothTestWinrt& operator=(const BluetoothTestWinrt&) = delete;

  ~BluetoothTestWinrt() override;

  bool UsesNewGattSessionHandling() const;
  bool UncachedGattDiscoveryForGattConnection() const;

  // Simulate a fake adapter whose power status cannot be
  // controlled because of a Windows Privacy setting.
  void InitFakeAdapterWithRadioAccessDenied();
  void SimulateSpuriousRadioStateChangedEvent();

  // BluetoothTestBase:
  bool PlatformSupportsLowEnergy() override;
  void InitWithDefaultAdapter() override;
  void InitWithoutDefaultAdapter() override;
  void InitWithFakeAdapter() override;
  void InitFakeAdapterWithoutRadio() override;
  void SimulateAdapterPowerFailure() override;
  void SimulateAdapterPoweredOn() override;
  void SimulateAdapterPoweredOff() override;
  BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override;
  void SimulateLowEnergyDiscoveryFailure() override;
  void SimulateDevicePaired(BluetoothDevice* device, bool is_paired) override;
  void SimulatePairingPinCode(BluetoothDevice* device,
                              std::string pin_code) override;
  // Currently only Win derived class has this function for create ConfirmOnly /
  // DisplayPin tests.  If in future we find that other platform need to test
  // for pairing_kind we should promote this function as virtual
  void SimulateConfirmOnly(BluetoothDevice* device);
  void SimulateDisplayPin(BluetoothDevice* device,
                          std::string_view display_pin);
  void SimulateAdvertisementStarted(
      BluetoothAdvertisement* advertisement) override;
  void SimulateAdvertisementStopped(
      BluetoothAdvertisement* advertisement) override;
  void SimulateAdvertisementError(
      BluetoothAdvertisement* advertisement,
      BluetoothAdvertisement::ErrorCode error_code) override;
  void SimulateGattConnection(BluetoothDevice* device) override;
  void SimulateGattConnectionError(
      BluetoothDevice* device,
      BluetoothDevice::ConnectErrorCode error_code) override;
  void SimulateGattDisconnection(BluetoothDevice* device) override;
  void SimulateDeviceBreaksConnection(BluetoothDevice* device) override;
  void SimulateGattNameChange(BluetoothDevice* device,
                              const std::string& new_name) override;
  void SimulateStatusChangeToDisconnect(BluetoothDevice* device) override;
  void SimulateGattServicesDiscovered(
      BluetoothDevice* device,
      const std::vector<std::string>& uuids,
      const std::vector<std::string>& blocked_uuids = {}) override;
  void SimulateGattServicesChanged(BluetoothDevice* device) override;
  void SimulateGattServiceRemoved(BluetoothRemoteGattService* service) override;
  void SimulateGattServicesDiscoveryError(BluetoothDevice* device) override;
  void SimulateGattCharacteristic(BluetoothRemoteGattService* service,
                                  const std::string& uuid,
                                  int properties) override;
  void SimulateGattNotifySessionStarted(
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattNotifySessionStartError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattNotifySessionStopped(
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattNotifySessionStopError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattCharacteristicChanged(
      BluetoothRemoteGattCharacteristic* characteristic,
      const std::vector<uint8_t>& value) override;
  void SimulateGattCharacteristicRead(
      BluetoothRemoteGattCharacteristic* characteristic,
      const std::vector<uint8_t>& value) override;
  void SimulateGattCharacteristicReadError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattCharacteristicWrite(
      BluetoothRemoteGattCharacteristic* characteristic) override;
  void SimulateGattCharacteristicWriteError(
      BluetoothRemoteGattCharacteristic* characteristic,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattDescriptor(BluetoothRemoteGattCharacteristic* characteristic,
                              const std::string& uuid) override;
  void SimulateGattDescriptorRead(BluetoothRemoteGattDescriptor* descriptor,
                                  const std::vector<uint8_t>& value) override;
  void SimulateGattDescriptorReadError(
      BluetoothRemoteGattDescriptor* descriptor,
      BluetoothGattService::GattErrorCode error_code) override;
  void SimulateGattDescriptorWrite(
      BluetoothRemoteGattDescriptor* descriptor) override;
  void SimulateGattDescriptorWriteError(
      BluetoothRemoteGattDescriptor* descriptor,
      BluetoothGattService::GattErrorCode error_code) override;
  void DeleteDevice(BluetoothDevice* device) override;

  void OnFakeBluetoothDeviceConnectGattAttempt();
  void OnFakeBluetoothDeviceGattServiceDiscoveryAttempt();
  void OnFakeBluetoothDeviceGattServiceDiscoveryAttemptWithCacheMode(
      ABI::Windows::Devices::Bluetooth::BluetoothCacheMode cache_mode);
  void OnFakeBluetoothGattDisconnect();
  void OnFakeBluetoothCharacteristicReadValue();
  void OnFakeBluetoothCharacteristicWriteValue(std::vector<uint8_t> value);
  void OnFakeBluetoothGattSetCharacteristicNotification(NotifyValueState state);
  void OnFakeBluetoothDescriptorReadValue();
  void OnFakeBluetoothDescriptorWriteValue(std::vector<uint8_t> value);

  int gatt_discovery_attempts_with_uncached_mode() const {
    return gatt_discovery_attempts_with_uncached_mode_;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  base::win::ScopedWinrtInitializer scoped_winrt_initializer_;
  int gatt_discovery_attempts_with_uncached_mode_ = 0;
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_WIN_H_