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