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
29 class FeedableDataImpl : public IFeedableData {
30 public:
GetTimeStamp()31 int64_t GetTimeStamp() override
32 {
33 return 1;
34 }
35 };
36
37 class FeedableDataProducerImpl : public IFeedableDataProducer {
38 public:
OnSmoothFinished(const std::shared_ptr<IFeedableData> & data)39 void OnSmoothFinished(const std::shared_ptr<IFeedableData> &data) override
40 {
41 if (data) {
42 data->GetTimeStamp();
43 }
44 return;
45 }
46 };
47
48 class DCameraFeedingSmootherImpl : public IFeedingSmoother {
49 public:
50 ~DCameraFeedingSmootherImpl() override = default;
51
PrepareSmooth()52 void PrepareSmooth() override
53 {
54 }
55
InitBaseline(const int64_t timeStampBaseline,const int64_t clockBaseline)56 void InitBaseline(const int64_t timeStampBaseline, const int64_t clockBaseline) override
57 {
58 }
59
InitTimeStatistician()60 void InitTimeStatistician() override
61 {
62 dCameraStatistician_ = std::make_shared<DCameraTimeStatistician>();
63 }
64
NotifySmoothFinished(const std::shared_ptr<IFeedableData> & data)65 int32_t NotifySmoothFinished(const std::shared_ptr<IFeedableData> &data) override
66 {
67 return listener_->OnSmoothFinished(data);
68 }
69
70 private:
71 constexpr static uint8_t DYNAMIC_BALANCE_THRE = 3;
72 constexpr static int32_t SMOOTH_BUFFER_TIME_US = 20000;
73 constexpr static uint32_t AVER_INTERVAL_DIFF_THRE_US = 2000;
74 constexpr static uint32_t FEED_ONCE_DIFF_THRE_US = 10000;
75
76 std::shared_ptr<DCameraTimeStatistician> dCameraStatistician_ = nullptr;
77 };
78
79 const int32_t SLEEP_TIME = 1000;
80 const int64_t FRAME_INTERVAL = 40000;
81 const uint8_t TWOFOLD = 2;
82 class DCameraFeedingSmotherTest : public testing::Test {
83 public:
84 static void SetUpTestCase(void);
85 static void TearDownTestCase(void);
86 void SetUp();
87 void TearDown();
88 };
89
SetUpTestCase(void)90 void DCameraFeedingSmotherTest::SetUpTestCase(void)
91 {
92 }
93
TearDownTestCase(void)94 void DCameraFeedingSmotherTest::TearDownTestCase(void)
95 {
96 }
97
SetUp(void)98 void DCameraFeedingSmotherTest::SetUp(void)
99 {
100 }
101
TearDown(void)102 void DCameraFeedingSmotherTest::TearDown(void)
103 {
104 }
105
106 /**
107 * @tc.name: dcamera_feeding_smoother_test_001
108 * @tc.desc: Verify StartSmooth func.
109 * @tc.type: FUNC
110 * @tc.require: issue
111 */
112 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_001, TestSize.Level1)
113 {
114 DHLOGI("dcamera_feeding_smoother_test_001");
115 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
116 smoother->state_ = SMOOTH_STOP;
117 int32_t ret = smoother->StartSmooth();
118 EXPECT_EQ(SMOOTH_SUCCESS, ret);
119 ret = smoother->StartSmooth();
120 EXPECT_EQ(SMOOTH_IS_STARTED, ret);
121 ret = smoother->StopSmooth();
122 EXPECT_EQ(SMOOTH_SUCCESS, ret);
123 }
124
125 /**
126 * @tc.name: dcamera_feeding_smoother_test_002
127 * @tc.desc: Verify StopSmooth func.
128 * @tc.type: FUNC
129 * @tc.require: issue
130 */
131 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_002, TestSize.Level1)
132 {
133 DHLOGI("dcamera_feeding_smoother_test_002");
134 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
135 smoother->state_ = SMOOTH_STOP;
136 int32_t ret = smoother->StopSmooth();
137 EXPECT_EQ(SMOOTH_IS_STOPED, ret);
138 smoother->StartSmooth();
139 std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
140 ret = smoother->StopSmooth();
141 EXPECT_EQ(SMOOTH_SUCCESS, ret);
142 }
143
144 /**
145 * @tc.name: dcamera_feeding_smoother_test_003
146 * @tc.desc: Verify LooperSmooth func.
147 * @tc.type: FUNC
148 * @tc.require: issue
149 */
150 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_003, TestSize.Level1)
151 {
152 DHLOGI("dcamera_feeding_smoother_test_003");
153 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
154 int32_t ret = smoother->StartSmooth();
155 size_t capacity = 1;
156 std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
157 smoother->PushData(data);
158 std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
159 ret = smoother->StopSmooth();
160 EXPECT_EQ(SMOOTH_SUCCESS, ret);
161 }
162
163 /**
164 * @tc.name: dcamera_feeding_smoother_test_004
165 * @tc.desc: Verify SmoothFeeding func.
166 * @tc.type: FUNC
167 * @tc.require: issue
168 */
169 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_004, TestSize.Level1)
170 {
171 DHLOGI("dcamera_feeding_smoother_test_004");
172 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
173 int32_t ret = smoother->StartSmooth();
174 size_t capacity = 1;
175 std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
176 data->frameInfo_.pts = FRAME_INTERVAL;
177 smoother->PushData(data);
178 std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
179 smoother->PushData(data);
180 smoother->SetProcessDynamicBalanceState(true);
181 smoother->smoothCon_.notify_one();
182 std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_TIME));
183 smoother->InitTimeStatistician();
184 smoother->PushData(data);
185 smoother->SetProcessDynamicBalanceState(false);
186 smoother->smoothCon_.notify_one();
187 ret = smoother->StopSmooth();
188 EXPECT_EQ(SMOOTH_SUCCESS, ret);
189 }
190
191 /**
192 * @tc.name: dcamera_feeding_smoother_test_005
193 * @tc.desc: Verify CheckIsProcessInDynamicBalance func.
194 * @tc.type: FUNC
195 * @tc.require: issue
196 */
197 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_005, TestSize.Level1)
198 {
199 DHLOGI("dcamera_feeding_smoother_test_005");
200 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
201 smoother->CheckIsProcessInDynamicBalance();
202 smoother->SetProcessDynamicBalanceState(false);
203 smoother->InitTimeStatistician();
204 smoother->state_ = SMOOTH_START;
205 size_t capacity = 1;
206 std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
207 for (uint8_t i = 1; i <= DCameraFeedingSmoother::DYNAMIC_BALANCE_THRE; i++) {
208 data->frameInfo_.pts = i * FRAME_INTERVAL;
209 data->frameInfo_.index = i;
210 smoother->PushData(data);
211 smoother->CheckIsProcessInDynamicBalanceOnce();
212 std::this_thread::sleep_for(std::chrono::microseconds(FRAME_INTERVAL));
213 }
214 smoother->CheckIsProcessInDynamicBalance();
215 smoother->SetDynamicBalanceThre(1);
216 smoother->CheckIsProcessInDynamicBalance();
217 smoother->state_ = SMOOTH_STOP;
218 int32_t ret = smoother->StopSmooth();
219 EXPECT_EQ(SMOOTH_IS_STOPED, ret);
220 }
221
222 /**
223 * @tc.name: dcamera_feeding_smoother_test_006
224 * @tc.desc: Verify AdjustSleepTime func.
225 * @tc.type: FUNC
226 * @tc.require: issue
227 */
228 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_006, TestSize.Level1)
229 {
230 DHLOGI("dcamera_feeding_smoother_test_006");
231 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
232 smoother->delta_ = FRAME_INTERVAL;
233 smoother->sleep_ = FRAME_INTERVAL;
234 smoother->AdjustSleepTime(FRAME_INTERVAL);
235 smoother->delta_ = -FRAME_INTERVAL;
236 smoother->AdjustSleepTime(FRAME_INTERVAL);
237 int32_t ret = smoother->StopSmooth();
238 EXPECT_EQ(SMOOTH_IS_STOPED, ret);
239 }
240
241 /**
242 * @tc.name: dcamera_feeding_smoother_test_007
243 * @tc.desc: Verify SyncClock func.
244 * @tc.type: FUNC
245 * @tc.require: issue
246 */
247 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_007, TestSize.Level1)
248 {
249 DHLOGI("dcamera_feeding_smoother_test_007");
250 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
251 int64_t timeStamp = TWOFOLD * FRAME_INTERVAL;
252 int64_t clock = TWOFOLD * FRAME_INTERVAL;
253 smoother->SyncClock(timeStamp, FRAME_INTERVAL, FRAME_INTERVAL);
254 smoother->SyncClock(FRAME_INTERVAL, FRAME_INTERVAL, clock);
255 int32_t ret = smoother->StopSmooth();
256 EXPECT_EQ(SMOOTH_IS_STOPED, ret);
257 }
258
259 /**
260 * @tc.name: dcamera_feeding_smoother_test_008
261 * @tc.desc: Verify SyncClock func.
262 * @tc.type: FUNC
263 * @tc.require: issue
264 */
265 HWTEST_F(DCameraFeedingSmotherTest, dcamera_feeding_smoother_test_008, TestSize.Level1)
266 {
267 DHLOGI("dcamera_feeding_smoother_test_008");
268 std::unique_ptr<IFeedingSmoother> smoother = std::make_unique<DCameraFeedingSmoother>();
269 int32_t ret = smoother->StartSmooth();
270 size_t capacity = 1;
271 std::shared_ptr<DataBuffer> data = std::make_shared<DataBuffer>(capacity);
272 smoother->state_ = SmoothState::SMOOTH_STOP;
273 smoother->PushData(data);
274 smoother->state_ = SmoothState::SMOOTH_START;
275 smoother->PushData(data);
276 smoother->InitTimeStatistician();
277 smoother->CheckIsProcessInDynamicBalanceOnce();
278 std::shared_ptr<IFeedableDataProducer> producer = std::make_shared<FeedableDataProducerImpl>();
279 std::shared_ptr<FeedingSmootherListener> listener = std::make_shared<FeedingSmootherListener>(producer);
280 smoother->RegisterListener(listener);
281 std::shared_ptr<IFeedableData> iFeedableData = std::make_shared<FeedableDataImpl>();
282 std::unique_ptr<IFeedingSmoother> smootherImpl = std::make_unique<DCameraFeedingSmootherImpl>();
283 smootherImpl->RegisterListener(listener);
284 smootherImpl->NotifySmoothFinished(iFeedableData);
285 std::this_thread::sleep_for(std::chrono::microseconds(1000));
286 ret = smootherImpl->StopSmooth();
287 ret = smoother->StopSmooth();
288 EXPECT_EQ(SMOOTH_SUCCESS, ret);
289 }
290 } // namespace DistributedHardware
291 } // namespace OHOS