!1237 feat: Add OnAsyncPowerStateChanged interface

Merge pull request !1237 from wangyantian/add_async_power_state_0823
This commit is contained in:
openharmony_ci 2024-08-24 08:15:09 +00:00 committed by Gitee
commit 431ba2aefb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
26 changed files with 177 additions and 46 deletions

View File

@ -315,10 +315,10 @@ bool PowerMgrClient::ResetRunningLocks()
return proxy_->ResetRunningLocks();
}
bool PowerMgrClient::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
bool PowerMgrClient::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
{
RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
bool ret = proxy_->RegisterPowerStateCallback(callback);
bool ret = proxy_->RegisterPowerStateCallback(callback, isSync);
return ret;
}

View File

@ -66,7 +66,7 @@ public:
virtual bool IsScreenOn(bool needPrintLog = true) = 0;
virtual bool IsFoldScreenOn() = 0;
virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs) = 0;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) = 0;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true) = 0;
virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) = 0;
virtual bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback,

View File

@ -26,6 +26,7 @@ namespace PowerMgr {
class IPowerStateCallback : public IRemoteBroker {
public:
virtual void OnPowerStateChanged(PowerState state) = 0;
virtual void OnAsyncPowerStateChanged(PowerState state) = 0;
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IPowerStateCallback");
};

View File

@ -162,7 +162,7 @@ public:
bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid);
bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos);
bool ResetRunningLocks();
bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true);
bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback);
bool UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback);

View File

@ -21,6 +21,7 @@ namespace OHOS {
namespace PowerMgr {
enum class PowerStateCallbackInterfaceCode {
POWER_STATE_CHANGED = 0,
ASYNC_POWER_STATE_CHANGED = 1,
};
} // space PowerMgr
} // namespace OHOS

View File

@ -83,7 +83,7 @@ public:
virtual bool ProxyRunningLocks(bool isProxied,
const std::vector<std::pair<pid_t, pid_t>>& processInfos) override;
virtual bool ResetRunningLocks() override;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true) override;
virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority) override;

View File

@ -112,7 +112,7 @@ public:
#ifdef POWER_MANAGER_POWER_ENABLE_S4
bool HibernateInner(bool clearMemory);
#endif
void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true);
void UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
void SetDelayTimer(int64_t delayTime, int32_t event);
void CancelDelayTimer(int32_t event);
@ -322,7 +322,8 @@ private:
std::mutex stateMutex_;
DevicePowerState mDeviceState_;
sptr<IRemoteObject::DeathRecipient> powerStateCBDeathRecipient_;
std::set<const sptr<IPowerStateCallback>, classcomp> powerStateListeners_;
std::set<const sptr<IPowerStateCallback>, classcomp> syncPowerStateListeners_;
std::set<const sptr<IPowerStateCallback>, classcomp> asyncPowerStateListeners_;
std::shared_ptr<IDeviceStateAction> stateAction_;
std::atomic<int64_t> displayOffTime_ {DEFAULT_DISPLAY_OFF_TIME};

View File

@ -1088,7 +1088,7 @@ bool PowerMgrService::ResetRunningLocks()
return true;
}
bool PowerMgrService::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
bool PowerMgrService::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
{
std::lock_guard lock(stateMutex_);
pid_t pid = IPCSkeleton::GetCallingPid();
@ -1096,8 +1096,9 @@ bool PowerMgrService::RegisterPowerStateCallback(const sptr<IPowerStateCallback>
if (!Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) {
return false;
}
POWER_HILOGI(FEATURE_POWER_STATE, "%{public}s: pid: %{public}d, uid: %{public}d", __func__, pid, uid);
powerStateMachine_->RegisterPowerStateCallback(callback);
POWER_HILOGI(FEATURE_POWER_STATE, "%{public}s: pid: %{public}d, uid: %{public}d, isSync: %{public}u", __func__, pid,
uid, static_cast<uint32_t>(isSync));
powerStateMachine_->RegisterPowerStateCallback(callback, isSync);
return true;
}

View File

@ -774,18 +774,28 @@ void PowerStateMachine::ReceiveScreenEvent(bool isScreenOn)
}
}
void PowerStateMachine::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
void PowerStateMachine::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
{
std::lock_guard lock(mutex_);
RETURN_IF(callback == nullptr);
auto object = callback->AsObject();
RETURN_IF(object == nullptr);
auto retIt = powerStateListeners_.insert(callback);
if (retIt.second) {
bool result = false;
if (isSync) {
auto retIt = syncPowerStateListeners_.insert(callback);
result = retIt.second;
POWER_HILOGD(FEATURE_POWER_STATE, "sync listeners.size = %{public}d, insertOk = %{public}d",
static_cast<unsigned int>(syncPowerStateListeners_.size()), retIt.second);
} else {
auto retIt = asyncPowerStateListeners_.insert(callback);
result = retIt.second;
POWER_HILOGD(FEATURE_POWER_STATE, "async listeners.size = %{public}d, insertOk = %{public}d",
static_cast<unsigned int>(asyncPowerStateListeners_.size()), retIt.second);
}
if (result) {
object->AddDeathRecipient(powerStateCBDeathRecipient_);
}
POWER_HILOGD(FEATURE_POWER_STATE, "listeners.size = %{public}d, insertOk = %{public}d",
static_cast<unsigned int>(powerStateListeners_.size()), retIt.second);
}
void PowerStateMachine::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
@ -794,12 +804,22 @@ void PowerStateMachine::UnRegisterPowerStateCallback(const sptr<IPowerStateCallb
RETURN_IF(callback == nullptr);
auto object = callback->AsObject();
RETURN_IF(object == nullptr);
size_t eraseNum = powerStateListeners_.erase(callback);
if (eraseNum != 0) {
object->RemoveDeathRecipient(powerStateCBDeathRecipient_);
size_t eraseNum = 0;
if (syncPowerStateListeners_.find(callback) != syncPowerStateListeners_.end()) {
eraseNum = syncPowerStateListeners_.erase(callback);
if (eraseNum != 0) {
object->RemoveDeathRecipient(powerStateCBDeathRecipient_);
}
POWER_HILOGD(FEATURE_POWER_STATE, "sync listeners.size = %{public}d, eraseNum = %{public}zu",
static_cast<unsigned int>(syncPowerStateListeners_.size()), eraseNum);
} else {
eraseNum = asyncPowerStateListeners_.erase(callback);
if (eraseNum != 0) {
object->RemoveDeathRecipient(powerStateCBDeathRecipient_);
}
POWER_HILOGD(FEATURE_POWER_STATE, "async listeners.size = %{public}d, eraseNum = %{public}zu",
static_cast<unsigned int>(asyncPowerStateListeners_.size()), eraseNum);
}
POWER_HILOGD(FEATURE_POWER_STATE, "listeners.size = %{public}d, eraseNum = %{public}zu",
static_cast<unsigned int>(powerStateListeners_.size()), eraseNum);
}
void PowerStateMachine::EnableMock(IDeviceStateAction* mockAction)
@ -823,16 +843,19 @@ void PowerStateMachine::NotifyPowerStateChanged(PowerState state)
return;
}
POWER_HILOGD(
FEATURE_POWER_STATE, "state=%{public}u, listeners.size=%{public}zu", state, powerStateListeners_.size());
HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "STATE", HiviewDFX::HiSysEvent::EventType::STATISTIC,
"STATE", static_cast<uint32_t>(state));
FEATURE_POWER_STATE, "state=%{public}u, listeners.size=%{public}zu", state, syncPowerStateListeners_.size());
HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "STATE", HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE",
static_cast<uint32_t>(state));
std::lock_guard lock(mutex_);
int64_t now = GetTickCount();
// Send Notification event
SendEventToPowerMgrNotify(state, now);
// Call back all native function
for (auto& listener : powerStateListeners_) {
for (auto& listener : asyncPowerStateListeners_) {
listener->OnAsyncPowerStateChanged(state);
}
for (auto& listener : syncPowerStateListeners_) {
listener->OnPowerStateChanged(state);
}
}
@ -1425,7 +1448,7 @@ void PowerStateMachine::HandleProximityScreenOffTimer(PowerState state, StateCha
if ((reason == StateChangeReason::STATE_CHANGE_REASON_DOUBLE_CLICK ||
reason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) &&
IsProximityClose() && state == PowerState::AWAKE) {
POWER_HILOGI(FEATURE_POWER_STATE, "Double-click or pickup is allowed to cancel proximity-screen-off timer");
POWER_HILOGI(FEATURE_POWER_STATE, "Double-click or pickup is not allowed to cancel proximity-screen-off timer");
return;
}
if (reason != StateChangeReason::STATE_CHANGE_REASON_PROXIMITY) {

View File

@ -174,15 +174,6 @@ void RunningLockMgr::InitLocksTypeProximity()
std::make_shared<LockCounter>(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL,
[this](bool active, [[maybe_unused]] RunningLockParam runningLockParam) -> int32_t {
POWER_HILOGD(FEATURE_RUNNING_LOCK, "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL action start");
auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
if (pms == nullptr) {
return RUNNINGLOCK_FAILURE;
}
auto stateMachine = pms->GetPowerStateMachine();
auto suspendController = pms->GetSuspendController();
if (stateMachine == nullptr || suspendController == nullptr) {
return RUNNINGLOCK_FAILURE;
}
if (active) {
POWER_HILOGI(FEATURE_RUNNING_LOCK, "[UL_POWER] RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL active");
proximityController_.Enable();

View File

@ -72,7 +72,7 @@ public:
virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override;
virtual PowerErrors ShutDownDevice(const std::string& reason) override;
virtual PowerErrors SetSuspendTag(const std::string& tag) override;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true) override;
virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
virtual bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
virtual bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;

View File

@ -35,6 +35,7 @@ public:
~PowerStateCallbackProxy() = default;
DISALLOW_COPY_AND_MOVE(PowerStateCallbackProxy);
virtual void OnPowerStateChanged(PowerState state) override;
virtual void OnAsyncPowerStateChanged(PowerState state) override;
private:
static inline BrokerDelegator<PowerStateCallbackProxy> delegator_;

View File

@ -30,9 +30,11 @@ public:
virtual ~PowerStateCallbackStub() = default;
int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
void OnPowerStateChanged(PowerState __attribute__((unused))state) override {}
void OnAsyncPowerStateChanged(PowerState __attribute__((unused))state) override {}
private:
int32_t OnPowerStateChangedStub(MessageParcel& data);
int32_t OnAsyncPowerStateChangedStub(MessageParcel& data);
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -724,7 +724,7 @@ bool PowerMgrProxy::IsFoldScreenOn()
return result;
}
bool PowerMgrProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
bool PowerMgrProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
{
sptr<IRemoteObject> remote = Remote();
RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
@ -739,6 +739,7 @@ bool PowerMgrProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>&
}
RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, isSync, false);
int ret = remote->SendRequest(
static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK),

View File

@ -496,10 +496,12 @@ int32_t PowerMgrStub::IsScreenOnStub(MessageParcel& data, MessageParcel& reply)
int32_t PowerMgrStub::RegisterPowerStateCallbackStub(MessageParcel& data)
{
sptr<IRemoteObject> obj = data.ReadRemoteObject();
bool isSync = true;
RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isSync, E_READ_PARCEL_ERROR);
RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
RegisterPowerStateCallback(callback);
RegisterPowerStateCallback(callback, isSync);
return ERR_OK;
}

View File

@ -47,5 +47,29 @@ void PowerStateCallbackProxy::OnPowerStateChanged(PowerState state)
POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
}
}
void PowerStateCallbackProxy::OnAsyncPowerStateChanged(PowerState state)
{
sptr<IRemoteObject> remote = Remote();
RETURN_IF(remote == nullptr);
MessageParcel data;
MessageParcel reply;
MessageOption option = { MessageOption::TF_ASYNC };
if (!data.WriteInterfaceToken(PowerStateCallbackProxy::GetDescriptor())) {
POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
return;
}
RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Uint32, static_cast<uint32_t>(state));
int ret = remote->SendRequest(
static_cast<int>(PowerMgr::PowerStateCallbackInterfaceCode::ASYNC_POWER_STATE_CHANGED),
data, reply, option);
if (ret != ERR_OK) {
POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
}
}
} // namespace PowerMgr
} // namespace OHOS

View File

@ -42,6 +42,8 @@ int PowerStateCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
int ret = ERR_OK;
if (code == static_cast<uint32_t>(PowerMgr::PowerStateCallbackInterfaceCode::POWER_STATE_CHANGED)) {
ret = OnPowerStateChangedStub(data);
} else if (code == static_cast<uint32_t>(PowerMgr::PowerStateCallbackInterfaceCode::ASYNC_POWER_STATE_CHANGED)) {
ret = OnAsyncPowerStateChangedStub(data);
} else {
ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
@ -56,5 +58,13 @@ int32_t PowerStateCallbackStub::OnPowerStateChangedStub(MessageParcel& data)
OnPowerStateChanged(static_cast<PowerState>(type));
return ERR_OK;
}
int32_t PowerStateCallbackStub::OnAsyncPowerStateChangedStub(MessageParcel& data)
{
uint32_t type;
RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
OnAsyncPowerStateChanged(static_cast<PowerState>(type));
return ERR_OK;
}
} // namespace PowerMgr
} // namespace OHOS

View File

@ -289,6 +289,7 @@ HWTEST_F(PowerMgrSTMockTest, PowerMgrMock071, TestSize.Level2)
.WillOnce(::testing::Return(ActionResult::SUCCESS));
sleep(REFRESHACTIVITY_WAIT_TIME_S + ONE_SECOND);
pms->Lock(token);
pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
EXPECT_EQ(pms->IsUsed(token), true);
pms->UnLock(token);
@ -328,6 +329,7 @@ HWTEST_F(PowerMgrSTMockTest, PowerMgrMock072, TestSize.Level2)
.WillOnce(::testing::Return(ActionResult::SUCCESS));
sleep(REFRESHACTIVITY_WAIT_TIME_S + ONE_SECOND);
pms->Lock(token);
pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
EXPECT_EQ(pms->IsUsed(token), true);
pms->UnLock(token);
EXPECT_EQ(pms->IsUsed(token), false);
@ -386,6 +388,7 @@ HWTEST_F(PowerMgrSTMockTest, PowerMgrMock075, TestSize.Level2)
RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
pms->CreateRunningLock(token, info);
pms->Lock(token);
pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test"));
EXPECT_EQ(pms->IsUsed(token), true);
EXPECT_EQ(PowerState::AWAKE, pms->GetState());
pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE);

View File

@ -50,12 +50,14 @@ public:
PowerStateTest1Callback() {};
virtual ~PowerStateTest1Callback() {};
virtual void OnPowerStateChanged(PowerState state) override;
virtual void OnAsyncPowerStateChanged(PowerState state) override;
};
class PowerStateTest2Callback : public PowerStateCallbackStub {
public:
PowerStateTest2Callback() {};
virtual ~PowerStateTest2Callback() {};
virtual void OnPowerStateChanged(PowerState state) override;
virtual void OnAsyncPowerStateChanged(PowerState state) override;
};
private:
};

View File

@ -53,6 +53,7 @@ public:
PowerStateTest1Callback() {};
virtual ~PowerStateTest1Callback() {};
virtual void OnPowerStateChanged(PowerState state) override;
virtual void OnAsyncPowerStateChanged(PowerState state) override;
};
class PowerStateTest2Callback : public PowerStateCallbackStub {
@ -60,6 +61,7 @@ public:
PowerStateTest2Callback() {};
virtual ~PowerStateTest2Callback() {};
virtual void OnPowerStateChanged(PowerState state) override;
virtual void OnAsyncPowerStateChanged(PowerState state) override;
};
} // namespace PowerMgr
} // namespace OHOS

View File

@ -65,7 +65,7 @@ public:
PowerErrors RebootDevice(const std::string& reason);
PowerErrors RebootDeviceForDeprecated(const std::string& reason);
PowerErrors ShutDownDevice(const std::string& reason);
bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true);
bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);
bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);

View File

@ -195,13 +195,25 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine007, TestSize.Level0)
void PowerStateMachineTest::PowerStateTest1Callback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %u.",
POWER_HILOGI(
LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
void PowerStateMachineTest::PowerStateTest1Callback::OnAsyncPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnAsyncPowerStateChanged state = %{public}u.",
static_cast<uint32_t>(state));
}
void PowerStateMachineTest::PowerStateTest2Callback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest2Callback::OnPowerStateChanged state = %u.",
POWER_HILOGI(
LABEL_TEST, "PowerStateTest2Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
void PowerStateMachineTest::PowerStateTest2Callback::OnAsyncPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest2Callback::OnAsyncPowerStateChanged state = %{public}u.",
static_cast<uint32_t>(state));
}
@ -229,6 +241,30 @@ HWTEST_F (PowerStateMachineTest, PowerStateCallback001, TestSize.Level0)
EXPECT_TRUE(powerMgrClient.UnRegisterPowerStateCallback(cb1));
POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback001 end.");
}
/**
* @tc.name: PowerStateCallback002
* @tc.desc: test PowerStateCallback
* @tc.type: FUNC
*/
HWTEST_F (PowerStateMachineTest, PowerStateCallback002, TestSize.Level0)
{
auto& powerMgrClient = PowerMgrClient::GetInstance();
sptr<IPowerStateCallback> cb1 = new PowerStateTest1Callback();
powerMgrClient.RegisterPowerStateCallback(cb1, false);
POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 1.");
{
sptr<IPowerStateCallback> cb2 = new PowerStateTest2Callback();
powerMgrClient.UnRegisterPowerStateCallback(cb2);
POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 2.");
powerMgrClient.RegisterPowerStateCallback(cb2, false);
POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 3.");
powerMgrClient.RegisterPowerStateCallback(cb2, false);
POWER_HILOGI(LABEL_TEST, "PowerStateCallback002 4.");
}
EXPECT_TRUE(powerMgrClient.UnRegisterPowerStateCallback(cb1));
POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::PowerStateCallback002 end.");
}
}
void PowerStateMachineTest::WakeUpthread()

View File

@ -28,12 +28,26 @@ void NativePowerStateMachineTest::SetUpTestCase() {}
void PowerStateTest1Callback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %u.", static_cast<uint32_t>(state));
POWER_HILOGI(
LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
void PowerStateTest1Callback::OnAsyncPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnAsyncPowerStateChanged state = %{public}u.",
static_cast<uint32_t>(state));
}
void PowerStateTest2Callback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest1Callback::OnPowerStateChanged state = %u.", static_cast<uint32_t>(state));
POWER_HILOGI(
LABEL_TEST, "PowerStateTest2Callback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
void PowerStateTest2Callback::OnAsyncPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTest2Callback::OnAsyncPowerStateChanged state = %{public}u.",
static_cast<uint32_t>(state));
}
namespace {
@ -98,6 +112,7 @@ HWTEST_F(NativePowerStateMachineTest, NativePowerStateMachine002, TestSize.Level
EXPECT_TRUE(stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true));
stateMachine->UnRegisterPowerStateCallback(cb);
sptr<IPowerStateCallback> cb1 = new PowerStateTest2Callback();
stateMachine->RegisterPowerStateCallback(cb1);
stateMachine->UnRegisterPowerStateCallback(cb1);
sptr<IPowerStateCallback> cb2 = nullptr;
stateMachine->RegisterPowerStateCallback(cb2);
@ -105,6 +120,19 @@ HWTEST_F(NativePowerStateMachineTest, NativePowerStateMachine002, TestSize.Level
stateMachine->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT, true);
EXPECT_TRUE(stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true));
sptr<IPowerStateCallback> cb3 = new PowerStateTest1Callback();
stateMachine->RegisterPowerStateCallback(cb3, false);
stateMachine->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true);
EXPECT_TRUE(stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true));
stateMachine->UnRegisterPowerStateCallback(cb3);
sptr<IPowerStateCallback> cb4 = new PowerStateTest2Callback();
sptr<IPowerStateCallback> cb5 = nullptr;
stateMachine->RegisterPowerStateCallback(cb4, false);
stateMachine->RegisterPowerStateCallback(cb5, false);
stateMachine->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT, true);
EXPECT_TRUE(stateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true));
stateMachine->UnRegisterPowerStateCallback(cb4);
stateMachine->UnRegisterPowerStateCallback(cb5);
POWER_HILOGI(LABEL_TEST, "NativePowerStateMachine002::fun is end!");
GTEST_LOG_(INFO) << "NativePowerStateMachine002: Suspend Device end.";
}

View File

@ -37,8 +37,8 @@ using namespace std;
void PowerStateTestCallback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %u.",
static_cast<uint32_t>(state));
POWER_HILOGI(
LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
namespace {

View File

@ -72,7 +72,8 @@ void PowerMgrServiceNativeTest::TearDown()
void PowerStateTestCallback::OnPowerStateChanged(PowerState state)
{
POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %u.", static_cast<uint32_t>(state));
POWER_HILOGI(
LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
}
namespace {

View File

@ -555,7 +555,7 @@ bool PowerMgrServiceTestProxy::IsScreenOn(bool needPrintLog)
return result;
}
bool PowerMgrServiceTestProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
bool PowerMgrServiceTestProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
{
RETURN_IF_WITH_RET((stub_ == nullptr) || (callback == nullptr), false);
@ -569,6 +569,7 @@ bool PowerMgrServiceTestProxy::RegisterPowerStateCallback(const sptr<IPowerState
}
data.WriteRemoteObject(callback->AsObject());
data.WriteBool(isSync);
int ret = stub_->OnRemoteRequest(
static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK),