OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" | 5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
12 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
13 #include "device/bluetooth/bluetooth_adapter.h" | 16 #include "device/bluetooth/bluetooth_adapter.h" |
14 #include "device/bluetooth/bluetooth_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
15 #include "device/bluetooth/bluetooth_discovery_session.h" | 18 #include "device/bluetooth/bluetooth_discovery_session.h" |
16 #include "device/bluetooth/bluetooth_uuid.h" | 19 #include "device/bluetooth/bluetooth_uuid.h" |
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
18 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 21 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
19 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 22 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
20 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | 23 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
22 | 25 |
| 26 using base::StringPiece; |
23 using device::BluetoothAdapter; | 27 using device::BluetoothAdapter; |
24 using device::BluetoothDevice; | 28 using device::BluetoothDevice; |
25 using device::BluetoothGattCharacteristic; | 29 using device::BluetoothGattCharacteristic; |
26 using device::BluetoothGattService; | 30 using device::BluetoothGattService; |
27 using device::BluetoothUUID; | 31 using device::BluetoothUUID; |
28 using device::MockBluetoothAdapter; | 32 using device::MockBluetoothAdapter; |
29 using device::MockBluetoothDevice; | 33 using device::MockBluetoothDevice; |
30 using device::MockBluetoothDiscoverySession; | 34 using device::MockBluetoothDiscoverySession; |
31 using device::MockBluetoothGattCharacteristic; | 35 using device::MockBluetoothGattCharacteristic; |
32 using device::MockBluetoothGattConnection; | 36 using device::MockBluetoothGattConnection; |
(...skipping 22 matching lines...) Expand all Loading... |
55 namespace { | 59 namespace { |
56 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). | 60 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). |
57 const char kBatteryServiceUUID[] = "180f"; | 61 const char kBatteryServiceUUID[] = "180f"; |
58 const char kGenericAccessServiceUUID[] = "1800"; | 62 const char kGenericAccessServiceUUID[] = "1800"; |
59 const char kGlucoseServiceUUID[] = "1808"; | 63 const char kGlucoseServiceUUID[] = "1808"; |
60 const char kHeartRateServiceUUID[] = "180d"; | 64 const char kHeartRateServiceUUID[] = "180d"; |
61 const char kHeartRateMeasurementUUID[] = "2a37"; | 65 const char kHeartRateMeasurementUUID[] = "2a37"; |
62 const char kBodySensorLocation[] = "2a38"; | 66 const char kBodySensorLocation[] = "2a38"; |
63 const char kDeviceNameUUID[] = "2a00"; | 67 const char kDeviceNameUUID[] = "2a00"; |
64 | 68 |
| 69 const int kDefaultTxPower = -10; // TxPower of a device broadcasting at 0.1mW. |
| 70 const int kDefaultRssi = -51; // RSSI at 1m from a device broadcasting at |
| 71 // 0.1mW. |
| 72 |
65 // Invokes Run() on the k-th argument of the function with no arguments. | 73 // Invokes Run() on the k-th argument of the function with no arguments. |
66 ACTION_TEMPLATE(RunCallback, | 74 ACTION_TEMPLATE(RunCallback, |
67 HAS_1_TEMPLATE_PARAMS(int, k), | 75 HAS_1_TEMPLATE_PARAMS(int, k), |
68 AND_0_VALUE_PARAMS()) { | 76 AND_0_VALUE_PARAMS()) { |
69 return ::testing::get<k>(args).Run(); | 77 return ::testing::get<k>(args).Run(); |
70 } | 78 } |
71 | 79 |
72 // Invokes Run() on the k-th argument of the function with 1 argument. | 80 // Invokes Run() on the k-th argument of the function with 1 argument. |
73 ACTION_TEMPLATE(RunCallback, | 81 ACTION_TEMPLATE(RunCallback, |
74 HAS_1_TEMPLATE_PARAMS(int, k), | 82 HAS_1_TEMPLATE_PARAMS(int, k), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return GetHeartRateAdapter(); | 142 return GetHeartRateAdapter(); |
135 else if (fake_adapter_name == "FailingConnectionsAdapter") | 143 else if (fake_adapter_name == "FailingConnectionsAdapter") |
136 return GetFailingConnectionsAdapter(); | 144 return GetFailingConnectionsAdapter(); |
137 else if (fake_adapter_name == "FailingGATTOperationsAdapter") | 145 else if (fake_adapter_name == "FailingGATTOperationsAdapter") |
138 return GetFailingGATTOperationsAdapter(); | 146 return GetFailingGATTOperationsAdapter(); |
139 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") | 147 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") |
140 return GetSecondDiscoveryFindsHeartRateAdapter(); | 148 return GetSecondDiscoveryFindsHeartRateAdapter(); |
141 else if (fake_adapter_name == "") | 149 else if (fake_adapter_name == "") |
142 return NULL; | 150 return NULL; |
143 | 151 |
| 152 if (base::StartsWith(fake_adapter_name, "PowerValueAdapter", |
| 153 base::CompareCase::SENSITIVE)) { |
| 154 std::vector<StringPiece> args = |
| 155 base::SplitStringPiece(fake_adapter_name, ":", base::KEEP_WHITESPACE, |
| 156 base::SPLIT_WANT_NONEMPTY); |
| 157 DCHECK(args[0] == "PowerValueAdapter"); |
| 158 DCHECK(args.size() == 3) << "Should contain AdapterName:TxPower:RSSI"; |
| 159 |
| 160 int tx_power; |
| 161 base::StringToInt(args[1], &tx_power); |
| 162 DCHECK(tx_power >= INT8_MIN && tx_power <= INT8_MAX) |
| 163 << "Must be between [-128, 127]"; |
| 164 int rssi; |
| 165 base::StringToInt(args[2], &rssi); |
| 166 DCHECK(rssi >= INT8_MIN && rssi <= INT8_MAX) |
| 167 << "Must be between [-128, 127]"; |
| 168 return GetPowerValueAdapter(tx_power, rssi); |
| 169 } |
| 170 |
| 171 if (base::StartsWith(fake_adapter_name, "PowerPresenceAdapter", |
| 172 base::CompareCase::SENSITIVE)) { |
| 173 std::vector<StringPiece> args = |
| 174 base::SplitStringPiece(fake_adapter_name, ":", base::KEEP_WHITESPACE, |
| 175 base::SPLIT_WANT_NONEMPTY); |
| 176 DCHECK(args[0] == "PowerPresenceAdapter"); |
| 177 DCHECK(args.size() == 3) |
| 178 << "Should contain AdapterName:TxPowerPresent:RSSIPResent"; |
| 179 DCHECK(args[1] == "true" || args[1] == "false"); |
| 180 DCHECK(args[2] == "true" || args[2] == "false"); |
| 181 |
| 182 return GetPowerPresenceAdapter(args[1] == "true" /* tx_power_present */, |
| 183 args[2] == "true" /* rssi_present */); |
| 184 } |
| 185 |
144 NOTREACHED() << fake_adapter_name; | 186 NOTREACHED() << fake_adapter_name; |
145 return NULL; | 187 return NULL; |
146 } | 188 } |
147 | 189 |
148 // Adapters | 190 // Adapters |
149 | 191 |
150 // static | 192 // static |
151 scoped_refptr<NiceMockBluetoothAdapter> | 193 scoped_refptr<NiceMockBluetoothAdapter> |
152 LayoutTestBluetoothAdapterProvider::GetBaseAdapter() { | 194 LayoutTestBluetoothAdapterProvider::GetBaseAdapter() { |
153 scoped_refptr<NiceMockBluetoothAdapter> adapter( | 195 scoped_refptr<NiceMockBluetoothAdapter> adapter( |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 290 |
249 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) | 291 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
250 .WillByDefault(RunCallbackWithResult<1 /* success_callback */>( | 292 .WillByDefault(RunCallbackWithResult<1 /* success_callback */>( |
251 []() { return GetDiscoverySession(); })); | 293 []() { return GetDiscoverySession(); })); |
252 | 294 |
253 return adapter.Pass(); | 295 return adapter.Pass(); |
254 } | 296 } |
255 | 297 |
256 // static | 298 // static |
257 scoped_refptr<NiceMockBluetoothAdapter> | 299 scoped_refptr<NiceMockBluetoothAdapter> |
| 300 LayoutTestBluetoothAdapterProvider::GetPowerValueAdapter(int8_t tx_power, |
| 301 int8_t rssi) { |
| 302 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 303 scoped_ptr<NiceMockBluetoothDevice> device(GetHeartRateDevice(adapter.get())); |
| 304 |
| 305 ON_CALL(*device, GetInquiryTxPower()).WillByDefault(Return(tx_power)); |
| 306 ON_CALL(*device, GetInquiryRSSI()).WillByDefault(Return(rssi)); |
| 307 |
| 308 adapter->AddMockDevice(device.Pass()); |
| 309 |
| 310 return adapter; |
| 311 } |
| 312 |
| 313 // static |
| 314 scoped_refptr<NiceMockBluetoothAdapter> |
| 315 LayoutTestBluetoothAdapterProvider::GetPowerPresenceAdapter( |
| 316 bool tx_power_present, |
| 317 bool rssi_present) { |
| 318 // TODO(ortuno): RSSI Unknown and Tx Power Unknown should have different |
| 319 // values. Add kUnknownTxPower when implemented: http://crbug.com/551572 |
| 320 int tx_power = |
| 321 tx_power_present ? kDefaultTxPower : BluetoothDevice::kUnknownPower; |
| 322 int rssi = rssi_present ? kDefaultRssi : BluetoothDevice::kUnknownPower; |
| 323 |
| 324 return GetPowerValueAdapter(tx_power, rssi); |
| 325 } |
| 326 |
| 327 // static |
| 328 scoped_refptr<NiceMockBluetoothAdapter> |
258 LayoutTestBluetoothAdapterProvider::GetGlucoseHeartRateAdapter() { | 329 LayoutTestBluetoothAdapterProvider::GetGlucoseHeartRateAdapter() { |
259 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 330 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
260 | 331 |
261 adapter->AddMockDevice(GetHeartRateDevice(adapter.get())); | 332 adapter->AddMockDevice(GetHeartRateDevice(adapter.get())); |
262 adapter->AddMockDevice(GetGlucoseDevice(adapter.get())); | 333 adapter->AddMockDevice(GetGlucoseDevice(adapter.get())); |
263 | 334 |
264 return adapter.Pass(); | 335 return adapter.Pass(); |
265 } | 336 } |
266 | 337 |
267 // Adds a device to |adapter| and notifies all observers about that new device. | 338 // Adds a device to |adapter| and notifies all observers about that new device. |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); | 777 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); |
707 } | 778 } |
708 | 779 |
709 // static | 780 // static |
710 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 781 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
711 return BluetoothDevice::CanonicalizeAddress( | 782 return BluetoothDevice::CanonicalizeAddress( |
712 base::StringPrintf("%012" PRIx64, addr)); | 783 base::StringPrintf("%012" PRIx64, addr)); |
713 } | 784 } |
714 | 785 |
715 } // namespace content | 786 } // namespace content |
OLD | NEW |