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