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
OnPowerStateChanged(PowerState state)41 void PowerMockObjectTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
42 {
43 POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
44 }
45
46 namespace {
47 /**
48 * @tc.name: PowerMockObjectTest001
49 * @tc.desc: Test Power proxy when the PowerRemoteObject is mock
50 * @tc.type: FUNC
51 * @tc.require: issueI5IUHE
52 */
53 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest001, TestSize.Level2)
54 {
55 PowerMode mode = PowerMode::NORMAL_MODE;
56 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
57 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
58 std::shared_ptr<PowerModeCallbackProxy> callbackProxy = std::make_shared<PowerModeCallbackProxy>(remote);
59 callbackProxy->OnPowerModeChanged(mode);
60 std::shared_ptr<PowerStateCallbackProxy> stateCallbackProxy = std::make_shared<PowerStateCallbackProxy>(remote);
61 PowerState state = PowerState::AWAKE;
62 stateCallbackProxy->OnPowerStateChanged(state);
63 sptr<IRemoteObject> token = new RunningLockTokenStub();
64 RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
65 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
66 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
67 state = sptrProxy->GetState();
68 EXPECT_EQ(state, PowerState::UNKNOWN);
69 EXPECT_FALSE(sptrProxy->Lock(token, 0));
70 EXPECT_FALSE(sptrProxy->UnLock(token));
71 EXPECT_FALSE(sptrProxy->IsUsed(token));
72 }
73
74 /**
75 * @tc.name: PowerMockObjectTest002
76 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
77 * @tc.type: FUNC
78 * @tc.require: issueI5IUHE
79 */
80 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest002, TestSize.Level2)
81 {
82 pid_t uid = 0;
83 pid_t pid = 0;
84 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
85 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
86 PowerMode mode = PowerMode::NORMAL_MODE;
87 int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
88 SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
89 auto error1 = sptrProxy->SuspendDevice(0, abnormaltype, false);
90 EXPECT_EQ(error1, PowerErrors::ERR_CONNECTION_FAIL);
91 EXPECT_FALSE(sptrProxy->SetDisplaySuspend(true));
92 auto error2 =
93 sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
94 EXPECT_EQ(error2, PowerErrors::ERR_CONNECTION_FAIL);
95 EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
96 EXPECT_EQ(sptrProxy->SetDeviceMode(mode), PowerErrors::ERR_CONNECTION_FAIL);
97 auto ret = sptrProxy->GetDeviceMode();
98 EXPECT_TRUE(ret == mode);
99 EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
100 EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BACKGROUND));
101 EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
102 EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());
103 }
104
105 /**
106 * @tc.name: PowerMockObjectTest003
107 * @tc.desc: Test Power proxy when PowerRemoteObject is mock
108 * @tc.type: FUNC
109 * @tc.require: issueI5IUHE
110 */
111 HWTEST_F(PowerMockObjectTest, PowerMockObjectTest003, TestSize.Level2)
112 {
113 sptr<MockPowerRemoteObject> remote = new MockPowerRemoteObject();
114 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
115 sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
116 sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
117 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
118 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
119 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
120 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
121 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
122 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
123 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
124 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
125 sptrProxy->RebootDevice(" ");
126 sptrProxy->RebootDeviceForDeprecated(" ");
127 sptrProxy->ShutDownDevice(" ");
128 EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0));
129 }
130 } // namespace