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 <base/threading/thread.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <algorithm>
23 #include <iostream>
24 
25 #include "avrcp_packet.h"
26 #include "avrcp_test_helper.h"
27 #include "device.h"
28 #include "internal_include/stack_config.h"
29 #include "tests/avrcp/avrcp_test_packets.h"
30 #include "tests/packet_test_helper.h"
31 #include "types/raw_address.h"
32 
33 // TODO(b/369381361) Enfore -Wmissing-prototypes
34 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
35 
btif_av_src_sink_coexist_enabled(void)36 bool btif_av_src_sink_coexist_enabled(void) { return true; }
37 
38 namespace bluetooth {
39 namespace avrcp {
40 
41 // TODO (apanicke): All the tests below are just basic positive unit tests.
42 // Add more tests to increase code coverage.
43 
44 using AvrcpResponse = std::unique_ptr<::bluetooth::PacketBuilder>;
45 using TestAvrcpPacket = TestPacketType<Packet>;
46 using TestBrowsePacket = TestPacketType<BrowsePacket>;
47 
48 using ::testing::_;
49 using ::testing::Mock;
50 using ::testing::MockFunction;
51 using ::testing::NiceMock;
52 using ::testing::Return;
53 using ::testing::SaveArg;
54 
get_pts_avrcp_test(void)55 bool get_pts_avrcp_test(void) { return false; }
56 
57 const stack_config_t interface = {get_pts_avrcp_test,
58                                   nullptr,
59                                   nullptr,
60                                   nullptr,
61                                   nullptr,
62                                   nullptr,
63                                   nullptr,
64                                   nullptr,
65                                   nullptr,
66                                   nullptr,
67                                   nullptr,
68                                   nullptr,
69                                   nullptr,
70                                   nullptr,
71                                   nullptr,
72                                   nullptr,
73                                   nullptr,
74                                   nullptr,
75                                   nullptr,
76                                   nullptr,
77                                   nullptr,
78                                   nullptr};
79 
80 // TODO (apanicke): All the tests below are just basic positive unit tests.
81 // Add more tests to increase code coverage.
82 class AvrcpDeviceTest : public ::testing::Test {
83 public:
SetUp()84   void SetUp() override {
85     // NOTE: We use a wrapper lambda for the MockFunction in order to
86     // add a const qualifier to the response. Otherwise the MockFunction
87     // type doesn't match the callback type and a compiler error occurs.
88     base::RepeatingCallback<void(uint8_t, bool, AvrcpResponse)> cb =
89             base::BindRepeating([](MockFunction<void(uint8_t, bool, const AvrcpResponse&)>* a,
90                                    uint8_t b, bool c, AvrcpResponse d) { a->Call(b, c, d); },
91                                 &response_cb);
92 
93     // TODO (apanicke): Test setting avrc13 to false once we have full
94     // functionality.
95     test_device = new Device(RawAddress::kAny, true, cb, 0xFFFF, 0xFFFF);
96   }
97 
TearDown()98   void TearDown() override {
99     delete test_device;
100     Mock::VerifyAndClear(&response_cb);
101   }
102 
SendMessage(uint8_t label,std::shared_ptr<Packet> message)103   void SendMessage(uint8_t label, std::shared_ptr<Packet> message) {
104     test_device->MessageReceived(label, message);
105   }
106 
SendBrowseMessage(uint8_t label,std::shared_ptr<BrowsePacket> message)107   void SendBrowseMessage(uint8_t label, std::shared_ptr<BrowsePacket> message) {
108     test_device->BrowseMessageReceived(label, message);
109   }
110 
SetBipClientStatus(bool connected)111   void SetBipClientStatus(bool connected) { test_device->SetBipClientStatus(connected); }
112 
FilterCoverArt(SongInfo & s)113   void FilterCoverArt(SongInfo& s) {
114     for (auto it = s.attributes.begin(); it != s.attributes.end(); it++) {
115       if (it->attribute() == Attribute::DEFAULT_COVER_ART) {
116         s.attributes.erase(it);
117         break;
118       }
119     }
120   }
121 
122   MockFunction<void(uint8_t, bool, const AvrcpResponse&)> response_cb;
123   Device* test_device;
124 };
125 
TEST_F(AvrcpDeviceTest,addressTest)126 TEST_F(AvrcpDeviceTest, addressTest) {
127   base::RepeatingCallback<void(uint8_t, bool, AvrcpResponse)> cb =
128           base::BindRepeating([](MockFunction<void(uint8_t, bool, const AvrcpResponse&)>* a,
129                                  uint8_t b, bool c, AvrcpResponse d) { a->Call(b, c, d); },
130                               &response_cb);
131 
132   Device device(RawAddress::kAny, true, cb, 0xFFFF, 0xFFFF);
133   ASSERT_EQ(device.GetAddress(), RawAddress::kAny);
134 }
135 
TEST_F(AvrcpDeviceTest,setBipClientStatusTest)136 TEST_F(AvrcpDeviceTest, setBipClientStatusTest) {
137   ASSERT_EQ(test_device->HasBipClient(), false);
138   SetBipClientStatus(true);
139   ASSERT_EQ(test_device->HasBipClient(), true);
140   SetBipClientStatus(false);
141   ASSERT_EQ(test_device->HasBipClient(), false);
142 }
143 
TEST_F(AvrcpDeviceTest,trackChangedTest)144 TEST_F(AvrcpDeviceTest, trackChangedTest) {
145   MockMediaInterface interface;
146   NiceMock<MockA2dpInterface> a2dp_interface;
147 
148   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
149 
150   SongInfo info = {"test_id",
151                    {// The attribute map
152                     AttributeEntry(Attribute::TITLE, "Test Song"),
153                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
154                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
155                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
156                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
157                     AttributeEntry(Attribute::GENRE, "Test Genre"),
158                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
159                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
160   std::vector<SongInfo> list = {info};
161 
162   EXPECT_CALL(interface, GetNowPlayingList(_))
163           .Times(2)
164           .WillRepeatedly(InvokeCb<0>("test_id", list));
165 
166   // Test the interim response for track changed
167   auto interim_response = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(true, 0x01);
168   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
169 
170   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::TRACK_CHANGED, 0);
171   auto pkt = TestAvrcpPacket::Make();
172   request->Serialize(pkt);
173   SendMessage(1, pkt);
174 
175   // Test the changed response for track changed
176   auto changed_response = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(false, 0x01);
177   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
178 
179   test_device->HandleTrackUpdate();
180 }
181 
TEST_F(AvrcpDeviceTest,playerSettingsChangedTest)182 TEST_F(AvrcpDeviceTest, playerSettingsChangedTest) {
183   MockMediaInterface interface;
184   NiceMock<MockA2dpInterface> a2dp_interface;
185   MockPlayerSettingsInterface player_settings_interface;
186   std::vector<PlayerAttribute> attributes = {PlayerAttribute::REPEAT, PlayerAttribute::SHUFFLE};
187   std::vector<uint8_t> attributes_values = {static_cast<uint8_t>(PlayerRepeatValue::OFF),
188                                             static_cast<uint8_t>(PlayerShuffleValue::ALL)};
189 
190   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
191 
192   EXPECT_CALL(player_settings_interface, GetCurrentPlayerSettingValue(_, _))
193           .Times(1)
194           .WillRepeatedly(InvokeCb<1>(attributes, attributes_values));
195 
196   // Test the interim response for player settings changed
197   auto interim_response = RegisterNotificationResponseBuilder::MakePlayerSettingChangedBuilder(
198           true, attributes, attributes_values);
199   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
200 
201   auto request = RegisterNotificationRequestBuilder::MakeBuilder(
202           Event::PLAYER_APPLICATION_SETTING_CHANGED, 0);
203   auto pkt = TestAvrcpPacket::Make();
204   request->Serialize(pkt);
205   SendMessage(1, pkt);
206 
207   // Test the changed response for player settings changed
208   auto changed_response = RegisterNotificationResponseBuilder::MakePlayerSettingChangedBuilder(
209           false, attributes, attributes_values);
210   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
211 
212   test_device->HandlePlayerSettingChanged(attributes, attributes_values);
213 }
214 
TEST_F(AvrcpDeviceTest,playerSettingsChangedNotSupportedTest)215 TEST_F(AvrcpDeviceTest, playerSettingsChangedNotSupportedTest) {
216   MockMediaInterface interface;
217   NiceMock<MockA2dpInterface> a2dp_interface;
218 
219   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
220 
221   auto response =
222           RejectBuilder::MakeBuilder(CommandPdu::REGISTER_NOTIFICATION, Status::INVALID_COMMAND);
223   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(response)))).Times(1);
224 
225   auto request = RegisterNotificationRequestBuilder::MakeBuilder(
226           Event::PLAYER_APPLICATION_SETTING_CHANGED, 0);
227   auto pkt = TestAvrcpPacket::Make();
228   request->Serialize(pkt);
229   SendMessage(1, pkt);
230 }
231 
TEST_F(AvrcpDeviceTest,playStatusTest)232 TEST_F(AvrcpDeviceTest, playStatusTest) {
233   MockMediaInterface interface;
234   NiceMock<MockA2dpInterface> a2dp_interface;
235 
236   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
237 
238   PlayStatus status1 = {0x1234, 0x5678, PlayState::PLAYING};
239   PlayStatus status2 = {0x1234, 0x5678, PlayState::STOPPED};
240 
241   EXPECT_CALL(interface, GetPlayStatus(_))
242           .Times(2)
243           .WillOnce(InvokeCb<0>(status1))
244           .WillOnce(InvokeCb<0>(status2));
245 
246   // Pretend the device is active
247   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
248 
249   // Test the interim response for play status changed
250   auto interim_response =
251           RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(true, PlayState::PLAYING);
252   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
253 
254   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::PLAYBACK_STATUS_CHANGED, 0);
255   auto pkt = TestAvrcpPacket::Make();
256   request->Serialize(pkt);
257   SendMessage(1, pkt);
258 
259   // Test the changed response for play status changed
260   auto changed_response =
261           RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(false, PlayState::STOPPED);
262   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
263   test_device->HandlePlayStatusUpdate();
264 }
265 
TEST_F(AvrcpDeviceTest,playPositionTest)266 TEST_F(AvrcpDeviceTest, playPositionTest) {
267   MockMediaInterface interface;
268   NiceMock<MockA2dpInterface> a2dp_interface;
269 
270   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
271 
272   // TODO (apanicke): Add an underlying message loop so we can test the playing
273   // state.
274   PlayStatus status1 = {0x1234, 0x5678, PlayState::PAUSED};
275   PlayStatus status2 = {0x5678, 0x9ABC, PlayState::STOPPED};
276 
277   EXPECT_CALL(interface, GetPlayStatus(_))
278           .Times(2)
279           .WillOnce(InvokeCb<0>(status1))
280           .WillOnce(InvokeCb<0>(status2));
281 
282   // Pretend the device is active
283   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
284 
285   // Test the interim response for play position changed
286   auto interim_response =
287           RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(true, 0x1234);
288   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
289 
290   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::PLAYBACK_POS_CHANGED, 0);
291   auto pkt = TestAvrcpPacket::Make();
292   request->Serialize(pkt);
293   SendMessage(1, pkt);
294 
295   // Test the changed response for play position changed
296   auto changed_response =
297           RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(false, 0x5678);
298   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
299   test_device->HandlePlayPosUpdate();
300 }
301 
TEST_F(AvrcpDeviceTest,trackChangedBeforeInterimTest)302 TEST_F(AvrcpDeviceTest, trackChangedBeforeInterimTest) {
303   MockMediaInterface interface;
304   NiceMock<MockA2dpInterface> a2dp_interface;
305 
306   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
307 
308   // Pretend the device is active
309   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
310 
311   SongInfo info = {"test_id",
312                    {// The attribute map
313                     AttributeEntry(Attribute::TITLE, "Test Song"),
314                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
315                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
316                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
317                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
318                     AttributeEntry(Attribute::GENRE, "Test Genre"),
319                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
320                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
321   std::vector<SongInfo> list = {info};
322 
323   MediaInterface::NowPlayingCallback interim_cb;
324   MediaInterface::NowPlayingCallback changed_cb;
325 
326   EXPECT_CALL(interface, GetNowPlayingList(_))
327           .Times(2)
328           .WillOnce(SaveArg<0>(&interim_cb))
329           .WillOnce(SaveArg<0>(&changed_cb));
330 
331   // Test that the changed response doesn't get sent before the interim
332   ::testing::InSequence s;
333   auto interim_response = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(true, 0x01);
334   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
335   auto changed_response = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(false, 0x01);
336   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
337 
338   // Register for the update, sets interim_cb
339   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::TRACK_CHANGED, 0);
340   auto pkt = TestAvrcpPacket::Make();
341   request->Serialize(pkt);
342   SendMessage(1, pkt);
343 
344   // Try to send track changed update, should fail and do nothing
345   test_device->HandleTrackUpdate();
346 
347   // Send the interim response
348   interim_cb.Run("test_id", list);
349 
350   // Try to send track changed update, should succeed
351   test_device->HandleTrackUpdate();
352   changed_cb.Run("test_id", list);
353 }
354 
TEST_F(AvrcpDeviceTest,playStatusChangedBeforeInterimTest)355 TEST_F(AvrcpDeviceTest, playStatusChangedBeforeInterimTest) {
356   MockMediaInterface interface;
357   NiceMock<MockA2dpInterface> a2dp_interface;
358 
359   // Pretend the device is active
360   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
361 
362   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
363 
364   MediaInterface::PlayStatusCallback interim_cb;
365   MediaInterface::PlayStatusCallback changed_cb;
366 
367   EXPECT_CALL(interface, GetPlayStatus(_))
368           .Times(2)
369           .WillOnce(SaveArg<0>(&interim_cb))
370           .WillOnce(SaveArg<0>(&changed_cb));
371 
372   // Test that the changed response doesn't get sent before the interim
373   ::testing::InSequence s;
374   auto interim_response =
375           RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(true, PlayState::PLAYING);
376   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
377   auto changed_response =
378           RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(false, PlayState::STOPPED);
379   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
380 
381   // Send the registration packet
382   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::PLAYBACK_STATUS_CHANGED, 0);
383   auto pkt = TestAvrcpPacket::Make();
384   request->Serialize(pkt);
385   SendMessage(1, pkt);
386 
387   // Send a play status update, should be ignored since the interim response
388   // hasn't been sent yet.
389   test_device->HandlePlayStatusUpdate();
390 
391   // Send the interim response.
392   PlayStatus status1 = {0x1234, 0x5678, PlayState::PLAYING};
393   interim_cb.Run(status1);
394 
395   // Send the changed response, should succeed this time
396   test_device->HandlePlayStatusUpdate();
397   PlayStatus status2 = {0x1234, 0x5678, PlayState::STOPPED};
398   changed_cb.Run(status2);
399 }
400 
TEST_F(AvrcpDeviceTest,playPositionChangedBeforeInterimTest)401 TEST_F(AvrcpDeviceTest, playPositionChangedBeforeInterimTest) {
402   MockMediaInterface interface;
403   NiceMock<MockA2dpInterface> a2dp_interface;
404 
405   // Pretend the device is active
406   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
407 
408   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
409 
410   MediaInterface::PlayStatusCallback interim_cb;
411   MediaInterface::PlayStatusCallback changed_cb;
412 
413   EXPECT_CALL(interface, GetPlayStatus(_))
414           .Times(2)
415           .WillOnce(SaveArg<0>(&interim_cb))
416           .WillOnce(SaveArg<0>(&changed_cb));
417 
418   // Test that the changed response doesn't get sent before the interim
419   ::testing::InSequence s;
420   auto interim_response =
421           RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(true, 0x1234);
422   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
423   auto changed_response =
424           RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(false, 0x5678);
425   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
426 
427   // Send the registration packet
428   auto request = RegisterNotificationRequestBuilder::MakeBuilder(Event::PLAYBACK_POS_CHANGED, 0);
429   auto pkt = TestAvrcpPacket::Make();
430   request->Serialize(pkt);
431   SendMessage(1, pkt);
432 
433   // Send a play position update, should be ignored since the notification
434   // isn't registered since no interim response has been sent.
435   test_device->HandlePlayPosUpdate();
436 
437   // Run the interim callback for GetPlayStatus which should be pointing to the
438   // GetPlayStatus call made by the update.
439   PlayStatus status1 = {0x1234, 0x5678, PlayState::PAUSED};
440   interim_cb.Run(status1);
441 
442   // Send a play position update, this one should succeed.
443   test_device->HandlePlayPosUpdate();
444   PlayStatus status2 = {0x5678, 0x9ABC, PlayState::STOPPED};
445   changed_cb.Run(status2);
446 }
447 
TEST_F(AvrcpDeviceTest,nowPlayingChangedBeforeInterim)448 TEST_F(AvrcpDeviceTest, nowPlayingChangedBeforeInterim) {
449   MockMediaInterface interface;
450   NiceMock<MockA2dpInterface> a2dp_interface;
451 
452   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
453 
454   SongInfo info = {"test_id",
455                    {// The attribute map
456                     AttributeEntry(Attribute::TITLE, "Test Song"),
457                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
458                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
459                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
460                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
461                     AttributeEntry(Attribute::GENRE, "Test Genre"),
462                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
463                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
464   std::vector<SongInfo> list = {info};
465 
466   MediaInterface::NowPlayingCallback interim_cb;
467   MediaInterface::NowPlayingCallback changed_cb;
468 
469   EXPECT_CALL(interface, GetNowPlayingList(_))
470           .Times(2)
471           .WillOnce(SaveArg<0>(&interim_cb))
472           .WillOnce(SaveArg<0>(&changed_cb));
473 
474   // Test that the changed response doesn't get sent before the interim
475   ::testing::InSequence s;
476   auto interim_response = RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(true);
477   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
478   auto changed_response = RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(false);
479   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
480 
481   // Send the registration packet
482   auto request =
483           RegisterNotificationRequestBuilder::MakeBuilder(Event::NOW_PLAYING_CONTENT_CHANGED, 0);
484   auto pkt = TestAvrcpPacket::Make();
485   request->Serialize(pkt);
486   SendMessage(1, pkt);
487 
488   // Send now playing changed, should fail since the interim response hasn't
489   // been sent
490   test_device->HandleNowPlayingUpdate();
491 
492   // Send the data needed for the interim response
493   interim_cb.Run("test_id", list);
494 
495   // Send now playing changed, should succeed
496   test_device->HandleNowPlayingUpdate();
497   changed_cb.Run("test_id", list);
498 }
499 
TEST_F(AvrcpDeviceTest,addressPlayerChangedBeforeInterim)500 TEST_F(AvrcpDeviceTest, addressPlayerChangedBeforeInterim) {
501   MockMediaInterface interface;
502   NiceMock<MockA2dpInterface> a2dp_interface;
503 
504   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
505 
506   MediaInterface::GetAddressedPlayerCallback interim_cb;
507   MediaInterface::GetAddressedPlayerCallback changed_cb;
508 
509   EXPECT_CALL(interface, GetAddressedPlayer(_))
510           .Times(2)
511           .WillOnce(SaveArg<0>(&interim_cb))
512           .WillOnce(SaveArg<0>(&changed_cb));
513 
514   // Test that the changed response doesn't get sent before the interim
515   ::testing::InSequence s;
516   auto interim_response =
517           RegisterNotificationResponseBuilder::MakeAddressedPlayerBuilder(true, 0, 0);
518   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
519   auto changed_response =
520           RegisterNotificationResponseBuilder::MakeAddressedPlayerBuilder(false, 0, 0);
521   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
522   // TODO (apanicke): Remove this expectation once b/110957802 is fixed and
523   // we don't try to reject notifications that aren't registered.
524   auto rejected_response = RejectBuilder::MakeBuilder(CommandPdu::REGISTER_NOTIFICATION,
525                                                       Status::ADDRESSED_PLAYER_CHANGED);
526   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(rejected_response)))).Times(4);
527 
528   // Send the registration packet
529   auto request =
530           RegisterNotificationRequestBuilder::MakeBuilder(Event::ADDRESSED_PLAYER_CHANGED, 0);
531   auto pkt = TestAvrcpPacket::Make();
532   request->Serialize(pkt);
533   SendMessage(1, pkt);
534 
535   // Send addressed player update, should fail since the interim response
536   // hasn't been sent
537   test_device->HandleAddressedPlayerUpdate();
538 
539   // Send the data needed for the interim response
540   MediaPlayerInfo info = {0, "Test Player", true};
541   std::vector<MediaPlayerInfo> list = {info};
542   interim_cb.Run(0);
543 
544   // Send addressed player update, should succeed
545   test_device->HandleAddressedPlayerUpdate();
546   changed_cb.Run(0);
547 }
548 
TEST_F(AvrcpDeviceTest,nowPlayingTest)549 TEST_F(AvrcpDeviceTest, nowPlayingTest) {
550   MockMediaInterface interface;
551   NiceMock<MockA2dpInterface> a2dp_interface;
552 
553   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
554 
555   SongInfo info = {"test_id",
556                    {// The attribute map
557                     AttributeEntry(Attribute::TITLE, "Test Song"),
558                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
559                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
560                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
561                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
562                     AttributeEntry(Attribute::GENRE, "Test Genre"),
563                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
564                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
565   std::vector<SongInfo> list = {info};
566   EXPECT_CALL(interface, GetNowPlayingList(_))
567           .Times(2)
568           .WillRepeatedly(InvokeCb<0>("test_id", list));
569 
570   // Test the interim response for now playing list changed
571   auto interim_response = RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(true);
572   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(interim_response)))).Times(1);
573 
574   auto request =
575           RegisterNotificationRequestBuilder::MakeBuilder(Event::NOW_PLAYING_CONTENT_CHANGED, 0);
576   auto pkt = TestAvrcpPacket::Make();
577   request->Serialize(pkt);
578   SendMessage(1, pkt);
579 
580   // Test the changed response for now playing list changed
581   auto changed_response = RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(false);
582   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(changed_response)))).Times(1);
583   test_device->HandleNowPlayingUpdate();
584 }
585 
TEST_F(AvrcpDeviceTest,getPlayStatusTest)586 TEST_F(AvrcpDeviceTest, getPlayStatusTest) {
587   MockMediaInterface interface;
588   NiceMock<MockA2dpInterface> a2dp_interface;
589 
590   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
591 
592   PlayStatus status = {0x1234, 0x5678, PlayState::PLAYING};
593 
594   EXPECT_CALL(interface, GetPlayStatus(_)).Times(1).WillOnce(InvokeCb<0>(status));
595 
596   // Pretend the device is active
597   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
598 
599   auto expected_response =
600           GetPlayStatusResponseBuilder::MakeBuilder(0x5678, 0x1234, PlayState::PLAYING);
601   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(expected_response)))).Times(1);
602 
603   auto request = TestAvrcpPacket::Make(get_play_status_request);
604   SendMessage(1, request);
605 }
606 
TEST_F(AvrcpDeviceTest,getElementAttributesTest)607 TEST_F(AvrcpDeviceTest, getElementAttributesTest) {
608   MockMediaInterface interface;
609   NiceMock<MockA2dpInterface> a2dp_interface;
610 
611   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
612 
613   SongInfo info = {"test_id",
614                    {// The attribute map
615                     AttributeEntry(Attribute::TITLE, "Test Song"),
616                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
617                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
618                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
619                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
620                     AttributeEntry(Attribute::GENRE, "Test Genre"),
621                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
622                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
623 
624   EXPECT_CALL(interface, GetSongInfo(_)).WillRepeatedly(InvokeCb<0>(info));
625 
626   auto compare_to_partial = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
627   compare_to_partial->AddAttributeEntry(Attribute::TITLE, "Test Song");
628   EXPECT_CALL(response_cb, Call(2, false, matchPacket(std::move(compare_to_partial)))).Times(1);
629   SendMessage(2, TestAvrcpPacket::Make(get_element_attributes_request_partial));
630 
631   auto compare_to_full = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
632   compare_to_full->AddAttributeEntry(Attribute::TITLE, "Test Song");
633   compare_to_full->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
634   compare_to_full->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
635   compare_to_full->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
636   compare_to_full->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
637   compare_to_full->AddAttributeEntry(Attribute::GENRE, "Test Genre");
638   compare_to_full->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
639   EXPECT_CALL(response_cb, Call(3, false, matchPacket(std::move(compare_to_full)))).Times(1);
640   SendMessage(3, TestAvrcpPacket::Make(get_element_attributes_request_full));
641 }
642 
TEST_F(AvrcpDeviceTest,getElementAttributesWithCoverArtTest)643 TEST_F(AvrcpDeviceTest, getElementAttributesWithCoverArtTest) {
644   MockMediaInterface interface;
645   NiceMock<MockA2dpInterface> a2dp_interface;
646 
647   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
648 
649   SongInfo info = {"test_id",
650                    {// The attribute map
651                     AttributeEntry(Attribute::TITLE, "Test Song"),
652                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
653                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
654                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
655                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
656                     AttributeEntry(Attribute::GENRE, "Test Genre"),
657                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
658                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
659 
660   EXPECT_CALL(interface, GetSongInfo(_)).WillRepeatedly(InvokeCb<0>(info));
661   SetBipClientStatus(false);
662 
663   auto compare_to_no_art = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
664   compare_to_no_art->AddAttributeEntry(Attribute::TITLE, "Test Song");
665   compare_to_no_art->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
666   compare_to_no_art->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
667   compare_to_no_art->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
668   compare_to_no_art->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
669   compare_to_no_art->AddAttributeEntry(Attribute::GENRE, "Test Genre");
670   compare_to_no_art->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
671   EXPECT_CALL(response_cb, Call(3, false, matchPacket(std::move(compare_to_no_art)))).Times(1);
672   SendMessage(3, TestAvrcpPacket::Make(get_element_attributes_request_full_cover_art));
673 
674   SetBipClientStatus(true);
675 
676   auto compare_to_full = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
677   compare_to_full->AddAttributeEntry(Attribute::TITLE, "Test Song");
678   compare_to_full->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
679   compare_to_full->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
680   compare_to_full->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
681   compare_to_full->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
682   compare_to_full->AddAttributeEntry(Attribute::GENRE, "Test Genre");
683   compare_to_full->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
684   compare_to_full->AddAttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001");
685   EXPECT_CALL(response_cb, Call(3, false, matchPacket(std::move(compare_to_full)))).Times(1);
686   SendMessage(3, TestAvrcpPacket::Make(get_element_attributes_request_full_cover_art));
687 }
688 
TEST_F(AvrcpDeviceTest,getElementAttributesMtuTest)689 TEST_F(AvrcpDeviceTest, getElementAttributesMtuTest) {
690   auto truncated_packet = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
691   truncated_packet->AddAttributeEntry(Attribute::TITLE, "1234");
692 
693   MockMediaInterface interface;
694   NiceMock<MockA2dpInterface> a2dp_interface;
695 
696   base::RepeatingCallback<void(uint8_t, bool, AvrcpResponse)> cb =
697           base::BindRepeating([](MockFunction<void(uint8_t, bool, const AvrcpResponse&)>* a,
698                                  uint8_t b, bool c, AvrcpResponse d) { a->Call(b, c, d); },
699                               &response_cb);
700   Device device(RawAddress::kAny, true, cb, truncated_packet->size(), 0xFFFF);
701 
702   device.RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
703 
704   SongInfo info = {"test_id", {AttributeEntry(Attribute::TITLE, "1234truncated")}};
705   EXPECT_CALL(interface, GetSongInfo(_)).WillRepeatedly(InvokeCb<0>(info));
706 
707   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(truncated_packet)))).Times(1);
708 
709   device.MessageReceived(1, TestAvrcpPacket::Make(get_element_attributes_request_full));
710 }
711 
TEST_F(AvrcpDeviceTest,getTotalNumberOfItemsMediaPlayersTest)712 TEST_F(AvrcpDeviceTest, getTotalNumberOfItemsMediaPlayersTest) {
713   MockMediaInterface interface;
714   NiceMock<MockA2dpInterface> a2dp_interface;
715 
716   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
717 
718   std::vector<MediaPlayerInfo> player_list = {
719           {0, "player1", true},
720           {1, "player2", true},
721           {2, "player3", true},
722   };
723 
724   EXPECT_CALL(interface, GetMediaPlayerList(_)).Times(1).WillOnce(InvokeCb<0>(0, player_list));
725 
726   auto expected_response = GetTotalNumberOfItemsResponseBuilder::MakeBuilder(Status::NO_ERROR, 0,
727                                                                              player_list.size());
728   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
729 
730   SendBrowseMessage(1, TestBrowsePacket::Make(get_total_number_of_items_request_media_players));
731 }
732 
TEST_F(AvrcpDeviceTest,getTotalNumberOfItemsVFSTest)733 TEST_F(AvrcpDeviceTest, getTotalNumberOfItemsVFSTest) {
734   MockMediaInterface interface;
735   NiceMock<MockA2dpInterface> a2dp_interface;
736 
737   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
738 
739   std::vector<ListItem> vfs_list = {
740           {ListItem::FOLDER, {"id1", true, "folder1"}, SongInfo()},
741           {ListItem::FOLDER, {"id2", true, "folder2"}, SongInfo()},
742   };
743 
744   EXPECT_CALL(interface, GetFolderItems(_, "", _)).Times(1).WillOnce(InvokeCb<2>(vfs_list));
745 
746   auto expected_response =
747           GetTotalNumberOfItemsResponseBuilder::MakeBuilder(Status::NO_AVAILABLE_PLAYERS, 0, 0);
748   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
749 
750   SendBrowseMessage(1, TestBrowsePacket::Make(get_total_number_of_items_request_vfs));
751 }
752 
TEST_F(AvrcpDeviceTest,getTotalNumberOfItemsNowPlayingTest)753 TEST_F(AvrcpDeviceTest, getTotalNumberOfItemsNowPlayingTest) {
754   MockMediaInterface interface;
755   NiceMock<MockA2dpInterface> a2dp_interface;
756 
757   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
758 
759   std::vector<SongInfo> now_playing_list = {
760           {"test_id1", {}}, {"test_id2", {}}, {"test_id3", {}}, {"test_id4", {}}, {"test_id5", {}},
761   };
762 
763   EXPECT_CALL(interface, GetNowPlayingList(_))
764           .WillRepeatedly(InvokeCb<0>("test_id1", now_playing_list));
765 
766   auto expected_response =
767           GetTotalNumberOfItemsResponseBuilder::MakeBuilder(Status::NO_AVAILABLE_PLAYERS, 0, 0);
768   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
769 
770   SendBrowseMessage(1, TestBrowsePacket::Make(get_total_number_of_items_request_now_playing));
771 }
772 
TEST_F(AvrcpDeviceTest,getMediaPlayerListTest)773 TEST_F(AvrcpDeviceTest, getMediaPlayerListTest) {
774   MockMediaInterface interface;
775   NiceMock<MockA2dpInterface> a2dp_interface;
776 
777   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
778 
779   MediaPlayerInfo info = {0, "Test Player", true};
780   std::vector<MediaPlayerInfo> list = {info};
781 
782   EXPECT_CALL(interface, GetMediaPlayerList(_)).Times(1).WillOnce(InvokeCb<0>(0, list));
783 
784   auto expected_response =
785           GetFolderItemsResponseBuilder::MakePlayerListBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
786   expected_response->AddMediaPlayer(MediaPlayerItem(0, "Test Player", true));
787   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
788 
789   auto request = TestBrowsePacket::Make(get_folder_items_request);
790   SendBrowseMessage(1, request);
791 }
792 
TEST_F(AvrcpDeviceTest,getNowPlayingListTest)793 TEST_F(AvrcpDeviceTest, getNowPlayingListTest) {
794   MockMediaInterface interface;
795   NiceMock<MockA2dpInterface> a2dp_interface;
796 
797   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
798   SetBipClientStatus(false);
799 
800   SongInfo info = {"test_id",
801                    {// The attribute map
802                     AttributeEntry(Attribute::TITLE, "Test Song"),
803                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
804                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
805                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
806                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
807                     AttributeEntry(Attribute::GENRE, "Test Genre"),
808                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
809                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
810   std::vector<SongInfo> list = {info};
811 
812   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
813 
814   FilterCoverArt(info);
815   auto expected_response =
816           GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
817   expected_response->AddSong(MediaElementItem(1, "Test Song", info.attributes));
818   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
819   auto request = TestBrowsePacket::Make(get_folder_items_request_now_playing);
820   SendBrowseMessage(1, request);
821 }
822 
TEST_F(AvrcpDeviceTest,getNowPlayingListWithCoverArtTest)823 TEST_F(AvrcpDeviceTest, getNowPlayingListWithCoverArtTest) {
824   MockMediaInterface interface;
825   NiceMock<MockA2dpInterface> a2dp_interface;
826 
827   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
828   SetBipClientStatus(true);
829 
830   SongInfo info = {"test_id",
831                    {// The attribute map
832                     AttributeEntry(Attribute::TITLE, "Test Song"),
833                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
834                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
835                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
836                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
837                     AttributeEntry(Attribute::GENRE, "Test Genre"),
838                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
839                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
840   std::vector<SongInfo> list = {info};
841 
842   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
843 
844   auto expected_response =
845           GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
846   expected_response->AddSong(MediaElementItem(1, "Test Song", info.attributes));
847 
848   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
849   auto request = TestBrowsePacket::Make(get_folder_items_request_now_playing);
850   SendBrowseMessage(1, request);
851 }
852 
TEST_F(AvrcpDeviceTest,getVFSFolderTest)853 TEST_F(AvrcpDeviceTest, getVFSFolderTest) {
854   MockMediaInterface interface;
855   NiceMock<MockA2dpInterface> a2dp_interface;
856 
857   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
858 
859   FolderInfo info = {"test_id", true, "Test Folder"};
860   ListItem item = {ListItem::FOLDER, info, SongInfo()};
861   std::vector<ListItem> list = {item};
862 
863   EXPECT_CALL(interface, GetFolderItems(_, "", _)).Times(1).WillOnce(InvokeCb<2>(list));
864 
865   auto expected_response =
866           GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
867   expected_response->AddFolder(FolderItem(1, 0, true, "Test Folder"));
868   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(expected_response)))).Times(1);
869 
870   auto request = TestBrowsePacket::Make(get_folder_items_request_vfs);
871   SendBrowseMessage(1, request);
872 }
873 
TEST_F(AvrcpDeviceTest,getFolderItemsMtuTest)874 TEST_F(AvrcpDeviceTest, getFolderItemsMtuTest) {
875   auto truncated_packet =
876           GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
877   truncated_packet->AddFolder(FolderItem(1, 0, true, "Test Folder0"));
878   truncated_packet->AddFolder(FolderItem(2, 0, true, "Test Folder1"));
879 
880   MockMediaInterface interface;
881   NiceMock<MockA2dpInterface> a2dp_interface;
882   base::RepeatingCallback<void(uint8_t, bool, AvrcpResponse)> cb =
883           base::BindRepeating([](MockFunction<void(uint8_t, bool, const AvrcpResponse&)>* a,
884                                  uint8_t b, bool c, AvrcpResponse d) { a->Call(b, c, d); },
885                               &response_cb);
886 
887   Device device(RawAddress::kAny, true, cb, 0xFFFF,
888                 truncated_packet->size() + FolderItem::kHeaderSize() + 5);
889   device.RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
890 
891   FolderInfo info0 = {"test_id0", true, "Test Folder0"};
892   FolderInfo info1 = {"test_id1", true, "Test Folder1"};
893   FolderInfo info2 = {"test_id2", true, "Truncated folder"};
894   // Used to ensure that adding an item that would fit in the MTU fails if
895   // adding a large item failed.
896   FolderInfo small_info = {"test_id2", true, "Small"};
897 
898   ListItem item0 = {ListItem::FOLDER, info0, SongInfo()};
899   ListItem item1 = {ListItem::FOLDER, info1, SongInfo()};
900   ListItem item2 = {ListItem::FOLDER, info2, SongInfo()};
901   ListItem item3 = {ListItem::FOLDER, small_info, SongInfo()};
902 
903   std::vector<ListItem> list0 = {item0, item1, item2, item3};
904   EXPECT_CALL(interface, GetFolderItems(_, "", _)).WillRepeatedly(InvokeCb<2>(list0));
905 
906   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(truncated_packet)))).Times(1);
907   device.BrowseMessageReceived(1, TestBrowsePacket::Make(get_folder_items_request_vfs));
908 }
909 
TEST_F(AvrcpDeviceTest,changePathTest)910 TEST_F(AvrcpDeviceTest, changePathTest) {
911   MockMediaInterface interface;
912   NiceMock<MockA2dpInterface> a2dp_interface;
913 
914   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
915 
916   FolderInfo info0 = {"test_id0", true, "Test Folder0"};
917   FolderInfo info1 = {"test_id1", true, "Test Folder1"};
918   ListItem item0 = {ListItem::FOLDER, info0, SongInfo()};
919   ListItem item1 = {ListItem::FOLDER, info1, SongInfo()};
920   std::vector<ListItem> list0 = {item0, item1};
921   EXPECT_CALL(interface, GetFolderItems(_, "", _)).Times(1).WillRepeatedly(InvokeCb<2>(list0));
922 
923   FolderInfo info2 = {"test_id2", true, "Test Folder2"};
924   FolderInfo info3 = {"test_id3", true, "Test Folder3"};
925   FolderInfo info4 = {"test_id4", true, "Test Folder4"};
926   ListItem item2 = {ListItem::FOLDER, info2, SongInfo()};
927   ListItem item3 = {ListItem::FOLDER, info3, SongInfo()};
928   ListItem item4 = {ListItem::FOLDER, info4, SongInfo()};
929   std::vector<ListItem> list1 = {item2, item3, item4};
930   EXPECT_CALL(interface, GetFolderItems(_, "test_id1", _))
931           .Times(3)
932           .WillRepeatedly(InvokeCb<2>(list1));
933 
934   std::vector<ListItem> list2 = {};
935   EXPECT_CALL(interface, GetFolderItems(_, "test_id3", _)).Times(1).WillOnce(InvokeCb<2>(list2));
936 
937   // Populate the VFS ID map
938   auto folder_items_response =
939           GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
940   folder_items_response->AddFolder(FolderItem(1, 0, true, "Test Folder0"));
941   folder_items_response->AddFolder(FolderItem(2, 0, true, "Test Folder1"));
942   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(folder_items_response)))).Times(1);
943 
944   auto folder_request_builder = GetFolderItemsRequestBuilder::MakeBuilder(Scope::VFS, 0, 3, {});
945   auto request = TestBrowsePacket::Make();
946   folder_request_builder->Serialize(request);
947   SendBrowseMessage(1, request);
948 
949   // Change path down into Test Folder1
950   auto change_path_response =
951           ChangePathResponseBuilder::MakeBuilder(Status::NO_ERROR, list1.size());
952   EXPECT_CALL(response_cb, Call(2, true, matchPacket(std::move(change_path_response))));
953   auto path_request_builder = ChangePathRequestBuilder::MakeBuilder(0, Direction::DOWN, 2);
954   request = TestBrowsePacket::Make();
955   path_request_builder->Serialize(request);
956   SendBrowseMessage(2, request);
957 
958   // Populate the new VFS ID
959   folder_items_response =
960           GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR, 0x0000, 0xFFFF);
961   folder_items_response->AddFolder(FolderItem(3, 0, true, "Test Folder2"));
962   folder_items_response->AddFolder(FolderItem(4, 0, true, "Test Folder3"));
963   folder_items_response->AddFolder(FolderItem(5, 0, true, "Test Folder4"));
964   EXPECT_CALL(response_cb, Call(3, true, matchPacket(std::move(folder_items_response)))).Times(1);
965   folder_request_builder = GetFolderItemsRequestBuilder::MakeBuilder(Scope::VFS, 0, 3, {});
966   request = TestBrowsePacket::Make();
967   folder_request_builder->Serialize(request);
968   SendBrowseMessage(3, request);
969 
970   // Change path down into Test Folder3
971   change_path_response = ChangePathResponseBuilder::MakeBuilder(Status::NO_ERROR, list2.size());
972   EXPECT_CALL(response_cb, Call(4, true, matchPacket(std::move(change_path_response))));
973   path_request_builder = ChangePathRequestBuilder::MakeBuilder(0, Direction::DOWN, 4);
974   request = TestBrowsePacket::Make();
975   path_request_builder->Serialize(request);
976   SendBrowseMessage(4, request);
977 
978   // Change path up back into Test Folder1
979   change_path_response = ChangePathResponseBuilder::MakeBuilder(Status::NO_ERROR, list1.size());
980   EXPECT_CALL(response_cb, Call(5, true, matchPacket(std::move(change_path_response))));
981   path_request_builder = ChangePathRequestBuilder::MakeBuilder(0, Direction::UP, 0);
982   request = TestBrowsePacket::Make();
983   path_request_builder->Serialize(request);
984   SendBrowseMessage(5, request);
985 }
986 
TEST_F(AvrcpDeviceTest,getItemAttributesNowPlayingTest)987 TEST_F(AvrcpDeviceTest, getItemAttributesNowPlayingTest) {
988   MockMediaInterface interface;
989   NiceMock<MockA2dpInterface> a2dp_interface;
990 
991   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
992 
993   SongInfo info = {"test_id",
994                    {// The attribute map
995                     AttributeEntry(Attribute::TITLE, "Test Song"),
996                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
997                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
998                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
999                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
1000                     AttributeEntry(Attribute::GENRE, "Test Genre"),
1001                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
1002                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
1003   std::vector<SongInfo> list = {info};
1004 
1005   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
1006 
1007   SetBipClientStatus(false);
1008 
1009   auto compare_to_full = GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
1010   compare_to_full->AddAttributeEntry(Attribute::TITLE, "Test Song");
1011   compare_to_full->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
1012   compare_to_full->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
1013   compare_to_full->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
1014   compare_to_full->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
1015   compare_to_full->AddAttributeEntry(Attribute::GENRE, "Test Genre");
1016   compare_to_full->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
1017   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(compare_to_full)))).Times(1);
1018 
1019   auto request = TestBrowsePacket::Make(get_item_attributes_request_all_attributes);
1020   SendBrowseMessage(1, request);
1021 }
1022 
TEST_F(AvrcpDeviceTest,getItemAttributesNowPlayingWithCoverArtTest)1023 TEST_F(AvrcpDeviceTest, getItemAttributesNowPlayingWithCoverArtTest) {
1024   MockMediaInterface interface;
1025   NiceMock<MockA2dpInterface> a2dp_interface;
1026 
1027   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1028 
1029   SongInfo info = {"test_id",
1030                    {// The attribute map
1031                     AttributeEntry(Attribute::TITLE, "Test Song"),
1032                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
1033                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
1034                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
1035                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
1036                     AttributeEntry(Attribute::GENRE, "Test Genre"),
1037                     AttributeEntry(Attribute::PLAYING_TIME, "1000"),
1038                     AttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001")}};
1039   std::vector<SongInfo> list = {info};
1040 
1041   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
1042 
1043   SetBipClientStatus(true);
1044 
1045   auto compare_to_full = GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
1046   compare_to_full->AddAttributeEntry(Attribute::TITLE, "Test Song");
1047   compare_to_full->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
1048   compare_to_full->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
1049   compare_to_full->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
1050   compare_to_full->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
1051   compare_to_full->AddAttributeEntry(Attribute::GENRE, "Test Genre");
1052   compare_to_full->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
1053   compare_to_full->AddAttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001");
1054   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(compare_to_full)))).Times(1);
1055 
1056   auto requestWithBip =
1057           TestBrowsePacket::Make(get_item_attributes_request_all_attributes_with_cover_art);
1058   SendBrowseMessage(1, requestWithBip);
1059 
1060   SetBipClientStatus(false);
1061 
1062   auto compare_to_no_art = GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
1063   compare_to_no_art->AddAttributeEntry(Attribute::TITLE, "Test Song");
1064   compare_to_no_art->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
1065   compare_to_no_art->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
1066   compare_to_no_art->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
1067   compare_to_no_art->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
1068   compare_to_no_art->AddAttributeEntry(Attribute::GENRE, "Test Genre");
1069   compare_to_no_art->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
1070   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(compare_to_no_art)))).Times(1);
1071 
1072   auto requestWithoutBip =
1073           TestBrowsePacket::Make(get_item_attributes_request_all_attributes_with_cover_art);
1074   SendBrowseMessage(1, requestWithoutBip);
1075 }
1076 
TEST_F(AvrcpDeviceTest,getItemAttributesMtuTest)1077 TEST_F(AvrcpDeviceTest, getItemAttributesMtuTest) {
1078   auto truncated_packet = GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
1079   truncated_packet->AddAttributeEntry(Attribute::TITLE, "1234");
1080 
1081   MockMediaInterface interface;
1082   NiceMock<MockA2dpInterface> a2dp_interface;
1083   base::RepeatingCallback<void(uint8_t, bool, AvrcpResponse)> cb =
1084           base::BindRepeating([](MockFunction<void(uint8_t, bool, const AvrcpResponse&)>* a,
1085                                  uint8_t b, bool c, AvrcpResponse d) { a->Call(b, c, d); },
1086                               &response_cb);
1087   Device device(RawAddress::kAny, true, cb, 0xFFFF, truncated_packet->size());
1088   device.RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1089 
1090   SongInfo info = {"test_id", {AttributeEntry(Attribute::TITLE, "1234truncated")}};
1091   std::vector<SongInfo> list = {info};
1092   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
1093 
1094   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(truncated_packet)))).Times(1);
1095   device.BrowseMessageReceived(1,
1096                                TestBrowsePacket::Make(get_item_attributes_request_all_attributes));
1097 }
1098 
TEST_F(AvrcpDeviceTest,setAddressedPlayerTest)1099 TEST_F(AvrcpDeviceTest, setAddressedPlayerTest) {
1100   MockMediaInterface interface;
1101   NiceMock<MockA2dpInterface> a2dp_interface;
1102 
1103   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1104 
1105   MediaPlayerInfo info = {0, "Test Player", true};
1106   std::vector<MediaPlayerInfo> list = {info};
1107 
1108   EXPECT_CALL(interface, SetAddressedPlayer(_, _)).WillRepeatedly(InvokeCb<1>(0));
1109 
1110   auto set_addr_player_rej_rsp =
1111           RejectBuilder::MakeBuilder(CommandPdu::SET_ADDRESSED_PLAYER, Status::INVALID_PLAYER_ID);
1112 
1113   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(set_addr_player_rej_rsp))))
1114           .Times(1);
1115 
1116   auto player_id_1_request = TestAvrcpPacket::Make(set_addressed_player_id_1_request);
1117   SendMessage(1, player_id_1_request);
1118 
1119   auto set_addr_player_rsp = SetAddressedPlayerResponseBuilder::MakeBuilder(Status::NO_ERROR);
1120 
1121   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(set_addr_player_rsp)))).Times(1);
1122 
1123   auto request = TestAvrcpPacket::Make(set_addressed_player_request);
1124   SendMessage(1, request);
1125 }
1126 
TEST_F(AvrcpDeviceTest,setBrowsedPlayerTest)1127 TEST_F(AvrcpDeviceTest, setBrowsedPlayerTest) {
1128   MockMediaInterface interface;
1129   NiceMock<MockA2dpInterface> a2dp_interface;
1130 
1131   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1132 
1133   EXPECT_CALL(interface, SetBrowsedPlayer(_, _))
1134           .Times(3)
1135           .WillOnce(InvokeCb<1>(true, "", 0))
1136           .WillOnce(InvokeCb<1>(false, "", 0))
1137           .WillOnce(InvokeCb<1>(true, "", 2));
1138 
1139   auto not_browsable_rsp = SetBrowsedPlayerResponseBuilder::MakeBuilder(
1140           Status::PLAYER_NOT_BROWSABLE, 0x0000, 0, 0, "");
1141   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(not_browsable_rsp)))).Times(1);
1142 
1143   auto player_id_0_request = TestBrowsePacket::Make(set_browsed_player_id_0_request);
1144   SendBrowseMessage(1, player_id_0_request);
1145 
1146   auto invalid_id_rsp =
1147           SetBrowsedPlayerResponseBuilder::MakeBuilder(Status::INVALID_PLAYER_ID, 0x0000, 0, 0, "");
1148   EXPECT_CALL(response_cb, Call(2, true, matchPacket(std::move(invalid_id_rsp)))).Times(1);
1149 
1150   SendBrowseMessage(2, player_id_0_request);
1151 
1152   auto response = SetBrowsedPlayerResponseBuilder::MakeBuilder(Status::NO_ERROR, 0x0000, 2, 0, "");
1153   EXPECT_CALL(response_cb, Call(3, true, matchPacket(std::move(response)))).Times(1);
1154 
1155   SendBrowseMessage(3, player_id_0_request);
1156 }
1157 
TEST_F(AvrcpDeviceTest,volumeChangedTest)1158 TEST_F(AvrcpDeviceTest, volumeChangedTest) {
1159   MockMediaInterface interface;
1160   NiceMock<MockA2dpInterface> a2dp_interface;
1161   MockVolumeInterface vol_interface;
1162 
1163   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1164 
1165   // Pretend the device is active
1166   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
1167 
1168   auto reg_notif = RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
1169   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(reg_notif)))).Times(1);
1170   test_device->RegisterVolumeChanged();
1171 
1172   EXPECT_CALL(vol_interface, DeviceConnected(test_device->GetAddress(), _))
1173           .Times(1)
1174           .WillOnce(InvokeCb<1>(0x30));
1175   auto set_vol = SetAbsoluteVolumeRequestBuilder::MakeBuilder(0x30);
1176   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(set_vol)))).Times(1);
1177 
1178   auto response = TestAvrcpPacket::Make(interim_volume_changed_notification);
1179   SendMessage(1, response);
1180 
1181   EXPECT_CALL(vol_interface, SetVolume(0x47)).Times(1);
1182   auto reg_notif2 = RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
1183   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(reg_notif2)))).Times(1);
1184   response = TestAvrcpPacket::Make(changed_volume_changed_notification);
1185   SendMessage(1, response);
1186   response = TestAvrcpPacket::Make(interim_volume_changed_notification);
1187   SendMessage(1, response);
1188 }
1189 
TEST_F(AvrcpDeviceTest,volumeChangedNonActiveTest)1190 TEST_F(AvrcpDeviceTest, volumeChangedNonActiveTest) {
1191   MockMediaInterface interface;
1192   NiceMock<MockA2dpInterface> a2dp_interface;
1193   MockVolumeInterface vol_interface;
1194 
1195   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1196 
1197   // Pretend the device isn't active
1198   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(RawAddress::kEmpty));
1199 
1200   auto reg_notif = RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
1201   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(reg_notif)))).Times(1);
1202   test_device->RegisterVolumeChanged();
1203 
1204   EXPECT_CALL(vol_interface, DeviceConnected(test_device->GetAddress(), _))
1205           .Times(1)
1206           .WillOnce(InvokeCb<1>(0x30));
1207   auto set_vol = SetAbsoluteVolumeRequestBuilder::MakeBuilder(0x30);
1208   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(set_vol)))).Times(1);
1209 
1210   auto response = TestAvrcpPacket::Make(interim_volume_changed_notification);
1211   SendMessage(1, response);
1212 
1213   // Ensure that SetVolume is never called
1214   EXPECT_CALL(vol_interface, SetVolume(0x47)).Times(0);
1215 
1216   auto reg_notif2 = RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
1217   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(reg_notif2)))).Times(1);
1218   response = TestAvrcpPacket::Make(changed_volume_changed_notification);
1219   SendMessage(1, response);
1220   response = TestAvrcpPacket::Make(interim_volume_changed_notification);
1221   SendMessage(1, response);
1222 }
1223 
TEST_F(AvrcpDeviceTest,volumeRejectedTest)1224 TEST_F(AvrcpDeviceTest, volumeRejectedTest) {
1225   MockMediaInterface interface;
1226   NiceMock<MockA2dpInterface> a2dp_interface;
1227   MockVolumeInterface vol_interface;
1228 
1229   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1230 
1231   auto reg_notif = RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
1232   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(reg_notif)))).Times(1);
1233   test_device->RegisterVolumeChanged();
1234 
1235   auto response = TestAvrcpPacket::Make(rejected_volume_changed_notification);
1236   SendMessage(1, response);
1237 
1238   EXPECT_CALL(response_cb, Call(_, _, _)).Times(0);
1239 }
1240 
TEST_F(AvrcpDeviceTest,setVolumeOnceTest)1241 TEST_F(AvrcpDeviceTest, setVolumeOnceTest) {
1242   int vol = 0x48;
1243 
1244   auto set_abs_vol = SetAbsoluteVolumeRequestBuilder::MakeBuilder(vol);
1245 
1246   // Ensure that SetVolume only been call once.
1247   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(set_abs_vol)))).Times(1);
1248 
1249   test_device->SetVolume(vol);
1250   test_device->SetVolume(vol);
1251 }
1252 
TEST_F(AvrcpDeviceTest,setVolumeAfterReconnectionTest)1253 TEST_F(AvrcpDeviceTest, setVolumeAfterReconnectionTest) {
1254   int vol = 0x48;
1255 
1256   auto set_abs_vol = SetAbsoluteVolumeRequestBuilder::MakeBuilder(vol);
1257 
1258   // Ensure that SetVolume is called twice as DeviceDisconnected will
1259   // reset the previous stored volume.
1260   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(set_abs_vol)))).Times(2);
1261 
1262   test_device->SetVolume(vol);
1263   test_device->DeviceDisconnected();
1264   test_device->SetVolume(vol);
1265 }
1266 
TEST_F(AvrcpDeviceTest,playPushedActiveDeviceTest)1267 TEST_F(AvrcpDeviceTest, playPushedActiveDeviceTest) {
1268   MockMediaInterface interface;
1269   NiceMock<MockA2dpInterface> a2dp_interface;
1270   MockVolumeInterface vol_interface;
1271 
1272   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1273 
1274   // Pretend the device is active
1275   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
1276 
1277   auto play_pushed = PassThroughPacketBuilder::MakeBuilder(false, true, 0x44);
1278   auto play_pushed_response = PassThroughPacketBuilder::MakeBuilder(true, true, 0x44);
1279   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(play_pushed_response)))).Times(1);
1280 
1281   PlayStatus status = {0x1234, 0x5678, PlayState::PLAYING};
1282   EXPECT_CALL(interface, GetPlayStatus(_)).Times(1).WillOnce(InvokeCb<0>(status));
1283 
1284   EXPECT_CALL(interface, SendKeyEvent(0x44, KeyState::PUSHED)).Times(1);
1285 
1286   auto play_pushed_pkt = TestAvrcpPacket::Make();
1287   play_pushed->Serialize(play_pushed_pkt);
1288 
1289   SendMessage(1, play_pushed_pkt);
1290 }
1291 
TEST_F(AvrcpDeviceTest,playPushedInactiveDeviceTest)1292 TEST_F(AvrcpDeviceTest, playPushedInactiveDeviceTest) {
1293   MockMediaInterface interface;
1294   NiceMock<MockA2dpInterface> a2dp_interface;
1295   MockVolumeInterface vol_interface;
1296 
1297   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1298 
1299   // Pretend the device is not active
1300   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(RawAddress::kEmpty));
1301 
1302   auto play_pushed = PassThroughPacketBuilder::MakeBuilder(false, true, 0x44);
1303   auto play_pushed_response = PassThroughPacketBuilder::MakeBuilder(true, true, 0x44);
1304   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(play_pushed_response)))).Times(1);
1305 
1306   // Expect that the device will try to set itself as active
1307   EXPECT_CALL(interface, SetActiveDevice(test_device->GetAddress())).Times(1);
1308 
1309   // No play command should be sent since the music is already playing
1310   PlayStatus status = {0x1234, 0x5678, PlayState::PLAYING};
1311   EXPECT_CALL(interface, GetPlayStatus(_)).Times(1).WillOnce(InvokeCb<0>(status));
1312   EXPECT_CALL(interface, SendKeyEvent(0x44, KeyState::PUSHED)).Times(0);
1313 
1314   auto play_pushed_pkt = TestAvrcpPacket::Make();
1315   play_pushed->Serialize(play_pushed_pkt);
1316 
1317   SendMessage(1, play_pushed_pkt);
1318 }
1319 
TEST_F(AvrcpDeviceTest,mediaKeyActiveDeviceTest)1320 TEST_F(AvrcpDeviceTest, mediaKeyActiveDeviceTest) {
1321   MockMediaInterface interface;
1322   NiceMock<MockA2dpInterface> a2dp_interface;
1323   MockVolumeInterface vol_interface;
1324 
1325   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1326 
1327   // Pretend the device is active
1328   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(test_device->GetAddress()));
1329 
1330   auto play_released = PassThroughPacketBuilder::MakeBuilder(false, false, 0x44);
1331   auto play_released_response = PassThroughPacketBuilder::MakeBuilder(true, false, 0x44);
1332   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(play_released_response)))).Times(1);
1333 
1334   EXPECT_CALL(interface, GetPlayStatus(_)).Times(0);
1335 
1336   EXPECT_CALL(interface, SendKeyEvent(0x44, KeyState::RELEASED)).Times(1);
1337 
1338   auto play_released_pkt = TestAvrcpPacket::Make();
1339   play_released->Serialize(play_released_pkt);
1340 
1341   SendMessage(1, play_released_pkt);
1342 }
1343 
TEST_F(AvrcpDeviceTest,mediaKeyInactiveDeviceTest)1344 TEST_F(AvrcpDeviceTest, mediaKeyInactiveDeviceTest) {
1345   MockMediaInterface interface;
1346   NiceMock<MockA2dpInterface> a2dp_interface;
1347   MockVolumeInterface vol_interface;
1348 
1349   test_device->RegisterInterfaces(&interface, &a2dp_interface, &vol_interface, nullptr);
1350 
1351   // Pretend the device is not active
1352   EXPECT_CALL(a2dp_interface, active_peer()).WillRepeatedly(Return(RawAddress::kEmpty));
1353 
1354   auto play_released = PassThroughPacketBuilder::MakeBuilder(false, false, 0x44);
1355   auto play_released_response = PassThroughPacketBuilder::MakeBuilder(true, false, 0x44);
1356   EXPECT_CALL(response_cb, Call(_, false, matchPacket(std::move(play_released_response)))).Times(1);
1357 
1358   EXPECT_CALL(interface, GetPlayStatus(_)).Times(0);
1359 
1360   // Expect that the key event wont be sent to the media interface
1361   EXPECT_CALL(interface, SendKeyEvent(0x44, KeyState::RELEASED)).Times(0);
1362 
1363   auto play_released_pkt = TestAvrcpPacket::Make();
1364   play_released->Serialize(play_released_pkt);
1365 
1366   SendMessage(1, play_released_pkt);
1367 }
1368 
TEST_F(AvrcpDeviceTest,getCapabilitiesTest)1369 TEST_F(AvrcpDeviceTest, getCapabilitiesTest) {
1370   MockMediaInterface interface;
1371   NiceMock<MockA2dpInterface> a2dp_interface;
1372   MockPlayerSettingsInterface player_settings_interface;
1373 
1374   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1375 
1376   // GetCapabilities with CapabilityID COMPANY_ID
1377   auto request_company_id_response = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x001958);
1378   request_company_id_response->AddCompanyId(0x002345);
1379   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(request_company_id_response))))
1380           .Times(1);
1381 
1382   auto request_company_id = TestAvrcpPacket::Make(get_capabilities_request_company_id);
1383   SendMessage(1, request_company_id);
1384 
1385   // GetCapabilities with CapabilityID EVENTS_SUPPORTED
1386   auto request_events_supported_response =
1387           GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
1388                   Event::PLAYBACK_STATUS_CHANGED);
1389   request_events_supported_response->AddEvent(Event::TRACK_CHANGED);
1390   request_events_supported_response->AddEvent(Event::PLAYBACK_POS_CHANGED);
1391   request_events_supported_response->AddEvent(Event::PLAYER_APPLICATION_SETTING_CHANGED);
1392 
1393   EXPECT_CALL(response_cb,
1394               Call(2, false, matchPacket(std::move(request_events_supported_response))))
1395           .Times(1);
1396 
1397   auto request_events_supported = TestAvrcpPacket::Make(get_capabilities_request);
1398   SendMessage(2, request_events_supported);
1399 
1400   // GetCapabilities with CapabilityID UNKNOWN
1401   auto request_unknown_response =
1402           RejectBuilder::MakeBuilder(CommandPdu::GET_CAPABILITIES, Status::INVALID_PARAMETER);
1403 
1404   EXPECT_CALL(response_cb, Call(3, false, matchPacket(std::move(request_unknown_response))))
1405           .Times(1);
1406 
1407   auto request_unknown = TestAvrcpPacket::Make(get_capabilities_request_unknown);
1408   SendMessage(3, request_unknown);
1409 }
1410 
TEST_F(AvrcpDeviceTest,getCapabilitiesPlayerSettingsNotSupportedTest)1411 TEST_F(AvrcpDeviceTest, getCapabilitiesPlayerSettingsNotSupportedTest) {
1412   MockMediaInterface interface;
1413   NiceMock<MockA2dpInterface> a2dp_interface;
1414 
1415   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1416 
1417   // GetCapabilities with CapabilityID COMPANY_ID
1418   auto request_company_id_response = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x001958);
1419   request_company_id_response->AddCompanyId(0x002345);
1420   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(request_company_id_response))))
1421           .Times(1);
1422 
1423   auto request_company_id = TestAvrcpPacket::Make(get_capabilities_request_company_id);
1424   SendMessage(1, request_company_id);
1425 
1426   // GetCapabilities with CapabilityID EVENTS_SUPPORTED
1427   auto request_events_supported_response =
1428           GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
1429                   Event::PLAYBACK_STATUS_CHANGED);
1430   request_events_supported_response->AddEvent(Event::TRACK_CHANGED);
1431   request_events_supported_response->AddEvent(Event::PLAYBACK_POS_CHANGED);
1432 
1433   EXPECT_CALL(response_cb,
1434               Call(2, false, matchPacket(std::move(request_events_supported_response))))
1435           .Times(1);
1436 
1437   auto request_events_supported = TestAvrcpPacket::Make(get_capabilities_request);
1438   SendMessage(2, request_events_supported);
1439 
1440   // GetCapabilities with CapabilityID UNKNOWN
1441   auto request_unknown_response =
1442           RejectBuilder::MakeBuilder(CommandPdu::GET_CAPABILITIES, Status::INVALID_PARAMETER);
1443 
1444   EXPECT_CALL(response_cb, Call(3, false, matchPacket(std::move(request_unknown_response))))
1445           .Times(1);
1446 
1447   auto request_unknown = TestAvrcpPacket::Make(get_capabilities_request_unknown);
1448   SendMessage(3, request_unknown);
1449 }
1450 
TEST_F(AvrcpDeviceTest,getInvalidItemAttributesTest)1451 TEST_F(AvrcpDeviceTest, getInvalidItemAttributesTest) {
1452   MockMediaInterface interface;
1453   NiceMock<MockA2dpInterface> a2dp_interface;
1454 
1455   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1456 
1457   SongInfo info = {"test_id",
1458                    {// The attribute map
1459                     AttributeEntry(Attribute::TITLE, "Test Song"),
1460                     AttributeEntry(Attribute::ARTIST_NAME, "Test Artist"),
1461                     AttributeEntry(Attribute::ALBUM_NAME, "Test Album"),
1462                     AttributeEntry(Attribute::TRACK_NUMBER, "1"),
1463                     AttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2"),
1464                     AttributeEntry(Attribute::GENRE, "Test Genre"),
1465                     AttributeEntry(Attribute::PLAYING_TIME, "1000")}};
1466   std::vector<SongInfo> list = {info};
1467 
1468   EXPECT_CALL(interface, GetNowPlayingList(_)).WillRepeatedly(InvokeCb<0>("test_id", list));
1469 
1470   auto compare_to_full =
1471           GetItemAttributesResponseBuilder::MakeBuilder(Status::UIDS_CHANGED, 0xFFFF);
1472   compare_to_full->AddAttributeEntry(Attribute::TITLE, "Test Song");
1473   compare_to_full->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
1474   compare_to_full->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
1475   compare_to_full->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
1476   compare_to_full->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
1477   compare_to_full->AddAttributeEntry(Attribute::GENRE, "Test Genre");
1478   compare_to_full->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
1479   compare_to_full->AddAttributeEntry(Attribute::DEFAULT_COVER_ART, "0000001");
1480   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(compare_to_full)))).Times(1);
1481 
1482   auto request = TestBrowsePacket::Make(get_item_attributes_request_all_attributes_invalid);
1483   SendBrowseMessage(1, request);
1484 }
1485 
TEST_F(AvrcpDeviceTest,listPlayerSettingsTest)1486 TEST_F(AvrcpDeviceTest, listPlayerSettingsTest) {
1487   MockMediaInterface interface;
1488   NiceMock<MockA2dpInterface> a2dp_interface;
1489   MockPlayerSettingsInterface player_settings_interface;
1490   std::vector<PlayerAttribute> attributes = {PlayerAttribute::REPEAT, PlayerAttribute::SHUFFLE};
1491 
1492   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1493 
1494   EXPECT_CALL(player_settings_interface, ListPlayerSettings(_))
1495           .WillRepeatedly(InvokeCb<0>(attributes));
1496 
1497   auto player_settings_list_response =
1498           ListPlayerApplicationSettingAttributesResponseBuilder::MakeBuilder(attributes);
1499 
1500   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(player_settings_list_response))))
1501           .Times(1);
1502 
1503   auto request = TestAvrcpPacket::Make(list_player_application_setting_attributes_request);
1504   SendMessage(1, request);
1505 }
1506 
TEST_F(AvrcpDeviceTest,listPlayerSettingsNotSupportedTest)1507 TEST_F(AvrcpDeviceTest, listPlayerSettingsNotSupportedTest) {
1508   MockMediaInterface interface;
1509   NiceMock<MockA2dpInterface> a2dp_interface;
1510 
1511   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1512 
1513   auto response = RejectBuilder::MakeBuilder(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES,
1514                                              Status::INVALID_COMMAND);
1515 
1516   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(response)))).Times(1);
1517 
1518   auto request = TestAvrcpPacket::Make(list_player_application_setting_attributes_request);
1519   SendMessage(1, request);
1520 }
1521 
TEST_F(AvrcpDeviceTest,listPlayerSettingValuesTest)1522 TEST_F(AvrcpDeviceTest, listPlayerSettingValuesTest) {
1523   MockMediaInterface interface;
1524   NiceMock<MockA2dpInterface> a2dp_interface;
1525   MockPlayerSettingsInterface player_settings_interface;
1526   PlayerAttribute attribute = PlayerAttribute::REPEAT;
1527   std::vector<uint8_t> attribute_values = {static_cast<uint8_t>(PlayerRepeatValue::OFF),
1528                                            static_cast<uint8_t>(PlayerRepeatValue::SINGLE),
1529                                            static_cast<uint8_t>(PlayerRepeatValue::ALL),
1530                                            static_cast<uint8_t>(PlayerRepeatValue::GROUP)};
1531 
1532   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1533 
1534   EXPECT_CALL(player_settings_interface, ListPlayerSettingValues(attribute, _))
1535           .WillRepeatedly(InvokeCb<1>(attribute, attribute_values));
1536 
1537   auto player_settings_list_values_response =
1538           ListPlayerApplicationSettingValuesResponseBuilder::MakeBuilder(attribute_values);
1539 
1540   EXPECT_CALL(response_cb,
1541               Call(1, false, matchPacket(std::move(player_settings_list_values_response))))
1542           .Times(1);
1543 
1544   auto request = TestAvrcpPacket::Make(list_player_application_setting_attribute_values_request);
1545   SendMessage(1, request);
1546 }
1547 
TEST_F(AvrcpDeviceTest,listPlayerSettingValuesNotSupportedTest)1548 TEST_F(AvrcpDeviceTest, listPlayerSettingValuesNotSupportedTest) {
1549   MockMediaInterface interface;
1550   NiceMock<MockA2dpInterface> a2dp_interface;
1551 
1552   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1553 
1554   auto response = RejectBuilder::MakeBuilder(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_VALUES,
1555                                              Status::INVALID_COMMAND);
1556 
1557   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(response)))).Times(1);
1558 
1559   auto request = TestAvrcpPacket::Make(list_player_application_setting_attribute_values_request);
1560   SendMessage(1, request);
1561 }
1562 
TEST_F(AvrcpDeviceTest,invalidSettingListPlayerSettingValuesTest)1563 TEST_F(AvrcpDeviceTest, invalidSettingListPlayerSettingValuesTest) {
1564   MockMediaInterface interface;
1565   NiceMock<MockA2dpInterface> a2dp_interface;
1566   MockPlayerSettingsInterface player_settings_interface;
1567 
1568   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1569 
1570   auto rej_rsp = RejectBuilder::MakeBuilder(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_VALUES,
1571                                             Status::INVALID_PARAMETER);
1572   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1573 
1574   auto list_values_request = TestAvrcpPacket::Make(
1575           invalid_setting_list_player_application_setting_attribute_values_request);
1576   SendMessage(1, list_values_request);
1577 }
1578 
TEST_F(AvrcpDeviceTest,invalidLengthListPlayerSettingValuesTest)1579 TEST_F(AvrcpDeviceTest, invalidLengthListPlayerSettingValuesTest) {
1580   MockMediaInterface interface;
1581   NiceMock<MockA2dpInterface> a2dp_interface;
1582   MockPlayerSettingsInterface player_settings_interface;
1583 
1584   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1585 
1586   auto rej_rsp = RejectBuilder::MakeBuilder(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_VALUES,
1587                                             Status::INVALID_PARAMETER);
1588   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1589 
1590   auto list_values_request = TestAvrcpPacket::Make(
1591           invalid_length_list_player_application_setting_attribute_values_request);
1592   SendMessage(1, list_values_request);
1593 }
1594 
TEST_F(AvrcpDeviceTest,getCurrentPlayerApplicationSettingValueTest)1595 TEST_F(AvrcpDeviceTest, getCurrentPlayerApplicationSettingValueTest) {
1596   MockMediaInterface interface;
1597   NiceMock<MockA2dpInterface> a2dp_interface;
1598   MockPlayerSettingsInterface player_settings_interface;
1599   std::vector<PlayerAttribute> attributes = {PlayerAttribute::REPEAT, PlayerAttribute::SHUFFLE};
1600   std::vector<uint8_t> attributes_values = {static_cast<uint8_t>(PlayerRepeatValue::OFF),
1601                                             static_cast<uint8_t>(PlayerShuffleValue::OFF)};
1602 
1603   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1604 
1605   EXPECT_CALL(player_settings_interface, GetCurrentPlayerSettingValue(attributes, _))
1606           .WillRepeatedly(InvokeCb<1>(attributes, attributes_values));
1607 
1608   auto player_settings_get_current_values_response =
1609           GetCurrentPlayerApplicationSettingValueResponseBuilder::MakeBuilder(attributes,
1610                                                                               attributes_values);
1611 
1612   EXPECT_CALL(response_cb,
1613               Call(1, false, matchPacket(std::move(player_settings_get_current_values_response))))
1614           .Times(1);
1615 
1616   auto request = TestAvrcpPacket::Make(get_current_player_application_setting_value_request);
1617   SendMessage(1, request);
1618 }
1619 
TEST_F(AvrcpDeviceTest,getCurrentPlayerApplicationSettingValueNotSupportedTest)1620 TEST_F(AvrcpDeviceTest, getCurrentPlayerApplicationSettingValueNotSupportedTest) {
1621   MockMediaInterface interface;
1622   NiceMock<MockA2dpInterface> a2dp_interface;
1623 
1624   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1625 
1626   auto response = RejectBuilder::MakeBuilder(
1627           CommandPdu::GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, Status::INVALID_COMMAND);
1628 
1629   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(response)))).Times(1);
1630 
1631   auto request = TestAvrcpPacket::Make(get_current_player_application_setting_value_request);
1632   SendMessage(1, request);
1633 }
1634 
TEST_F(AvrcpDeviceTest,invalidSettingGetCurrentPlayerApplicationSettingValueTest)1635 TEST_F(AvrcpDeviceTest, invalidSettingGetCurrentPlayerApplicationSettingValueTest) {
1636   MockMediaInterface interface;
1637   NiceMock<MockA2dpInterface> a2dp_interface;
1638   MockPlayerSettingsInterface player_settings_interface;
1639 
1640   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1641 
1642   auto rej_rsp = RejectBuilder::MakeBuilder(
1643           CommandPdu::GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, Status::INVALID_PARAMETER);
1644   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1645 
1646   auto request = TestAvrcpPacket::Make(
1647           invalid_setting_get_current_player_application_setting_value_request);
1648   SendMessage(1, request);
1649 }
1650 
TEST_F(AvrcpDeviceTest,invalidLengthGetCurrentPlayerApplicationSettingValueTest)1651 TEST_F(AvrcpDeviceTest, invalidLengthGetCurrentPlayerApplicationSettingValueTest) {
1652   MockMediaInterface interface;
1653   NiceMock<MockA2dpInterface> a2dp_interface;
1654   MockPlayerSettingsInterface player_settings_interface;
1655 
1656   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1657 
1658   auto rej_rsp = RejectBuilder::MakeBuilder(
1659           CommandPdu::GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, Status::INVALID_PARAMETER);
1660   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1661 
1662   auto request = TestAvrcpPacket::Make(
1663           invalid_length_get_current_player_application_setting_value_request);
1664   SendMessage(1, request);
1665 }
1666 
TEST_F(AvrcpDeviceTest,setPlayerApplicationSettingValueTest)1667 TEST_F(AvrcpDeviceTest, setPlayerApplicationSettingValueTest) {
1668   MockMediaInterface interface;
1669   NiceMock<MockA2dpInterface> a2dp_interface;
1670   MockPlayerSettingsInterface player_settings_interface;
1671   std::vector<PlayerAttribute> attributes = {PlayerAttribute::REPEAT, PlayerAttribute::SHUFFLE};
1672   std::vector<uint8_t> attributes_values = {static_cast<uint8_t>(PlayerRepeatValue::OFF),
1673                                             static_cast<uint8_t>(PlayerShuffleValue::OFF)};
1674 
1675   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1676 
1677   EXPECT_CALL(player_settings_interface, SetPlayerSettings(attributes, attributes_values, _))
1678           .WillRepeatedly(InvokeCb<2>(true));
1679 
1680   auto set_player_settings_response =
1681           SetPlayerApplicationSettingValueResponseBuilder::MakeBuilder();
1682 
1683   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(set_player_settings_response))))
1684           .Times(1);
1685 
1686   auto request = TestAvrcpPacket::Make(set_player_application_setting_value_request);
1687   SendMessage(1, request);
1688 }
1689 
TEST_F(AvrcpDeviceTest,setPlayerApplicationSettingValueNotSupportedTest)1690 TEST_F(AvrcpDeviceTest, setPlayerApplicationSettingValueNotSupportedTest) {
1691   MockMediaInterface interface;
1692   NiceMock<MockA2dpInterface> a2dp_interface;
1693 
1694   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1695 
1696   auto response = RejectBuilder::MakeBuilder(CommandPdu::SET_PLAYER_APPLICATION_SETTING_VALUE,
1697                                              Status::INVALID_COMMAND);
1698 
1699   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(response)))).Times(1);
1700 
1701   auto request = TestAvrcpPacket::Make(set_player_application_setting_value_request);
1702   SendMessage(1, request);
1703 }
1704 
TEST_F(AvrcpDeviceTest,invalidSettingSetPlayerApplicationSettingValueTest)1705 TEST_F(AvrcpDeviceTest, invalidSettingSetPlayerApplicationSettingValueTest) {
1706   MockMediaInterface interface;
1707   NiceMock<MockA2dpInterface> a2dp_interface;
1708   MockPlayerSettingsInterface player_settings_interface;
1709 
1710   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1711 
1712   auto rej_rsp = RejectBuilder::MakeBuilder(CommandPdu::SET_PLAYER_APPLICATION_SETTING_VALUE,
1713                                             Status::INVALID_PARAMETER);
1714   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1715 
1716   auto request =
1717           TestAvrcpPacket::Make(invalid_setting_set_player_application_setting_value_request);
1718   SendMessage(1, request);
1719 }
1720 
TEST_F(AvrcpDeviceTest,invalidValueSetPlayerApplicationSettingValueTest)1721 TEST_F(AvrcpDeviceTest, invalidValueSetPlayerApplicationSettingValueTest) {
1722   MockMediaInterface interface;
1723   NiceMock<MockA2dpInterface> a2dp_interface;
1724   MockPlayerSettingsInterface player_settings_interface;
1725 
1726   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1727 
1728   auto rej_rsp = RejectBuilder::MakeBuilder(CommandPdu::SET_PLAYER_APPLICATION_SETTING_VALUE,
1729                                             Status::INVALID_PARAMETER);
1730   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1731 
1732   auto request = TestAvrcpPacket::Make(invalid_value_set_player_application_setting_value_request);
1733   SendMessage(1, request);
1734 }
1735 
TEST_F(AvrcpDeviceTest,invalidLengthSetPlayerApplicationSettingValueTest)1736 TEST_F(AvrcpDeviceTest, invalidLengthSetPlayerApplicationSettingValueTest) {
1737   MockMediaInterface interface;
1738   NiceMock<MockA2dpInterface> a2dp_interface;
1739   MockPlayerSettingsInterface player_settings_interface;
1740 
1741   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, &player_settings_interface);
1742 
1743   auto rej_rsp = RejectBuilder::MakeBuilder(CommandPdu::SET_PLAYER_APPLICATION_SETTING_VALUE,
1744                                             Status::INVALID_PARAMETER);
1745   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rej_rsp)))).Times(1);
1746 
1747   auto request = TestAvrcpPacket::Make(invalid_length_set_player_application_setting_value_request);
1748   SendMessage(1, request);
1749 }
1750 
TEST_F(AvrcpDeviceTest,invalidRegisterNotificationTest)1751 TEST_F(AvrcpDeviceTest, invalidRegisterNotificationTest) {
1752   MockMediaInterface interface;
1753   NiceMock<MockA2dpInterface> a2dp_interface;
1754 
1755   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1756 
1757   auto reg_notif_rej_rsp =
1758           RejectBuilder::MakeBuilder(CommandPdu::REGISTER_NOTIFICATION, Status::INVALID_PARAMETER);
1759   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(reg_notif_rej_rsp)))).Times(1);
1760 
1761   auto reg_notif_request = TestAvrcpPacket::Make(register_notification_invalid);
1762   SendMessage(1, reg_notif_request);
1763 }
1764 
TEST_F(AvrcpDeviceTest,invalidVendorPacketTest)1765 TEST_F(AvrcpDeviceTest, invalidVendorPacketTest) {
1766   MockMediaInterface interface;
1767   NiceMock<MockA2dpInterface> a2dp_interface;
1768 
1769   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1770 
1771   auto rsp = RejectBuilder::MakeBuilder(static_cast<CommandPdu>(0), Status::INVALID_COMMAND);
1772   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1773   auto short_packet = TestAvrcpPacket::Make(short_vendor_packet);
1774   SendMessage(1, short_packet);
1775 }
1776 
TEST_F(AvrcpDeviceTest,invalidCapabilitiesPacketTest)1777 TEST_F(AvrcpDeviceTest, invalidCapabilitiesPacketTest) {
1778   MockMediaInterface interface;
1779   NiceMock<MockA2dpInterface> a2dp_interface;
1780 
1781   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1782 
1783   auto rsp = RejectBuilder::MakeBuilder(CommandPdu::GET_CAPABILITIES, Status::INVALID_PARAMETER);
1784   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1785   auto short_packet = TestAvrcpPacket::Make(short_get_capabilities_request);
1786   SendMessage(1, short_packet);
1787 }
1788 
TEST_F(AvrcpDeviceTest,invalidGetElementAttributesPacketTest)1789 TEST_F(AvrcpDeviceTest, invalidGetElementAttributesPacketTest) {
1790   MockMediaInterface interface;
1791   NiceMock<MockA2dpInterface> a2dp_interface;
1792 
1793   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1794 
1795   auto rsp =
1796           RejectBuilder::MakeBuilder(CommandPdu::GET_ELEMENT_ATTRIBUTES, Status::INVALID_PARAMETER);
1797   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1798   auto short_packet = TestAvrcpPacket::Make(short_get_element_attributes_request);
1799   SendMessage(1, short_packet);
1800 }
1801 
TEST_F(AvrcpDeviceTest,invalidPlayItemPacketTest)1802 TEST_F(AvrcpDeviceTest, invalidPlayItemPacketTest) {
1803   MockMediaInterface interface;
1804   NiceMock<MockA2dpInterface> a2dp_interface;
1805 
1806   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1807 
1808   auto rsp = RejectBuilder::MakeBuilder(CommandPdu::PLAY_ITEM, Status::INVALID_PARAMETER);
1809   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1810   auto short_packet = TestAvrcpPacket::Make(short_play_item_request);
1811   SendMessage(1, short_packet);
1812 }
1813 
TEST_F(AvrcpDeviceTest,invalidSetAddressedPlayerPacketTest)1814 TEST_F(AvrcpDeviceTest, invalidSetAddressedPlayerPacketTest) {
1815   MockMediaInterface interface;
1816   NiceMock<MockA2dpInterface> a2dp_interface;
1817 
1818   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1819 
1820   auto rsp =
1821           RejectBuilder::MakeBuilder(CommandPdu::SET_ADDRESSED_PLAYER, Status::INVALID_PARAMETER);
1822   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1823   auto short_packet = TestAvrcpPacket::Make(short_set_addressed_player_request);
1824   SendMessage(1, short_packet);
1825 }
1826 
TEST_F(AvrcpDeviceTest,invalidBrowsePacketTest)1827 TEST_F(AvrcpDeviceTest, invalidBrowsePacketTest) {
1828   MockMediaInterface interface;
1829   NiceMock<MockA2dpInterface> a2dp_interface;
1830 
1831   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1832 
1833   auto rsp = GeneralRejectBuilder::MakeBuilder(Status::INVALID_COMMAND);
1834   EXPECT_CALL(response_cb, Call(1, false, matchPacket(std::move(rsp)))).Times(1);
1835   auto short_packet = TestBrowsePacket::Make(short_browse_packet);
1836   SendBrowseMessage(1, short_packet);
1837 }
1838 
TEST_F(AvrcpDeviceTest,invalidGetFolderItemsPacketTest)1839 TEST_F(AvrcpDeviceTest, invalidGetFolderItemsPacketTest) {
1840   MockMediaInterface interface;
1841   NiceMock<MockA2dpInterface> a2dp_interface;
1842 
1843   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1844 
1845   auto rsp = GetFolderItemsResponseBuilder::MakePlayerListBuilder(Status::INVALID_PARAMETER, 0x0000,
1846                                                                   0xFFFF);
1847   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(rsp)))).Times(1);
1848   auto short_packet = TestBrowsePacket::Make(short_get_folder_items_request);
1849   SendBrowseMessage(1, short_packet);
1850 }
1851 
TEST_F(AvrcpDeviceTest,invalidGetTotalNumberOfItemsPacketTest)1852 TEST_F(AvrcpDeviceTest, invalidGetTotalNumberOfItemsPacketTest) {
1853   MockMediaInterface interface;
1854   NiceMock<MockA2dpInterface> a2dp_interface;
1855 
1856   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1857 
1858   auto rsp = GetTotalNumberOfItemsResponseBuilder::MakeBuilder(Status::INVALID_PARAMETER, 0x0000,
1859                                                                0xFFFF);
1860   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(rsp)))).Times(1);
1861   auto short_packet = TestBrowsePacket::Make(short_get_total_number_of_items_request);
1862   SendBrowseMessage(1, short_packet);
1863 }
1864 
TEST_F(AvrcpDeviceTest,invalidChangePathPacketTest)1865 TEST_F(AvrcpDeviceTest, invalidChangePathPacketTest) {
1866   MockMediaInterface interface;
1867   NiceMock<MockA2dpInterface> a2dp_interface;
1868 
1869   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1870 
1871   auto rsp = ChangePathResponseBuilder::MakeBuilder(Status::INVALID_PARAMETER, 0);
1872   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(rsp)))).Times(1);
1873   auto short_packet = TestBrowsePacket::Make(short_change_path_request);
1874   SendBrowseMessage(1, short_packet);
1875 }
1876 
TEST_F(AvrcpDeviceTest,invalidGetItemAttributesPacketTest)1877 TEST_F(AvrcpDeviceTest, invalidGetItemAttributesPacketTest) {
1878   MockMediaInterface interface;
1879   NiceMock<MockA2dpInterface> a2dp_interface;
1880 
1881   test_device->RegisterInterfaces(&interface, &a2dp_interface, nullptr, nullptr);
1882 
1883   auto rsp = GetItemAttributesResponseBuilder::MakeBuilder(Status::INVALID_PARAMETER, 0xFFFF);
1884   EXPECT_CALL(response_cb, Call(1, true, matchPacket(std::move(rsp)))).Times(1);
1885   auto short_packet = TestBrowsePacket::Make(short_get_item_attributes_request);
1886   SendBrowseMessage(1, short_packet);
1887 }
1888 
1889 }  // namespace avrcp
1890 }  // namespace bluetooth
1891 
stack_config_get_interface(void)1892 const stack_config_t* stack_config_get_interface(void) { return &bluetooth::avrcp::interface; }
1893