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_peer_test.h"
17 
18 #include <datetime_ex.h>
19 
20 #include "mock_power_remote_object.h"
21 #include "parcel.h"
22 #include "power_log.h"
23 #include "power_mgr_client.h"
24 #include "power_mgr_proxy.h"
25 #include "power_mode_callback_proxy.h"
26 #include "power_state_callback_proxy.h"
27 #include "power_runninglock_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 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36 
OnPowerModeChanged(PowerMode mode)37 void MockPeerTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
38 {
39     POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
40 }
41 
OnPowerStateChanged(PowerState state)42 void MockPeerTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
43 {
44     POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
45 }
46 
HandleRunningLockMessage(std::string message)47 void MockPeerTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
48 {
49     POWER_HILOGD(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
50 }
51 namespace {
52 /**
53  * @tc.name: PowerClientMockPeerTest001
54  * @tc.desc: Test Power client by mock peer, callback is nullptr
55  * @tc.type: FUNC
56  * @tc.require: issueI650CX
57  */
58 HWTEST_F(MockPeerTest, PowerClientMockPeerTest001, TestSize.Level2)
59 {
60     auto& powerMgrClient = PowerMgrClient::GetInstance();
61     sptr<IPowerStateCallback> stateCallback = nullptr;
62     sptr<IPowerModeCallback> modeCallback= nullptr;
63     sptr<IPowerRunninglockCallback> runninglockCallback = nullptr;
64 
65     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
66     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
67     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
68     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
69     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
70     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
71 }
72 
73 /**
74  * @tc.name: PowerClientMockPeerTest002
75  * @tc.desc: Test Power client by mock peer, callback is not nullptr
76  * @tc.type: FUNC
77  * @tc.require: issueI5IUHE
78  */
79 HWTEST_F(MockPeerTest, PowerClientMockPeerTest002, TestSize.Level2)
80 {
81     auto& powerMgrClient = PowerMgrClient::GetInstance();
82     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
83     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
84     sptr<IPowerRunninglockCallback> runninglockCallback = new PowerRunningLockTestCallback();
85 
86     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
87     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
88     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
89     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
90     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
91     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
92 }
93 
94 /**
95  * @tc.name: MockPeerTest001
96  * @tc.desc: Test proxy when the PeerHolder is nullptr
97  * @tc.type: FUNC
98  * @tc.require: issueI5IUHE
99  */
100 HWTEST_F(MockPeerTest, MockPeerTest001, TestSize.Level2)
101 {
102     pid_t uid = 0;
103     pid_t pid = 0;
104     sptr<IPCObjectStub> remote = new IPCObjectStub();
105     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
106     sptr<IRemoteObject> token = new RunningLockTokenStub();
107     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
108     EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
109     EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
110     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
111     EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
112     EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
113     EXPECT_FALSE(sptrProxy->IsUsed(token));
114     EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
115     EXPECT_FALSE(sptrProxy->ProxyRunningLocks(true, {std::make_pair(pid, uid)}));
116     EXPECT_FALSE(sptrProxy->ResetRunningLocks());
117 }
118 
119 /**
120  * @tc.name: MockPeerTest002
121  * @tc.desc: Test proxy when the PeerHolder is nullptr
122  * @tc.type: FUNC
123  * @tc.require: issueI5IUHE
124  */
125 HWTEST_F(MockPeerTest, MockPeerTest002, TestSize.Level2)
126 {
127     sptr<IPCObjectStub> remote = new IPCObjectStub();
128     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
129     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
130     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
131     EXPECT_EQ(sptrProxy->SuspendDevice(0, abnormaltype, false), PowerErrors::ERR_CONNECTION_FAIL);
132     auto error =
133         sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
134     EXPECT_EQ(error, PowerErrors::ERR_CONNECTION_FAIL);
135     EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
136     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200) == PowerErrors::ERR_OK);
137     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
138     auto state = sptrProxy->GetState();
139     EXPECT_EQ(state, PowerState::UNKNOWN);
140     EXPECT_FALSE(sptrProxy->IsScreenOn());
141     sptrProxy->SetDisplaySuspend(true);
142     PowerMode mode1 = PowerMode::NORMAL_MODE;
143     EXPECT_EQ(sptrProxy->SetDeviceMode(mode1), PowerErrors::ERR_CONNECTION_FAIL);
144     auto mode2 = sptrProxy->GetDeviceMode();
145     EXPECT_FALSE(mode2 == mode1);
146 }
147 
148 /**
149  * @tc.name: MockPeerTest003
150  * @tc.desc: Test proxy when the PeerHolder is nullptr
151  * @tc.type: FUNC
152  * @tc.require: issueI5IUHE
153  */
154 HWTEST_F(MockPeerTest, MockPeerTest003, TestSize.Level2)
155 {
156     sptr<IPCObjectStub> remote = new IPCObjectStub();
157     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
158     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
159     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
160     sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
161     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
162     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
163     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
164     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
165     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
166     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
167     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
168     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
169     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
170     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
171     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
172     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
173     EXPECT_EQ(sptrProxy->RebootDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
174     EXPECT_EQ(sptrProxy->ShutDownDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
175     EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0) == PowerErrors::ERR_OK);
176     static std::vector<std::string> dumpArgs;
177     dumpArgs.push_back("-a");
178     std::string errorCode = "remote error";
179     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
180     EXPECT_EQ(errorCode, actualDebugInfo);
181 }
182 } // namespace