Commit 12945053 authored by ortuno's avatar ortuno Committed by Commit bot

bluetooth: Refactor LayoutTestBluetoothAdapterProvider.

This CL removes all Invoke() calls and uses ACTION_TEMPLATE instead.
This makes the code easier to read as now the values passed to the callbacks are no
longer hidden inside other functions.

Also change the definition of GetDevices to call adapter->GetConstMockDevices directly.
This allows us to use the EmptyAdapter as a base adapter on which we can build
other adapters.

BUG=436284

Review URL: https://codereview.chromium.org/1138913006

Cr-Commit-Position: refs/heads/master@{#330760}
parent c7f40bc3
......@@ -25,6 +25,23 @@ using testing::Return;
using testing::NiceMock;
using testing::_;
namespace {
// Invokes Run() on the k-th argument of the function with no arguments.
ACTION_TEMPLATE(RunCallback,
HAS_1_TEMPLATE_PARAMS(int, k),
AND_0_VALUE_PARAMS()) {
return ::testing::get<k>(args).Run();
}
// Invokes Run() on the k-th argument of the function with the result
// of |func| as an argument.
ACTION_TEMPLATE(RunCallbackWithResult,
HAS_1_TEMPLATE_PARAMS(int, k),
AND_1_VALUE_PARAMS(func)) {
return ::testing::get<k>(args).Run(func());
}
}
namespace content {
// static
......@@ -54,11 +71,15 @@ LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() {
new NiceMock<MockBluetoothAdapter>());
ON_CALL(*adapter, StartDiscoverySession(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession));
.WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
[]() { return GetDiscoverySession(); }));
// Using Invoke allows the adapter returned from this method to be futher
// modified and have devices added to it. The call to ::GetDevices will
// invoke ::GetConstMockDevices, returning all devices added up to that time.
ON_CALL(*adapter, GetDevices())
.WillByDefault(Return(adapter->GetConstMockDevices()));
.WillByDefault(
Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices));
return adapter.Pass();
}
......@@ -66,18 +87,10 @@ LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() {
// static
scoped_refptr<NiceMock<MockBluetoothAdapter>>
LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() {
scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(
new NiceMock<MockBluetoothAdapter>());
ON_CALL(*adapter, StartDiscoverySession(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession));
scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter());
adapter->AddMockDevice(GetEmptyDevice(adapter.get()));
ON_CALL(*adapter, GetDevices())
.WillByDefault(Return(adapter->GetConstMockDevices()));
return adapter.Pass();
}
......@@ -105,24 +118,15 @@ LayoutTestBluetoothAdapterProvider::GetEmptyDevice(
}
// static
void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession(
const BluetoothAdapter::DiscoverySessionCallback& callback,
const BluetoothAdapter::ErrorCallback& error_callback) {
scoped_ptr<NiceMock<MockBluetoothDiscoverySession>>
LayoutTestBluetoothAdapterProvider::GetDiscoverySession() {
scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session(
new NiceMock<MockBluetoothDiscoverySession>());
ON_CALL(*discovery_session, Stop(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop));
.WillByDefault(RunCallback<0 /* success_callback */>());
callback.Run(discovery_session.Pass());
}
// static
void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop(
const base::Closure& callback,
const base::Closure& error_callback) {
callback.Run();
return discovery_session.Pass();
}
} // namespace content
......@@ -9,6 +9,7 @@
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
namespace content {
......@@ -23,29 +24,24 @@ class LayoutTestBluetoothAdapterProvider {
private:
// Returns "EmptyAdapter" fake BluetoothAdapter with the following
// characteristics:
// - |StartDiscoverySession| invokes |SuccessfulDiscoverySession|.
// - |StartDiscoverySession| runs the first argument with |DiscoverySession|
// as argument.
// - |GetDevices| returns an empty list of devices.
static scoped_refptr<testing::NiceMock<device::MockBluetoothAdapter>>
GetEmptyAdapter();
// Returns "SingleEmptyDevice" fake BluetoothAdapter with the following
// characteristics:
// - |StartDiscoverySession| invokes |SuccessfulDiscoverySession|.
// - |StartDiscoverySession| runs the first argument with |DiscoverySession|
// as argument.
// - |GetDevices| returns a list with an |EmptyDevice|.
static scoped_refptr<testing::NiceMock<device::MockBluetoothAdapter>>
GetSingleEmptyDeviceAdapter();
// Calls |callback| with a DiscoverySession with the following
// characteristics:
// - |Stop| will invoke |SuccessfulDiscoverySessionStop|.
static void SuccessfulDiscoverySession(
const device::BluetoothAdapter::DiscoverySessionCallback& callback,
const device::BluetoothAdapter::ErrorCallback& error_callback);
// Calls |callback|.
static void SuccessfulDiscoverySessionStop(
const base::Closure& callback,
const base::Closure& error_callback);
// Returns a fake DiscoverySession with the following characteristics:
// - |Stop| runs the first argument.
static scoped_ptr<testing::NiceMock<device::MockBluetoothDiscoverySession>>
GetDiscoverySession();
// Returns an |EmptyDevice| with the following characeteristics:
// - |GetAddress| returns "Empty Mock Device instanceID".
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment