• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "work_trigger.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::PowerMgr;
35 using namespace OHOS;
36 using namespace std;
37 
OnPowerModeChanged(PowerMode mode)38 void MockPeerTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
39 {
40     POWER_HILOGD(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
41 }
42 
ShutdownCallback()43 void MockPeerTest::PowerShutdownTestCallback::ShutdownCallback()
44 {
45     POWER_HILOGD(LABEL_TEST, "PowerShutdownTestCallback::ShutdownCallback.");
46 }
47 
OnPowerStateChanged(PowerState state)48 void MockPeerTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
49 {
50     POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
51 }
52 
53 namespace {
54 /**
55  * @tc.name: PowerClientMockPeerTest001
56  * @tc.desc: Test Power client by mock peer, callback is nullptr
57  * @tc.type: FUNC
58  * @tc.require: issueI650CX
59  */
60 HWTEST_F(MockPeerTest, PowerClientMockPeerTest001, TestSize.Level2)
61 {
62     auto& powerMgrClient = PowerMgrClient::GetInstance();
63     sptr<IPowerStateCallback> stateCallback = nullptr;
64     sptr<IShutdownCallback> shutdownCallback = nullptr;
65     sptr<IPowerModeCallback> modeCallback= nullptr;
66 
67     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
68     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
69     EXPECT_FALSE(powerMgrClient.RegisterShutdownCallback(shutdownCallback));
70     EXPECT_FALSE(powerMgrClient.UnRegisterShutdownCallback(shutdownCallback));
71     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
72     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
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     auto& powerMgrClient = PowerMgrClient::GetInstance();
84     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
85     sptr<IShutdownCallback> shutdownCallback = new PowerShutdownTestCallback();
86     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
87 
88     EXPECT_FALSE(powerMgrClient.RegisterPowerStateCallback(stateCallback));
89     EXPECT_FALSE(powerMgrClient.UnRegisterPowerStateCallback(stateCallback));
90     EXPECT_FALSE(powerMgrClient.RegisterShutdownCallback(shutdownCallback));
91     EXPECT_FALSE(powerMgrClient.UnRegisterShutdownCallback(shutdownCallback));
92     EXPECT_FALSE(powerMgrClient.RegisterPowerModeCallback(modeCallback));
93     EXPECT_FALSE(powerMgrClient.UnRegisterPowerModeCallback(modeCallback));
94 }
95 
96 /**
97  * @tc.name: MockPeerTest001
98  * @tc.desc: Test proxy when the PeerHolder is nullptr
99  * @tc.type: FUNC
100  * @tc.require: issueI5IUHE
101  */
102 HWTEST_F(MockPeerTest, MockPeerTest001, TestSize.Level2)
103 {
104     pid_t uid = 0;
105     pid_t pid = 0;
106     sptr<IPCObjectStub> remote = new IPCObjectStub();
107     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
108     sptr<IRemoteObject> token = new RunningLockTokenStub();
109     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
110     EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
111     EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
112     auto type = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
113     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(type));
114     EXPECT_FALSE(sptrProxy->Lock(token, info, 0));
115     EXPECT_FALSE(sptrProxy->UnLock(token));
116     EXPECT_FALSE(sptrProxy->IsUsed(token));
117     WorkTriggerList worklist;
118     worklist.push_back(nullptr);
119     worklist.push_back(nullptr);
120     worklist.push_back(nullptr);
121     EXPECT_FALSE(sptrProxy->SetWorkTriggerList(token, worklist));
122     EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, uid, pid));
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     sptr<IPCObjectStub> remote = new IPCObjectStub();
134     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
135     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
136     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
137     EXPECT_EQ(sptrProxy->SuspendDevice(0, abnormaltype, false), PowerErrors::ERR_CONNECTION_FAIL);
138     auto error =
139         sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
140     EXPECT_EQ(error, PowerErrors::ERR_CONNECTION_FAIL);
141     EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
142     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
143     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());
144     auto state = sptrProxy->GetState();
145     EXPECT_EQ(state, PowerState::UNKNOWN);
146     EXPECT_FALSE(sptrProxy->IsScreenOn());
147     sptrProxy->SetDisplaySuspend(true);
148     PowerMode mode1 = PowerMode::NORMAL_MODE;
149     EXPECT_EQ(sptrProxy->SetDeviceMode(mode1), PowerErrors::ERR_CONNECTION_FAIL);
150     auto mode2 = sptrProxy->GetDeviceMode();
151     EXPECT_FALSE(mode2 == mode1);
152 }
153 
154 /**
155  * @tc.name: MockPeerTest003
156  * @tc.desc: Test proxy when the PeerHolder is nullptr
157  * @tc.type: FUNC
158  * @tc.require: issueI5IUHE
159  */
160 HWTEST_F(MockPeerTest, MockPeerTest003, TestSize.Level2)
161 {
162     sptr<IPCObjectStub> remote = new IPCObjectStub();
163     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
164     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
165     sptr<IShutdownCallback> cb2 = new PowerShutdownTestCallback();
166     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
167     auto priority = IShutdownCallback::ShutdownPriority::POWER_SHUTDOWN_PRIORITY_LOW;
168     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
169     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
170     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
171     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
172     EXPECT_FALSE(sptrProxy->RegisterShutdownCallback(priority, cb2));
173     EXPECT_FALSE(sptrProxy->UnRegisterShutdownCallback(cb2));
174     EXPECT_FALSE(sptrProxy->RegisterShutdownCallback(priority, nullptr));
175     EXPECT_FALSE(sptrProxy->UnRegisterShutdownCallback(nullptr));
176     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
177     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
178     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
179     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
180     EXPECT_EQ(sptrProxy->RebootDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
181     EXPECT_EQ(sptrProxy->ShutDownDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
182     EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0));
183     static std::vector<std::string> dumpArgs;
184     dumpArgs.push_back("-a");
185     std::string errorCode = "remote error";
186     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
187     EXPECT_EQ(errorCode, actualDebugInfo);
188 }
189 } // namespace