/* * Copyright (c) 2022-2024 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_mock_proxy_test.h" #include #include "mock_power_remote_object.h" #include "parcel.h" #include "power_log.h" #include "power_mgr_client.h" #include "power_mgr_proxy.h" #include "power_mode_callback_proxy.h" #include "power_state_callback_proxy.h" #include "power_runninglock_callback_proxy.h" #include "power_state_machine_info.h" #include "running_lock.h" #include "running_lock_info.h" #include "running_lock_token_stub.h" #include "power_mgr_async_reply_stub.h" using namespace testing::ext; using namespace OHOS::PowerMgr; using namespace OHOS; using namespace std; void PowerMockProxyTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode) { POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged."); } void PowerMockProxyTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state) { POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged."); } void PowerMockProxyTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message) { POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage."); } namespace { /** * @tc.name: PowerMockProxyTest001 * @tc.desc: Test proxy when the parcel is mock * @tc.type: FUNC */ HWTEST_F(PowerMockProxyTest, PowerMockProxyTest001, TestSize.Level2) { POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest001 function start!"); pid_t uid = 0; pid_t pid = 0; int32_t powerError = 1; int32_t timeOutMs = -1; std::string name; bool lockTypeSupported = false; bool isUsed = false; sptr remote = new IPCObjectStub(); std::shared_ptr sptrProxy = std::make_shared(remote); sptr token = new RunningLockTokenStub(); RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); sptrProxy->CreateRunningLockIpc(token, info, powerError); EXPECT_FALSE(powerError == static_cast(PowerErrors::ERR_OK)); EXPECT_NE(sptrProxy->ReleaseRunningLockIpc(token, name), ERR_OK); sptrProxy->IsRunningLockTypeSupportedIpc( static_cast(RunningLockType::RUNNINGLOCK_BUTT), lockTypeSupported); EXPECT_FALSE(lockTypeSupported); sptrProxy->LockIpc(token, timeOutMs, powerError); EXPECT_FALSE(powerError == static_cast(PowerErrors::ERR_OK)); sptrProxy->UnLockIpc(token, name, powerError); EXPECT_FALSE(powerError == static_cast(PowerErrors::ERR_OK)); sptrProxy->IsUsedIpc(token, isUsed); EXPECT_FALSE(isUsed); EXPECT_NE(sptrProxy->ProxyRunningLockIpc(true, pid, uid), ERR_OK); std::unique_ptr vectorPairInfos = std::make_unique(); std::vector> processInfos; processInfos.emplace_back(pid, uid); vectorPairInfos->SetProcessInfos(processInfos); EXPECT_NE(sptrProxy->ProxyRunningLocksIpc(true, *vectorPairInfos), ERR_OK); EXPECT_NE(sptrProxy->ResetRunningLocksIpc(), ERR_OK); POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest001 function end!"); } /** * @tc.name: PowerMockProxyTest002 * @tc.desc: Test proxy when the parcel is mock * @tc.type: FUNC */ HWTEST_F(PowerMockProxyTest, PowerMockProxyTest002, TestSize.Level2) { int32_t powerError = 1; int32_t powerMode = 0; int32_t powerState = -1; std::string apiVersion = "-1"; bool isScreenOn = false; int32_t ret = 0; POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest002 function start!"); sptr remote = new IPCObjectStub(); std::shared_ptr sptrProxy = std::make_shared(remote); int32_t suspendReason = (static_cast(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1; SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason); ret = sptrProxy->SuspendDeviceIpc(0, static_cast(abnormaltype), false, apiVersion, powerError); EXPECT_EQ(ret, ERR_INVALID_DATA); ret = sptrProxy->WakeupDeviceIpc(GetTickCount(), static_cast(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION), std::string("app call"), apiVersion, powerError); EXPECT_EQ(ret, ERR_INVALID_DATA); int32_t userAttention = static_cast(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION); EXPECT_NE(sptrProxy->RefreshActivityIpc(GetTickCount(), userAttention, true), ERR_OK); sptrProxy->OverrideScreenOffTimeIpc(200, apiVersion, powerError); EXPECT_FALSE(powerError == static_cast(PowerErrors::ERR_OK)); sptrProxy->RestoreScreenOffTimeIpc(apiVersion, powerError); EXPECT_FALSE(powerError == static_cast(PowerErrors::ERR_OK)); ret = sptrProxy->GetStateIpc(powerState); EXPECT_NE(ret, ERR_OK); sptrProxy->IsScreenOnIpc(true, isScreenOn); EXPECT_FALSE(isScreenOn); sptrProxy->SetDisplaySuspendIpc(true); PowerMode mode1 = PowerMode::NORMAL_MODE; ret = sptrProxy->SetDeviceModeIpc(static_cast(mode1), powerError); EXPECT_EQ(ret, ERR_INVALID_DATA); ret = sptrProxy->GetDeviceModeIpc(powerMode); EXPECT_NE(ret, ERR_OK); POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest002 function end!"); } /** * @tc.name: PowerMockProxyTest003 * @tc.desc: Test proxy when the parcel is mock * @tc.type: FUNC */ HWTEST_F(PowerMockProxyTest, PowerMockProxyTest003, TestSize.Level2) { int32_t powerError = 1; std::string apiVersion = "-1"; int32_t ret = 0; POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest003 function start!"); sptr remote = new IPCObjectStub(); std::shared_ptr sptrProxy = std::make_shared(remote); sptr cb1 = new PowerStateTestCallback(); sptr cb3 = new PowerModeTestCallback(); sptr cb5 =new PowerRunningLockTestCallback(); sptr asyncCallback = new PowerMgrStubAsync(); sptr powerProxy = iface_cast(asyncCallback); EXPECT_NE(sptrProxy->RegisterPowerStateCallbackIpc(cb1, true), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterPowerStateCallbackIpc(cb1), ERR_OK); EXPECT_NE(sptrProxy->RegisterPowerStateCallbackIpc(nullptr, true), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterPowerStateCallbackIpc(nullptr), ERR_OK); EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(cb3), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(cb3), ERR_OK); EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(nullptr), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(nullptr), ERR_OK); EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(cb5), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(cb5), ERR_OK); EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(nullptr), ERR_OK); EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(nullptr), ERR_OK); ret = sptrProxy->RebootDeviceIpc(" ", powerError); EXPECT_EQ(ret, ERR_INVALID_DATA); ret = sptrProxy->ShutDownDeviceIpc(" ", powerError); EXPECT_EQ(ret, ERR_INVALID_DATA); ret = sptrProxy->ForceSuspendDeviceIpc(0, apiVersion, powerProxy); EXPECT_EQ(ret, ERR_INVALID_DATA); static std::vector dumpArgs; dumpArgs.push_back("-a"); std::string errorCode; std::string actualDebugInfo; sptrProxy->ShellDumpIpc(dumpArgs, dumpArgs.size(), actualDebugInfo); EXPECT_EQ(errorCode, actualDebugInfo); POWER_HILOGI(LABEL_TEST, "PowerMockProxyTest003 function end!"); } } // namespace