1 /*
2 * Copyright (c) 2025 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_service_ipc_adapter_test.h"
17 #include "ipower_mgr.h"
18 #include "iremote_object.h"
19 #include "power_log.h"
20 #include "power_mgr_service_ipc_adapter.h"
21 #include "power_mgr_stub.h"
22 #include "sp_singleton.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27
28 class TestPowerMgrServiceAdapter : public PowerMgrServiceAdapter {
29 public:
RebootDevice(const std::string & reason)30 PowerErrors RebootDevice(const std::string& reason)
31 {
32 return PowerErrors::ERR_OK;
33 }
RebootDeviceForDeprecated(const std::string & reason)34 PowerErrors RebootDeviceForDeprecated(const std::string& reason)
35 {
36 return PowerErrors::ERR_OK;
37 }
ShutDownDevice(const std::string & reason)38 PowerErrors ShutDownDevice(const std::string& reason)
39 {
40 return PowerErrors::ERR_OK;
41 }
SetSuspendTag(const std::string & tag)42 PowerErrors SetSuspendTag(const std::string& tag)
43 {
44 return PowerErrors::ERR_OK;
45 }
SuspendDevice(int64_t callTimeMs,SuspendDeviceType reason,bool suspendImmed,const std::string & apiVersion="-1")46 PowerErrors SuspendDevice(
47 int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion = "-1")
48 {
49 return PowerErrors::ERR_OK;
50 }
WakeupDevice(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details,const std::string & apiVersion="-1")51 PowerErrors WakeupDevice(
52 int64_t callTimeMs, WakeupDeviceType reason, const std::string& details, const std::string& apiVersion = "-1")
53 {
54 return PowerErrors::ERR_OK;
55 }
WakeupDeviceAsync(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details)56 void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) {}
RefreshActivity(int64_t callTimeMs,UserActivityType type,bool needChangeBacklight)57 bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight)
58 {
59 return true;
60 }
OverrideScreenOffTime(int64_t timeout,const std::string & apiVersion="-1")61 PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1")
62 {
63 return PowerErrors::ERR_OK;
64 }
RestoreScreenOffTime(const std::string & apiVersion="-1")65 PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1")
66 {
67 return PowerErrors::ERR_OK;
68 }
GetState()69 PowerState GetState()
70 {
71 return PowerState::AWAKE;
72 }
IsScreenOn(bool needPrintLog=true)73 bool IsScreenOn(bool needPrintLog = true)
74 {
75 return true;
76 }
IsFoldScreenOn()77 bool IsFoldScreenOn()
78 {
79 return true;
80 }
IsCollaborationScreenOn()81 bool IsCollaborationScreenOn()
82 {
83 return true;
84 }
IsForceSleeping()85 bool IsForceSleeping()
86 {
87 return true;
88 }
ForceSuspendDevice(int64_t callTimeMs,const std::string & apiVersion="-1")89 PowerErrors ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion = "-1")
90 {
91 return PowerErrors::ERR_OK;
92 }
Hibernate(bool clearMemory,const std::string & reason="",const std::string & apiVersion="-1")93 PowerErrors Hibernate(bool clearMemory, const std::string& reason = "", const std::string& apiVersion = "-1")
94 {
95 return PowerErrors::ERR_OK;
96 }
CreateRunningLock(const sptr<IRemoteObject> & remoteObj,const RunningLockInfo & runningLockInfo)97 PowerErrors CreateRunningLock(const sptr<IRemoteObject>& remoteObj, const RunningLockInfo& runningLockInfo)
98 {
99 return PowerErrors::ERR_OK;
100 }
ReleaseRunningLock(const sptr<IRemoteObject> & remoteObj,const std::string & name="")101 bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj, const std::string& name = "")
102 {
103 return true;
104 }
IsRunningLockTypeSupported(RunningLockType type)105 bool IsRunningLockTypeSupported(RunningLockType type)
106 {
107 return true;
108 }
UpdateWorkSource(const sptr<IRemoteObject> & remoteObj,const std::vector<int32_t> & workSources)109 bool UpdateWorkSource(const sptr<IRemoteObject>& remoteObj, const std::vector<int32_t>& workSources)
110 {
111 return true;
112 }
Lock(const sptr<IRemoteObject> & remoteObj,int32_t timeOutMs=-1)113 PowerErrors Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs = -1)
114 {
115 return PowerErrors::ERR_OK;
116 }
UnLock(const sptr<IRemoteObject> & remoteObj,const std::string & name="")117 PowerErrors UnLock(const sptr<IRemoteObject>& remoteObj, const std::string& name = "")
118 {
119 return PowerErrors::ERR_OK;
120 }
QueryRunningLockLists(std::map<std::string,RunningLockInfo> & runningLockLists)121 bool QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)
122 {
123 return true;
124 }
IsUsed(const sptr<IRemoteObject> & remoteObj)125 bool IsUsed(const sptr<IRemoteObject>& remoteObj)
126 {
127 return true;
128 }
ProxyRunningLock(bool isProxied,pid_t pid,pid_t uid)129 bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
130 {
131 return true;
132 }
ProxyRunningLocks(bool isProxied,const std::vector<std::pair<pid_t,pid_t>> & processInfos)133 bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
134 {
135 return true;
136 }
ResetRunningLocks()137 bool ResetRunningLocks()
138 {
139 return true;
140 }
RegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback,bool isSync=true)141 bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true)
142 {
143 return true;
144 }
UnRegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)145 bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
146 {
147 return true;
148 }
149
RegisterSyncSleepCallback(const sptr<ISyncSleepCallback> & callback,SleepPriority priority)150 bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority)
151 {
152 return true;
153 }
UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback> & callback)154 bool UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback)
155 {
156 return true;
157 }
RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & callback)158 bool RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
159 {
160 return true;
161 }
UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & callback)162 bool UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
163 {
164 return true;
165 }
RegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)166 bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
167 {
168 return true;
169 }
UnRegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)170 bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
171 {
172 return true;
173 }
174
RegisterScreenStateCallback(int32_t remainTime,const sptr<IScreenOffPreCallback> & callback)175 bool RegisterScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback)
176 {
177 return true;
178 }
UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback> & callback)179 bool UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback>& callback)
180 {
181 return true;
182 }
183
RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)184 bool RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
185 {
186 return true;
187 }
UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)188 bool UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
189 {
190 return true;
191 }
SetDisplaySuspend(bool enable)192 bool SetDisplaySuspend(bool enable)
193 {
194 return true;
195 }
SetDeviceMode(const PowerMode & mode)196 PowerErrors SetDeviceMode(const PowerMode& mode)
197 {
198 return PowerErrors::ERR_OK;
199 }
GetDeviceMode()200 PowerMode GetDeviceMode()
201 {
202 return PowerMode::NORMAL_MODE;
203 }
ShellDump(const std::vector<std::string> & args,uint32_t argc)204 std::string ShellDump(const std::vector<std::string>& args, uint32_t argc)
205 {
206 return "";
207 }
IsStandby(bool & isStandby)208 PowerErrors IsStandby(bool& isStandby)
209 {
210 return PowerErrors::ERR_OK;
211 }
SetForceTimingOut(bool enabled,const sptr<IRemoteObject> & token)212 PowerErrors SetForceTimingOut(bool enabled, const sptr<IRemoteObject>& token)
213 {
214 return PowerErrors::ERR_OK;
215 }
LockScreenAfterTimingOut(bool enabledLockScreen,bool checkLock,bool sendScreenOffEvent,const sptr<IRemoteObject> & token)216 PowerErrors LockScreenAfterTimingOut(
217 bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent, const sptr<IRemoteObject>& token)
218 {
219 return PowerErrors::ERR_OK;
220 }
IsRunningLockEnabled(const RunningLockType type,bool & result)221 PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result)
222 {
223 return PowerErrors::ERR_OK;
224 }
225
RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback,ShutdownPriority priority)226 void RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority) {}
UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback)227 void UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback) {}
228
RegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback,ShutdownPriority priority)229 void RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority) {}
UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback)230 void UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback) {}
RegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback,ShutdownPriority priority)231 void RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority) {}
UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback)232 void UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback) {}
RegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback> & callback,TakeOverSuspendPriority priority)233 bool RegisterSuspendTakeoverCallback(
234 const sptr<ITakeOverSuspendCallback>& callback, TakeOverSuspendPriority priority)
235 {
236 return true;
237 }
UnRegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback> & callback)238 bool UnRegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback>& callback)
239 {
240 return true;
241 }
242 };
243
244 class TestTakeOverSuspendCallback : public ITakeOverSuspendCallback {
245 public:
246 TestTakeOverSuspendCallback() = default;
247 virtual ~TestTakeOverSuspendCallback() = default;
248
OnTakeOverSuspend(SuspendDeviceType type)249 bool OnTakeOverSuspend(SuspendDeviceType type) override
250 {
251 return false;
252 }
AsObject()253 sptr<IRemoteObject> AsObject() override
254 {
255 return nullptr;
256 }
257 };
258
SetUpTestCase(void)259 void PowerMgrServiceIpcAdapterTest::SetUpTestCase(void)
260 {
261 }
TearDownTestCase(void)262 void PowerMgrServiceIpcAdapterTest::TearDownTestCase(void) {}
SetUp()263 void PowerMgrServiceIpcAdapterTest::SetUp() {}
TearDown()264 void PowerMgrServiceIpcAdapterTest::TearDown() {}
265
266 namespace {
267 /**
268 * @tc.name: PowerMgrServiceIpcAdapter001
269 * @tc.desc: test PowerMgrServiceIpcAdapter.RegisterSuspendTakeoverCallbackIpc
270 * @tc.type: FUNC
271 */
272 HWTEST_F(PowerMgrServiceIpcAdapterTest, PowerMgrServiceIpcAdapter001, TestSize.Level2) {
273 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceIpcAdapter001 function start!");
274 sptr<TestTakeOverSuspendCallback> callback = new TestTakeOverSuspendCallback();
275 auto adapter = DelayedSpSingleton<TestPowerMgrServiceAdapter>::GetInstance();
276 TakeOverSuspendPriority priority = TakeOverSuspendPriority::HIGH;
277 int32_t result = adapter->RegisterSuspendTakeoverCallbackIpc(callback, static_cast<int32_t>(priority));
278 result = adapter->RegisterSuspendTakeoverCallbackIpc(callback, 10);
279 EXPECT_EQ(result, -1);
280 result = adapter->RegisterSuspendTakeoverCallbackIpc(nullptr, static_cast<int32_t>(priority));
281 EXPECT_EQ(result, -1);
282 sptr<TestTakeOverSuspendCallback> callback2 = new TestTakeOverSuspendCallback();
283 result = adapter->RegisterSuspendTakeoverCallbackIpc(callback2, static_cast<int32_t>(-1));
284 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceIpcAdapter001 function end!");
285 }
286
287 /**
288 * @tc.name: PowerMgrServiceIpcAdapter002
289 * @tc.desc: test PowerMgrServiceIpcAdapter.UnRegisterSuspendTakeoverCallback
290 * @tc.type: FUNC
291 */
292 HWTEST_F(PowerMgrServiceIpcAdapterTest, PowerMgrServiceIpcAdapter002, TestSize.Level2) {
293 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceIpcAdapter002 function start!");
294 sptr<TestTakeOverSuspendCallback> callback = new TestTakeOverSuspendCallback();
295 auto adapter = DelayedSpSingleton<TestPowerMgrServiceAdapter>::GetInstance();
296 adapter->UnRegisterSuspendTakeoverCallbackIpc(callback);
297 int32_t result = adapter->UnRegisterSuspendTakeoverCallbackIpc(nullptr);
298 EXPECT_EQ(result, -1);
299 POWER_HILOGI(LABEL_TEST, "PowerMgrServiceIpcAdapter002 function end!");
300 }
301 }