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_object_test.h"
17
18 #include <datetime_ex.h>
19
20 #include "errors.h"
21 #include "mock_power_remote_object.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 "running_lock.h"
29 #include "running_lock_info.h"
30 #include "running_lock_token_stub.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36
OnPowerModeChanged(PowerMode mode)37 void PowerMockObjectTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
38 {
39 POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
40 }
41
OnPowerStateChanged(PowerState state)42 void PowerMockObjectTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
43 {
44 POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
45 }
46
HandleRunningLockMessage(std::string message)47 void PowerMockObjectTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
48 {
49 POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
50 }
51
52 namespace {
53 /**
54 * @tc.name: PowerMockObjectTest001
55 * @tc.desc: Test Power proxy when the PowerRemoteObject is mock
56 * @tc.type: FUNC
57 * @tc.require: issueI5IUHE
58 */
59 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest001, TestSize.Level2)
60 {
61 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest001 start.");
62 PowerMode mode = PowerMode::NORMAL_MODE;
63 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
64 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
65 std::shared_ptr<PowerModeCallbackProxy> callbackProxy = std::make_shared<PowerModeCallbackProxy>(remote);
66 callbackProxy->OnPowerModeChanged(mode);
67 std::shared_ptr<PowerStateCallbackProxy> stateCallbackProxy = std::make_shared<PowerStateCallbackProxy>(remote);
68 PowerState state = PowerState::AWAKE;
69 stateCallbackProxy->OnPowerStateChanged(state);
70 sptr<IRemoteObject> token = new RunningLockTokenStub();
71 RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
72 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
73 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
74 state = sptrProxy->GetState();
75 EXPECT_EQ(state, PowerState::UNKNOWN);
76 EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
77 EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
78 EXPECT_FALSE(sptrProxy->IsUsed(token));
79 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest001 end.");
80 }
81
82 /**
83 * @tc.name: PowerMockObjectTest002
84 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
85 * @tc.type: FUNC
86 * @tc.require: issueI5IUHE
87 */
88 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest002, TestSize.Level2)
89 {
90 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest002 start.");
91 pid_t uid = 0;
92 pid_t pid = 0;
93 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
94 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
95 PowerMode mode = PowerMode::NORMAL_MODE;
96 int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
97 SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
98 auto error1 = sptrProxy->SuspendDevice(0, abnormaltype, false);
99 EXPECT_EQ(error1, PowerErrors::ERR_CONNECTION_FAIL);
100 EXPECT_FALSE(sptrProxy->SetDisplaySuspend(true));
101 auto error2 =
102 sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
103 EXPECT_EQ(error2, PowerErrors::ERR_CONNECTION_FAIL);
104 EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
105 EXPECT_EQ(sptrProxy->SetDeviceMode(mode), PowerErrors::ERR_CONNECTION_FAIL);
106 auto ret = sptrProxy->GetDeviceMode();
107 EXPECT_TRUE(ret == mode);
108 EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
109 EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BACKGROUND));
110 EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200) == PowerErrors::ERR_OK);
111 EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
112 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest002 end.");
113 }
114
115 /**
116 * @tc.name: PowerMockObjectTest003
117 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
118 * @tc.type: FUNC
119 * @tc.require: issueI9C4GG
120 */
121 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest003, TestSize.Level2)
122 {
123 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest003 start.");
124 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
125 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
126 std::shared_ptr<PowerRunningLockCallbackProxy> callbackProxy =
127 std::make_shared<PowerRunningLockCallbackProxy>(remote);
128 std::string message = "runninglock message";
129 callbackProxy->HandleRunningLockMessage(message);
130 sptr<IRemoteObject> token = new RunningLockTokenStub();
131 #ifdef HAS_SENSORS_SENSOR_PART
132 RunningLockInfo info("test2", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
133 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
134 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
135 EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
136 EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
137 EXPECT_FALSE(sptrProxy->IsUsed(token));
138 #endif
139 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest003 end.");
140 }
141
142 /**
143 * @tc.name: PowerMockObjectTest004
144 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
145 * @tc.type: FUNC
146 * @tc.require: issueI5IUHE
147 */
148 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest004, TestSize.Level2)
149 {
150 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest004 start.");
151 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
152 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
153 sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
154 sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
155 sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
156 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
157 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
158 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
159 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
160 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
161 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
162 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
163 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
164 EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
165 EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
166 EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
167 EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
168 sptrProxy->RebootDevice(" ");
169 sptrProxy->RebootDeviceForDeprecated(" ");
170 sptrProxy->ShutDownDevice(" ");
171 EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0) == PowerErrors::ERR_OK);
172 POWER_HILOGI(LABEL_TEST, "PowerMockObjectTest004 end.");
173 }
174 } // namespace