Home | History | Annotate | Download | only in bluetooth
      1 // Copyright 2013 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/strings/utf_string_conversions.h"
      7 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
      8 #include "chromeos/dbus/fake_bluetooth_device_client.h"
      9 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
     10 #include "dbus/object_path.h"
     11 #include "device/bluetooth/bluetooth_adapter.h"
     12 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
     13 #include "device/bluetooth/bluetooth_adapter_factory.h"
     14 #include "device/bluetooth/bluetooth_device.h"
     15 #include "device/bluetooth/bluetooth_device_chromeos.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 using device::BluetoothAdapter;
     19 using device::BluetoothAdapterFactory;
     20 using device::BluetoothDevice;
     21 
     22 namespace chromeos {
     23 
     24 class TestObserver : public BluetoothAdapter::Observer {
     25  public:
     26   TestObserver(scoped_refptr<BluetoothAdapter> adapter)
     27       : present_changed_count_(0),
     28         powered_changed_count_(0),
     29         discovering_changed_count_(0),
     30         last_present_(false),
     31         last_powered_(false),
     32         last_discovering_(false),
     33         device_added_count_(0),
     34         device_changed_count_(0),
     35         device_removed_count_(0),
     36         last_device_(NULL),
     37         adapter_(adapter) {
     38   }
     39   virtual ~TestObserver() {}
     40 
     41   virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
     42                                      bool present) OVERRIDE {
     43     EXPECT_EQ(adapter_, adapter);
     44 
     45     ++present_changed_count_;
     46     last_present_ = present;
     47   }
     48 
     49   virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
     50                                      bool powered) OVERRIDE {
     51     EXPECT_EQ(adapter_, adapter);
     52 
     53     ++powered_changed_count_;
     54     last_powered_ = powered;
     55   }
     56 
     57   virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
     58                                          bool discovering) OVERRIDE {
     59     EXPECT_EQ(adapter_, adapter);
     60 
     61     ++discovering_changed_count_;
     62     last_discovering_ = discovering;
     63   }
     64 
     65   virtual void DeviceAdded(BluetoothAdapter* adapter,
     66                            BluetoothDevice* device) OVERRIDE {
     67     EXPECT_EQ(adapter_, adapter);
     68 
     69     ++device_added_count_;
     70     last_device_ = device;
     71     last_device_address_ = device->GetAddress();
     72 
     73     QuitMessageLoop();
     74   }
     75 
     76   virtual void DeviceChanged(BluetoothAdapter* adapter,
     77                              BluetoothDevice* device) OVERRIDE {
     78     EXPECT_EQ(adapter_, adapter);
     79 
     80     ++device_changed_count_;
     81     last_device_ = device;
     82     last_device_address_ = device->GetAddress();
     83 
     84     QuitMessageLoop();
     85   }
     86 
     87   virtual void DeviceRemoved(BluetoothAdapter* adapter,
     88                              BluetoothDevice* device) OVERRIDE {
     89     EXPECT_EQ(adapter_, adapter);
     90 
     91     ++device_removed_count_;
     92     // Can't save device, it may be freed
     93     last_device_address_ = device->GetAddress();
     94 
     95     QuitMessageLoop();
     96   }
     97 
     98   int present_changed_count_;
     99   int powered_changed_count_;
    100   int discovering_changed_count_;
    101   bool last_present_;
    102   bool last_powered_;
    103   bool last_discovering_;
    104   int device_added_count_;
    105   int device_changed_count_;
    106   int device_removed_count_;
    107   BluetoothDevice* last_device_;
    108   std::string last_device_address_;
    109 
    110  private:
    111   // Some tests use a message loop since background processing is simulated;
    112   // break out of those loops.
    113   void QuitMessageLoop() {
    114     if (base::MessageLoop::current() &&
    115         base::MessageLoop::current()->is_running())
    116       base::MessageLoop::current()->Quit();
    117   }
    118 
    119   scoped_refptr<BluetoothAdapter> adapter_;
    120 };
    121 
    122 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
    123  public:
    124   TestPairingDelegate()
    125       : call_count_(0),
    126         request_pincode_count_(0),
    127         request_passkey_count_(0),
    128         display_pincode_count_(0),
    129         display_passkey_count_(0),
    130         keys_entered_count_(0),
    131         confirm_passkey_count_(0),
    132         dismiss_count_(0),
    133         last_passkey_(9999999U),
    134         last_entered_(999U) {}
    135   virtual ~TestPairingDelegate() {}
    136 
    137   virtual void RequestPinCode(BluetoothDevice* device) OVERRIDE {
    138     ++call_count_;
    139     ++request_pincode_count_;
    140     QuitMessageLoop();
    141   }
    142 
    143   virtual void RequestPasskey(BluetoothDevice* device) OVERRIDE {
    144     ++call_count_;
    145     ++request_passkey_count_;
    146     QuitMessageLoop();
    147   }
    148 
    149   virtual void DisplayPinCode(BluetoothDevice* device,
    150                               const std::string& pincode) OVERRIDE {
    151     ++call_count_;
    152     ++display_pincode_count_;
    153     last_pincode_ = pincode;
    154     QuitMessageLoop();
    155   }
    156 
    157   virtual void DisplayPasskey(BluetoothDevice* device,
    158                               uint32 passkey) OVERRIDE {
    159     ++call_count_;
    160     ++display_passkey_count_;
    161     last_passkey_ = passkey;
    162     QuitMessageLoop();
    163   }
    164 
    165   virtual void KeysEntered(BluetoothDevice* device, uint32 entered) OVERRIDE {
    166     ++call_count_;
    167     ++keys_entered_count_;
    168     last_entered_ = entered;
    169     QuitMessageLoop();
    170   }
    171 
    172   virtual void ConfirmPasskey(BluetoothDevice* device,
    173                               uint32 passkey) OVERRIDE {
    174     ++call_count_;
    175     ++confirm_passkey_count_;
    176     last_passkey_ = passkey;
    177     QuitMessageLoop();
    178   }
    179 
    180   virtual void DismissDisplayOrConfirm() OVERRIDE {
    181     ++call_count_;
    182     ++dismiss_count_;
    183     QuitMessageLoop();
    184   }
    185 
    186   int call_count_;
    187   int request_pincode_count_;
    188   int request_passkey_count_;
    189   int display_pincode_count_;
    190   int display_passkey_count_;
    191   int keys_entered_count_;
    192   int confirm_passkey_count_;
    193   int dismiss_count_;
    194   uint32 last_passkey_;
    195   uint32 last_entered_;
    196   std::string last_pincode_;
    197 
    198   private:
    199    // Some tests use a message loop since background processing is simulated;
    200    // break out of those loops.
    201    void QuitMessageLoop() {
    202      if (base::MessageLoop::current() &&
    203          base::MessageLoop::current()->is_running())
    204        base::MessageLoop::current()->Quit();
    205    }
    206 };
    207 
    208 class BluetoothChromeOSTest : public testing::Test {
    209  public:
    210   virtual void SetUp() {
    211     mock_dbus_thread_manager_ =
    212         new MockDBusThreadManagerWithoutGMock();
    213     DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_);
    214 
    215     fake_bluetooth_adapter_client_ =
    216       mock_dbus_thread_manager_->fake_bluetooth_adapter_client();
    217     fake_bluetooth_device_client_ =
    218       mock_dbus_thread_manager_->fake_bluetooth_device_client();
    219 
    220     callback_count_ = 0;
    221     error_callback_count_ = 0;
    222     last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
    223   }
    224 
    225   virtual void TearDown() {
    226     adapter_ = NULL;
    227     DBusThreadManager::Shutdown();
    228   }
    229 
    230   // Generic callbacks
    231   void Callback() {
    232     ++callback_count_;
    233   }
    234 
    235   void ErrorCallback() {
    236     ++error_callback_count_;
    237   }
    238 
    239   void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode error) {
    240     ++error_callback_count_;
    241     last_connect_error_ = error;
    242   }
    243 
    244   // Call to fill the adapter_ member with a BluetoothAdapter instance.
    245   void GetAdapter() {
    246     adapter_ = new BluetoothAdapterChromeOS();
    247     ASSERT_TRUE(adapter_.get() != NULL);
    248     ASSERT_TRUE(adapter_->IsInitialized());
    249   }
    250 
    251   // Run a discovery phase until the named device is detected, or if the named
    252   // device is not created, the discovery process ends without finding it.
    253   //
    254   // The correct behavior of discovery is tested by the "Discovery" test case
    255   // without using this function.
    256   void DiscoverDevice(const std::string& address) {
    257     ASSERT_TRUE(adapter_.get() != NULL);
    258 
    259     if (base::MessageLoop::current() == NULL) {
    260       base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
    261       DiscoverDevices();
    262       return;
    263     }
    264 
    265     fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
    266 
    267     TestObserver observer(adapter_);
    268     adapter_->AddObserver(&observer);
    269 
    270     adapter_->SetPowered(
    271         true,
    272         base::Bind(&BluetoothChromeOSTest::Callback,
    273                    base::Unretained(this)),
    274         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    275                    base::Unretained(this)));
    276     adapter_->StartDiscovering(
    277         base::Bind(&BluetoothChromeOSTest::Callback,
    278                    base::Unretained(this)),
    279         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    280                    base::Unretained(this)));
    281     ASSERT_EQ(2, callback_count_);
    282     ASSERT_EQ(0, error_callback_count_);
    283     callback_count_ = 0;
    284 
    285     ASSERT_TRUE(adapter_->IsPowered());
    286     ASSERT_TRUE(adapter_->IsDiscovering());
    287 
    288     while (!observer.device_removed_count_ &&
    289            observer.last_device_address_ != address)
    290       base::MessageLoop::current()->Run();
    291 
    292     adapter_->StopDiscovering(
    293         base::Bind(&BluetoothChromeOSTest::Callback,
    294                    base::Unretained(this)),
    295         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    296                    base::Unretained(this)));
    297     ASSERT_EQ(1, callback_count_);
    298     ASSERT_EQ(0, error_callback_count_);
    299     callback_count_ = 0;
    300 
    301     ASSERT_FALSE(adapter_->IsDiscovering());
    302 
    303     adapter_->RemoveObserver(&observer);
    304   }
    305 
    306   // Run a discovery phase so we have devices that can be paired with.
    307   void DiscoverDevices() {
    308     // Pass an invalid address for the device so that the discovery process
    309     // completes with all devices.
    310     DiscoverDevice("does not exist");
    311   }
    312 
    313  protected:
    314   FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
    315   FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
    316   MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_;
    317   scoped_refptr<BluetoothAdapter> adapter_;
    318 
    319   int callback_count_;
    320   int error_callback_count_;
    321   enum BluetoothDevice::ConnectErrorCode last_connect_error_;
    322 };
    323 
    324 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
    325   GetAdapter();
    326 
    327   // This verifies that the class gets the list of adapters when created;
    328   // and initializes with an existing adapter if there is one.
    329   EXPECT_TRUE(adapter_->IsPresent());
    330   EXPECT_FALSE(adapter_->IsPowered());
    331   EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
    332             adapter_->GetAddress());
    333   EXPECT_FALSE(adapter_->IsDiscovering());
    334 
    335   // There should be a device
    336   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    337   EXPECT_EQ(1U, devices.size());
    338   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    339             devices[0]->GetAddress());
    340 }
    341 
    342 TEST_F(BluetoothChromeOSTest, BecomePresent) {
    343   fake_bluetooth_adapter_client_->SetVisible(false);
    344   GetAdapter();
    345   ASSERT_FALSE(adapter_->IsPresent());
    346 
    347   // Install an observer; expect the AdapterPresentChanged to be called
    348   // with true, and IsPresent() to return true.
    349   TestObserver observer(adapter_);
    350   adapter_->AddObserver(&observer);
    351 
    352   fake_bluetooth_adapter_client_->SetVisible(true);
    353 
    354   EXPECT_EQ(1, observer.present_changed_count_);
    355   EXPECT_TRUE(observer.last_present_);
    356 
    357   EXPECT_TRUE(adapter_->IsPresent());
    358 
    359   // We should have had a device announced.
    360   EXPECT_EQ(1, observer.device_added_count_);
    361   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    362             observer.last_device_address_);
    363 
    364   // Other callbacks shouldn't be called if the values are false.
    365   EXPECT_EQ(0, observer.powered_changed_count_);
    366   EXPECT_EQ(0, observer.discovering_changed_count_);
    367   EXPECT_FALSE(adapter_->IsPowered());
    368   EXPECT_FALSE(adapter_->IsDiscovering());
    369 }
    370 
    371 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
    372   GetAdapter();
    373   ASSERT_TRUE(adapter_->IsPresent());
    374 
    375   // Install an observer; expect the AdapterPresentChanged to be called
    376   // with false, and IsPresent() to return false.
    377   TestObserver observer(adapter_);
    378   adapter_->AddObserver(&observer);
    379 
    380   fake_bluetooth_adapter_client_->SetVisible(false);
    381 
    382   EXPECT_EQ(1, observer.present_changed_count_);
    383   EXPECT_FALSE(observer.last_present_);
    384 
    385   EXPECT_FALSE(adapter_->IsPresent());
    386 
    387   // We should have had a device removed.
    388   EXPECT_EQ(1, observer.device_removed_count_);
    389   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    390             observer.last_device_address_);
    391 
    392   // Other callbacks shouldn't be called since the values are false.
    393   EXPECT_EQ(0, observer.powered_changed_count_);
    394   EXPECT_EQ(0, observer.discovering_changed_count_);
    395   EXPECT_FALSE(adapter_->IsPowered());
    396   EXPECT_FALSE(adapter_->IsDiscovering());
    397 }
    398 
    399 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
    400   GetAdapter();
    401   ASSERT_TRUE(adapter_->IsPresent());
    402 
    403   // Install an observer, then add a second adapter. Nothing should change,
    404   // we ignore the second adapter.
    405   TestObserver observer(adapter_);
    406   adapter_->AddObserver(&observer);
    407 
    408   fake_bluetooth_adapter_client_->SetSecondVisible(true);
    409 
    410   EXPECT_EQ(0, observer.present_changed_count_);
    411 
    412   EXPECT_TRUE(adapter_->IsPresent());
    413   EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
    414             adapter_->GetAddress());
    415 
    416   // Try removing the first adapter, we should now act as if the adapter
    417   // is no longer present rather than fall back to the second.
    418   fake_bluetooth_adapter_client_->SetVisible(false);
    419 
    420   EXPECT_EQ(1, observer.present_changed_count_);
    421   EXPECT_FALSE(observer.last_present_);
    422 
    423   EXPECT_FALSE(adapter_->IsPresent());
    424 
    425   // We should have had a device removed.
    426   EXPECT_EQ(1, observer.device_removed_count_);
    427   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    428             observer.last_device_address_);
    429 
    430   // Other callbacks shouldn't be called since the values are false.
    431   EXPECT_EQ(0, observer.powered_changed_count_);
    432   EXPECT_EQ(0, observer.discovering_changed_count_);
    433   EXPECT_FALSE(adapter_->IsPowered());
    434   EXPECT_FALSE(adapter_->IsDiscovering());
    435 
    436   observer.device_removed_count_ = 0;
    437 
    438   // Removing the second adapter shouldn't set anything either.
    439   fake_bluetooth_adapter_client_->SetSecondVisible(false);
    440 
    441   EXPECT_EQ(0, observer.device_removed_count_);
    442   EXPECT_EQ(0, observer.powered_changed_count_);
    443   EXPECT_EQ(0, observer.discovering_changed_count_);
    444 }
    445 
    446 TEST_F(BluetoothChromeOSTest, BecomePowered) {
    447   GetAdapter();
    448   ASSERT_FALSE(adapter_->IsPowered());
    449 
    450   // Install an observer; expect the AdapterPoweredChanged to be called
    451   // with true, and IsPowered() to return true.
    452   TestObserver observer(adapter_);
    453   adapter_->AddObserver(&observer);
    454 
    455   adapter_->SetPowered(
    456       true,
    457       base::Bind(&BluetoothChromeOSTest::Callback,
    458                  base::Unretained(this)),
    459       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    460                  base::Unretained(this)));
    461   EXPECT_EQ(1, callback_count_);
    462   EXPECT_EQ(0, error_callback_count_);
    463 
    464   EXPECT_EQ(1, observer.powered_changed_count_);
    465   EXPECT_TRUE(observer.last_powered_);
    466 
    467   EXPECT_TRUE(adapter_->IsPowered());
    468 }
    469 
    470 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
    471   GetAdapter();
    472   adapter_->SetPowered(
    473       true,
    474       base::Bind(&BluetoothChromeOSTest::Callback,
    475                  base::Unretained(this)),
    476       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    477                  base::Unretained(this)));
    478   EXPECT_EQ(1, callback_count_);
    479   EXPECT_EQ(0, error_callback_count_);
    480   callback_count_ = 0;
    481 
    482   ASSERT_TRUE(adapter_->IsPowered());
    483 
    484   // Install an observer; expect the AdapterPoweredChanged to be called
    485   // with false, and IsPowered() to return false.
    486   TestObserver observer(adapter_);
    487   adapter_->AddObserver(&observer);
    488 
    489   adapter_->SetPowered(
    490       false,
    491       base::Bind(&BluetoothChromeOSTest::Callback,
    492                  base::Unretained(this)),
    493       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    494                  base::Unretained(this)));
    495   EXPECT_EQ(1, callback_count_);
    496   EXPECT_EQ(0, error_callback_count_);
    497 
    498   EXPECT_EQ(1, observer.powered_changed_count_);
    499   EXPECT_FALSE(observer.last_powered_);
    500 
    501   EXPECT_FALSE(adapter_->IsPowered());
    502 }
    503 
    504 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
    505   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
    506 
    507   GetAdapter();
    508 
    509   adapter_->SetPowered(
    510       true,
    511       base::Bind(&BluetoothChromeOSTest::Callback,
    512                  base::Unretained(this)),
    513       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    514                  base::Unretained(this)));
    515   adapter_->StartDiscovering(
    516       base::Bind(&BluetoothChromeOSTest::Callback,
    517                  base::Unretained(this)),
    518       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    519                  base::Unretained(this)));
    520   EXPECT_EQ(2, callback_count_);
    521   EXPECT_EQ(0, error_callback_count_);
    522   callback_count_ = 0;
    523 
    524   ASSERT_TRUE(adapter_->IsPowered());
    525   ASSERT_TRUE(adapter_->IsDiscovering());
    526 
    527   // Install an observer; aside from the callback, expect the
    528   // AdapterDiscoveringChanged method to be called and no longer to be
    529   // discovering,
    530   TestObserver observer(adapter_);
    531   adapter_->AddObserver(&observer);
    532 
    533   adapter_->StopDiscovering(
    534       base::Bind(&BluetoothChromeOSTest::Callback,
    535                  base::Unretained(this)),
    536       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    537                  base::Unretained(this)));
    538   EXPECT_EQ(1, callback_count_);
    539   EXPECT_EQ(0, error_callback_count_);
    540 
    541   EXPECT_EQ(1, observer.discovering_changed_count_);
    542   EXPECT_FALSE(observer.last_discovering_);
    543 
    544   EXPECT_FALSE(adapter_->IsDiscovering());
    545 }
    546 
    547 TEST_F(BluetoothChromeOSTest, StopDiscoveryAfterTwoStarts) {
    548   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
    549 
    550   GetAdapter();
    551 
    552   adapter_->SetPowered(
    553       true,
    554       base::Bind(&BluetoothChromeOSTest::Callback,
    555                  base::Unretained(this)),
    556       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    557                  base::Unretained(this)));
    558   adapter_->StartDiscovering(
    559       base::Bind(&BluetoothChromeOSTest::Callback,
    560                  base::Unretained(this)),
    561       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    562                  base::Unretained(this)));
    563   EXPECT_EQ(2, callback_count_);
    564   EXPECT_EQ(0, error_callback_count_);
    565   callback_count_ = 0;
    566 
    567   ASSERT_TRUE(adapter_->IsPowered());
    568   ASSERT_TRUE(adapter_->IsDiscovering());
    569 
    570   // Install an observer and start discovering again; only the callback
    571   // should be called since we were already discovering to begin with.
    572   TestObserver observer(adapter_);
    573   adapter_->AddObserver(&observer);
    574 
    575   adapter_->StartDiscovering(
    576       base::Bind(&BluetoothChromeOSTest::Callback,
    577                  base::Unretained(this)),
    578       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    579                  base::Unretained(this)));
    580   EXPECT_EQ(1, callback_count_);
    581   EXPECT_EQ(0, error_callback_count_);
    582   callback_count_ = 0;
    583 
    584   EXPECT_EQ(0, observer.discovering_changed_count_);
    585 
    586   // Stop discovering; only the callback should be called since we're still
    587   // discovering. The adapter should be still discovering.
    588   adapter_->StopDiscovering(
    589       base::Bind(&BluetoothChromeOSTest::Callback,
    590                  base::Unretained(this)),
    591       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    592                  base::Unretained(this)));
    593   EXPECT_EQ(1, callback_count_);
    594   EXPECT_EQ(0, error_callback_count_);
    595   callback_count_ = 0;
    596 
    597   EXPECT_EQ(0, observer.discovering_changed_count_);
    598 
    599   EXPECT_TRUE(adapter_->IsDiscovering());
    600 
    601   // Stop discovering one more time; aside from the callback, expect the
    602   // AdapterDiscoveringChanged method to be called and no longer to be
    603   // discovering,
    604   adapter_->StopDiscovering(
    605       base::Bind(&BluetoothChromeOSTest::Callback,
    606                  base::Unretained(this)),
    607       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    608                  base::Unretained(this)));
    609   EXPECT_EQ(1, callback_count_);
    610   EXPECT_EQ(0, error_callback_count_);
    611 
    612   EXPECT_EQ(1, observer.discovering_changed_count_);
    613   EXPECT_FALSE(observer.last_discovering_);
    614 
    615   EXPECT_FALSE(adapter_->IsDiscovering());
    616 }
    617 
    618 TEST_F(BluetoothChromeOSTest, Discovery) {
    619   // Test a simulated discovery session.
    620   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
    621 
    622   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
    623   GetAdapter();
    624 
    625   TestObserver observer(adapter_);
    626   adapter_->AddObserver(&observer);
    627 
    628   adapter_->SetPowered(
    629       true,
    630       base::Bind(&BluetoothChromeOSTest::Callback,
    631                  base::Unretained(this)),
    632       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    633                  base::Unretained(this)));
    634   adapter_->StartDiscovering(
    635       base::Bind(&BluetoothChromeOSTest::Callback,
    636                  base::Unretained(this)),
    637       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    638                  base::Unretained(this)));
    639   EXPECT_EQ(2, callback_count_);
    640   EXPECT_EQ(0, error_callback_count_);
    641   callback_count_ = 0;
    642 
    643   ASSERT_TRUE(adapter_->IsPowered());
    644   ASSERT_TRUE(adapter_->IsDiscovering());
    645 
    646   // First device to appear should be an Apple Mouse.
    647   message_loop.Run();
    648 
    649   EXPECT_EQ(1, observer.device_added_count_);
    650   EXPECT_EQ(FakeBluetoothDeviceClient::kAppleMouseAddress,
    651             observer.last_device_address_);
    652 
    653   // Next we should get another two devices...
    654   message_loop.Run();
    655   EXPECT_EQ(3, observer.device_added_count_);
    656 
    657   // Okay, let's run forward until a device is actually removed...
    658   while (!observer.device_removed_count_)
    659     message_loop.Run();
    660 
    661   EXPECT_EQ(1, observer.device_removed_count_);
    662   EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
    663             observer.last_device_address_);
    664 }
    665 
    666 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
    667   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
    668 
    669   GetAdapter();
    670   adapter_->SetPowered(
    671       true,
    672       base::Bind(&BluetoothChromeOSTest::Callback,
    673                  base::Unretained(this)),
    674       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    675                  base::Unretained(this)));
    676   adapter_->StartDiscovering(
    677       base::Bind(&BluetoothChromeOSTest::Callback,
    678                  base::Unretained(this)),
    679       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    680                  base::Unretained(this)));
    681   EXPECT_EQ(2, callback_count_);
    682   EXPECT_EQ(0, error_callback_count_);
    683   callback_count_ = 0;
    684 
    685   // Stop the timers that the simulation uses
    686   fake_bluetooth_device_client_->EndDiscoverySimulation(
    687       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
    688 
    689   ASSERT_TRUE(adapter_->IsPowered());
    690   ASSERT_TRUE(adapter_->IsDiscovering());
    691 
    692   fake_bluetooth_adapter_client_->SetVisible(false);
    693   ASSERT_FALSE(adapter_->IsPresent());
    694 
    695   // Install an observer; expect the AdapterPresentChanged,
    696   // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
    697   // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
    698   // return true.
    699   TestObserver observer(adapter_);
    700   adapter_->AddObserver(&observer);
    701 
    702   fake_bluetooth_adapter_client_->SetVisible(true);
    703 
    704   EXPECT_EQ(1, observer.present_changed_count_);
    705   EXPECT_TRUE(observer.last_present_);
    706   EXPECT_TRUE(adapter_->IsPresent());
    707 
    708   EXPECT_EQ(1, observer.powered_changed_count_);
    709   EXPECT_TRUE(observer.last_powered_);
    710   EXPECT_TRUE(adapter_->IsPowered());
    711 
    712   EXPECT_EQ(1, observer.discovering_changed_count_);
    713   EXPECT_TRUE(observer.last_discovering_);
    714   EXPECT_TRUE(adapter_->IsDiscovering());
    715 
    716   observer.present_changed_count_ = 0;
    717   observer.powered_changed_count_ = 0;
    718   observer.discovering_changed_count_ = 0;
    719 
    720   // Now mark the adapter not present again. Expect the methods to be called
    721   // again, to reset the properties back to false
    722   fake_bluetooth_adapter_client_->SetVisible(false);
    723 
    724   EXPECT_EQ(1, observer.present_changed_count_);
    725   EXPECT_FALSE(observer.last_present_);
    726   EXPECT_FALSE(adapter_->IsPresent());
    727 
    728   EXPECT_EQ(1, observer.powered_changed_count_);
    729   EXPECT_FALSE(observer.last_powered_);
    730   EXPECT_FALSE(adapter_->IsPowered());
    731 
    732   EXPECT_EQ(1, observer.discovering_changed_count_);
    733   EXPECT_FALSE(observer.last_discovering_);
    734   EXPECT_FALSE(adapter_->IsDiscovering());
    735 }
    736 
    737 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
    738   GetAdapter();
    739 
    740   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    741   ASSERT_EQ(1U, devices.size());
    742   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    743             devices[0]->GetAddress());
    744 
    745   // Verify the other device properties.
    746   EXPECT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
    747             devices[0]->GetName());
    748   EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
    749   EXPECT_TRUE(devices[0]->IsPaired());
    750   EXPECT_FALSE(devices[0]->IsConnected());
    751   EXPECT_FALSE(devices[0]->IsConnecting());
    752 
    753   // Non HID devices are always connectable.
    754   EXPECT_TRUE(devices[0]->IsConnectable());
    755 
    756   BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
    757   ASSERT_EQ(2U, uuids.size());
    758   EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
    759   EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
    760 
    761   EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
    762   EXPECT_EQ(0x030d, devices[0]->GetProductID());
    763   EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
    764 }
    765 
    766 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
    767   // Simulate a change of class of a device, as sometimes occurs
    768   // during discovery.
    769   GetAdapter();
    770 
    771   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    772   ASSERT_EQ(1U, devices.size());
    773   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    774             devices[0]->GetAddress());
    775   ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
    776 
    777   // Install an observer; expect the DeviceChanged method to be called when
    778   // we change the class of the device.
    779   TestObserver observer(adapter_);
    780   adapter_->AddObserver(&observer);
    781 
    782   FakeBluetoothDeviceClient::Properties* properties =
    783       fake_bluetooth_device_client_->GetProperties(
    784           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
    785 
    786   properties->bluetooth_class.ReplaceValue(0x002580);
    787 
    788   EXPECT_EQ(1, observer.device_changed_count_);
    789   EXPECT_EQ(devices[0], observer.last_device_);
    790 
    791   EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
    792 }
    793 
    794 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
    795   // Simulate a change of name of a device.
    796   GetAdapter();
    797 
    798   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    799   ASSERT_EQ(1U, devices.size());
    800   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    801             devices[0]->GetAddress());
    802   ASSERT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
    803             devices[0]->GetName());
    804 
    805   // Install an observer; expect the DeviceChanged method to be called when
    806   // we change the alias of the device.
    807   TestObserver observer(adapter_);
    808   adapter_->AddObserver(&observer);
    809 
    810   FakeBluetoothDeviceClient::Properties* properties =
    811       fake_bluetooth_device_client_->GetProperties(
    812           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
    813 
    814   static const std::string new_name("New Device Name");
    815   properties->alias.ReplaceValue(new_name);
    816 
    817   EXPECT_EQ(1, observer.device_changed_count_);
    818   EXPECT_EQ(devices[0], observer.last_device_);
    819 
    820   EXPECT_EQ(UTF8ToUTF16(new_name), devices[0]->GetName());
    821 }
    822 
    823 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
    824   // Simulate a change of advertised services of a device.
    825   GetAdapter();
    826 
    827   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    828   ASSERT_EQ(1U, devices.size());
    829   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    830             devices[0]->GetAddress());
    831 
    832   BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
    833   ASSERT_EQ(2U, uuids.size());
    834   ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
    835   ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
    836 
    837   // Install an observer; expect the DeviceChanged method to be called when
    838   // we change the class of the device.
    839   TestObserver observer(adapter_);
    840   adapter_->AddObserver(&observer);
    841 
    842   FakeBluetoothDeviceClient::Properties* properties =
    843       fake_bluetooth_device_client_->GetProperties(
    844           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
    845 
    846   uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
    847   uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
    848   uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
    849 
    850   properties->uuids.ReplaceValue(uuids);
    851 
    852   EXPECT_EQ(1, observer.device_changed_count_);
    853   EXPECT_EQ(devices[0], observer.last_device_);
    854 
    855   // Fetching the value should give the new one.
    856   uuids = devices[0]->GetServices();
    857   ASSERT_EQ(5U, uuids.size());
    858   EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
    859   EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
    860   EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb");
    861   EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb");
    862   EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb");
    863 }
    864 
    865 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
    866   GetAdapter();
    867 
    868   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
    869   ASSERT_EQ(1U, devices.size());
    870   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
    871             devices[0]->GetAddress());
    872 
    873   std::string address = devices[0]->GetAddress();
    874 
    875   // Install an observer; expect the DeviceRemoved method to be called
    876   // with the device we remove.
    877   TestObserver observer(adapter_);
    878   adapter_->AddObserver(&observer);
    879 
    880   devices[0]->Forget(
    881       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    882                  base::Unretained(this)));
    883   EXPECT_EQ(0, error_callback_count_);
    884 
    885   EXPECT_EQ(1, observer.device_removed_count_);
    886   EXPECT_EQ(address, observer.last_device_address_);
    887 
    888   // GetDevices shouldn't return the device either.
    889   devices = adapter_->GetDevices();
    890   ASSERT_EQ(0U, devices.size());
    891 }
    892 
    893 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
    894   GetAdapter();
    895   DiscoverDevices();
    896 
    897   BluetoothDevice* device = adapter_->GetDevice(
    898       FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
    899   ASSERT_TRUE(device != NULL);
    900   ASSERT_FALSE(device->IsPaired());
    901 
    902   // Connect the device so it becomes trusted and remembered.
    903   device->Connect(
    904       NULL,
    905       base::Bind(&BluetoothChromeOSTest::Callback,
    906                  base::Unretained(this)),
    907       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
    908                  base::Unretained(this)));
    909 
    910   ASSERT_EQ(1, callback_count_);
    911   ASSERT_EQ(0, error_callback_count_);
    912   callback_count_ = 0;
    913 
    914   ASSERT_TRUE(device->IsConnected());
    915   ASSERT_FALSE(device->IsConnecting());
    916 
    917   // Make sure the trusted property has been set to true.
    918   FakeBluetoothDeviceClient::Properties* properties =
    919       fake_bluetooth_device_client_->GetProperties(
    920           dbus::ObjectPath(FakeBluetoothDeviceClient::kMicrosoftMousePath));
    921   ASSERT_TRUE(properties->trusted.value());
    922 
    923   // Install an observer; expect the DeviceRemoved method to be called
    924   // with the device we remove.
    925   TestObserver observer(adapter_);
    926   adapter_->AddObserver(&observer);
    927 
    928   device->Forget(
    929       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
    930                  base::Unretained(this)));
    931   EXPECT_EQ(0, error_callback_count_);
    932 
    933   EXPECT_EQ(1, observer.device_removed_count_);
    934   EXPECT_EQ(FakeBluetoothDeviceClient::kMicrosoftMouseAddress,
    935             observer.last_device_address_);
    936 
    937   // GetDevices shouldn't return the device either.
    938   device = adapter_->GetDevice(
    939       FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
    940   EXPECT_FALSE(device != NULL);
    941 }
    942 
    943 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
    944   GetAdapter();
    945 
    946   BluetoothDevice* device = adapter_->GetDevice(
    947       FakeBluetoothDeviceClient::kPairedDeviceAddress);
    948   ASSERT_TRUE(device != NULL);
    949   ASSERT_TRUE(device->IsPaired());
    950 
    951   TestObserver observer(adapter_);
    952   adapter_->AddObserver(&observer);
    953 
    954   // Connect without a pairing delegate; since the device is already Paired
    955   // this should succeed and the device should become connected.
    956   device->Connect(
    957       NULL,
    958       base::Bind(&BluetoothChromeOSTest::Callback,
    959                  base::Unretained(this)),
    960       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
    961                  base::Unretained(this)));
    962 
    963   EXPECT_EQ(1, callback_count_);
    964   EXPECT_EQ(0, error_callback_count_);
    965 
    966   // Two changes for connecting, one for connected and one for for trusted
    967   // after connecting.
    968   EXPECT_EQ(4, observer.device_changed_count_);
    969   EXPECT_EQ(device, observer.last_device_);
    970 
    971   EXPECT_TRUE(device->IsConnected());
    972   EXPECT_FALSE(device->IsConnecting());
    973 }
    974 
    975 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
    976   GetAdapter();
    977   DiscoverDevices();
    978 
    979   BluetoothDevice* device = adapter_->GetDevice(
    980       FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
    981   ASSERT_TRUE(device != NULL);
    982   ASSERT_FALSE(device->IsPaired());
    983 
    984   TestObserver observer(adapter_);
    985   adapter_->AddObserver(&observer);
    986 
    987   // Connect without a pairing delegate; since the device does not require
    988   // pairing, this should succeed and the device should become connected.
    989   device->Connect(
    990       NULL,
    991       base::Bind(&BluetoothChromeOSTest::Callback,
    992                  base::Unretained(this)),
    993       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
    994                  base::Unretained(this)));
    995 
    996   EXPECT_EQ(1, callback_count_);
    997   EXPECT_EQ(0, error_callback_count_);
    998 
    999   // Two changes for connecting, one for connected, one for for trusted after
   1000   // connection, and one for the reconnect mode (IsConnectable).
   1001   EXPECT_EQ(5, observer.device_changed_count_);
   1002   EXPECT_EQ(device, observer.last_device_);
   1003 
   1004   EXPECT_TRUE(device->IsConnected());
   1005   EXPECT_FALSE(device->IsConnecting());
   1006 
   1007   // Make sure the trusted property has been set to true.
   1008   FakeBluetoothDeviceClient::Properties* properties =
   1009       fake_bluetooth_device_client_->GetProperties(
   1010           dbus::ObjectPath(FakeBluetoothDeviceClient::kMicrosoftMousePath));
   1011   EXPECT_TRUE(properties->trusted.value());
   1012 
   1013   // Verify is a HID device and is not connectable.
   1014   BluetoothDevice::ServiceList uuids = device->GetServices();
   1015   ASSERT_EQ(1U, uuids.size());
   1016   EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
   1017   EXPECT_FALSE(device->IsConnectable());
   1018 }
   1019 
   1020 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
   1021   GetAdapter();
   1022 
   1023   BluetoothDevice* device = adapter_->GetDevice(
   1024       FakeBluetoothDeviceClient::kPairedDeviceAddress);
   1025   ASSERT_TRUE(device != NULL);
   1026   ASSERT_TRUE(device->IsPaired());
   1027 
   1028   device->Connect(
   1029       NULL,
   1030       base::Bind(&BluetoothChromeOSTest::Callback,
   1031                  base::Unretained(this)),
   1032       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1033                  base::Unretained(this)));
   1034 
   1035   ASSERT_EQ(1, callback_count_);
   1036   ASSERT_EQ(0, error_callback_count_);
   1037   callback_count_ = 0;
   1038 
   1039   ASSERT_TRUE(device->IsConnected());
   1040 
   1041   // Connect again; since the device is already Connected, this shouldn't do
   1042   // anything to initiate the connection.
   1043   TestObserver observer(adapter_);
   1044   adapter_->AddObserver(&observer);
   1045 
   1046   device->Connect(
   1047       NULL,
   1048       base::Bind(&BluetoothChromeOSTest::Callback,
   1049                  base::Unretained(this)),
   1050       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1051                  base::Unretained(this)));
   1052 
   1053   EXPECT_EQ(1, callback_count_);
   1054   EXPECT_EQ(0, error_callback_count_);
   1055 
   1056   // The observer will be called because Connecting will toggle true and false,
   1057   // and the trusted property will be updated to true.
   1058   EXPECT_EQ(3, observer.device_changed_count_);
   1059 
   1060   EXPECT_TRUE(device->IsConnected());
   1061   EXPECT_FALSE(device->IsConnecting());
   1062 }
   1063 
   1064 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
   1065   GetAdapter();
   1066   DiscoverDevices();
   1067 
   1068   BluetoothDevice* device = adapter_->GetDevice(
   1069       FakeBluetoothDeviceClient::kAppleMouseAddress);
   1070   ASSERT_TRUE(device != NULL);
   1071   ASSERT_FALSE(device->IsPaired());
   1072 
   1073   TestObserver observer(adapter_);
   1074   adapter_->AddObserver(&observer);
   1075 
   1076   // Connect without a pairing delegate; since the device requires pairing,
   1077   // this should fail with an error.
   1078   device->Connect(
   1079       NULL,
   1080       base::Bind(&BluetoothChromeOSTest::Callback,
   1081                  base::Unretained(this)),
   1082       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1083                  base::Unretained(this)));
   1084 
   1085   EXPECT_EQ(0, callback_count_);
   1086   EXPECT_EQ(1, error_callback_count_);
   1087   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
   1088 
   1089   EXPECT_EQ(2, observer.device_changed_count_);
   1090 
   1091   EXPECT_FALSE(device->IsConnected());
   1092   EXPECT_FALSE(device->IsConnecting());
   1093 }
   1094 
   1095 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
   1096   GetAdapter();
   1097 
   1098   BluetoothDevice* device = adapter_->GetDevice(
   1099       FakeBluetoothDeviceClient::kPairedDeviceAddress);
   1100   ASSERT_TRUE(device != NULL);
   1101   ASSERT_TRUE(device->IsPaired());
   1102 
   1103   device->Connect(
   1104       NULL,
   1105       base::Bind(&BluetoothChromeOSTest::Callback,
   1106                  base::Unretained(this)),
   1107       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1108                  base::Unretained(this)));
   1109 
   1110   ASSERT_EQ(1, callback_count_);
   1111   ASSERT_EQ(0, error_callback_count_);
   1112   callback_count_ = 0;
   1113 
   1114   ASSERT_TRUE(device->IsConnected());
   1115   ASSERT_FALSE(device->IsConnecting());
   1116 
   1117   // Disconnect the device, we should see the observer method fire and the
   1118   // device get dropped.
   1119   TestObserver observer(adapter_);
   1120   adapter_->AddObserver(&observer);
   1121 
   1122   device->Disconnect(
   1123       base::Bind(&BluetoothChromeOSTest::Callback,
   1124                  base::Unretained(this)),
   1125       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
   1126                  base::Unretained(this)));
   1127 
   1128   EXPECT_EQ(1, callback_count_);
   1129   EXPECT_EQ(0, error_callback_count_);
   1130 
   1131   EXPECT_EQ(1, observer.device_changed_count_);
   1132   EXPECT_EQ(device, observer.last_device_);
   1133 
   1134   EXPECT_FALSE(device->IsConnected());
   1135 }
   1136 
   1137 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
   1138   GetAdapter();
   1139 
   1140   BluetoothDevice* device = adapter_->GetDevice(
   1141       FakeBluetoothDeviceClient::kPairedDeviceAddress);
   1142   ASSERT_TRUE(device != NULL);
   1143   ASSERT_TRUE(device->IsPaired());
   1144   ASSERT_FALSE(device->IsConnected());
   1145 
   1146   // Disconnect the device, we should see the observer method fire and the
   1147   // device get dropped.
   1148   TestObserver observer(adapter_);
   1149   adapter_->AddObserver(&observer);
   1150 
   1151   device->Disconnect(
   1152       base::Bind(&BluetoothChromeOSTest::Callback,
   1153                  base::Unretained(this)),
   1154       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
   1155                  base::Unretained(this)));
   1156 
   1157   EXPECT_EQ(0, callback_count_);
   1158   EXPECT_EQ(1, error_callback_count_);
   1159 
   1160   EXPECT_EQ(0, observer.device_changed_count_);
   1161 
   1162   EXPECT_FALSE(device->IsConnected());
   1163 }
   1164 
   1165 TEST_F(BluetoothChromeOSTest, PairAppleMouse) {
   1166   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1167   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1168 
   1169   GetAdapter();
   1170   DiscoverDevices();
   1171 
   1172   // The Apple Mouse requires no PIN or Passkey to pair; this is equivalent
   1173   // to Simple Secure Pairing or a device with a fixed 0000 PIN.
   1174   BluetoothDevice* device = adapter_->GetDevice(
   1175       FakeBluetoothDeviceClient::kAppleMouseAddress);
   1176   ASSERT_TRUE(device != NULL);
   1177   ASSERT_FALSE(device->IsPaired());
   1178 
   1179   TestObserver observer(adapter_);
   1180   adapter_->AddObserver(&observer);
   1181 
   1182   TestPairingDelegate pairing_delegate;
   1183   device->Connect(
   1184       &pairing_delegate,
   1185       base::Bind(&BluetoothChromeOSTest::Callback,
   1186                  base::Unretained(this)),
   1187       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1188                  base::Unretained(this)));
   1189 
   1190   EXPECT_EQ(0, pairing_delegate.call_count_);
   1191   EXPECT_TRUE(device->IsConnecting());
   1192 
   1193   message_loop.Run();
   1194 
   1195   EXPECT_EQ(1, callback_count_);
   1196   EXPECT_EQ(0, error_callback_count_);
   1197 
   1198   // Two changes for connecting, one change for connected, one for paired,
   1199   // two for trusted (after pairing and connection), and one for the reconnect
   1200   // mode (IsConnectable).
   1201   EXPECT_EQ(7, observer.device_changed_count_);
   1202   EXPECT_EQ(device, observer.last_device_);
   1203 
   1204   EXPECT_TRUE(device->IsConnected());
   1205   EXPECT_FALSE(device->IsConnecting());
   1206 
   1207   EXPECT_TRUE(device->IsPaired());
   1208 
   1209   // Verify is a HID device and is connectable.
   1210   BluetoothDevice::ServiceList uuids = device->GetServices();
   1211   ASSERT_EQ(1U, uuids.size());
   1212   EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
   1213   EXPECT_TRUE(device->IsConnectable());
   1214 
   1215   // Pairing dialog should be dismissed
   1216   EXPECT_EQ(1, pairing_delegate.call_count_);
   1217   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1218 
   1219   // Make sure the trusted property has been set to true.
   1220   FakeBluetoothDeviceClient::Properties* properties =
   1221       fake_bluetooth_device_client_->GetProperties(
   1222           dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleMousePath));
   1223   EXPECT_TRUE(properties->trusted.value());
   1224 }
   1225 
   1226 TEST_F(BluetoothChromeOSTest, PairAppleKeyboard) {
   1227   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1228   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1229 
   1230   GetAdapter();
   1231   DiscoverDevices();
   1232 
   1233   // The Apple Keyboard requires that we display a randomly generated
   1234   // PIN on the screen.
   1235   BluetoothDevice* device = adapter_->GetDevice(
   1236       FakeBluetoothDeviceClient::kAppleKeyboardAddress);
   1237   ASSERT_TRUE(device != NULL);
   1238   ASSERT_FALSE(device->IsPaired());
   1239 
   1240   TestObserver observer(adapter_);
   1241   adapter_->AddObserver(&observer);
   1242 
   1243   TestPairingDelegate pairing_delegate;
   1244   device->Connect(
   1245       &pairing_delegate,
   1246       base::Bind(&BluetoothChromeOSTest::Callback,
   1247                  base::Unretained(this)),
   1248       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1249                  base::Unretained(this)));
   1250 
   1251   EXPECT_EQ(1, pairing_delegate.call_count_);
   1252   EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
   1253   EXPECT_EQ("123456", pairing_delegate.last_pincode_);
   1254   EXPECT_TRUE(device->IsConnecting());
   1255 
   1256   message_loop.Run();
   1257 
   1258   EXPECT_EQ(1, callback_count_);
   1259   EXPECT_EQ(0, error_callback_count_);
   1260 
   1261   // Two changes for connecting, one change for connected, one for paired,
   1262   // two for trusted (after pairing and connection), and one for the reconnect
   1263   // mode (IsConnectable).
   1264   EXPECT_EQ(7, observer.device_changed_count_);
   1265   EXPECT_EQ(device, observer.last_device_);
   1266 
   1267   EXPECT_TRUE(device->IsConnected());
   1268   EXPECT_FALSE(device->IsConnecting());
   1269 
   1270   EXPECT_TRUE(device->IsPaired());
   1271 
   1272   // Verify is a HID device and is connectable.
   1273   BluetoothDevice::ServiceList uuids = device->GetServices();
   1274   ASSERT_EQ(1U, uuids.size());
   1275   EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
   1276   EXPECT_TRUE(device->IsConnectable());
   1277 
   1278   // Pairing dialog should be dismissed
   1279   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1280 
   1281   // Make sure the trusted property has been set to true.
   1282   FakeBluetoothDeviceClient::Properties* properties =
   1283       fake_bluetooth_device_client_->GetProperties(
   1284           dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleKeyboardPath));
   1285   EXPECT_TRUE(properties->trusted.value());
   1286 }
   1287 
   1288 TEST_F(BluetoothChromeOSTest, PairMotorolaKeyboard) {
   1289   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1290   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1291 
   1292   GetAdapter();
   1293   DiscoverDevices();
   1294 
   1295   // The Motorola Keyboard requires that we display a randomly generated
   1296   // Passkey on the screen, and notifies us as it's typed in.
   1297   BluetoothDevice* device = adapter_->GetDevice(
   1298       FakeBluetoothDeviceClient::kMotorolaKeyboardAddress);
   1299   ASSERT_TRUE(device != NULL);
   1300   ASSERT_FALSE(device->IsPaired());
   1301 
   1302   TestObserver observer(adapter_);
   1303   adapter_->AddObserver(&observer);
   1304 
   1305   TestPairingDelegate pairing_delegate;
   1306   device->Connect(
   1307       &pairing_delegate,
   1308       base::Bind(&BluetoothChromeOSTest::Callback,
   1309                  base::Unretained(this)),
   1310       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1311                  base::Unretained(this)));
   1312 
   1313   // One call for DisplayPasskey() and one for KeysEntered().
   1314   EXPECT_EQ(2, pairing_delegate.call_count_);
   1315   EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
   1316   EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
   1317   EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
   1318   EXPECT_EQ(0U, pairing_delegate.last_entered_);
   1319 
   1320   EXPECT_TRUE(device->IsConnecting());
   1321 
   1322   // One call to KeysEntered() for each key, including [enter].
   1323   for(int i = 1; i <= 7; ++i) {
   1324     message_loop.Run();
   1325 
   1326     EXPECT_EQ(2 + i, pairing_delegate.call_count_);
   1327     EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
   1328     EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
   1329   }
   1330 
   1331   message_loop.Run();
   1332 
   1333   // 8 KeysEntered notifications (0 to 7, inclusive). Two aditional calls for
   1334   // DisplayPasskey() and DismissDisplayOrConfirm().
   1335   EXPECT_EQ(10, pairing_delegate.call_count_);
   1336   EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
   1337   EXPECT_EQ(7U, pairing_delegate.last_entered_);
   1338 
   1339   EXPECT_EQ(1, callback_count_);
   1340   EXPECT_EQ(0, error_callback_count_);
   1341 
   1342   // Two changes for connecting, one change for connected, one for paired,
   1343   // two for trusted (after pairing and connection), and one for the reconnect
   1344   // mode (IsConnectable).
   1345   EXPECT_EQ(7, observer.device_changed_count_);
   1346   EXPECT_EQ(device, observer.last_device_);
   1347 
   1348   EXPECT_TRUE(device->IsConnected());
   1349   EXPECT_FALSE(device->IsConnecting());
   1350 
   1351   EXPECT_TRUE(device->IsPaired());
   1352 
   1353   // Verify is a HID device.
   1354   BluetoothDevice::ServiceList uuids = device->GetServices();
   1355   ASSERT_EQ(1U, uuids.size());
   1356   EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
   1357 
   1358   // Fake MotorolaKeyboard is not connectable.
   1359   EXPECT_FALSE(device->IsConnectable());
   1360 
   1361   // Pairing dialog should be dismissed
   1362   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1363 
   1364   // Make sure the trusted property has been set to true.
   1365   FakeBluetoothDeviceClient::Properties* properties =
   1366       fake_bluetooth_device_client_->GetProperties(
   1367           dbus::ObjectPath(FakeBluetoothDeviceClient::kMotorolaKeyboardPath));
   1368   EXPECT_TRUE(properties->trusted.value());
   1369 }
   1370 
   1371 TEST_F(BluetoothChromeOSTest, PairSonyHeadphones) {
   1372   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1373   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1374 
   1375   GetAdapter();
   1376   DiscoverDevices();
   1377 
   1378   // The Sony Headphones fake requires that the user enters a PIN for them.
   1379   BluetoothDevice* device = adapter_->GetDevice(
   1380       FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
   1381   ASSERT_TRUE(device != NULL);
   1382   ASSERT_FALSE(device->IsPaired());
   1383 
   1384   TestObserver observer(adapter_);
   1385   adapter_->AddObserver(&observer);
   1386 
   1387   TestPairingDelegate pairing_delegate;
   1388   device->Connect(
   1389       &pairing_delegate,
   1390       base::Bind(&BluetoothChromeOSTest::Callback,
   1391                  base::Unretained(this)),
   1392       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1393                  base::Unretained(this)));
   1394 
   1395   EXPECT_EQ(1, pairing_delegate.call_count_);
   1396   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
   1397   EXPECT_TRUE(device->IsConnecting());
   1398 
   1399   // Set the PIN.
   1400   device->SetPinCode("1234");
   1401   message_loop.Run();
   1402 
   1403   EXPECT_EQ(1, callback_count_);
   1404   EXPECT_EQ(0, error_callback_count_);
   1405 
   1406   // Two changes for connecting, one change for connected, one for paired and
   1407   // two for trusted (after pairing and connection).
   1408   EXPECT_EQ(6, observer.device_changed_count_);
   1409   EXPECT_EQ(device, observer.last_device_);
   1410 
   1411   EXPECT_TRUE(device->IsConnected());
   1412   EXPECT_FALSE(device->IsConnecting());
   1413 
   1414   EXPECT_TRUE(device->IsPaired());
   1415 
   1416   // Verify is not a HID device.
   1417   BluetoothDevice::ServiceList uuids = device->GetServices();
   1418   ASSERT_EQ(0U, uuids.size());
   1419 
   1420   // Non HID devices are always connectable.
   1421   EXPECT_TRUE(device->IsConnectable());
   1422 
   1423   // Pairing dialog should be dismissed
   1424   EXPECT_EQ(2, pairing_delegate.call_count_);
   1425   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1426 
   1427   // Make sure the trusted property has been set to true.
   1428   FakeBluetoothDeviceClient::Properties* properties =
   1429       fake_bluetooth_device_client_->GetProperties(
   1430           dbus::ObjectPath(FakeBluetoothDeviceClient::kSonyHeadphonesPath));
   1431   EXPECT_TRUE(properties->trusted.value());
   1432 }
   1433 
   1434 TEST_F(BluetoothChromeOSTest, PairPhone) {
   1435   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1436   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1437 
   1438   GetAdapter();
   1439   DiscoverDevices();
   1440 
   1441   // The fake phone requests that we confirm a displayed passkey.
   1442   BluetoothDevice* device = adapter_->GetDevice(
   1443       FakeBluetoothDeviceClient::kPhoneAddress);
   1444   ASSERT_TRUE(device != NULL);
   1445   ASSERT_FALSE(device->IsPaired());
   1446 
   1447   TestObserver observer(adapter_);
   1448   adapter_->AddObserver(&observer);
   1449 
   1450   TestPairingDelegate pairing_delegate;
   1451   device->Connect(
   1452       &pairing_delegate,
   1453       base::Bind(&BluetoothChromeOSTest::Callback,
   1454                  base::Unretained(this)),
   1455       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1456                  base::Unretained(this)));
   1457 
   1458   EXPECT_EQ(1, pairing_delegate.call_count_);
   1459   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
   1460   EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
   1461   EXPECT_TRUE(device->IsConnecting());
   1462 
   1463   // Confirm the passkey.
   1464   device->ConfirmPairing();
   1465   message_loop.Run();
   1466 
   1467   EXPECT_EQ(1, callback_count_);
   1468   EXPECT_EQ(0, error_callback_count_);
   1469 
   1470   // Two changes for connecting, one change for connected, one for paired and
   1471   // two for trusted (after pairing and connection).
   1472   EXPECT_EQ(6, observer.device_changed_count_);
   1473   EXPECT_EQ(device, observer.last_device_);
   1474 
   1475   EXPECT_TRUE(device->IsConnected());
   1476   EXPECT_FALSE(device->IsConnecting());
   1477 
   1478   EXPECT_TRUE(device->IsPaired());
   1479 
   1480   // Non HID devices are always connectable.
   1481   EXPECT_TRUE(device->IsConnectable());
   1482 
   1483   // Pairing dialog should be dismissed
   1484   EXPECT_EQ(2, pairing_delegate.call_count_);
   1485   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1486 
   1487   // Make sure the trusted property has been set to true.
   1488   FakeBluetoothDeviceClient::Properties* properties =
   1489       fake_bluetooth_device_client_->GetProperties(
   1490           dbus::ObjectPath(FakeBluetoothDeviceClient::kPhonePath));
   1491   EXPECT_TRUE(properties->trusted.value());
   1492 }
   1493 
   1494 TEST_F(BluetoothChromeOSTest, PairWeirdDevice) {
   1495   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1496   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1497 
   1498   GetAdapter();
   1499   DiscoverDevices();
   1500 
   1501   // Use the "weird device" fake that requires that the user enters a Passkey,
   1502   // this would be some kind of device that has a display, but doesn't use
   1503   // "just works" - maybe a car?
   1504   BluetoothDevice* device = adapter_->GetDevice(
   1505       FakeBluetoothDeviceClient::kWeirdDeviceAddress);
   1506   ASSERT_TRUE(device != NULL);
   1507   ASSERT_FALSE(device->IsPaired());
   1508 
   1509   TestObserver observer(adapter_);
   1510   adapter_->AddObserver(&observer);
   1511 
   1512   TestPairingDelegate pairing_delegate;
   1513   device->Connect(
   1514       &pairing_delegate,
   1515       base::Bind(&BluetoothChromeOSTest::Callback,
   1516                  base::Unretained(this)),
   1517       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1518                  base::Unretained(this)));
   1519 
   1520   EXPECT_EQ(1, pairing_delegate.call_count_);
   1521   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
   1522   EXPECT_TRUE(device->IsConnecting());
   1523 
   1524   // Set the Passkey.
   1525   device->SetPasskey(1234);
   1526   message_loop.Run();
   1527 
   1528   EXPECT_EQ(1, callback_count_);
   1529   EXPECT_EQ(0, error_callback_count_);
   1530 
   1531   // Two changes for connecting, one change for connected, one for paired and
   1532   // two for trusted (after pairing and connection).
   1533   EXPECT_EQ(6, observer.device_changed_count_);
   1534   EXPECT_EQ(device, observer.last_device_);
   1535 
   1536   EXPECT_TRUE(device->IsConnected());
   1537   EXPECT_FALSE(device->IsConnecting());
   1538 
   1539   EXPECT_TRUE(device->IsPaired());
   1540 
   1541   // Non HID devices are always connectable.
   1542   EXPECT_TRUE(device->IsConnectable());
   1543 
   1544   // Pairing dialog should be dismissed
   1545   EXPECT_EQ(2, pairing_delegate.call_count_);
   1546   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1547 
   1548   // Make sure the trusted property has been set to true.
   1549   FakeBluetoothDeviceClient::Properties* properties =
   1550       fake_bluetooth_device_client_->GetProperties(
   1551           dbus::ObjectPath(FakeBluetoothDeviceClient::kWeirdDevicePath));
   1552   EXPECT_TRUE(properties->trusted.value());
   1553 }
   1554 
   1555 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
   1556   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1557   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1558 
   1559   GetAdapter();
   1560   DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
   1561 
   1562   BluetoothDevice* device = adapter_->GetDevice(
   1563       FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
   1564   ASSERT_TRUE(device != NULL);
   1565   ASSERT_FALSE(device->IsPaired());
   1566 
   1567   TestObserver observer(adapter_);
   1568   adapter_->AddObserver(&observer);
   1569 
   1570   TestPairingDelegate pairing_delegate;
   1571   device->Connect(
   1572       &pairing_delegate,
   1573       base::Bind(&BluetoothChromeOSTest::Callback,
   1574                  base::Unretained(this)),
   1575       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1576                  base::Unretained(this)));
   1577 
   1578   EXPECT_EQ(0, pairing_delegate.call_count_);
   1579   EXPECT_TRUE(device->IsConnecting());
   1580 
   1581   // Run the loop to get the error..
   1582   message_loop.Run();
   1583 
   1584   EXPECT_EQ(0, callback_count_);
   1585   EXPECT_EQ(1, error_callback_count_);
   1586 
   1587   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
   1588 
   1589   EXPECT_FALSE(device->IsConnected());
   1590   EXPECT_FALSE(device->IsConnecting());
   1591   EXPECT_FALSE(device->IsPaired());
   1592 
   1593   // Pairing dialog should be dismissed
   1594   EXPECT_EQ(1, pairing_delegate.call_count_);
   1595   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1596 }
   1597 
   1598 TEST_F(BluetoothChromeOSTest, PairingFails) {
   1599   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1600   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1601 
   1602   GetAdapter();
   1603   DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
   1604 
   1605   // The vanishing device times out during pairing
   1606   BluetoothDevice* device = adapter_->GetDevice(
   1607       FakeBluetoothDeviceClient::kVanishingDeviceAddress);
   1608   ASSERT_TRUE(device != NULL);
   1609   ASSERT_FALSE(device->IsPaired());
   1610 
   1611   TestObserver observer(adapter_);
   1612   adapter_->AddObserver(&observer);
   1613 
   1614   TestPairingDelegate pairing_delegate;
   1615   device->Connect(
   1616       &pairing_delegate,
   1617       base::Bind(&BluetoothChromeOSTest::Callback,
   1618                  base::Unretained(this)),
   1619       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1620                  base::Unretained(this)));
   1621 
   1622   EXPECT_EQ(0, pairing_delegate.call_count_);
   1623   EXPECT_TRUE(device->IsConnecting());
   1624 
   1625   // Run the loop to get the error..
   1626   message_loop.Run();
   1627 
   1628   EXPECT_EQ(0, callback_count_);
   1629   EXPECT_EQ(1, error_callback_count_);
   1630 
   1631   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
   1632 
   1633   EXPECT_FALSE(device->IsConnected());
   1634   EXPECT_FALSE(device->IsConnecting());
   1635   EXPECT_FALSE(device->IsPaired());
   1636 
   1637   // Pairing dialog should be dismissed
   1638   EXPECT_EQ(1, pairing_delegate.call_count_);
   1639   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1640 }
   1641 
   1642 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
   1643   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1644   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1645 
   1646   GetAdapter();
   1647   DiscoverDevices();
   1648 
   1649   // Everything seems to go according to plan with the unconnectable device;
   1650   // it pairs, but then you can't make connections to it after.
   1651   BluetoothDevice* device = adapter_->GetDevice(
   1652       FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
   1653   ASSERT_TRUE(device != NULL);
   1654   ASSERT_FALSE(device->IsPaired());
   1655 
   1656   TestObserver observer(adapter_);
   1657   adapter_->AddObserver(&observer);
   1658 
   1659   TestPairingDelegate pairing_delegate;
   1660   device->Connect(
   1661       &pairing_delegate,
   1662       base::Bind(&BluetoothChromeOSTest::Callback,
   1663                  base::Unretained(this)),
   1664       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1665                  base::Unretained(this)));
   1666 
   1667   EXPECT_EQ(0, pairing_delegate.call_count_);
   1668   EXPECT_TRUE(device->IsConnecting());
   1669 
   1670   message_loop.Run();
   1671 
   1672   EXPECT_EQ(0, callback_count_);
   1673   EXPECT_EQ(1, error_callback_count_);
   1674   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
   1675 
   1676   // Two changes for connecting, one for paired and one for trusted after
   1677   // pairing. The device should not be connected.
   1678   EXPECT_EQ(4, observer.device_changed_count_);
   1679   EXPECT_EQ(device, observer.last_device_);
   1680 
   1681   EXPECT_FALSE(device->IsConnected());
   1682   EXPECT_FALSE(device->IsConnecting());
   1683 
   1684   EXPECT_TRUE(device->IsPaired());
   1685 
   1686   // Pairing dialog should be dismissed
   1687   EXPECT_EQ(1, pairing_delegate.call_count_);
   1688   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1689 
   1690   // Make sure the trusted property has been set to true still (since pairing
   1691   // worked).
   1692   FakeBluetoothDeviceClient::Properties* properties =
   1693       fake_bluetooth_device_client_->GetProperties(
   1694           dbus::ObjectPath(
   1695               FakeBluetoothDeviceClient::kUnconnectableDevicePath));
   1696   EXPECT_TRUE(properties->trusted.value());
   1697 }
   1698 
   1699 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
   1700   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1701   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1702 
   1703   GetAdapter();
   1704   DiscoverDevices();
   1705 
   1706   // Reject the pairing after we receive a request for the PIN code.
   1707   BluetoothDevice* device = adapter_->GetDevice(
   1708       FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
   1709   ASSERT_TRUE(device != NULL);
   1710   ASSERT_FALSE(device->IsPaired());
   1711 
   1712   TestObserver observer(adapter_);
   1713   adapter_->AddObserver(&observer);
   1714 
   1715   TestPairingDelegate pairing_delegate;
   1716   device->Connect(
   1717       &pairing_delegate,
   1718       base::Bind(&BluetoothChromeOSTest::Callback,
   1719                  base::Unretained(this)),
   1720       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1721                  base::Unretained(this)));
   1722 
   1723   EXPECT_EQ(1, pairing_delegate.call_count_);
   1724   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
   1725   EXPECT_TRUE(device->IsConnecting());
   1726 
   1727   // Reject the pairing.
   1728   device->RejectPairing();
   1729   message_loop.Run();
   1730 
   1731   EXPECT_EQ(0, callback_count_);
   1732   EXPECT_EQ(1, error_callback_count_);
   1733   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
   1734 
   1735   // Should be no changes except connecting going true and false.
   1736   EXPECT_EQ(2, observer.device_changed_count_);
   1737   EXPECT_FALSE(device->IsConnected());
   1738   EXPECT_FALSE(device->IsConnecting());
   1739   EXPECT_FALSE(device->IsPaired());
   1740 
   1741   // Pairing dialog should be dismissed
   1742   EXPECT_EQ(2, pairing_delegate.call_count_);
   1743   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1744 }
   1745 
   1746 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
   1747   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1748   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1749 
   1750   GetAdapter();
   1751   DiscoverDevices();
   1752 
   1753   // Cancel the pairing after we receive a request for the PIN code.
   1754   BluetoothDevice* device = adapter_->GetDevice(
   1755       FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
   1756   ASSERT_TRUE(device != NULL);
   1757   ASSERT_FALSE(device->IsPaired());
   1758 
   1759   TestObserver observer(adapter_);
   1760   adapter_->AddObserver(&observer);
   1761 
   1762   TestPairingDelegate pairing_delegate;
   1763   device->Connect(
   1764       &pairing_delegate,
   1765       base::Bind(&BluetoothChromeOSTest::Callback,
   1766                  base::Unretained(this)),
   1767       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1768                  base::Unretained(this)));
   1769 
   1770   EXPECT_EQ(1, pairing_delegate.call_count_);
   1771   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
   1772   EXPECT_TRUE(device->IsConnecting());
   1773 
   1774   // Cancel the pairing.
   1775   device->CancelPairing();
   1776   message_loop.Run();
   1777 
   1778   EXPECT_EQ(0, callback_count_);
   1779   EXPECT_EQ(1, error_callback_count_);
   1780   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
   1781 
   1782   // Should be no changes except connecting going true and false.
   1783   EXPECT_EQ(2, observer.device_changed_count_);
   1784   EXPECT_FALSE(device->IsConnected());
   1785   EXPECT_FALSE(device->IsConnecting());
   1786   EXPECT_FALSE(device->IsPaired());
   1787 
   1788   // Pairing dialog should be dismissed
   1789   EXPECT_EQ(2, pairing_delegate.call_count_);
   1790   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1791 }
   1792 
   1793 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
   1794   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1795   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1796 
   1797   GetAdapter();
   1798   DiscoverDevices();
   1799 
   1800   // Reject the pairing after we receive a request for the passkey.
   1801   BluetoothDevice* device = adapter_->GetDevice(
   1802       FakeBluetoothDeviceClient::kWeirdDeviceAddress);
   1803   ASSERT_TRUE(device != NULL);
   1804   ASSERT_FALSE(device->IsPaired());
   1805 
   1806   TestObserver observer(adapter_);
   1807   adapter_->AddObserver(&observer);
   1808 
   1809   TestPairingDelegate pairing_delegate;
   1810   device->Connect(
   1811       &pairing_delegate,
   1812       base::Bind(&BluetoothChromeOSTest::Callback,
   1813                  base::Unretained(this)),
   1814       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1815                  base::Unretained(this)));
   1816 
   1817   EXPECT_EQ(1, pairing_delegate.call_count_);
   1818   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
   1819   EXPECT_TRUE(device->IsConnecting());
   1820 
   1821   // Reject the pairing.
   1822   device->RejectPairing();
   1823   message_loop.Run();
   1824 
   1825   EXPECT_EQ(0, callback_count_);
   1826   EXPECT_EQ(1, error_callback_count_);
   1827   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
   1828 
   1829   // Should be no changes except connecting going true and false.
   1830   EXPECT_EQ(2, observer.device_changed_count_);
   1831   EXPECT_FALSE(device->IsConnected());
   1832   EXPECT_FALSE(device->IsConnecting());
   1833   EXPECT_FALSE(device->IsPaired());
   1834 
   1835   // Pairing dialog should be dismissed
   1836   EXPECT_EQ(2, pairing_delegate.call_count_);
   1837   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1838 }
   1839 
   1840 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
   1841   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1842   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1843 
   1844   GetAdapter();
   1845   DiscoverDevices();
   1846 
   1847   // Cancel the pairing after we receive a request for the passkey.
   1848   BluetoothDevice* device = adapter_->GetDevice(
   1849       FakeBluetoothDeviceClient::kWeirdDeviceAddress);
   1850   ASSERT_TRUE(device != NULL);
   1851   ASSERT_FALSE(device->IsPaired());
   1852 
   1853   TestObserver observer(adapter_);
   1854   adapter_->AddObserver(&observer);
   1855 
   1856   TestPairingDelegate pairing_delegate;
   1857   device->Connect(
   1858       &pairing_delegate,
   1859       base::Bind(&BluetoothChromeOSTest::Callback,
   1860                  base::Unretained(this)),
   1861       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1862                  base::Unretained(this)));
   1863 
   1864   EXPECT_EQ(1, pairing_delegate.call_count_);
   1865   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
   1866   EXPECT_TRUE(device->IsConnecting());
   1867 
   1868   // Cancel the pairing.
   1869   device->CancelPairing();
   1870   message_loop.Run();
   1871 
   1872   EXPECT_EQ(0, callback_count_);
   1873   EXPECT_EQ(1, error_callback_count_);
   1874   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
   1875 
   1876   // Should be no changes except connecting going true and false.
   1877   EXPECT_EQ(2, observer.device_changed_count_);
   1878   EXPECT_FALSE(device->IsConnected());
   1879   EXPECT_FALSE(device->IsConnecting());
   1880   EXPECT_FALSE(device->IsPaired());
   1881 
   1882   // Pairing dialog should be dismissed
   1883   EXPECT_EQ(2, pairing_delegate.call_count_);
   1884   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1885 }
   1886 
   1887 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
   1888   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1889   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1890 
   1891   GetAdapter();
   1892   DiscoverDevices();
   1893 
   1894   // Reject the pairing after we receive a request for passkey confirmation.
   1895   BluetoothDevice* device = adapter_->GetDevice(
   1896       FakeBluetoothDeviceClient::kPhoneAddress);
   1897   ASSERT_TRUE(device != NULL);
   1898   ASSERT_FALSE(device->IsPaired());
   1899 
   1900   TestObserver observer(adapter_);
   1901   adapter_->AddObserver(&observer);
   1902 
   1903   TestPairingDelegate pairing_delegate;
   1904   device->Connect(
   1905       &pairing_delegate,
   1906       base::Bind(&BluetoothChromeOSTest::Callback,
   1907                  base::Unretained(this)),
   1908       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1909                  base::Unretained(this)));
   1910 
   1911   EXPECT_EQ(1, pairing_delegate.call_count_);
   1912   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
   1913   EXPECT_TRUE(device->IsConnecting());
   1914 
   1915   // Reject the pairing.
   1916   device->RejectPairing();
   1917   message_loop.Run();
   1918 
   1919   EXPECT_EQ(0, callback_count_);
   1920   EXPECT_EQ(1, error_callback_count_);
   1921   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
   1922 
   1923   // Should be no changes except connecting going true and false.
   1924   EXPECT_EQ(2, observer.device_changed_count_);
   1925   EXPECT_FALSE(device->IsConnected());
   1926   EXPECT_FALSE(device->IsConnecting());
   1927   EXPECT_FALSE(device->IsPaired());
   1928 
   1929   // Pairing dialog should be dismissed
   1930   EXPECT_EQ(2, pairing_delegate.call_count_);
   1931   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1932 }
   1933 
   1934 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
   1935   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1936   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1937 
   1938   GetAdapter();
   1939   DiscoverDevices();
   1940 
   1941   // Cancel the pairing after we receive a request for the passkey.
   1942   BluetoothDevice* device = adapter_->GetDevice(
   1943       FakeBluetoothDeviceClient::kPhoneAddress);
   1944   ASSERT_TRUE(device != NULL);
   1945   ASSERT_FALSE(device->IsPaired());
   1946 
   1947   TestObserver observer(adapter_);
   1948   adapter_->AddObserver(&observer);
   1949 
   1950   TestPairingDelegate pairing_delegate;
   1951   device->Connect(
   1952       &pairing_delegate,
   1953       base::Bind(&BluetoothChromeOSTest::Callback,
   1954                  base::Unretained(this)),
   1955       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   1956                  base::Unretained(this)));
   1957 
   1958   EXPECT_EQ(1, pairing_delegate.call_count_);
   1959   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
   1960   EXPECT_TRUE(device->IsConnecting());
   1961 
   1962   // Cancel the pairing.
   1963   device->CancelPairing();
   1964   message_loop.Run();
   1965 
   1966   EXPECT_EQ(0, callback_count_);
   1967   EXPECT_EQ(1, error_callback_count_);
   1968   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
   1969 
   1970   // Should be no changes except connecting going true and false.
   1971   EXPECT_EQ(2, observer.device_changed_count_);
   1972   EXPECT_FALSE(device->IsConnected());
   1973   EXPECT_FALSE(device->IsConnecting());
   1974   EXPECT_FALSE(device->IsPaired());
   1975 
   1976   // Pairing dialog should be dismissed
   1977   EXPECT_EQ(2, pairing_delegate.call_count_);
   1978   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   1979 }
   1980 
   1981 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
   1982   base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
   1983   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
   1984 
   1985   GetAdapter();
   1986   DiscoverDevices();
   1987 
   1988   // Cancel the pairing while we're waiting for the remote host.
   1989   BluetoothDevice* device = adapter_->GetDevice(
   1990       FakeBluetoothDeviceClient::kAppleMouseAddress);
   1991   ASSERT_TRUE(device != NULL);
   1992   ASSERT_FALSE(device->IsPaired());
   1993 
   1994   TestObserver observer(adapter_);
   1995   adapter_->AddObserver(&observer);
   1996 
   1997   TestPairingDelegate pairing_delegate;
   1998   device->Connect(
   1999       &pairing_delegate,
   2000       base::Bind(&BluetoothChromeOSTest::Callback,
   2001                  base::Unretained(this)),
   2002       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
   2003                  base::Unretained(this)));
   2004 
   2005   EXPECT_EQ(0, pairing_delegate.call_count_);
   2006   EXPECT_TRUE(device->IsConnecting());
   2007 
   2008   // Cancel the pairing.
   2009   device->CancelPairing();
   2010   message_loop.Run();
   2011 
   2012   EXPECT_EQ(0, callback_count_);
   2013   EXPECT_EQ(1, error_callback_count_);
   2014   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
   2015 
   2016   // Should be no changes except connecting going true and false.
   2017   EXPECT_EQ(2, observer.device_changed_count_);
   2018   EXPECT_FALSE(device->IsConnected());
   2019   EXPECT_FALSE(device->IsConnecting());
   2020   EXPECT_FALSE(device->IsPaired());
   2021 
   2022   // Pairing dialog should be dismissed
   2023   EXPECT_EQ(1, pairing_delegate.call_count_);
   2024   EXPECT_EQ(1, pairing_delegate.dismiss_count_);
   2025 }
   2026 
   2027 }  // namespace chromeos
   2028