• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "work_trigger.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_HILOGD(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
41 }
42 
ShutdownCallback()43 void PowerMockProxyTest::PowerShutdownTestCallback::ShutdownCallback()
44 {
45     POWER_HILOGD(LABEL_TEST, "PowerShutdownTestCallback::ShutdownCallback.");
46 }
47 
OnPowerStateChanged(PowerState state)48 void PowerMockProxyTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
49 {
50     POWER_HILOGD(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
51 }
52 
53 namespace {
54 /**
55  * @tc.name: PowerMockProxyTest001
56  * @tc.desc: Test proxy when the parcel is mock
57  * @tc.type: FUNC
58  */
59 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest001, TestSize.Level2)
60 {
61     pid_t uid = 0;
62     pid_t pid = 0;
63     sptr<IPCObjectStub> remote = new IPCObjectStub();
64     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
65     sptr<IRemoteObject> token = new RunningLockTokenStub();
66     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN);
67     EXPECT_FALSE(sptrProxy->CreateRunningLock(token, info) == PowerErrors::ERR_OK);
68     EXPECT_FALSE(sptrProxy->ReleaseRunningLock(token));
69     auto type = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
70     EXPECT_FALSE(sptrProxy->IsRunningLockTypeSupported(type));
71     EXPECT_FALSE(sptrProxy->Lock(token, info, 0));
72     EXPECT_FALSE(sptrProxy->UnLock(token));
73     EXPECT_FALSE(sptrProxy->IsUsed(token));
74     WorkTriggerList worklist;
75     worklist.push_back(nullptr);
76     worklist.push_back(nullptr);
77     worklist.push_back(nullptr);
78     EXPECT_FALSE(sptrProxy->SetWorkTriggerList(token, worklist));
79     EXPECT_FALSE(sptrProxy->ProxyRunningLock(true, uid, pid));
80 }
81 
82 /**
83  * @tc.name: PowerMockProxyTest002
84  * @tc.desc: Test proxy when the parcel is mock
85  * @tc.type: FUNC
86  */
87 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest002, TestSize.Level2)
88 {
89     sptr<IPCObjectStub> remote = new IPCObjectStub();
90     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
91     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + 1;
92     SuspendDeviceType abnormaltype = SuspendDeviceType(suspendReason);
93     EXPECT_EQ(sptrProxy->SuspendDevice(0, abnormaltype, false), PowerErrors::ERR_CONNECTION_FAIL);
94     auto error =
95         sptrProxy->WakeupDevice(GetTickCount(), WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("app call"));
96     EXPECT_EQ(error, PowerErrors::ERR_CONNECTION_FAIL);
97     EXPECT_FALSE(sptrProxy->RefreshActivity(GetTickCount(), UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true));
98     EXPECT_FALSE(sptrProxy->OverrideScreenOffTime(200));
99     EXPECT_FALSE(sptrProxy->RestoreScreenOffTime());
100     auto state = sptrProxy->GetState();
101     EXPECT_EQ(state, PowerState::UNKNOWN);
102     EXPECT_FALSE(sptrProxy->IsScreenOn());
103     sptrProxy->SetDisplaySuspend(true);
104     PowerMode mode1 = PowerMode::NORMAL_MODE;
105     EXPECT_EQ(sptrProxy->SetDeviceMode(mode1), PowerErrors::ERR_CONNECTION_FAIL);
106     auto mode2 = sptrProxy->GetDeviceMode();
107     EXPECT_TRUE(mode2 == mode1);
108 }
109 
110 /**
111  * @tc.name: PowerMockProxyTest003
112  * @tc.desc: Test proxy when the parcel is mock
113  * @tc.type: FUNC
114  */
115 HWTEST_F(PowerMockProxyTest, PowerMockProxyTest003, TestSize.Level2)
116 {
117     sptr<IPCObjectStub> remote = new IPCObjectStub();
118     std::shared_ptr<PowerMgrProxy> sptrProxy = std::make_shared<PowerMgrProxy>(remote);
119     sptr<IPowerStateCallback> cb1 = new PowerStateTestCallback();
120     sptr<IShutdownCallback> cb2 = new PowerShutdownTestCallback();
121     sptr<IPowerModeCallback> cb3 = new PowerModeTestCallback();
122     auto priority = IShutdownCallback::ShutdownPriority::POWER_SHUTDOWN_PRIORITY_LOW;
123     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(cb1));
124     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(cb1));
125     EXPECT_FALSE(sptrProxy->RegisterPowerStateCallback(nullptr));
126     EXPECT_FALSE(sptrProxy->UnRegisterPowerStateCallback(nullptr));
127     EXPECT_FALSE(sptrProxy->RegisterShutdownCallback(priority, cb2));
128     EXPECT_FALSE(sptrProxy->UnRegisterShutdownCallback(cb2));
129     EXPECT_FALSE(sptrProxy->RegisterShutdownCallback(priority, nullptr));
130     EXPECT_FALSE(sptrProxy->UnRegisterShutdownCallback(nullptr));
131     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(cb3));
132     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(cb3));
133     EXPECT_FALSE(sptrProxy->RegisterPowerModeCallback(nullptr));
134     EXPECT_FALSE(sptrProxy->UnRegisterPowerModeCallback(nullptr));
135     EXPECT_EQ(sptrProxy->RebootDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
136     EXPECT_EQ(sptrProxy->ShutDownDevice(" "), PowerErrors::ERR_CONNECTION_FAIL);
137     EXPECT_FALSE(sptrProxy->ForceSuspendDevice(0));
138     static std::vector<std::string> dumpArgs;
139     dumpArgs.push_back("-a");
140     std::string errorCode = "remote error";
141     std::string actualDebugInfo = sptrProxy->ShellDump(dumpArgs, dumpArgs.size());
142     EXPECT_EQ(errorCode, actualDebugInfo);
143 }
144 } // namespace