summaryrefslogtreecommitdiff
path: root/system/packet/tests/avrcp/get_capabilities_packet_test.cc
blob: 4e4c195d08e3d150d0400da0c5d76d0869988024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/silent_death_test.h>
#include <gtest/gtest.h>

#include "avrcp_test_packets.h"
#include "capabilities_packet.h"
#include "packet_test_helper.h"

namespace bluetooth {
namespace avrcp {

using GetCapRequestTestPacket = TestPacketType<GetCapabilitiesRequest>;

// Test parsing a GetCapabilities Request
TEST(GetCapabilitiesRequestPacketTest, getterTest) {
  auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);

  ASSERT_EQ(test_packet->GetCapabilityRequested(),
            Capability::EVENTS_SUPPORTED);
}

TEST(GetCapabilitiesRequestPacketTest, validTest) {
  auto test_packet = GetCapRequestTestPacket::Make(get_capabilities_request);
  ASSERT_TRUE(test_packet->IsValid());
}

TEST(GetCapabilitiesRequestPacketTest, invalidTest) {
  std::vector<uint8_t> packet_copy = get_capabilities_request;
  packet_copy.push_back(0x00);
  auto test_packet = GetCapRequestTestPacket::Make(packet_copy);
  ASSERT_FALSE(test_packet->IsValid());

  std::vector<uint8_t> short_packet = {
      0, 1, 2, 3, 4, 5, 6,
  };
  test_packet = GetCapRequestTestPacket::Make(short_packet);
  ASSERT_FALSE(test_packet->IsValid());
}

// Test that the length returned by the builder grows correctlyas fields are
// added
TEST(GetCapabilityResponseBuilder, builderLengthTest) {
  auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
  ASSERT_EQ(builder->size(), 15u);
  builder->AddCompanyId(0x000001);
  ASSERT_EQ(builder->size(), 18u);
  builder->AddCompanyId(0x000002);
  ASSERT_EQ(builder->size(), 21u);

  builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
      Event::PLAYBACK_STATUS_CHANGED);
  ASSERT_EQ(builder->size(), 13u);
  builder->AddEvent(Event::TRACK_CHANGED);
  ASSERT_EQ(builder->size(), 14u);
  builder->AddEvent(Event::PLAYBACK_POS_CHANGED);
  ASSERT_EQ(builder->size(), 15u);
}

// Check to see that adding the same value multiple times does nothing
TEST(GetCapabilityResponseBuilder, duplicateAddTest) {
  auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);
  ASSERT_EQ(builder->size(), 15u);
  builder->AddCompanyId(0x000000);
  ASSERT_EQ(builder->size(), 15u);

  builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
      Event::PLAYBACK_STATUS_CHANGED);
  ASSERT_EQ(builder->size(), 13u);
  builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED);
  ASSERT_EQ(builder->size(), 13u);
}

// Test that trying to to add the wrong type of field to a builder causes death
TEST(GetCapabilityResponseBuilderDeathTest, mismatchAddDeathTest) {
  auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x000000);

  {
    // this will silent SIGABRT sent in ASSERT_DEATH below scop
    ScopedSilentDeath _silentDeath;

    ASSERT_DEATH(builder->AddEvent(Event::PLAYBACK_STATUS_CHANGED),
                 "capability_ == Capability::EVENTS_SUPPORTED");
  }

  builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
      Event::PLAYBACK_STATUS_CHANGED);

  // this will silent SIGABRT sent in ASSERT_DEATH below
  ScopedSilentDeath _silentDeath;

  ASSERT_DEATH(builder->AddCompanyId(0x000000),
               "capability_ == Capability::COMPANY_ID");
}

// Test building a GetCapabilities Response to a Company ID request
TEST(GetCapabilityResponseBuilder, comanyIdBuilderTest) {
  auto builder = GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(0x002345);
  builder->AddCompanyId(BLUETOOTH_COMPANY_ID);

  auto test_packet = GetCapRequestTestPacket::Make();
  builder->Serialize(test_packet);
  ASSERT_EQ(test_packet->GetData(), get_capabilities_response_company_id);
}

// Test building a GetCapabilities Response to an Events Supported request
TEST(GetCapabilityResponseBuilder, eventsSupportedBuilderTest) {
  auto builder = GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(
      Event::PLAYBACK_STATUS_CHANGED);
  builder->AddEvent(Event::TRACK_CHANGED);
  builder->AddEvent(Event::PLAYBACK_POS_CHANGED);

  auto test_packet = GetCapRequestTestPacket::Make();
  builder->Serialize(test_packet);
  ASSERT_EQ(test_packet->GetData(), get_capabilities_response_events_supported);
}

}  // namespace avrcp
}  // namespace bluetooth