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_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_HILOGD(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 PowerMode mode = PowerMode::NORMAL_MODE;
62 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
63 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
64 std::shared_ptr<PowerModeCallbackProxy> callbackProxy = std::make_shared<PowerModeCallbackProxy>(remote);
65 callbackProxy->OnPowerModeChanged(mode);
66 std::shared_ptr<PowerStateCallbackProxy> stateCallbackProxy = std::make_shared<PowerStateCallbackProxy>(remote);
67 PowerState state = PowerState::AWAKE;
68 stateCallbackProxy->OnPowerStateChanged(state);
69 sptr<IRemoteObject> token = new RunningLockTokenStub();
70 RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
71 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
72 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
73 state = sptrProxy->GetState();
74 EXPECT_EQ(state, PowerState::UNKNOWN);
75 EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
76 EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
77 EXPECT_FALSE(sptrProxy->IsUsed(token));
78 }
79
80 /**
81 * @tc.name: PowerMockObjectTest002
82 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
83 * @tc.type: FUNC
84 * @tc.require: issueI5IUHE
85 */
86 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest002, TestSize.Level2)
87 {
88 pid_t uid = 0;
89 pid_t pid = 0;
90 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
91 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
92 PowerMode mode = PowerMode::NORMAL_MODE;
93 int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
94 SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
95 auto error1 = sptrProxy->SuspendDevice(0, abnormaltype, false);
96 EXPECT_EQ(error1, PowerErrors::ERR_CONNECTION_FAIL);
97 EXPECT_FALSE(sptrProxy->SetDisplaySuspend(true));
98 auto error2 =
99 sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
100 EXPECT_EQ(error2, PowerErrors::ERR_CONNECTION_FAIL);
101 EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
102 EXPECT_EQ(sptrProxy->SetDeviceMode(mode), PowerErrors::ERR_CONNECTION_FAIL);
103 auto ret = sptrProxy->GetDeviceMode();
104 EXPECT_TRUE(ret == mode);
105 EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
106 EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BACKGROUND));
107 EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200) == PowerErrors::ERR_OK);
108 EXPECT_FALSE(sptrProxy->RestoreScreenOffTime() == PowerErrors::ERR_OK);
109 }
110
111 /**
112 * @tc.name: PowerMockObjectTest003
113 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
114 * @tc.type: FUNC
115 * @tc.require: issueI9C4GG
116 */
117 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest003, TestSize.Level2)
118 {
119 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
120 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
121 std::shared_ptr<PowerRunningLockCallbackProxy> callbackProxy =
122 std::make_shared<PowerRunningLockCallbackProxy>(remote);
123 std::string message = "runninglock message";
124 callbackProxy->HandleRunningLockMessage(message);
125 sptr<IRemoteObject> token = new RunningLockTokenStub();
126 RunningLockInfo info("test2", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
127 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
128 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
129 EXPECT_FALSE(sptrProxy->Lock(token) == PowerErrors::ERR_OK);
130 EXPECT_FALSE(sptrProxy->UnLock(token) == PowerErrors::ERR_OK);
131 EXPECT_FALSE(sptrProxy->IsUsed(token));
132 }
133
134 /**
135 * @tc.name: PowerMockObjectTest004
136 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
137 * @tc.type: FUNC
138 * @tc.require: issueI5IUHE
139 */
140 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest004, TestSize.Level2)
141 {
142 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
143 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
144 sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
145 sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
146 sptr<IPowerRunninglockCallback> cb5 =new PowerRunningLockTestCallback();
147 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
148 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
149 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
150 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
151 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
152 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
153 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
154 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
155 EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(cb5));
156 EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(cb5));
157 EXPECT_FALSE(sptrProxy->RegisterRunningLockCallback(nullptr));
158 EXPECT_FALSE(sptrProxy->UnRegisterRunningLockCallback(nullptr));
159 sptrProxy->RebootDevice(" ");
160 sptrProxy->RebootDeviceForDeprecated(" ");
161 sptrProxy->ShutDownDevice(" ");
162 EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0) == PowerErrors::ERR_OK);
163 }
164 } // namespace