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_mgr_client_native_test.h"
17 
18 #include <iostream>
19 
20 #include <datetime_ex.h>
21 #include <gtest/gtest.h>
22 #include <if_system_ability_manager.h>
23 #include <iservice_registry.h>
24 #include <system_ability_definition.h>
25 #include <ipc_skeleton.h>
26 #include <string_ex.h>
27 #include "running_lock_token_stub.h"
28 #include "power_common.h"
29 #include "power_mgr_client.h"
30 #include "power_mgr_service.h"
31 #include "power_state_machine.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::PowerMgr;
35 using namespace OHOS;
36 using namespace std;
37 
OnPowerStateChanged(PowerState state)38 void PowerStateTestCallback::OnPowerStateChanged(PowerState state)
39 {
40     POWER_HILOGD(
41         LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged state = %{public}u.", static_cast<uint32_t>(state));
42 }
43 
44 namespace {
45 /**
46  * @tc.name: PowerMgrClientNative001
47  * @tc.desc: test callback in PowerMgrClient
48  * @tc.type: FUNC
49  * @tc.require: issueI5MJZJ
50  */
HWTEST_F(PowerMgrClientNativeTest, PowerMgrClientNative001, TestSize.Level2)51 HWTEST_F(PowerMgrClientNativeTest, PowerMgrClientNative001, TestSize.Level2)
52 {
53     POWER_HILOGD(LABEL_TEST, "PowerMgrClient001::fun is start!");
54     auto& powerMgrClient = PowerMgrClient::GetInstance();
55     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56     wptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_SERVICE_ID);
57     powerMgrClient.ResetProxy(remoteObject_);
58     powerMgrClient.~PowerMgrClient();
59     EXPECT_TRUE(powerMgrClient.Connect() == ERR_OK);
60     powerMgrClient.ResetProxy(remoteObject_);
61     powerMgrClient.ResetProxy(nullptr);
62     if (powerMgrClient.deathRecipient_ != nullptr) {
63         powerMgrClient.deathRecipient_->OnRemoteDied(remoteObject_);
64         powerMgrClient.deathRecipient_->OnRemoteDied(nullptr);
65     }
66     EXPECT_FALSE(powerMgrClient.RegisterSyncSleepCallback(nullptr, SleepPriority::DEFAULT));
67     EXPECT_FALSE(powerMgrClient.UnRegisterSyncSleepCallback(nullptr));
68     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(nullptr));
69     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(nullptr));
70     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(nullptr));
71     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(nullptr));
72 
73     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(nullptr));
74     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(nullptr));
75     EXPECT_FALSE(powerMgrClient.RegisterScreenStateCallback(0, nullptr));
76     EXPECT_FALSE(powerMgrClient.UnRegisterScreenStateCallback(nullptr));
77     powerMgrClient.~PowerMgrClient();
78 
79     POWER_HILOGD(LABEL_TEST, "PowerMgrClient001::fun is end!");
80 }
81 
82 /**
83  * @tc.name: RunningLockNative001
84  * @tc.desc: test init in RunningLock
85  * @tc.type: FUNC
86  * @tc.require: issueI5MJZJ
87  */
HWTEST_F(PowerMgrClientNativeTest, RunningLockNative002, TestSize.Level2)88 HWTEST_F(PowerMgrClientNativeTest, RunningLockNative002, TestSize.Level2)
89 {
90     POWER_HILOGD(LABEL_TEST, "RunningLockNative002::fun is start!");
91     std::shared_ptr<RunningLock> runningLock =
92         std::make_shared<RunningLock>(nullptr, "runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
93     runningLock->Create();
94     EXPECT_TRUE(runningLock->UnLock() != ERR_OK);
95     runningLock->Release();
96     POWER_HILOGD(LABEL_TEST, "RunningLockNative002::fun is end!");
97 }
98 }