1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "power_mgr_service_native_test.h"
17
18 #ifdef THERMAL_GTEST
19 #define private public
20 #define protected public
21 #endif
22
23 #include <csignal>
24 #include <iostream>
25
26 #include "key_event.h"
27 #include "pointer_event.h"
28 #include "power_common.h"
29 #include "power_mgr_service.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::PowerMgr;
33 using namespace OHOS;
34 using namespace std;
35
36 namespace {
37 constexpr int32_t TIMEOUTMS = 7;
38 constexpr int64_t CALLTIMEMS = 1;
39 constexpr int64_t SUSCALLTIMEMS = 3;
40 constexpr pid_t PID = 1;
41 constexpr pid_t UID = 1;
42 constexpr int32_t UNCANCELID = -1;
43
44 sptr<PowerMgrService> g_pmsTest;
45 } // namespace
46
SetUp()47 void PowerMgrServiceNativeTest::SetUp()
48 {
49 g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
50 EXPECT_TRUE(g_pmsTest != nullptr) << "PowerMgrService02 fail to get PowerMgrService";
51 g_pmsTest->OnStart();
52
53 auto stateMachine = std::make_shared<PowerStateMachine>(g_pmsTest);
54 EXPECT_TRUE(stateMachine->Init());
55 UserActivityType userActivityType = UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY;
56 stateMachine->RefreshActivityInner(PID, CALLTIMEMS, userActivityType, true);
57 auto runningLockMgr = std::make_shared<RunningLockMgr>(g_pmsTest);
58 EXPECT_TRUE(runningLockMgr->Init());
59 sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
60 RunningLockParam runningLockParam {
61 "runninglockNativeTest1", RunningLockType::RUNNINGLOCK_SCREEN, TIMEOUTMS, PID, UID};
62 EXPECT_TRUE(runningLockMgr->CreateRunningLock(remoteObj, runningLockParam) != nullptr);
63 runningLockMgr->Lock(remoteObj, TIMEOUTMS);
64 EXPECT_FALSE(runningLockMgr->ReleaseLock(remoteObj));
65 }
66
TearDown()67 void PowerMgrServiceNativeTest::TearDown()
68 {
69 g_pmsTest->OnStop();
70 }
71
OnPowerStateChanged(PowerState state)72 void PowerStateTestCallback::OnPowerStateChanged(PowerState state)
73 {
74 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %u.", static_cast<uint32_t>(state));
75 }
76
77 namespace {
78 /**
79 * @tc.name: PowerMgrServiceNative001
80 * @tc.desc: test init in powerMgrService
81 * @tc.type: FUNC
82 */
83 HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative001, TestSize.Level0)
84 {
85 g_pmsTest->KeyMonitorCancel();
86
87 g_pmsTest->HallSensorSubscriberInit();
88 g_pmsTest->HallSensorSubscriberCancel();
89
90 int32_t keyCode = OHOS::MMI::KeyEvent::KEYCODE_F1;
91 g_pmsTest->HandleKeyEvent(keyCode);
92 int32_t type = OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE;
93 g_pmsTest->HandlePointEvent(type);
94
95 EXPECT_TRUE(g_pmsTest->OverrideScreenOffTime(TIMEOUTMS));
96 EXPECT_TRUE(g_pmsTest->RestoreScreenOffTime());
97
98 g_pmsTest->doubleClickId_ = UNCANCELID;
99 g_pmsTest->monitorId_ = UNCANCELID;
100 g_pmsTest->KeyMonitorCancel();
101 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative001 end.");
102 }
103
104 /**
105 * @tc.name: PowerMgrServiceNative002
106 * @tc.desc: test handleKeyEvent in powerMgrService
107 * @tc.type: FUNC
108 */
109 HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative002, TestSize.Level0)
110 {
111 int32_t keyCode = OHOS::MMI::KeyEvent::KEYCODE_F1;
112 g_pmsTest->HandleKeyEvent(keyCode);
113 keyCode = OHOS::MMI::KeyEvent::KEYCODE_F2;
114 g_pmsTest->HandleKeyEvent(keyCode);
115 keyCode = OHOS::MMI::KeyEvent::KEYCODE_VIRTUAL_MULTITASK;
116 g_pmsTest->HandleKeyEvent(keyCode);
117 keyCode = OHOS::MMI::KeyEvent::KEYCODE_WEAR_1;
118 g_pmsTest->HandleKeyEvent(keyCode);
119 int32_t type = OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE;
120 g_pmsTest->HandlePointEvent(type);
121 type = OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN;
122 g_pmsTest->HandlePointEvent(type);
123
124 EXPECT_TRUE(g_pmsTest->ShutDownDevice(SHUTDOWN_FAST_REASON) == PowerErrors::ERR_OK);
125 SuspendDeviceType reasonSDT = SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN;
126 EXPECT_TRUE(g_pmsTest->SuspendDevice(SUSCALLTIMEMS, reasonSDT, false) == PowerErrors::ERR_OK);
127 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative002 end.");
128 }
129 } // namespace
130