/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "power_mgr_service_native_test.h" #ifdef THERMAL_GTEST #define private public #define protected public #endif #include #include #include "key_event.h" #include "pointer_event.h" #include "power_common.h" #include "power_mgr_service.h" using namespace testing::ext; using namespace OHOS::PowerMgr; using namespace OHOS; using namespace std; namespace { constexpr int32_t TIMEOUTMS = 7; constexpr int64_t CALLTIMEMS = 1; constexpr int64_t SUSCALLTIMEMS = 3; constexpr pid_t PID = 1; constexpr pid_t UID = 1; constexpr int32_t UNCANCELID = -1; sptr g_pmsTest; } // namespace void PowerMgrServiceNativeTest::SetUp() { g_pmsTest = DelayedSpSingleton::GetInstance(); EXPECT_TRUE(g_pmsTest != nullptr) << "PowerMgrService02 fail to get PowerMgrService"; g_pmsTest->OnStart(); auto stateMachine = std::make_shared(g_pmsTest); EXPECT_TRUE(stateMachine->Init()); UserActivityType userActivityType = UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY; stateMachine->RefreshActivityInner(PID, CALLTIMEMS, userActivityType, true); auto runningLockMgr = std::make_shared(g_pmsTest); EXPECT_TRUE(runningLockMgr->Init()); sptr remoteObj = new RunningLockTokenStub(); RunningLockParam runningLockParam { "runninglockNativeTest1", RunningLockType::RUNNINGLOCK_SCREEN, TIMEOUTMS, PID, UID}; EXPECT_TRUE(runningLockMgr->CreateRunningLock(remoteObj, runningLockParam) != nullptr); runningLockMgr->Lock(remoteObj, TIMEOUTMS); EXPECT_FALSE(runningLockMgr->ReleaseLock(remoteObj)); } void PowerMgrServiceNativeTest::TearDown() { g_pmsTest->OnStop(); } void PowerStateTestCallback::OnPowerStateChanged(PowerState state) { POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %u.", static_cast(state)); } namespace { /** * @tc.name: PowerMgrServiceNative001 * @tc.desc: test init in powerMgrService * @tc.type: FUNC */ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative001, TestSize.Level0) { g_pmsTest->KeyMonitorCancel(); g_pmsTest->HallSensorSubscriberInit(); g_pmsTest->HallSensorSubscriberCancel(); int32_t keyCode = OHOS::MMI::KeyEvent::KEYCODE_F1; g_pmsTest->HandleKeyEvent(keyCode); int32_t type = OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE; g_pmsTest->HandlePointEvent(type); EXPECT_TRUE(g_pmsTest->OverrideScreenOffTime(TIMEOUTMS)); EXPECT_TRUE(g_pmsTest->RestoreScreenOffTime()); g_pmsTest->doubleClickId_ = UNCANCELID; g_pmsTest->monitorId_ = UNCANCELID; g_pmsTest->KeyMonitorCancel(); POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative001 end."); } /** * @tc.name: PowerMgrServiceNative002 * @tc.desc: test handleKeyEvent in powerMgrService * @tc.type: FUNC */ HWTEST_F(PowerMgrServiceNativeTest, PowerMgrServiceNative002, TestSize.Level0) { int32_t keyCode = OHOS::MMI::KeyEvent::KEYCODE_F1; g_pmsTest->HandleKeyEvent(keyCode); keyCode = OHOS::MMI::KeyEvent::KEYCODE_F2; g_pmsTest->HandleKeyEvent(keyCode); keyCode = OHOS::MMI::KeyEvent::KEYCODE_VIRTUAL_MULTITASK; g_pmsTest->HandleKeyEvent(keyCode); keyCode = OHOS::MMI::KeyEvent::KEYCODE_WEAR_1; g_pmsTest->HandleKeyEvent(keyCode); int32_t type = OHOS::MMI::PointerEvent::SOURCE_TYPE_MOUSE; g_pmsTest->HandlePointEvent(type); type = OHOS::MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN; g_pmsTest->HandlePointEvent(type); EXPECT_TRUE(g_pmsTest->ShutDownDevice(SHUTDOWN_FAST_REASON) == PowerErrors::ERR_OK); SuspendDeviceType reasonSDT = SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN; EXPECT_TRUE(g_pmsTest->SuspendDevice(SUSCALLTIMEMS, reasonSDT, false) == PowerErrors::ERR_OK); POWER_HILOGI(LABEL_TEST, "PowerMgrServiceNative002 end."); } } // namespace