1 /*
2 * Copyright (c) 2024 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 "camera_deferred_video_unittest.h"
17
18 #include "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "basic_definitions.h"
21 #include "gmock/gmock.h"
22 #include "hap_token_info.h"
23 #include "nativetoken_kit.h"
24 #include "ipc_skeleton.h"
25 #include "token_setproc.h"
26 #include "os_account_manager.h"
27 #include "events_monitor.h"
28 #include "camera_log.h"
29 #include "deferred_processing_service.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::CameraStandard::DeferredProcessing;
33
34 namespace OHOS {
35 namespace CameraStandard {
OnProcessVideoDone(const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)36 ErrCode TestDeferredVideoProcSessionCallback::OnProcessVideoDone(const std::string& videoId,
37 const sptr<IPCFileDescriptor>& ipcFd)
38 {
39 MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnProcessVideoDone.");
40 return ERR_OK;
41 }
42
OnError(const std::string & videoId,int32_t errorCode)43 ErrCode TestDeferredVideoProcSessionCallback::OnError(const std::string& videoId, int32_t errorCode)
44 {
45 MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnError.");
46 return ERR_OK;
47 }
48
OnStateChanged(int32_t status)49 ErrCode TestDeferredVideoProcSessionCallback::OnStateChanged(int32_t status)
50 {
51 MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnStateChanged.");
52 return ERR_OK;
53 }
54
SetUpTestCase(void)55 void DeferredVideoUnitTest::SetUpTestCase(void)
56 {
57 MEDIA_DEBUG_LOG("DeferredUnitTest::SetUpTestCase started!");
58 }
59
TearDownTestCase(void)60 void DeferredVideoUnitTest::TearDownTestCase(void)
61 {
62 MEDIA_DEBUG_LOG("DeferredUnitTest::TearDownTestCase started!");
63 }
64
SetUp()65 void DeferredVideoUnitTest::SetUp()
66 {
67 MEDIA_DEBUG_LOG("SetUp testName:%{public}s",
68 ::testing::UnitTest::GetInstance()->current_test_info()->name());
69 NativeAuthorization();
70 auto callback = sptr<TestDeferredVideoProcSessionCallback>::MakeSptr();
71 session_ = DeferredProcessingService::GetInstance().CreateDeferredVideoProcessingSession(g_userId_, callback);
72 sessionManager_ = DPS_GetSessionManager();
73 schedulerManager_ = DPS_GetSchedulerManager();
74 jobRepository_ = schedulerManager_->videoController_[g_userId_]->videoJobRepository_;
75 }
76
TearDown()77 void DeferredVideoUnitTest::TearDown()
78 {
79 MEDIA_DEBUG_LOG("DeferredUnitTest::TearDown started!");
80 }
81
NativeAuthorization()82 void DeferredVideoUnitTest::NativeAuthorization()
83 {
84 const char *perms[2];
85 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
86 perms[1] = "ohos.permission.CAMERA";
87 NativeTokenInfoParams infoInstance = {
88 .dcapsNum = 0,
89 .permsNum = 2,
90 .aclsNum = 0,
91 .dcaps = NULL,
92 .perms = perms,
93 .acls = NULL,
94 .processName = "native_camera_tdd",
95 .aplStr = "system_basic",
96 };
97 g_tokenId_ = GetAccessTokenId(&infoInstance);
98 g_uid_ = IPCSkeleton::GetCallingUid();
99 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(g_uid_, g_userId_);
100 MEDIA_DEBUG_LOG("tokenId:%{public}" PRIu64 " uid:%{public}d userId:%{public}d",
101 g_tokenId_, g_uid_, g_userId_);
102 SetSelfTokenID(g_tokenId_);
103 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
104 }
105
BeginSchedule(bool isCharging)106 void DeferredVideoUnitTest::BeginSchedule(bool isCharging)
107 {
108 if (isCharging) {
109 EventsMonitor::GetInstance().NotifyChargingStatus(CHARGING);
110 EventsMonitor::GetInstance().NotifyBatteryLevel(BATTERY_LEVEL_OKAY);
111 } else {
112 EventsMonitor::GetInstance().NotifyChargingStatus(DISCHARGING);
113 EventsMonitor::GetInstance().NotifyBatteryStatus(BATTERY_OKAY);
114 }
115 EventsMonitor::GetInstance().NotifyMediaLibraryStatus(true);
116 EventsMonitor::GetInstance().NotifyImageEnhanceStatus(HDI_READY);
117 EventsMonitor::GetInstance().NotifyThermalLevel(LEVEL_0);
118 EventsMonitor::GetInstance().NotifyScreenStatus(SCREEN_OFF);
119 }
120
PauseSchedule()121 void DeferredVideoUnitTest::PauseSchedule()
122 {
123 EventsMonitor::GetInstance().NotifyScreenStatus(SCREEN_ON);
124 }
125
126 constexpr int VIDEO_REQUEST_FD_ID1 = 111;
127 // constexpr int VIDEO_REQUEST_FD_ID2 = 222;
128 // constexpr int VIDEO_REQUEST_FD_ID3 = 333;
129 constexpr int VIDEO_REQUEST_FD_TEMP = 888;
130
131 constexpr int64_t TIME_PROCESS = 200;
132 constexpr int64_t TIME_TEST_WAIT = 1000;
133
134 HWTEST_F(DeferredVideoUnitTest, AddJob, TestSize.Level0)
135 {
136 std::string videoId = "video1";
137 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
138 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
139 session_->AddVideo(videoId, srcFd, dstFd);
140 BeginSchedule(false);
141 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
142 auto num = jobRepository_->GetRunningJobCounts();
143 ASSERT_EQ(num, 1);
144 auto taskVideo = jobRepository_->GetJob();
145 ASSERT_NE(taskVideo, nullptr);
146 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
147 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
148
149 PauseSchedule();
150 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
151 }
152
153 HWTEST_F(DeferredVideoUnitTest, AddJob_C, TestSize.Level0)
154 {
155 std::string videoId = "video1";
156 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
157 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
158 session_->AddVideo(videoId, srcFd, dstFd);
159 BeginSchedule(true);
160 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
161 auto num = jobRepository_->GetRunningJobCounts();
162 ASSERT_EQ(num, 1);
163 auto taskVideo = jobRepository_->GetJob();
164 ASSERT_NE(taskVideo, nullptr);
165 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
166 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
167
168 PauseSchedule();
169 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
170 }
171
172 HWTEST_F(DeferredVideoUnitTest, PauseJob, TestSize.Level0)
173 {
174 std::string videoId = "video1";
175 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
176 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
177 session_->AddVideo(videoId, srcFd, dstFd);
178 BeginSchedule(false);
179 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
180 auto num = jobRepository_->GetRunningJobCounts();
181 ASSERT_EQ(num, 1);
182 auto taskVideo = jobRepository_->GetJob();
183 ASSERT_NE(taskVideo, nullptr);
184 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
185 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
186
187 EventsMonitor::GetInstance().NotifyBatteryStatus(BATTERY_LOW);
188 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
189 num = jobRepository_->GetRunningJobCounts();
190 ASSERT_EQ(num, 0);
191 taskVideo = jobRepository_->GetJob();
192 ASSERT_NE(taskVideo, nullptr);
193 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
194 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::PAUSE);
195
196 PauseSchedule();
197 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
198 }
199
200 HWTEST_F(DeferredVideoUnitTest, PauseJob_C, TestSize.Level0)
201 {
202 std::string videoId = "video1";
203 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
204 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
205 session_->AddVideo(videoId, srcFd, dstFd);
206 BeginSchedule(true);
207 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
208 auto num = jobRepository_->GetRunningJobCounts();
209 ASSERT_EQ(num, 1);
210 auto taskVideo = jobRepository_->GetJob();
211 ASSERT_NE(taskVideo, nullptr);
212 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
213 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
214
215 EventsMonitor::GetInstance().NotifyBatteryLevel(BATTERY_LEVEL_LOW);
216 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
217 num = jobRepository_->GetRunningJobCounts();
218 ASSERT_EQ(num, 0);
219 taskVideo = jobRepository_->GetJob();
220 ASSERT_NE(taskVideo, nullptr);
221 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
222 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::PAUSE);
223
224 PauseSchedule();
225 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
226 }
227
228 HWTEST_F(DeferredVideoUnitTest, DeleteJob, TestSize.Level0)
229 {
230 std::string videoId_1 = "video1";
231 sptr<IPCFileDescriptor> srcFd_1 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
232 sptr<IPCFileDescriptor> dstFd_1 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
233 session_->AddVideo(videoId_1, srcFd_1, dstFd_1);
234 std::string videoId_2 = "video2";
235 sptr<IPCFileDescriptor> srcFd_2 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
236 sptr<IPCFileDescriptor> dstFd_2 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
237 session_->AddVideo(videoId_2, srcFd_2, dstFd_2);
238 BeginSchedule(true);
239 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
240 auto jobQueue = jobRepository_->jobQueue_;
241 ASSERT_NE(jobQueue, nullptr);
242 auto taskVideo = jobRepository_->GetJob();
243 ASSERT_NE(taskVideo, nullptr);
244 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
245 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
246
247 session_->RemoveVideo(videoId_2, false);
248 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
249 jobQueue = jobRepository_->jobQueue_;
250 ASSERT_NE(jobQueue, nullptr);
251 taskVideo = jobRepository_->GetJob();
252 ASSERT_NE(taskVideo, nullptr);
253 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
254 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
255
256
257 taskVideo = jobRepository_->GetJobUnLocked(videoId_2);
258 EXPECT_EQ(taskVideo, nullptr);
259
260 PauseSchedule();
261 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
262 }
263
264 HWTEST_F(DeferredVideoUnitTest, DeleteJob_R, TestSize.Level0)
265 {
266 std::string videoId_1 = "video1";
267 sptr<IPCFileDescriptor> srcFd_1 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
268 sptr<IPCFileDescriptor> dstFd_1 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
269 session_->AddVideo(videoId_1, srcFd_1, dstFd_1);
270 std::string videoId_2 = "video2";
271 sptr<IPCFileDescriptor> srcFd_2 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID1);
272 sptr<IPCFileDescriptor> dstFd_2 = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_TEMP);
273 session_->AddVideo(videoId_2, srcFd_2, dstFd_2);
274 BeginSchedule(true);
275 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
276 auto jobQueue = jobRepository_->jobQueue_;
277 ASSERT_NE(jobQueue, nullptr);
278 auto taskVideo = jobRepository_->GetJob();
279 ASSERT_NE(taskVideo, nullptr);
280 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
281 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
282
283 session_->RemoveVideo(videoId_2, true);
284 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PROCESS));
285 jobQueue = jobRepository_->jobQueue_;
286 ASSERT_NE(jobQueue, nullptr);
287 taskVideo = jobRepository_->GetJob();
288 ASSERT_NE(taskVideo, nullptr);
289 EXPECT_EQ(taskVideo->GetVideoId(), "video1");
290 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::RUNNING);
291
292 taskVideo = jobRepository_->GetJobUnLocked(videoId_2);
293 ASSERT_NE(jobQueue, nullptr);
294 EXPECT_EQ(taskVideo->GetVideoId(), "video2");
295 EXPECT_EQ(taskVideo->GetCurStatus(), VideoJobStatus::DELETED);
296
297 PauseSchedule();
298 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_TEST_WAIT));
299 }
300
301 } // CameraStandard
302 } // OHOS
303