Chromium Code Reviews| Index: content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc |
| diff --git a/content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc b/content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc |
| index f931660cc0ea069f7b036baaf381713e9678d154..f5b22582ec9212b2b3e987726c731d84414368db 100644 |
| --- a/content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc |
| +++ b/content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc |
| @@ -10,16 +10,20 @@ |
| #include "device/bluetooth/bluetooth_uuid.h" |
| #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| +#include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| using device::BluetoothAdapter; |
| using device::BluetoothAdapterFactory; |
| using device::BluetoothDevice; |
| using device::BluetoothDiscoverySession; |
| +using device::BluetoothGattConnection; |
| using device::BluetoothUUID; |
| using device::MockBluetoothAdapter; |
| using device::MockBluetoothDevice; |
| using device::MockBluetoothDiscoverySession; |
| +using device::MockBluetoothGattConnection; |
| +using testing::Between; |
| using testing::Invoke; |
| using testing::Return; |
| using testing::NiceMock; |
| @@ -33,6 +37,13 @@ ACTION_TEMPLATE(RunCallback, |
| return ::testing::get<k>(args).Run(); |
| } |
| +// Invokes Run() on the k-th argument of the function with 1 argument. |
| +ACTION_TEMPLATE(RunCallback, |
| + HAS_1_TEMPLATE_PARAMS(int, k), |
| + AND_1_VALUE_PARAMS(p0)) { |
| + return ::std::tr1::get<k>(args).Run(p0); |
| +} |
| + |
| // Invokes Run() on the k-th argument of the function with the result |
| // of |func| as an argument. |
| ACTION_TEMPLATE(RunCallbackWithResult, |
| @@ -40,6 +51,17 @@ ACTION_TEMPLATE(RunCallbackWithResult, |
| AND_1_VALUE_PARAMS(func)) { |
| return ::testing::get<k>(args).Run(func()); |
| } |
| + |
| +// Function to iterate over the adapter's devices and return the one |
| +// that matches the address. |
| +ACTION_P(GetMockDevice, adapter) { |
| + std::string address = arg0; |
| + for (auto& device : adapter->GetMockDevices()) { |
|
Jeffrey Yasskin
2015/05/20 22:19:48
Can you spell out "const BluetoothDevice*" here in
ortuno
2015/05/20 23:35:19
It's actually only BluetoothDevice*. Done.
|
| + if (device->GetAddress() == address) |
| + return device; |
| + } |
| + return NULL; |
| +} |
| } |
| namespace content { |
| @@ -57,6 +79,10 @@ LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
| else if (fake_adapter_name == "Single Empty Device" || |
| fake_adapter_name == "SingleEmptyDeviceAdapter") { |
| return GetSingleEmptyDeviceAdapter(); |
| + } else if (fake_adapter_name == "ConnectableDeviceAdapter") { |
| + return GetConnectableDeviceAdapter(); |
| + } else if (fake_adapter_name == "UnconnectableDeviceAdapter") { |
| + return GetUnconnectableDeviceAdapter(); |
| } else if (fake_adapter_name == "") { |
| return NULL; |
| } |
| @@ -81,6 +107,10 @@ LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
| .WillByDefault( |
| Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
| + // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| + // matching the address provided if the device was added to the mock. |
| + ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
|
Jeffrey Yasskin
2015/05/20 22:19:48
I would probably have GetMockDevice() take *adapte
ortuno
2015/05/20 23:35:19
GetMockDevice(*adapter) tries to copy adapter but
Jeffrey Yasskin
2015/05/21 00:12:00
Aha! Never mind then.
|
| + |
| return adapter.Pass(); |
| } |
| @@ -95,6 +125,38 @@ LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
| } |
| // static |
| +scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| +LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { |
| + scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| + |
| + adapter->AddMockDevice(GetConnectableDevice(adapter.get())); |
| + |
| + return adapter.Pass(); |
| +} |
| + |
| +// static |
| +scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| +LayoutTestBluetoothAdapterProvider::GetUnconnectableDeviceAdapter() { |
| + scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| + |
| + adapter->AddMockDevice(GetUnconnectableDevice(adapter.get())); |
| + |
| + return adapter.Pass(); |
| +} |
| + |
| +// static |
| +scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> |
| +LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { |
| + scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
| + new NiceMock<MockBluetoothDiscoverySession>()); |
| + |
| + ON_CALL(*discovery_session, Stop(_, _)) |
| + .WillByDefault(RunCallback<0 /* success_callback */>()); |
| + |
| + return discovery_session.Pass(); |
| +} |
| + |
| +// static |
| scoped_ptr<NiceMock<MockBluetoothDevice>> |
| LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| MockBluetoothAdapter* adapter) { |
| @@ -118,15 +180,36 @@ LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| } |
| // static |
| -scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> |
| -LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { |
|
Jeffrey Yasskin
2015/05/20 22:19:48
No need to revert it here, but try not to reorder
ortuno
2015/05/20 23:35:20
Sorry.
|
| - scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
| - new NiceMock<MockBluetoothDiscoverySession>()); |
| +scoped_ptr<NiceMock<MockBluetoothDevice>> |
| +LayoutTestBluetoothAdapterProvider::GetConnectableDevice( |
| + MockBluetoothAdapter* adapter) { |
| + scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); |
| - ON_CALL(*discovery_session, Stop(_, _)) |
| - .WillByDefault(RunCallback<0 /* success_callback */>()); |
| + BluetoothDevice* device_ptr = device.get(); |
| - return discovery_session.Pass(); |
| + ON_CALL(*device, CreateGattConnection(_, _)) |
| + .WillByDefault( |
| + RunCallbackWithResult<0 /* success_callback */>([device_ptr]() { |
| + scoped_ptr<NiceMock<MockBluetoothGattConnection>> connection( |
| + new NiceMock<MockBluetoothGattConnection>( |
| + device_ptr->GetAddress())); |
| + return connection.Pass(); |
| + })); |
| + |
| + return device.Pass(); |
| +} |
| + |
| +// static |
| +scoped_ptr<NiceMock<MockBluetoothDevice>> |
| +LayoutTestBluetoothAdapterProvider::GetUnconnectableDevice( |
| + MockBluetoothAdapter* adapter) { |
| + scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); |
| + |
| + ON_CALL(*device, CreateGattConnection(_, _)) |
| + .WillByDefault( |
| + RunCallback<1 /* error_callback */>(BluetoothDevice::ERROR_FAILED)); |
| + |
| + return device.Pass(); |
| } |
| } // namespace content |