1 /*
2  * Copyright (c) 2022 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_HILOGI(
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  */
51 HWTEST_F(PowerMgrClientNativeTest, PowerMgrClientNative001, TestSize.Level2)
52 {
53     POWER_HILOGI(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     EXPECT_TRUE(powerMgrClient.Connect() == ERR_OK);
59     powerMgrClient.ResetProxy(remoteObject_);
60     powerMgrClient.ResetProxy(nullptr);
61     if (powerMgrClient.deathRecipient_ != nullptr) {
62         powerMgrClient.deathRecipient_->OnRemoteDied(remoteObject_);
63         powerMgrClient.deathRecipient_->OnRemoteDied(nullptr);
64     }
65     EXPECT_FALSE(powerMgrClient.RegisterSyncSleepCallback(nullptr, SleepPriority::DEFAULT));
66     EXPECT_FALSE(powerMgrClient.UnRegisterSyncSleepCallback(nullptr));
67     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(nullptr));
68     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(nullptr));
69     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(nullptr));
70     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(nullptr));
71 
72     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(nullptr));
73     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(nullptr));
74     EXPECT_FALSE(powerMgrClient.RegisterScreenStateCallback(0, nullptr));
75     EXPECT_FALSE(powerMgrClient.UnRegisterScreenStateCallback(nullptr));
76     powerMgrClient.~PowerMgrClient();
77 
78     POWER_HILOGI(LABEL_TEST, "PowerMgrClient001::fun is end!");
79 }
80 
81 /**
82  * @tc.name: RunningLockNative001
83  * @tc.desc: test init in RunningLock
84  * @tc.type: FUNC
85  * @tc.require: issueI5MJZJ
86  */
87 HWTEST_F(PowerMgrClientNativeTest, RunningLockNative002, TestSize.Level2)
88 {
89     POWER_HILOGI(LABEL_TEST, "RunningLockNative002::fun is start!");
90     std::shared_ptr<RunningLock> runningLock =
91         std::make_shared<RunningLock>(nullptr, "runninglock1", RunningLockType::RUNNINGLOCK_SCREEN);
92     runningLock->Create();
93     EXPECT_TRUE(runningLock->UnLock() != ERR_OK);
94     runningLock->Release();
95     POWER_HILOGI(LABEL_TEST, "RunningLockNative002::fun is end!");
96 }
97 }