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

Side by Side Diff: device/bluetooth/bluetooth_gatt_chromeos_unittest.cc

Issue 418483003: device/bluetooth: Move GATT observer methods to BluetoothAdapter::Observer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
« no previous file with comments | « device/bluetooth/bluetooth_device_win.cc ('k') | device/bluetooth/bluetooth_gatt_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/scoped_vector.h" 5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" 8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" 9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h" 10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 bool ValuesEqual(const std::vector<uint8>& value0, 50 bool ValuesEqual(const std::vector<uint8>& value0,
51 const std::vector<uint8>& value1) { 51 const std::vector<uint8>& value1) {
52 if (value0.size() != value1.size()) 52 if (value0.size() != value1.size())
53 return false; 53 return false;
54 for (size_t i = 0; i < value0.size(); ++i) 54 for (size_t i = 0; i < value0.size(); ++i)
55 if (value0[i] != value1[i]) 55 if (value0[i] != value1[i])
56 return false; 56 return false;
57 return true; 57 return true;
58 } 58 }
59 59
60 class TestDeviceObserver : public BluetoothDevice::Observer { 60 class TestObserver : public BluetoothAdapter::Observer {
61 public: 61 public:
62 TestDeviceObserver(scoped_refptr<BluetoothAdapter> adapter, 62 TestObserver(scoped_refptr<BluetoothAdapter> adapter)
63 BluetoothDevice* device)
64 : gatt_service_added_count_(0), 63 : gatt_service_added_count_(0),
65 gatt_service_removed_count_(0), 64 gatt_service_removed_count_(0),
66 device_address_(device->GetAddress()), 65 gatt_service_changed_count_(0),
66 gatt_discovery_complete_count_(0),
67 gatt_characteristic_added_count_(0),
68 gatt_characteristic_removed_count_(0),
69 gatt_characteristic_value_changed_count_(0),
70 gatt_descriptor_added_count_(0),
71 gatt_descriptor_removed_count_(0),
72 gatt_descriptor_value_changed_count_(0),
67 adapter_(adapter) { 73 adapter_(adapter) {
68 device->AddObserver(this); 74 adapter_->AddObserver(this);
69 } 75 }
70 76
71 virtual ~TestDeviceObserver() { 77 virtual ~TestObserver() {
72 BluetoothDevice* device = adapter_->GetDevice(device_address_); 78 adapter_->RemoveObserver(this);
73 if (device)
74 device->RemoveObserver(this);
75 } 79 }
76 80
77 // BluetoothDevice::Observer overrides. 81 // BluetoothAdapter::Observer overrides.
78 virtual void GattServiceAdded( 82 virtual void GattServiceAdded(BluetoothAdapter* adapter,
79 BluetoothDevice* device, 83 BluetoothDevice* device,
80 BluetoothGattService* service) OVERRIDE { 84 BluetoothGattService* service) OVERRIDE {
81 ASSERT_EQ(device_address_, device->GetAddress()); 85 ASSERT_EQ(adapter_.get(), adapter);
86 ASSERT_EQ(service->GetDevice(), device);
82 87
83 ++gatt_service_added_count_; 88 ++gatt_service_added_count_;
84 last_gatt_service_id_ = service->GetIdentifier(); 89 last_gatt_service_id_ = service->GetIdentifier();
85 last_gatt_service_uuid_ = service->GetUUID(); 90 last_gatt_service_uuid_ = service->GetUUID();
86 91
87 EXPECT_FALSE(service->IsLocal()); 92 EXPECT_FALSE(service->IsLocal());
88 EXPECT_TRUE(service->IsPrimary()); 93 EXPECT_TRUE(service->IsPrimary());
89 94
90 EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service); 95 EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service);
91 96
92 QuitMessageLoop(); 97 QuitMessageLoop();
93 } 98 }
94 99
95 virtual void GattServiceRemoved( 100 virtual void GattServiceRemoved(BluetoothAdapter* adapter,
96 BluetoothDevice* device, 101 BluetoothDevice* device,
97 BluetoothGattService* service) OVERRIDE { 102 BluetoothGattService* service) OVERRIDE {
98 ASSERT_EQ(device_address_, device->GetAddress()); 103 ASSERT_EQ(adapter_.get(), adapter);
104 ASSERT_EQ(service->GetDevice(), device);
99 105
100 ++gatt_service_removed_count_; 106 ++gatt_service_removed_count_;
101 last_gatt_service_id_ = service->GetIdentifier(); 107 last_gatt_service_id_ = service->GetIdentifier();
102 last_gatt_service_uuid_ = service->GetUUID(); 108 last_gatt_service_uuid_ = service->GetUUID();
103 109
104 EXPECT_FALSE(service->IsLocal()); 110 EXPECT_FALSE(service->IsLocal());
105 EXPECT_TRUE(service->IsPrimary()); 111 EXPECT_TRUE(service->IsPrimary());
106 112
107 // The device should return NULL for this service. 113 // The device should return NULL for this service.
108 EXPECT_FALSE(device->GetGattService(last_gatt_service_id_)); 114 EXPECT_FALSE(device->GetGattService(last_gatt_service_id_));
109 115
110 QuitMessageLoop(); 116 QuitMessageLoop();
111 } 117 }
112 118
113 int gatt_service_added_count_;
114 int gatt_service_removed_count_;
115 std::string last_gatt_service_id_;
116 BluetoothUUID last_gatt_service_uuid_;
117
118 private:
119 // Some tests use a message loop since background processing is simulated;
120 // break out of those loops.
121 void QuitMessageLoop() {
122 if (base::MessageLoop::current() &&
123 base::MessageLoop::current()->is_running())
124 base::MessageLoop::current()->Quit();
125 }
126
127 std::string device_address_;
128 scoped_refptr<BluetoothAdapter> adapter_;
129 };
130
131 class TestGattServiceObserver : public BluetoothGattService::Observer {
132 public:
133 TestGattServiceObserver(scoped_refptr<BluetoothAdapter> adapter,
134 BluetoothDevice* device,
135 BluetoothGattService* service)
136 : gatt_service_changed_count_(0),
137 gatt_discovery_complete_count_(0),
138 gatt_characteristic_added_count_(0),
139 gatt_characteristic_removed_count_(0),
140 gatt_characteristic_value_changed_count_(0),
141 gatt_descriptor_added_count_(0),
142 gatt_descriptor_removed_count_(0),
143 gatt_descriptor_value_changed_count_(0),
144 device_address_(device->GetAddress()),
145 gatt_service_id_(service->GetIdentifier()),
146 adapter_(adapter) {
147 service->AddObserver(this);
148 }
149
150 virtual ~TestGattServiceObserver() {
151 // See if either the device or the service even exist.
152 BluetoothDevice* device = adapter_->GetDevice(device_address_);
153 if (!device)
154 return;
155
156 BluetoothGattService* service = device->GetGattService(gatt_service_id_);
157 if (!service)
158 return;
159
160 service->RemoveObserver(this);
161 }
162
163 // BluetoothGattService::Observer overrides.
164 virtual void GattDiscoveryCompleteForService( 119 virtual void GattDiscoveryCompleteForService(
120 BluetoothAdapter* adapter,
165 BluetoothGattService* service) OVERRIDE { 121 BluetoothGattService* service) OVERRIDE {
166 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); 122 ASSERT_EQ(adapter_.get(), adapter);
167 ++gatt_discovery_complete_count_; 123 ++gatt_discovery_complete_count_;
168 124
169 QuitMessageLoop(); 125 QuitMessageLoop();
170 } 126 }
171 127
172 virtual void GattServiceChanged(BluetoothGattService* service) OVERRIDE { 128 virtual void GattServiceChanged(BluetoothAdapter* adapter,
173 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); 129 BluetoothGattService* service) OVERRIDE {
130 ASSERT_EQ(adapter_.get(), adapter);
174 ++gatt_service_changed_count_; 131 ++gatt_service_changed_count_;
175 132
176 QuitMessageLoop(); 133 QuitMessageLoop();
177 } 134 }
178 135
179 virtual void GattCharacteristicAdded( 136 virtual void GattCharacteristicAdded(
180 BluetoothGattService* service, 137 BluetoothAdapter* adapter,
181 BluetoothGattCharacteristic* characteristic) OVERRIDE { 138 BluetoothGattCharacteristic* characteristic) OVERRIDE {
182 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); 139 ASSERT_EQ(adapter_.get(), adapter);
183 140
184 ++gatt_characteristic_added_count_; 141 ++gatt_characteristic_added_count_;
185 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); 142 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
186 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); 143 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
187 144
188 EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_), 145 ASSERT_TRUE(characteristic->GetService());
146 EXPECT_EQ(characteristic->GetService()->GetCharacteristic(
147 last_gatt_characteristic_id_),
189 characteristic); 148 characteristic);
190 EXPECT_EQ(service, characteristic->GetService());
191 149
192 QuitMessageLoop(); 150 QuitMessageLoop();
193 } 151 }
194 152
195 virtual void GattCharacteristicRemoved( 153 virtual void GattCharacteristicRemoved(
196 BluetoothGattService* service, 154 BluetoothAdapter* adapter,
197 BluetoothGattCharacteristic* characteristic) OVERRIDE { 155 BluetoothGattCharacteristic* characteristic) OVERRIDE {
198 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); 156 ASSERT_EQ(adapter_.get(), adapter);
199 157
200 ++gatt_characteristic_removed_count_; 158 ++gatt_characteristic_removed_count_;
201 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); 159 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
202 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); 160 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
203 161
204 // The service should return NULL for this characteristic. 162 // The service should return NULL for this characteristic.
205 EXPECT_FALSE(service->GetCharacteristic(last_gatt_characteristic_id_)); 163 ASSERT_TRUE(characteristic->GetService());
206 EXPECT_EQ(service, characteristic->GetService()); 164 EXPECT_FALSE(characteristic->GetService()->GetCharacteristic(
165 last_gatt_characteristic_id_));
207 166
208 QuitMessageLoop(); 167 QuitMessageLoop();
209 } 168 }
210 169
211 virtual void GattCharacteristicValueChanged( 170 virtual void GattCharacteristicValueChanged(
212 BluetoothGattService* service, 171 BluetoothAdapter* adapter,
213 BluetoothGattCharacteristic* characteristic, 172 BluetoothGattCharacteristic* characteristic,
214 const std::vector<uint8>& value) OVERRIDE { 173 const std::vector<uint8>& value) OVERRIDE {
215 ASSERT_EQ(gatt_service_id_, service->GetIdentifier()); 174 ASSERT_EQ(adapter_.get(), adapter);
216 175
217 ++gatt_characteristic_value_changed_count_; 176 ++gatt_characteristic_value_changed_count_;
218 last_gatt_characteristic_id_ = characteristic->GetIdentifier(); 177 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
219 last_gatt_characteristic_uuid_ = characteristic->GetUUID(); 178 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
220 last_changed_characteristic_value_ = value; 179 last_changed_characteristic_value_ = value;
221 180
222 EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_), 181 ASSERT_TRUE(characteristic->GetService());
182 EXPECT_EQ(characteristic->GetService()->GetCharacteristic(
183 last_gatt_characteristic_id_),
223 characteristic); 184 characteristic);
224 EXPECT_EQ(service, characteristic->GetService());
225 185
226 QuitMessageLoop(); 186 QuitMessageLoop();
227 } 187 }
228 188
229 virtual void GattDescriptorAdded( 189 virtual void GattDescriptorAdded(
230 BluetoothGattCharacteristic* characteristic, 190 BluetoothAdapter* adapter,
231 BluetoothGattDescriptor* descriptor) OVERRIDE { 191 BluetoothGattDescriptor* descriptor) OVERRIDE {
232 ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier()); 192 ASSERT_EQ(adapter_.get(), adapter);
233 193
234 ++gatt_descriptor_added_count_; 194 ++gatt_descriptor_added_count_;
235 last_gatt_descriptor_id_ = descriptor->GetIdentifier(); 195 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
236 last_gatt_descriptor_uuid_ = descriptor->GetUUID(); 196 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
237 197
238 EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_), 198 ASSERT_TRUE(descriptor->GetCharacteristic());
199 EXPECT_EQ(descriptor->GetCharacteristic()->GetDescriptor(
200 last_gatt_descriptor_id_),
239 descriptor); 201 descriptor);
240 EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
241 202
242 QuitMessageLoop(); 203 QuitMessageLoop();
243 } 204 }
244 205
245 virtual void GattDescriptorRemoved( 206 virtual void GattDescriptorRemoved(
246 BluetoothGattCharacteristic* characteristic, 207 BluetoothAdapter* adapter,
247 BluetoothGattDescriptor* descriptor) OVERRIDE { 208 BluetoothGattDescriptor* descriptor) OVERRIDE {
248 ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier()); 209 ASSERT_EQ(adapter_.get(), adapter);
249 210
250 ++gatt_descriptor_removed_count_; 211 ++gatt_descriptor_removed_count_;
251 last_gatt_descriptor_id_ = descriptor->GetIdentifier(); 212 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
252 last_gatt_descriptor_uuid_ = descriptor->GetUUID(); 213 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
253 214
254 // The characteristic should return NULL for this descriptor.. 215 // The characteristic should return NULL for this descriptor..
255 EXPECT_FALSE(characteristic->GetDescriptor(last_gatt_descriptor_id_)); 216 ASSERT_TRUE(descriptor->GetCharacteristic());
256 EXPECT_EQ(characteristic, descriptor->GetCharacteristic()); 217 EXPECT_FALSE(descriptor->GetCharacteristic()->GetDescriptor(
218 last_gatt_descriptor_id_));
257 219
258 QuitMessageLoop(); 220 QuitMessageLoop();
259 } 221 }
260 222
261 virtual void GattDescriptorValueChanged( 223 virtual void GattDescriptorValueChanged(
262 BluetoothGattCharacteristic* characteristic, 224 BluetoothAdapter* adapter,
263 BluetoothGattDescriptor* descriptor, 225 BluetoothGattDescriptor* descriptor,
264 const std::vector<uint8>& value) OVERRIDE { 226 const std::vector<uint8>& value) OVERRIDE {
265 ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier()); 227 ASSERT_EQ(adapter_.get(), adapter);
266 228
267 ++gatt_descriptor_value_changed_count_; 229 ++gatt_descriptor_value_changed_count_;
268 last_gatt_descriptor_id_ = descriptor->GetIdentifier(); 230 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
269 last_gatt_descriptor_uuid_ = descriptor->GetUUID(); 231 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
270 last_changed_descriptor_value_ = value; 232 last_changed_descriptor_value_ = value;
271 233
272 EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_), 234 ASSERT_TRUE(descriptor->GetCharacteristic());
235 EXPECT_EQ(descriptor->GetCharacteristic()->GetDescriptor(
236 last_gatt_descriptor_id_),
273 descriptor); 237 descriptor);
274 EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
275 238
276 QuitMessageLoop(); 239 QuitMessageLoop();
277 } 240 }
278 241
242 int gatt_service_added_count_;
243 int gatt_service_removed_count_;
279 int gatt_service_changed_count_; 244 int gatt_service_changed_count_;
280 int gatt_discovery_complete_count_; 245 int gatt_discovery_complete_count_;
281 int gatt_characteristic_added_count_; 246 int gatt_characteristic_added_count_;
282 int gatt_characteristic_removed_count_; 247 int gatt_characteristic_removed_count_;
283 int gatt_characteristic_value_changed_count_; 248 int gatt_characteristic_value_changed_count_;
284 int gatt_descriptor_added_count_; 249 int gatt_descriptor_added_count_;
285 int gatt_descriptor_removed_count_; 250 int gatt_descriptor_removed_count_;
286 int gatt_descriptor_value_changed_count_; 251 int gatt_descriptor_value_changed_count_;
252 std::string last_gatt_service_id_;
253 BluetoothUUID last_gatt_service_uuid_;
287 std::string last_gatt_characteristic_id_; 254 std::string last_gatt_characteristic_id_;
288 BluetoothUUID last_gatt_characteristic_uuid_; 255 BluetoothUUID last_gatt_characteristic_uuid_;
289 std::vector<uint8> last_changed_characteristic_value_; 256 std::vector<uint8> last_changed_characteristic_value_;
290 std::string last_gatt_descriptor_id_; 257 std::string last_gatt_descriptor_id_;
291 BluetoothUUID last_gatt_descriptor_uuid_; 258 BluetoothUUID last_gatt_descriptor_uuid_;
292 std::vector<uint8> last_changed_descriptor_value_; 259 std::vector<uint8> last_changed_descriptor_value_;
293 260
294 private: 261 private:
295 // Some tests use a message loop since background processing is simulated; 262 // Some tests use a message loop since background processing is simulated;
296 // break out of those loops. 263 // break out of those loops.
297 void QuitMessageLoop() { 264 void QuitMessageLoop() {
298 if (base::MessageLoop::current() && 265 if (base::MessageLoop::current() &&
299 base::MessageLoop::current()->is_running()) 266 base::MessageLoop::current()->is_running())
300 base::MessageLoop::current()->Quit(); 267 base::MessageLoop::current()->Quit();
301 } 268 }
302 269
303 std::string device_address_;
304 std::string gatt_service_id_;
305 scoped_refptr<BluetoothAdapter> adapter_; 270 scoped_refptr<BluetoothAdapter> adapter_;
306 }; 271 };
307 272
308 } // namespace 273 } // namespace
309 274
310 class BluetoothGattChromeOSTest : public testing::Test { 275 class BluetoothGattChromeOSTest : public testing::Test {
311 public: 276 public:
312 BluetoothGattChromeOSTest() 277 BluetoothGattChromeOSTest()
313 : fake_bluetooth_gatt_service_client_(NULL), 278 : fake_bluetooth_gatt_service_client_(NULL),
314 success_callback_count_(0), 279 success_callback_count_(0),
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { 473 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) {
509 // Create a fake LE device. We store the device pointer here because this is a 474 // Create a fake LE device. We store the device pointer here because this is a
510 // test. It's unsafe to do this in production as the device might get deleted. 475 // test. It's unsafe to do this in production as the device might get deleted.
511 fake_bluetooth_device_client_->CreateDevice( 476 fake_bluetooth_device_client_->CreateDevice(
512 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 477 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
513 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 478 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
514 BluetoothDevice* device = adapter_->GetDevice( 479 BluetoothDevice* device = adapter_->GetDevice(
515 FakeBluetoothDeviceClient::kLowEnergyAddress); 480 FakeBluetoothDeviceClient::kLowEnergyAddress);
516 ASSERT_TRUE(device); 481 ASSERT_TRUE(device);
517 482
518 TestDeviceObserver observer(adapter_, device); 483 TestObserver observer(adapter_);
484
519 EXPECT_EQ(0, observer.gatt_service_added_count_); 485 EXPECT_EQ(0, observer.gatt_service_added_count_);
520 EXPECT_EQ(0, observer.gatt_service_removed_count_); 486 EXPECT_EQ(0, observer.gatt_service_removed_count_);
521 EXPECT_TRUE(observer.last_gatt_service_id_.empty()); 487 EXPECT_TRUE(observer.last_gatt_service_id_.empty());
522 EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid()); 488 EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid());
523 EXPECT_TRUE(device->GetGattServices().empty()); 489 EXPECT_TRUE(device->GetGattServices().empty());
524 490
525 // Expose the fake Heart Rate Service. 491 // Expose the fake Heart Rate Service.
526 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 492 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
527 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 493 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
528 EXPECT_EQ(1, observer.gatt_service_added_count_); 494 EXPECT_EQ(1, observer.gatt_service_added_count_);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 } 564 }
599 565
600 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) { 566 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) {
601 fake_bluetooth_device_client_->CreateDevice( 567 fake_bluetooth_device_client_->CreateDevice(
602 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 568 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
603 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 569 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
604 BluetoothDevice* device = adapter_->GetDevice( 570 BluetoothDevice* device = adapter_->GetDevice(
605 FakeBluetoothDeviceClient::kLowEnergyAddress); 571 FakeBluetoothDeviceClient::kLowEnergyAddress);
606 ASSERT_TRUE(device); 572 ASSERT_TRUE(device);
607 573
608 TestDeviceObserver observer(adapter_, device); 574 TestObserver observer(adapter_);
609 575
610 // Expose the fake Heart Rate service. This will asynchronously expose 576 // Expose the fake Heart Rate service. This will asynchronously expose
611 // characteristics. 577 // characteristics.
612 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 578 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
613 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 579 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
614 ASSERT_EQ(1, observer.gatt_service_added_count_); 580 ASSERT_EQ(1, observer.gatt_service_added_count_);
615 581
616 BluetoothGattService* service = 582 BluetoothGattService* service =
617 device->GetGattService(observer.last_gatt_service_id_); 583 device->GetGattService(observer.last_gatt_service_id_);
618 584
619 TestGattServiceObserver service_observer(adapter_, device, service); 585 EXPECT_EQ(0, observer.gatt_service_changed_count_);
620 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 586 EXPECT_EQ(0, observer.gatt_discovery_complete_count_);
621 EXPECT_EQ(0, service_observer.gatt_discovery_complete_count_); 587 EXPECT_EQ(0, observer.gatt_characteristic_added_count_);
622 EXPECT_EQ(0, service_observer.gatt_characteristic_added_count_); 588 EXPECT_EQ(0, observer.gatt_characteristic_removed_count_);
623 EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_); 589 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
624 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
625 EXPECT_TRUE(service->GetCharacteristics().empty()); 590 EXPECT_TRUE(service->GetCharacteristics().empty());
626 591
627 // Run the message loop so that the characteristics appear. 592 // Run the message loop so that the characteristics appear.
628 base::MessageLoop::current()->Run(); 593 base::MessageLoop::current()->Run();
629 594
630 // 3 characteristics should appear. Only 1 of the characteristics sends 595 // 3 characteristics should appear. Only 1 of the characteristics sends
631 // value changed signals. Service changed should be fired once for 596 // value changed signals. Service changed should be fired once for
632 // descriptor added. 597 // descriptor added.
633 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 598 EXPECT_EQ(0, observer.gatt_service_changed_count_);
634 EXPECT_EQ(1, service_observer.gatt_discovery_complete_count_); 599 EXPECT_EQ(1, observer.gatt_discovery_complete_count_);
635 EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_); 600 EXPECT_EQ(3, observer.gatt_characteristic_added_count_);
636 EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_); 601 EXPECT_EQ(0, observer.gatt_characteristic_removed_count_);
637 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 602 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
638 EXPECT_EQ(3U, service->GetCharacteristics().size()); 603 EXPECT_EQ(3U, service->GetCharacteristics().size());
639 604
640 // Hide the characteristics. 3 removed signals should be received. 605 // Hide the characteristics. 3 removed signals should be received.
641 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics(); 606 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
642 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 607 EXPECT_EQ(0, observer.gatt_service_changed_count_);
643 EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_); 608 EXPECT_EQ(3, observer.gatt_characteristic_added_count_);
644 EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_); 609 EXPECT_EQ(3, observer.gatt_characteristic_removed_count_);
645 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 610 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
646 EXPECT_TRUE(service->GetCharacteristics().empty()); 611 EXPECT_TRUE(service->GetCharacteristics().empty());
647 612
648 // Re-expose the heart rate characteristics. We shouldn't get another 613 // Re-expose the heart rate characteristics. We shouldn't get another
649 // GattDiscoveryCompleteForService call, since the service thinks that 614 // GattDiscoveryCompleteForService call, since the service thinks that
650 // discovery is done. On the bluetoothd side, characteristics will be removed 615 // discovery is done. On the bluetoothd side, characteristics will be removed
651 // only if the service will also be subsequently removed. 616 // only if the service will also be subsequently removed.
652 fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics( 617 fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics(
653 fake_bluetooth_gatt_service_client_->GetHeartRateServicePath()); 618 fake_bluetooth_gatt_service_client_->GetHeartRateServicePath());
654 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 619 EXPECT_EQ(0, observer.gatt_service_changed_count_);
655 EXPECT_EQ(1, service_observer.gatt_discovery_complete_count_); 620 EXPECT_EQ(1, observer.gatt_discovery_complete_count_);
656 EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_); 621 EXPECT_EQ(6, observer.gatt_characteristic_added_count_);
657 EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_); 622 EXPECT_EQ(3, observer.gatt_characteristic_removed_count_);
658 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 623 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
659 EXPECT_EQ(3U, service->GetCharacteristics().size()); 624 EXPECT_EQ(3U, service->GetCharacteristics().size());
660 625
661 // Hide the service. All characteristics should disappear. 626 // Hide the service. All characteristics should disappear.
662 fake_bluetooth_gatt_service_client_->HideHeartRateService(); 627 fake_bluetooth_gatt_service_client_->HideHeartRateService();
663 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 628 EXPECT_EQ(0, observer.gatt_service_changed_count_);
664 EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_); 629 EXPECT_EQ(6, observer.gatt_characteristic_added_count_);
665 EXPECT_EQ(6, service_observer.gatt_characteristic_removed_count_); 630 EXPECT_EQ(6, observer.gatt_characteristic_removed_count_);
666 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 631 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
667 } 632 }
668 633
669 TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) { 634 TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) {
670 fake_bluetooth_device_client_->CreateDevice( 635 fake_bluetooth_device_client_->CreateDevice(
671 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 636 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
672 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 637 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
673 BluetoothDevice* device = adapter_->GetDevice( 638 BluetoothDevice* device = adapter_->GetDevice(
674 FakeBluetoothDeviceClient::kLowEnergyAddress); 639 FakeBluetoothDeviceClient::kLowEnergyAddress);
675 ASSERT_TRUE(device); 640 ASSERT_TRUE(device);
676 641
677 TestDeviceObserver observer(adapter_, device); 642 TestObserver observer(adapter_);
678 643
679 // Expose the fake Heart Rate service. This will asynchronously expose 644 // Expose the fake Heart Rate service. This will asynchronously expose
680 // characteristics. 645 // characteristics.
681 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 646 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
682 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 647 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
683 ASSERT_EQ(1, observer.gatt_service_added_count_); 648 ASSERT_EQ(1, observer.gatt_service_added_count_);
684 649
685 BluetoothGattService* service = 650 BluetoothGattService* service =
686 device->GetGattService(observer.last_gatt_service_id_); 651 device->GetGattService(observer.last_gatt_service_id_);
687 652
688 TestGattServiceObserver service_observer(adapter_, device, service); 653 EXPECT_EQ(0, observer.gatt_service_changed_count_);
689 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 654 EXPECT_EQ(0, observer.gatt_descriptor_added_count_);
690 EXPECT_EQ(0, service_observer.gatt_descriptor_added_count_); 655 EXPECT_EQ(0, observer.gatt_descriptor_removed_count_);
691 EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_); 656 EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_);
692 EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
693 657
694 EXPECT_TRUE(service->GetCharacteristics().empty()); 658 EXPECT_TRUE(service->GetCharacteristics().empty());
695 659
696 // Run the message loop so that the characteristics appear. 660 // Run the message loop so that the characteristics appear.
697 base::MessageLoop::current()->Run(); 661 base::MessageLoop::current()->Run();
698 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 662 EXPECT_EQ(0, observer.gatt_service_changed_count_);
699 663
700 // Only the Heart Rate Measurement characteristic has a descriptor. 664 // Only the Heart Rate Measurement characteristic has a descriptor.
701 EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_); 665 EXPECT_EQ(1, observer.gatt_descriptor_added_count_);
702 EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_); 666 EXPECT_EQ(0, observer.gatt_descriptor_removed_count_);
703 EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_); 667 EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_);
704 668
705 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( 669 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
706 fake_bluetooth_gatt_characteristic_client_-> 670 fake_bluetooth_gatt_characteristic_client_->
707 GetBodySensorLocationPath().value()); 671 GetBodySensorLocationPath().value());
708 ASSERT_TRUE(characteristic); 672 ASSERT_TRUE(characteristic);
709 EXPECT_TRUE(characteristic->GetDescriptors().empty()); 673 EXPECT_TRUE(characteristic->GetDescriptors().empty());
710 674
711 characteristic = service->GetCharacteristic( 675 characteristic = service->GetCharacteristic(
712 fake_bluetooth_gatt_characteristic_client_-> 676 fake_bluetooth_gatt_characteristic_client_->
713 GetHeartRateControlPointPath().value()); 677 GetHeartRateControlPointPath().value());
714 ASSERT_TRUE(characteristic); 678 ASSERT_TRUE(characteristic);
715 EXPECT_TRUE(characteristic->GetDescriptors().empty()); 679 EXPECT_TRUE(characteristic->GetDescriptors().empty());
716 680
717 characteristic = service->GetCharacteristic( 681 characteristic = service->GetCharacteristic(
718 fake_bluetooth_gatt_characteristic_client_-> 682 fake_bluetooth_gatt_characteristic_client_->
719 GetHeartRateMeasurementPath().value()); 683 GetHeartRateMeasurementPath().value());
720 ASSERT_TRUE(characteristic); 684 ASSERT_TRUE(characteristic);
721 EXPECT_EQ(1U, characteristic->GetDescriptors().size()); 685 EXPECT_EQ(1U, characteristic->GetDescriptors().size());
722 686
723 BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0]; 687 BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
724 EXPECT_FALSE(descriptor->IsLocal()); 688 EXPECT_FALSE(descriptor->IsLocal());
725 EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(), 689 EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
726 descriptor->GetUUID()); 690 descriptor->GetUUID());
727 EXPECT_EQ(descriptor->GetUUID(), 691 EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid_);
728 service_observer.last_gatt_descriptor_uuid_); 692 EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id_);
729 EXPECT_EQ(descriptor->GetIdentifier(),
730 service_observer.last_gatt_descriptor_id_);
731 693
732 // Hide the descriptor. 694 // Hide the descriptor.
733 fake_bluetooth_gatt_descriptor_client_->HideDescriptor( 695 fake_bluetooth_gatt_descriptor_client_->HideDescriptor(
734 dbus::ObjectPath(descriptor->GetIdentifier())); 696 dbus::ObjectPath(descriptor->GetIdentifier()));
735 EXPECT_TRUE(characteristic->GetDescriptors().empty()); 697 EXPECT_TRUE(characteristic->GetDescriptors().empty());
736 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 698 EXPECT_EQ(0, observer.gatt_service_changed_count_);
737 EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_); 699 EXPECT_EQ(1, observer.gatt_descriptor_added_count_);
738 EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_); 700 EXPECT_EQ(1, observer.gatt_descriptor_removed_count_);
739 EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_); 701 EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_);
740 702
741 // Expose the descriptor again. 703 // Expose the descriptor again.
742 service_observer.last_gatt_descriptor_id_.clear(); 704 observer.last_gatt_descriptor_id_.clear();
743 service_observer.last_gatt_descriptor_uuid_ = BluetoothUUID(); 705 observer.last_gatt_descriptor_uuid_ = BluetoothUUID();
744 fake_bluetooth_gatt_descriptor_client_->ExposeDescriptor( 706 fake_bluetooth_gatt_descriptor_client_->ExposeDescriptor(
745 dbus::ObjectPath(characteristic->GetIdentifier()), 707 dbus::ObjectPath(characteristic->GetIdentifier()),
746 FakeBluetoothGattDescriptorClient:: 708 FakeBluetoothGattDescriptorClient::
747 kClientCharacteristicConfigurationUUID); 709 kClientCharacteristicConfigurationUUID);
748 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 710 EXPECT_EQ(0, observer.gatt_service_changed_count_);
749 EXPECT_EQ(1U, characteristic->GetDescriptors().size()); 711 EXPECT_EQ(1U, characteristic->GetDescriptors().size());
750 EXPECT_EQ(2, service_observer.gatt_descriptor_added_count_); 712 EXPECT_EQ(2, observer.gatt_descriptor_added_count_);
751 EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_); 713 EXPECT_EQ(1, observer.gatt_descriptor_removed_count_);
752 EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_); 714 EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_);
753 715
754 descriptor = characteristic->GetDescriptors()[0]; 716 descriptor = characteristic->GetDescriptors()[0];
755 EXPECT_FALSE(descriptor->IsLocal()); 717 EXPECT_FALSE(descriptor->IsLocal());
756 EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(), 718 EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
757 descriptor->GetUUID()); 719 descriptor->GetUUID());
758 EXPECT_EQ(descriptor->GetUUID(), service_observer.last_gatt_descriptor_uuid_); 720 EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid_);
759 EXPECT_EQ(descriptor->GetIdentifier(), 721 EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id_);
760 service_observer.last_gatt_descriptor_id_);
761 } 722 }
762 723
763 TEST_F(BluetoothGattChromeOSTest, AdapterAddedAfterGattService) { 724 TEST_F(BluetoothGattChromeOSTest, AdapterAddedAfterGattService) {
764 // This unit test tests that all remote GATT objects are created for D-Bus 725 // This unit test tests that all remote GATT objects are created for D-Bus
765 // objects that were already exposed. 726 // objects that were already exposed.
766 adapter_ = NULL; 727 adapter_ = NULL;
767 ASSERT_FALSE(device::BluetoothAdapterFactory::HasSharedInstanceForTesting()); 728 ASSERT_FALSE(device::BluetoothAdapterFactory::HasSharedInstanceForTesting());
768 729
769 // Create the fake D-Bus objects. 730 // Create the fake D-Bus objects.
770 fake_bluetooth_device_client_->CreateDevice( 731 fake_bluetooth_device_client_->CreateDevice(
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 } 798 }
838 799
839 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { 800 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) {
840 fake_bluetooth_device_client_->CreateDevice( 801 fake_bluetooth_device_client_->CreateDevice(
841 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 802 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
842 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 803 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
843 BluetoothDevice* device = adapter_->GetDevice( 804 BluetoothDevice* device = adapter_->GetDevice(
844 FakeBluetoothDeviceClient::kLowEnergyAddress); 805 FakeBluetoothDeviceClient::kLowEnergyAddress);
845 ASSERT_TRUE(device); 806 ASSERT_TRUE(device);
846 807
847 TestDeviceObserver observer(adapter_, device); 808 TestObserver observer(adapter_);
848 809
849 // Expose the fake Heart Rate service. This will asynchronously expose 810 // Expose the fake Heart Rate service. This will asynchronously expose
850 // characteristics. 811 // characteristics.
851 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 812 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
852 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 813 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
853 ASSERT_EQ(1, observer.gatt_service_added_count_); 814 ASSERT_EQ(1, observer.gatt_service_added_count_);
854 815
855 BluetoothGattService* service = 816 BluetoothGattService* service =
856 device->GetGattService(observer.last_gatt_service_id_); 817 device->GetGattService(observer.last_gatt_service_id_);
857 818
858 TestGattServiceObserver service_observer(adapter_, device, service); 819 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
859 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
860 820
861 // Run the message loop so that the characteristics appear. 821 // Run the message loop so that the characteristics appear.
862 base::MessageLoop::current()->Run(); 822 base::MessageLoop::current()->Run();
863 823
864 // Issue write request to non-writeable characteristics. 824 // Issue write request to non-writeable characteristics.
865 service_observer.last_gatt_characteristic_id_.clear(); 825 observer.last_gatt_characteristic_id_.clear();
866 service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID(); 826 observer.last_gatt_characteristic_uuid_ = BluetoothUUID();
867 827
868 std::vector<uint8> write_value; 828 std::vector<uint8> write_value;
869 write_value.push_back(0x01); 829 write_value.push_back(0x01);
870 BluetoothGattCharacteristic* characteristic = 830 BluetoothGattCharacteristic* characteristic =
871 service->GetCharacteristic(fake_bluetooth_gatt_characteristic_client_-> 831 service->GetCharacteristic(fake_bluetooth_gatt_characteristic_client_->
872 GetHeartRateMeasurementPath().value()); 832 GetHeartRateMeasurementPath().value());
873 ASSERT_TRUE(characteristic); 833 ASSERT_TRUE(characteristic);
874 EXPECT_FALSE(characteristic->IsNotifying()); 834 EXPECT_FALSE(characteristic->IsNotifying());
875 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 835 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
876 GetHeartRateMeasurementPath().value(), 836 GetHeartRateMeasurementPath().value(),
877 characteristic->GetIdentifier()); 837 characteristic->GetIdentifier());
878 EXPECT_EQ(kHeartRateMeasurementUUID, characteristic->GetUUID()); 838 EXPECT_EQ(kHeartRateMeasurementUUID, characteristic->GetUUID());
879 characteristic->WriteRemoteCharacteristic( 839 characteristic->WriteRemoteCharacteristic(
880 write_value, 840 write_value,
881 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 841 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
882 base::Unretained(this)), 842 base::Unretained(this)),
883 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 843 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
884 base::Unretained(this))); 844 base::Unretained(this)));
885 EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty()); 845 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty());
886 EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid()); 846 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid());
887 EXPECT_EQ(0, success_callback_count_); 847 EXPECT_EQ(0, success_callback_count_);
888 EXPECT_EQ(1, error_callback_count_); 848 EXPECT_EQ(1, error_callback_count_);
889 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 849 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
890 850
891 characteristic = service->GetCharacteristic( 851 characteristic = service->GetCharacteristic(
892 fake_bluetooth_gatt_characteristic_client_-> 852 fake_bluetooth_gatt_characteristic_client_->
893 GetBodySensorLocationPath().value()); 853 GetBodySensorLocationPath().value());
894 ASSERT_TRUE(characteristic); 854 ASSERT_TRUE(characteristic);
895 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 855 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
896 GetBodySensorLocationPath().value(), 856 GetBodySensorLocationPath().value(),
897 characteristic->GetIdentifier()); 857 characteristic->GetIdentifier());
898 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); 858 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
899 characteristic->WriteRemoteCharacteristic( 859 characteristic->WriteRemoteCharacteristic(
900 write_value, 860 write_value,
901 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 861 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
902 base::Unretained(this)), 862 base::Unretained(this)),
903 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 863 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
904 base::Unretained(this))); 864 base::Unretained(this)));
905 EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty()); 865 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty());
906 EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid()); 866 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid());
907 EXPECT_EQ(0, success_callback_count_); 867 EXPECT_EQ(0, success_callback_count_);
908 EXPECT_EQ(2, error_callback_count_); 868 EXPECT_EQ(2, error_callback_count_);
909 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 869 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
910 870
911 // Issue write request to writeable characteristic. The "Body Sensor Location" 871 // Issue write request to writeable characteristic. The "Body Sensor Location"
912 // characteristic does not send notifications and WriteValue does not result 872 // characteristic does not send notifications and WriteValue does not result
913 // in a CharacteristicValueChanged event, thus no such event should be 873 // in a CharacteristicValueChanged event, thus no such event should be
914 // received. 874 // received.
915 characteristic = service->GetCharacteristic( 875 characteristic = service->GetCharacteristic(
916 fake_bluetooth_gatt_characteristic_client_-> 876 fake_bluetooth_gatt_characteristic_client_->
917 GetHeartRateControlPointPath().value()); 877 GetHeartRateControlPointPath().value());
918 ASSERT_TRUE(characteristic); 878 ASSERT_TRUE(characteristic);
919 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 879 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
920 GetHeartRateControlPointPath().value(), 880 GetHeartRateControlPointPath().value(),
921 characteristic->GetIdentifier()); 881 characteristic->GetIdentifier());
922 EXPECT_EQ(kHeartRateControlPointUUID, characteristic->GetUUID()); 882 EXPECT_EQ(kHeartRateControlPointUUID, characteristic->GetUUID());
923 characteristic->WriteRemoteCharacteristic( 883 characteristic->WriteRemoteCharacteristic(
924 write_value, 884 write_value,
925 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 885 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
926 base::Unretained(this)), 886 base::Unretained(this)),
927 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 887 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
928 base::Unretained(this))); 888 base::Unretained(this)));
929 EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty()); 889 EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty());
930 EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid()); 890 EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid());
931 EXPECT_EQ(1, success_callback_count_); 891 EXPECT_EQ(1, success_callback_count_);
932 EXPECT_EQ(2, error_callback_count_); 892 EXPECT_EQ(2, error_callback_count_);
933 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_); 893 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
934 894
935 // Issue a read request. A successful read results in a 895 // Issue a read request. A successful read results in a
936 // CharacteristicValueChanged notification. 896 // CharacteristicValueChanged notification.
937 characteristic = service->GetCharacteristic( 897 characteristic = service->GetCharacteristic(
938 fake_bluetooth_gatt_characteristic_client_-> 898 fake_bluetooth_gatt_characteristic_client_->
939 GetBodySensorLocationPath().value()); 899 GetBodySensorLocationPath().value());
940 ASSERT_TRUE(characteristic); 900 ASSERT_TRUE(characteristic);
941 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_-> 901 EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
942 GetBodySensorLocationPath().value(), 902 GetBodySensorLocationPath().value(),
943 characteristic->GetIdentifier()); 903 characteristic->GetIdentifier());
944 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID()); 904 EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
945 characteristic->ReadRemoteCharacteristic( 905 characteristic->ReadRemoteCharacteristic(
946 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, 906 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
947 base::Unretained(this)), 907 base::Unretained(this)),
948 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 908 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
949 base::Unretained(this))); 909 base::Unretained(this)));
950 EXPECT_EQ(2, success_callback_count_); 910 EXPECT_EQ(2, success_callback_count_);
951 EXPECT_EQ(2, error_callback_count_); 911 EXPECT_EQ(2, error_callback_count_);
952 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 912 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
953 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_)); 913 EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_));
954 } 914 }
955 915
956 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) { 916 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) {
957 fake_bluetooth_device_client_->CreateDevice( 917 fake_bluetooth_device_client_->CreateDevice(
958 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 918 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
959 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 919 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
960 BluetoothDevice* device = adapter_->GetDevice( 920 BluetoothDevice* device = adapter_->GetDevice(
961 FakeBluetoothDeviceClient::kLowEnergyAddress); 921 FakeBluetoothDeviceClient::kLowEnergyAddress);
962 ASSERT_TRUE(device); 922 ASSERT_TRUE(device);
963 923
964 TestDeviceObserver observer(adapter_, device); 924 TestObserver observer(adapter_);
965 925
966 // Expose the fake Heart Rate service. This will asynchronously expose 926 // Expose the fake Heart Rate service. This will asynchronously expose
967 // characteristics. 927 // characteristics.
968 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 928 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
969 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 929 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
970 930
971 BluetoothGattService* service = 931 BluetoothGattService* service =
972 device->GetGattService(observer.last_gatt_service_id_); 932 device->GetGattService(observer.last_gatt_service_id_);
973 933
974 TestGattServiceObserver service_observer(adapter_, device, service);
975 EXPECT_TRUE(service->GetCharacteristics().empty()); 934 EXPECT_TRUE(service->GetCharacteristics().empty());
976 935
977 // Run the message loop so that the characteristics appear. 936 // Run the message loop so that the characteristics appear.
978 base::MessageLoop::current()->Run(); 937 base::MessageLoop::current()->Run();
979 938
980 BluetoothGattCharacteristic *characteristic = service->GetCharacteristic( 939 BluetoothGattCharacteristic *characteristic = service->GetCharacteristic(
981 fake_bluetooth_gatt_characteristic_client_-> 940 fake_bluetooth_gatt_characteristic_client_->
982 GetBodySensorLocationPath().value()); 941 GetBodySensorLocationPath().value());
983 EXPECT_EQ(BluetoothGattCharacteristic::kPropertyRead, 942 EXPECT_EQ(BluetoothGattCharacteristic::kPropertyRead,
984 characteristic->GetProperties()); 943 characteristic->GetProperties());
(...skipping 12 matching lines...) Expand all
997 } 956 }
998 957
999 TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { 958 TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) {
1000 fake_bluetooth_device_client_->CreateDevice( 959 fake_bluetooth_device_client_->CreateDevice(
1001 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 960 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1002 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 961 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1003 BluetoothDevice* device = adapter_->GetDevice( 962 BluetoothDevice* device = adapter_->GetDevice(
1004 FakeBluetoothDeviceClient::kLowEnergyAddress); 963 FakeBluetoothDeviceClient::kLowEnergyAddress);
1005 ASSERT_TRUE(device); 964 ASSERT_TRUE(device);
1006 965
1007 TestDeviceObserver observer(adapter_, device); 966 TestObserver observer(adapter_);
1008 967
1009 // Expose the fake Heart Rate service. This will asynchronously expose 968 // Expose the fake Heart Rate service. This will asynchronously expose
1010 // characteristics. 969 // characteristics.
1011 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 970 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
1012 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 971 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1013 ASSERT_EQ(1, observer.gatt_service_added_count_); 972 ASSERT_EQ(1, observer.gatt_service_added_count_);
1014 973
1015 BluetoothGattService* service = 974 BluetoothGattService* service =
1016 device->GetGattService(observer.last_gatt_service_id_); 975 device->GetGattService(observer.last_gatt_service_id_);
1017 976
1018 TestGattServiceObserver service_observer(adapter_, device, service); 977 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1019 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 978 EXPECT_EQ(0, observer.gatt_discovery_complete_count_);
1020 EXPECT_EQ(0, service_observer.gatt_discovery_complete_count_); 979 EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_);
1021 EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
1022 EXPECT_TRUE(service->GetCharacteristics().empty()); 980 EXPECT_TRUE(service->GetCharacteristics().empty());
1023 981
1024 // Run the message loop so that the characteristics appear. 982 // Run the message loop so that the characteristics appear.
1025 base::MessageLoop::current()->Run(); 983 base::MessageLoop::current()->Run();
1026 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 984 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1027 EXPECT_EQ(1, service_observer.gatt_discovery_complete_count_); 985 EXPECT_EQ(1, observer.gatt_discovery_complete_count_);
1028 986
1029 // Only the Heart Rate Measurement characteristic has a descriptor. 987 // Only the Heart Rate Measurement characteristic has a descriptor.
1030 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( 988 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
1031 fake_bluetooth_gatt_characteristic_client_-> 989 fake_bluetooth_gatt_characteristic_client_->
1032 GetHeartRateMeasurementPath().value()); 990 GetHeartRateMeasurementPath().value());
1033 ASSERT_TRUE(characteristic); 991 ASSERT_TRUE(characteristic);
1034 EXPECT_EQ(1U, characteristic->GetDescriptors().size()); 992 EXPECT_EQ(1U, characteristic->GetDescriptors().size());
1035 993
1036 BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0]; 994 BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
1037 EXPECT_FALSE(descriptor->IsLocal()); 995 EXPECT_FALSE(descriptor->IsLocal());
(...skipping 16 matching lines...) Expand all
1054 // successful read. 1012 // successful read.
1055 descriptor->ReadRemoteDescriptor( 1013 descriptor->ReadRemoteDescriptor(
1056 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, 1014 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
1057 base::Unretained(this)), 1015 base::Unretained(this)),
1058 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1016 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1059 base::Unretained(this))); 1017 base::Unretained(this)));
1060 EXPECT_EQ(1, success_callback_count_); 1018 EXPECT_EQ(1, success_callback_count_);
1061 EXPECT_EQ(0, error_callback_count_); 1019 EXPECT_EQ(0, error_callback_count_);
1062 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); 1020 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
1063 EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue())); 1021 EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue()));
1064 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 1022 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1065 EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_); 1023 EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_);
1066 1024
1067 // Write value. Writes to this descriptor will fail. 1025 // Write value. Writes to this descriptor will fail.
1068 desc_value[0] = 0x03; 1026 desc_value[0] = 0x03;
1069 descriptor->WriteRemoteDescriptor( 1027 descriptor->WriteRemoteDescriptor(
1070 desc_value, 1028 desc_value,
1071 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 1029 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
1072 base::Unretained(this)), 1030 base::Unretained(this)),
1073 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1031 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1074 base::Unretained(this))); 1032 base::Unretained(this)));
1075 EXPECT_EQ(1, success_callback_count_); 1033 EXPECT_EQ(1, success_callback_count_);
1076 EXPECT_EQ(1, error_callback_count_); 1034 EXPECT_EQ(1, error_callback_count_);
1077 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); 1035 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
1078 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); 1036 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
1079 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 1037 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1080 EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_); 1038 EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_);
1081 1039
1082 // Read new value. 1040 // Read new value.
1083 descriptor->ReadRemoteDescriptor( 1041 descriptor->ReadRemoteDescriptor(
1084 base::Bind(&BluetoothGattChromeOSTest::ValueCallback, 1042 base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
1085 base::Unretained(this)), 1043 base::Unretained(this)),
1086 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1044 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1087 base::Unretained(this))); 1045 base::Unretained(this)));
1088 EXPECT_EQ(2, success_callback_count_); 1046 EXPECT_EQ(2, success_callback_count_);
1089 EXPECT_EQ(1, error_callback_count_); 1047 EXPECT_EQ(1, error_callback_count_);
1090 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); 1048 EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
1091 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); 1049 EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
1092 EXPECT_EQ(0, service_observer.gatt_service_changed_count_); 1050 EXPECT_EQ(0, observer.gatt_service_changed_count_);
1093 EXPECT_EQ(2, service_observer.gatt_descriptor_value_changed_count_); 1051 EXPECT_EQ(2, observer.gatt_descriptor_value_changed_count_);
1094 } 1052 }
1095 1053
1096 TEST_F(BluetoothGattChromeOSTest, NotifySessions) { 1054 TEST_F(BluetoothGattChromeOSTest, NotifySessions) {
1097 fake_bluetooth_device_client_->CreateDevice( 1055 fake_bluetooth_device_client_->CreateDevice(
1098 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 1056 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1099 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 1057 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1100 BluetoothDevice* device = 1058 BluetoothDevice* device =
1101 adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress); 1059 adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress);
1102 ASSERT_TRUE(device); 1060 ASSERT_TRUE(device);
1103 1061
1104 TestDeviceObserver observer(adapter_, device); 1062 TestObserver observer(adapter_);
1105 1063
1106 // Expose the fake Heart Rate service. This will asynchronously expose 1064 // Expose the fake Heart Rate service. This will asynchronously expose
1107 // characteristics. 1065 // characteristics.
1108 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 1066 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
1109 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 1067 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1110 ASSERT_EQ(1, observer.gatt_service_added_count_); 1068 ASSERT_EQ(1, observer.gatt_service_added_count_);
1111 1069
1112 BluetoothGattService* service = 1070 BluetoothGattService* service =
1113 device->GetGattService(observer.last_gatt_service_id_); 1071 device->GetGattService(observer.last_gatt_service_id_);
1114 1072
1115 TestGattServiceObserver service_observer(adapter_, device, service); 1073 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
1116 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
1117 1074
1118 // Run the message loop so that the characteristics appear. 1075 // Run the message loop so that the characteristics appear.
1119 base::MessageLoop::current()->Run(); 1076 base::MessageLoop::current()->Run();
1120 1077
1121 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( 1078 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
1122 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath() 1079 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath()
1123 .value()); 1080 .value());
1124 ASSERT_TRUE(characteristic); 1081 ASSERT_TRUE(characteristic);
1125 EXPECT_FALSE(characteristic->IsNotifying()); 1082 EXPECT_FALSE(characteristic->IsNotifying());
1126 EXPECT_TRUE(update_sessions_.empty()); 1083 EXPECT_TRUE(update_sessions_.empty());
1127 1084
1128 // Request to start notifications. 1085 // Request to start notifications.
1129 characteristic->StartNotifySession( 1086 characteristic->StartNotifySession(
1130 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1087 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1131 base::Unretained(this)), 1088 base::Unretained(this)),
1132 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1089 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1133 base::Unretained(this))); 1090 base::Unretained(this)));
1134 1091
1135 // The operation still hasn't completed but we should have received the first 1092 // The operation still hasn't completed but we should have received the first
1136 // notification. 1093 // notification.
1137 EXPECT_EQ(0, success_callback_count_); 1094 EXPECT_EQ(0, success_callback_count_);
1138 EXPECT_EQ(0, error_callback_count_); 1095 EXPECT_EQ(0, error_callback_count_);
1139 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1096 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1140 EXPECT_TRUE(update_sessions_.empty()); 1097 EXPECT_TRUE(update_sessions_.empty());
1141 1098
1142 // Send a two more requests, which should get queued. 1099 // Send a two more requests, which should get queued.
1143 characteristic->StartNotifySession( 1100 characteristic->StartNotifySession(
1144 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1101 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1145 base::Unretained(this)), 1102 base::Unretained(this)),
1146 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1103 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1147 base::Unretained(this))); 1104 base::Unretained(this)));
1148 characteristic->StartNotifySession( 1105 characteristic->StartNotifySession(
1149 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1106 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1150 base::Unretained(this)), 1107 base::Unretained(this)),
1151 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1108 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1152 base::Unretained(this))); 1109 base::Unretained(this)));
1153 EXPECT_EQ(0, success_callback_count_); 1110 EXPECT_EQ(0, success_callback_count_);
1154 EXPECT_EQ(0, error_callback_count_); 1111 EXPECT_EQ(0, error_callback_count_);
1155 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1112 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1156 EXPECT_TRUE(update_sessions_.empty()); 1113 EXPECT_TRUE(update_sessions_.empty());
1157 EXPECT_TRUE(characteristic->IsNotifying()); 1114 EXPECT_TRUE(characteristic->IsNotifying());
1158 1115
1159 // Run the main loop. The initial call should complete. The queued call should 1116 // Run the main loop. The initial call should complete. The queued call should
1160 // succeed immediately. 1117 // succeed immediately.
1161 base::MessageLoop::current()->Run(); 1118 base::MessageLoop::current()->Run();
1162 1119
1163 EXPECT_EQ(3, success_callback_count_); 1120 EXPECT_EQ(3, success_callback_count_);
1164 EXPECT_EQ(0, error_callback_count_); 1121 EXPECT_EQ(0, error_callback_count_);
1165 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1122 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1166 EXPECT_EQ(3U, update_sessions_.size()); 1123 EXPECT_EQ(3U, update_sessions_.size());
1167 1124
1168 // Notifications should be getting sent regularly now. 1125 // Notifications should be getting sent regularly now.
1169 base::MessageLoop::current()->Run(); 1126 base::MessageLoop::current()->Run();
1170 EXPECT_GT(service_observer.gatt_characteristic_value_changed_count_, 1); 1127 EXPECT_GT(observer.gatt_characteristic_value_changed_count_, 1);
1171 1128
1172 // Stop one of the sessions. The session should become inactive but the 1129 // Stop one of the sessions. The session should become inactive but the
1173 // characteristic should still be notifying. 1130 // characteristic should still be notifying.
1174 BluetoothGattNotifySession* session = update_sessions_[0]; 1131 BluetoothGattNotifySession* session = update_sessions_[0];
1175 EXPECT_TRUE(session->IsActive()); 1132 EXPECT_TRUE(session->IsActive());
1176 session->Stop(base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 1133 session->Stop(base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
1177 base::Unretained(this))); 1134 base::Unretained(this)));
1178 EXPECT_EQ(4, success_callback_count_); 1135 EXPECT_EQ(4, success_callback_count_);
1179 EXPECT_EQ(0, error_callback_count_); 1136 EXPECT_EQ(0, error_callback_count_);
1180 EXPECT_FALSE(session->IsActive()); 1137 EXPECT_FALSE(session->IsActive());
1181 EXPECT_EQ(characteristic->GetIdentifier(), 1138 EXPECT_EQ(characteristic->GetIdentifier(),
1182 session->GetCharacteristicIdentifier()); 1139 session->GetCharacteristicIdentifier());
1183 EXPECT_TRUE(characteristic->IsNotifying()); 1140 EXPECT_TRUE(characteristic->IsNotifying());
1184 1141
1185 // Delete another session. Characteristic should still be notifying. 1142 // Delete another session. Characteristic should still be notifying.
1186 update_sessions_.pop_back(); 1143 update_sessions_.pop_back();
1187 EXPECT_EQ(2U, update_sessions_.size()); 1144 EXPECT_EQ(2U, update_sessions_.size());
1188 EXPECT_TRUE(characteristic->IsNotifying()); 1145 EXPECT_TRUE(characteristic->IsNotifying());
1189 EXPECT_FALSE(update_sessions_[0]->IsActive()); 1146 EXPECT_FALSE(update_sessions_[0]->IsActive());
1190 EXPECT_TRUE(update_sessions_[1]->IsActive()); 1147 EXPECT_TRUE(update_sessions_[1]->IsActive());
1191 1148
1192 // Clear the last session. 1149 // Clear the last session.
1193 update_sessions_.clear(); 1150 update_sessions_.clear();
1194 EXPECT_TRUE(update_sessions_.empty()); 1151 EXPECT_TRUE(update_sessions_.empty());
1195 EXPECT_FALSE(characteristic->IsNotifying()); 1152 EXPECT_FALSE(characteristic->IsNotifying());
1196 1153
1197 success_callback_count_ = 0; 1154 success_callback_count_ = 0;
1198 service_observer.gatt_characteristic_value_changed_count_ = 0; 1155 observer.gatt_characteristic_value_changed_count_ = 0;
1199 1156
1200 // Enable notifications again. 1157 // Enable notifications again.
1201 characteristic->StartNotifySession( 1158 characteristic->StartNotifySession(
1202 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1159 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1203 base::Unretained(this)), 1160 base::Unretained(this)),
1204 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1161 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1205 base::Unretained(this))); 1162 base::Unretained(this)));
1206 EXPECT_EQ(0, success_callback_count_); 1163 EXPECT_EQ(0, success_callback_count_);
1207 EXPECT_EQ(0, error_callback_count_); 1164 EXPECT_EQ(0, error_callback_count_);
1208 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1165 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1209 EXPECT_TRUE(update_sessions_.empty()); 1166 EXPECT_TRUE(update_sessions_.empty());
1210 EXPECT_TRUE(characteristic->IsNotifying()); 1167 EXPECT_TRUE(characteristic->IsNotifying());
1211 1168
1212 // Run the message loop. Notifications should begin. 1169 // Run the message loop. Notifications should begin.
1213 base::MessageLoop::current()->Run(); 1170 base::MessageLoop::current()->Run();
1214 1171
1215 EXPECT_EQ(1, success_callback_count_); 1172 EXPECT_EQ(1, success_callback_count_);
1216 EXPECT_EQ(0, error_callback_count_); 1173 EXPECT_EQ(0, error_callback_count_);
1217 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1174 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1218 EXPECT_EQ(1U, update_sessions_.size()); 1175 EXPECT_EQ(1U, update_sessions_.size());
1219 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1176 EXPECT_TRUE(update_sessions_[0]->IsActive());
1220 EXPECT_TRUE(characteristic->IsNotifying()); 1177 EXPECT_TRUE(characteristic->IsNotifying());
1221 1178
1222 // Check that notifications are happening. 1179 // Check that notifications are happening.
1223 base::MessageLoop::current()->Run(); 1180 base::MessageLoop::current()->Run();
1224 EXPECT_GT(service_observer.gatt_characteristic_value_changed_count_, 1); 1181 EXPECT_GT(observer.gatt_characteristic_value_changed_count_, 1);
1225 1182
1226 // Request another session. This should return immediately. 1183 // Request another session. This should return immediately.
1227 characteristic->StartNotifySession( 1184 characteristic->StartNotifySession(
1228 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1185 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1229 base::Unretained(this)), 1186 base::Unretained(this)),
1230 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1187 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1231 base::Unretained(this))); 1188 base::Unretained(this)));
1232 EXPECT_EQ(2, success_callback_count_); 1189 EXPECT_EQ(2, success_callback_count_);
1233 EXPECT_EQ(0, error_callback_count_); 1190 EXPECT_EQ(0, error_callback_count_);
1234 EXPECT_EQ(2U, update_sessions_.size()); 1191 EXPECT_EQ(2U, update_sessions_.size());
1235 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1192 EXPECT_TRUE(update_sessions_[0]->IsActive());
1236 EXPECT_TRUE(update_sessions_[1]->IsActive()); 1193 EXPECT_TRUE(update_sessions_[1]->IsActive());
1237 EXPECT_TRUE(characteristic->IsNotifying()); 1194 EXPECT_TRUE(characteristic->IsNotifying());
1238 1195
1239 // Hide the characteristic. The sessions should become inactive. 1196 // Hide the characteristic. The sessions should become inactive.
1240 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics(); 1197 fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
1241 EXPECT_EQ(2U, update_sessions_.size()); 1198 EXPECT_EQ(2U, update_sessions_.size());
1242 EXPECT_FALSE(update_sessions_[0]->IsActive()); 1199 EXPECT_FALSE(update_sessions_[0]->IsActive());
1243 EXPECT_FALSE(update_sessions_[1]->IsActive()); 1200 EXPECT_FALSE(update_sessions_[1]->IsActive());
1244 } 1201 }
1245 1202
1246 TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { 1203 TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) {
1247 fake_bluetooth_device_client_->CreateDevice( 1204 fake_bluetooth_device_client_->CreateDevice(
1248 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), 1205 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1249 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 1206 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1250 BluetoothDevice* device = 1207 BluetoothDevice* device =
1251 adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress); 1208 adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress);
1252 ASSERT_TRUE(device); 1209 ASSERT_TRUE(device);
1253 1210
1254 TestDeviceObserver observer(adapter_, device); 1211 TestObserver observer(adapter_);
1255 1212
1256 // Expose the fake Heart Rate service. This will asynchronously expose 1213 // Expose the fake Heart Rate service. This will asynchronously expose
1257 // characteristics. 1214 // characteristics.
1258 fake_bluetooth_gatt_service_client_->ExposeHeartRateService( 1215 fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
1259 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); 1216 dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
1260 ASSERT_EQ(1, observer.gatt_service_added_count_); 1217 ASSERT_EQ(1, observer.gatt_service_added_count_);
1261 1218
1262 BluetoothGattService* service = 1219 BluetoothGattService* service =
1263 device->GetGattService(observer.last_gatt_service_id_); 1220 device->GetGattService(observer.last_gatt_service_id_);
1264 1221
1265 TestGattServiceObserver service_observer(adapter_, device, service); 1222 EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_);
1266 EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
1267 1223
1268 // Run the message loop so that the characteristics appear. 1224 // Run the message loop so that the characteristics appear.
1269 base::MessageLoop::current()->Run(); 1225 base::MessageLoop::current()->Run();
1270 1226
1271 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( 1227 BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
1272 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath() 1228 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath()
1273 .value()); 1229 .value());
1274 ASSERT_TRUE(characteristic); 1230 ASSERT_TRUE(characteristic);
1275 EXPECT_FALSE(characteristic->IsNotifying()); 1231 EXPECT_FALSE(characteristic->IsNotifying());
1276 EXPECT_TRUE(update_sessions_.empty()); 1232 EXPECT_TRUE(update_sessions_.empty());
(...skipping 17 matching lines...) Expand all
1294 characteristic->StartNotifySession( 1250 characteristic->StartNotifySession(
1295 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1251 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1296 base::Unretained(this)), 1252 base::Unretained(this)),
1297 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1253 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1298 base::Unretained(this))); 1254 base::Unretained(this)));
1299 1255
1300 // The operation still hasn't completed but we should have received the first 1256 // The operation still hasn't completed but we should have received the first
1301 // notification. 1257 // notification.
1302 EXPECT_EQ(0, success_callback_count_); 1258 EXPECT_EQ(0, success_callback_count_);
1303 EXPECT_EQ(0, error_callback_count_); 1259 EXPECT_EQ(0, error_callback_count_);
1304 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1260 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1305 EXPECT_TRUE(characteristic->IsNotifying()); 1261 EXPECT_TRUE(characteristic->IsNotifying());
1306 EXPECT_TRUE(update_sessions_.empty()); 1262 EXPECT_TRUE(update_sessions_.empty());
1307 1263
1308 // Run the main loop. The initial call should complete. The queued calls 1264 // Run the main loop. The initial call should complete. The queued calls
1309 // should succeed immediately. 1265 // should succeed immediately.
1310 base::MessageLoop::current()->Run(); 1266 base::MessageLoop::current()->Run();
1311 1267
1312 EXPECT_EQ(4, success_callback_count_); 1268 EXPECT_EQ(4, success_callback_count_);
1313 EXPECT_EQ(0, error_callback_count_); 1269 EXPECT_EQ(0, error_callback_count_);
1314 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1270 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1315 EXPECT_TRUE(characteristic->IsNotifying()); 1271 EXPECT_TRUE(characteristic->IsNotifying());
1316 EXPECT_EQ(4U, update_sessions_.size()); 1272 EXPECT_EQ(4U, update_sessions_.size());
1317 1273
1318 for (int i = 0; i < 4; i++) 1274 for (int i = 0; i < 4; i++)
1319 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1275 EXPECT_TRUE(update_sessions_[0]->IsActive());
1320 1276
1321 // Stop notifications directly through the client. The sessions should get 1277 // Stop notifications directly through the client. The sessions should get
1322 // marked as inactive. 1278 // marked as inactive.
1323 fake_bluetooth_gatt_characteristic_client_->StopNotify( 1279 fake_bluetooth_gatt_characteristic_client_->StopNotify(
1324 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath(), 1280 fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath(),
1325 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback, 1281 base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
1326 base::Unretained(this)), 1282 base::Unretained(this)),
1327 base::Bind(&BluetoothGattChromeOSTest::DBusErrorCallback, 1283 base::Bind(&BluetoothGattChromeOSTest::DBusErrorCallback,
1328 base::Unretained(this))); 1284 base::Unretained(this)));
1329 EXPECT_EQ(5, success_callback_count_); 1285 EXPECT_EQ(5, success_callback_count_);
1330 EXPECT_EQ(0, error_callback_count_); 1286 EXPECT_EQ(0, error_callback_count_);
1331 EXPECT_FALSE(characteristic->IsNotifying()); 1287 EXPECT_FALSE(characteristic->IsNotifying());
1332 EXPECT_EQ(4U, update_sessions_.size()); 1288 EXPECT_EQ(4U, update_sessions_.size());
1333 1289
1334 for (int i = 0; i < 4; i++) 1290 for (int i = 0; i < 4; i++)
1335 EXPECT_FALSE(update_sessions_[0]->IsActive()); 1291 EXPECT_FALSE(update_sessions_[0]->IsActive());
1336 1292
1337 // It should be possible to restart notifications and the call should reset 1293 // It should be possible to restart notifications and the call should reset
1338 // the session count and make a request through the client. 1294 // the session count and make a request through the client.
1339 update_sessions_.clear(); 1295 update_sessions_.clear();
1340 success_callback_count_ = 0; 1296 success_callback_count_ = 0;
1341 service_observer.gatt_characteristic_value_changed_count_ = 0; 1297 observer.gatt_characteristic_value_changed_count_ = 0;
1342 characteristic->StartNotifySession( 1298 characteristic->StartNotifySession(
1343 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, 1299 base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
1344 base::Unretained(this)), 1300 base::Unretained(this)),
1345 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback, 1301 base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
1346 base::Unretained(this))); 1302 base::Unretained(this)));
1347 1303
1348 EXPECT_EQ(0, success_callback_count_); 1304 EXPECT_EQ(0, success_callback_count_);
1349 EXPECT_EQ(0, error_callback_count_); 1305 EXPECT_EQ(0, error_callback_count_);
1350 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1306 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1351 EXPECT_TRUE(characteristic->IsNotifying()); 1307 EXPECT_TRUE(characteristic->IsNotifying());
1352 EXPECT_TRUE(update_sessions_.empty()); 1308 EXPECT_TRUE(update_sessions_.empty());
1353 1309
1354 base::MessageLoop::current()->Run(); 1310 base::MessageLoop::current()->Run();
1355 1311
1356 EXPECT_EQ(1, success_callback_count_); 1312 EXPECT_EQ(1, success_callback_count_);
1357 EXPECT_EQ(0, error_callback_count_); 1313 EXPECT_EQ(0, error_callback_count_);
1358 EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_); 1314 EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_);
1359 EXPECT_TRUE(characteristic->IsNotifying()); 1315 EXPECT_TRUE(characteristic->IsNotifying());
1360 EXPECT_EQ(1U, update_sessions_.size()); 1316 EXPECT_EQ(1U, update_sessions_.size());
1361 EXPECT_TRUE(update_sessions_[0]->IsActive()); 1317 EXPECT_TRUE(update_sessions_[0]->IsActive());
1362 } 1318 }
1363 1319
1364 } // namespace chromeos 1320 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device_win.cc ('k') | device/bluetooth/bluetooth_gatt_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698