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

bluetooth: Move mock creation out of BluetoothDispatcherHost to

LayoutTestBluetoothAdapterProvider.

This patch doesn't add any new tests.

Split for easier reviewing. This is the last of two patches to remove
testing from BluetoothDispatcherHost and BluetoothDispatcher:

  [1] http://crrev.com/1125133005
  [2] This patch.

BUG=436284

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

Cr-Commit-Position: refs/heads/master@{#330748}
parent 20187ac5
......@@ -16,16 +16,14 @@ using device::BluetoothAdapterFactory;
namespace content {
const uint32 kUnspecifiedDeviceClass =
0x1F00; // bluetooth.org/en-us/specification/assigned-numbers/baseband
const int kScanTime = 5; // 5 seconds of scan time
const int kTestingScanTime = 0; // No need to scan for tests
BluetoothDispatcherHost::BluetoothDispatcherHost()
: BrowserMessageFilter(BluetoothMsgStart),
bluetooth_mock_data_set_(MockData::NOT_MOCKING),
bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
current_scan_time_ = kScanTime;
if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable())
BluetoothAdapterFactory::GetAdapter(
base::Bind(&BluetoothDispatcherHost::set_adapter,
......@@ -56,20 +54,10 @@ bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) {
}
void BluetoothDispatcherHost::SetBluetoothAdapterForTesting(
const std::string& name) {
scoped_refptr<device::BluetoothAdapter> mock_adapter) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (name == "RejectRequestDevice_NotFoundError") {
bluetooth_mock_data_set_ = MockData::REJECT;
bluetooth_request_device_reject_type_ = BluetoothError::NOT_FOUND;
} else if (name == "RejectRequestDevice_SecurityError") {
bluetooth_mock_data_set_ = MockData::REJECT;
bluetooth_request_device_reject_type_ = BluetoothError::SECURITY;
} else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove.
name == "Single Empty Device") {
bluetooth_mock_data_set_ = MockData::RESOLVE;
} else {
bluetooth_mock_data_set_ = MockData::NOT_MOCKING;
}
current_scan_time_ = kTestingScanTime;
set_adapter(mock_adapter.Pass());
}
BluetoothDispatcherHost::~BluetoothDispatcherHost() {
......@@ -90,53 +78,21 @@ void BluetoothDispatcherHost::set_adapter(
void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// TODO(scheib) Extend this very simple mock implementation by using
// device/bluetooth/test mock adapter and related classes.
switch (bluetooth_mock_data_set_) {
case MockData::NOT_MOCKING: {
// TODO(scheib): Filter devices by services: crbug.com/440594
// TODO(scheib): Device selection UI: crbug.com/436280
// TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed.
BluetoothAdapter::DeviceList devices;
if (adapter_.get()) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted,
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id),
base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError,
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id));
} else {
DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice.";
Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id,
BluetoothError::NOT_FOUND));
}
return;
}
case MockData::REJECT: {
Send(new BluetoothMsg_RequestDeviceError(
thread_id, request_id, bluetooth_request_device_reject_type_));
return;
}
case MockData::RESOLVE: {
std::vector<std::string> uuids;
uuids.push_back("00001800-0000-1000-8000-00805f9b34fb");
uuids.push_back("00001801-0000-1000-8000-00805f9b34fb");
content::BluetoothDevice device_ipc(
"Empty Mock Device instanceID", // instance_id
base::UTF8ToUTF16("Empty Mock Device name"), // name
kUnspecifiedDeviceClass, // device_class
device::BluetoothDevice::VENDOR_ID_BLUETOOTH, // vendor_id_source
0xFFFF, // vendor_id
1, // product_id
2, // product_version
true, // paired
uuids); // uuids
Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id,
device_ipc));
return;
}
// TODO(scheib): Filter devices by services: crbug.com/440594
// TODO(scheib): Device selection UI: crbug.com/436280
// TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed.
if (adapter_.get()) {
adapter_->StartDiscoverySession(
base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted,
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id),
base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError,
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id));
} else {
DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice.";
Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id,
BluetoothError::NOT_FOUND));
}
NOTREACHED();
return;
}
void BluetoothDispatcherHost::OnConnectGATT(
......@@ -160,7 +116,7 @@ void BluetoothDispatcherHost::OnDiscoverySessionStarted(
base::Bind(&BluetoothDispatcherHost::StopDiscoverySession,
weak_ptr_factory_.GetWeakPtr(), thread_id, request_id,
base::Passed(&discovery_session)),
base::TimeDelta::FromSeconds(kScanTime));
base::TimeDelta::FromSeconds(current_scan_time_));
}
void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id,
......
......@@ -34,7 +34,8 @@ class CONTENT_EXPORT BluetoothDispatcherHost final
BrowserThread::ID* thread) override;
bool OnMessageReceived(const IPC::Message& message) override;
void SetBluetoothAdapterForTesting(const std::string& name);
void SetBluetoothAdapterForTesting(
scoped_refptr<device::BluetoothAdapter> mock_adapter);
protected:
~BluetoothDispatcherHost() override;
......@@ -69,13 +70,12 @@ class CONTENT_EXPORT BluetoothDispatcherHost final
void OnDiscoverySessionStopped(int thread_id, int request_id);
void OnDiscoverySessionStoppedError(int thread_id, int request_id);
// Defines how long to scan for.
int current_scan_time_;
// A BluetoothAdapter instance representing an adapter of the system.
scoped_refptr<device::BluetoothAdapter> adapter_;
enum class MockData { NOT_MOCKING, REJECT, RESOLVE };
MockData bluetooth_mock_data_set_;
BluetoothError bluetooth_request_device_reject_type_;
// Must be last member, see base/memory/weak_ptr.h documentation
base::WeakPtrFactory<BluetoothDispatcherHost> weak_ptr_factory_;
......
......@@ -51,6 +51,7 @@
'../components/components.gyp:devtools_http_handler',
'../components/components.gyp:web_cache_renderer',
'../device/bluetooth/bluetooth.gyp:device_bluetooth',
'../device/bluetooth/bluetooth.gyp:device_bluetooth_mocks',
'../gin/gin.gyp:gin',
'../gpu/gpu.gyp:gpu',
'../ipc/ipc.gyp:ipc',
......@@ -62,6 +63,8 @@
'../storage/storage_browser.gyp:storage',
'../third_party/WebKit/public/blink.gyp:blink',
'../third_party/WebKit/public/blink.gyp:blink_test_support',
'../testing/gmock.gyp:gmock',
'../testing/gtest.gyp:gtest',
'../ui/base/ime/ui_base_ime.gyp:ui_base_ime',
'../ui/base/ui_base.gyp:ui_base',
'../ui/events/events.gyp:events_base',
......@@ -100,6 +103,8 @@
'shell/browser/ipc_echo_message_filter.h',
'shell/browser/layout_test/layout_test_android.cc',
'shell/browser/layout_test/layout_test_android.h',
'shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc',
'shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h',
'shell/browser/layout_test/layout_test_browser_context.cc',
'shell/browser/layout_test/layout_test_browser_context.h',
'shell/browser/layout_test/layout_test_browser_main.cc',
......
......@@ -26,6 +26,10 @@ class WebView;
class WebURLResponse;
}
namespace device {
class BluetoothAdapter;
}
namespace content {
class PageState;
......@@ -94,7 +98,8 @@ void SetDeviceScaleFactor(RenderView* render_view, float factor);
void SetDeviceColorProfile(RenderView* render_view, const std::string& name);
// Change the bluetooth test adapter while running a layout test.
void SetBluetoothAdapter(int render_process_id, const std::string& name);
void SetBluetoothAdapter(int render_process_id,
scoped_refptr<device::BluetoothAdapter> adapter);
// Enables mock geofencing service while running a layout test.
// |service_available| indicates if the mock service should mock geofencing
......
......@@ -45,6 +45,8 @@ static_library("content_shell_lib") {
"browser/ipc_echo_message_filter.h",
"browser/layout_test/layout_test_android.cc",
"browser/layout_test/layout_test_android.h",
"browser/layout_test/layout_test_bluetooth_adapter_provider.cc",
"browser/layout_test/layout_test_bluetooth_adapter_provider.h",
"browser/layout_test/layout_test_browser_context.cc",
"browser/layout_test/layout_test_browser_context.h",
"browser/layout_test/layout_test_browser_main.cc",
......@@ -258,6 +260,7 @@ static_library("content_shell_lib") {
"//content/gpu",
"//content/test:layouttest_support",
"//device/bluetooth",
"//device/bluetooth:mocks",
"//gin",
"//gpu",
"//ipc",
......@@ -266,6 +269,8 @@ static_library("content_shell_lib") {
"//net:net_resources",
"//skia",
"//storage/browser",
"//testing/gmock",
"//testing/gtest",
"//third_party/WebKit/public:blink",
"//third_party/WebKit/public:image_resources",
"//third_party/WebKit/public:resources",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
#include "testing/gmock/include/gmock/gmock.h"
using device::BluetoothAdapter;
using device::BluetoothAdapterFactory;
using device::BluetoothDevice;
using device::BluetoothDiscoverySession;
using device::BluetoothUUID;
using device::MockBluetoothAdapter;
using device::MockBluetoothDevice;
using device::MockBluetoothDiscoverySession;
using testing::Invoke;
using testing::Return;
using testing::NiceMock;
using testing::_;
namespace content {
// static
scoped_refptr<BluetoothAdapter>
LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter(
const std::string& fake_adapter_name) {
// TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified
if (fake_adapter_name == "RejectRequestDevice_NotFoundError" ||
fake_adapter_name == "EmptyAdapter") {
return GetEmptyAdapter();
}
// TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified
else if (fake_adapter_name == "Single Empty Device" ||
fake_adapter_name == "SingleEmptyDeviceAdapter") {
return GetSingleEmptyDeviceAdapter();
} else if (fake_adapter_name == "") {
return NULL;
}
NOTREACHED();
return NULL;
}
// static
scoped_refptr<NiceMock<MockBluetoothAdapter>>
LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() {
scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(
new NiceMock<MockBluetoothAdapter>());
ON_CALL(*adapter, StartDiscoverySession(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession));
ON_CALL(*adapter, GetDevices())
.WillByDefault(Return(adapter->GetConstMockDevices()));
return adapter.Pass();
}
// static
scoped_refptr<NiceMock<MockBluetoothAdapter>>
LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() {
scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(
new NiceMock<MockBluetoothAdapter>());
ON_CALL(*adapter, StartDiscoverySession(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession));
adapter->AddMockDevice(GetEmptyDevice(adapter.get()));
ON_CALL(*adapter, GetDevices())
.WillByDefault(Return(adapter->GetConstMockDevices()));
return adapter.Pass();
}
// static
scoped_ptr<NiceMock<MockBluetoothDevice>>
LayoutTestBluetoothAdapterProvider::GetEmptyDevice(
MockBluetoothAdapter* adapter) {
scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device(
new NiceMock<MockBluetoothDevice>(
adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name",
"Empty Mock Device instanceID", true /* Paired */,
true /* Connected */));
ON_CALL(*empty_device, GetVendorIDSource())
.WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH));
ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF));
ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1));
ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2));
BluetoothDevice::UUIDList list;
list.push_back(BluetoothUUID("1800"));
list.push_back(BluetoothUUID("1801"));
ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list));
return empty_device.Pass();
}
// static
void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySession(
const BluetoothAdapter::DiscoverySessionCallback& callback,
const BluetoothAdapter::ErrorCallback& error_callback) {
scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session(
new NiceMock<MockBluetoothDiscoverySession>());
ON_CALL(*discovery_session, Stop(_, _))
.WillByDefault(Invoke(
&LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop));
callback.Run(discovery_session.Pass());
}
// static
void LayoutTestBluetoothAdapterProvider::SuccessfulDiscoverySessionStop(
const base::Closure& callback,
const base::Closure& error_callback) {
callback.Run();
}
} // namespace content
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_BLUETOOTH_ADAPTER_PROVIDER_H_
#define CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_BLUETOOTH_ADAPTER_PROVIDER_H_
#include "base/callback.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
namespace content {
// Implements fake adapters with named mock data set for use in tests as a
// result of layout tests calling testRunner.setBluetoothMockDataSet.
class LayoutTestBluetoothAdapterProvider {
public:
// Returns a BluetoothAdapter. Its behavior depends on |fake_adapter_name|.
static scoped_refptr<device::BluetoothAdapter> GetBluetoothAdapter(
const std::string& fake_adapter_name);
private:
// Returns "EmptyAdapter" fake BluetoothAdapter with the following
// characteristics:
// - |StartDiscoverySession| invokes |SuccessfulDiscoverySession|.
// - |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|.
// - |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 an |EmptyDevice| with the following characeteristics:
// - |GetAddress| returns "Empty Mock Device instanceID".
// - |GetName| returns "Empty Mock Device name".
// - |GetBluetoothClass| returns 0x1F00. "Unspecified Device Class": see
// bluetooth.org/en-us/specification/assigned-numbers/baseband
// - |GetVendorIDSource| returns |BluetoothDevice::VENDOR_ID_BLUETOOTH|.
// - |GetVendorID| returns 0xFFFF.
// - |GetProductID| returns 1.
// - |GetDeviceID| returns 2.
// - |IsPaired| returns true.
// - |GetUUIDs| returns a list with two UUIDs: "1800" and "1801".
static scoped_ptr<testing::NiceMock<device::MockBluetoothDevice>>
GetEmptyDevice(device::MockBluetoothAdapter* adapter);
};
} // namespace content
#endif // CONTENT_SHELL_BROWSER_LAYOUT_TEST_LAYOUT_TEST_BLUETOOTH_ADAPTER_PROVIDER_H_
......@@ -9,6 +9,7 @@
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/permission_type.h"
#include "content/public/test/layouttest_support.h"
#include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h"
#include "content/shell/browser/layout_test/layout_test_browser_context.h"
#include "content/shell/browser/layout_test/layout_test_content_browser_client.h"
#include "content/shell/browser/layout_test/layout_test_notification_manager.h"
......@@ -167,8 +168,9 @@ void LayoutTestMessageFilter::OnResetPermissions() {
void LayoutTestMessageFilter::OnSetBluetoothAdapter(const std::string& name) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// TODO(ortuno): Create mock adapter here. See http://crrev.com/1132943002
SetBluetoothAdapter(render_process_id_, name);
SetBluetoothAdapter(
render_process_id_,
LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter(name));
}
} // namespace content
......@@ -7,6 +7,7 @@ include_rules = [
"+chromeos/audio", # For WebRTC tests.
# Testing utilities can access anything in content/
"+content",
"+device/bluetooth", # For WebBluetooth tests
# For loading V8's initial snapshot from external files.
"+gin/v8_initializer.h",
"+media/audio", # For AudioParameters in WebRTC tests.
......
......@@ -24,6 +24,7 @@
#include "content/shell/renderer/test_runner/test_common.h"
#include "content/shell/renderer/test_runner/web_frame_test_proxy.h"
#include "content/shell/renderer/test_runner/web_test_proxy.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "third_party/WebKit/public/platform/WebBatteryStatus.h"
#include "third_party/WebKit/public/platform/WebGamepads.h"
#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionData.h"
......@@ -313,16 +314,17 @@ void SetDeviceColorProfile(RenderView* render_view, const std::string& name) {
SetDeviceColorProfileForTesting(color_profile);
}
void SetBluetoothAdapter(int render_process_id, const std::string& name) {
void SetBluetoothAdapter(int render_process_id,
scoped_refptr<device::BluetoothAdapter> adapter) {
RenderProcessHostImpl* render_process_host_impl =
static_cast<RenderProcessHostImpl*>(
RenderProcessHost::FromID(render_process_id));
BluetoothDispatcherHost* dispatcher_host = render_process_host_impl
->GetBluetoothDispatcherHost();
BluetoothDispatcherHost* dispatcher_host =
render_process_host_impl->GetBluetoothDispatcherHost();
if (dispatcher_host != NULL)
dispatcher_host->SetBluetoothAdapterForTesting(name);
dispatcher_host->SetBluetoothAdapterForTesting(adapter.Pass());
}
void SetGeofencingMockProvider(bool service_available) {
......
......@@ -48,6 +48,27 @@ void MockBluetoothAdapter::StartDiscoverySessionWithFilter(
error_callback);
}
void MockBluetoothAdapter::AddMockDevice(
scoped_ptr<MockBluetoothDevice> mock_device) {
mock_devices_.push_back(mock_device.Pass());
}
BluetoothAdapter::ConstDeviceList MockBluetoothAdapter::GetConstMockDevices() {
BluetoothAdapter::ConstDeviceList devices;
for (auto& it : mock_devices_) {
devices.push_back(it);
}
return devices;
}
BluetoothAdapter::DeviceList MockBluetoothAdapter::GetMockDevices() {
BluetoothAdapter::DeviceList devices;
for (auto& it : mock_devices_) {
devices.push_back(it);
}
return devices;
}
void MockBluetoothAdapter::RegisterAdvertisement(
scoped_ptr<BluetoothAdvertisement::Data> advertisement_data,
const CreateAdvertisementCallback& callback,
......
......@@ -8,10 +8,12 @@
#include <string>
#include "base/callback.h"
#include "base/memory/scoped_vector.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_audio_sink.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/test/mock_bluetooth_device.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace device {
......@@ -99,6 +101,14 @@ class MockBluetoothAdapter : public BluetoothAdapter {
const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback);
// BluetoothAdapter is supposed to manage the lifetime of BluetoothDevices.
// This methods takes ownership of the BluetoothDevices. This is only for
// convenience as far testing is concerned and it's possible to write test
// cases without using these functions.
void AddMockDevice(scoped_ptr<MockBluetoothDevice> mock_device);
BluetoothAdapter::ConstDeviceList GetConstMockDevices();
BluetoothAdapter::DeviceList GetMockDevices();
protected:
void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter,
const base::Closure& callback,
......@@ -117,6 +127,8 @@ class MockBluetoothAdapter : public BluetoothAdapter {
MOCK_METHOD1(RemovePairingDelegateInternal,
void(BluetoothDevice::PairingDelegate* pairing_delegate));
ScopedVector<MockBluetoothDevice> mock_devices_;
};
} // namespace device
......
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