1 /*
2 * Copyright (c) 2023-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 #include "power_wakeup_controller_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19 #include <cJSON.h>
20 #include <datetime_ex.h>
21 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
22 #include <input_manager.h>
23 #include "axis_event.h"
24 #include "input_device.h"
25 #include "pointer_event.h"
26 #endif
27 #include <securec.h>
28
29 #include "power_mgr_client.h"
30 #include "power_mgr_service.h"
31 #include "power_state_machine.h"
32 #include "setting_helper.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::PowerMgr;
36 using namespace OHOS;
37 using namespace std;
38 static sptr<PowerMgrService> g_service;
39 static constexpr int32_t SLEEP_WAIT_TIME_S = 2;
40 static constexpr int32_t SLEEP_WAIT_TIME_MS = 400;
41 static constexpr int32_t DISPLAY_OFF_TIME_MS = 600;
42 static constexpr int32_t RECOVER_DISPLAY_OFF_TIME_S = 30 * 1000;
43 static constexpr int32_t DISPLAY_POWER_MANAGER_ID = 3308;
44 static const std::string TEST_DEVICE_ID = "test_device_id";
45
46 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
47 class InputCallbackMock : public IInputEventConsumer {
48 public:
49 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
50 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
51 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
52 };
53 #endif
54
SetUpTestCase(void)55 void PowerWakeupControllerTest::SetUpTestCase(void)
56 {
57 PowerMgrClient::GetInstance().SuspendDevice();
58 EXPECT_FALSE(PowerMgrClient::GetInstance().IsScreenOn());
59 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
60 g_service->OnStart();
61 g_service->OnAddSystemAbility(DISPLAY_POWER_MANAGER_ID, TEST_DEVICE_ID);
62 }
63
TearDownTestCase(void)64 void PowerWakeupControllerTest::TearDownTestCase(void)
65 {
66 g_service->OnStop();
67 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
68 }
69
70 namespace {
71 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)72 MMI::PointerEvent::PointerItem CreatePointerItem(
73 int32_t pointerId, int32_t deviceId, const std::pair<int32_t, int32_t>& displayLocation, bool isPressed)
74 {
75 MMI::PointerEvent::PointerItem item;
76 item.SetPointerId(pointerId);
77 item.SetDeviceId(deviceId);
78 item.SetDisplayX(displayLocation.first);
79 item.SetDisplayY(displayLocation.second);
80 item.SetPressed(isPressed);
81 return item;
82 }
83 #endif
84
85 /**
86 * @tc.name: PowerWakeupControllerTest001
87 * @tc.desc: test ExecWakeupMonitorByReason(Normal and exception)
88 * @tc.type: FUNC
89 * @tc.require: issueI7COGR
90 */
91 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
92 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest001, TestSize.Level0)
93 {
94 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest001 function start!");
95 GTEST_LOG_(INFO) << "PowerWakeupControllerTest001: start";
96 g_service->WakeupControllerInit();
97 g_service->wakeupController_->ExecWakeupMonitorByReason(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
98 auto monitor = g_service->wakeupController_->monitorMap_[WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON];
99 EXPECT_TRUE(monitor != nullptr);
100
101 GTEST_LOG_(INFO) << "PowerWakeupControllerTest001: end";
102 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest001 function end!");
103 }
104 #endif
105
106 /**
107 * @tc.name: PowerWakeupControllerTest002
108 * @tc.desc: test Wakeup(Normal)
109 * @tc.type: FUNC
110 * @tc.require: issueI7COGR
111 */
112 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest002, TestSize.Level0)
113 {
114 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest002 function start!");
115 GTEST_LOG_(INFO) << "PowerWakeupControllerTest002: start";
116 sleep(SLEEP_WAIT_TIME_S);
117 g_service->WakeupControllerInit();
118 // test Normal
119 g_service->wakeupController_->Wakeup();
120 EXPECT_TRUE(g_service->wakeupController_ != nullptr);
121 GTEST_LOG_(INFO) << "PowerWakeupControllerTest002: end";
122 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest002 function end!");
123 }
124
125 /**
126 * @tc.name: PowerWakeupControllerTest003
127 * @tc.desc: test ControlListener(Normal)
128 * @tc.type: FUNC
129 * @tc.require: issueI7COGR
130 */
131 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest003, TestSize.Level0)
132 {
133 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest003 function start!");
134 GTEST_LOG_(INFO) << "PowerWakeupControllerTest003: start";
135
136 g_service->WakeupControllerInit();
137 g_service->SuspendControllerInit();
138
139 g_service->SuspendDevice(
140 static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
141 g_service->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
142 EXPECT_TRUE(g_service->wakeupController_ != nullptr);
143 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
144 WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerWakeupControllerTest003");
145 g_service->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
146 EXPECT_TRUE(static_cast<uint32_t>(g_service->wakeupController_->stateMachine_->GetState()) ==
147 static_cast<uint32_t>(PowerState::AWAKE));
148
149 g_service->wakeupController_->stateMachine_->EmplaceAwake();
150 g_service->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
151 EXPECT_TRUE(static_cast<uint32_t>(g_service->wakeupController_->stateMachine_->GetState()) ==
152 static_cast<uint32_t>(PowerState::AWAKE));
153
154 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
155 WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerWakeupControllerTest003");
156 g_service->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
157 EXPECT_TRUE(static_cast<uint32_t>(g_service->wakeupController_->stateMachine_->GetState()) ==
158 static_cast<uint32_t>(PowerState::AWAKE));
159
160 g_service->wakeupController_->stateMachine_->SetState(
161 PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT);
162 g_service->suspendController_->stateMachine_->controllerMap_.clear();
163 g_service->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
164 EXPECT_TRUE(static_cast<uint32_t>(g_service->wakeupController_->stateMachine_->GetState()) ==
165 static_cast<uint32_t>(PowerState::INACTIVE));
166 g_service->suspendController_->stateMachine_->InitStateMap();
167
168 GTEST_LOG_(INFO) << "PowerWakeupControllerTest003: end";
169 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest003 function end!");
170 }
171
172 /**
173 * @tc.name: PowerWakeupControllerTest004
174 * @tc.desc: test GetTargetPath
175 * @tc.type: FUNC
176 * @tc.require: issueI7G6OY
177 */
178 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest004, TestSize.Level0)
179 {
180 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest004 function start!");
181 GTEST_LOG_(INFO) << "PowerWakeupControllerTest004: start";
182 std::string targetPath;
183 WakeupSourceParser::GetTargetPath(targetPath);
184 EXPECT_TRUE(targetPath.size() != 0);
185 GTEST_LOG_(INFO) << "PowerWakeupControllerTest004: end";
186 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest004 function end!");
187 }
188
189 /**
190 * @tc.name: PowerWakeupControllerTest005
191 * @tc.desc: test CreateMonitor
192 * @tc.type: FUNC
193 * @tc.require: issueI7G6OY
194 */
195 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest005, TestSize.Level0)
196 {
197 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest005 function start!");
198 GTEST_LOG_(INFO) << "PowerWakeupControllerTest005: start";
199 g_service->WakeupControllerInit();
200 WakeupSource source1(WakeupDeviceType::WAKEUP_DEVICE_SINGLE_CLICK, 1, 0);
201 std::shared_ptr<WakeupMonitor> monitor1 = WakeupMonitor::CreateMonitor(source1);
202 EXPECT_TRUE(monitor1 != nullptr);
203
204 WakeupSource source2(WakeupDeviceType::WAKEUP_DEVICE_MAX, 1, 0);
205 std::shared_ptr<WakeupMonitor> monitor2 = WakeupMonitor::CreateMonitor(source2);
206 EXPECT_TRUE(static_cast<uint32_t>(source2.reason_) == static_cast<uint32_t>(WakeupDeviceType::WAKEUP_DEVICE_MAX));
207 GTEST_LOG_(INFO) << "PowerWakeupControllerTest005: end";
208 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest005 function end!");
209 }
210
211 /**
212 * @tc.name: PowerWakeupControllerTest006
213 * @tc.desc: test Cancel(Normal and exception)
214 * @tc.type: FUNC
215 * @tc.require: issueI7COGR
216 */
217 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest006, TestSize.Level0)
218 {
219 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest006 function start!");
220 GTEST_LOG_(INFO) << "PowerWakeupControllerTest006: start";
221
222 g_service->WakeupControllerInit();
223 g_service->wakeupController_->Cancel();
224 EXPECT_TRUE(g_service->wakeupController_->monitorMap_.size() == 0);
225 GTEST_LOG_(INFO) << "PowerWakeupControllerTest006: end";
226 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest006 function end!");
227 }
228
229 /**
230 * @tc.name: PowerWakeupControllerTest007
231 * @tc.desc: test OnInputEvent(Normal)
232 * @tc.type: FUNC
233 * @tc.require: issueI7COGR
234 */
235 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest007, TestSize.Level0)
236 {
237 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest007 function start!");
238 GTEST_LOG_(INFO) << "PowerWakeupControllerTest007: start";
239 g_service->WakeupControllerInit();
240 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
241 InputCallback* callback = new InputCallback();
242 InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
243 std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
244 keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
245
246 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F1);
247 callback_mock->OnInputEvent(keyEvent);
248 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_0);
249 callback_mock->OnInputEvent(keyEvent);
250 keyEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_F2);
251 callback_mock->OnInputEvent(keyEvent);
252 delete callback;
253 EXPECT_TRUE(g_service->wakeupController_ != nullptr);
254 #endif
255 GTEST_LOG_(INFO) << "PowerWakeupControllerTest007: end";
256 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest007 function end!");
257 }
258
259 /**
260 * @tc.name: PowerWakeupControllerTest008
261 * @tc.desc: test OnInputEvent(Normal 1)
262 * @tc.type: FUNC
263 * @tc.require: issueI7COGR
264 */
265 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest008, TestSize.Level0)
266 {
267 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest008 function start!");
268 GTEST_LOG_(INFO) << "PowerWakeupControllerTest008: start";
269
270 g_service->WakeupControllerInit();
271
272 constexpr int32_t DRAG_DST_X {500};
273 constexpr int32_t DRAG_DST_Y {500};
274 int32_t deviceMouseId {0};
275
276 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
277 InputCallback* callback = new InputCallback();
278 InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
279 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
280 MMI::PointerEvent::PointerItem curPointerItem;
281 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
282 pointerEvent->SetPointerId(0);
283
284 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
285 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
286 pointerEvent->AddPointerItem(curPointerItem);
287 callback_mock->OnInputEvent(pointerEvent);
288
289 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
290 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
291 pointerEvent->AddPointerItem(curPointerItem);
292 callback_mock->OnInputEvent(pointerEvent);
293
294 pointerEvent->SetSourceType(PointerEvent::TOOL_TYPE_PEN);
295 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
296 pointerEvent->AddPointerItem(curPointerItem);
297 callback_mock->OnInputEvent(pointerEvent);
298 EXPECT_TRUE(g_service->wakeupController_ != nullptr);
299 delete callback;
300 #endif
301 GTEST_LOG_(INFO) << "PowerWakeupControllerTest008: end";
302 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest008 function end!");
303 }
304
305 /**
306 * @tc.name: PowerWakeupControllerTest009
307 * @tc.desc: test OnInputEvent(Normal 2)
308 * @tc.type: FUNC
309 * @tc.require: issueI7COGR
310 */
311 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest009, TestSize.Level0)
312 {
313 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest009 function start!");
314 GTEST_LOG_(INFO) << "PowerWakeupControllerTest009: start";
315
316 g_service->WakeupControllerInit();
317
318 constexpr int32_t DRAG_DST_X {500};
319 constexpr int32_t DRAG_DST_Y {500};
320 int32_t deviceMouseId {0};
321
322 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
323 InputCallback* callback = new InputCallback();
324 InputCallbackMock* callback_mock = reinterpret_cast<InputCallbackMock*>(callback);
325 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
326 MMI::PointerEvent::PointerItem curPointerItem;
327 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
328 pointerEvent->SetPointerId(0);
329
330 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
331 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
332 pointerEvent->AddPointerItem(curPointerItem);
333 callback_mock->OnInputEvent(pointerEvent);
334
335 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
336 curPointerItem = CreatePointerItem(0, deviceMouseId, {DRAG_DST_X, DRAG_DST_Y}, true);
337 pointerEvent->AddPointerItem(curPointerItem);
338 callback_mock->OnInputEvent(pointerEvent);
339
340 std::shared_ptr<MMI::PointerEvent> pointerEvent2 = MMI::PointerEvent::Create();
341 pointerEvent2->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
342 pointerEvent2->SetPointerId(0);
343 curPointerItem = CreatePointerItem(1, PointerEvent::TOOL_TYPE_PEN, {DRAG_DST_X, DRAG_DST_Y}, true);
344 curPointerItem.SetToolType(PointerEvent::TOOL_TYPE_PEN);
345 pointerEvent2->AddPointerItem(curPointerItem);
346 callback_mock->OnInputEvent(pointerEvent2);
347
348 curPointerItem = CreatePointerItem(0, PointerEvent::TOOL_TYPE_PEN, {DRAG_DST_X, DRAG_DST_Y}, true);
349 curPointerItem.SetToolType(PointerEvent::TOOL_TYPE_PEN);
350 pointerEvent2->AddPointerItem(curPointerItem);
351 callback_mock->OnInputEvent(pointerEvent2);
352
353 delete callback;
354 #endif
355 EXPECT_TRUE(g_service->wakeupController_ != nullptr);
356 GTEST_LOG_(INFO) << "PowerWakeupControllerTest009: end";
357 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest009 function end!");
358 }
359
360 /**
361 * @tc.name: PowerWakeupControllerTest010
362 * @tc.desc: test getSourceKeys
363 * @tc.type: FUNC
364 * @tc.require: issueI7COGR
365 */
366 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest010, TestSize.Level0)
367 {
368 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest010 function start!");
369 GTEST_LOG_(INFO) << "PowerWakeupControllerTest010: start";
370
371 std::shared_ptr<WakeupSources> sources = WakeupSourceParser::ParseSources();
372 std::vector<std::string> tmp = sources->getSourceKeys();
373 EXPECT_TRUE(tmp.size() != 0);
374 GTEST_LOG_(INFO) << "PowerWakeupControllerTest010: end";
375 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest010 function end!");
376 }
377
378 /**
379 * @tc.name: PowerWakeupControllerTest011
380 * @tc.desc: test ParseSourcesProc(exception)
381 * @tc.type: FUNC
382 * @tc.require: issueI7COGR
383 */
384 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest011, TestSize.Level0)
385 {
386 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest011 function start!");
387 GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: start";
388
389 static const std::string jsonStr =
390 "{\"powerkey\": {\"enable\": false},\"keyborad\": {\"enable\": false},\"mouse\": {\"enable\": "
391 "false},\"touchscreen\": {\"enable\": false,\"click\": 2},\"touchpad\": {\"enable\": false},\"pen\": "
392 "{\"enable\": "
393 "false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": true},\"xxx\": {\"enable\": false}}";
394
395 std::shared_ptr<WakeupSources> parseSources = std::make_shared<WakeupSources>();
396
397 cJSON* root = cJSON_Parse(jsonStr.c_str());
398 if (!root) {
399 GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: json parse error";
400 return;
401 }
402 if (!cJSON_IsObject(root) || !cJSON_IsArray(root)) {
403 GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: root is not object";
404 cJSON_Delete(root);
405 return;
406 }
407
408 cJSON* item = NULL;
cJSON_ArrayForEach(item,root)409 cJSON_ArrayForEach(item, root) {
410 const char* key = item->string;
411 if (!key) {
412 continue;
413 }
414 std::string keyStr = std::string(key);
415 WakeupSourceParser::ParseSourcesProc(parseSources, item, keyStr);
416 }
417
418 cJSON_Delete(root);
419
420 EXPECT_TRUE(parseSources->GetSourceList().size() != 0);
421 GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: end";
422 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest011 function end!");
423 }
424
425 /**
426 * @tc.name: PowerWakeupControllerTest012
427 * @tc.desc: test OnInputEvent KeyEvent RefreshActivity
428 * @tc.type: FUNC
429 */
430 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest012, TestSize.Level0)
431 {
432 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest012 function start!");
433 GTEST_LOG_(INFO) << "PowerWakeupControllerTest012: start";
434 g_service->WakeupControllerInit();
435 g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS);
436 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
437 WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, "PowerWakeupControllerTest012");
438 EXPECT_TRUE(g_service->IsScreenOn());
439 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
440 usleep(SLEEP_WAIT_TIME_MS * 1000);
441 std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
442 InputCallback callback;
443 callback.OnInputEvent(keyEvent);
444 usleep(SLEEP_WAIT_TIME_MS * 1000);
445 EXPECT_TRUE(g_service->IsScreenOn());
446 #endif
447 g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S);
448 GTEST_LOG_(INFO) << "PowerWakeupControllerTest012: end";
449 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest012 function end!");
450 }
451
452 /**
453 * @tc.name: PowerWakeupControllerTest013
454 * @tc.desc: test OnInputEvent PointerEvent RefreshActivity
455 * @tc.type: FUNC
456 */
457 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest013, TestSize.Level0)
458 {
459 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest013 function start!");
460 GTEST_LOG_(INFO) << "PowerWakeupControllerTest013: start";
461 g_service->WakeupControllerInit();
462 g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS);
463 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
464 WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, "PowerWakeupControllerTest013");
465 EXPECT_TRUE(g_service->IsScreenOn());
466 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
467 usleep(SLEEP_WAIT_TIME_MS * 1000);
468 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
469 InputCallback callback;
470 callback.OnInputEvent(pointerEvent);
471 usleep(SLEEP_WAIT_TIME_MS * 1000);
472 EXPECT_TRUE(g_service->IsScreenOn());
473 #endif
474 g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S);
475 GTEST_LOG_(INFO) << "PowerWakeupControllerTest013: end";
476 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest013 function end!");
477 }
478
479 /**
480 * @tc.name: PowerWakeupControllerTest014
481 * @tc.desc: test OnInputEvent AxisEvent RefreshActivity
482 * @tc.type: FUNC
483 */
484 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest014, TestSize.Level0)
485 {
486 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest014 function start!");
487 GTEST_LOG_(INFO) << "PowerWakeupControllerTest014: start";
488 g_service->WakeupControllerInit();
489 g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS);
490 g_service->WakeupDevice(static_cast<int64_t>(time(nullptr)),
491 WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, "PowerWakeupControllerTest014");
492 EXPECT_TRUE(g_service->IsScreenOn());
493 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
494 usleep(SLEEP_WAIT_TIME_MS * 1000);
495 std::shared_ptr<MMI::AxisEvent> axisEvent = MMI::AxisEvent::Create();
496 InputCallback callback;
497 callback.OnInputEvent(axisEvent);
498 usleep(SLEEP_WAIT_TIME_MS * 1000);
499 EXPECT_TRUE(g_service->IsScreenOn());
500 #endif
501 g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S);
502 GTEST_LOG_(INFO) << "PowerWakeupControllerTest014: end";
503 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest014 function end!");
504 }
505
506 #ifdef POWER_DOUBLECLICK_ENABLE
507 /**
508 * @tc.name: PowerWakeupControllerTest015
509 * @tc.desc: test switch to turn on the screen by double click
510 * @tc.type: FUNC
511 */
512 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest015, TestSize.Level0)
513 {
514 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest015 function start!");
515 GTEST_LOG_(INFO) << "PowerWakeupControllerTest015: start";
516 g_service->WakeupControllerInit();
517 auto wakeupController_ = g_service->GetWakeupController();
518 EXPECT_TRUE(wakeupController_ != nullptr);
519 wakeupController_->ChangeWakeupSourceConfig(true);
520 auto resCode = wakeupController_->SetWakeupDoubleClickSensor(true);
521 EXPECT_TRUE(resCode != -1);
522 GTEST_LOG_(INFO) << "PowerWakeupControllerTest015: end";
523 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest015 function end!");
524 }
525 #endif
526 #ifdef POWER_PICKUP_ENABLE
527 /**
528 * @tc.name: PowerWakeupControllerTest016
529 * @tc.desc: test switch to turn on the screen by raising your hand
530 * @tc.type: FUNC
531 */
532 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest016, TestSize.Level0)
533 {
534 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest016 function start!");
535 GTEST_LOG_(INFO) << "PowerWakeupControllerTest016: start";
536 g_service->WakeupControllerInit();
537 auto wakeupController_ = g_service->GetWakeupController();
538 EXPECT_TRUE(wakeupController_ != nullptr);
539 wakeupController_->ChangePickupWakeupSourceConfig(true);
540 wakeupController_->PickupConnectMotionConfig(true);
541 GTEST_LOG_(INFO) << "PowerWakeupControllerTest016: end";
542 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest016 function end!");
543 }
544 #endif
545
546 /**
547 * @tc.name: PowerWakeupControllerTest017
548 * @tc.desc: test ExecWakeupMonitorByReason(WAKEUP_DEVICE_PEN)
549 * @tc.type: FUNC
550 * @tc.require: issueI9V16C
551 */
552 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest017, TestSize.Level0)
553 {
554 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest017 function start!");
555 GTEST_LOG_(INFO) << "PowerWakeupControllerTest017: start";
556 g_service->WakeupControllerInit();
557 g_service->wakeupController_->ExecWakeupMonitorByReason(WakeupDeviceType ::WAKEUP_DEVICE_PEN);
558 auto monitor = g_service->wakeupController_->monitorMap_[WakeupDeviceType ::WAKEUP_DEVICE_PEN];
559 EXPECT_TRUE(monitor != nullptr);
560
561 GTEST_LOG_(INFO) << "PowerWakeupControllerTest017: end";
562 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest017 function end!");
563 }
564
565 /**
566 * @tc.name: PowerWakeupControllerTest018
567 * @tc.desc: test ExecWakeupMonitorByReason(WAKEUP_DEVICE_DOUBLE_CLICK)
568 * @tc.type: FUNC
569 * @tc.require: issueI9V16C
570 */
571 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest018, TestSize.Level0)
572 {
573 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest018 function start!");
574 GTEST_LOG_(INFO) << "PowerWakeupControllerTest018: start";
575 g_service->WakeupControllerInit();
576 g_service->wakeupController_->ExecWakeupMonitorByReason(WakeupDeviceType ::WAKEUP_DEVICE_DOUBLE_CLICK);
577 auto monitor = g_service->wakeupController_->monitorMap_[WakeupDeviceType ::WAKEUP_DEVICE_DOUBLE_CLICK];
578 EXPECT_TRUE(monitor != nullptr);
579
580 GTEST_LOG_(INFO) << "PowerWakeupControllerTest018: end";
581 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest018 function end!");
582 }
583
584 /**
585 * @tc.name: PowerWakeupControllerTest019
586 * @tc.desc: test simulate powerkey event when screenoff
587 * @tc.type: FUNC
588 */
589 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest019, TestSize.Level0)
590 {
591 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest019 function start!");
592 GTEST_LOG_(INFO) << "PowerWakeupControllerTest019: start";
593
594 g_service->WakeupControllerInit();
595 g_service->SuspendControllerInit();
596 g_service->SuspendDevice(
597 static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
598 EXPECT_FALSE(g_service->IsScreenOn());
599 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
600 auto inputManager = MMI::InputManager::GetInstance();
601 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyDown = MMI::KeyEvent::Create();
602 keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
603 keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
604 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyUp = MMI::KeyEvent::Create();
605 keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
606 keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
607
608 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
609 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
610 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
611 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
612 sleep(SLEEP_WAIT_TIME_S);
613 //wake it up when the screen goes off after timeout
614 g_service->RefreshActivity(
615 static_cast<int64_t>(std::chrono::system_clock::now().time_since_epoch().count()),
616 UserActivityType::USER_ACTIVITY_TYPE_TOUCH, true);
617 EXPECT_TRUE(g_service->IsScreenOn());
618 #endif
619 GTEST_LOG_(INFO) << "PowerWakeupControllerTest019: end";
620 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest019 function end!");
621 }
622
623 /**
624 * @tc.name: PowerWakeupControllerTest020
625 * @tc.desc: test ProcessPowerOffInternalScreenOnly and ProcessPowerOnInternalScreenOnly
626 * @tc.type: FUNC
627 * @tc.require: issueI7COGR
628 */
629 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest020, TestSize.Level0)
630 {
631 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest020 function start!");
632 GTEST_LOG_(INFO) << "PowerWakeupControllerTest020: start";
633 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT
634 g_service->SuspendControllerInit();
635 g_service->WakeupControllerInit();
636 auto powerStateMachine = g_service->GetPowerStateMachine();
637 auto suspendController = g_service->GetSuspendController();
638 auto wakeupController = g_service->GetWakeupController();
639
640 // Mock open switch to wakeup deivce
641 powerStateMachine->SetSwitchState(true);
642 g_service->WakeupDevice(
643 static_cast<int64_t>(time(nullptr)), WakeupDeviceType::WAKEUP_DEVICE_SWITCH, "PowerWakeupControllerTest020");
644 EXPECT_TRUE(powerStateMachine->IsScreenOn());
645 // Mock close switch to suspend deivce, action is configured as ACTION_NONE
646 powerStateMachine->SetSwitchState(false);
647 suspendController->ControlListener(SuspendDeviceType::SUSPEND_DEVICE_REASON_SWITCH, SuspendAction::ACTION_NONE, 0);
648 EXPECT_TRUE(powerStateMachine->IsScreenOn());
649
650 // Mock open switch to power on internal screen
651 powerStateMachine->SetSwitchState(true);
652 wakeupController->ControlListener(WakeupDeviceType::WAKEUP_DEVICE_SWITCH);
653 EXPECT_TRUE(powerStateMachine->IsScreenOn());
654 #endif
655 GTEST_LOG_(INFO) << "PowerWakeupControllerTest020: end";
656 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest020 function end!");
657 }
658
659 /**
660 * @tc.name: PowerWakeupControllerTest021
661 * @tc.desc: test RegisterMonitor(Normal)
662 * @tc.type: FUNC
663 * @tc.require: issueI7COGR
664 */
665 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
666 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest021, TestSize.Level0)
667 {
668 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021 function start!");
669 auto powerStateMachine = g_service->GetPowerStateMachine();
670 g_service->WakeupControllerInit();
671 auto wakeupController_ = g_service->GetWakeupController();
672 EXPECT_TRUE(wakeupController_ != nullptr);
673 EXPECT_EQ(wakeupController_->monitorId_, 0);
674 wakeupController_->RegisterMonitor(PowerState::AWAKE);
675 EXPECT_EQ(wakeupController_->monitorId_, 0);
676 wakeupController_->RegisterMonitor(PowerState::INACTIVE);
677 EXPECT_EQ(wakeupController_->monitorId_, 0);
678 wakeupController_->RegisterMonitor(PowerState::UNKNOWN);
679 EXPECT_EQ(wakeupController_->monitorId_, 0);
680 powerStateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_APPLICATION);
681 EXPECT_EQ(wakeupController_->monitorId_, 0);
682 powerStateMachine->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_APPLICATION);
683 EXPECT_EQ(wakeupController_->monitorId_, 0);
684 InputCallback* callbackFirst = new InputCallback();
685 std::shared_ptr<MMI::KeyEvent> keyEvent = OHOS::MMI::KeyEvent::Create();
686 keyEvent = nullptr;
687 callbackFirst->OnInputEvent(keyEvent);
688 delete callbackFirst;
689 InputCallback* callbackSecond = new InputCallback();
690 std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
691 WakeupDeviceType wakeupType = WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN;
692 wakeupType = callbackSecond->DetermineWakeupDeviceType(PointerEvent::TOOL_TYPE_PEN,
693 PointerEvent::SOURCE_TYPE_UNKNOWN);
694 EXPECT_EQ(wakeupType, WakeupDeviceType::WAKEUP_DEVICE_PEN);
695 wakeupType = callbackSecond->DetermineWakeupDeviceType(PointerEvent::TOOL_TYPE_MOUSE,
696 PointerEvent::SOURCE_TYPE_MOUSE);
697 EXPECT_EQ(wakeupType, WakeupDeviceType::WAKEUP_DEVICE_MOUSE);
698 wakeupType = callbackSecond->DetermineWakeupDeviceType(PointerEvent::TOOL_TYPE_TOUCHPAD,
699 PointerEvent::SOURCE_TYPE_TOUCHPAD);
700 EXPECT_EQ(wakeupType, WakeupDeviceType::WAKEUP_DEVICE_TOUCHPAD);
701 wakeupType = callbackSecond->DetermineWakeupDeviceType(PointerEvent::TOOL_TYPE_TOUCHPAD,
702 PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
703 EXPECT_EQ(wakeupType, WakeupDeviceType::WAKEUP_DEVICE_SINGLE_CLICK);
704 wakeupType = callbackSecond->DetermineWakeupDeviceType(PointerEvent::POINTER_ACTION_UNKNOWN,
705 PointerEvent::SOURCE_TYPE_UNKNOWN);
706 EXPECT_EQ(wakeupType, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN);
707 pointerEvent = nullptr;
708 callbackSecond->OnInputEvent(pointerEvent);
709 delete callbackSecond;
710 InputCallback* callbackThird = new InputCallback();
711 std::shared_ptr<MMI::AxisEvent> axisEvent = MMI::AxisEvent::Create();
712 axisEvent = nullptr;
713 callbackThird->OnInputEvent(axisEvent);
714 delete callbackThird;
715 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021 function end!");
716 }
717 #endif
718
719 /**
720 * @tc.name: PowerWakeupControllerTest022
721 * @tc.desc: test RegisterMonitor(Normal)
722 * @tc.type: FUNC
723 * @tc.require: issueI7COGR
724 */
725 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
726 HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest022, TestSize.Level0)
727 {
728 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022 function start!");
729 GTEST_LOG_(INFO) << "PowerWakeupControllerTest022: start";
730 auto powerStateMachine = g_service->GetPowerStateMachine();
731 g_service->WakeupControllerInit();
732 auto wakeupController_ = g_service->GetWakeupController();
733 g_service->SwitchSubscriberInit();
734 g_service->InputMonitorInit();
735
736 powerStateMachine->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_APPLICATION);
737 wakeupController_->RegisterMonitor(PowerState::AWAKE);
738 EXPECT_EQ(wakeupController_->monitorId_, 0);
739
740 g_service->InputMonitorCancel();
741 g_service->SwitchSubscriberCancel();
742 GTEST_LOG_(INFO) << "PowerWakeupControllerTest022: end";
743 POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022 function end!");
744 }
745 #endif
746 } // namespace