| 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 "device/bluetooth/bluetooth_adapter.h" | 7 #include "device/bluetooth/bluetooth_adapter.h" |
| 8 #include "device/bluetooth/bluetooth_device.h" | 8 #include "device/bluetooth/bluetooth_device.h" |
| 9 #include "device/bluetooth/bluetooth_discovery_session.h" | 9 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 10 #include "device/bluetooth/bluetooth_uuid.h" | 10 #include "device/bluetooth/bluetooth_uuid.h" |
| 11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 12 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 12 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | 13 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 | 15 |
| 16 using device::BluetoothAdapter; | 16 using device::BluetoothAdapter; |
| 17 using device::BluetoothAdapterFactory; | 17 using device::BluetoothAdapterFactory; |
| 18 using device::BluetoothDevice; | 18 using device::BluetoothDevice; |
| 19 using device::BluetoothDiscoverySession; | 19 using device::BluetoothDiscoverySession; |
| 20 using device::BluetoothGattConnection; | 20 using device::BluetoothGattConnection; |
| 21 using device::BluetoothGattService; | 21 using device::BluetoothGattService; |
| 22 using device::BluetoothUUID; | 22 using device::BluetoothUUID; |
| 23 using device::MockBluetoothAdapter; | 23 using device::MockBluetoothAdapter; |
| 24 using device::MockBluetoothDevice; | 24 using device::MockBluetoothDevice; |
| 25 using device::MockBluetoothDiscoverySession; | 25 using device::MockBluetoothDiscoverySession; |
| 26 using device::MockBluetoothGattCharacteristic; | 26 using device::MockBluetoothGattCharacteristic; |
| 27 using device::MockBluetoothGattConnection; | 27 using device::MockBluetoothGattConnection; |
| 28 using device::MockBluetoothGattService; | 28 using device::MockBluetoothGattService; |
| 29 using testing::Between; | 29 using testing::Between; |
| 30 using testing::ElementsAre; |
| 30 using testing::Invoke; | 31 using testing::Invoke; |
| 32 using testing::NiceMock; |
| 33 using testing::ResultOf; |
| 31 using testing::Return; | 34 using testing::Return; |
| 32 using testing::NiceMock; | 35 using testing::WithArgs; |
| 33 using testing::_; | 36 using testing::_; |
| 34 | 37 |
| 35 namespace { | 38 namespace { |
| 39 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). |
| 40 const char kBatteryServiceUUID[] = "180f"; |
| 41 const char kGenericAccessServiceUUID[] = "1800"; |
| 42 const char kGenericAttributeServiceUUID[] = "1801"; |
| 43 const char kGlucoseServiceUUID[] = "1808"; |
| 44 const char kHeartRateServiceUUID[] = "180d"; |
| 45 |
| 36 // Invokes Run() on the k-th argument of the function with no arguments. | 46 // Invokes Run() on the k-th argument of the function with no arguments. |
| 37 ACTION_TEMPLATE(RunCallback, | 47 ACTION_TEMPLATE(RunCallback, |
| 38 HAS_1_TEMPLATE_PARAMS(int, k), | 48 HAS_1_TEMPLATE_PARAMS(int, k), |
| 39 AND_0_VALUE_PARAMS()) { | 49 AND_0_VALUE_PARAMS()) { |
| 40 return ::testing::get<k>(args).Run(); | 50 return ::testing::get<k>(args).Run(); |
| 41 } | 51 } |
| 42 | 52 |
| 43 // Invokes Run() on the k-th argument of the function with 1 argument. | 53 // Invokes Run() on the k-th argument of the function with 1 argument. |
| 44 ACTION_TEMPLATE(RunCallback, | 54 ACTION_TEMPLATE(RunCallback, |
| 45 HAS_1_TEMPLATE_PARAMS(int, k), | 55 HAS_1_TEMPLATE_PARAMS(int, k), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 59 // that matches the address. | 69 // that matches the address. |
| 60 ACTION_P(GetMockDevice, adapter) { | 70 ACTION_P(GetMockDevice, adapter) { |
| 61 std::string address = arg0; | 71 std::string address = arg0; |
| 62 for (BluetoothDevice* device : adapter->GetMockDevices()) { | 72 for (BluetoothDevice* device : adapter->GetMockDevices()) { |
| 63 if (device->GetAddress() == address) | 73 if (device->GetAddress() == address) |
| 64 return device; | 74 return device; |
| 65 } | 75 } |
| 66 return NULL; | 76 return NULL; |
| 67 } | 77 } |
| 68 | 78 |
| 79 std::set<BluetoothUUID> GetUUIDs( |
| 80 const device::BluetoothDiscoveryFilter* filter) { |
| 81 std::set<BluetoothUUID> result; |
| 82 filter->GetUUIDs(result); |
| 83 return result; |
| 84 }; |
| 85 |
| 69 } // namespace | 86 } // namespace |
| 70 | 87 |
| 71 namespace content { | 88 namespace content { |
| 72 | 89 |
| 73 // static | 90 // static |
| 74 scoped_refptr<BluetoothAdapter> | 91 scoped_refptr<BluetoothAdapter> |
| 75 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 92 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
| 76 const std::string& fake_adapter_name) { | 93 const std::string& fake_adapter_name) { |
| 77 if (fake_adapter_name == "EmptyAdapter") | 94 if (fake_adapter_name == "EmptyAdapter") |
| 78 return GetEmptyAdapter(); | 95 return GetEmptyAdapter(); |
| 79 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") | 96 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") |
| 80 return GetSingleEmptyDeviceAdapter(); | 97 return GetSingleEmptyDeviceAdapter(); |
| 81 else if (fake_adapter_name == "ConnectableDeviceAdapter") | 98 else if (fake_adapter_name == "ConnectableDeviceAdapter") |
| 82 return GetConnectableDeviceAdapter(); | 99 return GetConnectableDeviceAdapter(); |
| 83 else if (fake_adapter_name == "UnconnectableDeviceAdapter") | 100 else if (fake_adapter_name == "UnconnectableDeviceAdapter") |
| 84 return GetUnconnectableDeviceAdapter(); | 101 return GetUnconnectableDeviceAdapter(); |
| 102 else if (fake_adapter_name == "ScanFilterCheckingAdapter") |
| 103 return GetScanFilterCheckingAdapter(); |
| 104 else if (fake_adapter_name == "MultiDeviceAdapter") |
| 105 return GetMultiDeviceAdapter(); |
| 85 else if (fake_adapter_name == "") | 106 else if (fake_adapter_name == "") |
| 86 return NULL; | 107 return NULL; |
| 87 | 108 |
| 88 NOTREACHED(); | 109 NOTREACHED() << fake_adapter_name; |
| 89 return NULL; | 110 return NULL; |
| 90 } | 111 } |
| 91 | 112 |
| 92 // static | 113 // static |
| 93 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 114 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 94 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 115 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
| 95 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 116 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 96 new NiceMock<MockBluetoothAdapter>()); | 117 new NiceMock<MockBluetoothAdapter>()); |
| 97 | 118 |
| 98 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 119 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 99 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 120 .WillByDefault(RunCallbackWithResult<1 /* success_callback */>( |
| 100 []() { return GetDiscoverySession(); })); | 121 []() { return GetDiscoverySession(); })); |
| 101 | 122 |
| 102 // Using Invoke allows the adapter returned from this method to be futher | 123 // Using Invoke allows the adapter returned from this method to be futher |
| 103 // modified and have devices added to it. The call to ::GetDevices will | 124 // modified and have devices added to it. The call to ::GetDevices will |
| 104 // invoke ::GetConstMockDevices, returning all devices added up to that time. | 125 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
| 105 ON_CALL(*adapter, GetDevices()) | 126 ON_CALL(*adapter, GetDevices()) |
| 106 .WillByDefault( | 127 .WillByDefault( |
| 107 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); | 128 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
| 108 | 129 |
| 109 // The call to ::GetDevice will invoke GetMockDevice which returns a device | 130 // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| 110 // matching the address provided if the device was added to the mock. | 131 // matching the address provided if the device was added to the mock. |
| 111 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); | 132 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
| 112 | 133 |
| 113 return adapter.Pass(); | 134 return adapter.Pass(); |
| 114 } | 135 } |
| 115 | 136 |
| 116 // static | 137 // static |
| 117 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 138 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 139 LayoutTestBluetoothAdapterProvider::GetScanFilterCheckingAdapter() { |
| 140 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 141 new NiceMock<MockBluetoothAdapter>()); |
| 142 |
| 143 // This fails the test with an error message listing actual and expected UUIDs |
| 144 // if StartDiscoverySessionWithFilter() is called with the wrong argument. |
| 145 EXPECT_CALL( |
| 146 *adapter, |
| 147 StartDiscoverySessionWithFilterRaw( |
| 148 ResultOf(&GetUUIDs, ElementsAre(BluetoothUUID(kGlucoseServiceUUID), |
| 149 BluetoothUUID(kHeartRateServiceUUID), |
| 150 BluetoothUUID(kBatteryServiceUUID))), |
| 151 _, _)) |
| 152 .WillRepeatedly(RunCallbackWithResult<1 /* success_callback */>( |
| 153 []() { return GetDiscoverySession(); })); |
| 154 |
| 155 // Any unexpected call results in the failure callback. |
| 156 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 157 .WillByDefault(RunCallback<2 /* failure_callback */>()); |
| 158 |
| 159 scoped_ptr<NiceMock<MockBluetoothDevice>> battery_device = |
| 160 GetEmptyDevice(adapter.get(), "Battery Device"); |
| 161 BluetoothDevice::UUIDList battery_uuid_list; |
| 162 battery_uuid_list.push_back(BluetoothUUID(kBatteryServiceUUID)); |
| 163 ON_CALL(*battery_device, GetUUIDs()).WillByDefault(Return(battery_uuid_list)); |
| 164 adapter->AddMockDevice(battery_device.Pass()); |
| 165 |
| 166 // This adapter isn't modified further, so we just return a hard-coded list. |
| 167 ON_CALL(*adapter, GetDevices()) |
| 168 .WillByDefault(Return(adapter->GetConstMockDevices())); |
| 169 |
| 170 return adapter.Pass(); |
| 171 } |
| 172 |
| 173 // static |
| 174 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 118 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 175 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
| 119 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 176 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 120 | 177 |
| 121 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 178 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
| 122 | 179 |
| 123 return adapter.Pass(); | 180 return adapter.Pass(); |
| 124 } | 181 } |
| 125 | 182 |
| 126 // static | 183 // static |
| 127 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 184 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 185 LayoutTestBluetoothAdapterProvider::GetMultiDeviceAdapter() { |
| 186 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 187 |
| 188 scoped_ptr<NiceMock<MockBluetoothDevice>> heart_rate_device = |
| 189 GetEmptyDevice(adapter.get(), "Heart Rate Device"); |
| 190 BluetoothDevice::UUIDList heart_rate_uuid_list; |
| 191 heart_rate_uuid_list.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
| 192 heart_rate_uuid_list.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 193 heart_rate_uuid_list.push_back(BluetoothUUID(kGenericAttributeServiceUUID)); |
| 194 ON_CALL(*heart_rate_device, GetUUIDs()) |
| 195 .WillByDefault(Return(heart_rate_uuid_list)); |
| 196 adapter->AddMockDevice(heart_rate_device.Pass()); |
| 197 |
| 198 scoped_ptr<NiceMock<MockBluetoothDevice>> glucose_device = |
| 199 GetEmptyDevice(adapter.get(), "Glucose Device"); |
| 200 BluetoothDevice::UUIDList glucose_uuid_list; |
| 201 glucose_uuid_list.push_back(BluetoothUUID(kGlucoseServiceUUID)); |
| 202 glucose_uuid_list.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 203 glucose_uuid_list.push_back(BluetoothUUID(kGenericAttributeServiceUUID)); |
| 204 ON_CALL(*glucose_device, GetUUIDs()).WillByDefault(Return(glucose_uuid_list)); |
| 205 adapter->AddMockDevice(glucose_device.Pass()); |
| 206 |
| 207 return adapter.Pass(); |
| 208 } |
| 209 |
| 210 // static |
| 211 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 128 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { | 212 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { |
| 129 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 213 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 130 | 214 |
| 131 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); | 215 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); |
| 132 | 216 |
| 133 return adapter.Pass(); | 217 return adapter.Pass(); |
| 134 } | 218 } |
| 135 | 219 |
| 136 // static | 220 // static |
| 137 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 221 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 151 | 235 |
| 152 ON_CALL(*discovery_session, Stop(_, _)) | 236 ON_CALL(*discovery_session, Stop(_, _)) |
| 153 .WillByDefault(RunCallback<0 /* success_callback */>()); | 237 .WillByDefault(RunCallback<0 /* success_callback */>()); |
| 154 | 238 |
| 155 return discovery_session.Pass(); | 239 return discovery_session.Pass(); |
| 156 } | 240 } |
| 157 | 241 |
| 158 // static | 242 // static |
| 159 scoped_ptr<NiceMock<MockBluetoothDevice>> | 243 scoped_ptr<NiceMock<MockBluetoothDevice>> |
| 160 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 244 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| 161 MockBluetoothAdapter* adapter) { | 245 MockBluetoothAdapter* adapter, |
| 246 const std::string& device_name) { |
| 162 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 247 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
| 163 new NiceMock<MockBluetoothDevice>( | 248 new NiceMock<MockBluetoothDevice>( |
| 164 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 249 adapter, 0x1F00 /* Bluetooth Class */, device_name, |
| 165 "Empty Mock Device instanceID", true /* Paired */, | 250 device_name + " instanceID", true /* Paired */, |
| 166 true /* Connected */)); | 251 true /* Connected */)); |
| 167 | 252 |
| 168 ON_CALL(*empty_device, GetVendorIDSource()) | 253 ON_CALL(*empty_device, GetVendorIDSource()) |
| 169 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 254 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
| 170 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 255 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
| 171 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 256 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
| 172 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 257 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
| 173 | 258 |
| 174 BluetoothDevice::UUIDList list; | 259 BluetoothDevice::UUIDList list; |
| 175 list.push_back(BluetoothUUID("1800")); | 260 list.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 176 list.push_back(BluetoothUUID("1801")); | 261 list.push_back(BluetoothUUID(kGenericAttributeServiceUUID)); |
| 177 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 262 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
| 178 | 263 |
| 179 scoped_ptr<NiceMock<MockBluetoothGattService>> generic_access( | 264 scoped_ptr<NiceMock<MockBluetoothGattService>> generic_access( |
| 180 GetGattService(empty_device.get(), "1800" /* Generic Access */)); | 265 GetGattService(empty_device.get(), kGenericAccessServiceUUID)); |
| 181 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> device_name( | 266 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> |
| 182 GetGattCharacteristic(generic_access.get(), "2A00" /* Device Name */)); | 267 device_name_characteristic(GetGattCharacteristic( |
| 268 generic_access.get(), "2A00" /* Device Name */)); |
| 183 | 269 |
| 184 std::string value_str("Empty Mock Device name"); | 270 std::vector<uint8_t> device_name_value(device_name.begin(), |
| 185 std::vector<uint8_t> value(value_str.begin(), value_str.end()); | 271 device_name.end()); |
| 186 ON_CALL(*device_name, ReadRemoteCharacteristic(_, _)) | 272 ON_CALL(*device_name_characteristic, ReadRemoteCharacteristic(_, _)) |
| 187 .WillByDefault(RunCallback<0>(value)); | 273 .WillByDefault(RunCallback<0>(device_name_value)); |
| 188 | 274 |
| 189 generic_access->AddMockCharacteristic(device_name.Pass()); | 275 generic_access->AddMockCharacteristic(device_name_characteristic.Pass()); |
| 190 | 276 |
| 191 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> reconnection_address( | 277 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> reconnection_address( |
| 192 GetGattCharacteristic(generic_access.get(), | 278 GetGattCharacteristic(generic_access.get(), |
| 193 "2A03" /* Reconnection Address */)); | 279 "2A03" /* Reconnection Address */)); |
| 194 | 280 |
| 195 ON_CALL(*reconnection_address, ReadRemoteCharacteristic(_, _)) | 281 ON_CALL(*reconnection_address, ReadRemoteCharacteristic(_, _)) |
| 196 .WillByDefault( | 282 .WillByDefault( |
| 197 RunCallback<1>(BluetoothGattService::GATT_ERROR_NOT_PERMITTED)); | 283 RunCallback<1>(BluetoothGattService::GATT_ERROR_NOT_PERMITTED)); |
| 198 | 284 |
| 199 generic_access->AddMockCharacteristic(reconnection_address.Pass()); | 285 generic_access->AddMockCharacteristic(reconnection_address.Pass()); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> | 359 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> |
| 274 LayoutTestBluetoothAdapterProvider::GetGattCharacteristic( | 360 LayoutTestBluetoothAdapterProvider::GetGattCharacteristic( |
| 275 MockBluetoothGattService* service, | 361 MockBluetoothGattService* service, |
| 276 const std::string& uuid) { | 362 const std::string& uuid) { |
| 277 return make_scoped_ptr(new NiceMock<MockBluetoothGattCharacteristic>( | 363 return make_scoped_ptr(new NiceMock<MockBluetoothGattCharacteristic>( |
| 278 service, uuid /* identifier */, BluetoothUUID(uuid), false /* is_local */, | 364 service, uuid /* identifier */, BluetoothUUID(uuid), false /* is_local */, |
| 279 NULL /* properties */, NULL /* permissions */)); | 365 NULL /* properties */, NULL /* permissions */)); |
| 280 } | 366 } |
| 281 | 367 |
| 282 } // namespace content | 368 } // namespace content |
| OLD | NEW |