This source file includes following definitions.
- last_device_
- SetUp
- TearDown
- AdapterCallback
- Callback
- ErrorCallback
- ProfileCallback
- ConnectionCallback
- TEST_F
- TEST_F
#include "base/message_loop/message_loop.h"
#include "chromeos/dbus/fake_bluetooth_adapter_client.h"
#include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
#include "chromeos/dbus/fake_bluetooth_device_client.h"
#include "chromeos/dbus/fake_bluetooth_input_client.h"
#include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
#include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
#include "chromeos/dbus/fake_dbus_thread_manager.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_chromeos.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_device_chromeos.h"
#include "device/bluetooth/bluetooth_profile.h"
#include "device/bluetooth/bluetooth_profile_chromeos.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_chromeos.h"
#include "device/bluetooth/bluetooth_uuid.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;
using device::BluetoothUUID;
namespace chromeos {
class BluetoothProfileChromeOSTest : public testing::Test {
public:
BluetoothProfileChromeOSTest()
: callback_count_(0),
error_callback_count_(0),
profile_callback_count_(0),
connection_callback_count_(0),
last_profile_(NULL),
last_device_(NULL) {}
virtual void SetUp() {
FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
fake_bluetooth_profile_manager_client_ =
new FakeBluetoothProfileManagerClient;
fake_dbus_thread_manager->SetBluetoothProfileManagerClient(
scoped_ptr<BluetoothProfileManagerClient>(
fake_bluetooth_profile_manager_client_));
fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
scoped_ptr<BluetoothAgentManagerClient>(
new FakeBluetoothAgentManagerClient));
fake_dbus_thread_manager->SetBluetoothAdapterClient(
scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
fake_dbus_thread_manager->SetBluetoothDeviceClient(
scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
fake_dbus_thread_manager->SetBluetoothInputClient(
scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
device::BluetoothAdapterFactory::GetAdapter(
base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback,
base::Unretained(this)));
ASSERT_TRUE(adapter_.get() != 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::MessageLoopForIO message_loop_;
FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_;
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) {
BluetoothProfile::Options options;
BluetoothProfile::Register(
BluetoothUUID(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_;
FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
FakeBluetoothProfileManagerClient::kL2capUuid);
EXPECT_TRUE(profile_service_provider != NULL);
profile->SetConnectionCallback(
base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
base::Unretained(this)));
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);
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_buffer = new net::GrowableIOBuffer;
success = socket->Receive(read_buffer.get());
EXPECT_TRUE(success);
EXPECT_EQ(0, read_buffer->capacity());
EXPECT_EQ(0, read_buffer->offset());
EXPECT_EQ("", socket->GetLastErrorMessage());
write_buffer =
new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
success = socket->Send(write_buffer.get());
EXPECT_TRUE(success);
EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
EXPECT_EQ(0, write_buffer->BytesRemaining());
EXPECT_EQ("", socket->GetLastErrorMessage());
read_buffer = new net::GrowableIOBuffer;
do {
success = socket->Receive(read_buffer.get());
} 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_buffer =
new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
success = socket->Send(write_buffer.get());
EXPECT_FALSE(success);
EXPECT_EQ(0, write_buffer->BytesConsumed());
EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
read_buffer = new net::GrowableIOBuffer;
success = socket->Receive(read_buffer.get());
EXPECT_FALSE(success);
EXPECT_EQ(0, read_buffer->capacity());
EXPECT_EQ(0, read_buffer->offset());
EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
socket = NULL;
last_profile_->Unregister();
profile_service_provider =
fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
FakeBluetoothProfileManagerClient::kL2capUuid);
EXPECT_TRUE(profile_service_provider == NULL);
}
TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) {
BluetoothProfile::Options options;
BluetoothProfile::Register(
BluetoothUUID(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_;
FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider != NULL);
profile->SetConnectionCallback(
base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
base::Unretained(this)));
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);
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_buffer = new net::GrowableIOBuffer;
success = socket->Receive(read_buffer.get());
EXPECT_TRUE(success);
EXPECT_EQ(0, read_buffer->offset());
EXPECT_EQ("", socket->GetLastErrorMessage());
write_buffer =
new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
success = socket->Send(write_buffer.get());
EXPECT_TRUE(success);
EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
EXPECT_EQ(0, write_buffer->BytesRemaining());
EXPECT_EQ("", socket->GetLastErrorMessage());
read_buffer = new net::GrowableIOBuffer;
do {
success = socket->Receive(read_buffer.get());
} 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_buffer =
new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
success = socket->Send(write_buffer.get());
EXPECT_FALSE(success);
EXPECT_EQ(0, write_buffer->BytesConsumed());
EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
read_buffer = new net::GrowableIOBuffer;
success = socket->Receive(read_buffer.get());
EXPECT_FALSE(success);
EXPECT_EQ(0, read_buffer->offset());
EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
socket = NULL;
last_profile_->Unregister();
profile_service_provider =
fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
FakeBluetoothProfileManagerClient::kRfcommUuid);
EXPECT_TRUE(profile_service_provider == NULL);
}
}