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 "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
14 | 15 |
15 using device::BluetoothAdapter; | 16 using device::BluetoothAdapter; |
16 using device::BluetoothAdapterFactory; | 17 using device::BluetoothAdapterFactory; |
17 using device::BluetoothDevice; | 18 using device::BluetoothDevice; |
18 using device::BluetoothDiscoverySession; | 19 using device::BluetoothDiscoverySession; |
| 20 using device::BluetoothGattConnection; |
19 using device::BluetoothUUID; | 21 using device::BluetoothUUID; |
20 using device::MockBluetoothAdapter; | 22 using device::MockBluetoothAdapter; |
21 using device::MockBluetoothDevice; | 23 using device::MockBluetoothDevice; |
22 using device::MockBluetoothDiscoverySession; | 24 using device::MockBluetoothDiscoverySession; |
| 25 using device::MockBluetoothGattConnection; |
| 26 using testing::Between; |
23 using testing::Invoke; | 27 using testing::Invoke; |
24 using testing::Return; | 28 using testing::Return; |
25 using testing::NiceMock; | 29 using testing::NiceMock; |
26 using testing::_; | 30 using testing::_; |
27 | 31 |
28 namespace { | 32 namespace { |
29 // Invokes Run() on the k-th argument of the function with no arguments. | 33 // Invokes Run() on the k-th argument of the function with no arguments. |
30 ACTION_TEMPLATE(RunCallback, | 34 ACTION_TEMPLATE(RunCallback, |
31 HAS_1_TEMPLATE_PARAMS(int, k), | 35 HAS_1_TEMPLATE_PARAMS(int, k), |
32 AND_0_VALUE_PARAMS()) { | 36 AND_0_VALUE_PARAMS()) { |
33 return ::testing::get<k>(args).Run(); | 37 return ::testing::get<k>(args).Run(); |
34 } | 38 } |
35 | 39 |
| 40 // Invokes Run() on the k-th argument of the function with 1 argument. |
| 41 ACTION_TEMPLATE(RunCallback, |
| 42 HAS_1_TEMPLATE_PARAMS(int, k), |
| 43 AND_1_VALUE_PARAMS(p0)) { |
| 44 return ::testing::get<k>(args).Run(p0); |
| 45 } |
| 46 |
36 // Invokes Run() on the k-th argument of the function with the result | 47 // Invokes Run() on the k-th argument of the function with the result |
37 // of |func| as an argument. | 48 // of |func| as an argument. |
38 ACTION_TEMPLATE(RunCallbackWithResult, | 49 ACTION_TEMPLATE(RunCallbackWithResult, |
39 HAS_1_TEMPLATE_PARAMS(int, k), | 50 HAS_1_TEMPLATE_PARAMS(int, k), |
40 AND_1_VALUE_PARAMS(func)) { | 51 AND_1_VALUE_PARAMS(func)) { |
41 return ::testing::get<k>(args).Run(func()); | 52 return ::testing::get<k>(args).Run(func()); |
42 } | 53 } |
| 54 |
| 55 // Function to iterate over the adapter's devices and return the one |
| 56 // that matches the address. |
| 57 ACTION_P(GetMockDevice, adapter) { |
| 58 std::string address = arg0; |
| 59 for (BluetoothDevice* device : adapter->GetMockDevices()) { |
| 60 if (device->GetAddress() == address) |
| 61 return device; |
| 62 } |
| 63 return NULL; |
| 64 } |
43 } | 65 } |
44 | 66 |
45 namespace content { | 67 namespace content { |
46 | 68 |
47 // static | 69 // static |
48 scoped_refptr<BluetoothAdapter> | 70 scoped_refptr<BluetoothAdapter> |
49 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 71 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
50 const std::string& fake_adapter_name) { | 72 const std::string& fake_adapter_name) { |
51 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified | 73 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified |
52 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || | 74 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || |
53 fake_adapter_name == "EmptyAdapter") { | 75 fake_adapter_name == "EmptyAdapter") { |
54 return GetEmptyAdapter(); | 76 return GetEmptyAdapter(); |
55 } | 77 } |
56 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified | 78 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified |
57 else if (fake_adapter_name == "Single Empty Device" || | 79 else if (fake_adapter_name == "Single Empty Device" || |
58 fake_adapter_name == "SingleEmptyDeviceAdapter") { | 80 fake_adapter_name == "SingleEmptyDeviceAdapter") { |
59 return GetSingleEmptyDeviceAdapter(); | 81 return GetSingleEmptyDeviceAdapter(); |
| 82 } else if (fake_adapter_name == "ConnectableDeviceAdapter") { |
| 83 return GetConnectableDeviceAdapter(); |
| 84 } else if (fake_adapter_name == "UnconnectableDeviceAdapter") { |
| 85 return GetUnconnectableDeviceAdapter(); |
60 } else if (fake_adapter_name == "") { | 86 } else if (fake_adapter_name == "") { |
61 return NULL; | 87 return NULL; |
62 } | 88 } |
63 NOTREACHED(); | 89 NOTREACHED(); |
64 return NULL; | 90 return NULL; |
65 } | 91 } |
66 | 92 |
67 // static | 93 // static |
68 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 94 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
69 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 95 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
70 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 96 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
71 new NiceMock<MockBluetoothAdapter>()); | 97 new NiceMock<MockBluetoothAdapter>()); |
72 | 98 |
73 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 99 ON_CALL(*adapter, StartDiscoverySession(_, _)) |
74 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 100 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
75 []() { return GetDiscoverySession(); })); | 101 []() { return GetDiscoverySession(); })); |
76 | 102 |
77 // Using Invoke allows the adapter returned from this method to be futher | 103 // Using Invoke allows the adapter returned from this method to be futher |
78 // modified and have devices added to it. The call to ::GetDevices will | 104 // modified and have devices added to it. The call to ::GetDevices will |
79 // invoke ::GetConstMockDevices, returning all devices added up to that time. | 105 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
80 ON_CALL(*adapter, GetDevices()) | 106 ON_CALL(*adapter, GetDevices()) |
81 .WillByDefault( | 107 .WillByDefault( |
82 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); | 108 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
83 | 109 |
| 110 // The call to ::GetDevice will invoke GetMockDevice which returns a device |
| 111 // matching the address provided if the device was added to the mock. |
| 112 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); |
| 113 |
84 return adapter.Pass(); | 114 return adapter.Pass(); |
85 } | 115 } |
86 | 116 |
87 // static | 117 // static |
88 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 118 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
89 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 119 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
90 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 120 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
91 | 121 |
92 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 122 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
93 | 123 |
94 return adapter.Pass(); | 124 return adapter.Pass(); |
95 } | 125 } |
96 | 126 |
97 // static | 127 // static |
| 128 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 129 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { |
| 130 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 131 |
| 132 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); |
| 133 |
| 134 return adapter.Pass(); |
| 135 } |
| 136 |
| 137 // static |
| 138 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
| 139 LayoutTestBluetoothAdapterProvider::GetUnconnectableDeviceAdapter() { |
| 140 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
| 141 |
| 142 adapter->AddMockDevice(GetUnconnectableDevice(adapter.get())); |
| 143 |
| 144 return adapter.Pass(); |
| 145 } |
| 146 |
| 147 // static |
| 148 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> |
| 149 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { |
| 150 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( |
| 151 new NiceMock<MockBluetoothDiscoverySession>()); |
| 152 |
| 153 ON_CALL(*discovery_session, Stop(_, _)) |
| 154 .WillByDefault(RunCallback<0 /* success_callback */>()); |
| 155 |
| 156 return discovery_session.Pass(); |
| 157 } |
| 158 |
| 159 // static |
98 scoped_ptr<NiceMock<MockBluetoothDevice>> | 160 scoped_ptr<NiceMock<MockBluetoothDevice>> |
99 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 161 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
100 MockBluetoothAdapter* adapter) { | 162 MockBluetoothAdapter* adapter) { |
101 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 163 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
102 new NiceMock<MockBluetoothDevice>( | 164 new NiceMock<MockBluetoothDevice>( |
103 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 165 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", |
104 "Empty Mock Device instanceID", true /* Paired */, | 166 "Empty Mock Device instanceID", true /* Paired */, |
105 true /* Connected */)); | 167 true /* Connected */)); |
106 | 168 |
107 ON_CALL(*empty_device, GetVendorIDSource()) | 169 ON_CALL(*empty_device, GetVendorIDSource()) |
108 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 170 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
109 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 171 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
110 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 172 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
111 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 173 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
112 | 174 |
113 BluetoothDevice::UUIDList list; | 175 BluetoothDevice::UUIDList list; |
114 list.push_back(BluetoothUUID("1800")); | 176 list.push_back(BluetoothUUID("1800")); |
115 list.push_back(BluetoothUUID("1801")); | 177 list.push_back(BluetoothUUID("1801")); |
116 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 178 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
117 return empty_device.Pass(); | 179 return empty_device.Pass(); |
118 } | 180 } |
119 | 181 |
120 // static | 182 // static |
121 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> | 183 scoped_ptr<NiceMock<MockBluetoothDevice>> |
122 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { | 184 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( |
123 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | 185 MockBluetoothAdapter* adapter) { |
124 new NiceMock<MockBluetoothDiscoverySession>()); | 186 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); |
125 | 187 |
126 ON_CALL(*discovery_session, Stop(_, _)) | 188 BluetoothDevice* device_ptr = device.get(); |
127 .WillByDefault(RunCallback<0 /* success_callback */>()); | |
128 | 189 |
129 return discovery_session.Pass(); | 190 ON_CALL(*device, CreateGattConnection(_, _)) |
| 191 .WillByDefault( |
| 192 RunCallbackWithResult<0 /* success_callback */>([device_ptr]() { |
| 193 return make_scoped_ptr(new NiceMock<MockBluetoothGattConnection>( |
| 194 device_ptr->GetAddress())); |
| 195 })); |
| 196 |
| 197 return device.Pass(); |
| 198 } |
| 199 |
| 200 // static |
| 201 scoped_ptr<NiceMock<MockBluetoothDevice>> |
| 202 LayoutTestBluetoothAdapterProvider::GetUnconnectableDevice( |
| 203 MockBluetoothAdapter* adapter) { |
| 204 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); |
| 205 |
| 206 ON_CALL(*device, CreateGattConnection(_, _)) |
| 207 .WillByDefault( |
| 208 RunCallback<1 /* error_callback */>(BluetoothDevice::ERROR_FAILED)); |
| 209 |
| 210 return device.Pass(); |
130 } | 211 } |
131 | 212 |
132 } // namespace content | 213 } // namespace content |
OLD | NEW |