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