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 MOCK_METHOD1(is_peer_in_silence_mode, bool(const RawAddress&));
80 };
81
82 class MockSdpInterface : public SdpInterface {
83 public:
84 MOCK_METHOD6(InitDiscoveryDb,
85 bool(tSDP_DISCOVERY_DB*, uint32_t, uint16_t,
86 const bluetooth::Uuid*, uint16_t, uint16_t*));
87 MOCK_METHOD3(ServiceSearchAttributeRequest,
88 bool(const RawAddress&, tSDP_DISCOVERY_DB*, tSDP_DISC_CMPL_CB*));
89 MOCK_METHOD3(FindServiceInDb,
90 tSDP_DISC_REC*(tSDP_DISCOVERY_DB*, uint16_t, t_sdp_disc_rec*));
91 MOCK_METHOD2(FindAttributeInRec, tSDP_DISC_ATTR*(t_sdp_disc_rec*, uint16_t));
92 MOCK_METHOD3(FindProfileVersionInRec,
93 bool(t_sdp_disc_rec*, uint16_t, uint16_t*));
94 };
95
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_1_VALUE_PARAMS (input))96 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
97 AND_1_VALUE_PARAMS(input)) {
98 ::testing::get<k>(args).Run(input);
99 }
100
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_2_VALUE_PARAMS (a,b))101 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
102 AND_2_VALUE_PARAMS(a, b)) {
103 ::testing::get<k>(args).Run(a, b);
104 }
105
ACTION_TEMPLATE(InvokeCb,HAS_1_TEMPLATE_PARAMS (int,k),AND_3_VALUE_PARAMS (a,b,c))106 ACTION_TEMPLATE(InvokeCb, HAS_1_TEMPLATE_PARAMS(int, k),
107 AND_3_VALUE_PARAMS(a, b, c)) {
108 ::testing::get<k>(args).Run(a, b, c);
109 }
110
111 template <class PacketType>
112 class PacketMatcher : public ::testing::MatcherInterface<const AvrcpResponse&> {
113 public:
114 AvrcpResponse pkt_to_compare_to_;
115
PacketMatcher(AvrcpResponse && pkt)116 PacketMatcher(AvrcpResponse&& pkt) { pkt_to_compare_to_ = std::move(pkt); }
117
MatchAndExplain(const AvrcpResponse & r,::testing::MatchResultListener * listener)118 bool MatchAndExplain(
119 const AvrcpResponse& r,
120 ::testing::MatchResultListener* listener) const override {
121 auto packet1 = TestPacketType<PacketType>::Make();
122 r->Serialize(packet1);
123
124 auto packet2 = TestPacketType<PacketType>::Make();
125 pkt_to_compare_to_->Serialize(packet2);
126
127 if (packet1->GetData() != packet2->GetData()) {
128 *listener << "\nPacket to compare to: \n";
129 *listener << packet2->ToString();
130 *listener << "\nActual packet: \n";
131 *listener << packet1->ToString();
132 }
133
134 return packet1->GetData() == packet2->GetData();
135 }
136
DescribeTo(::std::ostream * os)137 void DescribeTo(::std::ostream* os) const override { *os << "Packets match"; }
138 };
139
matchPacket(std::unique_ptr<PacketBuilder> && arg)140 inline ::testing::Matcher<const AvrcpResponse&> matchPacket(
141 std::unique_ptr<PacketBuilder>&& arg) {
142 return MakeMatcher(new PacketMatcher<Packet>(std::move(arg)));
143 }
144
matchPacket(std::unique_ptr<BrowsePacketBuilder> && arg)145 inline ::testing::Matcher<const AvrcpResponse&> matchPacket(
146 std::unique_ptr<BrowsePacketBuilder>&& arg) {
147 return MakeMatcher(new PacketMatcher<BrowsePacket>(std::move(arg)));
148 }
149
150 } // namespace avrcp
151 } // namespace bluetooth
152