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