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