• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "media_errors.h"
17 #include "hiplayer_callback_looper_unit_test.h"
18 #include "osal/utils/steady_clock.h"
19 #include "osal/task/autolock.h"
20 #include "pipeline/pipeline.h"
21 
22 namespace OHOS {
23 namespace Media {
24 namespace {
25     constexpr int32_t WHAT_NONE = 0;
26     constexpr int32_t WHAT_MEDIA_PROGRESS = 1;
27     constexpr int32_t WHAT_INFO = 2;
28 }
29 using namespace std;
30 using namespace testing::ext;
31 
SetUpTestCase(void)32 void HiplayerCallbackLooperUnitTest::SetUpTestCase(void)
33 {
34 }
35 
TearDownTestCase(void)36 void HiplayerCallbackLooperUnitTest::TearDownTestCase(void)
37 {
38 }
39 
SetUp(void)40 void HiplayerCallbackLooperUnitTest::SetUp(void)
41 {
42     callback_ = std::make_unique<HiPlayerCallbackLooper>();
43     mockIPlayerEngine_ = new MockIPlayerEngine();
44     callback_->playerEngine_ = mockIPlayerEngine_;
45     testObs_ = std::make_shared<MockIPlayerEngineObs>();
46     callback_->task_ = std::make_unique<Task>("callbackTestThread");
47     callback_->StartWithPlayerEngineObs(testObs_);
48 }
49 
TearDown(void)50 void HiplayerCallbackLooperUnitTest::TearDown(void)
51 {
52     if (mockIPlayerEngine_) {
53         delete mockIPlayerEngine_;
54         mockIPlayerEngine_ = nullptr;
55     }
56     if (testObs_) {
57         testObs_ = nullptr;
58     }
59     callback_ = nullptr;
60 }
61 
62 /**
63  * @tc.name  : Test DoReportMediaProgress
64  * @tc.number: DoReportMediaProgress_001
65  * @tc.desc  : Test DoReportMediaProgress
66  */
67 HWTEST_F(HiplayerCallbackLooperUnitTest, DoReportMediaProgress_001, TestSize.Level0)
68 {
69     callback_->reportMediaProgress_ = true;
70     testObs_ = nullptr;
71     callback_->DoReportMediaProgress();
72     EXPECT_FALSE(callback_->isDropMediaProgress_);
73     callback_->reportMediaProgress_ = false;
74 }
75 
76 /**
77  * @tc.name  : Test DoReportMediaProgress
78  * @tc.number: DoReportMediaProgress_002
79  * @tc.desc  : Test DoReportMediaProgress
80  */
81 HWTEST_F(HiplayerCallbackLooperUnitTest, DoReportMediaProgress_002, TestSize.Level0)
82 {
83     callback_->reportMediaProgress_ = false;
84     callback_->isDropMediaProgress_ = true;
85     callback_->DoReportMediaProgress();
86     EXPECT_FALSE(testObs_->onInfoFlag);
87     callback_->reportMediaProgress_ = true;
88     callback_->isDropMediaProgress_ = true;
89     callback_->DoReportMediaProgress();
90     EXPECT_FALSE(testObs_->onInfoFlag);
91     callback_->reportMediaProgress_ = false;
92 }
93 
94 /**
95  * @tc.name  : Test StartReportMediaProgress
96  * @tc.number: StartReportMediaProgress_001
97  * @tc.desc  : Test StartReportMediaProgress
98  */
99 HWTEST_F(HiplayerCallbackLooperUnitTest, StartReportMediaProgress_001, TestSize.Level0)
100 {
101     int32_t updateIntervalMs = 0;
102     callback_->reportMediaProgress_ = true;
103     callback_->StartReportMediaProgress(updateIntervalMs);
104     EXPECT_EQ(callback_->reportMediaProgress_, true);
105 }
106 
107 /**
108  * @tc.name  : Test StartReportMediaProgress
109  * @tc.number: StartReportMediaProgress_002
110  * @tc.desc  : Test StartReportMediaProgress
111  */
112 HWTEST_F(HiplayerCallbackLooperUnitTest, StartReportMediaProgress_002, TestSize.Level0)
113 {
114     int32_t updateIntervalMs = 0;
115     callback_->reportMediaProgress_ = false;
116     callback_->StartReportMediaProgress(updateIntervalMs);
117     EXPECT_EQ(callback_->reportMediaProgress_, true);
118 }
119 
120 /**
121  * @tc.name  : Test StartCollectMaxAmplitude
122  * @tc.number: StartCollectMaxAmplitude_001
123  * @tc.desc  : Test StartCollectMaxAmplitude
124  */
125 HWTEST_F(HiplayerCallbackLooperUnitTest, StartCollectMaxAmplitude_001, TestSize.Level0)
126 {
127     int32_t updateIntervalMs = 0;
128     callback_->collectMaxAmplitude_ = true;
129     callback_->StartCollectMaxAmplitude(updateIntervalMs);
130     EXPECT_EQ(callback_->collectMaxAmplitudeIntervalMs_, updateIntervalMs);
131     callback_->reportMediaProgress_ = false;
132 }
133 
134 /**
135  * @tc.name  : Test StartCollectMaxAmplitude
136  * @tc.number: StartCollectMaxAmplitude_002
137  * @tc.desc  : Test StartCollectMaxAmplitude
138  */
139 HWTEST_F(HiplayerCallbackLooperUnitTest, StartCollectMaxAmplitude_002, TestSize.Level0)
140 {
141     int32_t updateIntervalMs = 0;
142     callback_->collectMaxAmplitude_ = false;
143     callback_->StartCollectMaxAmplitude(updateIntervalMs);
144     EXPECT_EQ(callback_->collectMaxAmplitude_, true);
145     callback_->reportMediaProgress_ = false;
146 }
147 
148 /**
149  * @tc.name  : Test Enqueue
150  * @tc.number: Enqueue_001
151  * @tc.desc  : Test Enqueue
152  */
153 HWTEST_F(HiplayerCallbackLooperUnitTest, Enqueue_001, TestSize.Level0)
154 {
155     std::shared_ptr<HiPlayerCallbackLooper::Event> event =
156         std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_NONE,
157         SteadyClock::GetCurrentTimeMs(), Any());
158     callback_->Enqueue(event);
159     EXPECT_FALSE(testObs_->onInfoFlag);
160     callback_->reportMediaProgress_ = false;
161 }
162 
163 /**
164  * @tc.name  : Test Enqueue
165  * @tc.number: Enqueue_002
166  * @tc.desc  : Test Enqueue
167  */
168 HWTEST_F(HiplayerCallbackLooperUnitTest, Enqueue_002, TestSize.Level0)
169 {
170     std::shared_ptr<HiPlayerCallbackLooper::Event> event =
171         std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_MEDIA_PROGRESS,
172         SteadyClock::GetCurrentTimeMs(), Any());
173     callback_->Enqueue(event);
174     EXPECT_FALSE(testObs_->onInfoFlag);
175     callback_->reportMediaProgress_ = false;
176 }
177 
178 /**
179  * @tc.name  : Test LoopOnce
180  * @tc.number: LoopOnce_001
181  * @tc.desc  : Test LoopOnce
182  */
183 HWTEST_F(HiplayerCallbackLooperUnitTest, LoopOnce_001, TestSize.Level0)
184 {
185     std::shared_ptr<HiPlayerCallbackLooper::Event> event =
186         std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_INFO,
187         SteadyClock::GetCurrentTimeMs() + 100, Any());
188     callback_->LoopOnce(event);
189     EXPECT_FALSE(testObs_->onInfoFlag);
190 }
191 
192 /**
193  * @tc.name  : Test LoopOnce
194  * @tc.number: LoopOnce_002
195  * @tc.desc  : Test LoopOnce
196  */
197 HWTEST_F(HiplayerCallbackLooperUnitTest, LoopOnce_002, TestSize.Level0)
198 {
199     std::shared_ptr<HiPlayerCallbackLooper::Event> event =
200         std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_NONE,
201         SteadyClock::GetCurrentTimeMs() + 100, Any());
202     callback_->LoopOnce(event);
203     EXPECT_FALSE(testObs_->onInfoFlag);
204 }
205 } // namespace Media
206 } // namespace OHOS