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