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