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/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "device/bluetooth/bluetooth_adapter.h" | 13 #include "device/bluetooth/bluetooth_adapter.h" |
14 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" |
15 #include "device/bluetooth/bluetooth_discovery_session.h" | 15 #include "device/bluetooth/bluetooth_discovery_session.h" |
16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
18 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 18 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
19 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 19 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
20 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
21 | 22 |
22 using device::BluetoothAdapter; | 23 using device::BluetoothAdapter; |
23 using device::BluetoothDevice; | 24 using device::BluetoothDevice; |
25 using device::BluetoothGattCharacteristic; | |
24 using device::BluetoothGattService; | 26 using device::BluetoothGattService; |
25 using device::BluetoothUUID; | 27 using device::BluetoothUUID; |
26 using device::MockBluetoothAdapter; | 28 using device::MockBluetoothAdapter; |
27 using device::MockBluetoothDevice; | 29 using device::MockBluetoothDevice; |
28 using device::MockBluetoothDiscoverySession; | 30 using device::MockBluetoothDiscoverySession; |
29 using device::MockBluetoothGattCharacteristic; | 31 using device::MockBluetoothGattCharacteristic; |
30 using device::MockBluetoothGattConnection; | 32 using device::MockBluetoothGattConnection; |
33 using device::MockBluetoothGattNotifySession; | |
31 using device::MockBluetoothGattService; | 34 using device::MockBluetoothGattService; |
32 using testing::ElementsAre; | 35 using testing::ElementsAre; |
33 using testing::Invoke; | 36 using testing::Invoke; |
34 using testing::ResultOf; | 37 using testing::ResultOf; |
35 using testing::Return; | 38 using testing::Return; |
36 using testing::_; | 39 using testing::_; |
37 | 40 |
38 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter; | 41 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter; |
39 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice; | 42 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice; |
40 typedef testing::NiceMock<MockBluetoothDiscoverySession> | 43 typedef testing::NiceMock<MockBluetoothDiscoverySession> |
41 NiceMockBluetoothDiscoverySession; | 44 NiceMockBluetoothDiscoverySession; |
42 typedef testing::NiceMock<MockBluetoothGattCharacteristic> | 45 typedef testing::NiceMock<MockBluetoothGattCharacteristic> |
43 NiceMockBluetoothGattCharacteristic; | 46 NiceMockBluetoothGattCharacteristic; |
44 typedef testing::NiceMock<MockBluetoothGattConnection> | 47 typedef testing::NiceMock<MockBluetoothGattConnection> |
45 NiceMockBluetoothGattConnection; | 48 NiceMockBluetoothGattConnection; |
46 typedef testing::NiceMock<MockBluetoothGattService> | 49 typedef testing::NiceMock<MockBluetoothGattService> |
47 NiceMockBluetoothGattService; | 50 NiceMockBluetoothGattService; |
51 typedef testing::NiceMock<MockBluetoothGattNotifySession> | |
52 NiceMockBluetoothGattNotifySession; | |
48 | 53 |
49 namespace { | 54 namespace { |
50 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). | 55 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). |
51 const char kBatteryServiceUUID[] = "180f"; | 56 const char kBatteryServiceUUID[] = "180f"; |
52 const char kGenericAccessServiceUUID[] = "1800"; | 57 const char kGenericAccessServiceUUID[] = "1800"; |
53 const char kGlucoseServiceUUID[] = "1808"; | 58 const char kGlucoseServiceUUID[] = "1808"; |
54 const char kHeartRateServiceUUID[] = "180d"; | 59 const char kHeartRateServiceUUID[] = "180d"; |
60 const char kHeartRateMeasurementUUID[] = "2a37"; | |
55 const char kDeviceNameUUID[] = "2a00"; | 61 const char kDeviceNameUUID[] = "2a00"; |
56 | 62 |
57 // Invokes Run() on the k-th argument of the function with no arguments. | 63 // Invokes Run() on the k-th argument of the function with no arguments. |
58 ACTION_TEMPLATE(RunCallback, | 64 ACTION_TEMPLATE(RunCallback, |
59 HAS_1_TEMPLATE_PARAMS(int, k), | 65 HAS_1_TEMPLATE_PARAMS(int, k), |
60 AND_0_VALUE_PARAMS()) { | 66 AND_0_VALUE_PARAMS()) { |
61 return ::testing::get<k>(args).Run(); | 67 return ::testing::get<k>(args).Run(); |
62 } | 68 } |
63 | 69 |
64 // Invokes Run() on the k-th argument of the function with 1 argument. | 70 // 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... | |
115 else if (fake_adapter_name == "FailStartDiscoveryAdapter") | 121 else if (fake_adapter_name == "FailStartDiscoveryAdapter") |
116 return GetFailStartDiscoveryAdapter(); | 122 return GetFailStartDiscoveryAdapter(); |
117 else if (fake_adapter_name == "GlucoseHeartRateAdapter") | 123 else if (fake_adapter_name == "GlucoseHeartRateAdapter") |
118 return GetGlucoseHeartRateAdapter(); | 124 return GetGlucoseHeartRateAdapter(); |
119 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter") | 125 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter") |
120 return GetMissingServiceGenericAccessAdapter(); | 126 return GetMissingServiceGenericAccessAdapter(); |
121 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter") | 127 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter") |
122 return GetMissingCharacteristicGenericAccessAdapter(); | 128 return GetMissingCharacteristicGenericAccessAdapter(); |
123 else if (fake_adapter_name == "GenericAccessAdapter") | 129 else if (fake_adapter_name == "GenericAccessAdapter") |
124 return GetGenericAccessAdapter(); | 130 return GetGenericAccessAdapter(); |
131 else if (fake_adapter_name == "HeartRateAdapter") | |
132 return GetHeartRateAdapter(); | |
125 else if (fake_adapter_name == "FailingConnectionsAdapter") | 133 else if (fake_adapter_name == "FailingConnectionsAdapter") |
126 return GetFailingConnectionsAdapter(); | 134 return GetFailingConnectionsAdapter(); |
127 else if (fake_adapter_name == "FailingGATTOperationsAdapter") | 135 else if (fake_adapter_name == "FailingGATTOperationsAdapter") |
128 return GetFailingGATTOperationsAdapter(); | 136 return GetFailingGATTOperationsAdapter(); |
129 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") | 137 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") |
130 return GetSecondDiscoveryFindsHeartRateAdapter(); | 138 return GetSecondDiscoveryFindsHeartRateAdapter(); |
131 else if (fake_adapter_name == "") | 139 else if (fake_adapter_name == "") |
132 return NULL; | 140 return NULL; |
133 | 141 |
134 NOTREACHED() << fake_adapter_name; | 142 NOTREACHED() << fake_adapter_name; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 | 354 |
347 generic_access->AddMockCharacteristic(device_name.Pass()); | 355 generic_access->AddMockCharacteristic(device_name.Pass()); |
348 device->AddMockService(generic_access.Pass()); | 356 device->AddMockService(generic_access.Pass()); |
349 adapter->AddMockDevice(device.Pass()); | 357 adapter->AddMockDevice(device.Pass()); |
350 | 358 |
351 return adapter.Pass(); | 359 return adapter.Pass(); |
352 } | 360 } |
353 | 361 |
354 // static | 362 // static |
355 scoped_refptr<NiceMockBluetoothAdapter> | 363 scoped_refptr<NiceMockBluetoothAdapter> |
364 LayoutTestBluetoothAdapterProvider::GetHeartRateAdapter() { | |
365 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | |
366 scoped_ptr<NiceMockBluetoothDevice> device(GetHeartRateDevice(adapter.get())); | |
367 scoped_ptr<NiceMockBluetoothGattService> heart_rate( | |
368 GetBaseGATTService(device.get(), kHeartRateServiceUUID)); | |
369 | |
370 // TODO(ortuno): Implement the rest of the service's characteristics | |
371 // See: http://crbug.com/529975 | |
372 scoped_ptr<NiceMockBluetoothGattCharacteristic> heart_rate_measurement( | |
373 GetBaseGATTCharacteristic(heart_rate.get(), kHeartRateMeasurementUUID)); | |
374 BluetoothGattCharacteristic* measurement_ptr = heart_rate_measurement.get(); | |
375 ON_CALL(*heart_rate_measurement, StartNotifySession(_, _)) | |
376 .WillByDefault( | |
377 RunCallbackWithResult<0 /* success_callback */>([measurement_ptr]() { | |
378 return GetBaseGATTNotifySession(measurement_ptr->GetIdentifier()); | |
379 })); | |
380 | |
381 heart_rate->AddMockCharacteristic(heart_rate_measurement.Pass()); | |
382 device->AddMockService(heart_rate.Pass()); | |
383 adapter->AddMockDevice(device.Pass()); | |
384 | |
385 return adapter.Pass(); | |
386 } | |
387 | |
388 // static | |
389 scoped_ptr<NiceMockBluetoothGattNotifySession> | |
390 LayoutTestBluetoothAdapterProvider::GetBaseGATTNotifySession( | |
scheib
2015/10/01 22:05:23
After ::GetBaseGATTCharacteristic, http://google-s
ortuno
2015/10/03 04:03:04
Moved it after GetErrorCharacteristic since GetBas
| |
391 const std::string& characteristic_identifier) { | |
392 scoped_ptr<NiceMockBluetoothGattNotifySession> session( | |
393 new NiceMockBluetoothGattNotifySession(characteristic_identifier)); | |
394 | |
395 ON_CALL(*session, Stop(_)).WillByDefault(RunCallback<0>()); | |
396 | |
397 return session.Pass(); | |
398 } | |
399 | |
400 // static | |
401 scoped_refptr<NiceMockBluetoothAdapter> | |
356 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() { | 402 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() { |
357 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 403 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
358 | 404 |
359 for (int error = BluetoothDevice::ERROR_UNKNOWN; | 405 for (int error = BluetoothDevice::ERROR_UNKNOWN; |
360 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) { | 406 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) { |
361 adapter->AddMockDevice(GetUnconnectableDevice( | 407 adapter->AddMockDevice(GetUnconnectableDevice( |
362 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error))); | 408 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error))); |
363 } | 409 } |
410 | |
364 return adapter.Pass(); | 411 return adapter.Pass(); |
365 } | 412 } |
366 | 413 |
367 // static | 414 // static |
368 scoped_refptr<NiceMockBluetoothAdapter> | 415 scoped_refptr<NiceMockBluetoothAdapter> |
369 LayoutTestBluetoothAdapterProvider::GetFailingGATTOperationsAdapter() { | 416 LayoutTestBluetoothAdapterProvider::GetFailingGATTOperationsAdapter() { |
370 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 417 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
371 | 418 |
372 const std::string errorsServiceUUID = errorUUID(0xA0); | 419 const std::string errorsServiceUUID = errorUUID(0xA0); |
373 | 420 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 } | 515 } |
469 | 516 |
470 // static | 517 // static |
471 scoped_ptr<NiceMockBluetoothDevice> | 518 scoped_ptr<NiceMockBluetoothDevice> |
472 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice( | 519 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice( |
473 MockBluetoothAdapter* adapter) { | 520 MockBluetoothAdapter* adapter) { |
474 BluetoothDevice::UUIDList uuids; | 521 BluetoothDevice::UUIDList uuids; |
475 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); | 522 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
476 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); | 523 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
477 | 524 |
478 return GetBaseDevice(adapter, "Heart Rate Device", uuids, | 525 return GetConnectableDevice(adapter, "Heart Rate Device", uuids, |
479 makeMACAddress(0x3)); | 526 makeMACAddress(0x3)); |
480 } | 527 } |
481 | 528 |
482 // static | 529 // static |
483 scoped_ptr<NiceMockBluetoothDevice> | 530 scoped_ptr<NiceMockBluetoothDevice> |
484 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( | 531 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( |
485 device::MockBluetoothAdapter* adapter, | 532 device::MockBluetoothAdapter* adapter, |
486 const std::string& device_name, | 533 const std::string& device_name, |
487 BluetoothDevice::UUIDList uuids) { | 534 BluetoothDevice::UUIDList uuids, |
535 const std::string& address) { | |
488 scoped_ptr<NiceMockBluetoothDevice> device( | 536 scoped_ptr<NiceMockBluetoothDevice> device( |
489 GetBaseDevice(adapter, device_name, uuids)); | 537 GetBaseDevice(adapter, device_name, uuids, address)); |
490 | 538 |
491 BluetoothDevice* device_ptr = device.get(); | 539 BluetoothDevice* device_ptr = device.get(); |
492 | 540 |
493 ON_CALL(*device, CreateGattConnection(_, _)) | 541 ON_CALL(*device, CreateGattConnection(_, _)) |
494 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 542 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
495 [adapter, device_ptr]() { | 543 [adapter, device_ptr]() { |
496 return make_scoped_ptr(new NiceMockBluetoothGattConnection( | 544 return make_scoped_ptr(new NiceMockBluetoothGattConnection( |
497 adapter, device_ptr->GetAddress())); | 545 adapter, device_ptr->GetAddress())); |
498 })); | 546 })); |
499 | 547 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 GetBaseGATTCharacteristic(service, errorUUID(error_alias))); | 622 GetBaseGATTCharacteristic(service, errorUUID(error_alias))); |
575 | 623 |
576 // Read response. | 624 // Read response. |
577 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) | 625 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) |
578 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); | 626 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); |
579 | 627 |
580 // Write response. | 628 // Write response. |
581 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) | 629 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) |
582 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); | 630 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); |
583 | 631 |
632 // StartNotifySession response | |
633 ON_CALL(*characteristic, StartNotifySession(_, _)) | |
634 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); | |
635 | |
584 return characteristic.Pass(); | 636 return characteristic.Pass(); |
585 } | 637 } |
586 | 638 |
587 // Helper functions | 639 // Helper functions |
588 | 640 |
589 // static | 641 // static |
590 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) { | 642 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) { |
591 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); | 643 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); |
592 } | 644 } |
593 | 645 |
594 // static | 646 // static |
595 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 647 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
596 return BluetoothDevice::CanonicalizeAddress( | 648 return BluetoothDevice::CanonicalizeAddress( |
597 base::StringPrintf("%012" PRIx64, addr)); | 649 base::StringPrintf("%012" PRIx64, addr)); |
598 } | 650 } |
599 | 651 |
600 } // namespace content | 652 } // namespace content |
OLD | NEW |