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_proxy_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
32 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36
OnPowerModeChanged(PowerMode mode)37 void PowerMockProxyTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
38 {
39 POWER_HILOGD(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
40 }
41
OnPowerStateChanged(PowerState state)42 void PowerMockProxyTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
43 {
44 POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
45 }
46
47 namespace {
48 /**
49 * @tc.name: PowerMockProxyTest001
50 * @tc.desc: Test proxy when the parcel is mock
51 * @tc.type: FUNC
52 */
53 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest001, TestSize.Level2)
54 {
55 pid_t uid = 0;
56 pid_t pid = 0;
57 sptr<IPCObjectStub> remote = new IPCObjectStub();
58 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
59 sptr<IRemoteObject> token = new RunningLockTokenStub();
60 RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
61 EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
62 EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
63 EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(RunningLockType::RUNNINGLOCK_BUTT));
64 EXPECT_FALSE(sptrProxy->Lock(token, 0));
65 EXPECT_FALSE(sptrProxy->UnLock(token));
66 EXPECT_FALSE(sptrProxy->IsUsed(token));
67 EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, pid, uid));
68 EXPECT_FALSE(sptrProxy->ProxyRunningLocks(true, {std::make_pair(pid, uid)}));
69 EXPECT_FALSE(sptrProxy->ResetRunningLocks());
70 }
71
72 /**
73 * @tc.name: PowerMockProxyTest002
74 * @tc.desc: Test proxy when the parcel is mock
75 * @tc.type: FUNC
76 */
77 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest002, TestSize.Level2)
78 {
79 sptr<IPCObjectStub> remote = new IPCObjectStub();
80 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
81 int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
82 SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
83 EXPECT_EQ(sptrProxy->SuspendDevice(0, abnormaltype, false), PowerErrors::ERR_CONNECTION_FAIL);
84 auto error =
85 sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
86 EXPECT_EQ(error, PowerErrors::ERR_CONNECTION_FAIL);
87 EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
88 EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
89 EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());
90 auto state = sptrProxy->GetState();
91 EXPECT_EQ(state, PowerState::UNKNOWN);
92 EXPECT_FALSE(sptrProxy->IsScreenOn());
93 sptrProxy->SetDisplaySuspend(true);
94 PowerMode mode1 = PowerMode::NORMAL_MODE;
95 EXPECT_EQ(sptrProxy->SetDeviceMode(mode1), PowerErrors::ERR_CONNECTION_FAIL);
96 auto mode2 = sptrProxy->GetDeviceMode();
97 EXPECT_TRUE(mode2 == mode1);
98 }
99
100 /**
101 * @tc.name: PowerMockProxyTest003
102 * @tc.desc: Test proxy when the parcel is mock
103 * @tc.type: FUNC
104 */
105 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest003, TestSize.Level2)
106 {
107 sptr<IPCObjectStub> remote = new IPCObjectStub();
108 std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
109 sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
110 sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
111 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
112 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
113 EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
114 EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
115 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
116 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
117 EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
118 EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
119 EXPECT_EQ(sptrProxy->RebootDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
120 EXPECT_EQ(sptrProxy->ShutDownDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
121 EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0));
122 static std::vector<std::string> dumpArgs;
123 dumpArgs.push_back("-a");
124 std::string errorCode = "remote error";
125 std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
126 EXPECT_EQ(errorCode, actualDebugInfo);
127 }
128 } // namespace