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 "get_folder_items.h"
21 #include "packet_test_helper.h"
22
23 namespace bluetooth {
24 namespace avrcp {
25
26 using TestGetFolderItemsReqPacket = TestPacketType<GetFolderItemsRequest>;
27
TEST(GetFolderItemsResponseBuilderTest,builderMediaPlayerSizeTest)28 TEST(GetFolderItemsResponseBuilderTest, builderMediaPlayerSizeTest) {
29 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
30 Status::NO_ERROR, 0x0000, 0xFFFF);
31 // If there are no items, then the only data in the packet is the status
32 ASSERT_EQ(builder->size(), get_folder_items_error_response.size());
33
34 auto player = MediaPlayerItem(0x0001, "com.google.android.music", true);
35 builder->AddMediaPlayer(player);
36 ASSERT_EQ(builder->size(), get_folder_items_media_player_response.size());
37 }
38
TEST(GetFolderItemsResponseBuilderTest,builderMediaPlayerAddTest)39 TEST(GetFolderItemsResponseBuilderTest, builderMediaPlayerAddTest) {
40 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
41 Status::NO_ERROR, 0x0000, 0xFFFF);
42 auto player = MediaPlayerItem(0x0001, "com.google.android.music", true);
43 builder->AddMediaPlayer(player);
44
45 auto test_packet = TestGetFolderItemsReqPacket::Make();
46 builder->Serialize(test_packet);
47 ASSERT_EQ(test_packet->GetData(), get_folder_items_media_player_response);
48 }
49
TEST(GetFolderItemsResponseBuilderTest,builderPlayerAddMtuTest)50 TEST(GetFolderItemsResponseBuilderTest, builderPlayerAddMtuTest) {
51 MediaPlayerItem player1(1, "Player 1 that fits", true);
52 MediaPlayerItem player2(2, "Player 2 that doesn't fit", true);
53 MediaPlayerItem player3(3, "Player 3 that fits", true);
54
55 // Browsing Header + Status field + UID Counter field + Number of Items field
56 auto packet_size = BrowsePacket::kMinSize() + 5;
57 packet_size += player1.size() + player3.size();
58
59 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
60 Status::NO_ERROR, 0x0000, packet_size);
61
62 ASSERT_TRUE(builder->AddMediaPlayer(player1));
63 ASSERT_FALSE(builder->AddMediaPlayer(player2));
64 ASSERT_TRUE(builder->AddMediaPlayer(player3));
65 }
66
TEST(GetFolderItemsResponseBuilderTest,builderFolderSizeTest)67 TEST(GetFolderItemsResponseBuilderTest, builderFolderSizeTest) {
68 auto builder = GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR,
69 0x0000, 0xFFFF);
70 ASSERT_EQ(builder->size(), get_folder_items_error_response.size());
71
72 auto folder = FolderItem(0x0000000000000001, 0x00, true, "Test Folder");
73 builder->AddFolder(folder);
74 ASSERT_EQ(builder->size(), get_folder_items_folder_response.size());
75 }
76
TEST(GetFolderItemsResponseBuilderTest,builderFolderAddTest)77 TEST(GetFolderItemsResponseBuilderTest, builderFolderAddTest) {
78 auto builder = GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR,
79 0x0000, 0xFFFF);
80 auto folder = FolderItem(0x0000000000000001, 0x00, true, "Test Folder");
81 builder->AddFolder(folder);
82
83 auto test_packet = TestGetFolderItemsReqPacket::Make();
84 builder->Serialize(test_packet);
85 ASSERT_EQ(test_packet->GetData(), get_folder_items_folder_response);
86 }
87
TEST(GetFolderItemsResponseBuilderTest,builderFolderAddMtuTest)88 TEST(GetFolderItemsResponseBuilderTest, builderFolderAddMtuTest) {
89 FolderItem folder1(0x01, 0x00, true, "Folder 1 that fits");
90 FolderItem folder2(0x02, 0x00, true, "Folder 2 that doesn't fit");
91 FolderItem folder3(0x03, 0x00, true, "Folder 3 that fits");
92
93 // Browsing Header + Status field + UID Counter field + Number of Items field
94 auto packet_size = BrowsePacket::kMinSize() + 5;
95 packet_size += folder1.size() + folder3.size();
96
97 auto builder = GetFolderItemsResponseBuilder::MakeVFSBuilder(
98 Status::NO_ERROR, 0x0000, packet_size);
99
100 ASSERT_TRUE(builder->AddFolder(folder1));
101 ASSERT_FALSE(builder->AddFolder(folder2));
102 ASSERT_TRUE(builder->AddFolder(folder3));
103 }
104
TEST(GetFolderItemsResponseBuilderTest,builderSongSizeTest)105 TEST(GetFolderItemsResponseBuilderTest, builderSongSizeTest) {
106 auto builder = GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(
107 Status::NO_ERROR, 0x0000, 0xFFFF);
108 ASSERT_EQ(builder->size(), get_folder_items_error_response.size());
109
110 std::set<AttributeEntry> attributes;
111 attributes.insert(AttributeEntry(Attribute::TITLE, "Test Title"));
112 auto song = MediaElementItem(0x02, "Test Title", attributes);
113 builder->AddSong(song);
114 ASSERT_EQ(builder->size(), get_folder_items_song_response.size());
115 }
116
TEST(GetFolderItemsResponseBuilderTest,builderSongAddTest)117 TEST(GetFolderItemsResponseBuilderTest, builderSongAddTest) {
118 auto builder = GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(
119 Status::NO_ERROR, 0x0000, 0xFFFF);
120 std::set<AttributeEntry> attributes;
121 attributes.insert(AttributeEntry(Attribute::TITLE, "Test Title"));
122 auto song = MediaElementItem(0x02, "Test Title", attributes);
123 builder->AddSong(song);
124
125 auto test_packet = TestGetFolderItemsReqPacket::Make();
126 builder->Serialize(test_packet);
127 ASSERT_EQ(test_packet->GetData(), get_folder_items_song_response);
128 }
129
TEST(GetFolderItemsResponseBuilderTest,builderSongAddMtuTest)130 TEST(GetFolderItemsResponseBuilderTest, builderSongAddMtuTest) {
131 MediaElementItem song1(0x01, "Song 1 that fits", std::set<AttributeEntry>());
132 MediaElementItem song2(0x02, "Song 2 that doesn't fit",
133 std::set<AttributeEntry>());
134 MediaElementItem song3(0x03, "Song 3 that fits", std::set<AttributeEntry>());
135
136 auto packet_size = BrowsePacket::kMinSize() + 5;
137 packet_size += song1.size() + song3.size();
138
139 auto builder = GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(
140 Status::NO_ERROR, 0x0000, packet_size);
141
142 ASSERT_TRUE(builder->AddSong(song1));
143 ASSERT_FALSE(builder->AddSong(song2));
144 ASSERT_TRUE(builder->AddSong(song3));
145 }
146
TEST(GetFolderItemsResponseBuilderTest,builderNoItemsTest)147 TEST(GetFolderItemsResponseBuilderTest, builderNoItemsTest) {
148 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
149 Status::NO_ERROR, 0x0000, 0xFFFF);
150 auto test_packet = TestGetFolderItemsReqPacket::Make();
151 builder->Serialize(test_packet);
152 ASSERT_EQ(test_packet->GetData(), get_folder_items_error_response);
153
154 builder = GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR,
155 0x0000, 0xFFFF);
156 test_packet = TestGetFolderItemsReqPacket::Make();
157 builder->Serialize(test_packet);
158 ASSERT_EQ(test_packet->GetData(), get_folder_items_error_response);
159
160 builder = GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(
161 Status::NO_ERROR, 0x0000, 0xFFFF);
162 test_packet = TestGetFolderItemsReqPacket::Make();
163 builder->Serialize(test_packet);
164 ASSERT_EQ(test_packet->GetData(), get_folder_items_error_response);
165 }
166
TEST(GetFolderItemsResponseBuilderTest,builderErrorStatusTest)167 TEST(GetFolderItemsResponseBuilderTest, builderErrorStatusTest) {
168 std::vector<uint8_t> get_folder_items_inv_scope = {0x71, 0x00, 0x01, 0x0a};
169
170 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
171 Status::INVALID_SCOPE, 0x0000, 0xFFFF);
172
173 // Check that the status remains INVALID_SCOPE even though there are zero
174 // items
175 auto test_packet = TestGetFolderItemsReqPacket::Make();
176 builder->Serialize(test_packet);
177 ASSERT_EQ(test_packet->GetData(), get_folder_items_inv_scope);
178
179 auto player = MediaPlayerItem(0x0001, "com.google.android.music", true);
180 builder->AddMediaPlayer(player);
181
182 // Check to make sure that even though we added an item, it doesn't get
183 // written to the packet
184 test_packet = TestGetFolderItemsReqPacket::Make();
185 builder->Serialize(test_packet);
186 ASSERT_EQ(test_packet->GetData(), get_folder_items_inv_scope);
187 }
188
TEST(GetFolderItemsResponseBuilderTest,builderDeathTest)189 TEST(GetFolderItemsResponseBuilderTest, builderDeathTest) {
190 auto player = MediaPlayerItem(0x0001, "com.google.android.music", true);
191 auto folder = FolderItem(0x01, 0x00, true, "test folder");
192 auto song = MediaElementItem(0x01, "test song", std::set<AttributeEntry>());
193
194 auto builder = GetFolderItemsResponseBuilder::MakePlayerListBuilder(
195 Status::NO_ERROR, 0x0000, 0xFFFF);
196 ASSERT_DEATH(builder->AddFolder(folder), "scope_ == Scope::VFS");
197 ASSERT_DEATH(builder->AddSong(song),
198 "scope_ == Scope::VFS \\|\\| scope_ == Scope::NOW_PLAYING");
199
200 builder = GetFolderItemsResponseBuilder::MakeVFSBuilder(Status::NO_ERROR,
201 0x0000, 0xFFFF);
202 ASSERT_DEATH(builder->AddMediaPlayer(player),
203 "scope_ == Scope::MEDIA_PLAYER_LIST");
204
205 builder = GetFolderItemsResponseBuilder::MakeNowPlayingBuilder(
206 Status::NO_ERROR, 0x0000, 0xFFFF);
207 ASSERT_DEATH(builder->AddMediaPlayer(player),
208 "scope_ == Scope::MEDIA_PLAYER_LIST");
209 ASSERT_DEATH(builder->AddFolder(folder), "scope_ == Scope::VFS");
210 }
211
TEST(GetFolderItemsRequestTest,getterTest)212 TEST(GetFolderItemsRequestTest, getterTest) {
213 auto test_packet =
214 TestGetFolderItemsReqPacket::Make(get_folder_items_request_vfs);
215
216 ASSERT_EQ(test_packet->GetScope(), Scope::VFS);
217 ASSERT_EQ(test_packet->GetStartItem(), 0x00000000u);
218 ASSERT_EQ(test_packet->GetEndItem(), 0x00000005u);
219 ASSERT_EQ(test_packet->GetNumAttributes(), 1);
220
221 std::vector<Attribute> attribute_list = {Attribute::TITLE};
222 ASSERT_EQ(test_packet->GetAttributesRequested(), attribute_list);
223 }
224
TEST(GetFolderItemsRequestBuilderTest,builderZeroAttrsTest)225 TEST(GetFolderItemsRequestBuilderTest, builderZeroAttrsTest) {
226 auto builder =
227 GetFolderItemsRequestBuilder::MakeBuilder(Scope::VFS, 0, 9, {});
228 ASSERT_EQ(builder->size(), get_folder_items_request_no_attrs.size());
229
230 auto test_packet = TestGetFolderItemsReqPacket::Make();
231 builder->Serialize(test_packet);
232 ASSERT_EQ(test_packet->GetData(), get_folder_items_request_no_attrs);
233 }
234
TEST(GetFolderItemsRequestBuilderTest,builderTest)235 TEST(GetFolderItemsRequestBuilderTest, builderTest) {
236 auto builder = GetFolderItemsRequestBuilder::MakeBuilder(Scope::VFS, 0, 9,
237 {Attribute::TITLE});
238 ASSERT_EQ(builder->size(), get_folder_items_request_title.size());
239
240 auto test_packet = TestGetFolderItemsReqPacket::Make();
241 builder->Serialize(test_packet);
242 ASSERT_EQ(test_packet->GetData(), get_folder_items_request_title);
243 }
244
245 } // namespace avrcp
246 } // namespace bluetooth