• 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_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_HILOGI(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     POWER_HILOGI(LABEL_TEST, "PowerClientMockPeerTest001 start.");
61     auto& powerMgrClient = PowerMgrClient::GetInstance();
62     sptr<IPowerStateCallback> stateCallback = nullptr;
63     sptr<IPowerModeCallback> modeCallback= nullptr;
64     sptr<IPowerRunninglockCallback> runninglockCallback = nullptr;
65 
66     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
67     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
68     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
69     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
70     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
71     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
72     POWER_HILOGI(LABEL_TEST, "PowerClientMockPeerTest001 end.");
73 }
74 
75 /**
76  * @tc.name: PowerClientMockPeerTest002
77  * @tc.desc: Test Power client by mock peer, callback is not nullptr
78  * @tc.type: FUNC
79  * @tc.require: issueI5IUHE
80  */
81 HWTEST_F(MockPeerTest, PowerClientMockPeerTest002, TestSize.Level2)
82 {
83     POWER_HILOGI(LABEL_TEST, "PowerClientMockPeerTest002 start.");
84     auto& powerMgrClient = PowerMgrClient::GetInstance();
85     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
86     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
87     sptr<IPowerRunninglockCallback> runninglockCallback = new PowerRunningLockTestCallback();
88 
89     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
90     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
91     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
92     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
93     EXPECT_FALSE(powerMgrClient.RegisterRunningLockCallback(runninglockCallback));
94     EXPECT_FALSE(powerMgrClient.UnRegisterRunningLockCallback(runninglockCallback));
95     POWER_HILOGI(LABEL_TEST, "PowerClientMockPeerTest002 end.");
96 }
97 
98 /**
99  * @tc.name: MockPeerTest001
100  * @tc.desc: Test proxy when the PeerHolder is nullptr
101  * @tc.type: FUNC
102  * @tc.require: issueI5IUHE
103  */
104 HWTEST_F(MockPeerTest, MockPeerTest001, TestSize.Level2)
105 {
106     POWER_HILOGI(LABEL_TEST, "MockPeerTest001 start.");
107     pid_t uid = 0;
108     pid_t pid = 0;
109     sptr<IPCObjectStub> remote = new IPCObjectStub();
110     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
111     sptr<IRemoteObject> token = new RunningLockTokenStub();
112     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
113     EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
114     EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
115     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
116     EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
117     EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
118     EXPECT_FALSE(sptrProxy->IsUsed(token));
119     EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
120     EXPECT_FALSE(sptrProxy->ProxyRunningLocks(true, {std::make_pair(pid, uid)}));
121     EXPECT_FALSE(sptrProxy->ResetRunningLocks());
122     POWER_HILOGI(LABEL_TEST, "MockPeerTest001 end.");
123 }
124 
125 /**
126  * @tc.name: MockPeerTest002
127  * @tc.desc: Test proxy when the PeerHolder is nullptr
128  * @tc.type: FUNC
129  * @tc.require: issueI5IUHE
130  */
131 HWTEST_F(MockPeerTest, MockPeerTest002, TestSize.Level2)
132 {
133     POWER_HILOGI(LABEL_TEST, "MockPeerTest002 start.");
134     sptr<IPCObjectStub> remote = new IPCObjectStub();
135     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
136     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
137     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
138     EXPECT_EQ(sptrProxy->SuspendDevice(0, abnormaltype, false), PowerErrors::ERR_CONNECTION_FAIL);
139     auto error =
140         sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
141     EXPECT_EQ(error, PowerErrors::ERR_CONNECTION_FAIL);
142     EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
143     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200) == PowerErrors::ERR_OK);
144     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
145     auto state = sptrProxy->GetState();
146     EXPECT_EQ(state, PowerState::UNKNOWN);
147     EXPECT_FALSE(sptrProxy->IsScreenOn());
148     sptrProxy->SetDisplaySuspend(true);
149     PowerMode mode1 = PowerMode::NORMAL_MODE;
150     EXPECT_EQ(sptrProxy->SetDeviceMode(mode1), PowerErrors::ERR_CONNECTION_FAIL);
151     auto mode2 = sptrProxy->GetDeviceMode();
152     EXPECT_FALSE(mode2 == mode1);
153     POWER_HILOGI(LABEL_TEST, "MockPeerTest002 end.");
154 }
155 
156 /**
157  * @tc.name: MockPeerTest003
158  * @tc.desc: Test proxy when the PeerHolder is nullptr
159  * @tc.type: FUNC
160  * @tc.require: issueI5IUHE
161  */
162 HWTEST_F(MockPeerTest, MockPeerTest003, TestSize.Level2)
163 {
164     POWER_HILOGI(LABEL_TEST, "MockPeerTest003 start.");
165     sptr<IPCObjectStub> remote = new IPCObjectStub();
166     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
167     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
168     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
169     sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
170     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
171     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
172     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
173     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
174     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
175     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
176     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
177     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
178     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
179     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
180     EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
181     EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
182     EXPECT_EQ(sptrProxy->RebootDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
183     EXPECT_EQ(sptrProxy->ShutDownDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
184     EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0) == PowerErrors::ERR_OK);
185     static std::vector<std::string> dumpArgs;
186     dumpArgs.push_back("-a");
187     std::string errorCode = "remote error";
188     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
189     EXPECT_EQ(errorCode, actualDebugInfo);
190     POWER_HILOGI(LABEL_TEST, "MockPeerTest003 end.");
191 }
192 } // namespace