• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "power_mgr_st_mock_test.h"
17 #include "power_mgr_client.h"
18 
19 using namespace testing::ext;
20 using namespace OHOS::PowerMgr;
21 using namespace OHOS;
22 using namespace std;
23 using ::testing::_;
24 
25 static sptr<PowerMgrService> g_service;
26 static MockStateAction* g_shutdownState;
27 static MockStateAction* g_stateAction;
28 static MockPowerAction* g_powerAction;
29 static MockLockAction* g_lockAction;
30 
ResetMockAction()31 static void ResetMockAction()
32 {
33     POWER_HILOGI(LABEL_TEST, "ResetMockAction:Start");
34     g_stateAction = new MockStateAction();
35     g_shutdownState = new MockStateAction();
36     g_powerAction = new MockPowerAction();
37     g_lockAction = new MockLockAction();
38     g_service->EnableMock(g_stateAction, g_shutdownState, g_powerAction, g_lockAction);
39     POWER_HILOGI(LABEL_TEST, "ResetMockAction:End");
40 }
41 
SetUpTestCase(void)42 void PowerMgrSTMockTest::SetUpTestCase(void)
43 {
44     // create singleton service object at the beginning
45     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
46     g_service->OnStart();
47     ResetMockAction();
48 }
49 
TearDownTestCase(void)50 void PowerMgrSTMockTest::TearDownTestCase(void)
51 {
52     g_service->OnStop();
53     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
54 }
55 
SetUp(void)56 void PowerMgrSTMockTest::SetUp(void)
57 {
58     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
59 }
60 
TearDown(void)61 void PowerMgrSTMockTest::TearDown(void)
62 {
63     ResetMockAction();
64 }
65 
66 namespace {
67 constexpr int32_t TEST_TIMES = 1000;
68 /**
69  * @tc.name: PowerMgrMock001
70  * @tc.desc: test RebootDevice by mock
71  * @tc.type: FUNC
72  * @tc.require: issueI5MJZJ
73  */
74 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock001, TestSize.Level2)
75 {
76     GTEST_LOG_(INFO) << "PowerMgrMock001: start";
77     POWER_HILOGI(LABEL_TEST, "PowerMgrMock001 function start!");
78 
79     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
80     if (pms == nullptr) {
81         GTEST_LOG_(INFO) << "PowerMgrMock001: Failed to get PowerMgrService";
82     }
83 
84     EXPECT_CALL(*g_powerAction, Reboot(std::string("test_case"))).Times(1);
85     pms->RebootDevice(std::string("test_case"));
86     usleep(SLEEP_WAIT_TIME_MS * TRANSFER_NS_TO_MS);
87     POWER_HILOGI(LABEL_TEST, "PowerMgrMock001 function end!");
88     GTEST_LOG_(INFO) << "PowerMgrMock001: end";
89 }
90 
91 /**
92  * @tc.name: PowerMgrMock002
93  * @tc.desc: test ShutDownDevice by mock
94  * @tc.type: FUNC
95  * @tc.require: issueI5MJZJ
96  */
97 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock002, TestSize.Level2)
98 {
99     GTEST_LOG_(INFO) << "PowerMgrMock002: start";
100     POWER_HILOGI(LABEL_TEST, "PowerMgrMock002 function start!");
101 
102     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
103     if (pms == nullptr) {
104         GTEST_LOG_(INFO) << "PowerMgrMock002: Failed to get PowerMgrService";
105     }
106 
107     EXPECT_CALL(*g_powerAction, Shutdown(std::string("test_case"))).Times(1);
108     pms->ShutDownDevice(std::string("test_case"));
109     usleep(SLEEP_WAIT_TIME_MS * TRANSFER_NS_TO_MS);
110     POWER_HILOGI(LABEL_TEST, "PowerMgrMock002 function end!");
111     GTEST_LOG_(INFO) << "PowerMgrMock002: end";
112 }
113 
114 /**
115  * @tc.name: PowerMgrMock003
116  * @tc.desc: test ForceSuspendDevice by mock
117  * @tc.type: FUNC
118  * @tc.require: issueI5MJZJ
119  */
120 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock003, TestSize.Level2)
121 {
122     GTEST_LOG_(INFO) << "PowerMgrMock003: start";
123     POWER_HILOGI(LABEL_TEST, "PowerMgrMock003 function start!");
124 
125     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
126     if (pms == nullptr) {
127         GTEST_LOG_(INFO) << "PowerMgrMock003: Failed to get PowerMgrService";
128     }
129 
130     POWER_HILOGI(LABEL_TEST, "PowerMgrMock003:forcesuspend");
131     EXPECT_EQ(pms->ForceSuspendDevice(0), PowerErrors::ERR_OK);
132 
133     POWER_HILOGI(LABEL_TEST, "PowerMgrMock003 function end!");
134     GTEST_LOG_(INFO) << "PowerMgrMock003: end";
135 }
136 
137 /**
138  * @tc.name: PowerMgrMock004
139  * @tc.desc: test RunningLock by mock
140  * @tc.type: FUNC
141  * @tc.require: issueI5MJZJ
142  */
143 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock004, TestSize.Level2)
144 {
145     GTEST_LOG_(INFO) << "PowerMgrMock004: start";
146     POWER_HILOGI(LABEL_TEST, "PowerMgrMock004 function start!");
147 
148     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
149     if (pms == nullptr) {
150         GTEST_LOG_(INFO) << "PowerMgrMock004: Failed to get PowerMgrService";
151     }
152 
153     pms->SetDisplayOffTime(SET_DISPLAY_OFF_TIME_MS);
154     sptr<IRemoteObject> token = new RunningLockTokenStub();
155     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
156     pms->CreateRunningLock(token, info);
157     pms->Lock(token);
158     EXPECT_EQ(pms->IsUsed(token), true);
159     pms->UnLock(token);
160     EXPECT_EQ(pms->IsUsed(token), false);
161 
162     pms->SetDisplayOffTime(PowerStateMachine::DEFAULT_DISPLAY_OFF_TIME_MS);
163     POWER_HILOGI(LABEL_TEST, "PowerMgrMock004 function end!");
164     GTEST_LOG_(INFO) << "PowerMgrMock004: end";
165 }
166 
167 /**
168  * @tc.name: PowerMgrMock005
169  * @tc.desc: test proximity RunningLock by mock
170  * @tc.type: FUNC
171  * @tc.require: issueI5MJZJ
172  */
173 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock005, TestSize.Level2)
174 {
175     GTEST_LOG_(INFO) << "PowerMgrMock005: start";
176     POWER_HILOGI(LABEL_TEST, "PowerMgrMock005 function start!");
177 #ifdef HAS_SENSORS_SENSOR_PART
178     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
179     if (pms == nullptr) {
180         GTEST_LOG_(INFO) << "PowerMgrMock005: Failed to get PowerMgrService";
181     }
182 
183     sptr<IRemoteObject> token = new RunningLockTokenStub();
184     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
185     pms->CreateRunningLock(token, info);
186 
187     pms->Lock(token);
188     EXPECT_EQ(pms->IsUsed(token), true);
189     sleep(SCREEN_DIM_WAIT_TIME_S + ONE_SECOND);
190 
191     pms->UnLock(token);
192     EXPECT_EQ(pms->IsUsed(token), false);
193 #endif
194     POWER_HILOGI(LABEL_TEST, "PowerMgrMock005 function end!");
195     GTEST_LOG_(INFO) << "PowerMgrMock005: end";
196 }
197 
198 /**
199  * @tc.name: PowerMgrMock006
200  * @tc.desc: test ForceSuspendDevice by mock during Inactive
201  * @tc.type: FUNC
202  * @tc.require: issueI5MJZJ
203  */
204 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock006, TestSize.Level2)
205 {
206     GTEST_LOG_(INFO) << "PowerMgrMock006: start";
207     POWER_HILOGI(LABEL_TEST, "PowerMgrMock006 function start!");
208     int64_t PARM_ZERO = 0;
209     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
210     if (pms == nullptr) {
211         GTEST_LOG_(INFO) << "PowerMgrMock006: Failed to get PowerMgrService";
212     }
213 
214     pms->SuspendDevice(PARM_ZERO, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
215     pms->ForceSuspendDevice(PARM_ZERO);
216     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
217 
218     POWER_HILOGI(LABEL_TEST, "PowerMgrMock006 function end!");
219     GTEST_LOG_(INFO) << "PowerMgrMock006: end";
220 }
221 
222 /**
223  * @tc.name: PowerMgrMock007
224  * @tc.desc: test Screen RunningLock by mock
225  * @tc.type: FUNC
226  * @tc.require: issueI5MJZJ
227  */
228 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock007, TestSize.Level2)
229 {
230     GTEST_LOG_(INFO) << "PowerMgrMock007: start";
231     POWER_HILOGI(LABEL_TEST, "PowerMgrMock007 function start!");
232 
233     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
234     if (pms == nullptr) {
235         GTEST_LOG_(INFO) << "PowerMgrMock007: Failed to get PowerMgrService";
236     }
237 
238     sptr<IRemoteObject> token = new RunningLockTokenStub();
239     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
240     pms->CreateRunningLock(token, info);
241 
242     pms->Lock(token);
243     EXPECT_EQ(pms->IsUsed(token), true);
244 
245     EXPECT_EQ(pms->ForceSuspendDevice(0), PowerErrors::ERR_OK);
246     sleep(SLEEP_WAIT_TIME_S + ONE_SECOND);
247 
248     pms->UnLock(token);
249     EXPECT_EQ(pms->IsUsed(token), false);
250 
251     POWER_HILOGI(LABEL_TEST, "PowerMgrMock007 function end!");
252     GTEST_LOG_(INFO) << "PowerMgrMock007: end";
253 }
254 
255 /**
256  * @tc.name: PowerMgrMock008
257  * @tc.desc: test proximity screen control RunningLock by mock
258  * @tc.type: FUNC
259  * @tc.require: issueI5MJZJ
260  */
261 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock008, TestSize.Level2)
262 {
263     GTEST_LOG_(INFO) << "PowerMgrMock008: start";
264     POWER_HILOGI(LABEL_TEST, "PowerMgrMock008 function start!");
265 
266 #ifdef HAS_SENSORS_SENSOR_PART
267     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
268     if (pms == nullptr) {
269         GTEST_LOG_(INFO) << "PowerMgrMock008: Failed to get PowerMgrService";
270     }
271 
272     pms->SetDisplayOffTime(SET_DISPLAY_OFF_TIME_MS);
273     sptr<IRemoteObject> token = new RunningLockTokenStub();
274     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
275     pms->CreateRunningLock(token, info);
276     EXPECT_CALL(*g_stateAction, GetDisplayState())
277         .Times(::testing::AtLeast(1))
278         .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_ON));
279     EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_OFF, ::testing::_))
280         .Times(::testing::AtLeast(1))
281         .WillOnce(::testing::Return(ActionResult::SUCCESS));
282     pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
283     EXPECT_EQ(PowerState::INACTIVE, pms->GetState());
284     EXPECT_CALL(*g_stateAction, GetDisplayState())
285         .Times(::testing::AtLeast(1))
286         .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_OFF));
287     EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_ON, ::testing::_))
288         .Times(::testing::AtLeast(1))
289         .WillOnce(::testing::Return(ActionResult::SUCCESS));
290     sleep(REFRESHACTIVITY_WAIT_TIME_S + ONE_SECOND);
291     pms->Lock(token);
292     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
293     EXPECT_EQ(pms->IsUsed(token), true);
294 
295     pms->UnLock(token);
296     EXPECT_EQ(pms->IsUsed(token), false);
297 
298     pms->SetDisplayOffTime(PowerStateMachine::DEFAULT_DISPLAY_OFF_TIME_MS);
299 #endif
300     POWER_HILOGI(LABEL_TEST, "PowerMgrMock008 function end!");
301     GTEST_LOG_(INFO) << "PowerMgrMock008: end";
302 }
303 
304 /**
305  * @tc.name: PowerMgrMock009
306  * @tc.desc: test proximity screen control RunningLock by mock
307  * @tc.type: FUNC
308  * @tc.require: issueI5MJZJ
309  */
310 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock009, TestSize.Level2)
311 {
312     GTEST_LOG_(INFO) << "PowerMgrMock009: start";
313     POWER_HILOGI(LABEL_TEST, "PowerMgrMock009 function start!");
314 #ifdef HAS_SENSORS_SENSOR_PART
315 
316     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
317     if (pms == nullptr) {
318         GTEST_LOG_(INFO) << "PowerMgrMock009: Failed to get PowerMgrService";
319     }
320 
321     pms->SetDisplayOffTime(SET_DISPLAY_OFF_TIME_MS);
322     sptr<IRemoteObject> token = new RunningLockTokenStub();
323     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
324     pms->CreateRunningLock(token, info);
325     EXPECT_EQ(pms->ForceSuspendDevice(0), PowerErrors::ERR_OK);
326     EXPECT_CALL(*g_stateAction, GetDisplayState())
327         .Times(::testing::AtLeast(1))
328         .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_OFF));
329     EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_ON, ::testing::_))
330         .Times(::testing::AtLeast(1))
331         .WillOnce(::testing::Return(ActionResult::SUCCESS));
332     sleep(REFRESHACTIVITY_WAIT_TIME_S + ONE_SECOND);
333     pms->Lock(token);
334     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
335     EXPECT_EQ(pms->IsUsed(token), true);
336     pms->UnLock(token);
337     EXPECT_EQ(pms->IsUsed(token), false);
338 
339     pms->SetDisplayOffTime(PowerStateMachine::DEFAULT_DISPLAY_OFF_TIME_MS);
340 #endif
341     POWER_HILOGI(LABEL_TEST, "PowerMgrMock009 function end!");
342     GTEST_LOG_(INFO) << "PowerMgrMock009: end";
343 }
344 
345 /**
346  * @tc.name: PowerMgrMock010
347  * @tc.desc: test proximity RunningLock by mock
348  * @tc.type: FUNC
349  * @tc.require: issueI5MJZJ
350  */
351 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock010, TestSize.Level2)
352 {
353     GTEST_LOG_(INFO) << "PowerMgrMock010: start";
354     POWER_HILOGI(LABEL_TEST, "PowerMgrMock010 function start!");
355 
356 #ifdef HAS_SENSORS_SENSOR_PART
357     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
358     if (pms == nullptr) {
359         GTEST_LOG_(INFO) << "PowerMgrMock010: Failed to get PowerMgrService";
360     }
361 
362     sptr<IRemoteObject> token = new RunningLockTokenStub();
363     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
364     pms->CreateRunningLock(token, info);
365 
366     pms->Lock(token);
367     EXPECT_EQ(pms->IsUsed(token), true);
368     pms->MockProximity(IProximityController::PROXIMITY_CLOSE);
369     pms->UnLock(token);
370 
371 #endif
372     POWER_HILOGI(LABEL_TEST, "PowerMgrMock010 function end!");
373     GTEST_LOG_(INFO) << "PowerMgrMock010: end";
374 }
375 
376 /**
377  * @tc.name: PowerMgrMock011
378  * @tc.desc: test proximity RunningLock by mock
379  * @tc.type: FUNC
380  * @tc.require: issueI5MJZJ
381  */
382 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock011, TestSize.Level2)
383 {
384     GTEST_LOG_(INFO) << "PowerMgrMock011: start";
385     POWER_HILOGI(LABEL_TEST, "PowerMgrMock011 function start!");
386 
387 #ifdef HAS_SENSORS_SENSOR_PART
388     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
389     if (pms == nullptr) {
390         GTEST_LOG_(INFO) << "PowerMgrMock011: Failed to get PowerMgrService";
391     }
392 
393     sptr<IRemoteObject> token = new RunningLockTokenStub();
394     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
395     pms->CreateRunningLock(token, info);
396     pms->Lock(token);
397     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
398     EXPECT_EQ(pms->IsUsed(token), true);
399     EXPECT_EQ(PowerState::AWAKE, pms->GetState());
400     pms->MockProximity(IProximityController::PROXIMITY_CLOSE);
401     pms->UnLock(token);
402 
403 #endif
404     POWER_HILOGI(LABEL_TEST, "PowerMgrMock011 function end!");
405     GTEST_LOG_(INFO) << "PowerMgrMock011: end";
406 }
407 
408 /**
409  * @tc.name: PowerMgrMock012
410  * @tc.desc: test proximity RunningLock by mock
411  * @tc.type: FUNC
412  * @tc.require: issueI5MJZJ
413  */
414 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock012, TestSize.Level2)
415 {
416     GTEST_LOG_(INFO) << "PowerMgrMock012: start";
417     POWER_HILOGI(LABEL_TEST, "PowerMgrMock012 function start!");
418 #ifdef HAS_SENSORS_SENSOR_PART
419     int i;
420 
421     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
422     if (pms == nullptr) {
423         GTEST_LOG_(INFO) << "PowerMgrMock012: Failed to get PowerMgrService";
424     }
425 
426     pms->SetDisplayOffTime(ASYNC_WAIT_TIME_S * TEST_TIMES);
427     sptr<IRemoteObject> token = new RunningLockTokenStub();
428     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
429     pms->CreateRunningLock(token, info);
430     for (i = 0; i < 10; i++) {
431         pms->Lock(token);
432         EXPECT_EQ(pms->IsUsed(token), true);
433         pms->UnLock(token);
434         EXPECT_EQ(pms->IsUsed(token), false);
435     }
436     pms->Lock(token);
437     EXPECT_EQ(pms->IsUsed(token), true);
438     pms->UnLock(token);
439 
440     pms->SetDisplayOffTime(PowerStateMachine::DEFAULT_DISPLAY_OFF_TIME_MS);
441 #endif
442     POWER_HILOGI(LABEL_TEST, "PowerMgrMock012 function end!");
443     GTEST_LOG_(INFO) << "PowerMgrMock012: end";
444 }
445 
446 /**
447  * @tc.name: PowerMgrMock013
448  * @tc.desc: test proximity RunningLock by mock
449  * @tc.type: FUNC
450  * @tc.require: issueI5MJZJ
451  */
452 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock013, TestSize.Level2)
453 {
454     GTEST_LOG_(INFO) << "PowerMgrMock013: start";
455     POWER_HILOGI(LABEL_TEST, "PowerMgrMock013 function start!");
456 
457 #ifdef HAS_SENSORS_SENSOR_PART
458     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
459     if (pms == nullptr) {
460         GTEST_LOG_(INFO) << "PowerMgrMock013: Failed to get PowerMgrService";
461     }
462 
463     sptr<IRemoteObject> token = new RunningLockTokenStub();
464     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
465     pms->CreateRunningLock(token, info);
466     pms->Lock(token);
467     EXPECT_EQ(pms->IsUsed(token), true);
468 
469     pms->ForceSuspendDevice(0);
470 
471     pms->UnLock(token);
472     EXPECT_EQ(pms->IsUsed(token), false);
473 
474 #endif
475     POWER_HILOGI(LABEL_TEST, "PowerMgrMock013 function end!");
476     GTEST_LOG_(INFO) << "PowerMgrMock013: end";
477 }
478 
479 /**
480  * @tc.name: PowerMgrMock014
481  * @tc.desc: test proximity RunningLock by mock
482  * @tc.type: FUNC
483  * @tc.require: issueI5MJZJ
484  */
485 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock014, TestSize.Level2)
486 {
487     GTEST_LOG_(INFO) << "PowerMgrMock014: start";
488     POWER_HILOGI(LABEL_TEST, "PowerMgrMock014 function start!");
489 
490 #ifdef HAS_SENSORS_SENSOR_PART
491     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
492     if (pms == nullptr) {
493         GTEST_LOG_(INFO) << "PowerMgrMock014: Failed to get PowerMgrService";
494     }
495 
496     pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
497     sptr<IRemoteObject> token = new RunningLockTokenStub();
498     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
499     pms->CreateRunningLock(token, info);
500 
501     pms->Lock(token);
502     EXPECT_EQ(pms->IsUsed(token), true);
503 
504     pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test"));
505     EXPECT_EQ(PowerState::AWAKE, pms->GetState());
506 
507 #endif
508     POWER_HILOGI(LABEL_TEST, "PowerMgrMock014 function end!");
509     GTEST_LOG_(INFO) << "PowerMgrMock014: end";
510 }
511 
512 /**
513  * @tc.name: PowerMgrMock015
514  * @tc.desc: test The display status and power status are inconsistent SuspendDevice
515  * @tc.type: FUNC
516  * @tc.require: issueI66HYP
517  */
518 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock015, TestSize.Level2)
519 {
520     GTEST_LOG_(INFO) << "PowerMgrMock015: start.";
521     POWER_HILOGI(LABEL_TEST, "PowerMgrMock015 function start!");
522 
523     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
524     ASSERT_TRUE(pms != nullptr);
525 
526     // Set the power status to INACTIVE
527     pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
528     EXPECT_TRUE(pms->GetState() == PowerState::INACTIVE || pms->GetState() == PowerState::SLEEP);
529 
530     // Analog display return DISPLAY_ON
531     EXPECT_CALL(*g_stateAction, GetDisplayState())
532         .Times(::testing::AtLeast(1))
533         .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_ON));
534     EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_OFF, ::testing::_))
535         .Times(::testing::AtLeast(1))
536         .WillOnce(::testing::Return(ActionResult::SUCCESS));
537     // The status is changed to display status ON, and the screen is off again
538     pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
539     EXPECT_TRUE(pms->GetState() == PowerState::INACTIVE || pms->GetState() == PowerState::SLEEP);
540 
541     POWER_HILOGI(LABEL_TEST, "PowerMgrMock015 function end!");
542     GTEST_LOG_(INFO) << "PowerMgrMock015: end.";
543 }
544 
545 /**
546  * @tc.name: PowerMgrMock016
547  * @tc.desc: test The display status and power status are inconsistent WakeupDevice
548  * @tc.type: FUNC
549  * @tc.require: issueI66HYP
550  */
551 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock016, TestSize.Level2)
552 {
553     GTEST_LOG_(INFO) << "PowerMgrMock016: start.";
554     POWER_HILOGI(LABEL_TEST, "PowerMgrMock016 function start!");
555     int64_t PARM_ZERO = 0;
556     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
557     ASSERT_TRUE(pms != nullptr);
558 
559     // Set the power status to AWAKE
560     pms->WakeupDevice(PARM_ZERO, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("PowerMgrMock016"));
561     EXPECT_TRUE(pms->GetState() == PowerState::AWAKE);
562 
563     // Analog display return DISPLAY_OFF
564     EXPECT_CALL(*g_stateAction, GetDisplayState())
565         .Times(::testing::AtLeast(1))
566         .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_OFF));
567     EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_ON, ::testing::_))
568         .Times(::testing::AtLeast(1))
569         .WillOnce(::testing::Return(ActionResult::SUCCESS));
570     // The status is changed to display status OFF, and the screen is off again
571     pms->WakeupDevice(PARM_ZERO, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("PowerMgrMock016_Again"));
572     EXPECT_TRUE(pms->GetState() == PowerState::AWAKE);
573 
574     POWER_HILOGI(LABEL_TEST, "PowerMgrMock016 function end!");
575     GTEST_LOG_(INFO) << "PowerMgrMock016: end.";
576 }
577 
578 /**
579  * @tc.name: PowerMgrMock017
580  * @tc.desc: test SetState will block transit from AWAKE to SLEEP/HIBERNATE
581  * @tc.type: FUNC
582  * @tc.require: issueI9AMZT
583  */
584 HWTEST_F(PowerMgrSTMockTest, PowerMgrMock017, TestSize.Level2)
585 {
586     GTEST_LOG_(INFO) << "PowerMgrMock017: start.";
587     POWER_HILOGI(LABEL_TEST, "PowerMgrMock017 function start!");
588 
589     vector<PowerState> sleepStates { PowerState::SLEEP, PowerState::HIBERNATE };
590 
591     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
592     ASSERT_TRUE(pms != nullptr);
593     std::shared_ptr<PowerStateMachine> stateMachine = pms->GetPowerStateMachine();
594     ASSERT_TRUE(stateMachine != nullptr);
595 
596     // Analog display return DISPLAY_ON
597     EXPECT_CALL(*g_stateAction, GetDisplayState())
598     .Times(::testing::AtLeast(1))
599     .WillRepeatedly(::testing::Return(DisplayState::DISPLAY_ON));
600 
601     // Set the power state to AWAKE
602     auto ret = stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_INIT);
603     EXPECT_TRUE(stateMachine->GetState() == PowerState::AWAKE);
604 #ifndef POWER_MANAGER_POWER_ENABLE_S4
605     for (auto targetState : sleepStates) {
606         // Set the power state to target state
607         auto ret = stateMachine->SetState(targetState, StateChangeReason::STATE_CHANGE_REASON_SYSTEM);
608         // SetState will block transit from AWAKE to these states, expect SetState fail
609         EXPECT_FALSE(ret);
610         EXPECT_TRUE(stateMachine->GetState() == PowerState::AWAKE);
611     }
612 #endif
613     POWER_HILOGI(LABEL_TEST, "PowerMgrMock017 function end!");
614     GTEST_LOG_(INFO) << "PowerMgrMock017: end.";
615 }
616 } // namespace
617