1 /*
2 * Copyright (c) 2022-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_mock_object_test.h"
17
18 #include <datetime_ex.h>
19
20 #include "errors.h"
21 #include "mock_power_remote_object.h"
22 #include "power_log.h"
23 #include "power_mgr_client.h"
24 #include "power_mgr_proxy.h"
25 #include "power_mode_callback_proxy.h"
26 #include "power_state_callback_proxy.h"
27 #include "power_runninglock_callback_proxy.h"
28 #include "running_lock.h"
29 #include "running_lock_info.h"
30 #include "running_lock_token_stub.h"
31 #include "power_mgr_async_reply_stub.h"
32 #include "takeover_suspend_callback_proxy.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::PowerMgr;
36 using namespace OHOS;
37 using namespace std;
38
OnPowerModeChanged(PowerMode mode)39 void PowerMockObjectTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
40 {
41 POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
42 }
43
OnPowerStateChanged(PowerState state)44 void PowerMockObjectTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
45 {
46 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
47 }
48
HandleRunningLockMessage(std::string message)49 void PowerMockObjectTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
50 {
51 POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
52 }
53
54 namespace {
55 /**
56 * @tc.name: PowerMockObjectTest001
57 * @tc.desc: Test Power proxy when the PowerRemoteObject is mock
58 * @tc.type: FUNC
59 * @tc.require: issueI5IUHE
60 */
61 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest001, TestSize.Level2)
62 {
63 int32_t powerError = 1;
64 int32_t timeOutMs = -1;
65 int32_t powerState = -1;
66 int32_t ret = 0;
67 std::string name;
68 bool isUsed = false;
69 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest001 function start!");
70 PowerMode mode = PowerMode::NORMAL_MODE;
71 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
72 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
73 std::shared_ptr<PowerModeCallbackProxy> callbackProxy = std::make_shared<PowerModeCallbackProxy>(remote);
74 callbackProxy->OnPowerModeChanged(mode);
75 std::shared_ptr<PowerStateCallbackProxy> stateCallbackProxy = std::make_shared<PowerStateCallbackProxy>(remote);
76 PowerState state = PowerState::AWAKE;
77 stateCallbackProxy->OnPowerStateChanged(state);
78 #ifdef POWER_MANAGER_TAKEOVER_SUSPEND
79 std::shared_ptr<TakeOverSuspendCallbackProxy> suspendCallbackProxy =
80 std::make_shared<TakeOverSuspendCallbackProxy>(remote);
81 SuspendDeviceType type = SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY;
82 suspendCallbackProxy->OnTakeOverSuspend(type);
83 #endif
84 sptr<IRemoteObject> token = new RunningLockTokenStub();
85 RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
86 sptrProxy->CreateRunningLockIpc(token, info, powerError);
87 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
88 EXPECT_NE(sptrProxy->ReleaseRunningLockIpc(token, name), ERR_OK);
89 ret = sptrProxy->GetStateIpc(powerState);
90 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
91 sptrProxy->LockIpc(token, timeOutMs, powerError);
92 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
93 sptrProxy->UnLockIpc(token, name, powerError);
94 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
95 sptrProxy->IsUsedIpc(token, isUsed);
96 EXPECT_FALSE(isUsed);
97 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest001 function end!");
98 }
99
100 /**
101 * @tc.name: PowerMockObjectTest002
102 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
103 * @tc.type: FUNC
104 * @tc.require: issueI5IUHE
105 */
106 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest002, TestSize.Level2)
107 {
108 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest002 function start!");
109 pid_t uid = 0;
110 pid_t pid = 0;
111 int32_t powerError = 1;
112 int32_t powerMode = 0;
113 int32_t ret = 0;
114 std::string apiVersion = "-1";
115 bool lockTypeSupported = false;
116 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
117 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
118 PowerMode mode = PowerMode::NORMAL_MODE;
119 int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
120 SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
121 ret = sptrProxy->SuspendDeviceIpc(0, static_cast<int32_t>(abnormaltype), false, apiVersion, powerError);
122 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
123 EXPECT_NE(sptrProxy->SetDisplaySuspendIpc(true), ERR_OK);
124 ret = sptrProxy->WakeupDeviceIpc(GetTickCount(), static_cast<int32_t>(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION),
125 std::string("app call"), apiVersion, powerError);
126 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
127 int32_t attention = static_cast<int32_t>(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
128 EXPECT_NE(sptrProxy->RefreshActivityIpc(GetTickCount(), attention, true), ERR_OK);
129 ret = sptrProxy->SetDeviceModeIpc(static_cast<int32_t>(mode), powerError);
130 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
131 sptrProxy->GetDeviceModeIpc(powerMode);
132 EXPECT_FALSE(powerMode == static_cast<int32_t>(mode));
133 EXPECT_NE(sptrProxy->ProxyRunningLockIpc(true, pid, uid), ERR_OK);
134 sptrProxy->IsRunningLockTypeSupportedIpc(
135 static_cast<int32_t>(RunningLockType::RUNNINGLOCK_BACKGROUND), lockTypeSupported);
136 EXPECT_FALSE(lockTypeSupported);
137 sptrProxy->OverrideScreenOffTimeIpc(200, apiVersion, powerError);
138 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
139 sptrProxy->RestoreScreenOffTimeIpc(apiVersion, powerError);
140 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
141 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest002 function end!");
142 }
143
144 /**
145 * @tc.name: PowerMockObjectTest003
146 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
147 * @tc.type: FUNC
148 * @tc.require: issueI9C4GG
149 */
150 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest003, TestSize.Level2)
151 {
152 int32_t powerError = 1;
153 int32_t timeOutMs = -1;
154 std::string name;
155 bool isUsed = false;
156 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest003 function start!");
157 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
158 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
159 std::shared_ptr<PowerRunningLockCallbackProxy> callbackProxy =
160 std::make_shared<PowerRunningLockCallbackProxy>(remote);
161 std::string message = "runninglock message";
162 callbackProxy->HandleRunningLockMessage(message);
163 sptr<IRemoteObject> token = new RunningLockTokenStub();
164 #ifdef HAS_SENSORS_SENSOR_PART
165 RunningLockInfo info("test2", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
166 sptrProxy->CreateRunningLockIpc(token, info, powerError);
167 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
168 EXPECT_NE(sptrProxy->ReleaseRunningLockIpc(token, name), ERR_OK);
169 sptrProxy->LockIpc(token, timeOutMs, powerError);
170 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
171 sptrProxy->UnLockIpc(token, name, powerError);
172 EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
173 sptrProxy->IsUsedIpc(token, isUsed);
174 EXPECT_FALSE(isUsed);
175 #endif
176 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest003 function end!");
177 }
178
179 /**
180 * @tc.name: PowerMockObjectTest004
181 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
182 * @tc.type: FUNC
183 * @tc.require: issueI5IUHE
184 */
185 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest004, TestSize.Level2)
186 {
187 int32_t powerError = 1;
188 std::string apiVersion = "-1";
189 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest004 function start!");
190 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
191 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
192 sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
193 sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
194 sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
195 sptr<PowerMgrStubAsync> asyncCallback = new PowerMgrStubAsync();
196 sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(asyncCallback);
197 EXPECT_NE(sptrProxy->RegisterPowerStateCallbackIpc(cb1, true), ERR_OK);
198 EXPECT_NE(sptrProxy->UnRegisterPowerStateCallbackIpc(cb1), ERR_OK);
199 EXPECT_NE(sptrProxy->RegisterPowerStateCallbackIpc(nullptr, true), ERR_OK);
200 EXPECT_NE(sptrProxy->UnRegisterPowerStateCallbackIpc(nullptr), ERR_OK);
201 EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(cb3), ERR_OK);
202 EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(cb3), ERR_OK);
203 EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(nullptr), ERR_OK);
204 EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(nullptr), ERR_OK);
205 EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(cb5), ERR_OK);
206 EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(cb5), ERR_OK);
207 EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(nullptr), ERR_OK);
208 EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(nullptr), ERR_OK);
209 sptrProxy->RebootDeviceIpc(" ", powerError);
210 sptrProxy->RebootDeviceForDeprecatedIpc(" ", powerError);
211 sptrProxy->ShutDownDeviceIpc(" ", powerError);
212 int32_t ret = sptrProxy->ForceSuspendDeviceIpc(0, apiVersion, powerProxy);
213 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
214 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest004 function end!");
215 }
216 } // namespace