Chromium Code Reviews| 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 29 matching lines...) Expand all Loading... | |
| 75 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 85 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
| 76 const std::string& fake_adapter_name) { | 86 const std::string& fake_adapter_name) { |
| 77 if (fake_adapter_name == "EmptyAdapter") | 87 if (fake_adapter_name == "EmptyAdapter") |
| 78 return GetEmptyAdapter(); | 88 return GetEmptyAdapter(); |
| 79 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") | 89 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") |
| 80 return GetSingleEmptyDeviceAdapter(); | 90 return GetSingleEmptyDeviceAdapter(); |
| 81 else if (fake_adapter_name == "ConnectableDeviceAdapter") | 91 else if (fake_adapter_name == "ConnectableDeviceAdapter") |
| 82 return GetConnectableDeviceAdapter(); | 92 return GetConnectableDeviceAdapter(); |
| 83 else if (fake_adapter_name == "UnconnectableDeviceAdapter") | 93 else if (fake_adapter_name == "UnconnectableDeviceAdapter") |
| 84 return GetUnconnectableDeviceAdapter(); | 94 return GetUnconnectableDeviceAdapter(); |
| 95 else if (fake_adapter_name == "ScanFilterCheckingAdapter") | |
| 96 return GetScanFilterCheckingAdapter(); | |
| 97 else if (fake_adapter_name == "MultiDeviceAdapter") | |
| 98 return GetMultiDeviceAdapter(); | |
| 85 else if (fake_adapter_name == "") | 99 else if (fake_adapter_name == "") |
| 86 return NULL; | 100 return NULL; |
| 87 | 101 |
| 88 NOTREACHED(); | 102 NOTREACHED() << fake_adapter_name; |
| 89 return NULL; | 103 return NULL; |
| 90 } | 104 } |
| 91 | 105 |
| 92 // static | 106 // static |
| 93 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 107 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 94 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 108 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
| 95 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 109 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 96 new NiceMock<MockBluetoothAdapter>()); | 110 new NiceMock<MockBluetoothAdapter>()); |
| 97 | 111 |
| 98 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 112 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 99 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 113 .WillByDefault(RunCallbackWithResult<1 /* success_callback */>( |
| 100 []() { return GetDiscoverySession(); })); | 114 []() { return GetDiscoverySession(); })); |
| 101 | 115 |
| 102 // Using Invoke allows the adapter returned from this method to be futher | 116 // 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 | 117 // modified and have devices added to it. The call to ::GetDevices will |
| 104 // invoke ::GetConstMockDevices, returning all devices added up to that time. | 118 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
| 105 ON_CALL(*adapter, GetDevices()) | 119 ON_CALL(*adapter, GetDevices()) |
| 106 .WillByDefault( | 120 .WillByDefault( |
| 107 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); | 121 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
| 108 | 122 |
| 109 // The call to ::GetDevice will invoke GetMockDevice which returns a device | 123 // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| 110 // matching the address provided if the device was added to the mock. | 124 // matching the address provided if the device was added to the mock. |
| 111 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); | 125 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
| 112 | 126 |
| 113 return adapter.Pass(); | 127 return adapter.Pass(); |
| 114 } | 128 } |
| 115 | 129 |
| 130 static std::set<BluetoothUUID> GetUUIDs( | |
|
scheib
2015/06/15 23:47:21
I have a style preference of keeping these methods
Jeffrey Yasskin
2015/06/16 00:48:10
Ick, but done, and for the definitions of MatchesF
| |
| 131 const device::BluetoothDiscoveryFilter* filter) { | |
| 132 std::set<BluetoothUUID> result; | |
| 133 filter->GetUUIDs(result); | |
| 134 return result; | |
| 135 }; | |
| 136 | |
| 137 // static | |
| 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 // This adapter isn't further modified, so we just return a hard-coded empty | |
| 160 // list. | |
| 161 ON_CALL(*adapter, GetDevices()) | |
|
scheib
2015/06/15 23:47:21
Let's return something so that in JS the promise c
Jeffrey Yasskin
2015/06/16 00:48:10
'k.
| |
| 162 .WillByDefault(Return(adapter->GetConstMockDevices())); | |
| 163 | |
| 164 return adapter.Pass(); | |
| 165 } | |
| 166 | |
| 116 // static | 167 // static |
| 117 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 168 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 118 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 169 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
| 119 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 170 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 120 | 171 |
| 121 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 172 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
| 122 | 173 |
| 123 return adapter.Pass(); | 174 return adapter.Pass(); |
| 124 } | 175 } |
| 125 | 176 |
| 126 // static | 177 // static |
| 127 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 178 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 179 LayoutTestBluetoothAdapterProvider::GetMultiDeviceAdapter() { | |
| 180 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | |
| 181 | |
| 182 scoped_ptr<NiceMock<MockBluetoothDevice>> heart_rate_device = | |
| 183 GetEmptyDevice(adapter.get(), "Heart Rate Device"); | |
| 184 BluetoothDevice::UUIDList heart_rate_uuid_list; | |
| 185 heart_rate_uuid_list.push_back(BluetoothUUID(kHeartRateServiceUUID)); | |
| 186 ON_CALL(*heart_rate_device, GetUUIDs()) | |
| 187 .WillByDefault(Return(heart_rate_uuid_list)); | |
| 188 adapter->AddMockDevice(heart_rate_device.Pass()); | |
| 189 | |
| 190 scoped_ptr<NiceMock<MockBluetoothDevice>> glucose_device = | |
| 191 GetEmptyDevice(adapter.get(), "Glucose Device"); | |
| 192 BluetoothDevice::UUIDList glucose_uuid_list; | |
| 193 glucose_uuid_list.push_back(BluetoothUUID(kGlucoseServiceUUID)); | |
| 194 ON_CALL(*glucose_device, GetUUIDs()).WillByDefault(Return(glucose_uuid_list)); | |
| 195 adapter->AddMockDevice(glucose_device.Pass()); | |
| 196 | |
| 197 return adapter.Pass(); | |
| 198 } | |
| 199 | |
| 200 // static | |
| 201 scoped_refptr<NiceMock<MockBluetoothAdapter>> | |
| 128 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { | 202 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { |
| 129 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 203 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 130 | 204 |
| 131 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); | 205 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); |
| 132 | 206 |
| 133 return adapter.Pass(); | 207 return adapter.Pass(); |
| 134 } | 208 } |
| 135 | 209 |
| 136 // static | 210 // static |
| 137 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 211 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 151 | 225 |
| 152 ON_CALL(*discovery_session, Stop(_, _)) | 226 ON_CALL(*discovery_session, Stop(_, _)) |
| 153 .WillByDefault(RunCallback<0 /* success_callback */>()); | 227 .WillByDefault(RunCallback<0 /* success_callback */>()); |
| 154 | 228 |
| 155 return discovery_session.Pass(); | 229 return discovery_session.Pass(); |
| 156 } | 230 } |
| 157 | 231 |
| 158 // static | 232 // static |
| 159 scoped_ptr<NiceMock<MockBluetoothDevice>> | 233 scoped_ptr<NiceMock<MockBluetoothDevice>> |
| 160 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 234 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| 161 MockBluetoothAdapter* adapter) { | 235 MockBluetoothAdapter* adapter, |
| 236 const std::string& device_name) { | |
| 162 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 237 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
| 163 new NiceMock<MockBluetoothDevice>( | 238 new NiceMock<MockBluetoothDevice>( |
| 164 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 239 adapter, 0x1F00 /* Bluetooth Class */, device_name, |
| 165 "Empty Mock Device instanceID", true /* Paired */, | 240 device_name + " instanceID", true /* Paired */, |
| 166 true /* Connected */)); | 241 true /* Connected */)); |
| 167 | 242 |
| 168 ON_CALL(*empty_device, GetVendorIDSource()) | 243 ON_CALL(*empty_device, GetVendorIDSource()) |
| 169 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 244 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
| 170 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 245 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
| 171 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 246 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
| 172 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 247 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
| 173 | 248 |
| 174 BluetoothDevice::UUIDList list; | 249 BluetoothDevice::UUIDList list; |
| 175 list.push_back(BluetoothUUID("1800")); | 250 list.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 176 list.push_back(BluetoothUUID("1801")); | 251 list.push_back(BluetoothUUID(kGenericAttributeServiceUUID)); |
| 177 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 252 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
| 178 | 253 |
| 179 scoped_ptr<NiceMock<MockBluetoothGattService>> generic_access( | 254 scoped_ptr<NiceMock<MockBluetoothGattService>> generic_access( |
| 180 GetGattService(empty_device.get(), "1800" /* Generic Access */)); | 255 GetGattService(empty_device.get(), kGenericAccessServiceUUID)); |
| 181 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> device_name( | 256 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> |
| 182 GetGattCharacteristic(generic_access.get(), "2A00" /* Device Name */)); | 257 device_name_characteristic(GetGattCharacteristic( |
| 258 generic_access.get(), "2A00" /* Device Name */)); | |
| 183 | 259 |
| 184 std::string value_str("Empty Mock Device name"); | 260 std::string value_str("Empty Mock Device name"); |
|
scheib
2015/06/15 23:47:21
Use 'device_name' instead of string literal here?
Jeffrey Yasskin
2015/06/16 00:48:10
I just replaced this whole string with 'device_nam
| |
| 185 std::vector<uint8_t> value(value_str.begin(), value_str.end()); | 261 std::vector<uint8_t> value(value_str.begin(), value_str.end()); |
|
scheib
2015/06/15 23:47:21
While here, rename to 'device_name_value'.
Jeffrey Yasskin
2015/06/16 00:48:10
Done.
| |
| 186 ON_CALL(*device_name, ReadRemoteCharacteristic(_, _)) | 262 ON_CALL(*device_name_characteristic, ReadRemoteCharacteristic(_, _)) |
| 187 .WillByDefault(RunCallback<0>(value)); | 263 .WillByDefault(RunCallback<0>(value)); |
| 188 | 264 |
| 189 generic_access->AddMockCharacteristic(device_name.Pass()); | 265 generic_access->AddMockCharacteristic(device_name_characteristic.Pass()); |
| 190 | 266 |
| 191 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> reconnection_address( | 267 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> reconnection_address( |
| 192 GetGattCharacteristic(generic_access.get(), | 268 GetGattCharacteristic(generic_access.get(), |
| 193 "2A03" /* Reconnection Address */)); | 269 "2A03" /* Reconnection Address */)); |
| 194 | 270 |
| 195 ON_CALL(*reconnection_address, ReadRemoteCharacteristic(_, _)) | 271 ON_CALL(*reconnection_address, ReadRemoteCharacteristic(_, _)) |
| 196 .WillByDefault( | 272 .WillByDefault( |
| 197 RunCallback<1>(BluetoothGattService::GATT_ERROR_NOT_PERMITTED)); | 273 RunCallback<1>(BluetoothGattService::GATT_ERROR_NOT_PERMITTED)); |
| 198 | 274 |
| 199 generic_access->AddMockCharacteristic(reconnection_address.Pass()); | 275 generic_access->AddMockCharacteristic(reconnection_address.Pass()); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> | 349 scoped_ptr<NiceMock<MockBluetoothGattCharacteristic>> |
| 274 LayoutTestBluetoothAdapterProvider::GetGattCharacteristic( | 350 LayoutTestBluetoothAdapterProvider::GetGattCharacteristic( |
| 275 MockBluetoothGattService* service, | 351 MockBluetoothGattService* service, |
| 276 const std::string& uuid) { | 352 const std::string& uuid) { |
| 277 return make_scoped_ptr(new NiceMock<MockBluetoothGattCharacteristic>( | 353 return make_scoped_ptr(new NiceMock<MockBluetoothGattCharacteristic>( |
| 278 service, uuid /* identifier */, BluetoothUUID(uuid), false /* is_local */, | 354 service, uuid /* identifier */, BluetoothUUID(uuid), false /* is_local */, |
| 279 NULL /* properties */, NULL /* permissions */)); | 355 NULL /* properties */, NULL /* permissions */)); |
| 280 } | 356 } |
| 281 | 357 |
| 282 } // namespace content | 358 } // namespace content |
| OLD | NEW |