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