Home | History | Annotate | Download | only in bluetooth
      1 // Copyright (c) 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 "chromeos/dbus/fake_bluetooth_device_client.h"
      7 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
      8 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
      9 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
     10 #include "device/bluetooth/bluetooth_adapter.h"
     11 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
     12 #include "device/bluetooth/bluetooth_adapter_factory.h"
     13 #include "device/bluetooth/bluetooth_device.h"
     14 #include "device/bluetooth/bluetooth_device_chromeos.h"
     15 #include "device/bluetooth/bluetooth_profile.h"
     16 #include "device/bluetooth/bluetooth_profile_chromeos.h"
     17 #include "device/bluetooth/bluetooth_socket.h"
     18 #include "device/bluetooth/bluetooth_socket_chromeos.h"
     19 #include "net/base/io_buffer.h"
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 
     22 using device::BluetoothAdapter;
     23 using device::BluetoothDevice;
     24 using device::BluetoothProfile;
     25 using device::BluetoothSocket;
     26 
     27 namespace chromeos {
     28 
     29 class BluetoothProfileChromeOSTest : public testing::Test {
     30  public:
     31   BluetoothProfileChromeOSTest()
     32       : message_loop_(base::MessageLoop::TYPE_IO),
     33         callback_count_(0),
     34         error_callback_count_(0),
     35         profile_callback_count_(0),
     36         connection_callback_count_(0),
     37         last_profile_(NULL),
     38         last_device_(NULL) {}
     39 
     40   virtual void SetUp() {
     41     mock_dbus_thread_manager_ =
     42         new MockDBusThreadManagerWithoutGMock();
     43     DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_);
     44 
     45     fake_bluetooth_profile_manager_client_ =
     46       mock_dbus_thread_manager_->fake_bluetooth_profile_manager_client();
     47 
     48     device::BluetoothAdapterFactory::GetAdapter(
     49         base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback,
     50                    base::Unretained(this)));
     51     ASSERT_TRUE(adapter_.get() != NULL);
     52     ASSERT_TRUE(adapter_->IsInitialized());
     53     ASSERT_TRUE(adapter_->IsPresent());
     54 
     55     adapter_->SetPowered(
     56         true,
     57         base::Bind(&base::DoNothing),
     58         base::Bind(&base::DoNothing));
     59     ASSERT_TRUE(adapter_->IsPowered());
     60   }
     61 
     62   virtual void TearDown() {
     63     adapter_ = NULL;
     64     DBusThreadManager::Shutdown();
     65   }
     66 
     67   void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
     68     adapter_ = adapter;
     69   }
     70 
     71   void Callback() {
     72     ++callback_count_;
     73   }
     74 
     75   void ErrorCallback() {
     76     ++error_callback_count_;
     77 
     78     message_loop_.Quit();
     79   }
     80 
     81   void ProfileCallback(BluetoothProfile* profile) {
     82     ++profile_callback_count_;
     83     last_profile_ = profile;
     84   }
     85 
     86   void ConnectionCallback(const BluetoothDevice *device,
     87                           scoped_refptr<BluetoothSocket> socket) {
     88     ++connection_callback_count_;
     89     last_device_ = device;
     90     last_socket_ = socket;
     91 
     92     message_loop_.Quit();
     93   }
     94 
     95  protected:
     96   base::MessageLoop message_loop_;
     97 
     98   FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_;
     99   MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_;
    100   scoped_refptr<BluetoothAdapter> adapter_;
    101 
    102   unsigned int callback_count_;
    103   unsigned int error_callback_count_;
    104   unsigned int profile_callback_count_;
    105   unsigned int connection_callback_count_;
    106   BluetoothProfile* last_profile_;
    107   const BluetoothDevice* last_device_;
    108   scoped_refptr<BluetoothSocket> last_socket_;
    109 };
    110 
    111 TEST_F(BluetoothProfileChromeOSTest, L2capEndToEnd) {
    112   // Register the profile and expect the profile object to be passed to the
    113   // callback.
    114   BluetoothProfile::Options options;
    115   BluetoothProfile::Register(
    116       FakeBluetoothProfileManagerClient::kL2capUuid,
    117       options,
    118       base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback,
    119                  base::Unretained(this)));
    120 
    121   EXPECT_EQ(1U, profile_callback_count_);
    122   EXPECT_TRUE(last_profile_ != NULL);
    123   BluetoothProfile* profile = last_profile_;
    124 
    125   // Make sure we have a profile service provider for it.
    126   FakeBluetoothProfileServiceProvider* profile_service_provider =
    127       fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
    128           FakeBluetoothProfileManagerClient::kL2capUuid);
    129   EXPECT_TRUE(profile_service_provider != NULL);
    130 
    131   // Register the connection callback.
    132   profile->SetConnectionCallback(
    133       base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
    134                  base::Unretained(this)));
    135 
    136   // Connect to the device, expect the success callback to be called and the
    137   // connection callback to be called with the device we passed and a new
    138   // socket instance.
    139   BluetoothDevice* device = adapter_->GetDevice(
    140       FakeBluetoothDeviceClient::kPairedDeviceAddress);
    141   ASSERT_TRUE(device != NULL);
    142 
    143   device->ConnectToProfile(
    144       profile,
    145       base::Bind(&BluetoothProfileChromeOSTest::Callback,
    146                  base::Unretained(this)),
    147       base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
    148                  base::Unretained(this)));
    149 
    150   message_loop_.Run();
    151 
    152   EXPECT_EQ(1U, callback_count_);
    153   EXPECT_EQ(0U, error_callback_count_);
    154 
    155   EXPECT_EQ(1U, connection_callback_count_);
    156   EXPECT_EQ(device, last_device_);
    157   EXPECT_TRUE(last_socket_.get() != NULL);
    158 
    159   // Take the ownership of the socket for the remainder of the test and set
    160   // up buffers for read/write tests.
    161   scoped_refptr<BluetoothSocket> socket = last_socket_;
    162   last_socket_ = NULL;
    163 
    164   bool success;
    165   scoped_refptr<net::GrowableIOBuffer> read_buffer;
    166 
    167   scoped_refptr<net::StringIOBuffer> base_buffer(
    168       new net::StringIOBuffer("test"));
    169   scoped_refptr<net::DrainableIOBuffer> write_buffer;
    170 
    171   // Read data from the socket; since no data should be waiting, this should
    172   // return success but no data.
    173   read_buffer = new net::GrowableIOBuffer;
    174   success = socket->Receive(read_buffer.get());
    175   EXPECT_TRUE(success);
    176   EXPECT_EQ(0, read_buffer->capacity());
    177   EXPECT_EQ(0, read_buffer->offset());
    178   EXPECT_EQ("", socket->GetLastErrorMessage());
    179 
    180   // Write data to the socket; the data should be consumed and no bytes should
    181   // be remaining.
    182   write_buffer =
    183       new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
    184   success = socket->Send(write_buffer.get());
    185   EXPECT_TRUE(success);
    186   EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
    187   EXPECT_EQ(0, write_buffer->BytesRemaining());
    188   EXPECT_EQ("", socket->GetLastErrorMessage());
    189 
    190   // Read data from the socket; this should match the data we sent since the
    191   // server just echoes us. We have to spin here until there is actually data
    192   // to read.
    193   read_buffer = new net::GrowableIOBuffer;
    194   do {
    195     success = socket->Receive(read_buffer.get());
    196   } while (success && read_buffer->offset() == 0);
    197   EXPECT_TRUE(success);
    198   EXPECT_NE(0, read_buffer->capacity());
    199   EXPECT_EQ(base_buffer->size(), read_buffer->offset());
    200   EXPECT_EQ("", socket->GetLastErrorMessage());
    201 
    202   std::string data = std::string(read_buffer->StartOfBuffer(),
    203                                  read_buffer->offset());
    204   EXPECT_EQ("test", data);
    205 
    206   // Write data to the socket; since the socket is closed, this should return
    207   // an error without writing the data and "Disconnected" as the message.
    208   write_buffer =
    209       new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
    210   success = socket->Send(write_buffer.get());
    211   EXPECT_FALSE(success);
    212   EXPECT_EQ(0, write_buffer->BytesConsumed());
    213   EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
    214   EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
    215 
    216   // Read data from the socket; since the socket is closed, this should return
    217   // an error with "Disconnected" as the last message.
    218   read_buffer = new net::GrowableIOBuffer;
    219   success = socket->Receive(read_buffer.get());
    220   EXPECT_FALSE(success);
    221   EXPECT_EQ(0, read_buffer->capacity());
    222   EXPECT_EQ(0, read_buffer->offset());
    223   EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
    224 
    225   // Close our end of the socket.
    226   socket = NULL;
    227 
    228   // Unregister the profile, make sure it's no longer registered.
    229   last_profile_->Unregister();
    230 
    231   profile_service_provider =
    232       fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
    233           FakeBluetoothProfileManagerClient::kL2capUuid);
    234   EXPECT_TRUE(profile_service_provider == NULL);
    235 }
    236 
    237 TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) {
    238   // Register the profile and expect the profile object to be passed to the
    239   // callback.
    240   BluetoothProfile::Options options;
    241   BluetoothProfile::Register(
    242       FakeBluetoothProfileManagerClient::kRfcommUuid,
    243       options,
    244       base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback,
    245                  base::Unretained(this)));
    246 
    247   EXPECT_EQ(1U, profile_callback_count_);
    248   EXPECT_TRUE(last_profile_ != NULL);
    249   BluetoothProfile* profile = last_profile_;
    250 
    251   // Make sure we have a profile service provider for it.
    252   FakeBluetoothProfileServiceProvider* profile_service_provider =
    253       fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
    254           FakeBluetoothProfileManagerClient::kRfcommUuid);
    255   EXPECT_TRUE(profile_service_provider != NULL);
    256 
    257   // Register the connection callback.
    258   profile->SetConnectionCallback(
    259       base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
    260                  base::Unretained(this)));
    261 
    262   // Connect to the device, expect the success callback to be called and the
    263   // connection callback to be called with the device we passed and a new
    264   // socket instance.
    265   BluetoothDevice* device = adapter_->GetDevice(
    266       FakeBluetoothDeviceClient::kPairedDeviceAddress);
    267   ASSERT_TRUE(device != NULL);
    268 
    269   device->ConnectToProfile(
    270       profile,
    271       base::Bind(&BluetoothProfileChromeOSTest::Callback,
    272                  base::Unretained(this)),
    273       base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
    274                  base::Unretained(this)));
    275 
    276   message_loop_.Run();
    277 
    278   EXPECT_EQ(1U, callback_count_);
    279   EXPECT_EQ(0U, error_callback_count_);
    280 
    281   EXPECT_EQ(1U, connection_callback_count_);
    282   EXPECT_EQ(device, last_device_);
    283   EXPECT_TRUE(last_socket_.get() != NULL);
    284 
    285   // Take the ownership of the socket for the remainder of the test and set
    286   // up buffers for read/write tests.
    287   scoped_refptr<BluetoothSocket> socket = last_socket_;
    288   last_socket_ = NULL;
    289 
    290   bool success;
    291   scoped_refptr<net::GrowableIOBuffer> read_buffer;
    292 
    293   scoped_refptr<net::StringIOBuffer> base_buffer(
    294       new net::StringIOBuffer("test"));
    295   scoped_refptr<net::DrainableIOBuffer> write_buffer;
    296 
    297   // Read data from the socket; since no data should be waiting, this should
    298   // return success but no data.
    299   read_buffer = new net::GrowableIOBuffer;
    300   success = socket->Receive(read_buffer.get());
    301   EXPECT_TRUE(success);
    302   EXPECT_EQ(0, read_buffer->offset());
    303   EXPECT_EQ("", socket->GetLastErrorMessage());
    304 
    305   // Write data to the socket; the data should be consumed and no bytes should
    306   // be remaining.
    307   write_buffer =
    308       new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
    309   success = socket->Send(write_buffer.get());
    310   EXPECT_TRUE(success);
    311   EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
    312   EXPECT_EQ(0, write_buffer->BytesRemaining());
    313   EXPECT_EQ("", socket->GetLastErrorMessage());
    314 
    315   // Read data from the socket; this should match the data we sent since the
    316   // server just echoes us. We have to spin here until there is actually data
    317   // to read.
    318   read_buffer = new net::GrowableIOBuffer;
    319   do {
    320     success = socket->Receive(read_buffer.get());
    321   } while (success && read_buffer->offset() == 0);
    322   EXPECT_TRUE(success);
    323   EXPECT_NE(0, read_buffer->capacity());
    324   EXPECT_EQ(base_buffer->size(), read_buffer->offset());
    325   EXPECT_EQ("", socket->GetLastErrorMessage());
    326 
    327   std::string data = std::string(read_buffer->StartOfBuffer(),
    328                                  read_buffer->offset());
    329   EXPECT_EQ("test", data);
    330 
    331   // Write data to the socket; since the socket is closed, this should return
    332   // an error without writing the data and "Disconnected" as the message.
    333   write_buffer =
    334       new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
    335   success = socket->Send(write_buffer.get());
    336   EXPECT_FALSE(success);
    337   EXPECT_EQ(0, write_buffer->BytesConsumed());
    338   EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
    339   EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
    340 
    341   // Read data from the socket; since the socket is closed, this should return
    342   // an error with "Disconnected" as the last message.
    343   read_buffer = new net::GrowableIOBuffer;
    344   success = socket->Receive(read_buffer.get());
    345   EXPECT_FALSE(success);
    346   EXPECT_EQ(0, read_buffer->offset());
    347   EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
    348 
    349   // Close our end of the socket.
    350   socket = NULL;
    351 
    352   // Unregister the profile, make sure it's no longer registered.
    353   last_profile_->Unregister();
    354 
    355   profile_service_provider =
    356       fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
    357           FakeBluetoothProfileManagerClient::kRfcommUuid);
    358   EXPECT_TRUE(profile_service_provider == NULL);
    359 }
    360 
    361 }  // namespace chromeos
    362