Home | History | Annotate | Download | only in bluetooth
      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/memory/scoped_vector.h"
      6 #include "base/message_loop/message_loop.h"
      7 #include "base/run_loop.h"
      8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
      9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
     10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
     11 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
     12 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
     13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
     14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
     15 #include "chromeos/dbus/fake_dbus_thread_manager.h"
     16 #include "dbus/object_path.h"
     17 #include "device/bluetooth/bluetooth_adapter.h"
     18 #include "device/bluetooth/bluetooth_adapter_factory.h"
     19 #include "device/bluetooth/bluetooth_device.h"
     20 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
     21 #include "device/bluetooth/bluetooth_gatt_connection.h"
     22 #include "device/bluetooth/bluetooth_gatt_descriptor.h"
     23 #include "device/bluetooth/bluetooth_gatt_notify_session.h"
     24 #include "device/bluetooth/bluetooth_gatt_service.h"
     25 #include "device/bluetooth/bluetooth_uuid.h"
     26 #include "testing/gtest/include/gtest/gtest.h"
     27 
     28 using device::BluetoothAdapter;
     29 using device::BluetoothDevice;
     30 using device::BluetoothGattCharacteristic;
     31 using device::BluetoothGattConnection;
     32 using device::BluetoothGattDescriptor;
     33 using device::BluetoothGattService;
     34 using device::BluetoothGattNotifySession;
     35 using device::BluetoothUUID;
     36 
     37 namespace chromeos {
     38 
     39 namespace {
     40 
     41 const BluetoothUUID kHeartRateMeasurementUUID(
     42     FakeBluetoothGattCharacteristicClient::kHeartRateMeasurementUUID);
     43 const BluetoothUUID kBodySensorLocationUUID(
     44     FakeBluetoothGattCharacteristicClient::kBodySensorLocationUUID);
     45 const BluetoothUUID kHeartRateControlPointUUID(
     46     FakeBluetoothGattCharacteristicClient::kHeartRateControlPointUUID);
     47 
     48 // Compares GATT characteristic/descriptor values. Returns true, if the values
     49 // are equal.
     50 bool ValuesEqual(const std::vector<uint8>& value0,
     51                  const std::vector<uint8>& value1) {
     52   if (value0.size() != value1.size())
     53     return false;
     54   for (size_t i = 0; i < value0.size(); ++i)
     55     if (value0[i] != value1[i])
     56       return false;
     57   return true;
     58 }
     59 
     60 class TestDeviceObserver : public BluetoothDevice::Observer {
     61  public:
     62   TestDeviceObserver(scoped_refptr<BluetoothAdapter> adapter,
     63                      BluetoothDevice* device)
     64       : gatt_service_added_count_(0),
     65         gatt_service_removed_count_(0),
     66         device_address_(device->GetAddress()),
     67         adapter_(adapter) {
     68     device->AddObserver(this);
     69   }
     70 
     71   virtual ~TestDeviceObserver() {
     72     BluetoothDevice* device = adapter_->GetDevice(device_address_);
     73     if (device)
     74       device->RemoveObserver(this);
     75   }
     76 
     77   // BluetoothDevice::Observer overrides.
     78   virtual void GattServiceAdded(
     79       BluetoothDevice* device,
     80       BluetoothGattService* service) OVERRIDE {
     81     ASSERT_EQ(device_address_, device->GetAddress());
     82 
     83     ++gatt_service_added_count_;
     84     last_gatt_service_id_ = service->GetIdentifier();
     85     last_gatt_service_uuid_ = service->GetUUID();
     86 
     87     EXPECT_FALSE(service->IsLocal());
     88     EXPECT_TRUE(service->IsPrimary());
     89 
     90     EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service);
     91 
     92     QuitMessageLoop();
     93   }
     94 
     95   virtual void GattServiceRemoved(
     96       BluetoothDevice* device,
     97       BluetoothGattService* service) OVERRIDE {
     98     ASSERT_EQ(device_address_, device->GetAddress());
     99 
    100     ++gatt_service_removed_count_;
    101     last_gatt_service_id_ = service->GetIdentifier();
    102     last_gatt_service_uuid_ = service->GetUUID();
    103 
    104     EXPECT_FALSE(service->IsLocal());
    105     EXPECT_TRUE(service->IsPrimary());
    106 
    107     // The device should return NULL for this service.
    108     EXPECT_FALSE(device->GetGattService(last_gatt_service_id_));
    109 
    110     QuitMessageLoop();
    111   }
    112 
    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_characteristic_added_count_(0),
    138         gatt_characteristic_removed_count_(0),
    139         gatt_characteristic_value_changed_count_(0),
    140         gatt_descriptor_added_count_(0),
    141         gatt_descriptor_removed_count_(0),
    142         gatt_descriptor_value_changed_count_(0),
    143         device_address_(device->GetAddress()),
    144         gatt_service_id_(service->GetIdentifier()),
    145         adapter_(adapter) {
    146     service->AddObserver(this);
    147   }
    148 
    149   virtual ~TestGattServiceObserver() {
    150     // See if either the device or the service even exist.
    151     BluetoothDevice* device = adapter_->GetDevice(device_address_);
    152     if (!device)
    153       return;
    154 
    155     BluetoothGattService* service = device->GetGattService(gatt_service_id_);
    156     if (!service)
    157       return;
    158 
    159     service->RemoveObserver(this);
    160   }
    161 
    162   // BluetoothGattService::Observer overrides.
    163   virtual void GattServiceChanged(BluetoothGattService* service) OVERRIDE {
    164     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
    165     ++gatt_service_changed_count_;
    166 
    167     QuitMessageLoop();
    168   }
    169 
    170   virtual void GattCharacteristicAdded(
    171       BluetoothGattService* service,
    172       BluetoothGattCharacteristic* characteristic) OVERRIDE {
    173     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
    174 
    175     ++gatt_characteristic_added_count_;
    176     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
    177     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
    178 
    179     EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_),
    180               characteristic);
    181     EXPECT_EQ(service, characteristic->GetService());
    182 
    183     QuitMessageLoop();
    184   }
    185 
    186   virtual void GattCharacteristicRemoved(
    187       BluetoothGattService* service,
    188       BluetoothGattCharacteristic* characteristic) OVERRIDE {
    189     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
    190 
    191     ++gatt_characteristic_removed_count_;
    192     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
    193     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
    194 
    195     // The service should return NULL for this characteristic.
    196     EXPECT_FALSE(service->GetCharacteristic(last_gatt_characteristic_id_));
    197     EXPECT_EQ(service, characteristic->GetService());
    198 
    199     QuitMessageLoop();
    200   }
    201 
    202   virtual void GattCharacteristicValueChanged(
    203       BluetoothGattService* service,
    204       BluetoothGattCharacteristic* characteristic,
    205       const std::vector<uint8>& value) OVERRIDE {
    206     ASSERT_EQ(gatt_service_id_, service->GetIdentifier());
    207 
    208     ++gatt_characteristic_value_changed_count_;
    209     last_gatt_characteristic_id_ = characteristic->GetIdentifier();
    210     last_gatt_characteristic_uuid_ = characteristic->GetUUID();
    211     last_changed_characteristic_value_ = value;
    212 
    213     EXPECT_EQ(service->GetCharacteristic(last_gatt_characteristic_id_),
    214               characteristic);
    215     EXPECT_EQ(service, characteristic->GetService());
    216 
    217     QuitMessageLoop();
    218   }
    219 
    220   virtual void GattDescriptorAdded(
    221       BluetoothGattCharacteristic* characteristic,
    222       BluetoothGattDescriptor* descriptor) OVERRIDE {
    223     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
    224 
    225     ++gatt_descriptor_added_count_;
    226     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
    227     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
    228 
    229     EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_),
    230               descriptor);
    231     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
    232 
    233     QuitMessageLoop();
    234   }
    235 
    236   virtual void GattDescriptorRemoved(
    237       BluetoothGattCharacteristic* characteristic,
    238       BluetoothGattDescriptor* descriptor) OVERRIDE {
    239     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
    240 
    241     ++gatt_descriptor_removed_count_;
    242     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
    243     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
    244 
    245     // The characteristic should return NULL for this descriptor..
    246     EXPECT_FALSE(characteristic->GetDescriptor(last_gatt_descriptor_id_));
    247     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
    248 
    249     QuitMessageLoop();
    250   }
    251 
    252   virtual void GattDescriptorValueChanged(
    253       BluetoothGattCharacteristic* characteristic,
    254       BluetoothGattDescriptor* descriptor,
    255       const std::vector<uint8>& value) OVERRIDE {
    256     ASSERT_EQ(gatt_service_id_, characteristic->GetService()->GetIdentifier());
    257 
    258     ++gatt_descriptor_value_changed_count_;
    259     last_gatt_descriptor_id_ = descriptor->GetIdentifier();
    260     last_gatt_descriptor_uuid_ = descriptor->GetUUID();
    261     last_changed_descriptor_value_ = value;
    262 
    263     EXPECT_EQ(characteristic->GetDescriptor(last_gatt_descriptor_id_),
    264               descriptor);
    265     EXPECT_EQ(characteristic, descriptor->GetCharacteristic());
    266 
    267     QuitMessageLoop();
    268   }
    269 
    270   int gatt_service_changed_count_;
    271   int gatt_characteristic_added_count_;
    272   int gatt_characteristic_removed_count_;
    273   int gatt_characteristic_value_changed_count_;
    274   int gatt_descriptor_added_count_;
    275   int gatt_descriptor_removed_count_;
    276   int gatt_descriptor_value_changed_count_;
    277   std::string last_gatt_characteristic_id_;
    278   BluetoothUUID last_gatt_characteristic_uuid_;
    279   std::vector<uint8> last_changed_characteristic_value_;
    280   std::string last_gatt_descriptor_id_;
    281   BluetoothUUID last_gatt_descriptor_uuid_;
    282   std::vector<uint8> last_changed_descriptor_value_;
    283 
    284  private:
    285   // Some tests use a message loop since background processing is simulated;
    286   // break out of those loops.
    287   void QuitMessageLoop() {
    288     if (base::MessageLoop::current() &&
    289         base::MessageLoop::current()->is_running())
    290       base::MessageLoop::current()->Quit();
    291   }
    292 
    293   std::string device_address_;
    294   std::string gatt_service_id_;
    295   scoped_refptr<BluetoothAdapter> adapter_;
    296 };
    297 
    298 }  // namespace
    299 
    300 class BluetoothGattChromeOSTest : public testing::Test {
    301  public:
    302   BluetoothGattChromeOSTest()
    303       : fake_bluetooth_gatt_service_client_(NULL),
    304         success_callback_count_(0),
    305         error_callback_count_(0) {
    306   }
    307 
    308   virtual void SetUp() {
    309     FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
    310     fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
    311     fake_bluetooth_gatt_service_client_ =
    312         new FakeBluetoothGattServiceClient;
    313     fake_bluetooth_gatt_characteristic_client_ =
    314         new FakeBluetoothGattCharacteristicClient;
    315     fake_bluetooth_gatt_descriptor_client_ =
    316         new FakeBluetoothGattDescriptorClient;
    317     fake_dbus_thread_manager->SetBluetoothDeviceClient(
    318         scoped_ptr<BluetoothDeviceClient>(
    319             fake_bluetooth_device_client_));
    320     fake_dbus_thread_manager->SetBluetoothGattServiceClient(
    321         scoped_ptr<BluetoothGattServiceClient>(
    322             fake_bluetooth_gatt_service_client_));
    323     fake_dbus_thread_manager->SetBluetoothGattCharacteristicClient(
    324         scoped_ptr<BluetoothGattCharacteristicClient>(
    325             fake_bluetooth_gatt_characteristic_client_));
    326     fake_dbus_thread_manager->SetBluetoothGattDescriptorClient(
    327         scoped_ptr<BluetoothGattDescriptorClient>(
    328             fake_bluetooth_gatt_descriptor_client_));
    329     fake_dbus_thread_manager->SetBluetoothAdapterClient(
    330         scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
    331     fake_dbus_thread_manager->SetBluetoothInputClient(
    332         scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
    333     fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
    334         scoped_ptr<BluetoothAgentManagerClient>(
    335             new FakeBluetoothAgentManagerClient));
    336     DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
    337 
    338     GetAdapter();
    339 
    340     adapter_->SetPowered(
    341         true,
    342         base::Bind(&base::DoNothing),
    343         base::Bind(&base::DoNothing));
    344     ASSERT_TRUE(adapter_->IsPowered());
    345   }
    346 
    347   virtual void TearDown() {
    348     adapter_ = NULL;
    349     update_sessions_.clear();
    350     gatt_conn_.reset();
    351     DBusThreadManager::Shutdown();
    352   }
    353 
    354   void GetAdapter() {
    355     device::BluetoothAdapterFactory::GetAdapter(
    356         base::Bind(&BluetoothGattChromeOSTest::AdapterCallback,
    357                    base::Unretained(this)));
    358     ASSERT_TRUE(adapter_.get() != NULL);
    359     ASSERT_TRUE(adapter_->IsInitialized());
    360     ASSERT_TRUE(adapter_->IsPresent());
    361   }
    362 
    363   void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
    364     adapter_ = adapter;
    365   }
    366 
    367   void SuccessCallback() {
    368     ++success_callback_count_;
    369   }
    370 
    371   void ValueCallback(const std::vector<uint8>& value) {
    372     ++success_callback_count_;
    373     last_read_value_ = value;
    374   }
    375 
    376   void GattConnectionCallback(scoped_ptr<BluetoothGattConnection> conn) {
    377     ++success_callback_count_;
    378     gatt_conn_ = conn.Pass();
    379   }
    380 
    381   void NotifySessionCallback(scoped_ptr<BluetoothGattNotifySession> session) {
    382     ++success_callback_count_;
    383     update_sessions_.push_back(session.release());
    384     QuitMessageLoop();
    385   }
    386 
    387   void ErrorCallback() {
    388     ++error_callback_count_;
    389   }
    390 
    391   void DBusErrorCallback(const std::string& error_name,
    392                          const std::string& error_message) {
    393     ++error_callback_count_;
    394   }
    395 
    396   void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
    397     ++error_callback_count_;
    398   }
    399 
    400  protected:
    401   void QuitMessageLoop() {
    402     if (base::MessageLoop::current() &&
    403         base::MessageLoop::current()->is_running())
    404       base::MessageLoop::current()->Quit();
    405   }
    406 
    407   base::MessageLoop message_loop_;
    408 
    409   FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
    410   FakeBluetoothGattServiceClient* fake_bluetooth_gatt_service_client_;
    411   FakeBluetoothGattCharacteristicClient*
    412       fake_bluetooth_gatt_characteristic_client_;
    413   FakeBluetoothGattDescriptorClient* fake_bluetooth_gatt_descriptor_client_;
    414   scoped_ptr<device::BluetoothGattConnection> gatt_conn_;
    415   ScopedVector<BluetoothGattNotifySession> update_sessions_;
    416   scoped_refptr<BluetoothAdapter> adapter_;
    417 
    418   int success_callback_count_;
    419   int error_callback_count_;
    420   std::vector<uint8> last_read_value_;
    421 };
    422 
    423 TEST_F(BluetoothGattChromeOSTest, GattConnection) {
    424   fake_bluetooth_device_client_->CreateDevice(
    425       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    426       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    427   BluetoothDevice* device = adapter_->GetDevice(
    428       FakeBluetoothDeviceClient::kLowEnergyAddress);
    429   ASSERT_TRUE(device);
    430   ASSERT_FALSE(device->IsConnected());
    431   ASSERT_FALSE(gatt_conn_.get());
    432   ASSERT_EQ(0, success_callback_count_);
    433   ASSERT_EQ(0, error_callback_count_);
    434 
    435   device->CreateGattConnection(
    436       base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
    437                  base::Unretained(this)),
    438       base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
    439                  base::Unretained(this)));
    440 
    441   EXPECT_EQ(1, success_callback_count_);
    442   EXPECT_EQ(0, error_callback_count_);
    443   EXPECT_TRUE(device->IsConnected());
    444   ASSERT_TRUE(gatt_conn_.get());
    445   EXPECT_TRUE(gatt_conn_->IsConnected());
    446   EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
    447             gatt_conn_->GetDeviceAddress());
    448 
    449   gatt_conn_->Disconnect(
    450       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
    451                  base::Unretained(this)));
    452   EXPECT_EQ(2, success_callback_count_);
    453   EXPECT_EQ(0, error_callback_count_);
    454   EXPECT_TRUE(device->IsConnected());
    455   EXPECT_FALSE(gatt_conn_->IsConnected());
    456 
    457   device->CreateGattConnection(
    458       base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
    459                  base::Unretained(this)),
    460       base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
    461                  base::Unretained(this)));
    462 
    463   EXPECT_EQ(3, success_callback_count_);
    464   EXPECT_EQ(0, error_callback_count_);
    465   EXPECT_TRUE(device->IsConnected());
    466   ASSERT_TRUE(gatt_conn_.get());
    467   EXPECT_TRUE(gatt_conn_->IsConnected());
    468 
    469   device->Disconnect(
    470       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
    471                  base::Unretained(this)),
    472       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
    473                  base::Unretained(this)));
    474 
    475   EXPECT_EQ(4, success_callback_count_);
    476   EXPECT_EQ(0, error_callback_count_);
    477   ASSERT_TRUE(gatt_conn_.get());
    478   EXPECT_FALSE(gatt_conn_->IsConnected());
    479 
    480   device->CreateGattConnection(
    481       base::Bind(&BluetoothGattChromeOSTest::GattConnectionCallback,
    482                  base::Unretained(this)),
    483       base::Bind(&BluetoothGattChromeOSTest::ConnectErrorCallback,
    484                  base::Unretained(this)));
    485 
    486   EXPECT_EQ(5, success_callback_count_);
    487   EXPECT_EQ(0, error_callback_count_);
    488   EXPECT_TRUE(device->IsConnected());
    489   EXPECT_TRUE(gatt_conn_->IsConnected());
    490 
    491   fake_bluetooth_device_client_->RemoveDevice(
    492       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    493       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    494   ASSERT_TRUE(gatt_conn_.get());
    495   EXPECT_FALSE(gatt_conn_->IsConnected());
    496 }
    497 
    498 TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) {
    499   // Create a fake LE device. We store the device pointer here because this is a
    500   // test. It's unsafe to do this in production as the device might get deleted.
    501   fake_bluetooth_device_client_->CreateDevice(
    502       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    503       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    504   BluetoothDevice* device = adapter_->GetDevice(
    505       FakeBluetoothDeviceClient::kLowEnergyAddress);
    506   ASSERT_TRUE(device);
    507 
    508   TestDeviceObserver observer(adapter_, device);
    509   EXPECT_EQ(0, observer.gatt_service_added_count_);
    510   EXPECT_EQ(0, observer.gatt_service_removed_count_);
    511   EXPECT_TRUE(observer.last_gatt_service_id_.empty());
    512   EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid());
    513   EXPECT_TRUE(device->GetGattServices().empty());
    514 
    515   // Expose the fake Heart Rate Service.
    516   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    517       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    518   EXPECT_EQ(1, observer.gatt_service_added_count_);
    519   EXPECT_EQ(0, observer.gatt_service_removed_count_);
    520   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
    521   EXPECT_EQ(1U, device->GetGattServices().size());
    522   EXPECT_EQ(
    523       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
    524       observer.last_gatt_service_uuid_);
    525 
    526   BluetoothGattService* service =
    527       device->GetGattService(observer.last_gatt_service_id_);
    528   EXPECT_FALSE(service->IsLocal());
    529   EXPECT_TRUE(service->IsPrimary());
    530   EXPECT_EQ(service, device->GetGattServices()[0]);
    531   EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
    532 
    533   EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID());
    534 
    535   // Hide the service.
    536   observer.last_gatt_service_uuid_ = BluetoothUUID();
    537   observer.last_gatt_service_id_.clear();
    538   fake_bluetooth_gatt_service_client_->HideHeartRateService();
    539 
    540   EXPECT_EQ(1, observer.gatt_service_added_count_);
    541   EXPECT_EQ(1, observer.gatt_service_removed_count_);
    542   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
    543   EXPECT_TRUE(device->GetGattServices().empty());
    544   EXPECT_EQ(
    545       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
    546       observer.last_gatt_service_uuid_);
    547 
    548   EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id_));
    549 
    550   // Expose the service again.
    551   observer.last_gatt_service_uuid_ = BluetoothUUID();
    552   observer.last_gatt_service_id_.clear();
    553   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    554       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    555   EXPECT_EQ(2, observer.gatt_service_added_count_);
    556   EXPECT_EQ(1, observer.gatt_service_removed_count_);
    557   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
    558   EXPECT_EQ(1U, device->GetGattServices().size());
    559   EXPECT_EQ(
    560       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
    561       observer.last_gatt_service_uuid_);
    562 
    563   // The object |service| points to should have been deallocated. |device|
    564   // should contain a brand new instance.
    565   service = device->GetGattService(observer.last_gatt_service_id_);
    566   EXPECT_EQ(service, device->GetGattServices()[0]);
    567   EXPECT_FALSE(service->IsLocal());
    568   EXPECT_TRUE(service->IsPrimary());
    569 
    570   EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID());
    571 
    572   // Remove the device. The observer should be notified of the removed service.
    573   // |device| becomes invalid after this.
    574   observer.last_gatt_service_uuid_ = BluetoothUUID();
    575   observer.last_gatt_service_id_.clear();
    576   fake_bluetooth_device_client_->RemoveDevice(
    577       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    578       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    579 
    580   EXPECT_EQ(2, observer.gatt_service_added_count_);
    581   EXPECT_EQ(2, observer.gatt_service_removed_count_);
    582   EXPECT_FALSE(observer.last_gatt_service_id_.empty());
    583   EXPECT_EQ(
    584       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
    585       observer.last_gatt_service_uuid_);
    586   EXPECT_EQ(
    587       NULL, adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress));
    588 }
    589 
    590 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) {
    591   fake_bluetooth_device_client_->CreateDevice(
    592       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    593       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    594   BluetoothDevice* device = adapter_->GetDevice(
    595       FakeBluetoothDeviceClient::kLowEnergyAddress);
    596   ASSERT_TRUE(device);
    597 
    598   TestDeviceObserver observer(adapter_, device);
    599 
    600   // Expose the fake Heart Rate service. This will asynchronously expose
    601   // characteristics.
    602   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    603       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    604   ASSERT_EQ(1, observer.gatt_service_added_count_);
    605 
    606   BluetoothGattService* service =
    607       device->GetGattService(observer.last_gatt_service_id_);
    608 
    609   TestGattServiceObserver service_observer(adapter_, device, service);
    610   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
    611   EXPECT_EQ(0, service_observer.gatt_characteristic_added_count_);
    612   EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_);
    613   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    614   EXPECT_TRUE(service->GetCharacteristics().empty());
    615 
    616   // Run the message loop so that the characteristics appear.
    617   base::MessageLoop::current()->Run();
    618 
    619   // 3 characteristics should appear. Only 1 of the characteristics sends
    620   // value changed signals. Service changed should be fired once for
    621   // descriptor added.
    622   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
    623   EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_);
    624   EXPECT_EQ(0, service_observer.gatt_characteristic_removed_count_);
    625   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    626   EXPECT_EQ(3U, service->GetCharacteristics().size());
    627 
    628   // Hide the characteristics. 3 removed signals should be received.
    629   fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
    630   EXPECT_EQ(8, service_observer.gatt_service_changed_count_);
    631   EXPECT_EQ(3, service_observer.gatt_characteristic_added_count_);
    632   EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_);
    633   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    634   EXPECT_TRUE(service->GetCharacteristics().empty());
    635 
    636   // Re-expose the heart rate characteristics.
    637   fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics(
    638       fake_bluetooth_gatt_service_client_->GetHeartRateServicePath());
    639   EXPECT_EQ(12, service_observer.gatt_service_changed_count_);
    640   EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_);
    641   EXPECT_EQ(3, service_observer.gatt_characteristic_removed_count_);
    642   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    643   EXPECT_EQ(3U, service->GetCharacteristics().size());
    644 
    645   // Hide the service. All characteristics should disappear.
    646   fake_bluetooth_gatt_service_client_->HideHeartRateService();
    647   EXPECT_EQ(16, service_observer.gatt_service_changed_count_);
    648   EXPECT_EQ(6, service_observer.gatt_characteristic_added_count_);
    649   EXPECT_EQ(6, service_observer.gatt_characteristic_removed_count_);
    650   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    651 }
    652 
    653 TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) {
    654   fake_bluetooth_device_client_->CreateDevice(
    655       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    656       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    657   BluetoothDevice* device = adapter_->GetDevice(
    658       FakeBluetoothDeviceClient::kLowEnergyAddress);
    659   ASSERT_TRUE(device);
    660 
    661   TestDeviceObserver observer(adapter_, device);
    662 
    663   // Expose the fake Heart Rate service. This will asynchronously expose
    664   // characteristics.
    665   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    666       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    667   ASSERT_EQ(1, observer.gatt_service_added_count_);
    668 
    669   BluetoothGattService* service =
    670       device->GetGattService(observer.last_gatt_service_id_);
    671 
    672   TestGattServiceObserver service_observer(adapter_, device, service);
    673   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
    674   EXPECT_EQ(0, service_observer.gatt_descriptor_added_count_);
    675   EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_);
    676   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
    677 
    678   EXPECT_TRUE(service->GetCharacteristics().empty());
    679 
    680   // Run the message loop so that the characteristics appear.
    681   base::MessageLoop::current()->Run();
    682   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
    683 
    684   // Only the Heart Rate Measurement characteristic has a descriptor.
    685   EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_);
    686   EXPECT_EQ(0, service_observer.gatt_descriptor_removed_count_);
    687   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
    688 
    689   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
    690       fake_bluetooth_gatt_characteristic_client_->
    691           GetBodySensorLocationPath().value());
    692   ASSERT_TRUE(characteristic);
    693   EXPECT_TRUE(characteristic->GetDescriptors().empty());
    694 
    695   characteristic = service->GetCharacteristic(
    696       fake_bluetooth_gatt_characteristic_client_->
    697           GetHeartRateControlPointPath().value());
    698   ASSERT_TRUE(characteristic);
    699   EXPECT_TRUE(characteristic->GetDescriptors().empty());
    700 
    701   characteristic = service->GetCharacteristic(
    702       fake_bluetooth_gatt_characteristic_client_->
    703           GetHeartRateMeasurementPath().value());
    704   ASSERT_TRUE(characteristic);
    705   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
    706 
    707   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
    708   EXPECT_FALSE(descriptor->IsLocal());
    709   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
    710             descriptor->GetUUID());
    711   EXPECT_EQ(descriptor->GetUUID(),
    712             service_observer.last_gatt_descriptor_uuid_);
    713   EXPECT_EQ(descriptor->GetIdentifier(),
    714             service_observer.last_gatt_descriptor_id_);
    715 
    716   // Hide the descriptor.
    717   fake_bluetooth_gatt_descriptor_client_->HideDescriptor(
    718       dbus::ObjectPath(descriptor->GetIdentifier()));
    719   EXPECT_TRUE(characteristic->GetDescriptors().empty());
    720   EXPECT_EQ(5, service_observer.gatt_service_changed_count_);
    721   EXPECT_EQ(1, service_observer.gatt_descriptor_added_count_);
    722   EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_);
    723   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
    724 
    725   // Expose the descriptor again.
    726   service_observer.last_gatt_descriptor_id_.clear();
    727   service_observer.last_gatt_descriptor_uuid_ = BluetoothUUID();
    728   fake_bluetooth_gatt_descriptor_client_->ExposeDescriptor(
    729       dbus::ObjectPath(characteristic->GetIdentifier()),
    730       FakeBluetoothGattDescriptorClient::
    731           kClientCharacteristicConfigurationUUID);
    732   EXPECT_EQ(6, service_observer.gatt_service_changed_count_);
    733   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
    734   EXPECT_EQ(2, service_observer.gatt_descriptor_added_count_);
    735   EXPECT_EQ(1, service_observer.gatt_descriptor_removed_count_);
    736   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
    737 
    738   descriptor = characteristic->GetDescriptors()[0];
    739   EXPECT_FALSE(descriptor->IsLocal());
    740   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
    741             descriptor->GetUUID());
    742   EXPECT_EQ(descriptor->GetUUID(), service_observer.last_gatt_descriptor_uuid_);
    743   EXPECT_EQ(descriptor->GetIdentifier(),
    744             service_observer.last_gatt_descriptor_id_);
    745 }
    746 
    747 TEST_F(BluetoothGattChromeOSTest, AdapterAddedAfterGattService) {
    748   // This unit test tests that all remote GATT objects are created for D-Bus
    749   // objects that were already exposed.
    750   adapter_ = NULL;
    751   ASSERT_FALSE(device::BluetoothAdapterFactory::HasSharedInstanceForTesting());
    752 
    753   // Create the fake D-Bus objects.
    754   fake_bluetooth_device_client_->CreateDevice(
    755       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    756       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    757   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    758       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    759   while (!fake_bluetooth_gatt_characteristic_client_->IsHeartRateVisible())
    760     base::RunLoop().RunUntilIdle();
    761   ASSERT_TRUE(fake_bluetooth_gatt_service_client_->IsHeartRateVisible());
    762   ASSERT_TRUE(fake_bluetooth_gatt_characteristic_client_->IsHeartRateVisible());
    763 
    764   // Create the adapter. This should create all the GATT objects.
    765   GetAdapter();
    766   BluetoothDevice* device = adapter_->GetDevice(
    767       FakeBluetoothDeviceClient::kLowEnergyAddress);
    768   ASSERT_TRUE(device);
    769   EXPECT_EQ(1U, device->GetGattServices().size());
    770 
    771   BluetoothGattService* service = device->GetGattServices()[0];
    772   ASSERT_TRUE(service);
    773   EXPECT_FALSE(service->IsLocal());
    774   EXPECT_TRUE(service->IsPrimary());
    775   EXPECT_EQ(
    776       BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID),
    777       service->GetUUID());
    778   EXPECT_EQ(service, device->GetGattServices()[0]);
    779   EXPECT_EQ(service, device->GetGattService(service->GetIdentifier()));
    780   EXPECT_FALSE(service->IsLocal());
    781   EXPECT_EQ(3U, service->GetCharacteristics().size());
    782 
    783   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
    784       fake_bluetooth_gatt_characteristic_client_->
    785           GetBodySensorLocationPath().value());
    786   ASSERT_TRUE(characteristic);
    787   EXPECT_EQ(
    788       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
    789           kBodySensorLocationUUID),
    790       characteristic->GetUUID());
    791   EXPECT_FALSE(characteristic->IsLocal());
    792   EXPECT_TRUE(characteristic->GetDescriptors().empty());
    793 
    794   characteristic = service->GetCharacteristic(
    795       fake_bluetooth_gatt_characteristic_client_->
    796           GetHeartRateControlPointPath().value());
    797   ASSERT_TRUE(characteristic);
    798   EXPECT_EQ(
    799       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
    800           kHeartRateControlPointUUID),
    801       characteristic->GetUUID());
    802   EXPECT_FALSE(characteristic->IsLocal());
    803   EXPECT_TRUE(characteristic->GetDescriptors().empty());
    804 
    805   characteristic = service->GetCharacteristic(
    806       fake_bluetooth_gatt_characteristic_client_->
    807           GetHeartRateMeasurementPath().value());
    808   ASSERT_TRUE(characteristic);
    809   EXPECT_EQ(
    810       BluetoothUUID(FakeBluetoothGattCharacteristicClient::
    811           kHeartRateMeasurementUUID),
    812       characteristic->GetUUID());
    813   EXPECT_FALSE(characteristic->IsLocal());
    814   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
    815 
    816   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
    817   ASSERT_TRUE(descriptor);
    818   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
    819             descriptor->GetUUID());
    820   EXPECT_FALSE(descriptor->IsLocal());
    821 }
    822 
    823 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) {
    824   fake_bluetooth_device_client_->CreateDevice(
    825       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    826       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    827   BluetoothDevice* device = adapter_->GetDevice(
    828       FakeBluetoothDeviceClient::kLowEnergyAddress);
    829   ASSERT_TRUE(device);
    830 
    831   TestDeviceObserver observer(adapter_, device);
    832 
    833   // Expose the fake Heart Rate service. This will asynchronously expose
    834   // characteristics.
    835   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    836       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    837   ASSERT_EQ(1, observer.gatt_service_added_count_);
    838 
    839   BluetoothGattService* service =
    840       device->GetGattService(observer.last_gatt_service_id_);
    841 
    842   TestGattServiceObserver service_observer(adapter_, device, service);
    843   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    844 
    845   // Run the message loop so that the characteristics appear.
    846   base::MessageLoop::current()->Run();
    847 
    848   // Issue write request to non-writeable characteristics.
    849   service_observer.last_gatt_characteristic_id_.clear();
    850   service_observer.last_gatt_characteristic_uuid_ = BluetoothUUID();
    851 
    852   std::vector<uint8> write_value;
    853   write_value.push_back(0x01);
    854   BluetoothGattCharacteristic* characteristic =
    855       service->GetCharacteristic(fake_bluetooth_gatt_characteristic_client_->
    856           GetHeartRateMeasurementPath().value());
    857   ASSERT_TRUE(characteristic);
    858   EXPECT_FALSE(characteristic->IsNotifying());
    859   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
    860                 GetHeartRateMeasurementPath().value(),
    861             characteristic->GetIdentifier());
    862   EXPECT_EQ(kHeartRateMeasurementUUID, characteristic->GetUUID());
    863   characteristic->WriteRemoteCharacteristic(
    864       write_value,
    865       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
    866                  base::Unretained(this)),
    867       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
    868                  base::Unretained(this)));
    869   EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty());
    870   EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid());
    871   EXPECT_EQ(0, success_callback_count_);
    872   EXPECT_EQ(1, error_callback_count_);
    873   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    874 
    875   characteristic = service->GetCharacteristic(
    876       fake_bluetooth_gatt_characteristic_client_->
    877           GetBodySensorLocationPath().value());
    878   ASSERT_TRUE(characteristic);
    879   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
    880                 GetBodySensorLocationPath().value(),
    881             characteristic->GetIdentifier());
    882   EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
    883   characteristic->WriteRemoteCharacteristic(
    884       write_value,
    885       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
    886                  base::Unretained(this)),
    887       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
    888                  base::Unretained(this)));
    889   EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty());
    890   EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid());
    891   EXPECT_EQ(0, success_callback_count_);
    892   EXPECT_EQ(2, error_callback_count_);
    893   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    894 
    895   // Issue write request to writeable characteristic. The "Body Sensor Location"
    896   // characteristic does not send notifications and WriteValue does not result
    897   // in a CharacteristicValueChanged event, thus no such event should be
    898   // received.
    899   characteristic = service->GetCharacteristic(
    900       fake_bluetooth_gatt_characteristic_client_->
    901           GetHeartRateControlPointPath().value());
    902   ASSERT_TRUE(characteristic);
    903   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
    904                 GetHeartRateControlPointPath().value(),
    905             characteristic->GetIdentifier());
    906   EXPECT_EQ(kHeartRateControlPointUUID, characteristic->GetUUID());
    907   characteristic->WriteRemoteCharacteristic(
    908       write_value,
    909       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
    910                  base::Unretained(this)),
    911       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
    912                  base::Unretained(this)));
    913   EXPECT_TRUE(service_observer.last_gatt_characteristic_id_.empty());
    914   EXPECT_FALSE(service_observer.last_gatt_characteristic_uuid_.IsValid());
    915   EXPECT_EQ(1, success_callback_count_);
    916   EXPECT_EQ(2, error_callback_count_);
    917   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
    918 
    919   // Issue a read request. A successful read results in a
    920   // CharacteristicValueChanged notification.
    921   characteristic = service->GetCharacteristic(
    922       fake_bluetooth_gatt_characteristic_client_->
    923           GetBodySensorLocationPath().value());
    924   ASSERT_TRUE(characteristic);
    925   EXPECT_EQ(fake_bluetooth_gatt_characteristic_client_->
    926                 GetBodySensorLocationPath().value(),
    927             characteristic->GetIdentifier());
    928   EXPECT_EQ(kBodySensorLocationUUID, characteristic->GetUUID());
    929   characteristic->ReadRemoteCharacteristic(
    930       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
    931                  base::Unretained(this)),
    932       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
    933                  base::Unretained(this)));
    934   EXPECT_EQ(2, success_callback_count_);
    935   EXPECT_EQ(2, error_callback_count_);
    936   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
    937   EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_));
    938 }
    939 
    940 TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) {
    941   fake_bluetooth_device_client_->CreateDevice(
    942       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    943       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    944   BluetoothDevice* device = adapter_->GetDevice(
    945       FakeBluetoothDeviceClient::kLowEnergyAddress);
    946   ASSERT_TRUE(device);
    947 
    948   TestDeviceObserver observer(adapter_, device);
    949 
    950   // Expose the fake Heart Rate service. This will asynchronously expose
    951   // characteristics.
    952   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    953       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    954 
    955   BluetoothGattService* service =
    956       device->GetGattService(observer.last_gatt_service_id_);
    957 
    958   TestGattServiceObserver service_observer(adapter_, device, service);
    959   EXPECT_TRUE(service->GetCharacteristics().empty());
    960 
    961   // Run the message loop so that the characteristics appear.
    962   base::MessageLoop::current()->Run();
    963 
    964   BluetoothGattCharacteristic *characteristic = service->GetCharacteristic(
    965       fake_bluetooth_gatt_characteristic_client_->
    966           GetBodySensorLocationPath().value());
    967   EXPECT_EQ(BluetoothGattCharacteristic::kPropertyRead,
    968             characteristic->GetProperties());
    969 
    970   characteristic = service->GetCharacteristic(
    971       fake_bluetooth_gatt_characteristic_client_->
    972           GetHeartRateControlPointPath().value());
    973   EXPECT_EQ(BluetoothGattCharacteristic::kPropertyWrite,
    974             characteristic->GetProperties());
    975 
    976   characteristic = service->GetCharacteristic(
    977       fake_bluetooth_gatt_characteristic_client_->
    978           GetHeartRateMeasurementPath().value());
    979   EXPECT_EQ(BluetoothGattCharacteristic::kPropertyNotify,
    980             characteristic->GetProperties());
    981 }
    982 
    983 TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) {
    984   fake_bluetooth_device_client_->CreateDevice(
    985       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    986       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    987   BluetoothDevice* device = adapter_->GetDevice(
    988       FakeBluetoothDeviceClient::kLowEnergyAddress);
    989   ASSERT_TRUE(device);
    990 
    991   TestDeviceObserver observer(adapter_, device);
    992 
    993   // Expose the fake Heart Rate service. This will asynchronously expose
    994   // characteristics.
    995   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
    996       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
    997   ASSERT_EQ(1, observer.gatt_service_added_count_);
    998 
    999   BluetoothGattService* service =
   1000       device->GetGattService(observer.last_gatt_service_id_);
   1001 
   1002   TestGattServiceObserver service_observer(adapter_, device, service);
   1003   EXPECT_EQ(0, service_observer.gatt_service_changed_count_);
   1004   EXPECT_EQ(0, service_observer.gatt_descriptor_value_changed_count_);
   1005   EXPECT_TRUE(service->GetCharacteristics().empty());
   1006 
   1007   // Run the message loop so that the characteristics appear.
   1008   base::MessageLoop::current()->Run();
   1009   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
   1010 
   1011   // Only the Heart Rate Measurement characteristic has a descriptor.
   1012   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
   1013       fake_bluetooth_gatt_characteristic_client_->
   1014           GetHeartRateMeasurementPath().value());
   1015   ASSERT_TRUE(characteristic);
   1016   EXPECT_EQ(1U, characteristic->GetDescriptors().size());
   1017 
   1018   BluetoothGattDescriptor* descriptor = characteristic->GetDescriptors()[0];
   1019   EXPECT_FALSE(descriptor->IsLocal());
   1020   EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(),
   1021             descriptor->GetUUID());
   1022 
   1023   std::vector<uint8> desc_value;
   1024   desc_value.push_back(1);
   1025   desc_value.push_back(0);
   1026 
   1027   /* The cached value will be empty until the first read request */
   1028   EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
   1029   EXPECT_TRUE(descriptor->GetValue().empty());
   1030 
   1031   EXPECT_EQ(0, success_callback_count_);
   1032   EXPECT_EQ(0, error_callback_count_);
   1033   EXPECT_TRUE(last_read_value_.empty());
   1034 
   1035   // Read value. GattDescriptorValueChanged event will be sent after a
   1036   // successful read.
   1037   descriptor->ReadRemoteDescriptor(
   1038       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
   1039                  base::Unretained(this)),
   1040       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1041                  base::Unretained(this)));
   1042   EXPECT_EQ(1, success_callback_count_);
   1043   EXPECT_EQ(0, error_callback_count_);
   1044   EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
   1045   EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue()));
   1046   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
   1047   EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_);
   1048 
   1049   // Write value. Writes to this descriptor will fail.
   1050   desc_value[0] = 0x03;
   1051   descriptor->WriteRemoteDescriptor(
   1052       desc_value,
   1053       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
   1054                  base::Unretained(this)),
   1055       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1056                  base::Unretained(this)));
   1057   EXPECT_EQ(1, success_callback_count_);
   1058   EXPECT_EQ(1, error_callback_count_);
   1059   EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
   1060   EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
   1061   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
   1062   EXPECT_EQ(1, service_observer.gatt_descriptor_value_changed_count_);
   1063 
   1064   // Read new value.
   1065   descriptor->ReadRemoteDescriptor(
   1066       base::Bind(&BluetoothGattChromeOSTest::ValueCallback,
   1067                  base::Unretained(this)),
   1068       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1069                  base::Unretained(this)));
   1070   EXPECT_EQ(2, success_callback_count_);
   1071   EXPECT_EQ(1, error_callback_count_);
   1072   EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue()));
   1073   EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue()));
   1074   EXPECT_EQ(4, service_observer.gatt_service_changed_count_);
   1075   EXPECT_EQ(2, service_observer.gatt_descriptor_value_changed_count_);
   1076 }
   1077 
   1078 TEST_F(BluetoothGattChromeOSTest, NotifySessions) {
   1079   fake_bluetooth_device_client_->CreateDevice(
   1080       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
   1081       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
   1082   BluetoothDevice* device =
   1083       adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress);
   1084   ASSERT_TRUE(device);
   1085 
   1086   TestDeviceObserver observer(adapter_, device);
   1087 
   1088   // Expose the fake Heart Rate service. This will asynchronously expose
   1089   // characteristics.
   1090   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
   1091       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
   1092   ASSERT_EQ(1, observer.gatt_service_added_count_);
   1093 
   1094   BluetoothGattService* service =
   1095       device->GetGattService(observer.last_gatt_service_id_);
   1096 
   1097   TestGattServiceObserver service_observer(adapter_, device, service);
   1098   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
   1099 
   1100   // Run the message loop so that the characteristics appear.
   1101   base::MessageLoop::current()->Run();
   1102 
   1103   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
   1104       fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath()
   1105           .value());
   1106   ASSERT_TRUE(characteristic);
   1107   EXPECT_FALSE(characteristic->IsNotifying());
   1108   EXPECT_TRUE(update_sessions_.empty());
   1109 
   1110   // Request to start notifications.
   1111   characteristic->StartNotifySession(
   1112       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1113                  base::Unretained(this)),
   1114       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1115                  base::Unretained(this)));
   1116 
   1117   // The operation still hasn't completed but we should have received the first
   1118   // notification.
   1119   EXPECT_EQ(0, success_callback_count_);
   1120   EXPECT_EQ(0, error_callback_count_);
   1121   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1122   EXPECT_TRUE(update_sessions_.empty());
   1123 
   1124   // Send a two more requests, which should get queued.
   1125   characteristic->StartNotifySession(
   1126       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1127                  base::Unretained(this)),
   1128       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1129                  base::Unretained(this)));
   1130   characteristic->StartNotifySession(
   1131       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1132                  base::Unretained(this)),
   1133       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1134                  base::Unretained(this)));
   1135   EXPECT_EQ(0, success_callback_count_);
   1136   EXPECT_EQ(0, error_callback_count_);
   1137   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1138   EXPECT_TRUE(update_sessions_.empty());
   1139   EXPECT_TRUE(characteristic->IsNotifying());
   1140 
   1141   // Run the main loop. The initial call should complete. The queued call should
   1142   // succeed immediately.
   1143   base::MessageLoop::current()->Run();
   1144 
   1145   EXPECT_EQ(3, success_callback_count_);
   1146   EXPECT_EQ(0, error_callback_count_);
   1147   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1148   EXPECT_EQ(3U, update_sessions_.size());
   1149 
   1150   // Notifications should be getting sent regularly now.
   1151   base::MessageLoop::current()->Run();
   1152   EXPECT_GT(service_observer.gatt_characteristic_value_changed_count_, 1);
   1153 
   1154   // Stop one of the sessions. The session should become inactive but the
   1155   // characteristic should still be notifying.
   1156   BluetoothGattNotifySession* session = update_sessions_[0];
   1157   EXPECT_TRUE(session->IsActive());
   1158   session->Stop(base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
   1159                            base::Unretained(this)));
   1160   EXPECT_EQ(4, success_callback_count_);
   1161   EXPECT_EQ(0, error_callback_count_);
   1162   EXPECT_FALSE(session->IsActive());
   1163   EXPECT_EQ(characteristic->GetIdentifier(),
   1164             session->GetCharacteristicIdentifier());
   1165   EXPECT_TRUE(characteristic->IsNotifying());
   1166 
   1167   // Delete another session. Characteristic should still be notifying.
   1168   update_sessions_.pop_back();
   1169   EXPECT_EQ(2U, update_sessions_.size());
   1170   EXPECT_TRUE(characteristic->IsNotifying());
   1171   EXPECT_FALSE(update_sessions_[0]->IsActive());
   1172   EXPECT_TRUE(update_sessions_[1]->IsActive());
   1173 
   1174   // Clear the last session.
   1175   update_sessions_.clear();
   1176   EXPECT_TRUE(update_sessions_.empty());
   1177   EXPECT_FALSE(characteristic->IsNotifying());
   1178 
   1179   success_callback_count_ = 0;
   1180   service_observer.gatt_characteristic_value_changed_count_ = 0;
   1181 
   1182   // Enable notifications again.
   1183   characteristic->StartNotifySession(
   1184       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1185                  base::Unretained(this)),
   1186       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1187                  base::Unretained(this)));
   1188   EXPECT_EQ(0, success_callback_count_);
   1189   EXPECT_EQ(0, error_callback_count_);
   1190   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1191   EXPECT_TRUE(update_sessions_.empty());
   1192   EXPECT_TRUE(characteristic->IsNotifying());
   1193 
   1194   // Run the message loop. Notifications should begin.
   1195   base::MessageLoop::current()->Run();
   1196 
   1197   EXPECT_EQ(1, success_callback_count_);
   1198   EXPECT_EQ(0, error_callback_count_);
   1199   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1200   EXPECT_EQ(1U, update_sessions_.size());
   1201   EXPECT_TRUE(update_sessions_[0]->IsActive());
   1202   EXPECT_TRUE(characteristic->IsNotifying());
   1203 
   1204   // Check that notifications are happening.
   1205   base::MessageLoop::current()->Run();
   1206   EXPECT_GT(service_observer.gatt_characteristic_value_changed_count_, 1);
   1207 
   1208   // Request another session. This should return immediately.
   1209   characteristic->StartNotifySession(
   1210       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1211                  base::Unretained(this)),
   1212       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1213                  base::Unretained(this)));
   1214   EXPECT_EQ(2, success_callback_count_);
   1215   EXPECT_EQ(0, error_callback_count_);
   1216   EXPECT_EQ(2U, update_sessions_.size());
   1217   EXPECT_TRUE(update_sessions_[0]->IsActive());
   1218   EXPECT_TRUE(update_sessions_[1]->IsActive());
   1219   EXPECT_TRUE(characteristic->IsNotifying());
   1220 
   1221   // Hide the characteristic. The sessions should become inactive.
   1222   fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics();
   1223   EXPECT_EQ(2U, update_sessions_.size());
   1224   EXPECT_FALSE(update_sessions_[0]->IsActive());
   1225   EXPECT_FALSE(update_sessions_[1]->IsActive());
   1226 }
   1227 
   1228 TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) {
   1229   fake_bluetooth_device_client_->CreateDevice(
   1230       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
   1231       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
   1232   BluetoothDevice* device =
   1233       adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress);
   1234   ASSERT_TRUE(device);
   1235 
   1236   TestDeviceObserver observer(adapter_, device);
   1237 
   1238   // Expose the fake Heart Rate service. This will asynchronously expose
   1239   // characteristics.
   1240   fake_bluetooth_gatt_service_client_->ExposeHeartRateService(
   1241       dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath));
   1242   ASSERT_EQ(1, observer.gatt_service_added_count_);
   1243 
   1244   BluetoothGattService* service =
   1245       device->GetGattService(observer.last_gatt_service_id_);
   1246 
   1247   TestGattServiceObserver service_observer(adapter_, device, service);
   1248   EXPECT_EQ(0, service_observer.gatt_characteristic_value_changed_count_);
   1249 
   1250   // Run the message loop so that the characteristics appear.
   1251   base::MessageLoop::current()->Run();
   1252 
   1253   BluetoothGattCharacteristic* characteristic = service->GetCharacteristic(
   1254       fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath()
   1255           .value());
   1256   ASSERT_TRUE(characteristic);
   1257   EXPECT_FALSE(characteristic->IsNotifying());
   1258   EXPECT_TRUE(update_sessions_.empty());
   1259 
   1260   // Send several requests to start notifications.
   1261   characteristic->StartNotifySession(
   1262       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1263                  base::Unretained(this)),
   1264       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1265                  base::Unretained(this)));
   1266   characteristic->StartNotifySession(
   1267       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1268                  base::Unretained(this)),
   1269       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1270                  base::Unretained(this)));
   1271   characteristic->StartNotifySession(
   1272       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1273                  base::Unretained(this)),
   1274       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1275                  base::Unretained(this)));
   1276   characteristic->StartNotifySession(
   1277       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1278                  base::Unretained(this)),
   1279       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1280                  base::Unretained(this)));
   1281 
   1282   // The operation still hasn't completed but we should have received the first
   1283   // notification.
   1284   EXPECT_EQ(0, success_callback_count_);
   1285   EXPECT_EQ(0, error_callback_count_);
   1286   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1287   EXPECT_TRUE(characteristic->IsNotifying());
   1288   EXPECT_TRUE(update_sessions_.empty());
   1289 
   1290   // Run the main loop. The initial call should complete. The queued calls
   1291   // should succeed immediately.
   1292   base::MessageLoop::current()->Run();
   1293 
   1294   EXPECT_EQ(4, success_callback_count_);
   1295   EXPECT_EQ(0, error_callback_count_);
   1296   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1297   EXPECT_TRUE(characteristic->IsNotifying());
   1298   EXPECT_EQ(4U, update_sessions_.size());
   1299 
   1300   for (int i = 0; i < 4; i++)
   1301     EXPECT_TRUE(update_sessions_[0]->IsActive());
   1302 
   1303   // Stop notifications directly through the client. The sessions should get
   1304   // marked as inactive.
   1305   fake_bluetooth_gatt_characteristic_client_->StopNotify(
   1306       fake_bluetooth_gatt_characteristic_client_->GetHeartRateMeasurementPath(),
   1307       base::Bind(&BluetoothGattChromeOSTest::SuccessCallback,
   1308                  base::Unretained(this)),
   1309       base::Bind(&BluetoothGattChromeOSTest::DBusErrorCallback,
   1310                  base::Unretained(this)));
   1311   EXPECT_EQ(5, success_callback_count_);
   1312   EXPECT_EQ(0, error_callback_count_);
   1313   EXPECT_FALSE(characteristic->IsNotifying());
   1314   EXPECT_EQ(4U, update_sessions_.size());
   1315 
   1316   for (int i = 0; i < 4; i++)
   1317     EXPECT_FALSE(update_sessions_[0]->IsActive());
   1318 
   1319   // It should be possible to restart notifications and the call should reset
   1320   // the session count and make a request through the client.
   1321   update_sessions_.clear();
   1322   success_callback_count_ = 0;
   1323   service_observer.gatt_characteristic_value_changed_count_ = 0;
   1324   characteristic->StartNotifySession(
   1325       base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback,
   1326                  base::Unretained(this)),
   1327       base::Bind(&BluetoothGattChromeOSTest::ErrorCallback,
   1328                  base::Unretained(this)));
   1329 
   1330   EXPECT_EQ(0, success_callback_count_);
   1331   EXPECT_EQ(0, error_callback_count_);
   1332   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1333   EXPECT_TRUE(characteristic->IsNotifying());
   1334   EXPECT_TRUE(update_sessions_.empty());
   1335 
   1336   base::MessageLoop::current()->Run();
   1337 
   1338   EXPECT_EQ(1, success_callback_count_);
   1339   EXPECT_EQ(0, error_callback_count_);
   1340   EXPECT_EQ(1, service_observer.gatt_characteristic_value_changed_count_);
   1341   EXPECT_TRUE(characteristic->IsNotifying());
   1342   EXPECT_EQ(1U, update_sessions_.size());
   1343   EXPECT_TRUE(update_sessions_[0]->IsActive());
   1344 }
   1345 
   1346 }  // namespace chromeos
   1347