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

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

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Remove binding variable in Device.Create 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
« no previous file with comments | « device/bluetooth/device.cc ('k') | device/bluetooth/public/interfaces/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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();
171
172 base::RunLoop().RunUntilIdle();
173
174 // Client: Sends more requests which run immediately.
175 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
176 Call::EXPECTED, 2 /* num_of_preceding_calls */));
177 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
178 Call::EXPECTED, 3 /* num_of_preceding_calls */));
179
180 base::RunLoop().RunUntilIdle();
181
182 // No more GetServices calls will complete.
183 SimulateGattServicesDiscovered();
184
185 // Wait for message pipe to process error.
186 base::RunLoop().RunUntilIdle();
187 }
188
189 TEST_F(BluetoothInterfaceDeviceTest,
190 GetServicesLostConnectionWithPendingRequests) {
191 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete())
192 .WillRepeatedly(Return(false));
193 // Client: Sends multiple requests for services.
194 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
195 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */));
196 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
197 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */));
198 EXPECT_EQ(0, callback_count_);
199
200 // Simulate connection loss.
201 device_.SetConnected(false);
202 SimulateDeviceChanged();
203 expect_device_service_deleted_ = true;
204
205 // Wait for message pipe to process error.
206 base::RunLoop().RunUntilIdle();
207 }
208
209 TEST_F(BluetoothInterfaceDeviceTest,
210 GetServicesForcedDisconnectionWithPendingRequests) {
211 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete())
212 .WillRepeatedly(Return(false));
213
214 // Client: Sends multiple requests for services.
215 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
216 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */));
217 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls(
218 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */));
219 EXPECT_EQ(0, callback_count_);
220
221 // Simulate connection loss.
222 proxy_->Disconnect();
223 expect_device_service_deleted_ = true;
224
225 // Wait for message pipe to process error.
226 base::RunLoop().RunUntilIdle();
227 }
228
229 } // namespace bluetooth
OLDNEW
« no previous file with comments | « device/bluetooth/device.cc ('k') | device/bluetooth/public/interfaces/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698