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/mock_bluetooth_adapter.h" | |
16 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
17 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using ::testing::Return; | |
21 | |
22 namespace bluetooth { | |
23 | |
24 typedef testing::NiceMock<device::MockBluetoothAdapter> | |
25 NiceMockBluetoothAdapter; | |
26 typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice; | |
27 typedef testing::NiceMock<device::MockBluetoothGattService> | |
28 NiceMockBluetoothGattService; | |
29 typedef testing::NiceMock<device::MockBluetoothGattConnection> | |
30 NiceMockBluetoothGattConnection; | |
31 | |
32 namespace { | |
33 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; | |
34 const char kTestLeDeviceName0[] = "Test LE Device 0"; | |
35 | |
36 const char kTestServiceId0[] = "service_id0"; | |
37 const char kTestServiceUuid0[] = "1234"; | |
38 | |
39 const char kTestServiceId1[] = "service_id1"; | |
40 const char kTestServiceUuid1[] = "5678"; | |
41 | |
42 class DeviceTest : public testing::Test { | |
43 public: | |
44 enum class Call { EXPECTED, NOT_EXPECTED }; | |
45 | |
46 DeviceTest() | |
47 : adapter_(new NiceMockBluetoothAdapter), | |
48 message_loop_(new base::MessageLoop), | |
49 weak_factory_(this) { | |
50 device_.reset( | |
51 new NiceMockBluetoothDevice(adapter_.get(), 0, kTestLeDeviceName0, | |
52 kTestLeDeviceAddress0, false, true)); | |
53 | |
54 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | |
55 .WillByDefault(Return(device_.get())); | |
56 | |
57 std::unique_ptr<NiceMockBluetoothGattService> service1( | |
58 new NiceMockBluetoothGattService( | |
59 device_.get(), kTestServiceId0, | |
60 device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */, | |
61 false /* is_local */)); | |
62 std::unique_ptr<NiceMockBluetoothGattService> service2( | |
63 new NiceMockBluetoothGattService( | |
64 device_.get(), kTestServiceId1, | |
65 device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */, | |
66 false /* is_local */)); | |
67 | |
68 device_->AddMockService(std::move(service1)); | |
69 device_->AddMockService(std::move(service2)); | |
70 | |
71 EXPECT_CALL(*device_, GetGattServices()) | |
72 .WillRepeatedly(Invoke(device_.get(), | |
73 &device::MockBluetoothDevice::GetMockServices)); | |
74 | |
75 std::unique_ptr<NiceMockBluetoothGattConnection> connection( | |
76 new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress())); | |
77 | |
78 // Owns itself. | |
79 device_service_ = | |
80 new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy_)); | |
81 } | |
82 | |
83 void TearDown() override { | |
84 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); | |
85 | |
86 if (!expect_device_service_deleted_) { | |
87 delete device_service_; | |
88 } | |
89 } | |
90 | |
91 protected: | |
92 void GetServicesCheckForPrecedingCalls( | |
93 Call expected, | |
94 size_t expected_length, | |
95 int num_of_preceding_calls, | |
96 const base::Closure& continuation, | |
97 std::vector<mojom::ServiceInfoPtr> services) { | |
98 EXPECT_EQ(num_of_preceding_calls, callback_count_); | |
99 ++callback_count_; | |
100 | |
101 if (expected == Call::EXPECTED) | |
102 ++actual_success_callback_calls_; | |
103 | |
104 EXPECT_EQ(expected_length, services.size()); | |
105 continuation.Run(); | |
106 } | |
107 | |
108 Device::GetServicesCallback GetGetServicesCheckForPrecedingCalls( | |
109 Call expected, | |
110 int num_of_preceding_calls, | |
111 const base::Closure& continuation) { | |
112 if (expected == Call::EXPECTED) | |
113 ++expected_success_callback_calls_; | |
114 | |
115 return base::Bind(&DeviceTest::GetServicesCheckForPrecedingCalls, | |
116 weak_factory_.GetWeakPtr(), expected, 2, | |
117 num_of_preceding_calls, continuation); | |
118 } | |
119 | |
120 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | |
121 std::unique_ptr<NiceMockBluetoothDevice> device_; | |
122 Device* device_service_; | |
123 std::unique_ptr<base::MessageLoop> message_loop_; | |
124 mojom::DevicePtr proxy_; | |
125 | |
126 bool expect_device_service_deleted_ = false; | |
127 int expected_success_callback_calls_ = 0; | |
128 int actual_success_callback_calls_ = 0; | |
129 int callback_count_ = 0; | |
130 | |
131 base::WeakPtrFactory<DeviceTest> weak_factory_; | |
132 }; | |
133 } // namespace | |
134 | |
135 TEST_F(DeviceTest, GetServices) { | |
136 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
137 .WillRepeatedly(Return(true)); | |
138 | |
139 base::RunLoop loop; | |
140 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
141 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
142 EXPECT_EQ(0, device_service_->GetPendingServiceRequestCountForTesting()); | |
143 | |
144 loop.Run(); | |
145 } | |
146 | |
147 TEST_F(DeviceTest, GetServicesNotDiscovered) { | |
148 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
149 .WillOnce(Return(false)) | |
150 .WillOnce(Return(false)) | |
151 .WillRepeatedly(Return(true)); | |
152 | |
153 base::RunLoop loop; | |
154 | |
155 // Client: Sends multiple requests for services. | |
156 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
157 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
158 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
159 Call::EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
160 | |
161 // Simulate: GattServicesDiscovered. | |
162 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
163 | |
164 // No more GetServices calls will complete. | |
165 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
166 | |
167 // Client: Sends more requests which run immediately. | |
168 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
169 Call::EXPECTED, 2 /* num_of_preceding_calls */, loop.QuitClosure())); | |
170 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
171 Call::EXPECTED, 3 /* num_of_preceding_calls */, loop.QuitClosure())); | |
172 | |
173 // No more GetServices calls will complete. | |
174 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
175 | |
176 loop.Run(); | |
177 } | |
178 | |
179 TEST_F(DeviceTest, GetServicesLostConnectionWithPendingRequests) { | |
180 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
181 .WillRepeatedly(Return(false)); | |
182 // Client: Sends multiple requests for services. | |
183 base::RunLoop loop; | |
184 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
185 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
186 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
187 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
188 EXPECT_EQ(2, device_service_->GetPendingServiceRequestCountForTesting()); | |
ortuno
2016/11/02 23:09:55
You could just check that callback_count_ is 0.
mbrunson
2016/11/03 18:50:19
Done.
| |
189 | |
190 // Simulate connection loss. | |
191 device_->SetConnected(false); | |
192 device_service_->DeviceChanged(nullptr /* adapter */, device_.get()); | |
193 expect_device_service_deleted_ = true; | |
194 } | |
195 | |
196 TEST_F(DeviceTest, GetServicesForcedDisconnectionWithPendingRequests) { | |
197 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
198 .WillRepeatedly(Return(false)); | |
199 // Client: Sends multiple requests for services. | |
200 base::RunLoop loop; | |
201 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
202 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
203 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
204 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
205 EXPECT_EQ(2, device_service_->GetPendingServiceRequestCountForTesting()); | |
206 | |
207 // Simulate connection loss. | |
208 device_service_->Disconnect(); | |
209 expect_device_service_deleted_ = true; | |
210 } | |
211 | |
212 TEST_F(DeviceTest, GetServicesPipeClosedWithPendingRequests) { | |
213 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
214 .WillRepeatedly(Return(false)); | |
215 // Client: Sends multiple requests for services. | |
216 base::RunLoop loop; | |
217 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
218 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
219 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
220 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
221 EXPECT_EQ(2, device_service_->GetPendingServiceRequestCountForTesting()); | |
222 | |
223 // Simulate message pipe error. | |
224 proxy_.reset(); | |
225 expect_device_service_deleted_ = true; | |
226 } | |
227 | |
228 } // namespace bluetooth | |
OLD | NEW |