[android/platform-hardware-interfaces.git] / bluetooth / a2dp / 1.0 / vts / functional / VtsHalBluetoothA2dpV1_0TargetTest.cpp
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 */
17 #define LOG_TAG "bluetooth_a2dp_hidl_hal_test"
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>
25 #include <VtsHalHidlTargetCallbackBase.h>
26 #include <VtsHalHidlTargetTestBase.h>
27 #include <VtsHalHidlTargetTestEnvBase.h>
29 using ::android::hardware::bluetooth::a2dp::V1_0::IBluetoothAudioHost;
30 using ::android::hardware::bluetooth::a2dp::V1_0::IBluetoothAudioOffload;
31 using ::android::hardware::bluetooth::a2dp::V1_0::Status;
32 using ::android::hardware::bluetooth::a2dp::V1_0::CodecType;
33 using ::android::hardware::bluetooth::a2dp::V1_0::SampleRate;
34 using ::android::hardware::bluetooth::a2dp::V1_0::BitsPerSample;
35 using ::android::hardware::bluetooth::a2dp::V1_0::ChannelMode;
36 using ::android::hardware::bluetooth::a2dp::V1_0::CodecConfiguration;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::sp;
41 // Test environment for Bluetooth HIDL A2DP HAL.
42 class 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 }
50 virtual void registerTestServices() override { registerTestService<IBluetoothAudioOffload>(); }
52 private:
53 BluetoothA2dpHidlEnvironment() {}
54 };
56 // The main test class for Bluetooth A2DP HIDL HAL.
57 class 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);
65 audio_host = new BluetoothAudioHost(*this);
66 ASSERT_NE(audio_host, nullptr);
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 }
76 virtual void TearDown() override {}
78 // A simple test implementation of IBluetoothAudioHost.
79 class BluetoothAudioHost
80 : public ::testing::VtsHalHidlTargetCallbackBase<BluetoothA2dpHidlTest>,
81 public IBluetoothAudioHost {
82 BluetoothA2dpHidlTest& parent_;
84 public:
85 BluetoothAudioHost(BluetoothA2dpHidlTest& parent) : parent_(parent){};
86 virtual ~BluetoothAudioHost() = default;
88 Return<void> startStream() override {
89 parent_.audio_offload->streamStarted(Status::SUCCESS);
90 return Void();
91 };
93 Return<void> suspendStream() override {
94 parent_.audio_offload->streamSuspended(Status::SUCCESS);
95 return Void();
96 };
98 Return<void> stopStream() override { return Void(); };
99 };
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 };
110 // Empty test: Initialize()/Close() are called in SetUp()/TearDown().
111 TEST_F(BluetoothA2dpHidlTest, InitializeAndClose) {}
113 // Test start and end session
114 TEST_F(BluetoothA2dpHidlTest, StartAndEndSession) {
115 EXPECT_EQ(Status::SUCCESS, audio_offload->startSession(audio_host, codec));
116 audio_offload->endSession();
117 }
119 int 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 }