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