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
18 #include "hdf_base.h"
19 #include "mock_wakelock_name.h"
20 #include "running_lock_impl.h"
21
22 using namespace OHOS::HDI;
23 using namespace OHOS::HDI::Power::V1_1;
24 using namespace testing::ext;
25
26 namespace {
27 constexpr int32_t DEFAULT_TIMEOUT_FOR_TEST_MS = 100;
28 constexpr int32_t DEFAULT_RUNNINGLOCK_INVALID_TYPE = 100;
29 constexpr int32_t RUNNINGLOCK_TIMEOUT_NONE = -1;
30 constexpr int32_t RUNNINGLOCK_TIMEOUT_DEFAULT = 0;
31 constexpr int32_t RUNNINGLOCK_TIMEOUT_SET_BASE_MS = 50;
32 const std::string runnninglockNameLabel = "runninglock.test.";
33 constexpr int32_t US_PER_MS = 1000;
34 class HdfPowerRunningLockTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 };
38
SetUpTestCase()39 void HdfPowerRunningLockTest::SetUpTestCase()
40 {
41 RunningLockImpl::SetDefaultTimeOutMs(DEFAULT_TIMEOUT_FOR_TEST_MS);
42 }
43 }
44
45 namespace {
46 /**
47 * @tc.name: HdfPowerRunningLockTest001
48 * @tc.desc: test Hold, running lock name is null
49 * @tc.type: FUNC
50 * @tc.require: issueI6IU18
51 */
52 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest001, TestSize.Level1)
53 {
54 PowerHdfState powerState = PowerHdfState::AWAKE;
55 RunningLockInfo runinglockInfo {};
56 runinglockInfo.name = "";
57 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
58 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
59
60 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
61 }
62
63 /**
64 * @tc.name: HdfPowerRunningLockTest002
65 * @tc.desc: test Hold, running lock type is invalid
66 * @tc.type: FUNC
67 * @tc.require: issueI6IU18
68 */
69 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest002, TestSize.Level1)
70 {
71 PowerHdfState powerState = PowerHdfState::AWAKE;
72 RunningLockInfo runinglockInfo {};
73 runinglockInfo.name = runnninglockNameLabel + "normal.2";
74 runinglockInfo.type = static_cast<RunningLockType>(DEFAULT_RUNNINGLOCK_INVALID_TYPE);
75 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
76
77 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
78 }
79
80 /**
81 * @tc.name: HdfPowerRunningLockTest003
82 * @tc.desc: test Unhold, running lock name is null
83 * @tc.type: FUNC
84 * @tc.require: issueI6IU18
85 */
86 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest003, TestSize.Level1)
87 {
88 RunningLockInfo runinglockInfo {};
89 runinglockInfo.name = "";
90 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
91 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
92
93 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Unhold(runinglockInfo));
94 }
95
96 /**
97 * @tc.name: HdfPowerRunningLockTest004
98 * @tc.desc: test Unhold, running lock type is invalid
99 * @tc.type: FUNC
100 * @tc.require: issueI6IU18
101 */
102 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest004, TestSize.Level1)
103 {
104 RunningLockInfo runinglockInfo {};
105 runinglockInfo.name = runnninglockNameLabel + "normal.4";
106 runinglockInfo.type = static_cast<RunningLockType>(DEFAULT_RUNNINGLOCK_INVALID_TYPE);
107 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
108
109 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Unhold(runinglockInfo));
110 }
111
112 /**
113 * @tc.name: HdfPowerRunningLockTest005
114 * @tc.desc: test Hold and UnHold, running lock type is phone
115 * @tc.type: FUNC
116 * @tc.require: issueI6IU18
117 */
118 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest005, TestSize.Level1)
119 {
120 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
121 std::string setLockNameOne = runnninglockNameLabel + "phone.5";
122 std::string setLockNameTwo = runnninglockNameLabel + "phone2.5";
123 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
124 std::string errorLockName = runnninglockNameLabel + "phone.error.5";
125
126 PowerHdfState powerState = PowerHdfState::AWAKE;
127 RunningLockInfo runinglockInfo {};
128 runinglockInfo.name = setLockNameOne;
129 runinglockInfo.type = setLockType;
130 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
131 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
132
133 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
134 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
135 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
136 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
137
138 // runninglock type and same & timeoutMs < 0, hold lock failed
139 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
140
141 runinglockInfo.name = setLockNameTwo;
142 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
143 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
144
145 // unhold a non-existent lock, return success
146 runinglockInfo.type = errorLockType;
147 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
148 runinglockInfo.type = setLockType;
149 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
150
151 runinglockInfo.name = errorLockName;
152 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
153 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
154 runinglockInfo.name = setLockNameTwo;
155
156 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
157 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
158
159 runinglockInfo.name = setLockNameOne;
160 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
161 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
162 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
163 }
164
165 /**
166 * @tc.name: HdfPowerRunningLockTest006
167 * @tc.desc: test Hold and UnHold, running lock type is notification
168 * @tc.type: FUNC
169 * @tc.require: issueI6IU18
170 */
171 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest006, TestSize.Level1)
172 {
173 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
174 std::string setLockNameOne = runnninglockNameLabel + "notify.6";
175 std::string setLockNameTwo = runnninglockNameLabel + "notify2.6";
176 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
177 std::string errorLockName = runnninglockNameLabel + "notify.error.6";
178
179 PowerHdfState powerState = PowerHdfState::AWAKE;
180 RunningLockInfo runinglockInfo {};
181 runinglockInfo.name = setLockNameOne;
182 runinglockInfo.type = setLockType;
183 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
184 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
185
186 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
187 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
188 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
189 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
190
191 // runninglock type and same & timeoutMs < 0, hold lock failed
192 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
193
194 runinglockInfo.name = setLockNameTwo;
195 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
196 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
197
198 // unhold a non-existent lock, return success
199 runinglockInfo.type = errorLockType;
200 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
201 runinglockInfo.type = setLockType;
202 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
203
204 runinglockInfo.name = errorLockName;
205 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
206 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
207 runinglockInfo.name = setLockNameTwo;
208
209 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
210 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
211
212 runinglockInfo.name = setLockNameOne;
213 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
214 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
215 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
216 }
217
218 /**
219 * @tc.name: HdfPowerRunningLockTest007
220 * @tc.desc: test Hold and UnHold, running lock type is audio
221 * @tc.type: FUNC
222 * @tc.require: issueI6IU18
223 */
224 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest007, TestSize.Level1)
225 {
226 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
227 std::string setLockNameOne = runnninglockNameLabel + "audio.7";
228 std::string setLockNameTwo = runnninglockNameLabel + "audio2.7";
229 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
230 std::string errorLockName = runnninglockNameLabel + "audio.error.7";
231
232 PowerHdfState powerState = PowerHdfState::AWAKE;
233 RunningLockInfo runinglockInfo {};
234 runinglockInfo.name = setLockNameOne;
235 runinglockInfo.type = setLockType;
236 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
237 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
238
239 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
240 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
241 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
242 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
243
244 // runninglock type and same & timeoutMs < 0, hold lock failed
245 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
246
247 runinglockInfo.name = setLockNameTwo;
248 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
249 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
250
251 // unhold a non-existent lock, return success
252 runinglockInfo.type = errorLockType;
253 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
254 runinglockInfo.type = setLockType;
255 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
256
257 runinglockInfo.name = errorLockName;
258 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
259 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
260 runinglockInfo.name = setLockNameTwo;
261
262 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
263 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
264
265 runinglockInfo.name = setLockNameOne;
266 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
267 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
268 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
269 }
270
271 /**
272 * @tc.name: HdfPowerRunningLockTest008
273 * @tc.desc: test Hold and UnHold, running lock type is sport
274 * @tc.type: FUNC
275 * @tc.require: issueI6IU18
276 */
277 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest008, TestSize.Level1)
278 {
279 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
280 std::string setLockNameOne = runnninglockNameLabel + "sport.8";
281 std::string setLockNameTwo = runnninglockNameLabel + "sport2.8";
282 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
283 std::string errorLockName = runnninglockNameLabel + "sport.error.8";
284
285 PowerHdfState powerState = PowerHdfState::AWAKE;
286 RunningLockInfo runinglockInfo {};
287 runinglockInfo.name = setLockNameOne;
288 runinglockInfo.type = setLockType;
289 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
290 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
291
292 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
293 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
294 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
295 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
296
297 // runninglock type and same & timeoutMs < 0, hold lock failed
298 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
299
300 runinglockInfo.name = setLockNameTwo;
301 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
302 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
303
304 // unhold a non-existent lock, return success
305 runinglockInfo.type = errorLockType;
306 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
307 runinglockInfo.type = setLockType;
308 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
309
310 runinglockInfo.name = errorLockName;
311 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
312 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
313 runinglockInfo.name = setLockNameTwo;
314
315 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
316 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
317
318 runinglockInfo.name = setLockNameOne;
319 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
320 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
321 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
322 }
323
324 /**
325 * @tc.name: HdfPowerRunningLockTest009
326 * @tc.desc: test Hold and UnHold, running lock type is navigation
327 * @tc.type: FUNC
328 * @tc.require: issueI6IU18
329 */
330 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest009, TestSize.Level1)
331 {
332 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
333 std::string setLockNameOne = runnninglockNameLabel + "navi.9";
334 std::string setLockNameTwo = runnninglockNameLabel + "navi.sec.9";
335 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
336 std::string errorLockName = runnninglockNameLabel + "navi.error.0";
337
338 PowerHdfState powerState = PowerHdfState::AWAKE;
339 RunningLockInfo runinglockInfo {};
340 runinglockInfo.name = setLockNameOne;
341 runinglockInfo.type = setLockType;
342 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
343 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
344
345 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
346 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
347 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
348 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
349
350 // runninglock type and same & timeoutMs < 0, hold lock failed
351 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
352
353 runinglockInfo.name = setLockNameTwo;
354 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
355 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
356
357 // unhold a non-existent lock, return success
358 runinglockInfo.type = errorLockType;
359 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
360 runinglockInfo.type = setLockType;
361 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
362
363 runinglockInfo.name = errorLockName;
364 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
365 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
366 runinglockInfo.name = setLockNameTwo;
367
368 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
369 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
370
371 runinglockInfo.name = setLockNameOne;
372 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
373 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
374 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
375 }
376
377 /**
378 * @tc.name: HdfPowerRunningLockTest010
379 * @tc.desc: test Hold and UnHold, running lock type is task
380 * @tc.type: FUNC
381 * @tc.require: issueI6IU18
382 */
383 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest010, TestSize.Level1)
384 {
385 RunningLockType setLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
386 std::string setLockNameOne = runnninglockNameLabel + "task.10";
387 std::string setLockNameTwo = runnninglockNameLabel + "task.sec.10";
388 RunningLockType errorLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
389 std::string errorLockName = runnninglockNameLabel + "task.error.10";
390
391 PowerHdfState powerState = PowerHdfState::AWAKE;
392 RunningLockInfo runinglockInfo {};
393 runinglockInfo.name = setLockNameOne;
394 runinglockInfo.type = setLockType;
395 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
396 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
397
398 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
399 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
400 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
401 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
402
403 // runninglock type and same & timeoutMs < 0, hold lock failed
404 EXPECT_EQ(HDF_FAILURE, RunningLockImpl::Hold(runinglockInfo, powerState));
405
406 runinglockInfo.name = setLockNameTwo;
407 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
408 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
409
410 // unhold a non-existent lock, return success
411 runinglockInfo.type = errorLockType;
412 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
413 runinglockInfo.type = setLockType;
414 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
415
416 runinglockInfo.name = errorLockName;
417 EXPECT_EQ(HDF_ERR_NOT_SUPPORT, RunningLockImpl::Unhold(runinglockInfo));
418 EXPECT_EQ(originCount + 2, RunningLockImpl::GetCount(runinglockInfo.type));
419 runinglockInfo.name = setLockNameTwo;
420
421 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
422 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
423
424 runinglockInfo.name = setLockNameOne;
425 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
426 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
427 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
428 }
429
430 /**
431 * @tc.name: HdfPowerRunningLockTest011
432 * @tc.desc: test Hold and UnHold, running lock type is 0, use default type Task
433 * @tc.type: FUNC
434 * @tc.require: issueI6IU18
435 */
436 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest011, TestSize.Level1)
437 {
438 RunningLockType setLockType = static_cast<RunningLockType>(0);
439 std::string setLockName = runnninglockNameLabel + "zero.11";
440 RunningLockType defaultLockType = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
441
442 PowerHdfState powerState = PowerHdfState::AWAKE;
443 RunningLockInfo runinglockInfo {};
444 runinglockInfo.name = setLockName;
445 runinglockInfo.type = setLockType;
446 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
447 uint32_t originCount = RunningLockImpl::GetCount(defaultLockType);
448
449 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
450 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(defaultLockType));
451
452 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
453 EXPECT_EQ(originCount, RunningLockImpl::GetCount(defaultLockType));
454 }
455
456 /**
457 * @tc.name: HdfPowerRunningLockTest012
458 * @tc.desc: test Hold and UnHold, running lock type and power state(sleep) are mutually exclusive
459 * @tc.type: FUNC
460 * @tc.require: issueI6IU18
461 */
462 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest012, TestSize.Level1)
463 {
464 PowerHdfState powerState = PowerHdfState::SLEEP;
465 std::string setLockName = runnninglockNameLabel + "sleep.12";
466
467 RunningLockInfo runinglockInfo {};
468 runinglockInfo.name = setLockName;
469 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
470
471 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
472 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
473
474 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
475 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
476
477 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
478 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
479
480 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
481 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
482
483 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
484 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
485
486 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
487 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
488 }
489
490 /**
491 * @tc.name: HdfPowerRunningLockTest013
492 * @tc.desc: test Hold and UnHold, running lock type and power state(sleep) are mutually exclusive
493 * @tc.type: FUNC
494 * @tc.require: issueI6IU18
495 */
496 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest013, TestSize.Level1)
497 {
498 PowerHdfState powerState = PowerHdfState::INACTIVE;
499 std::string setLockName = runnninglockNameLabel + "inactive.13";
500
501 RunningLockInfo runinglockInfo {};
502 runinglockInfo.name = setLockName;
503 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
504
505 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
506 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
507
508 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
509 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
510
511 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
512 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
513
514 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
515 EXPECT_EQ(HDF_ERR_INVALID_PARAM, RunningLockImpl::Hold(runinglockInfo, powerState));
516
517 runinglockInfo.name = setLockName + "phone.13";
518 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
519 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
520 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
521 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
522 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
523 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
524
525 runinglockInfo.name = setLockName + "notification.13";
526 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
527 originCount = RunningLockImpl::GetCount(runinglockInfo.type);
528 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
529 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
530 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
531 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
532 }
533
534 /**
535 * @tc.name: HdfPowerRunningLockTest014
536 * @tc.desc: test Hold and UnHold, timeout is None
537 * @tc.type: FUNC
538 * @tc.require: issueI6IU18
539 */
540 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest014, TestSize.Level1)
541 {
542 PowerHdfState powerState = PowerHdfState::AWAKE;
543 RunningLockInfo runinglockInfo {};
544 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
545 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
546
547 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
548 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
549 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
550
551 runinglockInfo.name = runnninglockNameLabel + "phone.14";
552 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
553 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
554 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
555 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
556 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
557
558 runinglockInfo.name = runnninglockNameLabel + "notify.14";
559 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
560 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
561 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
562 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
563 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
564
565 runinglockInfo.name = runnninglockNameLabel + "audio.14";
566 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
567 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
568 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
569 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
570 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
571
572 usleep(waitTimeOutMs * US_PER_MS);
573
574 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
575 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
576 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
577
578 runinglockInfo.name = runnninglockNameLabel + "phone.14";
579 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
580 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
581 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(runinglockInfo.type));
582 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
583
584 runinglockInfo.name = runnninglockNameLabel + "notify.14";
585 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
586 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
587 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(runinglockInfo.type));
588 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
589
590 runinglockInfo.name = runnninglockNameLabel + "audio.14";
591 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
592 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
593 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(runinglockInfo.type));
594 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
595 }
596
597 /**
598 * @tc.name: HdfPowerRunningLockTest015
599 * @tc.desc: test Hold and UnHold, timeout is None
600 * @tc.type: FUNC
601 * @tc.require: issueI6IU18
602 */
603 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest015, TestSize.Level1)
604 {
605 PowerHdfState powerState = PowerHdfState::AWAKE;
606 RunningLockInfo runinglockInfo {};
607 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
608 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
609
610 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
611 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
612 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
613
614 runinglockInfo.name = runnninglockNameLabel + "sport.15";
615 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
616 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
617 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
618 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
619 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
620
621 runinglockInfo.name = runnninglockNameLabel + "navi.15";
622 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
623 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
624 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
625 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
626 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
627
628 runinglockInfo.name = runnninglockNameLabel + "task.15";
629 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
630 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
631 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
632 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
633 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
634
635 usleep(waitTimeOutMs * US_PER_MS);
636
637 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
638 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
639 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
640
641 runinglockInfo.name = runnninglockNameLabel + "sport.15";
642 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
643 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
644 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(runinglockInfo.type));
645 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
646
647 runinglockInfo.name = runnninglockNameLabel + "navi.15";
648 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
649 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
650 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(runinglockInfo.type));
651 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
652
653 runinglockInfo.name = runnninglockNameLabel + "task.15";
654 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
655 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
656 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(runinglockInfo.type));
657 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
658 }
659
660 /**
661 * @tc.name: HdfPowerRunningLockTest016
662 * @tc.desc: test Hold and UnHold, timeout is default
663 * @tc.type: FUNC
664 * @tc.require: issueI6IU18
665 */
666 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest016, TestSize.Level1)
667 {
668 PowerHdfState powerState = PowerHdfState::AWAKE;
669 RunningLockInfo runinglockInfo {};
670 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
671 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
672
673 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
674 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
675 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
676 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
677 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
678 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
679 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
680 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
681 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
682
683 runinglockInfo.name = runnninglockNameLabel + "phone.16";
684 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
685 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
686 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
687
688 runinglockInfo.name = runnninglockNameLabel + "notify.16";
689 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
690 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
691 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
692
693 runinglockInfo.name = runnninglockNameLabel + "audio.16";
694 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
695 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
696 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
697
698 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
699 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
700 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
701 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
702 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
703 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
704
705 usleep(waitTimeOutMs * US_PER_MS);
706
707 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
708 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
709 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
710 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
711 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
712 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
713 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
714 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
715 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
716 }
717
718 /**
719 * @tc.name: HdfPowerRunningLockTest017
720 * @tc.desc: test Hold and UnHold, timeout is default
721 * @tc.type: FUNC
722 * @tc.require: issueI6IU18
723 */
724 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest017, TestSize.Level1)
725 {
726 PowerHdfState powerState = PowerHdfState::AWAKE;
727 RunningLockInfo runinglockInfo {};
728 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
729 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
730
731 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
732 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
733 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
734 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
735 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
736 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
737 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
738 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
739 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
740
741 runinglockInfo.name = runnninglockNameLabel + "sport.16";
742 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
743 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
744 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
745
746 runinglockInfo.name = runnninglockNameLabel + "navi.16";
747 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
748 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
749 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
750
751 runinglockInfo.name = runnninglockNameLabel + "task.16";
752 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
753 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
754 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
755
756 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
757 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
758 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
759 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
760 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
761 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
762
763 usleep(waitTimeOutMs * US_PER_MS);
764
765 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
766 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
767 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
768 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
769 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
770 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
771 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
772 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
773 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
774 }
775
776 /**
777 * @tc.name: HdfPowerRunningLockTest018
778 * @tc.desc: test Hold and UnHold, timeout is set
779 * @tc.type: FUNC
780 * @tc.require: issueI6IU18
781 */
782 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest018, TestSize.Level1)
783 {
784 PowerHdfState powerState = PowerHdfState::AWAKE;
785 RunningLockInfo runinglockInfo {};
786 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_SET_BASE_MS;
787 uint32_t timeoutIntervalMs = 10;
788 uint32_t waitTimeOutMs = 200;
789
790 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
791 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
792 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
793 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
794 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
795 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
796
797 runinglockInfo.name = runnninglockNameLabel + "phone.17";
798 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
799 runinglockInfo.timeoutMs += timeoutIntervalMs;
800 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
801 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
802
803 runinglockInfo.name = runnninglockNameLabel + "notify.17";
804 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
805 runinglockInfo.timeoutMs += timeoutIntervalMs;
806 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
807 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
808
809 runinglockInfo.name = runnninglockNameLabel + "audio.17";
810 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
811 runinglockInfo.timeoutMs += timeoutIntervalMs;
812 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
813 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
814
815 runinglockInfo.name = runnninglockNameLabel + "sport.17";
816 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
817 runinglockInfo.timeoutMs += timeoutIntervalMs;
818 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
819 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
820
821 runinglockInfo.name = runnninglockNameLabel + "navi.17";
822 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
823 runinglockInfo.timeoutMs += timeoutIntervalMs;
824 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
825 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
826
827 runinglockInfo.name = runnninglockNameLabel + "task.17";
828 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
829 runinglockInfo.timeoutMs += timeoutIntervalMs;
830 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
831 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
832
833 usleep(waitTimeOutMs * US_PER_MS);
834
835 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
836 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
837 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
838 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
839 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
840 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
841 }
842
843 /**
844 * @tc.name: HdfPowerRunningLockTest019
845 * @tc.desc: test Hold and UnHold, runninglock type and same & timeoutMs > 0, running lock updated
846 * @tc.type: FUNC
847 * @tc.require: issueI6IU18
848 */
849 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest019, TestSize.Level1)
850 {
851 PowerHdfState powerState = PowerHdfState::AWAKE;
852 RunningLockInfo runinglockInfo {};
853 runinglockInfo.name = runnninglockNameLabel + "phone.18";
854 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
855 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
856 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
857
858 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
859
860 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
861 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
862
863 usleep(waitTimeOutMs * US_PER_MS);
864 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
865
866 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
867 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
868 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
869
870 usleep(waitTimeOutMs * US_PER_MS);
871 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
872 }
873
874 /**
875 * @tc.name: HdfPowerRunningLockTest020
876 * @tc.desc: test Hold and UnHold, runninglock type and same & timeoutMs > 0, running lock updated
877 * @tc.type: FUNC
878 * @tc.require: issueI6IU18
879 */
880 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest020, TestSize.Level1)
881 {
882 PowerHdfState powerState = PowerHdfState::AWAKE;
883 RunningLockInfo runinglockInfo {};
884 runinglockInfo.name = runnninglockNameLabel + "audio.19";
885 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
886 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
887 uint32_t updateTimeOutMs = 50;
888 uint32_t waitTimeOutMs = 70;
889
890 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
891
892 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
893 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
894
895 runinglockInfo.timeoutMs = updateTimeOutMs;
896 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
897 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
898
899 usleep(waitTimeOutMs * US_PER_MS);
900 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
901 }
902
903 /**
904 * @tc.name: HdfPowerRunningLockTest021
905 * @tc.desc: test Hold and UnHold, manual unhold and timeout unhold
906 * @tc.type: FUNC
907 * @tc.require: issueI6IU18
908 */
909 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest021, TestSize.Level1)
910 {
911 PowerHdfState powerState = PowerHdfState::AWAKE;
912 RunningLockInfo runinglockInfo {};
913 runinglockInfo.name = runnninglockNameLabel + "sport.20";
914 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
915 runinglockInfo.timeoutMs = 100;
916 uint32_t manualUnholdTimeMs = 50;
917 uint32_t waitTimeOutMs = 120;
918
919 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
920
921 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
922 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
923
924 usleep(manualUnholdTimeMs * US_PER_MS);
925
926 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
927 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
928
929 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
930 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
931 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
932
933 usleep(waitTimeOutMs * US_PER_MS);
934 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
935
936 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
937 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
938 }
939 }