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_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() {}
131 void OnReceiveEvent(const CommonEventData &data) override
132 {
133 std::string action = data.GetWant().GetAction();
134 POWER_HILOGD(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_HILOGD(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_HILOGD(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;
168 void OnPowerStateChanged(PowerState state) override
169 {
170 POWER_HILOGD(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 }
193
194 namespace {
195 /**
196 * @tc.name: PowerCoordinationLockTest_001
197 * @tc.desc: test coordination runninglock func when power state is awake
198 * @tc.type: FUNC
199 * @tc.require: issueI8JBT4
200 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_001, TestSize.Level0)201 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_001, TestSize.Level0)
202 {
203 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_001 start");
204 auto& powerMgrClient = PowerMgrClient::GetInstance();
205 EXPECT_TRUE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_COORDINATION));
206 auto runninglock =
207 powerMgrClient.CreateRunningLock("CoordinationRunninglock001", RunningLockType::RUNNINGLOCK_COORDINATION);
208 ASSERT_NE(runninglock, nullptr);
209 EXPECT_FALSE(runninglock->IsUsed());
210
211 powerMgrClient.WakeupDevice();
212 EXPECT_TRUE(powerMgrClient.IsScreenOn());
213 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
214
215 runninglock->Lock();
216 EXPECT_TRUE(runninglock->IsUsed());
217 runninglock->UnLock();
218 EXPECT_FALSE(runninglock->IsUsed());
219
220 int32_t timeoutMs = 500;
221 runninglock->Lock(timeoutMs);
222 EXPECT_TRUE(runninglock->IsUsed());
223 usleep(timeoutMs * US_PER_MS);
224 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
225 EXPECT_FALSE(runninglock->IsUsed());
226 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_001 end");
227 }
228
229 /**
230 * @tc.name: PowerCoordinationLockTest_002
231 * @tc.desc: test coordination runninglock proxy func when power state is awake
232 * @tc.type: FUNC
233 * @tc.require: issueI8JBT4
234 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_002, TestSize.Level0)235 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_002, TestSize.Level0)
236 {
237 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_002 start");
238 auto& powerMgrClient = PowerMgrClient::GetInstance();
239 auto runninglock =
240 powerMgrClient.CreateRunningLock("CoordinationRunninglock002", RunningLockType::RUNNINGLOCK_COORDINATION);
241 ASSERT_NE(runninglock, nullptr);
242
243 powerMgrClient.WakeupDevice();
244 EXPECT_TRUE(powerMgrClient.IsScreenOn());
245 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
246
247 pid_t curUid = getuid();
248 pid_t curPid = getpid();
249
250 runninglock->Lock();
251 EXPECT_TRUE(runninglock->IsUsed());
252 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
253 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
254
255 runninglock->Lock();
256 EXPECT_TRUE(runninglock->IsUsed());
257 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
258 EXPECT_FALSE(runninglock->IsUsed());
259 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
260 EXPECT_TRUE(runninglock->IsUsed());
261 runninglock->UnLock();
262 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_002 end");
263 }
264
265
266 /**
267 * @tc.name: PowerCoordinationLockTest_003
268 * @tc.desc: test coordination runninglock proxy func when power state is awake
269 * @tc.type: FUNC
270 * @tc.require: issueI8JBT4
271 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_003, TestSize.Level0)272 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_003, TestSize.Level0)
273 {
274 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_003 start");
275 auto& powerMgrClient = PowerMgrClient::GetInstance();
276 auto runninglock =
277 powerMgrClient.CreateRunningLock("CoordinationRunninglock003", RunningLockType::RUNNINGLOCK_COORDINATION);
278 ASSERT_NE(runninglock, nullptr);
279
280 powerMgrClient.WakeupDevice();
281 EXPECT_TRUE(powerMgrClient.IsScreenOn());
282 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
283
284 pid_t curUid = getuid();
285 pid_t curPid = getpid();
286
287 int32_t timeoutMs = 500;
288 runninglock->Lock(timeoutMs);
289 EXPECT_TRUE(runninglock->IsUsed());
290
291 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
292 EXPECT_FALSE(runninglock->IsUsed());
293 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
294 EXPECT_TRUE(runninglock->IsUsed());
295 usleep(timeoutMs * US_PER_MS);
296 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
297 EXPECT_FALSE(runninglock->IsUsed());
298 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_003 end");
299 }
300
301 /**
302 * @tc.name: PowerCoordinationLockTest_004
303 * @tc.desc: test coordination runninglock proxy func when power state is awake
304 * @tc.type: FUNC
305 * @tc.require: issueI8JBT4
306 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_004, TestSize.Level0)307 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_004, TestSize.Level0)
308 {
309 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_004 start");
310 auto& powerMgrClient = PowerMgrClient::GetInstance();
311
312 powerMgrClient.WakeupDevice();
313 EXPECT_TRUE(powerMgrClient.IsScreenOn());
314 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
315
316 pid_t curUid = getuid();
317 pid_t curPid = getpid();
318
319 auto runninglock =
320 powerMgrClient.CreateRunningLock("CoordinationRunninglock004", RunningLockType::RUNNINGLOCK_COORDINATION);
321 ASSERT_NE(runninglock, nullptr);
322 runninglock->Lock();
323 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, curUid));
324 EXPECT_FALSE(runninglock->IsUsed());
325 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, curUid));
326 EXPECT_TRUE(runninglock->IsUsed());
327 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_004 end");
328 }
329
330 /**
331 * @tc.name: PowerCoordinationLockTest_005
332 * @tc.desc: test coordination runninglock proxy func when power state is sleep
333 * @tc.type: FUNC
334 * @tc.require: issueI8JBT4
335 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_005, TestSize.Level0)336 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_005, TestSize.Level0)
337 {
338 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_005 start");
339 auto& powerMgrClient = PowerMgrClient::GetInstance();
340 auto runninglock =
341 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_005", RunningLockType::RUNNINGLOCK_COORDINATION);
342 ASSERT_NE(runninglock, nullptr);
343
344 powerMgrClient.SuspendDevice();
345 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
346
347 EXPECT_FALSE(powerMgrClient.IsScreenOn());
348 EXPECT_EQ(powerMgrClient.GetState(), PowerState::SLEEP);
349
350 runninglock->Lock();
351 EXPECT_FALSE(runninglock->IsUsed());
352 runninglock->UnLock();
353 EXPECT_FALSE(runninglock->IsUsed());
354 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_005 end");
355 }
356
357 /**
358 * @tc.name: PowerCoordinationLockTest_006
359 * @tc.desc: test coordination runninglock is locked, not notify event and callback when inactive(suspend)
360 * @tc.type: FUNC
361 * @tc.require: issueI8JBT4
362 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_006, TestSize.Level0)363 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_006, TestSize.Level0)
364 {
365 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_006 start");
366 auto& powerMgrClient = PowerMgrClient::GetInstance();
367 auto runninglock =
368 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_006", RunningLockType::RUNNINGLOCK_COORDINATION);
369 ASSERT_NE(runninglock, nullptr);
370 EXPECT_FALSE(runninglock->IsUsed());
371 powerMgrClient.WakeupDevice();
372 EXPECT_TRUE(powerMgrClient.IsScreenOn());
373 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
374
375 runninglock->Lock();
376 EXPECT_TRUE(runninglock->IsUsed());
377
378 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
379 EXPECT_FALSE(subscriber == nullptr);
380 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
381 powerMgrClient.RegisterPowerStateCallback(stateCallback);
382
383 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
384 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
385 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
386
387 EXPECT_FALSE(g_screenOffEvent);
388 EXPECT_FALSE(g_inactiveCallback);
389 EXPECT_FALSE(powerMgrClient.IsScreenOn());
390 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
391
392 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
393 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
394
395 runninglock->UnLock();
396 EXPECT_FALSE(runninglock->IsUsed());
397 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
398
399 EXPECT_FALSE(g_screenOffEvent);
400 EXPECT_FALSE(g_inactiveCallback);
401 EXPECT_TRUE(powerMgrClient.IsScreenOn());
402 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
403
404 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
405 powerMgrClient.SuspendDevice();
406 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
407
408 EXPECT_TRUE(g_screenOffEvent);
409 EXPECT_TRUE(g_inactiveCallback);
410 EXPECT_FALSE(powerMgrClient.IsScreenOn());
411 EXPECT_EQ(powerMgrClient.GetState(), PowerState::SLEEP);
412
413 CommonEventManager::UnSubscribeCommonEvent(subscriber);
414 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
415 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_006 end");
416 }
417
418 /**
419 * @tc.name: PowerCoordinationLockTest_007
420 * @tc.desc: test coordination runninglock is locked, not notify event and callback when inactive(over time)
421 * @tc.type: FUNC
422 * @tc.require: issueI8JBT4
423 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_007, TestSize.Level0)424 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_007, TestSize.Level0)
425 {
426 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_007 start");
427 auto& powerMgrClient = PowerMgrClient::GetInstance();
428 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
429 auto runninglock =
430 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_007", RunningLockType::RUNNINGLOCK_COORDINATION);
431 ASSERT_NE(runninglock, nullptr);
432 EXPECT_FALSE(runninglock->IsUsed());
433 powerMgrClient.WakeupDevice();
434 EXPECT_TRUE(powerMgrClient.IsScreenOn());
435 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
436
437 runninglock->Lock();
438 EXPECT_TRUE(runninglock->IsUsed());
439
440 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
441 EXPECT_FALSE(subscriber == nullptr);
442 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
443 powerMgrClient.RegisterPowerStateCallback(stateCallback);
444 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
445
446 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
447
448 EXPECT_FALSE(g_screenOffEvent);
449 EXPECT_FALSE(g_inactiveCallback);
450 EXPECT_FALSE(powerMgrClient.IsScreenOn());
451 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
452
453 usleep(WAIT_AUTO_SUSPEND_SLEEP_TIME_MS * US_PER_MS);
454 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
455
456 runninglock->UnLock();
457 EXPECT_FALSE(runninglock->IsUsed());
458 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
459
460 EXPECT_FALSE(g_screenOffEvent);
461 EXPECT_FALSE(g_inactiveCallback);
462 EXPECT_TRUE(powerMgrClient.IsScreenOn());
463 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
464
465 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
466 CommonEventManager::UnSubscribeCommonEvent(subscriber);
467 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
468 powerMgrClient.RestoreScreenOffTime();
469 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_007 end");
470 }
471
472 /**
473 * @tc.name: PowerCoordinationLockTest_008
474 * @tc.desc: test coordination runninglock function, when the power state transitions
475 * @tc.type: FUNC
476 * @tc.require: issueI8JBT4
477 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_008, TestSize.Level0)478 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_008, TestSize.Level0)
479 {
480 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_008 start");
481 auto& powerMgrClient = PowerMgrClient::GetInstance();
482 auto runninglock =
483 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_008", RunningLockType::RUNNINGLOCK_COORDINATION);
484 ASSERT_NE(runninglock, nullptr);
485 EXPECT_FALSE(runninglock->IsUsed());
486 powerMgrClient.WakeupDevice();
487 EXPECT_TRUE(powerMgrClient.IsScreenOn());
488 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
489
490 runninglock->Lock();
491 EXPECT_TRUE(runninglock->IsUsed());
492
493 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
494 EXPECT_FALSE(subscriber == nullptr);
495 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
496 powerMgrClient.RegisterPowerStateCallback(stateCallback);
497
498 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
499 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
500 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
501
502 EXPECT_FALSE(g_screenOffEvent);
503 EXPECT_FALSE(g_inactiveCallback);
504 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
505
506 ResetTriggeredFlag();
507 powerMgrClient.WakeupDevice();
508 EXPECT_TRUE(powerMgrClient.IsScreenOn());
509 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
510 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
511 EXPECT_TRUE(g_screenOnEvent);
512 EXPECT_TRUE(g_awakeCallback);
513
514 runninglock->UnLock();
515 EXPECT_FALSE(runninglock->IsUsed());
516
517 EXPECT_FALSE(g_screenOffEvent);
518 EXPECT_FALSE(g_inactiveCallback);
519
520 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
521
522 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
523 CommonEventManager::UnSubscribeCommonEvent(subscriber);
524 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
525 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_008 end");
526 }
527
528 /**
529 * @tc.name: PowerCoordinationLockTest_009
530 * @tc.desc: test coordination runninglock function, when the power state transitions
531 * @tc.type: FUNC
532 * @tc.require: issueI8JBT4
533 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_009, TestSize.Level0)534 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_009, TestSize.Level0)
535 {
536 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_009 start");
537 auto& powerMgrClient = PowerMgrClient::GetInstance();
538 auto runninglockOne =
539 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_1", RunningLockType::RUNNINGLOCK_COORDINATION);
540 auto runninglockTwo =
541 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_009_2", RunningLockType::RUNNINGLOCK_COORDINATION);
542 ASSERT_NE(runninglockOne, nullptr);
543 ASSERT_NE(runninglockTwo, nullptr);
544 EXPECT_FALSE(runninglockOne->IsUsed());
545 EXPECT_FALSE(runninglockTwo->IsUsed());
546
547 powerMgrClient.WakeupDevice();
548
549 runninglockOne->Lock();
550 runninglockTwo->Lock();
551 EXPECT_TRUE(runninglockOne->IsUsed());
552 EXPECT_TRUE(runninglockTwo->IsUsed());
553
554 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
555 EXPECT_FALSE(subscriber == nullptr);
556 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
557 powerMgrClient.RegisterPowerStateCallback(stateCallback);
558
559 runninglockOne->UnLock();
560 EXPECT_FALSE(runninglockOne->IsUsed());
561
562 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
563 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
564 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
565
566 EXPECT_FALSE(g_screenOffEvent);
567 EXPECT_FALSE(g_inactiveCallback);
568 EXPECT_FALSE(powerMgrClient.IsScreenOn());
569 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
570
571 runninglockTwo->UnLock();
572 EXPECT_FALSE(runninglockTwo->IsUsed());
573 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
574
575 EXPECT_FALSE(g_screenOffEvent);
576 EXPECT_FALSE(g_inactiveCallback);
577 EXPECT_TRUE(powerMgrClient.IsScreenOn());
578 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
579
580 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
581 CommonEventManager::UnSubscribeCommonEvent(subscriber);
582 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
583 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_009 end");
584 }
585
586 /**
587 * @tc.name: PowerCoordinationLockTest_010
588 * @tc.desc: test coordination runninglock lock, screen keep off when touching the screen
589 * @tc.type: FUNC
590 * @tc.require: issueI8JBT4
591 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_010, TestSize.Level0)592 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_010, TestSize.Level0)
593 {
594 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_010 start");
595 auto& powerMgrClient = PowerMgrClient::GetInstance();
596 auto runninglock =
597 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_010", RunningLockType::RUNNINGLOCK_COORDINATION);
598 ASSERT_NE(runninglock, nullptr);
599 EXPECT_FALSE(runninglock->IsUsed());
600 powerMgrClient.WakeupDevice();
601 EXPECT_TRUE(powerMgrClient.IsScreenOn());
602 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
603
604 runninglock->Lock();
605 EXPECT_TRUE(runninglock->IsUsed());
606
607 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
608 EXPECT_FALSE(subscriber == nullptr);
609 const sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
610 powerMgrClient.RegisterPowerStateCallback(stateCallback);
611
612 powerMgrClient.LockScreenAfterTimingOut(false, false, false);
613 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false);
614 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
615
616 EXPECT_FALSE(g_screenOffEvent);
617 EXPECT_FALSE(g_inactiveCallback);
618 EXPECT_FALSE(powerMgrClient.IsScreenOn());
619 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
620
621 powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_TOUCH);
622 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
623
624 runninglock->UnLock();
625 EXPECT_FALSE(runninglock->IsUsed());
626 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
627
628 EXPECT_FALSE(g_screenOffEvent);
629 EXPECT_FALSE(g_inactiveCallback);
630 EXPECT_TRUE(powerMgrClient.IsScreenOn());
631 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
632
633 powerMgrClient.LockScreenAfterTimingOut(true, false, true);
634 CommonEventManager::UnSubscribeCommonEvent(subscriber);
635 powerMgrClient.UnRegisterPowerStateCallback(stateCallback);
636 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_010 end");
637 }
638
639 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
640 /**
641 * @tc.name: PowerCoordinationLockTest_011
642 * @tc.desc: test entering DIM state while coordination
643 * @tc.type: FUNC
644 * @tc.require: issueI8JBT4
645 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_011, TestSize.Level0)646 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_011, TestSize.Level0)
647 {
648 POWER_HILOGI(LABEL_TEST, "PowerCoordinationLockTest_011 start");
649 auto& powerMgrClient = PowerMgrClient::GetInstance();
650 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
651 EXPECT_FALSE(subscriber == nullptr);
652 auto runninglock =
653 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_011", RunningLockType::RUNNINGLOCK_COORDINATION);
654 ASSERT_NE(runninglock, nullptr);
655 EXPECT_FALSE(runninglock->IsUsed());
656 powerMgrClient.WakeupDevice();
657 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
658 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
659 runninglock->Lock();
660 EXPECT_TRUE(runninglock->IsUsed());
661
662 auto inputManager = MMI::InputManager::GetInstance();
663
664 std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
665 inputManager->SimulateInputEvent(keyEvent);
666 usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
667 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
668 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
669 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
670 // already in DIM, not resetting 10s timer
671 inputManager->SimulateInputEvent(keyEvent);
672 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
673 // already in DIM, not resetting 10s timer
674 inputManager->SimulateInputEvent(keyEvent);
675 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS / 2 * US_PER_MS);
676 // screen should be off now
677 EXPECT_FALSE(powerMgrClient.IsScreenOn());
678
679 powerMgrClient.WakeupDevice();
680 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
681 inputManager->SimulateInputEvent(keyEvent);
682 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
683 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
684 ResetTriggeredFlag();
685 powerMgrClient.WakeupDevice();
686 // DIM to AWAKE, no event
687 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
688 EXPECT_FALSE(g_screenOnEvent);
689 // AWAKE to AWAKE, no event
690 powerMgrClient.WakeupDevice();
691 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
692 EXPECT_FALSE(g_screenOnEvent);
693
694 // test pointer event
695 std::shared_ptr<PointerEvent> pointerEvent = CreatePointerEvent();
696 powerMgrClient.WakeupDevice();
697 inputManager->SimulateInputEvent(pointerEvent);
698 usleep((WAIT_EVENT_TIME_MS + WAIT_STATE_TIME_MS) * US_PER_MS);
699 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
700 usleep(OVER_TIME_SCREEN_OFF_TIME_TEST_MS * US_PER_MS);
701 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
702 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
703 EXPECT_EQ(powerMgrClient.GetState(), PowerState::INACTIVE);
704
705 powerMgrClient.RestoreScreenOffTime();
706 CommonEventManager::UnSubscribeCommonEvent(subscriber);
707 }
708 /**
709 * @tc.name: PowerCoordinationLockTest_012
710 * @tc.desc: test entering DIM state while coordination with SetForceTimingOut set to true at the same time
711 * @tc.type: FUNC
712 * @tc.require: issueI8JBT4
713 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_012, TestSize.Level0)714 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_012, TestSize.Level0)
715 {
716 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_012 start");
717 auto& powerMgrClient = PowerMgrClient::GetInstance();
718 shared_ptr<PowerStateCommonEventSubscriber> subscriber = PowerStateCommonEventSubscriber::RegisterEvent();
719 EXPECT_FALSE(subscriber == nullptr);
720 auto runninglock =
721 powerMgrClient.CreateRunningLock("PowerCoordinationLockTest_012", RunningLockType::RUNNINGLOCK_COORDINATION);
722 ASSERT_NE(runninglock, nullptr);
723 EXPECT_FALSE(runninglock->IsUsed());
724 powerMgrClient.WakeupDevice();
725 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
726 powerMgrClient.OverrideScreenOffTime(OVER_TIME_SCREEN_OFF_TIME_MS);
727 runninglock->Lock();
728 EXPECT_TRUE(runninglock->IsUsed());
729
730 auto runninglockScreen = powerMgrClient.CreateRunningLock(
731 "PowerCoordinationLockTest_012_ScreenOn", RunningLockType::RUNNINGLOCK_SCREEN);
732 runninglockScreen->Lock();
733 EXPECT_TRUE(runninglockScreen->IsUsed());
734 EXPECT_EQ(powerMgrClient.GetState(), PowerState::AWAKE);
735
736 auto inputManager = MMI::InputManager::GetInstance();
737
738 std::shared_ptr<MMI::KeyEvent> keyEvent = CreateKeyEvent();
739 FFRTUtils::SubmitTask([&inputManager, &keyEvent] {
740 inputManager->SimulateInputEvent(keyEvent);
741 });
742 FFRTTask callingInterface = [&powerMgrClient] {
743 usleep(50000);
744 powerMgrClient.SetForceTimingOut(true);
745 };
746 FFRTUtils::SubmitTask(callingInterface);
747 ffrt::wait();
748 usleep(WAIT_EVENT_TIME_MS * US_PER_MS);
749 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
750 usleep((OVER_TIME_SCREEN_OFF_TIME_MS + WAIT_SUSPEND_TIME_MS) * US_PER_MS);
751 EXPECT_EQ(powerMgrClient.GetState(), PowerState::DIM);
752 usleep(SCREEN_OFF_TIME_OVERRIDE_COORDINATION_MS * US_PER_MS);
753 EXPECT_FALSE(powerMgrClient.IsScreenOn());
754 powerMgrClient.SetForceTimingOut(false);
755 POWER_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_012 end");
756 }
757 #endif
758 /**
759 * @tc.name: PowerCoordinationLockTest_013
760 * @tc.desc: test publishing screen off event
761 * @tc.type: FUNC
762 */
HWTEST_F(PowerCoordinationLockTest, PowerCoordinationLockTest_013, TestSize.Level0)763 HWTEST_F (PowerCoordinationLockTest, PowerCoordinationLockTest_013, TestSize.Level0)
764 {
765 POWER_HILOGD(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_HILOGD(LABEL_TEST, "PowerCoordinationLockTest_013 end");
804 }
805 }
806