| 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::MockBluetoothGattConnection; | 26 using device::MockBluetoothGattConnection; |
| 27 using device::MockBluetoothGattService; | 27 using device::MockBluetoothGattService; |
| 28 using testing::Between; | 28 using testing::Between; |
| 29 using testing::ElementsAre; |
| 29 using testing::Invoke; | 30 using testing::Invoke; |
| 31 using testing::NiceMock; |
| 32 using testing::ResultOf; |
| 30 using testing::Return; | 33 using testing::Return; |
| 31 using testing::NiceMock; | 34 using testing::WithArgs; |
| 32 using testing::_; | 35 using testing::_; |
| 33 | 36 |
| 34 namespace { | 37 namespace { |
| 38 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). |
| 39 const char kBatteryServiceUUID[] = "180f"; |
| 40 const char kGenericAccessServiceUUID[] = "1800"; |
| 41 const char kGenericAttributeServiceUUID[] = "1801"; |
| 42 const char kGlucoseServiceUUID[] = "1808"; |
| 43 const char kHeartRateServiceUUID[] = "180d"; |
| 44 |
| 35 // Invokes Run() on the k-th argument of the function with no arguments. | 45 // Invokes Run() on the k-th argument of the function with no arguments. |
| 36 ACTION_TEMPLATE(RunCallback, | 46 ACTION_TEMPLATE(RunCallback, |
| 37 HAS_1_TEMPLATE_PARAMS(int, k), | 47 HAS_1_TEMPLATE_PARAMS(int, k), |
| 38 AND_0_VALUE_PARAMS()) { | 48 AND_0_VALUE_PARAMS()) { |
| 39 return ::testing::get<k>(args).Run(); | 49 return ::testing::get<k>(args).Run(); |
| 40 } | 50 } |
| 41 | 51 |
| 42 // Invokes Run() on the k-th argument of the function with 1 argument. | 52 // Invokes Run() on the k-th argument of the function with 1 argument. |
| 43 ACTION_TEMPLATE(RunCallback, | 53 ACTION_TEMPLATE(RunCallback, |
| 44 HAS_1_TEMPLATE_PARAMS(int, k), | 54 HAS_1_TEMPLATE_PARAMS(int, k), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 73 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 83 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
| 74 const std::string& fake_adapter_name) { | 84 const std::string& fake_adapter_name) { |
| 75 if (fake_adapter_name == "EmptyAdapter") | 85 if (fake_adapter_name == "EmptyAdapter") |
| 76 return GetEmptyAdapter(); | 86 return GetEmptyAdapter(); |
| 77 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") | 87 else if (fake_adapter_name == "SingleEmptyDeviceAdapter") |
| 78 return GetSingleEmptyDeviceAdapter(); | 88 return GetSingleEmptyDeviceAdapter(); |
| 79 else if (fake_adapter_name == "ConnectableDeviceAdapter") | 89 else if (fake_adapter_name == "ConnectableDeviceAdapter") |
| 80 return GetConnectableDeviceAdapter(); | 90 return GetConnectableDeviceAdapter(); |
| 81 else if (fake_adapter_name == "UnconnectableDeviceAdapter") | 91 else if (fake_adapter_name == "UnconnectableDeviceAdapter") |
| 82 return GetUnconnectableDeviceAdapter(); | 92 return GetUnconnectableDeviceAdapter(); |
| 93 else if (fake_adapter_name == "ScanFilterCheckingAdapter") |
| 94 return GetScanFilterCheckingAdapter(); |
| 95 else if (fake_adapter_name == "MultiDeviceAdapter") |
| 96 return GetMultiDeviceAdapter(); |
| 83 else if (fake_adapter_name == "") | 97 else if (fake_adapter_name == "") |
| 84 return NULL; | 98 return NULL; |
| 85 | 99 |
| 86 NOTREACHED(); | 100 NOTREACHED(); |
| 87 return NULL; | 101 return NULL; |
| 88 } | 102 } |
| 89 | 103 |
| 90 // static | 104 // static |
| 91 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 105 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 92 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 106 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
| 93 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 107 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 94 new NiceMock<MockBluetoothAdapter>()); | 108 new NiceMock<MockBluetoothAdapter>()); |
| 95 | 109 |
| 96 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 110 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 97 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 111 .WillByDefault(RunCallbackWithResult<1 /* success_callback */>( |
| 98 []() { return GetDiscoverySession(); })); | 112 []() { return GetDiscoverySession(); })); |
| 99 | 113 |
| 100 // Using Invoke allows the adapter returned from this method to be futher | 114 // Using Invoke allows the adapter returned from this method to be futher |
| 101 // modified and have devices added to it. The call to ::GetDevices will | 115 // modified and have devices added to it. The call to ::GetDevices will |
| 102 // invoke ::GetConstMockDevices, returning all devices added up to that time. | 116 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
| 103 ON_CALL(*adapter, GetDevices()) | 117 ON_CALL(*adapter, GetDevices()) |
| 104 .WillByDefault( | 118 .WillByDefault( |
| 105 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); | 119 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
| 106 | 120 |
| 107 // The call to ::GetDevice will invoke GetMockDevice which returns a device | 121 // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| 108 // matching the address provided if the device was added to the mock. | 122 // matching the address provided if the device was added to the mock. |
| 109 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); | 123 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
| 110 | 124 |
| 111 return adapter.Pass(); | 125 return adapter.Pass(); |
| 112 } | 126 } |
| 113 | 127 |
| 128 static std::set<BluetoothUUID> GetUUIDs( |
| 129 const device::BluetoothDiscoveryFilter* filter) { |
| 130 std::set<BluetoothUUID> result; |
| 131 filter->GetUUIDs(result); |
| 132 return result; |
| 133 }; |
| 134 |
| 135 static void LogFilterUUIDs(const device::BluetoothDiscoveryFilter* filter) { |
| 136 std::string uuid_message; |
| 137 for (const BluetoothUUID& uuid : GetUUIDs(filter)) { |
| 138 uuid_message += uuid.canonical_value() + ", "; |
| 139 } |
| 140 LOG(ERROR) << "Unexpected UUIDs in filter: " << uuid_message; |
| 141 } |
| 142 |
| 143 // static |
| 144 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 145 LayoutTestBluetoothAdapterProvider::GetScanFilterCheckingAdapter() { |
| 146 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
| 147 new NiceMock<MockBluetoothAdapter>()); |
| 148 |
| 149 EXPECT_CALL( |
| 150 *adapter, |
| 151 StartDiscoverySessionWithFilterRaw( |
| 152 ResultOf(&GetUUIDs, ElementsAre(BluetoothUUID(kGlucoseServiceUUID), |
| 153 BluetoothUUID(kHeartRateServiceUUID), |
| 154 BluetoothUUID(kBatteryServiceUUID))), |
| 155 _, _)) |
| 156 .WillRepeatedly(RunCallbackWithResult<1 /* success_callback */>( |
| 157 []() { return GetDiscoverySession(); })); |
| 158 |
| 159 // Any unexpected call results in the failure callback. |
| 160 ON_CALL(*adapter, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 161 .WillByDefault(DoAll(WithArgs<0>(Invoke(&LogFilterUUIDs)), |
| 162 RunCallback<2 /* failure_callback */>())); |
| 163 |
| 164 // Using Invoke allows the adapter returned from this method to be futher |
| 165 // modified and have devices added to it. The call to ::GetDevices will |
| 166 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
| 167 ON_CALL(*adapter, GetDevices()) |
| 168 .WillByDefault( |
| 169 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
| 170 |
| 171 // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| 172 // matching the address provided if the device was added to the mock. |
| 173 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
| 174 |
| 175 return adapter.Pass(); |
| 176 } |
| 177 |
| 114 // static | 178 // static |
| 115 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 179 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 116 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 180 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
| 117 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 181 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 118 | 182 |
| 119 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 183 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
| 120 | 184 |
| 121 return adapter.Pass(); | 185 return adapter.Pass(); |
| 122 } | 186 } |
| 123 | 187 |
| 124 // static | 188 // static |
| 125 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 189 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 190 LayoutTestBluetoothAdapterProvider::GetMultiDeviceAdapter() { |
| 191 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 192 |
| 193 scoped_ptr<NiceMock<MockBluetoothDevice>> heart_rate_device = |
| 194 GetEmptyDevice(adapter.get(), "Heart Rate Device"); |
| 195 BluetoothDevice::UUIDList heart_rate_uuid_list; |
| 196 heart_rate_uuid_list.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
| 197 ON_CALL(*heart_rate_device, GetUUIDs()) |
| 198 .WillByDefault(Return(heart_rate_uuid_list)); |
| 199 adapter->AddMockDevice(heart_rate_device.Pass()); |
| 200 |
| 201 scoped_ptr<NiceMock<MockBluetoothDevice>> glucose_device = |
| 202 GetEmptyDevice(adapter.get(), "Glucose Device"); |
| 203 BluetoothDevice::UUIDList glucose_uuid_list; |
| 204 glucose_uuid_list.push_back(BluetoothUUID(kGlucoseServiceUUID)); |
| 205 ON_CALL(*glucose_device, GetUUIDs()).WillByDefault(Return(glucose_uuid_list)); |
| 206 adapter->AddMockDevice(glucose_device.Pass()); |
| 207 |
| 208 return adapter.Pass(); |
| 209 } |
| 210 |
| 211 // static |
| 212 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 126 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { | 213 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { |
| 127 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 214 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 128 | 215 |
| 129 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); | 216 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); |
| 130 | 217 |
| 131 return adapter.Pass(); | 218 return adapter.Pass(); |
| 132 } | 219 } |
| 133 | 220 |
| 134 // static | 221 // static |
| 135 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 222 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 149 | 236 |
| 150 ON_CALL(*discovery_session, Stop(_, _)) | 237 ON_CALL(*discovery_session, Stop(_, _)) |
| 151 .WillByDefault(RunCallback<0 /* success_callback */>()); | 238 .WillByDefault(RunCallback<0 /* success_callback */>()); |
| 152 | 239 |
| 153 return discovery_session.Pass(); | 240 return discovery_session.Pass(); |
| 154 } | 241 } |
| 155 | 242 |
| 156 // static | 243 // static |
| 157 scoped_ptr<NiceMock<MockBluetoothDevice>> | 244 scoped_ptr<NiceMock<MockBluetoothDevice>> |
| 158 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 245 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
| 159 MockBluetoothAdapter* adapter) { | 246 MockBluetoothAdapter* adapter, |
| 247 const std::string& device_name) { |
| 160 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 248 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
| 161 new NiceMock<MockBluetoothDevice>( | 249 new NiceMock<MockBluetoothDevice>( |
| 162 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 250 adapter, 0x1F00 /* Bluetooth Class */, device_name, |
| 163 "Empty Mock Device instanceID", true /* Paired */, | 251 device_name + " instanceID", true /* Paired */, |
| 164 true /* Connected */)); | 252 true /* Connected */)); |
| 165 | 253 |
| 166 ON_CALL(*empty_device, GetVendorIDSource()) | 254 ON_CALL(*empty_device, GetVendorIDSource()) |
| 167 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 255 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
| 168 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 256 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
| 169 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 257 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
| 170 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 258 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
| 171 | 259 |
| 172 BluetoothDevice::UUIDList list; | 260 BluetoothDevice::UUIDList list; |
| 173 list.push_back(BluetoothUUID("1800")); | 261 list.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
| 174 list.push_back(BluetoothUUID("1801")); | 262 list.push_back(BluetoothUUID(kGenericAttributeServiceUUID)); |
| 175 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 263 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
| 176 | 264 |
| 177 empty_device->AddMockService( | 265 empty_device->AddMockService( |
| 178 GetMockService(empty_device.get(), "1800" /* Generic Access */)); | 266 GetMockService(empty_device.get(), kGenericAccessServiceUUID)); |
| 179 empty_device->AddMockService( | 267 empty_device->AddMockService( |
| 180 GetMockService(empty_device.get(), "1801" /* Generic Attribute */)); | 268 GetMockService(empty_device.get(), kGenericAttributeServiceUUID)); |
| 181 | 269 |
| 182 // Using Invoke allows the device returned from this method to be futher | 270 // Using Invoke allows the device returned from this method to be futher |
| 183 // modified and have more services added to it. The call to ::GetGattServices | 271 // modified and have more services added to it. The call to ::GetGattServices |
| 184 // will invoke ::GetMockServices, returning all services added up to that | 272 // will invoke ::GetMockServices, returning all services added up to that |
| 185 // time. | 273 // time. |
| 186 ON_CALL(*empty_device, GetGattServices()) | 274 ON_CALL(*empty_device, GetGattServices()) |
| 187 .WillByDefault( | 275 .WillByDefault( |
| 188 Invoke(empty_device.get(), &MockBluetoothDevice::GetMockServices)); | 276 Invoke(empty_device.get(), &MockBluetoothDevice::GetMockServices)); |
| 189 | 277 |
| 190 return empty_device.Pass(); | 278 return empty_device.Pass(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // static | 312 // static |
| 225 scoped_ptr<NiceMock<MockBluetoothGattService>> | 313 scoped_ptr<NiceMock<MockBluetoothGattService>> |
| 226 LayoutTestBluetoothAdapterProvider::GetMockService(MockBluetoothDevice* device, | 314 LayoutTestBluetoothAdapterProvider::GetMockService(MockBluetoothDevice* device, |
| 227 const std::string& uuid) { | 315 const std::string& uuid) { |
| 228 return make_scoped_ptr(new NiceMock<MockBluetoothGattService>( | 316 return make_scoped_ptr(new NiceMock<MockBluetoothGattService>( |
| 229 device, uuid /* identifier */, BluetoothUUID(uuid), true /* is_primary */, | 317 device, uuid /* identifier */, BluetoothUUID(uuid), true /* is_primary */, |
| 230 false /* is_local */)); | 318 false /* is_local */)); |
| 231 } | 319 } |
| 232 | 320 |
| 233 } // namespace content | 321 } // namespace content |
| OLD | NEW |