Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc

Issue 1723723002: bluetooth: Implement Service.getCharacteristics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Address jyasskin's comments and add moar tests Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 } 710 }
711 711
712 // static 712 // static
713 scoped_ptr<NiceMockBluetoothGattService> 713 scoped_ptr<NiceMockBluetoothGattService>
714 LayoutTestBluetoothAdapterProvider::GetDeviceInformationService( 714 LayoutTestBluetoothAdapterProvider::GetDeviceInformationService(
715 device::MockBluetoothDevice* device) { 715 device::MockBluetoothDevice* device) {
716 scoped_ptr<NiceMockBluetoothGattService> device_information( 716 scoped_ptr<NiceMockBluetoothGattService> device_information(
717 GetBaseGATTService(device, kDeviceInformationServiceUUID)); 717 GetBaseGATTService(device, kDeviceInformationServiceUUID));
718 718
719 scoped_ptr<NiceMockBluetoothGattCharacteristic> serial_number_string( 719 scoped_ptr<NiceMockBluetoothGattCharacteristic> serial_number_string(
720 GetBaseGATTCharacteristic(device_information.get(), 720 GetBaseGATTCharacteristic(
721 kSerialNumberStringUUID, 721 "Serial Number String", device_information.get(),
722 BluetoothGattCharacteristic::PROPERTY_READ)); 722 kSerialNumberStringUUID, BluetoothGattCharacteristic::PROPERTY_READ));
723 723
724 // Crash if ReadRemoteCharacteristic called. Not using GoogleMock's Expect 724 // Crash if ReadRemoteCharacteristic called. Not using GoogleMock's Expect
725 // because this is used in layout tests that may not report a mock expectation 725 // because this is used in layout tests that may not report a mock expectation
726 // error correctly as a layout test failure. 726 // error correctly as a layout test failure.
727 ON_CALL(*serial_number_string, ReadRemoteCharacteristic(_, _)) 727 ON_CALL(*serial_number_string, ReadRemoteCharacteristic(_, _))
728 .WillByDefault( 728 .WillByDefault(
729 Invoke([](const BluetoothGattCharacteristic::ValueCallback&, 729 Invoke([](const BluetoothGattCharacteristic::ValueCallback&,
730 const BluetoothGattCharacteristic::ErrorCallback&) { 730 const BluetoothGattCharacteristic::ErrorCallback&) {
731 NOTREACHED(); 731 NOTREACHED();
732 })); 732 }));
733 733
734 device_information->AddMockCharacteristic(std::move(serial_number_string)); 734 device_information->AddMockCharacteristic(std::move(serial_number_string));
735 735
736 return device_information; 736 return device_information;
737 } 737 }
738 738
739 // static 739 // static
740 scoped_ptr<NiceMockBluetoothGattService> 740 scoped_ptr<NiceMockBluetoothGattService>
741 LayoutTestBluetoothAdapterProvider::GetGenericAccessService( 741 LayoutTestBluetoothAdapterProvider::GetGenericAccessService(
742 device::MockBluetoothDevice* device) { 742 device::MockBluetoothDevice* device) {
743 scoped_ptr<NiceMockBluetoothGattService> generic_access( 743 scoped_ptr<NiceMockBluetoothGattService> generic_access(
744 GetBaseGATTService(device, kGenericAccessServiceUUID)); 744 GetBaseGATTService(device, kGenericAccessServiceUUID));
745 745
746 scoped_ptr<NiceMockBluetoothGattCharacteristic> device_name( 746 scoped_ptr<NiceMockBluetoothGattCharacteristic> device_name(
747 GetBaseGATTCharacteristic( 747 GetBaseGATTCharacteristic(
748 generic_access.get(), kDeviceNameUUID, 748 "Device Name", generic_access.get(), kDeviceNameUUID,
749 BluetoothGattCharacteristic::PROPERTY_READ | 749 BluetoothGattCharacteristic::PROPERTY_READ |
750 BluetoothGattCharacteristic::PROPERTY_WRITE)); 750 BluetoothGattCharacteristic::PROPERTY_WRITE));
751 751
752 // Read response. 752 // Read response.
753 std::string device_name_str = device->GetDeviceName(); 753 std::string device_name_str = device->GetDeviceName();
754 std::vector<uint8_t> device_name_value(device_name_str.begin(), 754 std::vector<uint8_t> device_name_value(device_name_str.begin(),
755 device_name_str.end()); 755 device_name_str.end());
756 756
757 ON_CALL(*device_name, ReadRemoteCharacteristic(_, _)) 757 ON_CALL(*device_name, ReadRemoteCharacteristic(_, _))
758 .WillByDefault(RunCallback<0>(device_name_value)); 758 .WillByDefault(RunCallback<0>(device_name_value));
(...skipping 10 matching lines...) Expand all
769 // static 769 // static
770 scoped_ptr<NiceMockBluetoothGattService> 770 scoped_ptr<NiceMockBluetoothGattService>
771 LayoutTestBluetoothAdapterProvider::GetHeartRateService( 771 LayoutTestBluetoothAdapterProvider::GetHeartRateService(
772 MockBluetoothAdapter* adapter, 772 MockBluetoothAdapter* adapter,
773 MockBluetoothDevice* device) { 773 MockBluetoothDevice* device) {
774 scoped_ptr<NiceMockBluetoothGattService> heart_rate( 774 scoped_ptr<NiceMockBluetoothGattService> heart_rate(
775 GetBaseGATTService(device, kHeartRateServiceUUID)); 775 GetBaseGATTService(device, kHeartRateServiceUUID));
776 776
777 // Heart Rate Measurement 777 // Heart Rate Measurement
778 scoped_ptr<NiceMockBluetoothGattCharacteristic> heart_rate_measurement( 778 scoped_ptr<NiceMockBluetoothGattCharacteristic> heart_rate_measurement(
779 GetBaseGATTCharacteristic(heart_rate.get(), kHeartRateMeasurementUUID, 779 GetBaseGATTCharacteristic("Heart Rate Measurement", heart_rate.get(),
780 kHeartRateMeasurementUUID,
780 BluetoothGattCharacteristic::PROPERTY_NOTIFY)); 781 BluetoothGattCharacteristic::PROPERTY_NOTIFY));
781 NiceMockBluetoothGattCharacteristic* measurement_ptr = 782 NiceMockBluetoothGattCharacteristic* measurement_ptr =
782 heart_rate_measurement.get(); 783 heart_rate_measurement.get();
783 784
784 ON_CALL(*heart_rate_measurement, StartNotifySession(_, _)) 785 ON_CALL(*heart_rate_measurement, StartNotifySession(_, _))
785 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 786 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
786 [adapter, measurement_ptr]() { 787 [adapter, measurement_ptr]() {
787 scoped_ptr<NiceMockBluetoothGattNotifySession> notify_session( 788 scoped_ptr<NiceMockBluetoothGattNotifySession> notify_session(
788 GetBaseGATTNotifySession(measurement_ptr->GetIdentifier())); 789 GetBaseGATTNotifySession(measurement_ptr->GetIdentifier()));
789 790
790 std::vector<uint8_t> rate(1 /* size */); 791 std::vector<uint8_t> rate(1 /* size */);
791 rate[0] = 60; 792 rate[0] = 60;
792 793
793 notify_session->StartTestNotifications(adapter, measurement_ptr, 794 notify_session->StartTestNotifications(adapter, measurement_ptr,
794 rate); 795 rate);
795 796
796 return notify_session; 797 return notify_session;
797 })); 798 }));
798 799
799 // Body Sensor Location Characteristic 800 // Body Sensor Location Characteristic (Chest)
800 scoped_ptr<NiceMockBluetoothGattCharacteristic> body_sensor_location( 801 scoped_ptr<NiceMockBluetoothGattCharacteristic> body_sensor_location_chest(
801 GetBaseGATTCharacteristic(heart_rate.get(), kBodySensorLocation, 802 GetBaseGATTCharacteristic("Body Sensor Location Chest", heart_rate.get(),
803 kBodySensorLocation,
802 BluetoothGattCharacteristic::PROPERTY_READ)); 804 BluetoothGattCharacteristic::PROPERTY_READ));
803 BluetoothGattCharacteristic* location_ptr = body_sensor_location.get(); 805 BluetoothGattCharacteristic* location_chest_ptr =
806 body_sensor_location_chest.get();
804 807
805 ON_CALL(*body_sensor_location, ReadRemoteCharacteristic(_, _)) 808 ON_CALL(*body_sensor_location_chest, ReadRemoteCharacteristic(_, _))
806 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 809 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
807 [adapter, location_ptr]() { 810 [adapter, location_chest_ptr]() {
808 std::vector<uint8_t> location(1 /* size */); 811 std::vector<uint8_t> location(1 /* size */);
809 location[0] = 1; // Chest 812 location[0] = 1; // Chest
810 // Read a characteristic has a side effect of 813 // Read a characteristic has a side effect of
811 // GattCharacteristicValueChanged being called. 814 // GattCharacteristicValueChanged being called.
812 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 815 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
813 adapter->GetObservers(), 816 adapter->GetObservers(),
814 GattCharacteristicValueChanged( 817 GattCharacteristicValueChanged(
815 adapter, location_ptr, location)); 818 adapter, location_chest_ptr, location));
819 return location;
820 }));
821
822 // Body Sensor Location Characteristic (Wrist)
823 scoped_ptr<NiceMockBluetoothGattCharacteristic> body_sensor_location_wrist(
824 GetBaseGATTCharacteristic("Body Sensor Location Wrist", heart_rate.get(),
825 kBodySensorLocation,
826 BluetoothGattCharacteristic::PROPERTY_READ));
827 BluetoothGattCharacteristic* location_wrist_ptr =
828 body_sensor_location_wrist.get();
829
830 ON_CALL(*body_sensor_location_wrist, ReadRemoteCharacteristic(_, _))
831 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
832 [adapter, location_wrist_ptr]() {
833 std::vector<uint8_t> location(1 /* size */);
834 location[0] = 2; // Wrist
835 // Read a characteristic has a side effect of
836 // GattCharacteristicValueChanged being called.
837 FOR_EACH_OBSERVER(BluetoothAdapter::Observer,
838 adapter->GetObservers(),
839 GattCharacteristicValueChanged(
840 adapter, location_wrist_ptr, location));
816 return location; 841 return location;
817 })); 842 }));
818 843
819 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement)); 844 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement));
820 heart_rate->AddMockCharacteristic(std::move(body_sensor_location)); 845 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_chest));
846 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_wrist));
821 847
822 return heart_rate; 848 return heart_rate;
823 } 849 }
824 850
825 // Characteristics 851 // Characteristics
826 852
827 // static 853 // static
828 scoped_ptr<NiceMockBluetoothGattCharacteristic> 854 scoped_ptr<NiceMockBluetoothGattCharacteristic>
829 LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic( 855 LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic(
856 const std::string& identifier,
830 MockBluetoothGattService* service, 857 MockBluetoothGattService* service,
831 const std::string& uuid, 858 const std::string& uuid,
832 BluetoothGattCharacteristic::Properties properties) { 859 BluetoothGattCharacteristic::Properties properties) {
833 return make_scoped_ptr(new NiceMockBluetoothGattCharacteristic( 860 return make_scoped_ptr(new NiceMockBluetoothGattCharacteristic(
834 service, uuid + " Identifier", BluetoothUUID(uuid), false /* is_local */, 861 service, identifier, BluetoothUUID(uuid), false /* is_local */,
835 properties, NULL /* permissions */)); 862 properties, NULL /* permissions */));
836 } 863 }
837 864
838 // static 865 // static
839 scoped_ptr<NiceMockBluetoothGattCharacteristic> 866 scoped_ptr<NiceMockBluetoothGattCharacteristic>
840 LayoutTestBluetoothAdapterProvider::GetErrorCharacteristic( 867 LayoutTestBluetoothAdapterProvider::GetErrorCharacteristic(
841 MockBluetoothGattService* service, 868 MockBluetoothGattService* service,
842 BluetoothGattService::GattErrorCode error_code) { 869 BluetoothGattService::GattErrorCode error_code) {
843 uint32_t error_alias = error_code + 0xA1; // Error UUIDs start at 0xA1. 870 uint32_t error_alias = error_code + 0xA1; // Error UUIDs start at 0xA1.
844 scoped_ptr<NiceMockBluetoothGattCharacteristic> characteristic( 871 scoped_ptr<NiceMockBluetoothGattCharacteristic> characteristic(
845 GetBaseGATTCharacteristic( 872 GetBaseGATTCharacteristic(
846 service, 873 // Use the UUID to generate unique identifiers.
874 "Error Characteristic " + errorUUID(error_alias), service,
847 errorUUID(error_alias), 875 errorUUID(error_alias),
848 BluetoothGattCharacteristic::PROPERTY_READ | 876 BluetoothGattCharacteristic::PROPERTY_READ |
849 BluetoothGattCharacteristic::PROPERTY_WRITE | 877 BluetoothGattCharacteristic::PROPERTY_WRITE |
850 BluetoothGattCharacteristic::PROPERTY_INDICATE)); 878 BluetoothGattCharacteristic::PROPERTY_INDICATE));
851
852 879
853 // Read response. 880 // Read response.
854 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) 881 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _))
855 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); 882 .WillByDefault(RunCallback<1 /* error_callback */>(error_code));
856 883
857 // Write response. 884 // Write response.
858 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) 885 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _))
859 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); 886 .WillByDefault(RunCallback<2 /* error_callback */>(error_code));
860 887
861 // StartNotifySession response 888 // StartNotifySession response
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 return BluetoothUUID(); 963 return BluetoothUUID();
937 } 964 }
938 965
939 // static 966 // static
940 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { 967 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) {
941 return BluetoothDevice::CanonicalizeAddress( 968 return BluetoothDevice::CanonicalizeAddress(
942 base::StringPrintf("%012" PRIx64, addr)); 969 base::StringPrintf("%012" PRIx64, addr));
943 } 970 }
944 971
945 } // namespace content 972 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698