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) {
64 return this->lc3_encoder_;
65 };
66 test::mock::osi_allocator::osi_free_and_reset.body = [&](void** p_ptr) {
67 delete this->lc3_encoder_;
68 lc3_encoder_ = nullptr;
69 *p_ptr = nullptr;
70 return;
71 };
72 std::fill(kOutputBuf, kOutputBuf + kOutputLen, 1);
73
74 HfpLc3EncoderTest::SetUp();
75 mmc::ConfigParam config;
76 *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
77 ASSERT_EQ(encoder_->init(config), mmc::HFP_LC3_PCM_BYTES);
78 }
TearDown()79 void TearDown() override {
80 HfpLc3EncoderTest::TearDown();
81 test::mock::embdrv_lc3::lc3_setup_encoder = {};
82 test::mock::osi_allocator::osi_malloc = {};
83 test::mock::osi_allocator::osi_free_and_reset = {};
84 std::fill(kOutputBuf, kOutputBuf + kOutputLen, 0);
85 }
86 struct lc3_encoder* lc3_encoder_ = nullptr;
87 };
88
TEST_F(HfpLc3EncoderTest,InitWrongCodec)89 TEST_F(HfpLc3EncoderTest, InitWrongCodec) {
90 mmc::ConfigParam config;
91 *config.mutable_hfp_lc3_decoder_param() = mmc::Lc3Param();
92
93 int ret = encoder_->init(config);
94 EXPECT_EQ(ret, -EINVAL);
95 EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 0);
96 }
97
TEST_F(HfpLc3EncoderTest,InitWrongConfig)98 TEST_F(HfpLc3EncoderTest, InitWrongConfig) {
99 mmc::ConfigParam config;
100 *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
101
102 // lc3_setup_encoder failed due to wrong parameters (returned nullptr).
103 test::mock::embdrv_lc3::lc3_setup_encoder.body =
104 [](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) { return nullptr; };
105
106 int ret = encoder_->init(config);
107 EXPECT_EQ(ret, -EINVAL);
108 EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 1);
109
110 test::mock::embdrv_lc3::lc3_setup_encoder = {};
111 }
112
TEST_F(HfpLc3EncoderTest,InitSuccess)113 TEST_F(HfpLc3EncoderTest, InitSuccess) {
114 mmc::ConfigParam config;
115 *config.mutable_hfp_lc3_encoder_param() = mmc::Lc3Param();
116
117 // lc3_setup_encoder returns encoder instance pointer.
118 struct lc3_encoder lc3_encoder;
119 test::mock::embdrv_lc3::lc3_setup_encoder.body =
120 [&lc3_encoder](int dt_us, int sr_hz, int sr_pcm_hz, void* mem) {
121 return &lc3_encoder;
122 };
123
124 int ret = encoder_->init(config);
125 EXPECT_EQ(ret, mmc::HFP_LC3_PCM_BYTES);
126 EXPECT_EQ(get_func_call_count("lc3_setup_encoder"), 1);
127
128 test::mock::embdrv_lc3::lc3_setup_encoder = {};
129 }
130
TEST_F(HfpLc3EncoderWithInitTest,CleanUp)131 TEST_F(HfpLc3EncoderWithInitTest, CleanUp) {
132 encoder_->cleanup();
133 EXPECT_EQ(get_func_call_count("osi_free_and_reset"), 1);
134 }
135
TEST_F(HfpLc3EncoderTest,TranscodeNullBuffer)136 TEST_F(HfpLc3EncoderTest, TranscodeNullBuffer) {
137 // Null input buffer.
138 int ret = encoder_->transcode(nullptr, 0, kOutputBuf, kOutputLen);
139 EXPECT_EQ(ret, -EINVAL);
140 EXPECT_EQ(get_func_call_count("lc3_encode"), 0);
141
142 // Null output buffer.
143 ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, nullptr, 0);
144 EXPECT_EQ(ret, -EINVAL);
145 EXPECT_EQ(get_func_call_count("lc3_encode"), 0);
146 }
147
TEST_F(HfpLc3EncoderWithInitTest,TranscodeWrongParam)148 TEST_F(HfpLc3EncoderWithInitTest, TranscodeWrongParam) {
149 // lc3_encode failed (returned non-zero value).
150 test::mock::embdrv_lc3::lc3_encode.return_value = 1;
151
152 int ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf,
153 kOutputLen);
154 EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
155 EXPECT_THAT(kOutputBuf, Each(0));
156 EXPECT_EQ(get_func_call_count("lc3_encode"), 1);
157
158 test::mock::embdrv_lc3::lc3_encode = {};
159 }
160
TEST_F(HfpLc3EncoderWithInitTest,TranscodeSuccess)161 TEST_F(HfpLc3EncoderWithInitTest, TranscodeSuccess) {
162 // lc3_encode succeeded (return zero value).
163 test::mock::embdrv_lc3::lc3_encode.return_value = 0;
164
165 int ret = encoder_->transcode((uint8_t*)kInputBuf, kInputLen, kOutputBuf,
166 kOutputLen);
167 EXPECT_EQ(ret, mmc::HFP_LC3_PKT_FRAME_LEN);
168 EXPECT_THAT(kOutputBuf, Contains(Ne(0)));
169 EXPECT_EQ(get_func_call_count("lc3_encode"), 1);
170
171 test::mock::embdrv_lc3::lc3_encode = {};
172 }
173
174 } // namespace
175