• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <gtest/gtest.h>
17 #include <limits>
18 #include <test_header.h>
19 
20 #include "hgm_frame_rate_manager.h"
21 #include "hgm_test_base.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr int32_t waitTaskFinishNs = 100000;
30 constexpr pid_t appPid = 0;
31 constexpr uint32_t touchCount = 1;
32 constexpr uint32_t delay_60Ms = 60;
33 constexpr uint32_t delay_110Ms = 110;
34 }
35 
36 class HgmPointerManagerTest : public HgmTestBase {
37 public:
SetUpTestCase()38     static void SetUpTestCase()
39     {
40         HgmTestBase::SetUpTestCase();
41     }
TearDownTestCase()42     static void TearDownTestCase() {}
SetUp()43     void SetUp() {}
TearDown()44     void TearDown() {}
45     HgmErrCode getPointerVoteInfo(VoteInfo& pointerVoteInfo);
46 };
47 
48 /**
49  * @tc.name: QuickClick
50  * @tc.desc: Verify the result of QuickClick function
51  * @tc.type: FUNC
52  * @tc.require:
53  */
54 HWTEST_F(HgmPointerManagerTest, QuickClick, Function | SmallTest | Level0)
55 {
56     int32_t clickNum = 100;
57     auto pointerManager = HgmPointerManager();
58     for (int i = 0; i < clickNum; i++) {
59         pointerManager.ChangeState(PointerState::POINTER_ACTIVE_STATE);
60         pointerManager.ChangeState(PointerState::POINTER_IDLE_STATE);
61     }
62     pointerManager.ChangeState(PointerState::POINTER_IDLE_STATE);
63     sleep(1); // wait for 1s for the async task to complete
64     ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_IDLE_STATE);
65 }
66 
67 /**
68  * @tc.name: ChangeState
69  * @tc.desc: Verify the result of ChangeState function
70  * @tc.type: FUNC
71  * @tc.require:
72  */
73 HWTEST_F(HgmPointerManagerTest, ChangeState, Function | SmallTest | Level0)
74 {
75     PART("CaseDescription") {
76         auto pointerManager = HgmPointerManager();
77         ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_IDLE_STATE);
78 
79         pointerManager.ChangeState(PointerState::POINTER_ACTIVE_STATE);
80         usleep(waitTaskFinishNs);
81         ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
82 
83         pointerManager.ChangeState(PointerState::POINTER_IDLE_STATE);
84         usleep(waitTaskFinishNs);
85         ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_IDLE_STATE);
86     }
87 }
88 
89 /**
90  * @tc.name: Active2IdleState
91  * @tc.desc: Verify the result of Active2IdleState function
92  * @tc.type: FUNC
93  * @tc.require:
94  */
95 HWTEST_F(HgmPointerManagerTest, Active2IdleState, Function | SmallTest | Level0)
96 {
97     PART("CaseDescription") {
98         auto pointerManager = HgmPointerManager();
99         const int32_t handleRsFrameUs = 1100000;
100         const int32_t handleRsFrameNum = 2;
101         const PointerState undefinedState = static_cast<PointerState>(100);
102 
103         STEP("1. 3000ms timeout") {
104             pointerManager.ChangeState(PointerState::POINTER_ACTIVE_STATE);
105             usleep(waitTaskFinishNs);
106             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
107 
108             for (int i = 0; i < handleRsFrameNum; i++) {
109                 pointerManager.HandleRsFrame();
110                 usleep(handleRsFrameUs);
111                 ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
112             }
113             usleep(handleRsFrameUs);
114             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_IDLE_STATE);
115         }
116         STEP("2. State2String") {
117             pointerManager.State2String(undefinedState);
118         }
119         STEP("3. CheckChangeStateValid") {
120             pointerManager.CheckChangeStateValid(PointerState::POINTER_IDLE_STATE, undefinedState);
121             pointerManager.CheckChangeStateValid(undefinedState, PointerState::POINTER_IDLE_STATE);
122         }
123         STEP("4. ExecuteCallback") {
124             pointerManager.ExecuteCallback(nullptr);
__anon57abedc80202() 125             pointerManager.ExecuteCallback([]() { usleep(1); });
126             pointerManager.ExecuteCallback(nullptr);
127         }
128     }
129 }
130 
131 /**
132  * @tc.name: HandleTimerResetEvent
133  * @tc.desc: Verify the result of HandleTimerResetEvent function
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(HgmPointerManagerTest, HandleTimerResetEvent, Function | SmallTest | Level0)
138 {
139     PART("CaseDescription") {
140         auto pointerManager = HgmPointerManager();
141         const int32_t activeTimeoutUs = 1300000;
142         const int32_t handleTimerResetTime = 1000000;
143 
144         STEP("HandleMoveTimeOut") {
145             pointerManager.ChangeState(PointerState::POINTER_ACTIVE_STATE);
146             usleep(waitTaskFinishNs);
147             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
148             usleep(handleTimerResetTime - waitTaskFinishNs);
149             pointerManager.HandleTimerReset();
150             usleep(handleTimerResetTime);
151             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
152             pointerManager.HandleTimerReset();
153             usleep(handleTimerResetTime);
154             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_ACTIVE_STATE);
155             pointerManager.HandleTimerReset();
156             usleep(activeTimeoutUs);
157             ASSERT_EQ(pointerManager.GetState(), PointerState::POINTER_IDLE_STATE);
158         }
159     }
160 }
161 
162 /**
163  * @tc.name: HgmSetPointerActiveFPS
164  * @tc.desc: Verify the result of HgmSetPointerActiveFPS function
165  * @tc.type: FUNC
166  * @tc.require:
167  */
168 HWTEST_F(HgmPointerManagerTest, HgmSetPointerActiveFPS, Function | SmallTest | Level0)
169 {
170     HgmFrameRateManager frameRateMgr;
171     constexpr uint32_t delay_1100Ms = 1100;
172     constexpr uint32_t delay_1300Ms = 1300;
173     frameRateMgr.Init(nullptr, nullptr, nullptr, nullptr);
174     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::TOUCH_BUTTON_DOWN, touchCount);
175     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
176     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
177 
178     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::TOUCH_MOVE, touchCount);
179     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
180     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
181 
182     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::TOUCH_BUTTON_UP, touchCount);
183     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
184     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
185 
186     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::TOUCH_MOVE, touchCount);
187     std::this_thread::sleep_for(std::chrono::milliseconds(delay_1100Ms));
188     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
189 
190     std::this_thread::sleep_for(std::chrono::milliseconds(delay_1300Ms));
191     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_IDLE_STATE);
192 
193     frameRateMgr.pointerManager_.ChangeState(PointerState::POINTER_IDLE_STATE);
194     sleep(1); // wait for handler task finished
195 }
196 
197 /**
198  * @tc.name: HgmSetAxisActiveFPS
199  * @tc.desc: Verify the result of HgmSetAxisActiveFPS function
200  * @tc.type: FUNC
201  * @tc.require:
202  */
203 HWTEST_F(HgmPointerManagerTest, HgmSetAxisActiveFPS, Function | SmallTest | Level1)
204 {
205     HgmFrameRateManager frameRateMgr;
206     constexpr uint32_t delay_1100Ms = 1100;
207     constexpr uint32_t delay_200Ms = 200;
208     frameRateMgr.Init(nullptr, nullptr, nullptr, nullptr);
209     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::AXIS_BEGIN, touchCount);
210     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
211     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
212 
213     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::AXIS_UPDATE, touchCount);
214     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
215     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
216 
217     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::AXIS_END, touchCount);
218     std::this_thread::sleep_for(std::chrono::milliseconds(delay_110Ms));
219     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
220 
221     frameRateMgr.HandleTouchEvent(appPid, TouchStatus::AXIS_UPDATE, touchCount);
222     std::this_thread::sleep_for(std::chrono::milliseconds(delay_1100Ms));
223     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_ACTIVE_STATE);
224 
225     std::this_thread::sleep_for(std::chrono::milliseconds(delay_200Ms));
226     ASSERT_EQ(frameRateMgr.pointerManager_.GetState(), PointerState::POINTER_IDLE_STATE);
227 
228     frameRateMgr.pointerManager_.ChangeState(PointerState::POINTER_IDLE_STATE);
229     sleep(1); // wait for handler task finished
230 }
231 
232 } // namespace Rosen
233 } // namespace OHOS