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_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_state_machine.h"
31 #include "setting_helper.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::PowerMgr;
35 using namespace OHOS;
36 using namespace std;
37
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 constexpr int32_t SCREEN_OFF_TIME_MS = 5000;
44 static const std::string TEST_DEVICE_ID = "test_device_id";
45
46 namespace {
47 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)48 MMI::PointerEvent::PointerItem CreatePointerItem(
49 int32_t pointerId, int32_t deviceId, const std::pair<int32_t, int32_t>& displayLocation, bool isPressed)
50 {
51 MMI::PointerEvent::PointerItem item;
52 item.SetPointerId(pointerId);
53 item.SetDeviceId(deviceId);
54 item.SetDisplayX(displayLocation.first);
55 item.SetDisplayY(displayLocation.second);
56 item.SetPressed(isPressed);
57 return item;
58 }
59 #endif
60
61 /**
62 * @tc.name: PowerWakeupTest001
63 * @tc.desc: test keyboard wakeup and powerkey pressed at the same time
64 * @tc.type: FUNC
65 */
66 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
67 HWTEST_F(PowerWakeupTest, PowerWakeupTest001, TestSize.Level1)
68 {
69 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest001 function start!");
70 auto& powerMgrClient = PowerMgrClient::GetInstance();
71 powerMgrClient.OverrideScreenOffTime(SCREEN_OFF_TIME_MS);
72 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
73 EXPECT_FALSE(powerMgrClient.IsScreenOn());
74
75 auto inputManager = MMI::InputManager::GetInstance();
76
77 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyDown = MMI::KeyEvent::Create();
78 keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
79 keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
80
81 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyUp = MMI::KeyEvent::Create();
82 keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
83 keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
84
85 std::shared_ptr<MMI::KeyEvent> keyEventKeyboard = MMI::KeyEvent::Create();
86 keyEventKeyboard->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
87 keyEventKeyboard->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
88
89 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
90 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
91 inputManager->SimulateInputEvent(keyEventKeyboard);
92 powerMgrClient.OverrideScreenOffTime(SCREEN_OFF_TIME_MS);
93 sleep(2);
94 EXPECT_TRUE(powerMgrClient.IsScreenOn());
95 powerMgrClient.RestoreScreenOffTime();
96 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest001 function end!");
97 }
98 #endif
99
100 /**
101 * @tc.name: PowerWakeupTest002
102 * @tc.desc: test simulate normal key event when screenoff
103 * @tc.type: FUNC
104 */
105 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
106 HWTEST_F(PowerWakeupTest, PowerWakeupTest002, TestSize.Level1)
107 {
108 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest002 function start!");
109 auto& powerMgrClient = PowerMgrClient::GetInstance();
110 powerMgrClient.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
111 EXPECT_FALSE(powerMgrClient.IsScreenOn());
112
113 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyDown = MMI::KeyEvent::Create();
114 keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
115 keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
116 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyUp = MMI::KeyEvent::Create();
117 keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
118 keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
119
120 auto inputManager = MMI::InputManager::GetInstance();
121 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
122 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
123 sleep(1);
124 EXPECT_TRUE(powerMgrClient.IsScreenOn());
125
126 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest002 function end!");
127 }
128 #endif
129 } // namespace
130