• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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 #include "mmc/codec_server/hfp_lc3_mmc_encoder.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <cerrno>
23 #include <cstdint>
24 
25 #include "mmc/codec_server/lc3_utils.h"
26 #include "mmc/proto/mmc_config.pb.h"
27 #include "mmc/test/mock/mock_embdrv_lc3.h"
28 #include "test/common/mock_functions.h"
29 #include "test/mock/mock_osi_allocator.h"
30 
31 namespace {
32 
33 using ::testing::Contains;
34 using ::testing::Each;
35 using ::testing::Ne;
36 using ::testing::Test;
37 
38 const int kInputLen = mmc::HFP_LC3_PCM_BYTES;
39 const int kOutputLen = mmc::HFP_LC3_PKT_FRAME_LEN;
40 const uint8_t kInputBuf[kInputLen] = {0};
41 static uint8_t kOutputBuf[kOutputLen] = {0};
42 
43 class HfpLc3EncoderTest : public Test {
44 public:
45 protected:
SetUp()46   void SetUp() override {
47     reset_mock_function_count_map();
48     encoder_ = std::make_unique<mmc::HfpLc3Encoder>();
49   }
TearDown()50   void TearDown() override { encoder_.release(); }
51   std::unique_ptr<mmc::HfpLc3Encoder> encoder_ = nullptr;
52 };
53 
54 class HfpLc3EncoderWithInitTest : public HfpLc3EncoderTest {
55 public:
56 protected:
SetUp()57   void SetUp() override {
58     test::mock::osi_allocator::osi_malloc.body = [&](size_t size) {
59       this->lc3_encoder_ = new struct lc3_encoder;
60       return (void*)this->lc3_encoder_;
61     };
62     test::mock::embdrv_lc3::lc3_setup_encoder.body =
63             [this](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return this->lc3_encoder_; };
64     test::mock::osi_allocator::osi_free_and_reset.body = [&](void** p_ptr) {
65       delete this->lc3_encoder_;
66       lc3_encoder_ = nullptr;
67       *p_ptr = nullptr;
68       return;
69     };
70     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 1);
71 
72     HfpLc3EncoderTest::SetUp();
73     mmc::ConfigParam config;
74     *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
75     ASSERT_EQ(encoder_->init(config), mmc::HFP_LC3_PCM_BYTES);
76   }
TearDown()77   void TearDown() override {
78     HfpLc3EncoderTest::TearDown();
79     test::mock::embdrv_lc3::lc3_setup_encoder = {};
80     test::mock::osi_allocator::osi_malloc = {};
81     test::mock::osi_allocator::osi_free_and_reset = {};
82     std::fill(kOutputBuf, kOutputBuf + kOutputLen, 0);
83   }
84   struct lc3_encoder* lc3_encoder_ = nullptr;
85 };
86 
TEST_F(HfpLc3EncoderTest,InitWrongCodec)87 TEST_F(HfpLc3EncoderTest, InitWrongCodec) {
88   mmc::ConfigParam config;
89   *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
90 
91   int ret = encoder_->init(config);
92   EXPECT_EQ(ret, -EINVAL);
93   EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 0);
94 }
95 
TEST_F(HfpLc3EncoderTest,InitWrongConfig)96 TEST_F(HfpLc3EncoderTest, InitWrongConfig) {
97   mmc::ConfigParam config;
98   *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
99 
100   // lc3_setup_encoder failed due to wrong parameters (returned nullptr).
101   test::mock::embdrv_lc3::lc3_setup_encoder.body = [](int dt_us, int sr_hz, int sr_pcm_hz,
102                                                       void* mem) { return nullptr; };
103 
104   int ret = encoder_->init(config);
105   EXPECT_EQ(ret, -EINVAL);
106   EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 1);
107 
108   test::mock::embdrv_lc3::lc3_setup_encoder = {};
109 }
110 
TEST_F(HfpLc3EncoderTest,InitSuccess)111 TEST_F(HfpLc3EncoderTest, InitSuccess) {
112   mmc::ConfigParam config;
113   *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
114 
115   // lc3_setup_encoder returns encoder instance pointer.
116   struct lc3_encoder lc3_encoder;
117   test::mock::embdrv_lc3::lc3_setup_encoder.body =
118           [&lc3_encoder](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return &lc3_encoder; };
119 
120   int ret = encoder_->init(config);
121   EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES);
122   EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 1);
123 
124   test::mock::embdrv_lc3::lc3_setup_encoder = {};
125 }
126 
TEST_F(HfpLc3EncoderWithInitTest,CleanUp)127 TEST_F(HfpLc3EncoderWithInitTest, CleanUp) {
128   encoder_->cleanup();
129   EXPECT_EQ(get_func_call_count("osi_free_and_reset"), 1);
130 }
131 
TEST_F(HfpLc3EncoderTest,TranscodeNullBuffer)132 TEST_F(HfpLc3EncoderTest, TranscodeNullBuffer) {
133   // Null input buffer.
134   int ret = encoder_->transcode(nullptr, 0, kOutputBuf, kOutputLen);
135   EXPECT_EQ(ret, -EINVAL);
136   EXPECT_EQ(get_func_call_count("lc3_encode"), 0);
137 
138   // Null output buffer.
139   ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, nullptr, 0);
140   EXPECT_EQ(ret, -EINVAL);
141   EXPECT_EQ(get_func_call_count("lc3_encode"), 0);
142 }
143 
TEST_F(HfpLc3EncoderWithInitTest,TranscodeWrongParam)144 TEST_F(HfpLc3EncoderWithInitTest, TranscodeWrongParam) {
145   // lc3_encode failed (returned non-zero value).
146   test::mock::embdrv_lc3::lc3_encode.return_value = 1;
147 
148   int ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf, kOutputLen);
149   EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
150   EXPECT_THAT(kOutputBuf, Each(0));
151   EXPECT_EQ(get_func_call_count("lc3_encode"), 1);
152 
153   test::mock::embdrv_lc3::lc3_encode = {};
154 }
155 
TEST_F(HfpLc3EncoderWithInitTest,TranscodeSuccess)156 TEST_F(HfpLc3EncoderWithInitTest, TranscodeSuccess) {
157   // lc3_encode succeeded (return zero value).
158   test::mock::embdrv_lc3::lc3_encode.return_value = 0;
159 
160   int ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf, kOutputLen);
161   EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
162   EXPECT_THAT(kOutputBuf, Contains(Ne(0)));
163   EXPECT_EQ(get_func_call_count("lc3_encode"), 1);
164 
165   test::mock::embdrv_lc3::lc3_encode = {};
166 }
167 
168 }  // namespace
169