| 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/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "device/bluetooth/bluetooth_adapter.h" | 9 #include "device/bluetooth/bluetooth_adapter.h" |
| 10 #include "device/bluetooth/bluetooth_device.h" | 10 #include "device/bluetooth/bluetooth_device.h" |
| 11 #include "device/bluetooth/bluetooth_discovery_session.h" | 11 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 12 #include "device/bluetooth/bluetooth_uuid.h" | 12 #include "device/bluetooth/bluetooth_uuid.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 13 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 14 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 14 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 15 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 15 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 16 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 | 18 |
| 18 using device::BluetoothAdapter; | 19 using device::BluetoothAdapter; |
| 19 using device::BluetoothDevice; | 20 using device::BluetoothDevice; |
| 21 using device::BluetoothGattCharacteristic; |
| 20 using device::BluetoothGattService; | 22 using device::BluetoothGattService; |
| 21 using device::BluetoothUUID; | 23 using device::BluetoothUUID; |
| 22 using device::MockBluetoothAdapter; | 24 using device::MockBluetoothAdapter; |
| 23 using device::MockBluetoothDevice; | 25 using device::MockBluetoothDevice; |
| 24 using device::MockBluetoothDiscoverySession; | 26 using device::MockBluetoothDiscoverySession; |
| 25 using device::MockBluetoothGattCharacteristic; | 27 using device::MockBluetoothGattCharacteristic; |
| 26 using device::MockBluetoothGattConnection; | 28 using device::MockBluetoothGattConnection; |
| 29 using device::MockBluetoothGattNotifySession; |
| 27 using device::MockBluetoothGattService; | 30 using device::MockBluetoothGattService; |
| 28 using testing::ElementsAre; | 31 using testing::ElementsAre; |
| 29 using testing::Invoke; | 32 using testing::Invoke; |
| 30 using testing::ResultOf; | 33 using testing::ResultOf; |
| 31 using testing::Return; | 34 using testing::Return; |
| 32 using testing::_; | 35 using testing::_; |
| 33 | 36 |
| 34 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter; | 37 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter; |
| 35 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice; | 38 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice; |
| 36 typedef testing::NiceMock<MockBluetoothDiscoverySession> | 39 typedef testing::NiceMock<MockBluetoothDiscoverySession> |
| 37 NiceMockBluetoothDiscoverySession; | 40 NiceMockBluetoothDiscoverySession; |
| 38 typedef testing::NiceMock<MockBluetoothGattCharacteristic> | 41 typedef testing::NiceMock<MockBluetoothGattCharacteristic> |
| 39 NiceMockBluetoothGattCharacteristic; | 42 NiceMockBluetoothGattCharacteristic; |
| 40 typedef testing::NiceMock<MockBluetoothGattConnection> | 43 typedef testing::NiceMock<MockBluetoothGattConnection> |
| 41 NiceMockBluetoothGattConnection; | 44 NiceMockBluetoothGattConnection; |
| 42 typedef testing::NiceMock<MockBluetoothGattService> | 45 typedef testing::NiceMock<MockBluetoothGattService> |
| 43 NiceMockBluetoothGattService; | 46 NiceMockBluetoothGattService; |
| 47 typedef testing::NiceMock<MockBluetoothGattNotifySession> |
| 48 NiceMockBluetoothGattNotifySession; |
| 44 | 49 |
| 45 namespace { | 50 namespace { |
| 46 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). | 51 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). |
| 47 const char kBatteryServiceUUID[] = "180f"; | 52 const char kBatteryServiceUUID[] = "180f"; |
| 48 const char kGenericAccessServiceUUID[] = "1800"; | 53 const char kGenericAccessServiceUUID[] = "1800"; |
| 49 const char kGlucoseServiceUUID[] = "1808"; | 54 const char kGlucoseServiceUUID[] = "1808"; |
| 50 const char kHeartRateServiceUUID[] = "180d"; | 55 const char kHeartRateServiceUUID[] = "180d"; |
| 56 const char kHeartRateMeasurementUUID[] = "2a37"; |
| 51 const char kDeviceNameUUID[] = "2a00"; | 57 const char kDeviceNameUUID[] = "2a00"; |
| 52 | 58 |
| 53 // Invokes Run() on the k-th argument of the function with no arguments. | 59 // Invokes Run() on the k-th argument of the function with no arguments. |
| 54 ACTION_TEMPLATE(RunCallback, | 60 ACTION_TEMPLATE(RunCallback, |
| 55 HAS_1_TEMPLATE_PARAMS(int, k), | 61 HAS_1_TEMPLATE_PARAMS(int, k), |
| 56 AND_0_VALUE_PARAMS()) { | 62 AND_0_VALUE_PARAMS()) { |
| 57 return ::testing::get<k>(args).Run(); | 63 return ::testing::get<k>(args).Run(); |
| 58 } | 64 } |
| 59 | 65 |
| 60 // Invokes Run() on the k-th argument of the function with 1 argument. | 66 // Invokes Run() on the k-th argument of the function with 1 argument. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 else if (fake_adapter_name == "FailStartDiscoveryAdapter") | 117 else if (fake_adapter_name == "FailStartDiscoveryAdapter") |
| 112 return GetFailStartDiscoveryAdapter(); | 118 return GetFailStartDiscoveryAdapter(); |
| 113 else if (fake_adapter_name == "GlucoseHeartRateAdapter") | 119 else if (fake_adapter_name == "GlucoseHeartRateAdapter") |
| 114 return GetGlucoseHeartRateAdapter(); | 120 return GetGlucoseHeartRateAdapter(); |
| 115 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter") | 121 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter") |
| 116 return GetMissingServiceGenericAccessAdapter(); | 122 return GetMissingServiceGenericAccessAdapter(); |
| 117 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter") | 123 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter") |
| 118 return GetMissingCharacteristicGenericAccessAdapter(); | 124 return GetMissingCharacteristicGenericAccessAdapter(); |
| 119 else if (fake_adapter_name == "GenericAccessAdapter") | 125 else if (fake_adapter_name == "GenericAccessAdapter") |
| 120 return GetGenericAccessAdapter(); | 126 return GetGenericAccessAdapter(); |
| 127 else if (fake_adapter_name == "HeartRateAdapter") |
| 128 return GetHeartRateAdapter(); |
| 121 else if (fake_adapter_name == "FailingConnectionsAdapter") | 129 else if (fake_adapter_name == "FailingConnectionsAdapter") |
| 122 return GetFailingConnectionsAdapter(); | 130 return GetFailingConnectionsAdapter(); |
| 123 else if (fake_adapter_name == "FailingGATTOperationsAdapter") | 131 else if (fake_adapter_name == "FailingGATTOperationsAdapter") |
| 124 return GetFailingGATTOperationsAdapter(); | 132 return GetFailingGATTOperationsAdapter(); |
| 125 else if (fake_adapter_name == "") | 133 else if (fake_adapter_name == "") |
| 126 return NULL; | 134 return NULL; |
| 127 | 135 |
| 128 NOTREACHED() << fake_adapter_name; | 136 NOTREACHED() << fake_adapter_name; |
| 129 return NULL; | 137 return NULL; |
| 130 } | 138 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 | 314 |
| 307 generic_access->AddMockCharacteristic(device_name.Pass()); | 315 generic_access->AddMockCharacteristic(device_name.Pass()); |
| 308 device->AddMockService(generic_access.Pass()); | 316 device->AddMockService(generic_access.Pass()); |
| 309 adapter->AddMockDevice(device.Pass()); | 317 adapter->AddMockDevice(device.Pass()); |
| 310 | 318 |
| 311 return adapter.Pass(); | 319 return adapter.Pass(); |
| 312 } | 320 } |
| 313 | 321 |
| 314 // static | 322 // static |
| 315 scoped_refptr<NiceMockBluetoothAdapter> | 323 scoped_refptr<NiceMockBluetoothAdapter> |
| 324 LayoutTestBluetoothAdapterProvider::GetHeartRateAdapter() { |
| 325 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 326 |
| 327 scoped_ptr<NiceMockBluetoothDevice> device(GetHeartRateDevice(adapter.get())); |
| 328 |
| 329 scoped_ptr<NiceMockBluetoothGattService> heart_rate( |
| 330 GetBaseGATTService(device.get(), kHeartRateServiceUUID)); |
| 331 |
| 332 // TODO(ortuno): Implement the rest of the service's characteristics |
| 333 // See: http://crbug.com/529975 |
| 334 |
| 335 scoped_ptr<NiceMockBluetoothGattCharacteristic> heart_rate_measurement( |
| 336 GetBaseGATTCharacteristic(heart_rate.get(), kHeartRateMeasurementUUID)); |
| 337 |
| 338 BluetoothGattCharacteristic* measurement_ptr = heart_rate_measurement.get(); |
| 339 |
| 340 ON_CALL(*heart_rate_measurement, StartNotifySession(_, _)) |
| 341 .WillByDefault( |
| 342 RunCallbackWithResult<0 /* success_callback */>([measurement_ptr]() { |
| 343 return GetBaseGATTNotifySession(measurement_ptr->GetIdentifier()); |
| 344 })); |
| 345 |
| 346 heart_rate->AddMockCharacteristic(heart_rate_measurement.Pass()); |
| 347 device->AddMockService(heart_rate.Pass()); |
| 348 adapter->AddMockDevice(device.Pass()); |
| 349 |
| 350 return adapter.Pass(); |
| 351 } |
| 352 |
| 353 // static |
| 354 scoped_ptr<NiceMockBluetoothGattNotifySession> |
| 355 LayoutTestBluetoothAdapterProvider::GetBaseGATTNotifySession( |
| 356 const std::string& characteristic_identifier) { |
| 357 scoped_ptr<NiceMockBluetoothGattNotifySession> session( |
| 358 new NiceMockBluetoothGattNotifySession(characteristic_identifier)); |
| 359 |
| 360 ON_CALL(*session, Stop(_)).WillByDefault(RunCallback<0>()); |
| 361 |
| 362 return session.Pass(); |
| 363 } |
| 364 |
| 365 // static |
| 366 scoped_refptr<NiceMockBluetoothAdapter> |
| 316 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() { | 367 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() { |
| 317 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 368 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 318 | 369 |
| 319 for (int error = BluetoothDevice::ERROR_UNKNOWN; | 370 for (int error = BluetoothDevice::ERROR_UNKNOWN; |
| 320 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) { | 371 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) { |
| 321 adapter->AddMockDevice(GetUnconnectableDevice( | 372 adapter->AddMockDevice(GetUnconnectableDevice( |
| 322 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error))); | 373 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error))); |
| 323 } | 374 } |
| 324 return adapter.Pass(); | 375 return adapter.Pass(); |
| 325 } | 376 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 } | 479 } |
| 429 | 480 |
| 430 // static | 481 // static |
| 431 scoped_ptr<NiceMockBluetoothDevice> | 482 scoped_ptr<NiceMockBluetoothDevice> |
| 432 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice( | 483 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice( |
| 433 MockBluetoothAdapter* adapter) { | 484 MockBluetoothAdapter* adapter) { |
| 434 BluetoothDevice::UUIDList uuids; | 485 BluetoothDevice::UUIDList uuids; |
| 435 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); | 486 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 436 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); | 487 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
| 437 | 488 |
| 438 return GetBaseDevice(adapter, "Heart Rate Device", uuids, | 489 return GetConnectableDevice(adapter, "Heart Rate Device", uuids, |
| 439 makeMACAddress(0x3)); | 490 makeMACAddress(0x3)); |
| 440 } | 491 } |
| 441 | 492 |
| 442 // static | 493 // static |
| 443 scoped_ptr<NiceMockBluetoothDevice> | 494 scoped_ptr<NiceMockBluetoothDevice> |
| 444 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( | 495 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( |
| 445 device::MockBluetoothAdapter* adapter, | 496 device::MockBluetoothAdapter* adapter, |
| 446 const std::string& device_name, | 497 const std::string& device_name, |
| 447 BluetoothDevice::UUIDList uuids) { | 498 BluetoothDevice::UUIDList uuids, |
| 499 const std::string& address) { |
| 448 scoped_ptr<NiceMockBluetoothDevice> device( | 500 scoped_ptr<NiceMockBluetoothDevice> device( |
| 449 GetBaseDevice(adapter, device_name, uuids)); | 501 GetBaseDevice(adapter, device_name, uuids, address)); |
| 450 | 502 |
| 451 BluetoothDevice* device_ptr = device.get(); | 503 BluetoothDevice* device_ptr = device.get(); |
| 452 | 504 |
| 453 ON_CALL(*device, CreateGattConnection(_, _)) | 505 ON_CALL(*device, CreateGattConnection(_, _)) |
| 454 .WillByDefault( | 506 .WillByDefault( |
| 455 RunCallbackWithResult<0 /* success_callback */>([device_ptr]() { | 507 RunCallbackWithResult<0 /* success_callback */>([device_ptr]() { |
| 456 return make_scoped_ptr( | 508 return make_scoped_ptr( |
| 457 new NiceMockBluetoothGattConnection(device_ptr->GetAddress())); | 509 new NiceMockBluetoothGattConnection(device_ptr->GetAddress())); |
| 458 })); | 510 })); |
| 459 | 511 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 GetBaseGATTCharacteristic(service, errorUUID(error_alias))); | 586 GetBaseGATTCharacteristic(service, errorUUID(error_alias))); |
| 535 | 587 |
| 536 // Read response. | 588 // Read response. |
| 537 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) | 589 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) |
| 538 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); | 590 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); |
| 539 | 591 |
| 540 // Write response. | 592 // Write response. |
| 541 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) | 593 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) |
| 542 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); | 594 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); |
| 543 | 595 |
| 596 // StartNotifySession response |
| 597 ON_CALL(*characteristic, StartNotifySession(_, _)) |
| 598 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); |
| 599 |
| 544 return characteristic.Pass(); | 600 return characteristic.Pass(); |
| 545 } | 601 } |
| 546 | 602 |
| 547 // Helper functions | 603 // Helper functions |
| 548 | 604 |
| 549 // static | 605 // static |
| 550 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) { | 606 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) { |
| 551 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); | 607 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); |
| 552 } | 608 } |
| 553 | 609 |
| 554 // static | 610 // static |
| 555 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 611 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
| 556 return BluetoothDevice::CanonicalizeAddress( | 612 return BluetoothDevice::CanonicalizeAddress( |
| 557 base::StringPrintf("%012" PRIx64, addr)); | 613 base::StringPrintf("%012" PRIx64, addr)); |
| 558 } | 614 } |
| 559 | 615 |
| 560 } // namespace content | 616 } // namespace content |
| OLD | NEW |