• 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 "gtest/gtest.h"
17 
18 #include "mock_element_node_iterator.h"
19 #define private public
20 #define protected public
21 #include "select_strategy.h"
22 #undef private
23 #undef protected
24 #include "ui_controller.h"
25 #include "ui_action.h"
26 
27 using namespace OHOS::uitest;
28 using namespace std;
29 
TEST(SelectStrategyTest,refreshWidgetBoundsParentAndChild)30 TEST(SelectStrategyTest, refreshWidgetBoundsParentAndChild)
31 {
32     std::vector<WidgetMatchModel> emptyMatcher;
33     StrategyBuildParam buildParam;
34     buildParam.myselfMatcher = emptyMatcher;
35     std::unique_ptr<SelectStrategy> plain = SelectStrategy::BuildSelectStrategy(buildParam, false);
36     Rect selectWindow{10, 100, 10, 300};
37     Rect layer1{10, 50, 10, 40};
38     Rect layer2{10, 20, 50, 100};
39     Window test(0);
40     std::vector<Rect> overlay;
41     overlay.emplace_back(layer1);
42     overlay.emplace_back(layer2);
43     plain->SetAndCalcSelectWindowRect(selectWindow, overlay);
44     Widget widget{"test"};
45     Rect widOriBounds{30, 30, 30, 60};
46     widget.SetBounds(widOriBounds);
47     widget.SetAttr(UiAttr::VISIBLE, "true");
48     widget.SetAttr(UiAttr::TYPE, "Scroll");
49     plain->RefreshWidgetBounds(widget, test);
50     ASSERT_EQ(widget.GetAttr(UiAttr::VISIBLE), "false");
51     auto rect = widget.GetBounds();
52     ASSERT_EQ(rect.left_, 0);
53     ASSERT_EQ(rect.right_, 0);
54     ASSERT_EQ(rect.top_, 0);
55     ASSERT_EQ(rect.bottom_, 0);
56 }
57 
TEST(SelectStrategyTest,refreshWidgetBoundsOver)58 TEST(SelectStrategyTest, refreshWidgetBoundsOver)
59 {
60     std::vector<WidgetMatchModel> emptyMatcher;
61     StrategyBuildParam buildParam;
62     buildParam.myselfMatcher = emptyMatcher;
63     std::unique_ptr<SelectStrategy> plain = SelectStrategy::BuildSelectStrategy(buildParam, false);
64     Rect selectWindow{10, 100, 10, 300};
65     Rect layer1{10, 50, 10, 40};
66     Rect layer2{10, 20, 50, 100};
67     Window test(0);
68     std::vector<Rect> overlay;
69     overlay.emplace_back(layer1);
70     overlay.emplace_back(layer2);
71     plain->SetAndCalcSelectWindowRect(selectWindow, overlay);
72     Widget widget{"test"};
73     Rect widOriBounds{20, 50, 30, 40};
74     widget.SetBounds(widOriBounds);
75     plain->RefreshWidgetBounds(widget, test);
76     ASSERT_EQ(widget.GetAttr(UiAttr::VISIBLE), "false");
77     auto rect = widget.GetBounds();
78     ASSERT_EQ(rect.left_, 0);
79     ASSERT_EQ(rect.right_, 0);
80     ASSERT_EQ(rect.top_, 0);
81     ASSERT_EQ(rect.bottom_, 0);
82 }
83 
TEST(SelectStrategyTest,refreshWidgetBoundsPartOver)84 TEST(SelectStrategyTest, refreshWidgetBoundsPartOver)
85 {
86     std::vector<WidgetMatchModel> emptyMatcher;
87     StrategyBuildParam buildParam;
88     buildParam.myselfMatcher = emptyMatcher;
89     std::unique_ptr<SelectStrategy> plain = SelectStrategy::BuildSelectStrategy(buildParam, false);
90     Rect selectWindow{10, 100, 10, 300};
91     Rect layer1{10, 50, 10, 40};
92     Rect layer2{10, 20, 50, 100};
93     Window test(0);
94     std::vector<Rect> overlay;
95     overlay.emplace_back(layer1);
96     overlay.emplace_back(layer2);
97     plain->SetAndCalcSelectWindowRect(selectWindow, overlay);
98     Widget widget{"test"};
99     Rect widOriBounds{20, 50, 30, 50};
100     widget.SetBounds(widOriBounds);
101     plain->RefreshWidgetBounds(widget, test);
102     ASSERT_EQ(widget.GetAttr(UiAttr::VISIBLE), "true");
103     auto rect = widget.GetBounds();
104     ASSERT_EQ(rect.left_, 20);
105     ASSERT_EQ(rect.right_, 50);
106     ASSERT_EQ(rect.top_, 40);
107     ASSERT_EQ(rect.bottom_, 50);
108 }
109 
ConstructIterator(std::unique_ptr<ElementNodeIterator> & iterator)110 void ConstructIterator(std::unique_ptr<ElementNodeIterator> &iterator)
111 {
112     std::string afterDom = R"(
113         {
114             "attributes":{
115                 "windowId":"12",
116                 "componentType":"List",
117                 "content":"Text List",
118                 "rectInScreen":"30,60,10,120"
119             },
120             "children":[
121                 {
122                      "attributes":{
123                         "windowId":"12",
124                         "componentType":"List",
125                         "content":"Text List",
126                         "rectInScreen":"30,60,10,120"
127                      },
128                      "children":[
129                         {
130                             "attributes":{
131                                 "windowId":"12",
132                                 "componentType":"Image",
133                                 "content":"Button",
134                                 "rectInScreen":"30,40,10,20"
135                             },
136                             "children":[
137                                 {
138                                     "attributes":{
139                                         "windowId":"12",
140                                         "accessibilityId":"4",
141                                         "componentType":"Text",
142                                         "content":"Text One",
143                                         "rectInScreen":"30,40,10,20"
144                                     },
145                                     "children":[]
146                                 }
147                             ]
148                         },
149                         {
150                             "attributes":{
151                                 "windowId":"12",
152                                 "componentType":"List",
153                                 "accessibilityId":"7",
154                                 "content":"Text List12",
155                                 "rectInScreen":"40,60,10,20"
156                             },
157                             "children":[
158                                 {
159                                     "attributes":{
160                                         "windowId":"12",
161                                         "accessibilityId":"5",
162                                         "componentType":"Text",
163                                         "content":"Text One",
164                                         "rectInScreen":"40,50,10,20"
165                                     },
166                                     "children":[]
167                                 },
168                                 {
169                                     "attributes":{
170                                         "windowId":"12",
171                                         "accessibilityId":"4",
172                                         "componentType":"Text",
173                                         "rectInScreen":"50,60,10,20"
174                                     },
175                                     "children":[]
176                                 }
177                             ]
178                         }
179                      ]
180                 },
181                 {
182                      "attributes":{
183                         "windowId":"12",
184                         "componentType":"Image",
185                         "content":"Button",
186                         "rectInScreen":"10,20,20,100"
187                      },
188                      "children":[]
189                 },
190                 {
191                      "attributes":{
192                         "windowId":"12",
193                         "componentType":"Scroll",
194                         "content":"Button",
195                         "rectInScreen":"20,100,20,100"
196                      },
197                      "children":[
198                         {
199                             "attributes":{
200                                 "windowId":"12",
201                                 "componentType":"Image",
202                                 "content":"",
203                                 "rectInScreen":"20,100,20,100"
204                             },
205                             "children":[]
206                         },
207                         {
208                             "attributes":{
209                                 "windowId":"12",
210                                 "componentType":"Button",
211                                 "accessibilityId":"11",
212                                 "content":"Button text2",
213                                 "focused":"true",
214                                 "rectInScreen":"20,100,20,100"
215                             },
216                             "children":[
217                                 {
218                                     "attributes":{
219                                         "windowId":"12",
220                                         "componentType":"Text",
221                                         "content":"Text End",
222                                         "rectInScreen":"20,100,20,100"
223                                     },
224                                     "children":[]
225                                 }
226                             ]
227                         }
228                      ]
229                 }
230             ]
231         }
232     )";
233     iterator = MockElementNodeIterator::ConstructIteratorByJson(afterDom);
234 }
235 
TEST(SelectStrategyTest,afterStrategyForAnchorIsBeforeTarget)236 TEST(SelectStrategyTest, afterStrategyForAnchorIsBeforeTarget)
237 {
238     Window w1{12};
239     w1.windowLayer_ = 2;
240     w1.bounds_ = Rect{0, 100, 0, 120};
241     std::vector<Widget> visits;
242     std::vector<int> targets;
243     WidgetMatchModel anchor1{UiAttr::TEXT, "test", ValueMatchPattern::EQ};
244     std::vector<WidgetMatchModel> anchorMatchers;
245     anchorMatchers.emplace_back(anchor1);
246     WidgetMatchModel sef1{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
247     std::vector<WidgetMatchModel> myselfMatchers;
248     myselfMatchers.emplace_back(sef1);
249     StrategyBuildParam buildParam;
250     buildParam.myselfMatcher = myselfMatchers;
251     buildParam.afterAnchorMatcherVec.emplace_back(anchorMatchers);
252     std::unique_ptr<SelectStrategy> afterStrategy = SelectStrategy::BuildSelectStrategy(buildParam, false);
253     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
254     ConstructIterator(iterator);
255     DumpOption option;
256     afterStrategy->LocateNode(w1, *iterator.get(), visits, targets, option);
257     ASSERT_EQ(visits.size(), 12);
258     ASSERT_EQ(targets.size(), 0);
259 }
TEST(SelectStrategyTest,afterStrategyForAnchorIsAfterSingleTarget)260 TEST(SelectStrategyTest, afterStrategyForAnchorIsAfterSingleTarget)
261 {
262     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
263     ConstructIterator(iterator);
264     Window w1{12};
265     w1.windowLayer_ = 2;
266     w1.bounds_ = Rect{0, 100, 0, 120};
267     std::vector<Widget> visits2;
268     std::vector<int> targets2;
269     WidgetMatchModel anchor2{UiAttr::TEXT, "Text List12", ValueMatchPattern::EQ};
270     std::vector<WidgetMatchModel> anchorMatchers;
271     anchorMatchers.emplace_back(anchor2);
272     WidgetMatchModel sef2{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
273     std::vector<WidgetMatchModel> myselfMatchers;
274     myselfMatchers.emplace_back(sef2);
275     StrategyBuildParam buildParam;
276     buildParam.myselfMatcher = myselfMatchers;
277     buildParam.afterAnchorMatcherVec.emplace_back(anchorMatchers);
278     std::unique_ptr<SelectStrategy> afterStrategy2 = SelectStrategy::BuildSelectStrategy(buildParam, false);
279     DumpOption option;
280     afterStrategy2->LocateNode(w1, *iterator.get(), visits2, targets2, option);
281     ASSERT_EQ(targets2.size(), 1);
282     ASSERT_EQ(visits2[targets2[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
283 }
284 
TEST(SelectStrategyTest,afterStrategyForAnchorIsAfterMultiTarget)285 TEST(SelectStrategyTest, afterStrategyForAnchorIsAfterMultiTarget)
286 {
287     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
288     ConstructIterator(iterator);
289     Window w1{12};
290     w1.windowLayer_ = 2;
291     w1.bounds_ = Rect{0, 100, 0, 120};
292     std::vector<Widget> visits3;
293     std::vector<int> targets3;
294     WidgetMatchModel anchor3{UiAttr::TEXT, "Text List", ValueMatchPattern::EQ};
295     WidgetMatchModel sef3{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
296     std::vector<WidgetMatchModel> anchorMatchers;
297     anchorMatchers.emplace_back(anchor3);
298     std::vector<WidgetMatchModel> myselfMatchers;
299     myselfMatchers.emplace_back(sef3);
300     StrategyBuildParam buildParam;
301     buildParam.myselfMatcher = myselfMatchers;
302     buildParam.afterAnchorMatcherVec.emplace_back(anchorMatchers);
303     std::unique_ptr<SelectStrategy> afterStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
304     DumpOption option;
305     afterStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
306     ASSERT_EQ(visits3.size(), 12);
307     ASSERT_EQ(targets3.size(), 2);
308     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
309     ASSERT_EQ(visits3[targets3[1]].GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
310 }
311 
TEST(SelectStrategyTest,beforeStrategyForAnchorIsAfterTarget)312 TEST(SelectStrategyTest, beforeStrategyForAnchorIsAfterTarget)
313 {
314     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
315     ConstructIterator(iterator);
316     Window w1{12};
317     w1.windowLayer_ = 2;
318     w1.bounds_ = Rect{0, 100, 0, 120};
319     std::vector<Widget> visits;
320     std::vector<int> targets;
321     WidgetMatchModel anchor1{UiAttr::TEXT, "test", ValueMatchPattern::EQ};
322     WidgetMatchModel sef1{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
323     std::vector<WidgetMatchModel> anchorMatchers;
324     anchorMatchers.emplace_back(anchor1);
325     std::vector<WidgetMatchModel> myselfMatchers;
326     myselfMatchers.emplace_back(sef1);
327     StrategyBuildParam buildParam;
328     buildParam.myselfMatcher = myselfMatchers;
329     buildParam.beforeAnchorMatcherVec.emplace_back(anchorMatchers);
330     std::unique_ptr<SelectStrategy> beforeStrategy = SelectStrategy::BuildSelectStrategy(buildParam, false);
331     DumpOption option;
332     beforeStrategy->LocateNode(w1, *iterator.get(), visits, targets, option);
333     ASSERT_EQ(visits.size(), 12);
334     ASSERT_EQ(targets.size(), 0);
335 }
TEST(SelectStrategyTest,beforeStrategyForAnchorIsBeforeSingleTarget)336 TEST(SelectStrategyTest, beforeStrategyForAnchorIsBeforeSingleTarget)
337 {
338     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
339     ConstructIterator(iterator);
340     Window w1{12};
341     w1.windowLayer_ = 2;
342     w1.bounds_ = Rect{0, 100, 0, 120};
343     std::vector<Widget> visits2;
344     std::vector<int> targets2;
345     WidgetMatchModel anchor2{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
346     WidgetMatchModel sef2{UiAttr::TEXT, "Text List12", ValueMatchPattern::EQ};
347     std::vector<WidgetMatchModel> anchorMatchers;
348     anchorMatchers.emplace_back(anchor2);
349     std::vector<WidgetMatchModel> myselfMatchers;
350     myselfMatchers.emplace_back(sef2);
351     StrategyBuildParam buildParam;
352     buildParam.myselfMatcher = myselfMatchers;
353     buildParam.beforeAnchorMatcherVec.emplace_back(anchorMatchers);
354     std::unique_ptr<SelectStrategy> beforeStrategy2 = SelectStrategy::BuildSelectStrategy(buildParam, false);
355     DumpOption option;
356     beforeStrategy2->LocateNode(w1, *iterator.get(), visits2, targets2, option);
357     ASSERT_EQ(targets2.size(), 1);
358     ASSERT_EQ(visits2[targets2[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "7");
359 }
TEST(SelectStrategyTest,beforeStrategyForAnchorIsBeforeMultiTarget)360 TEST(SelectStrategyTest, beforeStrategyForAnchorIsBeforeMultiTarget)
361 {
362     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
363     ConstructIterator(iterator);
364     Window w1{12};
365     w1.windowLayer_ = 2;
366     w1.bounds_ = Rect{0, 100, 0, 120};
367     std::vector<Widget> visits3;
368     std::vector<int> targets3;
369     WidgetMatchModel anchor3{UiAttr::TEXT, "Text End", ValueMatchPattern::EQ};
370     WidgetMatchModel sef3{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
371     std::vector<WidgetMatchModel> anchorMatchers;
372     anchorMatchers.emplace_back(anchor3);
373     std::vector<WidgetMatchModel> myselfMatchers;
374     myselfMatchers.emplace_back(sef3);
375     StrategyBuildParam buildParam;
376     buildParam.myselfMatcher = myselfMatchers;
377     buildParam.beforeAnchorMatcherVec.emplace_back(anchorMatchers);
378     std::unique_ptr<SelectStrategy> beforeStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
379     DumpOption option;
380     option.listWindows_ = false;
381     beforeStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
382     ASSERT_EQ(visits3.size(), 12);
383     ASSERT_EQ(targets3.size(), 2);
384     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
385     ASSERT_EQ(visits3[targets3[1]].GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
386 }
387 
TEST(SelectStrategyTest,beforeStrategyForAnchorIsTargetForMultiTargets)388 TEST(SelectStrategyTest, beforeStrategyForAnchorIsTargetForMultiTargets)
389 {
390     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
391     ConstructIterator(iterator);
392     Window w1{12};
393     w1.windowLayer_ = 2;
394     w1.bounds_ = Rect{0, 100, 0, 120};
395     std::vector<Widget> visits3;
396     std::vector<int> targets3;
397     WidgetMatchModel anchor3{UiAttr::TEXT, "Text End", ValueMatchPattern::EQ};
398     WidgetMatchModel sef3{UiAttr::TYPE, "Text", ValueMatchPattern::EQ};
399     std::vector<WidgetMatchModel> anchorMatchers;
400     anchorMatchers.emplace_back(anchor3);
401     std::vector<WidgetMatchModel> myselfMatchers;
402     myselfMatchers.emplace_back(sef3);
403     StrategyBuildParam buildParam;
404     buildParam.myselfMatcher = myselfMatchers;
405     buildParam.beforeAnchorMatcherVec.emplace_back(anchorMatchers);
406     std::unique_ptr<SelectStrategy> beforeStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
407     DumpOption option;
408     option.listWindows_ = false;
409     beforeStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
410     ASSERT_EQ(visits3.size(), 12);
411     ASSERT_EQ(targets3.size(), 3);
412     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::TEXT), "Text One");
413     ASSERT_EQ(visits3[targets3[1]].GetAttr(UiAttr::TEXT), "Text One");
414     ASSERT_EQ(visits3[targets3[2]].GetAttr(UiAttr::TEXT), "");
415 }
416 
TEST(SelectStrategyTest,beforeStrategyForAnchorIsTargetForSingleTarget)417 TEST(SelectStrategyTest, beforeStrategyForAnchorIsTargetForSingleTarget)
418 {
419     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
420     ConstructIterator(iterator);
421     Window w1{12};
422     w1.windowLayer_ = 2;
423     w1.bounds_ = Rect{0, 100, 0, 120};
424     std::vector<Widget> visits3;
425     std::vector<int> targets3;
426     WidgetMatchModel anchor3{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
427     WidgetMatchModel sef3{UiAttr::TYPE, "Image", ValueMatchPattern::EQ};
428     std::vector<WidgetMatchModel> anchorMatchers;
429     anchorMatchers.emplace_back(anchor3);
430     std::vector<WidgetMatchModel> myselfMatchers;
431     myselfMatchers.emplace_back(sef3);
432     StrategyBuildParam buildParam;
433     buildParam.myselfMatcher = myselfMatchers;
434     buildParam.beforeAnchorMatcherVec.emplace_back(anchorMatchers);
435     std::unique_ptr<SelectStrategy> beforeStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
436     DumpOption option;
437     option.listWindows_ = false;
438     beforeStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
439     ASSERT_EQ(visits3.size(), 12);
440     ASSERT_EQ(targets3.size(), 1);
441     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::TEXT), "Button");
442 }
443 
TEST(SelectStrategyTest,withInStrategyForAnchorIsOutTarget)444 TEST(SelectStrategyTest, withInStrategyForAnchorIsOutTarget)
445 {
446     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
447     ConstructIterator(iterator);
448     Window w1{12};
449     w1.windowLayer_ = 2;
450     w1.bounds_ = Rect{0, 100, 0, 120};
451     std::vector<Widget> visits;
452     std::vector<int> targets;
453     WidgetMatchModel anchor1{UiAttr::TEXT, "test", ValueMatchPattern::EQ};
454     WidgetMatchModel sef1{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
455     std::vector<WidgetMatchModel> anchorMatchers;
456     anchorMatchers.emplace_back(anchor1);
457     std::vector<WidgetMatchModel> myselfMatchers;
458     myselfMatchers.emplace_back(sef1);
459     StrategyBuildParam buildParam;
460     buildParam.myselfMatcher = myselfMatchers;
461     buildParam.withInAnchorMatcherVec.emplace_back(anchorMatchers);
462     std::unique_ptr<SelectStrategy> withInStrategy = SelectStrategy::BuildSelectStrategy(buildParam, false);
463     DumpOption option;
464     withInStrategy->LocateNode(w1, *iterator.get(), visits, targets, option);
465     ASSERT_EQ(visits.size(), 12);
466     ASSERT_EQ(targets.size(), 0);
467 }
TEST(SelectStrategyTest,withInStrategyForOneAnchorIsInTarget)468 TEST(SelectStrategyTest, withInStrategyForOneAnchorIsInTarget)
469 {
470     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
471     ConstructIterator(iterator);
472     Window w1{12};
473     w1.windowLayer_ = 2;
474     w1.bounds_ = Rect{0, 100, 0, 120};
475     std::vector<Widget> visits2;
476     std::vector<int> targets2;
477     WidgetMatchModel anchor2{UiAttr::TEXT, "Text List12", ValueMatchPattern::EQ};
478     WidgetMatchModel sef2{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
479     std::vector<WidgetMatchModel> anchorMatchers;
480     anchorMatchers.emplace_back(anchor2);
481     std::vector<WidgetMatchModel> myselfMatchers;
482     myselfMatchers.emplace_back(sef2);
483     StrategyBuildParam buildParam;
484     buildParam.myselfMatcher = myselfMatchers;
485     buildParam.withInAnchorMatcherVec.emplace_back(anchorMatchers);
486     std::unique_ptr<SelectStrategy> withInStrategy2 = SelectStrategy::BuildSelectStrategy(buildParam, false);
487     DumpOption option;
488     withInStrategy2->LocateNode(w1, *iterator.get(), visits2, targets2, option);
489     ASSERT_EQ(targets2.size(), 1);
490     ASSERT_EQ(visits2[targets2[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
491 }
TEST(SelectStrategyTest,withInStrategyFoTwoAnchorIsInTarget)492 TEST(SelectStrategyTest, withInStrategyFoTwoAnchorIsInTarget)
493 {
494     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
495     ConstructIterator(iterator);
496     Window w1{12};
497     w1.windowLayer_ = 2;
498     w1.bounds_ = Rect{0, 100, 0, 120};
499     std::vector<Widget> visits3;
500     std::vector<int> targets3;
501     WidgetMatchModel anchor3{UiAttr::TEXT, "Button", ValueMatchPattern::EQ};
502     WidgetMatchModel sef3{UiAttr::TEXT, "Button text2", ValueMatchPattern::EQ};
503     std::vector<WidgetMatchModel> anchorMatchers;
504     anchorMatchers.emplace_back(anchor3);
505     std::vector<WidgetMatchModel> myselfMatchers;
506     myselfMatchers.emplace_back(sef3);
507     StrategyBuildParam buildParam;
508     buildParam.myselfMatcher = myselfMatchers;
509     buildParam.withInAnchorMatcherVec.emplace_back(anchorMatchers);
510     std::unique_ptr<SelectStrategy> withInStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
511     DumpOption option;
512     withInStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
513     ASSERT_EQ(targets3.size(), 1);
514     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "11");
515 }
516 
TEST(SelectStrategyTest,withInStrategyFoTwoAnchorIsInMultiTarget)517 TEST(SelectStrategyTest, withInStrategyFoTwoAnchorIsInMultiTarget)
518 {
519     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
520     ConstructIterator(iterator);
521     Window w1{12};
522     w1.windowLayer_ = 2;
523     w1.bounds_ = Rect{0, 100, 0, 120};
524     std::vector<Widget> visits3;
525     std::vector<int> targets3;
526     WidgetMatchModel anchor3{UiAttr::TEXT, "Text List", ValueMatchPattern::EQ};
527     WidgetMatchModel sef3{UiAttr::TEXT, "Button", ValueMatchPattern::EQ};
528     std::vector<WidgetMatchModel> anchorMatchers;
529     anchorMatchers.emplace_back(anchor3);
530     std::vector<WidgetMatchModel> myselfMatchers;
531     myselfMatchers.emplace_back(sef3);
532     StrategyBuildParam buildParam;
533     buildParam.myselfMatcher = myselfMatchers;
534     buildParam.withInAnchorMatcherVec.emplace_back(anchorMatchers);
535     std::unique_ptr<SelectStrategy> withInStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
536     DumpOption option;
537     withInStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
538     ASSERT_EQ(targets3.size(), 3);
539     ASSERT_EQ(visits3[targets3[0]].GetAttr(UiAttr::TYPE), "Image");
540     ASSERT_EQ(visits3[targets3[1]].GetAttr(UiAttr::TYPE), "Image");
541     ASSERT_EQ(visits3[targets3[2]].GetAttr(UiAttr::TYPE), "Scroll");
542 }
543 
TEST(SelectStrategyTest,plainStrategyForOnlyOneTarget)544 TEST(SelectStrategyTest, plainStrategyForOnlyOneTarget)
545 {
546     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
547     ConstructIterator(iterator);
548     Window w1{12};
549     w1.windowLayer_ = 2;
550     w1.bounds_ = Rect{0, 100, 0, 120};
551     std::vector<Widget> visits;
552     std::vector<int> targets;
553     WidgetMatchModel sef1{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
554     std::vector<WidgetMatchModel> myselfMatchers;
555     myselfMatchers.emplace_back(sef1);
556     StrategyBuildParam buildParam;
557     buildParam.myselfMatcher = myselfMatchers;
558     std::unique_ptr<SelectStrategy> plainStrategy = SelectStrategy::BuildSelectStrategy(buildParam, false);
559     DumpOption option;
560     plainStrategy->LocateNode(w1, *iterator.get(), visits, targets, option);
561     ASSERT_EQ(targets.size(), 1);
562     ASSERT_EQ(visits[targets[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
563 }
TEST(SelectStrategyTest,plainStrategyForMultiTarget)564 TEST(SelectStrategyTest, plainStrategyForMultiTarget)
565 {
566     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
567     ConstructIterator(iterator);
568     Window w1{12};
569     w1.windowLayer_ = 2;
570     w1.bounds_ = Rect{0, 100, 0, 120};
571     std::vector<Widget> visits;
572     std::vector<Widget> visits2;
573     std::vector<int> targets2;
574     WidgetMatchModel sef2{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
575     std::vector<WidgetMatchModel> myselfMatchers;
576     myselfMatchers.emplace_back(sef2);
577     StrategyBuildParam buildParam;
578     buildParam.myselfMatcher = myselfMatchers;
579     std::unique_ptr<SelectStrategy> plainStrategy2 = SelectStrategy::BuildSelectStrategy(buildParam, true);
580     DumpOption option;
581     plainStrategy2->LocateNode(w1, *iterator.get(), visits2, targets2, option);
582     ASSERT_EQ(targets2.size(), 2);
583     ASSERT_EQ(visits2[targets2[0]].GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
584     ASSERT_EQ(visits2[targets2[1]].GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
585 }
TEST(SelectStrategyTest,plainStrategyForNoneTarget)586 TEST(SelectStrategyTest, plainStrategyForNoneTarget)
587 {
588     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
589     ConstructIterator(iterator);
590 
591     Window w1{12};
592     w1.windowLayer_ = 2;
593     w1.bounds_ = Rect{0, 100, 0, 120};
594     std::vector<Widget> visits3;
595     std::vector<int> targets3;
596     WidgetMatchModel sef3{UiAttr::TEXT, "Button text2112", ValueMatchPattern::EQ};
597     std::vector<WidgetMatchModel> anchorMatchers;
598     std::vector<WidgetMatchModel> myselfMatchers;
599     myselfMatchers.emplace_back(sef3);
600     StrategyBuildParam buildParam;
601     buildParam.myselfMatcher = myselfMatchers;
602     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
603     DumpOption option;
604     plainStrategy3->LocateNode(w1, *iterator.get(), visits3, targets3, option);
605     ASSERT_EQ(visits3.size(), 12);
606     ASSERT_EQ(targets3.size(), 0);
607 }
608 
TEST(SelectStrategyTest,complexStrategyForAfterAndBeforeNoneTarget)609 TEST(SelectStrategyTest, complexStrategyForAfterAndBeforeNoneTarget)
610 {
611     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
612     ConstructIterator(iterator);
613 
614     Window w1{12};
615     w1.windowLayer_ = 2;
616     w1.bounds_ = Rect{0, 100, 0, 120};
617     std::vector<Widget> visits;
618     std::vector<int> targets;
619     WidgetMatchModel sef{UiAttr::TEXT, "Text End", ValueMatchPattern::EQ};
620     std::vector<WidgetMatchModel> beforeAnchorMatchers;
621     std::vector<WidgetMatchModel> afterAnchorMatchers;
622     std::vector<WidgetMatchModel> myselfMatchers;
623     myselfMatchers.emplace_back(sef);
624     WidgetMatchModel beforeAnchor{UiAttr::ACCESSIBILITY_ID, "11", ValueMatchPattern::EQ};
625     WidgetMatchModel afterAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
626     beforeAnchorMatchers.emplace_back(beforeAnchor);
627     afterAnchorMatchers.emplace_back(afterAnchor);
628 
629     StrategyBuildParam buildParam;
630     buildParam.myselfMatcher = myselfMatchers;
631     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
632     buildParam.beforeAnchorMatcherVec.emplace_back(beforeAnchorMatchers);
633     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
634     DumpOption option;
635     plainStrategy3->LocateNode(w1, *iterator.get(), visits, targets, option);
636     ASSERT_EQ(targets.size(), 0);
637 }
638 
TEST(SelectStrategyTest,complexStrategyForAfterAndBeforeSingleTarget)639 TEST(SelectStrategyTest, complexStrategyForAfterAndBeforeSingleTarget)
640 {
641     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
642     ConstructIterator(iterator);
643 
644     Window w1{12};
645     w1.windowLayer_ = 2;
646     w1.bounds_ = Rect{0, 100, 0, 120};
647     std::vector<Widget> visits;
648     std::vector<int> targets;
649     WidgetMatchModel sef{UiAttr::TEXT, "Button", ValueMatchPattern::EQ};
650     std::vector<WidgetMatchModel> beforeAnchorMatchers;
651     std::vector<WidgetMatchModel> afterAnchorMatchers;
652     std::vector<WidgetMatchModel> myselfMatchers;
653     myselfMatchers.emplace_back(sef);
654     WidgetMatchModel afterAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
655     WidgetMatchModel beforeAnchor{UiAttr::ACCESSIBILITY_ID, "11", ValueMatchPattern::EQ};
656     beforeAnchorMatchers.emplace_back(beforeAnchor);
657     afterAnchorMatchers.emplace_back(afterAnchor);
658 
659     StrategyBuildParam buildParam;
660     buildParam.myselfMatcher = myselfMatchers;
661     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
662     buildParam.beforeAnchorMatcherVec.emplace_back(beforeAnchorMatchers);
663     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
664     DumpOption option;
665     plainStrategy3->LocateNode(w1, *iterator.get(), visits, targets, option);
666     ASSERT_EQ(targets.size(), 1);
667     ASSERT_EQ(visits.at(targets.at(0)).GetAttr(UiAttr::TYPE), "Image");
668 }
669 
TEST(SelectStrategyTest,complexStrategyForAfterAndBeforeMultiTarget)670 TEST(SelectStrategyTest, complexStrategyForAfterAndBeforeMultiTarget)
671 {
672     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
673     ConstructIterator(iterator);
674 
675     Window w1{12};
676     w1.windowLayer_ = 2;
677     w1.bounds_ = Rect{0, 100, 0, 120};
678     std::vector<Widget> visits;
679     std::vector<int> targets;
680     WidgetMatchModel sef{UiAttr::TEXT, "Button", ValueMatchPattern::EQ};
681     std::vector<WidgetMatchModel> beforeAnchorMatchers;
682     std::vector<WidgetMatchModel> afterAnchorMatchers;
683     std::vector<WidgetMatchModel> myselfMatchers;
684     myselfMatchers.emplace_back(sef);
685     WidgetMatchModel afterAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
686     WidgetMatchModel beforeAnchor{UiAttr::ACCESSIBILITY_ID, "11", ValueMatchPattern::EQ};
687     beforeAnchorMatchers.emplace_back(beforeAnchor);
688     afterAnchorMatchers.emplace_back(afterAnchor);
689 
690     StrategyBuildParam buildParam;
691     buildParam.myselfMatcher = myselfMatchers;
692     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
693     buildParam.beforeAnchorMatcherVec.emplace_back(beforeAnchorMatchers);
694     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, true);
695     DumpOption option;
696     plainStrategy3->LocateNode(w1, *iterator.get(), visits, targets, option);
697     ASSERT_EQ(targets.size(), 2);
698     ASSERT_EQ(visits.at(targets.at(0)).GetAttr(UiAttr::TYPE), "Image");
699     ASSERT_EQ(visits.at(targets.at(1)).GetAttr(UiAttr::TYPE), "Scroll");
700 }
701 
TEST(SelectStrategyTest,complexStrategyForAfterAndWithInNoneTarget)702 TEST(SelectStrategyTest, complexStrategyForAfterAndWithInNoneTarget)
703 {
704     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
705     ConstructIterator(iterator);
706 
707     Window w1{12};
708     w1.windowLayer_ = 2;
709     w1.bounds_ = Rect{0, 100, 0, 120};
710     std::vector<Widget> visits;
711     std::vector<int> targets;
712     WidgetMatchModel sef{UiAttr::TEXT, "Text End", ValueMatchPattern::EQ};
713     std::vector<WidgetMatchModel> withInAnchorMatchers;
714     std::vector<WidgetMatchModel> afterAnchorMatchers;
715     std::vector<WidgetMatchModel> myselfMatchers;
716     myselfMatchers.emplace_back(sef);
717     WidgetMatchModel withInAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
718     WidgetMatchModel afterAnchor{UiAttr::ACCESSIBILITY_ID, "11", ValueMatchPattern::EQ};
719     withInAnchorMatchers.emplace_back(withInAnchor);
720     afterAnchorMatchers.emplace_back(afterAnchor);
721 
722     StrategyBuildParam buildParam;
723     buildParam.myselfMatcher = myselfMatchers;
724     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
725     buildParam.withInAnchorMatcherVec.emplace_back(withInAnchorMatchers);
726     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
727     DumpOption option;
728     plainStrategy3->LocateNode(w1, *iterator.get(), visits, targets, option);
729     ASSERT_EQ(targets.size(), 0);
730 }
731 
TEST(SelectStrategyTest,complexStrategyForAfterAndWithInSingleTarget)732 TEST(SelectStrategyTest, complexStrategyForAfterAndWithInSingleTarget)
733 {
734     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
735     ConstructIterator(iterator);
736 
737     Window w1{12};
738     w1.windowLayer_ = 2;
739     w1.bounds_ = Rect{0, 100, 0, 120};
740     std::vector<Widget> visits;
741     std::vector<int> targets;
742     WidgetMatchModel sef{UiAttr::TYPE, "Text", ValueMatchPattern::EQ};
743     std::vector<WidgetMatchModel> withInAnchorMatchers;
744     std::vector<WidgetMatchModel> afterAnchorMatchers;
745     std::vector<WidgetMatchModel> myselfMatchers;
746     myselfMatchers.emplace_back(sef);
747     WidgetMatchModel withInAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
748     WidgetMatchModel afterAnchor{UiAttr::ACCESSIBILITY_ID, "5", ValueMatchPattern::EQ};
749     withInAnchorMatchers.emplace_back(withInAnchor);
750     afterAnchorMatchers.emplace_back(afterAnchor);
751 
752     StrategyBuildParam buildParam;
753     buildParam.myselfMatcher = myselfMatchers;
754     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
755     buildParam.withInAnchorMatcherVec.emplace_back(withInAnchorMatchers);
756     std::unique_ptr<SelectStrategy> plainStrategy3 = SelectStrategy::BuildSelectStrategy(buildParam, false);
757     DumpOption option;
758     plainStrategy3->LocateNode(w1, *iterator.get(), visits, targets, option);
759     ASSERT_EQ(targets.size(), 1);
760     ASSERT_EQ(visits.at(targets.at(0)).GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
761 }
762 
TEST(SelectStrategyTest,complexStrategyForAfterAndWithInMultiTarget)763 TEST(SelectStrategyTest, complexStrategyForAfterAndWithInMultiTarget)
764 {
765     std::unique_ptr<ElementNodeIterator> iterator = nullptr;
766     ConstructIterator(iterator);
767 
768     Window w1{12};
769     w1.windowLayer_ = 2;
770     w1.bounds_ = Rect{0, 100, 0, 120};
771     std::vector<Widget> visits;
772     std::vector<int> targets;
773     WidgetMatchModel sef{UiAttr::TYPE, "Text", ValueMatchPattern::EQ};
774     std::vector<WidgetMatchModel> withInAnchorMatchers;
775     std::vector<WidgetMatchModel> afterAnchorMatchers;
776     std::vector<WidgetMatchModel> myselfMatchers;
777     myselfMatchers.emplace_back(sef);
778     WidgetMatchModel withInAnchor{UiAttr::ACCESSIBILITY_ID, "7", ValueMatchPattern::EQ};
779     WidgetMatchModel afterAnchor{UiAttr::TEXT, "Text One", ValueMatchPattern::EQ};
780     withInAnchorMatchers.emplace_back(withInAnchor);
781     afterAnchorMatchers.emplace_back(afterAnchor);
782 
783     StrategyBuildParam buildParam;
784     buildParam.myselfMatcher = myselfMatchers;
785     buildParam.afterAnchorMatcherVec.emplace_back(afterAnchorMatchers);
786     buildParam.withInAnchorMatcherVec.emplace_back(withInAnchorMatchers);
787     std::unique_ptr<SelectStrategy> complexStrategy = SelectStrategy::BuildSelectStrategy(buildParam, true);
788     DumpOption option;
789     complexStrategy->LocateNode(w1, *iterator.get(), visits, targets, option);
790     ASSERT_EQ(targets.size(), 2);
791     ASSERT_EQ(visits.at(targets.at(0)).GetAttr(UiAttr::ACCESSIBILITY_ID), "5");
792     ASSERT_EQ(visits.at(targets.at(1)).GetAttr(UiAttr::ACCESSIBILITY_ID), "4");
793 }
794