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_element_attributes_packet.h"
21#include "packet_test_helper.h"
22
23namespace bluetooth {
24namespace avrcp {
25
26using TestGetElemAttrReqPacket = TestPacketType<GetElementAttributesRequest>;
27
28TEST(GetElementAttributesRequestPacketTest, getterTest) {
29  // Only Title is requested
30  auto test_packet =
31      TestGetElemAttrReqPacket::Make(get_element_attributes_request_partial);
32
33  ASSERT_EQ(test_packet->GetIdentifier(), 0u);
34
35  auto attribute_list = test_packet->GetAttributesRequested();
36  ASSERT_EQ(attribute_list.size(), 1u);
37  ASSERT_EQ(attribute_list[0], Attribute::TITLE);
38
39  // Title, Artist, Album, Media Numer, Playing Time, Total Number of Media,
40  // and Genre requested
41  test_packet =
42      TestGetElemAttrReqPacket::Make(get_element_attributes_request_full);
43
44  ASSERT_EQ(test_packet->GetIdentifier(), 0u);
45
46  attribute_list = test_packet->GetAttributesRequested();
47  ASSERT_EQ(attribute_list.size(), 7u);
48  ASSERT_EQ(attribute_list[0], Attribute::TITLE);
49  ASSERT_EQ(attribute_list[1], Attribute::ARTIST_NAME);
50  ASSERT_EQ(attribute_list[2], Attribute::ALBUM_NAME);
51  ASSERT_EQ(attribute_list[3], Attribute::TRACK_NUMBER);
52  ASSERT_EQ(attribute_list[4], Attribute::PLAYING_TIME);
53  ASSERT_EQ(attribute_list[5], Attribute::TOTAL_NUMBER_OF_TRACKS);
54  ASSERT_EQ(attribute_list[6], Attribute::GENRE);
55}
56
57TEST(GetElementAttributesRequestPacketTest, validTest) {
58  auto test_packet =
59      TestGetElemAttrReqPacket::Make(get_element_attributes_request_partial);
60  ASSERT_TRUE(test_packet->IsValid());
61
62  test_packet =
63      TestGetElemAttrReqPacket::Make(get_element_attributes_request_full);
64  ASSERT_TRUE(test_packet->IsValid());
65}
66
67TEST(GetElementAttributesRequestPacketTest, invalidTest) {
68  std::vector<uint8_t> packet_copy = get_element_attributes_request_partial;
69  packet_copy.push_back(0x00);
70  auto test_packet = TestGetElemAttrReqPacket::Make(packet_copy);
71  ASSERT_FALSE(test_packet->IsValid());
72
73  std::vector<uint8_t> short_packet = {0x00, 0x00, 0x00, 0x00, 0x00,
74                                       0x00, 0x00, 0x00, 0x00, 0x00};
75  test_packet = TestGetElemAttrReqPacket::Make(short_packet);
76  ASSERT_FALSE(test_packet->IsValid());
77}
78
79TEST(GetElementAttributesResponseBuilderTest, builderLengthTest) {
80  // Attributes have a size of 8 + string length
81  auto builder = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
82  ASSERT_EQ(builder->size(), 11u);
83  builder->AddAttributeEntry(Attribute::TITLE, "test");
84  ASSERT_EQ(builder->size(), 23u);
85  builder->AddAttributeEntry(Attribute::ARTIST_NAME, "test");
86  ASSERT_EQ(builder->size(), 35u);
87}
88
89TEST(GetElementAttributesResponseBuilderTest, builderTest) {
90  auto builder = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
91  builder->AddAttributeEntry(Attribute::TITLE, "Test Song");
92  builder->AddAttributeEntry(Attribute::ARTIST_NAME, "Test Artist");
93  builder->AddAttributeEntry(Attribute::ALBUM_NAME, "Test Album");
94  builder->AddAttributeEntry(Attribute::TRACK_NUMBER, "1");
95  builder->AddAttributeEntry(Attribute::TOTAL_NUMBER_OF_TRACKS, "2");
96  builder->AddAttributeEntry(Attribute::GENRE, "Test Genre");
97  builder->AddAttributeEntry(Attribute::PLAYING_TIME, "1000");
98
99  ASSERT_EQ(builder->size(), get_elements_attributes_response_full.size());
100
101  auto test_packet = TestGetElemAttrReqPacket::Make();
102  builder->Serialize(test_packet);
103  ASSERT_EQ(test_packet->GetData(), get_elements_attributes_response_full);
104}
105
106TEST(GetElementAttributesResponseBuilderTest, truncateBuilderTest) {
107  auto attribute = AttributeEntry(Attribute::TITLE, "1234");
108  size_t truncated_size = VendorPacket::kMinSize();
109  truncated_size += 1;                 // Number of attributes
110  truncated_size += attribute.size();  // Attribute size
111
112  auto truncated_builder =
113      GetElementAttributesResponseBuilder::MakeBuilder(truncated_size);
114  ASSERT_TRUE(
115      truncated_builder->AddAttributeEntry(Attribute::TITLE, "1234truncated"));
116  ASSERT_EQ(truncated_builder->size(), truncated_size);
117
118  ASSERT_FALSE(truncated_builder->AddAttributeEntry(Attribute::ARTIST_NAME,
119                                                    "Can not add"));
120
121  auto truncated_packet = TestGetElemAttrReqPacket::Make();
122  truncated_builder->Serialize(truncated_packet);
123
124  auto builder = GetElementAttributesResponseBuilder::MakeBuilder(0xFFFF);
125  builder->AddAttributeEntry(attribute);
126  auto test_packet = TestGetElemAttrReqPacket::Make();
127  builder->Serialize(test_packet);
128
129  ASSERT_EQ(truncated_packet->GetData(), test_packet->GetData());
130}
131
132}  // namespace avrcp
133}  // namespace bluetooth