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 "live_controller_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 using namespace std;
25 using namespace testing::ext;
26 namespace {
27 constexpr int32_t WHAT_LIVE_DELAY_TIME = 1;
28 constexpr int32_t UPDATE_INTERVAL_MS = 200;
29 }
30
SetUpTestCase(void)31 void LiveControllerUnitTest::SetUpTestCase(void)
32 {
33 }
34
TearDownTestCase(void)35 void LiveControllerUnitTest::TearDownTestCase(void)
36 {
37 }
38
SetUp(void)39 void LiveControllerUnitTest::SetUp(void)
40 {
41 liveController_ = std::make_unique<LiveController>();
42 testObs_ = std::make_shared<MockIPlayerEngineObs>();
43 liveController_->task_ = std::make_unique<Task>("checkliveDelayThread");
44 }
45
TearDown(void)46 void LiveControllerUnitTest::TearDown(void)
47 {
48 if (testObs_) {
49 testObs_ = nullptr;
50 }
51 liveController_ = nullptr;
52 }
53
54 /**
55 * @tc.name : Test flv smart play
56 * @tc.number : StartWithPlayerEngineObs_001
57 * @tc.desc : Test create flv live scheduled check task
58 * @tc.require :
59 */
60 HWTEST_F(LiveControllerUnitTest, StartWithPlayerEngineObs_001, TestSize.Level0)
61 {
62 liveController_->taskStarted_ = false;
63 liveController_->StartWithPlayerEngineObs(testObs_);
64 EXPECT_TRUE(liveController_->taskStarted_);
65 }
66
67 /**
68 * @tc.name : Test flv smart play
69 * @tc.number : StartAndStopTask_001
70 * @tc.desc : Test start and stop flv live scheduled check task
71 * @tc.require :
72 */
73 HWTEST_F(LiveControllerUnitTest, StartAndStopTask_001, TestSize.Level0)
74 {
75 liveController_->isCheckLiveDelayTimeSet_.store(true);
76 liveController_->StartCheckLiveDelayTime(UPDATE_INTERVAL_MS);
77 EXPECT_EQ(liveController_->checkLiveDelayTimeIntervalMs_, UPDATE_INTERVAL_MS);
78 liveController_->isCheckLiveDelayTimeSet_.store(false);
79 liveController_->StartCheckLiveDelayTime(UPDATE_INTERVAL_MS);
80 EXPECT_TRUE(liveController_->isCheckLiveDelayTimeSet_.load());
81
82 liveController_->StopCheckLiveDelayTime();
83 EXPECT_FALSE(liveController_->isCheckLiveDelayTimeSet_.load());
84 }
85
86 /**
87 * @tc.name : Test Enqueue
88 * @tc.number: Enqueue_001
89 * @tc.desc : Test Enqueue
90 */
91 HWTEST_F(LiveControllerUnitTest, Enqueue_001, TestSize.Level0)
92 {
93 liveController_->StartWithPlayerEngineObs(testObs_);
94 liveController_->isCheckLiveDelayTimeSet_.store(true);
95 std::shared_ptr<LiveController::Event> event =
96 std::make_shared<LiveController::Event>(WHAT_LIVE_DELAY_TIME,
97 SteadyClock::GetCurrentTimeMs(), Any());
98 liveController_->LoopOnce(event);
99 EXPECT_TRUE(testObs_->onSystemOperationFlag);
100 liveController_->StopCheckLiveDelayTime();
101 }
102 }
103 }