Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: device/bluetooth/device_unittest.cc

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Merge upstream, fix issues Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 NiceMockBluetoothAdapter = testing::NiceMock<device::MockBluetoothAdapter>;
26 NiceMockBluetoothDevice = testing::NiceMock<device::MockBluetoothDevice>;
27 NiceMockBluetoothGattService =
28 testing::NiceMock<device::MockBluetoothGattService>;
29 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 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 auto service1(new NiceMockBluetoothGattService(
58 device_.get(), kTestServiceId0,
59 device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */,
60 false /* is_local */));
61 auto service2(new NiceMockBluetoothGattService(
62 device_.get(), kTestServiceId1,
63 device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */,
64 false /* is_local */));
65
66 device_->AddMockService(std::move(service1));
67 device_->AddMockService(std::move(service2));
68
69 EXPECT_CALL(*device_, GetGattServices())
70 .WillRepeatedly(Invoke(device_.get(),
71 &device::MockBluetoothDevice::GetMockServices));
72
73 std::unique_ptr<NiceMockBluetoothGattConnection> connection(
dcheng 2016/11/15 07:24:02 auto connection = base::MakeUnique<NiceMockBluetoo
mbrunson 2016/11/16 03:32:03 Done.
74 new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress()));
75
76 // Owns itself.
77 device_service_ =
78 new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy_));
79 }
80
81 void TearDown() override {
82 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_);
83
84 if (!expect_device_service_deleted_) {
85 delete device_service_;
dcheng 2016/11/15 07:24:02 With a strong binding, this can just close the mes
mbrunson 2016/11/16 03:32:03 Done.
86 }
87 }
88
89 protected:
90 void GetServicesCheckForPrecedingCalls(
91 Call expected,
92 size_t expected_service_count,
93 int num_of_preceding_calls,
94 DeviceConnectionHelper* helper,
95 const base::Closure& continuation,
96 std::vector<mojom::ServiceInfoPtr> services) {
97 helper->SetCallbackRan(true);
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_service_count, 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(
116 &BluetoothInterfaceDeviceTest::GetServicesCheckForPrecedingCalls,
117 weak_factory_.GetWeakPtr(), expected, 2 /* expected_service_count */,
118 num_of_preceding_calls, base::Owned(new DeviceConnectionHelper(
119 device_service_->GetBindingForTesting())),
120 continuation);
121 }
122
123 scoped_refptr<NiceMockBluetoothAdapter> adapter_;
124 std::unique_ptr<NiceMockBluetoothDevice> device_;
125 Device* device_service_;
126 std::unique_ptr<base::MessageLoop> message_loop_;
127 mojom::DevicePtr proxy_;
128
129 bool expect_device_service_deleted_ = false;
130 int expected_success_callback_calls_ = 0;
131 int actual_success_callback_calls_ = 0;
132 int callback_count_ = 0;
133
134 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_;
135 };
136 } // namespace
137
138 TEST_F(BluetoothInterfaceDeviceTest, GetServices) {
139 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
140 .WillRepeatedly(Return(true));
141
142 base::RunLoop loop;
143 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
144 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure()));
145
146 loop.Run();
147 }
148
149 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) {
150 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
151 .WillOnce(Return(false))
152 .WillOnce(Return(false))
153 .WillRepeatedly(Return(true));
154
155 base::RunLoop loop;
156
157 // Client: Sends multiple requests for services.
158 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
159 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure()));
160 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
161 Call::EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure()));
162
163 // Simulate: GattServicesDiscovered.
164 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
165
166 // No more GetServices calls will complete.
167 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
168
169 // Client: Sends more requests which run immediately.
170 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
171 Call::EXPECTED, 2 /* num_of_preceding_calls */, loop.QuitClosure()));
172 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
173 Call::EXPECTED, 3 /* num_of_preceding_calls */, loop.QuitClosure()));
174
175 // No more GetServices calls will complete.
176 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
177
178 loop.Run();
179 }
180
181 TEST_F(BluetoothInterfaceDeviceTest,
182 GetServicesLostConnectionWithPendingRequests) {
183 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
184 .WillRepeatedly(Return(false));
185 // Client: Sends multiple requests for services.
186 base::RunLoop loop;
187 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
188 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure()));
189 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
190 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure()));
191 EXPECT_EQ(0, callback_count_);
192
193 // Simulate connection loss.
194 device_->SetConnected(false);
195 device_service_->DeviceChanged(nullptr /* adapter */, device_.get());
196 expect_device_service_deleted_ = true;
197 }
198
199 TEST_F(BluetoothInterfaceDeviceTest,
200 GetServicesForcedDisconnectionWithPendingRequests) {
201 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
202 .WillRepeatedly(Return(false));
203 // Client: Sends multiple requests for services.
204 base::RunLoop loop;
205 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
206 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure()));
207 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
208 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure()));
209 EXPECT_EQ(0, callback_count_);
210
211 // Simulate connection loss.
212 device_service_->Disconnect();
213 expect_device_service_deleted_ = true;
214 }
215
216 TEST_F(BluetoothInterfaceDeviceTest, GetServicesPipeClosedWithPendingRequests) {
217 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
218 .WillRepeatedly(Return(false));
219 // Client: Sends multiple requests for services.
220 base::RunLoop loop;
221 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
222 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure()));
223 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls(
224 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure()));
225 EXPECT_EQ(0, callback_count_);
226
227 // Simulate message pipe error.
228 proxy_.reset();
229 expect_device_service_deleted_ = true;
230 }
231
232 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698