1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #define private public
18 #include "dcamera_feeding_smoother.h"
19 #undef private
20 #include "distributed_camera_errno.h"
21 #include "distributed_hardware_log.h"
22 #include "dcamera_utils_tools.h"
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 const int32_t SLEEP_TIME = 1000;
29 const int64_t FRAME_INTERVAL = 40000;
30 const uint8_t TWOFOLD = 2;
31 class DCameraFeedingSmotherTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37 };
38 
SetUpTestCase(void)39 void DCameraFeedingSmotherTest::SetUpTestCase(void)
40 {
41 }
42 
TearDownTestCase(void)43 void DCameraFeedingSmotherTest::TearDownTestCase(void)
44 {
45 }
46 
SetUp(void)47 void DCameraFeedingSmotherTest::SetUp(void)
48 {
49 }
50 
TearDown(void)51 void DCameraFeedingSmotherTest::TearDown(void)
52 {
53 }
54 
55 /**
56  * @tc.name: dcamera_feeding_smoother_test_001
57  * @tc.desc: Verify StartSmooth func.
58  * @tc.type: FUNC
59  * @tc.require: issue
60  */
61 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_001, TestSize.Level1)
62 {
63     DHLOGI("dcamera_feeding_smoother_test_001");
64     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
65     smoother->state_ = SMOOTH_STOP;
66     int32_t ret = smoother->StartSmooth();
67     EXPECT_EQ(SMOOTH_SUCCESS, ret);
68     ret = smoother->StartSmooth();
69     EXPECT_EQ(SMOOTH_IS_STARTED, ret);
70     ret = smoother->StopSmooth();
71     EXPECT_EQ(SMOOTH_SUCCESS, ret);
72 }
73 
74 /**
75  * @tc.name: dcamera_feeding_smoother_test_002
76  * @tc.desc: Verify StopSmooth func.
77  * @tc.type: FUNC
78  * @tc.require: issue
79  */
80 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_002, TestSize.Level1)
81 {
82     DHLOGI("dcamera_feeding_smoother_test_002");
83     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
84     smoother->state_ = SMOOTH_STOP;
85     int32_t ret = smoother->StopSmooth();
86     EXPECT_EQ(SMOOTH_IS_STOPED, ret);
87     smoother->StartSmooth();
88     std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
89     ret = smoother->StopSmooth();
90     EXPECT_EQ(SMOOTH_SUCCESS, ret);
91 }
92 
93 /**
94  * @tc.name: dcamera_feeding_smoother_test_003
95  * @tc.desc: Verify LooperSmooth func.
96  * @tc.type: FUNC
97  * @tc.require: issue
98  */
99 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_003, TestSize.Level1)
100 {
101     DHLOGI("dcamera_feeding_smoother_test_003");
102     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
103     int32_t ret = smoother->StartSmooth();
104     size_t capacity = 1;
105     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
106     smoother->PushData(data);
107     std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
108     ret = smoother->StopSmooth();
109     EXPECT_EQ(SMOOTH_SUCCESS, ret);
110 }
111 
112 /**
113  * @tc.name: dcamera_feeding_smoother_test_004
114  * @tc.desc: Verify SmoothFeeding func.
115  * @tc.type: FUNC
116  * @tc.require: issue
117  */
118 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_004, TestSize.Level1)
119 {
120     DHLOGI("dcamera_feeding_smoother_test_004");
121     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
122     int32_t ret = smoother->StartSmooth();
123     size_t capacity = 1;
124     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
125     data->frameInfo_.pts = FRAME_INTERVAL;
126     smoother->PushData(data);
127     std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
128     smoother->PushData(data);
129     smoother->SetProcessDynamicBalanceState(true);
130     smoother->smoothCon_.notify_one();
131     std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
132     smoother->InitTimeStatistician();
133     smoother->PushData(data);
134     smoother->SetProcessDynamicBalanceState(false);
135     smoother->smoothCon_.notify_one();
136     ret = smoother->StopSmooth();
137     EXPECT_EQ(SMOOTH_SUCCESS, ret);
138 }
139 
140 /**
141  * @tc.name: dcamera_feeding_smoother_test_005
142  * @tc.desc: Verify CheckIsProcessInDynamicBalance func.
143  * @tc.type: FUNC
144  * @tc.require: issue
145  */
146 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_005, TestSize.Level1)
147 {
148     DHLOGI("dcamera_feeding_smoother_test_005");
149     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
150     smoother->CheckIsProcessInDynamicBalance();
151     smoother->SetProcessDynamicBalanceState(false);
152     smoother->InitTimeStatistician();
153     smoother->state_ = SMOOTH_START;
154     size_t capacity = 1;
155     std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
156     for (uint8_t i = 1; i <= DCameraFeedingSmoother::DYNAMIC_BALANCE_THRE; i++) {
157         data->frameInfo_.pts = i * FRAME_INTERVAL;
158         data->frameInfo_.index = i;
159         smoother->PushData(data);
160         smoother->CheckIsProcessInDynamicBalanceOnce();
161         std::this_thread::sleep_for(std::chrono::microseconds(FRAME_INTERVAL));
162     }
163     smoother->CheckIsProcessInDynamicBalance();
164     smoother->SetDynamicBalanceThre(1);
165     smoother->CheckIsProcessInDynamicBalance();
166     smoother->state_ = SMOOTH_STOP;
167     int32_t ret = smoother->StopSmooth();
168     EXPECT_EQ(SMOOTH_IS_STOPED, ret);
169 }
170 
171 /**
172  * @tc.name: dcamera_feeding_smoother_test_006
173  * @tc.desc: Verify AdjustSleepTime func.
174  * @tc.type: FUNC
175  * @tc.require: issue
176  */
177 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_006, TestSize.Level1)
178 {
179     DHLOGI("dcamera_feeding_smoother_test_006");
180     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
181     smoother->delta_ = FRAME_INTERVAL;
182     smoother->sleep_ = FRAME_INTERVAL;
183     smoother->AdjustSleepTime(FRAME_INTERVAL);
184     smoother->delta_ = -FRAME_INTERVAL;
185     smoother->AdjustSleepTime(FRAME_INTERVAL);
186     int32_t ret = smoother->StopSmooth();
187     EXPECT_EQ(SMOOTH_IS_STOPED, ret);
188 }
189 
190 /**
191  * @tc.name: dcamera_feeding_smoother_test_007
192  * @tc.desc: Verify SyncClock func.
193  * @tc.type: FUNC
194  * @tc.require: issue
195  */
196 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_007, TestSize.Level1)
197 {
198     DHLOGI("dcamera_feeding_smoother_test_007");
199     std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
200     int64_t timeStamp = TWOFOLD * FRAME_INTERVAL;
201     int64_t clock = TWOFOLD * FRAME_INTERVAL;
202     smoother->SyncClock(timeStamp, FRAME_INTERVAL, FRAME_INTERVAL);
203     smoother->SyncClock(FRAME_INTERVAL, FRAME_INTERVAL, clock);
204     int32_t ret = smoother->StopSmooth();
205     EXPECT_EQ(SMOOTH_IS_STOPED, ret);
206 }
207 } // namespace DistributedHardware
208 } // namespace OHOS