OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ | 5 #ifndef DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ |
6 #define DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ | 6 #define DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 class BluetoothAdapter; | 22 class BluetoothAdapter; |
23 class BluetoothDevice; | 23 class BluetoothDevice; |
24 | 24 |
25 // A test fixture for Bluetooth that abstracts platform specifics for creating | 25 // A test fixture for Bluetooth that abstracts platform specifics for creating |
26 // and controlling fake low level objects. | 26 // and controlling fake low level objects. |
27 // | 27 // |
28 // Subclasses on each platform implement this, and are then typedef-ed to | 28 // Subclasses on each platform implement this, and are then typedef-ed to |
29 // BluetoothTest. | 29 // BluetoothTest. |
30 class BluetoothTestBase : public testing::Test { | 30 class BluetoothTestBase : public testing::Test { |
31 public: | 31 public: |
32 enum class Call { EXPECTED, NOT_EXPECTED }; | |
33 | |
32 static const std::string kTestAdapterName; | 34 static const std::string kTestAdapterName; |
33 static const std::string kTestAdapterAddress; | 35 static const std::string kTestAdapterAddress; |
34 | 36 |
35 static const std::string kTestDeviceName; | 37 static const std::string kTestDeviceName; |
36 static const std::string kTestDeviceNameEmpty; | 38 static const std::string kTestDeviceNameEmpty; |
37 | 39 |
38 static const std::string kTestDeviceAddress1; | 40 static const std::string kTestDeviceAddress1; |
39 static const std::string kTestDeviceAddress2; | 41 static const std::string kTestDeviceAddress2; |
40 | 42 |
41 static const std::string kTestUUIDGenericAccess; | 43 static const std::string kTestUUIDGenericAccess; |
42 static const std::string kTestUUIDGenericAttribute; | 44 static const std::string kTestUUIDGenericAttribute; |
43 static const std::string kTestUUIDImmediateAlert; | 45 static const std::string kTestUUIDImmediateAlert; |
44 static const std::string kTestUUIDLinkLoss; | 46 static const std::string kTestUUIDLinkLoss; |
45 | 47 |
46 BluetoothTestBase(); | 48 BluetoothTestBase(); |
47 ~BluetoothTestBase() override; | 49 ~BluetoothTestBase() override; |
48 | 50 |
51 // Checks that no unexpected calls have been made to callbacks. | |
52 void TearDown() override; | |
53 | |
49 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, | 54 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, |
50 // and this fixture's callbacks. Then RunLoop().RunUntilIdle(). | 55 // and this fixture's callbacks. The success callback is expected to be |
56 // called. Then RunLoop().RunUntilIdle(). | |
scheib
2015/11/20 23:08:20
// Calls adapter_->StartDiscoverySessionWithFilter
ortuno
2015/11/21 01:12:49
Done.
| |
51 void StartLowEnergyDiscoverySession(); | 57 void StartLowEnergyDiscoverySession(); |
52 | 58 |
59 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, | |
60 // and this fixture's callbacks. The error callback is expected to be called. | |
61 // Then RunLoop().RunUntilIdle(). | |
62 void StartLowEnergyDiscoverySessionExpectedToFail(); | |
63 | |
53 // Check if Low Energy is available. On Mac, we require OS X >= 10.10. | 64 // Check if Low Energy is available. On Mac, we require OS X >= 10.10. |
54 virtual bool PlatformSupportsLowEnergy() = 0; | 65 virtual bool PlatformSupportsLowEnergy() = 0; |
55 | 66 |
56 // Initializes the BluetoothAdapter |adapter_| with the system adapter. | 67 // Initializes the BluetoothAdapter |adapter_| with the system adapter. |
57 virtual void InitWithDefaultAdapter(){}; | 68 virtual void InitWithDefaultAdapter(){}; |
58 | 69 |
59 // Initializes the BluetoothAdapter |adapter_| with the system adapter forced | 70 // Initializes the BluetoothAdapter |adapter_| with the system adapter forced |
60 // to be ignored as if it did not exist. This enables tests for when an | 71 // to be ignored as if it did not exist. This enables tests for when an |
61 // adapter is not present on the system. | 72 // adapter is not present on the system. |
62 virtual void InitWithoutDefaultAdapter(){}; | 73 virtual void InitWithoutDefaultAdapter(){}; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 152 |
142 // Simulates a Characteristic Write operation failing synchronously once for | 153 // Simulates a Characteristic Write operation failing synchronously once for |
143 // an unknown reason. | 154 // an unknown reason. |
144 virtual void SimulateGattCharacteristicWriteWillFailSynchronouslyOnce( | 155 virtual void SimulateGattCharacteristicWriteWillFailSynchronouslyOnce( |
145 BluetoothGattCharacteristic* characteristic) {} | 156 BluetoothGattCharacteristic* characteristic) {} |
146 | 157 |
147 // Remove the device from the adapter and delete it. | 158 // Remove the device from the adapter and delete it. |
148 virtual void DeleteDevice(BluetoothDevice* device); | 159 virtual void DeleteDevice(BluetoothDevice* device); |
149 | 160 |
150 // Callbacks that increment |callback_count_|, |error_callback_count_|: | 161 // Callbacks that increment |callback_count_|, |error_callback_count_|: |
151 void Callback(); | 162 void Callback(Call expected); |
152 void DiscoverySessionCallback(scoped_ptr<BluetoothDiscoverySession>); | 163 void DiscoverySessionCallback(Call expected, |
153 void GattConnectionCallback(scoped_ptr<BluetoothGattConnection>); | 164 scoped_ptr<BluetoothDiscoverySession>); |
154 void NotifyCallback(scoped_ptr<BluetoothGattNotifySession>); | 165 void GattConnectionCallback(Call expected, |
155 void ReadValueCallback(const std::vector<uint8>& value); | 166 scoped_ptr<BluetoothGattConnection>); |
156 void ErrorCallback(); | 167 void NotifyCallback(Call expected, scoped_ptr<BluetoothGattNotifySession>); |
157 void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode); | 168 void ReadValueCallback(Call expected, const std::vector<uint8>& value); |
158 void GattErrorCallback(BluetoothGattService::GattErrorCode); | 169 void ErrorCallback(Call expected); |
170 void ConnectErrorCallback(Call expected, | |
171 enum BluetoothDevice::ConnectErrorCode); | |
172 void GattErrorCallback(Call expected, BluetoothGattService::GattErrorCode); | |
159 | 173 |
160 // Accessors to get callbacks bound to this fixture: | 174 // Accessors to get callbacks bound to this fixture: |
161 base::Closure GetCallback(); | 175 base::Closure GetCallback(Call expected); |
162 BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback(); | 176 BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback( |
163 BluetoothDevice::GattConnectionCallback GetGattConnectionCallback(); | 177 Call expected); |
164 BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback(); | 178 BluetoothDevice::GattConnectionCallback GetGattConnectionCallback( |
165 BluetoothGattCharacteristic::ValueCallback GetReadValueCallback(); | 179 Call expected); |
166 BluetoothAdapter::ErrorCallback GetErrorCallback(); | 180 BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback( |
167 BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(); | 181 Call expected); |
182 BluetoothGattCharacteristic::ValueCallback GetReadValueCallback( | |
183 Call expected); | |
184 BluetoothAdapter::ErrorCallback GetErrorCallback(Call expected); | |
185 BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(Call expected); | |
168 base::Callback<void(BluetoothGattService::GattErrorCode)> | 186 base::Callback<void(BluetoothGattService::GattErrorCode)> |
169 GetGattErrorCallback(); | 187 GetGattErrorCallback(Call expected); |
170 | 188 |
171 // Reset all event count members to 0. | 189 // Reset all event count members to 0. |
172 void ResetEventCounts(); | 190 void ResetEventCounts(); |
173 | 191 |
174 // A Message loop is required by some implementations that will PostTasks and | 192 // A Message loop is required by some implementations that will PostTasks and |
175 // by base::RunLoop().RunUntilIdle() use in this fixuture. | 193 // by base::RunLoop().RunUntilIdle() use in this fixture. |
176 base::MessageLoop message_loop_; | 194 base::MessageLoop message_loop_; |
177 | 195 |
178 scoped_refptr<BluetoothAdapter> adapter_; | 196 scoped_refptr<BluetoothAdapter> adapter_; |
179 ScopedVector<BluetoothDiscoverySession> discovery_sessions_; | 197 ScopedVector<BluetoothDiscoverySession> discovery_sessions_; |
180 ScopedVector<BluetoothGattConnection> gatt_connections_; | 198 ScopedVector<BluetoothGattConnection> gatt_connections_; |
181 enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ = | 199 enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ = |
182 BluetoothDevice::ERROR_UNKNOWN; | 200 BluetoothDevice::ERROR_UNKNOWN; |
183 ScopedVector<BluetoothGattNotifySession> notify_sessions_; | 201 ScopedVector<BluetoothGattNotifySession> notify_sessions_; |
184 std::vector<uint8> last_read_value_; | 202 std::vector<uint8> last_read_value_; |
185 std::vector<uint8> last_write_value_; | 203 std::vector<uint8> last_write_value_; |
186 BluetoothGattService::GattErrorCode last_gatt_error_code_; | 204 BluetoothGattService::GattErrorCode last_gatt_error_code_; |
187 int callback_count_ = 0; | 205 int callback_count_ = 0; |
188 int error_callback_count_ = 0; | 206 int error_callback_count_ = 0; |
207 bool unexpected_callback_ = false; | |
189 int gatt_connection_attempts_ = 0; | 208 int gatt_connection_attempts_ = 0; |
190 int gatt_disconnection_attempts_ = 0; | 209 int gatt_disconnection_attempts_ = 0; |
191 int gatt_discovery_attempts_ = 0; | 210 int gatt_discovery_attempts_ = 0; |
192 int gatt_notify_characteristic_attempts_ = 0; | 211 int gatt_notify_characteristic_attempts_ = 0; |
193 int gatt_read_characteristic_attempts_ = 0; | 212 int gatt_read_characteristic_attempts_ = 0; |
194 int gatt_write_characteristic_attempts_ = 0; | 213 int gatt_write_characteristic_attempts_ = 0; |
195 base::WeakPtrFactory<BluetoothTestBase> weak_factory_; | 214 base::WeakPtrFactory<BluetoothTestBase> weak_factory_; |
196 }; | 215 }; |
197 | 216 |
198 } // namespace device | 217 } // namespace device |
199 | 218 |
200 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ | 219 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ |
OLD | NEW |