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_++); |
206 } | |
207 | |
208 void CheckGetCharacteristicsPropertiesImpl( | |
209 Call expected, | |
210 std::vector<Properties> expected_properties, | |
211 std::vector<mojom::CharacteristicInfoPtr> characteristics) { | |
212 if (expected == Call::EXPECTED) | |
213 ++actual_success_callback_calls_; | |
214 | |
215 ASSERT_EQ(expected_properties.size(), characteristics.size()); | |
216 | |
217 for (size_t i = 0; i < characteristics.size(); i++) { | |
218 EXPECT_EQ(expected_properties[i], characteristics[i]->properties); | |
219 } | |
220 } | |
221 | |
222 Device::GetCharacteristicsCallback CheckGetCharacteristicsProperties( | |
223 Call expected, | |
224 std::vector<Properties> expected_properties) { | |
225 if (expected == Call::EXPECTED) | |
226 ++expected_success_callback_calls_; | |
227 | |
228 return base::Bind( | |
229 &BluetoothInterfaceDeviceTest::CheckGetCharacteristicsPropertiesImpl, | |
230 weak_factory_.GetWeakPtr(), expected, std::move(expected_properties)); | |
125 } | 231 } |
126 | 232 |
127 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | 233 scoped_refptr<NiceMockBluetoothAdapter> adapter_; |
128 NiceMockBluetoothDevice device_; | 234 NiceMockBluetoothDevice device_; |
129 base::MessageLoop message_loop_; | 235 base::MessageLoop message_loop_; |
130 mojom::DevicePtr proxy_; | 236 mojom::DevicePtr proxy_; |
131 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; | 237 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; |
132 | 238 |
133 bool message_pipe_closed_ = false; | 239 bool message_pipe_closed_ = false; |
134 bool expect_device_service_deleted_ = false; | 240 bool expect_device_service_deleted_ = false; |
135 int expected_success_callback_calls_ = 0; | 241 int expected_success_callback_calls_ = 0; |
136 int actual_success_callback_calls_ = 0; | 242 int actual_success_callback_calls_ = 0; |
137 int callback_count_ = 0; | 243 int actual_callback_count_ = 0; |
244 int expected_callback_count_ = 0; | |
138 | 245 |
139 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; | 246 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; |
140 }; | 247 }; |
141 } // namespace | 248 } // namespace |
142 | 249 |
143 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { | 250 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { |
144 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 251 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
145 .WillRepeatedly(Return(true)); | 252 .WillRepeatedly(Return(true)); |
146 | 253 |
147 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 254 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
148 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | |
149 | 255 |
150 base::RunLoop().RunUntilIdle(); | 256 base::RunLoop().RunUntilIdle(); |
151 } | 257 } |
152 | 258 |
153 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { | 259 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { |
154 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 260 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
155 .WillOnce(Return(false)) | 261 .WillOnce(Return(false)) |
156 .WillOnce(Return(false)) | 262 .WillOnce(Return(false)) |
157 .WillRepeatedly(Return(true)); | 263 .WillRepeatedly(Return(true)); |
158 | 264 |
159 // Client: Sends multiple requests for services. | 265 // Client: Sends multiple requests for services. |
160 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 266 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
161 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | 267 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
162 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
163 Call::EXPECTED, 1 /* num_of_preceding_calls */)); | |
164 | 268 |
165 base::RunLoop().RunUntilIdle(); | 269 base::RunLoop().RunUntilIdle(); |
166 | 270 |
167 SimulateGattServicesDiscovered(); | 271 SimulateGattServicesDiscovered(); |
168 | 272 |
169 // No more GetServices calls will complete. | 273 // No more GetServices calls will complete. |
170 SimulateGattServicesDiscovered(); | 274 SimulateGattServicesDiscovered(); |
171 | 275 |
172 base::RunLoop().RunUntilIdle(); | 276 base::RunLoop().RunUntilIdle(); |
173 | 277 |
174 // Client: Sends more requests which run immediately. | 278 // Client: Sends more requests which run immediately. |
175 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 279 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
176 Call::EXPECTED, 2 /* num_of_preceding_calls */)); | 280 proxy_->GetServices(CheckGetServicesCount(Call::EXPECTED)); |
177 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
178 Call::EXPECTED, 3 /* num_of_preceding_calls */)); | |
179 | 281 |
180 base::RunLoop().RunUntilIdle(); | 282 base::RunLoop().RunUntilIdle(); |
181 | 283 |
182 // No more GetServices calls will complete. | 284 // No more GetServices calls will complete. |
183 SimulateGattServicesDiscovered(); | 285 SimulateGattServicesDiscovered(); |
184 | 286 |
185 // Wait for message pipe to process error. | 287 // Wait for message pipe to process error. |
186 base::RunLoop().RunUntilIdle(); | 288 base::RunLoop().RunUntilIdle(); |
187 } | 289 } |
188 | 290 |
189 TEST_F(BluetoothInterfaceDeviceTest, | 291 TEST_F(BluetoothInterfaceDeviceTest, |
190 GetServicesLostConnectionWithPendingRequests) { | 292 GetServicesLostConnectionWithPendingRequests) { |
191 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 293 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
192 .WillRepeatedly(Return(false)); | 294 .WillRepeatedly(Return(false)); |
193 // Client: Sends multiple requests for services. | 295 // Client: Sends multiple requests for services. |
194 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 296 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
195 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | 297 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
196 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 298 EXPECT_EQ(0, actual_callback_count_); |
197 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
198 EXPECT_EQ(0, callback_count_); | |
199 | 299 |
200 // Simulate connection loss. | 300 // Simulate connection loss. |
201 device_.SetConnected(false); | 301 device_.SetConnected(false); |
202 SimulateDeviceChanged(); | 302 SimulateDeviceChanged(); |
203 expect_device_service_deleted_ = true; | 303 expect_device_service_deleted_ = true; |
204 | 304 |
205 // Wait for message pipe to process error. | 305 // Wait for message pipe to process error. |
206 base::RunLoop().RunUntilIdle(); | 306 base::RunLoop().RunUntilIdle(); |
207 } | 307 } |
208 | 308 |
209 TEST_F(BluetoothInterfaceDeviceTest, | 309 TEST_F(BluetoothInterfaceDeviceTest, |
210 GetServicesForcedDisconnectionWithPendingRequests) { | 310 GetServicesForcedDisconnectionWithPendingRequests) { |
211 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | 311 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) |
212 .WillRepeatedly(Return(false)); | 312 .WillRepeatedly(Return(false)); |
213 | 313 |
214 // Client: Sends multiple requests for services. | 314 // Client: Sends multiple requests for services. |
215 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 315 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
216 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | 316 proxy_->GetServices(CheckGetServicesCount(Call::NOT_EXPECTED)); |
217 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | 317 EXPECT_EQ(0, actual_callback_count_); |
218 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
219 EXPECT_EQ(0, callback_count_); | |
220 | 318 |
221 // Simulate connection loss. | 319 // Simulate connection loss. |
222 proxy_->Disconnect(); | 320 proxy_->Disconnect(); |
321 expect_device_service_deleted_ = true; | |
322 | |
323 // Wait for message pipe to process error. | |
324 base::RunLoop().RunUntilIdle(); | |
325 } | |
326 | |
327 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristics) { | |
328 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
329 .WillRepeatedly(Return(true)); | |
330 | |
331 proxy_->GetCharacteristics(kTestServiceId0, | |
332 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
333 | |
334 base::RunLoop().RunUntilIdle(); | |
335 } | |
336 | |
337 TEST_F(BluetoothInterfaceDeviceTest, | |
338 GetCharacteristicsCheckPropertiesService0) { | |
339 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
340 .WillRepeatedly(Return(true)); | |
341 | |
342 std::vector<Properties> properties; | |
343 properties.push_back(kReadWriteProperties); | |
344 properties.push_back(kReadWriteProperties); | |
345 | |
346 proxy_->GetCharacteristics( | |
347 kTestServiceId0, | |
348 CheckGetCharacteristicsProperties(Call::EXPECTED, std::move(properties))); | |
349 | |
350 base::RunLoop().RunUntilIdle(); | |
351 } | |
352 | |
353 TEST_F(BluetoothInterfaceDeviceTest, | |
354 GetCharacteristicsCheckPropertiesService1) { | |
355 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
356 .WillRepeatedly(Return(true)); | |
357 | |
358 std::vector<Properties> properties; | |
359 properties.push_back(kAllProperties); | |
360 | |
361 proxy_->GetCharacteristics( | |
362 kTestServiceId1, | |
363 CheckGetCharacteristicsProperties(Call::EXPECTED, std::move(properties))); | |
364 | |
365 base::RunLoop().RunUntilIdle(); | |
366 } | |
367 | |
368 TEST_F(BluetoothInterfaceDeviceTest, GetCharacteristicsNotDiscovered) { | |
369 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
370 .WillOnce(Return(false)) | |
371 .WillOnce(Return(false)) | |
372 .WillRepeatedly(Return(true)); | |
373 | |
374 // Client: Sends multiple requests for services. | |
scheib
2017/01/14 00:07:14
for characteristics
mbrunson
2017/01/14 01:49:32
Done.
| |
375 proxy_->GetCharacteristics(kTestServiceId0, | |
376 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
377 proxy_->GetCharacteristics(kTestServiceId1, | |
378 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); | |
379 | |
380 base::RunLoop().RunUntilIdle(); | |
381 | |
382 SimulateGattServicesDiscovered(); | |
383 | |
384 // No more GetServices calls will complete. | |
385 SimulateGattServicesDiscovered(); | |
386 | |
387 base::RunLoop().RunUntilIdle(); | |
388 | |
389 // Client: Sends more requests which run immediately. | |
390 proxy_->GetCharacteristics(kTestServiceId0, | |
391 CheckGetCharacteristicsCount(Call::EXPECTED, 2)); | |
392 proxy_->GetCharacteristics(kTestServiceId1, | |
393 CheckGetCharacteristicsCount(Call::EXPECTED, 1)); | |
394 | |
395 base::RunLoop().RunUntilIdle(); | |
396 | |
397 // No more GetCharacteristics calls will complete. | |
398 SimulateGattServicesDiscovered(); | |
399 | |
400 // Wait for message pipe to process error. | |
401 base::RunLoop().RunUntilIdle(); | |
402 } | |
403 | |
404 TEST_F(BluetoothInterfaceDeviceTest, | |
405 GetCharacteristicsLostConnectionWithPendingRequests) { | |
406 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
407 .WillRepeatedly(Return(false)); | |
408 // Client: Sends multiple requests for characteristics. | |
409 proxy_->GetCharacteristics( | |
410 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); | |
411 proxy_->GetCharacteristics( | |
412 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); | |
413 EXPECT_EQ(0, actual_callback_count_); | |
414 | |
415 // Simulate connection loss. | |
416 device_.SetConnected(false); | |
417 SimulateDeviceChanged(); | |
418 expect_device_service_deleted_ = true; | |
419 | |
420 // Wait for message pipe to process error. | |
421 base::RunLoop().RunUntilIdle(); | |
422 } | |
423 | |
424 TEST_F(BluetoothInterfaceDeviceTest, | |
425 GetCharacteristicsForcedDisconnectionWithPendingRequests) { | |
426 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
427 .WillRepeatedly(Return(false)); | |
428 | |
429 // Client: Sends multiple requests for services. | |
430 proxy_->GetCharacteristics( | |
431 kTestServiceId0, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 2)); | |
432 proxy_->GetCharacteristics( | |
433 kTestServiceId1, CheckGetCharacteristicsCount(Call::NOT_EXPECTED, 1)); | |
434 EXPECT_EQ(0, actual_callback_count_); | |
435 | |
436 // Simulate connection loss. | |
437 proxy_->Disconnect(); | |
223 expect_device_service_deleted_ = true; | 438 expect_device_service_deleted_ = true; |
224 | 439 |
225 // Wait for message pipe to process error. | 440 // Wait for message pipe to process error. |
226 base::RunLoop().RunUntilIdle(); | 441 base::RunLoop().RunUntilIdle(); |
227 } | 442 } |
228 | 443 |
229 } // namespace bluetooth | 444 } // namespace bluetooth |
OLD | NEW |