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 #include <extern_api.h>
17 #include "gtest/gtest.h"
18 #include "widget_matcher.h"
19 #include "ui_model.h"
20
21 using namespace OHOS::uitest;
22 using namespace std;
23
24 static constexpr auto ATTR_TEXT = "text";
25
TEST(ValueMatcherTest,eqMatcher)26 TEST(ValueMatcherTest, eqMatcher)
27 {
28 auto matcher = ValueMatcher("wyz", EQ);
29 ASSERT_TRUE(matcher.Matches("wyz"));
30 ASSERT_FALSE(matcher.Matches("yz"));
31 ASSERT_FALSE(matcher.Matches("wy"));
32 ASSERT_FALSE(matcher.Matches(""));
33 ASSERT_FALSE(matcher.Matches("ab"));
34 }
35
TEST(ValueMatcherTest,containsMatcher)36 TEST(ValueMatcherTest, containsMatcher)
37 {
38 auto matcher = ValueMatcher("wyz", CONTAINS);
39 ASSERT_TRUE(matcher.Matches("wyz"));
40 ASSERT_TRUE(matcher.Matches("awyzb"));
41 ASSERT_TRUE(matcher.Matches("wyzab"));
42 ASSERT_FALSE(matcher.Matches(""));
43 ASSERT_FALSE(matcher.Matches("ab"));
44 }
45
TEST(ValueMatcherTest,startsWithMatcher)46 TEST(ValueMatcherTest, startsWithMatcher)
47 {
48 auto matcher = ValueMatcher("wyz", STARTS_WITH);
49 ASSERT_TRUE(matcher.Matches("wyz"));
50 ASSERT_FALSE(matcher.Matches("yz"));
51 ASSERT_TRUE(matcher.Matches("wyza"));
52 ASSERT_TRUE(matcher.Matches("wyzyzw"));
53 ASSERT_FALSE(matcher.Matches("ab"));
54 }
55
TEST(ValueMatcherTest,endsWithMatcher)56 TEST(ValueMatcherTest, endsWithMatcher)
57 {
58 auto matcher = ValueMatcher("wyz", ENDS_WITH);
59 ASSERT_TRUE(matcher.Matches("wyz"));
60 ASSERT_TRUE(matcher.Matches("wawyz"));
61 ASSERT_FALSE(matcher.Matches("wyza"));
62 ASSERT_FALSE(matcher.Matches("wyzz"));
63 }
64
TEST(ValueMatcherTest,matcherDesc)65 TEST(ValueMatcherTest, matcherDesc)
66 {
67 const auto testValue = "wyz";
68 const auto rules = {EQ, CONTAINS, STARTS_WITH, ENDS_WITH};
69 for (auto &rule:rules) {
70 auto matcher = ValueMatcher(testValue, rule);
71 auto desc = matcher.Describe();
72 // desc should contains the test_value
73 ASSERT_TRUE(desc.find(testValue) != string::npos);
74 // desc should contains the match rule
75 ASSERT_TRUE(desc.find(GetRuleName(rule)) != string::npos);
76 }
77 }
78
TEST(WidgetMatcherTest,widgetMatcher)79 TEST(WidgetMatcherTest, widgetMatcher)
80 {
81 Widget widget("");
82 widget.SetAttr(ATTR_TEXT, "wyz");
83
84 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
85 ASSERT_TRUE(matcherA.Matches(widget));
86 widget.SetAttr(ATTR_TEXT, "wlj");
87 ASSERT_FALSE(matcherA.Matches(widget));
88 }
89
TEST(WidgetMatcherTest,widgetMatcherDesc)90 TEST(WidgetMatcherTest, widgetMatcherDesc)
91 {
92 auto matcher = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
93 auto desc = matcher.Describe();
94 // desc should contains the target attribute name
95 ASSERT_TRUE(desc.find(ATTR_TEXT) != string::npos);
96 // desc should contains the match rule
97 ASSERT_TRUE(desc.find(GetRuleName(EQ)) != string::npos);
98 // desc should contains the test attribute value
99 ASSERT_TRUE(desc.find("wyz") != string::npos);
100 }
101
TEST(WidgetMatcherTest,rootMatcher)102 TEST(WidgetMatcherTest, rootMatcher)
103 {
104 Widget widget0("ROOT");
105 Widget widget1("123");
106 auto matcher = RootMatcher();
107
108 ASSERT_TRUE(matcher.Matches(widget0));
109 ASSERT_FALSE(matcher.Matches(widget1));
110 auto desc = matcher.Describe();
111 ASSERT_TRUE(desc.find("root") != string::npos);
112 }
113
TEST(WidgetMatcherTest,allWidgetAttrMatcher)114 TEST(WidgetMatcherTest, allWidgetAttrMatcher)
115 {
116 Widget widget("");
117 widget.SetAttr(ATTR_TEXT, "wyz");
118
119 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
120 auto matcherB = WidgetAttrMatcher(ATTR_TEXT, "yz", CONTAINS);
121 auto matcherC = WidgetAttrMatcher(ATTR_TEXT, "yz", STARTS_WITH);
122
123 auto allMatcher0 = All(matcherA, matcherB);
124 auto allMatcher1 = All(matcherA, matcherC);
125
126 ASSERT_TRUE(allMatcher0.Matches(widget));
127 ASSERT_FALSE(allMatcher1.Matches(widget));
128 }
129
TEST(WidgetMatcherTest,allWidgetMatcherDesc)130 TEST(WidgetMatcherTest, allWidgetMatcherDesc)
131 {
132 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
133 auto matcherB = WidgetAttrMatcher(ATTR_TEXT, "yz", CONTAINS);
134 auto allMatcher = All(matcherA, matcherB);
135
136 auto matcherADesc = matcherA.Describe();
137 auto matcherBDesc = matcherB.Describe();
138 auto allMatcherDesc = allMatcher.Describe();
139
140 // desc should contains the description of each leaf matchers
141 ASSERT_TRUE(allMatcherDesc.find(matcherADesc) != string::npos);
142 ASSERT_TRUE(allMatcherDesc.find(matcherBDesc) != string::npos);
143 // desc should contains the 'AND' operand
144 ASSERT_TRUE(allMatcherDesc.find("AND") != string::npos);
145 }
146
TEST(WidgetMatcherTest,anyWidgetAttrMatcher)147 TEST(WidgetMatcherTest, anyWidgetAttrMatcher)
148 {
149 Widget widget("");
150 widget.SetAttr(ATTR_TEXT, "wyz");
151
152 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
153 auto matcherB = WidgetAttrMatcher(ATTR_TEXT, "yz", CONTAINS);
154 auto matcherC = WidgetAttrMatcher(ATTR_TEXT, "yz", STARTS_WITH);
155 auto matcherD = WidgetAttrMatcher(ATTR_TEXT, "wy", ENDS_WITH);
156
157 auto anyMatcher0 = Any(matcherA, matcherB);
158 auto anyMatcher1 = Any(matcherA, matcherC);
159 auto anyMatcher2 = Any(matcherC, matcherD);
160 ASSERT_TRUE(anyMatcher0.Matches(widget));
161 ASSERT_TRUE(anyMatcher1.Matches(widget));
162 ASSERT_FALSE(anyMatcher2.Matches(widget));
163 }
164
165 // test the vector-parameter version
TEST(WidgetMatcherTest,anyWidgetAttrMatcher2)166 TEST(WidgetMatcherTest, anyWidgetAttrMatcher2)
167 {
168 Widget widget("");
169 widget.SetAttr(ATTR_TEXT, "wyz");
170
171 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
172 auto matcherB = WidgetAttrMatcher(ATTR_TEXT, "yz", CONTAINS);
173 auto matcherC = WidgetAttrMatcher(ATTR_TEXT, "yz", STARTS_WITH);
174 auto matcherD = WidgetAttrMatcher(ATTR_TEXT, "wy", ENDS_WITH);
175 vector<WidgetAttrMatcher> vec0;
176 vector<WidgetAttrMatcher> vec1;
177 vector<WidgetAttrMatcher> vec2;
178 vec0.emplace_back(matcherA);
179 vec0.emplace_back(matcherB);
180 vec1.emplace_back(matcherA);
181 vec1.emplace_back(matcherC);
182 vec2.emplace_back(matcherC);
183 vec2.emplace_back(matcherD);
184
185 auto anyMatcher0 = Any(vec0);
186 auto anyMatcher1 = Any(vec1);
187 auto anyMatcher2 = Any(vec2);
188 ASSERT_TRUE(anyMatcher0.Matches(widget));
189 ASSERT_TRUE(anyMatcher1.Matches(widget));
190 ASSERT_FALSE(anyMatcher2.Matches(widget));
191 }
192
TEST(WidgetMatcherTest,anyWidgetMatcherDesc)193 TEST(WidgetMatcherTest, anyWidgetMatcherDesc)
194 {
195 auto matcherA = WidgetAttrMatcher(ATTR_TEXT, "wyz", EQ);
196 auto matcherB = WidgetAttrMatcher(ATTR_TEXT, "yz", CONTAINS);
197 auto anyMatcher = Any(matcherA, matcherB);
198
199 auto matcherADesc = matcherA.Describe();
200 auto matcherBDesc = matcherB.Describe();
201 auto anyMatcherDesc = anyMatcher.Describe();
202
203 // desc should contains the description of each leaf matchers
204 ASSERT_TRUE(anyMatcherDesc.find(matcherADesc) != string::npos);
205 ASSERT_TRUE(anyMatcherDesc.find(matcherBDesc) != string::npos);
206 // desc should contains the 'AND' operand
207 ASSERT_TRUE(anyMatcherDesc.find("OR") != string::npos);
208 }