Index: device/bluetooth/bluetooth_profile_chromeos_unittest.cc |
diff --git a/device/bluetooth/bluetooth_profile_chromeos_unittest.cc b/device/bluetooth/bluetooth_profile_chromeos_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..620116c26ec2eb31163c091596f7a72003e34da7 |
--- /dev/null |
+++ b/device/bluetooth/bluetooth_profile_chromeos_unittest.cc |
@@ -0,0 +1,364 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/command_line.h" |
+#include "base/message_loop.h" |
+#include "chromeos/chromeos_switches.h" |
+#include "chromeos/dbus/fake_bluetooth_device_client.h" |
+#include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" |
+#include "chromeos/dbus/fake_bluetooth_profile_service_provider.h" |
+#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" |
+#include "device/bluetooth/bluetooth_adapter.h" |
+#include "device/bluetooth/bluetooth_adapter_experimental_chromeos.h" |
+#include "device/bluetooth/bluetooth_adapter_factory.h" |
+#include "device/bluetooth/bluetooth_device.h" |
+#include "device/bluetooth/bluetooth_device_experimental_chromeos.h" |
+#include "device/bluetooth/bluetooth_profile.h" |
+#include "device/bluetooth/bluetooth_profile_experimental_chromeos.h" |
+#include "device/bluetooth/bluetooth_socket.h" |
+#include "device/bluetooth/bluetooth_socket_experimental_chromeos.h" |
+#include "net/base/io_buffer.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using device::BluetoothAdapter; |
+using device::BluetoothDevice; |
+using device::BluetoothProfile; |
+using device::BluetoothSocket; |
+ |
+namespace chromeos { |
+ |
+class BluetoothProfileChromeOSTest : public testing::Test { |
+ public: |
+ BluetoothProfileChromeOSTest() |
+ : message_loop_(MessageLoop::TYPE_IO), |
+ callback_count_(0), |
+ error_callback_count_(0), |
+ profile_callback_count_(0), |
+ connection_callback_count_(0), |
+ last_profile_(NULL), |
+ last_device_(NULL) {} |
+ |
+ virtual void SetUp() { |
+ if (!CommandLine::ForCurrentProcess()->HasSwitch( |
+ chromeos::switches::kEnableExperimentalBluetooth)) |
+ CommandLine::ForCurrentProcess()->AppendSwitch( |
+ chromeos::switches::kEnableExperimentalBluetooth); |
+ |
+ mock_dbus_thread_manager_ = |
+ new MockDBusThreadManagerWithoutGMock(); |
+ DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
+ |
+ fake_bluetooth_profile_manager_client_ = |
+ mock_dbus_thread_manager_->fake_bluetooth_profile_manager_client(); |
+ |
+ device::BluetoothAdapterFactory::GetAdapter( |
+ base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback, |
+ base::Unretained(this))); |
+ ASSERT_TRUE(adapter_ != NULL); |
+ ASSERT_TRUE(adapter_->IsInitialized()); |
+ ASSERT_TRUE(adapter_->IsPresent()); |
+ |
+ adapter_->SetPowered( |
+ true, |
+ base::Bind(&base::DoNothing), |
+ base::Bind(&base::DoNothing)); |
+ ASSERT_TRUE(adapter_->IsPowered()); |
+ } |
+ |
+ virtual void TearDown() { |
+ adapter_ = NULL; |
+ DBusThreadManager::Shutdown(); |
+ } |
+ |
+ void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { |
+ adapter_ = adapter; |
+ } |
+ |
+ void Callback() { |
+ ++callback_count_; |
+ } |
+ |
+ void ErrorCallback() { |
+ ++error_callback_count_; |
+ |
+ message_loop_.Quit(); |
+ } |
+ |
+ void ProfileCallback(BluetoothProfile* profile) { |
+ ++profile_callback_count_; |
+ last_profile_ = profile; |
+ } |
+ |
+ void ConnectionCallback(const BluetoothDevice *device, |
+ scoped_refptr<BluetoothSocket> socket) { |
+ ++connection_callback_count_; |
+ last_device_ = device; |
+ last_socket_ = socket; |
+ |
+ message_loop_.Quit(); |
+ } |
+ |
+ protected: |
+ base::MessageLoop message_loop_; |
+ |
+ FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_; |
+ MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_; |
+ scoped_refptr<BluetoothAdapter> adapter_; |
+ |
+ unsigned int callback_count_; |
+ unsigned int error_callback_count_; |
+ unsigned int profile_callback_count_; |
+ unsigned int connection_callback_count_; |
+ BluetoothProfile* last_profile_; |
+ const BluetoothDevice* last_device_; |
+ scoped_refptr<BluetoothSocket> last_socket_; |
+}; |
+ |
+TEST_F(BluetoothProfileChromeOSTest, L2capEndToEnd) { |
+ // Register the profile and expect the profile object to be passed to the |
+ // callback. |
+ BluetoothProfile::Options options; |
+ BluetoothProfile::Register( |
+ FakeBluetoothProfileManagerClient::kL2capUuid, |
+ options, |
+ base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback, |
+ base::Unretained(this))); |
+ |
+ EXPECT_EQ(1U, profile_callback_count_); |
+ EXPECT_TRUE(last_profile_ != NULL); |
+ BluetoothProfile* profile = last_profile_; |
+ |
+ // Make sure we have a profile service provider for it. |
+ FakeBluetoothProfileServiceProvider* profile_service_provider = |
+ fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( |
+ FakeBluetoothProfileManagerClient::kL2capUuid); |
+ EXPECT_TRUE(profile_service_provider != NULL); |
+ |
+ // Register the connection callback. |
+ profile->SetConnectionCallback( |
+ base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback, |
+ base::Unretained(this))); |
+ |
+ // Connect to the device, expect the success callback to be called and the |
+ // connection callback to be called with the device we passed and a new |
+ // socket instance. |
+ BluetoothDevice* device = adapter_->GetDevice( |
+ FakeBluetoothDeviceClient::kPairedDeviceAddress); |
+ ASSERT_TRUE(device != NULL); |
+ |
+ device->ConnectToProfile( |
+ profile, |
+ base::Bind(&BluetoothProfileChromeOSTest::Callback, |
+ base::Unretained(this)), |
+ base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback, |
+ base::Unretained(this))); |
+ |
+ message_loop_.Run(); |
+ |
+ EXPECT_EQ(1U, callback_count_); |
+ EXPECT_EQ(0U, error_callback_count_); |
+ |
+ EXPECT_EQ(1U, connection_callback_count_); |
+ EXPECT_EQ(device, last_device_); |
+ EXPECT_TRUE(last_socket_.get() != NULL); |
+ |
+ // Take the ownership of the socket for the remainder of the test and set |
+ // up buffers for read/write tests. |
+ scoped_refptr<BluetoothSocket> socket = last_socket_; |
+ last_socket_ = NULL; |
+ |
+ bool success; |
+ scoped_refptr<net::GrowableIOBuffer> read_buffer; |
+ |
+ scoped_refptr<net::StringIOBuffer> base_buffer( |
+ new net::StringIOBuffer("test")); |
+ scoped_refptr<net::DrainableIOBuffer> write_buffer; |
+ |
+ // Read data from the socket; since no data should be waiting, this should |
+ // return success but no data. |
+ read_buffer = new net::GrowableIOBuffer; |
+ success = socket->Receive(read_buffer); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(0, read_buffer->capacity()); |
+ EXPECT_EQ(0, read_buffer->offset()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ // Write data to the socket; the data should be consumed and no bytes should |
+ // be remaining. |
+ write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size()); |
+ success = socket->Send(write_buffer); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed()); |
+ EXPECT_EQ(0, write_buffer->BytesRemaining()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ // Read data from the socket; this should match the data we sent since the |
+ // server just echoes us. We have to spin here until there is actually data |
+ // to read. |
+ read_buffer = new net::GrowableIOBuffer; |
+ do { |
+ success = socket->Receive(read_buffer); |
+ } while (success && read_buffer->offset() == 0); |
+ EXPECT_TRUE(success); |
+ EXPECT_NE(0, read_buffer->capacity()); |
+ EXPECT_EQ(base_buffer->size(), read_buffer->offset()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ std::string data = std::string(read_buffer->StartOfBuffer(), |
+ read_buffer->offset()); |
+ EXPECT_EQ("test", data); |
+ |
+ // Write data to the socket; since the socket is closed, this should return |
+ // an error without writing the data and "Disconnected" as the message. |
+ write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size()); |
+ success = socket->Send(write_buffer); |
+ EXPECT_FALSE(success); |
+ EXPECT_EQ(0, write_buffer->BytesConsumed()); |
+ EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining()); |
+ EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); |
+ |
+ // Read data from the socket; since the socket is closed, this should return |
+ // an error with "Disconnected" as the last message. |
+ read_buffer = new net::GrowableIOBuffer; |
+ success = socket->Receive(read_buffer); |
+ EXPECT_FALSE(success); |
+ EXPECT_EQ(0, read_buffer->capacity()); |
+ EXPECT_EQ(0, read_buffer->offset()); |
+ EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); |
+ |
+ // Close our end of the socket. |
+ socket = NULL; |
+ |
+ // Unregister the profile, make sure it's no longer registered. |
+ last_profile_->Unregister(); |
+ |
+ profile_service_provider = |
+ fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( |
+ FakeBluetoothProfileManagerClient::kL2capUuid); |
+ EXPECT_TRUE(profile_service_provider == NULL); |
+} |
+ |
+TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) { |
+ // Register the profile and expect the profile object to be passed to the |
+ // callback. |
+ BluetoothProfile::Options options; |
+ BluetoothProfile::Register( |
+ FakeBluetoothProfileManagerClient::kRfcommUuid, |
+ options, |
+ base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback, |
+ base::Unretained(this))); |
+ |
+ EXPECT_EQ(1U, profile_callback_count_); |
+ EXPECT_TRUE(last_profile_ != NULL); |
+ BluetoothProfile* profile = last_profile_; |
+ |
+ // Make sure we have a profile service provider for it. |
+ FakeBluetoothProfileServiceProvider* profile_service_provider = |
+ fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( |
+ FakeBluetoothProfileManagerClient::kRfcommUuid); |
+ EXPECT_TRUE(profile_service_provider != NULL); |
+ |
+ // Register the connection callback. |
+ profile->SetConnectionCallback( |
+ base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback, |
+ base::Unretained(this))); |
+ |
+ // Connect to the device, expect the success callback to be called and the |
+ // connection callback to be called with the device we passed and a new |
+ // socket instance. |
+ BluetoothDevice* device = adapter_->GetDevice( |
+ FakeBluetoothDeviceClient::kPairedDeviceAddress); |
+ ASSERT_TRUE(device != NULL); |
+ |
+ device->ConnectToProfile( |
+ profile, |
+ base::Bind(&BluetoothProfileChromeOSTest::Callback, |
+ base::Unretained(this)), |
+ base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback, |
+ base::Unretained(this))); |
+ |
+ message_loop_.Run(); |
+ |
+ EXPECT_EQ(1U, callback_count_); |
+ EXPECT_EQ(0U, error_callback_count_); |
+ |
+ EXPECT_EQ(1U, connection_callback_count_); |
+ EXPECT_EQ(device, last_device_); |
+ EXPECT_TRUE(last_socket_.get() != NULL); |
+ |
+ // Take the ownership of the socket for the remainder of the test and set |
+ // up buffers for read/write tests. |
+ scoped_refptr<BluetoothSocket> socket = last_socket_; |
+ last_socket_ = NULL; |
+ |
+ bool success; |
+ scoped_refptr<net::GrowableIOBuffer> read_buffer; |
+ |
+ scoped_refptr<net::StringIOBuffer> base_buffer( |
+ new net::StringIOBuffer("test")); |
+ scoped_refptr<net::DrainableIOBuffer> write_buffer; |
+ |
+ // Read data from the socket; since no data should be waiting, this should |
+ // return success but no data. |
+ read_buffer = new net::GrowableIOBuffer; |
+ success = socket->Receive(read_buffer); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(0, read_buffer->offset()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ // Write data to the socket; the data should be consumed and no bytes should |
+ // be remaining. |
+ write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size()); |
+ success = socket->Send(write_buffer); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed()); |
+ EXPECT_EQ(0, write_buffer->BytesRemaining()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ // Read data from the socket; this should match the data we sent since the |
+ // server just echoes us. We have to spin here until there is actually data |
+ // to read. |
+ read_buffer = new net::GrowableIOBuffer; |
+ do { |
+ success = socket->Receive(read_buffer); |
+ } while (success && read_buffer->offset() == 0); |
+ EXPECT_TRUE(success); |
+ EXPECT_NE(0, read_buffer->capacity()); |
+ EXPECT_EQ(base_buffer->size(), read_buffer->offset()); |
+ EXPECT_EQ("", socket->GetLastErrorMessage()); |
+ |
+ std::string data = std::string(read_buffer->StartOfBuffer(), |
+ read_buffer->offset()); |
+ EXPECT_EQ("test", data); |
+ |
+ // Write data to the socket; since the socket is closed, this should return |
+ // an error without writing the data and "Disconnected" as the message. |
+ write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size()); |
+ success = socket->Send(write_buffer); |
+ EXPECT_FALSE(success); |
+ EXPECT_EQ(0, write_buffer->BytesConsumed()); |
+ EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining()); |
+ EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); |
+ |
+ // Read data from the socket; since the socket is closed, this should return |
+ // an error with "Disconnected" as the last message. |
+ read_buffer = new net::GrowableIOBuffer; |
+ success = socket->Receive(read_buffer); |
+ EXPECT_FALSE(success); |
+ EXPECT_EQ(0, read_buffer->offset()); |
+ EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); |
+ |
+ // Close our end of the socket. |
+ socket = NULL; |
+ |
+ // Unregister the profile, make sure it's no longer registered. |
+ last_profile_->Unregister(); |
+ |
+ profile_service_provider = |
+ fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( |
+ FakeBluetoothProfileManagerClient::kRfcommUuid); |
+ EXPECT_TRUE(profile_service_provider == NULL); |
+} |
+ |
+} // namespace chromeos |