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 <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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 return result; | 123 return result; |
124 } | 124 } |
125 | 125 |
126 // Notifies the adapter's observers that the services have been discovered. | 126 // Notifies the adapter's observers that the services have been discovered. |
127 void NotifyServicesDiscovered(MockBluetoothAdapter* adapter, | 127 void NotifyServicesDiscovered(MockBluetoothAdapter* adapter, |
128 MockBluetoothDevice* device) { | 128 MockBluetoothDevice* device) { |
129 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, adapter->GetObservers(), | 129 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, adapter->GetObservers(), |
130 GattServicesDiscovered(adapter, device)); | 130 GattServicesDiscovered(adapter, device)); |
131 } | 131 } |
132 | 132 |
| 133 // Notifies the adapter's observers that a device has changed. |
| 134 void NotifyDeviceChanged(MockBluetoothAdapter* adapter, |
| 135 MockBluetoothDevice* device) { |
| 136 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, adapter->GetObservers(), |
| 137 DeviceChanged(adapter, device)); |
| 138 } |
| 139 |
133 } // namespace | 140 } // namespace |
134 | 141 |
135 namespace content { | 142 namespace content { |
136 | 143 |
137 // static | 144 // static |
138 scoped_refptr<BluetoothAdapter> | 145 scoped_refptr<BluetoothAdapter> |
139 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 146 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
140 const std::string& fake_adapter_name) { | 147 const std::string& fake_adapter_name) { |
141 if (fake_adapter_name == "BaseAdapter") | 148 if (fake_adapter_name == "BaseAdapter") |
142 return GetBaseAdapter(); | 149 return GetBaseAdapter(); |
(...skipping 14 matching lines...) Expand all Loading... |
157 if (fake_adapter_name == "MissingServiceHeartRateAdapter") | 164 if (fake_adapter_name == "MissingServiceHeartRateAdapter") |
158 return GetMissingServiceHeartRateAdapter(); | 165 return GetMissingServiceHeartRateAdapter(); |
159 if (fake_adapter_name == "MissingCharacteristicHeartRateAdapter") | 166 if (fake_adapter_name == "MissingCharacteristicHeartRateAdapter") |
160 return GetMissingCharacteristicHeartRateAdapter(); | 167 return GetMissingCharacteristicHeartRateAdapter(); |
161 if (fake_adapter_name == "HeartRateAdapter") | 168 if (fake_adapter_name == "HeartRateAdapter") |
162 return GetHeartRateAdapter(); | 169 return GetHeartRateAdapter(); |
163 if (fake_adapter_name == "TwoHeartRateServicesAdapter") | 170 if (fake_adapter_name == "TwoHeartRateServicesAdapter") |
164 return GetTwoHeartRateServicesAdapter(); | 171 return GetTwoHeartRateServicesAdapter(); |
165 if (fake_adapter_name == "DisconnectingHeartRateAdapter") | 172 if (fake_adapter_name == "DisconnectingHeartRateAdapter") |
166 return GetDisconnectingHeartRateAdapter(); | 173 return GetDisconnectingHeartRateAdapter(); |
| 174 if (fake_adapter_name == "DisconnectingDuringServiceRetrievalAdapter") |
| 175 return GetServicesDiscoveredAfterReconnectionAdapter(true /* disconnect */); |
| 176 if (fake_adapter_name == "ServicesDiscoveredAfterReconnectionAdapter") |
| 177 return GetServicesDiscoveredAfterReconnectionAdapter( |
| 178 false /* disconnect */); |
167 if (fake_adapter_name == "BlacklistTestAdapter") | 179 if (fake_adapter_name == "BlacklistTestAdapter") |
168 return GetBlacklistTestAdapter(); | 180 return GetBlacklistTestAdapter(); |
169 if (fake_adapter_name == "FailingConnectionsAdapter") | 181 if (fake_adapter_name == "FailingConnectionsAdapter") |
170 return GetFailingConnectionsAdapter(); | 182 return GetFailingConnectionsAdapter(); |
171 if (fake_adapter_name == "FailingGATTOperationsAdapter") | 183 if (fake_adapter_name == "FailingGATTOperationsAdapter") |
172 return GetFailingGATTOperationsAdapter(); | 184 return GetFailingGATTOperationsAdapter(); |
173 if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") | 185 if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") |
174 return GetSecondDiscoveryFindsHeartRateAdapter(); | 186 return GetSecondDiscoveryFindsHeartRateAdapter(); |
175 if (fake_adapter_name == "DelayedServicesDiscoveryAdapter") | 187 if (fake_adapter_name == "DelayedServicesDiscoveryAdapter") |
176 return GetDelayedServicesDiscoveryAdapter(); | 188 return GetDelayedServicesDiscoveryAdapter(); |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 | 542 |
531 disconnection_service->AddMockCharacteristic( | 543 disconnection_service->AddMockCharacteristic( |
532 std::move(disconnection_characteristic)); | 544 std::move(disconnection_characteristic)); |
533 device->AddMockService(std::move(disconnection_service)); | 545 device->AddMockService(std::move(disconnection_service)); |
534 adapter->AddMockDevice(std::move(device)); | 546 adapter->AddMockDevice(std::move(device)); |
535 | 547 |
536 return adapter; | 548 return adapter; |
537 } | 549 } |
538 | 550 |
539 // static | 551 // static |
| 552 scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: |
| 553 GetServicesDiscoveredAfterReconnectionAdapter(bool disconnect) { |
| 554 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 555 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); |
| 556 std::unique_ptr<NiceMockBluetoothDevice> device( |
| 557 GetHeartRateDevice(adapter.get())); |
| 558 NiceMockBluetoothDevice* device_ptr = device.get(); |
| 559 |
| 560 // When called before IsGattDiscoveryComplete, run success callback with a new |
| 561 // Gatt connection. When called after after IsGattDiscoveryComplete runs |
| 562 // success callback with a new Gatt connection and notifies of services |
| 563 // discovered. |
| 564 ON_CALL(*device, CreateGattConnection(_, _)) |
| 565 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
| 566 [adapter_ptr, device_ptr]() { |
| 567 std::vector<BluetoothRemoteGattService*> services = |
| 568 device_ptr->GetMockServices(); |
| 569 |
| 570 if (services.size() != 0) { |
| 571 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 572 FROM_HERE, |
| 573 base::Bind(&NotifyServicesDiscovered, |
| 574 base::RetainedRef(adapter_ptr), device_ptr)); |
| 575 } |
| 576 |
| 577 return base::MakeUnique<NiceMockBluetoothGattConnection>( |
| 578 adapter_ptr, device_ptr->GetAddress()); |
| 579 })); |
| 580 |
| 581 // The first time this function is called we: |
| 582 // 1. Add a service (This indicates that this function has been called) |
| 583 // 2. If |disconnect| is true, disconnect the device. |
| 584 // 3. Return false. |
| 585 // The second time this function is called we just return true. |
| 586 ON_CALL(*device, IsGattServicesDiscoveryComplete()) |
| 587 .WillByDefault(Invoke([adapter_ptr, device_ptr, disconnect] { |
| 588 std::vector<BluetoothRemoteGattService*> services = |
| 589 device_ptr->GetMockServices(); |
| 590 if (services.size() == 0) { |
| 591 std::unique_ptr<NiceMockBluetoothGattService> heart_rate( |
| 592 GetBaseGATTService("Heart Rate", device_ptr, |
| 593 kHeartRateServiceUUID)); |
| 594 |
| 595 device_ptr->AddMockService(GetGenericAccessService(device_ptr)); |
| 596 device_ptr->AddMockService( |
| 597 GetHeartRateService(adapter_ptr, device_ptr)); |
| 598 |
| 599 if (disconnect) { |
| 600 device_ptr->SetConnected(false); |
| 601 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 602 FROM_HERE, |
| 603 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr), |
| 604 device_ptr)); |
| 605 } |
| 606 DCHECK(services.size() == 0); |
| 607 return false; |
| 608 } |
| 609 |
| 610 return true; |
| 611 })); |
| 612 adapter->AddMockDevice(std::move(device)); |
| 613 |
| 614 return adapter; |
| 615 } |
| 616 |
| 617 // static |
540 scoped_refptr<NiceMockBluetoothAdapter> | 618 scoped_refptr<NiceMockBluetoothAdapter> |
541 LayoutTestBluetoothAdapterProvider::GetBlacklistTestAdapter() { | 619 LayoutTestBluetoothAdapterProvider::GetBlacklistTestAdapter() { |
542 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 620 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
543 | 621 |
544 BluetoothDevice::UUIDList uuids; | 622 BluetoothDevice::UUIDList uuids; |
545 uuids.push_back(BluetoothUUID(kBlacklistTestServiceUUID)); | 623 uuids.push_back(BluetoothUUID(kBlacklistTestServiceUUID)); |
546 uuids.push_back(BluetoothUUID(kDeviceInformationServiceUUID)); | 624 uuids.push_back(BluetoothUUID(kDeviceInformationServiceUUID)); |
547 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); | 625 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
548 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); | 626 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
549 uuids.push_back(BluetoothUUID(kHumanInterfaceDeviceServiceUUID)); | 627 uuids.push_back(BluetoothUUID(kHumanInterfaceDeviceServiceUUID)); |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1074 return BluetoothUUID(); | 1152 return BluetoothUUID(); |
1075 } | 1153 } |
1076 | 1154 |
1077 // static | 1155 // static |
1078 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 1156 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
1079 return BluetoothDevice::CanonicalizeAddress( | 1157 return BluetoothDevice::CanonicalizeAddress( |
1080 base::StringPrintf("%012" PRIx64, addr)); | 1158 base::StringPrintf("%012" PRIx64, addr)); |
1081 } | 1159 } |
1082 | 1160 |
1083 } // namespace content | 1161 } // namespace content |
OLD | NEW |