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.Level0)
204 {
205 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_001 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 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.Level0)
238 {
239 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_002 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 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.Level0)
275 {
276 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_003 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 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.Level0)
310 {
311 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_004 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 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.Level0)
339 {
340 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_005 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 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.Level0)
366 {
367 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_006 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 EXPECT_TRUE(powerMgrClient.IsScreenOn());
404 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
405
406 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
407 powerMgrClient.SuspendDevice();
408 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
409
410 EXPECT_TRUE(g_screenOffEvent);
411 EXPECT_TRUE(g_inactiveCallback);
412 EXPECT_FALSE(powerMgrClient.IsScreenOn());
413 EXPECT_EQ(powerMgrClient.GetState(), PowerState::SLEEP);
414
415 CommonEventManager::UnSubscribeCommonEvent(subscriber);
416 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
417 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_006 end");
418 }
419
420 /**
421 * @tc.name: PowerCoordinationLockTest_007
422 * @tc.desc: test coordination runninglock is locked, not notify event and callback when inactive(over time)
423 * @tc.type: FUNC
424 * @tc.require: issueI8JBT4
425 */
426 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_007, TestSize.Level0)
427 {
428 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_007 start");
429 auto& powerMgrClient = PowerMgrClient::GetInstance();
430 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
431 auto runninglock =
432 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_007", RunningLockType::RUNNINGLOCK_COORDINATION);
433 ASSERT_NE(runninglock, nullptr);
434 EXPECT_FALSE(runninglock->IsUsed());
435 powerMgrClient.WakeupDevice();
436 EXPECT_TRUE(powerMgrClient.IsScreenOn());
437 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
438
439 runninglock->Lock();
440 EXPECT_TRUE(runninglock->IsUsed());
441
442 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
443 EXPECT_FALSE(subscriber == nullptr);
444 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
445 powerMgrClient.RegisterPowerStateCallback(stateCallback);
446 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
447
448 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
449
450 EXPECT_FALSE(g_screenOffEvent);
451 EXPECT_FALSE(g_inactiveCallback);
452 EXPECT_FALSE(powerMgrClient.IsScreenOn());
453 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
454
455 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
456 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
457
458 runninglock->UnLock();
459 EXPECT_FALSE(runninglock->IsUsed());
460 usleep((WAIT_EVENT_TIME_MS + WAIT_WAKEUP_TIME_MS) * US_PER_MS);
461
462 EXPECT_FALSE(g_screenOffEvent);
463 EXPECT_FALSE(g_inactiveCallback);
464 EXPECT_TRUE(powerMgrClient.IsScreenOn());
465 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
466
467 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
468 CommonEventManager::UnSubscribeCommonEvent(subscriber);
469 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
470 powerMgrClient.RestoreScreenOffTime();
471 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_007 end");
472 }
473
474 /**
475 * @tc.name: PowerCoordinationLockTest_008
476 * @tc.desc: test coordination runninglock function, when the power state transitions
477 * @tc.type: FUNC
478 * @tc.require: issueI8JBT4
479 */
480 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_008, TestSize.Level0)
481 {
482 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_008 start");
483 auto& powerMgrClient = PowerMgrClient::GetInstance();
484 auto runninglock =
485 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_008", RunningLockType::RUNNINGLOCK_COORDINATION);
486 ASSERT_NE(runninglock, nullptr);
487 EXPECT_FALSE(runninglock->IsUsed());
488 powerMgrClient.WakeupDevice();
489 EXPECT_TRUE(powerMgrClient.IsScreenOn());
490 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
491
492 runninglock->Lock();
493 EXPECT_TRUE(runninglock->IsUsed());
494
495 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
496 EXPECT_FALSE(subscriber == nullptr);
497 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
498 powerMgrClient.RegisterPowerStateCallback(stateCallback);
499
500 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
501 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
502 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
503
504 EXPECT_FALSE(g_screenOffEvent);
505 EXPECT_FALSE(g_inactiveCallback);
506 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
507
508 ResetTriggeredFlag();
509 powerMgrClient.WakeupDevice();
510 EXPECT_TRUE(powerMgrClient.IsScreenOn());
511 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
512 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
513 EXPECT_TRUE(g_screenOnEvent);
514 EXPECT_TRUE(g_awakeCallback);
515
516 runninglock->UnLock();
517 EXPECT_FALSE(runninglock->IsUsed());
518
519 EXPECT_FALSE(g_screenOffEvent);
520 EXPECT_FALSE(g_inactiveCallback);
521
522 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
523
524 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
525 CommonEventManager::UnSubscribeCommonEvent(subscriber);
526 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
527 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_008 end");
528 }
529
530 /**
531 * @tc.name: PowerCoordinationLockTest_009
532 * @tc.desc: test coordination runninglock function, when the power state transitions
533 * @tc.type: FUNC
534 * @tc.require: issueI8JBT4
535 */
536 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_009, TestSize.Level0)
537 {
538 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_009 start");
539 auto& powerMgrClient = PowerMgrClient::GetInstance();
540 auto runninglockOne =
541 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_1", RunningLockType::RUNNINGLOCK_COORDINATION);
542 auto runninglockTwo =
543 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_2", RunningLockType::RUNNINGLOCK_COORDINATION);
544 ASSERT_NE(runninglockOne, nullptr);
545 ASSERT_NE(runninglockTwo, nullptr);
546 EXPECT_FALSE(runninglockOne->IsUsed());
547 EXPECT_FALSE(runninglockTwo->IsUsed());
548
549 powerMgrClient.WakeupDevice();
550
551 runninglockOne->Lock();
552 runninglockTwo->Lock();
553 EXPECT_TRUE(runninglockOne->IsUsed());
554 EXPECT_TRUE(runninglockTwo->IsUsed());
555
556 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
557 EXPECT_FALSE(subscriber == nullptr);
558 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
559 powerMgrClient.RegisterPowerStateCallback(stateCallback);
560
561 runninglockOne->UnLock();
562 EXPECT_FALSE(runninglockOne->IsUsed());
563
564 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
565 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
566 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
567
568 EXPECT_FALSE(g_screenOffEvent);
569 EXPECT_FALSE(g_inactiveCallback);
570 EXPECT_FALSE(powerMgrClient.IsScreenOn());
571 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
572
573 runninglockTwo->UnLock();
574 EXPECT_FALSE(runninglockTwo->IsUsed());
575 usleep((WAIT_EVENT_TIME_MS + WAIT_WAKEUP_TIME_MS) * US_PER_MS);
576
577 EXPECT_FALSE(g_screenOffEvent);
578 EXPECT_FALSE(g_inactiveCallback);
579 EXPECT_TRUE(powerMgrClient.IsScreenOn());
580 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
581
582 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
583 CommonEventManager::UnSubscribeCommonEvent(subscriber);
584 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
585 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_009 end");
586 }
587
588 /**
589 * @tc.name: PowerCoordinationLockTest_010
590 * @tc.desc: test coordination runninglock lock, screen keep off when touching the screen
591 * @tc.type: FUNC
592 * @tc.require: issueI8JBT4
593 */
594 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_010, TestSize.Level0)
595 {
596 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_010 start");
597 auto& powerMgrClient = PowerMgrClient::GetInstance();
598 auto runninglock =
599 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_010", RunningLockType::RUNNINGLOCK_COORDINATION);
600 ASSERT_NE(runninglock, nullptr);
601 EXPECT_FALSE(runninglock->IsUsed());
602 powerMgrClient.WakeupDevice();
603 EXPECT_TRUE(powerMgrClient.IsScreenOn());
604 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
605
606 runninglock->Lock();
607 EXPECT_TRUE(runninglock->IsUsed());
608
609 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
610 EXPECT_FALSE(subscriber == nullptr);
611 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
612 powerMgrClient.RegisterPowerStateCallback(stateCallback);
613
614 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
615 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
616 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
617
618 EXPECT_FALSE(g_screenOffEvent);
619 EXPECT_FALSE(g_inactiveCallback);
620 EXPECT_FALSE(powerMgrClient.IsScreenOn());
621 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
622
623 powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_TOUCH);
624 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
625
626 runninglock->UnLock();
627 EXPECT_FALSE(runninglock->IsUsed());
628 usleep((WAIT_EVENT_TIME_MS + WAIT_WAKEUP_TIME_MS) * US_PER_MS);
629
630 EXPECT_FALSE(g_screenOffEvent);
631 EXPECT_FALSE(g_inactiveCallback);
632 EXPECT_TRUE(powerMgrClient.IsScreenOn());
633 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
634
635 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
636 CommonEventManager::UnSubscribeCommonEvent(subscriber);
637 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
638 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_010 end");
639 }
640
641 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
642 /**
643 * @tc.name: PowerCoordinationLockTest_011
644 * @tc.desc: test entering DIM state while coordination
645 * @tc.type: FUNC
646 * @tc.require: issueI8JBT4
647 */
648 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_011, TestSize.Level0)
649 {
650 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_011 start");
651 auto& powerMgrClient = PowerMgrClient::GetInstance();
652 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
653 EXPECT_FALSE(subscriber == nullptr);
654 auto runninglock =
655 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_011", RunningLockType::RUNNINGLOCK_COORDINATION);
656 ASSERT_NE(runninglock, nullptr);
657 EXPECT_FALSE(runninglock->IsUsed());
658 powerMgrClient.WakeupDevice();
659 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
660 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
661 runninglock->Lock();
662 EXPECT_TRUE(runninglock->IsUsed());
663
664 auto inputManager = MMI::InputManager::GetInstance();
665
666 std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
667 inputManager->SimulateInputEvent(keyEvent);
668 usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
669 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
670 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
671 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
672 // already in DIM, not resetting 10s timer
673 inputManager->SimulateInputEvent(keyEvent);
674 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
675 // already in DIM, not resetting 10s timer
676 inputManager->SimulateInputEvent(keyEvent);
677 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
678 // screen should be off now
679 EXPECT_FALSE(powerMgrClient.IsScreenOn());
680
681 powerMgrClient.WakeupDevice();
682 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
683 inputManager->SimulateInputEvent(keyEvent);
684 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
685 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
686 ResetTriggeredFlag();
687 powerMgrClient.WakeupDevice();
688 // DIM to AWAKE, no event
689 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
690 EXPECT_FALSE(g_screenOnEvent);
691 // AWAKE to AWAKE, no event
692 powerMgrClient.WakeupDevice();
693 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
694 EXPECT_FALSE(g_screenOnEvent);
695
696 // test pointer event
697 std::shared_ptr<PointerEvent> pointerEvent = CreatePointerEvent();
698 powerMgrClient.WakeupDevice();
699 inputManager->SimulateInputEvent(pointerEvent);
700 usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
701 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
702 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
703 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
704 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
705 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
706
707 powerMgrClient.RestoreScreenOffTime();
708 CommonEventManager::UnSubscribeCommonEvent(subscriber);
709 }
710 /**
711 * @tc.name: PowerCoordinationLockTest_012
712 * @tc.desc: test entering DIM state while coordination with SetForceTimingOut set to true at the same time
713 * @tc.type: FUNC
714 * @tc.require: issueI8JBT4
715 */
716 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_012, TestSize.Level0)
717 {
718 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_012 start");
719 auto& powerMgrClient = PowerMgrClient::GetInstance();
720 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
721 EXPECT_FALSE(subscriber == nullptr);
722 auto runninglock =
723 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_012", RunningLockType::RUNNINGLOCK_COORDINATION);
724 ASSERT_NE(runninglock, nullptr);
725 EXPECT_FALSE(runninglock->IsUsed());
726 powerMgrClient.WakeupDevice();
727 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
728 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
729 runninglock->Lock();
730 EXPECT_TRUE(runninglock->IsUsed());
731
732 auto runninglockScreen = powerMgrClient.CreateRunningLock(
733 "PowerCoordinationLockTest_012_ScreenOn", RunningLockType::RUNNINGLOCK_SCREEN);
734 runninglockScreen->Lock();
735 EXPECT_TRUE(runninglockScreen->IsUsed());
736 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
737
738 auto inputManager = MMI::InputManager::GetInstance();
739
740 std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
__anonc4d2928d0302null741 FFRTUtils::SubmitTask([&inputManager, &keyEvent] {
742 inputManager->SimulateInputEvent(keyEvent);
743 });
__anonc4d2928d0402null744 FFRTTask callingInterface = [&powerMgrClient] {
745 usleep(50000);
746 powerMgrClient.SetForceTimingOut(true);
747 };
748 FFRTUtils::SubmitTask(callingInterface);
749 ffrt::wait();
750 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
751 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
752 usleep((OVER_TIME_SCREEN_OFF_TIME_MS + WAIT_SUSPEND_TIME_MS) * US_PER_MS);
753 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
754 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
755 EXPECT_FALSE(powerMgrClient.IsScreenOn());
756 powerMgrClient.SetForceTimingOut(false);
757 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_012 end");
758 }
759 #endif
760 /**
761 * @tc.name: PowerCoordinationLockTest_013
762 * @tc.desc: test publishing screen off event
763 * @tc.type: FUNC
764 */
765 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_013, TestSize.Level0)
766 {
767 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_013 start");
768 int PARM_ZERO = 0;
769 int PARM_ONE = 1;
770 int PARM_TWO = 2;
771 auto& powerMgrClient = PowerMgrClient::GetInstance();
772 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
773 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
774 powerMgrClient.RegisterPowerStateCallback(stateCallback);
775 EXPECT_FALSE(subscriber == nullptr);
776 powerMgrClient.WakeupDevice();
777 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
778 powerMgrClient.SuspendDevice();
779 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
780 EXPECT_TRUE(g_screenOffEvent);
781 EXPECT_TRUE(g_inactiveCallback);
782 for (int i = 0; i < 4; ++i) {
783 powerMgrClient.WakeupDevice();
784 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
785 ResetTriggeredFlag();
786 powerMgrClient.LockScreenAfterTimingOut(i % PARM_TWO, i / PARM_TWO, PARM_ZERO);
787 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
788 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
789 if (i % PARM_TWO) {
790 EXPECT_TRUE(g_screenOffEvent);
791 EXPECT_TRUE(g_inactiveCallback);
792 } else {
793 EXPECT_FALSE(g_screenOffEvent);
794 EXPECT_FALSE(g_inactiveCallback);
795 }
796 }
797 for (int i = 0; i < 4; ++i) {
798 powerMgrClient.WakeupDevice();
799 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
800 ResetTriggeredFlag();
801 powerMgrClient.LockScreenAfterTimingOut(i % PARM_TWO, i / PARM_TWO, PARM_ONE);
802 powerMgrClient.SuspendDevice();
803 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
804 EXPECT_TRUE(g_screenOffEvent);
805 EXPECT_TRUE(g_inactiveCallback);
806 }
807 powerMgrClient.LockScreenAfterTimingOut(PARM_ONE, PARM_ZERO, PARM_ONE);
808 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_013 end");
809 }
810 }
811