1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include "ohos.power.proj.hpp"
19 #include "ohos.power.impl.hpp"
20 #include "ohos.power.user.hpp"
21 #include "taihe/runtime.hpp"
22 #include "power_mgr_client.h"
23 #include "power_errors.h"
24 #include "power_log.h"
25
26 using namespace taihe;
27 using namespace ohos::power;
28 using namespace OHOS::PowerMgr;
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace {
34 PowerErrors g_error = PowerErrors::ERR_OK;
35 bool g_pass = false;
36 PowerMode g_mode = PowerMode::NORMAL_MODE;
37 int32_t g_errCode = 0;
38 }
39
40 namespace taihe {
set_business_error(int32_t err_code,taihe::string_view msg)41 void set_business_error(int32_t err_code, taihe::string_view msg)
42 {
43 g_errCode = err_code;
44 (void)msg;
45 }
46 }
47
48 namespace OHOS::PowerMgr {
IsScreenOn(bool needPrintLog)49 bool PowerMgrClient::IsScreenOn(bool needPrintLog)
50 {
51 g_pass = true;
52 return false;
53 }
54
WakeupDevice(WakeupDeviceType reason,const std::string & detail,const std::string & apiVersion)55 PowerErrors PowerMgrClient::WakeupDevice(
56 WakeupDeviceType reason, const std::string& detail, const std::string& apiVersion)
57 {
58 g_pass = true;
59 return g_error;
60 }
61
ForceSuspendDevice(const std::string & apiVersion)62 PowerErrors PowerMgrClient::ForceSuspendDevice(const std::string& apiVersion)
63 {
64 g_pass = true;
65 return g_error;
66 }
67
SuspendDevice(SuspendDeviceType reason,bool suspendImmed,const std::string & apiVersion)68 PowerErrors PowerMgrClient::SuspendDevice(
69 SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion)
70 {
71 g_pass = true;
72 return g_error;
73 }
74
GetDeviceMode()75 PowerMode PowerMgrClient::GetDeviceMode()
76 {
77 g_pass = true;
78 return g_mode;
79 }
80
SetDeviceMode(const PowerMode mode)81 PowerErrors PowerMgrClient::SetDeviceMode(const PowerMode mode)
82 {
83 g_pass = true;
84 return g_error;
85 }
86
IsStandby(bool & isStandby)87 PowerErrors PowerMgrClient::IsStandby(bool& isStandby)
88 {
89 g_pass = true;
90 return g_error;
91 }
92
Hibernate(bool clearMemory,const std::string & reason,const std::string & apiVersion)93 PowerErrors PowerMgrClient::Hibernate(bool clearMemory, const std::string& reason, const std::string& apiVersion)
94 {
95 g_pass = true;
96 return g_error;
97 }
98
RestoreScreenOffTime(const std::string & apiVersion)99 PowerErrors PowerMgrClient::RestoreScreenOffTime(const std::string& apiVersion)
100 {
101 g_pass = true;
102 return g_error;
103 }
104
OverrideScreenOffTime(int64_t timeout,const std::string & apiVersion)105 PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion)
106 {
107 g_pass = true;
108 return g_error;
109 }
110 } // namespace OHOS::PowerMgr
111
112 namespace {
113 class PowerTaiheNativeTest : public ::testing::Test {
114 public:
SetUpTestCase()115 static void SetUpTestCase() {}
TearDownTestCase()116 static void TearDownTestCase() {}
SetUp()117 void SetUp()
118 {
119 EXPECT_FALSE(g_pass);
120 }
TearDown()121 void TearDown()
122 {
123 g_pass = false;
124 g_mode = PowerMode::NORMAL_MODE;
125 g_error = PowerErrors::ERR_OK;
126 g_errCode = 0;
127 }
128 };
129
130 /**
131 * @tc.name: PowerTaiheNativeTest_001
132 * @tc.desc: test power taihe native
133 * @tc.type: FUNC
134 * @tc.require: issue#ICAK9Z
135 */
136 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_001, TestSize.Level1)
137 {
138 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_001 start");
139 IsActive();
140 EXPECT_TRUE(g_pass);
141 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_001 end");
142 }
143
144 /**
145 * @tc.name: PowerTaiheNativeTest_002
146 * @tc.desc: test power taihe native
147 * @tc.type: FUNC
148 * @tc.require: issue#ICAK9Z
149 */
150 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_002, TestSize.Level1)
151 {
152 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_002 start");
153 string_view detail = "PowerTaiheNativeTest_002";
154 Wakeup(detail);
155
156 g_error = PowerErrors::ERR_FAILURE;
157 Wakeup(detail);
158 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
159
160 g_error = PowerErrors::ERR_SYSTEM_API_DENIED;
161 Wakeup(detail);
162 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_SYSTEM_API_DENIED));
163 EXPECT_TRUE(g_pass);
164 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_002 end");
165 }
166
167 /**
168 * @tc.name: PowerTaiheNativeTest_003
169 * @tc.desc: test power taihe native
170 * @tc.type: FUNC
171 * @tc.require: issue#ICAK9Z
172 */
173 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_003, TestSize.Level1)
174 {
175 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_003 start");
176 bool isImmediate = false;
177 Suspend(&isImmediate);
178
179 isImmediate = true;
180 Suspend(&isImmediate);
181
182 g_error = PowerErrors::ERR_FAILURE;
183 Suspend(&isImmediate);
184 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
185
186 g_error = PowerErrors::ERR_SYSTEM_API_DENIED;
187 Suspend(&isImmediate);
188 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_SYSTEM_API_DENIED));
189 EXPECT_TRUE(g_pass);
190 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_003 end");
191 }
192
193 /**
194 * @tc.name: PowerTaiheNativeTest_004
195 * @tc.desc: test power taihe native
196 * @tc.type: FUNC
197 * @tc.require: issue#ICAK9Z
198 */
199 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_004, TestSize.Level1)
200 {
201 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_004 start");
202 GetPowerMode();
203 g_mode = PowerMode::POWER_SAVE_MODE;
204 GetPowerMode();
205 g_mode = PowerMode::PERFORMANCE_MODE;
206 GetPowerMode();
207 g_mode = PowerMode::EXTREME_POWER_SAVE_MODE;
208 GetPowerMode();
209 g_mode = static_cast<PowerMode>(0);
210 GetPowerMode();
211 EXPECT_TRUE(g_pass);
212 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_004 end");
213 }
214
215 /**
216 * @tc.name: PowerTaiheNativeTest_005
217 * @tc.desc: test power taihe native
218 * @tc.type: FUNC
219 * @tc.require: issue#ICAK9Z
220 */
221 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_005, TestSize.Level1)
222 {
223 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_005 start");
224 DevicePowerMode mode(DevicePowerMode::key_t::MODE_NORMAL);
225 SetPowerModeSync(mode);
226
227 g_error = PowerErrors::ERR_FAILURE;
228 SetPowerModeSync(mode);
229 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
230
231 g_error = PowerErrors::ERR_CONNECTION_FAIL;
232 SetPowerModeSync(mode);
233 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_CONNECTION_FAIL));
234 EXPECT_TRUE(g_pass);
235 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_005 end");
236 }
237
238 /**
239 * @tc.name: PowerTaiheNativeTest_006
240 * @tc.desc: test power taihe native
241 * @tc.type: FUNC
242 * @tc.require: issue#ICAK9Z
243 */
244 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_006, TestSize.Level1)
245 {
246 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_006 start");
247 IsStandby();
248
249 g_error = PowerErrors::ERR_FAILURE;
250 IsStandby();
251 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
252
253 g_error = PowerErrors::ERR_CONNECTION_FAIL;
254 IsStandby();
255 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_CONNECTION_FAIL));
256 EXPECT_TRUE(g_pass);
257 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_006 end");
258 }
259
260 /**
261 * @tc.name: PowerTaiheNativeTest_007
262 * @tc.desc: test power taihe native
263 * @tc.type: FUNC
264 * @tc.require: issue#ICAK9Z
265 */
266 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_007, TestSize.Level1)
267 {
268 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_007 start");
269 bool clearMemory = false;
270 Hibernate(clearMemory);
271
272 g_error = PowerErrors::ERR_FAILURE;
273 Hibernate(clearMemory);
274 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
275
276 clearMemory = true;
277 g_error = PowerErrors::ERR_SYSTEM_API_DENIED;
278 Hibernate(clearMemory);
279 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_SYSTEM_API_DENIED));
280 EXPECT_TRUE(g_pass);
281 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_007 end");
282 }
283
284 /**
285 * @tc.name: PowerTaiheNativeTest_008
286 * @tc.desc: test power taihe native
287 * @tc.type: FUNC
288 * @tc.require: issue#ICAK9Z
289 */
290 HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_008, TestSize.Level1)
291 {
292 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_008 start");
293 int64_t timeout = 0;
294 SetScreenOffTime(timeout);
295 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID));
296
297 constexpr int32_t INVALID_TIMEOUT = -2;
298 SetScreenOffTime(INVALID_TIMEOUT);
299 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID));
300
301 constexpr int32_t RESTORE_DEFAULT_SCREENOFF_TIME = -1;
302 SetScreenOffTime(RESTORE_DEFAULT_SCREENOFF_TIME);
303 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_PARAM_INVALID));
304
305 timeout = 30000; // ms
306 SetScreenOffTime(timeout);
307
308 g_error = PowerErrors::ERR_FAILURE;
309 SetScreenOffTime(timeout);
310 EXPECT_TRUE(g_errCode != static_cast<int32_t>(PowerErrors::ERR_FAILURE));
311
312 g_error = PowerErrors::ERR_SYSTEM_API_DENIED;
313 SetScreenOffTime(timeout);
314 EXPECT_TRUE(g_errCode == static_cast<int32_t>(PowerErrors::ERR_SYSTEM_API_DENIED));
315 EXPECT_TRUE(g_pass);
316 POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_008 end");
317 }
318 } // namespace
319
320