1 //
2 //  Copyright (C) 2017 Google, Inc.
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 <base/macros.h>
20 #include <base/observer_list.h>
21 
22 #include "abstract_observer_list.h"
23 #include "service/hal/bluetooth_av_interface.h"
24 
25 namespace bluetooth {
26 namespace hal {
27 
28 class FakeBluetoothAvInterface : public BluetoothAvInterface {
29  public:
30   // Handles HAL Bluetooth A2DP API calls for testing. Test code can provide a
31   // fake or mock implementation of this and all calls will be routed to it.
32   class TestA2dpSourceHandler {
33    public:
34     virtual bt_status_t Connect(RawAddress bda) = 0;
35     virtual bt_status_t Disconnect(RawAddress bda) = 0;
36 
37    protected:
38     virtual ~TestA2dpSourceHandler() = default;
39   };
40   class TestA2dpSinkHandler {
41    public:
42     virtual bt_status_t Connect(RawAddress bda) = 0;
43     virtual bt_status_t Disconnect(RawAddress bda) = 0;
44     virtual void SetAudioFocusState(int focus_state) = 0;
45     virtual void SetAudioTrackGain(float gain) = 0;
46 
47    protected:
48     virtual ~TestA2dpSinkHandler() = default;
49   };
50 
51   // Constructs the fake with the given handlers. Implementations can
52   // provide their own handlers or simply pass "nullptr" for the default
53   // behavior in which BT_STATUS_FAIL will be returned from all calls.
54   FakeBluetoothAvInterface(
55       std::shared_ptr<TestA2dpSourceHandler> a2dp_source_handler);
56   FakeBluetoothAvInterface(
57       std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler);
58   ~FakeBluetoothAvInterface();
59 
60   // The methods below can be used to notify observers with certain events and
61   // given parameters.
62 
63   // A2DP common callbacks
64   void NotifyConnectionState(const RawAddress& bda,
65                              btav_connection_state_t state);
66   void NotifyAudioState(const RawAddress& bda, btav_audio_state_t state);
67   // A2DP source callbacks
68   void NotifyAudioConfig(
69       const RawAddress& bda, const btav_a2dp_codec_config_t& codec_config,
70       const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
71       const std::vector<btav_a2dp_codec_config_t>
72           codecs_selectable_capabilities);
73   bool QueryMandatoryCodecPreferred(const RawAddress& bda);
74   // A2DP sink callbacks
75   void NotifyAudioConfig(const RawAddress& bda, uint32_t sample_rate,
76                          uint8_t channel_count);
77 
78   // BluetoothAvInterface overrides:
79   bool A2dpSourceEnable(
80       std::vector<btav_a2dp_codec_config_t> codec_priorities) override;
81   void A2dpSourceDisable() override;
82   bool A2dpSinkEnable() override;
83   void A2dpSinkDisable() override;
84   void AddA2dpSourceObserver(A2dpSourceObserver* observer) override;
85   void RemoveA2dpSourceObserver(A2dpSourceObserver* observer) override;
86   void AddA2dpSinkObserver(A2dpSinkObserver* observer) override;
87   void RemoveA2dpSinkObserver(A2dpSinkObserver* observer) override;
88   const btav_source_interface_t* GetA2dpSourceHALInterface() override;
89   const btav_sink_interface_t* GetA2dpSinkHALInterface() override;
90 
91  private:
92   btbase::AbstractObserverList<A2dpSourceObserver> a2dp_source_observers_;
93   btbase::AbstractObserverList<A2dpSinkObserver> a2dp_sink_observers_;
94 
95   DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAvInterface);
96 };
97 
98 }  // namespace hal
99 }  // namespace bluetooth
100