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 "power_coordination_lock_test.h"
17 
18 #include <common_event_data.h>
19 #include <common_event_manager.h>
20 #include <common_event_publish_info.h>
21 #include <common_event_subscriber.h>
22 #include <common_event_support.h>
23 
24 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
25 #include "input_manager.h"
26 #include "pointer_event.h"
27 #endif
28 #include "power_log.h"
29 #include "power_mgr_client.h"
30 #include "power_mgr_service.h"
31 #include "power_state_callback_stub.h"
32 
33 using namespace OHOS;
34 using namespace OHOS::EventFwk;
35 using namespace OHOS::PowerMgr;
36 using namespace std;
37 using namespace testing::ext;
38 
39 namespace {
40 constexpr uint32_t SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS = 10000;
41 constexpr int32_t US_PER_MS = 1000;
42 constexpr uint32_t AUTO_SLEEP_DELAY_MS = 5000;
43 constexpr uint32_t WAIT_AUTO_SUSPEND_SLEEP_TIME_MS = AUTO_SLEEP_DELAY_MS + 2000;
44 constexpr int32_t WAIT_EVENT_TIME_MS = 400;
45 constexpr int32_t RETRY_WAIT_TIME_MS = 100;
46 constexpr int32_t WAIT_STATE_TIME_MS = 500;
47 constexpr int32_t OVER_TIME_SCREEN_OFF_TIME_MS = 2000;
48 constexpr int32_t OVER_TIME_SCREEN_OFF_TIME_TEST_MS = 2000 + 2000;
49 constexpr int32_t WAIT_SUSPEND_TIME_MS = 2000;
50 constexpr int32_t COLLABORATION_REMOTE_DEVICE_ID = 0xAAAAAAFF;
51 bool g_screenOnEvent = false;
52 bool g_screenOffEvent = false;
53 bool g_awakeCallback = false;
54 bool g_inactiveCallback = false;
55 PowerMode g_modeBeforeTest = PowerMode::NORMAL_MODE;
56 
57 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
CreateKeyEvent()58 std::shared_ptr<KeyEvent> CreateKeyEvent()
59 {
60     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
61     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
62     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
63     keyEvent->SetDeviceId(COLLABORATION_REMOTE_DEVICE_ID);
64     return keyEvent;
65 }
66 
CreatePointerEvent()67 std::shared_ptr<PointerEvent> CreatePointerEvent()
68 {
69     constexpr const int32_t ARBITRARY_NON_MAGIC_NUMBER_SIX = 6;
70     constexpr const int32_t ARBITRARY_NON_MAGIC_NUMBER_EIGHT = 8;
71     constexpr const int32_t ARBITRARY_NON_MAGIC_NUMBER_TEN = 10;
72     auto pointerEvent = PointerEvent::Create();
73 
74     PointerEvent::PointerItem item;
75     item.SetPointerId(0);
76     item.SetDisplayX(ARBITRARY_NON_MAGIC_NUMBER_SIX);
77     item.SetDisplayY(ARBITRARY_NON_MAGIC_NUMBER_SIX);
78     item.SetPressure(ARBITRARY_NON_MAGIC_NUMBER_SIX);
79     pointerEvent->AddPointerItem(item);
80 
81     item.SetPointerId(1);
82     item.SetDisplayX(ARBITRARY_NON_MAGIC_NUMBER_SIX);
83     item.SetDisplayY(ARBITRARY_NON_MAGIC_NUMBER_TEN);
84     item.SetPressure(ARBITRARY_NON_MAGIC_NUMBER_EIGHT);
85     pointerEvent->AddPointerItem(item);
86 
87     pointerEvent->SetDeviceId(COLLABORATION_REMOTE_DEVICE_ID);
88     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
89     pointerEvent->SetPointerId(1);
90     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
91     return pointerEvent;
92 }
93 #endif
94 
ResetTriggeredFlag()95 void ResetTriggeredFlag()
96 {
97     g_screenOnEvent = false;
98     g_screenOffEvent = false;
99     g_awakeCallback = false;
100     g_inactiveCallback = false;
101 }
102 
MatchCommonEventTriggered(std::string event)103 void MatchCommonEventTriggered(std::string event)
104 {
105     if (event == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
106         g_screenOnEvent = true;
107     } else if (event == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
108         g_screenOffEvent = true;
109     }
110 }
111 
MatchPowerStateTriggered(PowerState state)112 void MatchPowerStateTriggered(PowerState state)
113 {
114     switch (state) {
115         case PowerState::AWAKE:
116             g_awakeCallback =  true;
117             break;
118         case PowerState::INACTIVE:
119             g_inactiveCallback = true;
120             break;
121         default:
122             break;
123     }
124 }
125 
126 class PowerStateCommonEventSubscriber : public CommonEventSubscriber {
127 public:
PowerStateCommonEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo)128     explicit PowerStateCommonEventSubscriber(const CommonEventSubscribeInfo& subscribeInfo)
129         : CommonEventSubscriber(subscribeInfo) {}
~PowerStateCommonEventSubscriber()130     virtual ~PowerStateCommonEventSubscriber() {}
OnReceiveEvent(const CommonEventData & data)131     void OnReceiveEvent(const CommonEventData &data) override
132     {
133         std::string action = data.GetWant().GetAction();
134         POWER_HILOGI(LABEL_TEST, "On receive common event=%{public}s", action.c_str());
135         MatchCommonEventTriggered(action);
136     }
137     static shared_ptr<PowerStateCommonEventSubscriber> RegisterEvent();
138 };
139 
RegisterEvent()140 shared_ptr<PowerStateCommonEventSubscriber> PowerStateCommonEventSubscriber::RegisterEvent()
141 {
142     POWER_HILOGI(LABEL_TEST, "Regist subscriber screen off event");
143     int32_t retryTimes = 2;
144     bool succeed = false;
145     MatchingSkills matchingSkills;
146     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
147     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
148     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
149     auto subscriberPtr = std::make_shared<PowerStateCommonEventSubscriber>(subscribeInfo);
150     for (int32_t tryTimes = 0; tryTimes < retryTimes; tryTimes++) {
151         succeed = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
152         if (succeed) {
153             break;
154         }
155         usleep(RETRY_WAIT_TIME_MS * US_PER_MS);
156     }
157     if (!succeed) {
158         POWER_HILOGI(LABEL_TEST, "Failed to register subscriber");
159         return nullptr;
160     }
161     return subscriberPtr;
162 }
163 
164 class PowerStateTestCallback : public PowerStateCallbackStub {
165 public:
166     PowerStateTestCallback() = default;
167     virtual ~PowerStateTestCallback() = default;
OnPowerStateChanged(PowerState state)168     void OnPowerStateChanged(PowerState state) override
169     {
170         POWER_HILOGI(LABEL_TEST, "On power state=%{public}d changed callback", state);
171         MatchPowerStateTriggered(state);
172     }
173 };
174 }
175 
SetUpTestCase(void)176 void PowerCoordinationLockTest::SetUpTestCase(void)
177 {
178     auto& powerMgrClient = PowerMgrClient::GetInstance();
179     g_modeBeforeTest = powerMgrClient.GetDeviceMode();
180     EXPECT_EQ(powerMgrClient.SetDeviceMode(PowerMode::NORMAL_MODE), PowerErrors::ERR_OK);
181 }
182 
TearDownTestCase(void)183 void PowerCoordinationLockTest::TearDownTestCase(void)
184 {
185     auto& powerMgrClient = PowerMgrClient::GetInstance();
186     powerMgrClient.SetDeviceMode(g_modeBeforeTest);
187 }
188 
TearDown(void)189 void PowerCoordinationLockTest::TearDown(void)
190 {
191     ResetTriggeredFlag();
192     sleep(1); //wait for async wakeup task to be done
193 }
194 
195 namespace {
196 /**
197  * @tc.name: PowerCoordinationLockTest_001
198  * @tc.desc: test coordination runninglock func when power state is awake
199  * @tc.type: FUNC
200  * @tc.require: issueI8JBT4
201  */
202 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_001, TestSize.Level0)
203 {
204     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_001 start");
205     auto& powerMgrClient = PowerMgrClient::GetInstance();
206     EXPECT_TRUE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_COORDINATION));
207     auto runninglock =
208         powerMgrClient.CreateRunningLock("CoordinationRunninglock001", RunningLockType::RUNNINGLOCK_COORDINATION);
209     ASSERT_NE(runninglock, nullptr);
210     EXPECT_FALSE(runninglock->IsUsed());
211 
212     powerMgrClient.WakeupDevice();
213     EXPECT_TRUE(powerMgrClient.IsScreenOn());
214     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
215 
216     runninglock->Lock();
217     EXPECT_TRUE(runninglock->IsUsed());
218     runninglock->UnLock();
219     EXPECT_FALSE(runninglock->IsUsed());
220 
221     int32_t timeoutMs = 500;
222     runninglock->Lock(timeoutMs);
223     EXPECT_TRUE(runninglock->IsUsed());
224     usleep(timeoutMs * US_PER_MS);
225     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
226     EXPECT_FALSE(runninglock->IsUsed());
227     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_001 end");
228 }
229 
230 /**
231  * @tc.name: PowerCoordinationLockTest_002
232  * @tc.desc: test coordination runninglock proxy func when power state is awake
233  * @tc.type: FUNC
234  * @tc.require: issueI8JBT4
235  */
236 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_002, TestSize.Level0)
237 {
238     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_002 start");
239     auto& powerMgrClient = PowerMgrClient::GetInstance();
240     auto runninglock =
241         powerMgrClient.CreateRunningLock("CoordinationRunninglock002", RunningLockType::RUNNINGLOCK_COORDINATION);
242     ASSERT_NE(runninglock, nullptr);
243 
244     powerMgrClient.WakeupDevice();
245     EXPECT_TRUE(powerMgrClient.IsScreenOn());
246     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
247 
248     pid_t curUid = getuid();
249     pid_t curPid = getpid();
250 
251     runninglock->Lock();
252     EXPECT_TRUE(runninglock->IsUsed());
253     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
254     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
255 
256     runninglock->Lock();
257     EXPECT_TRUE(runninglock->IsUsed());
258     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
259     EXPECT_FALSE(runninglock->IsUsed());
260     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
261     EXPECT_TRUE(runninglock->IsUsed());
262     runninglock->UnLock();
263     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_002 end");
264 }
265 
266 
267 /**
268  * @tc.name: PowerCoordinationLockTest_003
269  * @tc.desc: test coordination runninglock proxy func when power state is awake
270  * @tc.type: FUNC
271  * @tc.require: issueI8JBT4
272  */
273 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_003, TestSize.Level0)
274 {
275     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_003 start");
276     auto& powerMgrClient = PowerMgrClient::GetInstance();
277     auto runninglock =
278         powerMgrClient.CreateRunningLock("CoordinationRunninglock003", RunningLockType::RUNNINGLOCK_COORDINATION);
279     ASSERT_NE(runninglock, nullptr);
280 
281     powerMgrClient.WakeupDevice();
282     EXPECT_TRUE(powerMgrClient.IsScreenOn());
283     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
284 
285     pid_t curUid = getuid();
286     pid_t curPid = getpid();
287 
288     int32_t timeoutMs = 500;
289     runninglock->Lock(timeoutMs);
290     EXPECT_TRUE(runninglock->IsUsed());
291 
292     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
293     EXPECT_FALSE(runninglock->IsUsed());
294     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
295     EXPECT_TRUE(runninglock->IsUsed());
296     usleep(timeoutMs * US_PER_MS);
297     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
298     EXPECT_FALSE(runninglock->IsUsed());
299     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_003 end");
300 }
301 
302 /**
303  * @tc.name: PowerCoordinationLockTest_004
304  * @tc.desc: test coordination runninglock proxy func when power state is awake
305  * @tc.type: FUNC
306  * @tc.require: issueI8JBT4
307  */
308 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_004, TestSize.Level0)
309 {
310     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_004 start");
311     auto& powerMgrClient = PowerMgrClient::GetInstance();
312 
313     powerMgrClient.WakeupDevice();
314     EXPECT_TRUE(powerMgrClient.IsScreenOn());
315     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
316 
317     pid_t curUid = getuid();
318     pid_t curPid = getpid();
319 
320     auto runninglock =
321         powerMgrClient.CreateRunningLock("CoordinationRunninglock004", RunningLockType::RUNNINGLOCK_COORDINATION);
322     ASSERT_NE(runninglock, nullptr);
323     runninglock->Lock();
324     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
325     EXPECT_FALSE(runninglock->IsUsed());
326     EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
327     EXPECT_TRUE(runninglock->IsUsed());
328     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_004 end");
329 }
330 
331 /**
332  * @tc.name: PowerCoordinationLockTest_005
333  * @tc.desc: test coordination runninglock proxy func when power state is sleep
334  * @tc.type: FUNC
335  * @tc.require: issueI8JBT4
336  */
337 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_005, TestSize.Level0)
338 {
339     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_005 start");
340     auto& powerMgrClient = PowerMgrClient::GetInstance();
341     auto runninglock =
342         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_005", RunningLockType::RUNNINGLOCK_COORDINATION);
343     ASSERT_NE(runninglock, nullptr);
344 
345     powerMgrClient.SuspendDevice();
346     usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
347 
348     EXPECT_FALSE(powerMgrClient.IsScreenOn());
349     EXPECT_EQ(powerMgrClient.GetState(), PowerState::SLEEP);
350 
351     runninglock->Lock();
352     EXPECT_FALSE(runninglock->IsUsed());
353     runninglock->UnLock();
354     EXPECT_FALSE(runninglock->IsUsed());
355     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_005 end");
356 }
357 
358 /**
359  * @tc.name: PowerCoordinationLockTest_006
360  * @tc.desc: test coordination runninglock is locked, not notify event and callback when inactive(suspend)
361  * @tc.type: FUNC
362  * @tc.require: issueI8JBT4
363  */
364 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_006, TestSize.Level0)
365 {
366     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_006 start");
367     auto& powerMgrClient = PowerMgrClient::GetInstance();
368     auto runninglock =
369         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_006", RunningLockType::RUNNINGLOCK_COORDINATION);
370     ASSERT_NE(runninglock, nullptr);
371     EXPECT_FALSE(runninglock->IsUsed());
372     powerMgrClient.WakeupDevice();
373     EXPECT_TRUE(powerMgrClient.IsScreenOn());
374     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
375 
376     runninglock->Lock();
377     EXPECT_TRUE(runninglock->IsUsed());
378 
379     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
380     EXPECT_FALSE(subscriber == nullptr);
381     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
382     powerMgrClient.RegisterPowerStateCallback(stateCallback);
383 
384     powerMgrClient.LockScreenAfterTimingOut(false, false, false);
385     powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
386     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
387 
388     EXPECT_FALSE(g_screenOffEvent);
389     EXPECT_FALSE(g_inactiveCallback);
390     EXPECT_FALSE(powerMgrClient.IsScreenOn());
391     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
392 
393     usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
394     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
395 
396     runninglock->UnLock();
397     EXPECT_FALSE(runninglock->IsUsed());
398     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
399 
400     EXPECT_FALSE(g_screenOffEvent);
401     EXPECT_FALSE(g_inactiveCallback);
402     EXPECT_TRUE(powerMgrClient.IsScreenOn());
403     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
404 
405     powerMgrClient.LockScreenAfterTimingOut(true, false, true);
406     powerMgrClient.SuspendDevice();
407     usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
408 
409     EXPECT_TRUE(g_screenOffEvent);
410     EXPECT_TRUE(g_inactiveCallback);
411     EXPECT_FALSE(powerMgrClient.IsScreenOn());
412     EXPECT_EQ(powerMgrClient.GetState(), PowerState::SLEEP);
413 
414     CommonEventManager::UnSubscribeCommonEvent(subscriber);
415     powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
416     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_006 end");
417 }
418 
419 /**
420  * @tc.name: PowerCoordinationLockTest_007
421  * @tc.desc: test coordination runninglock is locked, not notify event and callback when inactive(over time)
422  * @tc.type: FUNC
423  * @tc.require: issueI8JBT4
424  */
425 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_007, TestSize.Level0)
426 {
427     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_007 start");
428     auto& powerMgrClient = PowerMgrClient::GetInstance();
429     powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
430     auto runninglock =
431         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_007", RunningLockType::RUNNINGLOCK_COORDINATION);
432     ASSERT_NE(runninglock, nullptr);
433     EXPECT_FALSE(runninglock->IsUsed());
434     powerMgrClient.WakeupDevice();
435     EXPECT_TRUE(powerMgrClient.IsScreenOn());
436     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
437 
438     runninglock->Lock();
439     EXPECT_TRUE(runninglock->IsUsed());
440 
441     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
442     EXPECT_FALSE(subscriber == nullptr);
443     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
444     powerMgrClient.RegisterPowerStateCallback(stateCallback);
445     powerMgrClient.LockScreenAfterTimingOut(false, false, false);
446 
447     usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
448 
449     EXPECT_FALSE(g_screenOffEvent);
450     EXPECT_FALSE(g_inactiveCallback);
451     EXPECT_FALSE(powerMgrClient.IsScreenOn());
452     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
453 
454     usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
455     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
456 
457     runninglock->UnLock();
458     EXPECT_FALSE(runninglock->IsUsed());
459     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
460 
461     EXPECT_FALSE(g_screenOffEvent);
462     EXPECT_FALSE(g_inactiveCallback);
463     EXPECT_TRUE(powerMgrClient.IsScreenOn());
464     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
465 
466     powerMgrClient.LockScreenAfterTimingOut(true, false, true);
467     CommonEventManager::UnSubscribeCommonEvent(subscriber);
468     powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
469     powerMgrClient.RestoreScreenOffTime();
470     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_007 end");
471 }
472 
473 /**
474  * @tc.name: PowerCoordinationLockTest_008
475  * @tc.desc: test coordination runninglock function, when the power state transitions
476  * @tc.type: FUNC
477  * @tc.require: issueI8JBT4
478  */
479 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_008, TestSize.Level0)
480 {
481     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_008 start");
482     auto& powerMgrClient = PowerMgrClient::GetInstance();
483     auto runninglock =
484         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_008", RunningLockType::RUNNINGLOCK_COORDINATION);
485     ASSERT_NE(runninglock, nullptr);
486     EXPECT_FALSE(runninglock->IsUsed());
487     powerMgrClient.WakeupDevice();
488     EXPECT_TRUE(powerMgrClient.IsScreenOn());
489     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
490 
491     runninglock->Lock();
492     EXPECT_TRUE(runninglock->IsUsed());
493 
494     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
495     EXPECT_FALSE(subscriber == nullptr);
496     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
497     powerMgrClient.RegisterPowerStateCallback(stateCallback);
498 
499     powerMgrClient.LockScreenAfterTimingOut(false, false, false);
500     powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
501     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
502 
503     EXPECT_FALSE(g_screenOffEvent);
504     EXPECT_FALSE(g_inactiveCallback);
505     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
506 
507     ResetTriggeredFlag();
508     powerMgrClient.WakeupDevice();
509     EXPECT_TRUE(powerMgrClient.IsScreenOn());
510     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
511     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
512     EXPECT_TRUE(g_screenOnEvent);
513     EXPECT_TRUE(g_awakeCallback);
514 
515     runninglock->UnLock();
516     EXPECT_FALSE(runninglock->IsUsed());
517 
518     EXPECT_FALSE(g_screenOffEvent);
519     EXPECT_FALSE(g_inactiveCallback);
520 
521     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
522 
523     powerMgrClient.LockScreenAfterTimingOut(true, false, true);
524     CommonEventManager::UnSubscribeCommonEvent(subscriber);
525     powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
526     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_008 end");
527 }
528 
529 /**
530  * @tc.name: PowerCoordinationLockTest_009
531  * @tc.desc: test coordination runninglock function, when the power state transitions
532  * @tc.type: FUNC
533  * @tc.require: issueI8JBT4
534  */
535 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_009, TestSize.Level0)
536 {
537     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_009 start");
538     auto& powerMgrClient = PowerMgrClient::GetInstance();
539     auto runninglockOne =
540         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_1", RunningLockType::RUNNINGLOCK_COORDINATION);
541     auto runninglockTwo =
542         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_2", RunningLockType::RUNNINGLOCK_COORDINATION);
543     ASSERT_NE(runninglockOne, nullptr);
544     ASSERT_NE(runninglockTwo, nullptr);
545     EXPECT_FALSE(runninglockOne->IsUsed());
546     EXPECT_FALSE(runninglockTwo->IsUsed());
547 
548     powerMgrClient.WakeupDevice();
549 
550     runninglockOne->Lock();
551     runninglockTwo->Lock();
552     EXPECT_TRUE(runninglockOne->IsUsed());
553     EXPECT_TRUE(runninglockTwo->IsUsed());
554 
555     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
556     EXPECT_FALSE(subscriber == nullptr);
557     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
558     powerMgrClient.RegisterPowerStateCallback(stateCallback);
559 
560     runninglockOne->UnLock();
561     EXPECT_FALSE(runninglockOne->IsUsed());
562 
563     powerMgrClient.LockScreenAfterTimingOut(false, false, false);
564     powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
565     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
566 
567     EXPECT_FALSE(g_screenOffEvent);
568     EXPECT_FALSE(g_inactiveCallback);
569     EXPECT_FALSE(powerMgrClient.IsScreenOn());
570     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
571 
572     runninglockTwo->UnLock();
573     EXPECT_FALSE(runninglockTwo->IsUsed());
574     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
575 
576     EXPECT_FALSE(g_screenOffEvent);
577     EXPECT_FALSE(g_inactiveCallback);
578     EXPECT_TRUE(powerMgrClient.IsScreenOn());
579     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
580 
581     powerMgrClient.LockScreenAfterTimingOut(true, false, true);
582     CommonEventManager::UnSubscribeCommonEvent(subscriber);
583     powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
584     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_009 end");
585 }
586 
587 /**
588  * @tc.name: PowerCoordinationLockTest_010
589  * @tc.desc: test coordination runninglock lock, screen keep off when touching the screen
590  * @tc.type: FUNC
591  * @tc.require: issueI8JBT4
592  */
593 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_010, TestSize.Level0)
594 {
595     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_010 start");
596     auto& powerMgrClient = PowerMgrClient::GetInstance();
597     auto runninglock =
598         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_010", RunningLockType::RUNNINGLOCK_COORDINATION);
599     ASSERT_NE(runninglock, nullptr);
600     EXPECT_FALSE(runninglock->IsUsed());
601     powerMgrClient.WakeupDevice();
602     EXPECT_TRUE(powerMgrClient.IsScreenOn());
603     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
604 
605     runninglock->Lock();
606     EXPECT_TRUE(runninglock->IsUsed());
607 
608     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
609     EXPECT_FALSE(subscriber == nullptr);
610     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
611     powerMgrClient.RegisterPowerStateCallback(stateCallback);
612 
613     powerMgrClient.LockScreenAfterTimingOut(false, false, false);
614     powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
615     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
616 
617     EXPECT_FALSE(g_screenOffEvent);
618     EXPECT_FALSE(g_inactiveCallback);
619     EXPECT_FALSE(powerMgrClient.IsScreenOn());
620     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
621 
622     powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_TOUCH);
623     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
624 
625     runninglock->UnLock();
626     EXPECT_FALSE(runninglock->IsUsed());
627     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
628 
629     EXPECT_FALSE(g_screenOffEvent);
630     EXPECT_FALSE(g_inactiveCallback);
631     EXPECT_TRUE(powerMgrClient.IsScreenOn());
632     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
633 
634     powerMgrClient.LockScreenAfterTimingOut(true, false, true);
635     CommonEventManager::UnSubscribeCommonEvent(subscriber);
636     powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
637     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_010 end");
638 }
639 
640 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
641 /**
642  * @tc.name: PowerCoordinationLockTest_011
643  * @tc.desc: test entering DIM state while coordination
644  * @tc.type: FUNC
645  * @tc.require: issueI8JBT4
646  */
647 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_011, TestSize.Level0)
648 {
649     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_011 start");
650     auto& powerMgrClient = PowerMgrClient::GetInstance();
651     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
652     EXPECT_FALSE(subscriber == nullptr);
653     auto runninglock =
654         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_011", RunningLockType::RUNNINGLOCK_COORDINATION);
655     ASSERT_NE(runninglock, nullptr);
656     EXPECT_FALSE(runninglock->IsUsed());
657     powerMgrClient.WakeupDevice();
658     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
659     powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
660     runninglock->Lock();
661     EXPECT_TRUE(runninglock->IsUsed());
662 
663     auto inputManager = MMI::InputManager::GetInstance();
664 
665     std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
666     inputManager->SimulateInputEvent(keyEvent);
667     usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
668     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
669     usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
670     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
671     // already in DIM, not resetting 10s timer
672     inputManager->SimulateInputEvent(keyEvent);
673     usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
674     // already in DIM, not resetting 10s timer
675     inputManager->SimulateInputEvent(keyEvent);
676     usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
677     // screen should be off now
678     EXPECT_FALSE(powerMgrClient.IsScreenOn());
679 
680     powerMgrClient.WakeupDevice();
681     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
682     inputManager->SimulateInputEvent(keyEvent);
683     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
684     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
685     ResetTriggeredFlag();
686     powerMgrClient.WakeupDevice();
687     // DIM to AWAKE, no event
688     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
689     EXPECT_FALSE(g_screenOnEvent);
690     // AWAKE to AWAKE, no event
691     powerMgrClient.WakeupDevice();
692     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
693     EXPECT_FALSE(g_screenOnEvent);
694 
695     // test pointer event
696     std::shared_ptr<PointerEvent> pointerEvent = CreatePointerEvent();
697     powerMgrClient.WakeupDevice();
698     inputManager->SimulateInputEvent(pointerEvent);
699     usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
700     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
701     usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
702     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
703     usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
704     EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
705 
706     powerMgrClient.RestoreScreenOffTime();
707     CommonEventManager::UnSubscribeCommonEvent(subscriber);
708 }
709 /**
710  * @tc.name: PowerCoordinationLockTest_012
711  * @tc.desc: test entering DIM state while coordination with SetForceTimingOut set to true at the same time
712  * @tc.type: FUNC
713  * @tc.require: issueI8JBT4
714  */
715 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_012, TestSize.Level0)
716 {
717     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_012 start");
718     auto& powerMgrClient = PowerMgrClient::GetInstance();
719     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
720     EXPECT_FALSE(subscriber == nullptr);
721     auto runninglock =
722         powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_012", RunningLockType::RUNNINGLOCK_COORDINATION);
723     ASSERT_NE(runninglock, nullptr);
724     EXPECT_FALSE(runninglock->IsUsed());
725     powerMgrClient.WakeupDevice();
726     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
727     powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
728     runninglock->Lock();
729     EXPECT_TRUE(runninglock->IsUsed());
730 
731     auto runninglockScreen = powerMgrClient.CreateRunningLock(
732         "PowerCoordinationLockTest_012_ScreenOn", RunningLockType::RUNNINGLOCK_SCREEN);
733     runninglockScreen->Lock();
734     EXPECT_TRUE(runninglockScreen->IsUsed());
735     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
736 
737     auto inputManager = MMI::InputManager::GetInstance();
738 
739     std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
__anon83de98850302null740     FFRTUtils::SubmitTask([&inputManager, &keyEvent] {
741         inputManager->SimulateInputEvent(keyEvent);
742     });
__anon83de98850402null743     FFRTTask callingInterface = [&powerMgrClient] {
744         usleep(50000);
745         powerMgrClient.SetForceTimingOut(true);
746     };
747     FFRTUtils::SubmitTask(callingInterface);
748     ffrt::wait();
749     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
750     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
751     usleep((OVER_TIME_SCREEN_OFF_TIME_MS + WAIT_SUSPEND_TIME_MS) * US_PER_MS);
752     EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
753     usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
754     EXPECT_FALSE(powerMgrClient.IsScreenOn());
755     powerMgrClient.SetForceTimingOut(false);
756 }
757 #endif
758 /**
759  * @tc.name: PowerCoordinationLockTest_013
760  * @tc.desc: test publishing screen off event
761  * @tc.type: FUNC
762  */
763 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_013, TestSize.Level0)
764 {
765     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_013 start");
766     auto& powerMgrClient = PowerMgrClient::GetInstance();
767     shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
768     const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
769     powerMgrClient.RegisterPowerStateCallback(stateCallback);
770     EXPECT_FALSE(subscriber == nullptr);
771     powerMgrClient.WakeupDevice();
772     EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
773     powerMgrClient.SuspendDevice();
774     usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
775     EXPECT_TRUE(g_screenOffEvent);
776     EXPECT_TRUE(g_inactiveCallback);
777     for (int i = 0; i < 4; ++i) {
778         powerMgrClient.WakeupDevice();
779         EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
780         ResetTriggeredFlag();
781         powerMgrClient.LockScreenAfterTimingOut(i % 2, i / 2, 0);
782         powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
783         usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
784         if (i % 2) {
785             EXPECT_TRUE(g_screenOffEvent);
786             EXPECT_TRUE(g_inactiveCallback);
787         } else {
788             EXPECT_FALSE(g_screenOffEvent);
789             EXPECT_FALSE(g_inactiveCallback);
790         }
791     }
792     for (int i = 0; i < 4; ++i) {
793         powerMgrClient.WakeupDevice();
794         EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
795         ResetTriggeredFlag();
796         powerMgrClient.LockScreenAfterTimingOut(i % 2, i / 2, 1);
797         powerMgrClient.SuspendDevice();
798         usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
799         EXPECT_TRUE(g_screenOffEvent);
800         EXPECT_TRUE(g_inactiveCallback);
801     }
802     powerMgrClient.LockScreenAfterTimingOut(1, 0, 1);
803     POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_013 end");
804 }
805 }
806