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