1 /*
2  * Copyright (C) 2023 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 <map>
18 #include <memory>
19 #include "accessibility_common_helper.h"
20 #include "accessibility_screen_touch.h"
21 #include "accessibility_ut_helper.h"
22 #include "accessible_ability_manager_service.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Accessibility {
29 namespace {
30     constexpr uint32_t IGNORE_REPEAT_CLICK_TIME_SHORTEST = 100; // ms
31     constexpr uint32_t CLICK_RESPONSE_TIME_LONG = 600; // ms
32     constexpr uint32_t IGNORE_REPEAT_CLICK_TIME_LONGEST = 1300; // ms
33     constexpr uint32_t CLICK_RESPONSE_DELAY_LONG = 2;
34     constexpr uint32_t IGNORE_REPEAT_CLICK_LONGEST = 4;
35     constexpr uint32_t TIMESTAMP_800 = 800;
36     constexpr uint32_t TIMESTAMP_1200 = 1200;
37     constexpr uint32_t TIMESTAMP_1500 = 1500;
38     constexpr uint32_t TIMESTAMP_1600 = 1600;
39     constexpr uint32_t TIMESTAMP_1700 = 1700;
40     constexpr uint32_t TIMESTAMP_2700 = 2700;
41     constexpr uint32_t TIMESTAMP_2800 = 2800;
42 } // namespace
43 
44 class AccessibilityScreenTouchUnitTest : public ::testing::Test {
45 public:
AccessibilityScreenTouchUnitTest()46     AccessibilityScreenTouchUnitTest()
47     {}
~AccessibilityScreenTouchUnitTest()48     ~AccessibilityScreenTouchUnitTest()
49     {}
50 
51     static void SetUpTestCase();
52     static void TearDownTestCase();
53     void SetUp() override;
54     void TearDown() override;
55     std::shared_ptr<MMI::PointerEvent> SetPointerEvent(uint32_t time, uint32_t action);
56 
57     std::shared_ptr<AccessibilityScreenTouch> screenTouch_ = nullptr;
58 };
59 
SetUpTestCase()60 void AccessibilityScreenTouchUnitTest::SetUpTestCase()
61 {
62     GTEST_LOG_(INFO) << "###################### AccessibilityScreenTouchUnitTest Start ######################";
63     Singleton<AccessibleAbilityManagerService>::GetInstance().OnStart();
64     AccessibilityCommonHelper::GetInstance().WaitForServicePublish();
65 }
66 
TearDownTestCase()67 void AccessibilityScreenTouchUnitTest::TearDownTestCase()
68 {
69     GTEST_LOG_(INFO) << "###################### AccessibilityScreenTouchUnitTest End #####################";
70     Singleton<AccessibleAbilityManagerService>::GetInstance().OnStop();
71 }
72 
SetUp()73 void AccessibilityScreenTouchUnitTest::SetUp()
74 {
75     GTEST_LOG_(INFO) << "SetUp";
76 }
77 
TearDown()78 void AccessibilityScreenTouchUnitTest::TearDown()
79 {
80     GTEST_LOG_(INFO) << "TearDown";
81 }
82 
SetPointerEvent(uint32_t time,uint32_t action)83 std::shared_ptr<MMI::PointerEvent> AccessibilityScreenTouchUnitTest::SetPointerEvent(uint32_t time, uint32_t action)
84 {
85     std::shared_ptr<MMI::PointerEvent> event = MMI::PointerEvent::Create();
86     event->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
87     event->SetPointerAction(action);
88     event->SetPointerId(0);
89     MMI::PointerEvent::PointerItem item;
90     item.SetPointerId(0);
91     event->AddPointerItem(item);
92     event->SetActionTime(time);
93     return event;
94 }
95 
96 /**
97  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_001
98  * @tc.name: OnPointerEvent
99  * @tc.desc: Test function OnPointerEvent
100  */
101 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_001, TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_001 start";
104     sptr<AccessibilityAccountData> accountData =
105         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
106     if (!accountData) {
107         GTEST_LOG_(INFO) << "accountData is nullptr";
108         return;
109     }
110 
111     accountData->GetConfig()->SetClickResponseTime(CLICK_RESPONSE_DELAY_LONG);
112     accountData->GetConfig()->SetIgnoreRepeatClickState(true);
113     accountData->GetConfig()->SetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_LONGEST);
114     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
115 
116     EXPECT_EQ(screenTouch_->GetRealClickResponseTime(), CLICK_RESPONSE_TIME_LONG);
117     EXPECT_EQ(screenTouch_->GetRealIgnoreRepeatClickTime(), IGNORE_REPEAT_CLICK_TIME_LONGEST);
118     EXPECT_EQ(screenTouch_->GetRealIgnoreRepeatClickState(), true);
119     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_001 end";
120 }
121 
122 /**
123  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_002
124  * @tc.name: OnPointerEvent
125  * @tc.desc: Test function OnPointerEvent
126  */
127 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_002, TestSize.Level1)
128 {
129     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_002 start";
130     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
131     std::shared_ptr<MMI::PointerEvent> event = MMI::PointerEvent::Create();
132     if (screenTouch_ == nullptr || event == nullptr) {
133         GTEST_LOG_(INFO) << "null pointer.";
134     }
135 
136     event->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_MOUSE);
137     EXPECT_EQ(screenTouch_->OnPointerEvent(*event), false);
138     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_002 end";
139 }
140 
141 /**
142  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_003
143  * @tc.name: OnPointerEvent
144  * @tc.desc: Test function OnPointerEvent
145  */
146 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_003, TestSize.Level1)
147 {
148     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_003 start";
149     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
150     std::shared_ptr<MMI::PointerEvent> event = MMI::PointerEvent::Create();
151     if (screenTouch_ == nullptr || event == nullptr) {
152         GTEST_LOG_(INFO) << "null pointer";
153     }
154 
155     MMI::PointerEvent::PointerItem pointer = {};
156     pointer.SetPointerId(1);
157     event->SetPointerId(1);
158     event->AddPointerItem(pointer);
159     event->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
160     event->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_CANCEL);
161 
162     EXPECT_EQ(screenTouch_->OnPointerEvent(*event), true);
163     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_003 end.";
164 }
165 
166 /**
167  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_004
168  * @tc.name: OnPointerEvent
169  * @tc.desc: Test function OnPointerEvent
170  */
171 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_004, TestSize.Level1)
172 {
173     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_004 start";
174     sptr<AccessibilityAccountData> accountData =
175         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
176     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
177     if (accountData == nullptr || screenTouch_ == nullptr) {
178         return;
179     }
180 
181     accountData->GetConfig()->SetClickResponseTime(0);
182     accountData->GetConfig()->SetIgnoreRepeatClickState(true);
183     accountData->GetConfig()->SetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_LONGEST);
184 
185     auto eventDown = SetPointerEvent(TIMESTAMP_1500, MMI::PointerEvent::POINTER_ACTION_DOWN);
186     screenTouch_->OnPointerEvent(*eventDown);
187 
188     auto eventMove = SetPointerEvent(TIMESTAMP_1600, MMI::PointerEvent::POINTER_ACTION_MOVE);
189     screenTouch_->OnPointerEvent(*eventMove);
190 
191     auto eventUp = SetPointerEvent(TIMESTAMP_1700, MMI::PointerEvent::POINTER_ACTION_UP);
192     EXPECT_EQ(screenTouch_->OnPointerEvent(*eventUp), true);
193     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_004 end";
194 }
195 
196 /**
197  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_005
198  * @tc.name: OnPointerEvent
199  * @tc.desc: Test function OnPointerEvent
200  */
201 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_005, TestSize.Level1)
202 {
203     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_005 start";
204     sptr<AccessibilityAccountData> accountData =
205         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
206     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
207     if (accountData == nullptr || screenTouch_ == nullptr) {
208         return;
209     }
210 
211     accountData->GetConfig()->SetClickResponseTime(0);
212     accountData->GetConfig()->SetIgnoreRepeatClickState(true);
213     accountData->GetConfig()->SetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_LONGEST);
214 
215     auto eventDown = SetPointerEvent(TIMESTAMP_1200, MMI::PointerEvent::POINTER_ACTION_DOWN);
216     screenTouch_->OnPointerEvent(*eventDown);
217 
218     auto eventMove = SetPointerEvent(TIMESTAMP_1600, MMI::PointerEvent::POINTER_ACTION_MOVE);
219     screenTouch_->OnPointerEvent(*eventMove);
220 
221     auto eventUp = SetPointerEvent(TIMESTAMP_1700, MMI::PointerEvent::POINTER_ACTION_UP);
222     EXPECT_EQ(screenTouch_->OnPointerEvent(*eventUp), true);
223     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_005 end";
224 }
225 
226 /**
227  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_006
228  * @tc.name: OnPointerEvent
229  * @tc.desc: Test function OnPointerEvent
230  */
231 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_006, TestSize.Level1)
232 {
233     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_006 start";
234     sptr<AccessibilityAccountData> accountData =
235         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
236     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
237     if (accountData == nullptr || screenTouch_ == nullptr) {
238         return;
239     }
240 
241     accountData->GetConfig()->SetClickResponseTime(CLICK_RESPONSE_DELAY_LONG);
242     accountData->GetConfig()->SetIgnoreRepeatClickState(false);
243     accountData->GetConfig()->SetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_LONGEST);
244 
245     auto eventDown = SetPointerEvent(0, MMI::PointerEvent::POINTER_ACTION_DOWN);
246     screenTouch_->OnPointerEvent(*eventDown);
247 
248     auto eventMove1 = SetPointerEvent(TIMESTAMP_800, MMI::PointerEvent::POINTER_ACTION_MOVE);
249     screenTouch_->OnPointerEvent(*eventMove1);
250 
251     auto eventMove2 = SetPointerEvent(TIMESTAMP_1600, MMI::PointerEvent::POINTER_ACTION_MOVE);
252     screenTouch_->OnPointerEvent(*eventMove2);
253 
254     auto eventUp = SetPointerEvent(TIMESTAMP_1700, MMI::PointerEvent::POINTER_ACTION_UP);
255     EXPECT_EQ(screenTouch_->OnPointerEvent(*eventUp), true);
256     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_006 end";
257 }
258 
259 /**
260  * @tc.number: AccessibilityScreenTouch_Unittest_OnPointerEvent_007
261  * @tc.name: OnPointerEvent
262  * @tc.desc: Test function OnPointerEvent
263  */
264 HWTEST_F(AccessibilityScreenTouchUnitTest, AccessibilityScreenTouch_Unittest_OnPointerEvent_007, TestSize.Level1)
265 {
266     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_007 start";
267     sptr<AccessibilityAccountData> accountData =
268         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
269     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
270     if (accountData == nullptr || screenTouch_ == nullptr) {
271         GTEST_LOG_(INFO) << "accountData is nullptr or screenTouch_ is nullptr";
272         return;
273     }
274 
275     accountData->GetConfig()->SetClickResponseTime(CLICK_RESPONSE_DELAY_LONG);
276     accountData->GetConfig()->SetIgnoreRepeatClickState(true);
277     accountData->GetConfig()->SetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_LONGEST);
278 
279     auto eventDown = SetPointerEvent(TIMESTAMP_1600, MMI::PointerEvent::POINTER_ACTION_DOWN);
280     screenTouch_->OnPointerEvent(*eventDown);
281 
282     auto eventMove1 = SetPointerEvent(TIMESTAMP_1700, MMI::PointerEvent::POINTER_ACTION_MOVE);
283     screenTouch_->OnPointerEvent(*eventMove1);
284 
285     auto eventMove2 = SetPointerEvent(TIMESTAMP_2700, MMI::PointerEvent::POINTER_ACTION_MOVE);
286     screenTouch_->OnPointerEvent(*eventMove2);
287 
288     auto eventUp = SetPointerEvent(TIMESTAMP_2800, MMI::PointerEvent::POINTER_ACTION_UP);
289     EXPECT_EQ(screenTouch_->OnPointerEvent(*eventUp), true);
290     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_OnPointerEvent_007 end";
291 }
292 
293 /**
294  * @tc.number: AccessibilityScreenTouch_Unittest_GetRealClickResponseTime_001
295  * @tc.name: GetRealClickResponseTime
296  * @tc.desc: Test function GetRealClickResponseTime
297  */
298 HWTEST_F(AccessibilityScreenTouchUnitTest,
299     AccessibilityScreenTouch_Unittest_GetRealClickResponseTime_001, TestSize.Level1)
300 {
301     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_GetRealClickResponseTime_001 start";
302     sptr<AccessibilityAccountData> accountData =
303         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
304     if (!accountData) {
305         GTEST_LOG_(INFO) << "accountData is nullptr";
306         return;
307     }
308 
309     accountData->GetConfig()->SetClickResponseTime(900);
310     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
311 
312     EXPECT_EQ(screenTouch_->GetRealClickResponseTime(), 0);
313     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_GetRealClickResponseTime_001 end";
314 }
315 
316 /**
317  * @tc.number: AccessibilityScreenTouch_Unittest_GetRealIgnoreRepeatClickTime_001
318  * @tc.name: GetRealIgnoreRepeatClickTime
319  * @tc.desc: Test function OnPointerEvent
320  */
321 HWTEST_F(AccessibilityScreenTouchUnitTest,
322     AccessibilityScreenTouch_Unittest_GetRealIgnoreRepeatClickTime_001, TestSize.Level1)
323 {
324     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_GetRealIgnoreRepeatClickTime_001 start";
325     sptr<AccessibilityAccountData> accountData =
326         Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountData();
327     if (!accountData) {
328         GTEST_LOG_(INFO) << "accountData is nullptr";
329         return;
330     }
331 
332     accountData->GetConfig()->SetIgnoreRepeatClickTime(2000);
333     screenTouch_ = std::make_shared<AccessibilityScreenTouch>();
334 
335     EXPECT_EQ(screenTouch_->GetRealIgnoreRepeatClickTime(), IGNORE_REPEAT_CLICK_TIME_SHORTEST);
336     GTEST_LOG_(INFO) << "AccessibilityScreenTouch_Unittest_GetRealIgnoreRepeatClickTime end";
337 }
338 } // namespace Accessibility
339 } // namespace OHOS