• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef WIDGET_MATCHER_H
17 #define WIDGET_MATCHER_H
18 
19 #include <string>
20 #include <vector>
21 #include "ui_model.h"
22 
23 namespace OHOS::uitest {
24     /**Enumerates the supported string value match rules.*/
25     enum ValueMatchPattern : uint8_t { EQ, CONTAINS, STARTS_WITH, ENDS_WITH };
26 
27     /** get the readable name of the ValueMatchPattern value.*/
28     std::string GetRuleName(ValueMatchPattern rule);
29 
30     class ValueMatcher {
31     public:
32         explicit ValueMatcher(std::string testValue, ValueMatchPattern rule = EQ)
testValue_(std::move (testValue))33             : testValue_(std::move(testValue)), rule_(rule) {}
34 
~ValueMatcher()35         virtual ~ValueMatcher() {}
36 
37         bool Matches(std::string_view testedValue) const;
38 
39         std::string Describe() const;
40 
41     private:
42         const std::string testValue_;
43         const ValueMatchPattern rule_;
44     };
45 
46     /**Base type of all widget matchers, test on a single Widget and returns true if it's
47      * matched, false otherwise. */
48     class WidgetMatcher {
49     public:
50         virtual bool Matches(const Widget &widget) const = 0;
51 
52         virtual std::string Describe() const = 0;
53     };
54 
55     /**match the root widget node.*/
56     class RootMatcher : public WidgetMatcher {
57     public:
58         bool Matches(const Widget &widget) const override;
59 
60         std::string Describe() const override;
61     };
62 
63     /**
64      * Matcher to test a single widget attribute.
65      * */
66     class WidgetAttrMatcher final : public WidgetMatcher {
67     public:
68         WidgetAttrMatcher() = delete;
69 
70         explicit WidgetAttrMatcher(std::string_view attr, std::string_view testValue, ValueMatchPattern rule);
71 
72         bool Matches(const Widget &widget) const override;
73 
74         std::string Describe() const override;
75 
76     private:
77         std::string attrName_;
78         std::string testVal_;
79         ValueMatchPattern matchRule_;
80     };
81 
82     /**
83      * Matcher that evaluates several widget-attr-matchers and combine the results with operational rule <b>AND</b>.
84      **/
85     class All final : public WidgetMatcher {
86     public:
87         /**
88          * Create a <code>All</code> widget matcher.
89          * @param ma the plain widget matcher a.
90          * @param mb the plain widget matcher b.
91          * */
92         All(const WidgetAttrMatcher &ma, const WidgetAttrMatcher &mb);
93 
94         /**
95          * Create a <code>All</code> widget matcher.
96          * @param matchers the leaf matcher list.
97          * */
98         explicit All(const std::vector<WidgetAttrMatcher> &matchers);
99 
100         bool Matches(const Widget &testedValue) const override;
101 
102         std::string Describe() const override;
103 
104     private:
105         // hold the leaf matchers
106         std::vector<WidgetAttrMatcher> matchers_;
107     };
108 
109     /**
110      * Matcher that evaluates several widget-attr-matchers and combine the results with operational rule <b>OR</b>.
111      **/
112     class Any final : public WidgetMatcher {
113     public:
114         /**
115          * Create a <code>Any</code> widget matcher.
116          * @param ma the plain widget matcher a.
117          * @param mb the plain widget matcher b.
118          * */
119         Any(const WidgetAttrMatcher &ma, const WidgetAttrMatcher &mb);
120 
121         /**
122          * Create a <code>Any</code> widget matcher.
123          * @param matchers the leaf matcher list.
124          * */
125         explicit Any(const std::vector<WidgetAttrMatcher> &matchers);
126 
127         bool Matches(const Widget &testedValue) const override;
128 
129         std::string Describe() const override;
130 
131     private:
132         // hold the leaf matchers
133         std::vector<WidgetAttrMatcher> matchers_;
134     };
135 
136     /**
137     * Common helper visitor to find and collect matched Widget nodes by WidgetMatcher. Matcher widgets are
138      * arranged in the given receiver container in visiting order.
139     * */
140     class MatchedWidgetCollector : public WidgetVisitor {
141     public:
MatchedWidgetCollector(const WidgetMatcher & matcher,std::vector<std::reference_wrapper<const Widget>> & recv)142         MatchedWidgetCollector(const WidgetMatcher &matcher, std::vector<std::reference_wrapper<const Widget>> &recv)
143             : matcher_(matcher), receiver_(recv) {}
144 
~MatchedWidgetCollector()145         ~MatchedWidgetCollector() {}
146 
147         void Visit(const Widget &widget) override;
148 
149     private:
150         const WidgetMatcher &matcher_;
151         std::vector<std::reference_wrapper<const Widget>> &receiver_;
152     };
153 } // namespace uitest
154 
155 #endif