Convert browser_tests to Swarming.
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_bluetooth_adapter_provider.cc
blobd7c1c845827d911aa8f54509a6b11963bf224882
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h"
7 #include "device/bluetooth/bluetooth_adapter.h"
8 #include "device/bluetooth/bluetooth_device.h"
9 #include "device/bluetooth/bluetooth_discovery_session.h"
10 #include "device/bluetooth/bluetooth_uuid.h"
11 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
12 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
13 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 using device::BluetoothAdapter;
17 using device::BluetoothAdapterFactory;
18 using device::BluetoothDevice;
19 using device::BluetoothDiscoverySession;
20 using device::BluetoothGattConnection;
21 using device::BluetoothGattService;
22 using device::BluetoothUUID;
23 using device::MockBluetoothAdapter;
24 using device::MockBluetoothDevice;
25 using device::MockBluetoothDiscoverySession;
26 using device::MockBluetoothGattCharacteristic;
27 using device::MockBluetoothGattConnection;
28 using device::MockBluetoothGattService;
29 using testing::Between;
30 using testing::Invoke;
31 using testing::Return;
32 using testing::NiceMock;
33 using testing::_;
35 namespace {
36 // Invokes Run() on the k-th argument of the function with no arguments.
37 ACTION_TEMPLATE(RunCallback,
38 HAS_1_TEMPLATE_PARAMS(int, k),
39 AND_0_VALUE_PARAMS()) {
40 return ::testing::get<k>(args).Run();
43 // Invokes Run() on the k-th argument of the function with 1 argument.
44 ACTION_TEMPLATE(RunCallback,
45 HAS_1_TEMPLATE_PARAMS(int, k),
46 AND_1_VALUE_PARAMS(p0)) {
47 return ::testing::get<k>(args).Run(p0);
50 // Invokes Run() on the k-th argument of the function with the result
51 // of |func| as an argument.
52 ACTION_TEMPLATE(RunCallbackWithResult,
53 HAS_1_TEMPLATE_PARAMS(int, k),
54 AND_1_VALUE_PARAMS(func)) {
55 return ::testing::get<k>(args).Run(func());
58 // Function to iterate over the adapter's devices and return the one
59 // that matches the address.
60 ACTION_P(GetMockDevice, adapter) {
61 std::string address = arg0;
62 for (BluetoothDevice* device : adapter->GetMockDevices()) {
63 if (device->GetAddress() == address)
64 return device;
66 return NULL;
69 } // namespace
71 namespace content {
73 // static
74 scoped_refptr<BluetoothAdapter>
75 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter(
76 const std::string& fake_adapter_name) {
77 if (fake_adapter_name == "EmptyAdapter")
78 return GetEmptyAdapter();
79 else if (fake_adapter_name == "SingleEmptyDeviceAdapter")
80 return GetSingleEmptyDeviceAdapter();
81 else if (fake_adapter_name == "ConnectableDeviceAdapter")
82 return GetConnectableDeviceAdapter();
83 else if (fake_adapter_name == "UnconnectableDeviceAdapter")
84 return GetUnconnectableDeviceAdapter();
85 else if (fake_adapter_name == "")
86 return NULL;
88 NOTREACHED();
89 return NULL;
92 // static
93 scoped_refptr<NiceMock<MockBluetoothAdapter>>
94 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() {
95 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(
96 new NiceMock<MockBluetoothAdapter>());
98 ON_CALL(*adapter, StartDiscoverySession(_, _))
99 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
100 []() { return GetDiscoverySession(); }));
102 // Using Invoke allows the adapter returned from this method to be futher
103 // modified and have devices added to it. The call to ::GetDevices will
104 // invoke ::GetConstMockDevices, returning all devices added up to that time.
105 ON_CALL(*adapter, GetDevices())
106 .WillByDefault(
107 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices));
109 // The call to ::GetDevice will invoke GetMockDevice which returns a device
110 // matching the address provided if the device was added to the mock.
111 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get()));
113 return adapter.Pass();
116 // static
117 scoped_refptr<NiceMock<MockBluetoothAdapter>>
118 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() {
119 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter());
121 adapter->AddMockDevice(GetEmptyDevice(adapter.get()));
123 return adapter.Pass();
126 // static
127 scoped_refptr<NiceMock<MockBluetoothAdapter>>
128 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() {
129 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter());
131 adapter->AddMockDevice(GetConnectableDevice(adapter.get()));
133 return adapter.Pass();
136 // static
137 scoped_refptr<NiceMock<MockBluetoothAdapter>>
138 LayoutTestBluetoothAdapterProvider::GetUnconnectableDeviceAdapter() {
139 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter());
141 adapter->AddMockDevice(GetUnconnectableDevice(adapter.get()));
143 return adapter.Pass();
146 // static
147 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>>
148 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() {
149 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session(
150 new NiceMock<MockBluetoothDiscoverySession>());
152 ON_CALL(*discovery_session, Stop(_, _))
153 .WillByDefault(RunCallback<0 /* success_callback */>());
155 return discovery_session.Pass();
158 // static
159 scoped_ptr<NiceMock<MockBluetoothDevice>>
160 LayoutTestBluetoothAdapterProvider::GetEmptyDevice(
161 MockBluetoothAdapter* adapter) {
162 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device(
163 new NiceMock<MockBluetoothDevice>(
164 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name",
165 "Empty Mock Device instanceID", true /* Paired */,
166 true /* Connected */));
168 ON_CALL(*empty_device, GetVendorIDSource())
169 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH));
170 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF));
171 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1));
172 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2));
174 BluetoothDevice::UUIDList list;
175 list.push_back(BluetoothUUID("1800"));
176 list.push_back(BluetoothUUID("1801"));
177 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list));
179 scoped_ptr<NiceMock<MockBluetoothGattService>> generic_access(
180 GetGattService(empty_device.get(), "1800" /* Generic Access */));
181 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> device_name(
182 GetGattCharacteristic(generic_access.get(), "2A00" /* Device Name */));
184 std::string value_str("Empty Mock Device name");
185 std::vector<uint8_t> value(value_str.begin(), value_str.end());
186 ON_CALL(*device_name, ReadRemoteCharacteristic(_, _))
187 .WillByDefault(RunCallback<0>(value));
189 generic_access->AddMockCharacteristic(device_name.Pass());
191 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> reconnection_address(
192 GetGattCharacteristic(generic_access.get(),
193 "2A03" /* Reconnection Address */));
195 ON_CALL(*reconnection_address, ReadRemoteCharacteristic(_, _))
196 .WillByDefault(
197 RunCallback<1>(BluetoothGattService::GATT_ERROR_NOT_PERMITTED));
199 generic_access->AddMockCharacteristic(reconnection_address.Pass());
201 empty_device->AddMockService(generic_access.Pass());
203 // Using Invoke allows the device returned from this method to be futher
204 // modified and have more services added to it. The call to ::GetGattServices
205 // will invoke ::GetMockServices, returning all services added up to that
206 // time.
207 ON_CALL(*empty_device, GetGattServices())
208 .WillByDefault(
209 Invoke(empty_device.get(), &MockBluetoothDevice::GetMockServices));
211 // The call to BluetoothDevice::GetGattService will invoke ::GetMockService
212 // which returns a service matching the identifier provided if the service
213 // was added to the mock.
214 ON_CALL(*empty_device, GetGattService(_))
215 .WillByDefault(
216 Invoke(empty_device.get(), &MockBluetoothDevice::GetMockService));
218 return empty_device.Pass();
221 // static
222 scoped_ptr<NiceMock<MockBluetoothDevice>>
223 LayoutTestBluetoothAdapterProvider::GetConnectableDevice(
224 MockBluetoothAdapter* adapter) {
225 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter));
227 BluetoothDevice* device_ptr = device.get();
229 ON_CALL(*device, CreateGattConnection(_, _))
230 .WillByDefault(
231 RunCallbackWithResult<0 /* success_callback */>([device_ptr]() {
232 return make_scoped_ptr(new NiceMock<MockBluetoothGattConnection>(
233 device_ptr->GetAddress()));
234 }));
236 return device.Pass();
239 // static
240 scoped_ptr<NiceMock<MockBluetoothDevice>>
241 LayoutTestBluetoothAdapterProvider::GetUnconnectableDevice(
242 MockBluetoothAdapter* adapter) {
243 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter));
245 ON_CALL(*device, CreateGattConnection(_, _))
246 .WillByDefault(
247 RunCallback<1 /* error_callback */>(BluetoothDevice::ERROR_FAILED));
249 return device.Pass();
252 // static
253 scoped_ptr<NiceMock<MockBluetoothGattService>>
254 LayoutTestBluetoothAdapterProvider::GetGattService(MockBluetoothDevice* device,
255 const std::string& uuid) {
256 scoped_ptr<NiceMock<MockBluetoothGattService>> service(
257 new NiceMock<MockBluetoothGattService>(
258 device, uuid /* identifier */, BluetoothUUID(uuid),
259 true /* is_primary */, false /* is_local */));
261 ON_CALL(*service, GetCharacteristics())
262 .WillByDefault(Invoke(service.get(),
263 &MockBluetoothGattService::GetMockCharacteristics));
265 ON_CALL(*service, GetCharacteristic(_))
266 .WillByDefault(Invoke(service.get(),
267 &MockBluetoothGattService::GetMockCharacteristic));
269 return service.Pass();
272 // static
273 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>>
274 LayoutTestBluetoothAdapterProvider::GetGattCharacteristic(
275 MockBluetoothGattService* service,
276 const std::string& uuid) {
277 return make_scoped_ptr(new NiceMock<MockBluetoothGattCharacteristic>(
278 service, uuid /* identifier */, BluetoothUUID(uuid), false /* is_local */,
279 NULL /* properties */, NULL /* permissions */));
282 } // namespace content