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 <gtest/gtest.h>
18
19 #include "avrcp_test_packets.h"
20 #include "packet_test_helper.h"
21 #include "register_notification_packet.h"
22
23 namespace bluetooth {
24 namespace avrcp {
25
26 using TestRegNotifReqPacket = TestPacketType<RegisterNotificationRequest>;
27 using TestRegNotifRspPacket = TestPacketType<RegisterNotificationResponse>;
28
TEST(RegisterNotificationRequestTest,getterTest)29 TEST(RegisterNotificationRequestTest, getterTest) {
30 auto test_packet =
31 TestRegNotifReqPacket::Make(register_play_status_notification);
32
33 ASSERT_EQ(test_packet->GetEventRegistered(), Event::PLAYBACK_STATUS_CHANGED);
34 ASSERT_EQ(test_packet->GetInterval(), 5u);
35 }
36
TEST(RegisterNotificationRequestTest,validTest)37 TEST(RegisterNotificationRequestTest, validTest) {
38 auto test_packet =
39 TestRegNotifReqPacket::Make(register_play_status_notification);
40 ASSERT_TRUE(test_packet->IsValid());
41 }
42
TEST(RegisterNotificationRequestTest,invalidTest)43 TEST(RegisterNotificationRequestTest, invalidTest) {
44 std::vector<uint8_t> packet_copy = register_play_status_notification;
45 packet_copy.push_back(0x00);
46 auto test_packet = TestRegNotifReqPacket::Make(packet_copy);
47 ASSERT_FALSE(test_packet->IsValid());
48
49 std::vector<uint8_t> short_packet = {0, 1, 2, 3, 4};
50 test_packet = TestRegNotifReqPacket::Make(short_packet);
51 ASSERT_FALSE(test_packet->IsValid());
52 }
53
TEST(RegisterNotificationRequestBuilderTest,builderTest)54 TEST(RegisterNotificationRequestBuilderTest, builderTest) {
55 auto builder =
56 RegisterNotificationRequestBuilder::MakeBuilder(Event::VOLUME_CHANGED, 0);
57 ASSERT_EQ(builder->size(), register_volume_changed_notification.size());
58
59 auto test_packet = TestRegNotifReqPacket::Make();
60 builder->Serialize(test_packet);
61 ASSERT_EQ(test_packet->GetData(), register_volume_changed_notification);
62 }
63
TEST(RegisterNotificationResponseTest,volumeGetterTest)64 TEST(RegisterNotificationResponseTest, volumeGetterTest) {
65 auto test_packet =
66 TestRegNotifRspPacket::Make(interim_volume_changed_notification);
67
68 ASSERT_TRUE(test_packet->IsInterim());
69 ASSERT_EQ(test_packet->GetEvent(), Event::VOLUME_CHANGED);
70 ASSERT_EQ(test_packet->GetVolume(), 0x47);
71 }
72
TEST(RegisterNotificationResponseTest,validTest)73 TEST(RegisterNotificationResponseTest, validTest) {
74 auto test_packet =
75 TestRegNotifRspPacket::Make(interim_volume_changed_notification);
76
77 ASSERT_TRUE(test_packet->IsValid());
78 }
79
TEST(RegisterNotificationResponseTest,invalidTest)80 TEST(RegisterNotificationResponseTest, invalidTest) {
81 std::vector<uint8_t> packet_copy = interim_volume_changed_notification;
82 packet_copy.push_back(0x00);
83 auto test_packet = TestRegNotifRspPacket::Make(packet_copy);
84 ASSERT_FALSE(test_packet->IsValid());
85
86 std::vector<uint8_t> short_packet = {0, 1, 2, 3, 4};
87 test_packet = TestRegNotifRspPacket::Make(short_packet);
88 ASSERT_FALSE(test_packet->IsValid());
89
90 auto wrong_ctype = interim_volume_changed_notification;
91 wrong_ctype[0] = 0x00;
92 test_packet = TestRegNotifRspPacket::Make(short_packet);
93 ASSERT_FALSE(test_packet->IsValid());
94 }
95
TEST(RegisterNotificationResponseTest,wrongEventDeathTest)96 TEST(RegisterNotificationResponseTest, wrongEventDeathTest) {
97 auto wrong_event = interim_volume_changed_notification;
98 wrong_event[10] = 0x00;
99 auto test_packet = TestRegNotifRspPacket::Make(wrong_event);
100
101 ASSERT_DEATH(test_packet->GetVolume(),
102 "GetEvent\\(\\) == Event::VOLUME_CHANGED");
103 }
104
TEST(RegisterNotificationResponseBuilderTest,playStatusBuilderTest)105 TEST(RegisterNotificationResponseBuilderTest, playStatusBuilderTest) {
106 auto builder = RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(
107 true, 0x00);
108 ASSERT_EQ(builder->size(), interim_play_status_notification.size());
109 auto test_packet = TestRegNotifReqPacket::Make();
110 builder->Serialize(test_packet);
111 ASSERT_EQ(test_packet->GetData(), interim_play_status_notification);
112 }
113
TEST(RegisterNotificationResponseBuilderTest,trackChangedBuilderTest)114 TEST(RegisterNotificationResponseBuilderTest, trackChangedBuilderTest) {
115 auto builder = RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(
116 true, 0x0000000000000000);
117 ASSERT_EQ(builder->size(), interim_track_changed_notification.size());
118 auto test_packet = TestRegNotifReqPacket::Make();
119 builder->Serialize(test_packet);
120 ASSERT_EQ(test_packet->GetData(), interim_track_changed_notification);
121 }
122
TEST(RegisterNotificationResponseBuilderTest,playPositionBuilderTest)123 TEST(RegisterNotificationResponseBuilderTest, playPositionBuilderTest) {
124 auto builder =
125 RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(
126 false, 0x00000000);
127 ASSERT_EQ(builder->size(), changed_play_pos_notification.size());
128 auto test_packet = TestRegNotifReqPacket::Make();
129 builder->Serialize(test_packet);
130 ASSERT_EQ(test_packet->GetData(), changed_play_pos_notification);
131 }
132
TEST(RegisterNotificationResponseBuilderTest,nowPlayingBuilderTest)133 TEST(RegisterNotificationResponseBuilderTest, nowPlayingBuilderTest) {
134 auto builder =
135 RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(true);
136 ASSERT_EQ(builder->size(), interim_now_playing_notification.size());
137 auto test_packet = TestRegNotifReqPacket::Make();
138 builder->Serialize(test_packet);
139 ASSERT_EQ(test_packet->GetData(), interim_now_playing_notification);
140 }
141
TEST(RegisterNotificationResponseBuilderTest,availablePlayersBuilderTest)142 TEST(RegisterNotificationResponseBuilderTest, availablePlayersBuilderTest) {
143 auto builder =
144 RegisterNotificationResponseBuilder::MakeAvailablePlayersBuilder(true);
145 ASSERT_EQ(builder->size(), interim_available_players_notification.size());
146 auto test_packet = TestRegNotifReqPacket::Make();
147 builder->Serialize(test_packet);
148 ASSERT_EQ(test_packet->GetData(), interim_available_players_notification);
149 }
150
TEST(RegisterNotificationResponseBuilderTest,addressedPlayerBuilderTest)151 TEST(RegisterNotificationResponseBuilderTest, addressedPlayerBuilderTest) {
152 auto builder =
153 RegisterNotificationResponseBuilder::MakeAddressedPlayerBuilder(true, 1,
154 0x0000);
155 ASSERT_EQ(builder->size(), interim_addressed_player_notification.size());
156 auto test_packet = TestRegNotifReqPacket::Make();
157 builder->Serialize(test_packet);
158 ASSERT_EQ(test_packet->GetData(), interim_addressed_player_notification);
159 }
160
TEST(RegisterNotificationResponseBuilderTest,uidsChangedBuilderTest)161 TEST(RegisterNotificationResponseBuilderTest, uidsChangedBuilderTest) {
162 auto builder =
163 RegisterNotificationResponseBuilder::MakeUidsChangedBuilder(true, 0x0000);
164 ASSERT_EQ(builder->size(), interim_uids_notificaiton.size());
165 auto test_packet = TestRegNotifReqPacket::Make();
166 builder->Serialize(test_packet);
167 ASSERT_EQ(test_packet->GetData(), interim_uids_notificaiton);
168 }
169
170 } // namespace avrcp
171 } // namespace bluetooth
172