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