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
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_service.h"
28 #include "power_state_machine.h"
29 #include "setting_helper.h"
30 #include "json/reader.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::PowerMgr;
34 using namespace OHOS;
35 using namespace std;
36 static sptr<PowerMgrService> g_service;
37 static constexpr int32_t SLEEP_WAIT_TIME_S = 2;
38 static constexpr int32_t SLEEP_WAIT_TIME_MS = 400;
39 static constexpr int32_t DISPLAY_OFF_TIME_MS = 600;
40 static constexpr int32_t RECOVER_DISPLAY_OFF_TIME_S = 30 * 1000;
41 static constexpr int32_t DISPLAY_POWER_MANAGER_ID = 3308;
42 static const std::string TEST_DEVICE_ID = "test_device_id";
43
SetUpTestCase(void)44 void PowerWakeupTest::SetUpTestCase(void)
45 {
46 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
47 g_service->OnStart();
48 g_service->OnAddSystemAbility(DISPLAY_POWER_MANAGER_ID, TEST_DEVICE_ID);
49 }
50
TearDownTestCase(void)51 void PowerWakeupTest::TearDownTestCase(void)
52 {
53 g_service->OnStop();
54 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
55 }
56
57 namespace {
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)58 MMI::PointerEvent::PointerItem CreatePointerItem(
59 int32_t pointerId, int32_t deviceId, const std::pair<int32_t, int32_t>& displayLocation, bool isPressed)
60 {
61 MMI::PointerEvent::PointerItem item;
62 item.SetPointerId(pointerId);
63 item.SetDeviceId(deviceId);
64 item.SetDisplayX(displayLocation.first);
65 item.SetDisplayY(displayLocation.second);
66 item.SetPressed(isPressed);
67 return item;
68 }
69
70 /**
71 * @tc.name: PowerWakeupTest001
72 * @tc.desc: test keyboard wakeup and powerkey pressed at the same time
73 * @tc.type: FUNC
74 */
75 HWTEST_F(PowerWakeupTest, PowerWakeupTest001, TestSize.Level0)
76 {
77 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest001: start");
78 g_service->WakeupControllerInit();
79 g_service->SuspendControllerInit();
80 g_service->OverrideScreenOffTime(5000);
81 g_service->SuspendDevice(
82 static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
83 EXPECT_FALSE(g_service->IsScreenOn());
84
85 auto inputManager = MMI::InputManager::GetInstance();
86
87 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyDown = MMI::KeyEvent::Create();
88 keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
89 keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
90
91 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyUp = MMI::KeyEvent::Create();
92 keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
93 keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER);
94
95 std::shared_ptr<MMI::KeyEvent> keyEventKeyboard = MMI::KeyEvent::Create();
96 keyEventKeyboard->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
97 keyEventKeyboard->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
98
99 inputManager->SimulateInputEvent(keyEventKeyboard);
100 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
101 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
102
103 sleep(2);
104 EXPECT_TRUE(g_service->IsScreenOn());
105 g_service->RestoreScreenOffTime();
106 }
107
108 /**
109 * @tc.name: PowerWakeupTest002
110 * @tc.desc: test simulate normal key event when screenoff
111 * @tc.type: FUNC
112 */
113 HWTEST_F(PowerWakeupTest, PowerWakeupTest002, TestSize.Level0)
114 {
115 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest002: start");
116
117 g_service->WakeupControllerInit();
118 g_service->SuspendControllerInit();
119 g_service->SuspendDevice(
120 static_cast<int64_t>(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false);
121 EXPECT_FALSE(g_service->IsScreenOn());
122
123 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyDown = MMI::KeyEvent::Create();
124 keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN);
125 keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
126 std::shared_ptr<MMI::KeyEvent> keyEventPowerkeyUp = MMI::KeyEvent::Create();
127 keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
128 keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_0);
129
130 auto inputManager = MMI::InputManager::GetInstance();
131 inputManager->SimulateInputEvent(keyEventPowerkeyDown);
132 inputManager->SimulateInputEvent(keyEventPowerkeyUp);
133 sleep(1);
134 EXPECT_TRUE(g_service->IsScreenOn());
135
136 POWER_HILOGI(LABEL_TEST, "PowerWakeupTest002: end");
137 }
138 } // namespace