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_2;
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_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
473 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
474
475 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
476 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
477 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
478
479 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
480 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
481 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
482
483 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
484 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
485 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
486
487 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
488 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
489 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
490 }
491
492 /**
493 * @tc.name: HdfPowerRunningLockTest013
494 * @tc.desc: test Hold and UnHold, running lock type and power state(sleep) are mutually exclusive
495 * @tc.type: FUNC
496 * @tc.require: issueI6IU18
497 */
498 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest013, TestSize.Level1)
499 {
500 PowerHdfState powerState = PowerHdfState::INACTIVE;
501 std::string setLockName = runnninglockNameLabel + "inactive.13";
502
503 RunningLockInfo runinglockInfo {};
504 runinglockInfo.name = setLockName;
505 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
506
507 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
508 runinglockInfo.name = setLockName + "sport.13";
509 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
510 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
511
512 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
513 runinglockInfo.name = setLockName + "navigation.13";
514 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
515 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
516
517 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
518 runinglockInfo.name = setLockName + "task.13";
519 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
520 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
521
522 runinglockInfo.name = setLockName + "phone.13";
523 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
524 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
525 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
526 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
527 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
528 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
529
530 runinglockInfo.name = setLockName + "notification.13";
531 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
532 originCount = RunningLockImpl::GetCount(runinglockInfo.type);
533 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
534 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
535 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
536 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
537
538 runinglockInfo.name = setLockName + "audio.13";
539 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
540 originCount = RunningLockImpl::GetCount(runinglockInfo.type);
541 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
542 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
543 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
544 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
545 }
546
547 /**
548 * @tc.name: HdfPowerRunningLockTest014
549 * @tc.desc: test Hold and UnHold, timeout is None
550 * @tc.type: FUNC
551 * @tc.require: issueI6IU18
552 */
553 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest014, TestSize.Level1)
554 {
555 PowerHdfState powerState = PowerHdfState::AWAKE;
556 RunningLockInfo runinglockInfo {};
557 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
558 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
559
560 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
561 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
562 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
563
564 runinglockInfo.name = runnninglockNameLabel + "phone.14";
565 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
566 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
567 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
568 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
569 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
570
571 runinglockInfo.name = runnninglockNameLabel + "notify.14";
572 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
573 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
574 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
575 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
576 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
577
578 runinglockInfo.name = runnninglockNameLabel + "audio.14";
579 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
580 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
581 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
582 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
583 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
584
585 usleep(waitTimeOutMs * US_PER_MS);
586
587 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
588 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
589 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
590
591 runinglockInfo.name = runnninglockNameLabel + "phone.14";
592 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
593 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
594 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(runinglockInfo.type));
595 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
596
597 runinglockInfo.name = runnninglockNameLabel + "notify.14";
598 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
599 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
600 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(runinglockInfo.type));
601 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
602
603 runinglockInfo.name = runnninglockNameLabel + "audio.14";
604 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
605 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
606 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(runinglockInfo.type));
607 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
608 }
609
610 /**
611 * @tc.name: HdfPowerRunningLockTest015
612 * @tc.desc: test Hold and UnHold, timeout is None
613 * @tc.type: FUNC
614 * @tc.require: issueI6IU18
615 */
616 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest015, TestSize.Level1)
617 {
618 PowerHdfState powerState = PowerHdfState::AWAKE;
619 RunningLockInfo runinglockInfo {};
620 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
621 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
622
623 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
624 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
625 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
626
627 runinglockInfo.name = runnninglockNameLabel + "sport.15";
628 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
629 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
630 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
631 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
632 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
633
634 runinglockInfo.name = runnninglockNameLabel + "navi.15";
635 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
636 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
637 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
638 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
639 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
640
641 runinglockInfo.name = runnninglockNameLabel + "task.15";
642 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
643 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
644 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
645 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
646 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
647
648 usleep(waitTimeOutMs * US_PER_MS);
649
650 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
651 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
652 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
653
654 runinglockInfo.name = runnninglockNameLabel + "sport.15";
655 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
656 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
657 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(runinglockInfo.type));
658 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
659
660 runinglockInfo.name = runnninglockNameLabel + "navi.15";
661 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
662 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
663 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(runinglockInfo.type));
664 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
665
666 runinglockInfo.name = runnninglockNameLabel + "task.15";
667 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
668 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
669 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(runinglockInfo.type));
670 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(RunningLockImpl::GetRunningLockTag(runinglockInfo.type)));
671 }
672
673 /**
674 * @tc.name: HdfPowerRunningLockTest016
675 * @tc.desc: test Hold and UnHold, timeout is default
676 * @tc.type: FUNC
677 * @tc.require: issueI6IU18
678 */
679 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest016, TestSize.Level1)
680 {
681 PowerHdfState powerState = PowerHdfState::AWAKE;
682 RunningLockInfo runinglockInfo {};
683 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
684 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
685
686 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
687 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
688 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
689 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
690 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
691 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
692 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
693 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
694 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
695
696 runinglockInfo.name = runnninglockNameLabel + "phone.16";
697 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
698 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
699 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
700
701 runinglockInfo.name = runnninglockNameLabel + "notify.16";
702 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
703 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
704 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
705
706 runinglockInfo.name = runnninglockNameLabel + "audio.16";
707 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
708 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
709 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
710
711 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
712 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
713 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
714 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
715 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
716 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
717
718 usleep(waitTimeOutMs * US_PER_MS);
719
720 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
721 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
722 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
723 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
724 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE)));
725 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
726 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION)));
727 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
728 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)));
729 }
730
731 /**
732 * @tc.name: HdfPowerRunningLockTest017
733 * @tc.desc: test Hold and UnHold, timeout is default
734 * @tc.type: FUNC
735 * @tc.require: issueI6IU18
736 */
737 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest017, TestSize.Level1)
738 {
739 PowerHdfState powerState = PowerHdfState::AWAKE;
740 RunningLockInfo runinglockInfo {};
741 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
742 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
743
744 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
745 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
746 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
747 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
748 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
749 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
750 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
751 ASSERT_EQ(false, MockWakeLockName::FindWakeLockName(
752 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
753
754 runinglockInfo.name = runnninglockNameLabel + "sport.16";
755 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
756 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
757 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
758
759 runinglockInfo.name = runnninglockNameLabel + "navi.16";
760 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
761 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
762 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
763
764 runinglockInfo.name = runnninglockNameLabel + "task.16";
765 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
766 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
767 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
768
769 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
770 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
771 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
772 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
773 EXPECT_EQ(true, MockWakeLockName::FindWakeLockName(
774 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
775
776 usleep(waitTimeOutMs * US_PER_MS);
777
778 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
779 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
780 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
781 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
782 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT)));
783 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
784 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION)));
785 EXPECT_EQ(false, MockWakeLockName::FindWakeLockName(
786 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK)));
787 }
788
789 /**
790 * @tc.name: HdfPowerRunningLockTest018
791 * @tc.desc: test Hold and UnHold, timeout is set
792 * @tc.type: FUNC
793 * @tc.require: issueI6IU18
794 */
795 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest018, TestSize.Level1)
796 {
797 PowerHdfState powerState = PowerHdfState::AWAKE;
798 RunningLockInfo runinglockInfo {};
799 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_SET_BASE_MS;
800 uint32_t timeoutIntervalMs = 10;
801 uint32_t waitTimeOutMs = 200;
802
803 uint32_t oriPhoneCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
804 uint32_t oriNotifyCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
805 uint32_t oriAudioCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
806 uint32_t oriSportCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
807 uint32_t oriNaviCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
808 uint32_t oriTaskCount = RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
809
810 runinglockInfo.name = runnninglockNameLabel + "phone.17";
811 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
812 runinglockInfo.timeoutMs += timeoutIntervalMs;
813 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
814 EXPECT_EQ(oriPhoneCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
815
816 runinglockInfo.name = runnninglockNameLabel + "notify.17";
817 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION;
818 runinglockInfo.timeoutMs += timeoutIntervalMs;
819 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
820 EXPECT_EQ(oriNotifyCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
821
822 runinglockInfo.name = runnninglockNameLabel + "audio.17";
823 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
824 runinglockInfo.timeoutMs += timeoutIntervalMs;
825 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
826 EXPECT_EQ(oriAudioCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
827
828 runinglockInfo.name = runnninglockNameLabel + "sport.17";
829 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
830 runinglockInfo.timeoutMs += timeoutIntervalMs;
831 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
832 EXPECT_EQ(oriSportCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
833
834 runinglockInfo.name = runnninglockNameLabel + "navi.17";
835 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION;
836 runinglockInfo.timeoutMs += timeoutIntervalMs;
837 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
838 EXPECT_EQ(oriNaviCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
839
840 runinglockInfo.name = runnninglockNameLabel + "task.17";
841 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
842 runinglockInfo.timeoutMs += timeoutIntervalMs;
843 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
844 EXPECT_EQ(oriTaskCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
845
846 usleep(waitTimeOutMs * US_PER_MS);
847
848 EXPECT_EQ(oriPhoneCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE));
849 EXPECT_EQ(oriNotifyCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION));
850 EXPECT_EQ(oriAudioCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO));
851 EXPECT_EQ(oriSportCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT));
852 EXPECT_EQ(oriNaviCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION));
853 EXPECT_EQ(oriTaskCount, RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK));
854 }
855
856 /**
857 * @tc.name: HdfPowerRunningLockTest019
858 * @tc.desc: test Hold and UnHold, runninglock type and same & timeoutMs > 0, running lock updated
859 * @tc.type: FUNC
860 * @tc.require: issueI6IU18
861 */
862 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest019, TestSize.Level1)
863 {
864 PowerHdfState powerState = PowerHdfState::AWAKE;
865 RunningLockInfo runinglockInfo {};
866 runinglockInfo.name = runnninglockNameLabel + "phone.18";
867 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE;
868 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
869 uint32_t waitTimeOutMs = DEFAULT_TIMEOUT_FOR_TEST_MS + 10;
870
871 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
872
873 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
874 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
875
876 usleep(waitTimeOutMs * US_PER_MS);
877 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
878
879 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
880 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
881 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
882
883 usleep(waitTimeOutMs * US_PER_MS);
884 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
885 }
886
887 /**
888 * @tc.name: HdfPowerRunningLockTest020
889 * @tc.desc: test Hold and UnHold, runninglock type and same & timeoutMs > 0, running lock updated
890 * @tc.type: FUNC
891 * @tc.require: issueI6IU18
892 */
893 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest020, TestSize.Level1)
894 {
895 PowerHdfState powerState = PowerHdfState::AWAKE;
896 RunningLockInfo runinglockInfo {};
897 runinglockInfo.name = runnninglockNameLabel + "audio.19";
898 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO;
899 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_DEFAULT;
900 uint32_t updateTimeOutMs = 50;
901 uint32_t waitTimeOutMs = 70;
902
903 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
904
905 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
906 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
907
908 runinglockInfo.timeoutMs = updateTimeOutMs;
909 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
910 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
911
912 usleep(waitTimeOutMs * US_PER_MS);
913 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
914 }
915
916 /**
917 * @tc.name: HdfPowerRunningLockTest021
918 * @tc.desc: test Hold and UnHold, manual unhold and timeout unhold
919 * @tc.type: FUNC
920 * @tc.require: issueI6IU18
921 */
922 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest021, TestSize.Level1)
923 {
924 PowerHdfState powerState = PowerHdfState::AWAKE;
925 RunningLockInfo runinglockInfo {};
926 runinglockInfo.name = runnninglockNameLabel + "sport.20";
927 runinglockInfo.type = RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT;
928 runinglockInfo.timeoutMs = 100;
929 uint32_t manualUnholdTimeMs = 50;
930 uint32_t waitTimeOutMs = 120;
931
932 uint32_t originCount = RunningLockImpl::GetCount(runinglockInfo.type);
933
934 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
935 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
936
937 usleep(manualUnholdTimeMs * US_PER_MS);
938
939 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
940 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
941
942 runinglockInfo.timeoutMs = RUNNINGLOCK_TIMEOUT_NONE;
943 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Hold(runinglockInfo, powerState));
944 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
945
946 usleep(waitTimeOutMs * US_PER_MS);
947 EXPECT_EQ(originCount + 1, RunningLockImpl::GetCount(runinglockInfo.type));
948
949 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::Unhold(runinglockInfo));
950 EXPECT_EQ(originCount, RunningLockImpl::GetCount(runinglockInfo.type));
951 }
952
953 /**
954 * @tc.name: HdfPowerRunningLockTest022
955 * @tc.desc: test HoldLock and UnholdLock
956 * @tc.type: FUNC
957 * @tc.require: issueI9C4GG
958 */
959 HWTEST_F(HdfPowerRunningLockTest, HdfPowerRunningLockTest022, TestSize.Level1)
960 {
961 PowerHdfState powerState = PowerHdfState::AWAKE;
962 RunningLockInfo runinglockInfo1 {};
963 runinglockInfo1.name = runnninglockNameLabel + "task.22";
964 runinglockInfo1.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
965 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::HoldLock(runinglockInfo1, powerState));
966 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::UnholdLock(runinglockInfo1));
967
968 RunningLockInfo runinglockInfo2 {};
969 runinglockInfo2.name = "";
970 runinglockInfo2.type = RunningLockType::RUNNINGLOCK_BACKGROUND_TASK;
971 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::HoldLock(runinglockInfo2, powerState));
972 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::UnholdLock(runinglockInfo2));
973
974 powerState = PowerHdfState::SLEEP;
975 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::HoldLock(runinglockInfo1, powerState));
976 EXPECT_EQ(HDF_SUCCESS, RunningLockImpl::UnholdLock(runinglockInfo1));
977 }
978 }