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 "capabilities_packet.h"
21 #include "packet_test_helper.h"
22
23 namespace bluetooth {
24 namespace avrcp {
25
26 using GetCapRequestTestPacket = TestPacketType<GetCapabilitiesRequest>;
27
28 // Test parsing a GetCapabilities Request
TEST(GetCapabilitiesRequestPacketTest,getterTest)29 TEST(GetCapabilitiesRequestPacketTest, getterTest) {
30 auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);
31
32 ASSERT_EQ(test_packet->GetCapabilityRequested(),
33 Capability::EVENTS_SUPPORTED);
34 }
35
TEST(GetCapabilitiesRequestPacketTest,validTest)36 TEST(GetCapabilitiesRequestPacketTest, validTest) {
37 auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);
38 ASSERT_TRUE(test_packet->IsValid());
39 }
40
TEST(GetCapabilitiesRequestPacketTest,invalidTest)41 TEST(GetCapabilitiesRequestPacketTest, invalidTest) {
42 std::vector<uint8_t> packet_copy = get_capabilities_request;
43 packet_copy.push_back(0x00);
44 auto test_packet = GetCapRequestTestPacket::Make(packet_copy);
45 ASSERT_FALSE(test_packet->IsValid());
46
47 std::vector<uint8_t> short_packet = {
48 0, 1, 2, 3, 4, 5, 6,
49 };
50 test_packet = GetCapRequestTestPacket::Make(short_packet);
51 ASSERT_FALSE(test_packet->IsValid());
52 }
53
54 // Test that the length returned by the builder grows correctlyas fields are
55 // added
TEST(GetCapabilityResponseBuilder,builderLengthTest)56 TEST(GetCapabilityResponseBuilder, builderLengthTest) {
57 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
58 ASSERT_EQ(builder->size(), 15u);
59 builder->AddCompanyId(0x000001);
60 ASSERT_EQ(builder->size(), 18u);
61 builder->AddCompanyId(0x000002);
62 ASSERT_EQ(builder->size(), 21u);
63
64 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
65 Event::PLAYBACK_STATUS_CHANGED);
66 ASSERT_EQ(builder->size(), 13u);
67 builder->AddEvent(Event::TRACK_CHANGED);
68 ASSERT_EQ(builder->size(), 14u);
69 builder->AddEvent(Event::PLAYBACK_POS_CHANGED);
70 ASSERT_EQ(builder->size(), 15u);
71 }
72
73 // Check to see that adding the same value multiple times does nothing
TEST(GetCapabilityResponseBuilder,duplicateAddTest)74 TEST(GetCapabilityResponseBuilder, duplicateAddTest) {
75 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
76 ASSERT_EQ(builder->size(), 15u);
77 builder->AddCompanyId(0x000000);
78 ASSERT_EQ(builder->size(), 15u);
79
80 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
81 Event::PLAYBACK_STATUS_CHANGED);
82 ASSERT_EQ(builder->size(), 13u);
83 builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED);
84 ASSERT_EQ(builder->size(), 13u);
85 }
86
87 // Test that trying to to add the wrong type of field to a builder causes death
TEST(GetCapabilityResponseBuilder,mismatchAddDeathTest)88 TEST(GetCapabilityResponseBuilder, mismatchAddDeathTest) {
89 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
90 ASSERT_DEATH(builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED),
91 "capability_ == Capability::EVENTS_SUPPORTED");
92
93 builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
94 Event::PLAYBACK_STATUS_CHANGED);
95 ASSERT_DEATH(builder->AddCompanyId(0x000000),
96 "capability_ == Capability::COMPANY_ID");
97 }
98
99 // Test building a GetCapabilities Response to a Company ID request
TEST(GetCapabilityResponseBuilder,comanyIdBuilderTest)100 TEST(GetCapabilityResponseBuilder, comanyIdBuilderTest) {
101 auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x002345);
102 builder->AddCompanyId(BLUETOOTH_COMPANY_ID);
103
104 auto test_packet = GetCapRequestTestPacket::Make();
105 builder->Serialize(test_packet);
106 ASSERT_EQ(test_packet->GetData(), get_capabilities_response_company_id);
107 }
108
109 // Test building a GetCapabilities Response to an Events Supported request
TEST(GetCapabilityResponseBuilder,eventsSupportedBuilderTest)110 TEST(GetCapabilityResponseBuilder, eventsSupportedBuilderTest) {
111 auto builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
112 Event::PLAYBACK_STATUS_CHANGED);
113 builder->AddEvent(Event::TRACK_CHANGED);
114 builder->AddEvent(Event::PLAYBACK_POS_CHANGED);
115
116 auto test_packet = GetCapRequestTestPacket::Make();
117 builder->Serialize(test_packet);
118 ASSERT_EQ(test_packet->GetData(), get_capabilities_response_events_supported);
119 }
120
121 } // namespace avrcp
122 } // namespace bluetooth