• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_state_machine_info.h"
29 #include "running_lock.h"
30 #include "running_lock_info.h"
31 #include "running_lock_token_stub.h"
32 #include "work_trigger.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 MockParcelTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
40 {
41     POWER_HILOGD(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
42 }
43 
ShutdownCallback()44 void MockParcelTest::PowerShutdownTestCallback::ShutdownCallback()
45 {
46     POWER_HILOGD(LABEL_TEST, "PowerShutdownTestCallback::ShutdownCallback.");
47 }
48 
OnPowerStateChanged(PowerState state)49 void MockParcelTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
50 {
51     POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
52 }
53 namespace {
54 /**
55  * @tc.name: PowerMockParcelTest001
56  * @tc.desc: Test Power Client when the Parcel is mock
57  * @tc.type: FUNC
58  * @tc.require: issueI5IUHE
59  */
60 HWTEST_F(MockParcelTest, PowerMockParcelTest001, TestSize.Level2)
61 {
62     auto& powerMgrClient = PowerMgrClient::GetInstance();
63     auto type1 = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
64     auto type2 = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_SCREEN);
65     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(type1));
66     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(type2));
67     EXPECT_FALSE(powerMgrClient.IsScreenOn());
68     powerMgrClient.SetDisplaySuspend(true);
69     powerMgrClient.WakeupDevice();
70     powerMgrClient.RefreshActivity();
71     powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
72     powerMgrClient.GetState();
73     powerMgrClient.RebootDevice(" ");
74     powerMgrClient.ShutDownDevice(" ");
75     powerMgrClient.ForceSuspendDevice();
76     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
77     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
78     powerMgrClient.SuspendDevice(abnormaltype, false);
79     EXPECT_FALSE(powerMgrClient.OverrideScreenOffTime(100));
80     EXPECT_FALSE(powerMgrClient.RestoreScreenOffTime());
81 }
82 
83 /**
84  * @tc.name: PowerMockParcelTest002
85  * @tc.desc: Test Power Client when the Parcel is mock
86  * @tc.type: FUNC
87  * @tc.require: issueI5IUHE
88  */
89 HWTEST_F(MockParcelTest, PowerMockParcelTest002, TestSize.Level2)
90 {
91     auto& powerMgrClient = PowerMgrClient::GetInstance();
92     PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
93     powerMgrClient.SetDeviceMode(mode1);
94     powerMgrClient.GetDeviceMode();
95     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
96     sptr<IShutdownCallback> shutdownCallback = new PowerShutdownTestCallback();
97     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
98 
99     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
100     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
101     EXPECT_FALSE(powerMgrClient.RegisterShutdownCallback(shutdownCallback));
102     EXPECT_FALSE(powerMgrClient.UnRegisterShutdownCallback(shutdownCallback));
103     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
104     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
105     static std::vector<std::string> dumpArgs;
106     dumpArgs.push_back("-a");
107     std::string errCode = "can't connect service";
108     std::string actualDebugInfo = powerMgrClient.Dump(dumpArgs);
109     EXPECT_EQ(actualDebugInfo, errCode);
110 }
111 
112 /**
113  * @tc.name: PowerMockParcelTest003
114  * @tc.desc: Test lock info when the Parcel is mock
115  * @tc.type: FUNC
116  * @tc.require: issueI5IUHE
117  */
118 HWTEST_F(MockParcelTest, PowerMockParcelTest003, TestSize.Level2)
119 {
120     Parcel parcel;
121     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND);
122     EXPECT_FALSE(info.ReadFromParcelWorkTriggerList(parcel, info.workTriggerlist));
123     EXPECT_FALSE(info.MarshallingWorkTriggerList(parcel, info.workTriggerlist));
124     EXPECT_FALSE(info.ReadFromParcel(parcel));
125     EXPECT_FALSE(info.Marshalling(parcel));
126     EXPECT_FALSE(info.Unmarshalling(parcel));
127     WorkTrigger* workTrigger = new WorkTrigger();
128     EXPECT_FALSE(workTrigger->ReadFromParcel(parcel));
129     EXPECT_FALSE(workTrigger->Marshalling(parcel));
130     EXPECT_FALSE(workTrigger->Unmarshalling(parcel));
131 }
132 
133 /**
134  * @tc.name: PowerMockParcelTest004
135  * @tc.desc: Test proxy when the parcel is mock
136  * @tc.type: FUNC
137  * @tc.require: issueI5IUHE
138  */
139 HWTEST_F(MockParcelTest, PowerMockParcelTest004, TestSize.Level2)
140 {
141     pid_t uid = 0;
142     pid_t pid = 0;
143     sptr<IPCObjectStub> remote = new IPCObjectStub();
144     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
145     sptr<IRemoteObject> token = new RunningLockTokenStub();
146     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
147     sptrProxy->CreateRunningLock(token, info);
148     sptrProxy->ReleaseRunningLock(token);
149     WorkTriggerList worklist;
150     worklist.push_back(nullptr);
151     worklist.push_back(nullptr);
152     worklist.push_back(nullptr);
153     sptrProxy->SetWorkTriggerList(token, worklist);
154     sptrProxy->ProxyRunningLock(true, uid, pid);
155     auto type = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
156     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(type));
157     sptrProxy->Lock(token, info, 0);
158     sptrProxy->UnLock(token);
159     EXPECT_FALSE(sptrProxy->IsUsed(token));
160 }
161 
162 /**
163  * @tc.name: PowerMockParcelTest005
164  * @tc.desc: Test proxy when the parcel is mock
165  * @tc.type: FUNC
166  * @tc.require: issueI5IUHE
167  */
168 HWTEST_F(MockParcelTest, PowerMockParcelTest005, TestSize.Level2)
169 {
170     sptr<IPCObjectStub> remote = new IPCObjectStub();
171     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
172     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
173     sptr<IShutdownCallback> cb2 = new PowerShutdownTestCallback();
174     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
175     auto priority = IShutdownCallback::ShutdownPriority::POWER_SHUTDOWN_PRIORITY_LOW;
176     sptrProxy->RegisterPowerStateCallback(cb1);
177     sptrProxy->UnRegisterPowerStateCallback(cb1);
178     sptrProxy->RegisterPowerStateCallback(nullptr);
179     sptrProxy->UnRegisterPowerStateCallback(nullptr);
180     sptrProxy->RegisterShutdownCallback(priority, cb2);
181     sptrProxy->UnRegisterShutdownCallback(cb2);
182     sptrProxy->RegisterShutdownCallback(priority, nullptr);
183     sptrProxy->UnRegisterShutdownCallback(nullptr);
184     sptrProxy->RegisterPowerModeCallback(cb3);
185     sptrProxy->UnRegisterPowerModeCallback(cb3);
186     sptrProxy->RegisterPowerModeCallback(nullptr);
187     sptrProxy->UnRegisterPowerModeCallback(nullptr);
188 }
189 
190 /**
191  * @tc.name: PowerMockParcelTest006
192  * @tc.desc: Test proxy when the parcel is mock
193  * @tc.type: FUNC
194  * @tc.require: issueI5IUHE
195  */
196 HWTEST_F(MockParcelTest, PowerMockParcelTest006, TestSize.Level2)
197 {
198     sptr<IPCObjectStub> remote = new IPCObjectStub();
199     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
200     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
201     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
202     sptrProxy->SuspendDevice(0, abnormaltype, false);
203     sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
204     sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true);
205     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
206     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());
207     auto state = sptrProxy->GetState();
208     EXPECT_EQ(state, PowerState::UNKNOWN);
209     EXPECT_FALSE(sptrProxy->IsScreenOn());
210     sptrProxy->SetDisplaySuspend(true);
211     PowerMode mode1 = PowerMode::NORMAL_MODE;
212     sptrProxy->SetDeviceMode(mode1);
213     sptrProxy->GetDeviceMode();
214     sptrProxy->RebootDevice(" ");
215     sptrProxy->ShutDownDevice(" ");
216     sptrProxy->ForceSuspendDevice(0);
217     static std::vector<std::string> dumpArgs;
218     dumpArgs.push_back("-a");
219     std::string errCode = "remote error";
220     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
221     EXPECT_EQ(actualDebugInfo, errCode);
222 }
223 
224 /**
225  * @tc.name: PowerUtilMockParcelTest001
226  * @tc.desc: test Permission function
227  * @tc.type: FUNC
228  * @tc.require: issueI650CX
229  */
230 HWTEST_F (MockParcelTest, PowerUtilMockParcelTest001, TestSize.Level2)
231 {
232     EXPECT_FALSE(Permission::IsSystemCore());
233     EXPECT_FALSE(Permission::IsSystemBasic());
234     EXPECT_FALSE(Permission::IsSystemApl());
235     EXPECT_TRUE(Permission::IsSystemHap());
236     EXPECT_TRUE(Permission::IsSystem());
237     EXPECT_TRUE(Permission::IsPermissionGranted(""));
238     EXPECT_TRUE(Permission::IsSystemHapPermGranted(""));
239 }
240 } // namespace
241