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