• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "transcoder_callback_unit_test.h"
17 #include <utility>
18 #include "common/log.h"
19 #include "osal/task/autolock.h"
20 #include "osal/utils/steady_clock.h"
21 #include "hitranscoder_callback_looper.h"
22 
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 using namespace std;
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace Media {
31 namespace {
32     constexpr int32_t WHAT_NONE = 0;
33     constexpr int32_t WHAT_INFO = 2;
34     constexpr int32_t DEFAULT_EVENT = -1;
35 }
SetUpTestCase(void)36 void TranscoderCallbackUnitTest::SetUpTestCase(void) {}
37 
TearDownTestCase(void)38 void TranscoderCallbackUnitTest::TearDownTestCase(void) {}
39 
SetUp(void)40 void TranscoderCallbackUnitTest::SetUp(void)
41 {
42     callback_ = std::make_shared<HiTransCoderCallbackLooper>();
43     mockTranscoderEngine_ = new MockITransCoderEngine();
44     callback_->transCoderEngine_ = mockTranscoderEngine_;
45     testObs_ = std::make_shared<MockITransCoderEngineObs>();
46     callback_->task_ = std::make_unique<Task>("callbackTestThread");
47     callback_->StartWithTransCoderEngineObs(testObs_);
48 }
49 
TearDown(void)50 void TranscoderCallbackUnitTest::TearDown(void)
51 {
52     if (mockTranscoderEngine_) {
53         delete mockTranscoderEngine_;
54         mockTranscoderEngine_ = nullptr;
55     }
56     if (testObs_) {
57         testObs_ = nullptr;
58     }
59     callback_ = nullptr;
60 }
61 
62 /**
63  * @tc.name: DoReportMediaProgress_01
64  * @tc.desc: DoReportMediaProgress_01
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(TranscoderCallbackUnitTest, DoReportMediaProgress_01, TestSize.Level0)
69 {
70     callback_->reportMediaProgress_ = true;
71     testObs_ = nullptr;
72     callback_->DoReportMediaProgress();
73     EXPECT_FALSE(callback_->isDropMediaProgress_);
74     callback_->reportMediaProgress_ = false;
75 }
76 
77 /**
78  * @tc.name: DoReportMediaProgress_02
79  * @tc.desc: DoReportMediaProgress_02
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(TranscoderCallbackUnitTest, DoReportMediaProgress_02, TestSize.Level0)
84 {
85     callback_->reportMediaProgress_ = true;
86     callback_->isDropMediaProgress_ = true;
87     callback_->DoReportMediaProgress();
88     EXPECT_FALSE(testObs_->onInfoFlag);
89     callback_->reportMediaProgress_ = false;
90 }
91 
92 /**
93  * @tc.name: Enqueue
94  * @tc.desc: Enqueue
95  * @tc.type: FUNC
96  * @tc.require:
97  */
98 HWTEST_F(TranscoderCallbackUnitTest, Enqueue, TestSize.Level0)
99 {
100     std::shared_ptr<HiTransCoderCallbackLooper::Event> event =
101         std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_NONE,
102         SteadyClock::GetCurrentTimeMs(), Any());
103     callback_->Enqueue(event);
104     EXPECT_FALSE(testObs_->onInfoFlag);
105     callback_->reportMediaProgress_ = false;
106 }
107 
108 /**
109  * @tc.name: LoopOnce
110  * @tc.desc: LoopOnce
111  * @tc.type: FUNC
112  * @tc.require:
113  */
114 HWTEST_F(TranscoderCallbackUnitTest, LoopOnce, TestSize.Level0)
115 {
116     std::shared_ptr<HiTransCoderCallbackLooper::Event> event =
117         std::make_shared<HiTransCoderCallbackLooper::Event>(DEFAULT_EVENT,
118         SteadyClock::GetCurrentTimeMs(), Any());
119     callback_->LoopOnce(event);
120     event = std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_INFO,
121         SteadyClock::GetCurrentTimeMs() + 100, Any());
122     callback_->LoopOnce(event);
123     EXPECT_FALSE(testObs_->onInfoFlag);
124 }
125 
126 /**
127  * @tc.name: DoReportErrorAndInfo_001
128  * @tc.desc: DoReportErrorAndInfo_001
129  * @tc.type: FUNC
130  * @tc.require:
131  */
132 HWTEST_F(TranscoderCallbackUnitTest, DoReportErrorAndInfo_001, TestSize.Level0)
133 {
134     Any error = nullptr;
135     callback_->DoReportError(error);
136     EXPECT_FALSE(testObs_->onErrorFlag);
137 
138     TransCoderOnInfoType infoType = TransCoderOnInfoType::INFO_TYPE_TRANSCODER_COMPLETED;
139     int32_t infoCode = 0;
140     std::shared_ptr<HiTransCoderCallbackLooper::Event> event = std::make_shared<HiTransCoderCallbackLooper::Event>(
141         WHAT_INFO, SteadyClock::GetCurrentTimeMs(), std::make_tuple(infoType, infoCode));
142     callback_->DoReportInfo(event->detail);
143     EXPECT_TRUE(testObs_->onInfoFlag);
144 }
145 } // namespace Media
146 } // namespace OHOS