OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/bluetooth/device.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/run_loop.h" | |
15 #include "device/bluetooth/test/device_connection_helper.h" | |
16 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
17 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
18 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 using ::testing::Return; | |
22 | |
23 namespace bluetooth { | |
24 | |
25 typedef testing::NiceMock<device::MockBluetoothAdapter> | |
26 NiceMockBluetoothAdapter; | |
27 typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice; | |
28 typedef testing::NiceMock<device::MockBluetoothGattService> | |
29 NiceMockBluetoothGattService; | |
30 typedef testing::NiceMock<device::MockBluetoothGattConnection> | |
31 NiceMockBluetoothGattConnection; | |
dcheng
2016/11/11 01:25:33
Nit: prefer using A = B; to typedef B A; in new co
mbrunson
2016/11/11 21:26:15
Done.
| |
32 | |
33 namespace { | |
34 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; | |
35 const char kTestLeDeviceName0[] = "Test LE Device 0"; | |
36 | |
37 const char kTestServiceId0[] = "service_id0"; | |
38 const char kTestServiceUuid0[] = "1234"; | |
39 | |
40 const char kTestServiceId1[] = "service_id1"; | |
41 const char kTestServiceUuid1[] = "5678"; | |
42 | |
43 class BluetoothInterfaceDeviceTest : public testing::Test { | |
44 public: | |
45 enum class Call { EXPECTED, NOT_EXPECTED }; | |
46 | |
47 BluetoothInterfaceDeviceTest() | |
48 : adapter_(new NiceMockBluetoothAdapter), | |
49 message_loop_(new base::MessageLoop), | |
50 weak_factory_(this) { | |
51 device_.reset( | |
52 new NiceMockBluetoothDevice(adapter_.get(), 0, kTestLeDeviceName0, | |
53 kTestLeDeviceAddress0, false, true)); | |
54 | |
55 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | |
56 .WillByDefault(Return(device_.get())); | |
57 | |
58 std::unique_ptr<NiceMockBluetoothGattService> service1( | |
dcheng
2016/11/11 01:25:33
auto service1 = base::MakeUnique<NiceMockBluetooth
mbrunson
2016/11/11 21:26:15
Done.
| |
59 new NiceMockBluetoothGattService( | |
60 device_.get(), kTestServiceId0, | |
61 device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */, | |
62 false /* is_local */)); | |
63 std::unique_ptr<NiceMockBluetoothGattService> service2( | |
dcheng
2016/11/11 01:25:33
Ditto.
mbrunson
2016/11/11 21:26:15
Done.
| |
64 new NiceMockBluetoothGattService( | |
65 device_.get(), kTestServiceId1, | |
66 device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */, | |
67 false /* is_local */)); | |
68 | |
69 device_->AddMockService(std::move(service1)); | |
70 device_->AddMockService(std::move(service2)); | |
71 | |
72 EXPECT_CALL(*device_, GetGattServices()) | |
73 .WillRepeatedly(Invoke(device_.get(), | |
74 &device::MockBluetoothDevice::GetMockServices)); | |
75 | |
76 std::unique_ptr<NiceMockBluetoothGattConnection> connection( | |
77 new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress())); | |
78 | |
79 // Owns itself. | |
80 device_service_ = | |
81 new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy_)); | |
82 } | |
83 | |
84 void TearDown() override { | |
85 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); | |
86 | |
87 if (!expect_device_service_deleted_) { | |
88 delete device_service_; | |
dcheng
2016/11/11 01:25:33
This is confusing. Previous comments seem to imply
mbrunson
2016/11/11 21:26:15
There are a few tests that determine whether the D
| |
89 } | |
90 } | |
91 | |
92 protected: | |
93 void GetServicesCheckForPrecedingCalls( | |
94 Call expected, | |
95 size_t expected_service_count, | |
96 int num_of_preceding_calls, | |
97 DeviceConnectionHelper* helper, | |
98 const base::Closure& continuation, | |
99 std::vector<mojom::ServiceInfoPtr> services) { | |
100 helper->SetCallbackRan(true); | |
101 EXPECT_EQ(num_of_preceding_calls, callback_count_); | |
102 ++callback_count_; | |
103 | |
104 if (expected == Call::EXPECTED) | |
105 ++actual_success_callback_calls_; | |
106 | |
107 EXPECT_EQ(expected_service_count, services.size()); | |
108 continuation.Run(); | |
109 } | |
110 | |
111 Device::GetServicesCallback GetGetServicesCheckForPrecedingCalls( | |
112 Call expected, | |
113 int num_of_preceding_calls, | |
114 const base::Closure& continuation) { | |
115 if (expected == Call::EXPECTED) | |
116 ++expected_success_callback_calls_; | |
117 | |
118 return base::Bind( | |
119 &BluetoothInterfaceDeviceTest::GetServicesCheckForPrecedingCalls, | |
120 weak_factory_.GetWeakPtr(), expected, 2 /* expected_service_count */, | |
121 num_of_preceding_calls, base::Owned(new DeviceConnectionHelper( | |
122 device_service_->GetBindingForTesting())), | |
123 continuation); | |
124 } | |
125 | |
126 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | |
127 std::unique_ptr<NiceMockBluetoothDevice> device_; | |
128 Device* device_service_; | |
129 std::unique_ptr<base::MessageLoop> message_loop_; | |
dcheng
2016/11/11 01:25:33
Nit: device_ and message_loop_ can just be members
mbrunson
2016/11/11 21:26:15
Are you saying they should be put in the TEST_F se
dcheng
2016/11/15 07:24:02
The unique_ptr indirection is unnecessary: they ca
mbrunson
2016/11/16 03:32:03
Done.
| |
130 mojom::DevicePtr proxy_; | |
131 | |
132 bool expect_device_service_deleted_ = false; | |
133 int expected_success_callback_calls_ = 0; | |
134 int actual_success_callback_calls_ = 0; | |
135 int callback_count_ = 0; | |
136 | |
137 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; | |
138 }; | |
139 } // namespace | |
140 | |
141 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { | |
142 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
143 .WillRepeatedly(Return(true)); | |
144 | |
145 base::RunLoop loop; | |
146 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
147 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
148 | |
149 loop.Run(); | |
150 } | |
151 | |
152 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { | |
153 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
154 .WillOnce(Return(false)) | |
155 .WillOnce(Return(false)) | |
156 .WillRepeatedly(Return(true)); | |
157 | |
158 base::RunLoop loop; | |
159 | |
160 // Client: Sends multiple requests for services. | |
161 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
162 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
163 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
164 Call::EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
165 | |
166 // Simulate: GattServicesDiscovered. | |
167 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
168 | |
169 // No more GetServices calls will complete. | |
170 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
171 | |
172 // Client: Sends more requests which run immediately. | |
173 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
174 Call::EXPECTED, 2 /* num_of_preceding_calls */, loop.QuitClosure())); | |
175 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
176 Call::EXPECTED, 3 /* num_of_preceding_calls */, loop.QuitClosure())); | |
177 | |
178 // No more GetServices calls will complete. | |
179 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
180 | |
181 loop.Run(); | |
182 } | |
183 | |
184 TEST_F(BluetoothInterfaceDeviceTest, | |
185 GetServicesLostConnectionWithPendingRequests) { | |
186 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
187 .WillRepeatedly(Return(false)); | |
188 // Client: Sends multiple requests for services. | |
189 base::RunLoop loop; | |
190 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
191 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
192 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
193 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
194 EXPECT_EQ(0, callback_count_); | |
195 | |
196 // Simulate connection loss. | |
197 device_->SetConnected(false); | |
198 device_service_->DeviceChanged(nullptr /* adapter */, device_.get()); | |
199 expect_device_service_deleted_ = true; | |
200 } | |
201 | |
202 TEST_F(BluetoothInterfaceDeviceTest, | |
203 GetServicesForcedDisconnectionWithPendingRequests) { | |
204 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
205 .WillRepeatedly(Return(false)); | |
206 // Client: Sends multiple requests for services. | |
207 base::RunLoop loop; | |
208 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
209 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
210 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
211 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
212 EXPECT_EQ(0, callback_count_); | |
213 | |
214 // Simulate connection loss. | |
215 device_service_->Disconnect(); | |
216 expect_device_service_deleted_ = true; | |
217 } | |
218 | |
219 TEST_F(BluetoothInterfaceDeviceTest, GetServicesPipeClosedWithPendingRequests) { | |
220 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
221 .WillRepeatedly(Return(false)); | |
222 // Client: Sends multiple requests for services. | |
223 base::RunLoop loop; | |
224 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
225 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
226 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
227 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
228 EXPECT_EQ(0, callback_count_); | |
229 | |
230 // Simulate message pipe error. | |
231 proxy_.reset(); | |
232 expect_device_service_deleted_ = true; | |
233 } | |
234 | |
235 } // namespace bluetooth | |
OLD | NEW |