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

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

Issue 2829853006: bluetooth: Match real ReadRemoteCharacteristic's behavior (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 observer.GattServicesDiscovered(adapter, device); 157 observer.GattServicesDiscovered(adapter, device);
158 } 158 }
159 159
160 // Notifies the adapter's observers that a device has changed. 160 // Notifies the adapter's observers that a device has changed.
161 void NotifyDeviceChanged(MockBluetoothAdapter* adapter, 161 void NotifyDeviceChanged(MockBluetoothAdapter* adapter,
162 MockBluetoothDevice* device) { 162 MockBluetoothDevice* device) {
163 for (auto& observer : adapter->GetObservers()) 163 for (auto& observer : adapter->GetObservers())
164 observer.DeviceChanged(adapter, device); 164 observer.DeviceChanged(adapter, device);
165 } 165 }
166 166
167 void PerformCharacteristicReadValue(
168 MockBluetoothAdapter* adapter,
169 MockBluetoothGattCharacteristic* characteristic,
170 const BluetoothRemoteGattCharacteristic::ValueCallback& callback,
171 const std::vector<uint8_t>& value) {
172 for (auto& observer : adapter->GetObservers()) {
173 observer.GattCharacteristicValueChanged(adapter, characteristic, value);
174 }
175 callback.Run(value);
176 }
177
178 void PerformDescriptorReadValue(
179 MockBluetoothAdapter* adapter,
180 MockBluetoothGattDescriptor* descriptor,
181 const BluetoothRemoteGattDescriptor::ValueCallback& callback,
182 const std::vector<uint8_t>& value) {
183 for (auto& observer : adapter->GetObservers()) {
184 observer.GattDescriptorValueChanged(adapter, descriptor, value);
185 }
186 callback.Run(value);
187 }
188
189 } // namespace 167 } // namespace
190 168
191 namespace content { 169 namespace content {
192 170
193 // static 171 // static
194 scoped_refptr<BluetoothAdapter> 172 scoped_refptr<BluetoothAdapter>
195 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( 173 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter(
196 const std::string& fake_adapter_name) { 174 const std::string& fake_adapter_name) {
197 if (fake_adapter_name == "BaseAdapter") 175 if (fake_adapter_name == "BaseAdapter")
198 return GetBaseAdapter(); 176 return GetBaseAdapter();
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 auto measurement_interval(GetBaseGATTCharacteristic( 695 auto measurement_interval(GetBaseGATTCharacteristic(
718 "Measurement Interval", health_thermometer.get(), 696 "Measurement Interval", health_thermometer.get(),
719 kMeasurementIntervalUUID, 697 kMeasurementIntervalUUID,
720 BluetoothRemoteGattCharacteristic::PROPERTY_READ | 698 BluetoothRemoteGattCharacteristic::PROPERTY_READ |
721 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE | 699 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE |
722 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY)); 700 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY));
723 NiceMockBluetoothGattCharacteristic* measurement_ptr = 701 NiceMockBluetoothGattCharacteristic* measurement_ptr =
724 measurement_interval.get(); 702 measurement_interval.get();
725 703
726 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _)) 704 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _))
727 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 705 .WillByDefault(
728 [adapter_ptr, measurement_ptr]() { 706 RunCallback<0 /* success_callback */>(std::vector<uint8_t>({1})));
729 std::vector<uint8_t> interval({1});
730 for (auto& observer : adapter_ptr->GetObservers()) {
731 observer.GattCharacteristicValueChanged(
732 adapter_ptr, measurement_ptr, interval);
733 }
734 return interval;
735 }));
736 707
737 ON_CALL(*measurement_interval, WriteRemoteCharacteristic(_, _, _)) 708 ON_CALL(*measurement_interval, WriteRemoteCharacteristic(_, _, _))
738 .WillByDefault(RunCallback<1 /* success_callback */>()); 709 .WillByDefault(RunCallback<1 /* success_callback */>());
739 710
740 ON_CALL(*measurement_interval, StartNotifySession(_, _)) 711 ON_CALL(*measurement_interval, StartNotifySession(_, _))
741 .WillByDefault( 712 .WillByDefault(
742 RunCallbackWithResult<0 /* success_callback */>([measurement_ptr]() { 713 RunCallbackWithResult<0 /* success_callback */>([measurement_ptr]() {
743 return GetBaseGATTNotifySession(measurement_ptr->GetWeakPtr()); 714 return GetBaseGATTNotifySession(measurement_ptr->GetWeakPtr());
744 })); 715 }));
745 716
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 measurement_interval.get(); 1012 measurement_interval.get();
1042 1013
1043 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _)) 1014 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _))
1044 .WillByDefault(Invoke([adapter_ptr, device_ptr, measurement_ptr, 1015 .WillByDefault(Invoke([adapter_ptr, device_ptr, measurement_ptr,
1045 disconnect, succeeds]( 1016 disconnect, succeeds](
1046 const BluetoothRemoteGattCharacteristic::ValueCallback& callback, 1017 const BluetoothRemoteGattCharacteristic::ValueCallback& callback,
1047 const BluetoothRemoteGattCharacteristic::ErrorCallback& 1018 const BluetoothRemoteGattCharacteristic::ErrorCallback&
1048 error_callback) { 1019 error_callback) {
1049 base::Closure pending; 1020 base::Closure pending;
1050 if (succeeds) { 1021 if (succeeds) {
1051 pending = base::Bind(&PerformCharacteristicReadValue, 1022 pending = base::Bind(callback, std::vector<uint8_t>({1}));
1052 base::RetainedRef(adapter_ptr), measurement_ptr,
1053 callback, std::vector<uint8_t>({1}));
1054 } else { 1023 } else {
1055 pending = base::Bind(error_callback, 1024 pending = base::Bind(error_callback,
1056 BluetoothRemoteGattService::GATT_ERROR_FAILED); 1025 BluetoothRemoteGattService::GATT_ERROR_FAILED);
1057 } 1026 }
1058 device_ptr->PushPendingCallback(pending); 1027 device_ptr->PushPendingCallback(pending);
1059 if (disconnect) { 1028 if (disconnect) {
1060 device_ptr->SetConnected(false); 1029 device_ptr->SetConnected(false);
1061 base::ThreadTaskRunnerHandle::Get()->PostTask( 1030 base::ThreadTaskRunnerHandle::Get()->PostTask(
1062 FROM_HERE, 1031 FROM_HERE,
1063 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr), 1032 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ); 1087 device::BluetoothRemoteGattCharacteristic::PROPERTY_READ);
1119 1088
1120 NiceMockBluetoothGattDescriptor* user_descriptor_ptr = user_descriptor.get(); 1089 NiceMockBluetoothGattDescriptor* user_descriptor_ptr = user_descriptor.get();
1121 ON_CALL(*user_descriptor, ReadRemoteDescriptor(_, _)) 1090 ON_CALL(*user_descriptor, ReadRemoteDescriptor(_, _))
1122 .WillByDefault(Invoke([adapter_ptr, device_ptr, user_descriptor_ptr, 1091 .WillByDefault(Invoke([adapter_ptr, device_ptr, user_descriptor_ptr,
1123 disconnect, succeeds]( 1092 disconnect, succeeds](
1124 const BluetoothRemoteGattDescriptor::ValueCallback& callback, 1093 const BluetoothRemoteGattDescriptor::ValueCallback& callback,
1125 const BluetoothRemoteGattDescriptor::ErrorCallback& error_callback) { 1094 const BluetoothRemoteGattDescriptor::ErrorCallback& error_callback) {
1126 base::Closure pending; 1095 base::Closure pending;
1127 if (succeeds) { 1096 if (succeeds) {
1128 pending = base::Bind( 1097 pending = base::Bind(callback, std::vector<uint8_t>({1}));
1129 &PerformDescriptorReadValue, base::RetainedRef(adapter_ptr),
1130 user_descriptor_ptr, callback, std::vector<uint8_t>({1}));
1131 } else { 1098 } else {
1132 pending = base::Bind(error_callback, 1099 pending = base::Bind(error_callback,
1133 BluetoothRemoteGattService::GATT_ERROR_FAILED); 1100 BluetoothRemoteGattService::GATT_ERROR_FAILED);
1134 } 1101 }
1135 device_ptr->PushPendingCallback(pending); 1102 device_ptr->PushPendingCallback(pending);
1136 if (disconnect) { 1103 if (disconnect) {
1137 device_ptr->SetConnected(false); 1104 device_ptr->SetConnected(false);
1138 base::ThreadTaskRunnerHandle::Get()->PostTask( 1105 base::ThreadTaskRunnerHandle::Get()->PostTask(
1139 FROM_HERE, 1106 FROM_HERE,
1140 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr), 1107 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr),
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 rate); 1573 rate);
1607 1574
1608 return notify_session; 1575 return notify_session;
1609 })); 1576 }));
1610 1577
1611 // Body Sensor Location Characteristic (Chest) 1578 // Body Sensor Location Characteristic (Chest)
1612 std::unique_ptr<NiceMockBluetoothGattCharacteristic> 1579 std::unique_ptr<NiceMockBluetoothGattCharacteristic>
1613 body_sensor_location_chest(GetBaseGATTCharacteristic( 1580 body_sensor_location_chest(GetBaseGATTCharacteristic(
1614 "Body Sensor Location Chest", heart_rate.get(), kBodySensorLocation, 1581 "Body Sensor Location Chest", heart_rate.get(), kBodySensorLocation,
1615 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 1582 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
1616 BluetoothRemoteGattCharacteristic* location_chest_ptr =
1617 body_sensor_location_chest.get();
1618 1583
1619 ON_CALL(*body_sensor_location_chest, ReadRemoteCharacteristic(_, _)) 1584 ON_CALL(*body_sensor_location_chest, ReadRemoteCharacteristic(_, _))
1620 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 1585 .WillByDefault(RunCallback<0 /* success_callback */>(
1621 [adapter, location_chest_ptr]() { 1586 std::vector<uint8_t>({1} /* Chest */)));
1622 std::vector<uint8_t> location(1 /* size */);
1623 location[0] = 1; // Chest
1624 // Read a characteristic has a side effect of
1625 // GattCharacteristicValueChanged being called.
1626 for (auto& observer : adapter->GetObservers()) {
1627 observer.GattCharacteristicValueChanged(
1628 adapter, location_chest_ptr, location);
1629 }
1630 return location;
1631 }));
1632 1587
1633 // Body Sensor Location Characteristic (Wrist) 1588 // Body Sensor Location Characteristic (Wrist)
1634 std::unique_ptr<NiceMockBluetoothGattCharacteristic> 1589 std::unique_ptr<NiceMockBluetoothGattCharacteristic>
1635 body_sensor_location_wrist(GetBaseGATTCharacteristic( 1590 body_sensor_location_wrist(GetBaseGATTCharacteristic(
1636 "Body Sensor Location Wrist", heart_rate.get(), kBodySensorLocation, 1591 "Body Sensor Location Wrist", heart_rate.get(), kBodySensorLocation,
1637 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); 1592 BluetoothRemoteGattCharacteristic::PROPERTY_READ));
1638 BluetoothRemoteGattCharacteristic* location_wrist_ptr =
1639 body_sensor_location_wrist.get();
1640 1593
1641 ON_CALL(*body_sensor_location_wrist, ReadRemoteCharacteristic(_, _)) 1594 ON_CALL(*body_sensor_location_wrist, ReadRemoteCharacteristic(_, _))
1642 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 1595 .WillByDefault(RunCallback<0 /* success_callback */>(
1643 [adapter, location_wrist_ptr]() { 1596 std::vector<uint8_t>({2} /* Wrist */)));
1644 std::vector<uint8_t> location(1 /* size */);
1645 location[0] = 2; // Wrist
1646 // Read a characteristic has a side effect of
1647 // GattCharacteristicValueChanged being called.
1648 for (auto& observer : adapter->GetObservers()) {
1649 observer.GattCharacteristicValueChanged(
1650 adapter, location_wrist_ptr, location);
1651 }
1652 return location;
1653 }));
1654 1597
1655 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement)); 1598 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement));
1656 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_chest)); 1599 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_chest));
1657 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_wrist)); 1600 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_wrist));
1658 1601
1659 return heart_rate; 1602 return heart_rate;
1660 } 1603 }
1661 1604
1662 // static 1605 // static
1663 std::unique_ptr<NiceMockBluetoothGattService> 1606 std::unique_ptr<NiceMockBluetoothGattService>
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1796 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); 1739 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias);
1797 } 1740 }
1798 1741
1799 // static 1742 // static
1800 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { 1743 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) {
1801 return BluetoothDevice::CanonicalizeAddress( 1744 return BluetoothDevice::CanonicalizeAddress(
1802 base::StringPrintf("%012" PRIx64, addr)); 1745 base::StringPrintf("%012" PRIx64, addr));
1803 } 1746 }
1804 1747
1805 } // namespace content 1748 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698