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