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