OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 6 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
7 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" | 7 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" |
8 #include "chromeos/dbus/fake_bluetooth_device_client.h" | 8 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
9 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" | 9 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h" |
10 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" | 10 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h" |
11 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" |
12 #include "chromeos/dbus/fake_bluetooth_input_client.h" | 12 #include "chromeos/dbus/fake_bluetooth_input_client.h" |
13 #include "chromeos/dbus/fake_dbus_thread_manager.h" | 13 #include "chromeos/dbus/fake_dbus_thread_manager.h" |
14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
15 #include "device/bluetooth/bluetooth_adapter.h" | 15 #include "device/bluetooth/bluetooth_adapter.h" |
16 #include "device/bluetooth/bluetooth_adapter_factory.h" | 16 #include "device/bluetooth/bluetooth_adapter_factory.h" |
17 #include "device/bluetooth/bluetooth_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
18 #include "device/bluetooth/bluetooth_gatt_service.h" | 19 #include "device/bluetooth/bluetooth_gatt_service.h" |
19 #include "device/bluetooth/bluetooth_uuid.h" | 20 #include "device/bluetooth/bluetooth_uuid.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using device::BluetoothAdapter; | 23 using device::BluetoothAdapter; |
23 using device::BluetoothDevice; | 24 using device::BluetoothDevice; |
| 25 using device::BluetoothGattCharacteristic; |
24 using device::BluetoothGattService; | 26 using device::BluetoothGattService; |
25 using device::BluetoothUUID; | 27 using device::BluetoothUUID; |
26 | 28 |
27 namespace chromeos { | 29 namespace chromeos { |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 class TestObserver : public BluetoothDevice::Observer { | 33 const BluetoothUUID kHeartRateMeasurementUUID( |
| 34 FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID); |
| 35 const BluetoothUUID kBodySensorLocationUUID( |
| 36 FakeBluetoothGattCharacteristicClient::kBodySensorLocationUUID); |
| 37 const BluetoothUUID kHeartRateControlPointUUID( |
| 38 FakeBluetoothGattCharacteristicClient::kHeartRateControlPointUUID); |
| 39 |
| 40 // Compares GATT characteristic/descriptor values. Returns true, if the values |
| 41 // are equal. |
| 42 bool ValuesEqual(const std::vector<uint8>& value0, |
| 43 const std::vector<uint8>& value1) { |
| 44 if (value0.size() != value1.size()) |
| 45 return false; |
| 46 for (size_t i = 0; i < value0.size(); ++i) |
| 47 if (value0[i] != value1[i]) |
| 48 return false; |
| 49 return true; |
| 50 } |
| 51 |
| 52 class TestDeviceObserver : public BluetoothDevice::Observer { |
32 public: | 53 public: |
33 TestObserver(scoped_refptr<BluetoothAdapter> adapter, | 54 TestDeviceObserver(scoped_refptr<BluetoothAdapter> adapter, |
34 BluetoothDevice* device) | 55 BluetoothDevice* device) |
35 : gatt_service_added_count_(0), | 56 : gatt_service_added_count_(0), |
36 gatt_service_removed_count_(0), | 57 gatt_service_removed_count_(0), |
37 device_address_(device->GetAddress()), | 58 device_address_(device->GetAddress()), |
38 adapter_(adapter) { | 59 adapter_(adapter) { |
39 device->AddObserver(this); | 60 device->AddObserver(this); |
40 } | 61 } |
41 | 62 |
42 virtual ~TestObserver() { | 63 virtual ~TestDeviceObserver() { |
43 BluetoothDevice* device = adapter_->GetDevice(device_address_); | 64 BluetoothDevice* device = adapter_->GetDevice(device_address_); |
44 if (device) | 65 if (device) |
45 device->RemoveObserver(this); | 66 device->RemoveObserver(this); |
46 } | 67 } |
47 | 68 |
48 // BluetoothDevice::Observer overrides. | 69 // BluetoothDevice::Observer overrides. |
49 virtual void GattServiceAdded( | 70 virtual void GattServiceAdded( |
50 BluetoothDevice* device, | 71 BluetoothDevice* device, |
51 BluetoothGattService* service) OVERRIDE { | 72 BluetoothGattService* service) OVERRIDE { |
52 EXPECT_EQ(device_address_, device->GetAddress()); | 73 ASSERT_EQ(device_address_, device->GetAddress()); |
53 | 74 |
54 ++gatt_service_added_count_; | 75 ++gatt_service_added_count_; |
55 last_gatt_service_id_ = service->GetIdentifier(); | 76 last_gatt_service_id_ = service->GetIdentifier(); |
56 last_gatt_service_uuid_ = service->GetUUID(); | 77 last_gatt_service_uuid_ = service->GetUUID(); |
57 | 78 |
58 EXPECT_FALSE(service->IsLocal()); | 79 EXPECT_FALSE(service->IsLocal()); |
59 EXPECT_TRUE(service->IsPrimary()); | 80 EXPECT_TRUE(service->IsPrimary()); |
60 | 81 |
| 82 EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service); |
| 83 |
61 QuitMessageLoop(); | 84 QuitMessageLoop(); |
62 } | 85 } |
63 | 86 |
64 virtual void GattServiceRemoved( | 87 virtual void GattServiceRemoved( |
65 BluetoothDevice* device, | 88 BluetoothDevice* device, |
66 BluetoothGattService* service) OVERRIDE { | 89 BluetoothGattService* service) OVERRIDE { |
67 EXPECT_EQ(device_address_, device->GetAddress()); | 90 ASSERT_EQ(device_address_, device->GetAddress()); |
68 | 91 |
69 ++gatt_service_removed_count_; | 92 ++gatt_service_removed_count_; |
70 last_gatt_service_id_ = service->GetIdentifier(); | 93 last_gatt_service_id_ = service->GetIdentifier(); |
71 last_gatt_service_uuid_ = service->GetUUID(); | 94 last_gatt_service_uuid_ = service->GetUUID(); |
72 | 95 |
73 EXPECT_FALSE(service->IsLocal()); | 96 EXPECT_FALSE(service->IsLocal()); |
74 EXPECT_TRUE(service->IsPrimary()); | 97 EXPECT_TRUE(service->IsPrimary()); |
75 | 98 |
| 99 // The device should return NULL for this service. |
| 100 EXPECT_FALSE(device->GetGattService(last_gatt_service_id_)); |
| 101 |
76 QuitMessageLoop(); | 102 QuitMessageLoop(); |
77 } | 103 } |
78 | 104 |
79 int gatt_service_added_count_; | 105 int gatt_service_added_count_; |
80 int gatt_service_removed_count_; | 106 int gatt_service_removed_count_; |
81 std::string last_gatt_service_id_; | 107 std::string last_gatt_service_id_; |
82 BluetoothUUID last_gatt_service_uuid_; | 108 BluetoothUUID last_gatt_service_uuid_; |
83 | 109 |
84 private: | 110 private: |
85 // Some tests use a message loop since background processing is simulated; | 111 // Some tests use a message loop since background processing is simulated; |
86 // break out of those loops. | 112 // break out of those loops. |
87 void QuitMessageLoop() { | 113 void QuitMessageLoop() { |
88 if (base::MessageLoop::current() && | 114 if (base::MessageLoop::current() && |
89 base::MessageLoop::current()->is_running()) | 115 base::MessageLoop::current()->is_running()) |
90 base::MessageLoop::current()->Quit(); | 116 base::MessageLoop::current()->Quit(); |
91 } | 117 } |
92 | 118 |
93 std::string device_address_; | 119 std::string device_address_; |
94 scoped_refptr<BluetoothAdapter> adapter_; | 120 scoped_refptr<BluetoothAdapter> adapter_; |
95 }; | 121 }; |
96 | 122 |
| 123 class TestGattServiceObserver : public BluetoothGattService::Observer { |
| 124 public: |
| 125 TestGattServiceObserver(scoped_refptr<BluetoothAdapter> adapter, |
| 126 BluetoothDevice* device, |
| 127 BluetoothGattService* service) |
| 128 : gatt_service_changed_count_(0), |
| 129 gatt_characteristic_added_count_(0), |
| 130 gatt_characteristic_removed_count_(0), |
| 131 gatt_characteristic_value_changed_count_(0), |
| 132 device_address_(device->GetAddress()), |
| 133 gatt_service_id_(service->GetIdentifier()), |
| 134 adapter_(adapter) { |
| 135 service->AddObserver(this); |
| 136 } |
| 137 |
| 138 virtual ~TestGattServiceObserver() { |
| 139 // See if either the device or the service even exist. |
| 140 BluetoothDevice* device = adapter_->GetDevice(device_address_); |
| 141 if (!device) |
| 142 return; |
| 143 |
| 144 BluetoothGattService* service = device->GetGattService(gatt_service_id_); |
| 145 if (!service) |
| 146 return; |
| 147 |
| 148 service->RemoveObserver(this); |
| 149 } |
| 150 |
| 151 // BluetoothGattService::Observer overrides. |
| 152 virtual void GattServiceChanged(BluetoothGattService* service) OVERRIDE { |
| 153 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); |
| 154 ++gatt_service_changed_count_; |
| 155 |
| 156 QuitMessageLoop(); |
| 157 } |
| 158 |
| 159 virtual void GattCharacteristicAdded( |
| 160 BluetoothGattService* service, |
| 161 BluetoothGattCharacteristic* characteristic) OVERRIDE { |
| 162 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); |
| 163 |
| 164 ++gatt_characteristic_added_count_; |
| 165 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); |
| 166 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); |
| 167 |
| 168 EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_), |
| 169 characteristic); |
| 170 EXPECT_EQ(service, characteristic->GetService()); |
| 171 |
| 172 QuitMessageLoop(); |
| 173 } |
| 174 |
| 175 virtual void GattCharacteristicRemoved( |
| 176 BluetoothGattService* service, |
| 177 BluetoothGattCharacteristic* characteristic) OVERRIDE { |
| 178 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); |
| 179 |
| 180 ++gatt_characteristic_removed_count_; |
| 181 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); |
| 182 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); |
| 183 |
| 184 // The service should return NULL for this characteristic. |
| 185 EXPECT_FALSE(service->GetCharacteristic(last_gatt_characteristic_id_)); |
| 186 EXPECT_EQ(service, characteristic->GetService()); |
| 187 |
| 188 QuitMessageLoop(); |
| 189 } |
| 190 |
| 191 virtual void GattCharacteristicValueChanged( |
| 192 BluetoothGattService* service, |
| 193 BluetoothGattCharacteristic* characteristic, |
| 194 const std::vector<uint8>& value) OVERRIDE { |
| 195 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); |
| 196 |
| 197 ++gatt_characteristic_value_changed_count_; |
| 198 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); |
| 199 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); |
| 200 last_changed_characteristic_value_ = characteristic->GetValue(); |
| 201 |
| 202 EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_), |
| 203 characteristic); |
| 204 |
| 205 QuitMessageLoop(); |
| 206 } |
| 207 |
| 208 int gatt_service_changed_count_; |
| 209 int gatt_characteristic_added_count_; |
| 210 int gatt_characteristic_removed_count_; |
| 211 int gatt_characteristic_value_changed_count_; |
| 212 std::string last_gatt_characteristic_id_; |
| 213 BluetoothUUID last_gatt_characteristic_uuid_; |
| 214 std::vector<uint8> last_changed_characteristic_value_; |
| 215 |
| 216 private: |
| 217 // Some tests use a message loop since background processing is simulated; |
| 218 // break out of those loops. |
| 219 void QuitMessageLoop() { |
| 220 if (base::MessageLoop::current() && |
| 221 base::MessageLoop::current()->is_running()) |
| 222 base::MessageLoop::current()->Quit(); |
| 223 } |
| 224 |
| 225 std::string device_address_; |
| 226 std::string gatt_service_id_; |
| 227 scoped_refptr<BluetoothAdapter> adapter_; |
| 228 }; |
| 229 |
97 } // namespace | 230 } // namespace |
98 | 231 |
99 class BluetoothGattChromeOSTest : public testing::Test { | 232 class BluetoothGattChromeOSTest : public testing::Test { |
100 public: | 233 public: |
101 BluetoothGattChromeOSTest() | 234 BluetoothGattChromeOSTest() |
102 : fake_bluetooth_gatt_service_client_(NULL), | 235 : fake_bluetooth_gatt_service_client_(NULL), |
103 last_gatt_service_(NULL) { | 236 success_callback_count_(0), |
| 237 error_callback_count_(0) { |
104 } | 238 } |
105 | 239 |
106 virtual void SetUp() { | 240 virtual void SetUp() { |
107 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; | 241 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; |
108 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient; | 242 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient; |
109 fake_bluetooth_gatt_service_client_ = | 243 fake_bluetooth_gatt_service_client_ = |
110 new FakeBluetoothGattServiceClient; | 244 new FakeBluetoothGattServiceClient; |
| 245 fake_bluetooth_gatt_characteristic_client_ = |
| 246 new FakeBluetoothGattCharacteristicClient; |
111 fake_dbus_thread_manager->SetBluetoothDeviceClient( | 247 fake_dbus_thread_manager->SetBluetoothDeviceClient( |
112 scoped_ptr<BluetoothDeviceClient>( | 248 scoped_ptr<BluetoothDeviceClient>( |
113 fake_bluetooth_device_client_)); | 249 fake_bluetooth_device_client_)); |
114 fake_dbus_thread_manager->SetBluetoothGattServiceClient( | 250 fake_dbus_thread_manager->SetBluetoothGattServiceClient( |
115 scoped_ptr<BluetoothGattServiceClient>( | 251 scoped_ptr<BluetoothGattServiceClient>( |
116 fake_bluetooth_gatt_service_client_)); | 252 fake_bluetooth_gatt_service_client_)); |
117 fake_dbus_thread_manager->SetBluetoothGattCharacteristicClient( | 253 fake_dbus_thread_manager->SetBluetoothGattCharacteristicClient( |
118 scoped_ptr<BluetoothGattCharacteristicClient>( | 254 scoped_ptr<BluetoothGattCharacteristicClient>( |
119 new FakeBluetoothGattCharacteristicClient)); | 255 fake_bluetooth_gatt_characteristic_client_)); |
120 fake_dbus_thread_manager->SetBluetoothGattDescriptorClient( | 256 fake_dbus_thread_manager->SetBluetoothGattDescriptorClient( |
121 scoped_ptr<BluetoothGattDescriptorClient>( | 257 scoped_ptr<BluetoothGattDescriptorClient>( |
122 new FakeBluetoothGattDescriptorClient)); | 258 new FakeBluetoothGattDescriptorClient)); |
123 fake_dbus_thread_manager->SetBluetoothAdapterClient( | 259 fake_dbus_thread_manager->SetBluetoothAdapterClient( |
124 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient)); | 260 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient)); |
125 fake_dbus_thread_manager->SetBluetoothInputClient( | 261 fake_dbus_thread_manager->SetBluetoothInputClient( |
126 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient)); | 262 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient)); |
127 fake_dbus_thread_manager->SetBluetoothAgentManagerClient( | 263 fake_dbus_thread_manager->SetBluetoothAgentManagerClient( |
128 scoped_ptr<BluetoothAgentManagerClient>( | 264 scoped_ptr<BluetoothAgentManagerClient>( |
129 new FakeBluetoothAgentManagerClient)); | 265 new FakeBluetoothAgentManagerClient)); |
(...skipping 15 matching lines...) Expand all Loading... |
145 | 281 |
146 virtual void TearDown() { | 282 virtual void TearDown() { |
147 adapter_ = NULL; | 283 adapter_ = NULL; |
148 DBusThreadManager::Shutdown(); | 284 DBusThreadManager::Shutdown(); |
149 } | 285 } |
150 | 286 |
151 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | 287 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { |
152 adapter_ = adapter; | 288 adapter_ = adapter; |
153 } | 289 } |
154 | 290 |
| 291 void SuccessCallback() { |
| 292 ++success_callback_count_; |
| 293 } |
| 294 |
| 295 void ValueCallback(const std::vector<uint8>& value) { |
| 296 ++success_callback_count_; |
| 297 last_read_value_ = value; |
| 298 } |
| 299 |
| 300 void ErrorCallback() { |
| 301 ++error_callback_count_; |
| 302 } |
| 303 |
155 protected: | 304 protected: |
156 base::MessageLoop message_loop_; | 305 base::MessageLoop message_loop_; |
157 | 306 |
158 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; | 307 FakeBluetoothDeviceClient* fake_bluetooth_device_client_; |
159 FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; | 308 FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_; |
| 309 FakeBluetoothGattCharacteristicClient* |
| 310 fake_bluetooth_gatt_characteristic_client_; |
160 scoped_refptr<BluetoothAdapter> adapter_; | 311 scoped_refptr<BluetoothAdapter> adapter_; |
161 | 312 |
162 BluetoothGattService* last_gatt_service_; | 313 int success_callback_count_; |
| 314 int error_callback_count_; |
| 315 std::vector<uint8> last_read_value_; |
163 }; | 316 }; |
164 | 317 |
165 TEST_F(BluetoothGattChromeOSTest, GattServiceAdded) { | 318 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { |
166 // Create a fake LE device. We store the device pointer here because this is a | 319 // Create a fake LE device. We store the device pointer here because this is a |
167 // test. It's unsafe to do this in production as the device might get deleted. | 320 // test. It's unsafe to do this in production as the device might get deleted. |
168 fake_bluetooth_device_client_->CreateDevice( | 321 fake_bluetooth_device_client_->CreateDevice( |
169 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), | 322 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
170 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); | 323 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
171 BluetoothDevice* device = adapter_->GetDevice( | 324 BluetoothDevice* device = adapter_->GetDevice( |
172 FakeBluetoothDeviceClient::kLowEnergyAddress); | 325 FakeBluetoothDeviceClient::kLowEnergyAddress); |
173 ASSERT_TRUE(device); | 326 ASSERT_TRUE(device); |
174 | 327 |
175 TestObserver observer(adapter_, device); | 328 TestDeviceObserver observer(adapter_, device); |
176 EXPECT_EQ(0, observer.gatt_service_added_count_); | 329 EXPECT_EQ(0, observer.gatt_service_added_count_); |
177 EXPECT_EQ(0, observer.gatt_service_removed_count_); | 330 EXPECT_EQ(0, observer.gatt_service_removed_count_); |
178 EXPECT_TRUE(observer.last_gatt_service_id_.empty()); | 331 EXPECT_TRUE(observer.last_gatt_service_id_.empty()); |
179 EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid()); | 332 EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid()); |
| 333 EXPECT_TRUE(device->GetGattServices().empty()); |
180 | 334 |
181 // Expose the fake Heart Rate Service. | 335 // Expose the fake Heart Rate Service. |
182 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( | 336 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( |
183 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); | 337 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
184 EXPECT_EQ(1, observer.gatt_service_added_count_); | 338 EXPECT_EQ(1, observer.gatt_service_added_count_); |
185 EXPECT_EQ(0, observer.gatt_service_removed_count_); | 339 EXPECT_EQ(0, observer.gatt_service_removed_count_); |
186 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); | 340 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); |
187 EXPECT_EQ(1U, device->GetGattServices().size()); | 341 EXPECT_EQ(1U, device->GetGattServices().size()); |
188 EXPECT_EQ( | 342 EXPECT_EQ( |
189 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), | 343 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), |
190 observer.last_gatt_service_uuid_); | 344 observer.last_gatt_service_uuid_); |
191 | 345 |
192 BluetoothGattService* service = | 346 BluetoothGattService* service = |
193 device->GetGattService(observer.last_gatt_service_id_); | 347 device->GetGattService(observer.last_gatt_service_id_); |
194 EXPECT_FALSE(service->IsLocal()); | 348 EXPECT_FALSE(service->IsLocal()); |
195 EXPECT_TRUE(service->IsPrimary()); | 349 EXPECT_TRUE(service->IsPrimary()); |
| 350 EXPECT_EQ(service, device->GetGattServices()[0]); |
196 | 351 |
197 EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); | 352 EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); |
198 | 353 |
199 // Hide the service. | 354 // Hide the service. |
200 observer.last_gatt_service_uuid_ = BluetoothUUID(); | 355 observer.last_gatt_service_uuid_ = BluetoothUUID(); |
201 observer.last_gatt_service_id_.clear(); | 356 observer.last_gatt_service_id_.clear(); |
202 fake_bluetooth_gatt_service_client_->HideHeartRateService(); | 357 fake_bluetooth_gatt_service_client_->HideHeartRateService(); |
203 | 358 |
204 EXPECT_EQ(1, observer.gatt_service_added_count_); | 359 EXPECT_EQ(1, observer.gatt_service_added_count_); |
205 EXPECT_EQ(1, observer.gatt_service_removed_count_); | 360 EXPECT_EQ(1, observer.gatt_service_removed_count_); |
(...skipping 11 matching lines...) Expand all Loading... |
217 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( | 372 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( |
218 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); | 373 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
219 EXPECT_EQ(2, observer.gatt_service_added_count_); | 374 EXPECT_EQ(2, observer.gatt_service_added_count_); |
220 EXPECT_EQ(1, observer.gatt_service_removed_count_); | 375 EXPECT_EQ(1, observer.gatt_service_removed_count_); |
221 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); | 376 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); |
222 EXPECT_EQ(1U, device->GetGattServices().size()); | 377 EXPECT_EQ(1U, device->GetGattServices().size()); |
223 EXPECT_EQ( | 378 EXPECT_EQ( |
224 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), | 379 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), |
225 observer.last_gatt_service_uuid_); | 380 observer.last_gatt_service_uuid_); |
226 | 381 |
| 382 // The object |service| points to should have been deallocated. |device| |
| 383 // should contain a brand new instance. |
227 service = device->GetGattService(observer.last_gatt_service_id_); | 384 service = device->GetGattService(observer.last_gatt_service_id_); |
| 385 EXPECT_EQ(service, device->GetGattServices()[0]); |
228 EXPECT_FALSE(service->IsLocal()); | 386 EXPECT_FALSE(service->IsLocal()); |
229 EXPECT_TRUE(service->IsPrimary()); | 387 EXPECT_TRUE(service->IsPrimary()); |
230 | 388 |
231 EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); | 389 EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); |
232 | 390 |
233 // Remove the device. The observer should be notified of the removed service. | 391 // Remove the device. The observer should be notified of the removed service. |
234 // |device| becomes invalid after this. | 392 // |device| becomes invalid after this. |
235 observer.last_gatt_service_uuid_ = BluetoothUUID(); | 393 observer.last_gatt_service_uuid_ = BluetoothUUID(); |
236 observer.last_gatt_service_id_.clear(); | 394 observer.last_gatt_service_id_.clear(); |
237 fake_bluetooth_device_client_->RemoveDevice( | 395 fake_bluetooth_device_client_->RemoveDevice( |
238 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), | 396 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
239 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); | 397 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
240 | 398 |
241 EXPECT_EQ(2, observer.gatt_service_added_count_); | 399 EXPECT_EQ(2, observer.gatt_service_added_count_); |
242 EXPECT_EQ(2, observer.gatt_service_removed_count_); | 400 EXPECT_EQ(2, observer.gatt_service_removed_count_); |
243 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); | 401 EXPECT_FALSE(observer.last_gatt_service_id_.empty()); |
244 EXPECT_EQ( | 402 EXPECT_EQ( |
245 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), | 403 BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), |
246 observer.last_gatt_service_uuid_); | 404 observer.last_gatt_service_uuid_); |
247 EXPECT_EQ( | 405 EXPECT_EQ( |
248 NULL, adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress)); | 406 NULL, adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress)); |
249 } | 407 } |
250 | 408 |
| 409 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) { |
| 410 fake_bluetooth_device_client_->CreateDevice( |
| 411 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
| 412 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 413 BluetoothDevice* device = adapter_->GetDevice( |
| 414 FakeBluetoothDeviceClient::kLowEnergyAddress); |
| 415 ASSERT_TRUE(device); |
| 416 |
| 417 TestDeviceObserver observer(adapter_, device); |
| 418 |
| 419 // Expose the fake Heart Rate service. This will asynchronously expose |
| 420 // characteristics. |
| 421 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( |
| 422 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 423 ASSERT_EQ(1, observer.gatt_service_added_count_); |
| 424 |
| 425 BluetoothGattService* service = |
| 426 device->GetGattService(observer.last_gatt_service_id_); |
| 427 |
| 428 TestGattServiceObserver service_observer(adapter_, device, service); |
| 429 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); |
| 430 EXPECT_EQ(0, service_observer.gatt_characteristic_added_count_); |
| 431 EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_); |
| 432 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); |
| 433 EXPECT_TRUE(service->GetCharacteristics().empty()); |
| 434 |
| 435 // Run the message loop so that the characteristics appear. |
| 436 base::MessageLoop::current()->Run(); |
| 437 |
| 438 // 3 characteristics should appear. Only 1 of the characteristics sends |
| 439 // value changed signals. |
| 440 EXPECT_EQ(3, service_observer.gatt_service_changed_count_); |
| 441 EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_); |
| 442 EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_); |
| 443 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); |
| 444 EXPECT_EQ(3U, service->GetCharacteristics().size()); |
| 445 |
| 446 // Hide the characteristics. 3 removed signals should be received. |
| 447 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics(); |
| 448 EXPECT_EQ(6, service_observer.gatt_service_changed_count_); |
| 449 EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_); |
| 450 EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_); |
| 451 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); |
| 452 EXPECT_TRUE(service->GetCharacteristics().empty()); |
| 453 |
| 454 // Re-expose the heart rate characteristics. |
| 455 fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics( |
| 456 fake_bluetooth_gatt_service_client_->GetHeartRateServicePath()); |
| 457 EXPECT_EQ(9, service_observer.gatt_service_changed_count_); |
| 458 EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_); |
| 459 EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_); |
| 460 EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_); |
| 461 EXPECT_EQ(3U, service->GetCharacteristics().size()); |
| 462 |
| 463 // Hide the service. All characteristics should disappear. |
| 464 fake_bluetooth_gatt_service_client_->HideHeartRateService(); |
| 465 EXPECT_EQ(12, service_observer.gatt_service_changed_count_); |
| 466 EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_); |
| 467 EXPECT_EQ(6, service_observer.gatt_characteristic_removed_count_); |
| 468 EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_); |
| 469 } |
| 470 |
| 471 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { |
| 472 fake_bluetooth_device_client_->CreateDevice( |
| 473 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), |
| 474 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 475 BluetoothDevice* device = adapter_->GetDevice( |
| 476 FakeBluetoothDeviceClient::kLowEnergyAddress); |
| 477 ASSERT_TRUE(device); |
| 478 |
| 479 TestDeviceObserver observer(adapter_, device); |
| 480 |
| 481 // Expose the fake Heart Rate service. This will asynchronously expose |
| 482 // characteristics. |
| 483 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( |
| 484 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); |
| 485 ASSERT_EQ(1, observer.gatt_service_added_count_); |
| 486 |
| 487 BluetoothGattService* service = |
| 488 device->GetGattService(observer.last_gatt_service_id_); |
| 489 |
| 490 TestGattServiceObserver service_observer(adapter_, device, service); |
| 491 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); |
| 492 |
| 493 // Run the message loop so that the characteristics appear. |
| 494 base::MessageLoop::current()->Run(); |
| 495 |
| 496 // We should get an initial value changed signal from the Heart Rate |
| 497 // Measurement characteristic when it getsadded. |
| 498 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); |
| 499 |
| 500 // The Heart Rate Measurement characteristic should send regular |
| 501 // notifications. |
| 502 base::MessageLoop::current()->Run(); |
| 503 EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_); |
| 504 EXPECT_EQ(kHeartRateMeasurementUUID, |
| 505 service_observer.last_gatt_characteristic_uuid_); |
| 506 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 507 GetHeartRateMeasurementPath().value(), |
| 508 service_observer.last_gatt_characteristic_id_); |
| 509 |
| 510 // Receive another notification. |
| 511 service_observer.last_gatt_characteristic_id_.clear(); |
| 512 service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID(); |
| 513 base::MessageLoop::current()->Run(); |
| 514 EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_); |
| 515 EXPECT_EQ(kHeartRateMeasurementUUID, |
| 516 service_observer.last_gatt_characteristic_uuid_); |
| 517 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 518 GetHeartRateMeasurementPath().value(), |
| 519 service_observer.last_gatt_characteristic_id_); |
| 520 |
| 521 // Issue write request to non-writeable characteristics. |
| 522 service_observer.last_gatt_characteristic_id_.clear(); |
| 523 service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID(); |
| 524 |
| 525 std::vector<uint8> write_value; |
| 526 write_value.push_back(0x01); |
| 527 BluetoothGattCharacteristic* characteristic = |
| 528 service->GetCharacteristic(fake_bluetooth_gatt_characteristic_client_-> |
| 529 GetHeartRateMeasurementPath().value()); |
| 530 ASSERT_TRUE(characteristic); |
| 531 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 532 GetHeartRateMeasurementPath().value(), |
| 533 characteristic->GetIdentifier()); |
| 534 EXPECT_EQ(kHeartRateMeasurementUUID, characteristic->GetUUID()); |
| 535 characteristic->WriteRemoteCharacteristic( |
| 536 write_value, |
| 537 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, |
| 538 base::Unretained(this)), |
| 539 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, |
| 540 base::Unretained(this))); |
| 541 EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty()); |
| 542 EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid()); |
| 543 EXPECT_EQ(0, success_callback_count_); |
| 544 EXPECT_EQ(1, error_callback_count_); |
| 545 EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_); |
| 546 |
| 547 characteristic = service->GetCharacteristic( |
| 548 fake_bluetooth_gatt_characteristic_client_-> |
| 549 GetBodySensorLocationPath().value()); |
| 550 ASSERT_TRUE(characteristic); |
| 551 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 552 GetBodySensorLocationPath().value(), |
| 553 characteristic->GetIdentifier()); |
| 554 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); |
| 555 characteristic->WriteRemoteCharacteristic( |
| 556 write_value, |
| 557 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, |
| 558 base::Unretained(this)), |
| 559 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, |
| 560 base::Unretained(this))); |
| 561 EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty()); |
| 562 EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid()); |
| 563 EXPECT_EQ(0, success_callback_count_); |
| 564 EXPECT_EQ(2, error_callback_count_); |
| 565 EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_); |
| 566 |
| 567 // Issue write request to writeable characteristic. Writing "1" to the control |
| 568 // point characteristic will immediately change its value back to "0", hence |
| 569 // sending "ValueChanged" events twice. |
| 570 characteristic = service->GetCharacteristic( |
| 571 fake_bluetooth_gatt_characteristic_client_-> |
| 572 GetHeartRateControlPointPath().value()); |
| 573 ASSERT_TRUE(characteristic); |
| 574 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 575 GetHeartRateControlPointPath().value(), |
| 576 characteristic->GetIdentifier()); |
| 577 EXPECT_EQ(kHeartRateControlPointUUID, characteristic->GetUUID()); |
| 578 characteristic->WriteRemoteCharacteristic( |
| 579 write_value, |
| 580 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, |
| 581 base::Unretained(this)), |
| 582 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, |
| 583 base::Unretained(this))); |
| 584 EXPECT_EQ(characteristic->GetIdentifier(), |
| 585 service_observer.last_gatt_characteristic_id_); |
| 586 EXPECT_EQ(characteristic->GetUUID(), |
| 587 service_observer.last_gatt_characteristic_uuid_); |
| 588 EXPECT_EQ(1, success_callback_count_); |
| 589 EXPECT_EQ(2, error_callback_count_); |
| 590 EXPECT_EQ(5, service_observer.gatt_characteristic_value_changed_count_); |
| 591 |
| 592 // Issue a read request. |
| 593 characteristic = service->GetCharacteristic( |
| 594 fake_bluetooth_gatt_characteristic_client_-> |
| 595 GetBodySensorLocationPath().value()); |
| 596 ASSERT_TRUE(characteristic); |
| 597 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 598 GetBodySensorLocationPath().value(), |
| 599 characteristic->GetIdentifier()); |
| 600 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); |
| 601 characteristic->ReadRemoteCharacteristic( |
| 602 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, |
| 603 base::Unretained(this)), |
| 604 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, |
| 605 base::Unretained(this))); |
| 606 EXPECT_EQ(2, success_callback_count_); |
| 607 EXPECT_EQ(2, error_callback_count_); |
| 608 EXPECT_EQ(5, service_observer.gatt_characteristic_value_changed_count_); |
| 609 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_)); |
| 610 |
| 611 // One last value changed notification. |
| 612 base::MessageLoop::current()->Run(); |
| 613 EXPECT_EQ(6, service_observer.gatt_characteristic_value_changed_count_); |
| 614 EXPECT_EQ(kHeartRateMeasurementUUID, |
| 615 service_observer.last_gatt_characteristic_uuid_); |
| 616 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> |
| 617 GetHeartRateMeasurementPath().value(), |
| 618 service_observer.last_gatt_characteristic_id_); |
| 619 } |
| 620 |
251 } // namespace chromeos | 621 } // namespace chromeos |
OLD | NEW |