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_item_attributes.h"
21 #include "packet_test_helper.h"
22
23 namespace bluetooth {
24 namespace avrcp {
25
26 using TestGetItemAttrsReqPacket = TestPacketType<GetItemAttributesRequest>;
27
TEST(GetItemAttributesResponseBuilderTest,builderSizeTest)28 TEST(GetItemAttributesResponseBuilderTest, builderSizeTest) {
29 auto builder =
30 GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
31 ASSERT_EQ(builder->size(), 5u);
32
33 builder->AddAttributeEntry(Attribute::TITLE, "Test Song");
34 ASSERT_EQ(builder->size(), 22u);
35
36 builder->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
37 ASSERT_EQ(builder->size(), 41u);
38
39 builder->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
40 ASSERT_EQ(builder->size(), get_item_attributes_song_response.size());
41 }
42
TEST(GetItemAttributesResponseBuilderTest,builderTest)43 TEST(GetItemAttributesResponseBuilderTest, builderTest) {
44 auto builder =
45 GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
46 builder->AddAttributeEntry(Attribute::TITLE, "Test Song");
47 builder->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
48 builder->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
49
50 auto test_packet = TestGetItemAttrsReqPacket::Make();
51 builder->Serialize(test_packet);
52 ASSERT_EQ(test_packet->GetData(), get_item_attributes_song_response);
53 }
54
TEST(GetItemAttributesResponseBuilderTest,truncateBuilderTest)55 TEST(GetItemAttributesResponseBuilderTest, truncateBuilderTest) {
56 auto attribute = AttributeEntry(Attribute::TITLE, "1234");
57 size_t truncated_size = BrowsePacket::kMinSize();
58 truncated_size += 2; // Status field + Number of attributes field
59 truncated_size += attribute.size(); // Attribute size
60
61 auto truncated_builder = GetItemAttributesResponseBuilder::MakeBuilder(
62 Status::NO_ERROR, truncated_size);
63 ASSERT_TRUE(
64 truncated_builder->AddAttributeEntry(Attribute::TITLE, "1234truncated"));
65 ASSERT_EQ(truncated_builder->size(), truncated_size);
66
67 ASSERT_FALSE(truncated_builder->AddAttributeEntry(Attribute::ARTIST_NAME,
68 "Can not add"));
69
70 auto truncated_packet = TestGetItemAttrsReqPacket::Make();
71 truncated_builder->Serialize(truncated_packet);
72
73 auto builder =
74 GetItemAttributesResponseBuilder::MakeBuilder(Status::NO_ERROR, 0xFFFF);
75 builder->AddAttributeEntry(attribute);
76 auto test_packet = TestGetItemAttrsReqPacket::Make();
77 builder->Serialize(test_packet);
78
79 ASSERT_EQ(truncated_packet->GetData(), test_packet->GetData());
80 }
81
TEST(GetItemAttributesResponseBuilderTest,errorStatusTest)82 TEST(GetItemAttributesResponseBuilderTest, errorStatusTest) {
83 std::vector<uint8_t> does_not_exist_status = {0x73, 0x00, 0x01, 0x09};
84 auto builder = GetItemAttributesResponseBuilder::MakeBuilder(
85 Status::DOES_NOT_EXIST, 0xFFFF);
86 ASSERT_EQ(builder->size(), does_not_exist_status.size());
87
88 auto test_packet = TestGetItemAttrsReqPacket::Make();
89 builder->Serialize(test_packet);
90 ASSERT_EQ(test_packet->GetData(), does_not_exist_status);
91 }
92
TEST(GetItemAttributesRequestTest,getterTest)93 TEST(GetItemAttributesRequestTest, getterTest) {
94 auto test_packet = TestGetItemAttrsReqPacket::Make(
95 get_item_attributes_request_all_attributes);
96
97 ASSERT_EQ(test_packet->GetScope(), Scope::NOW_PLAYING);
98 ASSERT_EQ(test_packet->GetUidCounter(), 0x0000u);
99 ASSERT_EQ(test_packet->GetUid(), 0x0000000000000001u);
100 ASSERT_EQ(test_packet->GetNumAttributes(), 7);
101 std::vector<Attribute> attrs_requested = {Attribute::TITLE,
102 Attribute::ARTIST_NAME,
103 Attribute::ALBUM_NAME,
104 Attribute::TRACK_NUMBER,
105 Attribute::TOTAL_NUMBER_OF_TRACKS,
106 Attribute::GENRE,
107 Attribute::PLAYING_TIME};
108 ASSERT_EQ(test_packet->GetAttributesRequested(), attrs_requested);
109 }
110
TEST(GetItemAttributesRequestTest,validTest)111 TEST(GetItemAttributesRequestTest, validTest) {
112 auto test_packet = TestGetItemAttrsReqPacket::Make(
113 get_item_attributes_request_all_attributes);
114 ASSERT_TRUE(test_packet->IsValid());
115 }
116
TEST(GetItemAttributesRequestTest,invalidTest)117 TEST(GetItemAttributesRequestTest, invalidTest) {
118 auto packet_copy = get_item_attributes_request_all_attributes;
119 packet_copy.push_back(0x00);
120 auto test_packet = TestGetItemAttrsReqPacket::Make(packet_copy);
121 ASSERT_FALSE(test_packet->IsValid());
122
123 std::vector<uint8_t> short_packet = {0x00, 0x01, 0x02, 0x03,
124 0x04, 0x05, 0x06, 0x07};
125 test_packet = TestGetItemAttrsReqPacket::Make(short_packet);
126 ASSERT_FALSE(test_packet->IsValid());
127 }
128
129 } // namespace avrcp
130 } // namespace bluetooth