1 /*
2 * Copyright (c) 2021-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 <bundle_mgr_proxy.h>
17 #include <datetime_ex.h>
18 #include <gtest/gtest.h>
19 #include <if_system_ability_manager.h>
20 #include <iostream>
21 #include <ipc_skeleton.h>
22 #include <string_ex.h>
23
24 #include "common_event_manager.h"
25 #include "ipower_mode_callback.h"
26 #include "power_common.h"
27 #include "power_mgr_client.h"
28 #include "power_mgr_powersavemode_test.h"
29 #include "power_mgr_service.h"
30 #include "power_state_machine.h"
31 #include "power_state_machine_info.h"
32 #include "running_lock.h"
33 #include "running_lock_info.h"
34
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS::EventFwk;
38 using namespace OHOS;
39 using namespace std;
40
OnPowerModeChanged(PowerMode mode)41 void PowerMgrPowerSavemodeTest::PowerModeTest1Callback::OnPowerModeChanged(PowerMode mode)
42 {
43 POWER_HILOGD(LABEL_TEST, "PowerModeTest1Callback::OnPowerModeChanged.");
44 }
45
SetUpTestCase(void)46 void PowerMgrPowerSavemodeTest::SetUpTestCase(void)
47 {
48 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
49 pms->OnStart();
50 SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr());
51 }
52
TearDownTestCase(void)53 void PowerMgrPowerSavemodeTest::TearDownTestCase(void)
54 {
55 }
56
SetUp(void)57 void PowerMgrPowerSavemodeTest::SetUp(void)
58 {
59 }
60
TearDown(void)61 void PowerMgrPowerSavemodeTest::TearDown(void)
62 {
63 }
64
65 namespace {
66 /**
67 * @tc.name: PowerSavemode_001
68 * @tc.desc: test SetDeviceMode in proxy
69 * @tc.type: FUNC
70 * @tc.require: issueI5MJZJ
71 */
72 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_001, TestSize.Level2)
73 {
74 GTEST_LOG_(INFO) << "PowerSavemode_001: SetDeviceMode start.";
75
76 auto& powerMgrClient = PowerMgrClient::GetInstance();
77 PowerMode mode = PowerMode::POWER_SAVE_MODE;
78 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
79 powerMgrClient.SetDeviceMode(mode);
80 mode = powerMgrClient.GetDeviceMode();
81 EXPECT_EQ(mode, mode1) << "PowerSavemode_001 fail to SetDeviceMode";
82 GTEST_LOG_(INFO) << "PowerSavemode_001: SetDeviceMode end.";
83 }
84
85 /**
86 * @tc.name: PowerSavemode_002
87 * @tc.desc: test SetDeviceMode in proxy
88 * @tc.type: FUNC
89 * @tc.require: issueI5MJZJ
90 */
91 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_002, TestSize.Level2)
92 {
93 GTEST_LOG_(INFO) << "PowerSavemode_002: SetDeviceMode start.";
94
95 auto& powerMgrClient = PowerMgrClient::GetInstance();
96 PowerMode mode = PowerMode::PERFORMANCE_MODE;
97 PowerMode mode1 = PowerMode::PERFORMANCE_MODE;
98 powerMgrClient.SetDeviceMode(mode);
99 mode = powerMgrClient.GetDeviceMode();
100 EXPECT_EQ(mode, mode1) << "PowerSavemode_002 fail to SetDeviceMode";
101 }
102
103 /**
104 * @tc.name: PowerSavemode_003
105 * @tc.desc: test SetDeviceMode in proxy
106 * @tc.type: FUNC
107 * @tc.require: issueI5MJZJ
108 */
109 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_003, TestSize.Level2)
110 {
111 GTEST_LOG_(INFO) << "PowerSavemode_003: SetDeviceMode start.";
112
113 auto& powerMgrClient = PowerMgrClient::GetInstance();
114 PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
115 PowerMode mode1 = PowerMode::EXTREME_POWER_SAVE_MODE;
116 powerMgrClient.SetDeviceMode(mode);
117 mode = powerMgrClient.GetDeviceMode();
118 EXPECT_EQ(mode, mode1) << "PowerSavemode_003 fail to SetDeviceMode";
119 GTEST_LOG_(INFO) << "PowerSavemode_003: SetDeviceMode end.";
120 }
121
122 /**
123 * @tc.name: PowerSavemode_028
124 * @tc.desc: test SetDeviceMode in proxy
125 * @tc.type: FUNC
126 * @tc.require: issueI5MJZJ
127 */
128 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_028, TestSize.Level2)
129 {
130 GTEST_LOG_(INFO) << "PowerSavemode_028: SetDeviceMode start.";
131
132 auto& powerMgrClient = PowerMgrClient::GetInstance();
133 PowerMode mode = static_cast<PowerMode>(1);
134 PowerMode mode1 = static_cast<PowerMode>(1);
135 powerMgrClient.SetDeviceMode(mode);
136 mode = powerMgrClient.GetDeviceMode();
137 EXPECT_NE(mode, mode1) << "PowerSavemode_028 fail to SetDeviceMode abnormal";
138 GTEST_LOG_(INFO) << "PowerSavemode_028: SetDeviceMode end.";
139 }
140
141 /**
142 * @tc.name: PowerSavemode_029
143 * @tc.desc: test SetDeviceMode in proxy
144 * @tc.type: FUNC
145 * @tc.require: issueI5MJZJ
146 */
147 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_029, TestSize.Level2)
148 {
149 GTEST_LOG_(INFO) << "PowerSavemode_029: SetDeviceMode start.";
150
151 auto& powerMgrClient = PowerMgrClient::GetInstance();
152 PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
153 PowerMode mode1 = PowerMode::EXTREME_POWER_SAVE_MODE;
154 for (int i = 0; i < 100; i++) {
155 powerMgrClient.SetDeviceMode(mode);
156 }
157 mode = powerMgrClient.GetDeviceMode();
158 EXPECT_EQ(mode, mode1) << "PowerSavemode_029 fail to SetDeviceMode";
159 GTEST_LOG_(INFO) << "PowerSavemode_029: SetDeviceMode end.";
160 }
161
162 /**
163 * @tc.name: PowerSavemode_030
164 * @tc.desc: test GetDeviceMode in proxy
165 * @tc.type: FUNC
166 * @tc.require: issueI5MJZJ
167 */
168 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_030, TestSize.Level2)
169 {
170 GTEST_LOG_(INFO) << "PowerSavemode_030: GetDeviceMode start.";
171
172 auto& powerMgrClient = PowerMgrClient::GetInstance();
173 PowerMode mode = static_cast<PowerMode>(0);
174 PowerMode mode1 = PowerMode::NORMAL_MODE;
175 mode = powerMgrClient.GetDeviceMode();
176 EXPECT_NE(mode, mode1) << "PowerSavemode_030 fail to GetDeviceMode";
177 }
178
179 /**
180 * @tc.name: PowerSavemode_031
181 * @tc.desc: test GetDeviceMode in proxy
182 * @tc.type: FUNC
183 * @tc.require: issueI5MJZJ
184 */
185 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_031, TestSize.Level2)
186 {
187 GTEST_LOG_(INFO) << "PowerSavemode_031: GetDeviceMode start.";
188
189 auto& powerMgrClient = PowerMgrClient::GetInstance();
190 PowerMode mode = PowerMode::POWER_SAVE_MODE;
191 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
192 powerMgrClient.SetDeviceMode(mode);
193 for (int i = 0; i < 100; i++) {
194 mode = powerMgrClient.GetDeviceMode();
195 }
196 EXPECT_EQ(mode, mode1) << "PowerSavemode_031 fail to GetDeviceMode";
197 }
198
199 /**
200 * @tc.name: PowerSavemode_032
201 * @tc.desc: test PowerModeCallback
202 * @tc.type: FUNC
203 * @tc.require: issueI5MJZJ
204 */
205 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_032, TestSize.Level0)
206 {
207 GTEST_LOG_(INFO) << "PowerSavemode_032: RegisterPowerModeCallback start.";
208
209 auto& powerMgrClient = PowerMgrClient::GetInstance();
210 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
211 powerMgrClient.RegisterPowerModeCallback(cb1);
212 PowerMode mode = PowerMode::POWER_SAVE_MODE;
213 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
214 powerMgrClient.SetDeviceMode(mode);
215 mode = powerMgrClient.GetDeviceMode();
216 EXPECT_EQ(mode, mode1) << "PowerSavemode_032 fail to PowerModeCallback";
217
218 POWER_HILOGD(LABEL_TEST, "PowerSavemode_032 end.");
219 }
220
221 /**
222 * @tc.name: PowerSavemode_033
223 * @tc.desc: test PowerModeCallback
224 * @tc.type: FUNC
225 * @tc.require: issueI5MJZJ
226 */
227 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_033, TestSize.Level0)
228 {
229 GTEST_LOG_(INFO) << "PowerSavemode_033: RegisterPowerModeCallback start.";
230
231 auto& powerMgrClient = PowerMgrClient::GetInstance();
232 POWER_HILOGD(LABEL_TEST, "PowerSavemode_033 Start.");
233 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
234 powerMgrClient.RegisterPowerModeCallback(cb1);
235 PowerMode mode = static_cast<PowerMode>(1);
236 PowerMode mode1 = static_cast<PowerMode>(1);
237 powerMgrClient.SetDeviceMode(mode);
238 mode = powerMgrClient.GetDeviceMode();
239 EXPECT_NE(mode, mode1) << "PowerSavemode_033 fail to PowerModeCallback";
240
241 POWER_HILOGD(LABEL_TEST, "PowerSavemode_033 end.");
242 }
243
244 /**
245 * @tc.name: PowerSavemode_035
246 * @tc.desc: test PowerModeCallback
247 * @tc.type: FUNC
248 * @tc.require: issueI5MJZJ
249 */
250 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_035, TestSize.Level0)
251 {
252 GTEST_LOG_(INFO) << "PowerSavemode_035: RegisterPowerModeCallback start.";
253
254 auto& powerMgrClient = PowerMgrClient::GetInstance();
255 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
256 for (int i = 0; i < 100; i++) {
257 powerMgrClient.RegisterPowerModeCallback(cb1);
258 }
259 PowerMode mode = PowerMode::POWER_SAVE_MODE;
260 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
261 powerMgrClient.SetDeviceMode(mode);
262 mode = powerMgrClient.GetDeviceMode();
263 EXPECT_EQ(mode, mode1) << "PowerSavemode_035 fail to PowerModeCallback";
264
265 POWER_HILOGD(LABEL_TEST, "PowerSavemode_035 end.");
266 }
267
268 /**
269 * @tc.name: PowerSavemode_036
270 * @tc.desc: test PowerModeCallback
271 * @tc.type: FUNC
272 * @tc.require: issueI5MJZJ
273 */
274 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_036, TestSize.Level0)
275 {
276 GTEST_LOG_(INFO) << "PowerSavemode_036: UnRegisterPowerModeCallback start.";
277
278 auto& powerMgrClient = PowerMgrClient::GetInstance();
279 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
280 powerMgrClient.UnRegisterPowerModeCallback(cb1);
281 PowerMode mode = PowerMode::POWER_SAVE_MODE;
282 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
283 powerMgrClient.SetDeviceMode(mode);
284 mode = powerMgrClient.GetDeviceMode();
285 EXPECT_EQ(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback";
286
287 POWER_HILOGD(LABEL_TEST, "PowerSavemode_036 end.");
288 }
289
290 /**
291 * @tc.name: PowerSavemode_037
292 * @tc.desc: test PowerModeCallback
293 * @tc.type: FUNC
294 * @tc.require: issueI5MJZJ
295 */
296 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_037, TestSize.Level0)
297 {
298 GTEST_LOG_(INFO) << "PowerSavemode_037: UnRegisterPowerModeCallback start.";
299
300 auto& powerMgrClient = PowerMgrClient::GetInstance();
301 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
302 powerMgrClient.UnRegisterPowerModeCallback(cb1);
303 PowerMode mode = static_cast<PowerMode>(1);
304 PowerMode mode1 = static_cast<PowerMode>(1);
305 powerMgrClient.SetDeviceMode(mode);
306 mode = powerMgrClient.GetDeviceMode();
307 EXPECT_NE(mode, mode1) << "PowerSavemode_037 fail to PowerModeCallback";
308
309 POWER_HILOGD(LABEL_TEST, "PowerSavemode_037 UnRegisterPowerModeCallback end.");
310 }
311
312 /**
313 * @tc.name: PowerSavemode_039
314 * @tc.desc: test PowerModeCallback
315 * @tc.type: FUNC
316 * @tc.require: issueI5MJZJ
317 */
318 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_039, TestSize.Level0)
319 {
320 GTEST_LOG_(INFO) << "PowerSavemode_039: UnRegisterPowerModeCallback start.";
321
322 auto& powerMgrClient = PowerMgrClient::GetInstance();
323 const sptr<IPowerModeCallback> cb1 = new PowerModeTest1Callback();
324 for (int i = 0; i < 100; i++) {
325 powerMgrClient.UnRegisterPowerModeCallback(cb1);
326 }
327 sleep(SLEEP_WAIT_TIME_S);
328 PowerMode mode = PowerMode::POWER_SAVE_MODE;
329 PowerMode mode1 = PowerMode::POWER_SAVE_MODE;
330 powerMgrClient.SetDeviceMode(mode);
331 mode = powerMgrClient.GetDeviceMode();
332 EXPECT_EQ(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback";
333
334 POWER_HILOGD(LABEL_TEST, "PowerSavemode_039 end.");
335 }
336 } // namespace
337
CommonEventServiCesSystemTest(const CommonEventSubscribeInfo & subscriberInfo)338 PowerMgrPowerSavemodeTest::CommonEventServiCesSystemTest::CommonEventServiCesSystemTest(
339 const CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo)
340 {
341 POWER_HILOGD(LABEL_TEST, "subscribe.");
342 }
343
344 static uint32_t g_i = 0;
345 static int g_judgeNum = 2;
346
OnReceiveEvent(const CommonEventData & data)347 void PowerMgrPowerSavemodeTest::CommonEventServiCesSystemTest::OnReceiveEvent(const CommonEventData& data)
348 {
349 std::string action = data.GetWant().GetAction();
350 if (action == CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED) {
351 POWER_HILOGD(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent.");
352 g_i = g_judgeNum;
353 }
354 uint32_t j = 2;
355 EXPECT_EQ(g_i, j) << "PowerSavemode_022 fail to PowerModeCallback";
356 POWER_HILOGD(LABEL_TEST, "CommonEventServiCesSystemTest::OnReceiveEvent other.");
357 }
358
359 namespace {
360 /**
361 * @tc.name: PowerSavemode_022
362 * @tc.desc: ReceiveEvent
363 * @tc.type: FUNC
364 * @tc.require: issueI5MJZJ
365 */
366 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_022, TestSize.Level0)
367 {
368 GTEST_LOG_(INFO) << "PowerSavemode_022: UnRegisterPowerModeCallback start.";
369
370 bool result = false;
371 MatchingSkills matchingSkills;
372 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
373 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
374 auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
375 result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
376 EXPECT_TRUE(result);
377 auto& powerMgrClient = PowerMgrClient::GetInstance();
378 PowerMode mode = PowerMode::PERFORMANCE_MODE;
379 powerMgrClient.SetDeviceMode(mode);
380 CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
381
382 POWER_HILOGD(LABEL_TEST, "PowerSavemode_022 UnRegisterPowerModeCallback end.");
383 }
384
385 /**
386 * @tc.name: PowerSavemode_023
387 * @tc.desc: ReceiveEvent
388 * @tc.type: FUNC
389 * @tc.require: issueI5MJZJ
390 */
391 HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_023, TestSize.Level0)
392 {
393 GTEST_LOG_(INFO) << "PowerSavemode_023: UnRegisterPowerModeCallback start.";
394
395 bool result = false;
396 MatchingSkills matchingSkills;
397 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
398 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
399 auto subscriberPtr = std::make_shared<CommonEventServiCesSystemTest>(subscribeInfo);
400 result = CommonEventManager::SubscribeCommonEvent(subscriberPtr);
401 EXPECT_TRUE(result);
402 auto& powerMgrClient = PowerMgrClient::GetInstance();
403 PowerMode mode = PowerMode::EXTREME_POWER_SAVE_MODE;
404 powerMgrClient.SetDeviceMode(mode);
405 CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
406
407 POWER_HILOGD(LABEL_TEST, "PowerSavemode_023 end.");
408 }
409 } // namespace
410