• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <gmock/gmock.h>
20 #include <type_traits>
21 
22 #include "avrcp.h"
23 #include "avrcp_internal.h"
24 #include "avrcp_packet.h"
25 #include "tests/packet_test_helper.h"
26 
27 namespace bluetooth {
28 namespace avrcp {
29 
30 using AvrcpResponse = std::unique_ptr<::bluetooth::PacketBuilder>;
31 using TestAvrcpPacket = TestPacketType<Packet>;
32 using TestBrowsePacket = TestPacketType<BrowsePacket>;
33 
34 class MockMediaInterface : public MediaInterface {
35  public:
36   MOCK_METHOD2(SendKeyEvent, void(uint8_t, KeyState));
37   MOCK_METHOD1(GetSongInfo, void(MediaInterface::SongInfoCallback));
38   MOCK_METHOD1(GetPlayStatus, void(MediaInterface::PlayStatusCallback));
39   MOCK_METHOD1(GetNowPlayingList, void(MediaInterface::NowPlayingCallback));
40   MOCK_METHOD1(GetMediaPlayerList, void(MediaInterface::MediaListCallback));
41   MOCK_METHOD3(GetFolderItems, void(uint16_t, std::string,
42                                     MediaInterface::FolderItemsCallback));
43   MOCK_METHOD2(SetBrowsedPlayer,
44                void(uint16_t, MediaInterface::SetBrowsedPlayerCallback));
45   MOCK_METHOD3(PlayItem, void(uint16_t, bool, std::string));
46   MOCK_METHOD1(SetActiveDevice, void(const RawAddress&));
47   MOCK_METHOD1(RegisterUpdateCallback, void(MediaCallbacks*));
48   MOCK_METHOD1(UnregisterUpdateCallback, void(MediaCallbacks*));
49 };
50 
51 class MockVolumeInterface : public VolumeInterface {
52  public:
53   MOCK_METHOD1(DeviceConnected, void(const RawAddress&));
54   MOCK_METHOD2(DeviceConnected, void(const RawAddress&, VolumeChangedCb));
55   MOCK_METHOD1(DeviceDisconnected, void(const RawAddress&));
56   MOCK_METHOD1(SetVolume, void(int8_t));
57 };
58 
59 class MockAvrcpInterface : public AvrcpInterface {
60  public:
61   MOCK_METHOD7(AddRecord, uint16_t(uint16_t, const char*, const char*, uint16_t,
62                                    uint32_t, bool, uint16_t));
63   MOCK_METHOD4(FindService, uint16_t(uint16_t, const RawAddress&,
64                                      tAVRC_SDP_DB_PARAMS*, tAVRC_FIND_CBACK));
65   MOCK_METHOD3(Open, uint16_t(uint8_t*, tAVRC_CONN_CB*, const RawAddress&));
66   MOCK_METHOD2(OpenBrowse, uint16_t(uint8_t, uint8_t));
67   MOCK_METHOD1(GetPeerMtu, uint16_t(uint8_t));
68   MOCK_METHOD1(GetBrowseMtu, uint16_t(uint8_t));
69   MOCK_METHOD1(Close, uint16_t(uint8_t));
70   MOCK_METHOD1(CloseBrowse, uint16_t(uint8_t));
71   MOCK_METHOD4(MsgReq, uint16_t(uint8_t, uint8_t, uint8_t, BT_HDR*));
72 };
73 
74 class MockA2dpInterface : public A2dpInterface {
75  public:
76   MOCK_METHOD1(event_open, void(const RawAddress&));
77   MOCK_METHOD1(event_close, void(const RawAddress&));
78   MOCK_METHOD0(active_peer, RawAddress());
79 };
80 
81 class MockSdpInterface : public SdpInterface {
82  public:
83   MOCK_METHOD6(InitDiscoveryDb,
84                bool(tSDP_DISCOVERY_DB*, uint32_t, uint16_t,
85                     const bluetooth::Uuid*, uint16_t, uint16_t*));
86   MOCK_METHOD3(ServiceSearchAttributeRequest,
87                bool(const RawAddress&, tSDP_DISCOVERY_DB*, tSDP_DISC_CMPL_CB*));
88   MOCK_METHOD3(FindServiceInDb,
89                tSDP_DISC_REC*(tSDP_DISCOVERY_DB*, uint16_t, t_sdp_disc_rec*));
90   MOCK_METHOD2(FindAttributeInRec, tSDP_DISC_ATTR*(t_sdp_disc_rec*, uint16_t));
91   MOCK_METHOD3(FindProfileVersionInRec,
92                bool(t_sdp_disc_rec*, uint16_t, uint16_t*));
93 };
94 
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (input))95 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
96                 AND_1_VALUE_PARAMS(input)) {
97   ::testing::get<k>(args).Run(input);
98 }
99 
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_2_VALUE_PARAMS (a,b))100 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
101                 AND_2_VALUE_PARAMS(a, b)) {
102   ::testing::get<k>(args).Run(a, b);
103 }
104 
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_3_VALUE_PARAMS (a,b,c))105 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
106                 AND_3_VALUE_PARAMS(a, b, c)) {
107   ::testing::get<k>(args).Run(a, b, c);
108 }
109 
110 template <class PacketType>
111 class PacketMatcher : public ::testing::MatcherInterface<const AvrcpResponse&> {
112  public:
113   AvrcpResponse pkt_to_compare_to_;
114 
PacketMatcher(AvrcpResponse && pkt)115   PacketMatcher(AvrcpResponse&& pkt) { pkt_to_compare_to_ = std::move(pkt); }
116 
MatchAndExplain(const AvrcpResponse & r,::testing::MatchResultListener * listener)117   bool MatchAndExplain(
118       const AvrcpResponse& r,
119       ::testing::MatchResultListener* listener) const override {
120     auto packet1 = TestPacketType<PacketType>::Make();
121     r->Serialize(packet1);
122 
123     auto packet2 = TestPacketType<PacketType>::Make();
124     pkt_to_compare_to_->Serialize(packet2);
125 
126     if (packet1->GetData() != packet2->GetData()) {
127       *listener << "\nPacket to compare to: \n";
128       *listener << packet2->ToString();
129       *listener << "\nActual packet: \n";
130       *listener << packet1->ToString();
131     }
132 
133     return packet1->GetData() == packet2->GetData();
134   }
135 
DescribeTo(::std::ostream * os)136   void DescribeTo(::std::ostream* os) const override { *os << "Packets match"; }
137 };
138 
matchPacket(std::unique_ptr<PacketBuilder> && arg)139 inline ::testing::Matcher<const AvrcpResponse&> matchPacket(
140     std::unique_ptr<PacketBuilder>&& arg) {
141   return MakeMatcher(new PacketMatcher<Packet>(std::move(arg)));
142 }
143 
matchPacket(std::unique_ptr<BrowsePacketBuilder> && arg)144 inline ::testing::Matcher<const AvrcpResponse&> matchPacket(
145     std::unique_ptr<BrowsePacketBuilder>&& arg) {
146   return MakeMatcher(new PacketMatcher<BrowsePacket>(std::move(arg)));
147 }
148 
149 }  // namespace avrcp
150 }  // namespace bluetooth
151