• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_parcel_test.h"
17 
18 #include <datetime_ex.h>
19 
20 #include "mock_power_remote_object.h"
21 #include "parcel.h"
22 #include "permission.h"
23 #include "power_log.h"
24 #include "power_mgr_client.h"
25 #include "power_mgr_proxy.h"
26 #include "power_mode_callback_proxy.h"
27 #include "power_state_callback_proxy.h"
28 #include "power_runninglock_callback_proxy.h"
29 #include "power_state_machine_info.h"
30 #include "running_lock.h"
31 #include "running_lock_info.h"
32 #include "running_lock_token_stub.h"
33 #include "power_mgr_async_reply_stub.h"
34 
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace std;
39 
OnPowerModeChanged(PowerMode mode)40 void MockParcelTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
41 {
42     POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
43 }
44 
OnPowerStateChanged(PowerState state)45 void MockParcelTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
46 {
47     POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
48 }
49 
HandleRunningLockMessage(std::string message)50 void MockParcelTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
51 {
52     POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
53 }
54 
55 namespace {
56 /**
57  * @tc.name: PowerMockParcelTest001
58  * @tc.desc: Test Power Client when the Parcel is mock
59  * @tc.type: FUNC
60  * @tc.require: issueI5IUHE
61  */
62 HWTEST_F(MockParcelTest, PowerMockParcelTest001, TestSize.Level2)
63 {
64     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest001 function start!");
65     auto& powerMgrClient = PowerMgrClient::GetInstance();
66     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
67     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_SCREEN));
68     EXPECT_FALSE(powerMgrClient.IsScreenOn());
69     powerMgrClient.SetDisplaySuspend(true);
70     powerMgrClient.WakeupDevice();
71     powerMgrClient.RefreshActivity();
72     powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
73     powerMgrClient.GetState();
74     powerMgrClient.RebootDevice(" ");
75     powerMgrClient.RebootDeviceForDeprecated(" ");
76     powerMgrClient.ShutDownDevice(" ");
77     powerMgrClient.ForceSuspendDevice();
78     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
79     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
80     powerMgrClient.SuspendDevice(abnormaltype, false);
81     EXPECT_FALSE(powerMgrClient.OverrideScreenOffTime(100) == PowerErrors::ERR_OK);
82     EXPECT_FALSE(powerMgrClient.RestoreScreenOffTime() == PowerErrors::ERR_OK);
83     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest001 function end!");
84 }
85 
86 /**
87  * @tc.name: PowerMockParcelTest002
88  * @tc.desc: Test Power Client when the Parcel is mock
89  * @tc.type: FUNC
90  * @tc.require: issueI5IUHE
91  */
92 HWTEST_F(MockParcelTest, PowerMockParcelTest002, TestSize.Level2)
93 {
94     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest002 function start!");
95     auto& powerMgrClient = PowerMgrClient::GetInstance();
96     PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
97     powerMgrClient.SetDeviceMode(mode1);
98     powerMgrClient.GetDeviceMode();
99     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
100     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
101     sptr<IPowerRunninglockCallback> runninglockCallback = new PowerRunningLockTestCallback();
102 
103     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
104     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
105     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
106     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
107     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
108     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
109     static std::vector<std::string> dumpArgs;
110     dumpArgs.push_back("-a");
111     std::string errCode = "can't connect service";
112     std::string actualDebugInfo = powerMgrClient.Dump(dumpArgs);
113     EXPECT_EQ(actualDebugInfo, errCode);
114     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest002 function end!");
115 }
116 
117 /**
118  * @tc.name: PowerMockParcelTest003
119  * @tc.desc: Test lock info when the Parcel is mock
120  * @tc.type: FUNC
121  * @tc.require: issueI5IUHE
122  */
123 HWTEST_F(MockParcelTest, PowerMockParcelTest003, TestSize.Level2)
124 {
125     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest003 function start!");
126     Parcel parcel;
127     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND);
128     EXPECT_FALSE(info.ReadFromParcel(parcel));
129     EXPECT_FALSE(info.Marshalling(parcel));
130     EXPECT_FALSE(info.Unmarshalling(parcel));
131     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest003 function end!");
132 }
133 
134 /**
135  * @tc.name: PowerMockParcelTest004
136  * @tc.desc: Test proxy when the parcel is mock
137  * @tc.type: FUNC
138  * @tc.require: issueI5IUHE
139  */
140 HWTEST_F(MockParcelTest, PowerMockParcelTest004, TestSize.Level2)
141 {
142     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest004 function start!");
143     pid_t uid = 0;
144     pid_t pid = 0;
145     int32_t powerError = 1;
146     int32_t timeOutMs = -1;
147     std::string name;
148     bool lockTypeSupported = false;
149     bool isUsed = false;
150     sptr<IPCObjectStub> remote = new IPCObjectStub();
151     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
152     sptr<IRemoteObject> token = new RunningLockTokenStub();
153     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
154     sptrProxy->CreateRunningLockIpc(token, info, powerError);
155     sptrProxy->ReleaseRunningLockIpc(token, name);
156     sptrProxy->ProxyRunningLockIpc(true, pid, uid);
157     sptrProxy->IsRunningLockTypeSupportedIpc(
158         static_cast<int32_t>(RunningLockType::RUNNINGLOCK_BUTT), lockTypeSupported);
159     EXPECT_FALSE(lockTypeSupported);
160     sptrProxy->LockIpc(token, timeOutMs, powerError);
161     sptrProxy->UnLockIpc(token, name, powerError);
162     sptrProxy->IsUsedIpc(token, isUsed);
163     EXPECT_FALSE(isUsed);
164     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest004 function end!");
165 }
166 
167 /**
168  * @tc.name: PowerMockParcelTest005
169  * @tc.desc: Test proxy when the parcel is mock
170  * @tc.type: FUNC
171  * @tc.require: issueI5IUHE
172  */
173 HWTEST_F(MockParcelTest, PowerMockParcelTest005, TestSize.Level2)
174 {
175     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest005 function start!");
176     int32_t ret = 0;
177     sptr<IPCObjectStub> remote = new IPCObjectStub();
178     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
179     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
180     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
181     sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
182     ret = sptrProxy->RegisterPowerStateCallbackIpc(cb1, true);
183     EXPECT_EQ(ret, ERR_INVALID_VALUE);
184     sptrProxy->UnRegisterPowerStateCallbackIpc(cb1);
185     sptrProxy->RegisterPowerStateCallbackIpc(nullptr, true);
186     sptrProxy->UnRegisterPowerStateCallbackIpc(nullptr);
187     EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(cb3), ERR_OK);
188     EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(cb3), ERR_OK);
189     EXPECT_NE(sptrProxy->RegisterPowerModeCallbackIpc(nullptr), ERR_OK);
190     EXPECT_NE(sptrProxy->UnRegisterPowerModeCallbackIpc(nullptr), ERR_OK);
191     EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(cb5), ERR_OK);
192     EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(cb5), ERR_OK);
193     EXPECT_NE(sptrProxy->RegisterRunningLockCallbackIpc(nullptr), ERR_OK);
194     EXPECT_NE(sptrProxy->UnRegisterRunningLockCallbackIpc(nullptr), ERR_OK);
195     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest005 function end!");
196 }
197 
198 /**
199  * @tc.name: PowerMockParcelTest006
200  * @tc.desc: Test proxy when the parcel is mock
201  * @tc.type: FUNC
202  * @tc.require: issueI5IUHE
203  */
204 HWTEST_F(MockParcelTest, PowerMockParcelTest006, TestSize.Level2)
205 {
206     int32_t powerError = 1;
207     int32_t powerMode = 0;
208     int32_t powerState = -1;
209     std::string apiVersion = "-1";
210     bool isScreenOn = false;
211     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest006 function start!");
212     sptr<IPCObjectStub> remote = new IPCObjectStub();
213     sptr<PowerMgrStubAsync> asyncCallback = new PowerMgrStubAsync();
214     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(asyncCallback);
215     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
216     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
217     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
218     sptrProxy->SuspendDeviceIpc(0, static_cast<int32_t>(abnormaltype), false, apiVersion, powerError);
219     sptrProxy->WakeupDeviceIpc(GetTickCount(), static_cast<int32_t>(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION),
220         std::string("app call"), apiVersion, powerError);
221     int32_t userAttention = static_cast<int32_t>(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
222     sptrProxy->RefreshActivityIpc(GetTickCount(), userAttention, true);
223     sptrProxy->OverrideScreenOffTimeIpc(200, apiVersion, powerError);
224     EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
225     sptrProxy->RestoreScreenOffTimeIpc(apiVersion, powerError);
226     EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
227     int32_t result = sptrProxy->GetStateIpc(powerState);
228     EXPECT_EQ(result, ERR_INVALID_VALUE);
229     sptrProxy->IsScreenOnIpc(true, isScreenOn);
230     EXPECT_FALSE(isScreenOn);
231     sptrProxy->SetDisplaySuspendIpc(true);
232     PowerMode mode1 = PowerMode::NORMAL_MODE;
233     sptrProxy->SetDeviceModeIpc(static_cast<int32_t>(mode1), powerError);
234     sptrProxy->GetDeviceModeIpc(powerMode);
235     sptrProxy->RebootDeviceIpc(" ", powerError);
236     sptrProxy->ShutDownDeviceIpc(" ", powerError);
237     sptrProxy->ForceSuspendDeviceIpc(0, apiVersion, powerProxy);
238     int waitTime = 100;
239     PowerErrors ret = static_cast<PowerErrors>(asyncCallback->WaitForAsyncReply(waitTime));
240     static std::vector<std::string> dumpArgs;
241     dumpArgs.push_back("-a");
242     std::string errCode;
243     std::string actualDebugInfo;
244     sptrProxy->ShellDumpIpc(dumpArgs, dumpArgs.size(), actualDebugInfo);
245     EXPECT_EQ(actualDebugInfo, errCode);
246     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest006 function end!");
247 }
248 
249 /**
250  * @tc.name: PowerUtilMockParcelTest001
251  * @tc.desc: test Permission function
252  * @tc.type: FUNC
253  * @tc.require: issueI650CX
254  */
255 HWTEST_F (MockParcelTest, PowerUtilMockParcelTest001, TestSize.Level2)
256 {
257     POWER_HILOGI(LABEL_TEST, "PowerUtilMockParcelTest001 function start!");
258     EXPECT_TRUE(Permission::IsSystem());
259     EXPECT_TRUE(Permission::IsPermissionGranted(""));
260     POWER_HILOGI(LABEL_TEST, "PowerUtilMockParcelTest001 function end!");
261 }
262 } // namespace