1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <base/functional/bind.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "avrcp_internal.h"
22 #include "avrcp_test_helper.h"
23 #include "connection_handler.h"
24 #include "sdpdefs.h"
25 #include "types/raw_address.h"
26
27 using ::testing::_;
28 using ::testing::DoAll;
29 using ::testing::MockFunction;
30 using ::testing::NiceMock;
31 using ::testing::Return;
32 using ::testing::SaveArg;
33 using ::testing::SaveArgPointee;
34 using ::testing::SetArgPointee;
35 using ::testing::StrictMock;
36
btif_av_peer_is_connected_sink(const RawAddress & peer_address)37 bool btif_av_peer_is_connected_sink(const RawAddress& peer_address) {
38 return true;
39 }
btif_av_peer_is_connected_source(const RawAddress & peer_address)40 bool btif_av_peer_is_connected_source(const RawAddress& peer_address) {
41 return false;
42 }
btif_av_both_enable(void)43 bool btif_av_both_enable(void) { return false; }
44
45 namespace bluetooth {
46 namespace avrcp {
47
48 using device_ptr = std::shared_ptr<Device>;
49
50 class AvrcpConnectionHandlerTest : public testing::Test {
51 public:
SetUp()52 void SetUp() override {
53 ON_CALL(mock_avrcp_, Close(_)).WillByDefault(Return(0));
54 }
55
SetUpSdp(tAVRC_FIND_CBACK * sdp_cb,bool browsing,bool absolute_volume)56 void SetUpSdp(tAVRC_FIND_CBACK* sdp_cb, bool browsing, bool absolute_volume) {
57 EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
58 .Times(1)
59 .WillOnce(DoAll(SaveArg<3>(sdp_cb), Return(0)));
60
61 static tSDP_DISC_ATTR fake_features;
62
63 fake_features = {
64 .p_next_attr = nullptr,
65 .attr_id = 0,
66 .attr_len_type = (UINT_DESC_TYPE<<12) | 2,
67 .attr_value = {.v = {.u16 = 0}},
68 };
69
70 if (browsing) fake_features.attr_value.v.u16 |= AVRC_SUPF_CT_BROWSE;
71 if (absolute_volume) fake_features.attr_value.v.u16 |= AVRC_SUPF_CT_CAT2;
72
73 EXPECT_CALL(mock_sdp_, FindAttributeInRec(_, _))
74 .Times(4)
75 .WillRepeatedly(Return(&fake_features));
76
77 EXPECT_CALL(mock_sdp_, FindServiceInDb(_, _, _))
78 .Times(2)
79 .WillOnce(Return((tSDP_DISC_REC*)0x01)) // Return any non null pointer
80 .WillOnce(Return((tSDP_DISC_REC*)0x01)); // Return any non null pointer
81
82 EXPECT_CALL(mock_sdp_, FindProfileVersionInRec(_, _, _))
83 .Times(2)
84 .WillRepeatedly(DoAll(SetArgPointee<2>(AVRC_REV_1_6), Return(true)));
85 }
86
87 protected:
88 ConnectionHandler* connection_handler_ = nullptr;
89
90 // We use NiceMock's here because each function of this code does quite a few
91 // operations. This way it is much easier to write a higher number of smaller
92 // tests without having a large amount of warnings.
93 NiceMock<MockFunction<void(device_ptr)>> device_cb;
94 NiceMock<MockAvrcpInterface> mock_avrcp_;
95 NiceMock<MockSdpInterface> mock_sdp_;
96 NiceMock<MockVolumeInterface> mock_volume_;
97 };
98
TEST_F(AvrcpConnectionHandlerTest,initializeTest)99 TEST_F(AvrcpConnectionHandlerTest, initializeTest) {
100 // Set an Expectation that Open will be called as an acceptor and save the
101 // connection callback once it is called
102 tAVRC_CONN_CB conn_cb;
103 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
104 .Times(1)
105 .WillOnce(
106 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));
107
108 auto bound_callback = base::BindRepeating(
109 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
110 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
111 &mock_sdp_, &mock_volume_));
112 connection_handler_ = ConnectionHandler::Get();
113
114 // Check that the callback was sent with us as the acceptor
115 ASSERT_EQ(conn_cb.conn, 1);
116
117 connection_handler_ = nullptr;
118 ConnectionHandler::CleanUp();
119 }
120
121 // Check that disconnecting without an active connection
TEST_F(AvrcpConnectionHandlerTest,notConnectedDisconnectTest)122 TEST_F(AvrcpConnectionHandlerTest, notConnectedDisconnectTest) {
123 // Set an Expectation that Open will be called twice as an acceptor and save
124 // the connection callback once it is called.
125 tAVRC_CONN_CB conn_cb;
126 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
127 .Times(1)
128 .WillOnce(
129 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));
130
131 // Initialize the interface
132 auto bound_callback = base::BindRepeating(
133 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
134 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
135 &mock_sdp_, &mock_volume_));
136 connection_handler_ = ConnectionHandler::Get();
137
138 // Call the callback with a message saying the connection has closed
139 conn_cb.ctrl_cback.Run(1, AVRC_CLOSE_IND_EVT, 0, &RawAddress::kAny);
140
141 connection_handler_ = nullptr;
142 ConnectionHandler::CleanUp();
143 };
144
145 // Test calling the connection callback after the handler is cleaned up
TEST_F(AvrcpConnectionHandlerTest,disconnectAfterCleanupTest)146 TEST_F(AvrcpConnectionHandlerTest, disconnectAfterCleanupTest) {
147 // Set an Expectation that Open will be called twice as an acceptor and save
148 // the connection callback once it is called.
149 tAVRC_CONN_CB conn_cb;
150 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
151 .Times(1)
152 .WillOnce(
153 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));
154
155 // Initialize the interface
156 auto bound_callback = base::BindRepeating(
157 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
158 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
159 &mock_sdp_, &mock_volume_));
160 connection_handler_ = ConnectionHandler::Get();
161
162 connection_handler_ = nullptr;
163 ConnectionHandler::CleanUp();
164
165 // Call the callback with a message saying the connection has closed
166 conn_cb.ctrl_cback.Run(1, AVRC_CLOSE_IND_EVT, 0, &RawAddress::kAny);
167 };
168
169 /**
170 * Check that we can handle having a remote device connect to us, start SDP, and
171 * open another acceptor connection.
172 */
TEST_F(AvrcpConnectionHandlerTest,remoteDeviceConnectionTest)173 TEST_F(AvrcpConnectionHandlerTest, remoteDeviceConnectionTest) {
174 // Set an Expectation that Open will be called twice as an acceptor and save
175 // the connection callback once it is called.
176 tAVRC_CONN_CB conn_cb;
177 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
178 .Times(2)
179 .WillOnce(
180 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
181 .WillOnce(
182 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));
183
184 // Initialize the interface
185 auto bound_callback = base::BindRepeating(
186 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
187 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
188 &mock_sdp_, &mock_volume_));
189 connection_handler_ = ConnectionHandler::Get();
190
191 // Check that the callback was sent with us as the acceptor
192 ASSERT_EQ(conn_cb.conn, 1);
193
194 // Set an Expectations that SDP will be performed
195 tAVRC_FIND_CBACK sdp_cb;
196 SetUpSdp(&sdp_cb, false, false);
197
198 // Set an expectation that a device will be created
199 EXPECT_CALL(device_cb, Call(_)).Times(1);
200
201 // Set an Expectation that OpenBrowse will be called in acceptor mode when the
202 // device connects.
203 EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_ACP)).Times(1);
204
205 // Call the callback with a message saying that a remote device has connected
206 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
207
208 // Run the SDP callback with status success
209 sdp_cb.Run(0);
210
211 connection_handler_ = nullptr;
212 ConnectionHandler::CleanUp();
213 }
214
215 /**
216 * Check that when a device does not support absolute volume, that the
217 * handler reports that via the volume interface.
218 */
TEST_F(AvrcpConnectionHandlerTest,noAbsoluteVolumeTest)219 TEST_F(AvrcpConnectionHandlerTest, noAbsoluteVolumeTest) {
220 // Set an Expectation that Open will be called twice as an acceptor and save
221 // the connection callback once it is called.
222 tAVRC_CONN_CB conn_cb;
223 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
224 .Times(2)
225 .WillOnce(
226 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
227 .WillOnce(
228 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));
229
230 // Initialize the interface
231 auto bound_callback = base::BindRepeating(
232 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
233 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
234 &mock_sdp_, &mock_volume_));
235 connection_handler_ = ConnectionHandler::Get();
236
237 // Set an Expectations that SDP will be performed
238 tAVRC_FIND_CBACK sdp_cb;
239 SetUpSdp(&sdp_cb, false, false);
240
241 EXPECT_CALL(mock_volume_, DeviceConnected(RawAddress::kAny)).Times(1);
242
243 // Call the callback with a message saying that a remote device has connected
244 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
245
246 // Run the SDP callback with status success
247 sdp_cb.Run(0);
248
249 connection_handler_ = nullptr;
250 ConnectionHandler::CleanUp();
251 }
252
253 /**
254 * Check that when a device does support absolute volume, that the handler
255 * doesn't report it. Instead that will be left up to the device.
256 */
TEST_F(AvrcpConnectionHandlerTest,absoluteVolumeTest)257 TEST_F(AvrcpConnectionHandlerTest, absoluteVolumeTest) {
258 // Set an Expectation that Open will be called twice as an acceptor and save
259 // the connection callback once it is called.
260 tAVRC_CONN_CB conn_cb;
261 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
262 .Times(2)
263 .WillOnce(
264 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
265 .WillOnce(
266 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));
267
268 // Initialize the interface
269 auto bound_callback = base::BindRepeating(
270 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
271
272 StrictMock<MockVolumeInterface> strict_volume;
273 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
274 &mock_sdp_, &strict_volume));
275 connection_handler_ = ConnectionHandler::Get();
276
277 // Set an Expectations that SDP will be performed with absolute volume
278 // supported
279 tAVRC_FIND_CBACK sdp_cb;
280 SetUpSdp(&sdp_cb, false, true);
281
282 // Call the callback with a message saying that a remote device has connected
283 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
284
285 // Run the SDP callback with status success
286 sdp_cb.Run(0);
287
288 connection_handler_ = nullptr;
289 ConnectionHandler::CleanUp();
290 }
291
TEST_F(AvrcpConnectionHandlerTest,disconnectTest)292 TEST_F(AvrcpConnectionHandlerTest, disconnectTest) {
293 // Set an Expectation that Open will be called twice as an acceptor and save
294 // the connection callback once it is called.
295 tAVRC_CONN_CB conn_cb;
296 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
297 .Times(2)
298 .WillOnce(
299 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
300 .WillOnce(DoAll(SetArgPointee<0>(2), Return(0)));
301
302 // Initialize the interface
303 auto bound_callback = base::BindRepeating(
304 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
305 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
306 &mock_sdp_, &mock_volume_));
307 connection_handler_ = ConnectionHandler::Get();
308
309 // Call the callback with a message saying that a remote device has connected
310 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
311
312 // Set up the expectation that Close will be called
313 EXPECT_CALL(mock_avrcp_, Close(1)).Times(1);
314
315 // Call the callback with a message saying that a remote device has
316 // disconnected
317 conn_cb.ctrl_cback.Run(1, AVRC_CLOSE_IND_EVT, 0, &RawAddress::kAny);
318
319 connection_handler_ = nullptr;
320 ConnectionHandler::CleanUp();
321 }
322
323 /**
324 * Check that we can handle having a remote device connect to us, start SDP, and
325 * open another acceptor connection.
326 */
TEST_F(AvrcpConnectionHandlerTest,multipleRemoteDeviceConnectionTest)327 TEST_F(AvrcpConnectionHandlerTest, multipleRemoteDeviceConnectionTest) {
328 // Set an Expectation that Open will be called three times as an acceptor and
329 // save the connection callback once it is called.
330 tAVRC_CONN_CB conn_cb;
331 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
332 .Times(3)
333 .WillOnce(
334 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
335 .WillOnce(
336 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)))
337 .WillOnce(
338 DoAll(SetArgPointee<0>(3), SaveArgPointee<1>(&conn_cb), Return(0)));
339
340 // Initialize the interface
341 auto bound_callback = base::BindRepeating(
342 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
343 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
344 &mock_sdp_, &mock_volume_));
345 connection_handler_ = ConnectionHandler::Get();
346
347 // Check that the callback was sent with us as the acceptor
348 ASSERT_EQ(conn_cb.conn, 1);
349
350 // Set an Expectations that SDP will be performed
351 tAVRC_FIND_CBACK sdp_cb;
352 SetUpSdp(&sdp_cb, false, false);
353
354 // Set an expectation that a device will be created
355 EXPECT_CALL(device_cb, Call(_)).Times(1);
356
357 // Set an Expectation that OpenBrowse will be called in acceptor mode when the
358 // device connects on handle 1
359 EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_ACP)).Times(1);
360
361 // Call the callback with a message saying that a remote device has connected
362 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
363
364 // Run the SDP callback with status success
365 sdp_cb.Run(0);
366
367 // Set an Expectations that SDP will be performed again
368 SetUpSdp(&sdp_cb, false, false);
369
370 // Set an expectation that a device will be created again
371 EXPECT_CALL(device_cb, Call(_)).Times(1);
372
373 // Set an Expectation that OpenBrowse will be called in acceptor mode when the
374 // device connects on handle 2
375 EXPECT_CALL(mock_avrcp_, OpenBrowse(2, AVCT_ACP)).Times(1);
376
377 // Call the callback with a message saying that a remote device has connected
378 // with a different address
379 conn_cb.ctrl_cback.Run(2, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);
380
381 // Run the SDP callback with status success
382 sdp_cb.Run(0);
383
384 connection_handler_ = nullptr;
385 ConnectionHandler::CleanUp();
386 }
387
TEST_F(AvrcpConnectionHandlerTest,cleanupTest)388 TEST_F(AvrcpConnectionHandlerTest, cleanupTest) {
389 // Set Up Expectations for Initialize
390 tAVRC_CONN_CB conn_cb;
391 EXPECT_CALL(mock_avrcp_, Open(_, _, _))
392 .WillOnce(
393 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
394 .WillOnce(
395 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)))
396 .WillOnce(
397 DoAll(SetArgPointee<0>(3), SaveArgPointee<1>(&conn_cb), Return(0)));
398
399 // Initialize the interface
400 auto bound_callback = base::BindRepeating(
401 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
402 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
403 &mock_sdp_, &mock_volume_));
404 connection_handler_ = ConnectionHandler::Get();
405
406 // Call the callback twice with a message saying that a remote device has
407 // connected
408 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
409 conn_cb.ctrl_cback.Run(2, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);
410
411 // Set an Expectation that Close will be called twice with handles 1 and 2
412 EXPECT_CALL(mock_avrcp_, Close(1));
413 EXPECT_CALL(mock_avrcp_, Close(2));
414
415 // Cleanup the object causing all open connections to be closed
416 connection_handler_ = nullptr;
417 ConnectionHandler::CleanUp();
418 }
419
TEST_F(AvrcpConnectionHandlerTest,connectToRemoteDeviceTest)420 TEST_F(AvrcpConnectionHandlerTest, connectToRemoteDeviceTest) {
421 // Initialize the interface
422 auto bound_callback = base::BindRepeating(
423 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
424 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
425 &mock_sdp_, &mock_volume_));
426 connection_handler_ = ConnectionHandler::Get();
427
428 // Set an Expectation that SDP will be performed
429 tAVRC_FIND_CBACK sdp_cb;
430 SetUpSdp(&sdp_cb, false, false);
431
432 // Connect to the device which starts SDP
433 connection_handler_->ConnectDevice(RawAddress::kEmpty);
434
435 // Set an expectation that the handler will try to open an AVRCP connection
436 // after doing SDP
437 tAVRC_CONN_CB conn_cb;
438 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kEmpty))
439 .Times(1)
440 .WillOnce(
441 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));
442
443 // Complete SDP
444 sdp_cb.Run(0);
445
446 // Check that the callback was sent with us as the initiator
447 ASSERT_EQ(conn_cb.conn, 0);
448
449 // Set an expectation that a device will be created
450 EXPECT_CALL(device_cb, Call(_)).Times(1);
451
452 // Set an Expectation that OpenBrowse will NOT be called since the SDP entry
453 // didn't list browsing as a feature
454 EXPECT_CALL(mock_avrcp_, OpenBrowse(_, _)).Times(0);
455
456 // Call the callback with a message saying that a remote device has connected
457 // with a different address
458 conn_cb.ctrl_cback.Run(2, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);
459
460 // Cleanup the object causing all open connections to be closed
461 connection_handler_ = nullptr;
462 ConnectionHandler::CleanUp();
463 }
464
TEST_F(AvrcpConnectionHandlerTest,connectToBrowsableRemoteDeviceTest)465 TEST_F(AvrcpConnectionHandlerTest, connectToBrowsableRemoteDeviceTest) {
466 // Initialize the interface
467 auto bound_callback = base::BindRepeating(
468 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
469 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
470 &mock_sdp_, &mock_volume_));
471 connection_handler_ = ConnectionHandler::Get();
472
473 // Set an Expectation that SDP will be performed
474 tAVRC_FIND_CBACK sdp_cb;
475 SetUpSdp(&sdp_cb, true, false);
476
477 // Connect to the device which starts SDP
478 connection_handler_->ConnectDevice(RawAddress::kEmpty);
479
480 // Set an expectation that the handler will try to open an AVRCP connection
481 // after doing SDP
482 tAVRC_CONN_CB conn_cb;
483 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kEmpty))
484 .Times(1)
485 .WillOnce(
486 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)));
487
488 // Complete SDP
489 sdp_cb.Run(0);
490
491 // Check that the callback was sent with us as the initiator
492 ASSERT_EQ(conn_cb.conn, 0);
493
494 // Set an expectation that a device will be created
495 EXPECT_CALL(device_cb, Call(_)).Times(1);
496
497 // Set an Expectation that OpenBrowse will be called since browsing is listed
498 // as supported in SDP
499 EXPECT_CALL(mock_avrcp_, OpenBrowse(1, AVCT_INT)).Times(1);
500
501 // Call the callback with a message saying that a remote device has connected
502 // with a different address
503 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kEmpty);
504
505 // Cleanup the object causing all open connections to be closed
506 connection_handler_ = nullptr;
507 ConnectionHandler::CleanUp();
508 }
509
TEST_F(AvrcpConnectionHandlerTest,disconnectWhileDoingSdpTest)510 TEST_F(AvrcpConnectionHandlerTest, disconnectWhileDoingSdpTest) {
511 // Set an Expectation that Open will be called twice as an acceptor and save
512 // the connection callback once it is called.
513 tAVRC_CONN_CB conn_cb;
514 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
515 .Times(2)
516 .WillOnce(
517 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
518 .WillOnce(DoAll(SetArgPointee<0>(2), Return(0)));
519
520 // Initialize the interface
521 auto bound_callback = base::BindRepeating(
522 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
523 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
524 &mock_sdp_, &mock_volume_));
525 connection_handler_ = ConnectionHandler::Get();
526
527 // Set an Expectation that SDP will be performed
528 tAVRC_FIND_CBACK sdp_cb;
529 SetUpSdp(&sdp_cb, true, false);
530
531 // Call the callback with a message saying that a remote device has connected
532 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
533
534 // Call the callback with a message saying that a remote device has
535 // disconnected
536 conn_cb.ctrl_cback.Run(1, AVRC_CLOSE_IND_EVT, 0, &RawAddress::kAny);
537
538 // Signal that SDP has completed
539 sdp_cb.Run(0);
540
541 connection_handler_ = nullptr;
542 ConnectionHandler::CleanUp();
543 }
544
545 /**
546 * Check that when an incoming connection happens at the same time as the
547 * that the SDP search for initiator is running the collision is handled.
548 */
TEST_F(AvrcpConnectionHandlerTest,connectionCollisionTest)549 TEST_F(AvrcpConnectionHandlerTest, connectionCollisionTest) {
550 tAVRC_CONN_CB conn_cb;
551 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
552 .Times(2)
553 .WillOnce(
554 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
555 .WillOnce(
556 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));
557
558 // Initialize the interface
559 auto bound_callback = base::BindRepeating(
560 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
561 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
562 &mock_sdp_, &mock_volume_));
563 connection_handler_ = ConnectionHandler::Get();
564
565 // Check that the callback was sent with us as the acceptor
566 ASSERT_EQ(conn_cb.conn, 1);
567
568 // Set an Expectations that SDP will be performed
569 tAVRC_FIND_CBACK sdp_cb;
570 SetUpSdp(&sdp_cb, false, false);
571
572 connection_handler_->ConnectDevice(RawAddress::kAny);
573
574 // Set an expectation that a device will be created
575 EXPECT_CALL(device_cb, Call(_)).Times(1);
576
577 // Set an Expectations that SDP search will be performed again but will fail
578 EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
579 .Times(1)
580 .WillOnce(DoAll(SaveArg<3>(&sdp_cb), Return(1)));
581
582 // Set an expectation that the incoming connection will be closed
583 EXPECT_CALL(mock_avrcp_, Close(1));
584
585 // Call the callback with a message saying that a remote device has connected
586 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
587
588 // Set an expectation that cleanup will close the last connection
589 EXPECT_CALL(mock_avrcp_, Close(_));
590
591 // Run the SDP callback with status success
592 sdp_cb.Run(0);
593
594 connection_handler_ = nullptr;
595 ConnectionHandler::CleanUp();
596 }
597
598 /**
599 * Check that we are not proceeding with the connection if the SDP search
600 * failed.
601 */
TEST_F(AvrcpConnectionHandlerTest,acceptorSdpSearchFailTest)602 TEST_F(AvrcpConnectionHandlerTest, acceptorSdpSearchFailTest) {
603 // Set an Expectation that Open will be called twice as an acceptor and
604 // save the connection callback once it is called.
605 tAVRC_CONN_CB conn_cb;
606 EXPECT_CALL(mock_avrcp_, Open(_, _, RawAddress::kAny))
607 .Times(2)
608 .WillOnce(
609 DoAll(SetArgPointee<0>(1), SaveArgPointee<1>(&conn_cb), Return(0)))
610 .WillOnce(
611 DoAll(SetArgPointee<0>(2), SaveArgPointee<1>(&conn_cb), Return(0)));
612
613 // Initialize the interface
614 auto bound_callback = base::BindRepeating(
615 &MockFunction<void(device_ptr)>::Call, base::Unretained(&device_cb));
616 ASSERT_TRUE(ConnectionHandler::Initialize(bound_callback, &mock_avrcp_,
617 &mock_sdp_, &mock_volume_));
618 connection_handler_ = ConnectionHandler::Get();
619
620 // Check that the callback was sent with us as the acceptor
621 ASSERT_EQ(conn_cb.conn, 1);
622
623 // Set an expectation that a device will be created
624 EXPECT_CALL(device_cb, Call(_)).Times(1);
625
626 // Set an expectation that SDP search will be performed but will fail
627 tAVRC_FIND_CBACK sdp_cb;
628 EXPECT_CALL(mock_avrcp_, FindService(_, _, _, _))
629 .Times(1)
630 .WillOnce(DoAll(SaveArg<3>(&sdp_cb), Return(1)));
631
632 // Set an expectation that the incoming connection will be closed
633 EXPECT_CALL(mock_avrcp_, Close(1));
634
635 // Call the callback with a message saying that a remote device has connected
636 conn_cb.ctrl_cback.Run(1, AVRC_OPEN_IND_EVT, 0, &RawAddress::kAny);
637
638 // Set an expectation that cleanup will close the last connection
639 EXPECT_CALL(mock_avrcp_, Close(_));
640
641 connection_handler_ = nullptr;
642 ConnectionHandler::CleanUp();
643 }
644
645 } // namespace avrcp
646 } // namespace bluetooth
647