Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_gatt_chromeos_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
8 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
9 #include "chromeos/dbus/fake_bluetooth_device_client.h"
10 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
12 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
13 #include "chromeos/dbus/fake_bluetooth_input_client.h"
14 #include "chromeos/dbus/fake_dbus_thread_manager.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_factory.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
20 #include "device/bluetooth/bluetooth_gatt_descriptor.h"
21 #include "device/bluetooth/bluetooth_gatt_service.h"
22 #include "device/bluetooth/bluetooth_uuid.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using device::BluetoothAdapter;
26 using device::BluetoothDevice;
27 using device::BluetoothGattCharacteristic;
28 using device::BluetoothGattDescriptor;
29 using device::BluetoothGattService;
30 using device::BluetoothUUID;
31
32 namespace chromeos {
33
34 namespace {
35
36 const BluetoothUUID kHeartRateMeasurementUUID(
37     FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID);
38 const BluetoothUUID kBodySensorLocationUUID(
39     FakeBluetoothGattCharacteristicClient::kBodySensorLocationUUID);
40 const BluetoothUUID kHeartRateControlPointUUID(
41     FakeBluetoothGattCharacteristicClient::kHeartRateControlPointUUID);
42
43 // Compares GATT characteristic/descriptor values. Returns true, if the values
44 // are equal.
45 bool ValuesEqual(const std::vector<uint8>& value0,
46                  const std::vector<uint8>& value1) {
47   if (value0.size() != value1.size())
48     return false;
49   for (size_t i = 0; i < value0.size(); ++i)
50     if (value0[i] != value1[i])
51       return false;
52   return true;
53 }
54
55 class TestDeviceObserver : public BluetoothDevice::Observer {
56  public:
57   TestDeviceObserver(scoped_refptr<BluetoothAdapter> adapter,
58                      BluetoothDevice* device)
59       : gatt_service_added_count_(0),
60         gatt_service_removed_count_(0),
61         device_address_(device->GetAddress()),
62         adapter_(adapter) {
63     device->AddObserver(this);
64   }
65
66   virtual ~TestDeviceObserver() {
67     BluetoothDevice* device = adapter_->GetDevice(device_address_);
68     if (device)
69       device->RemoveObserver(this);
70   }
71
72   // BluetoothDevice::Observer overrides.
73   virtual void GattServiceAdded(
74       BluetoothDevice* device,
75       BluetoothGattService* service) OVERRIDE {
76     ASSERT_EQ(device_address_, device->GetAddress());
77
78     ++gatt_service_added_count_;
79     last_gatt_service_id_ = service->GetIdentifier();
80     last_gatt_service_uuid_ = service->GetUUID();
81
82     EXPECT_FALSE(service->IsLocal());
83     EXPECT_TRUE(service->IsPrimary());
84
85     EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service);
86
87     QuitMessageLoop();
88   }
89
90   virtual void GattServiceRemoved(
91       BluetoothDevice* device,
92       BluetoothGattService* service) OVERRIDE {
93     ASSERT_EQ(device_address_, device->GetAddress());
94
95     ++gatt_service_removed_count_;
96     last_gatt_service_id_ = service->GetIdentifier();
97     last_gatt_service_uuid_ = service->GetUUID();
98
99     EXPECT_FALSE(service->IsLocal());
100     EXPECT_TRUE(service->IsPrimary());
101
102     // The device should return NULL for this service.
103     EXPECT_FALSE(device->GetGattService(last_gatt_service_id_));
104
105     QuitMessageLoop();
106   }
107
108   int gatt_service_added_count_;
109   int gatt_service_removed_count_;
110   std::string last_gatt_service_id_;
111   BluetoothUUID last_gatt_service_uuid_;
112
113  private:
114   // Some tests use a message loop since background processing is simulated;
115   // break out of those loops.
116   void QuitMessageLoop() {
117     if (base::MessageLoop::current() &&
118         base::MessageLoop::current()->is_running())
119       base::MessageLoop::current()->Quit();
120   }
121
122   std::string device_address_;
123   scoped_refptr<BluetoothAdapter> adapter_;
124 };
125
126 class TestGattServiceObserver : public BluetoothGattService::Observer {
127  public:
128   TestGattServiceObserver(scoped_refptr<BluetoothAdapter> adapter,
129                           BluetoothDevice* device,
130                           BluetoothGattService* service)
131       : gatt_service_changed_count_(0),
132         gatt_characteristic_added_count_(0),
133         gatt_characteristic_removed_count_(0),
134         gatt_characteristic_value_changed_count_(0),
135         gatt_descriptor_added_count_(0),
136         gatt_descriptor_removed_count_(0),
137         gatt_descriptor_value_changed_count_(0),
138         device_address_(device->GetAddress()),
139         gatt_service_id_(service->GetIdentifier()),
140         adapter_(adapter) {
141     service->AddObserver(this);
142   }
143
144   virtual ~TestGattServiceObserver() {
145     // See if either the device or the service even exist.
146     BluetoothDevice* device = adapter_->GetDevice(device_address_);
147     if (!device)
148       return;
149
150     BluetoothGattService* service = device->GetGattService(gatt_service_id_);
151     if (!service)
152       return;
153
154     service->RemoveObserver(this);
155   }
156
157   // BluetoothGattService::Observer overrides.
158   virtual void GattServiceChanged(BluetoothGattService* service) OVERRIDE {
159     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
160     ++gatt_service_changed_count_;
161
162     QuitMessageLoop();
163   }
164
165   virtual void GattCharacteristicAdded(
166       BluetoothGattService* service,
167       BluetoothGattCharacteristic* characteristic) OVERRIDE {
168     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
169
170     ++gatt_characteristic_added_count_;
171     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
172     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
173
174     EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_),
175               characteristic);
176     EXPECT_EQ(service, characteristic->GetService());
177
178     QuitMessageLoop();
179   }
180
181   virtual void GattCharacteristicRemoved(
182       BluetoothGattService* service,
183       BluetoothGattCharacteristic* characteristic) OVERRIDE {
184     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
185
186     ++gatt_characteristic_removed_count_;
187     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
188     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
189
190     // The service should return NULL for this characteristic.
191     EXPECT_FALSE(service->GetCharacteristic(last_gatt_characteristic_id_));
192     EXPECT_EQ(service, characteristic->GetService());
193
194     QuitMessageLoop();
195   }
196
197   virtual void GattCharacteristicValueChanged(
198       BluetoothGattService* service,
199       BluetoothGattCharacteristic* characteristic,
200       const std::vector<uint8>& value) OVERRIDE {
201     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
202
203     ++gatt_characteristic_value_changed_count_;
204     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
205     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
206     last_changed_characteristic_value_ = value;
207
208     EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_),
209               characteristic);
210     EXPECT_EQ(service, characteristic->GetService());
211
212     QuitMessageLoop();
213   }
214
215   virtual void GattDescriptorAdded(
216       BluetoothGattCharacteristic* characteristic,
217       BluetoothGattDescriptor* descriptor) OVERRIDE {
218     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
219
220     ++gatt_descriptor_added_count_;
221     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
222     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
223
224     EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_),
225               descriptor);
226     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
227
228     QuitMessageLoop();
229   }
230
231   virtual void GattDescriptorRemoved(
232       BluetoothGattCharacteristic* characteristic,
233       BluetoothGattDescriptor* descriptor) OVERRIDE {
234     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
235
236     ++gatt_descriptor_removed_count_;
237     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
238     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
239
240     // The characteristic should return NULL for this descriptor..
241     EXPECT_FALSE(characteristic->GetDescriptor(last_gatt_descriptor_id_));
242     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
243
244     QuitMessageLoop();
245   }
246
247   virtual void GattDescriptorValueChanged(
248       BluetoothGattCharacteristic* characteristic,
249       BluetoothGattDescriptor* descriptor,
250       const std::vector<uint8>& value) OVERRIDE {
251     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
252
253     ++gatt_descriptor_value_changed_count_;
254     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
255     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
256     last_changed_descriptor_value_ = value;
257
258     EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_),
259               descriptor);
260     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
261
262     QuitMessageLoop();
263   }
264
265   int gatt_service_changed_count_;
266   int gatt_characteristic_added_count_;
267   int gatt_characteristic_removed_count_;
268   int gatt_characteristic_value_changed_count_;
269   int gatt_descriptor_added_count_;
270   int gatt_descriptor_removed_count_;
271   int gatt_descriptor_value_changed_count_;
272   std::string last_gatt_characteristic_id_;
273   BluetoothUUID last_gatt_characteristic_uuid_;
274   std::vector<uint8> last_changed_characteristic_value_;
275   std::string last_gatt_descriptor_id_;
276   BluetoothUUID last_gatt_descriptor_uuid_;
277   std::vector<uint8> last_changed_descriptor_value_;
278
279  private:
280   // Some tests use a message loop since background processing is simulated;
281   // break out of those loops.
282   void QuitMessageLoop() {
283     if (base::MessageLoop::current() &&
284         base::MessageLoop::current()->is_running())
285       base::MessageLoop::current()->Quit();
286   }
287
288   std::string device_address_;
289   std::string gatt_service_id_;
290   scoped_refptr<BluetoothAdapter> adapter_;
291 };
292
293 }  // namespace
294
295 class BluetoothGattChromeOSTest : public testing::Test {
296  public:
297   BluetoothGattChromeOSTest()
298       : fake_bluetooth_gatt_service_client_(NULL),
299         success_callback_count_(0),
300         error_callback_count_(0) {
301   }
302
303   virtual void SetUp() {
304     FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
305     fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
306     fake_bluetooth_gatt_service_client_ =
307         new FakeBluetoothGattServiceClient;
308     fake_bluetooth_gatt_characteristic_client_ =
309         new FakeBluetoothGattCharacteristicClient;
310     fake_bluetooth_gatt_descriptor_client_ =
311         new FakeBluetoothGattDescriptorClient;
312     fake_dbus_thread_manager->SetBluetoothDeviceClient(
313         scoped_ptr<BluetoothDeviceClient>(
314             fake_bluetooth_device_client_));
315     fake_dbus_thread_manager->SetBluetoothGattServiceClient(
316         scoped_ptr<BluetoothGattServiceClient>(
317             fake_bluetooth_gatt_service_client_));
318     fake_dbus_thread_manager->SetBluetoothGattCharacteristicClient(
319         scoped_ptr<BluetoothGattCharacteristicClient>(
320             fake_bluetooth_gatt_characteristic_client_));
321     fake_dbus_thread_manager->SetBluetoothGattDescriptorClient(
322         scoped_ptr<BluetoothGattDescriptorClient>(
323             fake_bluetooth_gatt_descriptor_client_));
324     fake_dbus_thread_manager->SetBluetoothAdapterClient(
325         scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
326     fake_dbus_thread_manager->SetBluetoothInputClient(
327         scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
328     fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
329         scoped_ptr<BluetoothAgentManagerClient>(
330             new FakeBluetoothAgentManagerClient));
331     DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
332
333     GetAdapter();
334
335     adapter_->SetPowered(
336         true,
337         base::Bind(&base::DoNothing),
338         base::Bind(&base::DoNothing));
339     ASSERT_TRUE(adapter_->IsPowered());
340   }
341
342   virtual void TearDown() {
343     adapter_ = NULL;
344     DBusThreadManager::Shutdown();
345   }
346
347   void GetAdapter() {
348     device::BluetoothAdapterFactory::GetAdapter(
349         base::Bind(&BluetoothGattChromeOSTest::AdapterCallback,
350                    base::Unretained(this)));
351     ASSERT_TRUE(adapter_.get() != NULL);
352     ASSERT_TRUE(adapter_->IsInitialized());
353     ASSERT_TRUE(adapter_->IsPresent());
354   }
355
356   void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
357     adapter_ = adapter;
358   }
359
360   void SuccessCallback() {
361     ++success_callback_count_;
362   }
363
364   void ValueCallback(const std::vector<uint8>& value) {
365     ++success_callback_count_;
366     last_read_value_ = value;
367   }
368
369   void ErrorCallback() {
370     ++error_callback_count_;
371   }
372
373  protected:
374   base::MessageLoop message_loop_;
375
376   FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
377   FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_;
378   FakeBluetoothGattCharacteristicClient*
379       fake_bluetooth_gatt_characteristic_client_;
380   FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_;
381   scoped_refptr<BluetoothAdapter> adapter_;
382
383   int success_callback_count_;
384   int error_callback_count_;
385   std::vector<uint8> last_read_value_;
386 };
387
388 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) {
389   // Create a fake LE device. We store the device pointer here because this is a
390   // test. It's unsafe to do this in production as the device might get deleted.
391   fake_bluetooth_device_client_->CreateDevice(
392       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
393       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
394   BluetoothDevice* device = adapter_->GetDevice(
395       FakeBluetoothDeviceClient::kLowEnergyAddress);
396   ASSERT_TRUE(device);
397
398   TestDeviceObserver observer(adapter_, device);
399   EXPECT_EQ(0, observer.gatt_service_added_count_);
400   EXPECT_EQ(0, observer.gatt_service_removed_count_);
401   EXPECT_TRUE(observer.last_gatt_service_id_.empty());
402   EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid());
403   EXPECT_TRUE(device->GetGattServices().empty());
404
405   // Expose the fake Heart Rate Service.
406   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
407       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
408   EXPECT_EQ(1, observer.gatt_service_added_count_);
409   EXPECT_EQ(0, observer.gatt_service_removed_count_);
410   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
411   EXPECT_EQ(1U, device->GetGattServices().size());
412   EXPECT_EQ(
413       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
414       observer.last_gatt_service_uuid_);
415
416   BluetoothGattService* service =
417       device->GetGattService(observer.last_gatt_service_id_);
418   EXPECT_FALSE(service->IsLocal());
419   EXPECT_TRUE(service->IsPrimary());
420   EXPECT_EQ(service, device->GetGattServices()[0]);
421   EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
422
423   EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID());
424
425   // Hide the service.
426   observer.last_gatt_service_uuid_ = BluetoothUUID();
427   observer.last_gatt_service_id_.clear();
428   fake_bluetooth_gatt_service_client_->HideHeartRateService();
429
430   EXPECT_EQ(1, observer.gatt_service_added_count_);
431   EXPECT_EQ(1, observer.gatt_service_removed_count_);
432   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
433   EXPECT_TRUE(device->GetGattServices().empty());
434   EXPECT_EQ(
435       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
436       observer.last_gatt_service_uuid_);
437
438   EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id_));
439
440   // Expose the service again.
441   observer.last_gatt_service_uuid_ = BluetoothUUID();
442   observer.last_gatt_service_id_.clear();
443   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
444       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
445   EXPECT_EQ(2, observer.gatt_service_added_count_);
446   EXPECT_EQ(1, observer.gatt_service_removed_count_);
447   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
448   EXPECT_EQ(1U, device->GetGattServices().size());
449   EXPECT_EQ(
450       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
451       observer.last_gatt_service_uuid_);
452
453   // The object |service| points to should have been deallocated. |device|
454   // should contain a brand new instance.
455   service = device->GetGattService(observer.last_gatt_service_id_);
456   EXPECT_EQ(service, device->GetGattServices()[0]);
457   EXPECT_FALSE(service->IsLocal());
458   EXPECT_TRUE(service->IsPrimary());
459
460   EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID());
461
462   // Remove the device. The observer should be notified of the removed service.
463   // |device| becomes invalid after this.
464   observer.last_gatt_service_uuid_ = BluetoothUUID();
465   observer.last_gatt_service_id_.clear();
466   fake_bluetooth_device_client_->RemoveDevice(
467       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
468       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
469
470   EXPECT_EQ(2, observer.gatt_service_added_count_);
471   EXPECT_EQ(2, observer.gatt_service_removed_count_);
472   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
473   EXPECT_EQ(
474       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
475       observer.last_gatt_service_uuid_);
476   EXPECT_EQ(
477       NULL, adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress));
478 }
479
480 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) {
481   fake_bluetooth_device_client_->CreateDevice(
482       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
483       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
484   BluetoothDevice* device = adapter_->GetDevice(
485       FakeBluetoothDeviceClient::kLowEnergyAddress);
486   ASSERT_TRUE(device);
487
488   TestDeviceObserver observer(adapter_, device);
489
490   // Expose the fake Heart Rate service. This will asynchronously expose
491   // characteristics.
492   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
493       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
494   ASSERT_EQ(1, observer.gatt_service_added_count_);
495
496   BluetoothGattService* service =
497       device->GetGattService(observer.last_gatt_service_id_);
498
499   TestGattServiceObserver service_observer(adapter_, device, service);
500   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
501   EXPECT_EQ(0, service_observer.gatt_characteristic_added_count_);
502   EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_);
503   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
504   EXPECT_TRUE(service->GetCharacteristics().empty());
505
506   // Run the message loop so that the characteristics appear.
507   base::MessageLoop::current()->Run();
508
509   // 3 characteristics should appear. Only 1 of the characteristics sends
510   // value changed signals. Service changed should be fired once for
511   // descriptor added.
512   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
513   EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_);
514   EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_);
515   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
516   EXPECT_EQ(3U, service->GetCharacteristics().size());
517
518   // Hide the characteristics. 3 removed signals should be received.
519   fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
520   EXPECT_EQ(8, service_observer.gatt_service_changed_count_);
521   EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_);
522   EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_);
523   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
524   EXPECT_TRUE(service->GetCharacteristics().empty());
525
526   // Re-expose the heart rate characteristics.
527   fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics(
528       fake_bluetooth_gatt_service_client_->GetHeartRateServicePath());
529   EXPECT_EQ(12, service_observer.gatt_service_changed_count_);
530   EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_);
531   EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_);
532   EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_);
533   EXPECT_EQ(3U, service->GetCharacteristics().size());
534
535   // Hide the service. All characteristics should disappear.
536   fake_bluetooth_gatt_service_client_->HideHeartRateService();
537   EXPECT_EQ(16, service_observer.gatt_service_changed_count_);
538   EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_);
539   EXPECT_EQ(6, service_observer.gatt_characteristic_removed_count_);
540   EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_);
541 }
542
543 TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) {
544   fake_bluetooth_device_client_->CreateDevice(
545       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
546       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
547   BluetoothDevice* device = adapter_->GetDevice(
548       FakeBluetoothDeviceClient::kLowEnergyAddress);
549   ASSERT_TRUE(device);
550
551   TestDeviceObserver observer(adapter_, device);
552
553   // Expose the fake Heart Rate service. This will asynchronously expose
554   // characteristics.
555   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
556       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
557   ASSERT_EQ(1, observer.gatt_service_added_count_);
558
559   BluetoothGattService* service =
560       device->GetGattService(observer.last_gatt_service_id_);
561
562   TestGattServiceObserver service_observer(adapter_, device, service);
563   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
564   EXPECT_EQ(0, service_observer.gatt_descriptor_added_count_);
565   EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_);
566   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
567
568   EXPECT_TRUE(service->GetCharacteristics().empty());
569
570   // Run the message loop so that the characteristics appear.
571   base::MessageLoop::current()->Run();
572   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
573
574   // Only the Heart Rate Measurement characteristic has a descriptor.
575   EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_);
576   EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_);
577   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
578
579   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
580       fake_bluetooth_gatt_characteristic_client_->
581           GetBodySensorLocationPath().value());
582   ASSERT_TRUE(characteristic);
583   EXPECT_TRUE(characteristic->GetDescriptors().empty());
584
585   characteristic = service->GetCharacteristic(
586       fake_bluetooth_gatt_characteristic_client_->
587           GetHeartRateControlPointPath().value());
588   ASSERT_TRUE(characteristic);
589   EXPECT_TRUE(characteristic->GetDescriptors().empty());
590
591   characteristic = service->GetCharacteristic(
592       fake_bluetooth_gatt_characteristic_client_->
593           GetHeartRateMeasurementPath().value());
594   ASSERT_TRUE(characteristic);
595   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
596
597   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
598   EXPECT_FALSE(descriptor->IsLocal());
599   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
600             descriptor->GetUUID());
601   EXPECT_EQ(descriptor->GetUUID(),
602             service_observer.last_gatt_descriptor_uuid_);
603   EXPECT_EQ(descriptor->GetIdentifier(),
604             service_observer.last_gatt_descriptor_id_);
605
606   // Hide the descriptor.
607   fake_bluetooth_gatt_descriptor_client_->HideDescriptor(
608       dbus::ObjectPath(descriptor->GetIdentifier()));
609   EXPECT_TRUE(characteristic->GetDescriptors().empty());
610   EXPECT_EQ(5, service_observer.gatt_service_changed_count_);
611   EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_);
612   EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_);
613   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
614
615   // Expose the descriptor again.
616   service_observer.last_gatt_descriptor_id_.clear();
617   service_observer.last_gatt_descriptor_uuid_ = BluetoothUUID();
618   fake_bluetooth_gatt_descriptor_client_->ExposeDescriptor(
619       dbus::ObjectPath(characteristic->GetIdentifier()),
620       FakeBluetoothGattDescriptorClient::
621           kClientCharacteristicConfigurationUUID);
622   EXPECT_EQ(6, service_observer.gatt_service_changed_count_);
623   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
624   EXPECT_EQ(2, service_observer.gatt_descriptor_added_count_);
625   EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_);
626   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
627
628   descriptor = characteristic->GetDescriptors()[0];
629   EXPECT_FALSE(descriptor->IsLocal());
630   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
631             descriptor->GetUUID());
632   EXPECT_EQ(descriptor->GetUUID(), service_observer.last_gatt_descriptor_uuid_);
633   EXPECT_EQ(descriptor->GetIdentifier(),
634             service_observer.last_gatt_descriptor_id_);
635 }
636
637 TEST_F(BluetoothGattChromeOSTest, AdapterAddedAfterGattService) {
638   // This unit test tests that all remote GATT objects are created for D-Bus
639   // objects that were already exposed.
640   adapter_ = NULL;
641   EXPECT_FALSE(device::BluetoothAdapterFactory::HasSharedInstanceForTesting());
642
643   // Create the fake D-Bus objects.
644   fake_bluetooth_device_client_->CreateDevice(
645       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
646       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
647   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
648       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
649   while (!fake_bluetooth_gatt_characteristic_client_->IsHeartRateVisible())
650     base::RunLoop().RunUntilIdle();
651   ASSERT_TRUE(fake_bluetooth_gatt_service_client_->IsHeartRateVisible());
652   ASSERT_TRUE(fake_bluetooth_gatt_characteristic_client_->IsHeartRateVisible());
653
654   // Create the adapter. This should create all the GATT objects.
655   GetAdapter();
656   BluetoothDevice* device = adapter_->GetDevice(
657       FakeBluetoothDeviceClient::kLowEnergyAddress);
658   ASSERT_TRUE(device);
659   EXPECT_EQ(1U, device->GetGattServices().size());
660
661   BluetoothGattService* service = device->GetGattServices()[0];
662   ASSERT_TRUE(service);
663   EXPECT_FALSE(service->IsLocal());
664   EXPECT_TRUE(service->IsPrimary());
665   EXPECT_EQ(
666       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
667       service->GetUUID());
668   EXPECT_EQ(service, device->GetGattServices()[0]);
669   EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
670   EXPECT_FALSE(service->IsLocal());
671   EXPECT_EQ(3U, service->GetCharacteristics().size());
672
673   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
674       fake_bluetooth_gatt_characteristic_client_->
675           GetBodySensorLocationPath().value());
676   ASSERT_TRUE(characteristic);
677   EXPECT_EQ(
678       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
679           kBodySensorLocationUUID),
680       characteristic->GetUUID());
681   EXPECT_FALSE(characteristic->IsLocal());
682   EXPECT_TRUE(characteristic->GetDescriptors().empty());
683
684   characteristic = service->GetCharacteristic(
685       fake_bluetooth_gatt_characteristic_client_->
686           GetHeartRateControlPointPath().value());
687   ASSERT_TRUE(characteristic);
688   EXPECT_EQ(
689       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
690           kHeartRateControlPointUUID),
691       characteristic->GetUUID());
692   EXPECT_FALSE(characteristic->IsLocal());
693   EXPECT_TRUE(characteristic->GetDescriptors().empty());
694
695   characteristic = service->GetCharacteristic(
696       fake_bluetooth_gatt_characteristic_client_->
697           GetHeartRateMeasurementPath().value());
698   ASSERT_TRUE(characteristic);
699   EXPECT_EQ(
700       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
701           kHeartRateMeasurementUUID),
702       characteristic->GetUUID());
703   EXPECT_FALSE(characteristic->IsLocal());
704   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
705
706   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
707   ASSERT_TRUE(descriptor);
708   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
709             descriptor->GetUUID());
710   EXPECT_FALSE(descriptor->IsLocal());
711 }
712
713 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) {
714   fake_bluetooth_device_client_->CreateDevice(
715       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
716       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
717   BluetoothDevice* device = adapter_->GetDevice(
718       FakeBluetoothDeviceClient::kLowEnergyAddress);
719   ASSERT_TRUE(device);
720
721   TestDeviceObserver observer(adapter_, device);
722
723   // Expose the fake Heart Rate service. This will asynchronously expose
724   // characteristics.
725   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
726       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
727   ASSERT_EQ(1, observer.gatt_service_added_count_);
728
729   BluetoothGattService* service =
730       device->GetGattService(observer.last_gatt_service_id_);
731
732   TestGattServiceObserver service_observer(adapter_, device, service);
733   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
734
735   // Run the message loop so that the characteristics appear.
736   base::MessageLoop::current()->Run();
737
738   // We should get an initial value changed signal from the Heart Rate
739   // Measurement characteristic when it getsadded.
740   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
741
742   // The Heart Rate Measurement characteristic should send regular
743   // notifications.
744   base::MessageLoop::current()->Run();
745   EXPECT_EQ(2, service_observer.gatt_characteristic_value_changed_count_);
746   EXPECT_EQ(kHeartRateMeasurementUUID,
747             service_observer.last_gatt_characteristic_uuid_);
748   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
749                 GetHeartRateMeasurementPath().value(),
750             service_observer.last_gatt_characteristic_id_);
751
752   // Receive another notification.
753   service_observer.last_gatt_characteristic_id_.clear();
754   service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID();
755   base::MessageLoop::current()->Run();
756   EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_);
757   EXPECT_EQ(kHeartRateMeasurementUUID,
758             service_observer.last_gatt_characteristic_uuid_);
759   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
760                 GetHeartRateMeasurementPath().value(),
761             service_observer.last_gatt_characteristic_id_);
762
763   // Issue write request to non-writeable characteristics.
764   service_observer.last_gatt_characteristic_id_.clear();
765   service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID();
766
767   std::vector<uint8> write_value;
768   write_value.push_back(0x01);
769   BluetoothGattCharacteristic* characteristic =
770       service->GetCharacteristic(fake_bluetooth_gatt_characteristic_client_->
771           GetHeartRateMeasurementPath().value());
772   ASSERT_TRUE(characteristic);
773   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
774                 GetHeartRateMeasurementPath().value(),
775             characteristic->GetIdentifier());
776   EXPECT_EQ(kHeartRateMeasurementUUID, characteristic->GetUUID());
777   characteristic->WriteRemoteCharacteristic(
778       write_value,
779       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
780                  base::Unretained(this)),
781       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
782                  base::Unretained(this)));
783   EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty());
784   EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid());
785   EXPECT_EQ(0, success_callback_count_);
786   EXPECT_EQ(1, error_callback_count_);
787   EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_);
788
789   characteristic = service->GetCharacteristic(
790       fake_bluetooth_gatt_characteristic_client_->
791           GetBodySensorLocationPath().value());
792   ASSERT_TRUE(characteristic);
793   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
794                 GetBodySensorLocationPath().value(),
795             characteristic->GetIdentifier());
796   EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
797   characteristic->WriteRemoteCharacteristic(
798       write_value,
799       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
800                  base::Unretained(this)),
801       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
802                  base::Unretained(this)));
803   EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty());
804   EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid());
805   EXPECT_EQ(0, success_callback_count_);
806   EXPECT_EQ(2, error_callback_count_);
807   EXPECT_EQ(3, service_observer.gatt_characteristic_value_changed_count_);
808
809   // Issue write request to writeable characteristic. Writing "1" to the control
810   // point characteristic will immediately change its value back to "0", hence
811   // sending "ValueChanged" events twice.
812   characteristic = service->GetCharacteristic(
813       fake_bluetooth_gatt_characteristic_client_->
814           GetHeartRateControlPointPath().value());
815   ASSERT_TRUE(characteristic);
816   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
817                 GetHeartRateControlPointPath().value(),
818             characteristic->GetIdentifier());
819   EXPECT_EQ(kHeartRateControlPointUUID, characteristic->GetUUID());
820   characteristic->WriteRemoteCharacteristic(
821       write_value,
822       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
823                  base::Unretained(this)),
824       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
825                  base::Unretained(this)));
826   EXPECT_EQ(characteristic->GetIdentifier(),
827             service_observer.last_gatt_characteristic_id_);
828   EXPECT_EQ(characteristic->GetUUID(),
829             service_observer.last_gatt_characteristic_uuid_);
830   EXPECT_EQ(1, success_callback_count_);
831   EXPECT_EQ(2, error_callback_count_);
832   EXPECT_EQ(5, service_observer.gatt_characteristic_value_changed_count_);
833
834   // Issue a read request.
835   characteristic = service->GetCharacteristic(
836       fake_bluetooth_gatt_characteristic_client_->
837           GetBodySensorLocationPath().value());
838   ASSERT_TRUE(characteristic);
839   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
840                 GetBodySensorLocationPath().value(),
841             characteristic->GetIdentifier());
842   EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
843   characteristic->ReadRemoteCharacteristic(
844       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
845                  base::Unretained(this)),
846       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
847                  base::Unretained(this)));
848   EXPECT_EQ(2, success_callback_count_);
849   EXPECT_EQ(2, error_callback_count_);
850   EXPECT_EQ(5, service_observer.gatt_characteristic_value_changed_count_);
851   EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_));
852
853   // One last value changed notification.
854   base::MessageLoop::current()->Run();
855   EXPECT_EQ(6, service_observer.gatt_characteristic_value_changed_count_);
856   EXPECT_EQ(kHeartRateMeasurementUUID,
857             service_observer.last_gatt_characteristic_uuid_);
858   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
859                 GetHeartRateMeasurementPath().value(),
860             service_observer.last_gatt_characteristic_id_);
861 }
862
863 TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) {
864   fake_bluetooth_device_client_->CreateDevice(
865       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
866       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
867   BluetoothDevice* device = adapter_->GetDevice(
868       FakeBluetoothDeviceClient::kLowEnergyAddress);
869   ASSERT_TRUE(device);
870
871   TestDeviceObserver observer(adapter_, device);
872
873   // Expose the fake Heart Rate service. This will asynchronously expose
874   // characteristics.
875   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
876       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
877   ASSERT_EQ(1, observer.gatt_service_added_count_);
878
879   BluetoothGattService* service =
880       device->GetGattService(observer.last_gatt_service_id_);
881
882   TestGattServiceObserver service_observer(adapter_, device, service);
883   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
884   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
885   EXPECT_TRUE(service->GetCharacteristics().empty());
886
887   // Run the message loop so that the characteristics appear.
888   base::MessageLoop::current()->Run();
889   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
890
891   // Only the Heart Rate Measurement characteristic has a descriptor.
892   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
893       fake_bluetooth_gatt_characteristic_client_->
894           GetHeartRateMeasurementPath().value());
895   ASSERT_TRUE(characteristic);
896   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
897
898   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
899   EXPECT_FALSE(descriptor->IsLocal());
900   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
901             descriptor->GetUUID());
902
903   std::vector<uint8> desc_value;
904   desc_value.push_back(0);
905   desc_value.push_back(0);
906   EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue()));
907
908   EXPECT_EQ(0, success_callback_count_);
909   EXPECT_EQ(0, error_callback_count_);
910   EXPECT_TRUE(last_read_value_.empty());
911
912   // Read value.
913   descriptor->ReadRemoteDescriptor(
914       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
915                  base::Unretained(this)),
916       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
917                  base::Unretained(this)));
918   EXPECT_EQ(1, success_callback_count_);
919   EXPECT_EQ(0, error_callback_count_);
920   EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
921   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
922   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
923
924   // Write value.
925   desc_value[0] = 0x03;
926   descriptor->WriteRemoteDescriptor(
927       desc_value,
928       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
929                  base::Unretained(this)),
930       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
931                  base::Unretained(this)));
932   EXPECT_EQ(2, success_callback_count_);
933   EXPECT_EQ(0, error_callback_count_);
934   EXPECT_FALSE(ValuesEqual(last_read_value_, descriptor->GetValue()));
935   EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue()));
936   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
937   EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_);
938
939   // Read new value.
940   descriptor->ReadRemoteDescriptor(
941       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
942                  base::Unretained(this)),
943       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
944                  base::Unretained(this)));
945   EXPECT_EQ(3, success_callback_count_);
946   EXPECT_EQ(0, error_callback_count_);
947   EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
948   EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue()));
949   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
950   EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_);
951 }
952
953 }  // namespace chromeos