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