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 using NiceMockBluetoothAdapter = | |
25 testing::NiceMock<device::MockBluetoothAdapter>; | |
26 using NiceMockBluetoothDevice = testing::NiceMock<device::MockBluetoothDevice>; | |
27 using NiceMockBluetoothGattService = | |
28 testing::NiceMock<device::MockBluetoothGattService>; | |
29 using NiceMockBluetoothGattConnection = | |
30 testing::NiceMock<device::MockBluetoothGattConnection>; | |
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 BluetoothInterfaceDeviceTest : public testing::Test { | |
43 public: | |
44 enum class Call { EXPECTED, NOT_EXPECTED }; | |
45 | |
46 BluetoothInterfaceDeviceTest() | |
47 : adapter_(new NiceMockBluetoothAdapter), | |
48 device_(adapter_.get(), | |
49 0, | |
50 kTestLeDeviceName0, | |
51 kTestLeDeviceAddress0, | |
52 false, | |
53 true), | |
54 weak_factory_(this) { | |
55 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | |
56 .WillByDefault(Return(&device_)); | |
57 | |
58 auto service1 = base::MakeUnique<NiceMockBluetoothGattService>( | |
59 &device_, kTestServiceId0, device::BluetoothUUID(kTestServiceUuid0), | |
60 true /* is_primary */, false /* is_local */); | |
61 auto service2 = base::MakeUnique<NiceMockBluetoothGattService>( | |
62 &device_, kTestServiceId1, device::BluetoothUUID(kTestServiceUuid1), | |
63 true /* is_primary */, false /* is_local */); | |
64 | |
65 device_.AddMockService(std::move(service1)); | |
66 device_.AddMockService(std::move(service2)); | |
67 | |
68 EXPECT_CALL(device_, GetGattServices()) | |
69 .WillRepeatedly( | |
70 Invoke(&device_, &device::MockBluetoothDevice::GetMockServices)); | |
71 | |
72 auto connection = base::MakeUnique<NiceMockBluetoothGattConnection>( | |
73 adapter_, device_.GetAddress()); | |
74 | |
75 Device::Create(adapter_, std::move(connection), mojo::GetProxy(&proxy_)); | |
76 | |
77 proxy_.set_connection_error_handler( | |
78 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, | |
79 weak_factory_.GetWeakPtr())); | |
80 } | |
81 | |
82 void TearDown() override { | |
83 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); | |
84 EXPECT_EQ(message_pipe_closed_, expect_device_service_deleted_); | |
85 proxy_.reset(); | |
86 } | |
87 | |
88 protected: | |
89 void OnConnectionError() { message_pipe_closed_ = true; } | |
90 | |
91 void SimulateGattServicesDiscovered() { | |
92 for (auto& observer : adapter_->GetObservers()) | |
93 observer.GattServicesDiscovered(adapter_.get(), &device_); | |
94 } | |
95 | |
96 void SimulateDeviceChanged() { | |
97 for (auto& observer : adapter_->GetObservers()) | |
98 observer.DeviceChanged(adapter_.get(), &device_); | |
99 } | |
100 | |
101 void GetServicesCheckForPrecedingCalls( | |
102 Call expected, | |
103 size_t expected_service_count, | |
104 int num_of_preceding_calls, | |
105 std::vector<mojom::ServiceInfoPtr> services) { | |
106 EXPECT_EQ(num_of_preceding_calls, callback_count_); | |
107 ++callback_count_; | |
108 | |
109 if (expected == Call::EXPECTED) | |
110 ++actual_success_callback_calls_; | |
111 | |
112 EXPECT_EQ(expected_service_count, services.size()); | |
113 } | |
114 | |
115 Device::GetServicesCallback GetGetServicesCheckForPrecedingCalls( | |
116 Call expected, | |
117 int num_of_preceding_calls) { | |
118 if (expected == Call::EXPECTED) | |
119 ++expected_success_callback_calls_; | |
120 | |
121 return base::Bind( | |
122 &BluetoothInterfaceDeviceTest::GetServicesCheckForPrecedingCalls, | |
123 weak_factory_.GetWeakPtr(), expected, 2 /* expected_service_count */, | |
124 num_of_preceding_calls); | |
125 } | |
126 | |
127 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | |
128 NiceMockBluetoothDevice device_; | |
129 base::MessageLoop message_loop_; | |
130 mojom::DevicePtr proxy_; | |
131 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; | |
132 | |
133 bool message_pipe_closed_ = false; | |
134 bool expect_device_service_deleted_ = false; | |
135 int expected_success_callback_calls_ = 0; | |
136 int actual_success_callback_calls_ = 0; | |
137 int callback_count_ = 0; | |
138 | |
139 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; | |
140 }; | |
141 } // namespace | |
142 | |
143 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { | |
144 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
145 .WillRepeatedly(Return(true)); | |
146 | |
147 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
148 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | |
149 | |
150 base::RunLoop().RunUntilIdle(); | |
151 } | |
152 | |
153 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { | |
154 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
155 .WillOnce(Return(false)) | |
156 .WillOnce(Return(false)) | |
157 .WillRepeatedly(Return(true)); | |
158 | |
159 // Client: Sends multiple requests for services. | |
160 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
161 Call::EXPECTED, 0 /* num_of_preceding_calls */)); | |
162 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
163 Call::EXPECTED, 1 /* num_of_preceding_calls */)); | |
164 | |
165 base::RunLoop().RunUntilIdle(); | |
166 | |
167 SimulateGattServicesDiscovered(); | |
168 | |
169 // No more GetServices calls will complete. | |
170 SimulateGattServicesDiscovered(); | |
ortuno
2016/11/17 03:54:45
You probably want to run the loop first before you
mbrunson
2016/11/17 19:33:33
Done.
| |
171 | |
172 // Client: Sends more requests which run immediately. | |
173 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
174 Call::EXPECTED, 2 /* num_of_preceding_calls */)); | |
175 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
176 Call::EXPECTED, 3 /* num_of_preceding_calls */)); | |
177 | |
178 base::RunLoop().RunUntilIdle(); | |
179 | |
180 // No more GetServices calls will complete. | |
181 SimulateGattServicesDiscovered(); | |
182 | |
183 // Wait for message pipe to process error. | |
184 base::RunLoop().RunUntilIdle(); | |
185 } | |
186 | |
187 TEST_F(BluetoothInterfaceDeviceTest, | |
188 GetServicesLostConnectionWithPendingRequests) { | |
189 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
190 .WillRepeatedly(Return(false)); | |
191 // Client: Sends multiple requests for services. | |
192 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
193 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | |
194 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
195 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
196 EXPECT_EQ(0, callback_count_); | |
197 | |
198 // Simulate connection loss. | |
199 device_.SetConnected(false); | |
200 SimulateDeviceChanged(); | |
201 expect_device_service_deleted_ = true; | |
202 | |
203 // Wait for message pipe to process error. | |
204 base::RunLoop().RunUntilIdle(); | |
205 } | |
206 | |
207 TEST_F(BluetoothInterfaceDeviceTest, | |
ortuno
2016/11/17 03:54:45
Do you think it makes sense to have a test for whe
mbrunson
2016/11/17 19:33:33
I think it's unnecessary. What would this test be
| |
208 GetServicesForcedDisconnectionWithPendingRequests) { | |
209 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
210 .WillRepeatedly(Return(false)); | |
211 | |
212 // Client: Sends multiple requests for services. | |
213 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
214 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */)); | |
215 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
216 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */)); | |
217 EXPECT_EQ(0, callback_count_); | |
218 | |
219 // Simulate connection loss. | |
220 proxy_->Disconnect(); | |
221 expect_device_service_deleted_ = true; | |
222 | |
223 // Wait for message pipe to process error. | |
224 base::RunLoop().RunUntilIdle(); | |
225 } | |
226 | |
227 } // namespace bluetooth | |
OLD | NEW |