1/*
2 * Copyright (C) 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#define LOG_TAG "bluetooth_a2dp_hidl_hal_test"
18
19#include <android-base/logging.h>
20#include <android/hardware/bluetooth/a2dp/1.0/IBluetoothAudioHost.h>
21#include <android/hardware/bluetooth/a2dp/1.0/IBluetoothAudioOffload.h>
22#include <hardware/bluetooth.h>
23#include <utils/Log.h>
24
25#include <VtsHalHidlTargetCallbackBase.h>
26#include <VtsHalHidlTargetTestBase.h>
27#include <VtsHalHidlTargetTestEnvBase.h>
28
29using ::android::hardware::bluetooth::a2dp::V1_0::IBluetoothAudioHost;
30using ::android::hardware::bluetooth::a2dp::V1_0::IBluetoothAudioOffload;
31using ::android::hardware::bluetooth::a2dp::V1_0::Status;
32using ::android::hardware::bluetooth::a2dp::V1_0::CodecType;
33using ::android::hardware::bluetooth::a2dp::V1_0::SampleRate;
34using ::android::hardware::bluetooth::a2dp::V1_0::BitsPerSample;
35using ::android::hardware::bluetooth::a2dp::V1_0::ChannelMode;
36using ::android::hardware::bluetooth::a2dp::V1_0::CodecConfiguration;
37using ::android::hardware::Return;
38using ::android::hardware::Void;
39using ::android::sp;
40
41// Test environment for Bluetooth HIDL A2DP HAL.
42class BluetoothA2dpHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
43   public:
44    // get the test environment singleton
45    static BluetoothA2dpHidlEnvironment* Instance() {
46        static BluetoothA2dpHidlEnvironment* instance = new BluetoothA2dpHidlEnvironment;
47        return instance;
48    }
49
50    virtual void registerTestServices() override { registerTestService<IBluetoothAudioOffload>(); }
51
52   private:
53    BluetoothA2dpHidlEnvironment() {}
54};
55
56// The main test class for Bluetooth A2DP HIDL HAL.
57class BluetoothA2dpHidlTest : public ::testing::VtsHalHidlTargetTestBase {
58   public:
59    virtual void SetUp() override {
60        // currently test passthrough mode only
61        audio_offload = ::testing::VtsHalHidlTargetTestBase::getService<IBluetoothAudioOffload>(
62            BluetoothA2dpHidlEnvironment::Instance()->getServiceName<IBluetoothAudioOffload>());
63        ASSERT_NE(audio_offload, nullptr);
64
65        audio_host = new BluetoothAudioHost(*this);
66        ASSERT_NE(audio_host, nullptr);
67
68        codec.codecType = CodecType::AAC;
69        codec.sampleRate = SampleRate::RATE_44100;
70        codec.bitsPerSample = BitsPerSample::BITS_16;
71        codec.channelMode = ChannelMode::STEREO;
72        codec.encodedAudioBitrate = 320000;
73        codec.peerMtu = 1000;
74    }
75
76    virtual void TearDown() override {}
77
78    // A simple test implementation of IBluetoothAudioHost.
79    class BluetoothAudioHost
80        : public ::testing::VtsHalHidlTargetCallbackBase<BluetoothA2dpHidlTest>,
81          public IBluetoothAudioHost {
82        BluetoothA2dpHidlTest& parent_;
83
84       public:
85        BluetoothAudioHost(BluetoothA2dpHidlTest& parent) : parent_(parent){};
86        virtual ~BluetoothAudioHost() = default;
87
88        Return<void> startStream() override {
89            parent_.audio_offload->streamStarted(Status::SUCCESS);
90            return Void();
91        };
92
93        Return<void> suspendStream() override {
94            parent_.audio_offload->streamSuspended(Status::SUCCESS);
95            return Void();
96        };
97
98        Return<void> stopStream() override { return Void(); };
99    };
100
101    // audio_host is for the Audio HAL to send stream start/suspend/stop commands to Bluetooth
102    sp<IBluetoothAudioHost> audio_host;
103    // audio_offload is for the Bluetooth HAL to report session started/ended and handled audio
104    // stream started/suspended
105    sp<IBluetoothAudioOffload> audio_offload;
106    // codec is the currently used codec
107    CodecConfiguration codec;
108};
109
110// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
111TEST_F(BluetoothA2dpHidlTest, InitializeAndClose) {}
112
113// Test start and end session
114TEST_F(BluetoothA2dpHidlTest, StartAndEndSession) {
115    EXPECT_EQ(Status::SUCCESS, audio_offload->startSession(audio_host, codec));
116    audio_offload->endSession();
117}
118
119int main(int argc, char** argv) {
120    ::testing::AddGlobalTestEnvironment(BluetoothA2dpHidlEnvironment::Instance());
121    ::testing::InitGoogleTest(&argc, argv);
122    BluetoothA2dpHidlEnvironment::Instance()->init(&argc, argv);
123    int status = RUN_ALL_TESTS();
124    LOG(INFO) << "Test result = " << status;
125    return status;
126}
127