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 "capturer_clock_manager.h"
21 #include "capturer_clock.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;
30 constexpr uint32_t MOCK_SAMPLE_RATE = 48'000;
31 constexpr uint32_t MOCK_SAMPLE_RATE_2 = 96'000;
32
33 constexpr uint64_t MOCK_POSITION_1 = 0;
34 constexpr uint64_t MOCK_POSITION_2 = 960;
35 constexpr uint64_t MOCK_POSITION_3 = 1920;
36 constexpr uint64_t MOCK_POSITION_4 = 2880;
37 constexpr uint64_t MOCK_POSITION_5 = 3840;
38 constexpr uint64_t MOCK_TIMESTAMP_1 = 1'000'000'000;
39 constexpr uint64_t MOCK_TIMESTAMP_2 = 1'020'000'000;
40 constexpr uint64_t MOCK_TIMESTAMP_3 = 1'040'000'000;
41 constexpr uint64_t MOCK_TIMESTAMP_4 = 1'100'000'000;
42 constexpr uint64_t MOCK_TIMESTAMP_4_IN_CAPTURER = 1'120'000'000;
43 constexpr uint64_t MOCK_TIMESTAMP_5 = 1'120'000'000;
44 constexpr uint64_t MOCK_TIMESTAMP_5_IN_CAPTURER = 1'140'000'000;
45
46 class CapturerClockUnitTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
SetUp()50 virtual void SetUp() {}
TearDown()51 virtual void TearDown() {}
52 private:
53 static std::shared_ptr<CapturerClock> capturerClock_;
54 };
55
56 std::shared_ptr<CapturerClock> CapturerClockUnitTest::capturerClock_ = nullptr;
57
SetUpTestCase()58 void CapturerClockUnitTest::SetUpTestCase()
59 {
60 CapturerClockManager::GetInstance().CreateCapturerClock(1, MOCK_SAMPLE_RATE);
61 capturerClock_ = CapturerClockManager::GetInstance().GetCapturerClock(1);
62 }
63
TearDownTestCase()64 void CapturerClockUnitTest::TearDownTestCase()
65 {
66 CapturerClockManager::GetInstance().DeleteCapturerClock(1);
67 }
68
69 /**
70 * @tc.name : Test capturer clock
71 * @tc.number : CapturerClockUnitTest_001
72 * @tc.desc : Test capturer clock normal case
73 */
74 HWTEST_F(CapturerClockUnitTest, CapturerClockUnitTest_001, TestSize.Level1)
75 {
76 capturerClock_->SetTimeStampByPosition(MOCK_TIMESTAMP_1, MOCK_SAMPLE_RATE, MOCK_POSITION_INC);
77 EXPECT_EQ(capturerClock_->timestamp_, 0);
78
79 capturerClock_->Start();
80 capturerClock_->SetTimeStampByPosition(MOCK_TIMESTAMP_1, MOCK_SAMPLE_RATE, MOCK_POSITION_INC);
81 capturerClock_->SetTimeStampByPosition(MOCK_TIMESTAMP_2, MOCK_SAMPLE_RATE, MOCK_POSITION_INC);
82 uint64_t timestamp;
83 capturerClock_->GetTimeStampByPosition(MOCK_POSITION_1, timestamp);
84 EXPECT_EQ(timestamp, MOCK_TIMESTAMP_1);
85 capturerClock_->GetTimeStampByPosition(MOCK_POSITION_2, timestamp);
86 EXPECT_EQ(timestamp, MOCK_TIMESTAMP_2);
87 capturerClock_->GetTimeStampByPosition(MOCK_POSITION_3, timestamp);
88 EXPECT_EQ(timestamp, MOCK_TIMESTAMP_3);
89 capturerClock_->Stop();
90 }
91
92 /**
93 * @tc.name : Test capturer clock
94 * @tc.number : CapturerClockUnitTest_002
95 * @tc.desc : Test capturer clock in different period
96 */
97 HWTEST_F(CapturerClockUnitTest, CapturerClockUnitTest_002, TestSize.Level1)
98 {
99 capturerClock_->Start();
100
101 capturerClock_->SetTimeStampByPosition(MOCK_TIMESTAMP_4, MOCK_SAMPLE_RATE_2, MOCK_POSITION_INC * 2);
102 capturerClock_->SetTimeStampByPosition(MOCK_TIMESTAMP_5, MOCK_SAMPLE_RATE_2, MOCK_POSITION_INC * 2);
103
104 uint64_t timestamp;
105 capturerClock_->GetTimeStampByPosition(MOCK_POSITION_4, timestamp);
106 EXPECT_EQ(timestamp, MOCK_TIMESTAMP_4_IN_CAPTURER);
107 capturerClock_->GetTimeStampByPosition(MOCK_POSITION_5, timestamp);
108 EXPECT_EQ(timestamp, MOCK_TIMESTAMP_5_IN_CAPTURER);
109 }
110
111 } // namespace AudioStandard
112 } // namespace OHOS
113