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
18 using namespace OHOS::uitest;
19 using namespace std;
20
21 static constexpr auto ATTR_TEXT = "text";
22 static constexpr auto ATTR_ID = "id";
23
TEST(RectTest,testRectBase)24 TEST(RectTest, testRectBase)
25 {
26 Rect rect(100, 200, 300, 400);
27 ASSERT_EQ(100, rect.left_);
28 ASSERT_EQ(200, rect.right_);
29 ASSERT_EQ(300, rect.top_);
30 ASSERT_EQ(400, rect.bottom_);
31
32 ASSERT_EQ(rect.GetCenterX(), (100 + 200) / 2);
33 ASSERT_EQ(rect.GetCenterY(), (300 + 400) / 2);
34 ASSERT_EQ(rect.GetHeight(), 400 - 300);
35 ASSERT_EQ(rect.GetWidth(), 200 - 100);
36 }
37
TEST(WidgetTest,testAttributes)38 TEST(WidgetTest, testAttributes)
39 {
40 Widget widget("hierarchy");
41 // get not-exist attribute, should return default value
42 ASSERT_EQ("none", widget.GetAttr(UiAttr::MAX));
43 // get exist attribute, should return actual value
44 widget.SetAttr(UiAttr::MAX, "wyz");
45 ASSERT_EQ("", widget.GetAttr(UiAttr::TEXT));
46 }
47
48 /** NOTE:: Widget need to be movable since we need to move a constructed widget to
49 * its hosting tree. We must ensure that the move-created widget be same as the
50 * moved one (No any attribute/filed should be lost during moving).
51 * */
TEST(WidgetTest,testSafeMovable)52 TEST(WidgetTest, testSafeMovable)
53 {
54 Widget widget("hierarchy");
55 widget.SetAttr(UiAttr::TEXT, "wyz");
56 widget.SetAttr(UiAttr::ID, "100");
57 widget.SetBounds(Rect(1, 2, 3, 4));
58
59 auto newWidget = move(widget);
60 ASSERT_EQ("hierarchy", newWidget.GetHierarchy());
61 ASSERT_EQ("wyz", newWidget.GetAttr(UiAttr::TEXT));
62 ASSERT_EQ("100", newWidget.GetAttr(UiAttr::ID));
63 auto bounds = newWidget.GetBounds();
64 ASSERT_EQ(1, bounds.left_);
65 ASSERT_EQ(2, bounds.right_);
66 ASSERT_EQ(3, bounds.top_);
67 ASSERT_EQ(4, bounds.bottom_);
68 }
69
TEST(WidgetTest,testToStr)70 TEST(WidgetTest, testToStr)
71 {
72 Widget widget("hierarchy");
73
74 // get exist attribute, should return default value
75 widget.SetAttr(UiAttr::TEXT, "wyz");
76 widget.SetAttr(UiAttr::ID, "100");
77
78 auto str0 = widget.ToStr();
79 ASSERT_TRUE(str0.find(ATTR_TEXT) != string::npos);
80 ASSERT_TRUE(str0.find("wyz") != string::npos);
81 ASSERT_TRUE(str0.find(ATTR_ID) != string::npos);
82 ASSERT_TRUE(str0.find("100") != string::npos);
83
84 // change attribute
85 widget.SetAttr(UiAttr::ID, "211");
86 str0 = widget.ToStr();
87 ASSERT_TRUE(str0.find(ATTR_TEXT) != string::npos);
88 ASSERT_TRUE(str0.find("wyz") != string::npos);
89 ASSERT_TRUE(str0.find(ATTR_ID) != string::npos);
90 ASSERT_TRUE(str0.find("211") != string::npos);
91 ASSERT_TRUE(str0.find("100") == string::npos);
92 }
93
TEST(WidgetTest,testClone)94 TEST(WidgetTest, testClone)
95 {
96 Widget widget("hierarchy");
97 // get exist attribute, should return default value
98 widget.SetAttr(UiAttr::TEXT, "Camera");
99 widget.SetAttr(UiAttr::ACCESSIBILITY_ID, "12");
100 auto ret = widget.Clone("TEST");
101 ASSERT_TRUE(ret->GetAttr(UiAttr::TEXT) == "Camera");
102 }