• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "powermgr_service_mock_parcel_test.h"
17 
18 #include <datetime_ex.h>
19 
20 #include "power_log.h"
21 #include "power_mode_callback_proxy.h"
22 #include "power_mgr_service.h"
23 #include "power_runninglock_callback_proxy.h"
24 #include "power_state_callback_proxy.h"
25 #include "power_state_machine_info.h"
26 #include "powermgr_service_test_proxy.h"
27 #include "running_lock_token_stub.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::PowerMgr;
31 using namespace OHOS;
32 using namespace std;
33 
34 namespace {
35 static sptr<PowerMgrService> g_powerMgrService = nullptr;
36 static std::shared_ptr<PowerMgrServiceTestProxy> g_powerMgrServiceProxy = nullptr;
37 } // namespace
38 
SetUpTestCase()39 void PowerMgrServiceMockParcelTest::SetUpTestCase()
40 {
41     g_powerMgrService = DelayedSpSingleton<PowerMgrService>::GetInstance();
42     g_powerMgrService->OnStart();
43 
44     if (g_powerMgrServiceProxy == nullptr) {
45         g_powerMgrServiceProxy = std::make_shared<PowerMgrServiceTestProxy>(g_powerMgrService);
46     }
47 }
48 
TearDownTestCase()49 void PowerMgrServiceMockParcelTest::TearDownTestCase()
50 {
51     g_powerMgrService->OnStop();
52     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
53 }
54 
OnPowerModeChanged(PowerMode mode)55 void PowerMgrServiceMockParcelTest::PowerModeTestCallback::OnPowerModeChanged(PowerMode mode)
56 {
57     POWER_HILOGI(LABEL_TEST, "PowerModeTestCallback::OnPowerModeChanged.");
58 }
59 
OnPowerStateChanged(PowerState state)60 void PowerMgrServiceMockParcelTest::PowerStateTestCallback::OnPowerStateChanged(PowerState state)
61 {
62     POWER_HILOGI(LABEL_TEST, "PowerStateTestCallback::OnPowerStateChanged.");
63 }
64 
HandleRunningLockMessage(std::string message)65 void PowerMgrServiceMockParcelTest::PowerRunningLockTestCallback::HandleRunningLockMessage(std::string message)
66 {
67     POWER_HILOGI(LABEL_TEST, "PowerRunningLockTestCallback::HandleRunningLockMessage.");
68 }
69 namespace {
70 /**
71  * @tc.name: PowerMgrServiceMockParcelTest001
72  * @tc.desc: PowerMgr service test when the Parcel is mock
73  * @tc.type: FUNC
74  * @tc.require: issueI67Z62
75  */
76 HWTEST_F(PowerMgrServiceMockParcelTest, PowerMgrServiceMockParcelTest001, TestSize.Level2)
77 {
78     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest001 start.");
79     ASSERT_NE(g_powerMgrServiceProxy, nullptr);
80     sptr<IRemoteObject> token = new RunningLockTokenStub();
81     RunningLockInfo runningLockInfo;
82     runningLockInfo.name = "runninglock";
83     runningLockInfo.type = RunningLockType::RUNNINGLOCK_SCREEN;
84     pid_t uid = 0;
85     pid_t pid = 0;
86     int32_t timeOutMs = 0;
87     int32_t powerError = 1;
88     std::string name;
89     bool lockTypeSupported = false;
90     bool isUsed = false;
91     g_powerMgrServiceProxy->CreateRunningLockIpc(token, runningLockInfo, powerError);
92     EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
93     EXPECT_NE(g_powerMgrServiceProxy->ReleaseRunningLockIpc(token), ERR_OK);
94     g_powerMgrServiceProxy->IsRunningLockTypeSupportedIpc(
95         static_cast<int32_t>(RunningLockType::RUNNINGLOCK_BUTT), lockTypeSupported);
96     EXPECT_FALSE(lockTypeSupported);
97     g_powerMgrServiceProxy->LockIpc(token, timeOutMs, powerError);
98     EXPECT_NE(powerError, ERR_OK);
99     g_powerMgrServiceProxy->UnLockIpc(token, name, powerError);
100     EXPECT_NE(powerError, ERR_OK);
101     g_powerMgrServiceProxy->IsUsedIpc(token, isUsed);
102     EXPECT_FALSE(isUsed);
103     EXPECT_NE(g_powerMgrServiceProxy->ProxyRunningLockIpc(true, pid, uid), ERR_OK);
104     std::unique_ptr<VectorPair> vectorPairInfos = std::make_unique<VectorPair>();
105     std::vector<std::pair<pid_t, pid_t>> processInfos;
106     processInfos.emplace_back(pid, uid);
107     vectorPairInfos->SetProcessInfos(processInfos);
108     EXPECT_NE(g_powerMgrServiceProxy->ProxyRunningLocksIpc(true, *vectorPairInfos), ERR_OK);
109     EXPECT_NE(g_powerMgrServiceProxy->ResetRunningLocksIpc(), ERR_OK);
110     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest001 end.");
111 }
112 
113 /**
114  * @tc.name: PowerMgrServiceMockParcelTest002
115  * @tc.desc: PowerMgr service test when the Parcel is mock
116  * @tc.type: FUNC
117  * @tc.require: issueI67Z62
118  */
119 HWTEST_F(PowerMgrServiceMockParcelTest, PowerMgrServiceMockParcelTest002, TestSize.Level2)
120 {
121     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest002 start.");
122     int32_t PARM_ONE = 1;
123     int32_t powerError = 1;
124     int32_t powerState = -1;
125     std::string apiVersion = "-1";
126     int32_t ret = 0;
127     bool isScreenOn = false;
128     ASSERT_NE(g_powerMgrServiceProxy, nullptr);
129     int32_t suspendReason = (static_cast<int32_t>(SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX)) + PARM_ONE;
130     ret = g_powerMgrServiceProxy->SuspendDeviceIpc(0, suspendReason, false, apiVersion, powerError);
131     EXPECT_EQ(ret, ERR_INVALID_DATA);
132     ret = g_powerMgrServiceProxy->WakeupDeviceIpc(GetTickCount(),
133         static_cast<int32_t>(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION),
134         std::string("app call"), apiVersion, powerError);
135     EXPECT_EQ(ret, ERR_INVALID_DATA);
136     int32_t attention = static_cast<int32_t>(UserActivityType::USER_ACTIVITY_TYPE_ATTENTION);
137     EXPECT_NE(g_powerMgrServiceProxy->RefreshActivityIpc(GetTickCount(), attention, true), ERR_OK);
138     g_powerMgrServiceProxy->OverrideScreenOffTimeIpc(200, powerError);
139     EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
140     g_powerMgrServiceProxy->RestoreScreenOffTimeIpc(apiVersion, powerError);
141     EXPECT_FALSE(powerError == static_cast<int32_t>(PowerErrors::ERR_OK));
142     ret = g_powerMgrServiceProxy->GetStateIpc(powerState);
143     EXPECT_EQ(ret, ERR_INVALID_VALUE);
144     g_powerMgrServiceProxy->IsScreenOnIpc(true, isScreenOn);
145     EXPECT_FALSE(isScreenOn);
146     g_powerMgrServiceProxy->SetDisplaySuspendIpc(true);
147     PowerMode setMode = PowerMode::NORMAL_MODE;
148     ret = g_powerMgrServiceProxy->SetDeviceModeIpc(static_cast<int32_t>(setMode), powerError);
149     EXPECT_EQ(ret, ERR_INVALID_DATA);
150     int32_t getMode = 0;
151     ret = g_powerMgrServiceProxy->GetDeviceModeIpc(getMode);
152     EXPECT_EQ(ret, ERR_INVALID_VALUE);
153     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest002 end.");
154 }
155 
156 /**
157  * @tc.name: PowerMgrServiceMockParcelTest003
158  * @tc.desc: PowerMgr service test when the Parcel is mock
159  * @tc.type: FUNC
160  * @tc.require: issueI67Z62
161  */
162 HWTEST_F(PowerMgrServiceMockParcelTest, PowerMgrServiceMockParcelTest003, TestSize.Level2)
163 {
164     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest003 start.");
165     int32_t powerError = 1;
166     std::string apiVersion = "-1";
167     ASSERT_NE(g_powerMgrServiceProxy, nullptr);
168     sptr<IPowerStateCallback> stateCb = new PowerStateTestCallback();
169     sptr<IPowerModeCallback> modeCb = new PowerModeTestCallback();
170     sptr<IPowerRunninglockCallback> RunninglockCb =new PowerRunningLockTestCallback();
171     EXPECT_NE(g_powerMgrServiceProxy->RegisterPowerStateCallbackIpc(stateCb), ERR_OK);
172     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterPowerStateCallbackIpc(stateCb), ERR_OK);
173     EXPECT_NE(g_powerMgrServiceProxy->RegisterPowerStateCallbackIpc(nullptr), ERR_OK);
174     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterPowerStateCallbackIpc(nullptr), ERR_OK);
175     EXPECT_NE(g_powerMgrServiceProxy->RegisterPowerModeCallbackIpc(modeCb), ERR_OK);
176     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterPowerModeCallbackIpc(modeCb), ERR_OK);
177     EXPECT_NE(g_powerMgrServiceProxy->RegisterPowerModeCallbackIpc(nullptr), ERR_OK);
178     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterPowerModeCallbackIpc(nullptr), ERR_OK);
179     EXPECT_NE(g_powerMgrServiceProxy->RegisterRunningLockCallbackIpc(RunninglockCb), ERR_OK);
180     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterRunningLockCallbackIpc(RunninglockCb), ERR_OK);
181     EXPECT_NE(g_powerMgrServiceProxy->RegisterRunningLockCallbackIpc(nullptr), ERR_OK);
182     EXPECT_NE(g_powerMgrServiceProxy->UnRegisterRunningLockCallbackIpc(nullptr), ERR_OK);
183     EXPECT_FALSE(g_powerMgrServiceProxy->ForceSuspendDeviceIpc(0) == ERR_OK);
184     static std::vector<std::string> dumpArgs;
185     dumpArgs.push_back("-a");
186     std::string errorCode;
187     std::string actualDebugInfo;
188     g_powerMgrServiceProxy->ShellDumpIpc(dumpArgs, dumpArgs.size(), actualDebugInfo);
189     EXPECT_EQ(errorCode, actualDebugInfo);
190     POWER_HILOGI(LABEL_TEST, "PowerMgrServiceMockParcelTest003 end.");
191 }
192 } // namespace