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 #include "gtest/gtest.h"
16 #include "ui_model.h"
17 #include <regex.h>
18
19 using namespace OHOS::uitest;
20 using namespace std;
21
22 static constexpr auto ATTR_TEXT = "text";
23 static constexpr auto ATTR_ID = "id";
24
TEST(REGEXPTestTrue,testRegex)25 TEST(REGEXPTestTrue, testRegex)
26 {
27 // make a widget object
28 Widget widget("hierarchy");
29 widget.SetAttr(UiAttr::TEXT, "checkBox1");
30 widget.SetAttr(UiAttr::ID, "btnTest");
31 widget.SetAttr(UiAttr::HINT, "btnTest12345");
32 widget.SetBounds(Rect(1, 2, 3, 4));
33 // use regex to match widget
34 auto matcherTxt = WidgetMatchModel(UiAttr::TEXT, "checkBox\\w", ValueMatchPattern::REG_EXP);
35 auto matcherId = WidgetMatchModel(UiAttr::ID, "btn\\w{2,4}", ValueMatchPattern::REG_EXP);
36 bool matchResultId = widget.MatchAttr(matcherId);
37 bool matchResultTxt = widget.MatchAttr(matcherTxt);
38 ASSERT_EQ(true, matchResultId);
39 ASSERT_EQ(true, matchResultTxt);
40
41 // use regex to match widget, use ValueMatchPattern::REG_EXP_ICASE
42 auto matcherTxtIcase = WidgetMatchModel(UiAttr::TEXT, "^Check\\w{2,5}$", ValueMatchPattern::REG_EXP_ICASE);
43 bool matchResultTxtIcase = widget.MatchAttr(matcherTxtIcase);
44 ASSERT_EQ(true, matchResultTxtIcase);
45 }
46
TEST(REGEXPTestFalse,testRegex)47 TEST(REGEXPTestFalse, testRegex)
48 {
49 // make a widget object
50 Widget widget("hierarchy");
51 widget.SetAttr(UiAttr::TEXT, "checkBox1");
52 widget.SetAttr(UiAttr::ID, "btnTest");
53 widget.SetAttr(UiAttr::HINT, "btnTest12345");
54 widget.SetBounds(Rect(1, 2, 3, 4));
55 // use regex to match widget
56 auto matcherTxt1 = WidgetMatchModel(UiAttr::TEXT, "^Check\\w{2,5}$", ValueMatchPattern::REG_EXP);
57 bool matchResultTxt1 = widget.MatchAttr(matcherTxt1);
58 ASSERT_EQ(false, matchResultTxt1);
59
60 auto matcherTxt2 = WidgetMatchModel(UiAttr::HINT, "^btn_Test\\w{2,3}$", ValueMatchPattern::REG_EXP_ICASE);
61 bool matchResultTxt2 = widget.MatchAttr(matcherTxt2);
62 ASSERT_EQ(false, matchResultTxt2);
63 }
64
65
TEST(RectTest,testRectBase)66 TEST(RectTest, testRectBase)
67 {
68 Rect rect(100, 200, 300, 400);
69 ASSERT_EQ(100, rect.left_);
70 ASSERT_EQ(200, rect.right_);
71 ASSERT_EQ(300, rect.top_);
72 ASSERT_EQ(400, rect.bottom_);
73
74 ASSERT_EQ(rect.GetCenterX(), (100 + 200) / 2);
75 ASSERT_EQ(rect.GetCenterY(), (300 + 400) / 2);
76 ASSERT_EQ(rect.GetHeight(), 400 - 300);
77 ASSERT_EQ(rect.GetWidth(), 200 - 100);
78 }
79
TEST(WidgetTest,testAttributes)80 TEST(WidgetTest, testAttributes)
81 {
82 Widget widget("hierarchy");
83 // get not-exist attribute, should return default value
84 ASSERT_EQ("none", widget.GetAttr(UiAttr::MAX));
85 // get exist attribute, should return actual value
86 widget.SetAttr(UiAttr::MAX, "wyz");
87 ASSERT_EQ("", widget.GetAttr(UiAttr::TEXT));
88 }
89
90 /** NOTE:: Widget need to be movable since we need to move a constructed widget to
91 * its hosting tree. We must ensure that the move-created widget be same as the
92 * moved one (No any attribute/filed should be lost during moving).
93 * */
TEST(WidgetTest,testSafeMovable)94 TEST(WidgetTest, testSafeMovable)
95 {
96 Widget widget("hierarchy");
97 widget.SetAttr(UiAttr::TEXT, "wyz");
98 widget.SetAttr(UiAttr::ID, "100");
99 widget.SetBounds(Rect(1, 2, 3, 4));
100
101 auto newWidget = move(widget);
102 ASSERT_EQ("hierarchy", newWidget.GetHierarchy());
103 ASSERT_EQ("wyz", newWidget.GetAttr(UiAttr::TEXT));
104 ASSERT_EQ("100", newWidget.GetAttr(UiAttr::ID));
105 auto bounds = newWidget.GetBounds();
106 ASSERT_EQ(1, bounds.left_);
107 ASSERT_EQ(2, bounds.right_);
108 ASSERT_EQ(3, bounds.top_);
109 ASSERT_EQ(4, bounds.bottom_);
110 }
111
TEST(WidgetTest,testToStr)112 TEST(WidgetTest, testToStr)
113 {
114 Widget widget("hierarchy");
115
116 // get exist attribute, should return default value
117 widget.SetAttr(UiAttr::TEXT, "wyz");
118 widget.SetAttr(UiAttr::ID, "100");
119
120 auto str0 = widget.ToStr();
121 ASSERT_TRUE(str0.find(ATTR_TEXT) != string::npos);
122 ASSERT_TRUE(str0.find("wyz") != string::npos);
123 ASSERT_TRUE(str0.find(ATTR_ID) != string::npos);
124 ASSERT_TRUE(str0.find("100") != string::npos);
125
126 // change attribute
127 widget.SetAttr(UiAttr::ID, "211");
128 str0 = widget.ToStr();
129 ASSERT_TRUE(str0.find(ATTR_TEXT) != string::npos);
130 ASSERT_TRUE(str0.find("wyz") != string::npos);
131 ASSERT_TRUE(str0.find(ATTR_ID) != string::npos);
132 ASSERT_TRUE(str0.find("211") != string::npos);
133 ASSERT_TRUE(str0.find("100") == string::npos);
134 }
135
TEST(WidgetTest,testClone)136 TEST(WidgetTest, testClone)
137 {
138 Widget widget("hierarchy");
139 // get exist attribute, should return default value
140 widget.SetAttr(UiAttr::TEXT, "Camera");
141 widget.SetAttr(UiAttr::ACCESSIBILITY_ID, "12");
142 auto ret = widget.Clone("TEST");
143 ASSERT_TRUE(ret->GetAttr(UiAttr::TEXT) == "Camera");
144 }