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 ::std::tr1::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 ACTION_P(GetMockDevice, adapter) { | |
scheib
2015/05/20 20:14:08
Comment
ortuno
2015/05/20 20:54:47
Done.
| |
56 std::string address = arg0; | |
57 for (auto& device : adapter->GetMockDevices()) { | |
58 if (device->GetAddress() == address) | |
59 return device; | |
60 } | |
61 return NULL; | |
62 } | |
43 } | 63 } |
44 | 64 |
45 namespace content { | 65 namespace content { |
46 | 66 |
47 // static | 67 // static |
48 scoped_refptr<BluetoothAdapter> | 68 scoped_refptr<BluetoothAdapter> |
49 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 69 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
50 const std::string& fake_adapter_name) { | 70 const std::string& fake_adapter_name) { |
51 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified | 71 // TODO(ortuno): Remove RejectRequestDevice once LayoutTests are modified |
52 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || | 72 if (fake_adapter_name == "RejectRequestDevice_NotFoundError" || |
53 fake_adapter_name == "EmptyAdapter") { | 73 fake_adapter_name == "EmptyAdapter") { |
54 return GetEmptyAdapter(); | 74 return GetEmptyAdapter(); |
55 } | 75 } |
56 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified | 76 // TODO(ortuno): Remove "Single Empty Device" once LayoutTests are modified |
57 else if (fake_adapter_name == "Single Empty Device" || | 77 else if (fake_adapter_name == "Single Empty Device" || |
58 fake_adapter_name == "SingleEmptyDeviceAdapter") { | 78 fake_adapter_name == "SingleEmptyDeviceAdapter") { |
59 return GetSingleEmptyDeviceAdapter(); | 79 return GetSingleEmptyDeviceAdapter(); |
80 } else if (fake_adapter_name == "ConnectableDeviceAdapter") { | |
81 return GetConnectableDeviceAdapter(); | |
82 } else if (fake_adapter_name == "UnconnectableDeviceAdapter") { | |
83 return GetUnconnectableDeviceAdapter(); | |
60 } else if (fake_adapter_name == "") { | 84 } else if (fake_adapter_name == "") { |
61 return NULL; | 85 return NULL; |
62 } | 86 } |
63 NOTREACHED(); | 87 NOTREACHED(); |
64 return NULL; | 88 return NULL; |
65 } | 89 } |
66 | 90 |
67 // static | 91 // static |
68 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 92 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
69 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { | 93 LayoutTestBluetoothAdapterProvider::GetEmptyAdapter() { |
70 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( | 94 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter( |
71 new NiceMock<MockBluetoothAdapter>()); | 95 new NiceMock<MockBluetoothAdapter>()); |
72 | 96 |
73 ON_CALL(*adapter, StartDiscoverySession(_, _)) | 97 ON_CALL(*adapter, StartDiscoverySession(_, _)) |
74 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( | 98 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
75 []() { return GetDiscoverySession(); })); | 99 []() { return GetDiscoverySession(); })); |
76 | 100 |
77 // Using Invoke allows the adapter returned from this method to be futher | 101 // 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 | 102 // modified and have devices added to it. The call to ::GetDevices will |
79 // invoke ::GetConstMockDevices, returning all devices added up to that time. | 103 // invoke ::GetConstMockDevices, returning all devices added up to that time. |
80 ON_CALL(*adapter, GetDevices()) | 104 ON_CALL(*adapter, GetDevices()) |
81 .WillByDefault( | 105 .WillByDefault( |
82 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); | 106 Invoke(adapter.get(), &MockBluetoothAdapter::GetConstMockDevices)); |
83 | 107 |
108 ON_CALL(*adapter, GetDevice(_)).WillByDefault(GetMockDevice(adapter.get())); | |
109 | |
84 return adapter.Pass(); | 110 return adapter.Pass(); |
85 } | 111 } |
86 | 112 |
87 // static | 113 // static |
88 scoped_refptr<NiceMock<MockBluetoothAdapter>> | 114 scoped_refptr<NiceMock<MockBluetoothAdapter>> |
89 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { | 115 LayoutTestBluetoothAdapterProvider::GetSingleEmptyDeviceAdapter() { |
90 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | 116 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); |
91 | 117 |
92 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); | 118 adapter->AddMockDevice(GetEmptyDevice(adapter.get())); |
93 | 119 |
94 return adapter.Pass(); | 120 return adapter.Pass(); |
95 } | 121 } |
96 | 122 |
97 // static | 123 // static |
124 scoped_refptr<NiceMock<MockBluetoothAdapter>> | |
125 LayoutTestBluetoothAdapterProvider::GetConnectableDeviceAdapter() { | |
126 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | |
127 | |
128 adapter->AddMockDevice(GetConnectableDevice(adapter.get())); | |
129 | |
130 return adapter.Pass(); | |
131 } | |
132 | |
133 // static | |
134 scoped_refptr<NiceMock<MockBluetoothAdapter>> | |
135 LayoutTestBluetoothAdapterProvider::GetUnconnectableDeviceAdapter() { | |
136 scoped_refptr<NiceMock<MockBluetoothAdapter>> adapter(GetEmptyAdapter()); | |
137 | |
138 adapter->AddMockDevice(GetUnconnectableDevice(adapter.get())); | |
139 | |
140 return adapter.Pass(); | |
141 } | |
142 | |
143 // static | |
144 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> | |
145 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { | |
146 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | |
147 new NiceMock<MockBluetoothDiscoverySession>()); | |
148 | |
149 ON_CALL(*discovery_session, Stop(_, _)) | |
150 .WillByDefault(RunCallback<0 /* success_callback */>()); | |
151 | |
152 return discovery_session.Pass(); | |
153 } | |
154 | |
155 // static | |
98 scoped_ptr<NiceMock<MockBluetoothDevice>> | 156 scoped_ptr<NiceMock<MockBluetoothDevice>> |
99 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( | 157 LayoutTestBluetoothAdapterProvider::GetEmptyDevice( |
100 MockBluetoothAdapter* adapter) { | 158 MockBluetoothAdapter* adapter) { |
101 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( | 159 scoped_ptr<NiceMock<MockBluetoothDevice>> empty_device( |
102 new NiceMock<MockBluetoothDevice>( | 160 new NiceMock<MockBluetoothDevice>( |
103 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", | 161 adapter, 0x1F00 /* Bluetooth Class */, "Empty Mock Device name", |
104 "Empty Mock Device instanceID", true /* Paired */, | 162 "Empty Mock Device instanceID", true /* Paired */, |
105 true /* Connected */)); | 163 true /* Connected */)); |
106 | 164 |
107 ON_CALL(*empty_device, GetVendorIDSource()) | 165 ON_CALL(*empty_device, GetVendorIDSource()) |
108 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); | 166 .WillByDefault(Return(BluetoothDevice::VENDOR_ID_BLUETOOTH)); |
109 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); | 167 ON_CALL(*empty_device, GetVendorID()).WillByDefault(Return(0xFFFF)); |
110 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); | 168 ON_CALL(*empty_device, GetProductID()).WillByDefault(Return(1)); |
111 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); | 169 ON_CALL(*empty_device, GetDeviceID()).WillByDefault(Return(2)); |
112 | 170 |
113 BluetoothDevice::UUIDList list; | 171 BluetoothDevice::UUIDList list; |
114 list.push_back(BluetoothUUID("1800")); | 172 list.push_back(BluetoothUUID("1800")); |
115 list.push_back(BluetoothUUID("1801")); | 173 list.push_back(BluetoothUUID("1801")); |
116 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); | 174 ON_CALL(*empty_device, GetUUIDs()).WillByDefault(Return(list)); |
117 return empty_device.Pass(); | 175 return empty_device.Pass(); |
118 } | 176 } |
119 | 177 |
120 // static | 178 // static |
121 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> | 179 scoped_ptr<NiceMock<MockBluetoothDevice>> |
122 LayoutTestBluetoothAdapterProvider::GetDiscoverySession() { | 180 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( |
123 scoped_ptr<NiceMock<MockBluetoothDiscoverySession>> discovery_session( | 181 MockBluetoothAdapter* adapter) { |
124 new NiceMock<MockBluetoothDiscoverySession>()); | 182 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); |
125 | 183 |
126 ON_CALL(*discovery_session, Stop(_, _)) | 184 BluetoothDevice* device_ptr = device.get(); |
scheib
2015/05/20 20:14:08
This is OK, but it may be simpler to read the bloc
ortuno
2015/05/20 20:54:47
Hmm this way we avoid creating a copy of the addre
Jeffrey Yasskin
2015/05/20 21:14:07
To elaborate, we have to capture the address by va
scheib
2015/05/20 21:32:13
I'm fine either way. I thought capturing the addre
Jeffrey Yasskin
2015/05/20 22:29:25
make_scoped_ptr() would also make this shorter:
ht
ortuno
2015/05/20 23:35:19
Done.
Jeffrey Yasskin
2015/05/21 00:12:00
Good point. I don't think it's worth diverging fro
| |
127 .WillByDefault(RunCallback<0 /* success_callback */>()); | |
128 | 185 |
129 return discovery_session.Pass(); | 186 ON_CALL(*device, CreateGattConnection(_, _)) |
187 .WillByDefault( | |
188 RunCallbackWithResult<0 /* success_callback */>([device_ptr]() { | |
189 scoped_ptr<NiceMock<MockBluetoothGattConnection>> connection( | |
190 new NiceMock<MockBluetoothGattConnection>( | |
191 device_ptr->GetAddress())); | |
192 return connection.Pass(); | |
193 })); | |
194 | |
195 return device.Pass(); | |
196 } | |
197 | |
198 // static | |
199 scoped_ptr<NiceMock<MockBluetoothDevice>> | |
200 LayoutTestBluetoothAdapterProvider::GetUnconnectableDevice( | |
201 MockBluetoothAdapter* adapter) { | |
202 scoped_ptr<NiceMock<MockBluetoothDevice>> device(GetEmptyDevice(adapter)); | |
203 | |
204 ON_CALL(*device, CreateGattConnection(_, _)) | |
205 .WillByDefault( | |
206 RunCallback<1 /* error_callback */>(BluetoothDevice::ERROR_FAILED)); | |
207 | |
208 return device.Pass(); | |
130 } | 209 } |
131 | 210 |
132 } // namespace content | 211 } // namespace content |
OLD | NEW |