Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: device/bluetooth/test/bluetooth_test.h

Issue 1465863003: bluetooth: Fix crash when trying to read or write when operation pending (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Address scheib's comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // Overrides of this method should always call the parent's class method.
53 void TearDown() override;
54
49 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, 55 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport,
50 // and this fixture's callbacks. Then RunLoop().RunUntilIdle(). 56 // and this fixture's callbacks expecting success.
57 // Then RunLoop().RunUntilIdle().
51 void StartLowEnergyDiscoverySession(); 58 void StartLowEnergyDiscoverySession();
52 59
60 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport,
61 // and this fixture's callbacks expecting error.
62 // Then RunLoop().RunUntilIdle().
63 void StartLowEnergyDiscoverySessionExpectedToFail();
64
53 // Check if Low Energy is available. On Mac, we require OS X >= 10.10. 65 // Check if Low Energy is available. On Mac, we require OS X >= 10.10.
54 virtual bool PlatformSupportsLowEnergy() = 0; 66 virtual bool PlatformSupportsLowEnergy() = 0;
55 67
56 // Initializes the BluetoothAdapter |adapter_| with the system adapter. 68 // Initializes the BluetoothAdapter |adapter_| with the system adapter.
57 virtual void InitWithDefaultAdapter(){}; 69 virtual void InitWithDefaultAdapter(){};
58 70
59 // Initializes the BluetoothAdapter |adapter_| with the system adapter forced 71 // 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 72 // to be ignored as if it did not exist. This enables tests for when an
61 // adapter is not present on the system. 73 // adapter is not present on the system.
62 virtual void InitWithoutDefaultAdapter(){}; 74 virtual void InitWithoutDefaultAdapter(){};
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 153
142 // Simulates a Characteristic Write operation failing synchronously once for 154 // Simulates a Characteristic Write operation failing synchronously once for
143 // an unknown reason. 155 // an unknown reason.
144 virtual void SimulateGattCharacteristicWriteWillFailSynchronouslyOnce( 156 virtual void SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(
145 BluetoothGattCharacteristic* characteristic) {} 157 BluetoothGattCharacteristic* characteristic) {}
146 158
147 // Remove the device from the adapter and delete it. 159 // Remove the device from the adapter and delete it.
148 virtual void DeleteDevice(BluetoothDevice* device); 160 virtual void DeleteDevice(BluetoothDevice* device);
149 161
150 // Callbacks that increment |callback_count_|, |error_callback_count_|: 162 // Callbacks that increment |callback_count_|, |error_callback_count_|:
151 void Callback(); 163 void Callback(Call expected);
152 void DiscoverySessionCallback(scoped_ptr<BluetoothDiscoverySession>); 164 void DiscoverySessionCallback(Call expected,
153 void GattConnectionCallback(scoped_ptr<BluetoothGattConnection>); 165 scoped_ptr<BluetoothDiscoverySession>);
154 void NotifyCallback(scoped_ptr<BluetoothGattNotifySession>); 166 void GattConnectionCallback(Call expected,
155 void ReadValueCallback(const std::vector<uint8>& value); 167 scoped_ptr<BluetoothGattConnection>);
156 void ErrorCallback(); 168 void NotifyCallback(Call expected, scoped_ptr<BluetoothGattNotifySession>);
157 void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode); 169 void ReadValueCallback(Call expected, const std::vector<uint8>& value);
158 void GattErrorCallback(BluetoothGattService::GattErrorCode); 170 void ErrorCallback(Call expected);
171 void ConnectErrorCallback(Call expected,
172 enum BluetoothDevice::ConnectErrorCode);
173 void GattErrorCallback(Call expected, BluetoothGattService::GattErrorCode);
159 174
160 // Accessors to get callbacks bound to this fixture: 175 // Accessors to get callbacks bound to this fixture:
161 base::Closure GetCallback(); 176 base::Closure GetCallback(Call expected);
162 BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback(); 177 BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback(
163 BluetoothDevice::GattConnectionCallback GetGattConnectionCallback(); 178 Call expected);
164 BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback(); 179 BluetoothDevice::GattConnectionCallback GetGattConnectionCallback(
165 BluetoothGattCharacteristic::ValueCallback GetReadValueCallback(); 180 Call expected);
166 BluetoothAdapter::ErrorCallback GetErrorCallback(); 181 BluetoothGattCharacteristic::NotifySessionCallback GetNotifyCallback(
167 BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(); 182 Call expected);
183 BluetoothGattCharacteristic::ValueCallback GetReadValueCallback(
184 Call expected);
185 BluetoothAdapter::ErrorCallback GetErrorCallback(Call expected);
186 BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(Call expected);
168 base::Callback<void(BluetoothGattService::GattErrorCode)> 187 base::Callback<void(BluetoothGattService::GattErrorCode)>
169 GetGattErrorCallback(); 188 GetGattErrorCallback(Call expected);
170 189
171 // Reset all event count members to 0. 190 // Reset all event count members to 0.
172 void ResetEventCounts(); 191 void ResetEventCounts();
173 192
174 // A Message loop is required by some implementations that will PostTasks and 193 // A Message loop is required by some implementations that will PostTasks and
175 // by base::RunLoop().RunUntilIdle() use in this fixuture. 194 // by base::RunLoop().RunUntilIdle() use in this fixture.
176 base::MessageLoop message_loop_; 195 base::MessageLoop message_loop_;
177 196
178 scoped_refptr<BluetoothAdapter> adapter_; 197 scoped_refptr<BluetoothAdapter> adapter_;
179 ScopedVector<BluetoothDiscoverySession> discovery_sessions_; 198 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
180 ScopedVector<BluetoothGattConnection> gatt_connections_; 199 ScopedVector<BluetoothGattConnection> gatt_connections_;
181 enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ = 200 enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ =
182 BluetoothDevice::ERROR_UNKNOWN; 201 BluetoothDevice::ERROR_UNKNOWN;
183 ScopedVector<BluetoothGattNotifySession> notify_sessions_; 202 ScopedVector<BluetoothGattNotifySession> notify_sessions_;
184 std::vector<uint8> last_read_value_; 203 std::vector<uint8> last_read_value_;
185 std::vector<uint8> last_write_value_; 204 std::vector<uint8> last_write_value_;
186 BluetoothGattService::GattErrorCode last_gatt_error_code_; 205 BluetoothGattService::GattErrorCode last_gatt_error_code_;
206
187 int callback_count_ = 0; 207 int callback_count_ = 0;
188 int error_callback_count_ = 0; 208 int error_callback_count_ = 0;
189 int gatt_connection_attempts_ = 0; 209 int gatt_connection_attempts_ = 0;
190 int gatt_disconnection_attempts_ = 0; 210 int gatt_disconnection_attempts_ = 0;
191 int gatt_discovery_attempts_ = 0; 211 int gatt_discovery_attempts_ = 0;
192 int gatt_notify_characteristic_attempts_ = 0; 212 int gatt_notify_characteristic_attempts_ = 0;
193 int gatt_read_characteristic_attempts_ = 0; 213 int gatt_read_characteristic_attempts_ = 0;
194 int gatt_write_characteristic_attempts_ = 0; 214 int gatt_write_characteristic_attempts_ = 0;
215
216 // The following values are used to make sure the correct callbacks
217 // have been called. They are not reset when calling ResetEventCounts().
218 int expected_success_callback_calls_ = 0;
219 int expected_error_callback_calls_ = 0;
220 int actual_success_callback_calls_ = 0;
221 int actual_error_callback_calls_ = 0;
222 bool unexpected_success_callback_ = false;
223 bool unexpected_error_callback_ = false;
224
195 base::WeakPtrFactory<BluetoothTestBase> weak_factory_; 225 base::WeakPtrFactory<BluetoothTestBase> weak_factory_;
196 }; 226 };
197 227
198 } // namespace device 228 } // namespace device
199 229
200 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ 230 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_remote_gatt_characteristic_android.cc ('k') | device/bluetooth/test/bluetooth_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698