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 "register_notification_packet.h"
18 
19 #include <android-base/silent_death_test.h>
20 #include <gtest/gtest.h>
21 
22 #include "avrcp_common.h"
23 #include "avrcp_test_packets.h"
24 #include "packet_test_helper.h"
25 
26 namespace bluetooth {
27 namespace avrcp {
28 
29 using TestRegNotifReqPacket = TestPacketType<RegisterNotificationRequest>;
30 using TestRegNotifRspPacket = TestPacketType<RegisterNotificationResponse>;
31 
TEST(RegisterNotificationRequestTest,getterTest)32 TEST(RegisterNotificationRequestTest, getterTest) {
33   auto test_packet =
34       TestRegNotifReqPacket::Make(register_play_status_notification);
35 
36   ASSERT_EQ(test_packet->GetEventRegistered(), Event::PLAYBACK_STATUS_CHANGED);
37   ASSERT_EQ(test_packet->GetInterval(), 5u);
38 }
39 
TEST(RegisterNotificationRequestTest,validTest)40 TEST(RegisterNotificationRequestTest, validTest) {
41   auto test_packet =
42       TestRegNotifReqPacket::Make(register_play_status_notification);
43   ASSERT_TRUE(test_packet->IsValid());
44 }
45 
TEST(RegisterNotificationRequestTest,invalidTest)46 TEST(RegisterNotificationRequestTest, invalidTest) {
47   std::vector<uint8_t> packet_copy = register_play_status_notification;
48   packet_copy.push_back(0x00);
49   auto test_packet = TestRegNotifReqPacket::Make(packet_copy);
50   ASSERT_FALSE(test_packet->IsValid());
51 
52   std::vector<uint8_t> short_packet = {0, 1, 2, 3, 4};
53   test_packet = TestRegNotifReqPacket::Make(short_packet);
54   ASSERT_FALSE(test_packet->IsValid());
55 }
56 
TEST(RegisterNotificationRequestBuilderTest,builderTest)57 TEST(RegisterNotificationRequestBuilderTest, builderTest) {
58   auto builder =
59       RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
60   ASSERT_EQ(builder->size(), register_volume_changed_notification.size());
61 
62   auto test_packet = TestRegNotifReqPacket::Make();
63   builder->Serialize(test_packet);
64   ASSERT_EQ(test_packet->GetData(), register_volume_changed_notification);
65 }
66 
TEST(RegisterNotificationResponseTest,volumeGetterTest)67 TEST(RegisterNotificationResponseTest, volumeGetterTest) {
68   auto test_packet =
69       TestRegNotifRspPacket::Make(interim_volume_changed_notification);
70 
71   ASSERT_TRUE(test_packet->IsInterim());
72   ASSERT_EQ(test_packet->GetEvent(), Event::VOLUME_CHANGED);
73   ASSERT_EQ(test_packet->GetVolume(), 0x47);
74 }
75 
TEST(RegisterNotificationResponseTest,validTest)76 TEST(RegisterNotificationResponseTest, validTest) {
77   auto test_packet =
78       TestRegNotifRspPacket::Make(interim_volume_changed_notification);
79 
80   ASSERT_TRUE(test_packet->IsValid());
81 }
82 
TEST(RegisterNotificationResponseTest,invalidTest)83 TEST(RegisterNotificationResponseTest, invalidTest) {
84   std::vector<uint8_t> packet_copy = interim_volume_changed_notification;
85   packet_copy.push_back(0x00);
86   auto test_packet = TestRegNotifRspPacket::Make(packet_copy);
87   ASSERT_FALSE(test_packet->IsValid());
88 
89   std::vector<uint8_t> short_packet = {0, 1, 2, 3, 4};
90   test_packet = TestRegNotifRspPacket::Make(short_packet);
91   ASSERT_FALSE(test_packet->IsValid());
92 
93   auto wrong_ctype = interim_volume_changed_notification;
94   wrong_ctype[0] = 0x00;
95   test_packet = TestRegNotifRspPacket::Make(short_packet);
96   ASSERT_FALSE(test_packet->IsValid());
97 }
98 
TEST(RegisterNotificationResponseDeathTest,wrongEventDeathTest)99 TEST(RegisterNotificationResponseDeathTest, wrongEventDeathTest) {
100   auto wrong_event = interim_volume_changed_notification;
101   wrong_event[10] = 0x00;
102   auto test_packet = TestRegNotifRspPacket::Make(wrong_event);
103 
104   // this will silent SIGABRT sent in ASSERT_DEATH below
105   ScopedSilentDeath _silentDeath;
106 
107   ASSERT_DEATH(test_packet->GetVolume(),
108                "GetEvent\\(\\) == Event::VOLUME_CHANGED");
109 }
110 
TEST(RegisterNotificationResponseBuilderTest,playStatusBuilderTest)111 TEST(RegisterNotificationResponseBuilderTest, playStatusBuilderTest) {
112   auto builder = RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(
113       true, 0x00);
114   ASSERT_EQ(builder->size(), interim_play_status_notification.size());
115   auto test_packet = TestRegNotifReqPacket::Make();
116   builder->Serialize(test_packet);
117   ASSERT_EQ(test_packet->GetData(), interim_play_status_notification);
118 }
119 
TEST(RegisterNotificationResponseBuilderTest,trackChangedBuilderTest)120 TEST(RegisterNotificationResponseBuilderTest, trackChangedBuilderTest) {
121   auto builder = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(
122       true, 0x0102030405060708);
123   ASSERT_EQ(builder->size(), interim_track_changed_notification.size());
124   auto test_packet = TestRegNotifReqPacket::Make();
125   builder->Serialize(test_packet);
126   ASSERT_EQ(test_packet->GetData(), interim_track_changed_notification);
127 }
128 
TEST(RegisterNotificationResponseBuilderTest,interimPlayerSettingChangedBuilderTest)129 TEST(RegisterNotificationResponseBuilderTest,
130      interimPlayerSettingChangedBuilderTest) {
131   std::vector<PlayerAttribute> attrs = {
132       PlayerAttribute::EQUALIZER, PlayerAttribute::REPEAT,
133       PlayerAttribute::SHUFFLE, PlayerAttribute::SCAN};
134   std::vector<uint8_t> vals = {0x01, 0x01, 0x01, 0x01};  // All off
135   auto builder =
136       RegisterNotificationResponseBuilder::MakePlayerSettingChangedBuilder(
137           true, attrs, vals);
138   ASSERT_EQ(builder->size(),
139             interim_changed_player_setting_notification.size());
140   auto test_packet = TestRegNotifReqPacket::Make();
141   builder->Serialize(test_packet);
142   ASSERT_EQ(test_packet->GetData(),
143             interim_changed_player_setting_notification);
144 }
145 
TEST(RegisterNotificationResponseBuilderTest,playerSettingChangedBuilderTest)146 TEST(RegisterNotificationResponseBuilderTest, playerSettingChangedBuilderTest) {
147   std::vector<PlayerAttribute> attrs = {PlayerAttribute::REPEAT,
148                                         PlayerAttribute::SHUFFLE};
149   std::vector<uint8_t> vals = {(uint8_t)PlayerRepeatValue::OFF,
150                                (uint8_t)PlayerShuffleValue::ALL};
151   auto builder =
152       RegisterNotificationResponseBuilder::MakePlayerSettingChangedBuilder(
153           false, attrs, vals);
154   ASSERT_EQ(builder->size(), changed_player_setting_notification.size());
155   auto test_packet = TestRegNotifReqPacket::Make();
156   builder->Serialize(test_packet);
157   ASSERT_EQ(test_packet->GetData(), changed_player_setting_notification);
158 }
159 
TEST(RegisterNotificationResponseBuilderTest,playPositionBuilderTest)160 TEST(RegisterNotificationResponseBuilderTest, playPositionBuilderTest) {
161   auto builder =
162       RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(
163           false, 0x00000000);
164   ASSERT_EQ(builder->size(), changed_play_pos_notification.size());
165   auto test_packet = TestRegNotifReqPacket::Make();
166   builder->Serialize(test_packet);
167   ASSERT_EQ(test_packet->GetData(), changed_play_pos_notification);
168 }
169 
TEST(RegisterNotificationResponseBuilderTest,nowPlayingBuilderTest)170 TEST(RegisterNotificationResponseBuilderTest, nowPlayingBuilderTest) {
171   auto builder =
172       RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(true);
173   ASSERT_EQ(builder->size(), interim_now_playing_notification.size());
174   auto test_packet = TestRegNotifReqPacket::Make();
175   builder->Serialize(test_packet);
176   ASSERT_EQ(test_packet->GetData(), interim_now_playing_notification);
177 }
178 
TEST(RegisterNotificationResponseBuilderTest,availablePlayersBuilderTest)179 TEST(RegisterNotificationResponseBuilderTest, availablePlayersBuilderTest) {
180   auto builder =
181       RegisterNotificationResponseBuilder::MakeAvailablePlayersBuilder(true);
182   ASSERT_EQ(builder->size(), interim_available_players_notification.size());
183   auto test_packet = TestRegNotifReqPacket::Make();
184   builder->Serialize(test_packet);
185   ASSERT_EQ(test_packet->GetData(), interim_available_players_notification);
186 }
187 
TEST(RegisterNotificationResponseBuilderTest,addressedPlayerBuilderTest)188 TEST(RegisterNotificationResponseBuilderTest, addressedPlayerBuilderTest) {
189   auto builder =
190       RegisterNotificationResponseBuilder::MakeAddressedPlayerBuilder(true, 1,
191                                                                       0x0000);
192   ASSERT_EQ(builder->size(), interim_addressed_player_notification.size());
193   auto test_packet = TestRegNotifReqPacket::Make();
194   builder->Serialize(test_packet);
195   ASSERT_EQ(test_packet->GetData(), interim_addressed_player_notification);
196 }
197 
TEST(RegisterNotificationResponseBuilderTest,uidsChangedBuilderTest)198 TEST(RegisterNotificationResponseBuilderTest, uidsChangedBuilderTest) {
199   auto builder =
200       RegisterNotificationResponseBuilder::MakeUidsChangedBuilder(true, 0x0000);
201   ASSERT_EQ(builder->size(), interim_uids_notificaiton.size());
202   auto test_packet = TestRegNotifReqPacket::Make();
203   builder->Serialize(test_packet);
204   ASSERT_EQ(test_packet->GetData(), interim_uids_notificaiton);
205 }
206 
207 }  // namespace avrcp
208 }  // namespace bluetooth
209