This source file includes following definitions.
- adapter_
- AdapterPresentChanged
- AdapterPoweredChanged
- AdapterDiscoverableChanged
- AdapterDiscoveringChanged
- DeviceAdded
- DeviceChanged
- DeviceRemoved
- QuitMessageLoop
- last_entered_
- RequestPinCode
- RequestPasskey
- DisplayPinCode
- DisplayPasskey
- KeysEntered
- ConfirmPasskey
- AuthorizePairing
- QuitMessageLoop
- SetUp
- TearDown
- Callback
- DiscoverySessionCallback
- ErrorCallback
- DBusErrorCallback
- ConnectErrorCallback
- GetAdapter
- DiscoverDevice
- DiscoverDevices
- QuitMessageLoop
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.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_dbus_thread_manager.h"
#include "dbus/object_path.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_discovery_session.h"
#include "device/bluetooth/bluetooth_pairing_chromeos.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using device::BluetoothAdapter;
using device::BluetoothAdapterFactory;
using device::BluetoothDevice;
using device::BluetoothDiscoverySession;
using device::BluetoothUUID;
namespace chromeos {
class TestObserver : public BluetoothAdapter::Observer {
public:
TestObserver(scoped_refptr<BluetoothAdapter> adapter)
: present_changed_count_(0),
powered_changed_count_(0),
discoverable_changed_count_(0),
discovering_changed_count_(0),
last_present_(false),
last_powered_(false),
last_discovering_(false),
device_added_count_(0),
device_changed_count_(0),
device_removed_count_(0),
last_device_(NULL),
adapter_(adapter) {
adapter_->AddObserver(this);
}
virtual ~TestObserver() {
adapter_->RemoveObserver(this);
}
virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
bool present) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++present_changed_count_;
last_present_ = present;
}
virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
bool powered) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++powered_changed_count_;
last_powered_ = powered;
}
virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
bool discoverable) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++discoverable_changed_count_;
}
virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
bool discovering) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++discovering_changed_count_;
last_discovering_ = discovering;
}
virtual void DeviceAdded(BluetoothAdapter* adapter,
BluetoothDevice* device) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++device_added_count_;
last_device_ = device;
last_device_address_ = device->GetAddress();
QuitMessageLoop();
}
virtual void DeviceChanged(BluetoothAdapter* adapter,
BluetoothDevice* device) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++device_changed_count_;
last_device_ = device;
last_device_address_ = device->GetAddress();
QuitMessageLoop();
}
virtual void DeviceRemoved(BluetoothAdapter* adapter,
BluetoothDevice* device) OVERRIDE {
EXPECT_EQ(adapter_, adapter);
++device_removed_count_;
last_device_address_ = device->GetAddress();
QuitMessageLoop();
}
int present_changed_count_;
int powered_changed_count_;
int discoverable_changed_count_;
int discovering_changed_count_;
bool last_present_;
bool last_powered_;
bool last_discovering_;
int device_added_count_;
int device_changed_count_;
int device_removed_count_;
BluetoothDevice* last_device_;
std::string last_device_address_;
private:
void QuitMessageLoop() {
if (base::MessageLoop::current() &&
base::MessageLoop::current()->is_running())
base::MessageLoop::current()->Quit();
}
scoped_refptr<BluetoothAdapter> adapter_;
};
class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
public:
TestPairingDelegate()
: call_count_(0),
request_pincode_count_(0),
request_passkey_count_(0),
display_pincode_count_(0),
display_passkey_count_(0),
keys_entered_count_(0),
confirm_passkey_count_(0),
authorize_pairing_count_(0),
last_passkey_(9999999U),
last_entered_(999U) {}
virtual ~TestPairingDelegate() {}
virtual void RequestPinCode(BluetoothDevice* device) OVERRIDE {
++call_count_;
++request_pincode_count_;
QuitMessageLoop();
}
virtual void RequestPasskey(BluetoothDevice* device) OVERRIDE {
++call_count_;
++request_passkey_count_;
QuitMessageLoop();
}
virtual void DisplayPinCode(BluetoothDevice* device,
const std::string& pincode) OVERRIDE {
++call_count_;
++display_pincode_count_;
last_pincode_ = pincode;
QuitMessageLoop();
}
virtual void DisplayPasskey(BluetoothDevice* device,
uint32 passkey) OVERRIDE {
++call_count_;
++display_passkey_count_;
last_passkey_ = passkey;
QuitMessageLoop();
}
virtual void KeysEntered(BluetoothDevice* device, uint32 entered) OVERRIDE {
++call_count_;
++keys_entered_count_;
last_entered_ = entered;
QuitMessageLoop();
}
virtual void ConfirmPasskey(BluetoothDevice* device,
uint32 passkey) OVERRIDE {
++call_count_;
++confirm_passkey_count_;
last_passkey_ = passkey;
QuitMessageLoop();
}
virtual void AuthorizePairing(BluetoothDevice* device) OVERRIDE {
++call_count_;
++authorize_pairing_count_;
QuitMessageLoop();
}
int call_count_;
int request_pincode_count_;
int request_passkey_count_;
int display_pincode_count_;
int display_passkey_count_;
int keys_entered_count_;
int confirm_passkey_count_;
int authorize_pairing_count_;
uint32 last_passkey_;
uint32 last_entered_;
std::string last_pincode_;
private:
void QuitMessageLoop() {
if (base::MessageLoop::current() &&
base::MessageLoop::current()->is_running())
base::MessageLoop::current()->Quit();
}
};
class BluetoothChromeOSTest : public testing::Test {
public:
virtual void SetUp() {
FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
fake_dbus_thread_manager->SetBluetoothAdapterClient(
scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
fake_dbus_thread_manager->SetBluetoothDeviceClient(
scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
fake_dbus_thread_manager->SetBluetoothInputClient(
scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
scoped_ptr<BluetoothAgentManagerClient>(
new FakeBluetoothAgentManagerClient));
DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
callback_count_ = 0;
error_callback_count_ = 0;
last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
last_client_error_ = "";
}
virtual void TearDown() {
for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
discovery_sessions_.begin();
iter != discovery_sessions_.end();
++iter) {
BluetoothDiscoverySession* session = *iter;
if (!session->IsActive())
continue;
callback_count_ = 0;
session->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
ASSERT_EQ(1, callback_count_);
}
discovery_sessions_.clear();
adapter_ = NULL;
DBusThreadManager::Shutdown();
}
void Callback() {
++callback_count_;
QuitMessageLoop();
}
void DiscoverySessionCallback(
scoped_ptr<BluetoothDiscoverySession> discovery_session) {
++callback_count_;
discovery_sessions_.push_back(discovery_session.release());
QuitMessageLoop();
}
void ErrorCallback() {
++error_callback_count_;
QuitMessageLoop();
}
void DBusErrorCallback(const std::string& error_name,
const std::string& error_message) {
++error_callback_count_;
last_client_error_ = error_name;
QuitMessageLoop();
}
void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
++error_callback_count_;
last_connect_error_ = error;
}
void GetAdapter() {
adapter_ = new BluetoothAdapterChromeOS();
ASSERT_TRUE(adapter_.get() != NULL);
ASSERT_TRUE(adapter_->IsInitialized());
}
void DiscoverDevice(const std::string& address) {
ASSERT_TRUE(adapter_.get() != NULL);
ASSERT_TRUE(base::MessageLoop::current() != NULL);
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
TestObserver observer(adapter_);
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
base::MessageLoop::current()->Run();
ASSERT_EQ(2, callback_count_);
ASSERT_EQ(0, error_callback_count_);
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
while (!observer.device_removed_count_ &&
observer.last_device_address_ != address)
base::MessageLoop::current()->Run();
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
base::MessageLoop::current()->Run();
ASSERT_EQ(1, callback_count_);
ASSERT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_FALSE(adapter_->IsDiscovering());
}
void DiscoverDevices() {
DiscoverDevice("does not exist");
}
protected:
base::MessageLoop message_loop_;
FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
scoped_refptr<BluetoothAdapter> adapter_;
int callback_count_;
int error_callback_count_;
enum BluetoothDevice::ConnectErrorCode last_connect_error_;
std::string last_client_error_;
ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
private:
void QuitMessageLoop() {
if (base::MessageLoop::current() &&
base::MessageLoop::current()->is_running())
base::MessageLoop::current()->Quit();
}
};
TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
GetAdapter();
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
adapter_->GetAddress());
EXPECT_FALSE(adapter_->IsDiscovering());
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
EXPECT_EQ(1U, devices.size());
EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
}
TEST_F(BluetoothChromeOSTest, BecomePresent) {
fake_bluetooth_adapter_client_->SetVisible(false);
GetAdapter();
ASSERT_FALSE(adapter_->IsPresent());
TestObserver observer(adapter_);
fake_bluetooth_adapter_client_->SetVisible(true);
EXPECT_EQ(1, observer.present_changed_count_);
EXPECT_TRUE(observer.last_present_);
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_EQ(1, observer.device_added_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
observer.last_device_address_);
EXPECT_EQ(0, observer.powered_changed_count_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
GetAdapter();
ASSERT_TRUE(adapter_->IsPresent());
TestObserver observer(adapter_);
fake_bluetooth_adapter_client_->SetVisible(false);
EXPECT_EQ(1, observer.present_changed_count_);
EXPECT_FALSE(observer.last_present_);
EXPECT_FALSE(adapter_->IsPresent());
EXPECT_EQ(1, observer.device_removed_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
observer.last_device_address_);
EXPECT_EQ(0, observer.powered_changed_count_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest, SecondAdapter) {
GetAdapter();
ASSERT_TRUE(adapter_->IsPresent());
TestObserver observer(adapter_);
fake_bluetooth_adapter_client_->SetSecondVisible(true);
EXPECT_EQ(0, observer.present_changed_count_);
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
adapter_->GetAddress());
fake_bluetooth_adapter_client_->SetVisible(false);
EXPECT_EQ(1, observer.present_changed_count_);
EXPECT_FALSE(observer.last_present_);
EXPECT_FALSE(adapter_->IsPresent());
EXPECT_EQ(1, observer.device_removed_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
observer.last_device_address_);
EXPECT_EQ(0, observer.powered_changed_count_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscovering());
observer.device_removed_count_ = 0;
fake_bluetooth_adapter_client_->SetSecondVisible(false);
EXPECT_EQ(0, observer.device_removed_count_);
EXPECT_EQ(0, observer.powered_changed_count_);
EXPECT_EQ(0, observer.discovering_changed_count_);
}
TEST_F(BluetoothChromeOSTest, BecomePowered) {
GetAdapter();
ASSERT_FALSE(adapter_->IsPowered());
TestObserver observer(adapter_);
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.powered_changed_count_);
EXPECT_TRUE(observer.last_powered_);
EXPECT_TRUE(adapter_->IsPowered());
}
TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
TestObserver observer(adapter_);
adapter_->SetPowered(
false,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.powered_changed_count_);
EXPECT_FALSE(observer.last_powered_);
EXPECT_FALSE(adapter_->IsPowered());
}
TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
GetAdapter();
static const std::string new_name(".__.");
adapter_->SetName(
new_name,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(new_name, adapter_->GetName());
}
TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
GetAdapter();
ASSERT_FALSE(adapter_->IsDiscoverable());
TestObserver observer(adapter_);
adapter_->SetDiscoverable(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discoverable_changed_count_);
EXPECT_TRUE(adapter_->IsDiscoverable());
}
TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
GetAdapter();
adapter_->SetDiscoverable(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsDiscoverable());
TestObserver observer(adapter_);
adapter_->SetDiscoverable(
false,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discoverable_changed_count_);
EXPECT_FALSE(adapter_->IsDiscoverable());
}
TEST_F(BluetoothChromeOSTest, StopDiscovery) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
TestObserver observer(adapter_);
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest, Discovery) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestObserver observer(adapter_);
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
message_loop_.Run();
EXPECT_EQ(2, observer.device_added_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
observer.last_device_address_);
message_loop_.Run();
EXPECT_EQ(4, observer.device_added_count_);
while (!observer.device_removed_count_)
message_loop_.Run();
EXPECT_EQ(1, observer.device_removed_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
observer.last_device_address_);
}
TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_EQ((size_t)1, discovery_sessions_.size());
ASSERT_TRUE(discovery_sessions_[0]->IsActive());
fake_bluetooth_device_client_->EndDiscoverySimulation(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
fake_bluetooth_adapter_client_->SetVisible(false);
ASSERT_FALSE(adapter_->IsPresent());
ASSERT_FALSE(discovery_sessions_[0]->IsActive());
TestObserver observer(adapter_);
fake_bluetooth_adapter_client_->SetVisible(true);
EXPECT_EQ(1, observer.present_changed_count_);
EXPECT_TRUE(observer.last_present_);
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_EQ(1, observer.powered_changed_count_);
EXPECT_TRUE(observer.last_powered_);
EXPECT_TRUE(adapter_->IsPowered());
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
observer.present_changed_count_ = 0;
observer.powered_changed_count_ = 0;
observer.discovering_changed_count_ = 0;
fake_bluetooth_adapter_client_->SetVisible(false);
EXPECT_EQ(1, observer.present_changed_count_);
EXPECT_FALSE(observer.last_present_);
EXPECT_FALSE(adapter_->IsPresent());
EXPECT_EQ(1, observer.powered_changed_count_);
EXPECT_FALSE(observer.last_powered_);
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(adapter_->IsPowered());
callback_count_ = 0;
TestObserver observer(adapter_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 3; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(3, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
for (int i = 0; i < 2; i++) {
discovery_sessions_[i]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(5, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
EXPECT_TRUE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
EXPECT_FALSE(discovery_sessions_[1]->IsActive());
EXPECT_TRUE(discovery_sessions_[2]->IsActive());
for (int i = 0; i < 3; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(8, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)6, discovery_sessions_.size());
for (int i = 2; i < 6; i++) {
discovery_sessions_[i]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
message_loop_.Run();
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_EQ(12, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 6; i++)
EXPECT_FALSE(discovery_sessions_[i]->IsActive());
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_EQ(12, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest,
UnexpectedChangesDuringMultipleDiscoverySessions) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(adapter_->IsPowered());
callback_count_ = 0;
TestObserver observer(adapter_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 3; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(3, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
for (int i = 0; i < 3; i++)
EXPECT_TRUE(discovery_sessions_[i]->IsActive());
fake_bluetooth_device_client_->EndDiscoverySimulation(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
fake_bluetooth_adapter_client_->StopDiscovery(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_EQ(4, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 3; i++)
EXPECT_FALSE(discovery_sessions_[i]->IsActive());
discovery_sessions_.clear();
for (int i = 0; i < 2; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
message_loop_.Run();
EXPECT_EQ(3, observer.discovering_changed_count_);
EXPECT_EQ(6, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)2, discovery_sessions_.size());
for (int i = 0; i < 2; i++)
EXPECT_TRUE(discovery_sessions_[i]->IsActive());
fake_bluetooth_device_client_->EndDiscoverySimulation(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
fake_bluetooth_adapter_client_->SetVisible(false);
ASSERT_FALSE(adapter_->IsPresent());
EXPECT_EQ(4, observer.discovering_changed_count_);
EXPECT_EQ(6, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 2; i++)
EXPECT_FALSE(discovery_sessions_[i]->IsActive());
discovery_sessions_.clear();
fake_bluetooth_adapter_client_->SetVisible(true);
ASSERT_TRUE(adapter_->IsPresent());
EXPECT_EQ(5, observer.discovering_changed_count_);
EXPECT_EQ(6, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(5, observer.discovering_changed_count_);
EXPECT_EQ(7, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(5, observer.discovering_changed_count_);
EXPECT_EQ(8, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
discovery_sessions_.clear();
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(5, observer.discovering_changed_count_);
EXPECT_EQ(9, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
fake_bluetooth_adapter_client_->StopDiscovery(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(5, observer.discovering_changed_count_);
EXPECT_EQ(10, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(6, observer.discovering_changed_count_);
EXPECT_EQ(11, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
}
TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(adapter_->IsPowered());
callback_count_ = 0;
TestObserver observer(adapter_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
for (int i = 0; i < 3; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(3, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
for (int i = 0; i < 3; i++)
EXPECT_TRUE(discovery_sessions_[i]->IsActive());
fake_bluetooth_device_client_->EndDiscoverySimulation(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
ASSERT_TRUE(adapter_->IsPowered());
ASSERT_TRUE(adapter_->IsDiscovering());
discovery_sessions_.pop_back();
discovery_sessions_.pop_back();
ASSERT_EQ((size_t)1, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
EXPECT_TRUE(adapter_->IsDiscovering());
fake_bluetooth_adapter_client_->StopDiscovery(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_EQ(4, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
}
TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(adapter_->IsPowered());
callback_count_ = 0;
TestObserver observer(adapter_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, callback_count_);
fake_bluetooth_device_client_->EndDiscoverySimulation(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
EXPECT_TRUE(discovery_sessions_.empty());
for (int i = 0; i < 2; i++) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
EXPECT_TRUE(discovery_sessions_.empty());
message_loop_.Run();
EXPECT_EQ(3, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
for (int i = 0; i < 3; i++) {
discovery_sessions_[i]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
}
EXPECT_EQ(5, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
EXPECT_FALSE(discovery_sessions_[1]->IsActive());
EXPECT_TRUE(discovery_sessions_[2]->IsActive());
discovery_sessions_[2]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(5, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_TRUE(discovery_sessions_[2]->IsActive());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(5, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
message_loop_.Run();
EXPECT_EQ(6, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(3, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
EXPECT_FALSE(discovery_sessions_[2]->IsActive());
message_loop_.Run();
EXPECT_EQ(7, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(3, observer.discovering_changed_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)4, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[3]->IsActive());
}
TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
GetAdapter();
adapter_->SetPowered(
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(adapter_->IsPowered());
callback_count_ = 0;
TestObserver observer(adapter_);
EXPECT_EQ(0, observer.discovering_changed_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
EXPECT_TRUE(discovery_sessions_.empty());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)1, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(2, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)2, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[0]->IsActive());
adapter_->StartDiscoverySession(
base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(3, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
EXPECT_TRUE(discovery_sessions_[1]->IsActive());
EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
discovery_sessions_[0]->Stop(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(1, observer.discovering_changed_count_);
EXPECT_EQ(4, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_TRUE(observer.last_discovering_);
EXPECT_TRUE(adapter_->IsDiscovering());
ASSERT_EQ((size_t)3, discovery_sessions_.size());
EXPECT_FALSE(discovery_sessions_[0]->IsActive());
EXPECT_TRUE(discovery_sessions_[1]->IsActive());
discovery_sessions_.clear();
while (observer.last_discovering_)
message_loop_.RunUntilIdle();
EXPECT_EQ(2, observer.discovering_changed_count_);
EXPECT_EQ(4, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_FALSE(observer.last_discovering_);
EXPECT_FALSE(adapter_->IsDiscovering());
}
TEST_F(BluetoothChromeOSTest, DeviceProperties) {
GetAdapter();
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
ASSERT_EQ(1U, devices.size());
ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
devices[0]->GetName());
EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
EXPECT_TRUE(devices[0]->IsPaired());
EXPECT_FALSE(devices[0]->IsConnected());
EXPECT_FALSE(devices[0]->IsConnecting());
EXPECT_TRUE(devices[0]->IsConnectable());
BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
ASSERT_EQ(2U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
EXPECT_EQ(0x030d, devices[0]->GetProductID());
EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
}
TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
GetAdapter();
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
ASSERT_EQ(1U, devices.size());
ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
TestObserver observer(adapter_);
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
properties->bluetooth_class.ReplaceValue(0x002580);
EXPECT_EQ(1, observer.device_changed_count_);
EXPECT_EQ(devices[0], observer.last_device_);
EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
}
TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
GetAdapter();
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
ASSERT_EQ(1U, devices.size());
ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
devices[0]->GetName());
TestObserver observer(adapter_);
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
static const std::string new_name("New Device Name");
properties->alias.ReplaceValue(new_name);
EXPECT_EQ(1, observer.device_changed_count_);
EXPECT_EQ(devices[0], observer.last_device_);
EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
}
TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
GetAdapter();
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
ASSERT_EQ(1U, devices.size());
ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
ASSERT_EQ(2U, uuids.size());
ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
TestObserver observer(adapter_);
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
std::vector<std::string> new_uuids;
new_uuids.push_back(uuids[0].canonical_value());
new_uuids.push_back(uuids[1].canonical_value());
new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(new_uuids);
EXPECT_EQ(1, observer.device_changed_count_);
EXPECT_EQ(devices[0], observer.last_device_);
uuids = devices[0]->GetUUIDs();
ASSERT_EQ(5U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
}
TEST_F(BluetoothChromeOSTest, ForgetDevice) {
GetAdapter();
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
ASSERT_EQ(1U, devices.size());
ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
devices[0]->GetAddress());
std::string address = devices[0]->GetAddress();
TestObserver observer(adapter_);
devices[0]->Forget(
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.device_removed_count_);
EXPECT_EQ(address, observer.last_device_address_);
devices = adapter_->GetDevices();
ASSERT_EQ(0U, devices.size());
}
TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConnectUnpairableAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
ASSERT_EQ(1, callback_count_);
ASSERT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(device->IsConnected());
ASSERT_FALSE(device->IsConnecting());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
ASSERT_TRUE(properties->trusted.value());
TestObserver observer(adapter_);
device->Forget(
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.device_removed_count_);
EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
observer.last_device_address_);
device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConnectUnpairableAddress);
EXPECT_FALSE(device != NULL);
}
TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
GetAdapter();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_TRUE(device->IsPaired());
TestObserver observer(adapter_);
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(4, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
}
TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConnectUnpairableAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(5, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
EXPECT_TRUE(properties->trusted.value());
BluetoothDevice::UUIDList uuids = device->GetUUIDs();
ASSERT_EQ(1U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
EXPECT_FALSE(device->IsConnectable());
}
TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
GetAdapter();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_TRUE(device->IsPaired());
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
ASSERT_EQ(1, callback_count_);
ASSERT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(device->IsConnected());
TestObserver observer(adapter_);
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(3, observer.device_changed_count_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
}
TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kLegacyAutopairAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
}
TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
GetAdapter();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_TRUE(device->IsPaired());
device->Connect(
NULL,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
ASSERT_EQ(1, callback_count_);
ASSERT_EQ(0, error_callback_count_);
callback_count_ = 0;
ASSERT_TRUE(device->IsConnected());
ASSERT_FALSE(device->IsConnecting());
TestObserver observer(adapter_);
device->Disconnect(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(1, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_FALSE(device->IsConnected());
}
TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
GetAdapter();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kPairedDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_TRUE(device->IsPaired());
ASSERT_FALSE(device->IsConnected());
TestObserver observer(adapter_);
device->Disconnect(
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
}
TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kLegacyAutopairAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
EXPECT_TRUE(device->IsConnecting());
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(7, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
BluetoothDevice::UUIDList uuids = device->GetUUIDs();
ASSERT_EQ(1U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
EXPECT_EQ("123456", pairing_delegate.last_pincode_);
EXPECT_TRUE(device->IsConnecting());
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(7, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
BluetoothDevice::UUIDList uuids = device->GetUUIDs();
ASSERT_EQ(1U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(2, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
EXPECT_EQ(0U, pairing_delegate.last_entered_);
EXPECT_TRUE(device->IsConnecting());
for(int i = 1; i <= 7; ++i) {
message_loop_.Run();
EXPECT_EQ(2 + i, pairing_delegate.call_count_);
EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
}
message_loop_.Run();
EXPECT_EQ(9, pairing_delegate.call_count_);
EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
EXPECT_EQ(7U, pairing_delegate.last_entered_);
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(7, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
BluetoothDevice::UUIDList uuids = device->GetUUIDs();
ASSERT_EQ(1U, uuids.size());
EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
EXPECT_FALSE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->IsConnecting());
device->SetPinCode("1234");
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(6, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
BluetoothDevice::UUIDList uuids = device->GetUUIDs();
ASSERT_EQ(0U, uuids.size());
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
EXPECT_TRUE(device->IsConnecting());
device->ConfirmPairing();
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(6, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
EXPECT_TRUE(device->IsConnecting());
device->SetPasskey(1234);
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(6, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairJustWorks) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kJustWorksAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(6, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
EXPECT_TRUE(device->IsConnectable());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
EXPECT_TRUE(device->IsConnecting());
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingFails) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kVanishingDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
EXPECT_TRUE(device->IsConnecting());
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
EXPECT_TRUE(device->IsConnecting());
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
EXPECT_EQ(4, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_TRUE(device->IsPaired());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(
FakeBluetoothDeviceClient::kUnconnectableDevicePath));
EXPECT_TRUE(properties->trusted.value());
}
TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->IsConnecting());
device->RejectPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
EXPECT_TRUE(device->IsConnecting());
device->CancelPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
EXPECT_TRUE(device->IsConnecting());
device->RejectPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
EXPECT_TRUE(device->IsConnecting());
device->CancelPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
EXPECT_TRUE(device->IsConnecting());
device->RejectPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
EXPECT_TRUE(device->IsConnecting());
device->CancelPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
DiscoverDevices();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kLegacyAutopairAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
TestPairingDelegate pairing_delegate;
device->Connect(
&pairing_delegate,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
base::Unretained(this)));
EXPECT_EQ(0, pairing_delegate.call_count_);
EXPECT_TRUE(device->IsConnecting());
device->CancelPairing();
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_FALSE(device->IsConnected());
EXPECT_FALSE(device->IsConnecting());
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestPairingDelegate pairing_delegate;
adapter_->AddPairingDelegate(
&pairing_delegate,
BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
device->SetPinCode("1234");
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsPaired());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
ASSERT_TRUE(properties->trusted.value());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestPairingDelegate pairing_delegate;
adapter_->AddPairingDelegate(
&pairing_delegate,
BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
device->ConfirmPairing();
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsPaired());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
ASSERT_TRUE(properties->trusted.value());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestPairingDelegate pairing_delegate;
adapter_->AddPairingDelegate(
&pairing_delegate,
BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
device->SetPasskey(1234);
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsPaired());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
ASSERT_TRUE(properties->trusted.value());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestPairingDelegate pairing_delegate;
adapter_->AddPairingDelegate(
&pairing_delegate,
BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kJustWorksAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
device->ConfirmPairing();
message_loop_.Run();
EXPECT_EQ(1, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(2, observer.device_changed_count_);
EXPECT_EQ(device, observer.last_device_);
EXPECT_TRUE(device->IsPaired());
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
ASSERT_TRUE(properties->trusted.value());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPinCodeAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsPaired());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsPaired());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsPaired());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kJustWorksAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(1, error_callback_count_);
EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsPaired());
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
}
TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
GetAdapter();
TestPairingDelegate pairing_delegate;
adapter_->AddPairingDelegate(
&pairing_delegate,
BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
fake_bluetooth_device_client_->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kRequestPasskeyAddress);
ASSERT_TRUE(device != NULL);
ASSERT_FALSE(device->IsPaired());
TestObserver observer(adapter_);
fake_bluetooth_device_client_->SimulatePairing(
dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
true,
base::Bind(&BluetoothChromeOSTest::Callback,
base::Unretained(this)),
base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
base::Unretained(this)));
EXPECT_EQ(1, pairing_delegate.call_count_);
EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
BluetoothDeviceChromeOS* device_chromeos =
static_cast<BluetoothDeviceChromeOS*>(device);
ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
adapter_->RemovePairingDelegate(&pairing_delegate);
EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
device->SetPasskey(1234);
EXPECT_EQ(0, callback_count_);
EXPECT_EQ(0, error_callback_count_);
EXPECT_EQ(0, observer.device_changed_count_);
EXPECT_FALSE(device->IsPaired());
}
TEST_F(BluetoothChromeOSTest, DeviceId) {
GetAdapter();
BluetoothDevice* device = adapter_->GetDevice(
FakeBluetoothDeviceClient::kPairedDeviceAddress);
FakeBluetoothDeviceClient::Properties* properties =
fake_bluetooth_device_client_->GetProperties(
dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
ASSERT_TRUE(device != NULL);
ASSERT_TRUE(properties != NULL);
ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
EXPECT_EQ(0x05ac, device->GetVendorID());
EXPECT_EQ(0x030d, device->GetProductID());
EXPECT_EQ(0x0306, device->GetDeviceID());
properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
EXPECT_EQ(0x00e0, device->GetVendorID());
EXPECT_EQ(0x2400, device->GetProductID());
EXPECT_EQ(0x0400, device->GetDeviceID());
properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
EXPECT_EQ(0, device->GetVendorID());
EXPECT_EQ(0, device->GetProductID());
EXPECT_EQ(0, device->GetDeviceID());
properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
EXPECT_EQ(0, device->GetVendorID());
EXPECT_EQ(0, device->GetProductID());
EXPECT_EQ(0, device->GetDeviceID());
properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
EXPECT_EQ(0, device->GetVendorID());
EXPECT_EQ(0, device->GetProductID());
EXPECT_EQ(0, device->GetDeviceID());
}
}