1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include "audio_utils.h"
20 #include "audio_source_clock.h"
21 #include "capturer_clock_manager.h"
22
23 using namespace testing::ext;
24 using namespace std;
25
26 namespace OHOS {
27 namespace AudioStandard {
28
29 constexpr uint64_t MOCK_POSITION_INC = 960 * 2 * 2;
30 constexpr uint32_t MOCK_SAMPLE_RATE = 48'000;
31 constexpr AudioSampleFormat MOCK_FORMAT = AudioSampleFormat::SAMPLE_S16LE;
32 constexpr uint32_t MOCK_CHANNEL = 2;
33
34 class AudioSourceClockUnitTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
SetUp()38 virtual void SetUp() {}
TearDown()39 virtual void TearDown() {}
40 private:
41 static std::shared_ptr<AudioSourceClock> srcClock_;
42 static std::shared_ptr<CapturerClock> capturerClock_;
43 };
44
45 std::shared_ptr<AudioSourceClock> AudioSourceClockUnitTest::srcClock_ = nullptr;
46 std::shared_ptr<CapturerClock> AudioSourceClockUnitTest::capturerClock_ = nullptr;
47
SetUpTestCase()48 void AudioSourceClockUnitTest::SetUpTestCase()
49 {
50 srcClock_ = std::make_shared<AudioSourceClock>();
51 CapturerClockManager::GetInstance().RegisterAudioSourceClock(1, srcClock_);
52 srcClock_->Init(MOCK_SAMPLE_RATE, MOCK_FORMAT, MOCK_CHANNEL);
53
54 CapturerClockManager::GetInstance().CreateCapturerClock(1, MOCK_SAMPLE_RATE);
55 capturerClock_ = CapturerClockManager::GetInstance().GetCapturerClock(1);
56 capturerClock_->Start();
57 }
58
TearDownTestCase()59 void AudioSourceClockUnitTest::TearDownTestCase()
60 {
61 srcClock_ = nullptr;
62 capturerClock_ = nullptr;
63 CapturerClockManager::GetInstance().DeleteAudioSourceClock(1);
64 CapturerClockManager::GetInstance().DeleteCapturerClock(1);
65 }
66
67 /**
68 * @tc.name : Test Audio source clock
69 * @tc.number : AudioSourceClockUnitTest_001
70 * @tc.desc : Test Audio source clock normal case
71 */
72 HWTEST_F(AudioSourceClockUnitTest, AudioSourceClockUnitTest_001, TestSize.Level1)
73 {
74 EXPECT_EQ(srcClock_->sizePerPos_, 4); // 4:data size
75
76 srcClock_->Renew(MOCK_POSITION_INC);
77 EXPECT_TRUE(capturerClock_->timestamp_ == 0);
78
79 std::vector<int32_t> sessionIdList = { 1 };
80 srcClock_->UpdateSessionId(sessionIdList);
81 EXPECT_EQ(srcClock_->sessionIdList_.size(), 1);
82
83 srcClock_->Renew(MOCK_POSITION_INC);
84 EXPECT_TRUE(capturerClock_->timestamp_ > 0);
85 }
86
87 /**
88 * @tc.name : Test Init
89 * @tc.number : Init_001
90 * @tc.desc : Test Init
91 */
92 HWTEST_F(AudioSourceClockUnitTest, Init_001, TestSize.Level1)
93 {
94 AudioSampleFormat format = AudioSampleFormat::SAMPLE_U8;
95 srcClock_->Init(MOCK_SAMPLE_RATE, format, MOCK_CHANNEL);
96 EXPECT_EQ(srcClock_->sizePerPos_, 2);
97 }
98
99 /**
100 * @tc.name : Test Init
101 * @tc.number : Init_002
102 * @tc.desc : Test Init
103 */
104 HWTEST_F(AudioSourceClockUnitTest, Init_002, TestSize.Level1)
105 {
106 AudioSampleFormat format = AudioSampleFormat::SAMPLE_S24LE;
107 srcClock_->Init(MOCK_SAMPLE_RATE, format, MOCK_CHANNEL);
108 EXPECT_EQ(srcClock_->sizePerPos_, 6);
109 }
110
111 /**
112 * @tc.name : Test Init
113 * @tc.number : Init_003
114 * @tc.desc : Test Init
115 */
116 HWTEST_F(AudioSourceClockUnitTest, Init_003, TestSize.Level1)
117 {
118 AudioSampleFormat format = AudioSampleFormat::SAMPLE_S32LE;
119 srcClock_->Init(MOCK_SAMPLE_RATE, format, MOCK_CHANNEL);
120 EXPECT_EQ(srcClock_->sizePerPos_, 8);
121 }
122
123 /**
124 * @tc.name : Test Init
125 * @tc.number : Init_004
126 * @tc.desc : Test Init
127 */
128 HWTEST_F(AudioSourceClockUnitTest, Init_004, TestSize.Level1)
129 {
130 AudioSampleFormat format = AudioSampleFormat::INVALID_WIDTH;
131 srcClock_->Init(MOCK_SAMPLE_RATE, format, MOCK_CHANNEL);
132 EXPECT_EQ(srcClock_->sizePerPos_, 4);
133 }
134 } // namespace AudioStandard
135 } // namespace OHOS
136