OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #include "device/bluetooth/device.h" | 5 #include "device/bluetooth/device.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 15 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
16 #include "device/bluetooth/test/mock_bluetooth_device.h" | 16 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 17 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" |
17 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 18 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 19 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 using ::testing::Return; | 22 using ::testing::Return; |
21 | 23 |
22 namespace bluetooth { | 24 namespace bluetooth { |
23 | 25 |
24 using NiceMockBluetoothAdapter = | 26 using NiceMockBluetoothAdapter = |
25 testing::NiceMock<device::MockBluetoothAdapter>; | 27 testing::NiceMock<device::MockBluetoothAdapter>; |
26 using NiceMockBluetoothDevice = testing::NiceMock<device::MockBluetoothDevice>; | 28 using NiceMockBluetoothDevice = testing::NiceMock<device::MockBluetoothDevice>; |
27 using NiceMockBluetoothGattService = | 29 using NiceMockBluetoothGattService = |
28 testing::NiceMock<device::MockBluetoothGattService>; | 30 testing::NiceMock<device::MockBluetoothGattService>; |
| 31 using NiceMockBluetoothGattCharacteristic = |
| 32 testing::NiceMock<device::MockBluetoothGattCharacteristic>; |
29 using NiceMockBluetoothGattConnection = | 33 using NiceMockBluetoothGattConnection = |
30 testing::NiceMock<device::MockBluetoothGattConnection>; | 34 testing::NiceMock<device::MockBluetoothGattConnection>; |
31 | 35 |
| 36 using Properties = device::BluetoothGattCharacteristic::Properties; |
| 37 using Property = device::BluetoothGattCharacteristic::Property; |
| 38 |
32 namespace { | 39 namespace { |
33 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; | 40 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; |
34 const char kTestLeDeviceName0[] = "Test LE Device 0"; | 41 const char kTestLeDeviceName0[] = "Test LE Device 0"; |
35 | 42 |
36 const char kTestServiceId0[] = "service_id0"; | 43 const char kTestServiceId0[] = "service_id0"; |
37 const char kTestServiceUuid0[] = "1234"; | 44 const char kTestServiceUuid0[] = "1234"; |
38 | 45 |
39 const char kTestServiceId1[] = "service_id1"; | 46 const char kTestServiceId1[] = "service_id1"; |
40 const char kTestServiceUuid1[] = "5678"; | 47 const char kTestServiceUuid1[] = "5678"; |
41 | 48 |
| 49 const char kTestCharacteristicId0[] = "characteristic_id0"; |
| 50 const char kTestCharacteristicUuid0[] = "1234"; |
| 51 |
| 52 const char kTestCharacteristicId1[] = "characteristic_id1"; |
| 53 const char kTestCharacteristicUuid1[] = "5678"; |
| 54 |
| 55 const char kTestCharacteristicId2[] = "characteristic_id2"; |
| 56 const char kTestCharacteristicUuid2[] = "9012"; |
| 57 |
| 58 const Properties kReadWriteProperties = |
| 59 Property::PROPERTY_READ | Property::PROPERTY_WRITE; |
| 60 const Properties kAllProperties = Property::NUM_PROPERTY - 1; |
| 61 |
42 class BluetoothInterfaceDeviceTest : public testing::Test { | 62 class BluetoothInterfaceDeviceTest : public testing::Test { |
43 public: | 63 public: |
44 enum class Call { EXPECTED, NOT_EXPECTED }; | 64 enum class Call { EXPECTED, NOT_EXPECTED }; |
45 | 65 |
46 BluetoothInterfaceDeviceTest() | 66 BluetoothInterfaceDeviceTest() |
47 : adapter_(new NiceMockBluetoothAdapter), | 67 : adapter_(new NiceMockBluetoothAdapter), |
48 device_(adapter_.get(), | 68 device_(adapter_.get(), |
49 0, | 69 0, |
50 kTestLeDeviceName0, | 70 kTestLeDeviceName0, |
51 kTestLeDeviceAddress0, | 71 kTestLeDeviceAddress0, |
52 false, | 72 false, |
53 true), | 73 true), |
54 weak_factory_(this) { | 74 weak_factory_(this) { |
55 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | 75 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) |
56 .WillByDefault(Return(&device_)); | 76 .WillByDefault(Return(&device_)); |
57 | 77 |
58 auto service1 = base::MakeUnique<NiceMockBluetoothGattService>( | 78 auto service1 = base::MakeUnique<NiceMockBluetoothGattService>( |
59 &device_, kTestServiceId0, device::BluetoothUUID(kTestServiceUuid0), | 79 &device_, kTestServiceId0, device::BluetoothUUID(kTestServiceUuid0), |
60 true /* is_primary */, false /* is_local */); | 80 true /* is_primary */, false /* is_local */); |
| 81 |
| 82 auto characteristic1 = |
| 83 base::MakeUnique<NiceMockBluetoothGattCharacteristic>( |
| 84 service1.get(), kTestCharacteristicId0, |
| 85 device::BluetoothUUID(kTestCharacteristicUuid0), |
| 86 false /* is_local */, kReadWriteProperties, 0 /* permissions */); |
| 87 |
| 88 auto characteristic2 = |
| 89 base::MakeUnique<NiceMockBluetoothGattCharacteristic>( |
| 90 service1.get(), kTestCharacteristicId1, |
| 91 device::BluetoothUUID(kTestCharacteristicUuid1), |
| 92 false /* is_local */, kReadWriteProperties, 0 /* permissions */); |
| 93 |
| 94 service1->AddMockCharacteristic(std::move(characteristic1)); |
| 95 service1->AddMockCharacteristic(std::move(characteristic2)); |
| 96 |
61 auto service2 = base::MakeUnique<NiceMockBluetoothGattService>( | 97 auto service2 = base::MakeUnique<NiceMockBluetoothGattService>( |
62 &device_, kTestServiceId1, device::BluetoothUUID(kTestServiceUuid1), | 98 &device_, kTestServiceId1, device::BluetoothUUID(kTestServiceUuid1), |
63 true /* is_primary */, false /* is_local */); | 99 true /* is_primary */, false /* is_local */); |
64 | 100 |
| 101 auto characteristic3 = |
| 102 base::MakeUnique<NiceMockBluetoothGattCharacteristic>( |
| 103 service2.get(), kTestCharacteristicId2, |
| 104 device::BluetoothUUID(kTestCharacteristicUuid2), |
| 105 false /* is_local */, kAllProperties, 0 /* permissions */); |
| 106 |
| 107 service2->AddMockCharacteristic(std::move(characteristic3)); |
| 108 |
| 109 EXPECT_CALL(*service1, GetCharacteristics()) |
| 110 .WillRepeatedly( |
| 111 Invoke(service1.get(), |
| 112 &device::MockBluetoothGattService::GetMockCharacteristics)); |
| 113 |
| 114 EXPECT_CALL(*service2, GetCharacteristics()) |
| 115 .WillRepeatedly( |
| 116 Invoke(service2.get(), |
| 117 &device::MockBluetoothGattService::GetMockCharacteristics)); |
| 118 |
65 device_.AddMockService(std::move(service1)); | 119 device_.AddMockService(std::move(service1)); |
66 device_.AddMockService(std::move(service2)); | 120 device_.AddMockService(std::move(service2)); |
67 | 121 |
68 EXPECT_CALL(device_, GetGattServices()) | 122 EXPECT_CALL(device_, GetGattServices()) |
69 .WillRepeatedly( | 123 .WillRepeatedly( |
70 Invoke(&device_, &device::MockBluetoothDevice::GetMockServices)); | 124 Invoke(&device_, &device::MockBluetoothDevice::GetMockServices)); |
71 | 125 |
| 126 EXPECT_CALL(device_, GetGattService(testing::_)) |
| 127 .WillRepeatedly( |
| 128 Invoke(&device_, &device::MockBluetoothDevice::GetMockService)); |
| 129 |
72 auto connection = base::MakeUnique<NiceMockBluetoothGattConnection>( | 130 auto connection = base::MakeUnique<NiceMockBluetoothGattConnection>( |
73 adapter_, device_.GetAddress()); | 131 adapter_, device_.GetAddress()); |
74 | 132 |
75 Device::Create(adapter_, std::move(connection), mojo::MakeRequest(&proxy_)); | 133 Device::Create(adapter_, std::move(connection), mojo::MakeRequest(&proxy_)); |
76 | 134 |
77 proxy_.set_connection_error_handler( | 135 proxy_.set_connection_error_handler( |
78 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, | 136 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, |
79 weak_factory_.GetWeakPtr())); | 137 weak_factory_.GetWeakPtr())); |
80 } | 138 } |
81 | 139 |
82 void TearDown() override { | 140 void TearDown() override { |
83 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); | 141 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); |
84 EXPECT_EQ(message_pipe_closed_, expect_device_service_deleted_); | 142 EXPECT_EQ(message_pipe_closed_, expect_device_service_deleted_); |
85 proxy_.reset(); | 143 proxy_.reset(); |
86 } | 144 } |
87 | 145 |
88 protected: | 146 protected: |
89 void OnConnectionError() { message_pipe_closed_ = true; } | 147 void OnConnectionError() { message_pipe_closed_ = true; } |
90 | 148 |
91 void SimulateGattServicesDiscovered() { | 149 void SimulateGattServicesDiscovered() { |
92 for (auto& observer : adapter_->GetObservers()) | 150 for (auto& observer : adapter_->GetObservers()) |
93 observer.GattServicesDiscovered(adapter_.get(), &device_); | 151 observer.GattServicesDiscovered(adapter_.get(), &device_); |
94 } | 152 } |
95 | 153 |
96 void SimulateDeviceChanged() { | 154 void SimulateDeviceChanged() { |
97 for (auto& observer : adapter_->GetObservers()) | 155 for (auto& observer : adapter_->GetObservers()) |
98 observer.DeviceChanged(adapter_.get(), &device_); | 156 observer.DeviceChanged(adapter_.get(), &device_); |
99 } | 157 } |
100 | 158 |
101 void GetServicesCheckForPrecedingCalls( | 159 void CheckGetServicesCountImpl(Call expected, |
102 Call expected, | 160 size_t expected_service_count, |
103 size_t expected_service_count, | 161 int num_of_preceding_calls, |
104 int num_of_preceding_calls, | 162 std::vector<mojom::ServiceInfoPtr> services) { |
105 std::vector<mojom::ServiceInfoPtr> services) { | 163 EXPECT_EQ(num_of_preceding_calls, actual_callback_count_); |
106 EXPECT_EQ(num_of_preceding_calls, callback_count_); | 164 ++actual_callback_count_; |
107 ++callback_count_; | |
108 | 165 |
109 if (expected == Call::EXPECTED) | 166 if (expected == Call::EXPECTED) |
110 ++actual_success_callback_calls_; | 167 ++actual_success_callback_calls_; |
111 | 168 |
112 EXPECT_EQ(expected_service_count, services.size()); | 169 EXPECT_EQ(expected_service_count, services.size()); |
113 } | 170 } |
114 | 171 |
115 Device::GetServicesCallback GetGetServicesCheckForPrecedingCalls( | 172 Device::GetServicesCallback CheckGetServicesCount(Call expected) { |
| 173 if (expected == Call::EXPECTED) |
| 174 ++expected_success_callback_calls_; |
| 175 |
| 176 return base::Bind(&BluetoothInterfaceDeviceTest::CheckGetServicesCountImpl, |
| 177 weak_factory_.GetWeakPtr(), expected, |
| 178 2 /* expected_service_count */, |
| 179 expected_callback_count_++); |
| 180 } |
| 181 |
| 182 void CheckGetCharacteristicsCountImpl( |
116 Call expected, | 183 Call expected, |
117 int num_of_preceding_calls) { | 184 size_t expected_count, |
| 185 int num_of_preceding_calls, |
| 186 std::vector<mojom::CharacteristicInfoPtr> characteristics) { |
| 187 EXPECT_EQ(num_of_preceding_calls, actual_callback_count_); |
| 188 ++actual_callback_count_; |
| 189 |
| 190 if (expected == Call::EXPECTED) |
| 191 ++actual_success_callback_calls_; |
| 192 |
| 193 EXPECT_EQ(expected_count, characteristics.size()); |
| 194 } |
| 195 |
| 196 Device::GetCharacteristicsCallback CheckGetCharacteristicsCount( |
| 197 Call expected, |
| 198 int expected_count) { |
118 if (expected == Call::EXPECTED) | 199 if (expected == Call::EXPECTED) |
119 ++expected_success_callback_calls_; | 200 ++expected_success_callback_calls_; |
120 | 201 |
121 return base::Bind( | 202 return base::Bind( |
122 &BluetoothInterfaceDeviceTest::GetServicesCheckForPrecedingCalls, | 203 &BluetoothInterfaceDeviceTest::CheckGetCharacteristicsCountImpl, |
123 weak_factory_.GetWeakPtr(), expected, 2 /* expected_service_count */, | 204 weak_factory_.GetWeakPtr(), expected, expected_count, |
124 num_of_preceding_calls); | 205 expected_callback_count_++); |
125 } | 206 } |
126 | 207 |
127 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | 208 scoped_refptr<NiceMockBluetoothAdapter> adapter_; |
128 NiceMockBluetoothDevice device_; | 209 NiceMockBluetoothDevice device_; |
129 base::MessageLoop message_loop_; | 210 base::MessageLoop message_loop_; |
130 mojom::DevicePtr proxy_; | 211 mojom::DevicePtr proxy_; |
131 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; | 212 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; |
132 | 213 |
133 bool message_pipe_closed_ = false; | 214 bool message_pipe_closed_ = false; |
134 bool expect_device_service_deleted_ = false; | 215 bool expect_device_service_deleted_ = false; |
135 int expected_success_callback_calls_ = 0; | 216 int expected_success_callback_calls_ = 0; |
136 int actual_success_callback_calls_ = 0; | 217 int actual_success_callback_calls_ = 0; |
137 int callback_count_ = 0; | 218 int actual_callback_count_ = 0; |
| 219 int expected_callback_count_ = 0; |
138 | 220 |
139 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; | 221 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; |
140 }; | 222 }; |
141 } // namespace | 223 } // namespace |
142 | 224 |
143 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { | 225 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { |
144 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 226 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
145 .WillRepeatedly(Return(true)); | 227 .WillRepeatedly(Return(true)); |
146 | 228 |
147 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 229 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
148 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | |
149 | 230 |
150 base::RunLoop().RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
151 } | 232 } |
152 | 233 |
153 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { | 234 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { |
154 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 235 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
155 .WillOnce(Return(false)) | 236 .WillOnce(Return(false)) |
156 .WillOnce(Return(false)) | 237 .WillOnce(Return(false)) |
157 .WillRepeatedly(Return(true)); | 238 .WillRepeatedly(Return(true)); |
158 | 239 |
159 // Client: Sends multiple requests for services. | 240 // Client: Sends multiple requests for services. |
160 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 241 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
161 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | 242 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
162 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
163 Call::EXPECTED, 1 /* num_of_preceding_calls */)); | |
164 | 243 |
165 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
166 | 245 |
167 SimulateGattServicesDiscovered(); | 246 SimulateGattServicesDiscovered(); |
168 | 247 |
169 // No more GetServices calls will complete. | 248 // No more GetServices calls will complete. |
170 SimulateGattServicesDiscovered(); | 249 SimulateGattServicesDiscovered(); |
171 | 250 |
172 base::RunLoop().RunUntilIdle(); | 251 base::RunLoop().RunUntilIdle(); |
173 | 252 |
174 // Client: Sends more requests which run immediately. | 253 // Client: Sends more requests which run immediately. |
175 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 254 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
176 Call::EXPECTED, 2 /* num_of_preceding_calls */)); | 255 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
177 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
178 Call::EXPECTED, 3 /* num_of_preceding_calls */)); | |
179 | 256 |
180 base::RunLoop().RunUntilIdle(); | 257 base::RunLoop().RunUntilIdle(); |
181 | 258 |
182 // No more GetServices calls will complete. | 259 // No more GetServices calls will complete. |
183 SimulateGattServicesDiscovered(); | 260 SimulateGattServicesDiscovered(); |
184 | 261 |
185 // Wait for message pipe to process error. | 262 // Wait for message pipe to process error. |
186 base::RunLoop().RunUntilIdle(); | 263 base::RunLoop().RunUntilIdle(); |
187 } | 264 } |
188 | 265 |
189 TEST_F(BluetoothInterfaceDeviceTest, | 266 TEST_F(BluetoothInterfaceDeviceTest, |
190 GetServicesLostConnectionWithPendingRequests) { | 267 GetServicesLostConnectionWithPendingRequests) { |
191 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 268 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
192 .WillRepeatedly(Return(false)); | 269 .WillRepeatedly(Return(false)); |
193 // Client: Sends multiple requests for services. | 270 // Client: Sends multiple requests for services. |
194 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 271 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
195 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | 272 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
196 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 273 EXPECT_EQ(0, actual_callback_count_); |
197 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
198 EXPECT_EQ(0, callback_count_); | |
199 | 274 |
200 // Simulate connection loss. | 275 // Simulate connection loss. |
201 device_.SetConnected(false); | 276 device_.SetConnected(false); |
202 SimulateDeviceChanged(); | 277 SimulateDeviceChanged(); |
203 expect_device_service_deleted_ = true; | 278 expect_device_service_deleted_ = true; |
204 | 279 |
205 // Wait for message pipe to process error. | 280 // Wait for message pipe to process error. |
206 base::RunLoop().RunUntilIdle(); | 281 base::RunLoop().RunUntilIdle(); |
207 } | 282 } |
208 | 283 |
209 TEST_F(BluetoothInterfaceDeviceTest, | 284 TEST_F(BluetoothInterfaceDeviceTest, |
210 GetServicesForcedDisconnectionWithPendingRequests) { | 285 GetServicesForcedDisconnectionWithPendingRequests) { |
211 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 286 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
212 .WillRepeatedly(Return(false)); | 287 .WillRepeatedly(Return(false)); |
213 | 288 |
214 // Client: Sends multiple requests for services. | 289 // Client: Sends multiple requests for services. |
215 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 290 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
216 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | 291 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
217 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 292 EXPECT_EQ(0, actual_callback_count_); |
218 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
219 EXPECT_EQ(0, callback_count_); | |
220 | 293 |
221 // Simulate connection loss. | 294 // Simulate connection loss. |
222 proxy_->Disconnect(); | 295 proxy_->Disconnect(); |
| 296 expect_device_service_deleted_ = true; |
| 297 |
| 298 // Wait for message pipe to process error. |
| 299 base::RunLoop().RunUntilIdle(); |
| 300 } |
| 301 |
| 302 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristics) { |
| 303 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
| 304 .WillRepeatedly(Return(true)); |
| 305 |
| 306 proxy_->GetCharacteristics(kTestServiceId0, |
| 307 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); |
| 308 |
| 309 base::RunLoop().RunUntilIdle(); |
| 310 } |
| 311 |
| 312 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristicsNotDiscovered) { |
| 313 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
| 314 .WillOnce(Return(false)) |
| 315 .WillOnce(Return(false)) |
| 316 .WillRepeatedly(Return(true)); |
| 317 |
| 318 // Client: Sends multiple requests for characteristics. |
| 319 proxy_->GetCharacteristics(kTestServiceId0, |
| 320 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); |
| 321 proxy_->GetCharacteristics(kTestServiceId1, |
| 322 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); |
| 323 |
| 324 base::RunLoop().RunUntilIdle(); |
| 325 |
| 326 SimulateGattServicesDiscovered(); |
| 327 |
| 328 // No more GetCharacteristics calls will complete. |
| 329 SimulateGattServicesDiscovered(); |
| 330 |
| 331 base::RunLoop().RunUntilIdle(); |
| 332 |
| 333 // Client: Sends more requests which run immediately. |
| 334 proxy_->GetCharacteristics(kTestServiceId0, |
| 335 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); |
| 336 proxy_->GetCharacteristics(kTestServiceId1, |
| 337 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); |
| 338 |
| 339 base::RunLoop().RunUntilIdle(); |
| 340 |
| 341 // No more GetCharacteristics calls will complete. |
| 342 SimulateGattServicesDiscovered(); |
| 343 |
| 344 // Wait for message pipe to process error. |
| 345 base::RunLoop().RunUntilIdle(); |
| 346 } |
| 347 |
| 348 TEST_F(BluetoothInterfaceDeviceTest, |
| 349 GetCharacteristicsLostConnectionWithPendingRequests) { |
| 350 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
| 351 .WillRepeatedly(Return(false)); |
| 352 // Client: Sends multiple requests for characteristics. |
| 353 proxy_->GetCharacteristics( |
| 354 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); |
| 355 proxy_->GetCharacteristics( |
| 356 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); |
| 357 EXPECT_EQ(0, actual_callback_count_); |
| 358 |
| 359 // Simulate connection loss. |
| 360 device_.SetConnected(false); |
| 361 SimulateDeviceChanged(); |
| 362 expect_device_service_deleted_ = true; |
| 363 |
| 364 // Wait for message pipe to process error. |
| 365 base::RunLoop().RunUntilIdle(); |
| 366 } |
| 367 |
| 368 TEST_F(BluetoothInterfaceDeviceTest, |
| 369 GetCharacteristicsForcedDisconnectionWithPendingRequests) { |
| 370 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
| 371 .WillRepeatedly(Return(false)); |
| 372 |
| 373 // Client: Sends multiple requests for characteristics. |
| 374 proxy_->GetCharacteristics( |
| 375 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); |
| 376 proxy_->GetCharacteristics( |
| 377 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); |
| 378 EXPECT_EQ(0, actual_callback_count_); |
| 379 |
| 380 // Simulate connection loss. |
| 381 proxy_->Disconnect(); |
223 expect_device_service_deleted_ = true; | 382 expect_device_service_deleted_ = true; |
224 | 383 |
225 // Wait for message pipe to process error. | 384 // Wait for message pipe to process error. |
226 base::RunLoop().RunUntilIdle(); | 385 base::RunLoop().RunUntilIdle(); |
227 } | 386 } |
228 | 387 |
229 } // namespace bluetooth | 388 } // namespace bluetooth |
OLD | NEW |