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_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 
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_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
42 }
43 
OnPowerStateChanged(PowerState state)44 void MockParcelTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
45 {
46     POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
47 }
48 
HandleRunningLockMessage(std::string message)49 void MockParcelTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
50 {
51     POWER_HILOGD(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
52 }
53 
54 namespace {
55 /**
56  * @tc.name: PowerMockParcelTest001
57  * @tc.desc: Test Power Client when the Parcel is mock
58  * @tc.type: FUNC
59  * @tc.require: issueI5IUHE
60  */
61 HWTEST_F(MockParcelTest, PowerMockParcelTest001, TestSize.Level2)
62 {
63     auto& powerMgrClient = PowerMgrClient::GetInstance();
64     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
65     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_SCREEN));
66     EXPECT_FALSE(powerMgrClient.IsScreenOn());
67     powerMgrClient.SetDisplaySuspend(true);
68     powerMgrClient.WakeupDevice();
69     powerMgrClient.RefreshActivity();
70     powerMgrClient.RefreshActivity(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
71     powerMgrClient.GetState();
72     powerMgrClient.RebootDevice(" ");
73     powerMgrClient.RebootDeviceForDeprecated(" ");
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) == PowerErrors::ERR_OK);
80     EXPECT_FALSE(powerMgrClient.RestoreScreenOffTime() == PowerErrors::ERR_OK);
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<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
97     sptr<IPowerRunninglockCallback> runninglockCallback = new PowerRunningLockTestCallback();
98 
99     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
100     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
101     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
102     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
103     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
104     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
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.ReadFromParcel(parcel));
123     EXPECT_FALSE(info.Marshalling(parcel));
124     EXPECT_FALSE(info.Unmarshalling(parcel));
125 }
126 
127 /**
128  * @tc.name: PowerMockParcelTest004
129  * @tc.desc: Test proxy when the parcel is mock
130  * @tc.type: FUNC
131  * @tc.require: issueI5IUHE
132  */
133 HWTEST_F(MockParcelTest, PowerMockParcelTest004, TestSize.Level2)
134 {
135     pid_t uid = 0;
136     pid_t pid = 0;
137     sptr<IPCObjectStub> remote = new IPCObjectStub();
138     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
139     sptr<IRemoteObject> token = new RunningLockTokenStub();
140     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
141     sptrProxy->CreateRunningLock(token, info);
142     sptrProxy->ReleaseRunningLock(token);
143     sptrProxy->ProxyRunningLock(true, pid, uid);
144     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
145     sptrProxy->Lock(token);
146     sptrProxy->UnLock(token);
147     EXPECT_FALSE(sptrProxy->IsUsed(token));
148 }
149 
150 /**
151  * @tc.name: PowerMockParcelTest005
152  * @tc.desc: Test proxy when the parcel is mock
153  * @tc.type: FUNC
154  * @tc.require: issueI5IUHE
155  */
156 HWTEST_F(MockParcelTest, PowerMockParcelTest005, TestSize.Level2)
157 {
158     bool ret = 0;
159     sptr<IPCObjectStub> remote = new IPCObjectStub();
160     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
161     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
162     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
163     sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
164     ret = sptrProxy->RegisterPowerStateCallback(cb1);
165     EXPECT_EQ(ret, false);
166     sptrProxy->UnRegisterPowerStateCallback(cb1);
167     sptrProxy->RegisterPowerStateCallback(nullptr);
168     sptrProxy->UnRegisterPowerStateCallback(nullptr);
169     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
170     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
171     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
172     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
173     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
174     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
175     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
176     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
177 }
178 
179 /**
180  * @tc.name: PowerMockParcelTest006
181  * @tc.desc: Test proxy when the parcel is mock
182  * @tc.type: FUNC
183  * @tc.require: issueI5IUHE
184  */
185 HWTEST_F(MockParcelTest, PowerMockParcelTest006, TestSize.Level2)
186 {
187     sptr<IPCObjectStub> remote = new IPCObjectStub();
188     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
189     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
190     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
191     sptrProxy->SuspendDevice(0, abnormaltype, false);
192     sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
193     sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true);
194     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200) == PowerErrors::ERR_OK);
195     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
196     auto state = sptrProxy->GetState();
197     EXPECT_EQ(state, PowerState::UNKNOWN);
198     EXPECT_FALSE(sptrProxy->IsScreenOn());
199     sptrProxy->SetDisplaySuspend(true);
200     PowerMode mode1 = PowerMode::NORMAL_MODE;
201     sptrProxy->SetDeviceMode(mode1);
202     sptrProxy->GetDeviceMode();
203     sptrProxy->RebootDevice(" ");
204     sptrProxy->ShutDownDevice(" ");
205     sptrProxy->ForceSuspendDevice(0);
206     static std::vector<std::string> dumpArgs;
207     dumpArgs.push_back("-a");
208     std::string errCode = "remote error";
209     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
210     EXPECT_EQ(actualDebugInfo, errCode);
211 }
212 
213 /**
214  * @tc.name: PowerUtilMockParcelTest001
215  * @tc.desc: test Permission function
216  * @tc.type: FUNC
217  * @tc.require: issueI650CX
218  */
219 HWTEST_F (MockParcelTest, PowerUtilMockParcelTest001, TestSize.Level2)
220 {
221     EXPECT_TRUE(Permission::IsSystem());
222     EXPECT_TRUE(Permission::IsPermissionGranted(""));
223 }
224 } // namespace
225