• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
17 
18 #include "gtest/gtest.h"
19 
20 #define private public
21 #define protected public
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/base/view_stack_processor.h"
25 #include "core/components_ng/event/long_press_event.h"
26 #include "core/components_ng/pattern/button/button_pattern.h"
27 #include "core/components_ng/pattern/linear_layout/column_model.h"
28 #include "core/components_ng/pattern/linear_layout/column_model_ng.h"
29 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_ng/pattern/text/span_node.h"
32 #include "core/components_ng/property/accessibility_property.h"
33 #include "core/components_v2/inspector/inspector_constants.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 
38 namespace OHOS::Ace::NG {
39 namespace {
40 const size_t ARRAY_SIZE = 1;
41 } // namespace
42 
43 class MockPattern : public Pattern {
44 public:
45     MockPattern() = default;
46     ~MockPattern() override = default;
47 };
48 class AccessibilityPropertyTestNg : public testing::Test {
49 public:
SetUpTestCase()50     static void SetUpTestCase() {};
TearDownTestCase()51     static void TearDownTestCase() {};
52 };
53 
54 /**
55  * @tc.name: AccessibilityPropertyTest001
56  * @tc.desc: Set show value into supportActions_ and get SupportAction length.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest001, TestSize.Level1)
60 {
61     /**
62      * @tc.steps: step1. supportActions_ Use default values
63      * @tc.expected: Make the array length returned by the GetSupportAction function 0
64      */
65     AccessibilityProperty props;
66     auto actions = props.GetSupportAction();
67     EXPECT_EQ(actions.size(), 0);
68 
69     /**
70      * @tc.steps: step2. supportActions_ = AceAction::ACTION_SCROLL_FORWARD
71      * @tc.expected: Make the array length returned by the GetSupportAction function 1
72      */
73     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_SCROLL_FORWARD);
74     actions = props.GetSupportAction();
75     EXPECT_EQ(actions.size(), ARRAY_SIZE);
76 
77     /**
78      * @tc.steps: step3. supportActions_ = AceAction::ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
79      * @tc.expected: Make the array length returned by the GetSupportAction function 1
80      */
81     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
82     actions = props.GetSupportAction();
83     EXPECT_EQ(actions.size(), 2);
84 
85     /**
86      * @tc.steps: step4. supportActions_ = AceAction::ACTION_SET_TEXT
87      * @tc.expected: Make the array length returned by the GetSupportAction function 1
88      */
89     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_SET_TEXT);
90     actions = props.GetSupportAction();
91     EXPECT_EQ(actions.size(), ARRAY_SIZE);
92 
93     /**
94      * @tc.steps: step5. supportActions_ = AceAction::ACTION_COPY
95      * @tc.expected: Make the array length returned by the GetSupportAction function 1
96      */
97     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_COPY);
98     actions = props.GetSupportAction();
99     EXPECT_EQ(actions.size(), 2);
100 
101     /**
102      * @tc.steps: step6. supportActions_ = AceAction::ACTION_PASTE
103      * @tc.expected: Make the array length returned by the GetSupportAction function 1
104      */
105     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_PASTE);
106     actions = props.GetSupportAction();
107     EXPECT_EQ(actions.size(), 2);
108 
109     /**
110      * @tc.steps: step7. supportActions_ = AceAction::ACTION_CUT
111      * @tc.expected: Make the array length returned by the GetSupportAction function 1
112      */
113     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_CUT);
114     actions = props.GetSupportAction();
115     EXPECT_EQ(actions.size(), 3);
116 
117     /**
118      * @tc.steps: step8. supportActions_ = AceAction::ACTION_SELECT
119      * @tc.expected: Make the array length returned by the GetSupportAction function 1
120      */
121     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_SELECT);
122     actions = props.GetSupportAction();
123     EXPECT_EQ(actions.size(), 0);
124 
125     /**
126      * @tc.steps: step9. supportActions_ = AceAction::ACTION_SELECT
127      * @tc.expected: Make the array length returned by the GetSupportAction function 1
128      */
129     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_SET_SELECTION);
130     actions = props.GetSupportAction();
131     EXPECT_EQ(actions.size(), 1);
132 
133     /**
134      * @tc.steps: step10. supportActions_ = AceAction::ACTION_CLEAR_SELECTION
135      * @tc.expected: Make the array length returned by the GetSupportAction function 1
136      */
137     props.supportActions_ = static_cast<uint32_t>(AceAction::ACTION_CLEAR_SELECTION);
138     actions = props.GetSupportAction();
139     EXPECT_EQ(actions.size(), 1);
140 }
141 
142 /**
143  * @tc.name: AccessibilityPropertyTest002
144  * @tc.desc: Set action and execute it.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest002, TestSize.Level1)
148 {
149     /**
150      * @tc.steps: step1. actions is empty
151      * @tc.expected: act every action return false
152      */
153     AccessibilityProperty props;
154     EXPECT_FALSE(props.ActActionClearSelection());
155     EXPECT_FALSE(props.ActActionCopy());
156     EXPECT_FALSE(props.ActActionCut());
157     EXPECT_FALSE(props.ActActionMoveText(1, true));
158     EXPECT_FALSE(props.ActActionPaste());
159     EXPECT_FALSE(props.ActActionScrollBackward());
160     EXPECT_FALSE(props.ActActionScrollForward());
161     EXPECT_FALSE(props.ActActionSelect());
162     EXPECT_FALSE(props.ActActionSetSelection(0, 1));
163     EXPECT_FALSE(props.ActActionSetText("abc"));
164 
165     /**
166      * @tc.steps: step2. Set action functions
167      * @tc.expected: act every action return true
168      */
__anon69fc92c20202() 169     props.SetActionClearSelection([]() {});
170     EXPECT_TRUE(props.ActActionClearSelection());
__anon69fc92c20302() 171     props.SetActionCopy([]() {});
172     EXPECT_TRUE(props.ActActionCopy());
__anon69fc92c20402() 173     props.SetActionCut([]() {});
174     EXPECT_TRUE(props.ActActionCut());
__anon69fc92c20502(int32_t moveUnit, bool forward) 175     props.SetActionMoveText([](int32_t moveUnit, bool forward) {});
176     EXPECT_TRUE(props.ActActionMoveText(1, true));
__anon69fc92c20602() 177     props.SetActionPaste([]() {});
178     EXPECT_TRUE(props.ActActionPaste());
__anon69fc92c20702() 179     props.SetActionScrollBackward([]() {});
180     EXPECT_TRUE(props.ActActionScrollBackward());
__anon69fc92c20802() 181     props.SetActionScrollForward([]() {});
182     EXPECT_TRUE(props.ActActionScrollForward());
__anon69fc92c20902() 183     props.SetActionSelect([]() {});
184     EXPECT_TRUE(props.ActActionSelect());
__anon69fc92c20a02(int32_t start, int32_t end) 185     props.SetActionSetSelection([](int32_t start, int32_t end) {});
186     EXPECT_TRUE(props.ActActionSetSelection(0, 1));
__anon69fc92c20b02(std::string text) 187     props.SetActionSetText([](std::string text) {});
188     EXPECT_TRUE(props.ActActionSetText("abc"));
189 }
190 
191 /**
192  * @tc.name: AccessibilityPropertyTest003
193  * @tc.desc: Set and get accessibility property.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest003, TestSize.Level1)
197 {
198     /**
199      * @tc.steps: step1. Set accessibility property value
200      * @tc.expected: Get default property value
201      */
202     AccessibilityProperty props;
203     props.SetText("test");
204     EXPECT_EQ(props.GetText(), "test");
205     auto json = std::make_unique<JsonValue>();
206     props.ToJsonValue(json);
207     EXPECT_FALSE(json->GetBool("scrollable", false));
208     EXPECT_FALSE(props.IsCheckable());
209     EXPECT_FALSE(props.IsChecked());
210     EXPECT_FALSE(props.IsSelected());
211     EXPECT_FALSE(props.IsPassword());
212     EXPECT_FALSE(props.IsEditable());
213     EXPECT_FALSE(props.IsMultiLine());
214     EXPECT_FALSE(props.IsDeletable());
215 
216     EXPECT_FALSE(props.IsHint());
217     EXPECT_FALSE(props.IsScrollable());
218     EXPECT_EQ(props.GetCurrentIndex(), -1);
219     EXPECT_EQ(props.GetBeginIndex(), -1);
220     EXPECT_EQ(props.GetEndIndex(), -1);
221     EXPECT_FALSE(props.HasRange());
222     EXPECT_EQ(props.GetAccessibilityValue().min, 0.0);
223     EXPECT_EQ(props.GetAccessibilityValue().current, 0.0);
224     EXPECT_EQ(props.GetAccessibilityValue().max, 0.0);
225     EXPECT_EQ(props.GetHintText(), "");
226     EXPECT_EQ(props.GetTextLengthLimit(), -1);
227     EXPECT_EQ(props.GetCollectionInfo().columns, 0);
228     EXPECT_EQ(props.GetCollectionInfo().rows, 0);
229     EXPECT_EQ(props.GetCollectionInfo().selectMode, 0);
230     EXPECT_EQ(props.GetCollectionItemInfo().column, 0);
231     EXPECT_EQ(props.GetCollectionItemInfo().columnSpan, 0);
232     EXPECT_EQ(props.GetCollectionItemInfo().row, 0);
233     EXPECT_EQ(props.GetCollectionItemInfo().rowSpan, 0);
234     EXPECT_FALSE(props.GetCollectionItemInfo().heading);
235     EXPECT_EQ(props.GetErrorText(), "");
236     EXPECT_EQ(props.GetTextSelectionStart(), 0);
237     EXPECT_EQ(props.GetTextSelectionEnd(), 0);
238     EXPECT_EQ(props.GetTextInputType(), AceTextCategory::INPUT_TYPE_DEFAULT);
239     EXPECT_EQ(props.GetCollectionItemCounts(), 0);
240     EXPECT_TRUE(props.GetContentInvalid());
241     props.ResetSupportAction();
242     auto actions = props.GetSupportAction();
243     EXPECT_EQ(actions.size(), 0);
244 }
245 
246 /**
247  * @tc.name: AccessibilityPropertyTest004
248  * @tc.desc: Set show value into supportActions_ and get SupportAction length.
249  * @tc.type: FUNC
250  */
251 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest004, TestSize.Level1)
252 {
253     /**
254      * @tc.steps: step1. supportActions_ Use default values
255      * @tc.expected: Make the array length returned by the GetSupportAction function 0
256      */
257     auto columnFrameNode1 = FrameNode::GetOrCreateFrameNode(
__anon69fc92c20c02() 258         V2::COLUMN_ETS_TAG, 0, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
259     auto columnFrameNode2 = FrameNode::GetOrCreateFrameNode(
__anon69fc92c20d02() 260         V2::COLUMN_ETS_TAG, 1, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
261     auto buttonNode1 =
__anon69fc92c20e02() 262         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 2, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
263     auto buttonNode2 =
__anon69fc92c20f02() 264         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 3, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
265     auto buttonNode3 =
__anon69fc92c21002() 266         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 4, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
267     auto buttonNode4 =
__anon69fc92c21102() 268         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 5, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
269     auto buttonAccessibilityProperty1 = buttonNode1->GetAccessibilityProperty<AccessibilityProperty>();
270     auto buttonAccessibilityProperty2 = buttonNode2->GetAccessibilityProperty<AccessibilityProperty>();
271     auto buttonAccessibilityProperty3 = buttonNode3->GetAccessibilityProperty<AccessibilityProperty>();
272     auto buttonAccessibilityProperty4 = buttonNode4->GetAccessibilityProperty<AccessibilityProperty>();
273     auto columnAccessibilityProperty2 = columnFrameNode2->GetAccessibilityProperty<AccessibilityProperty>();
274     buttonAccessibilityProperty1->SetText("Button1");
275     buttonAccessibilityProperty2->SetText("Button2");
276     buttonAccessibilityProperty3->SetText("Button3");
277     buttonAccessibilityProperty4->SetText("Button4");
278     columnAccessibilityProperty2->SetAccessibilityText("column2");
279     buttonAccessibilityProperty1->SetAccessibilityDescription("Button1");
280 
281     columnFrameNode1->AddChild(buttonNode1);
282     columnFrameNode1->AddChild(buttonNode2);
283     columnFrameNode1->AddChild(columnFrameNode2);
284     columnFrameNode2->AddChild(buttonNode3);
285     columnFrameNode2->AddChild(buttonNode4);
286 
287     auto columnAccessibilityProperty1 = columnFrameNode1->GetAccessibilityProperty<AccessibilityProperty>();
288     columnAccessibilityProperty1->SetAccessibilityGroup(true);
289     columnAccessibilityProperty1->SetAccessibilityLevel("yes");
290 
291     buttonAccessibilityProperty3->SetAccessibilityText("buttonAccessibilityProperty3");
292     buttonAccessibilityProperty3->SetAccessibilityGroup(true);
293     buttonAccessibilityProperty3->SetAccessibilityLevel("yes");
294     auto columnAccessibilityText1 = columnAccessibilityProperty1->GetAccessibilityText();
295     /**
296      * @tc.expected: step1. expect target text combine
297      */
298     EXPECT_EQ(buttonAccessibilityProperty1->GetAccessibilityDescription(), "Button1");
299     EXPECT_EQ(columnAccessibilityText1, "Button1, Button2, column2");
300 
301     columnAccessibilityProperty1->SetAccessibilityLevel("no");
302     columnAccessibilityText1 = columnAccessibilityProperty1->GetAccessibilityText();
303     EXPECT_EQ(columnAccessibilityText1, "");
304 }
305 
306 /**
307  * @tc.name: AccessibilityPropertyTest005
308  * @tc.desc: Set show value into supportActions_ and get SupportAction length.
309  * @tc.type: FUNC
310  */
311 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest005, TestSize.Level1)
312 {
313     std::string tag = "root";
314 
315     auto spanNode = SpanNode::GetOrCreateSpanNode(1);
316     auto columnFrameNode1 = FrameNode::GetOrCreateFrameNode(
__anon69fc92c21202() 317         V2::COLUMN_ETS_TAG, 0, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
318     auto buttonNode1 =
__anon69fc92c21302() 319         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 2, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
320     spanNode->AddChild(buttonNode1);
321     columnFrameNode1->AddChild(spanNode);
322 
323     std::list<RefPtr<FrameNode>> children;
324 
325     auto columnAccessibilityProperty1 = columnFrameNode1->GetAccessibilityProperty<AccessibilityProperty>();
326     columnAccessibilityProperty1->SetAccessibilityGroup(true);
327     columnAccessibilityProperty1->SetAccessibilityLevel("yes");
328     columnAccessibilityProperty1->SetAccessibilityText("column1");
329 
330     auto text = columnAccessibilityProperty1->GetAccessibilityText();
331     EXPECT_EQ(text, "column1");
332 }
333 
334 /**
335  * @tc.name: AccessibilityPropertyTest006
336  * @tc.desc: Set show value into supportActions_ and get SupportAction length.
337  * @tc.type: FUNC
338  */
339 HWTEST_F(AccessibilityPropertyTestNg, AccessibilityPropertyTest006, TestSize.Level1)
340 {
341     /**
342      * @tc.steps1: Create FrameNode and Add Children
343      */
344     std::string tag = "root";
345     auto spanNode = SpanNode::GetOrCreateSpanNode(1);
346     auto columnFrameNode1 = FrameNode::GetOrCreateFrameNode(
__anon69fc92c21402() 347         V2::COLUMN_ETS_TAG, 0, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
348     auto buttonNode1 =
__anon69fc92c21502() 349         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 2, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
350     auto customFrameNode2 = CustomNode::CreateCustomNode(1, "Column");
351     spanNode->AddChild(buttonNode1);
352     spanNode->AddChild(customFrameNode2);
353     columnFrameNode1->AddChild(spanNode);
354     columnFrameNode1->AddChild(buttonNode1);
355     auto columnAccessibilityProperty1 = columnFrameNode1->GetAccessibilityProperty<AccessibilityProperty>();
356     columnAccessibilityProperty1->SetAccessibilityGroup(true);
357     columnAccessibilityProperty1->SetAccessibilityLevel("yes");
358     columnAccessibilityProperty1->SetAccessibilityText("column1");
359 
360     /**
361      * @tc.steps2: call GetAccessibilityText
362      * @tc.expected: 'column1'
363      */
364     auto text = columnAccessibilityProperty1->GetAccessibilityText(true);
365     EXPECT_EQ(text, "column1");
366 
367     /**
368      * @tc.steps3: reset accessibilityText_ and call GetAccessibilityText
369      * @tc.expected: ''
370      */
371     columnAccessibilityProperty1->accessibilityText_.reset();
372     text = columnAccessibilityProperty1->GetAccessibilityText(true);
373     EXPECT_EQ(text, "");
374 
375     /**
376      * @tc.steps4: add a new frameNode and SetAccessibilityText
377      * @tc.expected: 'test'
378      */
379     auto buttonNode2 =
__anon69fc92c21602() 380         FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 3, []() { return AceType::MakeRefPtr<ButtonPattern>(); });
381     buttonNode2->GetAccessibilityProperty<AccessibilityProperty>()->SetAccessibilityText("test");
382     columnFrameNode1->AddChild(buttonNode2);
383     columnAccessibilityProperty1 = columnFrameNode1->GetAccessibilityProperty<AccessibilityProperty>();
384     columnAccessibilityProperty1->SetAccessibilityGroup(true);
385     columnAccessibilityProperty1->SetAccessibilityLevel("yes");
386     text = columnAccessibilityProperty1->GetAccessibilityText(true);
387     EXPECT_EQ(text, "test");
388 }
389 } // namespace OHOS::Ace::NG
390