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 3 // found in the LICENSE file. 4 5 #ifndef DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_WIN_H_ 6 #define DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_WIN_H_ 7 8 #include "device/bluetooth/test/bluetooth_test.h" 9 10 #include <string> 11 #include <vector> 12 13 #include "base/macros.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/optional.h" 16 #include "base/test/scoped_feature_list.h" 17 #include "base/test/test_pending_task.h" 18 #include "base/test/test_simple_task_runner.h" 19 #include "base/win/scoped_winrt_initializer.h" 20 #include "device/bluetooth/bluetooth_classic_win_fake.h" 21 #include "device/bluetooth/bluetooth_low_energy_win_fake.h" 22 #include "device/bluetooth/bluetooth_task_manager_win.h" 23 24 namespace device { 25 26 // Windows implementation of BluetoothTestBase. 27 class BluetoothTestWin : public BluetoothTestBase, 28 public win::BluetoothLowEnergyWrapperFake::Observer { 29 public: 30 BluetoothTestWin(); 31 ~BluetoothTestWin() override; 32 33 // BluetoothTestBase overrides 34 bool PlatformSupportsLowEnergy() override; 35 void InitWithDefaultAdapter() override; 36 void InitWithoutDefaultAdapter() override; 37 void InitWithFakeAdapter() override; 38 bool DenyPermission() override; 39 void StartLowEnergyDiscoverySession() override; 40 BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override; 41 base::Optional<BluetoothUUID> GetTargetGattService( 42 BluetoothDevice* device) override; 43 void SimulateGattConnection(BluetoothDevice* device) override; 44 void SimulateStatusChangeToDisconnect(BluetoothDevice* device) override; 45 void SimulateGattServicesDiscovered( 46 BluetoothDevice* device, 47 const std::vector<std::string>& uuids, 48 const std::vector<std::string>& blocked_uuids = {}) override; 49 void SimulateGattServiceRemoved(BluetoothRemoteGattService* service) override; 50 void SimulateGattCharacteristic(BluetoothRemoteGattService* service, 51 const std::string& uuid, 52 int properties) override; 53 void SimulateGattCharacteristicRemoved( 54 BluetoothRemoteGattService* service, 55 BluetoothRemoteGattCharacteristic* characteristic) override; 56 void RememberCharacteristicForSubsequentAction( 57 BluetoothRemoteGattCharacteristic* characteristic) override; 58 void SimulateGattCharacteristicRead( 59 BluetoothRemoteGattCharacteristic* characteristic, 60 const std::vector<uint8_t>& value) override; 61 void SimulateGattCharacteristicReadError( 62 BluetoothRemoteGattCharacteristic* characteristic, 63 BluetoothRemoteGattService::GattErrorCode error_code) override; 64 void SimulateGattCharacteristicWrite( 65 BluetoothRemoteGattCharacteristic* characteristic) override; 66 void SimulateGattCharacteristicWriteError( 67 BluetoothRemoteGattCharacteristic* characteristic, 68 BluetoothRemoteGattService::GattErrorCode error_code) override; 69 void RememberDeviceForSubsequentAction(BluetoothDevice* device) override; 70 void DeleteDevice(BluetoothDevice* device) override; 71 void SimulateGattDescriptor(BluetoothRemoteGattCharacteristic* characteristic, 72 const std::string& uuid) override; 73 void SimulateGattNotifySessionStarted( 74 BluetoothRemoteGattCharacteristic* characteristic) override; 75 void SimulateGattNotifySessionStartError( 76 BluetoothRemoteGattCharacteristic* characteristic, 77 BluetoothRemoteGattService::GattErrorCode error_code) override; 78 void SimulateGattCharacteristicChanged( 79 BluetoothRemoteGattCharacteristic* characteristic, 80 const std::vector<uint8_t>& value) override; 81 82 // win::BluetoothLowEnergyWrapperFake::Observer overrides. 83 void OnReadGattCharacteristicValue() override; 84 void OnWriteGattCharacteristicValue( 85 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) override; 86 void OnStartCharacteristicNotification() override; 87 void OnWriteGattDescriptorValue(const std::vector<uint8_t>& value) override; 88 89 private: 90 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; 91 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; 92 93 win::BluetoothLowEnergyWrapperFake* fake_bt_le_wrapper_; 94 95 // This is used for retaining access to a single deleted device. 96 std::string remembered_device_address_; 97 98 void AdapterInitCallback(); 99 win::GattService* GetSimulatedService(win::BLEDevice* device, 100 BluetoothRemoteGattService* service); 101 win::GattCharacteristic* GetSimulatedCharacteristic( 102 BluetoothRemoteGattCharacteristic* characteristic); 103 104 // Run pending Bluetooth tasks until the first callback that the test fixture 105 // tracks is called. 106 void RunPendingTasksUntilCallback(); 107 void ForceRefreshDevice(); 108 void FinishPendingTasks(); 109 }; 110 111 // Defines common test fixture name. Use TEST_F(BluetoothTest, YourTestName). 112 typedef BluetoothTestWin BluetoothTest; 113 114 struct BluetoothTestWinrtParam { 115 // The feature state of |kNewBLEWinImplementation|. 116 bool new_ble_implementation_enabled; 117 // The feature state of |kNewBLEGattSessionHandling|. 118 bool new_gatt_session_handling_enabled; 119 120 friend std::ostream& operator<<(std::ostream& os, 121 const BluetoothTestWinrtParam& p) { 122 return os << "{new_ble_implementation_enabled=" 123 << p.new_ble_implementation_enabled 124 << ", new_gatt_session_handling_enabled=" 125 << p.new_gatt_session_handling_enabled << "}"; 126 } 127 }; 128 129 constexpr BluetoothTestWinrtParam kBluetoothTestWinrtParamAll[] = { 130 {false, false}, 131 {false, true}, 132 {true, false}, 133 {true, true}, 134 }; 135 136 constexpr BluetoothTestWinrtParam kBluetoothTestWinrtParamWinrtOnly[] = { 137 {true, false}, 138 {true, true}, 139 }; 140 141 constexpr BluetoothTestWinrtParam kBluetoothTestWinrtParamWin32Only[] = { 142 {false, false}, 143 {false, true}, 144 }; 145 146 // This test suite represents tests that should run with the new BLE 147 // implementation both enabled and disabled. This requires declaring tests 148 // in the following way: TEST_P(BluetoothTestWinrt, YourTestName). 149 // 150 // Test suites inheriting from this class should be instantiated as 151 // 152 // INSTANTIATE_TEST_SUITE_P( 153 // All, FooTestSuiteWinrt, 154 // ::testing::ValuesIn( 155 // <kBluetoothTestWinrtParamWin32Only | 156 // kBluetoothTestWinrtParamWinrtOnly | 157 // kBluetoothTestWinrtParamAll>)); 158 // 159 // depending on whether they should run only the old or new implementation or 160 // both. 161 class BluetoothTestWinrt 162 : public BluetoothTestWin, 163 public ::testing::WithParamInterface<BluetoothTestWinrtParam> { 164 public: 165 BluetoothTestWinrt(); 166 ~BluetoothTestWinrt() override; 167 168 bool UsesNewBleImplementation() const; 169 bool UsesNewGattSessionHandling() const; 170 171 // Simulate a fake adapter whose power status cannot be 172 // controlled because of a Windows Privacy setting. 173 void InitFakeAdapterWithRadioAccessDenied(); 174 void SimulateSpuriousRadioStateChangedEvent(); 175 176 // BluetoothTestBase: 177 bool PlatformSupportsLowEnergy() override; 178 void InitWithDefaultAdapter() override; 179 void InitWithoutDefaultAdapter() override; 180 void InitWithFakeAdapter() override; 181 void InitFakeAdapterWithoutRadio() override; 182 void SimulateAdapterPowerFailure() override; 183 void SimulateAdapterPoweredOn() override; 184 void SimulateAdapterPoweredOff() override; 185 BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override; 186 void SimulateLowEnergyDiscoveryFailure() override; 187 void SimulateDevicePaired(BluetoothDevice* device, bool is_paired) override; 188 void SimulatePairingPinCode(BluetoothDevice* device, 189 std::string pin_code) override; 190 void SimulateAdvertisementStarted( 191 BluetoothAdvertisement* advertisement) override; 192 void SimulateAdvertisementStopped( 193 BluetoothAdvertisement* advertisement) override; 194 void SimulateAdvertisementError( 195 BluetoothAdvertisement* advertisement, 196 BluetoothAdvertisement::ErrorCode error_code) override; 197 void SimulateGattConnection(BluetoothDevice* device) override; 198 void SimulateGattConnectionError( 199 BluetoothDevice* device, 200 BluetoothDevice::ConnectErrorCode error_code) override; 201 void SimulateGattDisconnection(BluetoothDevice* device) override; 202 void SimulateDeviceBreaksConnection(BluetoothDevice* device) override; 203 void SimulateGattNameChange(BluetoothDevice* device, 204 const std::string& new_name) override; 205 void SimulateStatusChangeToDisconnect(BluetoothDevice* device) override; 206 void SimulateGattServicesDiscovered( 207 BluetoothDevice* device, 208 const std::vector<std::string>& uuids, 209 const std::vector<std::string>& blocked_uuids = {}) override; 210 void SimulateGattServicesChanged(BluetoothDevice* device) override; 211 void SimulateGattServiceRemoved(BluetoothRemoteGattService* service) override; 212 void SimulateGattServicesDiscoveryError(BluetoothDevice* device) override; 213 void SimulateGattCharacteristic(BluetoothRemoteGattService* service, 214 const std::string& uuid, 215 int properties) override; 216 void SimulateGattNotifySessionStarted( 217 BluetoothRemoteGattCharacteristic* characteristic) override; 218 void SimulateGattNotifySessionStartError( 219 BluetoothRemoteGattCharacteristic* characteristic, 220 BluetoothRemoteGattService::GattErrorCode error_code) override; 221 void SimulateGattNotifySessionStopped( 222 BluetoothRemoteGattCharacteristic* characteristic) override; 223 void SimulateGattNotifySessionStopError( 224 BluetoothRemoteGattCharacteristic* characteristic, 225 BluetoothRemoteGattService::GattErrorCode error_code) override; 226 void SimulateGattCharacteristicChanged( 227 BluetoothRemoteGattCharacteristic* characteristic, 228 const std::vector<uint8_t>& value) override; 229 void SimulateGattCharacteristicRead( 230 BluetoothRemoteGattCharacteristic* characteristic, 231 const std::vector<uint8_t>& value) override; 232 void SimulateGattCharacteristicReadError( 233 BluetoothRemoteGattCharacteristic* characteristic, 234 BluetoothRemoteGattService::GattErrorCode error_code) override; 235 void SimulateGattCharacteristicWrite( 236 BluetoothRemoteGattCharacteristic* characteristic) override; 237 void SimulateGattCharacteristicWriteError( 238 BluetoothRemoteGattCharacteristic* characteristic, 239 BluetoothRemoteGattService::GattErrorCode error_code) override; 240 void SimulateGattDescriptor(BluetoothRemoteGattCharacteristic* characteristic, 241 const std::string& uuid) override; 242 void SimulateGattDescriptorRead(BluetoothRemoteGattDescriptor* descriptor, 243 const std::vector<uint8_t>& value) override; 244 void SimulateGattDescriptorReadError( 245 BluetoothRemoteGattDescriptor* descriptor, 246 BluetoothRemoteGattService::GattErrorCode error_code) override; 247 void SimulateGattDescriptorWrite( 248 BluetoothRemoteGattDescriptor* descriptor) override; 249 void SimulateGattDescriptorWriteError( 250 BluetoothRemoteGattDescriptor* descriptor, 251 BluetoothRemoteGattService::GattErrorCode error_code) override; 252 void DeleteDevice(BluetoothDevice* device) override; 253 254 void OnFakeBluetoothDeviceConnectGattAttempt(); 255 void OnFakeBluetoothDeviceGattServiceDiscoveryAttempt(); 256 void OnFakeBluetoothGattDisconnect(); 257 void OnFakeBluetoothCharacteristicReadValue(); 258 void OnFakeBluetoothCharacteristicWriteValue(std::vector<uint8_t> value); 259 void OnFakeBluetoothGattSetCharacteristicNotification(NotifyValueState state); 260 void OnFakeBluetoothDescriptorReadValue(); 261 void OnFakeBluetoothDescriptorWriteValue(std::vector<uint8_t> value); 262 263 private: 264 base::test::ScopedFeatureList scoped_feature_list_; 265 base::Optional<base::win::ScopedWinrtInitializer> scoped_winrt_initializer_; 266 267 DISALLOW_COPY_AND_ASSIGN(BluetoothTestWinrt); 268 }; 269 270 using BluetoothTestWinrtOnly = BluetoothTestWinrt; 271 272 } // namespace device 273 274 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_WIN_H_ 275