• 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 
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_HILOGI(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     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest001 start.");
64     auto& powerMgrClient = PowerMgrClient::GetInstance();
65     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
66     EXPECT_FALSE(powerMgrClient.IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_SCREEN));
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.RebootDeviceForDeprecated(" ");
75     powerMgrClient.ShutDownDevice(" ");
76     powerMgrClient.ForceSuspendDevice();
77     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
78     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
79     powerMgrClient.SuspendDevice(abnormaltype, false);
80     EXPECT_FALSE(powerMgrClient.OverrideScreenOffTime(100) == PowerErrors::ERR_OK);
81     EXPECT_FALSE(powerMgrClient.RestoreScreenOffTime() == PowerErrors::ERR_OK);
82     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest001 end.");
83 }
84 
85 /**
86  * @tc.name: PowerMockParcelTest002
87  * @tc.desc: Test Power Client when the Parcel is mock
88  * @tc.type: FUNC
89  * @tc.require: issueI5IUHE
90  */
91 HWTEST_F(MockParcelTest, PowerMockParcelTest002, TestSize.Level2)
92 {
93     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest002 start.");
94     auto& powerMgrClient = PowerMgrClient::GetInstance();
95     PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
96     powerMgrClient.SetDeviceMode(mode1);
97     powerMgrClient.GetDeviceMode();
98     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
99     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
100     sptr<IPowerRunninglockCallback> runninglockCallback = new PowerRunningLockTestCallback();
101 
102     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
103     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
104     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
105     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
106     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
107     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
108     static std::vector<std::string> dumpArgs;
109     dumpArgs.push_back("-a");
110     std::string errCode = "can't connect service";
111     std::string actualDebugInfo = powerMgrClient.Dump(dumpArgs);
112     EXPECT_EQ(actualDebugInfo, errCode);
113     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest002 end.");
114 }
115 
116 /**
117  * @tc.name: PowerMockParcelTest003
118  * @tc.desc: Test lock info when the Parcel is mock
119  * @tc.type: FUNC
120  * @tc.require: issueI5IUHE
121  */
122 HWTEST_F(MockParcelTest, PowerMockParcelTest003, TestSize.Level2)
123 {
124     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest003 start.");
125     Parcel parcel;
126     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND);
127     EXPECT_FALSE(info.ReadFromParcel(parcel));
128     EXPECT_FALSE(info.Marshalling(parcel));
129     EXPECT_FALSE(info.Unmarshalling(parcel));
130     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest003 end.");
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     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest004 start.");
142     pid_t uid = 0;
143     pid_t pid = 0;
144     sptr<IPCObjectStub> remote = new IPCObjectStub();
145     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
146     sptr<IRemoteObject> token = new RunningLockTokenStub();
147     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
148     sptrProxy->CreateRunningLock(token, info);
149     sptrProxy->ReleaseRunningLock(token);
150     sptrProxy->ProxyRunningLock(true, pid, uid);
151     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
152     sptrProxy->Lock(token);
153     sptrProxy->UnLock(token);
154     EXPECT_FALSE(sptrProxy->IsUsed(token));
155     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest004 end.");
156 }
157 
158 /**
159  * @tc.name: PowerMockParcelTest005
160  * @tc.desc: Test proxy when the parcel is mock
161  * @tc.type: FUNC
162  * @tc.require: issueI5IUHE
163  */
164 HWTEST_F(MockParcelTest, PowerMockParcelTest005, TestSize.Level2)
165 {
166     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest005 start.");
167     bool ret = 0;
168     sptr<IPCObjectStub> remote = new IPCObjectStub();
169     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
170     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
171     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
172     sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
173     ret = sptrProxy->RegisterPowerStateCallback(cb1);
174     EXPECT_EQ(ret, false);
175     sptrProxy->UnRegisterPowerStateCallback(cb1);
176     sptrProxy->RegisterPowerStateCallback(nullptr);
177     sptrProxy->UnRegisterPowerStateCallback(nullptr);
178     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
179     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
180     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
181     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
182     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
183     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
184     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
185     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
186     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest005 end.");
187 }
188 
189 /**
190  * @tc.name: PowerMockParcelTest006
191  * @tc.desc: Test proxy when the parcel is mock
192  * @tc.type: FUNC
193  * @tc.require: issueI5IUHE
194  */
195 HWTEST_F(MockParcelTest, PowerMockParcelTest006, TestSize.Level2)
196 {
197     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest006 start.");
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) == PowerErrors::ERR_OK);
206     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
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     POWER_HILOGI(LABEL_TEST, "PowerMockParcelTest006 end.");
223 }
224 
225 /**
226  * @tc.name: PowerUtilMockParcelTest001
227  * @tc.desc: test Permission function
228  * @tc.type: FUNC
229  * @tc.require: issueI650CX
230  */
231 HWTEST_F (MockParcelTest, PowerUtilMockParcelTest001, TestSize.Level2)
232 {
233     POWER_HILOGI(LABEL_TEST, "PowerUtilMockParcelTest001 start.");
234     EXPECT_TRUE(Permission::IsSystem());
235     EXPECT_TRUE(Permission::IsPermissionGranted(""));
236     POWER_HILOGI(LABEL_TEST, "PowerUtilMockParcelTest001 end.");
237 }
238 } // namespace
239